2 * TI Bandgap temperature sensor driver
4 * Copyright (C) 2011-2012 Texas Instruments Incorporated - http://www.ti.com/
5 * Author: J Keerthy <j-keerthy@ti.com>
6 * Author: Moiz Sonasath <m-sonasath@ti.com>
7 * Couple of fixes, DT and MFD adaptation:
8 * Eduardo Valentin <eduardo.valentin@ti.com>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 #include <linux/module.h>
27 #include <linux/export.h>
28 #include <linux/init.h>
29 #include <linux/kernel.h>
30 #include <linux/interrupt.h>
31 #include <linux/clk.h>
32 #include <linux/gpio.h>
33 #include <linux/platform_device.h>
34 #include <linux/err.h>
35 #include <linux/types.h>
36 #include <linux/spinlock.h>
37 #include <linux/reboot.h>
38 #include <linux/of_device.h>
39 #include <linux/of_platform.h>
40 #include <linux/of_irq.h>
41 #include <linux/of_gpio.h>
44 #include "ti-bandgap.h"
46 static int ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id);
48 /*** Helper functions to access registers and their bitfields ***/
51 * ti_bandgap_readl() - simple read helper function
52 * @bgp: pointer to ti_bandgap structure
53 * @reg: desired register (offset) to be read
55 * Helper function to read bandgap registers. It uses the io remapped area.
56 * Return: the register value.
58 static u32 ti_bandgap_readl(struct ti_bandgap *bgp, u32 reg)
60 return readl(bgp->base + reg);
64 * ti_bandgap_writel() - simple write helper function
65 * @bgp: pointer to ti_bandgap structure
66 * @val: desired register value to be written
67 * @reg: desired register (offset) to be written
69 * Helper function to write bandgap registers. It uses the io remapped area.
71 static void ti_bandgap_writel(struct ti_bandgap *bgp, u32 val, u32 reg)
73 writel(val, bgp->base + reg);
77 * DOC: macro to update bits.
79 * RMW_BITS() - used to read, modify and update bandgap bitfields.
80 * The value passed will be shifted.
82 #define RMW_BITS(bgp, id, reg, mask, val) \
84 struct temp_sensor_registers *t; \
87 t = bgp->conf->sensors[(id)].registers; \
88 r = ti_bandgap_readl(bgp, t->reg); \
90 r |= (val) << __ffs(t->mask); \
91 ti_bandgap_writel(bgp, r, t->reg); \
94 /*** Basic helper functions ***/
97 * ti_bandgap_power() - controls the power state of a bandgap device
98 * @bgp: pointer to ti_bandgap structure
99 * @on: desired power state (1 - on, 0 - off)
101 * Used to power on/off a bandgap device instance. Only used on those
102 * that features tempsoff bit.
104 * Return: 0 on success, -ENOTSUPP if tempsoff is not supported.
106 static int ti_bandgap_power(struct ti_bandgap *bgp, bool on)
110 if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH))
113 for (i = 0; i < bgp->conf->sensor_count; i++)
115 RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
120 * ti_errata814_bandgap_read_temp() - helper function to read dra7 sensor temperature
121 * @bgp: pointer to ti_bandgap structure
122 * @reg: desired register (offset) to be read
124 * Function to read dra7 bandgap sensor temperature. This is done separately
125 * so as to workaround the errata "Bandgap Temperature read Dtemp can be
126 * corrupted" - Errata ID: i814".
127 * Read accesses to registers listed below can be corrupted due to incorrect
128 * resynchronization between clock domains.
129 * Read access to registers below can be corrupted :
130 * CTRL_CORE_DTEMP_MPU/GPU/CORE/DSPEVE/IVA_n (n = 0 to 4)
131 * CTRL_CORE_TEMP_SENSOR_MPU/GPU/CORE/DSPEVE/IVA_n
133 * Return: the register value.
135 static u32 ti_errata814_bandgap_read_temp(struct ti_bandgap *bgp, u32 reg)
139 val1 = ti_bandgap_readl(bgp, reg);
140 val2 = ti_bandgap_readl(bgp, reg);
142 /* If both times we read the same value then that is right */
146 /* if val1 and val2 are different read it third time */
147 return ti_bandgap_readl(bgp, reg);
151 * ti_bandgap_read_temp() - helper function to read sensor temperature
152 * @bgp: pointer to ti_bandgap structure
153 * @id: bandgap sensor id
155 * Function to concentrate the steps to read sensor temperature register.
156 * This function is desired because, depending on bandgap device version,
157 * it might be needed to freeze the bandgap state machine, before fetching
158 * the register value.
160 * Return: temperature in ADC values.
162 static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
164 struct temp_sensor_registers *tsr;
167 tsr = bgp->conf->sensors[id].registers;
168 reg = tsr->temp_sensor_ctrl;
170 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
171 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
173 * In case we cannot read from cur_dtemp / dtemp_0,
174 * then we read from the last valid temp read
176 reg = tsr->ctrl_dtemp_1;
179 /* read temperature */
180 if (TI_BANDGAP_HAS(bgp, ERRATA_814))
181 temp = ti_errata814_bandgap_read_temp(bgp, reg);
183 temp = ti_bandgap_readl(bgp, reg);
185 temp &= tsr->bgap_dtemp_mask;
187 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
188 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
193 /*** IRQ handlers ***/
196 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
198 * @data: private data (struct ti_bandgap *)
200 * This is the Talert handler. Use it only if bandgap device features
201 * HAS(TALERT). This handler goes over all sensors and checks their
202 * conditions and acts accordingly. In case there are events pending,
203 * it will reset the event mask to wait for the opposite event (next event).
204 * Every time there is a new event, it will be reported to thermal layer.
206 * Return: IRQ_HANDLED
208 static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
210 struct ti_bandgap *bgp = data;
211 struct temp_sensor_registers *tsr;
212 u32 t_hot = 0, t_cold = 0, ctrl;
215 spin_lock(&bgp->lock);
216 for (i = 0; i < bgp->conf->sensor_count; i++) {
217 tsr = bgp->conf->sensors[i].registers;
218 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
220 /* Read the status of t_hot */
221 t_hot = ctrl & tsr->status_hot_mask;
223 /* Read the status of t_cold */
224 t_cold = ctrl & tsr->status_cold_mask;
226 if (!t_cold && !t_hot)
229 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
231 * One TALERT interrupt: Two sources
232 * If the interrupt is due to t_hot then mask t_hot and
233 * and unmask t_cold else mask t_cold and unmask t_hot
236 ctrl &= ~tsr->mask_hot_mask;
237 ctrl |= tsr->mask_cold_mask;
239 ctrl &= ~tsr->mask_cold_mask;
240 ctrl |= tsr->mask_hot_mask;
243 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
246 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
247 __func__, bgp->conf->sensors[i].domain,
250 /* report temperature to whom may concern */
251 if (bgp->conf->report_temperature)
252 bgp->conf->report_temperature(bgp, i);
254 spin_unlock(&bgp->lock);
260 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
262 * @data: private data (unused)
264 * This is the Tshut handler. Use it only if bandgap device features
265 * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
268 * Return: IRQ_HANDLED
270 static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
272 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
275 orderly_poweroff(true);
280 /*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
283 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
284 * @bgp: struct ti_bandgap pointer
285 * @adc_val: value in ADC representation
286 * @t: address where to write the resulting temperature in mCelsius
288 * Simple conversion from ADC representation to mCelsius. In case the ADC value
289 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
290 * The conversion table is indexed by the ADC values.
292 * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val
293 * argument is out of the ADC conv table range.
296 int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
298 const struct ti_bandgap_data *conf = bgp->conf;
300 /* look up for temperature in the table and return the temperature */
301 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val)
304 *t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
309 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
310 * @bgp: struct ti_bandgap pointer
311 * @id: bandgap sensor id
313 * Checks if the bandgap pointer is valid and if the sensor id is also
316 * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
317 * @id cannot index @bgp sensors.
319 static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
321 if (!bgp || IS_ERR(bgp)) {
322 pr_err("%s: invalid bandgap pointer\n", __func__);
326 if ((id < 0) || (id >= bgp->conf->sensor_count)) {
327 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
336 * ti_bandgap_read_counter() - read the sensor counter
337 * @bgp: pointer to bandgap instance
339 * @interval: resulting update interval in miliseconds
341 static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id,
344 struct temp_sensor_registers *tsr;
347 tsr = bgp->conf->sensors[id].registers;
348 time = ti_bandgap_readl(bgp, tsr->bgap_counter);
349 time = (time & tsr->counter_mask) >>
350 __ffs(tsr->counter_mask);
351 time = time * 1000 / bgp->clk_rate;
356 * ti_bandgap_read_counter_delay() - read the sensor counter delay
357 * @bgp: pointer to bandgap instance
359 * @interval: resulting update interval in miliseconds
361 static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id,
364 struct temp_sensor_registers *tsr;
367 tsr = bgp->conf->sensors[id].registers;
369 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
370 reg_val = (reg_val & tsr->mask_counter_delay_mask) >>
371 __ffs(tsr->mask_counter_delay_mask);
392 dev_warn(bgp->dev, "Wrong counter delay value read from register %X",
398 * ti_bandgap_read_update_interval() - read the sensor update interval
399 * @bgp: pointer to bandgap instance
401 * @interval: resulting update interval in miliseconds
403 * Return: 0 on success or the proper error code
405 int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
410 ret = ti_bandgap_validate(bgp, id);
414 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
415 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
420 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
421 ti_bandgap_read_counter(bgp, id, interval);
425 ti_bandgap_read_counter_delay(bgp, id, interval);
431 * ti_bandgap_write_counter_delay() - set the counter_delay
432 * @bgp: pointer to bandgap instance
434 * @interval: desired update interval in miliseconds
436 * Return: 0 on success or the proper error code
438 static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id,
444 case 0: /* Immediate conversion */
447 case 1: /* Conversion after ever 1ms */
450 case 10: /* Conversion after ever 10ms */
453 case 100: /* Conversion after ever 100ms */
456 case 250: /* Conversion after ever 250ms */
459 case 500: /* Conversion after ever 500ms */
463 dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval);
467 spin_lock(&bgp->lock);
468 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval);
469 spin_unlock(&bgp->lock);
475 * ti_bandgap_write_counter() - set the bandgap sensor counter
476 * @bgp: pointer to bandgap instance
478 * @interval: desired update interval in miliseconds
480 static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id,
483 interval = interval * bgp->clk_rate / 1000;
484 spin_lock(&bgp->lock);
485 RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
486 spin_unlock(&bgp->lock);
490 * ti_bandgap_write_update_interval() - set the update interval
491 * @bgp: pointer to bandgap instance
493 * @interval: desired update interval in miliseconds
495 * Return: 0 on success or the proper error code
497 int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
498 int id, u32 interval)
500 int ret = ti_bandgap_validate(bgp, id);
504 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
505 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
510 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
511 ti_bandgap_write_counter(bgp, id, interval);
515 ret = ti_bandgap_write_counter_delay(bgp, id, interval);
521 * ti_bandgap_read_temperature() - report current temperature
522 * @bgp: pointer to bandgap instance
524 * @temperature: resulting temperature
526 * Return: 0 on success or the proper error code
528 int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
534 ret = ti_bandgap_validate(bgp, id);
538 if (!TI_BANDGAP_HAS(bgp, MODE_CONFIG)) {
539 ret = ti_bandgap_force_single_read(bgp, id);
544 spin_lock(&bgp->lock);
545 temp = ti_bandgap_read_temp(bgp, id);
546 spin_unlock(&bgp->lock);
548 ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
558 * ti_bandgap_set_sensor_data() - helper function to store thermal
559 * framework related data.
560 * @bgp: pointer to bandgap instance
562 * @data: thermal framework related data to be stored
564 * Return: 0 on success or the proper error code
566 int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
568 int ret = ti_bandgap_validate(bgp, id);
572 bgp->regval[id].data = data;
578 * ti_bandgap_get_sensor_data() - helper function to get thermal
579 * framework related data.
580 * @bgp: pointer to bandgap instance
583 * Return: data stored by set function with sensor id on success or NULL
585 void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
587 int ret = ti_bandgap_validate(bgp, id);
591 return bgp->regval[id].data;
594 /*** Helper functions used during device initialization ***/
597 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
598 * @bgp: pointer to struct ti_bandgap
599 * @id: sensor id which it is desired to read 1 temperature
601 * Used to initialize the conversion state machine and set it to a valid
602 * state. Called during device initialization and context restore events.
607 ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
610 struct temp_sensor_registers *tsr;
612 /* Select single conversion mode */
613 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
614 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
616 /* Start of Conversion = 1 */
617 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
619 /* Wait for EOCZ going up */
620 tsr = bgp->conf->sensors[id].registers;
623 if (ti_bandgap_readl(bgp, tsr->temp_sensor_ctrl) &
628 /* Start of Conversion = 0 */
629 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
631 /* Wait for EOCZ going down */
634 if (!(ti_bandgap_readl(bgp, tsr->temp_sensor_ctrl) &
635 tsr->bgap_eocz_mask))
643 * ti_bandgap_set_continuous_mode() - One time enabling of continuous mode
644 * @bgp: pointer to struct ti_bandgap
646 * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
647 * be used for junction temperature monitoring, it is desirable that the
648 * sensors are operational all the time, so that alerts are generated
653 static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
657 for (i = 0; i < bgp->conf->sensor_count; i++) {
658 /* Perform a single read just before enabling continuous */
659 ti_bandgap_force_single_read(bgp, i);
660 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
667 * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
668 * @bgp: pointer to struct ti_bandgap
669 * @id: id of the individual sensor
670 * @trend: Pointer to trend.
672 * This function needs to be called to fetch the temperature trend of a
673 * Particular sensor. The function computes the difference in temperature
674 * w.r.t time. For the bandgaps with built in history buffer the temperatures
675 * are read from the buffer and for those without the Buffer -ENOTSUPP is
678 * Return: 0 if no error, else return corresponding error. If no
679 * error then the trend value is passed on to trend parameter
681 int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend)
683 struct temp_sensor_registers *tsr;
684 u32 temp1, temp2, reg1, reg2;
685 int t1, t2, interval, ret = 0;
687 ret = ti_bandgap_validate(bgp, id);
691 if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) ||
692 !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
697 spin_lock(&bgp->lock);
699 tsr = bgp->conf->sensors[id].registers;
701 /* Freeze and read the last 2 valid readings */
702 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
703 reg1 = tsr->ctrl_dtemp_1;
704 reg2 = tsr->ctrl_dtemp_2;
706 /* read temperature from history buffer */
707 temp1 = ti_bandgap_readl(bgp, reg1);
708 temp1 &= tsr->bgap_dtemp_mask;
710 temp2 = ti_bandgap_readl(bgp, reg2);
711 temp2 &= tsr->bgap_dtemp_mask;
713 /* Convert from adc values to mCelsius temperature */
714 ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1);
718 ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2);
722 /* Fetch the update interval */
723 ret = ti_bandgap_read_update_interval(bgp, id, &interval);
727 /* Set the interval to 1 ms if bandgap counter delay is not set */
731 *trend = (t1 - t2) / interval;
733 dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
737 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
738 spin_unlock(&bgp->lock);
744 * ti_bandgap_tshut_init() - setup and initialize tshut handling
745 * @bgp: pointer to struct ti_bandgap
746 * @pdev: pointer to device struct platform_device
748 * Call this function only in case the bandgap features HAS(TSHUT).
749 * In this case, the driver needs to handle the TSHUT signal as an IRQ.
750 * The IRQ is wired as a GPIO, and for this purpose, it is required
751 * to specify which GPIO line is used. TSHUT IRQ is fired anytime
752 * one of the bandgap sensors violates the TSHUT high/hot threshold.
753 * And in that case, the system must go off.
755 * Return: 0 if no error, else error status
757 static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
758 struct platform_device *pdev)
760 int gpio_nr = bgp->tshut_gpio;
763 /* Request for gpio_86 line */
764 status = gpio_request(gpio_nr, "tshut");
766 dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86);
769 status = gpio_direction_input(gpio_nr);
771 dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr);
775 status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler,
776 IRQF_TRIGGER_RISING, "tshut", NULL);
779 dev_err(bgp->dev, "request irq failed for TSHUT");
786 * ti_bandgap_alert_init() - setup and initialize talert handling
787 * @bgp: pointer to struct ti_bandgap
788 * @pdev: pointer to device struct platform_device
790 * Call this function only in case the bandgap features HAS(TALERT).
791 * In this case, the driver needs to handle the TALERT signals as an IRQs.
792 * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
793 * are violated. In these situation, the driver must reprogram the thresholds,
794 * accordingly to specified policy.
796 * Return: 0 if no error, else return corresponding error.
798 static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
799 struct platform_device *pdev)
803 bgp->irq = platform_get_irq(pdev, 0);
805 dev_err(&pdev->dev, "get_irq failed\n");
808 ret = request_threaded_irq(bgp->irq, NULL,
809 ti_bandgap_talert_irq_handler,
810 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
813 dev_err(&pdev->dev, "Request threaded irq failed.\n");
820 static const struct of_device_id of_ti_bandgap_match[];
822 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
823 * @pdev: pointer to device struct platform_device
825 * Used to read the device tree properties accordingly to the bandgap
826 * matching version. Based on bandgap version and its capabilities it
827 * will build a struct ti_bandgap out of the required DT entries.
829 * Return: valid bandgap structure if successful, else returns ERR_PTR
830 * return value must be verified with IS_ERR.
832 static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
834 struct device_node *node = pdev->dev.of_node;
835 const struct of_device_id *of_id;
836 struct ti_bandgap *bgp;
837 struct resource *res;
840 /* just for the sake */
842 dev_err(&pdev->dev, "no platform information available\n");
843 return ERR_PTR(-EINVAL);
846 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
848 return ERR_PTR(-ENOMEM);
850 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
852 bgp->conf = of_id->data;
854 /* register shadow for context save and restore */
855 bgp->regval = devm_kcalloc(&pdev->dev, bgp->conf->sensor_count,
856 sizeof(*bgp->regval), GFP_KERNEL);
858 return ERR_PTR(-ENOMEM);
864 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
867 chunk = devm_ioremap_resource(&pdev->dev, res);
871 return ERR_CAST(chunk);
876 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
877 bgp->tshut_gpio = of_get_gpio(node, 0);
878 if (!gpio_is_valid(bgp->tshut_gpio)) {
879 dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
881 return ERR_PTR(-EINVAL);
888 /*** Device driver call backs ***/
891 int ti_bandgap_probe(struct platform_device *pdev)
893 struct ti_bandgap *bgp;
894 int clk_rate, ret, i;
896 bgp = ti_bandgap_build(pdev);
898 dev_err(&pdev->dev, "failed to fetch platform data\n");
901 bgp->dev = &pdev->dev;
903 if (TI_BANDGAP_HAS(bgp, UNRELIABLE))
905 "This OMAP thermal sensor is unreliable. You've been warned\n");
907 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
908 ret = ti_bandgap_tshut_init(bgp, pdev);
911 "failed to initialize system tshut IRQ\n");
916 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
917 if (IS_ERR(bgp->fclock)) {
918 dev_err(&pdev->dev, "failed to request fclock reference\n");
919 ret = PTR_ERR(bgp->fclock);
923 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
924 if (IS_ERR(bgp->div_clk)) {
925 dev_err(&pdev->dev, "failed to request div_ts_ck clock ref\n");
926 ret = PTR_ERR(bgp->div_clk);
930 for (i = 0; i < bgp->conf->sensor_count; i++) {
931 struct temp_sensor_registers *tsr;
934 tsr = bgp->conf->sensors[i].registers;
936 * check if the efuse has a non-zero value if not
937 * it is an untrimmed sample and the temperatures
938 * may not be accurate
940 val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
943 "Non-trimmed BGAP, Temp not accurate\n");
946 clk_rate = clk_round_rate(bgp->div_clk,
947 bgp->conf->sensors[0].ts_data->max_freq);
948 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
951 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
955 ret = clk_set_rate(bgp->div_clk, clk_rate);
957 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
959 bgp->clk_rate = clk_rate;
960 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
961 clk_prepare_enable(bgp->fclock);
964 spin_lock_init(&bgp->lock);
965 bgp->dev = &pdev->dev;
966 platform_set_drvdata(pdev, bgp);
968 ti_bandgap_power(bgp, true);
970 /* Set default counter to 1 for now */
971 if (TI_BANDGAP_HAS(bgp, COUNTER))
972 for (i = 0; i < bgp->conf->sensor_count; i++)
973 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
975 /* Set default thresholds for alert and shutdown */
976 for (i = 0; i < bgp->conf->sensor_count; i++) {
977 struct temp_sensor_data *ts_data;
979 ts_data = bgp->conf->sensors[i].ts_data;
981 if (TI_BANDGAP_HAS(bgp, TALERT)) {
982 /* Set initial Talert thresholds */
983 RMW_BITS(bgp, i, bgap_threshold,
984 threshold_tcold_mask, ts_data->t_cold);
985 RMW_BITS(bgp, i, bgap_threshold,
986 threshold_thot_mask, ts_data->t_hot);
987 /* Enable the alert events */
988 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
989 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
992 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
993 /* Set initial Tshut thresholds */
994 RMW_BITS(bgp, i, tshut_threshold,
995 tshut_hot_mask, ts_data->tshut_hot);
996 RMW_BITS(bgp, i, tshut_threshold,
997 tshut_cold_mask, ts_data->tshut_cold);
1001 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1002 ti_bandgap_set_continuous_mode(bgp);
1004 /* Set .250 seconds time as default counter */
1005 if (TI_BANDGAP_HAS(bgp, COUNTER))
1006 for (i = 0; i < bgp->conf->sensor_count; i++)
1007 RMW_BITS(bgp, i, bgap_counter, counter_mask,
1010 /* Every thing is good? Then expose the sensors */
1011 for (i = 0; i < bgp->conf->sensor_count; i++) {
1014 if (bgp->conf->sensors[i].register_cooling) {
1015 ret = bgp->conf->sensors[i].register_cooling(bgp, i);
1017 goto remove_sensors;
1020 if (bgp->conf->expose_sensor) {
1021 domain = bgp->conf->sensors[i].domain;
1022 ret = bgp->conf->expose_sensor(bgp, i, domain);
1024 goto remove_last_cooling;
1029 * Enable the Interrupts once everything is set. Otherwise irq handler
1030 * might be called as soon as it is enabled where as rest of framework
1031 * is still getting initialised.
1033 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1034 ret = ti_bandgap_talert_init(bgp, pdev);
1036 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
1037 i = bgp->conf->sensor_count;
1044 remove_last_cooling:
1045 if (bgp->conf->sensors[i].unregister_cooling)
1046 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1048 for (i--; i >= 0; i--) {
1049 if (bgp->conf->sensors[i].unregister_cooling)
1050 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1051 if (bgp->conf->remove_sensor)
1052 bgp->conf->remove_sensor(bgp, i);
1054 ti_bandgap_power(bgp, false);
1056 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1057 clk_disable_unprepare(bgp->fclock);
1059 clk_put(bgp->div_clk);
1061 clk_put(bgp->fclock);
1063 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1064 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1065 gpio_free(bgp->tshut_gpio);
1072 int ti_bandgap_remove(struct platform_device *pdev)
1074 struct ti_bandgap *bgp = platform_get_drvdata(pdev);
1077 /* First thing is to remove sensor interfaces */
1078 for (i = 0; i < bgp->conf->sensor_count; i++) {
1079 if (bgp->conf->sensors[i].unregister_cooling)
1080 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1082 if (bgp->conf->remove_sensor)
1083 bgp->conf->remove_sensor(bgp, i);
1086 ti_bandgap_power(bgp, false);
1088 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1089 clk_disable_unprepare(bgp->fclock);
1090 clk_put(bgp->fclock);
1091 clk_put(bgp->div_clk);
1093 if (TI_BANDGAP_HAS(bgp, TALERT))
1094 free_irq(bgp->irq, bgp);
1096 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1097 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1098 gpio_free(bgp->tshut_gpio);
1104 #ifdef CONFIG_PM_SLEEP
1105 static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
1109 for (i = 0; i < bgp->conf->sensor_count; i++) {
1110 struct temp_sensor_registers *tsr;
1111 struct temp_sensor_regval *rval;
1113 rval = &bgp->regval[i];
1114 tsr = bgp->conf->sensors[i].registers;
1116 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1117 rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
1118 tsr->bgap_mode_ctrl);
1119 if (TI_BANDGAP_HAS(bgp, COUNTER))
1120 rval->bg_counter = ti_bandgap_readl(bgp,
1122 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1123 rval->bg_threshold = ti_bandgap_readl(bgp,
1124 tsr->bgap_threshold);
1125 rval->bg_ctrl = ti_bandgap_readl(bgp,
1126 tsr->bgap_mask_ctrl);
1129 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1130 rval->tshut_threshold = ti_bandgap_readl(bgp,
1131 tsr->tshut_threshold);
1137 static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
1141 for (i = 0; i < bgp->conf->sensor_count; i++) {
1142 struct temp_sensor_registers *tsr;
1143 struct temp_sensor_regval *rval;
1146 rval = &bgp->regval[i];
1147 tsr = bgp->conf->sensors[i].registers;
1149 if (TI_BANDGAP_HAS(bgp, COUNTER))
1150 val = ti_bandgap_readl(bgp, tsr->bgap_counter);
1152 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1153 ti_bandgap_writel(bgp, rval->tshut_threshold,
1154 tsr->tshut_threshold);
1155 /* Force immediate temperature measurement and update
1156 * of the DTEMP field
1158 ti_bandgap_force_single_read(bgp, i);
1160 if (TI_BANDGAP_HAS(bgp, COUNTER))
1161 ti_bandgap_writel(bgp, rval->bg_counter,
1163 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1164 ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1165 tsr->bgap_mode_ctrl);
1166 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1167 ti_bandgap_writel(bgp, rval->bg_threshold,
1168 tsr->bgap_threshold);
1169 ti_bandgap_writel(bgp, rval->bg_ctrl,
1170 tsr->bgap_mask_ctrl);
1177 static int ti_bandgap_suspend(struct device *dev)
1179 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1182 err = ti_bandgap_save_ctxt(bgp);
1183 ti_bandgap_power(bgp, false);
1185 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1186 clk_disable_unprepare(bgp->fclock);
1191 static int ti_bandgap_resume(struct device *dev)
1193 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1195 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1196 clk_prepare_enable(bgp->fclock);
1198 ti_bandgap_power(bgp, true);
1200 return ti_bandgap_restore_ctxt(bgp);
1202 static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend,
1205 #define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
1207 #define DEV_PM_OPS NULL
1210 static const struct of_device_id of_ti_bandgap_match[] = {
1211 #ifdef CONFIG_OMAP3_THERMAL
1213 .compatible = "ti,omap34xx-bandgap",
1214 .data = (void *)&omap34xx_data,
1217 .compatible = "ti,omap36xx-bandgap",
1218 .data = (void *)&omap36xx_data,
1221 #ifdef CONFIG_OMAP4_THERMAL
1223 .compatible = "ti,omap4430-bandgap",
1224 .data = (void *)&omap4430_data,
1227 .compatible = "ti,omap4460-bandgap",
1228 .data = (void *)&omap4460_data,
1231 .compatible = "ti,omap4470-bandgap",
1232 .data = (void *)&omap4470_data,
1235 #ifdef CONFIG_OMAP5_THERMAL
1237 .compatible = "ti,omap5430-bandgap",
1238 .data = (void *)&omap5430_data,
1241 #ifdef CONFIG_DRA752_THERMAL
1243 .compatible = "ti,dra752-bandgap",
1244 .data = (void *)&dra752_data,
1250 MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
1252 static struct platform_driver ti_bandgap_sensor_driver = {
1253 .probe = ti_bandgap_probe,
1254 .remove = ti_bandgap_remove,
1256 .name = "ti-soc-thermal",
1258 .of_match_table = of_ti_bandgap_match,
1262 module_platform_driver(ti_bandgap_sensor_driver);
1264 MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1265 MODULE_LICENSE("GPL v2");
1266 MODULE_ALIAS("platform:ti-soc-thermal");
1267 MODULE_AUTHOR("Texas Instrument Inc.");