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 /*** Helper functions to access registers and their bitfields ***/
49 * ti_bandgap_readl() - simple read helper function
50 * @bgp: pointer to ti_bandgap structure
51 * @reg: desired register (offset) to be read
53 * Helper function to read bandgap registers. It uses the io remapped area.
54 * Return: the register value.
56 static u32
ti_bandgap_readl(struct ti_bandgap
*bgp
, u32 reg
)
58 return readl(bgp
->base
+ reg
);
62 * ti_bandgap_writel() - simple write helper function
63 * @bgp: pointer to ti_bandgap structure
64 * @val: desired register value to be written
65 * @reg: desired register (offset) to be written
67 * Helper function to write bandgap registers. It uses the io remapped area.
69 static void ti_bandgap_writel(struct ti_bandgap
*bgp
, u32 val
, u32 reg
)
71 writel(val
, bgp
->base
+ reg
);
75 * DOC: macro to update bits.
77 * RMW_BITS() - used to read, modify and update bandgap bitfields.
78 * The value passed will be shifted.
80 #define RMW_BITS(bgp, id, reg, mask, val) \
82 struct temp_sensor_registers *t; \
85 t = bgp->conf->sensors[(id)].registers; \
86 r = ti_bandgap_readl(bgp, t->reg); \
88 r |= (val) << __ffs(t->mask); \
89 ti_bandgap_writel(bgp, r, t->reg); \
92 /*** Basic helper functions ***/
95 * ti_bandgap_power() - controls the power state of a bandgap device
96 * @bgp: pointer to ti_bandgap structure
97 * @on: desired power state (1 - on, 0 - off)
99 * Used to power on/off a bandgap device instance. Only used on those
100 * that features tempsoff bit.
102 * Return: 0 on success, -ENOTSUPP if tempsoff is not supported.
104 static int ti_bandgap_power(struct ti_bandgap
*bgp
, bool on
)
108 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
);
122 * ti_bandgap_read_temp() - helper function to read sensor temperature
123 * @bgp: pointer to ti_bandgap structure
124 * @id: bandgap sensor id
126 * Function to concentrate the steps to read sensor temperature register.
127 * This function is desired because, depending on bandgap device version,
128 * it might be needed to freeze the bandgap state machine, before fetching
129 * the register value.
131 * Return: temperature in ADC values.
133 static u32
ti_bandgap_read_temp(struct ti_bandgap
*bgp
, int id
)
135 struct temp_sensor_registers
*tsr
;
138 tsr
= bgp
->conf
->sensors
[id
].registers
;
139 reg
= tsr
->temp_sensor_ctrl
;
141 if (TI_BANDGAP_HAS(bgp
, FREEZE_BIT
)) {
142 RMW_BITS(bgp
, id
, bgap_mask_ctrl
, mask_freeze_mask
, 1);
144 * In case we cannot read from cur_dtemp / dtemp_0,
145 * then we read from the last valid temp read
147 reg
= tsr
->ctrl_dtemp_1
;
150 /* read temperature */
151 temp
= ti_bandgap_readl(bgp
, reg
);
152 temp
&= tsr
->bgap_dtemp_mask
;
154 if (TI_BANDGAP_HAS(bgp
, FREEZE_BIT
))
155 RMW_BITS(bgp
, id
, bgap_mask_ctrl
, mask_freeze_mask
, 0);
160 /*** IRQ handlers ***/
163 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
165 * @data: private data (struct ti_bandgap *)
167 * This is the Talert handler. Use it only if bandgap device features
168 * HAS(TALERT). This handler goes over all sensors and checks their
169 * conditions and acts accordingly. In case there are events pending,
170 * it will reset the event mask to wait for the opposite event (next event).
171 * Every time there is a new event, it will be reported to thermal layer.
173 * Return: IRQ_HANDLED
175 static irqreturn_t
ti_bandgap_talert_irq_handler(int irq
, void *data
)
177 struct ti_bandgap
*bgp
= data
;
178 struct temp_sensor_registers
*tsr
;
179 u32 t_hot
= 0, t_cold
= 0, ctrl
;
182 spin_lock(&bgp
->lock
);
183 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
184 tsr
= bgp
->conf
->sensors
[i
].registers
;
185 ctrl
= ti_bandgap_readl(bgp
, tsr
->bgap_status
);
187 /* Read the status of t_hot */
188 t_hot
= ctrl
& tsr
->status_hot_mask
;
190 /* Read the status of t_cold */
191 t_cold
= ctrl
& tsr
->status_cold_mask
;
193 if (!t_cold
&& !t_hot
)
196 ctrl
= ti_bandgap_readl(bgp
, tsr
->bgap_mask_ctrl
);
198 * One TALERT interrupt: Two sources
199 * If the interrupt is due to t_hot then mask t_hot and
200 * and unmask t_cold else mask t_cold and unmask t_hot
203 ctrl
&= ~tsr
->mask_hot_mask
;
204 ctrl
|= tsr
->mask_cold_mask
;
206 ctrl
&= ~tsr
->mask_cold_mask
;
207 ctrl
|= tsr
->mask_hot_mask
;
210 ti_bandgap_writel(bgp
, ctrl
, tsr
->bgap_mask_ctrl
);
213 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
214 __func__
, bgp
->conf
->sensors
[i
].domain
,
217 /* report temperature to whom may concern */
218 if (bgp
->conf
->report_temperature
)
219 bgp
->conf
->report_temperature(bgp
, i
);
221 spin_unlock(&bgp
->lock
);
227 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
229 * @data: private data (unused)
231 * This is the Tshut handler. Use it only if bandgap device features
232 * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
235 * Return: IRQ_HANDLED
237 static irqreturn_t
ti_bandgap_tshut_irq_handler(int irq
, void *data
)
239 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
242 orderly_poweroff(true);
247 /*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
250 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
251 * @bgp: struct ti_bandgap pointer
252 * @adc_val: value in ADC representation
253 * @t: address where to write the resulting temperature in mCelsius
255 * Simple conversion from ADC representation to mCelsius. In case the ADC value
256 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
257 * The conversion table is indexed by the ADC values.
259 * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val
260 * argument is out of the ADC conv table range.
263 int ti_bandgap_adc_to_mcelsius(struct ti_bandgap
*bgp
, int adc_val
, int *t
)
265 const struct ti_bandgap_data
*conf
= bgp
->conf
;
268 /* look up for temperature in the table and return the temperature */
269 if (adc_val
< conf
->adc_start_val
|| adc_val
> conf
->adc_end_val
) {
274 *t
= bgp
->conf
->conv_table
[adc_val
- conf
->adc_start_val
];
281 * ti_bandgap_mcelsius_to_adc() - converts a mCelsius value to ADC scale
282 * @bgp: struct ti_bandgap pointer
283 * @temp: value in mCelsius
284 * @adc: address where to write the resulting temperature in ADC representation
286 * Simple conversion from mCelsius to ADC values. In case the temp value
287 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
288 * The conversion table is indexed by the ADC values.
290 * Return: 0 if conversion was successful, else -ERANGE in case the @temp
291 * argument is out of the ADC conv table range.
294 int ti_bandgap_mcelsius_to_adc(struct ti_bandgap
*bgp
, long temp
, int *adc
)
296 const struct ti_bandgap_data
*conf
= bgp
->conf
;
297 const int *conv_table
= bgp
->conf
->conv_table
;
298 int high
, low
, mid
, ret
= 0;
301 high
= conf
->adc_end_val
- conf
->adc_start_val
;
302 mid
= (high
+ low
) / 2;
304 if (temp
< conv_table
[low
] || temp
> conv_table
[high
]) {
310 if (temp
< conv_table
[mid
])
314 mid
= (low
+ high
) / 2;
317 *adc
= conf
->adc_start_val
+ low
;
324 * ti_bandgap_add_hyst() - add hysteresis (in mCelsius) to an ADC value
325 * @bgp: struct ti_bandgap pointer
326 * @adc_val: temperature value in ADC representation
327 * @hyst_val: hysteresis value in mCelsius
328 * @sum: address where to write the resulting temperature (in ADC scale)
330 * Adds an hysteresis value (in mCelsius) to a ADC temperature value.
332 * Return: 0 on success, -ERANGE otherwise.
335 int ti_bandgap_add_hyst(struct ti_bandgap
*bgp
, int adc_val
, int hyst_val
,
341 * Need to add in the mcelsius domain, so we have a temperature
342 * the conv_table range
344 ret
= ti_bandgap_adc_to_mcelsius(bgp
, adc_val
, &temp
);
350 ret
= ti_bandgap_mcelsius_to_adc(bgp
, temp
, sum
);
356 /*** Helper functions handling device Alert/Shutdown signals ***/
359 * ti_bandgap_unmask_interrupts() - unmasks the events of thot & tcold
360 * @bgp: struct ti_bandgap pointer
361 * @id: bandgap sensor id
362 * @t_hot: hot temperature value to trigger alert signal
363 * @t_cold: cold temperature value to trigger alert signal
365 * Checks the requested t_hot and t_cold values and configures the IRQ event
366 * masks accordingly. Call this function only if bandgap features HAS(TALERT).
368 static void ti_bandgap_unmask_interrupts(struct ti_bandgap
*bgp
, int id
,
369 u32 t_hot
, u32 t_cold
)
371 struct temp_sensor_registers
*tsr
;
374 /* Read the current on die temperature */
375 temp
= ti_bandgap_read_temp(bgp
, id
);
377 tsr
= bgp
->conf
->sensors
[id
].registers
;
378 reg_val
= ti_bandgap_readl(bgp
, tsr
->bgap_mask_ctrl
);
381 reg_val
|= tsr
->mask_hot_mask
;
383 reg_val
&= ~tsr
->mask_hot_mask
;
386 reg_val
|= tsr
->mask_cold_mask
;
388 reg_val
&= ~tsr
->mask_cold_mask
;
389 ti_bandgap_writel(bgp
, reg_val
, tsr
->bgap_mask_ctrl
);
393 * ti_bandgap_update_alert_threshold() - sequence to update thresholds
394 * @bgp: struct ti_bandgap pointer
395 * @id: bandgap sensor id
396 * @val: value (ADC) of a new threshold
397 * @hot: desired threshold to be updated. true if threshold hot, false if
400 * It will program the required thresholds (hot and cold) for TALERT signal.
401 * This function can be used to update t_hot or t_cold, depending on @hot value.
402 * It checks the resulting t_hot and t_cold values, based on the new passed @val
403 * and configures the thresholds so that t_hot is always greater than t_cold.
404 * Call this function only if bandgap features HAS(TALERT).
406 * Return: 0 if no error, else corresponding error
408 static int ti_bandgap_update_alert_threshold(struct ti_bandgap
*bgp
, int id
,
411 struct temp_sensor_data
*ts_data
= bgp
->conf
->sensors
[id
].ts_data
;
412 struct temp_sensor_registers
*tsr
;
413 u32 thresh_val
, reg_val
, t_hot
, t_cold
;
416 tsr
= bgp
->conf
->sensors
[id
].registers
;
418 /* obtain the current value */
419 thresh_val
= ti_bandgap_readl(bgp
, tsr
->bgap_threshold
);
420 t_cold
= (thresh_val
& tsr
->threshold_tcold_mask
) >>
421 __ffs(tsr
->threshold_tcold_mask
);
422 t_hot
= (thresh_val
& tsr
->threshold_thot_mask
) >>
423 __ffs(tsr
->threshold_thot_mask
);
429 if (t_cold
> t_hot
) {
431 err
= ti_bandgap_add_hyst(bgp
, t_hot
,
435 err
= ti_bandgap_add_hyst(bgp
, t_cold
,
440 /* write the new threshold values */
441 reg_val
= thresh_val
&
442 ~(tsr
->threshold_thot_mask
| tsr
->threshold_tcold_mask
);
443 reg_val
|= (t_hot
<< __ffs(tsr
->threshold_thot_mask
)) |
444 (t_cold
<< __ffs(tsr
->threshold_tcold_mask
));
445 ti_bandgap_writel(bgp
, reg_val
, tsr
->bgap_threshold
);
448 dev_err(bgp
->dev
, "failed to reprogram thot threshold\n");
453 ti_bandgap_unmask_interrupts(bgp
, id
, t_hot
, t_cold
);
459 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
460 * @bgp: struct ti_bandgap pointer
461 * @id: bandgap sensor id
463 * Checks if the bandgap pointer is valid and if the sensor id is also
466 * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
467 * @id cannot index @bgp sensors.
469 static inline int ti_bandgap_validate(struct ti_bandgap
*bgp
, int id
)
473 if (!bgp
|| IS_ERR(bgp
)) {
474 pr_err("%s: invalid bandgap pointer\n", __func__
);
479 if ((id
< 0) || (id
>= bgp
->conf
->sensor_count
)) {
480 dev_err(bgp
->dev
, "%s: sensor id out of range (%d)\n",
490 * _ti_bandgap_write_threshold() - helper to update TALERT t_cold or t_hot
491 * @bgp: struct ti_bandgap pointer
492 * @id: bandgap sensor id
493 * @val: value (mCelsius) of a new threshold
494 * @hot: desired threshold to be updated. true if threshold hot, false if
497 * It will update the required thresholds (hot and cold) for TALERT signal.
498 * This function can be used to update t_hot or t_cold, depending on @hot value.
499 * Validates the mCelsius range and update the requested threshold.
500 * Call this function only if bandgap features HAS(TALERT).
502 * Return: 0 if no error, else corresponding error value.
504 static int _ti_bandgap_write_threshold(struct ti_bandgap
*bgp
, int id
, int val
,
507 struct temp_sensor_data
*ts_data
;
508 struct temp_sensor_registers
*tsr
;
512 ret
= ti_bandgap_validate(bgp
, id
);
516 if (!TI_BANDGAP_HAS(bgp
, TALERT
)) {
521 ts_data
= bgp
->conf
->sensors
[id
].ts_data
;
522 tsr
= bgp
->conf
->sensors
[id
].registers
;
524 if (val
< ts_data
->min_temp
+ ts_data
->hyst_val
)
527 if (val
> ts_data
->max_temp
+ ts_data
->hyst_val
)
534 ret
= ti_bandgap_mcelsius_to_adc(bgp
, val
, &adc_val
);
538 spin_lock(&bgp
->lock
);
539 ret
= ti_bandgap_update_alert_threshold(bgp
, id
, adc_val
, hot
);
540 spin_unlock(&bgp
->lock
);
547 * _ti_bandgap_read_threshold() - helper to read TALERT t_cold or t_hot
548 * @bgp: struct ti_bandgap pointer
549 * @id: bandgap sensor id
550 * @val: value (mCelsius) of a threshold
551 * @hot: desired threshold to be read. true if threshold hot, false if
554 * It will fetch the required thresholds (hot and cold) for TALERT signal.
555 * This function can be used to read t_hot or t_cold, depending on @hot value.
556 * Call this function only if bandgap features HAS(TALERT).
558 * Return: 0 if no error, -ENOTSUPP if it has no TALERT support, or the
559 * corresponding error value if some operation fails.
561 static int _ti_bandgap_read_threshold(struct ti_bandgap
*bgp
, int id
,
564 struct temp_sensor_registers
*tsr
;
568 ret
= ti_bandgap_validate(bgp
, id
);
572 if (!TI_BANDGAP_HAS(bgp
, TALERT
)) {
577 tsr
= bgp
->conf
->sensors
[id
].registers
;
579 mask
= tsr
->threshold_thot_mask
;
581 mask
= tsr
->threshold_tcold_mask
;
583 temp
= ti_bandgap_readl(bgp
, tsr
->bgap_threshold
);
584 temp
= (temp
& mask
) >> __ffs(mask
);
585 ret
|= ti_bandgap_adc_to_mcelsius(bgp
, temp
, &temp
);
587 dev_err(bgp
->dev
, "failed to read thot\n");
598 /*** Exposed APIs ***/
601 * ti_bandgap_read_thot() - reads sensor current thot
602 * @bgp: pointer to bandgap instance
604 * @thot: resulting current thot value
606 * Return: 0 on success or the proper error code
608 int ti_bandgap_read_thot(struct ti_bandgap
*bgp
, int id
, int *thot
)
610 return _ti_bandgap_read_threshold(bgp
, id
, thot
, true);
614 * ti_bandgap_write_thot() - sets sensor current thot
615 * @bgp: pointer to bandgap instance
617 * @val: desired thot value
619 * Return: 0 on success or the proper error code
621 int ti_bandgap_write_thot(struct ti_bandgap
*bgp
, int id
, int val
)
623 return _ti_bandgap_write_threshold(bgp
, id
, val
, true);
627 * ti_bandgap_read_tcold() - reads sensor current tcold
628 * @bgp: pointer to bandgap instance
630 * @tcold: resulting current tcold value
632 * Return: 0 on success or the proper error code
634 int ti_bandgap_read_tcold(struct ti_bandgap
*bgp
, int id
, int *tcold
)
636 return _ti_bandgap_read_threshold(bgp
, id
, tcold
, false);
640 * ti_bandgap_write_tcold() - sets the sensor tcold
641 * @bgp: pointer to bandgap instance
643 * @val: desired tcold value
645 * Return: 0 on success or the proper error code
647 int ti_bandgap_write_tcold(struct ti_bandgap
*bgp
, int id
, int val
)
649 return _ti_bandgap_write_threshold(bgp
, id
, val
, false);
653 * ti_bandgap_read_counter() - read the sensor counter
654 * @bgp: pointer to bandgap instance
656 * @interval: resulting update interval in miliseconds
658 static void ti_bandgap_read_counter(struct ti_bandgap
*bgp
, int id
,
661 struct temp_sensor_registers
*tsr
;
664 tsr
= bgp
->conf
->sensors
[id
].registers
;
665 time
= ti_bandgap_readl(bgp
, tsr
->bgap_counter
);
666 time
= (time
& tsr
->counter_mask
) >>
667 __ffs(tsr
->counter_mask
);
668 time
= time
* 1000 / bgp
->clk_rate
;
673 * ti_bandgap_read_counter_delay() - read the sensor counter delay
674 * @bgp: pointer to bandgap instance
676 * @interval: resulting update interval in miliseconds
678 static void ti_bandgap_read_counter_delay(struct ti_bandgap
*bgp
, int id
,
681 struct temp_sensor_registers
*tsr
;
684 tsr
= bgp
->conf
->sensors
[id
].registers
;
686 reg_val
= ti_bandgap_readl(bgp
, tsr
->bgap_mask_ctrl
);
687 reg_val
= (reg_val
& tsr
->mask_counter_delay_mask
) >>
688 __ffs(tsr
->mask_counter_delay_mask
);
709 dev_warn(bgp
->dev
, "Wrong counter delay value read from register %X",
715 * ti_bandgap_read_update_interval() - read the sensor update interval
716 * @bgp: pointer to bandgap instance
718 * @interval: resulting update interval in miliseconds
720 * Return: 0 on success or the proper error code
722 int ti_bandgap_read_update_interval(struct ti_bandgap
*bgp
, int id
,
727 ret
= ti_bandgap_validate(bgp
, id
);
731 if (!TI_BANDGAP_HAS(bgp
, COUNTER
) &&
732 !TI_BANDGAP_HAS(bgp
, COUNTER_DELAY
)) {
737 if (TI_BANDGAP_HAS(bgp
, COUNTER
)) {
738 ti_bandgap_read_counter(bgp
, id
, interval
);
742 ti_bandgap_read_counter_delay(bgp
, id
, interval
);
748 * ti_bandgap_write_counter_delay() - set the counter_delay
749 * @bgp: pointer to bandgap instance
751 * @interval: desired update interval in miliseconds
753 * Return: 0 on success or the proper error code
755 static int ti_bandgap_write_counter_delay(struct ti_bandgap
*bgp
, int id
,
761 case 0: /* Immediate conversion */
764 case 1: /* Conversion after ever 1ms */
767 case 10: /* Conversion after ever 10ms */
770 case 100: /* Conversion after ever 100ms */
773 case 250: /* Conversion after ever 250ms */
776 case 500: /* Conversion after ever 500ms */
780 dev_warn(bgp
->dev
, "Delay %d ms is not supported\n", interval
);
784 spin_lock(&bgp
->lock
);
785 RMW_BITS(bgp
, id
, bgap_mask_ctrl
, mask_counter_delay_mask
, rval
);
786 spin_unlock(&bgp
->lock
);
792 * ti_bandgap_write_counter() - set the bandgap sensor counter
793 * @bgp: pointer to bandgap instance
795 * @interval: desired update interval in miliseconds
797 static void ti_bandgap_write_counter(struct ti_bandgap
*bgp
, int id
,
800 interval
= interval
* bgp
->clk_rate
/ 1000;
801 spin_lock(&bgp
->lock
);
802 RMW_BITS(bgp
, id
, bgap_counter
, counter_mask
, interval
);
803 spin_unlock(&bgp
->lock
);
807 * ti_bandgap_write_update_interval() - set the update interval
808 * @bgp: pointer to bandgap instance
810 * @interval: desired update interval in miliseconds
812 * Return: 0 on success or the proper error code
814 int ti_bandgap_write_update_interval(struct ti_bandgap
*bgp
,
815 int id
, u32 interval
)
817 int ret
= ti_bandgap_validate(bgp
, id
);
821 if (!TI_BANDGAP_HAS(bgp
, COUNTER
) &&
822 !TI_BANDGAP_HAS(bgp
, COUNTER_DELAY
)) {
827 if (TI_BANDGAP_HAS(bgp
, COUNTER
)) {
828 ti_bandgap_write_counter(bgp
, id
, interval
);
832 ret
= ti_bandgap_write_counter_delay(bgp
, id
, interval
);
838 * ti_bandgap_read_temperature() - report current temperature
839 * @bgp: pointer to bandgap instance
841 * @temperature: resulting temperature
843 * Return: 0 on success or the proper error code
845 int ti_bandgap_read_temperature(struct ti_bandgap
*bgp
, int id
,
851 ret
= ti_bandgap_validate(bgp
, id
);
855 spin_lock(&bgp
->lock
);
856 temp
= ti_bandgap_read_temp(bgp
, id
);
857 spin_unlock(&bgp
->lock
);
859 ret
|= ti_bandgap_adc_to_mcelsius(bgp
, temp
, &temp
);
869 * ti_bandgap_set_sensor_data() - helper function to store thermal
870 * framework related data.
871 * @bgp: pointer to bandgap instance
873 * @data: thermal framework related data to be stored
875 * Return: 0 on success or the proper error code
877 int ti_bandgap_set_sensor_data(struct ti_bandgap
*bgp
, int id
, void *data
)
879 int ret
= ti_bandgap_validate(bgp
, id
);
883 bgp
->regval
[id
].data
= data
;
889 * ti_bandgap_get_sensor_data() - helper function to get thermal
890 * framework related data.
891 * @bgp: pointer to bandgap instance
894 * Return: data stored by set function with sensor id on success or NULL
896 void *ti_bandgap_get_sensor_data(struct ti_bandgap
*bgp
, int id
)
898 int ret
= ti_bandgap_validate(bgp
, id
);
902 return bgp
->regval
[id
].data
;
905 /*** Helper functions used during device initialization ***/
908 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
909 * @bgp: pointer to struct ti_bandgap
910 * @id: sensor id which it is desired to read 1 temperature
912 * Used to initialize the conversion state machine and set it to a valid
913 * state. Called during device initialization and context restore events.
918 ti_bandgap_force_single_read(struct ti_bandgap
*bgp
, int id
)
920 u32 temp
= 0, counter
= 1000;
922 /* Select single conversion mode */
923 if (TI_BANDGAP_HAS(bgp
, MODE_CONFIG
))
924 RMW_BITS(bgp
, id
, bgap_mode_ctrl
, mode_ctrl_mask
, 0);
926 /* Start of Conversion = 1 */
927 RMW_BITS(bgp
, id
, temp_sensor_ctrl
, bgap_soc_mask
, 1);
928 /* Wait until DTEMP is updated */
929 temp
= ti_bandgap_read_temp(bgp
, id
);
931 while ((temp
== 0) && --counter
)
932 temp
= ti_bandgap_read_temp(bgp
, id
);
933 /* REVISIT: Check correct condition for end of conversion */
935 /* Start of Conversion = 0 */
936 RMW_BITS(bgp
, id
, temp_sensor_ctrl
, bgap_soc_mask
, 0);
942 * ti_bandgap_set_continous_mode() - One time enabling of continuous mode
943 * @bgp: pointer to struct ti_bandgap
945 * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
946 * be used for junction temperature monitoring, it is desirable that the
947 * sensors are operational all the time, so that alerts are generated
952 static int ti_bandgap_set_continuous_mode(struct ti_bandgap
*bgp
)
956 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
957 /* Perform a single read just before enabling continuous */
958 ti_bandgap_force_single_read(bgp
, i
);
959 RMW_BITS(bgp
, i
, bgap_mode_ctrl
, mode_ctrl_mask
, 1);
966 * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
967 * @bgp: pointer to struct ti_bandgap
968 * @id: id of the individual sensor
969 * @trend: Pointer to trend.
971 * This function needs to be called to fetch the temperature trend of a
972 * Particular sensor. The function computes the difference in temperature
973 * w.r.t time. For the bandgaps with built in history buffer the temperatures
974 * are read from the buffer and for those without the Buffer -ENOTSUPP is
977 * Return: 0 if no error, else return corresponding error. If no
978 * error then the trend value is passed on to trend parameter
980 int ti_bandgap_get_trend(struct ti_bandgap
*bgp
, int id
, int *trend
)
982 struct temp_sensor_registers
*tsr
;
983 u32 temp1
, temp2
, reg1
, reg2
;
984 int t1
, t2
, interval
, ret
= 0;
986 ret
= ti_bandgap_validate(bgp
, id
);
990 if (!TI_BANDGAP_HAS(bgp
, HISTORY_BUFFER
) ||
991 !TI_BANDGAP_HAS(bgp
, FREEZE_BIT
)) {
996 spin_lock(&bgp
->lock
);
998 tsr
= bgp
->conf
->sensors
[id
].registers
;
1000 /* Freeze and read the last 2 valid readings */
1001 RMW_BITS(bgp
, id
, bgap_mask_ctrl
, mask_freeze_mask
, 1);
1002 reg1
= tsr
->ctrl_dtemp_1
;
1003 reg2
= tsr
->ctrl_dtemp_2
;
1005 /* read temperature from history buffer */
1006 temp1
= ti_bandgap_readl(bgp
, reg1
);
1007 temp1
&= tsr
->bgap_dtemp_mask
;
1009 temp2
= ti_bandgap_readl(bgp
, reg2
);
1010 temp2
&= tsr
->bgap_dtemp_mask
;
1012 /* Convert from adc values to mCelsius temperature */
1013 ret
= ti_bandgap_adc_to_mcelsius(bgp
, temp1
, &t1
);
1017 ret
= ti_bandgap_adc_to_mcelsius(bgp
, temp2
, &t2
);
1021 /* Fetch the update interval */
1022 ret
= ti_bandgap_read_update_interval(bgp
, id
, &interval
);
1026 /* Set the interval to 1 ms if bandgap counter delay is not set */
1030 *trend
= (t1
- t2
) / interval
;
1032 dev_dbg(bgp
->dev
, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
1036 RMW_BITS(bgp
, id
, bgap_mask_ctrl
, mask_freeze_mask
, 0);
1037 spin_unlock(&bgp
->lock
);
1043 * ti_bandgap_tshut_init() - setup and initialize tshut handling
1044 * @bgp: pointer to struct ti_bandgap
1045 * @pdev: pointer to device struct platform_device
1047 * Call this function only in case the bandgap features HAS(TSHUT).
1048 * In this case, the driver needs to handle the TSHUT signal as an IRQ.
1049 * The IRQ is wired as a GPIO, and for this purpose, it is required
1050 * to specify which GPIO line is used. TSHUT IRQ is fired anytime
1051 * one of the bandgap sensors violates the TSHUT high/hot threshold.
1052 * And in that case, the system must go off.
1054 * Return: 0 if no error, else error status
1056 static int ti_bandgap_tshut_init(struct ti_bandgap
*bgp
,
1057 struct platform_device
*pdev
)
1059 int gpio_nr
= bgp
->tshut_gpio
;
1062 /* Request for gpio_86 line */
1063 status
= gpio_request(gpio_nr
, "tshut");
1065 dev_err(bgp
->dev
, "Could not request for TSHUT GPIO:%i\n", 86);
1068 status
= gpio_direction_input(gpio_nr
);
1070 dev_err(bgp
->dev
, "Cannot set input TSHUT GPIO %d\n", gpio_nr
);
1074 status
= request_irq(gpio_to_irq(gpio_nr
), ti_bandgap_tshut_irq_handler
,
1075 IRQF_TRIGGER_RISING
, "tshut", NULL
);
1078 dev_err(bgp
->dev
, "request irq failed for TSHUT");
1085 * ti_bandgap_alert_init() - setup and initialize talert handling
1086 * @bgp: pointer to struct ti_bandgap
1087 * @pdev: pointer to device struct platform_device
1089 * Call this function only in case the bandgap features HAS(TALERT).
1090 * In this case, the driver needs to handle the TALERT signals as an IRQs.
1091 * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
1092 * are violated. In these situation, the driver must reprogram the thresholds,
1093 * accordingly to specified policy.
1095 * Return: 0 if no error, else return corresponding error.
1097 static int ti_bandgap_talert_init(struct ti_bandgap
*bgp
,
1098 struct platform_device
*pdev
)
1102 bgp
->irq
= platform_get_irq(pdev
, 0);
1104 dev_err(&pdev
->dev
, "get_irq failed\n");
1107 ret
= request_threaded_irq(bgp
->irq
, NULL
,
1108 ti_bandgap_talert_irq_handler
,
1109 IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
,
1112 dev_err(&pdev
->dev
, "Request threaded irq failed.\n");
1119 static const struct of_device_id of_ti_bandgap_match
[];
1121 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
1122 * @pdev: pointer to device struct platform_device
1124 * Used to read the device tree properties accordingly to the bandgap
1125 * matching version. Based on bandgap version and its capabilities it
1126 * will build a struct ti_bandgap out of the required DT entries.
1128 * Return: valid bandgap structure if successful, else returns ERR_PTR
1129 * return value must be verified with IS_ERR.
1131 static struct ti_bandgap
*ti_bandgap_build(struct platform_device
*pdev
)
1133 struct device_node
*node
= pdev
->dev
.of_node
;
1134 const struct of_device_id
*of_id
;
1135 struct ti_bandgap
*bgp
;
1136 struct resource
*res
;
1139 /* just for the sake */
1141 dev_err(&pdev
->dev
, "no platform information available\n");
1142 return ERR_PTR(-EINVAL
);
1145 bgp
= devm_kzalloc(&pdev
->dev
, sizeof(*bgp
), GFP_KERNEL
);
1147 dev_err(&pdev
->dev
, "Unable to allocate mem for driver ref\n");
1148 return ERR_PTR(-ENOMEM
);
1151 of_id
= of_match_device(of_ti_bandgap_match
, &pdev
->dev
);
1153 bgp
->conf
= of_id
->data
;
1155 /* register shadow for context save and restore */
1156 bgp
->regval
= devm_kzalloc(&pdev
->dev
, sizeof(*bgp
->regval
) *
1157 bgp
->conf
->sensor_count
, GFP_KERNEL
);
1159 dev_err(&pdev
->dev
, "Unable to allocate mem for driver ref\n");
1160 return ERR_PTR(-ENOMEM
);
1165 void __iomem
*chunk
;
1167 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, i
);
1170 chunk
= devm_ioremap_resource(&pdev
->dev
, res
);
1174 return ERR_CAST(chunk
);
1179 if (TI_BANDGAP_HAS(bgp
, TSHUT
)) {
1180 bgp
->tshut_gpio
= of_get_gpio(node
, 0);
1181 if (!gpio_is_valid(bgp
->tshut_gpio
)) {
1182 dev_err(&pdev
->dev
, "invalid gpio for tshut (%d)\n",
1184 return ERR_PTR(-EINVAL
);
1191 /*** Device driver call backs ***/
1194 int ti_bandgap_probe(struct platform_device
*pdev
)
1196 struct ti_bandgap
*bgp
;
1197 int clk_rate
, ret
= 0, i
;
1199 bgp
= ti_bandgap_build(pdev
);
1201 dev_err(&pdev
->dev
, "failed to fetch platform data\n");
1202 return PTR_ERR(bgp
);
1204 bgp
->dev
= &pdev
->dev
;
1206 if (TI_BANDGAP_HAS(bgp
, TSHUT
)) {
1207 ret
= ti_bandgap_tshut_init(bgp
, pdev
);
1210 "failed to initialize system tshut IRQ\n");
1215 bgp
->fclock
= clk_get(NULL
, bgp
->conf
->fclock_name
);
1216 ret
= IS_ERR(bgp
->fclock
);
1218 dev_err(&pdev
->dev
, "failed to request fclock reference\n");
1219 ret
= PTR_ERR(bgp
->fclock
);
1223 bgp
->div_clk
= clk_get(NULL
, bgp
->conf
->div_ck_name
);
1224 ret
= IS_ERR(bgp
->div_clk
);
1227 "failed to request div_ts_ck clock ref\n");
1228 ret
= PTR_ERR(bgp
->div_clk
);
1232 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
1233 struct temp_sensor_registers
*tsr
;
1236 tsr
= bgp
->conf
->sensors
[i
].registers
;
1238 * check if the efuse has a non-zero value if not
1239 * it is an untrimmed sample and the temperatures
1240 * may not be accurate
1242 val
= ti_bandgap_readl(bgp
, tsr
->bgap_efuse
);
1244 dev_info(&pdev
->dev
,
1245 "Non-trimmed BGAP, Temp not accurate\n");
1248 clk_rate
= clk_round_rate(bgp
->div_clk
,
1249 bgp
->conf
->sensors
[0].ts_data
->max_freq
);
1250 if (clk_rate
< bgp
->conf
->sensors
[0].ts_data
->min_freq
||
1253 dev_err(&pdev
->dev
, "wrong clock rate (%d)\n", clk_rate
);
1257 ret
= clk_set_rate(bgp
->div_clk
, clk_rate
);
1259 dev_err(&pdev
->dev
, "Cannot re-set clock rate. Continuing\n");
1261 bgp
->clk_rate
= clk_rate
;
1262 if (TI_BANDGAP_HAS(bgp
, CLK_CTRL
))
1263 clk_prepare_enable(bgp
->fclock
);
1266 spin_lock_init(&bgp
->lock
);
1267 bgp
->dev
= &pdev
->dev
;
1268 platform_set_drvdata(pdev
, bgp
);
1270 ti_bandgap_power(bgp
, true);
1272 /* Set default counter to 1 for now */
1273 if (TI_BANDGAP_HAS(bgp
, COUNTER
))
1274 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++)
1275 RMW_BITS(bgp
, i
, bgap_counter
, counter_mask
, 1);
1277 /* Set default thresholds for alert and shutdown */
1278 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
1279 struct temp_sensor_data
*ts_data
;
1281 ts_data
= bgp
->conf
->sensors
[i
].ts_data
;
1283 if (TI_BANDGAP_HAS(bgp
, TALERT
)) {
1284 /* Set initial Talert thresholds */
1285 RMW_BITS(bgp
, i
, bgap_threshold
,
1286 threshold_tcold_mask
, ts_data
->t_cold
);
1287 RMW_BITS(bgp
, i
, bgap_threshold
,
1288 threshold_thot_mask
, ts_data
->t_hot
);
1289 /* Enable the alert events */
1290 RMW_BITS(bgp
, i
, bgap_mask_ctrl
, mask_hot_mask
, 1);
1291 RMW_BITS(bgp
, i
, bgap_mask_ctrl
, mask_cold_mask
, 1);
1294 if (TI_BANDGAP_HAS(bgp
, TSHUT_CONFIG
)) {
1295 /* Set initial Tshut thresholds */
1296 RMW_BITS(bgp
, i
, tshut_threshold
,
1297 tshut_hot_mask
, ts_data
->tshut_hot
);
1298 RMW_BITS(bgp
, i
, tshut_threshold
,
1299 tshut_cold_mask
, ts_data
->tshut_cold
);
1303 if (TI_BANDGAP_HAS(bgp
, MODE_CONFIG
))
1304 ti_bandgap_set_continuous_mode(bgp
);
1306 /* Set .250 seconds time as default counter */
1307 if (TI_BANDGAP_HAS(bgp
, COUNTER
))
1308 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++)
1309 RMW_BITS(bgp
, i
, bgap_counter
, counter_mask
,
1312 /* Every thing is good? Then expose the sensors */
1313 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
1316 if (bgp
->conf
->sensors
[i
].register_cooling
) {
1317 ret
= bgp
->conf
->sensors
[i
].register_cooling(bgp
, i
);
1319 goto remove_sensors
;
1322 if (bgp
->conf
->expose_sensor
) {
1323 domain
= bgp
->conf
->sensors
[i
].domain
;
1324 ret
= bgp
->conf
->expose_sensor(bgp
, i
, domain
);
1326 goto remove_last_cooling
;
1331 * Enable the Interrupts once everything is set. Otherwise irq handler
1332 * might be called as soon as it is enabled where as rest of framework
1333 * is still getting initialised.
1335 if (TI_BANDGAP_HAS(bgp
, TALERT
)) {
1336 ret
= ti_bandgap_talert_init(bgp
, pdev
);
1338 dev_err(&pdev
->dev
, "failed to initialize Talert IRQ\n");
1339 i
= bgp
->conf
->sensor_count
;
1346 remove_last_cooling
:
1347 if (bgp
->conf
->sensors
[i
].unregister_cooling
)
1348 bgp
->conf
->sensors
[i
].unregister_cooling(bgp
, i
);
1350 for (i
--; i
>= 0; i
--) {
1351 if (bgp
->conf
->sensors
[i
].unregister_cooling
)
1352 bgp
->conf
->sensors
[i
].unregister_cooling(bgp
, i
);
1353 if (bgp
->conf
->remove_sensor
)
1354 bgp
->conf
->remove_sensor(bgp
, i
);
1356 ti_bandgap_power(bgp
, false);
1358 if (TI_BANDGAP_HAS(bgp
, CLK_CTRL
))
1359 clk_disable_unprepare(bgp
->fclock
);
1361 clk_put(bgp
->fclock
);
1362 clk_put(bgp
->div_clk
);
1364 if (TI_BANDGAP_HAS(bgp
, TSHUT
)) {
1365 free_irq(gpio_to_irq(bgp
->tshut_gpio
), NULL
);
1366 gpio_free(bgp
->tshut_gpio
);
1373 int ti_bandgap_remove(struct platform_device
*pdev
)
1375 struct ti_bandgap
*bgp
= platform_get_drvdata(pdev
);
1378 /* First thing is to remove sensor interfaces */
1379 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
1380 if (bgp
->conf
->sensors
[i
].unregister_cooling
)
1381 bgp
->conf
->sensors
[i
].unregister_cooling(bgp
, i
);
1383 if (bgp
->conf
->remove_sensor
)
1384 bgp
->conf
->remove_sensor(bgp
, i
);
1387 ti_bandgap_power(bgp
, false);
1389 if (TI_BANDGAP_HAS(bgp
, CLK_CTRL
))
1390 clk_disable_unprepare(bgp
->fclock
);
1391 clk_put(bgp
->fclock
);
1392 clk_put(bgp
->div_clk
);
1394 if (TI_BANDGAP_HAS(bgp
, TALERT
))
1395 free_irq(bgp
->irq
, bgp
);
1397 if (TI_BANDGAP_HAS(bgp
, TSHUT
)) {
1398 free_irq(gpio_to_irq(bgp
->tshut_gpio
), NULL
);
1399 gpio_free(bgp
->tshut_gpio
);
1406 static int ti_bandgap_save_ctxt(struct ti_bandgap
*bgp
)
1410 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
1411 struct temp_sensor_registers
*tsr
;
1412 struct temp_sensor_regval
*rval
;
1414 rval
= &bgp
->regval
[i
];
1415 tsr
= bgp
->conf
->sensors
[i
].registers
;
1417 if (TI_BANDGAP_HAS(bgp
, MODE_CONFIG
))
1418 rval
->bg_mode_ctrl
= ti_bandgap_readl(bgp
,
1419 tsr
->bgap_mode_ctrl
);
1420 if (TI_BANDGAP_HAS(bgp
, COUNTER
))
1421 rval
->bg_counter
= ti_bandgap_readl(bgp
,
1423 if (TI_BANDGAP_HAS(bgp
, TALERT
)) {
1424 rval
->bg_threshold
= ti_bandgap_readl(bgp
,
1425 tsr
->bgap_threshold
);
1426 rval
->bg_ctrl
= ti_bandgap_readl(bgp
,
1427 tsr
->bgap_mask_ctrl
);
1430 if (TI_BANDGAP_HAS(bgp
, TSHUT_CONFIG
))
1431 rval
->tshut_threshold
= ti_bandgap_readl(bgp
,
1432 tsr
->tshut_threshold
);
1438 static int ti_bandgap_restore_ctxt(struct ti_bandgap
*bgp
)
1442 for (i
= 0; i
< bgp
->conf
->sensor_count
; i
++) {
1443 struct temp_sensor_registers
*tsr
;
1444 struct temp_sensor_regval
*rval
;
1447 rval
= &bgp
->regval
[i
];
1448 tsr
= bgp
->conf
->sensors
[i
].registers
;
1450 if (TI_BANDGAP_HAS(bgp
, COUNTER
))
1451 val
= ti_bandgap_readl(bgp
, tsr
->bgap_counter
);
1453 if (TI_BANDGAP_HAS(bgp
, TSHUT_CONFIG
))
1454 ti_bandgap_writel(bgp
, rval
->tshut_threshold
,
1455 tsr
->tshut_threshold
);
1456 /* Force immediate temperature measurement and update
1457 * of the DTEMP field
1459 ti_bandgap_force_single_read(bgp
, i
);
1461 if (TI_BANDGAP_HAS(bgp
, COUNTER
))
1462 ti_bandgap_writel(bgp
, rval
->bg_counter
,
1464 if (TI_BANDGAP_HAS(bgp
, MODE_CONFIG
))
1465 ti_bandgap_writel(bgp
, rval
->bg_mode_ctrl
,
1466 tsr
->bgap_mode_ctrl
);
1467 if (TI_BANDGAP_HAS(bgp
, TALERT
)) {
1468 ti_bandgap_writel(bgp
, rval
->bg_threshold
,
1469 tsr
->bgap_threshold
);
1470 ti_bandgap_writel(bgp
, rval
->bg_ctrl
,
1471 tsr
->bgap_mask_ctrl
);
1478 static int ti_bandgap_suspend(struct device
*dev
)
1480 struct ti_bandgap
*bgp
= dev_get_drvdata(dev
);
1483 err
= ti_bandgap_save_ctxt(bgp
);
1484 ti_bandgap_power(bgp
, false);
1486 if (TI_BANDGAP_HAS(bgp
, CLK_CTRL
))
1487 clk_disable_unprepare(bgp
->fclock
);
1492 static int ti_bandgap_resume(struct device
*dev
)
1494 struct ti_bandgap
*bgp
= dev_get_drvdata(dev
);
1496 if (TI_BANDGAP_HAS(bgp
, CLK_CTRL
))
1497 clk_prepare_enable(bgp
->fclock
);
1499 ti_bandgap_power(bgp
, true);
1501 return ti_bandgap_restore_ctxt(bgp
);
1503 static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops
, ti_bandgap_suspend
,
1506 #define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
1508 #define DEV_PM_OPS NULL
1511 static const struct of_device_id of_ti_bandgap_match
[] = {
1512 #ifdef CONFIG_OMAP4_THERMAL
1514 .compatible
= "ti,omap4430-bandgap",
1515 .data
= (void *)&omap4430_data
,
1518 .compatible
= "ti,omap4460-bandgap",
1519 .data
= (void *)&omap4460_data
,
1522 .compatible
= "ti,omap4470-bandgap",
1523 .data
= (void *)&omap4470_data
,
1526 #ifdef CONFIG_OMAP5_THERMAL
1528 .compatible
= "ti,omap5430-bandgap",
1529 .data
= (void *)&omap5430_data
,
1532 #ifdef CONFIG_DRA752_THERMAL
1534 .compatible
= "ti,dra752-bandgap",
1535 .data
= (void *)&dra752_data
,
1541 MODULE_DEVICE_TABLE(of
, of_ti_bandgap_match
);
1543 static struct platform_driver ti_bandgap_sensor_driver
= {
1544 .probe
= ti_bandgap_probe
,
1545 .remove
= ti_bandgap_remove
,
1547 .name
= "ti-soc-thermal",
1549 .of_match_table
= of_ti_bandgap_match
,
1553 module_platform_driver(ti_bandgap_sensor_driver
);
1555 MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1556 MODULE_LICENSE("GPL v2");
1557 MODULE_ALIAS("platform:ti-soc-thermal");
1558 MODULE_AUTHOR("Texas Instrument Inc.");