2 * OMAP4 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/mutex.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 "omap-bandgap.h"
45 /*** Helper functions to access registers and their bitfields ***/
48 * omap_bandgap_readl() - simple read helper function
49 * @bg_ptr: pointer to omap_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 omap_bandgap_readl(struct omap_bandgap *bg_ptr, u32 reg)
57 return readl(bg_ptr->base + reg);
61 * omap_bandgap_writel() - simple write helper function
62 * @bg_ptr: pointer to omap_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 omap_bandgap_writel(struct omap_bandgap *bg_ptr, u32 val, u32 reg)
70 writel(val, bg_ptr->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(bg_ptr, id, reg, mask, val) \
81 struct temp_sensor_registers *t; \
84 t = bg_ptr->conf->sensors[(id)].registers; \
85 r = omap_bandgap_readl(bg_ptr, t->reg); \
87 r |= (val) << __ffs(t->mask); \
88 omap_bandgap_writel(bg_ptr, r, t->reg); \
91 /*** Basic helper functions ***/
94 * omap_bandgap_power() - controls the power state of a bandgap device
95 * @bg_ptr: pointer to omap_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 omap_bandgap_power(struct omap_bandgap *bg_ptr, bool on)
105 if (!OMAP_BANDGAP_HAS(bg_ptr, POWER_SWITCH))
108 for (i = 0; i < bg_ptr->conf->sensor_count; i++)
110 RMW_BITS(bg_ptr, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
117 * omap_bandgap_read_temp() - helper function to read sensor temperature
118 * @bg_ptr: pointer to omap_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 omap_bandgap_read_temp(struct omap_bandgap *bg_ptr, int id)
128 struct temp_sensor_registers *tsr;
131 tsr = bg_ptr->conf->sensors[id].registers;
132 reg = tsr->temp_sensor_ctrl;
134 if (OMAP_BANDGAP_HAS(bg_ptr, FREEZE_BIT)) {
135 RMW_BITS(bg_ptr, 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 = omap_bandgap_readl(bg_ptr, reg);
145 temp &= tsr->bgap_dtemp_mask;
147 if (OMAP_BANDGAP_HAS(bg_ptr, FREEZE_BIT))
148 RMW_BITS(bg_ptr, id, bgap_mask_ctrl, mask_freeze_mask, 0);
153 /*** IRQ handlers ***/
156 * omap_bandgap_talert_irq_handler() - handles Temperature alert IRQs
158 * @data: private data (struct omap_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 omap_bandgap_talert_irq_handler(int irq, void *data)
168 struct omap_bandgap *bg_ptr = data;
169 struct temp_sensor_registers *tsr;
170 u32 t_hot = 0, t_cold = 0, ctrl;
173 for (i = 0; i < bg_ptr->conf->sensor_count; i++) {
174 tsr = bg_ptr->conf->sensors[i].registers;
175 ctrl = omap_bandgap_readl(bg_ptr, tsr->bgap_status);
177 /* Read the status of t_hot */
178 t_hot = ctrl & tsr->status_hot_mask;
180 /* Read the status of t_cold */
181 t_cold = ctrl & tsr->status_cold_mask;
183 if (!t_cold && !t_hot)
186 ctrl = omap_bandgap_readl(bg_ptr, tsr->bgap_mask_ctrl);
188 * One TALERT interrupt: Two sources
189 * If the interrupt is due to t_hot then mask t_hot and
190 * and unmask t_cold else mask t_cold and unmask t_hot
193 ctrl &= ~tsr->mask_hot_mask;
194 ctrl |= tsr->mask_cold_mask;
196 ctrl &= ~tsr->mask_cold_mask;
197 ctrl |= tsr->mask_hot_mask;
200 omap_bandgap_writel(bg_ptr, ctrl, tsr->bgap_mask_ctrl);
203 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
204 __func__, bg_ptr->conf->sensors[i].domain,
207 /* report temperature to whom may concern */
208 if (bg_ptr->conf->report_temperature)
209 bg_ptr->conf->report_temperature(bg_ptr, i);
216 * omap_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
218 * @data: private data (unused)
220 * This is the Tshut handler. Use it only if bandgap device features
221 * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
224 static irqreturn_t omap_bandgap_tshut_irq_handler(int irq, void *data)
226 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
229 orderly_poweroff(true);
234 /*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
237 * omap_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
238 * @bg_ptr: struct omap_bandgap pointer
239 * @adc_val: value in ADC representation
240 * @t: address where to write the resulting temperature in mCelsius
242 * Simple conversion from ADC representation to mCelsius. In case the ADC value
243 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
244 * The conversion table is indexed by the ADC values.
247 int omap_bandgap_adc_to_mcelsius(struct omap_bandgap *bg_ptr,
250 struct omap_bandgap_data *conf = bg_ptr->conf;
253 /* look up for temperature in the table and return the temperature */
254 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val) {
259 *t = bg_ptr->conf->conv_table[adc_val - conf->adc_start_val];
266 * omap_bandgap_mcelsius_to_adc() - converts a mCelsius value to ADC scale
267 * @bg_ptr: struct omap_bandgap pointer
268 * @temp: value in mCelsius
269 * @adc: address where to write the resulting temperature in ADC representation
271 * Simple conversion from mCelsius to ADC values. In case the temp value
272 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
273 * The conversion table is indexed by the ADC values.
276 int omap_bandgap_mcelsius_to_adc(struct omap_bandgap *bg_ptr, long temp,
279 struct omap_bandgap_data *conf = bg_ptr->conf;
280 const int *conv_table = bg_ptr->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 * omap_bandgap_add_hyst() - add hysteresis (in mCelsius) to an ADC value
308 * @bg_ptr: struct omap_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 omap_bandgap_add_hyst(struct omap_bandgap *bg_ptr, int adc_val,
318 int hyst_val, u32 *sum)
323 * Need to add in the mcelsius domain, so we have a temperature
324 * the conv_table range
326 ret = omap_bandgap_adc_to_mcelsius(bg_ptr, adc_val, &temp);
332 ret = omap_bandgap_mcelsius_to_adc(bg_ptr, temp, sum);
338 /*** Helper functions handling device Alert/Shutdown signals ***/
340 /* Talert masks. Call it only if HAS(TALERT) is set */
341 static int temp_sensor_unmask_interrupts(struct omap_bandgap *bg_ptr, int id,
342 u32 t_hot, u32 t_cold)
344 struct temp_sensor_registers *tsr;
347 /* Read the current on die temperature */
348 temp = omap_bandgap_read_temp(bg_ptr, id);
350 tsr = bg_ptr->conf->sensors[id].registers;
351 reg_val = omap_bandgap_readl(bg_ptr, tsr->bgap_mask_ctrl);
354 reg_val |= tsr->mask_hot_mask;
356 reg_val &= ~tsr->mask_hot_mask;
359 reg_val |= tsr->mask_cold_mask;
361 reg_val &= ~tsr->mask_cold_mask;
362 omap_bandgap_writel(bg_ptr, reg_val, tsr->bgap_mask_ctrl);
367 /* Talert Thot threshold. Call it only if HAS(TALERT) is set */
369 int temp_sensor_configure_thot(struct omap_bandgap *bg_ptr, int id, int t_hot)
371 struct temp_sensor_data *ts_data = bg_ptr->conf->sensors[id].ts_data;
372 struct temp_sensor_registers *tsr;
373 u32 thresh_val, reg_val;
376 tsr = bg_ptr->conf->sensors[id].registers;
378 /* obtain the T cold value */
379 thresh_val = omap_bandgap_readl(bg_ptr, tsr->bgap_threshold);
380 cold = (thresh_val & tsr->threshold_tcold_mask) >>
381 __ffs(tsr->threshold_tcold_mask);
383 /* change the t_cold to t_hot - 5000 millidegrees */
384 err |= omap_bandgap_add_hyst(bg_ptr, t_hot,
385 -ts_data->hyst_val, &cold);
386 /* write the new t_cold value */
387 reg_val = thresh_val & (~tsr->threshold_tcold_mask);
388 reg_val |= cold << __ffs(tsr->threshold_tcold_mask);
389 omap_bandgap_writel(bg_ptr, reg_val, tsr->bgap_threshold);
390 thresh_val = reg_val;
393 /* write the new t_hot value */
394 reg_val = thresh_val & ~tsr->threshold_thot_mask;
395 reg_val |= (t_hot << __ffs(tsr->threshold_thot_mask));
396 omap_bandgap_writel(bg_ptr, reg_val, tsr->bgap_threshold);
398 dev_err(bg_ptr->dev, "failed to reprogram thot threshold\n");
402 return temp_sensor_unmask_interrupts(bg_ptr, id, t_hot, cold);
405 /* Talert Tcold threshold. Call it only if HAS(TALERT) is set */
407 int temp_sensor_configure_tcold(struct omap_bandgap *bg_ptr, int id,
410 struct temp_sensor_data *ts_data = bg_ptr->conf->sensors[id].ts_data;
411 struct temp_sensor_registers *tsr;
412 u32 thresh_val, reg_val;
415 tsr = bg_ptr->conf->sensors[id].registers;
416 /* obtain the T cold value */
417 thresh_val = omap_bandgap_readl(bg_ptr, tsr->bgap_threshold);
418 hot = (thresh_val & tsr->threshold_thot_mask) >>
419 __ffs(tsr->threshold_thot_mask);
422 /* change the t_hot to t_cold + 5000 millidegrees */
423 err |= omap_bandgap_add_hyst(bg_ptr, t_cold,
424 ts_data->hyst_val, &hot);
425 /* write the new t_hot value */
426 reg_val = thresh_val & (~tsr->threshold_thot_mask);
427 reg_val |= hot << __ffs(tsr->threshold_thot_mask);
428 omap_bandgap_writel(bg_ptr, reg_val, tsr->bgap_threshold);
429 thresh_val = reg_val;
432 /* write the new t_cold value */
433 reg_val = thresh_val & ~tsr->threshold_tcold_mask;
434 reg_val |= (t_cold << __ffs(tsr->threshold_tcold_mask));
435 omap_bandgap_writel(bg_ptr, reg_val, tsr->bgap_threshold);
437 dev_err(bg_ptr->dev, "failed to reprogram tcold threshold\n");
441 return temp_sensor_unmask_interrupts(bg_ptr, id, hot, t_cold);
444 #define bandgap_is_valid(b) \
446 #define bandgap_is_valid_sensor_id(b, i) \
447 ((i) >= 0 && (i) < (b)->conf->sensor_count)
448 static inline int omap_bandgap_validate(struct omap_bandgap *bg_ptr, int id)
450 if (!bandgap_is_valid(bg_ptr)) {
451 pr_err("%s: invalid bandgap pointer\n", __func__);
455 if (!bandgap_is_valid_sensor_id(bg_ptr, id)) {
456 dev_err(bg_ptr->dev, "%s: sensor id out of range (%d)\n",
466 * omap_bandgap_read_thot() - reads sensor current thot
467 * @bg_ptr - pointer to bandgap instance
469 * @thot - resulting current thot value
471 * returns 0 on success or the proper error code
473 int omap_bandgap_read_thot(struct omap_bandgap *bg_ptr, int id,
476 struct temp_sensor_registers *tsr;
480 ret = omap_bandgap_validate(bg_ptr, id);
484 if (!OMAP_BANDGAP_HAS(bg_ptr, TALERT))
487 tsr = bg_ptr->conf->sensors[id].registers;
488 temp = omap_bandgap_readl(bg_ptr, tsr->bgap_threshold);
489 temp = (temp & tsr->threshold_thot_mask) >>
490 __ffs(tsr->threshold_thot_mask);
491 ret |= omap_bandgap_adc_to_mcelsius(bg_ptr, temp, &temp);
493 dev_err(bg_ptr->dev, "failed to read thot\n");
503 * omap_bandgap_write_thot() - sets sensor current thot
504 * @bg_ptr - pointer to bandgap instance
506 * @val - desired thot value
508 * returns 0 on success or the proper error code
510 int omap_bandgap_write_thot(struct omap_bandgap *bg_ptr, int id, int val)
512 struct temp_sensor_data *ts_data;
513 struct temp_sensor_registers *tsr;
517 ret = omap_bandgap_validate(bg_ptr, id);
521 if (!OMAP_BANDGAP_HAS(bg_ptr, TALERT))
524 ts_data = bg_ptr->conf->sensors[id].ts_data;
525 tsr = bg_ptr->conf->sensors[id].registers;
527 if (val < ts_data->min_temp + ts_data->hyst_val)
529 ret = omap_bandgap_mcelsius_to_adc(bg_ptr, val, &t_hot);
533 mutex_lock(&bg_ptr->bg_mutex);
534 temp_sensor_configure_thot(bg_ptr, id, t_hot);
535 mutex_unlock(&bg_ptr->bg_mutex);
541 * omap_bandgap_read_tcold() - reads sensor current tcold
542 * @bg_ptr - pointer to bandgap instance
544 * @tcold - resulting current tcold value
546 * returns 0 on success or the proper error code
548 int omap_bandgap_read_tcold(struct omap_bandgap *bg_ptr, int id,
551 struct temp_sensor_registers *tsr;
555 ret = omap_bandgap_validate(bg_ptr, id);
559 if (!OMAP_BANDGAP_HAS(bg_ptr, TALERT))
562 tsr = bg_ptr->conf->sensors[id].registers;
563 temp = omap_bandgap_readl(bg_ptr, tsr->bgap_threshold);
564 temp = (temp & tsr->threshold_tcold_mask)
565 >> __ffs(tsr->threshold_tcold_mask);
566 ret |= omap_bandgap_adc_to_mcelsius(bg_ptr, temp, &temp);
576 * omap_bandgap_write_tcold() - sets the sensor tcold
577 * @bg_ptr - pointer to bandgap instance
579 * @val - desired tcold value
581 * returns 0 on success or the proper error code
583 int omap_bandgap_write_tcold(struct omap_bandgap *bg_ptr, int id, int val)
585 struct temp_sensor_data *ts_data;
586 struct temp_sensor_registers *tsr;
590 ret = omap_bandgap_validate(bg_ptr, id);
594 if (!OMAP_BANDGAP_HAS(bg_ptr, TALERT))
597 ts_data = bg_ptr->conf->sensors[id].ts_data;
598 tsr = bg_ptr->conf->sensors[id].registers;
599 if (val > ts_data->max_temp + ts_data->hyst_val)
602 ret = omap_bandgap_mcelsius_to_adc(bg_ptr, val, &t_cold);
606 mutex_lock(&bg_ptr->bg_mutex);
607 temp_sensor_configure_tcold(bg_ptr, id, t_cold);
608 mutex_unlock(&bg_ptr->bg_mutex);
614 * omap_bandgap_read_update_interval() - read the sensor update interval
615 * @bg_ptr - pointer to bandgap instance
617 * @interval - resulting update interval in miliseconds
619 * returns 0 on success or the proper error code
621 int omap_bandgap_read_update_interval(struct omap_bandgap *bg_ptr, int id,
624 struct temp_sensor_registers *tsr;
628 ret = omap_bandgap_validate(bg_ptr, id);
632 if (!OMAP_BANDGAP_HAS(bg_ptr, COUNTER))
635 tsr = bg_ptr->conf->sensors[id].registers;
636 time = omap_bandgap_readl(bg_ptr, tsr->bgap_counter);
637 time = (time & tsr->counter_mask) >> __ffs(tsr->counter_mask);
638 time = time * 1000 / bg_ptr->clk_rate;
646 * omap_bandgap_write_update_interval() - set the update interval
647 * @bg_ptr - pointer to bandgap instance
649 * @interval - desired update interval in miliseconds
651 * returns 0 on success or the proper error code
653 int omap_bandgap_write_update_interval(struct omap_bandgap *bg_ptr,
654 int id, u32 interval)
656 int ret = omap_bandgap_validate(bg_ptr, id);
660 if (!OMAP_BANDGAP_HAS(bg_ptr, COUNTER))
663 interval = interval * bg_ptr->clk_rate / 1000;
664 mutex_lock(&bg_ptr->bg_mutex);
665 RMW_BITS(bg_ptr, id, bgap_counter, counter_mask, interval);
666 mutex_unlock(&bg_ptr->bg_mutex);
672 * omap_bandgap_read_temperature() - report current temperature
673 * @bg_ptr - pointer to bandgap instance
675 * @temperature - resulting temperature
677 * returns 0 on success or the proper error code
679 int omap_bandgap_read_temperature(struct omap_bandgap *bg_ptr, int id,
685 ret = omap_bandgap_validate(bg_ptr, id);
689 mutex_lock(&bg_ptr->bg_mutex);
690 temp = omap_bandgap_read_temp(bg_ptr, id);
691 mutex_unlock(&bg_ptr->bg_mutex);
693 ret |= omap_bandgap_adc_to_mcelsius(bg_ptr, temp, &temp);
703 * omap_bandgap_set_sensor_data() - helper function to store thermal
704 * framework related data.
705 * @bg_ptr - pointer to bandgap instance
707 * @data - thermal framework related data to be stored
709 * returns 0 on success or the proper error code
711 int omap_bandgap_set_sensor_data(struct omap_bandgap *bg_ptr, int id,
714 int ret = omap_bandgap_validate(bg_ptr, id);
718 bg_ptr->conf->sensors[id].data = data;
724 * omap_bandgap_get_sensor_data() - helper function to get thermal
725 * framework related data.
726 * @bg_ptr - pointer to bandgap instance
729 * returns data stored by set function with sensor id on success or NULL
731 void *omap_bandgap_get_sensor_data(struct omap_bandgap *bg_ptr, int id)
733 int ret = omap_bandgap_validate(bg_ptr, id);
737 return bg_ptr->conf->sensors[id].data;
741 omap_bandgap_force_single_read(struct omap_bandgap *bg_ptr, int id)
743 u32 temp = 0, counter = 1000;
745 /* Select single conversion mode */
746 if (OMAP_BANDGAP_HAS(bg_ptr, MODE_CONFIG))
747 RMW_BITS(bg_ptr, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
749 /* Start of Conversion = 1 */
750 RMW_BITS(bg_ptr, id, temp_sensor_ctrl, bgap_soc_mask, 1);
751 /* Wait until DTEMP is updated */
752 temp = omap_bandgap_read_temp(bg_ptr, id);
754 while ((temp == 0) && --counter)
755 temp = omap_bandgap_read_temp(bg_ptr, id);
756 /* REVISIT: Check correct condition for end of conversion */
758 /* Start of Conversion = 0 */
759 RMW_BITS(bg_ptr, id, temp_sensor_ctrl, bgap_soc_mask, 0);
765 * enable_continuous_mode() - One time enabling of continuous conversion mode
766 * @bg_ptr - pointer to scm instance
768 * Call this function only if HAS(MODE_CONFIG) is set
770 static int enable_continuous_mode(struct omap_bandgap *bg_ptr)
774 for (i = 0; i < bg_ptr->conf->sensor_count; i++) {
775 /* Perform a single read just before enabling continuous */
776 omap_bandgap_force_single_read(bg_ptr, i);
777 RMW_BITS(bg_ptr, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
783 static int omap_bandgap_tshut_init(struct omap_bandgap *bg_ptr,
784 struct platform_device *pdev)
786 int gpio_nr = bg_ptr->tshut_gpio;
789 /* Request for gpio_86 line */
790 status = gpio_request(gpio_nr, "tshut");
793 "Could not request for TSHUT GPIO:%i\n", 86);
796 status = gpio_direction_input(gpio_nr);
799 "Cannot set input TSHUT GPIO %d\n", gpio_nr);
803 status = request_irq(gpio_to_irq(gpio_nr),
804 omap_bandgap_tshut_irq_handler,
805 IRQF_TRIGGER_RISING, "tshut",
809 dev_err(bg_ptr->dev, "request irq failed for TSHUT");
815 /* Initialization of Talert. Call it only if HAS(TALERT) is set */
816 static int omap_bandgap_talert_init(struct omap_bandgap *bg_ptr,
817 struct platform_device *pdev)
821 bg_ptr->irq = platform_get_irq(pdev, 0);
822 if (bg_ptr->irq < 0) {
823 dev_err(&pdev->dev, "get_irq failed\n");
826 ret = request_threaded_irq(bg_ptr->irq, NULL,
827 omap_bandgap_talert_irq_handler,
828 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
831 dev_err(&pdev->dev, "Request threaded irq failed.\n");
838 static const struct of_device_id of_omap_bandgap_match[];
839 static struct omap_bandgap *omap_bandgap_build(struct platform_device *pdev)
841 struct device_node *node = pdev->dev.of_node;
842 const struct of_device_id *of_id;
843 struct omap_bandgap *bg_ptr;
844 struct resource *res;
848 /* just for the sake */
850 dev_err(&pdev->dev, "no platform information available\n");
851 return ERR_PTR(-EINVAL);
854 bg_ptr = devm_kzalloc(&pdev->dev, sizeof(struct omap_bandgap),
857 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
858 return ERR_PTR(-ENOMEM);
861 of_id = of_match_device(of_omap_bandgap_match, &pdev->dev);
863 bg_ptr->conf = of_id->data;
869 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
872 chunk = devm_ioremap_resource(&pdev->dev, res);
874 bg_ptr->base = chunk;
876 return ERR_CAST(chunk);
881 if (OMAP_BANDGAP_HAS(bg_ptr, TSHUT)) {
882 if (of_property_read_u32(node, "ti,tshut-gpio", &prop) < 0) {
883 dev_err(&pdev->dev, "missing tshut gpio in device tree\n");
884 return ERR_PTR(-EINVAL);
886 bg_ptr->tshut_gpio = prop;
887 if (!gpio_is_valid(bg_ptr->tshut_gpio)) {
888 dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
890 return ERR_PTR(-EINVAL);
898 int omap_bandgap_probe(struct platform_device *pdev)
900 struct omap_bandgap *bg_ptr;
901 int clk_rate, ret = 0, i;
903 bg_ptr = omap_bandgap_build(pdev);
904 if (IS_ERR_OR_NULL(bg_ptr)) {
905 dev_err(&pdev->dev, "failed to fetch platform data\n");
906 return PTR_ERR(bg_ptr);
908 bg_ptr->dev = &pdev->dev;
910 if (OMAP_BANDGAP_HAS(bg_ptr, TSHUT)) {
911 ret = omap_bandgap_tshut_init(bg_ptr, pdev);
914 "failed to initialize system tshut IRQ\n");
919 bg_ptr->fclock = clk_get(NULL, bg_ptr->conf->fclock_name);
920 ret = IS_ERR_OR_NULL(bg_ptr->fclock);
922 dev_err(&pdev->dev, "failed to request fclock reference\n");
926 bg_ptr->div_clk = clk_get(NULL, bg_ptr->conf->div_ck_name);
927 ret = IS_ERR_OR_NULL(bg_ptr->div_clk);
930 "failed to request div_ts_ck clock ref\n");
934 for (i = 0; i < bg_ptr->conf->sensor_count; i++) {
935 struct temp_sensor_registers *tsr;
938 tsr = bg_ptr->conf->sensors[i].registers;
940 * check if the efuse has a non-zero value if not
941 * it is an untrimmed sample and the temperatures
942 * may not be accurate
944 val = omap_bandgap_readl(bg_ptr, tsr->bgap_efuse);
947 "Non-trimmed BGAP, Temp not accurate\n");
950 clk_rate = clk_round_rate(bg_ptr->div_clk,
951 bg_ptr->conf->sensors[0].ts_data->max_freq);
952 if (clk_rate < bg_ptr->conf->sensors[0].ts_data->min_freq ||
953 clk_rate == 0xffffffff) {
955 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
959 ret = clk_set_rate(bg_ptr->div_clk, clk_rate);
961 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
963 bg_ptr->clk_rate = clk_rate;
964 if (OMAP_BANDGAP_HAS(bg_ptr, CLK_CTRL))
965 clk_prepare_enable(bg_ptr->fclock);
968 mutex_init(&bg_ptr->bg_mutex);
969 bg_ptr->dev = &pdev->dev;
970 platform_set_drvdata(pdev, bg_ptr);
972 omap_bandgap_power(bg_ptr, true);
974 /* Set default counter to 1 for now */
975 if (OMAP_BANDGAP_HAS(bg_ptr, COUNTER))
976 for (i = 0; i < bg_ptr->conf->sensor_count; i++)
977 RMW_BITS(bg_ptr, i, bgap_counter, counter_mask, 1);
979 /* Set default thresholds for alert and shutdown */
980 for (i = 0; i < bg_ptr->conf->sensor_count; i++) {
981 struct temp_sensor_data *ts_data;
983 ts_data = bg_ptr->conf->sensors[i].ts_data;
985 if (OMAP_BANDGAP_HAS(bg_ptr, TALERT)) {
986 /* Set initial Talert thresholds */
987 RMW_BITS(bg_ptr, i, bgap_threshold,
988 threshold_tcold_mask, ts_data->t_cold);
989 RMW_BITS(bg_ptr, i, bgap_threshold,
990 threshold_thot_mask, ts_data->t_hot);
991 /* Enable the alert events */
992 RMW_BITS(bg_ptr, i, bgap_mask_ctrl, mask_hot_mask, 1);
993 RMW_BITS(bg_ptr, i, bgap_mask_ctrl, mask_cold_mask, 1);
996 if (OMAP_BANDGAP_HAS(bg_ptr, TSHUT_CONFIG)) {
997 /* Set initial Tshut thresholds */
998 RMW_BITS(bg_ptr, i, tshut_threshold,
999 tshut_hot_mask, ts_data->tshut_hot);
1000 RMW_BITS(bg_ptr, i, tshut_threshold,
1001 tshut_cold_mask, ts_data->tshut_cold);
1005 if (OMAP_BANDGAP_HAS(bg_ptr, MODE_CONFIG))
1006 enable_continuous_mode(bg_ptr);
1008 /* Set .250 seconds time as default counter */
1009 if (OMAP_BANDGAP_HAS(bg_ptr, COUNTER))
1010 for (i = 0; i < bg_ptr->conf->sensor_count; i++)
1011 RMW_BITS(bg_ptr, i, bgap_counter, counter_mask,
1012 bg_ptr->clk_rate / 4);
1014 /* Every thing is good? Then expose the sensors */
1015 for (i = 0; i < bg_ptr->conf->sensor_count; i++) {
1018 if (bg_ptr->conf->sensors[i].register_cooling)
1019 bg_ptr->conf->sensors[i].register_cooling(bg_ptr, i);
1021 domain = bg_ptr->conf->sensors[i].domain;
1022 if (bg_ptr->conf->expose_sensor)
1023 bg_ptr->conf->expose_sensor(bg_ptr, i, domain);
1027 * Enable the Interrupts once everything is set. Otherwise irq handler
1028 * might be called as soon as it is enabled where as rest of framework
1029 * is still getting initialised.
1031 if (OMAP_BANDGAP_HAS(bg_ptr, TALERT)) {
1032 ret = omap_bandgap_talert_init(bg_ptr, pdev);
1034 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
1035 i = bg_ptr->conf->sensor_count;
1043 if (OMAP_BANDGAP_HAS(bg_ptr, CLK_CTRL))
1044 clk_disable_unprepare(bg_ptr->fclock);
1046 clk_put(bg_ptr->fclock);
1047 clk_put(bg_ptr->div_clk);
1049 if (OMAP_BANDGAP_HAS(bg_ptr, TSHUT)) {
1050 free_irq(gpio_to_irq(bg_ptr->tshut_gpio), NULL);
1051 gpio_free(bg_ptr->tshut_gpio);
1058 int omap_bandgap_remove(struct platform_device *pdev)
1060 struct omap_bandgap *bg_ptr = platform_get_drvdata(pdev);
1063 /* First thing is to remove sensor interfaces */
1064 for (i = 0; i < bg_ptr->conf->sensor_count; i++) {
1065 if (bg_ptr->conf->sensors[i].register_cooling)
1066 bg_ptr->conf->sensors[i].unregister_cooling(bg_ptr, i);
1068 if (bg_ptr->conf->remove_sensor)
1069 bg_ptr->conf->remove_sensor(bg_ptr, i);
1072 omap_bandgap_power(bg_ptr, false);
1074 if (OMAP_BANDGAP_HAS(bg_ptr, CLK_CTRL))
1075 clk_disable_unprepare(bg_ptr->fclock);
1076 clk_put(bg_ptr->fclock);
1077 clk_put(bg_ptr->div_clk);
1079 if (OMAP_BANDGAP_HAS(bg_ptr, TALERT))
1080 free_irq(bg_ptr->irq, bg_ptr);
1082 if (OMAP_BANDGAP_HAS(bg_ptr, TSHUT)) {
1083 free_irq(gpio_to_irq(bg_ptr->tshut_gpio), NULL);
1084 gpio_free(bg_ptr->tshut_gpio);
1091 static int omap_bandgap_save_ctxt(struct omap_bandgap *bg_ptr)
1095 for (i = 0; i < bg_ptr->conf->sensor_count; i++) {
1096 struct temp_sensor_registers *tsr;
1097 struct temp_sensor_regval *rval;
1099 rval = &bg_ptr->conf->sensors[i].regval;
1100 tsr = bg_ptr->conf->sensors[i].registers;
1102 if (OMAP_BANDGAP_HAS(bg_ptr, MODE_CONFIG))
1103 rval->bg_mode_ctrl = omap_bandgap_readl(bg_ptr,
1104 tsr->bgap_mode_ctrl);
1105 if (OMAP_BANDGAP_HAS(bg_ptr, COUNTER))
1106 rval->bg_counter = omap_bandgap_readl(bg_ptr,
1108 if (OMAP_BANDGAP_HAS(bg_ptr, TALERT)) {
1109 rval->bg_threshold = omap_bandgap_readl(bg_ptr,
1110 tsr->bgap_threshold);
1111 rval->bg_ctrl = omap_bandgap_readl(bg_ptr,
1112 tsr->bgap_mask_ctrl);
1115 if (OMAP_BANDGAP_HAS(bg_ptr, TSHUT_CONFIG))
1116 rval->tshut_threshold = omap_bandgap_readl(bg_ptr,
1117 tsr->tshut_threshold);
1123 static int omap_bandgap_restore_ctxt(struct omap_bandgap *bg_ptr)
1127 for (i = 0; i < bg_ptr->conf->sensor_count; i++) {
1128 struct temp_sensor_registers *tsr;
1129 struct temp_sensor_regval *rval;
1132 rval = &bg_ptr->conf->sensors[i].regval;
1133 tsr = bg_ptr->conf->sensors[i].registers;
1135 if (OMAP_BANDGAP_HAS(bg_ptr, COUNTER))
1136 val = omap_bandgap_readl(bg_ptr, tsr->bgap_counter);
1138 if (OMAP_BANDGAP_HAS(bg_ptr, TSHUT_CONFIG))
1139 omap_bandgap_writel(bg_ptr, rval->tshut_threshold,
1140 tsr->tshut_threshold);
1141 /* Force immediate temperature measurement and update
1142 * of the DTEMP field
1144 omap_bandgap_force_single_read(bg_ptr, i);
1146 if (OMAP_BANDGAP_HAS(bg_ptr, COUNTER))
1147 omap_bandgap_writel(bg_ptr, rval->bg_counter,
1149 if (OMAP_BANDGAP_HAS(bg_ptr, MODE_CONFIG))
1150 omap_bandgap_writel(bg_ptr, rval->bg_mode_ctrl,
1151 tsr->bgap_mode_ctrl);
1152 if (OMAP_BANDGAP_HAS(bg_ptr, TALERT)) {
1153 omap_bandgap_writel(bg_ptr, rval->bg_threshold,
1154 tsr->bgap_threshold);
1155 omap_bandgap_writel(bg_ptr, rval->bg_ctrl,
1156 tsr->bgap_mask_ctrl);
1163 static int omap_bandgap_suspend(struct device *dev)
1165 struct omap_bandgap *bg_ptr = dev_get_drvdata(dev);
1168 err = omap_bandgap_save_ctxt(bg_ptr);
1169 omap_bandgap_power(bg_ptr, false);
1171 if (OMAP_BANDGAP_HAS(bg_ptr, CLK_CTRL))
1172 clk_disable_unprepare(bg_ptr->fclock);
1177 static int omap_bandgap_resume(struct device *dev)
1179 struct omap_bandgap *bg_ptr = dev_get_drvdata(dev);
1181 if (OMAP_BANDGAP_HAS(bg_ptr, CLK_CTRL))
1182 clk_prepare_enable(bg_ptr->fclock);
1184 omap_bandgap_power(bg_ptr, true);
1186 return omap_bandgap_restore_ctxt(bg_ptr);
1188 static const struct dev_pm_ops omap_bandgap_dev_pm_ops = {
1189 SET_SYSTEM_SLEEP_PM_OPS(omap_bandgap_suspend,
1190 omap_bandgap_resume)
1193 #define DEV_PM_OPS (&omap_bandgap_dev_pm_ops)
1195 #define DEV_PM_OPS NULL
1198 static const struct of_device_id of_omap_bandgap_match[] = {
1199 #ifdef CONFIG_OMAP4_THERMAL
1201 .compatible = "ti,omap4430-bandgap",
1202 .data = (void *)&omap4430_data,
1205 .compatible = "ti,omap4460-bandgap",
1206 .data = (void *)&omap4460_data,
1209 .compatible = "ti,omap4470-bandgap",
1210 .data = (void *)&omap4470_data,
1213 #ifdef CONFIG_OMAP5_THERMAL
1215 .compatible = "ti,omap5430-bandgap",
1216 .data = (void *)&omap5430_data,
1222 MODULE_DEVICE_TABLE(of, of_omap_bandgap_match);
1224 static struct platform_driver omap_bandgap_sensor_driver = {
1225 .probe = omap_bandgap_probe,
1226 .remove = omap_bandgap_remove,
1228 .name = "omap-bandgap",
1230 .of_match_table = of_omap_bandgap_match,
1234 module_platform_driver(omap_bandgap_sensor_driver);
1236 MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1237 MODULE_LICENSE("GPL v2");
1238 MODULE_ALIAS("platform:omap-bandgap");
1239 MODULE_AUTHOR("Texas Instrument Inc.");