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 * Returns 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 static int ti_bandgap_power(struct ti_bandgap *bgp, bool on)
105 if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH)) {
110 for (i = 0; i < bgp->conf->sensor_count; i++)
112 RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
119 * ti_bandgap_read_temp() - helper function to read sensor temperature
120 * @bgp: pointer to ti_bandgap structure
121 * @id: bandgap sensor id
123 * Function to concentrate the steps to read sensor temperature register.
124 * This function is desired because, depending on bandgap device version,
125 * it might be needed to freeze the bandgap state machine, before fetching
126 * the register value.
128 static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
130 struct temp_sensor_registers *tsr;
133 tsr = bgp->conf->sensors[id].registers;
134 reg = tsr->temp_sensor_ctrl;
136 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
137 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
139 * In case we cannot read from cur_dtemp / dtemp_0,
140 * then we read from the last valid temp read
142 reg = tsr->ctrl_dtemp_1;
145 /* read temperature */
146 temp = ti_bandgap_readl(bgp, reg);
147 temp &= tsr->bgap_dtemp_mask;
149 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
150 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
155 /*** IRQ handlers ***/
158 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
160 * @data: private data (struct ti_bandgap *)
162 * This is the Talert handler. Use it only if bandgap device features
163 * HAS(TALERT). This handler goes over all sensors and checks their
164 * conditions and acts accordingly. In case there are events pending,
165 * it will reset the event mask to wait for the opposite event (next event).
166 * Every time there is a new event, it will be reported to thermal layer.
168 static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
170 struct ti_bandgap *bgp = data;
171 struct temp_sensor_registers *tsr;
172 u32 t_hot = 0, t_cold = 0, ctrl;
175 spin_lock(&bgp->lock);
176 for (i = 0; i < bgp->conf->sensor_count; i++) {
177 tsr = bgp->conf->sensors[i].registers;
178 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
180 /* Read the status of t_hot */
181 t_hot = ctrl & tsr->status_hot_mask;
183 /* Read the status of t_cold */
184 t_cold = ctrl & tsr->status_cold_mask;
186 if (!t_cold && !t_hot)
189 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
191 * One TALERT interrupt: Two sources
192 * If the interrupt is due to t_hot then mask t_hot and
193 * and unmask t_cold else mask t_cold and unmask t_hot
196 ctrl &= ~tsr->mask_hot_mask;
197 ctrl |= tsr->mask_cold_mask;
199 ctrl &= ~tsr->mask_cold_mask;
200 ctrl |= tsr->mask_hot_mask;
203 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
206 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
207 __func__, bgp->conf->sensors[i].domain,
210 /* report temperature to whom may concern */
211 if (bgp->conf->report_temperature)
212 bgp->conf->report_temperature(bgp, i);
214 spin_unlock(&bgp->lock);
220 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
222 * @data: private data (unused)
224 * This is the Tshut handler. Use it only if bandgap device features
225 * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
228 static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
230 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
233 orderly_poweroff(true);
238 /*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
241 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
242 * @bgp: struct ti_bandgap pointer
243 * @adc_val: value in ADC representation
244 * @t: address where to write the resulting temperature in mCelsius
246 * Simple conversion from ADC representation to mCelsius. In case the ADC value
247 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
248 * The conversion table is indexed by the ADC values.
251 int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
253 const struct ti_bandgap_data *conf = bgp->conf;
256 /* look up for temperature in the table and return the temperature */
257 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val) {
262 *t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
269 * ti_bandgap_mcelsius_to_adc() - converts a mCelsius value to ADC scale
270 * @bgp: struct ti_bandgap pointer
271 * @temp: value in mCelsius
272 * @adc: address where to write the resulting temperature in ADC representation
274 * Simple conversion from mCelsius to ADC values. In case the temp value
275 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
276 * The conversion table is indexed by the ADC values.
279 int ti_bandgap_mcelsius_to_adc(struct ti_bandgap *bgp, long temp, int *adc)
281 const struct ti_bandgap_data *conf = bgp->conf;
282 const int *conv_table = bgp->conf->conv_table;
283 int high, low, mid, ret = 0;
286 high = conf->adc_end_val - conf->adc_start_val;
287 mid = (high + low) / 2;
289 if (temp < conv_table[low] || temp > conv_table[high]) {
295 if (temp < conv_table[mid])
299 mid = (low + high) / 2;
302 *adc = conf->adc_start_val + low;
309 * ti_bandgap_add_hyst() - add hysteresis (in mCelsius) to an ADC value
310 * @bgp: struct ti_bandgap pointer
311 * @adc_val: temperature value in ADC representation
312 * @hyst_val: hysteresis value in mCelsius
313 * @sum: address where to write the resulting temperature (in ADC scale)
315 * Adds an hysteresis value (in mCelsius) to a ADC temperature value.
316 * Returns 0 on success, -ERANGE otherwise.
319 int ti_bandgap_add_hyst(struct ti_bandgap *bgp, int adc_val, int hyst_val,
325 * Need to add in the mcelsius domain, so we have a temperature
326 * the conv_table range
328 ret = ti_bandgap_adc_to_mcelsius(bgp, adc_val, &temp);
334 ret = ti_bandgap_mcelsius_to_adc(bgp, temp, sum);
340 /*** Helper functions handling device Alert/Shutdown signals ***/
343 * ti_bandgap_unmask_interrupts() - unmasks the events of thot & tcold
344 * @bgp: struct ti_bandgap pointer
345 * @id: bandgap sensor id
346 * @t_hot: hot temperature value to trigger alert signal
347 * @t_cold: cold temperature value to trigger alert signal
349 * Checks the requested t_hot and t_cold values and configures the IRQ event
350 * masks accordingly. Call this function only if bandgap features HAS(TALERT).
352 static void ti_bandgap_unmask_interrupts(struct ti_bandgap *bgp, int id,
353 u32 t_hot, u32 t_cold)
355 struct temp_sensor_registers *tsr;
358 /* Read the current on die temperature */
359 temp = ti_bandgap_read_temp(bgp, id);
361 tsr = bgp->conf->sensors[id].registers;
362 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
365 reg_val |= tsr->mask_hot_mask;
367 reg_val &= ~tsr->mask_hot_mask;
370 reg_val |= tsr->mask_cold_mask;
372 reg_val &= ~tsr->mask_cold_mask;
373 ti_bandgap_writel(bgp, reg_val, tsr->bgap_mask_ctrl);
377 * ti_bandgap_update_alert_threshold() - sequence to update thresholds
378 * @bgp: struct ti_bandgap pointer
379 * @id: bandgap sensor id
380 * @val: value (ADC) of a new threshold
381 * @hot: desired threshold to be updated. true if threshold hot, false if
384 * It will program the required thresholds (hot and cold) for TALERT signal.
385 * This function can be used to update t_hot or t_cold, depending on @hot value.
386 * It checks the resulting t_hot and t_cold values, based on the new passed @val
387 * and configures the thresholds so that t_hot is always greater than t_cold.
388 * Call this function only if bandgap features HAS(TALERT).
390 static int ti_bandgap_update_alert_threshold(struct ti_bandgap *bgp, int id,
393 struct temp_sensor_data *ts_data = bgp->conf->sensors[id].ts_data;
394 struct temp_sensor_registers *tsr;
395 u32 thresh_val, reg_val, t_hot, t_cold;
398 tsr = bgp->conf->sensors[id].registers;
400 /* obtain the current value */
401 thresh_val = ti_bandgap_readl(bgp, tsr->bgap_threshold);
402 t_cold = (thresh_val & tsr->threshold_tcold_mask) >>
403 __ffs(tsr->threshold_tcold_mask);
404 t_hot = (thresh_val & tsr->threshold_thot_mask) >>
405 __ffs(tsr->threshold_thot_mask);
411 if (t_cold > t_hot) {
413 err = ti_bandgap_add_hyst(bgp, t_hot,
417 err = ti_bandgap_add_hyst(bgp, t_cold,
422 /* write the new threshold values */
423 reg_val = thresh_val &
424 ~(tsr->threshold_thot_mask | tsr->threshold_tcold_mask);
425 reg_val |= (t_hot << __ffs(tsr->threshold_thot_mask)) |
426 (t_cold << __ffs(tsr->threshold_tcold_mask));
427 ti_bandgap_writel(bgp, reg_val, tsr->bgap_threshold);
430 dev_err(bgp->dev, "failed to reprogram thot threshold\n");
435 ti_bandgap_unmask_interrupts(bgp, id, t_hot, t_cold);
441 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
442 * @bgp: struct ti_bandgap pointer
443 * @id: bandgap sensor id
445 * Checks if the bandgap pointer is valid and if the sensor id is also
448 static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
452 if (IS_ERR_OR_NULL(bgp)) {
453 pr_err("%s: invalid bandgap pointer\n", __func__);
458 if ((id < 0) || (id >= bgp->conf->sensor_count)) {
459 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
469 * _ti_bandgap_write_threshold() - helper to update TALERT t_cold or t_hot
470 * @bgp: struct ti_bandgap pointer
471 * @id: bandgap sensor id
472 * @val: value (mCelsius) of a new threshold
473 * @hot: desired threshold to be updated. true if threshold hot, false if
476 * It will update the required thresholds (hot and cold) for TALERT signal.
477 * This function can be used to update t_hot or t_cold, depending on @hot value.
478 * Validates the mCelsius range and update the requested threshold.
479 * Call this function only if bandgap features HAS(TALERT).
481 static int _ti_bandgap_write_threshold(struct ti_bandgap *bgp, int id, int val,
484 struct temp_sensor_data *ts_data;
485 struct temp_sensor_registers *tsr;
489 ret = ti_bandgap_validate(bgp, id);
493 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
498 ts_data = bgp->conf->sensors[id].ts_data;
499 tsr = bgp->conf->sensors[id].registers;
501 if (val < ts_data->min_temp + ts_data->hyst_val)
504 if (val > ts_data->max_temp + ts_data->hyst_val)
511 ret = ti_bandgap_mcelsius_to_adc(bgp, val, &adc_val);
515 spin_lock(&bgp->lock);
516 ret = ti_bandgap_update_alert_threshold(bgp, id, adc_val, hot);
517 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 static int _ti_bandgap_read_threshold(struct ti_bandgap *bgp, int id,
538 struct temp_sensor_registers *tsr;
542 ret = ti_bandgap_validate(bgp, id);
546 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
551 tsr = bgp->conf->sensors[id].registers;
553 mask = tsr->threshold_thot_mask;
555 mask = tsr->threshold_tcold_mask;
557 temp = ti_bandgap_readl(bgp, tsr->bgap_threshold);
558 temp = (temp & mask) >> __ffs(mask);
559 ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
561 dev_err(bgp->dev, "failed to read thot\n");
572 /*** Exposed APIs ***/
575 * ti_bandgap_read_thot() - reads sensor current thot
576 * @bgp: pointer to bandgap instance
578 * @thot: resulting current thot value
580 * returns 0 on success or the proper error code
582 int ti_bandgap_read_thot(struct ti_bandgap *bgp, int id, int *thot)
584 return _ti_bandgap_read_threshold(bgp, id, thot, true);
588 * ti_bandgap_write_thot() - sets sensor current thot
589 * @bgp: pointer to bandgap instance
591 * @val: desired thot value
593 * returns 0 on success or the proper error code
595 int ti_bandgap_write_thot(struct ti_bandgap *bgp, int id, int val)
597 return _ti_bandgap_write_threshold(bgp, id, val, true);
601 * ti_bandgap_read_tcold() - reads sensor current tcold
602 * @bgp: pointer to bandgap instance
604 * @tcold: resulting current tcold value
606 * returns 0 on success or the proper error code
608 int ti_bandgap_read_tcold(struct ti_bandgap *bgp, int id, int *tcold)
610 return _ti_bandgap_read_threshold(bgp, id, tcold, false);
614 * ti_bandgap_write_tcold() - sets the sensor tcold
615 * @bgp: pointer to bandgap instance
617 * @val: desired tcold value
619 * returns 0 on success or the proper error code
621 int ti_bandgap_write_tcold(struct ti_bandgap *bgp, int id, int val)
623 return _ti_bandgap_write_threshold(bgp, id, val, false);
627 * ti_bandgap_read_update_interval() - read the sensor update interval
628 * @bgp: pointer to bandgap instance
630 * @interval: resulting update interval in miliseconds
632 * returns 0 on success or the proper error code
634 int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
637 struct temp_sensor_registers *tsr;
641 ret = ti_bandgap_validate(bgp, id);
645 if (!TI_BANDGAP_HAS(bgp, COUNTER))
648 tsr = bgp->conf->sensors[id].registers;
649 time = ti_bandgap_readl(bgp, tsr->bgap_counter);
650 time = (time & tsr->counter_mask) >> __ffs(tsr->counter_mask);
651 time = time * 1000 / bgp->clk_rate;
659 * ti_bandgap_write_update_interval() - set the update interval
660 * @bgp: pointer to bandgap instance
662 * @interval: desired update interval in miliseconds
664 * returns 0 on success or the proper error code
666 int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
667 int id, u32 interval)
669 int ret = ti_bandgap_validate(bgp, id);
673 if (!TI_BANDGAP_HAS(bgp, COUNTER))
676 interval = interval * bgp->clk_rate / 1000;
677 spin_lock(&bgp->lock);
678 RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
679 spin_unlock(&bgp->lock);
685 * ti_bandgap_read_temperature() - report current temperature
686 * @bgp: pointer to bandgap instance
688 * @temperature: resulting temperature
690 * returns 0 on success or the proper error code
692 int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
698 ret = ti_bandgap_validate(bgp, id);
702 spin_lock(&bgp->lock);
703 temp = ti_bandgap_read_temp(bgp, id);
704 spin_unlock(&bgp->lock);
706 ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
716 * ti_bandgap_set_sensor_data() - helper function to store thermal
717 * framework related data.
718 * @bgp: pointer to bandgap instance
720 * @data: thermal framework related data to be stored
722 * returns 0 on success or the proper error code
724 int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
726 int ret = ti_bandgap_validate(bgp, id);
730 bgp->regval[id].data = data;
736 * ti_bandgap_get_sensor_data() - helper function to get thermal
737 * framework related data.
738 * @bgp: pointer to bandgap instance
741 * returns data stored by set function with sensor id on success or NULL
743 void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
745 int ret = ti_bandgap_validate(bgp, id);
749 return bgp->regval[id].data;
752 /*** Helper functions used during device initialization ***/
755 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
756 * @bgp: pointer to struct ti_bandgap
757 * @id: sensor id which it is desired to read 1 temperature
759 * Used to initialize the conversion state machine and set it to a valid
760 * state. Called during device initialization and context restore events.
763 ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
765 u32 temp = 0, counter = 1000;
767 /* Select single conversion mode */
768 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
769 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
771 /* Start of Conversion = 1 */
772 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
773 /* Wait until DTEMP is updated */
774 temp = ti_bandgap_read_temp(bgp, id);
776 while ((temp == 0) && --counter)
777 temp = ti_bandgap_read_temp(bgp, id);
778 /* REVISIT: Check correct condition for end of conversion */
780 /* Start of Conversion = 0 */
781 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
787 * ti_bandgap_set_continous_mode() - One time enabling of continuous mode
788 * @bgp: pointer to struct ti_bandgap
790 * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
791 * be used for junction temperature monitoring, it is desirable that the
792 * sensors are operational all the time, so that alerts are generated
795 static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
799 for (i = 0; i < bgp->conf->sensor_count; i++) {
800 /* Perform a single read just before enabling continuous */
801 ti_bandgap_force_single_read(bgp, i);
802 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
809 * ti_bandgap_tshut_init() - setup and initialize tshut handling
810 * @bgp: pointer to struct ti_bandgap
811 * @pdev: pointer to device struct platform_device
813 * Call this function only in case the bandgap features HAS(TSHUT).
814 * In this case, the driver needs to handle the TSHUT signal as an IRQ.
815 * The IRQ is wired as a GPIO, and for this purpose, it is required
816 * to specify which GPIO line is used. TSHUT IRQ is fired anytime
817 * one of the bandgap sensors violates the TSHUT high/hot threshold.
818 * And in that case, the system must go off.
820 static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
821 struct platform_device *pdev)
823 int gpio_nr = bgp->tshut_gpio;
826 /* Request for gpio_86 line */
827 status = gpio_request(gpio_nr, "tshut");
829 dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86);
832 status = gpio_direction_input(gpio_nr);
834 dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr);
838 status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler,
839 IRQF_TRIGGER_RISING, "tshut", NULL);
842 dev_err(bgp->dev, "request irq failed for TSHUT");
849 * ti_bandgap_alert_init() - setup and initialize talert handling
850 * @bgp: pointer to struct ti_bandgap
851 * @pdev: pointer to device struct platform_device
853 * Call this function only in case the bandgap features HAS(TALERT).
854 * In this case, the driver needs to handle the TALERT signals as an IRQs.
855 * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
856 * are violated. In these situation, the driver must reprogram the thresholds,
857 * accordingly to specified policy.
859 static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
860 struct platform_device *pdev)
864 bgp->irq = platform_get_irq(pdev, 0);
866 dev_err(&pdev->dev, "get_irq failed\n");
869 ret = request_threaded_irq(bgp->irq, NULL,
870 ti_bandgap_talert_irq_handler,
871 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
874 dev_err(&pdev->dev, "Request threaded irq failed.\n");
881 static const struct of_device_id of_ti_bandgap_match[];
883 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
884 * @pdev: pointer to device struct platform_device
886 * Used to read the device tree properties accordingly to the bandgap
887 * matching version. Based on bandgap version and its capabilities it
888 * will build a struct ti_bandgap out of the required DT entries.
890 static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
892 struct device_node *node = pdev->dev.of_node;
893 const struct of_device_id *of_id;
894 struct ti_bandgap *bgp;
895 struct resource *res;
899 /* just for the sake */
901 dev_err(&pdev->dev, "no platform information available\n");
902 return ERR_PTR(-EINVAL);
905 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
907 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
908 return ERR_PTR(-ENOMEM);
911 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
913 bgp->conf = of_id->data;
915 /* register shadow for context save and restore */
916 bgp->regval = devm_kzalloc(&pdev->dev, sizeof(*bgp->regval) *
917 bgp->conf->sensor_count, GFP_KERNEL);
919 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
920 return ERR_PTR(-ENOMEM);
927 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
930 chunk = devm_ioremap_resource(&pdev->dev, res);
934 return ERR_CAST(chunk);
939 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
940 if (of_property_read_u32(node, "ti,tshut-gpio", &prop) < 0) {
941 dev_err(&pdev->dev, "missing tshut gpio in device tree\n");
942 return ERR_PTR(-EINVAL);
944 bgp->tshut_gpio = prop;
945 if (!gpio_is_valid(bgp->tshut_gpio)) {
946 dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
948 return ERR_PTR(-EINVAL);
955 /*** Device driver call backs ***/
958 int ti_bandgap_probe(struct platform_device *pdev)
960 struct ti_bandgap *bgp;
961 int clk_rate, ret = 0, i;
963 bgp = ti_bandgap_build(pdev);
964 if (IS_ERR_OR_NULL(bgp)) {
965 dev_err(&pdev->dev, "failed to fetch platform data\n");
968 bgp->dev = &pdev->dev;
970 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
971 ret = ti_bandgap_tshut_init(bgp, pdev);
974 "failed to initialize system tshut IRQ\n");
979 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
980 ret = IS_ERR_OR_NULL(bgp->fclock);
982 dev_err(&pdev->dev, "failed to request fclock reference\n");
986 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
987 ret = IS_ERR_OR_NULL(bgp->div_clk);
990 "failed to request div_ts_ck clock ref\n");
994 for (i = 0; i < bgp->conf->sensor_count; i++) {
995 struct temp_sensor_registers *tsr;
998 tsr = bgp->conf->sensors[i].registers;
1000 * check if the efuse has a non-zero value if not
1001 * it is an untrimmed sample and the temperatures
1002 * may not be accurate
1004 val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
1006 dev_info(&pdev->dev,
1007 "Non-trimmed BGAP, Temp not accurate\n");
1010 clk_rate = clk_round_rate(bgp->div_clk,
1011 bgp->conf->sensors[0].ts_data->max_freq);
1012 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
1013 clk_rate == 0xffffffff) {
1015 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
1019 ret = clk_set_rate(bgp->div_clk, clk_rate);
1021 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
1023 bgp->clk_rate = clk_rate;
1024 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1025 clk_prepare_enable(bgp->fclock);
1028 spin_lock_init(&bgp->lock);
1029 bgp->dev = &pdev->dev;
1030 platform_set_drvdata(pdev, bgp);
1032 ti_bandgap_power(bgp, true);
1034 /* Set default counter to 1 for now */
1035 if (TI_BANDGAP_HAS(bgp, COUNTER))
1036 for (i = 0; i < bgp->conf->sensor_count; i++)
1037 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
1039 /* Set default thresholds for alert and shutdown */
1040 for (i = 0; i < bgp->conf->sensor_count; i++) {
1041 struct temp_sensor_data *ts_data;
1043 ts_data = bgp->conf->sensors[i].ts_data;
1045 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1046 /* Set initial Talert thresholds */
1047 RMW_BITS(bgp, i, bgap_threshold,
1048 threshold_tcold_mask, ts_data->t_cold);
1049 RMW_BITS(bgp, i, bgap_threshold,
1050 threshold_thot_mask, ts_data->t_hot);
1051 /* Enable the alert events */
1052 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
1053 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
1056 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
1057 /* Set initial Tshut thresholds */
1058 RMW_BITS(bgp, i, tshut_threshold,
1059 tshut_hot_mask, ts_data->tshut_hot);
1060 RMW_BITS(bgp, i, tshut_threshold,
1061 tshut_cold_mask, ts_data->tshut_cold);
1065 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1066 ti_bandgap_set_continuous_mode(bgp);
1068 /* Set .250 seconds time as default counter */
1069 if (TI_BANDGAP_HAS(bgp, COUNTER))
1070 for (i = 0; i < bgp->conf->sensor_count; i++)
1071 RMW_BITS(bgp, i, bgap_counter, counter_mask,
1074 /* Every thing is good? Then expose the sensors */
1075 for (i = 0; i < bgp->conf->sensor_count; i++) {
1078 if (bgp->conf->sensors[i].register_cooling)
1079 bgp->conf->sensors[i].register_cooling(bgp, i);
1081 domain = bgp->conf->sensors[i].domain;
1082 if (bgp->conf->expose_sensor)
1083 bgp->conf->expose_sensor(bgp, i, domain);
1087 * Enable the Interrupts once everything is set. Otherwise irq handler
1088 * might be called as soon as it is enabled where as rest of framework
1089 * is still getting initialised.
1091 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1092 ret = ti_bandgap_talert_init(bgp, pdev);
1094 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
1095 i = bgp->conf->sensor_count;
1103 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1104 clk_disable_unprepare(bgp->fclock);
1106 clk_put(bgp->fclock);
1107 clk_put(bgp->div_clk);
1109 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1110 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1111 gpio_free(bgp->tshut_gpio);
1118 int ti_bandgap_remove(struct platform_device *pdev)
1120 struct ti_bandgap *bgp = platform_get_drvdata(pdev);
1123 /* First thing is to remove sensor interfaces */
1124 for (i = 0; i < bgp->conf->sensor_count; i++) {
1125 if (bgp->conf->sensors[i].register_cooling)
1126 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1128 if (bgp->conf->remove_sensor)
1129 bgp->conf->remove_sensor(bgp, i);
1132 ti_bandgap_power(bgp, false);
1134 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1135 clk_disable_unprepare(bgp->fclock);
1136 clk_put(bgp->fclock);
1137 clk_put(bgp->div_clk);
1139 if (TI_BANDGAP_HAS(bgp, TALERT))
1140 free_irq(bgp->irq, bgp);
1142 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1143 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1144 gpio_free(bgp->tshut_gpio);
1151 static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
1155 for (i = 0; i < bgp->conf->sensor_count; i++) {
1156 struct temp_sensor_registers *tsr;
1157 struct temp_sensor_regval *rval;
1159 rval = &bgp->regval[i];
1160 tsr = bgp->conf->sensors[i].registers;
1162 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1163 rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
1164 tsr->bgap_mode_ctrl);
1165 if (TI_BANDGAP_HAS(bgp, COUNTER))
1166 rval->bg_counter = ti_bandgap_readl(bgp,
1168 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1169 rval->bg_threshold = ti_bandgap_readl(bgp,
1170 tsr->bgap_threshold);
1171 rval->bg_ctrl = ti_bandgap_readl(bgp,
1172 tsr->bgap_mask_ctrl);
1175 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1176 rval->tshut_threshold = ti_bandgap_readl(bgp,
1177 tsr->tshut_threshold);
1183 static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
1187 for (i = 0; i < bgp->conf->sensor_count; i++) {
1188 struct temp_sensor_registers *tsr;
1189 struct temp_sensor_regval *rval;
1192 rval = &bgp->regval[i];
1193 tsr = bgp->conf->sensors[i].registers;
1195 if (TI_BANDGAP_HAS(bgp, COUNTER))
1196 val = ti_bandgap_readl(bgp, tsr->bgap_counter);
1198 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1199 ti_bandgap_writel(bgp, rval->tshut_threshold,
1200 tsr->tshut_threshold);
1201 /* Force immediate temperature measurement and update
1202 * of the DTEMP field
1204 ti_bandgap_force_single_read(bgp, i);
1206 if (TI_BANDGAP_HAS(bgp, COUNTER))
1207 ti_bandgap_writel(bgp, rval->bg_counter,
1209 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1210 ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1211 tsr->bgap_mode_ctrl);
1212 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1213 ti_bandgap_writel(bgp, rval->bg_threshold,
1214 tsr->bgap_threshold);
1215 ti_bandgap_writel(bgp, rval->bg_ctrl,
1216 tsr->bgap_mask_ctrl);
1223 static int ti_bandgap_suspend(struct device *dev)
1225 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1228 err = ti_bandgap_save_ctxt(bgp);
1229 ti_bandgap_power(bgp, false);
1231 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1232 clk_disable_unprepare(bgp->fclock);
1237 static int ti_bandgap_resume(struct device *dev)
1239 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1241 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1242 clk_prepare_enable(bgp->fclock);
1244 ti_bandgap_power(bgp, true);
1246 return ti_bandgap_restore_ctxt(bgp);
1248 static const struct dev_pm_ops ti_bandgap_dev_pm_ops = {
1249 SET_SYSTEM_SLEEP_PM_OPS(ti_bandgap_suspend,
1253 #define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
1255 #define DEV_PM_OPS NULL
1258 static const struct of_device_id of_ti_bandgap_match[] = {
1259 #ifdef CONFIG_OMAP4_THERMAL
1261 .compatible = "ti,omap4430-bandgap",
1262 .data = (void *)&omap4430_data,
1265 .compatible = "ti,omap4460-bandgap",
1266 .data = (void *)&omap4460_data,
1269 .compatible = "ti,omap4470-bandgap",
1270 .data = (void *)&omap4470_data,
1273 #ifdef CONFIG_OMAP5_THERMAL
1275 .compatible = "ti,omap5430-bandgap",
1276 .data = (void *)&omap5430_data,
1282 MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
1284 static struct platform_driver ti_bandgap_sensor_driver = {
1285 .probe = ti_bandgap_probe,
1286 .remove = ti_bandgap_remove,
1288 .name = "ti-soc-thermal",
1290 .of_match_table = of_ti_bandgap_match,
1294 module_platform_driver(ti_bandgap_sensor_driver);
1296 MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1297 MODULE_LICENSE("GPL v2");
1298 MODULE_ALIAS("platform:ti-soc-thermal");
1299 MODULE_AUTHOR("Texas Instrument Inc.");