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>
43 #include "ti-bandgap.h"
45 /*** Helper functions to access registers and their bitfields ***/
48 * ti_bandgap_readl() - simple read helper function
49 * @bgp: pointer to ti_bandgap structure
50 * @reg: desired register (offset) to be read
52 * Helper function to read bandgap registers. It uses the io remapped area.
53 * Return: the register value.
55 static u32 ti_bandgap_readl(struct ti_bandgap *bgp, u32 reg)
57 return readl(bgp->base + reg);
61 * ti_bandgap_writel() - simple write helper function
62 * @bgp: pointer to ti_bandgap structure
63 * @val: desired register value to be written
64 * @reg: desired register (offset) to be written
66 * Helper function to write bandgap registers. It uses the io remapped area.
68 static void ti_bandgap_writel(struct ti_bandgap *bgp, u32 val, u32 reg)
70 writel(val, bgp->base + reg);
74 * DOC: macro to update bits.
76 * RMW_BITS() - used to read, modify and update bandgap bitfields.
77 * The value passed will be shifted.
79 #define RMW_BITS(bgp, id, reg, mask, val) \
81 struct temp_sensor_registers *t; \
84 t = bgp->conf->sensors[(id)].registers; \
85 r = ti_bandgap_readl(bgp, t->reg); \
87 r |= (val) << __ffs(t->mask); \
88 ti_bandgap_writel(bgp, r, t->reg); \
91 /*** Basic helper functions ***/
94 * ti_bandgap_power() - controls the power state of a bandgap device
95 * @bgp: pointer to ti_bandgap structure
96 * @on: desired power state (1 - on, 0 - off)
98 * Used to power on/off a bandgap device instance. Only used on those
99 * that features tempsoff bit.
101 * Return: 0 on success, -ENOTSUPP if tempsoff is not supported.
103 static int ti_bandgap_power(struct ti_bandgap *bgp, bool on)
107 if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH)) {
112 for (i = 0; i < bgp->conf->sensor_count; i++)
114 RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
121 * ti_bandgap_read_temp() - helper function to read sensor temperature
122 * @bgp: pointer to ti_bandgap structure
123 * @id: bandgap sensor id
125 * Function to concentrate the steps to read sensor temperature register.
126 * This function is desired because, depending on bandgap device version,
127 * it might be needed to freeze the bandgap state machine, before fetching
128 * the register value.
130 * Return: temperature in ADC values.
132 static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
134 struct temp_sensor_registers *tsr;
137 tsr = bgp->conf->sensors[id].registers;
138 reg = tsr->temp_sensor_ctrl;
140 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
141 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
143 * In case we cannot read from cur_dtemp / dtemp_0,
144 * then we read from the last valid temp read
146 reg = tsr->ctrl_dtemp_1;
149 /* read temperature */
150 temp = ti_bandgap_readl(bgp, reg);
151 temp &= tsr->bgap_dtemp_mask;
153 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
154 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
159 /*** IRQ handlers ***/
162 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
164 * @data: private data (struct ti_bandgap *)
166 * This is the Talert handler. Use it only if bandgap device features
167 * HAS(TALERT). This handler goes over all sensors and checks their
168 * conditions and acts accordingly. In case there are events pending,
169 * it will reset the event mask to wait for the opposite event (next event).
170 * Every time there is a new event, it will be reported to thermal layer.
172 * Return: IRQ_HANDLED
174 static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
176 struct ti_bandgap *bgp = data;
177 struct temp_sensor_registers *tsr;
178 u32 t_hot = 0, t_cold = 0, ctrl;
181 spin_lock(&bgp->lock);
182 for (i = 0; i < bgp->conf->sensor_count; i++) {
183 tsr = bgp->conf->sensors[i].registers;
184 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
186 /* Read the status of t_hot */
187 t_hot = ctrl & tsr->status_hot_mask;
189 /* Read the status of t_cold */
190 t_cold = ctrl & tsr->status_cold_mask;
192 if (!t_cold && !t_hot)
195 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
197 * One TALERT interrupt: Two sources
198 * If the interrupt is due to t_hot then mask t_hot and
199 * and unmask t_cold else mask t_cold and unmask t_hot
202 ctrl &= ~tsr->mask_hot_mask;
203 ctrl |= tsr->mask_cold_mask;
205 ctrl &= ~tsr->mask_cold_mask;
206 ctrl |= tsr->mask_hot_mask;
209 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
212 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
213 __func__, bgp->conf->sensors[i].domain,
216 /* report temperature to whom may concern */
217 if (bgp->conf->report_temperature)
218 bgp->conf->report_temperature(bgp, i);
220 spin_unlock(&bgp->lock);
226 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
228 * @data: private data (unused)
230 * This is the Tshut handler. Use it only if bandgap device features
231 * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
234 * Return: IRQ_HANDLED
236 static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
238 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
241 orderly_poweroff(true);
246 /*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
249 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
250 * @bgp: struct ti_bandgap pointer
251 * @adc_val: value in ADC representation
252 * @t: address where to write the resulting temperature in mCelsius
254 * Simple conversion from ADC representation to mCelsius. In case the ADC value
255 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
256 * The conversion table is indexed by the ADC values.
258 * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val
259 * argument is out of the ADC conv table range.
262 int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
264 const struct ti_bandgap_data *conf = bgp->conf;
267 /* look up for temperature in the table and return the temperature */
268 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val) {
273 *t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
280 * ti_bandgap_mcelsius_to_adc() - converts a mCelsius value to ADC scale
281 * @bgp: struct ti_bandgap pointer
282 * @temp: value in mCelsius
283 * @adc: address where to write the resulting temperature in ADC representation
285 * Simple conversion from mCelsius to ADC values. In case the temp value
286 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
287 * The conversion table is indexed by the ADC values.
289 * Return: 0 if conversion was successful, else -ERANGE in case the @temp
290 * argument is out of the ADC conv table range.
293 int ti_bandgap_mcelsius_to_adc(struct ti_bandgap *bgp, long temp, int *adc)
295 const struct ti_bandgap_data *conf = bgp->conf;
296 const int *conv_table = bgp->conf->conv_table;
297 int high, low, mid, ret = 0;
300 high = conf->adc_end_val - conf->adc_start_val;
301 mid = (high + low) / 2;
303 if (temp < conv_table[low] || temp > conv_table[high]) {
309 if (temp < conv_table[mid])
313 mid = (low + high) / 2;
316 *adc = conf->adc_start_val + low;
323 * ti_bandgap_add_hyst() - add hysteresis (in mCelsius) to an ADC value
324 * @bgp: struct ti_bandgap pointer
325 * @adc_val: temperature value in ADC representation
326 * @hyst_val: hysteresis value in mCelsius
327 * @sum: address where to write the resulting temperature (in ADC scale)
329 * Adds an hysteresis value (in mCelsius) to a ADC temperature value.
331 * Return: 0 on success, -ERANGE otherwise.
334 int ti_bandgap_add_hyst(struct ti_bandgap *bgp, int adc_val, int hyst_val,
340 * Need to add in the mcelsius domain, so we have a temperature
341 * the conv_table range
343 ret = ti_bandgap_adc_to_mcelsius(bgp, adc_val, &temp);
349 ret = ti_bandgap_mcelsius_to_adc(bgp, temp, sum);
355 /*** Helper functions handling device Alert/Shutdown signals ***/
358 * ti_bandgap_unmask_interrupts() - unmasks the events of thot & tcold
359 * @bgp: struct ti_bandgap pointer
360 * @id: bandgap sensor id
361 * @t_hot: hot temperature value to trigger alert signal
362 * @t_cold: cold temperature value to trigger alert signal
364 * Checks the requested t_hot and t_cold values and configures the IRQ event
365 * masks accordingly. Call this function only if bandgap features HAS(TALERT).
367 static void ti_bandgap_unmask_interrupts(struct ti_bandgap *bgp, int id,
368 u32 t_hot, u32 t_cold)
370 struct temp_sensor_registers *tsr;
373 /* Read the current on die temperature */
374 temp = ti_bandgap_read_temp(bgp, id);
376 tsr = bgp->conf->sensors[id].registers;
377 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
380 reg_val |= tsr->mask_hot_mask;
382 reg_val &= ~tsr->mask_hot_mask;
385 reg_val |= tsr->mask_cold_mask;
387 reg_val &= ~tsr->mask_cold_mask;
388 ti_bandgap_writel(bgp, reg_val, tsr->bgap_mask_ctrl);
392 * ti_bandgap_update_alert_threshold() - sequence to update thresholds
393 * @bgp: struct ti_bandgap pointer
394 * @id: bandgap sensor id
395 * @val: value (ADC) of a new threshold
396 * @hot: desired threshold to be updated. true if threshold hot, false if
399 * It will program the required thresholds (hot and cold) for TALERT signal.
400 * This function can be used to update t_hot or t_cold, depending on @hot value.
401 * It checks the resulting t_hot and t_cold values, based on the new passed @val
402 * and configures the thresholds so that t_hot is always greater than t_cold.
403 * Call this function only if bandgap features HAS(TALERT).
405 * Return: 0 if no error, else corresponding error
407 static int ti_bandgap_update_alert_threshold(struct ti_bandgap *bgp, int id,
410 struct temp_sensor_data *ts_data = bgp->conf->sensors[id].ts_data;
411 struct temp_sensor_registers *tsr;
412 u32 thresh_val, reg_val, t_hot, t_cold;
415 tsr = bgp->conf->sensors[id].registers;
417 /* obtain the current value */
418 thresh_val = ti_bandgap_readl(bgp, tsr->bgap_threshold);
419 t_cold = (thresh_val & tsr->threshold_tcold_mask) >>
420 __ffs(tsr->threshold_tcold_mask);
421 t_hot = (thresh_val & tsr->threshold_thot_mask) >>
422 __ffs(tsr->threshold_thot_mask);
428 if (t_cold > t_hot) {
430 err = ti_bandgap_add_hyst(bgp, t_hot,
434 err = ti_bandgap_add_hyst(bgp, t_cold,
439 /* write the new threshold values */
440 reg_val = thresh_val &
441 ~(tsr->threshold_thot_mask | tsr->threshold_tcold_mask);
442 reg_val |= (t_hot << __ffs(tsr->threshold_thot_mask)) |
443 (t_cold << __ffs(tsr->threshold_tcold_mask));
444 ti_bandgap_writel(bgp, reg_val, tsr->bgap_threshold);
447 dev_err(bgp->dev, "failed to reprogram thot threshold\n");
452 ti_bandgap_unmask_interrupts(bgp, id, t_hot, t_cold);
458 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
459 * @bgp: struct ti_bandgap pointer
460 * @id: bandgap sensor id
462 * Checks if the bandgap pointer is valid and if the sensor id is also
465 * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
466 * @id cannot index @bgp sensors.
468 static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
472 if (IS_ERR_OR_NULL(bgp)) {
473 pr_err("%s: invalid bandgap pointer\n", __func__);
478 if ((id < 0) || (id >= bgp->conf->sensor_count)) {
479 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
489 * _ti_bandgap_write_threshold() - helper to update TALERT t_cold or t_hot
490 * @bgp: struct ti_bandgap pointer
491 * @id: bandgap sensor id
492 * @val: value (mCelsius) of a new threshold
493 * @hot: desired threshold to be updated. true if threshold hot, false if
496 * It will update the required thresholds (hot and cold) for TALERT signal.
497 * This function can be used to update t_hot or t_cold, depending on @hot value.
498 * Validates the mCelsius range and update the requested threshold.
499 * Call this function only if bandgap features HAS(TALERT).
501 * Return: 0 if no error, else corresponding error value.
503 static int _ti_bandgap_write_threshold(struct ti_bandgap *bgp, int id, int val,
506 struct temp_sensor_data *ts_data;
507 struct temp_sensor_registers *tsr;
511 ret = ti_bandgap_validate(bgp, id);
515 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
520 ts_data = bgp->conf->sensors[id].ts_data;
521 tsr = bgp->conf->sensors[id].registers;
523 if (val < ts_data->min_temp + ts_data->hyst_val)
526 if (val > ts_data->max_temp + ts_data->hyst_val)
533 ret = ti_bandgap_mcelsius_to_adc(bgp, val, &adc_val);
537 spin_lock(&bgp->lock);
538 ret = ti_bandgap_update_alert_threshold(bgp, id, adc_val, hot);
539 spin_unlock(&bgp->lock);
546 * _ti_bandgap_read_threshold() - helper to read TALERT t_cold or t_hot
547 * @bgp: struct ti_bandgap pointer
548 * @id: bandgap sensor id
549 * @val: value (mCelsius) of a threshold
550 * @hot: desired threshold to be read. true if threshold hot, false if
553 * It will fetch the required thresholds (hot and cold) for TALERT signal.
554 * This function can be used to read t_hot or t_cold, depending on @hot value.
555 * Call this function only if bandgap features HAS(TALERT).
557 * Return: 0 if no error, -ENOTSUPP if it has no TALERT support, or the
558 * corresponding error value if some operation fails.
560 static int _ti_bandgap_read_threshold(struct ti_bandgap *bgp, int id,
563 struct temp_sensor_registers *tsr;
567 ret = ti_bandgap_validate(bgp, id);
571 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
576 tsr = bgp->conf->sensors[id].registers;
578 mask = tsr->threshold_thot_mask;
580 mask = tsr->threshold_tcold_mask;
582 temp = ti_bandgap_readl(bgp, tsr->bgap_threshold);
583 temp = (temp & mask) >> __ffs(mask);
584 ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
586 dev_err(bgp->dev, "failed to read thot\n");
597 /*** Exposed APIs ***/
600 * ti_bandgap_read_thot() - reads sensor current thot
601 * @bgp: pointer to bandgap instance
603 * @thot: resulting current thot value
605 * Return: 0 on success or the proper error code
607 int ti_bandgap_read_thot(struct ti_bandgap *bgp, int id, int *thot)
609 return _ti_bandgap_read_threshold(bgp, id, thot, true);
613 * ti_bandgap_write_thot() - sets sensor current thot
614 * @bgp: pointer to bandgap instance
616 * @val: desired thot value
618 * Return: 0 on success or the proper error code
620 int ti_bandgap_write_thot(struct ti_bandgap *bgp, int id, int val)
622 return _ti_bandgap_write_threshold(bgp, id, val, true);
626 * ti_bandgap_read_tcold() - reads sensor current tcold
627 * @bgp: pointer to bandgap instance
629 * @tcold: resulting current tcold value
631 * Return: 0 on success or the proper error code
633 int ti_bandgap_read_tcold(struct ti_bandgap *bgp, int id, int *tcold)
635 return _ti_bandgap_read_threshold(bgp, id, tcold, false);
639 * ti_bandgap_write_tcold() - sets the sensor tcold
640 * @bgp: pointer to bandgap instance
642 * @val: desired tcold value
644 * Return: 0 on success or the proper error code
646 int ti_bandgap_write_tcold(struct ti_bandgap *bgp, int id, int val)
648 return _ti_bandgap_write_threshold(bgp, id, val, false);
652 * ti_bandgap_read_counter() - read the sensor counter
653 * @bgp: pointer to bandgap instance
655 * @interval: resulting update interval in miliseconds
657 static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id,
660 struct temp_sensor_registers *tsr;
663 tsr = bgp->conf->sensors[id].registers;
664 time = ti_bandgap_readl(bgp, tsr->bgap_counter);
665 time = (time & tsr->counter_mask) >>
666 __ffs(tsr->counter_mask);
667 time = time * 1000 / bgp->clk_rate;
672 * ti_bandgap_read_counter_delay() - read the sensor counter delay
673 * @bgp: pointer to bandgap instance
675 * @interval: resulting update interval in miliseconds
677 static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id,
680 struct temp_sensor_registers *tsr;
683 tsr = bgp->conf->sensors[id].registers;
685 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
686 reg_val = (reg_val & tsr->mask_counter_delay_mask) >>
687 __ffs(tsr->mask_counter_delay_mask);
708 dev_warn(bgp->dev, "Wrong counter delay value read from register %X",
714 * ti_bandgap_read_update_interval() - read the sensor update interval
715 * @bgp: pointer to bandgap instance
717 * @interval: resulting update interval in miliseconds
719 * Return: 0 on success or the proper error code
721 int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
726 ret = ti_bandgap_validate(bgp, id);
730 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
731 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
736 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
737 ti_bandgap_read_counter(bgp, id, interval);
741 ti_bandgap_read_counter_delay(bgp, id, interval);
747 * ti_bandgap_write_counter_delay() - set the counter_delay
748 * @bgp: pointer to bandgap instance
750 * @interval: desired update interval in miliseconds
752 * Return: 0 on success or the proper error code
754 static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id,
760 case 0: /* Immediate conversion */
763 case 1: /* Conversion after ever 1ms */
766 case 10: /* Conversion after ever 10ms */
769 case 100: /* Conversion after ever 100ms */
772 case 250: /* Conversion after ever 250ms */
775 case 500: /* Conversion after ever 500ms */
779 dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval);
783 spin_lock(&bgp->lock);
784 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval);
785 spin_unlock(&bgp->lock);
791 * ti_bandgap_write_counter() - set the bandgap sensor counter
792 * @bgp: pointer to bandgap instance
794 * @interval: desired update interval in miliseconds
796 static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id,
799 interval = interval * bgp->clk_rate / 1000;
800 spin_lock(&bgp->lock);
801 RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
802 spin_unlock(&bgp->lock);
806 * ti_bandgap_write_update_interval() - set the update interval
807 * @bgp: pointer to bandgap instance
809 * @interval: desired update interval in miliseconds
811 * Return: 0 on success or the proper error code
813 int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
814 int id, u32 interval)
816 int ret = ti_bandgap_validate(bgp, id);
820 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
821 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
826 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
827 ti_bandgap_write_counter(bgp, id, interval);
831 ret = ti_bandgap_write_counter_delay(bgp, id, interval);
837 * ti_bandgap_read_temperature() - report current temperature
838 * @bgp: pointer to bandgap instance
840 * @temperature: resulting temperature
842 * Return: 0 on success or the proper error code
844 int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
850 ret = ti_bandgap_validate(bgp, id);
854 spin_lock(&bgp->lock);
855 temp = ti_bandgap_read_temp(bgp, id);
856 spin_unlock(&bgp->lock);
858 ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
868 * ti_bandgap_set_sensor_data() - helper function to store thermal
869 * framework related data.
870 * @bgp: pointer to bandgap instance
872 * @data: thermal framework related data to be stored
874 * Return: 0 on success or the proper error code
876 int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
878 int ret = ti_bandgap_validate(bgp, id);
882 bgp->regval[id].data = data;
888 * ti_bandgap_get_sensor_data() - helper function to get thermal
889 * framework related data.
890 * @bgp: pointer to bandgap instance
893 * Return: data stored by set function with sensor id on success or NULL
895 void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
897 int ret = ti_bandgap_validate(bgp, id);
901 return bgp->regval[id].data;
904 /*** Helper functions used during device initialization ***/
907 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
908 * @bgp: pointer to struct ti_bandgap
909 * @id: sensor id which it is desired to read 1 temperature
911 * Used to initialize the conversion state machine and set it to a valid
912 * state. Called during device initialization and context restore events.
917 ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
919 u32 temp = 0, counter = 1000;
921 /* Select single conversion mode */
922 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
923 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
925 /* Start of Conversion = 1 */
926 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
927 /* Wait until DTEMP is updated */
928 temp = ti_bandgap_read_temp(bgp, id);
930 while ((temp == 0) && --counter)
931 temp = ti_bandgap_read_temp(bgp, id);
932 /* REVISIT: Check correct condition for end of conversion */
934 /* Start of Conversion = 0 */
935 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
941 * ti_bandgap_set_continous_mode() - One time enabling of continuous mode
942 * @bgp: pointer to struct ti_bandgap
944 * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
945 * be used for junction temperature monitoring, it is desirable that the
946 * sensors are operational all the time, so that alerts are generated
951 static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
955 for (i = 0; i < bgp->conf->sensor_count; i++) {
956 /* Perform a single read just before enabling continuous */
957 ti_bandgap_force_single_read(bgp, i);
958 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
965 * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
966 * @bgp: pointer to struct ti_bandgap
967 * @id: id of the individual sensor
968 * @trend: Pointer to trend.
970 * This function needs to be called to fetch the temperature trend of a
971 * Particular sensor. The function computes the difference in temperature
972 * w.r.t time. For the bandgaps with built in history buffer the temperatures
973 * are read from the buffer and for those without the Buffer -ENOTSUPP is
976 * Return: 0 if no error, else return corresponding error. If no
977 * error then the trend value is passed on to trend parameter
979 int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend)
981 struct temp_sensor_registers *tsr;
982 u32 temp1, temp2, reg1, reg2;
983 int t1, t2, interval, ret = 0;
985 ret = ti_bandgap_validate(bgp, id);
989 if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) ||
990 !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
995 tsr = bgp->conf->sensors[id].registers;
997 /* Freeze and read the last 2 valid readings */
998 reg1 = tsr->ctrl_dtemp_1;
999 reg2 = tsr->ctrl_dtemp_2;
1001 /* read temperature from history buffer */
1002 temp1 = ti_bandgap_readl(bgp, reg1);
1003 temp1 &= tsr->bgap_dtemp_mask;
1005 temp2 = ti_bandgap_readl(bgp, reg2);
1006 temp2 &= tsr->bgap_dtemp_mask;
1008 /* Convert from adc values to mCelsius temperature */
1009 ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1);
1013 ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2);
1017 /* Fetch the update interval */
1018 ret = ti_bandgap_read_update_interval(bgp, id, &interval);
1019 if (ret || !interval)
1022 *trend = (t1 - t2) / interval;
1024 dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
1032 * ti_bandgap_tshut_init() - setup and initialize tshut handling
1033 * @bgp: pointer to struct ti_bandgap
1034 * @pdev: pointer to device struct platform_device
1036 * Call this function only in case the bandgap features HAS(TSHUT).
1037 * In this case, the driver needs to handle the TSHUT signal as an IRQ.
1038 * The IRQ is wired as a GPIO, and for this purpose, it is required
1039 * to specify which GPIO line is used. TSHUT IRQ is fired anytime
1040 * one of the bandgap sensors violates the TSHUT high/hot threshold.
1041 * And in that case, the system must go off.
1043 * Return: 0 if no error, else error status
1045 static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
1046 struct platform_device *pdev)
1048 int gpio_nr = bgp->tshut_gpio;
1051 /* Request for gpio_86 line */
1052 status = gpio_request(gpio_nr, "tshut");
1054 dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86);
1057 status = gpio_direction_input(gpio_nr);
1059 dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr);
1063 status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler,
1064 IRQF_TRIGGER_RISING, "tshut", NULL);
1067 dev_err(bgp->dev, "request irq failed for TSHUT");
1074 * ti_bandgap_alert_init() - setup and initialize talert handling
1075 * @bgp: pointer to struct ti_bandgap
1076 * @pdev: pointer to device struct platform_device
1078 * Call this function only in case the bandgap features HAS(TALERT).
1079 * In this case, the driver needs to handle the TALERT signals as an IRQs.
1080 * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
1081 * are violated. In these situation, the driver must reprogram the thresholds,
1082 * accordingly to specified policy.
1084 * Return: 0 if no error, else return corresponding error.
1086 static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
1087 struct platform_device *pdev)
1091 bgp->irq = platform_get_irq(pdev, 0);
1093 dev_err(&pdev->dev, "get_irq failed\n");
1096 ret = request_threaded_irq(bgp->irq, NULL,
1097 ti_bandgap_talert_irq_handler,
1098 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
1101 dev_err(&pdev->dev, "Request threaded irq failed.\n");
1108 static const struct of_device_id of_ti_bandgap_match[];
1110 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
1111 * @pdev: pointer to device struct platform_device
1113 * Used to read the device tree properties accordingly to the bandgap
1114 * matching version. Based on bandgap version and its capabilities it
1115 * will build a struct ti_bandgap out of the required DT entries.
1117 * Return: valid bandgap structure if successful, else returns ERR_PTR
1118 * return value must be verified with IS_ERR.
1120 static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
1122 struct device_node *node = pdev->dev.of_node;
1123 const struct of_device_id *of_id;
1124 struct ti_bandgap *bgp;
1125 struct resource *res;
1129 /* just for the sake */
1131 dev_err(&pdev->dev, "no platform information available\n");
1132 return ERR_PTR(-EINVAL);
1135 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
1137 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
1138 return ERR_PTR(-ENOMEM);
1141 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
1143 bgp->conf = of_id->data;
1145 /* register shadow for context save and restore */
1146 bgp->regval = devm_kzalloc(&pdev->dev, sizeof(*bgp->regval) *
1147 bgp->conf->sensor_count, GFP_KERNEL);
1149 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
1150 return ERR_PTR(-ENOMEM);
1155 void __iomem *chunk;
1157 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
1160 chunk = devm_ioremap_resource(&pdev->dev, res);
1164 return ERR_CAST(chunk);
1169 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1170 if (of_property_read_u32(node, "ti,tshut-gpio", &prop) < 0) {
1171 dev_err(&pdev->dev, "missing tshut gpio in device tree\n");
1172 return ERR_PTR(-EINVAL);
1174 bgp->tshut_gpio = prop;
1175 if (!gpio_is_valid(bgp->tshut_gpio)) {
1176 dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
1178 return ERR_PTR(-EINVAL);
1185 /*** Device driver call backs ***/
1188 int ti_bandgap_probe(struct platform_device *pdev)
1190 struct ti_bandgap *bgp;
1191 int clk_rate, ret = 0, i;
1193 bgp = ti_bandgap_build(pdev);
1194 if (IS_ERR_OR_NULL(bgp)) {
1195 dev_err(&pdev->dev, "failed to fetch platform data\n");
1196 return PTR_ERR(bgp);
1198 bgp->dev = &pdev->dev;
1200 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1201 ret = ti_bandgap_tshut_init(bgp, pdev);
1204 "failed to initialize system tshut IRQ\n");
1209 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
1210 ret = IS_ERR_OR_NULL(bgp->fclock);
1212 dev_err(&pdev->dev, "failed to request fclock reference\n");
1216 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
1217 ret = IS_ERR_OR_NULL(bgp->div_clk);
1220 "failed to request div_ts_ck clock ref\n");
1224 for (i = 0; i < bgp->conf->sensor_count; i++) {
1225 struct temp_sensor_registers *tsr;
1228 tsr = bgp->conf->sensors[i].registers;
1230 * check if the efuse has a non-zero value if not
1231 * it is an untrimmed sample and the temperatures
1232 * may not be accurate
1234 val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
1236 dev_info(&pdev->dev,
1237 "Non-trimmed BGAP, Temp not accurate\n");
1240 clk_rate = clk_round_rate(bgp->div_clk,
1241 bgp->conf->sensors[0].ts_data->max_freq);
1242 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
1243 clk_rate == 0xffffffff) {
1245 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
1249 ret = clk_set_rate(bgp->div_clk, clk_rate);
1251 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
1253 bgp->clk_rate = clk_rate;
1254 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1255 clk_prepare_enable(bgp->fclock);
1258 spin_lock_init(&bgp->lock);
1259 bgp->dev = &pdev->dev;
1260 platform_set_drvdata(pdev, bgp);
1262 ti_bandgap_power(bgp, true);
1264 /* Set default counter to 1 for now */
1265 if (TI_BANDGAP_HAS(bgp, COUNTER))
1266 for (i = 0; i < bgp->conf->sensor_count; i++)
1267 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
1269 /* Set default thresholds for alert and shutdown */
1270 for (i = 0; i < bgp->conf->sensor_count; i++) {
1271 struct temp_sensor_data *ts_data;
1273 ts_data = bgp->conf->sensors[i].ts_data;
1275 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1276 /* Set initial Talert thresholds */
1277 RMW_BITS(bgp, i, bgap_threshold,
1278 threshold_tcold_mask, ts_data->t_cold);
1279 RMW_BITS(bgp, i, bgap_threshold,
1280 threshold_thot_mask, ts_data->t_hot);
1281 /* Enable the alert events */
1282 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
1283 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
1286 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
1287 /* Set initial Tshut thresholds */
1288 RMW_BITS(bgp, i, tshut_threshold,
1289 tshut_hot_mask, ts_data->tshut_hot);
1290 RMW_BITS(bgp, i, tshut_threshold,
1291 tshut_cold_mask, ts_data->tshut_cold);
1295 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1296 ti_bandgap_set_continuous_mode(bgp);
1298 /* Set .250 seconds time as default counter */
1299 if (TI_BANDGAP_HAS(bgp, COUNTER))
1300 for (i = 0; i < bgp->conf->sensor_count; i++)
1301 RMW_BITS(bgp, i, bgap_counter, counter_mask,
1304 /* Every thing is good? Then expose the sensors */
1305 for (i = 0; i < bgp->conf->sensor_count; i++) {
1308 if (bgp->conf->sensors[i].register_cooling) {
1309 ret = bgp->conf->sensors[i].register_cooling(bgp, i);
1311 goto remove_sensors;
1314 if (bgp->conf->expose_sensor) {
1315 domain = bgp->conf->sensors[i].domain;
1316 ret = bgp->conf->expose_sensor(bgp, i, domain);
1318 goto remove_last_cooling;
1323 * Enable the Interrupts once everything is set. Otherwise irq handler
1324 * might be called as soon as it is enabled where as rest of framework
1325 * is still getting initialised.
1327 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1328 ret = ti_bandgap_talert_init(bgp, pdev);
1330 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
1331 i = bgp->conf->sensor_count;
1338 remove_last_cooling:
1339 if (bgp->conf->sensors[i].unregister_cooling)
1340 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1342 for (i--; i >= 0; i--) {
1343 if (bgp->conf->sensors[i].unregister_cooling)
1344 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1345 if (bgp->conf->remove_sensor)
1346 bgp->conf->remove_sensor(bgp, i);
1348 ti_bandgap_power(bgp, false);
1350 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1351 clk_disable_unprepare(bgp->fclock);
1353 clk_put(bgp->fclock);
1354 clk_put(bgp->div_clk);
1356 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1357 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1358 gpio_free(bgp->tshut_gpio);
1365 int ti_bandgap_remove(struct platform_device *pdev)
1367 struct ti_bandgap *bgp = platform_get_drvdata(pdev);
1370 /* First thing is to remove sensor interfaces */
1371 for (i = 0; i < bgp->conf->sensor_count; i++) {
1372 if (bgp->conf->sensors[i].unregister_cooling)
1373 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1375 if (bgp->conf->remove_sensor)
1376 bgp->conf->remove_sensor(bgp, i);
1379 ti_bandgap_power(bgp, false);
1381 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1382 clk_disable_unprepare(bgp->fclock);
1383 clk_put(bgp->fclock);
1384 clk_put(bgp->div_clk);
1386 if (TI_BANDGAP_HAS(bgp, TALERT))
1387 free_irq(bgp->irq, bgp);
1389 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1390 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1391 gpio_free(bgp->tshut_gpio);
1398 static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
1402 for (i = 0; i < bgp->conf->sensor_count; i++) {
1403 struct temp_sensor_registers *tsr;
1404 struct temp_sensor_regval *rval;
1406 rval = &bgp->regval[i];
1407 tsr = bgp->conf->sensors[i].registers;
1409 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1410 rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
1411 tsr->bgap_mode_ctrl);
1412 if (TI_BANDGAP_HAS(bgp, COUNTER))
1413 rval->bg_counter = ti_bandgap_readl(bgp,
1415 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1416 rval->bg_threshold = ti_bandgap_readl(bgp,
1417 tsr->bgap_threshold);
1418 rval->bg_ctrl = ti_bandgap_readl(bgp,
1419 tsr->bgap_mask_ctrl);
1422 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1423 rval->tshut_threshold = ti_bandgap_readl(bgp,
1424 tsr->tshut_threshold);
1430 static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
1434 for (i = 0; i < bgp->conf->sensor_count; i++) {
1435 struct temp_sensor_registers *tsr;
1436 struct temp_sensor_regval *rval;
1439 rval = &bgp->regval[i];
1440 tsr = bgp->conf->sensors[i].registers;
1442 if (TI_BANDGAP_HAS(bgp, COUNTER))
1443 val = ti_bandgap_readl(bgp, tsr->bgap_counter);
1445 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1446 ti_bandgap_writel(bgp, rval->tshut_threshold,
1447 tsr->tshut_threshold);
1448 /* Force immediate temperature measurement and update
1449 * of the DTEMP field
1451 ti_bandgap_force_single_read(bgp, i);
1453 if (TI_BANDGAP_HAS(bgp, COUNTER))
1454 ti_bandgap_writel(bgp, rval->bg_counter,
1456 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1457 ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1458 tsr->bgap_mode_ctrl);
1459 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1460 ti_bandgap_writel(bgp, rval->bg_threshold,
1461 tsr->bgap_threshold);
1462 ti_bandgap_writel(bgp, rval->bg_ctrl,
1463 tsr->bgap_mask_ctrl);
1470 static int ti_bandgap_suspend(struct device *dev)
1472 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1475 err = ti_bandgap_save_ctxt(bgp);
1476 ti_bandgap_power(bgp, false);
1478 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1479 clk_disable_unprepare(bgp->fclock);
1484 static int ti_bandgap_resume(struct device *dev)
1486 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1488 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1489 clk_prepare_enable(bgp->fclock);
1491 ti_bandgap_power(bgp, true);
1493 return ti_bandgap_restore_ctxt(bgp);
1495 static const struct dev_pm_ops ti_bandgap_dev_pm_ops = {
1496 SET_SYSTEM_SLEEP_PM_OPS(ti_bandgap_suspend,
1500 #define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
1502 #define DEV_PM_OPS NULL
1505 static const struct of_device_id of_ti_bandgap_match[] = {
1506 #ifdef CONFIG_OMAP4_THERMAL
1508 .compatible = "ti,omap4430-bandgap",
1509 .data = (void *)&omap4430_data,
1512 .compatible = "ti,omap4460-bandgap",
1513 .data = (void *)&omap4460_data,
1516 .compatible = "ti,omap4470-bandgap",
1517 .data = (void *)&omap4470_data,
1520 #ifdef CONFIG_OMAP5_THERMAL
1522 .compatible = "ti,omap5430-bandgap",
1523 .data = (void *)&omap5430_data,
1529 MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
1531 static struct platform_driver ti_bandgap_sensor_driver = {
1532 .probe = ti_bandgap_probe,
1533 .remove = ti_bandgap_remove,
1535 .name = "ti-soc-thermal",
1537 .of_match_table = of_ti_bandgap_match,
1541 module_platform_driver(ti_bandgap_sensor_driver);
1543 MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1544 MODULE_LICENSE("GPL v2");
1545 MODULE_ALIAS("platform:ti-soc-thermal");
1546 MODULE_AUTHOR("Texas Instrument Inc.");