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_errata814_bandgap_read_temp() - helper function to read dra7 sensor temperature
123 * @bgp: pointer to ti_bandgap structure
124 * @reg: desired register (offset) to be read
126 * Function to read dra7 bandgap sensor temperature. This is done separately
127 * so as to workaround the errata "Bandgap Temperature read Dtemp can be
128 * corrupted" - Errata ID: i814".
129 * Read accesses to registers listed below can be corrupted due to incorrect
130 * resynchronization between clock domains.
131 * Read access to registers below can be corrupted :
132 * CTRL_CORE_DTEMP_MPU/GPU/CORE/DSPEVE/IVA_n (n = 0 to 4)
133 * CTRL_CORE_TEMP_SENSOR_MPU/GPU/CORE/DSPEVE/IVA_n
135 * Return: the register value.
137 static u32 ti_errata814_bandgap_read_temp(struct ti_bandgap *bgp, u32 reg)
141 val1 = ti_bandgap_readl(bgp, reg);
142 val2 = ti_bandgap_readl(bgp, reg);
144 /* If both times we read the same value then that is right */
148 /* if val1 and val2 are different read it third time */
149 return ti_bandgap_readl(bgp, reg);
153 * ti_bandgap_read_temp() - helper function to read sensor temperature
154 * @bgp: pointer to ti_bandgap structure
155 * @id: bandgap sensor id
157 * Function to concentrate the steps to read sensor temperature register.
158 * This function is desired because, depending on bandgap device version,
159 * it might be needed to freeze the bandgap state machine, before fetching
160 * the register value.
162 * Return: temperature in ADC values.
164 static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
166 struct temp_sensor_registers *tsr;
169 tsr = bgp->conf->sensors[id].registers;
170 reg = tsr->temp_sensor_ctrl;
172 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
173 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
175 * In case we cannot read from cur_dtemp / dtemp_0,
176 * then we read from the last valid temp read
178 reg = tsr->ctrl_dtemp_1;
181 /* read temperature */
182 if (TI_BANDGAP_HAS(bgp, ERRATA_814))
183 temp = ti_errata814_bandgap_read_temp(bgp, reg);
185 temp = ti_bandgap_readl(bgp, reg);
187 temp &= tsr->bgap_dtemp_mask;
189 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
190 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
195 /*** IRQ handlers ***/
198 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
200 * @data: private data (struct ti_bandgap *)
202 * This is the Talert handler. Use it only if bandgap device features
203 * HAS(TALERT). This handler goes over all sensors and checks their
204 * conditions and acts accordingly. In case there are events pending,
205 * it will reset the event mask to wait for the opposite event (next event).
206 * Every time there is a new event, it will be reported to thermal layer.
208 * Return: IRQ_HANDLED
210 static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
212 struct ti_bandgap *bgp = data;
213 struct temp_sensor_registers *tsr;
214 u32 t_hot = 0, t_cold = 0, ctrl;
217 spin_lock(&bgp->lock);
218 for (i = 0; i < bgp->conf->sensor_count; i++) {
219 tsr = bgp->conf->sensors[i].registers;
220 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
222 /* Read the status of t_hot */
223 t_hot = ctrl & tsr->status_hot_mask;
225 /* Read the status of t_cold */
226 t_cold = ctrl & tsr->status_cold_mask;
228 if (!t_cold && !t_hot)
231 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
233 * One TALERT interrupt: Two sources
234 * If the interrupt is due to t_hot then mask t_hot and
235 * and unmask t_cold else mask t_cold and unmask t_hot
238 ctrl &= ~tsr->mask_hot_mask;
239 ctrl |= tsr->mask_cold_mask;
241 ctrl &= ~tsr->mask_cold_mask;
242 ctrl |= tsr->mask_hot_mask;
245 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
248 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
249 __func__, bgp->conf->sensors[i].domain,
252 /* report temperature to whom may concern */
253 if (bgp->conf->report_temperature)
254 bgp->conf->report_temperature(bgp, i);
256 spin_unlock(&bgp->lock);
262 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
264 * @data: private data (unused)
266 * This is the Tshut handler. Use it only if bandgap device features
267 * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
270 * Return: IRQ_HANDLED
272 static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
274 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
277 orderly_poweroff(true);
282 /*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
285 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
286 * @bgp: struct ti_bandgap pointer
287 * @adc_val: value in ADC representation
288 * @t: address where to write the resulting temperature in mCelsius
290 * Simple conversion from ADC representation to mCelsius. In case the ADC value
291 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
292 * The conversion table is indexed by the ADC values.
294 * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val
295 * argument is out of the ADC conv table range.
298 int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
300 const struct ti_bandgap_data *conf = bgp->conf;
303 /* look up for temperature in the table and return the temperature */
304 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val) {
309 *t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
316 * ti_bandgap_mcelsius_to_adc() - converts a mCelsius value to ADC scale
317 * @bgp: struct ti_bandgap pointer
318 * @temp: value in mCelsius
319 * @adc: address where to write the resulting temperature in ADC representation
321 * Simple conversion from mCelsius to ADC values. In case the temp value
322 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
323 * The conversion table is indexed by the ADC values.
325 * Return: 0 if conversion was successful, else -ERANGE in case the @temp
326 * argument is out of the ADC conv table range.
329 int ti_bandgap_mcelsius_to_adc(struct ti_bandgap *bgp, long temp, int *adc)
331 const struct ti_bandgap_data *conf = bgp->conf;
332 const int *conv_table = bgp->conf->conv_table;
333 int high, low, mid, ret = 0;
336 high = conf->adc_end_val - conf->adc_start_val;
337 mid = (high + low) / 2;
339 if (temp < conv_table[low] || temp > conv_table[high]) {
345 if (temp < conv_table[mid])
349 mid = (low + high) / 2;
352 *adc = conf->adc_start_val + low;
359 * ti_bandgap_add_hyst() - add hysteresis (in mCelsius) to an ADC value
360 * @bgp: struct ti_bandgap pointer
361 * @adc_val: temperature value in ADC representation
362 * @hyst_val: hysteresis value in mCelsius
363 * @sum: address where to write the resulting temperature (in ADC scale)
365 * Adds an hysteresis value (in mCelsius) to a ADC temperature value.
367 * Return: 0 on success, -ERANGE otherwise.
370 int ti_bandgap_add_hyst(struct ti_bandgap *bgp, int adc_val, int hyst_val,
376 * Need to add in the mcelsius domain, so we have a temperature
377 * the conv_table range
379 ret = ti_bandgap_adc_to_mcelsius(bgp, adc_val, &temp);
385 ret = ti_bandgap_mcelsius_to_adc(bgp, temp, sum);
391 /*** Helper functions handling device Alert/Shutdown signals ***/
394 * ti_bandgap_unmask_interrupts() - unmasks the events of thot & tcold
395 * @bgp: struct ti_bandgap pointer
396 * @id: bandgap sensor id
397 * @t_hot: hot temperature value to trigger alert signal
398 * @t_cold: cold temperature value to trigger alert signal
400 * Checks the requested t_hot and t_cold values and configures the IRQ event
401 * masks accordingly. Call this function only if bandgap features HAS(TALERT).
403 static void ti_bandgap_unmask_interrupts(struct ti_bandgap *bgp, int id,
404 u32 t_hot, u32 t_cold)
406 struct temp_sensor_registers *tsr;
409 /* Read the current on die temperature */
410 temp = ti_bandgap_read_temp(bgp, id);
412 tsr = bgp->conf->sensors[id].registers;
413 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
416 reg_val |= tsr->mask_hot_mask;
418 reg_val &= ~tsr->mask_hot_mask;
421 reg_val |= tsr->mask_cold_mask;
423 reg_val &= ~tsr->mask_cold_mask;
424 ti_bandgap_writel(bgp, reg_val, tsr->bgap_mask_ctrl);
428 * ti_bandgap_update_alert_threshold() - sequence to update thresholds
429 * @bgp: struct ti_bandgap pointer
430 * @id: bandgap sensor id
431 * @val: value (ADC) of a new threshold
432 * @hot: desired threshold to be updated. true if threshold hot, false if
435 * It will program the required thresholds (hot and cold) for TALERT signal.
436 * This function can be used to update t_hot or t_cold, depending on @hot value.
437 * It checks the resulting t_hot and t_cold values, based on the new passed @val
438 * and configures the thresholds so that t_hot is always greater than t_cold.
439 * Call this function only if bandgap features HAS(TALERT).
441 * Return: 0 if no error, else corresponding error
443 static int ti_bandgap_update_alert_threshold(struct ti_bandgap *bgp, int id,
446 struct temp_sensor_data *ts_data = bgp->conf->sensors[id].ts_data;
447 struct temp_sensor_registers *tsr;
448 u32 thresh_val, reg_val, t_hot, t_cold, ctrl;
451 tsr = bgp->conf->sensors[id].registers;
453 /* obtain the current value */
454 thresh_val = ti_bandgap_readl(bgp, tsr->bgap_threshold);
455 t_cold = (thresh_val & tsr->threshold_tcold_mask) >>
456 __ffs(tsr->threshold_tcold_mask);
457 t_hot = (thresh_val & tsr->threshold_thot_mask) >>
458 __ffs(tsr->threshold_thot_mask);
464 if (t_cold > t_hot) {
466 err = ti_bandgap_add_hyst(bgp, t_hot,
470 err = ti_bandgap_add_hyst(bgp, t_cold,
475 /* write the new threshold values */
476 reg_val = thresh_val &
477 ~(tsr->threshold_thot_mask | tsr->threshold_tcold_mask);
478 reg_val |= (t_hot << __ffs(tsr->threshold_thot_mask)) |
479 (t_cold << __ffs(tsr->threshold_tcold_mask));
483 * Spurious Thermal Alert: Talert can happen randomly while the device
484 * remains under the temperature limit defined for this event to trig.
485 * This spurious event is caused by a incorrect re-synchronization
486 * between clock domains. The comparison between configured threshold
487 * and current temperature value can happen while the value is
488 * transitioning (metastable), thus causing inappropriate event
489 * generation. No spurious event occurs as long as the threshold value
490 * stays unchanged. Spurious event can be generated while a thermal
491 * alert threshold is modified in
492 * CONTROL_BANDGAP_THRESHOLD_MPU/GPU/CORE/DSPEVE/IVA_n.
495 if (TI_BANDGAP_HAS(bgp, ERRATA_813)) {
496 /* Mask t_hot and t_cold events at the IP Level */
497 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
500 ctrl &= ~tsr->mask_hot_mask;
502 ctrl &= ~tsr->mask_cold_mask;
504 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
507 /* Write the threshold value */
508 ti_bandgap_writel(bgp, reg_val, tsr->bgap_threshold);
510 if (TI_BANDGAP_HAS(bgp, ERRATA_813)) {
511 /* Unmask t_hot and t_cold events at the IP Level */
512 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
514 ctrl |= tsr->mask_hot_mask;
516 ctrl |= tsr->mask_cold_mask;
518 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
522 dev_err(bgp->dev, "failed to reprogram thot threshold\n");
527 ti_bandgap_unmask_interrupts(bgp, id, t_hot, t_cold);
533 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
534 * @bgp: struct ti_bandgap pointer
535 * @id: bandgap sensor id
537 * Checks if the bandgap pointer is valid and if the sensor id is also
540 * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
541 * @id cannot index @bgp sensors.
543 static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
547 if (!bgp || IS_ERR(bgp)) {
548 pr_err("%s: invalid bandgap pointer\n", __func__);
553 if ((id < 0) || (id >= bgp->conf->sensor_count)) {
554 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
564 * _ti_bandgap_write_threshold() - helper to update TALERT t_cold or t_hot
565 * @bgp: struct ti_bandgap pointer
566 * @id: bandgap sensor id
567 * @val: value (mCelsius) of a new threshold
568 * @hot: desired threshold to be updated. true if threshold hot, false if
571 * It will update the required thresholds (hot and cold) for TALERT signal.
572 * This function can be used to update t_hot or t_cold, depending on @hot value.
573 * Validates the mCelsius range and update the requested threshold.
574 * Call this function only if bandgap features HAS(TALERT).
576 * Return: 0 if no error, else corresponding error value.
578 static int _ti_bandgap_write_threshold(struct ti_bandgap *bgp, int id, int val,
581 struct temp_sensor_data *ts_data;
582 struct temp_sensor_registers *tsr;
586 ret = ti_bandgap_validate(bgp, id);
590 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
595 ts_data = bgp->conf->sensors[id].ts_data;
596 tsr = bgp->conf->sensors[id].registers;
598 if (val < ts_data->min_temp + ts_data->hyst_val)
601 if (val > ts_data->max_temp + ts_data->hyst_val)
608 ret = ti_bandgap_mcelsius_to_adc(bgp, val, &adc_val);
612 spin_lock(&bgp->lock);
613 ret = ti_bandgap_update_alert_threshold(bgp, id, adc_val, hot);
614 spin_unlock(&bgp->lock);
621 * _ti_bandgap_read_threshold() - helper to read TALERT t_cold or t_hot
622 * @bgp: struct ti_bandgap pointer
623 * @id: bandgap sensor id
624 * @val: value (mCelsius) of a threshold
625 * @hot: desired threshold to be read. true if threshold hot, false if
628 * It will fetch the required thresholds (hot and cold) for TALERT signal.
629 * This function can be used to read t_hot or t_cold, depending on @hot value.
630 * Call this function only if bandgap features HAS(TALERT).
632 * Return: 0 if no error, -ENOTSUPP if it has no TALERT support, or the
633 * corresponding error value if some operation fails.
635 static int _ti_bandgap_read_threshold(struct ti_bandgap *bgp, int id,
638 struct temp_sensor_registers *tsr;
642 ret = ti_bandgap_validate(bgp, id);
646 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
651 tsr = bgp->conf->sensors[id].registers;
653 mask = tsr->threshold_thot_mask;
655 mask = tsr->threshold_tcold_mask;
657 temp = ti_bandgap_readl(bgp, tsr->bgap_threshold);
658 temp = (temp & mask) >> __ffs(mask);
659 ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
661 dev_err(bgp->dev, "failed to read thot\n");
672 /*** Exposed APIs ***/
675 * ti_bandgap_read_thot() - reads sensor current thot
676 * @bgp: pointer to bandgap instance
678 * @thot: resulting current thot value
680 * Return: 0 on success or the proper error code
682 int ti_bandgap_read_thot(struct ti_bandgap *bgp, int id, int *thot)
684 return _ti_bandgap_read_threshold(bgp, id, thot, true);
688 * ti_bandgap_write_thot() - sets sensor current thot
689 * @bgp: pointer to bandgap instance
691 * @val: desired thot value
693 * Return: 0 on success or the proper error code
695 int ti_bandgap_write_thot(struct ti_bandgap *bgp, int id, int val)
697 return _ti_bandgap_write_threshold(bgp, id, val, true);
701 * ti_bandgap_read_tcold() - reads sensor current tcold
702 * @bgp: pointer to bandgap instance
704 * @tcold: resulting current tcold value
706 * Return: 0 on success or the proper error code
708 int ti_bandgap_read_tcold(struct ti_bandgap *bgp, int id, int *tcold)
710 return _ti_bandgap_read_threshold(bgp, id, tcold, false);
714 * ti_bandgap_write_tcold() - sets the sensor tcold
715 * @bgp: pointer to bandgap instance
717 * @val: desired tcold value
719 * Return: 0 on success or the proper error code
721 int ti_bandgap_write_tcold(struct ti_bandgap *bgp, int id, int val)
723 return _ti_bandgap_write_threshold(bgp, id, val, false);
727 * ti_bandgap_read_counter() - read the sensor counter
728 * @bgp: pointer to bandgap instance
730 * @interval: resulting update interval in miliseconds
732 static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id,
735 struct temp_sensor_registers *tsr;
738 tsr = bgp->conf->sensors[id].registers;
739 time = ti_bandgap_readl(bgp, tsr->bgap_counter);
740 time = (time & tsr->counter_mask) >>
741 __ffs(tsr->counter_mask);
742 time = time * 1000 / bgp->clk_rate;
747 * ti_bandgap_read_counter_delay() - read the sensor counter delay
748 * @bgp: pointer to bandgap instance
750 * @interval: resulting update interval in miliseconds
752 static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id,
755 struct temp_sensor_registers *tsr;
758 tsr = bgp->conf->sensors[id].registers;
760 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
761 reg_val = (reg_val & tsr->mask_counter_delay_mask) >>
762 __ffs(tsr->mask_counter_delay_mask);
783 dev_warn(bgp->dev, "Wrong counter delay value read from register %X",
789 * ti_bandgap_read_update_interval() - read the sensor update interval
790 * @bgp: pointer to bandgap instance
792 * @interval: resulting update interval in miliseconds
794 * Return: 0 on success or the proper error code
796 int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
801 ret = ti_bandgap_validate(bgp, id);
805 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
806 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
811 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
812 ti_bandgap_read_counter(bgp, id, interval);
816 ti_bandgap_read_counter_delay(bgp, id, interval);
822 * ti_bandgap_write_counter_delay() - set the counter_delay
823 * @bgp: pointer to bandgap instance
825 * @interval: desired update interval in miliseconds
827 * Return: 0 on success or the proper error code
829 static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id,
835 case 0: /* Immediate conversion */
838 case 1: /* Conversion after ever 1ms */
841 case 10: /* Conversion after ever 10ms */
844 case 100: /* Conversion after ever 100ms */
847 case 250: /* Conversion after ever 250ms */
850 case 500: /* Conversion after ever 500ms */
854 dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval);
858 spin_lock(&bgp->lock);
859 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval);
860 spin_unlock(&bgp->lock);
866 * ti_bandgap_write_counter() - set the bandgap sensor counter
867 * @bgp: pointer to bandgap instance
869 * @interval: desired update interval in miliseconds
871 static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id,
874 interval = interval * bgp->clk_rate / 1000;
875 spin_lock(&bgp->lock);
876 RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
877 spin_unlock(&bgp->lock);
881 * ti_bandgap_write_update_interval() - set the update interval
882 * @bgp: pointer to bandgap instance
884 * @interval: desired update interval in miliseconds
886 * Return: 0 on success or the proper error code
888 int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
889 int id, u32 interval)
891 int ret = ti_bandgap_validate(bgp, id);
895 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
896 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
901 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
902 ti_bandgap_write_counter(bgp, id, interval);
906 ret = ti_bandgap_write_counter_delay(bgp, id, interval);
912 * ti_bandgap_read_temperature() - report current temperature
913 * @bgp: pointer to bandgap instance
915 * @temperature: resulting temperature
917 * Return: 0 on success or the proper error code
919 int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
925 ret = ti_bandgap_validate(bgp, id);
929 spin_lock(&bgp->lock);
930 temp = ti_bandgap_read_temp(bgp, id);
931 spin_unlock(&bgp->lock);
933 ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
943 * ti_bandgap_set_sensor_data() - helper function to store thermal
944 * framework related data.
945 * @bgp: pointer to bandgap instance
947 * @data: thermal framework related data to be stored
949 * Return: 0 on success or the proper error code
951 int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
953 int ret = ti_bandgap_validate(bgp, id);
957 bgp->regval[id].data = data;
963 * ti_bandgap_get_sensor_data() - helper function to get thermal
964 * framework related data.
965 * @bgp: pointer to bandgap instance
968 * Return: data stored by set function with sensor id on success or NULL
970 void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
972 int ret = ti_bandgap_validate(bgp, id);
976 return bgp->regval[id].data;
979 /*** Helper functions used during device initialization ***/
982 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
983 * @bgp: pointer to struct ti_bandgap
984 * @id: sensor id which it is desired to read 1 temperature
986 * Used to initialize the conversion state machine and set it to a valid
987 * state. Called during device initialization and context restore events.
992 ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
994 u32 temp = 0, counter = 1000;
996 /* Select single conversion mode */
997 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
998 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
1000 /* Start of Conversion = 1 */
1001 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
1002 /* Wait until DTEMP is updated */
1003 temp = ti_bandgap_read_temp(bgp, id);
1005 while ((temp == 0) && --counter)
1006 temp = ti_bandgap_read_temp(bgp, id);
1007 /* REVISIT: Check correct condition for end of conversion */
1009 /* Start of Conversion = 0 */
1010 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
1016 * ti_bandgap_set_continous_mode() - One time enabling of continuous mode
1017 * @bgp: pointer to struct ti_bandgap
1019 * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
1020 * be used for junction temperature monitoring, it is desirable that the
1021 * sensors are operational all the time, so that alerts are generated
1026 static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
1030 for (i = 0; i < bgp->conf->sensor_count; i++) {
1031 /* Perform a single read just before enabling continuous */
1032 ti_bandgap_force_single_read(bgp, i);
1033 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
1040 * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
1041 * @bgp: pointer to struct ti_bandgap
1042 * @id: id of the individual sensor
1043 * @trend: Pointer to trend.
1045 * This function needs to be called to fetch the temperature trend of a
1046 * Particular sensor. The function computes the difference in temperature
1047 * w.r.t time. For the bandgaps with built in history buffer the temperatures
1048 * are read from the buffer and for those without the Buffer -ENOTSUPP is
1051 * Return: 0 if no error, else return corresponding error. If no
1052 * error then the trend value is passed on to trend parameter
1054 int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend)
1056 struct temp_sensor_registers *tsr;
1057 u32 temp1, temp2, reg1, reg2;
1058 int t1, t2, interval, ret = 0;
1060 ret = ti_bandgap_validate(bgp, id);
1064 if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) ||
1065 !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
1070 spin_lock(&bgp->lock);
1072 tsr = bgp->conf->sensors[id].registers;
1074 /* Freeze and read the last 2 valid readings */
1075 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
1076 reg1 = tsr->ctrl_dtemp_1;
1077 reg2 = tsr->ctrl_dtemp_2;
1079 /* read temperature from history buffer */
1080 temp1 = ti_bandgap_readl(bgp, reg1);
1081 temp1 &= tsr->bgap_dtemp_mask;
1083 temp2 = ti_bandgap_readl(bgp, reg2);
1084 temp2 &= tsr->bgap_dtemp_mask;
1086 /* Convert from adc values to mCelsius temperature */
1087 ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1);
1091 ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2);
1095 /* Fetch the update interval */
1096 ret = ti_bandgap_read_update_interval(bgp, id, &interval);
1100 /* Set the interval to 1 ms if bandgap counter delay is not set */
1104 *trend = (t1 - t2) / interval;
1106 dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
1110 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
1111 spin_unlock(&bgp->lock);
1117 * ti_bandgap_tshut_init() - setup and initialize tshut handling
1118 * @bgp: pointer to struct ti_bandgap
1119 * @pdev: pointer to device struct platform_device
1121 * Call this function only in case the bandgap features HAS(TSHUT).
1122 * In this case, the driver needs to handle the TSHUT signal as an IRQ.
1123 * The IRQ is wired as a GPIO, and for this purpose, it is required
1124 * to specify which GPIO line is used. TSHUT IRQ is fired anytime
1125 * one of the bandgap sensors violates the TSHUT high/hot threshold.
1126 * And in that case, the system must go off.
1128 * Return: 0 if no error, else error status
1130 static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
1131 struct platform_device *pdev)
1133 int gpio_nr = bgp->tshut_gpio;
1136 /* Request for gpio_86 line */
1137 status = gpio_request(gpio_nr, "tshut");
1139 dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86);
1142 status = gpio_direction_input(gpio_nr);
1144 dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr);
1148 status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler,
1149 IRQF_TRIGGER_RISING, "tshut", NULL);
1152 dev_err(bgp->dev, "request irq failed for TSHUT");
1159 * ti_bandgap_alert_init() - setup and initialize talert handling
1160 * @bgp: pointer to struct ti_bandgap
1161 * @pdev: pointer to device struct platform_device
1163 * Call this function only in case the bandgap features HAS(TALERT).
1164 * In this case, the driver needs to handle the TALERT signals as an IRQs.
1165 * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
1166 * are violated. In these situation, the driver must reprogram the thresholds,
1167 * accordingly to specified policy.
1169 * Return: 0 if no error, else return corresponding error.
1171 static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
1172 struct platform_device *pdev)
1176 bgp->irq = platform_get_irq(pdev, 0);
1178 dev_err(&pdev->dev, "get_irq failed\n");
1181 ret = request_threaded_irq(bgp->irq, NULL,
1182 ti_bandgap_talert_irq_handler,
1183 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
1186 dev_err(&pdev->dev, "Request threaded irq failed.\n");
1193 static const struct of_device_id of_ti_bandgap_match[];
1195 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
1196 * @pdev: pointer to device struct platform_device
1198 * Used to read the device tree properties accordingly to the bandgap
1199 * matching version. Based on bandgap version and its capabilities it
1200 * will build a struct ti_bandgap out of the required DT entries.
1202 * Return: valid bandgap structure if successful, else returns ERR_PTR
1203 * return value must be verified with IS_ERR.
1205 static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
1207 struct device_node *node = pdev->dev.of_node;
1208 const struct of_device_id *of_id;
1209 struct ti_bandgap *bgp;
1210 struct resource *res;
1213 /* just for the sake */
1215 dev_err(&pdev->dev, "no platform information available\n");
1216 return ERR_PTR(-EINVAL);
1219 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
1221 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
1222 return ERR_PTR(-ENOMEM);
1225 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
1227 bgp->conf = of_id->data;
1229 /* register shadow for context save and restore */
1230 bgp->regval = devm_kzalloc(&pdev->dev, sizeof(*bgp->regval) *
1231 bgp->conf->sensor_count, GFP_KERNEL);
1233 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
1234 return ERR_PTR(-ENOMEM);
1239 void __iomem *chunk;
1241 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
1244 chunk = devm_ioremap_resource(&pdev->dev, res);
1248 return ERR_CAST(chunk);
1253 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1254 bgp->tshut_gpio = of_get_gpio(node, 0);
1255 if (!gpio_is_valid(bgp->tshut_gpio)) {
1256 dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
1258 return ERR_PTR(-EINVAL);
1265 /*** Device driver call backs ***/
1268 int ti_bandgap_probe(struct platform_device *pdev)
1270 struct ti_bandgap *bgp;
1271 int clk_rate, ret = 0, i;
1273 bgp = ti_bandgap_build(pdev);
1275 dev_err(&pdev->dev, "failed to fetch platform data\n");
1276 return PTR_ERR(bgp);
1278 bgp->dev = &pdev->dev;
1280 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1281 ret = ti_bandgap_tshut_init(bgp, pdev);
1284 "failed to initialize system tshut IRQ\n");
1289 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
1290 ret = IS_ERR(bgp->fclock);
1292 dev_err(&pdev->dev, "failed to request fclock reference\n");
1293 ret = PTR_ERR(bgp->fclock);
1297 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
1298 ret = IS_ERR(bgp->div_clk);
1301 "failed to request div_ts_ck clock ref\n");
1302 ret = PTR_ERR(bgp->div_clk);
1306 for (i = 0; i < bgp->conf->sensor_count; i++) {
1307 struct temp_sensor_registers *tsr;
1310 tsr = bgp->conf->sensors[i].registers;
1312 * check if the efuse has a non-zero value if not
1313 * it is an untrimmed sample and the temperatures
1314 * may not be accurate
1316 val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
1318 dev_info(&pdev->dev,
1319 "Non-trimmed BGAP, Temp not accurate\n");
1322 clk_rate = clk_round_rate(bgp->div_clk,
1323 bgp->conf->sensors[0].ts_data->max_freq);
1324 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
1327 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
1331 ret = clk_set_rate(bgp->div_clk, clk_rate);
1333 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
1335 bgp->clk_rate = clk_rate;
1336 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1337 clk_prepare_enable(bgp->fclock);
1340 spin_lock_init(&bgp->lock);
1341 bgp->dev = &pdev->dev;
1342 platform_set_drvdata(pdev, bgp);
1344 ti_bandgap_power(bgp, true);
1346 /* Set default counter to 1 for now */
1347 if (TI_BANDGAP_HAS(bgp, COUNTER))
1348 for (i = 0; i < bgp->conf->sensor_count; i++)
1349 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
1351 /* Set default thresholds for alert and shutdown */
1352 for (i = 0; i < bgp->conf->sensor_count; i++) {
1353 struct temp_sensor_data *ts_data;
1355 ts_data = bgp->conf->sensors[i].ts_data;
1357 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1358 /* Set initial Talert thresholds */
1359 RMW_BITS(bgp, i, bgap_threshold,
1360 threshold_tcold_mask, ts_data->t_cold);
1361 RMW_BITS(bgp, i, bgap_threshold,
1362 threshold_thot_mask, ts_data->t_hot);
1363 /* Enable the alert events */
1364 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
1365 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
1368 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
1369 /* Set initial Tshut thresholds */
1370 RMW_BITS(bgp, i, tshut_threshold,
1371 tshut_hot_mask, ts_data->tshut_hot);
1372 RMW_BITS(bgp, i, tshut_threshold,
1373 tshut_cold_mask, ts_data->tshut_cold);
1377 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1378 ti_bandgap_set_continuous_mode(bgp);
1380 /* Set .250 seconds time as default counter */
1381 if (TI_BANDGAP_HAS(bgp, COUNTER))
1382 for (i = 0; i < bgp->conf->sensor_count; i++)
1383 RMW_BITS(bgp, i, bgap_counter, counter_mask,
1386 /* Every thing is good? Then expose the sensors */
1387 for (i = 0; i < bgp->conf->sensor_count; i++) {
1390 if (bgp->conf->sensors[i].register_cooling) {
1391 ret = bgp->conf->sensors[i].register_cooling(bgp, i);
1393 goto remove_sensors;
1396 if (bgp->conf->expose_sensor) {
1397 domain = bgp->conf->sensors[i].domain;
1398 ret = bgp->conf->expose_sensor(bgp, i, domain);
1400 goto remove_last_cooling;
1405 * Enable the Interrupts once everything is set. Otherwise irq handler
1406 * might be called as soon as it is enabled where as rest of framework
1407 * is still getting initialised.
1409 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1410 ret = ti_bandgap_talert_init(bgp, pdev);
1412 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
1413 i = bgp->conf->sensor_count;
1420 remove_last_cooling:
1421 if (bgp->conf->sensors[i].unregister_cooling)
1422 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1424 for (i--; i >= 0; i--) {
1425 if (bgp->conf->sensors[i].unregister_cooling)
1426 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1427 if (bgp->conf->remove_sensor)
1428 bgp->conf->remove_sensor(bgp, i);
1430 ti_bandgap_power(bgp, false);
1432 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1433 clk_disable_unprepare(bgp->fclock);
1435 clk_put(bgp->fclock);
1436 clk_put(bgp->div_clk);
1438 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1439 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1440 gpio_free(bgp->tshut_gpio);
1447 int ti_bandgap_remove(struct platform_device *pdev)
1449 struct ti_bandgap *bgp = platform_get_drvdata(pdev);
1452 /* First thing is to remove sensor interfaces */
1453 for (i = 0; i < bgp->conf->sensor_count; i++) {
1454 if (bgp->conf->sensors[i].unregister_cooling)
1455 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1457 if (bgp->conf->remove_sensor)
1458 bgp->conf->remove_sensor(bgp, i);
1461 ti_bandgap_power(bgp, false);
1463 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1464 clk_disable_unprepare(bgp->fclock);
1465 clk_put(bgp->fclock);
1466 clk_put(bgp->div_clk);
1468 if (TI_BANDGAP_HAS(bgp, TALERT))
1469 free_irq(bgp->irq, bgp);
1471 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1472 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1473 gpio_free(bgp->tshut_gpio);
1479 #ifdef CONFIG_PM_SLEEP
1480 static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
1484 for (i = 0; i < bgp->conf->sensor_count; i++) {
1485 struct temp_sensor_registers *tsr;
1486 struct temp_sensor_regval *rval;
1488 rval = &bgp->regval[i];
1489 tsr = bgp->conf->sensors[i].registers;
1491 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1492 rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
1493 tsr->bgap_mode_ctrl);
1494 if (TI_BANDGAP_HAS(bgp, COUNTER))
1495 rval->bg_counter = ti_bandgap_readl(bgp,
1497 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1498 rval->bg_threshold = ti_bandgap_readl(bgp,
1499 tsr->bgap_threshold);
1500 rval->bg_ctrl = ti_bandgap_readl(bgp,
1501 tsr->bgap_mask_ctrl);
1504 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1505 rval->tshut_threshold = ti_bandgap_readl(bgp,
1506 tsr->tshut_threshold);
1512 static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
1516 for (i = 0; i < bgp->conf->sensor_count; i++) {
1517 struct temp_sensor_registers *tsr;
1518 struct temp_sensor_regval *rval;
1521 rval = &bgp->regval[i];
1522 tsr = bgp->conf->sensors[i].registers;
1524 if (TI_BANDGAP_HAS(bgp, COUNTER))
1525 val = ti_bandgap_readl(bgp, tsr->bgap_counter);
1527 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1528 ti_bandgap_writel(bgp, rval->tshut_threshold,
1529 tsr->tshut_threshold);
1530 /* Force immediate temperature measurement and update
1531 * of the DTEMP field
1533 ti_bandgap_force_single_read(bgp, i);
1535 if (TI_BANDGAP_HAS(bgp, COUNTER))
1536 ti_bandgap_writel(bgp, rval->bg_counter,
1538 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1539 ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1540 tsr->bgap_mode_ctrl);
1541 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1542 ti_bandgap_writel(bgp, rval->bg_threshold,
1543 tsr->bgap_threshold);
1544 ti_bandgap_writel(bgp, rval->bg_ctrl,
1545 tsr->bgap_mask_ctrl);
1552 static int ti_bandgap_suspend(struct device *dev)
1554 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1557 err = ti_bandgap_save_ctxt(bgp);
1558 ti_bandgap_power(bgp, false);
1560 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1561 clk_disable_unprepare(bgp->fclock);
1566 static int ti_bandgap_resume(struct device *dev)
1568 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1570 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1571 clk_prepare_enable(bgp->fclock);
1573 ti_bandgap_power(bgp, true);
1575 return ti_bandgap_restore_ctxt(bgp);
1577 static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend,
1580 #define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
1582 #define DEV_PM_OPS NULL
1585 static const struct of_device_id of_ti_bandgap_match[] = {
1586 #ifdef CONFIG_OMAP4_THERMAL
1588 .compatible = "ti,omap4430-bandgap",
1589 .data = (void *)&omap4430_data,
1592 .compatible = "ti,omap4460-bandgap",
1593 .data = (void *)&omap4460_data,
1596 .compatible = "ti,omap4470-bandgap",
1597 .data = (void *)&omap4470_data,
1600 #ifdef CONFIG_OMAP5_THERMAL
1602 .compatible = "ti,omap5430-bandgap",
1603 .data = (void *)&omap5430_data,
1606 #ifdef CONFIG_DRA752_THERMAL
1608 .compatible = "ti,dra752-bandgap",
1609 .data = (void *)&dra752_data,
1615 MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
1617 static struct platform_driver ti_bandgap_sensor_driver = {
1618 .probe = ti_bandgap_probe,
1619 .remove = ti_bandgap_remove,
1621 .name = "ti-soc-thermal",
1623 .of_match_table = of_ti_bandgap_match,
1627 module_platform_driver(ti_bandgap_sensor_driver);
1629 MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1630 MODULE_LICENSE("GPL v2");
1631 MODULE_ALIAS("platform:ti-soc-thermal");
1632 MODULE_AUTHOR("Texas Instrument Inc.");