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/consumer.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/sys_soc.h>
24 #include <linux/reboot.h>
25 #include <linux/of_device.h>
26 #include <linux/of_platform.h>
27 #include <linux/of_irq.h>
29 #include <linux/cpu_pm.h>
30 #include <linux/device.h>
31 #include <linux/pm_runtime.h>
34 #include <linux/of_device.h>
36 #include "ti-bandgap.h"
38 static int ti_bandgap_force_single_read(struct ti_bandgap
*bgp
, int id
);
39 #ifdef CONFIG_PM_SLEEP
40 static int bandgap_omap_cpu_notifier(struct notifier_block
*nb
,
41 unsigned long cmd
, void *v
);
44 /*** Helper functions to access registers and their bitfields ***/
47 * ti_bandgap_readl() - simple read helper function
48 * @bgp: pointer to ti_bandgap structure
49 * @reg: desired register (offset) to be read
51 * Helper function to read bandgap registers. It uses the io remapped area.
52 * Return: the register value.
54 static u32
ti_bandgap_readl(struct ti_bandgap
*bgp
, u32 reg
)
56 return readl(bgp
->base
+ reg
);
60 * ti_bandgap_writel() - simple write helper function
61 * @bgp: pointer to ti_bandgap structure
62 * @val: desired register value to be written
63 * @reg: desired register (offset) to be written
65 * Helper function to write bandgap registers. It uses the io remapped area.
67 static void ti_bandgap_writel(struct ti_bandgap
*bgp
, u32 val
, u32 reg
)
69 writel(val
, bgp
->base
+ reg
);
73 * DOC: macro to update bits.
75 * RMW_BITS() - used to read, modify and update bandgap bitfields.
76 * The value passed will be shifted.
78 #define RMW_BITS(bgp, id, reg, mask, val) \
80 struct temp_sensor_registers *t; \
83 t = bgp->conf->sensors[(id)].registers; \
84 r = ti_bandgap_readl(bgp, t->reg); \
86 r |= (val) << __ffs(t->mask); \
87 ti_bandgap_writel(bgp, r, t->reg); \
90 /*** Basic helper functions ***/
93 * ti_bandgap_power() - controls the power state of a bandgap device
94 * @bgp: pointer to ti_bandgap structure
95 * @on: desired power state (1 - on, 0 - off)
97 * Used to power on/off a bandgap device instance. Only used on those
98 * that features tempsoff bit.
100 * Return: 0 on success, -ENOTSUPP if tempsoff is not supported.
102 static int ti_bandgap_power(struct ti_bandgap
*bgp
, bool on
)
106 if (!TI_BANDGAP_HAS(bgp
, POWER_SWITCH
))
109 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++)
111 RMW_BITS(bgp
, i
, temp_sensor_ctrl
, bgap_tempsoff_mask
, !on
);
116 * ti_errata814_bandgap_read_temp() - helper function to read dra7 sensor temperature
117 * @bgp: pointer to ti_bandgap structure
118 * @reg: desired register (offset) to be read
120 * Function to read dra7 bandgap sensor temperature. This is done separately
121 * so as to workaround the errata "Bandgap Temperature read Dtemp can be
122 * corrupted" - Errata ID: i814".
123 * Read accesses to registers listed below can be corrupted due to incorrect
124 * resynchronization between clock domains.
125 * Read access to registers below can be corrupted :
126 * CTRL_CORE_DTEMP_MPU/GPU/CORE/DSPEVE/IVA_n (n = 0 to 4)
127 * CTRL_CORE_TEMP_SENSOR_MPU/GPU/CORE/DSPEVE/IVA_n
129 * Return: the register value.
131 static u32
ti_errata814_bandgap_read_temp(struct ti_bandgap
*bgp
, u32 reg
)
135 val1
= ti_bandgap_readl(bgp
, reg
);
136 val2
= ti_bandgap_readl(bgp
, reg
);
138 /* If both times we read the same value then that is right */
142 /* if val1 and val2 are different read it third time */
143 return ti_bandgap_readl(bgp
, reg
);
147 * ti_bandgap_read_temp() - helper function to read sensor temperature
148 * @bgp: pointer to ti_bandgap structure
149 * @id: bandgap sensor id
151 * Function to concentrate the steps to read sensor temperature register.
152 * This function is desired because, depending on bandgap device version,
153 * it might be needed to freeze the bandgap state machine, before fetching
154 * the register value.
156 * Return: temperature in ADC values.
158 static u32
ti_bandgap_read_temp(struct ti_bandgap
*bgp
, int id
)
160 struct temp_sensor_registers
*tsr
;
163 tsr
= bgp
->conf
->sensors
[id
].registers
;
164 reg
= tsr
->temp_sensor_ctrl
;
166 if (TI_BANDGAP_HAS(bgp
, FREEZE_BIT
)) {
167 RMW_BITS(bgp
, id
, bgap_mask_ctrl
, mask_freeze_mask
, 1);
169 * In case we cannot read from cur_dtemp / dtemp_0,
170 * then we read from the last valid temp read
172 reg
= tsr
->ctrl_dtemp_1
;
175 /* read temperature */
176 if (TI_BANDGAP_HAS(bgp
, ERRATA_814
))
177 temp
= ti_errata814_bandgap_read_temp(bgp
, reg
);
179 temp
= ti_bandgap_readl(bgp
, reg
);
181 temp
&= tsr
->bgap_dtemp_mask
;
183 if (TI_BANDGAP_HAS(bgp
, FREEZE_BIT
))
184 RMW_BITS(bgp
, id
, bgap_mask_ctrl
, mask_freeze_mask
, 0);
189 /*** IRQ handlers ***/
192 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
194 * @data: private data (struct ti_bandgap *)
196 * This is the Talert handler. Use it only if bandgap device features
197 * HAS(TALERT). This handler goes over all sensors and checks their
198 * conditions and acts accordingly. In case there are events pending,
199 * it will reset the event mask to wait for the opposite event (next event).
200 * Every time there is a new event, it will be reported to thermal layer.
202 * Return: IRQ_HANDLED
204 static irqreturn_t
ti_bandgap_talert_irq_handler(int irq
, void *data
)
206 struct ti_bandgap
*bgp
= data
;
207 struct temp_sensor_registers
*tsr
;
208 u32 t_hot
= 0, t_cold
= 0, ctrl
;
211 spin_lock(&bgp
->lock
);
212 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
213 tsr
= bgp
->conf
->sensors
[i
].registers
;
214 ctrl
= ti_bandgap_readl(bgp
, tsr
->bgap_status
);
216 /* Read the status of t_hot */
217 t_hot
= ctrl
& tsr
->status_hot_mask
;
219 /* Read the status of t_cold */
220 t_cold
= ctrl
& tsr
->status_cold_mask
;
222 if (!t_cold
&& !t_hot
)
225 ctrl
= ti_bandgap_readl(bgp
, tsr
->bgap_mask_ctrl
);
227 * One TALERT interrupt: Two sources
228 * If the interrupt is due to t_hot then mask t_hot and
229 * and unmask t_cold else mask t_cold and unmask t_hot
232 ctrl
&= ~tsr
->mask_hot_mask
;
233 ctrl
|= tsr
->mask_cold_mask
;
235 ctrl
&= ~tsr
->mask_cold_mask
;
236 ctrl
|= tsr
->mask_hot_mask
;
239 ti_bandgap_writel(bgp
, ctrl
, tsr
->bgap_mask_ctrl
);
242 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
243 __func__
, bgp
->conf
->sensors
[i
].domain
,
246 /* report temperature to whom may concern */
247 if (bgp
->conf
->report_temperature
)
248 bgp
->conf
->report_temperature(bgp
, i
);
250 spin_unlock(&bgp
->lock
);
256 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
258 * @data: private data (unused)
260 * This is the Tshut handler. Use it only if bandgap device features
261 * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
264 * Return: IRQ_HANDLED
266 static irqreturn_t
ti_bandgap_tshut_irq_handler(int irq
, void *data
)
268 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
271 orderly_poweroff(true);
276 /*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
279 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
280 * @bgp: struct ti_bandgap pointer
281 * @adc_val: value in ADC representation
282 * @t: address where to write the resulting temperature in mCelsius
284 * Simple conversion from ADC representation to mCelsius. In case the ADC value
285 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
286 * The conversion table is indexed by the ADC values.
288 * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val
289 * argument is out of the ADC conv table range.
292 int ti_bandgap_adc_to_mcelsius(struct ti_bandgap
*bgp
, int adc_val
, int *t
)
294 const struct ti_bandgap_data
*conf
= bgp
->conf
;
296 /* look up for temperature in the table and return the temperature */
297 if (adc_val
< conf
->adc_start_val
|| adc_val
> conf
->adc_end_val
)
300 *t
= bgp
->conf
->conv_table
[adc_val
- conf
->adc_start_val
];
305 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
306 * @bgp: struct ti_bandgap pointer
307 * @id: bandgap sensor id
309 * Checks if the bandgap pointer is valid and if the sensor id is also
312 * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
313 * @id cannot index @bgp sensors.
315 static inline int ti_bandgap_validate(struct ti_bandgap
*bgp
, int id
)
317 if (!bgp
|| IS_ERR(bgp
)) {
318 pr_err("%s: invalid bandgap pointer\n", __func__
);
322 if ((id
< 0) || (id
>= bgp
->conf
->sensor_count
)) {
323 dev_err(bgp
->dev
, "%s: sensor id out of range (%d)\n",
332 * ti_bandgap_read_counter() - read the sensor counter
333 * @bgp: pointer to bandgap instance
335 * @interval: resulting update interval in miliseconds
337 static void ti_bandgap_read_counter(struct ti_bandgap
*bgp
, int id
,
340 struct temp_sensor_registers
*tsr
;
343 tsr
= bgp
->conf
->sensors
[id
].registers
;
344 time
= ti_bandgap_readl(bgp
, tsr
->bgap_counter
);
345 time
= (time
& tsr
->counter_mask
) >>
346 __ffs(tsr
->counter_mask
);
347 time
= time
* 1000 / bgp
->clk_rate
;
352 * ti_bandgap_read_counter_delay() - read the sensor counter delay
353 * @bgp: pointer to bandgap instance
355 * @interval: resulting update interval in miliseconds
357 static void ti_bandgap_read_counter_delay(struct ti_bandgap
*bgp
, int id
,
360 struct temp_sensor_registers
*tsr
;
363 tsr
= bgp
->conf
->sensors
[id
].registers
;
365 reg_val
= ti_bandgap_readl(bgp
, tsr
->bgap_mask_ctrl
);
366 reg_val
= (reg_val
& tsr
->mask_counter_delay_mask
) >>
367 __ffs(tsr
->mask_counter_delay_mask
);
388 dev_warn(bgp
->dev
, "Wrong counter delay value read from register %X",
394 * ti_bandgap_read_update_interval() - read the sensor update interval
395 * @bgp: pointer to bandgap instance
397 * @interval: resulting update interval in miliseconds
399 * Return: 0 on success or the proper error code
401 int ti_bandgap_read_update_interval(struct ti_bandgap
*bgp
, int id
,
406 ret
= ti_bandgap_validate(bgp
, id
);
410 if (!TI_BANDGAP_HAS(bgp
, COUNTER
) &&
411 !TI_BANDGAP_HAS(bgp
, COUNTER_DELAY
)) {
416 if (TI_BANDGAP_HAS(bgp
, COUNTER
)) {
417 ti_bandgap_read_counter(bgp
, id
, interval
);
421 ti_bandgap_read_counter_delay(bgp
, id
, interval
);
427 * ti_bandgap_write_counter_delay() - set the counter_delay
428 * @bgp: pointer to bandgap instance
430 * @interval: desired update interval in miliseconds
432 * Return: 0 on success or the proper error code
434 static int ti_bandgap_write_counter_delay(struct ti_bandgap
*bgp
, int id
,
440 case 0: /* Immediate conversion */
443 case 1: /* Conversion after ever 1ms */
446 case 10: /* Conversion after ever 10ms */
449 case 100: /* Conversion after ever 100ms */
452 case 250: /* Conversion after ever 250ms */
455 case 500: /* Conversion after ever 500ms */
459 dev_warn(bgp
->dev
, "Delay %d ms is not supported\n", interval
);
463 spin_lock(&bgp
->lock
);
464 RMW_BITS(bgp
, id
, bgap_mask_ctrl
, mask_counter_delay_mask
, rval
);
465 spin_unlock(&bgp
->lock
);
471 * ti_bandgap_write_counter() - set the bandgap sensor counter
472 * @bgp: pointer to bandgap instance
474 * @interval: desired update interval in miliseconds
476 static void ti_bandgap_write_counter(struct ti_bandgap
*bgp
, int id
,
479 interval
= interval
* bgp
->clk_rate
/ 1000;
480 spin_lock(&bgp
->lock
);
481 RMW_BITS(bgp
, id
, bgap_counter
, counter_mask
, interval
);
482 spin_unlock(&bgp
->lock
);
486 * ti_bandgap_write_update_interval() - set the update interval
487 * @bgp: pointer to bandgap instance
489 * @interval: desired update interval in miliseconds
491 * Return: 0 on success or the proper error code
493 int ti_bandgap_write_update_interval(struct ti_bandgap
*bgp
,
494 int id
, u32 interval
)
496 int ret
= ti_bandgap_validate(bgp
, id
);
500 if (!TI_BANDGAP_HAS(bgp
, COUNTER
) &&
501 !TI_BANDGAP_HAS(bgp
, COUNTER_DELAY
)) {
506 if (TI_BANDGAP_HAS(bgp
, COUNTER
)) {
507 ti_bandgap_write_counter(bgp
, id
, interval
);
511 ret
= ti_bandgap_write_counter_delay(bgp
, id
, interval
);
517 * ti_bandgap_read_temperature() - report current temperature
518 * @bgp: pointer to bandgap instance
520 * @temperature: resulting temperature
522 * Return: 0 on success or the proper error code
524 int ti_bandgap_read_temperature(struct ti_bandgap
*bgp
, int id
,
530 ret
= ti_bandgap_validate(bgp
, id
);
534 if (!TI_BANDGAP_HAS(bgp
, MODE_CONFIG
)) {
535 ret
= ti_bandgap_force_single_read(bgp
, id
);
540 spin_lock(&bgp
->lock
);
541 temp
= ti_bandgap_read_temp(bgp
, id
);
542 spin_unlock(&bgp
->lock
);
544 ret
= ti_bandgap_adc_to_mcelsius(bgp
, temp
, &temp
);
554 * ti_bandgap_set_sensor_data() - helper function to store thermal
555 * framework related data.
556 * @bgp: pointer to bandgap instance
558 * @data: thermal framework related data to be stored
560 * Return: 0 on success or the proper error code
562 int ti_bandgap_set_sensor_data(struct ti_bandgap
*bgp
, int id
, void *data
)
564 int ret
= ti_bandgap_validate(bgp
, id
);
568 bgp
->regval
[id
].data
= data
;
574 * ti_bandgap_get_sensor_data() - helper function to get thermal
575 * framework related data.
576 * @bgp: pointer to bandgap instance
579 * Return: data stored by set function with sensor id on success or NULL
581 void *ti_bandgap_get_sensor_data(struct ti_bandgap
*bgp
, int id
)
583 int ret
= ti_bandgap_validate(bgp
, id
);
587 return bgp
->regval
[id
].data
;
590 /*** Helper functions used during device initialization ***/
593 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
594 * @bgp: pointer to struct ti_bandgap
595 * @id: sensor id which it is desired to read 1 temperature
597 * Used to initialize the conversion state machine and set it to a valid
598 * state. Called during device initialization and context restore events.
603 ti_bandgap_force_single_read(struct ti_bandgap
*bgp
, int id
)
606 struct temp_sensor_registers
*tsr
;
608 /* Select single conversion mode */
609 if (TI_BANDGAP_HAS(bgp
, MODE_CONFIG
))
610 RMW_BITS(bgp
, id
, bgap_mode_ctrl
, mode_ctrl_mask
, 0);
612 /* Start of Conversion = 1 */
613 RMW_BITS(bgp
, id
, temp_sensor_ctrl
, bgap_soc_mask
, 1);
615 /* Wait for EOCZ going up */
616 tsr
= bgp
->conf
->sensors
[id
].registers
;
619 if (ti_bandgap_readl(bgp
, tsr
->temp_sensor_ctrl
) &
624 /* Start of Conversion = 0 */
625 RMW_BITS(bgp
, id
, temp_sensor_ctrl
, bgap_soc_mask
, 0);
627 /* Wait for EOCZ going down */
630 if (!(ti_bandgap_readl(bgp
, tsr
->temp_sensor_ctrl
) &
631 tsr
->bgap_eocz_mask
))
639 * ti_bandgap_set_continuous_mode() - One time enabling of continuous mode
640 * @bgp: pointer to struct ti_bandgap
642 * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
643 * be used for junction temperature monitoring, it is desirable that the
644 * sensors are operational all the time, so that alerts are generated
649 static int ti_bandgap_set_continuous_mode(struct ti_bandgap
*bgp
)
653 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
654 /* Perform a single read just before enabling continuous */
655 ti_bandgap_force_single_read(bgp
, i
);
656 RMW_BITS(bgp
, i
, bgap_mode_ctrl
, mode_ctrl_mask
, 1);
663 * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
664 * @bgp: pointer to struct ti_bandgap
665 * @id: id of the individual sensor
666 * @trend: Pointer to trend.
668 * This function needs to be called to fetch the temperature trend of a
669 * Particular sensor. The function computes the difference in temperature
670 * w.r.t time. For the bandgaps with built in history buffer the temperatures
671 * are read from the buffer and for those without the Buffer -ENOTSUPP is
674 * Return: 0 if no error, else return corresponding error. If no
675 * error then the trend value is passed on to trend parameter
677 int ti_bandgap_get_trend(struct ti_bandgap
*bgp
, int id
, int *trend
)
679 struct temp_sensor_registers
*tsr
;
680 u32 temp1
, temp2
, reg1
, reg2
;
681 int t1
, t2
, interval
, ret
= 0;
683 ret
= ti_bandgap_validate(bgp
, id
);
687 if (!TI_BANDGAP_HAS(bgp
, HISTORY_BUFFER
) ||
688 !TI_BANDGAP_HAS(bgp
, FREEZE_BIT
)) {
693 spin_lock(&bgp
->lock
);
695 tsr
= bgp
->conf
->sensors
[id
].registers
;
697 /* Freeze and read the last 2 valid readings */
698 RMW_BITS(bgp
, id
, bgap_mask_ctrl
, mask_freeze_mask
, 1);
699 reg1
= tsr
->ctrl_dtemp_1
;
700 reg2
= tsr
->ctrl_dtemp_2
;
702 /* read temperature from history buffer */
703 temp1
= ti_bandgap_readl(bgp
, reg1
);
704 temp1
&= tsr
->bgap_dtemp_mask
;
706 temp2
= ti_bandgap_readl(bgp
, reg2
);
707 temp2
&= tsr
->bgap_dtemp_mask
;
709 /* Convert from adc values to mCelsius temperature */
710 ret
= ti_bandgap_adc_to_mcelsius(bgp
, temp1
, &t1
);
714 ret
= ti_bandgap_adc_to_mcelsius(bgp
, temp2
, &t2
);
718 /* Fetch the update interval */
719 ret
= ti_bandgap_read_update_interval(bgp
, id
, &interval
);
723 /* Set the interval to 1 ms if bandgap counter delay is not set */
727 *trend
= (t1
- t2
) / interval
;
729 dev_dbg(bgp
->dev
, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
733 RMW_BITS(bgp
, id
, bgap_mask_ctrl
, mask_freeze_mask
, 0);
734 spin_unlock(&bgp
->lock
);
740 * ti_bandgap_tshut_init() - setup and initialize tshut handling
741 * @bgp: pointer to struct ti_bandgap
742 * @pdev: pointer to device struct platform_device
744 * Call this function only in case the bandgap features HAS(TSHUT).
745 * In this case, the driver needs to handle the TSHUT signal as an IRQ.
746 * The IRQ is wired as a GPIO, and for this purpose, it is required
747 * to specify which GPIO line is used. TSHUT IRQ is fired anytime
748 * one of the bandgap sensors violates the TSHUT high/hot threshold.
749 * And in that case, the system must go off.
751 * Return: 0 if no error, else error status
753 static int ti_bandgap_tshut_init(struct ti_bandgap
*bgp
,
754 struct platform_device
*pdev
)
758 status
= request_irq(gpiod_to_irq(bgp
->tshut_gpiod
),
759 ti_bandgap_tshut_irq_handler
,
760 IRQF_TRIGGER_RISING
, "tshut", NULL
);
762 dev_err(bgp
->dev
, "request irq failed for TSHUT");
768 * ti_bandgap_alert_init() - setup and initialize talert handling
769 * @bgp: pointer to struct ti_bandgap
770 * @pdev: pointer to device struct platform_device
772 * Call this function only in case the bandgap features HAS(TALERT).
773 * In this case, the driver needs to handle the TALERT signals as an IRQs.
774 * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
775 * are violated. In these situation, the driver must reprogram the thresholds,
776 * accordingly to specified policy.
778 * Return: 0 if no error, else return corresponding error.
780 static int ti_bandgap_talert_init(struct ti_bandgap
*bgp
,
781 struct platform_device
*pdev
)
785 bgp
->irq
= platform_get_irq(pdev
, 0);
789 ret
= request_threaded_irq(bgp
->irq
, NULL
,
790 ti_bandgap_talert_irq_handler
,
791 IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
,
794 dev_err(&pdev
->dev
, "Request threaded irq failed.\n");
801 static const struct of_device_id of_ti_bandgap_match
[];
803 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
804 * @pdev: pointer to device struct platform_device
806 * Used to read the device tree properties accordingly to the bandgap
807 * matching version. Based on bandgap version and its capabilities it
808 * will build a struct ti_bandgap out of the required DT entries.
810 * Return: valid bandgap structure if successful, else returns ERR_PTR
811 * return value must be verified with IS_ERR.
813 static struct ti_bandgap
*ti_bandgap_build(struct platform_device
*pdev
)
815 struct device_node
*node
= pdev
->dev
.of_node
;
816 const struct of_device_id
*of_id
;
817 struct ti_bandgap
*bgp
;
818 struct resource
*res
;
821 /* just for the sake */
823 dev_err(&pdev
->dev
, "no platform information available\n");
824 return ERR_PTR(-EINVAL
);
827 bgp
= devm_kzalloc(&pdev
->dev
, sizeof(*bgp
), GFP_KERNEL
);
829 return ERR_PTR(-ENOMEM
);
831 of_id
= of_match_device(of_ti_bandgap_match
, &pdev
->dev
);
833 bgp
->conf
= of_id
->data
;
835 /* register shadow for context save and restore */
836 bgp
->regval
= devm_kcalloc(&pdev
->dev
, bgp
->conf
->sensor_count
,
837 sizeof(*bgp
->regval
), GFP_KERNEL
);
839 return ERR_PTR(-ENOMEM
);
845 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, i
);
848 chunk
= devm_ioremap_resource(&pdev
->dev
, res
);
852 return ERR_CAST(chunk
);
857 if (TI_BANDGAP_HAS(bgp
, TSHUT
)) {
858 bgp
->tshut_gpiod
= devm_gpiod_get(&pdev
->dev
, NULL
, GPIOD_IN
);
859 if (IS_ERR(bgp
->tshut_gpiod
)) {
860 dev_err(&pdev
->dev
, "invalid gpio for tshut\n");
861 return ERR_CAST(bgp
->tshut_gpiod
);
869 * List of SoCs on which the CPU PM notifier can cause erros on the DTEMP
871 * Enabled notifier on these machines results in erroneous, random values which
872 * could trigger unexpected thermal shutdown.
874 static const struct soc_device_attribute soc_no_cpu_notifier
[] = {
875 { .machine
= "OMAP4430" },
879 /*** Device driver call backs ***/
882 int ti_bandgap_probe(struct platform_device
*pdev
)
884 struct ti_bandgap
*bgp
;
885 int clk_rate
, ret
, i
;
887 bgp
= ti_bandgap_build(pdev
);
889 dev_err(&pdev
->dev
, "failed to fetch platform data\n");
892 bgp
->dev
= &pdev
->dev
;
894 if (TI_BANDGAP_HAS(bgp
, UNRELIABLE
))
896 "This OMAP thermal sensor is unreliable. You've been warned\n");
898 if (TI_BANDGAP_HAS(bgp
, TSHUT
)) {
899 ret
= ti_bandgap_tshut_init(bgp
, pdev
);
902 "failed to initialize system tshut IRQ\n");
907 bgp
->fclock
= clk_get(NULL
, bgp
->conf
->fclock_name
);
908 if (IS_ERR(bgp
->fclock
)) {
909 dev_err(&pdev
->dev
, "failed to request fclock reference\n");
910 ret
= PTR_ERR(bgp
->fclock
);
914 bgp
->div_clk
= clk_get(NULL
, bgp
->conf
->div_ck_name
);
915 if (IS_ERR(bgp
->div_clk
)) {
916 dev_err(&pdev
->dev
, "failed to request div_ts_ck clock ref\n");
917 ret
= PTR_ERR(bgp
->div_clk
);
921 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
922 struct temp_sensor_registers
*tsr
;
925 tsr
= bgp
->conf
->sensors
[i
].registers
;
927 * check if the efuse has a non-zero value if not
928 * it is an untrimmed sample and the temperatures
929 * may not be accurate
931 val
= ti_bandgap_readl(bgp
, tsr
->bgap_efuse
);
934 "Non-trimmed BGAP, Temp not accurate\n");
937 clk_rate
= clk_round_rate(bgp
->div_clk
,
938 bgp
->conf
->sensors
[0].ts_data
->max_freq
);
939 if (clk_rate
< bgp
->conf
->sensors
[0].ts_data
->min_freq
||
942 dev_err(&pdev
->dev
, "wrong clock rate (%d)\n", clk_rate
);
946 ret
= clk_set_rate(bgp
->div_clk
, clk_rate
);
948 dev_err(&pdev
->dev
, "Cannot re-set clock rate. Continuing\n");
950 bgp
->clk_rate
= clk_rate
;
951 if (TI_BANDGAP_HAS(bgp
, CLK_CTRL
))
952 clk_prepare_enable(bgp
->fclock
);
955 spin_lock_init(&bgp
->lock
);
956 bgp
->dev
= &pdev
->dev
;
957 platform_set_drvdata(pdev
, bgp
);
959 ti_bandgap_power(bgp
, true);
961 /* Set default counter to 1 for now */
962 if (TI_BANDGAP_HAS(bgp
, COUNTER
))
963 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++)
964 RMW_BITS(bgp
, i
, bgap_counter
, counter_mask
, 1);
966 /* Set default thresholds for alert and shutdown */
967 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
968 struct temp_sensor_data
*ts_data
;
970 ts_data
= bgp
->conf
->sensors
[i
].ts_data
;
972 if (TI_BANDGAP_HAS(bgp
, TALERT
)) {
973 /* Set initial Talert thresholds */
974 RMW_BITS(bgp
, i
, bgap_threshold
,
975 threshold_tcold_mask
, ts_data
->t_cold
);
976 RMW_BITS(bgp
, i
, bgap_threshold
,
977 threshold_thot_mask
, ts_data
->t_hot
);
978 /* Enable the alert events */
979 RMW_BITS(bgp
, i
, bgap_mask_ctrl
, mask_hot_mask
, 1);
980 RMW_BITS(bgp
, i
, bgap_mask_ctrl
, mask_cold_mask
, 1);
983 if (TI_BANDGAP_HAS(bgp
, TSHUT_CONFIG
)) {
984 /* Set initial Tshut thresholds */
985 RMW_BITS(bgp
, i
, tshut_threshold
,
986 tshut_hot_mask
, ts_data
->tshut_hot
);
987 RMW_BITS(bgp
, i
, tshut_threshold
,
988 tshut_cold_mask
, ts_data
->tshut_cold
);
992 if (TI_BANDGAP_HAS(bgp
, MODE_CONFIG
))
993 ti_bandgap_set_continuous_mode(bgp
);
995 /* Set .250 seconds time as default counter */
996 if (TI_BANDGAP_HAS(bgp
, COUNTER
))
997 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++)
998 RMW_BITS(bgp
, i
, bgap_counter
, counter_mask
,
1001 /* Every thing is good? Then expose the sensors */
1002 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
1005 if (bgp
->conf
->sensors
[i
].register_cooling
) {
1006 ret
= bgp
->conf
->sensors
[i
].register_cooling(bgp
, i
);
1008 goto remove_sensors
;
1011 if (bgp
->conf
->expose_sensor
) {
1012 domain
= bgp
->conf
->sensors
[i
].domain
;
1013 ret
= bgp
->conf
->expose_sensor(bgp
, i
, domain
);
1015 goto remove_last_cooling
;
1020 * Enable the Interrupts once everything is set. Otherwise irq handler
1021 * might be called as soon as it is enabled where as rest of framework
1022 * is still getting initialised.
1024 if (TI_BANDGAP_HAS(bgp
, TALERT
)) {
1025 ret
= ti_bandgap_talert_init(bgp
, pdev
);
1027 dev_err(&pdev
->dev
, "failed to initialize Talert IRQ\n");
1028 i
= bgp
->conf
->sensor_count
;
1033 #ifdef CONFIG_PM_SLEEP
1034 bgp
->nb
.notifier_call
= bandgap_omap_cpu_notifier
;
1035 if (!soc_device_match(soc_no_cpu_notifier
))
1036 cpu_pm_register_notifier(&bgp
->nb
);
1041 remove_last_cooling
:
1042 if (bgp
->conf
->sensors
[i
].unregister_cooling
)
1043 bgp
->conf
->sensors
[i
].unregister_cooling(bgp
, i
);
1045 for (i
--; i
>= 0; i
--) {
1046 if (bgp
->conf
->sensors
[i
].unregister_cooling
)
1047 bgp
->conf
->sensors
[i
].unregister_cooling(bgp
, i
);
1048 if (bgp
->conf
->remove_sensor
)
1049 bgp
->conf
->remove_sensor(bgp
, i
);
1051 ti_bandgap_power(bgp
, false);
1053 if (TI_BANDGAP_HAS(bgp
, CLK_CTRL
))
1054 clk_disable_unprepare(bgp
->fclock
);
1056 clk_put(bgp
->div_clk
);
1058 clk_put(bgp
->fclock
);
1060 if (TI_BANDGAP_HAS(bgp
, TSHUT
))
1061 free_irq(gpiod_to_irq(bgp
->tshut_gpiod
), NULL
);
1067 int ti_bandgap_remove(struct platform_device
*pdev
)
1069 struct ti_bandgap
*bgp
= platform_get_drvdata(pdev
);
1072 if (!soc_device_match(soc_no_cpu_notifier
))
1073 cpu_pm_unregister_notifier(&bgp
->nb
);
1075 /* Remove sensor interfaces */
1076 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
1077 if (bgp
->conf
->sensors
[i
].unregister_cooling
)
1078 bgp
->conf
->sensors
[i
].unregister_cooling(bgp
, i
);
1080 if (bgp
->conf
->remove_sensor
)
1081 bgp
->conf
->remove_sensor(bgp
, i
);
1084 ti_bandgap_power(bgp
, false);
1086 if (TI_BANDGAP_HAS(bgp
, CLK_CTRL
))
1087 clk_disable_unprepare(bgp
->fclock
);
1088 clk_put(bgp
->fclock
);
1089 clk_put(bgp
->div_clk
);
1091 if (TI_BANDGAP_HAS(bgp
, TALERT
))
1092 free_irq(bgp
->irq
, bgp
);
1094 if (TI_BANDGAP_HAS(bgp
, TSHUT
))
1095 free_irq(gpiod_to_irq(bgp
->tshut_gpiod
), NULL
);
1100 #ifdef CONFIG_PM_SLEEP
1101 static int ti_bandgap_save_ctxt(struct ti_bandgap
*bgp
)
1105 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
1106 struct temp_sensor_registers
*tsr
;
1107 struct temp_sensor_regval
*rval
;
1109 rval
= &bgp
->regval
[i
];
1110 tsr
= bgp
->conf
->sensors
[i
].registers
;
1112 if (TI_BANDGAP_HAS(bgp
, MODE_CONFIG
))
1113 rval
->bg_mode_ctrl
= ti_bandgap_readl(bgp
,
1114 tsr
->bgap_mode_ctrl
);
1115 if (TI_BANDGAP_HAS(bgp
, COUNTER
))
1116 rval
->bg_counter
= ti_bandgap_readl(bgp
,
1118 if (TI_BANDGAP_HAS(bgp
, TALERT
)) {
1119 rval
->bg_threshold
= ti_bandgap_readl(bgp
,
1120 tsr
->bgap_threshold
);
1121 rval
->bg_ctrl
= ti_bandgap_readl(bgp
,
1122 tsr
->bgap_mask_ctrl
);
1125 if (TI_BANDGAP_HAS(bgp
, TSHUT_CONFIG
))
1126 rval
->tshut_threshold
= ti_bandgap_readl(bgp
,
1127 tsr
->tshut_threshold
);
1133 static int ti_bandgap_restore_ctxt(struct ti_bandgap
*bgp
)
1137 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
1138 struct temp_sensor_registers
*tsr
;
1139 struct temp_sensor_regval
*rval
;
1142 rval
= &bgp
->regval
[i
];
1143 tsr
= bgp
->conf
->sensors
[i
].registers
;
1145 if (TI_BANDGAP_HAS(bgp
, COUNTER
))
1146 val
= ti_bandgap_readl(bgp
, tsr
->bgap_counter
);
1148 if (TI_BANDGAP_HAS(bgp
, TSHUT_CONFIG
))
1149 ti_bandgap_writel(bgp
, rval
->tshut_threshold
,
1150 tsr
->tshut_threshold
);
1151 /* Force immediate temperature measurement and update
1152 * of the DTEMP field
1154 ti_bandgap_force_single_read(bgp
, i
);
1156 if (TI_BANDGAP_HAS(bgp
, COUNTER
))
1157 ti_bandgap_writel(bgp
, rval
->bg_counter
,
1159 if (TI_BANDGAP_HAS(bgp
, MODE_CONFIG
))
1160 ti_bandgap_writel(bgp
, rval
->bg_mode_ctrl
,
1161 tsr
->bgap_mode_ctrl
);
1162 if (TI_BANDGAP_HAS(bgp
, TALERT
)) {
1163 ti_bandgap_writel(bgp
, rval
->bg_threshold
,
1164 tsr
->bgap_threshold
);
1165 ti_bandgap_writel(bgp
, rval
->bg_ctrl
,
1166 tsr
->bgap_mask_ctrl
);
1173 static int ti_bandgap_suspend(struct device
*dev
)
1175 struct ti_bandgap
*bgp
= dev_get_drvdata(dev
);
1178 err
= ti_bandgap_save_ctxt(bgp
);
1179 ti_bandgap_power(bgp
, false);
1181 if (TI_BANDGAP_HAS(bgp
, CLK_CTRL
))
1182 clk_disable_unprepare(bgp
->fclock
);
1184 bgp
->is_suspended
= true;
1189 static int bandgap_omap_cpu_notifier(struct notifier_block
*nb
,
1190 unsigned long cmd
, void *v
)
1192 struct ti_bandgap
*bgp
;
1194 bgp
= container_of(nb
, struct ti_bandgap
, nb
);
1196 spin_lock(&bgp
->lock
);
1198 case CPU_CLUSTER_PM_ENTER
:
1199 if (bgp
->is_suspended
)
1201 ti_bandgap_save_ctxt(bgp
);
1202 ti_bandgap_power(bgp
, false);
1203 if (TI_BANDGAP_HAS(bgp
, CLK_CTRL
))
1204 clk_disable(bgp
->fclock
);
1206 case CPU_CLUSTER_PM_ENTER_FAILED
:
1207 case CPU_CLUSTER_PM_EXIT
:
1208 if (bgp
->is_suspended
)
1210 if (TI_BANDGAP_HAS(bgp
, CLK_CTRL
))
1211 clk_enable(bgp
->fclock
);
1212 ti_bandgap_power(bgp
, true);
1213 ti_bandgap_restore_ctxt(bgp
);
1216 spin_unlock(&bgp
->lock
);
1221 static int ti_bandgap_resume(struct device
*dev
)
1223 struct ti_bandgap
*bgp
= dev_get_drvdata(dev
);
1225 if (TI_BANDGAP_HAS(bgp
, CLK_CTRL
))
1226 clk_prepare_enable(bgp
->fclock
);
1228 ti_bandgap_power(bgp
, true);
1229 bgp
->is_suspended
= false;
1231 return ti_bandgap_restore_ctxt(bgp
);
1233 static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops
, ti_bandgap_suspend
,
1236 #define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
1238 #define DEV_PM_OPS NULL
1241 static const struct of_device_id of_ti_bandgap_match
[] = {
1242 #ifdef CONFIG_OMAP3_THERMAL
1244 .compatible
= "ti,omap34xx-bandgap",
1245 .data
= (void *)&omap34xx_data
,
1248 .compatible
= "ti,omap36xx-bandgap",
1249 .data
= (void *)&omap36xx_data
,
1252 #ifdef CONFIG_OMAP4_THERMAL
1254 .compatible
= "ti,omap4430-bandgap",
1255 .data
= (void *)&omap4430_data
,
1258 .compatible
= "ti,omap4460-bandgap",
1259 .data
= (void *)&omap4460_data
,
1262 .compatible
= "ti,omap4470-bandgap",
1263 .data
= (void *)&omap4470_data
,
1266 #ifdef CONFIG_OMAP5_THERMAL
1268 .compatible
= "ti,omap5430-bandgap",
1269 .data
= (void *)&omap5430_data
,
1272 #ifdef CONFIG_DRA752_THERMAL
1274 .compatible
= "ti,dra752-bandgap",
1275 .data
= (void *)&dra752_data
,
1281 MODULE_DEVICE_TABLE(of
, of_ti_bandgap_match
);
1283 static struct platform_driver ti_bandgap_sensor_driver
= {
1284 .probe
= ti_bandgap_probe
,
1285 .remove
= ti_bandgap_remove
,
1287 .name
= "ti-soc-thermal",
1289 .of_match_table
= of_ti_bandgap_match
,
1293 module_platform_driver(ti_bandgap_sensor_driver
);
1295 MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1296 MODULE_LICENSE("GPL v2");
1297 MODULE_ALIAS("platform:ti-soc-thermal");
1298 MODULE_AUTHOR("Texas Instrument Inc.");