1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI Bandgap temperature sensor driver
5 * Copyright (C) 2011-2012 Texas Instruments Incorporated - http://www.ti.com/
6 * Author: J Keerthy <j-keerthy@ti.com>
7 * Author: Moiz Sonasath <m-sonasath@ti.com>
8 * Couple of fixes, DT and MFD adaptation:
9 * Eduardo Valentin <eduardo.valentin@ti.com>
12 #include <linux/module.h>
13 #include <linux/export.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/interrupt.h>
17 #include <linux/clk.h>
18 #include <linux/gpio.h>
19 #include <linux/platform_device.h>
20 #include <linux/err.h>
21 #include <linux/types.h>
22 #include <linux/spinlock.h>
23 #include <linux/reboot.h>
24 #include <linux/of_device.h>
25 #include <linux/of_platform.h>
26 #include <linux/of_irq.h>
27 #include <linux/of_gpio.h>
30 #include "ti-bandgap.h"
32 static int ti_bandgap_force_single_read(struct ti_bandgap
*bgp
, int id
);
34 /*** Helper functions to access registers and their bitfields ***/
37 * ti_bandgap_readl() - simple read helper function
38 * @bgp: pointer to ti_bandgap structure
39 * @reg: desired register (offset) to be read
41 * Helper function to read bandgap registers. It uses the io remapped area.
42 * Return: the register value.
44 static u32
ti_bandgap_readl(struct ti_bandgap
*bgp
, u32 reg
)
46 return readl(bgp
->base
+ reg
);
50 * ti_bandgap_writel() - simple write helper function
51 * @bgp: pointer to ti_bandgap structure
52 * @val: desired register value to be written
53 * @reg: desired register (offset) to be written
55 * Helper function to write bandgap registers. It uses the io remapped area.
57 static void ti_bandgap_writel(struct ti_bandgap
*bgp
, u32 val
, u32 reg
)
59 writel(val
, bgp
->base
+ reg
);
63 * DOC: macro to update bits.
65 * RMW_BITS() - used to read, modify and update bandgap bitfields.
66 * The value passed will be shifted.
68 #define RMW_BITS(bgp, id, reg, mask, val) \
70 struct temp_sensor_registers *t; \
73 t = bgp->conf->sensors[(id)].registers; \
74 r = ti_bandgap_readl(bgp, t->reg); \
76 r |= (val) << __ffs(t->mask); \
77 ti_bandgap_writel(bgp, r, t->reg); \
80 /*** Basic helper functions ***/
83 * ti_bandgap_power() - controls the power state of a bandgap device
84 * @bgp: pointer to ti_bandgap structure
85 * @on: desired power state (1 - on, 0 - off)
87 * Used to power on/off a bandgap device instance. Only used on those
88 * that features tempsoff bit.
90 * Return: 0 on success, -ENOTSUPP if tempsoff is not supported.
92 static int ti_bandgap_power(struct ti_bandgap
*bgp
, bool on
)
96 if (!TI_BANDGAP_HAS(bgp
, POWER_SWITCH
))
99 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++)
101 RMW_BITS(bgp
, i
, temp_sensor_ctrl
, bgap_tempsoff_mask
, !on
);
106 * ti_errata814_bandgap_read_temp() - helper function to read dra7 sensor temperature
107 * @bgp: pointer to ti_bandgap structure
108 * @reg: desired register (offset) to be read
110 * Function to read dra7 bandgap sensor temperature. This is done separately
111 * so as to workaround the errata "Bandgap Temperature read Dtemp can be
112 * corrupted" - Errata ID: i814".
113 * Read accesses to registers listed below can be corrupted due to incorrect
114 * resynchronization between clock domains.
115 * Read access to registers below can be corrupted :
116 * CTRL_CORE_DTEMP_MPU/GPU/CORE/DSPEVE/IVA_n (n = 0 to 4)
117 * CTRL_CORE_TEMP_SENSOR_MPU/GPU/CORE/DSPEVE/IVA_n
119 * Return: the register value.
121 static u32
ti_errata814_bandgap_read_temp(struct ti_bandgap
*bgp
, u32 reg
)
125 val1
= ti_bandgap_readl(bgp
, reg
);
126 val2
= ti_bandgap_readl(bgp
, reg
);
128 /* If both times we read the same value then that is right */
132 /* if val1 and val2 are different read it third time */
133 return ti_bandgap_readl(bgp
, reg
);
137 * ti_bandgap_read_temp() - helper function to read sensor temperature
138 * @bgp: pointer to ti_bandgap structure
139 * @id: bandgap sensor id
141 * Function to concentrate the steps to read sensor temperature register.
142 * This function is desired because, depending on bandgap device version,
143 * it might be needed to freeze the bandgap state machine, before fetching
144 * the register value.
146 * Return: temperature in ADC values.
148 static u32
ti_bandgap_read_temp(struct ti_bandgap
*bgp
, int id
)
150 struct temp_sensor_registers
*tsr
;
153 tsr
= bgp
->conf
->sensors
[id
].registers
;
154 reg
= tsr
->temp_sensor_ctrl
;
156 if (TI_BANDGAP_HAS(bgp
, FREEZE_BIT
)) {
157 RMW_BITS(bgp
, id
, bgap_mask_ctrl
, mask_freeze_mask
, 1);
159 * In case we cannot read from cur_dtemp / dtemp_0,
160 * then we read from the last valid temp read
162 reg
= tsr
->ctrl_dtemp_1
;
165 /* read temperature */
166 if (TI_BANDGAP_HAS(bgp
, ERRATA_814
))
167 temp
= ti_errata814_bandgap_read_temp(bgp
, reg
);
169 temp
= ti_bandgap_readl(bgp
, reg
);
171 temp
&= tsr
->bgap_dtemp_mask
;
173 if (TI_BANDGAP_HAS(bgp
, FREEZE_BIT
))
174 RMW_BITS(bgp
, id
, bgap_mask_ctrl
, mask_freeze_mask
, 0);
179 /*** IRQ handlers ***/
182 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
184 * @data: private data (struct ti_bandgap *)
186 * This is the Talert handler. Use it only if bandgap device features
187 * HAS(TALERT). This handler goes over all sensors and checks their
188 * conditions and acts accordingly. In case there are events pending,
189 * it will reset the event mask to wait for the opposite event (next event).
190 * Every time there is a new event, it will be reported to thermal layer.
192 * Return: IRQ_HANDLED
194 static irqreturn_t
ti_bandgap_talert_irq_handler(int irq
, void *data
)
196 struct ti_bandgap
*bgp
= data
;
197 struct temp_sensor_registers
*tsr
;
198 u32 t_hot
= 0, t_cold
= 0, ctrl
;
201 spin_lock(&bgp
->lock
);
202 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
203 tsr
= bgp
->conf
->sensors
[i
].registers
;
204 ctrl
= ti_bandgap_readl(bgp
, tsr
->bgap_status
);
206 /* Read the status of t_hot */
207 t_hot
= ctrl
& tsr
->status_hot_mask
;
209 /* Read the status of t_cold */
210 t_cold
= ctrl
& tsr
->status_cold_mask
;
212 if (!t_cold
&& !t_hot
)
215 ctrl
= ti_bandgap_readl(bgp
, tsr
->bgap_mask_ctrl
);
217 * One TALERT interrupt: Two sources
218 * If the interrupt is due to t_hot then mask t_hot and
219 * and unmask t_cold else mask t_cold and unmask t_hot
222 ctrl
&= ~tsr
->mask_hot_mask
;
223 ctrl
|= tsr
->mask_cold_mask
;
225 ctrl
&= ~tsr
->mask_cold_mask
;
226 ctrl
|= tsr
->mask_hot_mask
;
229 ti_bandgap_writel(bgp
, ctrl
, tsr
->bgap_mask_ctrl
);
232 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
233 __func__
, bgp
->conf
->sensors
[i
].domain
,
236 /* report temperature to whom may concern */
237 if (bgp
->conf
->report_temperature
)
238 bgp
->conf
->report_temperature(bgp
, i
);
240 spin_unlock(&bgp
->lock
);
246 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
248 * @data: private data (unused)
250 * This is the Tshut handler. Use it only if bandgap device features
251 * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
254 * Return: IRQ_HANDLED
256 static irqreturn_t
ti_bandgap_tshut_irq_handler(int irq
, void *data
)
258 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
261 orderly_poweroff(true);
266 /*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
269 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
270 * @bgp: struct ti_bandgap pointer
271 * @adc_val: value in ADC representation
272 * @t: address where to write the resulting temperature in mCelsius
274 * Simple conversion from ADC representation to mCelsius. In case the ADC value
275 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
276 * The conversion table is indexed by the ADC values.
278 * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val
279 * argument is out of the ADC conv table range.
282 int ti_bandgap_adc_to_mcelsius(struct ti_bandgap
*bgp
, int adc_val
, int *t
)
284 const struct ti_bandgap_data
*conf
= bgp
->conf
;
286 /* look up for temperature in the table and return the temperature */
287 if (adc_val
< conf
->adc_start_val
|| adc_val
> conf
->adc_end_val
)
290 *t
= bgp
->conf
->conv_table
[adc_val
- conf
->adc_start_val
];
295 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
296 * @bgp: struct ti_bandgap pointer
297 * @id: bandgap sensor id
299 * Checks if the bandgap pointer is valid and if the sensor id is also
302 * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
303 * @id cannot index @bgp sensors.
305 static inline int ti_bandgap_validate(struct ti_bandgap
*bgp
, int id
)
307 if (!bgp
|| IS_ERR(bgp
)) {
308 pr_err("%s: invalid bandgap pointer\n", __func__
);
312 if ((id
< 0) || (id
>= bgp
->conf
->sensor_count
)) {
313 dev_err(bgp
->dev
, "%s: sensor id out of range (%d)\n",
322 * ti_bandgap_read_counter() - read the sensor counter
323 * @bgp: pointer to bandgap instance
325 * @interval: resulting update interval in miliseconds
327 static void ti_bandgap_read_counter(struct ti_bandgap
*bgp
, int id
,
330 struct temp_sensor_registers
*tsr
;
333 tsr
= bgp
->conf
->sensors
[id
].registers
;
334 time
= ti_bandgap_readl(bgp
, tsr
->bgap_counter
);
335 time
= (time
& tsr
->counter_mask
) >>
336 __ffs(tsr
->counter_mask
);
337 time
= time
* 1000 / bgp
->clk_rate
;
342 * ti_bandgap_read_counter_delay() - read the sensor counter delay
343 * @bgp: pointer to bandgap instance
345 * @interval: resulting update interval in miliseconds
347 static void ti_bandgap_read_counter_delay(struct ti_bandgap
*bgp
, int id
,
350 struct temp_sensor_registers
*tsr
;
353 tsr
= bgp
->conf
->sensors
[id
].registers
;
355 reg_val
= ti_bandgap_readl(bgp
, tsr
->bgap_mask_ctrl
);
356 reg_val
= (reg_val
& tsr
->mask_counter_delay_mask
) >>
357 __ffs(tsr
->mask_counter_delay_mask
);
378 dev_warn(bgp
->dev
, "Wrong counter delay value read from register %X",
384 * ti_bandgap_read_update_interval() - read the sensor update interval
385 * @bgp: pointer to bandgap instance
387 * @interval: resulting update interval in miliseconds
389 * Return: 0 on success or the proper error code
391 int ti_bandgap_read_update_interval(struct ti_bandgap
*bgp
, int id
,
396 ret
= ti_bandgap_validate(bgp
, id
);
400 if (!TI_BANDGAP_HAS(bgp
, COUNTER
) &&
401 !TI_BANDGAP_HAS(bgp
, COUNTER_DELAY
)) {
406 if (TI_BANDGAP_HAS(bgp
, COUNTER
)) {
407 ti_bandgap_read_counter(bgp
, id
, interval
);
411 ti_bandgap_read_counter_delay(bgp
, id
, interval
);
417 * ti_bandgap_write_counter_delay() - set the counter_delay
418 * @bgp: pointer to bandgap instance
420 * @interval: desired update interval in miliseconds
422 * Return: 0 on success or the proper error code
424 static int ti_bandgap_write_counter_delay(struct ti_bandgap
*bgp
, int id
,
430 case 0: /* Immediate conversion */
433 case 1: /* Conversion after ever 1ms */
436 case 10: /* Conversion after ever 10ms */
439 case 100: /* Conversion after ever 100ms */
442 case 250: /* Conversion after ever 250ms */
445 case 500: /* Conversion after ever 500ms */
449 dev_warn(bgp
->dev
, "Delay %d ms is not supported\n", interval
);
453 spin_lock(&bgp
->lock
);
454 RMW_BITS(bgp
, id
, bgap_mask_ctrl
, mask_counter_delay_mask
, rval
);
455 spin_unlock(&bgp
->lock
);
461 * ti_bandgap_write_counter() - set the bandgap sensor counter
462 * @bgp: pointer to bandgap instance
464 * @interval: desired update interval in miliseconds
466 static void ti_bandgap_write_counter(struct ti_bandgap
*bgp
, int id
,
469 interval
= interval
* bgp
->clk_rate
/ 1000;
470 spin_lock(&bgp
->lock
);
471 RMW_BITS(bgp
, id
, bgap_counter
, counter_mask
, interval
);
472 spin_unlock(&bgp
->lock
);
476 * ti_bandgap_write_update_interval() - set the update interval
477 * @bgp: pointer to bandgap instance
479 * @interval: desired update interval in miliseconds
481 * Return: 0 on success or the proper error code
483 int ti_bandgap_write_update_interval(struct ti_bandgap
*bgp
,
484 int id
, u32 interval
)
486 int ret
= ti_bandgap_validate(bgp
, id
);
490 if (!TI_BANDGAP_HAS(bgp
, COUNTER
) &&
491 !TI_BANDGAP_HAS(bgp
, COUNTER_DELAY
)) {
496 if (TI_BANDGAP_HAS(bgp
, COUNTER
)) {
497 ti_bandgap_write_counter(bgp
, id
, interval
);
501 ret
= ti_bandgap_write_counter_delay(bgp
, id
, interval
);
507 * ti_bandgap_read_temperature() - report current temperature
508 * @bgp: pointer to bandgap instance
510 * @temperature: resulting temperature
512 * Return: 0 on success or the proper error code
514 int ti_bandgap_read_temperature(struct ti_bandgap
*bgp
, int id
,
520 ret
= ti_bandgap_validate(bgp
, id
);
524 if (!TI_BANDGAP_HAS(bgp
, MODE_CONFIG
)) {
525 ret
= ti_bandgap_force_single_read(bgp
, id
);
530 spin_lock(&bgp
->lock
);
531 temp
= ti_bandgap_read_temp(bgp
, id
);
532 spin_unlock(&bgp
->lock
);
534 ret
= ti_bandgap_adc_to_mcelsius(bgp
, temp
, &temp
);
544 * ti_bandgap_set_sensor_data() - helper function to store thermal
545 * framework related data.
546 * @bgp: pointer to bandgap instance
548 * @data: thermal framework related data to be stored
550 * Return: 0 on success or the proper error code
552 int ti_bandgap_set_sensor_data(struct ti_bandgap
*bgp
, int id
, void *data
)
554 int ret
= ti_bandgap_validate(bgp
, id
);
558 bgp
->regval
[id
].data
= data
;
564 * ti_bandgap_get_sensor_data() - helper function to get thermal
565 * framework related data.
566 * @bgp: pointer to bandgap instance
569 * Return: data stored by set function with sensor id on success or NULL
571 void *ti_bandgap_get_sensor_data(struct ti_bandgap
*bgp
, int id
)
573 int ret
= ti_bandgap_validate(bgp
, id
);
577 return bgp
->regval
[id
].data
;
580 /*** Helper functions used during device initialization ***/
583 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
584 * @bgp: pointer to struct ti_bandgap
585 * @id: sensor id which it is desired to read 1 temperature
587 * Used to initialize the conversion state machine and set it to a valid
588 * state. Called during device initialization and context restore events.
593 ti_bandgap_force_single_read(struct ti_bandgap
*bgp
, int id
)
596 struct temp_sensor_registers
*tsr
;
598 /* Select single conversion mode */
599 if (TI_BANDGAP_HAS(bgp
, MODE_CONFIG
))
600 RMW_BITS(bgp
, id
, bgap_mode_ctrl
, mode_ctrl_mask
, 0);
602 /* Start of Conversion = 1 */
603 RMW_BITS(bgp
, id
, temp_sensor_ctrl
, bgap_soc_mask
, 1);
605 /* Wait for EOCZ going up */
606 tsr
= bgp
->conf
->sensors
[id
].registers
;
609 if (ti_bandgap_readl(bgp
, tsr
->temp_sensor_ctrl
) &
614 /* Start of Conversion = 0 */
615 RMW_BITS(bgp
, id
, temp_sensor_ctrl
, bgap_soc_mask
, 0);
617 /* Wait for EOCZ going down */
620 if (!(ti_bandgap_readl(bgp
, tsr
->temp_sensor_ctrl
) &
621 tsr
->bgap_eocz_mask
))
629 * ti_bandgap_set_continuous_mode() - One time enabling of continuous mode
630 * @bgp: pointer to struct ti_bandgap
632 * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
633 * be used for junction temperature monitoring, it is desirable that the
634 * sensors are operational all the time, so that alerts are generated
639 static int ti_bandgap_set_continuous_mode(struct ti_bandgap
*bgp
)
643 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
644 /* Perform a single read just before enabling continuous */
645 ti_bandgap_force_single_read(bgp
, i
);
646 RMW_BITS(bgp
, i
, bgap_mode_ctrl
, mode_ctrl_mask
, 1);
653 * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
654 * @bgp: pointer to struct ti_bandgap
655 * @id: id of the individual sensor
656 * @trend: Pointer to trend.
658 * This function needs to be called to fetch the temperature trend of a
659 * Particular sensor. The function computes the difference in temperature
660 * w.r.t time. For the bandgaps with built in history buffer the temperatures
661 * are read from the buffer and for those without the Buffer -ENOTSUPP is
664 * Return: 0 if no error, else return corresponding error. If no
665 * error then the trend value is passed on to trend parameter
667 int ti_bandgap_get_trend(struct ti_bandgap
*bgp
, int id
, int *trend
)
669 struct temp_sensor_registers
*tsr
;
670 u32 temp1
, temp2
, reg1
, reg2
;
671 int t1
, t2
, interval
, ret
= 0;
673 ret
= ti_bandgap_validate(bgp
, id
);
677 if (!TI_BANDGAP_HAS(bgp
, HISTORY_BUFFER
) ||
678 !TI_BANDGAP_HAS(bgp
, FREEZE_BIT
)) {
683 spin_lock(&bgp
->lock
);
685 tsr
= bgp
->conf
->sensors
[id
].registers
;
687 /* Freeze and read the last 2 valid readings */
688 RMW_BITS(bgp
, id
, bgap_mask_ctrl
, mask_freeze_mask
, 1);
689 reg1
= tsr
->ctrl_dtemp_1
;
690 reg2
= tsr
->ctrl_dtemp_2
;
692 /* read temperature from history buffer */
693 temp1
= ti_bandgap_readl(bgp
, reg1
);
694 temp1
&= tsr
->bgap_dtemp_mask
;
696 temp2
= ti_bandgap_readl(bgp
, reg2
);
697 temp2
&= tsr
->bgap_dtemp_mask
;
699 /* Convert from adc values to mCelsius temperature */
700 ret
= ti_bandgap_adc_to_mcelsius(bgp
, temp1
, &t1
);
704 ret
= ti_bandgap_adc_to_mcelsius(bgp
, temp2
, &t2
);
708 /* Fetch the update interval */
709 ret
= ti_bandgap_read_update_interval(bgp
, id
, &interval
);
713 /* Set the interval to 1 ms if bandgap counter delay is not set */
717 *trend
= (t1
- t2
) / interval
;
719 dev_dbg(bgp
->dev
, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
723 RMW_BITS(bgp
, id
, bgap_mask_ctrl
, mask_freeze_mask
, 0);
724 spin_unlock(&bgp
->lock
);
730 * ti_bandgap_tshut_init() - setup and initialize tshut handling
731 * @bgp: pointer to struct ti_bandgap
732 * @pdev: pointer to device struct platform_device
734 * Call this function only in case the bandgap features HAS(TSHUT).
735 * In this case, the driver needs to handle the TSHUT signal as an IRQ.
736 * The IRQ is wired as a GPIO, and for this purpose, it is required
737 * to specify which GPIO line is used. TSHUT IRQ is fired anytime
738 * one of the bandgap sensors violates the TSHUT high/hot threshold.
739 * And in that case, the system must go off.
741 * Return: 0 if no error, else error status
743 static int ti_bandgap_tshut_init(struct ti_bandgap
*bgp
,
744 struct platform_device
*pdev
)
746 int gpio_nr
= bgp
->tshut_gpio
;
749 /* Request for gpio_86 line */
750 status
= gpio_request(gpio_nr
, "tshut");
752 dev_err(bgp
->dev
, "Could not request for TSHUT GPIO:%i\n", 86);
755 status
= gpio_direction_input(gpio_nr
);
757 dev_err(bgp
->dev
, "Cannot set input TSHUT GPIO %d\n", gpio_nr
);
761 status
= request_irq(gpio_to_irq(gpio_nr
), ti_bandgap_tshut_irq_handler
,
762 IRQF_TRIGGER_RISING
, "tshut", NULL
);
765 dev_err(bgp
->dev
, "request irq failed for TSHUT");
772 * ti_bandgap_alert_init() - setup and initialize talert handling
773 * @bgp: pointer to struct ti_bandgap
774 * @pdev: pointer to device struct platform_device
776 * Call this function only in case the bandgap features HAS(TALERT).
777 * In this case, the driver needs to handle the TALERT signals as an IRQs.
778 * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
779 * are violated. In these situation, the driver must reprogram the thresholds,
780 * accordingly to specified policy.
782 * Return: 0 if no error, else return corresponding error.
784 static int ti_bandgap_talert_init(struct ti_bandgap
*bgp
,
785 struct platform_device
*pdev
)
789 bgp
->irq
= platform_get_irq(pdev
, 0);
791 dev_err(&pdev
->dev
, "get_irq failed\n");
794 ret
= request_threaded_irq(bgp
->irq
, NULL
,
795 ti_bandgap_talert_irq_handler
,
796 IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
,
799 dev_err(&pdev
->dev
, "Request threaded irq failed.\n");
806 static const struct of_device_id of_ti_bandgap_match
[];
808 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
809 * @pdev: pointer to device struct platform_device
811 * Used to read the device tree properties accordingly to the bandgap
812 * matching version. Based on bandgap version and its capabilities it
813 * will build a struct ti_bandgap out of the required DT entries.
815 * Return: valid bandgap structure if successful, else returns ERR_PTR
816 * return value must be verified with IS_ERR.
818 static struct ti_bandgap
*ti_bandgap_build(struct platform_device
*pdev
)
820 struct device_node
*node
= pdev
->dev
.of_node
;
821 const struct of_device_id
*of_id
;
822 struct ti_bandgap
*bgp
;
823 struct resource
*res
;
826 /* just for the sake */
828 dev_err(&pdev
->dev
, "no platform information available\n");
829 return ERR_PTR(-EINVAL
);
832 bgp
= devm_kzalloc(&pdev
->dev
, sizeof(*bgp
), GFP_KERNEL
);
834 return ERR_PTR(-ENOMEM
);
836 of_id
= of_match_device(of_ti_bandgap_match
, &pdev
->dev
);
838 bgp
->conf
= of_id
->data
;
840 /* register shadow for context save and restore */
841 bgp
->regval
= devm_kcalloc(&pdev
->dev
, bgp
->conf
->sensor_count
,
842 sizeof(*bgp
->regval
), GFP_KERNEL
);
844 return ERR_PTR(-ENOMEM
);
850 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, i
);
853 chunk
= devm_ioremap_resource(&pdev
->dev
, res
);
857 return ERR_CAST(chunk
);
862 if (TI_BANDGAP_HAS(bgp
, TSHUT
)) {
863 bgp
->tshut_gpio
= of_get_gpio(node
, 0);
864 if (!gpio_is_valid(bgp
->tshut_gpio
)) {
865 dev_err(&pdev
->dev
, "invalid gpio for tshut (%d)\n",
867 return ERR_PTR(-EINVAL
);
874 /*** Device driver call backs ***/
877 int ti_bandgap_probe(struct platform_device
*pdev
)
879 struct ti_bandgap
*bgp
;
880 int clk_rate
, ret
, i
;
882 bgp
= ti_bandgap_build(pdev
);
884 dev_err(&pdev
->dev
, "failed to fetch platform data\n");
887 bgp
->dev
= &pdev
->dev
;
889 if (TI_BANDGAP_HAS(bgp
, UNRELIABLE
))
891 "This OMAP thermal sensor is unreliable. You've been warned\n");
893 if (TI_BANDGAP_HAS(bgp
, TSHUT
)) {
894 ret
= ti_bandgap_tshut_init(bgp
, pdev
);
897 "failed to initialize system tshut IRQ\n");
902 bgp
->fclock
= clk_get(NULL
, bgp
->conf
->fclock_name
);
903 if (IS_ERR(bgp
->fclock
)) {
904 dev_err(&pdev
->dev
, "failed to request fclock reference\n");
905 ret
= PTR_ERR(bgp
->fclock
);
909 bgp
->div_clk
= clk_get(NULL
, bgp
->conf
->div_ck_name
);
910 if (IS_ERR(bgp
->div_clk
)) {
911 dev_err(&pdev
->dev
, "failed to request div_ts_ck clock ref\n");
912 ret
= PTR_ERR(bgp
->div_clk
);
916 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
917 struct temp_sensor_registers
*tsr
;
920 tsr
= bgp
->conf
->sensors
[i
].registers
;
922 * check if the efuse has a non-zero value if not
923 * it is an untrimmed sample and the temperatures
924 * may not be accurate
926 val
= ti_bandgap_readl(bgp
, tsr
->bgap_efuse
);
929 "Non-trimmed BGAP, Temp not accurate\n");
932 clk_rate
= clk_round_rate(bgp
->div_clk
,
933 bgp
->conf
->sensors
[0].ts_data
->max_freq
);
934 if (clk_rate
< bgp
->conf
->sensors
[0].ts_data
->min_freq
||
937 dev_err(&pdev
->dev
, "wrong clock rate (%d)\n", clk_rate
);
941 ret
= clk_set_rate(bgp
->div_clk
, clk_rate
);
943 dev_err(&pdev
->dev
, "Cannot re-set clock rate. Continuing\n");
945 bgp
->clk_rate
= clk_rate
;
946 if (TI_BANDGAP_HAS(bgp
, CLK_CTRL
))
947 clk_prepare_enable(bgp
->fclock
);
950 spin_lock_init(&bgp
->lock
);
951 bgp
->dev
= &pdev
->dev
;
952 platform_set_drvdata(pdev
, bgp
);
954 ti_bandgap_power(bgp
, true);
956 /* Set default counter to 1 for now */
957 if (TI_BANDGAP_HAS(bgp
, COUNTER
))
958 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++)
959 RMW_BITS(bgp
, i
, bgap_counter
, counter_mask
, 1);
961 /* Set default thresholds for alert and shutdown */
962 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
963 struct temp_sensor_data
*ts_data
;
965 ts_data
= bgp
->conf
->sensors
[i
].ts_data
;
967 if (TI_BANDGAP_HAS(bgp
, TALERT
)) {
968 /* Set initial Talert thresholds */
969 RMW_BITS(bgp
, i
, bgap_threshold
,
970 threshold_tcold_mask
, ts_data
->t_cold
);
971 RMW_BITS(bgp
, i
, bgap_threshold
,
972 threshold_thot_mask
, ts_data
->t_hot
);
973 /* Enable the alert events */
974 RMW_BITS(bgp
, i
, bgap_mask_ctrl
, mask_hot_mask
, 1);
975 RMW_BITS(bgp
, i
, bgap_mask_ctrl
, mask_cold_mask
, 1);
978 if (TI_BANDGAP_HAS(bgp
, TSHUT_CONFIG
)) {
979 /* Set initial Tshut thresholds */
980 RMW_BITS(bgp
, i
, tshut_threshold
,
981 tshut_hot_mask
, ts_data
->tshut_hot
);
982 RMW_BITS(bgp
, i
, tshut_threshold
,
983 tshut_cold_mask
, ts_data
->tshut_cold
);
987 if (TI_BANDGAP_HAS(bgp
, MODE_CONFIG
))
988 ti_bandgap_set_continuous_mode(bgp
);
990 /* Set .250 seconds time as default counter */
991 if (TI_BANDGAP_HAS(bgp
, COUNTER
))
992 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++)
993 RMW_BITS(bgp
, i
, bgap_counter
, counter_mask
,
996 /* Every thing is good? Then expose the sensors */
997 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
1000 if (bgp
->conf
->sensors
[i
].register_cooling
) {
1001 ret
= bgp
->conf
->sensors
[i
].register_cooling(bgp
, i
);
1003 goto remove_sensors
;
1006 if (bgp
->conf
->expose_sensor
) {
1007 domain
= bgp
->conf
->sensors
[i
].domain
;
1008 ret
= bgp
->conf
->expose_sensor(bgp
, i
, domain
);
1010 goto remove_last_cooling
;
1015 * Enable the Interrupts once everything is set. Otherwise irq handler
1016 * might be called as soon as it is enabled where as rest of framework
1017 * is still getting initialised.
1019 if (TI_BANDGAP_HAS(bgp
, TALERT
)) {
1020 ret
= ti_bandgap_talert_init(bgp
, pdev
);
1022 dev_err(&pdev
->dev
, "failed to initialize Talert IRQ\n");
1023 i
= bgp
->conf
->sensor_count
;
1030 remove_last_cooling
:
1031 if (bgp
->conf
->sensors
[i
].unregister_cooling
)
1032 bgp
->conf
->sensors
[i
].unregister_cooling(bgp
, i
);
1034 for (i
--; i
>= 0; i
--) {
1035 if (bgp
->conf
->sensors
[i
].unregister_cooling
)
1036 bgp
->conf
->sensors
[i
].unregister_cooling(bgp
, i
);
1037 if (bgp
->conf
->remove_sensor
)
1038 bgp
->conf
->remove_sensor(bgp
, i
);
1040 ti_bandgap_power(bgp
, false);
1042 if (TI_BANDGAP_HAS(bgp
, CLK_CTRL
))
1043 clk_disable_unprepare(bgp
->fclock
);
1045 clk_put(bgp
->div_clk
);
1047 clk_put(bgp
->fclock
);
1049 if (TI_BANDGAP_HAS(bgp
, TSHUT
)) {
1050 free_irq(gpio_to_irq(bgp
->tshut_gpio
), NULL
);
1051 gpio_free(bgp
->tshut_gpio
);
1058 int ti_bandgap_remove(struct platform_device
*pdev
)
1060 struct ti_bandgap
*bgp
= platform_get_drvdata(pdev
);
1063 /* First thing is to remove sensor interfaces */
1064 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
1065 if (bgp
->conf
->sensors
[i
].unregister_cooling
)
1066 bgp
->conf
->sensors
[i
].unregister_cooling(bgp
, i
);
1068 if (bgp
->conf
->remove_sensor
)
1069 bgp
->conf
->remove_sensor(bgp
, i
);
1072 ti_bandgap_power(bgp
, false);
1074 if (TI_BANDGAP_HAS(bgp
, CLK_CTRL
))
1075 clk_disable_unprepare(bgp
->fclock
);
1076 clk_put(bgp
->fclock
);
1077 clk_put(bgp
->div_clk
);
1079 if (TI_BANDGAP_HAS(bgp
, TALERT
))
1080 free_irq(bgp
->irq
, bgp
);
1082 if (TI_BANDGAP_HAS(bgp
, TSHUT
)) {
1083 free_irq(gpio_to_irq(bgp
->tshut_gpio
), NULL
);
1084 gpio_free(bgp
->tshut_gpio
);
1090 #ifdef CONFIG_PM_SLEEP
1091 static int ti_bandgap_save_ctxt(struct ti_bandgap
*bgp
)
1095 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
1096 struct temp_sensor_registers
*tsr
;
1097 struct temp_sensor_regval
*rval
;
1099 rval
= &bgp
->regval
[i
];
1100 tsr
= bgp
->conf
->sensors
[i
].registers
;
1102 if (TI_BANDGAP_HAS(bgp
, MODE_CONFIG
))
1103 rval
->bg_mode_ctrl
= ti_bandgap_readl(bgp
,
1104 tsr
->bgap_mode_ctrl
);
1105 if (TI_BANDGAP_HAS(bgp
, COUNTER
))
1106 rval
->bg_counter
= ti_bandgap_readl(bgp
,
1108 if (TI_BANDGAP_HAS(bgp
, TALERT
)) {
1109 rval
->bg_threshold
= ti_bandgap_readl(bgp
,
1110 tsr
->bgap_threshold
);
1111 rval
->bg_ctrl
= ti_bandgap_readl(bgp
,
1112 tsr
->bgap_mask_ctrl
);
1115 if (TI_BANDGAP_HAS(bgp
, TSHUT_CONFIG
))
1116 rval
->tshut_threshold
= ti_bandgap_readl(bgp
,
1117 tsr
->tshut_threshold
);
1123 static int ti_bandgap_restore_ctxt(struct ti_bandgap
*bgp
)
1127 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
1128 struct temp_sensor_registers
*tsr
;
1129 struct temp_sensor_regval
*rval
;
1132 rval
= &bgp
->regval
[i
];
1133 tsr
= bgp
->conf
->sensors
[i
].registers
;
1135 if (TI_BANDGAP_HAS(bgp
, COUNTER
))
1136 val
= ti_bandgap_readl(bgp
, tsr
->bgap_counter
);
1138 if (TI_BANDGAP_HAS(bgp
, TSHUT_CONFIG
))
1139 ti_bandgap_writel(bgp
, rval
->tshut_threshold
,
1140 tsr
->tshut_threshold
);
1141 /* Force immediate temperature measurement and update
1142 * of the DTEMP field
1144 ti_bandgap_force_single_read(bgp
, i
);
1146 if (TI_BANDGAP_HAS(bgp
, COUNTER
))
1147 ti_bandgap_writel(bgp
, rval
->bg_counter
,
1149 if (TI_BANDGAP_HAS(bgp
, MODE_CONFIG
))
1150 ti_bandgap_writel(bgp
, rval
->bg_mode_ctrl
,
1151 tsr
->bgap_mode_ctrl
);
1152 if (TI_BANDGAP_HAS(bgp
, TALERT
)) {
1153 ti_bandgap_writel(bgp
, rval
->bg_threshold
,
1154 tsr
->bgap_threshold
);
1155 ti_bandgap_writel(bgp
, rval
->bg_ctrl
,
1156 tsr
->bgap_mask_ctrl
);
1163 static int ti_bandgap_suspend(struct device
*dev
)
1165 struct ti_bandgap
*bgp
= dev_get_drvdata(dev
);
1168 err
= ti_bandgap_save_ctxt(bgp
);
1169 ti_bandgap_power(bgp
, false);
1171 if (TI_BANDGAP_HAS(bgp
, CLK_CTRL
))
1172 clk_disable_unprepare(bgp
->fclock
);
1177 static int ti_bandgap_resume(struct device
*dev
)
1179 struct ti_bandgap
*bgp
= dev_get_drvdata(dev
);
1181 if (TI_BANDGAP_HAS(bgp
, CLK_CTRL
))
1182 clk_prepare_enable(bgp
->fclock
);
1184 ti_bandgap_power(bgp
, true);
1186 return ti_bandgap_restore_ctxt(bgp
);
1188 static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops
, ti_bandgap_suspend
,
1191 #define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
1193 #define DEV_PM_OPS NULL
1196 static const struct of_device_id of_ti_bandgap_match
[] = {
1197 #ifdef CONFIG_OMAP3_THERMAL
1199 .compatible
= "ti,omap34xx-bandgap",
1200 .data
= (void *)&omap34xx_data
,
1203 .compatible
= "ti,omap36xx-bandgap",
1204 .data
= (void *)&omap36xx_data
,
1207 #ifdef CONFIG_OMAP4_THERMAL
1209 .compatible
= "ti,omap4430-bandgap",
1210 .data
= (void *)&omap4430_data
,
1213 .compatible
= "ti,omap4460-bandgap",
1214 .data
= (void *)&omap4460_data
,
1217 .compatible
= "ti,omap4470-bandgap",
1218 .data
= (void *)&omap4470_data
,
1221 #ifdef CONFIG_OMAP5_THERMAL
1223 .compatible
= "ti,omap5430-bandgap",
1224 .data
= (void *)&omap5430_data
,
1227 #ifdef CONFIG_DRA752_THERMAL
1229 .compatible
= "ti,dra752-bandgap",
1230 .data
= (void *)&dra752_data
,
1236 MODULE_DEVICE_TABLE(of
, of_ti_bandgap_match
);
1238 static struct platform_driver ti_bandgap_sensor_driver
= {
1239 .probe
= ti_bandgap_probe
,
1240 .remove
= ti_bandgap_remove
,
1242 .name
= "ti-soc-thermal",
1244 .of_match_table
= of_ti_bandgap_match
,
1248 module_platform_driver(ti_bandgap_sensor_driver
);
1250 MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1251 MODULE_LICENSE("GPL v2");
1252 MODULE_ALIAS("platform:ti-soc-thermal");
1253 MODULE_AUTHOR("Texas Instrument Inc.");