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))
111 for (i = 0; i < bgp->conf->sensor_count; i++)
113 RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
118 * ti_bandgap_read_temp() - helper function to read sensor temperature
119 * @bgp: pointer to ti_bandgap structure
120 * @id: bandgap sensor id
122 * Function to concentrate the steps to read sensor temperature register.
123 * This function is desired because, depending on bandgap device version,
124 * it might be needed to freeze the bandgap state machine, before fetching
125 * the register value.
127 * Return: temperature in ADC values.
129 static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
131 struct temp_sensor_registers *tsr;
134 tsr = bgp->conf->sensors[id].registers;
135 reg = tsr->temp_sensor_ctrl;
137 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
138 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
140 * In case we cannot read from cur_dtemp / dtemp_0,
141 * then we read from the last valid temp read
143 reg = tsr->ctrl_dtemp_1;
146 /* read temperature */
147 temp = ti_bandgap_readl(bgp, reg);
148 temp &= tsr->bgap_dtemp_mask;
150 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
151 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
156 /*** IRQ handlers ***/
159 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
161 * @data: private data (struct ti_bandgap *)
163 * This is the Talert handler. Use it only if bandgap device features
164 * HAS(TALERT). This handler goes over all sensors and checks their
165 * conditions and acts accordingly. In case there are events pending,
166 * it will reset the event mask to wait for the opposite event (next event).
167 * Every time there is a new event, it will be reported to thermal layer.
169 * Return: IRQ_HANDLED
171 static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
173 struct ti_bandgap *bgp = data;
174 struct temp_sensor_registers *tsr;
175 u32 t_hot = 0, t_cold = 0, ctrl;
178 spin_lock(&bgp->lock);
179 for (i = 0; i < bgp->conf->sensor_count; i++) {
180 tsr = bgp->conf->sensors[i].registers;
181 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
183 /* Read the status of t_hot */
184 t_hot = ctrl & tsr->status_hot_mask;
186 /* Read the status of t_cold */
187 t_cold = ctrl & tsr->status_cold_mask;
189 if (!t_cold && !t_hot)
192 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
194 * One TALERT interrupt: Two sources
195 * If the interrupt is due to t_hot then mask t_hot and
196 * and unmask t_cold else mask t_cold and unmask t_hot
199 ctrl &= ~tsr->mask_hot_mask;
200 ctrl |= tsr->mask_cold_mask;
202 ctrl &= ~tsr->mask_cold_mask;
203 ctrl |= tsr->mask_hot_mask;
206 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
209 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
210 __func__, bgp->conf->sensors[i].domain,
213 /* report temperature to whom may concern */
214 if (bgp->conf->report_temperature)
215 bgp->conf->report_temperature(bgp, i);
217 spin_unlock(&bgp->lock);
223 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
225 * @data: private data (unused)
227 * This is the Tshut handler. Use it only if bandgap device features
228 * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
231 * Return: IRQ_HANDLED
233 static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
235 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
238 orderly_poweroff(true);
243 /*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
246 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
247 * @bgp: struct ti_bandgap pointer
248 * @adc_val: value in ADC representation
249 * @t: address where to write the resulting temperature in mCelsius
251 * Simple conversion from ADC representation to mCelsius. In case the ADC value
252 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
253 * The conversion table is indexed by the ADC values.
255 * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val
256 * argument is out of the ADC conv table range.
259 int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
261 const struct ti_bandgap_data *conf = bgp->conf;
263 /* look up for temperature in the table and return the temperature */
264 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val)
267 *t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
272 * ti_bandgap_mcelsius_to_adc() - converts a mCelsius value to ADC scale
273 * @bgp: struct ti_bandgap pointer
274 * @temp: value in mCelsius
275 * @adc: address where to write the resulting temperature in ADC representation
277 * Simple conversion from mCelsius to ADC values. In case the temp value
278 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
279 * The conversion table is indexed by the ADC values.
281 * Return: 0 if conversion was successful, else -ERANGE in case the @temp
282 * argument is out of the ADC conv table range.
285 int ti_bandgap_mcelsius_to_adc(struct ti_bandgap *bgp, long temp, int *adc)
287 const struct ti_bandgap_data *conf = bgp->conf;
288 const int *conv_table = bgp->conf->conv_table;
292 high = conf->adc_end_val - conf->adc_start_val;
293 mid = (high + low) / 2;
295 if (temp < conv_table[low] || temp > conv_table[high])
299 if (temp < conv_table[mid])
303 mid = (low + high) / 2;
306 *adc = conf->adc_start_val + low;
311 * ti_bandgap_add_hyst() - add hysteresis (in mCelsius) to an ADC value
312 * @bgp: struct ti_bandgap pointer
313 * @adc_val: temperature value in ADC representation
314 * @hyst_val: hysteresis value in mCelsius
315 * @sum: address where to write the resulting temperature (in ADC scale)
317 * Adds an hysteresis value (in mCelsius) to a ADC temperature value.
319 * Return: 0 on success, -ERANGE otherwise.
322 int ti_bandgap_add_hyst(struct ti_bandgap *bgp, int adc_val, int hyst_val,
328 * Need to add in the mcelsius domain, so we have a temperature
329 * the conv_table range
331 ret = ti_bandgap_adc_to_mcelsius(bgp, adc_val, &temp);
337 ret = ti_bandgap_mcelsius_to_adc(bgp, temp, sum);
341 /*** Helper functions handling device Alert/Shutdown signals ***/
344 * ti_bandgap_unmask_interrupts() - unmasks the events of thot & tcold
345 * @bgp: struct ti_bandgap pointer
346 * @id: bandgap sensor id
347 * @t_hot: hot temperature value to trigger alert signal
348 * @t_cold: cold temperature value to trigger alert signal
350 * Checks the requested t_hot and t_cold values and configures the IRQ event
351 * masks accordingly. Call this function only if bandgap features HAS(TALERT).
353 static void ti_bandgap_unmask_interrupts(struct ti_bandgap *bgp, int id,
354 u32 t_hot, u32 t_cold)
356 struct temp_sensor_registers *tsr;
359 /* Read the current on die temperature */
360 temp = ti_bandgap_read_temp(bgp, id);
362 tsr = bgp->conf->sensors[id].registers;
363 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
366 reg_val |= tsr->mask_hot_mask;
368 reg_val &= ~tsr->mask_hot_mask;
371 reg_val |= tsr->mask_cold_mask;
373 reg_val &= ~tsr->mask_cold_mask;
374 ti_bandgap_writel(bgp, reg_val, tsr->bgap_mask_ctrl);
378 * ti_bandgap_update_alert_threshold() - sequence to update thresholds
379 * @bgp: struct ti_bandgap pointer
380 * @id: bandgap sensor id
381 * @val: value (ADC) of a new threshold
382 * @hot: desired threshold to be updated. true if threshold hot, false if
385 * It will program the required thresholds (hot and cold) for TALERT signal.
386 * This function can be used to update t_hot or t_cold, depending on @hot value.
387 * It checks the resulting t_hot and t_cold values, based on the new passed @val
388 * and configures the thresholds so that t_hot is always greater than t_cold.
389 * Call this function only if bandgap features HAS(TALERT).
391 * Return: 0 if no error, else corresponding error
393 static int ti_bandgap_update_alert_threshold(struct ti_bandgap *bgp, int id,
396 struct temp_sensor_data *ts_data = bgp->conf->sensors[id].ts_data;
397 struct temp_sensor_registers *tsr;
398 u32 thresh_val, reg_val, t_hot, t_cold;
401 tsr = bgp->conf->sensors[id].registers;
403 /* obtain the current value */
404 thresh_val = ti_bandgap_readl(bgp, tsr->bgap_threshold);
405 t_cold = (thresh_val & tsr->threshold_tcold_mask) >>
406 __ffs(tsr->threshold_tcold_mask);
407 t_hot = (thresh_val & tsr->threshold_thot_mask) >>
408 __ffs(tsr->threshold_thot_mask);
414 if (t_cold > t_hot) {
416 err = ti_bandgap_add_hyst(bgp, t_hot,
420 err = ti_bandgap_add_hyst(bgp, t_cold,
425 /* write the new threshold values */
426 reg_val = thresh_val &
427 ~(tsr->threshold_thot_mask | tsr->threshold_tcold_mask);
428 reg_val |= (t_hot << __ffs(tsr->threshold_thot_mask)) |
429 (t_cold << __ffs(tsr->threshold_tcold_mask));
430 ti_bandgap_writel(bgp, reg_val, tsr->bgap_threshold);
433 dev_err(bgp->dev, "failed to reprogram thot threshold\n");
438 ti_bandgap_unmask_interrupts(bgp, id, t_hot, t_cold);
444 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
445 * @bgp: struct ti_bandgap pointer
446 * @id: bandgap sensor id
448 * Checks if the bandgap pointer is valid and if the sensor id is also
451 * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
452 * @id cannot index @bgp sensors.
454 static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
456 if (!bgp || IS_ERR(bgp)) {
457 pr_err("%s: invalid bandgap pointer\n", __func__);
461 if ((id < 0) || (id >= bgp->conf->sensor_count)) {
462 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
471 * _ti_bandgap_write_threshold() - helper to update TALERT t_cold or t_hot
472 * @bgp: struct ti_bandgap pointer
473 * @id: bandgap sensor id
474 * @val: value (mCelsius) of a new threshold
475 * @hot: desired threshold to be updated. true if threshold hot, false if
478 * It will update the required thresholds (hot and cold) for TALERT signal.
479 * This function can be used to update t_hot or t_cold, depending on @hot value.
480 * Validates the mCelsius range and update the requested threshold.
481 * Call this function only if bandgap features HAS(TALERT).
483 * Return: 0 if no error, else corresponding error value.
485 static int _ti_bandgap_write_threshold(struct ti_bandgap *bgp, int id, int val,
488 struct temp_sensor_data *ts_data;
489 struct temp_sensor_registers *tsr;
493 ret = ti_bandgap_validate(bgp, id);
497 if (!TI_BANDGAP_HAS(bgp, TALERT))
500 ts_data = bgp->conf->sensors[id].ts_data;
501 tsr = bgp->conf->sensors[id].registers;
503 if (val < ts_data->min_temp + ts_data->hyst_val)
506 if (val > ts_data->max_temp + ts_data->hyst_val)
513 ret = ti_bandgap_mcelsius_to_adc(bgp, val, &adc_val);
517 spin_lock(&bgp->lock);
518 ret = ti_bandgap_update_alert_threshold(bgp, id, adc_val, hot);
519 spin_unlock(&bgp->lock);
524 * _ti_bandgap_read_threshold() - helper to read TALERT t_cold or t_hot
525 * @bgp: struct ti_bandgap pointer
526 * @id: bandgap sensor id
527 * @val: value (mCelsius) of a threshold
528 * @hot: desired threshold to be read. true if threshold hot, false if
531 * It will fetch the required thresholds (hot and cold) for TALERT signal.
532 * This function can be used to read t_hot or t_cold, depending on @hot value.
533 * Call this function only if bandgap features HAS(TALERT).
535 * Return: 0 if no error, -ENOTSUPP if it has no TALERT support, or the
536 * corresponding error value if some operation fails.
538 static int _ti_bandgap_read_threshold(struct ti_bandgap *bgp, int id,
541 struct temp_sensor_registers *tsr;
545 ret = ti_bandgap_validate(bgp, id);
549 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
554 tsr = bgp->conf->sensors[id].registers;
556 mask = tsr->threshold_thot_mask;
558 mask = tsr->threshold_tcold_mask;
560 temp = ti_bandgap_readl(bgp, tsr->bgap_threshold);
561 temp = (temp & mask) >> __ffs(mask);
562 ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
564 dev_err(bgp->dev, "failed to read thot\n");
575 /*** Exposed APIs ***/
578 * ti_bandgap_read_thot() - reads sensor current thot
579 * @bgp: pointer to bandgap instance
581 * @thot: resulting current thot value
583 * Return: 0 on success or the proper error code
585 int ti_bandgap_read_thot(struct ti_bandgap *bgp, int id, int *thot)
587 return _ti_bandgap_read_threshold(bgp, id, thot, true);
591 * ti_bandgap_write_thot() - sets sensor current thot
592 * @bgp: pointer to bandgap instance
594 * @val: desired thot value
596 * Return: 0 on success or the proper error code
598 int ti_bandgap_write_thot(struct ti_bandgap *bgp, int id, int val)
600 return _ti_bandgap_write_threshold(bgp, id, val, true);
604 * ti_bandgap_read_tcold() - reads sensor current tcold
605 * @bgp: pointer to bandgap instance
607 * @tcold: resulting current tcold value
609 * Return: 0 on success or the proper error code
611 int ti_bandgap_read_tcold(struct ti_bandgap *bgp, int id, int *tcold)
613 return _ti_bandgap_read_threshold(bgp, id, tcold, false);
617 * ti_bandgap_write_tcold() - sets the sensor tcold
618 * @bgp: pointer to bandgap instance
620 * @val: desired tcold value
622 * Return: 0 on success or the proper error code
624 int ti_bandgap_write_tcold(struct ti_bandgap *bgp, int id, int val)
626 return _ti_bandgap_write_threshold(bgp, id, val, false);
630 * ti_bandgap_read_counter() - read the sensor counter
631 * @bgp: pointer to bandgap instance
633 * @interval: resulting update interval in miliseconds
635 static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id,
638 struct temp_sensor_registers *tsr;
641 tsr = bgp->conf->sensors[id].registers;
642 time = ti_bandgap_readl(bgp, tsr->bgap_counter);
643 time = (time & tsr->counter_mask) >>
644 __ffs(tsr->counter_mask);
645 time = time * 1000 / bgp->clk_rate;
650 * ti_bandgap_read_counter_delay() - read the sensor counter delay
651 * @bgp: pointer to bandgap instance
653 * @interval: resulting update interval in miliseconds
655 static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id,
658 struct temp_sensor_registers *tsr;
661 tsr = bgp->conf->sensors[id].registers;
663 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
664 reg_val = (reg_val & tsr->mask_counter_delay_mask) >>
665 __ffs(tsr->mask_counter_delay_mask);
686 dev_warn(bgp->dev, "Wrong counter delay value read from register %X",
692 * ti_bandgap_read_update_interval() - read the sensor update interval
693 * @bgp: pointer to bandgap instance
695 * @interval: resulting update interval in miliseconds
697 * Return: 0 on success or the proper error code
699 int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
704 ret = ti_bandgap_validate(bgp, id);
708 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
709 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
714 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
715 ti_bandgap_read_counter(bgp, id, interval);
719 ti_bandgap_read_counter_delay(bgp, id, interval);
725 * ti_bandgap_write_counter_delay() - set the counter_delay
726 * @bgp: pointer to bandgap instance
728 * @interval: desired update interval in miliseconds
730 * Return: 0 on success or the proper error code
732 static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id,
738 case 0: /* Immediate conversion */
741 case 1: /* Conversion after ever 1ms */
744 case 10: /* Conversion after ever 10ms */
747 case 100: /* Conversion after ever 100ms */
750 case 250: /* Conversion after ever 250ms */
753 case 500: /* Conversion after ever 500ms */
757 dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval);
761 spin_lock(&bgp->lock);
762 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval);
763 spin_unlock(&bgp->lock);
769 * ti_bandgap_write_counter() - set the bandgap sensor counter
770 * @bgp: pointer to bandgap instance
772 * @interval: desired update interval in miliseconds
774 static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id,
777 interval = interval * bgp->clk_rate / 1000;
778 spin_lock(&bgp->lock);
779 RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
780 spin_unlock(&bgp->lock);
784 * ti_bandgap_write_update_interval() - set the update interval
785 * @bgp: pointer to bandgap instance
787 * @interval: desired update interval in miliseconds
789 * Return: 0 on success or the proper error code
791 int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
792 int id, u32 interval)
794 int ret = ti_bandgap_validate(bgp, id);
798 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
799 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
804 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
805 ti_bandgap_write_counter(bgp, id, interval);
809 ret = ti_bandgap_write_counter_delay(bgp, id, interval);
815 * ti_bandgap_read_temperature() - report current temperature
816 * @bgp: pointer to bandgap instance
818 * @temperature: resulting temperature
820 * Return: 0 on success or the proper error code
822 int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
828 ret = ti_bandgap_validate(bgp, id);
832 spin_lock(&bgp->lock);
833 temp = ti_bandgap_read_temp(bgp, id);
834 spin_unlock(&bgp->lock);
836 ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
846 * ti_bandgap_set_sensor_data() - helper function to store thermal
847 * framework related data.
848 * @bgp: pointer to bandgap instance
850 * @data: thermal framework related data to be stored
852 * Return: 0 on success or the proper error code
854 int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
856 int ret = ti_bandgap_validate(bgp, id);
860 bgp->regval[id].data = data;
866 * ti_bandgap_get_sensor_data() - helper function to get thermal
867 * framework related data.
868 * @bgp: pointer to bandgap instance
871 * Return: data stored by set function with sensor id on success or NULL
873 void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
875 int ret = ti_bandgap_validate(bgp, id);
879 return bgp->regval[id].data;
882 /*** Helper functions used during device initialization ***/
885 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
886 * @bgp: pointer to struct ti_bandgap
887 * @id: sensor id which it is desired to read 1 temperature
889 * Used to initialize the conversion state machine and set it to a valid
890 * state. Called during device initialization and context restore events.
895 ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
897 u32 temp = 0, counter = 1000;
899 /* Select single conversion mode */
900 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
901 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
903 /* Start of Conversion = 1 */
904 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
905 /* Wait until DTEMP is updated */
906 temp = ti_bandgap_read_temp(bgp, id);
908 while ((temp == 0) && --counter)
909 temp = ti_bandgap_read_temp(bgp, id);
910 /* REVISIT: Check correct condition for end of conversion */
912 /* Start of Conversion = 0 */
913 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
919 * ti_bandgap_set_continous_mode() - One time enabling of continuous mode
920 * @bgp: pointer to struct ti_bandgap
922 * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
923 * be used for junction temperature monitoring, it is desirable that the
924 * sensors are operational all the time, so that alerts are generated
929 static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
933 for (i = 0; i < bgp->conf->sensor_count; i++) {
934 /* Perform a single read just before enabling continuous */
935 ti_bandgap_force_single_read(bgp, i);
936 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
943 * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
944 * @bgp: pointer to struct ti_bandgap
945 * @id: id of the individual sensor
946 * @trend: Pointer to trend.
948 * This function needs to be called to fetch the temperature trend of a
949 * Particular sensor. The function computes the difference in temperature
950 * w.r.t time. For the bandgaps with built in history buffer the temperatures
951 * are read from the buffer and for those without the Buffer -ENOTSUPP is
954 * Return: 0 if no error, else return corresponding error. If no
955 * error then the trend value is passed on to trend parameter
957 int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend)
959 struct temp_sensor_registers *tsr;
960 u32 temp1, temp2, reg1, reg2;
961 int t1, t2, interval, ret = 0;
963 ret = ti_bandgap_validate(bgp, id);
967 if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) ||
968 !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
973 spin_lock(&bgp->lock);
975 tsr = bgp->conf->sensors[id].registers;
977 /* Freeze and read the last 2 valid readings */
978 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
979 reg1 = tsr->ctrl_dtemp_1;
980 reg2 = tsr->ctrl_dtemp_2;
982 /* read temperature from history buffer */
983 temp1 = ti_bandgap_readl(bgp, reg1);
984 temp1 &= tsr->bgap_dtemp_mask;
986 temp2 = ti_bandgap_readl(bgp, reg2);
987 temp2 &= tsr->bgap_dtemp_mask;
989 /* Convert from adc values to mCelsius temperature */
990 ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1);
994 ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2);
998 /* Fetch the update interval */
999 ret = ti_bandgap_read_update_interval(bgp, id, &interval);
1003 /* Set the interval to 1 ms if bandgap counter delay is not set */
1007 *trend = (t1 - t2) / interval;
1009 dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
1013 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
1014 spin_unlock(&bgp->lock);
1020 * ti_bandgap_tshut_init() - setup and initialize tshut handling
1021 * @bgp: pointer to struct ti_bandgap
1022 * @pdev: pointer to device struct platform_device
1024 * Call this function only in case the bandgap features HAS(TSHUT).
1025 * In this case, the driver needs to handle the TSHUT signal as an IRQ.
1026 * The IRQ is wired as a GPIO, and for this purpose, it is required
1027 * to specify which GPIO line is used. TSHUT IRQ is fired anytime
1028 * one of the bandgap sensors violates the TSHUT high/hot threshold.
1029 * And in that case, the system must go off.
1031 * Return: 0 if no error, else error status
1033 static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
1034 struct platform_device *pdev)
1036 int gpio_nr = bgp->tshut_gpio;
1039 /* Request for gpio_86 line */
1040 status = gpio_request(gpio_nr, "tshut");
1042 dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86);
1045 status = gpio_direction_input(gpio_nr);
1047 dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr);
1051 status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler,
1052 IRQF_TRIGGER_RISING, "tshut", NULL);
1055 dev_err(bgp->dev, "request irq failed for TSHUT");
1062 * ti_bandgap_alert_init() - setup and initialize talert handling
1063 * @bgp: pointer to struct ti_bandgap
1064 * @pdev: pointer to device struct platform_device
1066 * Call this function only in case the bandgap features HAS(TALERT).
1067 * In this case, the driver needs to handle the TALERT signals as an IRQs.
1068 * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
1069 * are violated. In these situation, the driver must reprogram the thresholds,
1070 * accordingly to specified policy.
1072 * Return: 0 if no error, else return corresponding error.
1074 static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
1075 struct platform_device *pdev)
1079 bgp->irq = platform_get_irq(pdev, 0);
1081 dev_err(&pdev->dev, "get_irq failed\n");
1084 ret = request_threaded_irq(bgp->irq, NULL,
1085 ti_bandgap_talert_irq_handler,
1086 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
1089 dev_err(&pdev->dev, "Request threaded irq failed.\n");
1096 static const struct of_device_id of_ti_bandgap_match[];
1098 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
1099 * @pdev: pointer to device struct platform_device
1101 * Used to read the device tree properties accordingly to the bandgap
1102 * matching version. Based on bandgap version and its capabilities it
1103 * will build a struct ti_bandgap out of the required DT entries.
1105 * Return: valid bandgap structure if successful, else returns ERR_PTR
1106 * return value must be verified with IS_ERR.
1108 static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
1110 struct device_node *node = pdev->dev.of_node;
1111 const struct of_device_id *of_id;
1112 struct ti_bandgap *bgp;
1113 struct resource *res;
1116 /* just for the sake */
1118 dev_err(&pdev->dev, "no platform information available\n");
1119 return ERR_PTR(-EINVAL);
1122 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
1124 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
1125 return ERR_PTR(-ENOMEM);
1128 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
1130 bgp->conf = of_id->data;
1132 /* register shadow for context save and restore */
1133 bgp->regval = devm_kzalloc(&pdev->dev, sizeof(*bgp->regval) *
1134 bgp->conf->sensor_count, GFP_KERNEL);
1136 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
1137 return ERR_PTR(-ENOMEM);
1142 void __iomem *chunk;
1144 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
1147 chunk = devm_ioremap_resource(&pdev->dev, res);
1151 return ERR_CAST(chunk);
1156 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1157 bgp->tshut_gpio = of_get_gpio(node, 0);
1158 if (!gpio_is_valid(bgp->tshut_gpio)) {
1159 dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
1161 return ERR_PTR(-EINVAL);
1168 /*** Device driver call backs ***/
1171 int ti_bandgap_probe(struct platform_device *pdev)
1173 struct ti_bandgap *bgp;
1174 int clk_rate, ret = 0, i;
1176 bgp = ti_bandgap_build(pdev);
1178 dev_err(&pdev->dev, "failed to fetch platform data\n");
1179 return PTR_ERR(bgp);
1181 bgp->dev = &pdev->dev;
1183 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1184 ret = ti_bandgap_tshut_init(bgp, pdev);
1187 "failed to initialize system tshut IRQ\n");
1192 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
1193 ret = IS_ERR(bgp->fclock);
1195 dev_err(&pdev->dev, "failed to request fclock reference\n");
1196 ret = PTR_ERR(bgp->fclock);
1200 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
1201 ret = IS_ERR(bgp->div_clk);
1203 dev_err(&pdev->dev, "failed to request div_ts_ck clock ref\n");
1204 ret = PTR_ERR(bgp->div_clk);
1208 for (i = 0; i < bgp->conf->sensor_count; i++) {
1209 struct temp_sensor_registers *tsr;
1212 tsr = bgp->conf->sensors[i].registers;
1214 * check if the efuse has a non-zero value if not
1215 * it is an untrimmed sample and the temperatures
1216 * may not be accurate
1218 val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
1220 dev_info(&pdev->dev,
1221 "Non-trimmed BGAP, Temp not accurate\n");
1224 clk_rate = clk_round_rate(bgp->div_clk,
1225 bgp->conf->sensors[0].ts_data->max_freq);
1226 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
1229 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
1233 ret = clk_set_rate(bgp->div_clk, clk_rate);
1235 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
1237 bgp->clk_rate = clk_rate;
1238 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1239 clk_prepare_enable(bgp->fclock);
1242 spin_lock_init(&bgp->lock);
1243 bgp->dev = &pdev->dev;
1244 platform_set_drvdata(pdev, bgp);
1246 ti_bandgap_power(bgp, true);
1248 /* Set default counter to 1 for now */
1249 if (TI_BANDGAP_HAS(bgp, COUNTER))
1250 for (i = 0; i < bgp->conf->sensor_count; i++)
1251 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
1253 /* Set default thresholds for alert and shutdown */
1254 for (i = 0; i < bgp->conf->sensor_count; i++) {
1255 struct temp_sensor_data *ts_data;
1257 ts_data = bgp->conf->sensors[i].ts_data;
1259 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1260 /* Set initial Talert thresholds */
1261 RMW_BITS(bgp, i, bgap_threshold,
1262 threshold_tcold_mask, ts_data->t_cold);
1263 RMW_BITS(bgp, i, bgap_threshold,
1264 threshold_thot_mask, ts_data->t_hot);
1265 /* Enable the alert events */
1266 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
1267 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
1270 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
1271 /* Set initial Tshut thresholds */
1272 RMW_BITS(bgp, i, tshut_threshold,
1273 tshut_hot_mask, ts_data->tshut_hot);
1274 RMW_BITS(bgp, i, tshut_threshold,
1275 tshut_cold_mask, ts_data->tshut_cold);
1279 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1280 ti_bandgap_set_continuous_mode(bgp);
1282 /* Set .250 seconds time as default counter */
1283 if (TI_BANDGAP_HAS(bgp, COUNTER))
1284 for (i = 0; i < bgp->conf->sensor_count; i++)
1285 RMW_BITS(bgp, i, bgap_counter, counter_mask,
1288 /* Every thing is good? Then expose the sensors */
1289 for (i = 0; i < bgp->conf->sensor_count; i++) {
1292 if (bgp->conf->sensors[i].register_cooling) {
1293 ret = bgp->conf->sensors[i].register_cooling(bgp, i);
1295 goto remove_sensors;
1298 if (bgp->conf->expose_sensor) {
1299 domain = bgp->conf->sensors[i].domain;
1300 ret = bgp->conf->expose_sensor(bgp, i, domain);
1302 goto remove_last_cooling;
1307 * Enable the Interrupts once everything is set. Otherwise irq handler
1308 * might be called as soon as it is enabled where as rest of framework
1309 * is still getting initialised.
1311 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1312 ret = ti_bandgap_talert_init(bgp, pdev);
1314 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
1315 i = bgp->conf->sensor_count;
1322 remove_last_cooling:
1323 if (bgp->conf->sensors[i].unregister_cooling)
1324 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1326 for (i--; i >= 0; i--) {
1327 if (bgp->conf->sensors[i].unregister_cooling)
1328 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1329 if (bgp->conf->remove_sensor)
1330 bgp->conf->remove_sensor(bgp, i);
1332 ti_bandgap_power(bgp, false);
1334 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1335 clk_disable_unprepare(bgp->fclock);
1337 clk_put(bgp->fclock);
1338 clk_put(bgp->div_clk);
1340 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1341 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1342 gpio_free(bgp->tshut_gpio);
1349 int ti_bandgap_remove(struct platform_device *pdev)
1351 struct ti_bandgap *bgp = platform_get_drvdata(pdev);
1354 /* First thing is to remove sensor interfaces */
1355 for (i = 0; i < bgp->conf->sensor_count; i++) {
1356 if (bgp->conf->sensors[i].unregister_cooling)
1357 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1359 if (bgp->conf->remove_sensor)
1360 bgp->conf->remove_sensor(bgp, i);
1363 ti_bandgap_power(bgp, false);
1365 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1366 clk_disable_unprepare(bgp->fclock);
1367 clk_put(bgp->fclock);
1368 clk_put(bgp->div_clk);
1370 if (TI_BANDGAP_HAS(bgp, TALERT))
1371 free_irq(bgp->irq, bgp);
1373 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1374 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1375 gpio_free(bgp->tshut_gpio);
1381 #ifdef CONFIG_PM_SLEEP
1382 static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
1386 for (i = 0; i < bgp->conf->sensor_count; i++) {
1387 struct temp_sensor_registers *tsr;
1388 struct temp_sensor_regval *rval;
1390 rval = &bgp->regval[i];
1391 tsr = bgp->conf->sensors[i].registers;
1393 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1394 rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
1395 tsr->bgap_mode_ctrl);
1396 if (TI_BANDGAP_HAS(bgp, COUNTER))
1397 rval->bg_counter = ti_bandgap_readl(bgp,
1399 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1400 rval->bg_threshold = ti_bandgap_readl(bgp,
1401 tsr->bgap_threshold);
1402 rval->bg_ctrl = ti_bandgap_readl(bgp,
1403 tsr->bgap_mask_ctrl);
1406 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1407 rval->tshut_threshold = ti_bandgap_readl(bgp,
1408 tsr->tshut_threshold);
1414 static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
1418 for (i = 0; i < bgp->conf->sensor_count; i++) {
1419 struct temp_sensor_registers *tsr;
1420 struct temp_sensor_regval *rval;
1423 rval = &bgp->regval[i];
1424 tsr = bgp->conf->sensors[i].registers;
1426 if (TI_BANDGAP_HAS(bgp, COUNTER))
1427 val = ti_bandgap_readl(bgp, tsr->bgap_counter);
1429 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1430 ti_bandgap_writel(bgp, rval->tshut_threshold,
1431 tsr->tshut_threshold);
1432 /* Force immediate temperature measurement and update
1433 * of the DTEMP field
1435 ti_bandgap_force_single_read(bgp, i);
1437 if (TI_BANDGAP_HAS(bgp, COUNTER))
1438 ti_bandgap_writel(bgp, rval->bg_counter,
1440 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1441 ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1442 tsr->bgap_mode_ctrl);
1443 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1444 ti_bandgap_writel(bgp, rval->bg_threshold,
1445 tsr->bgap_threshold);
1446 ti_bandgap_writel(bgp, rval->bg_ctrl,
1447 tsr->bgap_mask_ctrl);
1454 static int ti_bandgap_suspend(struct device *dev)
1456 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1459 err = ti_bandgap_save_ctxt(bgp);
1460 ti_bandgap_power(bgp, false);
1462 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1463 clk_disable_unprepare(bgp->fclock);
1468 static int ti_bandgap_resume(struct device *dev)
1470 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1472 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1473 clk_prepare_enable(bgp->fclock);
1475 ti_bandgap_power(bgp, true);
1477 return ti_bandgap_restore_ctxt(bgp);
1479 static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend,
1482 #define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
1484 #define DEV_PM_OPS NULL
1487 static const struct of_device_id of_ti_bandgap_match[] = {
1488 #ifdef CONFIG_OMAP4_THERMAL
1490 .compatible = "ti,omap4430-bandgap",
1491 .data = (void *)&omap4430_data,
1494 .compatible = "ti,omap4460-bandgap",
1495 .data = (void *)&omap4460_data,
1498 .compatible = "ti,omap4470-bandgap",
1499 .data = (void *)&omap4470_data,
1502 #ifdef CONFIG_OMAP5_THERMAL
1504 .compatible = "ti,omap5430-bandgap",
1505 .data = (void *)&omap5430_data,
1508 #ifdef CONFIG_DRA752_THERMAL
1510 .compatible = "ti,dra752-bandgap",
1511 .data = (void *)&dra752_data,
1517 MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
1519 static struct platform_driver ti_bandgap_sensor_driver = {
1520 .probe = ti_bandgap_probe,
1521 .remove = ti_bandgap_remove,
1523 .name = "ti-soc-thermal",
1525 .of_match_table = of_ti_bandgap_match,
1529 module_platform_driver(ti_bandgap_sensor_driver);
1531 MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1532 MODULE_LICENSE("GPL v2");
1533 MODULE_ALIAS("platform:ti-soc-thermal");
1534 MODULE_AUTHOR("Texas Instrument Inc.");