1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI Bandgap temperature sensor driver
5 * Copyright (C) 2011-2012 Texas Instruments Incorporated - http://www.ti.com/
6 * Author: J Keerthy <j-keerthy@ti.com>
7 * Author: Moiz Sonasath <m-sonasath@ti.com>
8 * Couple of fixes, DT and MFD adaptation:
9 * Eduardo Valentin <eduardo.valentin@ti.com>
12 #include <linux/module.h>
13 #include <linux/export.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/interrupt.h>
17 #include <linux/clk.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/platform_device.h>
20 #include <linux/err.h>
21 #include <linux/types.h>
22 #include <linux/spinlock.h>
23 #include <linux/sys_soc.h>
24 #include <linux/reboot.h>
25 #include <linux/of_device.h>
26 #include <linux/of_platform.h>
27 #include <linux/of_irq.h>
29 #include <linux/iopoll.h>
30 #include <linux/cpu_pm.h>
31 #include <linux/device.h>
32 #include <linux/pm_runtime.h>
35 #include <linux/of_device.h>
37 #include "ti-bandgap.h"
39 static int ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id);
40 #ifdef CONFIG_PM_SLEEP
41 static int bandgap_omap_cpu_notifier(struct notifier_block *nb,
42 unsigned long cmd, void *v);
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))
110 for (i = 0; i < bgp->conf->sensor_count; i++)
112 RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
117 * ti_errata814_bandgap_read_temp() - helper function to read dra7 sensor temperature
118 * @bgp: pointer to ti_bandgap structure
119 * @reg: desired register (offset) to be read
121 * Function to read dra7 bandgap sensor temperature. This is done separately
122 * so as to workaround the errata "Bandgap Temperature read Dtemp can be
123 * corrupted" - Errata ID: i814".
124 * Read accesses to registers listed below can be corrupted due to incorrect
125 * resynchronization between clock domains.
126 * Read access to registers below can be corrupted :
127 * CTRL_CORE_DTEMP_MPU/GPU/CORE/DSPEVE/IVA_n (n = 0 to 4)
128 * CTRL_CORE_TEMP_SENSOR_MPU/GPU/CORE/DSPEVE/IVA_n
130 * Return: the register value.
132 static u32 ti_errata814_bandgap_read_temp(struct ti_bandgap *bgp, u32 reg)
136 val1 = ti_bandgap_readl(bgp, reg);
137 val2 = ti_bandgap_readl(bgp, reg);
139 /* If both times we read the same value then that is right */
143 /* if val1 and val2 are different read it third time */
144 return ti_bandgap_readl(bgp, reg);
148 * ti_bandgap_read_temp() - helper function to read sensor temperature
149 * @bgp: pointer to ti_bandgap structure
150 * @id: bandgap sensor id
152 * Function to concentrate the steps to read sensor temperature register.
153 * This function is desired because, depending on bandgap device version,
154 * it might be needed to freeze the bandgap state machine, before fetching
155 * the register value.
157 * Return: temperature in ADC values.
159 static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
161 struct temp_sensor_registers *tsr;
164 tsr = bgp->conf->sensors[id].registers;
165 reg = tsr->temp_sensor_ctrl;
167 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
168 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
170 * In case we cannot read from cur_dtemp / dtemp_0,
171 * then we read from the last valid temp read
173 reg = tsr->ctrl_dtemp_1;
176 /* read temperature */
177 if (TI_BANDGAP_HAS(bgp, ERRATA_814))
178 temp = ti_errata814_bandgap_read_temp(bgp, reg);
180 temp = ti_bandgap_readl(bgp, reg);
182 temp &= tsr->bgap_dtemp_mask;
184 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
185 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
190 /*** IRQ handlers ***/
193 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
195 * @data: private data (struct ti_bandgap *)
197 * This is the Talert handler. Use it only if bandgap device features
198 * HAS(TALERT). This handler goes over all sensors and checks their
199 * conditions and acts accordingly. In case there are events pending,
200 * it will reset the event mask to wait for the opposite event (next event).
201 * Every time there is a new event, it will be reported to thermal layer.
203 * Return: IRQ_HANDLED
205 static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
207 struct ti_bandgap *bgp = data;
208 struct temp_sensor_registers *tsr;
209 u32 t_hot = 0, t_cold = 0, ctrl;
212 spin_lock(&bgp->lock);
213 for (i = 0; i < bgp->conf->sensor_count; i++) {
214 tsr = bgp->conf->sensors[i].registers;
215 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
217 /* Read the status of t_hot */
218 t_hot = ctrl & tsr->status_hot_mask;
220 /* Read the status of t_cold */
221 t_cold = ctrl & tsr->status_cold_mask;
223 if (!t_cold && !t_hot)
226 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
228 * One TALERT interrupt: Two sources
229 * If the interrupt is due to t_hot then mask t_hot and
230 * and unmask t_cold else mask t_cold and unmask t_hot
233 ctrl &= ~tsr->mask_hot_mask;
234 ctrl |= tsr->mask_cold_mask;
236 ctrl &= ~tsr->mask_cold_mask;
237 ctrl |= tsr->mask_hot_mask;
240 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
243 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
244 __func__, bgp->conf->sensors[i].domain,
247 /* report temperature to whom may concern */
248 if (bgp->conf->report_temperature)
249 bgp->conf->report_temperature(bgp, i);
251 spin_unlock(&bgp->lock);
257 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
259 * @data: private data (unused)
261 * This is the Tshut handler. Use it only if bandgap device features
262 * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
265 * Return: IRQ_HANDLED
267 static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
269 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
272 orderly_poweroff(true);
277 /*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
280 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
281 * @bgp: struct ti_bandgap pointer
282 * @adc_val: value in ADC representation
283 * @t: address where to write the resulting temperature in mCelsius
285 * Simple conversion from ADC representation to mCelsius. In case the ADC 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 @adc_val
290 * argument is out of the ADC conv table range.
293 int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
295 const struct ti_bandgap_data *conf = bgp->conf;
297 /* look up for temperature in the table and return the temperature */
298 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val)
301 *t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
306 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
307 * @bgp: struct ti_bandgap pointer
308 * @id: bandgap sensor id
310 * Checks if the bandgap pointer is valid and if the sensor id is also
313 * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
314 * @id cannot index @bgp sensors.
316 static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
318 if (!bgp || IS_ERR(bgp)) {
319 pr_err("%s: invalid bandgap pointer\n", __func__);
323 if ((id < 0) || (id >= bgp->conf->sensor_count)) {
324 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
333 * ti_bandgap_read_counter() - read the sensor counter
334 * @bgp: pointer to bandgap instance
336 * @interval: resulting update interval in miliseconds
338 static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id,
341 struct temp_sensor_registers *tsr;
344 tsr = bgp->conf->sensors[id].registers;
345 time = ti_bandgap_readl(bgp, tsr->bgap_counter);
346 time = (time & tsr->counter_mask) >>
347 __ffs(tsr->counter_mask);
348 time = time * 1000 / bgp->clk_rate;
353 * ti_bandgap_read_counter_delay() - read the sensor counter delay
354 * @bgp: pointer to bandgap instance
356 * @interval: resulting update interval in miliseconds
358 static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id,
361 struct temp_sensor_registers *tsr;
364 tsr = bgp->conf->sensors[id].registers;
366 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
367 reg_val = (reg_val & tsr->mask_counter_delay_mask) >>
368 __ffs(tsr->mask_counter_delay_mask);
389 dev_warn(bgp->dev, "Wrong counter delay value read from register %X",
395 * ti_bandgap_read_update_interval() - read the sensor update interval
396 * @bgp: pointer to bandgap instance
398 * @interval: resulting update interval in miliseconds
400 * Return: 0 on success or the proper error code
402 int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
407 ret = ti_bandgap_validate(bgp, id);
411 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
412 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
417 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
418 ti_bandgap_read_counter(bgp, id, interval);
422 ti_bandgap_read_counter_delay(bgp, id, interval);
428 * ti_bandgap_write_counter_delay() - set the counter_delay
429 * @bgp: pointer to bandgap instance
431 * @interval: desired update interval in miliseconds
433 * Return: 0 on success or the proper error code
435 static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id,
441 case 0: /* Immediate conversion */
444 case 1: /* Conversion after ever 1ms */
447 case 10: /* Conversion after ever 10ms */
450 case 100: /* Conversion after ever 100ms */
453 case 250: /* Conversion after ever 250ms */
456 case 500: /* Conversion after ever 500ms */
460 dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval);
464 spin_lock(&bgp->lock);
465 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval);
466 spin_unlock(&bgp->lock);
472 * ti_bandgap_write_counter() - set the bandgap sensor counter
473 * @bgp: pointer to bandgap instance
475 * @interval: desired update interval in miliseconds
477 static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id,
480 interval = interval * bgp->clk_rate / 1000;
481 spin_lock(&bgp->lock);
482 RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
483 spin_unlock(&bgp->lock);
487 * ti_bandgap_write_update_interval() - set the update interval
488 * @bgp: pointer to bandgap instance
490 * @interval: desired update interval in miliseconds
492 * Return: 0 on success or the proper error code
494 int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
495 int id, u32 interval)
497 int ret = ti_bandgap_validate(bgp, id);
501 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
502 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
507 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
508 ti_bandgap_write_counter(bgp, id, interval);
512 ret = ti_bandgap_write_counter_delay(bgp, id, interval);
518 * ti_bandgap_read_temperature() - report current temperature
519 * @bgp: pointer to bandgap instance
521 * @temperature: resulting temperature
523 * Return: 0 on success or the proper error code
525 int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
531 ret = ti_bandgap_validate(bgp, id);
535 if (!TI_BANDGAP_HAS(bgp, MODE_CONFIG)) {
536 ret = ti_bandgap_force_single_read(bgp, id);
541 spin_lock(&bgp->lock);
542 temp = ti_bandgap_read_temp(bgp, id);
543 spin_unlock(&bgp->lock);
545 ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
555 * ti_bandgap_set_sensor_data() - helper function to store thermal
556 * framework related data.
557 * @bgp: pointer to bandgap instance
559 * @data: thermal framework related data to be stored
561 * Return: 0 on success or the proper error code
563 int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
565 int ret = ti_bandgap_validate(bgp, id);
569 bgp->regval[id].data = data;
575 * ti_bandgap_get_sensor_data() - helper function to get thermal
576 * framework related data.
577 * @bgp: pointer to bandgap instance
580 * Return: data stored by set function with sensor id on success or NULL
582 void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
584 int ret = ti_bandgap_validate(bgp, id);
588 return bgp->regval[id].data;
591 /*** Helper functions used during device initialization ***/
594 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
595 * @bgp: pointer to struct ti_bandgap
596 * @id: sensor id which it is desired to read 1 temperature
598 * Used to initialize the conversion state machine and set it to a valid
599 * state. Called during device initialization and context restore events.
604 ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
606 struct temp_sensor_registers *tsr = bgp->conf->sensors[id].registers;
607 void __iomem *temp_sensor_ctrl = bgp->base + tsr->temp_sensor_ctrl;
611 /* Select continuous or single conversion mode */
612 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG)) {
613 if (TI_BANDGAP_HAS(bgp, CONT_MODE_ONLY))
614 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 1);
616 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
619 /* Set Start of Conversion if available */
620 if (tsr->bgap_soc_mask) {
621 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
623 /* Wait for EOCZ going up */
624 error = readl_poll_timeout_atomic(temp_sensor_ctrl, val,
625 val & tsr->bgap_eocz_mask,
628 dev_warn(bgp->dev, "eocz timed out waiting high\n");
630 /* Clear Start of Conversion if available */
631 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
634 /* Wait for EOCZ going down, always needed even if no bgap_soc_mask */
635 error = readl_poll_timeout_atomic(temp_sensor_ctrl, val,
636 !(val & tsr->bgap_eocz_mask),
639 dev_warn(bgp->dev, "eocz timed out waiting low\n");
645 * ti_bandgap_set_continuous_mode() - One time enabling of continuous mode
646 * @bgp: pointer to struct ti_bandgap
648 * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
649 * be used for junction temperature monitoring, it is desirable that the
650 * sensors are operational all the time, so that alerts are generated
655 static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
659 for (i = 0; i < bgp->conf->sensor_count; i++) {
660 /* Perform a single read just before enabling continuous */
661 ti_bandgap_force_single_read(bgp, i);
662 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
669 * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
670 * @bgp: pointer to struct ti_bandgap
671 * @id: id of the individual sensor
672 * @trend: Pointer to trend.
674 * This function needs to be called to fetch the temperature trend of a
675 * Particular sensor. The function computes the difference in temperature
676 * w.r.t time. For the bandgaps with built in history buffer the temperatures
677 * are read from the buffer and for those without the Buffer -ENOTSUPP is
680 * Return: 0 if no error, else return corresponding error. If no
681 * error then the trend value is passed on to trend parameter
683 int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend)
685 struct temp_sensor_registers *tsr;
686 u32 temp1, temp2, reg1, reg2;
687 int t1, t2, interval, ret = 0;
689 ret = ti_bandgap_validate(bgp, id);
693 if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) ||
694 !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
699 spin_lock(&bgp->lock);
701 tsr = bgp->conf->sensors[id].registers;
703 /* Freeze and read the last 2 valid readings */
704 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
705 reg1 = tsr->ctrl_dtemp_1;
706 reg2 = tsr->ctrl_dtemp_2;
708 /* read temperature from history buffer */
709 temp1 = ti_bandgap_readl(bgp, reg1);
710 temp1 &= tsr->bgap_dtemp_mask;
712 temp2 = ti_bandgap_readl(bgp, reg2);
713 temp2 &= tsr->bgap_dtemp_mask;
715 /* Convert from adc values to mCelsius temperature */
716 ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1);
720 ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2);
724 /* Fetch the update interval */
725 ret = ti_bandgap_read_update_interval(bgp, id, &interval);
729 /* Set the interval to 1 ms if bandgap counter delay is not set */
733 *trend = (t1 - t2) / interval;
735 dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
739 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
740 spin_unlock(&bgp->lock);
746 * ti_bandgap_tshut_init() - setup and initialize tshut handling
747 * @bgp: pointer to struct ti_bandgap
748 * @pdev: pointer to device struct platform_device
750 * Call this function only in case the bandgap features HAS(TSHUT).
751 * In this case, the driver needs to handle the TSHUT signal as an IRQ.
752 * The IRQ is wired as a GPIO, and for this purpose, it is required
753 * to specify which GPIO line is used. TSHUT IRQ is fired anytime
754 * one of the bandgap sensors violates the TSHUT high/hot threshold.
755 * And in that case, the system must go off.
757 * Return: 0 if no error, else error status
759 static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
760 struct platform_device *pdev)
764 status = request_irq(gpiod_to_irq(bgp->tshut_gpiod),
765 ti_bandgap_tshut_irq_handler,
766 IRQF_TRIGGER_RISING, "tshut", NULL);
768 dev_err(bgp->dev, "request irq failed for TSHUT");
774 * ti_bandgap_alert_init() - setup and initialize talert handling
775 * @bgp: pointer to struct ti_bandgap
776 * @pdev: pointer to device struct platform_device
778 * Call this function only in case the bandgap features HAS(TALERT).
779 * In this case, the driver needs to handle the TALERT signals as an IRQs.
780 * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
781 * are violated. In these situation, the driver must reprogram the thresholds,
782 * accordingly to specified policy.
784 * Return: 0 if no error, else return corresponding error.
786 static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
787 struct platform_device *pdev)
791 bgp->irq = platform_get_irq(pdev, 0);
795 ret = request_threaded_irq(bgp->irq, NULL,
796 ti_bandgap_talert_irq_handler,
797 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
800 dev_err(&pdev->dev, "Request threaded irq failed.\n");
807 static const struct of_device_id of_ti_bandgap_match[];
809 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
810 * @pdev: pointer to device struct platform_device
812 * Used to read the device tree properties accordingly to the bandgap
813 * matching version. Based on bandgap version and its capabilities it
814 * will build a struct ti_bandgap out of the required DT entries.
816 * Return: valid bandgap structure if successful, else returns ERR_PTR
817 * return value must be verified with IS_ERR.
819 static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
821 struct device_node *node = pdev->dev.of_node;
822 const struct of_device_id *of_id;
823 struct ti_bandgap *bgp;
824 struct resource *res;
827 /* just for the sake */
829 dev_err(&pdev->dev, "no platform information available\n");
830 return ERR_PTR(-EINVAL);
833 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
835 return ERR_PTR(-ENOMEM);
837 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
839 bgp->conf = of_id->data;
841 /* register shadow for context save and restore */
842 bgp->regval = devm_kcalloc(&pdev->dev, bgp->conf->sensor_count,
843 sizeof(*bgp->regval), GFP_KERNEL);
845 return ERR_PTR(-ENOMEM);
851 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
854 chunk = devm_ioremap_resource(&pdev->dev, res);
858 return ERR_CAST(chunk);
863 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
864 bgp->tshut_gpiod = devm_gpiod_get(&pdev->dev, NULL, GPIOD_IN);
865 if (IS_ERR(bgp->tshut_gpiod)) {
866 dev_err(&pdev->dev, "invalid gpio for tshut\n");
867 return ERR_CAST(bgp->tshut_gpiod);
875 * List of SoCs on which the CPU PM notifier can cause erros on the DTEMP
877 * Enabled notifier on these machines results in erroneous, random values which
878 * could trigger unexpected thermal shutdown.
880 static const struct soc_device_attribute soc_no_cpu_notifier[] = {
881 { .machine = "OMAP4430" },
885 /*** Device driver call backs ***/
888 int ti_bandgap_probe(struct platform_device *pdev)
890 struct ti_bandgap *bgp;
891 int clk_rate, ret, i;
893 bgp = ti_bandgap_build(pdev);
895 dev_err(&pdev->dev, "failed to fetch platform data\n");
898 bgp->dev = &pdev->dev;
900 if (TI_BANDGAP_HAS(bgp, UNRELIABLE))
902 "This OMAP thermal sensor is unreliable. You've been warned\n");
904 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
905 ret = ti_bandgap_tshut_init(bgp, pdev);
908 "failed to initialize system tshut IRQ\n");
913 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
914 if (IS_ERR(bgp->fclock)) {
915 dev_err(&pdev->dev, "failed to request fclock reference\n");
916 ret = PTR_ERR(bgp->fclock);
920 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
921 if (IS_ERR(bgp->div_clk)) {
922 dev_err(&pdev->dev, "failed to request div_ts_ck clock ref\n");
923 ret = PTR_ERR(bgp->div_clk);
927 for (i = 0; i < bgp->conf->sensor_count; i++) {
928 struct temp_sensor_registers *tsr;
931 tsr = bgp->conf->sensors[i].registers;
933 * check if the efuse has a non-zero value if not
934 * it is an untrimmed sample and the temperatures
935 * may not be accurate
937 val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
940 "Non-trimmed BGAP, Temp not accurate\n");
943 clk_rate = clk_round_rate(bgp->div_clk,
944 bgp->conf->sensors[0].ts_data->max_freq);
945 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
948 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
952 ret = clk_set_rate(bgp->div_clk, clk_rate);
954 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
956 bgp->clk_rate = clk_rate;
957 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
958 clk_prepare_enable(bgp->fclock);
961 spin_lock_init(&bgp->lock);
962 bgp->dev = &pdev->dev;
963 platform_set_drvdata(pdev, bgp);
965 ti_bandgap_power(bgp, true);
967 /* Set default counter to 1 for now */
968 if (TI_BANDGAP_HAS(bgp, COUNTER))
969 for (i = 0; i < bgp->conf->sensor_count; i++)
970 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
972 /* Set default thresholds for alert and shutdown */
973 for (i = 0; i < bgp->conf->sensor_count; i++) {
974 struct temp_sensor_data *ts_data;
976 ts_data = bgp->conf->sensors[i].ts_data;
978 if (TI_BANDGAP_HAS(bgp, TALERT)) {
979 /* Set initial Talert thresholds */
980 RMW_BITS(bgp, i, bgap_threshold,
981 threshold_tcold_mask, ts_data->t_cold);
982 RMW_BITS(bgp, i, bgap_threshold,
983 threshold_thot_mask, ts_data->t_hot);
984 /* Enable the alert events */
985 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
986 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
989 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
990 /* Set initial Tshut thresholds */
991 RMW_BITS(bgp, i, tshut_threshold,
992 tshut_hot_mask, ts_data->tshut_hot);
993 RMW_BITS(bgp, i, tshut_threshold,
994 tshut_cold_mask, ts_data->tshut_cold);
998 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
999 ti_bandgap_set_continuous_mode(bgp);
1001 /* Set .250 seconds time as default counter */
1002 if (TI_BANDGAP_HAS(bgp, COUNTER))
1003 for (i = 0; i < bgp->conf->sensor_count; i++)
1004 RMW_BITS(bgp, i, bgap_counter, counter_mask,
1007 /* Every thing is good? Then expose the sensors */
1008 for (i = 0; i < bgp->conf->sensor_count; i++) {
1011 if (bgp->conf->sensors[i].register_cooling) {
1012 ret = bgp->conf->sensors[i].register_cooling(bgp, i);
1014 goto remove_sensors;
1017 if (bgp->conf->expose_sensor) {
1018 domain = bgp->conf->sensors[i].domain;
1019 ret = bgp->conf->expose_sensor(bgp, i, domain);
1021 goto remove_last_cooling;
1026 * Enable the Interrupts once everything is set. Otherwise irq handler
1027 * might be called as soon as it is enabled where as rest of framework
1028 * is still getting initialised.
1030 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1031 ret = ti_bandgap_talert_init(bgp, pdev);
1033 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
1034 i = bgp->conf->sensor_count;
1039 #ifdef CONFIG_PM_SLEEP
1040 bgp->nb.notifier_call = bandgap_omap_cpu_notifier;
1041 if (!soc_device_match(soc_no_cpu_notifier))
1042 cpu_pm_register_notifier(&bgp->nb);
1047 remove_last_cooling:
1048 if (bgp->conf->sensors[i].unregister_cooling)
1049 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1051 for (i--; i >= 0; i--) {
1052 if (bgp->conf->sensors[i].unregister_cooling)
1053 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1054 if (bgp->conf->remove_sensor)
1055 bgp->conf->remove_sensor(bgp, i);
1057 ti_bandgap_power(bgp, false);
1059 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1060 clk_disable_unprepare(bgp->fclock);
1062 clk_put(bgp->div_clk);
1064 clk_put(bgp->fclock);
1066 if (TI_BANDGAP_HAS(bgp, TSHUT))
1067 free_irq(gpiod_to_irq(bgp->tshut_gpiod), NULL);
1073 int ti_bandgap_remove(struct platform_device *pdev)
1075 struct ti_bandgap *bgp = platform_get_drvdata(pdev);
1078 if (!soc_device_match(soc_no_cpu_notifier))
1079 cpu_pm_unregister_notifier(&bgp->nb);
1081 /* Remove sensor interfaces */
1082 for (i = 0; i < bgp->conf->sensor_count; i++) {
1083 if (bgp->conf->sensors[i].unregister_cooling)
1084 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1086 if (bgp->conf->remove_sensor)
1087 bgp->conf->remove_sensor(bgp, i);
1090 ti_bandgap_power(bgp, false);
1092 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1093 clk_disable_unprepare(bgp->fclock);
1094 clk_put(bgp->fclock);
1095 clk_put(bgp->div_clk);
1097 if (TI_BANDGAP_HAS(bgp, TALERT))
1098 free_irq(bgp->irq, bgp);
1100 if (TI_BANDGAP_HAS(bgp, TSHUT))
1101 free_irq(gpiod_to_irq(bgp->tshut_gpiod), NULL);
1106 #ifdef CONFIG_PM_SLEEP
1107 static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
1111 for (i = 0; i < bgp->conf->sensor_count; i++) {
1112 struct temp_sensor_registers *tsr;
1113 struct temp_sensor_regval *rval;
1115 rval = &bgp->regval[i];
1116 tsr = bgp->conf->sensors[i].registers;
1118 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1119 rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
1120 tsr->bgap_mode_ctrl);
1121 if (TI_BANDGAP_HAS(bgp, COUNTER))
1122 rval->bg_counter = ti_bandgap_readl(bgp,
1124 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1125 rval->bg_threshold = ti_bandgap_readl(bgp,
1126 tsr->bgap_threshold);
1127 rval->bg_ctrl = ti_bandgap_readl(bgp,
1128 tsr->bgap_mask_ctrl);
1131 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1132 rval->tshut_threshold = ti_bandgap_readl(bgp,
1133 tsr->tshut_threshold);
1139 static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
1143 for (i = 0; i < bgp->conf->sensor_count; i++) {
1144 struct temp_sensor_registers *tsr;
1145 struct temp_sensor_regval *rval;
1148 rval = &bgp->regval[i];
1149 tsr = bgp->conf->sensors[i].registers;
1151 if (TI_BANDGAP_HAS(bgp, COUNTER))
1152 val = ti_bandgap_readl(bgp, tsr->bgap_counter);
1154 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1155 ti_bandgap_writel(bgp, rval->tshut_threshold,
1156 tsr->tshut_threshold);
1157 /* Force immediate temperature measurement and update
1158 * of the DTEMP field
1160 ti_bandgap_force_single_read(bgp, i);
1162 if (TI_BANDGAP_HAS(bgp, COUNTER))
1163 ti_bandgap_writel(bgp, rval->bg_counter,
1165 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1166 ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1167 tsr->bgap_mode_ctrl);
1168 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1169 ti_bandgap_writel(bgp, rval->bg_threshold,
1170 tsr->bgap_threshold);
1171 ti_bandgap_writel(bgp, rval->bg_ctrl,
1172 tsr->bgap_mask_ctrl);
1179 static int ti_bandgap_suspend(struct device *dev)
1181 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1184 err = ti_bandgap_save_ctxt(bgp);
1185 ti_bandgap_power(bgp, false);
1187 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1188 clk_disable_unprepare(bgp->fclock);
1190 bgp->is_suspended = true;
1195 static int bandgap_omap_cpu_notifier(struct notifier_block *nb,
1196 unsigned long cmd, void *v)
1198 struct ti_bandgap *bgp;
1200 bgp = container_of(nb, struct ti_bandgap, nb);
1202 spin_lock(&bgp->lock);
1204 case CPU_CLUSTER_PM_ENTER:
1205 if (bgp->is_suspended)
1207 ti_bandgap_save_ctxt(bgp);
1208 ti_bandgap_power(bgp, false);
1209 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1210 clk_disable(bgp->fclock);
1212 case CPU_CLUSTER_PM_ENTER_FAILED:
1213 case CPU_CLUSTER_PM_EXIT:
1214 if (bgp->is_suspended)
1216 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1217 clk_enable(bgp->fclock);
1218 ti_bandgap_power(bgp, true);
1219 ti_bandgap_restore_ctxt(bgp);
1222 spin_unlock(&bgp->lock);
1227 static int ti_bandgap_resume(struct device *dev)
1229 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1231 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1232 clk_prepare_enable(bgp->fclock);
1234 ti_bandgap_power(bgp, true);
1235 bgp->is_suspended = false;
1237 return ti_bandgap_restore_ctxt(bgp);
1239 static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend,
1242 #define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
1244 #define DEV_PM_OPS NULL
1247 static const struct of_device_id of_ti_bandgap_match[] = {
1248 #ifdef CONFIG_OMAP3_THERMAL
1250 .compatible = "ti,omap34xx-bandgap",
1251 .data = (void *)&omap34xx_data,
1254 .compatible = "ti,omap36xx-bandgap",
1255 .data = (void *)&omap36xx_data,
1258 #ifdef CONFIG_OMAP4_THERMAL
1260 .compatible = "ti,omap4430-bandgap",
1261 .data = (void *)&omap4430_data,
1264 .compatible = "ti,omap4460-bandgap",
1265 .data = (void *)&omap4460_data,
1268 .compatible = "ti,omap4470-bandgap",
1269 .data = (void *)&omap4470_data,
1272 #ifdef CONFIG_OMAP5_THERMAL
1274 .compatible = "ti,omap5430-bandgap",
1275 .data = (void *)&omap5430_data,
1278 #ifdef CONFIG_DRA752_THERMAL
1280 .compatible = "ti,dra752-bandgap",
1281 .data = (void *)&dra752_data,
1287 MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
1289 static struct platform_driver ti_bandgap_sensor_driver = {
1290 .probe = ti_bandgap_probe,
1291 .remove = ti_bandgap_remove,
1293 .name = "ti-soc-thermal",
1295 .of_match_table = of_ti_bandgap_match,
1299 module_platform_driver(ti_bandgap_sensor_driver);
1301 MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1302 MODULE_LICENSE("GPL v2");
1303 MODULE_ALIAS("platform:ti-soc-thermal");
1304 MODULE_AUTHOR("Texas Instrument Inc.");