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))
108 for (i = 0; i < bgp->conf->sensor_count; i++)
110 RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
117 * ti_bandgap_read_temp() - helper function to read sensor temperature
118 * @bgp: pointer to ti_bandgap structure
119 * @id: bandgap sensor id
121 * Function to concentrate the steps to read sensor temperature register.
122 * This function is desired because, depending on bandgap device version,
123 * it might be needed to freeze the bandgap state machine, before fetching
124 * the register value.
126 static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
128 struct temp_sensor_registers *tsr;
131 tsr = bgp->conf->sensors[id].registers;
132 reg = tsr->temp_sensor_ctrl;
134 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
135 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
137 * In case we cannot read from cur_dtemp / dtemp_0,
138 * then we read from the last valid temp read
140 reg = tsr->ctrl_dtemp_1;
143 /* read temperature */
144 temp = ti_bandgap_readl(bgp, reg);
145 temp &= tsr->bgap_dtemp_mask;
147 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
148 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
153 /*** IRQ handlers ***/
156 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
158 * @data: private data (struct ti_bandgap *)
160 * This is the Talert handler. Use it only if bandgap device features
161 * HAS(TALERT). This handler goes over all sensors and checks their
162 * conditions and acts accordingly. In case there are events pending,
163 * it will reset the event mask to wait for the opposite event (next event).
164 * Every time there is a new event, it will be reported to thermal layer.
166 static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
168 struct ti_bandgap *bgp = data;
169 struct temp_sensor_registers *tsr;
170 u32 t_hot = 0, t_cold = 0, ctrl;
173 spin_lock(&bgp->lock);
174 for (i = 0; i < bgp->conf->sensor_count; i++) {
175 tsr = bgp->conf->sensors[i].registers;
176 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
178 /* Read the status of t_hot */
179 t_hot = ctrl & tsr->status_hot_mask;
181 /* Read the status of t_cold */
182 t_cold = ctrl & tsr->status_cold_mask;
184 if (!t_cold && !t_hot)
187 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
189 * One TALERT interrupt: Two sources
190 * If the interrupt is due to t_hot then mask t_hot and
191 * and unmask t_cold else mask t_cold and unmask t_hot
194 ctrl &= ~tsr->mask_hot_mask;
195 ctrl |= tsr->mask_cold_mask;
197 ctrl &= ~tsr->mask_cold_mask;
198 ctrl |= tsr->mask_hot_mask;
201 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
204 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
205 __func__, bgp->conf->sensors[i].domain,
208 /* report temperature to whom may concern */
209 if (bgp->conf->report_temperature)
210 bgp->conf->report_temperature(bgp, i);
212 spin_unlock(&bgp->lock);
218 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
220 * @data: private data (unused)
222 * This is the Tshut handler. Use it only if bandgap device features
223 * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
226 static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
228 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
231 orderly_poweroff(true);
236 /*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
239 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
240 * @bgp: struct ti_bandgap pointer
241 * @adc_val: value in ADC representation
242 * @t: address where to write the resulting temperature in mCelsius
244 * Simple conversion from ADC representation to mCelsius. In case the ADC value
245 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
246 * The conversion table is indexed by the ADC values.
249 int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
251 const struct ti_bandgap_data *conf = bgp->conf;
254 /* look up for temperature in the table and return the temperature */
255 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val) {
260 *t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
267 * ti_bandgap_mcelsius_to_adc() - converts a mCelsius value to ADC scale
268 * @bgp: struct ti_bandgap pointer
269 * @temp: value in mCelsius
270 * @adc: address where to write the resulting temperature in ADC representation
272 * Simple conversion from mCelsius to ADC values. In case the temp value
273 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
274 * The conversion table is indexed by the ADC values.
277 int ti_bandgap_mcelsius_to_adc(struct ti_bandgap *bgp, long temp, int *adc)
279 const struct ti_bandgap_data *conf = bgp->conf;
280 const int *conv_table = bgp->conf->conv_table;
281 int high, low, mid, ret = 0;
284 high = conf->adc_end_val - conf->adc_start_val;
285 mid = (high + low) / 2;
287 if (temp < conv_table[low] || temp > conv_table[high]) {
293 if (temp < conv_table[mid])
297 mid = (low + high) / 2;
300 *adc = conf->adc_start_val + low;
307 * ti_bandgap_add_hyst() - add hysteresis (in mCelsius) to an ADC value
308 * @bgp: struct ti_bandgap pointer
309 * @adc_val: temperature value in ADC representation
310 * @hyst_val: hysteresis value in mCelsius
311 * @sum: address where to write the resulting temperature (in ADC scale)
313 * Adds an hysteresis value (in mCelsius) to a ADC temperature value.
314 * Returns 0 on success, -ERANGE otherwise.
317 int ti_bandgap_add_hyst(struct ti_bandgap *bgp, int adc_val, int hyst_val,
323 * Need to add in the mcelsius domain, so we have a temperature
324 * the conv_table range
326 ret = ti_bandgap_adc_to_mcelsius(bgp, adc_val, &temp);
332 ret = ti_bandgap_mcelsius_to_adc(bgp, temp, sum);
338 /*** Helper functions handling device Alert/Shutdown signals ***/
341 * ti_bandgap_unmask_interrupts() - unmasks the events of thot & tcold
342 * @bgp: struct ti_bandgap pointer
343 * @t_hot: hot temperature value to trigger alert signal
344 * @t_cold: cold temperature value to trigger alert signal
346 * Checks the requested t_hot and t_cold values and configures the IRQ event
347 * masks accordingly. Call this function only if bandgap features HAS(TALERT).
349 static void ti_bandgap_unmask_interrupts(struct ti_bandgap *bgp, int id,
350 u32 t_hot, u32 t_cold)
352 struct temp_sensor_registers *tsr;
355 /* Read the current on die temperature */
356 temp = ti_bandgap_read_temp(bgp, id);
358 tsr = bgp->conf->sensors[id].registers;
359 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
362 reg_val |= tsr->mask_hot_mask;
364 reg_val &= ~tsr->mask_hot_mask;
367 reg_val |= tsr->mask_cold_mask;
369 reg_val &= ~tsr->mask_cold_mask;
370 ti_bandgap_writel(bgp, reg_val, tsr->bgap_mask_ctrl);
374 * ti_bandgap_update_alert_threshold() - sequence to update thresholds
375 * @bgp: struct ti_bandgap pointer
376 * @id: bandgap sensor id
377 * @val: value (ADC) of a new threshold
378 * @hot: desired threshold to be updated. true if threshold hot, false if
381 * It will program the required thresholds (hot and cold) for TALERT signal.
382 * This function can be used to update t_hot or t_cold, depending on @hot value.
383 * It checks the resulting t_hot and t_cold values, based on the new passed @val
384 * and configures the thresholds so that t_hot is always greater than t_cold.
385 * Call this function only if bandgap features HAS(TALERT).
387 static int ti_bandgap_update_alert_threshold(struct ti_bandgap *bgp, int id,
390 struct temp_sensor_data *ts_data = bgp->conf->sensors[id].ts_data;
391 struct temp_sensor_registers *tsr;
392 u32 thresh_val, reg_val, t_hot, t_cold;
395 tsr = bgp->conf->sensors[id].registers;
397 /* obtain the current value */
398 thresh_val = ti_bandgap_readl(bgp, tsr->bgap_threshold);
399 t_cold = (thresh_val & tsr->threshold_tcold_mask) >>
400 __ffs(tsr->threshold_tcold_mask);
401 t_hot = (thresh_val & tsr->threshold_thot_mask) >>
402 __ffs(tsr->threshold_thot_mask);
408 if (t_cold < t_hot) {
410 err = ti_bandgap_add_hyst(bgp, t_hot,
414 err = ti_bandgap_add_hyst(bgp, t_cold,
419 /* write the new threshold values */
420 reg_val = thresh_val & ~tsr->threshold_thot_mask;
421 reg_val |= (t_hot << __ffs(tsr->threshold_thot_mask));
422 reg_val |= thresh_val & ~tsr->threshold_tcold_mask;
423 reg_val |= (t_cold << __ffs(tsr->threshold_tcold_mask));
424 ti_bandgap_writel(bgp, reg_val, tsr->bgap_threshold);
427 dev_err(bgp->dev, "failed to reprogram thot threshold\n");
432 ti_bandgap_unmask_interrupts(bgp, id, t_hot, t_cold);
438 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
439 * @bgp: struct ti_bandgap pointer
440 * @id: bandgap sensor id
442 * Checks if the bandgap pointer is valid and if the sensor id is also
445 static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
449 if (IS_ERR_OR_NULL(bgp)) {
450 pr_err("%s: invalid bandgap pointer\n", __func__);
455 if ((id < 0) || (id >= bgp->conf->sensor_count)) {
456 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
466 * _ti_bandgap_write_threshold() - helper to update TALERT t_cold or t_hot
467 * @bgp: struct ti_bandgap pointer
468 * @id: bandgap sensor id
469 * @val: value (mCelsius) of a new threshold
470 * @hot: desired threshold to be updated. true if threshold hot, false if
473 * It will update the required thresholds (hot and cold) for TALERT signal.
474 * This function can be used to update t_hot or t_cold, depending on @hot value.
475 * Validates the mCelsius range and update the requested threshold.
476 * Call this function only if bandgap features HAS(TALERT).
478 static int _ti_bandgap_write_threshold(struct ti_bandgap *bgp, int id, int val,
481 struct temp_sensor_data *ts_data;
482 struct temp_sensor_registers *tsr;
486 ret = ti_bandgap_validate(bgp, id);
490 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
495 ts_data = bgp->conf->sensors[id].ts_data;
496 tsr = bgp->conf->sensors[id].registers;
498 if (val < ts_data->min_temp + ts_data->hyst_val)
501 if (val > ts_data->max_temp + ts_data->hyst_val)
508 ret = ti_bandgap_mcelsius_to_adc(bgp, val, &adc_val);
512 spin_lock(&bgp->lock);
513 ti_bandgap_update_alert_threshold(bgp, id, adc_val, hot);
514 spin_unlock(&bgp->lock);
521 * _ti_bandgap_read_threshold() - helper to read TALERT t_cold or t_hot
522 * @bgp: struct ti_bandgap pointer
523 * @id: bandgap sensor id
524 * @val: value (mCelsius) of a threshold
525 * @hot: desired threshold to be read. true if threshold hot, false if
528 * It will fetch the required thresholds (hot and cold) for TALERT signal.
529 * This function can be used to read t_hot or t_cold, depending on @hot value.
530 * Call this function only if bandgap features HAS(TALERT).
532 static int _ti_bandgap_read_threshold(struct ti_bandgap *bgp, int id,
535 struct temp_sensor_registers *tsr;
539 ret = ti_bandgap_validate(bgp, id);
543 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
548 tsr = bgp->conf->sensors[id].registers;
550 mask = tsr->threshold_thot_mask;
552 mask = tsr->threshold_tcold_mask;
554 temp = ti_bandgap_readl(bgp, tsr->bgap_threshold);
555 temp = (temp & mask) >> __ffs(mask);
556 ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
558 dev_err(bgp->dev, "failed to read thot\n");
569 /*** Exposed APIs ***/
572 * ti_bandgap_read_thot() - reads sensor current thot
573 * @bgp - pointer to bandgap instance
575 * @thot - resulting current thot value
577 * returns 0 on success or the proper error code
579 int ti_bandgap_read_thot(struct ti_bandgap *bgp, int id, int *thot)
581 return _ti_bandgap_read_threshold(bgp, id, thot, true);
585 * ti_bandgap_write_thot() - sets sensor current thot
586 * @bgp - pointer to bandgap instance
588 * @val - desired thot value
590 * returns 0 on success or the proper error code
592 int ti_bandgap_write_thot(struct ti_bandgap *bgp, int id, int val)
594 return _ti_bandgap_write_threshold(bgp, id, val, true);
598 * ti_bandgap_read_tcold() - reads sensor current tcold
599 * @bgp - pointer to bandgap instance
601 * @tcold - resulting current tcold value
603 * returns 0 on success or the proper error code
605 int ti_bandgap_read_tcold(struct ti_bandgap *bgp, int id, int *tcold)
607 return _ti_bandgap_read_threshold(bgp, id, tcold, false);
611 * ti_bandgap_write_tcold() - sets the sensor tcold
612 * @bgp - pointer to bandgap instance
614 * @val - desired tcold value
616 * returns 0 on success or the proper error code
618 int ti_bandgap_write_tcold(struct ti_bandgap *bgp, int id, int val)
620 return _ti_bandgap_write_threshold(bgp, id, val, false);
624 * ti_bandgap_read_update_interval() - read the sensor update interval
625 * @bgp - pointer to bandgap instance
627 * @interval - resulting update interval in miliseconds
629 * returns 0 on success or the proper error code
631 int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
634 struct temp_sensor_registers *tsr;
638 ret = ti_bandgap_validate(bgp, id);
642 if (!TI_BANDGAP_HAS(bgp, COUNTER))
645 tsr = bgp->conf->sensors[id].registers;
646 time = ti_bandgap_readl(bgp, tsr->bgap_counter);
647 time = (time & tsr->counter_mask) >> __ffs(tsr->counter_mask);
648 time = time * 1000 / bgp->clk_rate;
656 * ti_bandgap_write_update_interval() - set the update interval
657 * @bgp - pointer to bandgap instance
659 * @interval - desired update interval in miliseconds
661 * returns 0 on success or the proper error code
663 int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
664 int id, u32 interval)
666 int ret = ti_bandgap_validate(bgp, id);
670 if (!TI_BANDGAP_HAS(bgp, COUNTER))
673 interval = interval * bgp->clk_rate / 1000;
674 spin_lock(&bgp->lock);
675 RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
676 spin_unlock(&bgp->lock);
682 * ti_bandgap_read_temperature() - report current temperature
683 * @bgp - pointer to bandgap instance
685 * @temperature - resulting temperature
687 * returns 0 on success or the proper error code
689 int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
695 ret = ti_bandgap_validate(bgp, id);
699 spin_lock(&bgp->lock);
700 temp = ti_bandgap_read_temp(bgp, id);
701 spin_unlock(&bgp->lock);
703 ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
713 * ti_bandgap_set_sensor_data() - helper function to store thermal
714 * framework related data.
715 * @bgp - pointer to bandgap instance
717 * @data - thermal framework related data to be stored
719 * returns 0 on success or the proper error code
721 int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
723 int ret = ti_bandgap_validate(bgp, id);
727 bgp->regval[id].data = data;
733 * ti_bandgap_get_sensor_data() - helper function to get thermal
734 * framework related data.
735 * @bgp - pointer to bandgap instance
738 * returns data stored by set function with sensor id on success or NULL
740 void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
742 int ret = ti_bandgap_validate(bgp, id);
746 return bgp->regval[id].data;
749 /*** Helper functions used during device initialization ***/
752 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
753 * @bgp: pointer to struct ti_bandgap
754 * @id: sensor id which it is desired to read 1 temperature
756 * Used to initialize the conversion state machine and set it to a valid
757 * state. Called during device initialization and context restore events.
760 ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
762 u32 temp = 0, counter = 1000;
764 /* Select single conversion mode */
765 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
766 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
768 /* Start of Conversion = 1 */
769 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
770 /* Wait until DTEMP is updated */
771 temp = ti_bandgap_read_temp(bgp, id);
773 while ((temp == 0) && --counter)
774 temp = ti_bandgap_read_temp(bgp, id);
775 /* REVISIT: Check correct condition for end of conversion */
777 /* Start of Conversion = 0 */
778 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
784 * ti_bandgap_set_continous_mode() - One time enabling of continuous mode
785 * @bgp: pointer to struct ti_bandgap
787 * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
788 * be used for junction temperature monitoring, it is desirable that the
789 * sensors are operational all the time, so that alerts are generated
792 static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
796 for (i = 0; i < bgp->conf->sensor_count; i++) {
797 /* Perform a single read just before enabling continuous */
798 ti_bandgap_force_single_read(bgp, i);
799 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
806 * ti_bandgap_tshut_init() - setup and initialize tshut handling
807 * @bgp: pointer to struct ti_bandgap
808 * @pdev: pointer to device struct platform_device
810 * Call this function only in case the bandgap features HAS(TSHUT).
811 * In this case, the driver needs to handle the TSHUT signal as an IRQ.
812 * The IRQ is wired as a GPIO, and for this purpose, it is required
813 * to specify which GPIO line is used. TSHUT IRQ is fired anytime
814 * one of the bandgap sensors violates the TSHUT high/hot threshold.
815 * And in that case, the system must go off.
817 static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
818 struct platform_device *pdev)
820 int gpio_nr = bgp->tshut_gpio;
823 /* Request for gpio_86 line */
824 status = gpio_request(gpio_nr, "tshut");
826 dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86);
829 status = gpio_direction_input(gpio_nr);
831 dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr);
835 status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler,
836 IRQF_TRIGGER_RISING, "tshut", NULL);
839 dev_err(bgp->dev, "request irq failed for TSHUT");
846 * ti_bandgap_alert_init() - setup and initialize talert handling
847 * @bgp: pointer to struct ti_bandgap
848 * @pdev: pointer to device struct platform_device
850 * Call this function only in case the bandgap features HAS(TALERT).
851 * In this case, the driver needs to handle the TALERT signals as an IRQs.
852 * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
853 * are violated. In these situation, the driver must reprogram the thresholds,
854 * accordingly to specified policy.
856 static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
857 struct platform_device *pdev)
861 bgp->irq = platform_get_irq(pdev, 0);
863 dev_err(&pdev->dev, "get_irq failed\n");
866 ret = request_threaded_irq(bgp->irq, NULL,
867 ti_bandgap_talert_irq_handler,
868 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
871 dev_err(&pdev->dev, "Request threaded irq failed.\n");
879 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
880 * @bgp: pointer to struct ti_bandgap
881 * @pdev: pointer to device struct platform_device
883 * Used to read the device tree properties accordingly to the bandgap
884 * matching version. Based on bandgap version and its capabilities it
885 * will build a struct ti_bandgap out of the required DT entries.
887 static const struct of_device_id of_ti_bandgap_match[];
888 static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
890 struct device_node *node = pdev->dev.of_node;
891 const struct of_device_id *of_id;
892 struct ti_bandgap *bgp;
893 struct resource *res;
897 /* just for the sake */
899 dev_err(&pdev->dev, "no platform information available\n");
900 return ERR_PTR(-EINVAL);
903 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
905 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
906 return ERR_PTR(-ENOMEM);
909 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
911 bgp->conf = of_id->data;
913 /* register shadow for context save and restore */
914 bgp->regval = devm_kzalloc(&pdev->dev, sizeof(*bgp->regval) *
915 bgp->conf->sensor_count, GFP_KERNEL);
917 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
918 return ERR_PTR(-ENOMEM);
925 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
928 chunk = devm_ioremap_resource(&pdev->dev, res);
932 return ERR_CAST(chunk);
937 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
938 if (of_property_read_u32(node, "ti,tshut-gpio", &prop) < 0) {
939 dev_err(&pdev->dev, "missing tshut gpio in device tree\n");
940 return ERR_PTR(-EINVAL);
942 bgp->tshut_gpio = prop;
943 if (!gpio_is_valid(bgp->tshut_gpio)) {
944 dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
946 return ERR_PTR(-EINVAL);
953 /*** Device driver call backs ***/
956 int ti_bandgap_probe(struct platform_device *pdev)
958 struct ti_bandgap *bgp;
959 int clk_rate, ret = 0, i;
961 bgp = ti_bandgap_build(pdev);
962 if (IS_ERR_OR_NULL(bgp)) {
963 dev_err(&pdev->dev, "failed to fetch platform data\n");
966 bgp->dev = &pdev->dev;
968 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
969 ret = ti_bandgap_tshut_init(bgp, pdev);
972 "failed to initialize system tshut IRQ\n");
977 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
978 ret = IS_ERR_OR_NULL(bgp->fclock);
980 dev_err(&pdev->dev, "failed to request fclock reference\n");
984 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
985 ret = IS_ERR_OR_NULL(bgp->div_clk);
988 "failed to request div_ts_ck clock ref\n");
992 for (i = 0; i < bgp->conf->sensor_count; i++) {
993 struct temp_sensor_registers *tsr;
996 tsr = bgp->conf->sensors[i].registers;
998 * check if the efuse has a non-zero value if not
999 * it is an untrimmed sample and the temperatures
1000 * may not be accurate
1002 val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
1004 dev_info(&pdev->dev,
1005 "Non-trimmed BGAP, Temp not accurate\n");
1008 clk_rate = clk_round_rate(bgp->div_clk,
1009 bgp->conf->sensors[0].ts_data->max_freq);
1010 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
1011 clk_rate == 0xffffffff) {
1013 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
1017 ret = clk_set_rate(bgp->div_clk, clk_rate);
1019 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
1021 bgp->clk_rate = clk_rate;
1022 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1023 clk_prepare_enable(bgp->fclock);
1026 spin_lock_init(&bgp->lock);
1027 bgp->dev = &pdev->dev;
1028 platform_set_drvdata(pdev, bgp);
1030 ti_bandgap_power(bgp, true);
1032 /* Set default counter to 1 for now */
1033 if (TI_BANDGAP_HAS(bgp, COUNTER))
1034 for (i = 0; i < bgp->conf->sensor_count; i++)
1035 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
1037 /* Set default thresholds for alert and shutdown */
1038 for (i = 0; i < bgp->conf->sensor_count; i++) {
1039 struct temp_sensor_data *ts_data;
1041 ts_data = bgp->conf->sensors[i].ts_data;
1043 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1044 /* Set initial Talert thresholds */
1045 RMW_BITS(bgp, i, bgap_threshold,
1046 threshold_tcold_mask, ts_data->t_cold);
1047 RMW_BITS(bgp, i, bgap_threshold,
1048 threshold_thot_mask, ts_data->t_hot);
1049 /* Enable the alert events */
1050 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
1051 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
1054 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
1055 /* Set initial Tshut thresholds */
1056 RMW_BITS(bgp, i, tshut_threshold,
1057 tshut_hot_mask, ts_data->tshut_hot);
1058 RMW_BITS(bgp, i, tshut_threshold,
1059 tshut_cold_mask, ts_data->tshut_cold);
1063 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1064 ti_bandgap_set_continuous_mode(bgp);
1066 /* Set .250 seconds time as default counter */
1067 if (TI_BANDGAP_HAS(bgp, COUNTER))
1068 for (i = 0; i < bgp->conf->sensor_count; i++)
1069 RMW_BITS(bgp, i, bgap_counter, counter_mask,
1072 /* Every thing is good? Then expose the sensors */
1073 for (i = 0; i < bgp->conf->sensor_count; i++) {
1076 if (bgp->conf->sensors[i].register_cooling)
1077 bgp->conf->sensors[i].register_cooling(bgp, i);
1079 domain = bgp->conf->sensors[i].domain;
1080 if (bgp->conf->expose_sensor)
1081 bgp->conf->expose_sensor(bgp, i, domain);
1085 * Enable the Interrupts once everything is set. Otherwise irq handler
1086 * might be called as soon as it is enabled where as rest of framework
1087 * is still getting initialised.
1089 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1090 ret = ti_bandgap_talert_init(bgp, pdev);
1092 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
1093 i = bgp->conf->sensor_count;
1101 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1102 clk_disable_unprepare(bgp->fclock);
1104 clk_put(bgp->fclock);
1105 clk_put(bgp->div_clk);
1107 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1108 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1109 gpio_free(bgp->tshut_gpio);
1116 int ti_bandgap_remove(struct platform_device *pdev)
1118 struct ti_bandgap *bgp = platform_get_drvdata(pdev);
1121 /* First thing is to remove sensor interfaces */
1122 for (i = 0; i < bgp->conf->sensor_count; i++) {
1123 if (bgp->conf->sensors[i].register_cooling)
1124 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1126 if (bgp->conf->remove_sensor)
1127 bgp->conf->remove_sensor(bgp, i);
1130 ti_bandgap_power(bgp, false);
1132 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1133 clk_disable_unprepare(bgp->fclock);
1134 clk_put(bgp->fclock);
1135 clk_put(bgp->div_clk);
1137 if (TI_BANDGAP_HAS(bgp, TALERT))
1138 free_irq(bgp->irq, bgp);
1140 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1141 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1142 gpio_free(bgp->tshut_gpio);
1149 static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
1153 for (i = 0; i < bgp->conf->sensor_count; i++) {
1154 struct temp_sensor_registers *tsr;
1155 struct temp_sensor_regval *rval;
1157 rval = &bgp->regval[i];
1158 tsr = bgp->conf->sensors[i].registers;
1160 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1161 rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
1162 tsr->bgap_mode_ctrl);
1163 if (TI_BANDGAP_HAS(bgp, COUNTER))
1164 rval->bg_counter = ti_bandgap_readl(bgp,
1166 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1167 rval->bg_threshold = ti_bandgap_readl(bgp,
1168 tsr->bgap_threshold);
1169 rval->bg_ctrl = ti_bandgap_readl(bgp,
1170 tsr->bgap_mask_ctrl);
1173 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1174 rval->tshut_threshold = ti_bandgap_readl(bgp,
1175 tsr->tshut_threshold);
1181 static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
1185 for (i = 0; i < bgp->conf->sensor_count; i++) {
1186 struct temp_sensor_registers *tsr;
1187 struct temp_sensor_regval *rval;
1190 rval = &bgp->regval[i];
1191 tsr = bgp->conf->sensors[i].registers;
1193 if (TI_BANDGAP_HAS(bgp, COUNTER))
1194 val = ti_bandgap_readl(bgp, tsr->bgap_counter);
1196 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1197 ti_bandgap_writel(bgp, rval->tshut_threshold,
1198 tsr->tshut_threshold);
1199 /* Force immediate temperature measurement and update
1200 * of the DTEMP field
1202 ti_bandgap_force_single_read(bgp, i);
1204 if (TI_BANDGAP_HAS(bgp, COUNTER))
1205 ti_bandgap_writel(bgp, rval->bg_counter,
1207 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1208 ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1209 tsr->bgap_mode_ctrl);
1210 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1211 ti_bandgap_writel(bgp, rval->bg_threshold,
1212 tsr->bgap_threshold);
1213 ti_bandgap_writel(bgp, rval->bg_ctrl,
1214 tsr->bgap_mask_ctrl);
1221 static int ti_bandgap_suspend(struct device *dev)
1223 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1226 err = ti_bandgap_save_ctxt(bgp);
1227 ti_bandgap_power(bgp, false);
1229 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1230 clk_disable_unprepare(bgp->fclock);
1235 static int ti_bandgap_resume(struct device *dev)
1237 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1239 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1240 clk_prepare_enable(bgp->fclock);
1242 ti_bandgap_power(bgp, true);
1244 return ti_bandgap_restore_ctxt(bgp);
1246 static const struct dev_pm_ops ti_bandgap_dev_pm_ops = {
1247 SET_SYSTEM_SLEEP_PM_OPS(ti_bandgap_suspend,
1251 #define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
1253 #define DEV_PM_OPS NULL
1256 static const struct of_device_id of_ti_bandgap_match[] = {
1257 #ifdef CONFIG_OMAP4_THERMAL
1259 .compatible = "ti,omap4430-bandgap",
1260 .data = (void *)&omap4430_data,
1263 .compatible = "ti,omap4460-bandgap",
1264 .data = (void *)&omap4460_data,
1267 .compatible = "ti,omap4470-bandgap",
1268 .data = (void *)&omap4470_data,
1271 #ifdef CONFIG_OMAP5_THERMAL
1273 .compatible = "ti,omap5430-bandgap",
1274 .data = (void *)&omap5430_data,
1280 MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
1282 static struct platform_driver ti_bandgap_sensor_driver = {
1283 .probe = ti_bandgap_probe,
1284 .remove = ti_bandgap_remove,
1286 .name = "ti-soc-thermal",
1288 .of_match_table = of_ti_bandgap_match,
1292 module_platform_driver(ti_bandgap_sensor_driver);
1294 MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1295 MODULE_LICENSE("GPL v2");
1296 MODULE_ALIAS("platform:ti-soc-thermal");
1297 MODULE_AUTHOR("Texas Instrument Inc.");