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);
235 int adc_to_temp_conversion(struct omap_bandgap
*bg_ptr
, int id
, int adc_val
,
238 struct temp_sensor_data
*ts_data
= bg_ptr
->conf
->sensors
[id
].ts_data
;
240 /* look up for temperature in the table and return the temperature */
241 if (adc_val
< ts_data
->adc_start_val
|| adc_val
> ts_data
->adc_end_val
)
244 *t
= bg_ptr
->conf
->conv_table
[adc_val
- ts_data
->adc_start_val
];
249 static int temp_to_adc_conversion(long temp
, struct omap_bandgap
*bg_ptr
, int i
,
252 struct temp_sensor_data
*ts_data
= bg_ptr
->conf
->sensors
[i
].ts_data
;
253 const int *conv_table
= bg_ptr
->conf
->conv_table
;
257 high
= ts_data
->adc_end_val
- ts_data
->adc_start_val
;
258 mid
= (high
+ low
) / 2;
260 if (temp
< conv_table
[low
] || temp
> conv_table
[high
])
264 if (temp
< conv_table
[mid
])
268 mid
= (low
+ high
) / 2;
271 *adc
= ts_data
->adc_start_val
+ low
;
276 /* Talert masks. Call it only if HAS(TALERT) is set */
277 static int temp_sensor_unmask_interrupts(struct omap_bandgap
*bg_ptr
, int id
,
278 u32 t_hot
, u32 t_cold
)
280 struct temp_sensor_registers
*tsr
;
283 /* Read the current on die temperature */
284 temp
= omap_bandgap_read_temp(bg_ptr
, id
);
286 tsr
= bg_ptr
->conf
->sensors
[id
].registers
;
287 reg_val
= omap_bandgap_readl(bg_ptr
, tsr
->bgap_mask_ctrl
);
290 reg_val
|= tsr
->mask_hot_mask
;
292 reg_val
&= ~tsr
->mask_hot_mask
;
295 reg_val
|= tsr
->mask_cold_mask
;
297 reg_val
&= ~tsr
->mask_cold_mask
;
298 omap_bandgap_writel(bg_ptr
, reg_val
, tsr
->bgap_mask_ctrl
);
304 int add_hyst(int adc_val
, int hyst_val
, struct omap_bandgap
*bg_ptr
, int i
,
309 ret
= adc_to_temp_conversion(bg_ptr
, i
, adc_val
, &temp
);
315 return temp_to_adc_conversion(temp
, bg_ptr
, i
, sum
);
318 /* Talert Thot threshold. Call it only if HAS(TALERT) is set */
320 int temp_sensor_configure_thot(struct omap_bandgap
*bg_ptr
, int id
, int t_hot
)
322 struct temp_sensor_data
*ts_data
= bg_ptr
->conf
->sensors
[id
].ts_data
;
323 struct temp_sensor_registers
*tsr
;
324 u32 thresh_val
, reg_val
;
327 tsr
= bg_ptr
->conf
->sensors
[id
].registers
;
329 /* obtain the T cold value */
330 thresh_val
= omap_bandgap_readl(bg_ptr
, tsr
->bgap_threshold
);
331 cold
= (thresh_val
& tsr
->threshold_tcold_mask
) >>
332 __ffs(tsr
->threshold_tcold_mask
);
334 /* change the t_cold to t_hot - 5000 millidegrees */
335 err
|= add_hyst(t_hot
, -ts_data
->hyst_val
, bg_ptr
, id
, &cold
);
336 /* write the new t_cold value */
337 reg_val
= thresh_val
& (~tsr
->threshold_tcold_mask
);
338 reg_val
|= cold
<< __ffs(tsr
->threshold_tcold_mask
);
339 omap_bandgap_writel(bg_ptr
, reg_val
, tsr
->bgap_threshold
);
340 thresh_val
= reg_val
;
343 /* write the new t_hot value */
344 reg_val
= thresh_val
& ~tsr
->threshold_thot_mask
;
345 reg_val
|= (t_hot
<< __ffs(tsr
->threshold_thot_mask
));
346 omap_bandgap_writel(bg_ptr
, reg_val
, tsr
->bgap_threshold
);
348 dev_err(bg_ptr
->dev
, "failed to reprogram thot threshold\n");
352 return temp_sensor_unmask_interrupts(bg_ptr
, id
, t_hot
, cold
);
355 /* Talert Tcold threshold. Call it only if HAS(TALERT) is set */
357 int temp_sensor_configure_tcold(struct omap_bandgap
*bg_ptr
, int id
,
360 struct temp_sensor_data
*ts_data
= bg_ptr
->conf
->sensors
[id
].ts_data
;
361 struct temp_sensor_registers
*tsr
;
362 u32 thresh_val
, reg_val
;
365 tsr
= bg_ptr
->conf
->sensors
[id
].registers
;
366 /* obtain the T cold value */
367 thresh_val
= omap_bandgap_readl(bg_ptr
, tsr
->bgap_threshold
);
368 hot
= (thresh_val
& tsr
->threshold_thot_mask
) >>
369 __ffs(tsr
->threshold_thot_mask
);
372 /* change the t_hot to t_cold + 5000 millidegrees */
373 err
|= add_hyst(t_cold
, ts_data
->hyst_val
, bg_ptr
, id
, &hot
);
374 /* write the new t_hot value */
375 reg_val
= thresh_val
& (~tsr
->threshold_thot_mask
);
376 reg_val
|= hot
<< __ffs(tsr
->threshold_thot_mask
);
377 omap_bandgap_writel(bg_ptr
, reg_val
, tsr
->bgap_threshold
);
378 thresh_val
= reg_val
;
381 /* write the new t_cold value */
382 reg_val
= thresh_val
& ~tsr
->threshold_tcold_mask
;
383 reg_val
|= (t_cold
<< __ffs(tsr
->threshold_tcold_mask
));
384 omap_bandgap_writel(bg_ptr
, reg_val
, tsr
->bgap_threshold
);
386 dev_err(bg_ptr
->dev
, "failed to reprogram tcold threshold\n");
390 return temp_sensor_unmask_interrupts(bg_ptr
, id
, hot
, t_cold
);
393 #define bandgap_is_valid(b) \
395 #define bandgap_is_valid_sensor_id(b, i) \
396 ((i) >= 0 && (i) < (b)->conf->sensor_count)
397 static inline int omap_bandgap_validate(struct omap_bandgap
*bg_ptr
, int id
)
399 if (!bandgap_is_valid(bg_ptr
)) {
400 pr_err("%s: invalid bandgap pointer\n", __func__
);
404 if (!bandgap_is_valid_sensor_id(bg_ptr
, id
)) {
405 dev_err(bg_ptr
->dev
, "%s: sensor id out of range (%d)\n",
415 * omap_bandgap_read_thot() - reads sensor current thot
416 * @bg_ptr - pointer to bandgap instance
418 * @thot - resulting current thot value
420 * returns 0 on success or the proper error code
422 int omap_bandgap_read_thot(struct omap_bandgap
*bg_ptr
, int id
,
425 struct temp_sensor_registers
*tsr
;
429 ret
= omap_bandgap_validate(bg_ptr
, id
);
433 if (!OMAP_BANDGAP_HAS(bg_ptr
, TALERT
))
436 tsr
= bg_ptr
->conf
->sensors
[id
].registers
;
437 temp
= omap_bandgap_readl(bg_ptr
, tsr
->bgap_threshold
);
438 temp
= (temp
& tsr
->threshold_thot_mask
) >>
439 __ffs(tsr
->threshold_thot_mask
);
440 ret
|= adc_to_temp_conversion(bg_ptr
, id
, temp
, &temp
);
442 dev_err(bg_ptr
->dev
, "failed to read thot\n");
452 * omap_bandgap_write_thot() - sets sensor current thot
453 * @bg_ptr - pointer to bandgap instance
455 * @val - desired thot value
457 * returns 0 on success or the proper error code
459 int omap_bandgap_write_thot(struct omap_bandgap
*bg_ptr
, int id
, int val
)
461 struct temp_sensor_data
*ts_data
;
462 struct temp_sensor_registers
*tsr
;
466 ret
= omap_bandgap_validate(bg_ptr
, id
);
470 if (!OMAP_BANDGAP_HAS(bg_ptr
, TALERT
))
473 ts_data
= bg_ptr
->conf
->sensors
[id
].ts_data
;
474 tsr
= bg_ptr
->conf
->sensors
[id
].registers
;
476 if (val
< ts_data
->min_temp
+ ts_data
->hyst_val
)
478 ret
= temp_to_adc_conversion(val
, bg_ptr
, id
, &t_hot
);
482 mutex_lock(&bg_ptr
->bg_mutex
);
483 temp_sensor_configure_thot(bg_ptr
, id
, t_hot
);
484 mutex_unlock(&bg_ptr
->bg_mutex
);
490 * omap_bandgap_read_tcold() - reads sensor current tcold
491 * @bg_ptr - pointer to bandgap instance
493 * @tcold - resulting current tcold value
495 * returns 0 on success or the proper error code
497 int omap_bandgap_read_tcold(struct omap_bandgap
*bg_ptr
, int id
,
500 struct temp_sensor_registers
*tsr
;
504 ret
= omap_bandgap_validate(bg_ptr
, id
);
508 if (!OMAP_BANDGAP_HAS(bg_ptr
, TALERT
))
511 tsr
= bg_ptr
->conf
->sensors
[id
].registers
;
512 temp
= omap_bandgap_readl(bg_ptr
, tsr
->bgap_threshold
);
513 temp
= (temp
& tsr
->threshold_tcold_mask
)
514 >> __ffs(tsr
->threshold_tcold_mask
);
515 ret
|= adc_to_temp_conversion(bg_ptr
, id
, temp
, &temp
);
525 * omap_bandgap_write_tcold() - sets the sensor tcold
526 * @bg_ptr - pointer to bandgap instance
528 * @val - desired tcold value
530 * returns 0 on success or the proper error code
532 int omap_bandgap_write_tcold(struct omap_bandgap
*bg_ptr
, int id
, int val
)
534 struct temp_sensor_data
*ts_data
;
535 struct temp_sensor_registers
*tsr
;
539 ret
= omap_bandgap_validate(bg_ptr
, id
);
543 if (!OMAP_BANDGAP_HAS(bg_ptr
, TALERT
))
546 ts_data
= bg_ptr
->conf
->sensors
[id
].ts_data
;
547 tsr
= bg_ptr
->conf
->sensors
[id
].registers
;
548 if (val
> ts_data
->max_temp
+ ts_data
->hyst_val
)
551 ret
= temp_to_adc_conversion(val
, bg_ptr
, id
, &t_cold
);
555 mutex_lock(&bg_ptr
->bg_mutex
);
556 temp_sensor_configure_tcold(bg_ptr
, id
, t_cold
);
557 mutex_unlock(&bg_ptr
->bg_mutex
);
563 * omap_bandgap_read_update_interval() - read the sensor update interval
564 * @bg_ptr - pointer to bandgap instance
566 * @interval - resulting update interval in miliseconds
568 * returns 0 on success or the proper error code
570 int omap_bandgap_read_update_interval(struct omap_bandgap
*bg_ptr
, int id
,
573 struct temp_sensor_registers
*tsr
;
577 ret
= omap_bandgap_validate(bg_ptr
, id
);
581 if (!OMAP_BANDGAP_HAS(bg_ptr
, COUNTER
))
584 tsr
= bg_ptr
->conf
->sensors
[id
].registers
;
585 time
= omap_bandgap_readl(bg_ptr
, tsr
->bgap_counter
);
586 time
= (time
& tsr
->counter_mask
) >> __ffs(tsr
->counter_mask
);
587 time
= time
* 1000 / bg_ptr
->clk_rate
;
595 * omap_bandgap_write_update_interval() - set the update interval
596 * @bg_ptr - pointer to bandgap instance
598 * @interval - desired update interval in miliseconds
600 * returns 0 on success or the proper error code
602 int omap_bandgap_write_update_interval(struct omap_bandgap
*bg_ptr
,
603 int id
, u32 interval
)
605 int ret
= omap_bandgap_validate(bg_ptr
, id
);
609 if (!OMAP_BANDGAP_HAS(bg_ptr
, COUNTER
))
612 interval
= interval
* bg_ptr
->clk_rate
/ 1000;
613 mutex_lock(&bg_ptr
->bg_mutex
);
614 RMW_BITS(bg_ptr
, id
, bgap_counter
, counter_mask
, interval
);
615 mutex_unlock(&bg_ptr
->bg_mutex
);
621 * omap_bandgap_read_temperature() - report current temperature
622 * @bg_ptr - pointer to bandgap instance
624 * @temperature - resulting temperature
626 * returns 0 on success or the proper error code
628 int omap_bandgap_read_temperature(struct omap_bandgap
*bg_ptr
, int id
,
634 ret
= omap_bandgap_validate(bg_ptr
, id
);
638 mutex_lock(&bg_ptr
->bg_mutex
);
639 temp
= omap_bandgap_read_temp(bg_ptr
, id
);
640 mutex_unlock(&bg_ptr
->bg_mutex
);
642 ret
|= adc_to_temp_conversion(bg_ptr
, id
, temp
, &temp
);
652 * omap_bandgap_set_sensor_data() - helper function to store thermal
653 * framework related data.
654 * @bg_ptr - pointer to bandgap instance
656 * @data - thermal framework related data to be stored
658 * returns 0 on success or the proper error code
660 int omap_bandgap_set_sensor_data(struct omap_bandgap
*bg_ptr
, int id
,
663 int ret
= omap_bandgap_validate(bg_ptr
, id
);
667 bg_ptr
->conf
->sensors
[id
].data
= data
;
673 * omap_bandgap_get_sensor_data() - helper function to get thermal
674 * framework related data.
675 * @bg_ptr - pointer to bandgap instance
678 * returns data stored by set function with sensor id on success or NULL
680 void *omap_bandgap_get_sensor_data(struct omap_bandgap
*bg_ptr
, int id
)
682 int ret
= omap_bandgap_validate(bg_ptr
, id
);
686 return bg_ptr
->conf
->sensors
[id
].data
;
690 omap_bandgap_force_single_read(struct omap_bandgap
*bg_ptr
, int id
)
692 u32 temp
= 0, counter
= 1000;
694 /* Select single conversion mode */
695 if (OMAP_BANDGAP_HAS(bg_ptr
, MODE_CONFIG
))
696 RMW_BITS(bg_ptr
, id
, bgap_mode_ctrl
, mode_ctrl_mask
, 0);
698 /* Start of Conversion = 1 */
699 RMW_BITS(bg_ptr
, id
, temp_sensor_ctrl
, bgap_soc_mask
, 1);
700 /* Wait until DTEMP is updated */
701 temp
= omap_bandgap_read_temp(bg_ptr
, id
);
703 while ((temp
== 0) && --counter
)
704 temp
= omap_bandgap_read_temp(bg_ptr
, id
);
705 /* REVISIT: Check correct condition for end of conversion */
707 /* Start of Conversion = 0 */
708 RMW_BITS(bg_ptr
, id
, temp_sensor_ctrl
, bgap_soc_mask
, 0);
714 * enable_continuous_mode() - One time enabling of continuous conversion mode
715 * @bg_ptr - pointer to scm instance
717 * Call this function only if HAS(MODE_CONFIG) is set
719 static int enable_continuous_mode(struct omap_bandgap
*bg_ptr
)
723 for (i
= 0; i
< bg_ptr
->conf
->sensor_count
; i
++) {
724 /* Perform a single read just before enabling continuous */
725 omap_bandgap_force_single_read(bg_ptr
, i
);
726 RMW_BITS(bg_ptr
, i
, bgap_mode_ctrl
, mode_ctrl_mask
, 1);
732 static int omap_bandgap_tshut_init(struct omap_bandgap
*bg_ptr
,
733 struct platform_device
*pdev
)
735 int gpio_nr
= bg_ptr
->tshut_gpio
;
738 /* Request for gpio_86 line */
739 status
= gpio_request(gpio_nr
, "tshut");
742 "Could not request for TSHUT GPIO:%i\n", 86);
745 status
= gpio_direction_input(gpio_nr
);
748 "Cannot set input TSHUT GPIO %d\n", gpio_nr
);
752 status
= request_irq(gpio_to_irq(gpio_nr
),
753 omap_bandgap_tshut_irq_handler
,
754 IRQF_TRIGGER_RISING
, "tshut",
758 dev_err(bg_ptr
->dev
, "request irq failed for TSHUT");
764 /* Initialization of Talert. Call it only if HAS(TALERT) is set */
765 static int omap_bandgap_talert_init(struct omap_bandgap
*bg_ptr
,
766 struct platform_device
*pdev
)
770 bg_ptr
->irq
= platform_get_irq(pdev
, 0);
771 if (bg_ptr
->irq
< 0) {
772 dev_err(&pdev
->dev
, "get_irq failed\n");
775 ret
= request_threaded_irq(bg_ptr
->irq
, NULL
,
776 omap_bandgap_talert_irq_handler
,
777 IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
,
780 dev_err(&pdev
->dev
, "Request threaded irq failed.\n");
787 static const struct of_device_id of_omap_bandgap_match
[];
788 static struct omap_bandgap
*omap_bandgap_build(struct platform_device
*pdev
)
790 struct device_node
*node
= pdev
->dev
.of_node
;
791 const struct of_device_id
*of_id
;
792 struct omap_bandgap
*bg_ptr
;
793 struct resource
*res
;
797 /* just for the sake */
799 dev_err(&pdev
->dev
, "no platform information available\n");
800 return ERR_PTR(-EINVAL
);
803 bg_ptr
= devm_kzalloc(&pdev
->dev
, sizeof(struct omap_bandgap
),
806 dev_err(&pdev
->dev
, "Unable to allocate mem for driver ref\n");
807 return ERR_PTR(-ENOMEM
);
810 of_id
= of_match_device(of_omap_bandgap_match
, &pdev
->dev
);
812 bg_ptr
->conf
= of_id
->data
;
818 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, i
);
821 chunk
= devm_ioremap_resource(&pdev
->dev
, res
);
823 bg_ptr
->base
= chunk
;
825 return ERR_CAST(chunk
);
830 if (OMAP_BANDGAP_HAS(bg_ptr
, TSHUT
)) {
831 if (of_property_read_u32(node
, "ti,tshut-gpio", &prop
) < 0) {
832 dev_err(&pdev
->dev
, "missing tshut gpio in device tree\n");
833 return ERR_PTR(-EINVAL
);
835 bg_ptr
->tshut_gpio
= prop
;
836 if (!gpio_is_valid(bg_ptr
->tshut_gpio
)) {
837 dev_err(&pdev
->dev
, "invalid gpio for tshut (%d)\n",
839 return ERR_PTR(-EINVAL
);
847 int omap_bandgap_probe(struct platform_device
*pdev
)
849 struct omap_bandgap
*bg_ptr
;
850 int clk_rate
, ret
= 0, i
;
852 bg_ptr
= omap_bandgap_build(pdev
);
853 if (IS_ERR_OR_NULL(bg_ptr
)) {
854 dev_err(&pdev
->dev
, "failed to fetch platform data\n");
855 return PTR_ERR(bg_ptr
);
857 bg_ptr
->dev
= &pdev
->dev
;
859 if (OMAP_BANDGAP_HAS(bg_ptr
, TSHUT
)) {
860 ret
= omap_bandgap_tshut_init(bg_ptr
, pdev
);
863 "failed to initialize system tshut IRQ\n");
868 bg_ptr
->fclock
= clk_get(NULL
, bg_ptr
->conf
->fclock_name
);
869 ret
= IS_ERR_OR_NULL(bg_ptr
->fclock
);
871 dev_err(&pdev
->dev
, "failed to request fclock reference\n");
875 bg_ptr
->div_clk
= clk_get(NULL
, bg_ptr
->conf
->div_ck_name
);
876 ret
= IS_ERR_OR_NULL(bg_ptr
->div_clk
);
879 "failed to request div_ts_ck clock ref\n");
883 for (i
= 0; i
< bg_ptr
->conf
->sensor_count
; i
++) {
884 struct temp_sensor_registers
*tsr
;
887 tsr
= bg_ptr
->conf
->sensors
[i
].registers
;
889 * check if the efuse has a non-zero value if not
890 * it is an untrimmed sample and the temperatures
891 * may not be accurate
893 val
= omap_bandgap_readl(bg_ptr
, tsr
->bgap_efuse
);
896 "Non-trimmed BGAP, Temp not accurate\n");
899 clk_rate
= clk_round_rate(bg_ptr
->div_clk
,
900 bg_ptr
->conf
->sensors
[0].ts_data
->max_freq
);
901 if (clk_rate
< bg_ptr
->conf
->sensors
[0].ts_data
->min_freq
||
902 clk_rate
== 0xffffffff) {
904 dev_err(&pdev
->dev
, "wrong clock rate (%d)\n", clk_rate
);
908 ret
= clk_set_rate(bg_ptr
->div_clk
, clk_rate
);
910 dev_err(&pdev
->dev
, "Cannot re-set clock rate. Continuing\n");
912 bg_ptr
->clk_rate
= clk_rate
;
913 if (OMAP_BANDGAP_HAS(bg_ptr
, CLK_CTRL
))
914 clk_prepare_enable(bg_ptr
->fclock
);
917 mutex_init(&bg_ptr
->bg_mutex
);
918 bg_ptr
->dev
= &pdev
->dev
;
919 platform_set_drvdata(pdev
, bg_ptr
);
921 omap_bandgap_power(bg_ptr
, true);
923 /* Set default counter to 1 for now */
924 if (OMAP_BANDGAP_HAS(bg_ptr
, COUNTER
))
925 for (i
= 0; i
< bg_ptr
->conf
->sensor_count
; i
++)
926 RMW_BITS(bg_ptr
, i
, bgap_counter
, counter_mask
, 1);
928 /* Set default thresholds for alert and shutdown */
929 for (i
= 0; i
< bg_ptr
->conf
->sensor_count
; i
++) {
930 struct temp_sensor_data
*ts_data
;
932 ts_data
= bg_ptr
->conf
->sensors
[i
].ts_data
;
934 if (OMAP_BANDGAP_HAS(bg_ptr
, TALERT
)) {
935 /* Set initial Talert thresholds */
936 RMW_BITS(bg_ptr
, i
, bgap_threshold
,
937 threshold_tcold_mask
, ts_data
->t_cold
);
938 RMW_BITS(bg_ptr
, i
, bgap_threshold
,
939 threshold_thot_mask
, ts_data
->t_hot
);
940 /* Enable the alert events */
941 RMW_BITS(bg_ptr
, i
, bgap_mask_ctrl
, mask_hot_mask
, 1);
942 RMW_BITS(bg_ptr
, i
, bgap_mask_ctrl
, mask_cold_mask
, 1);
945 if (OMAP_BANDGAP_HAS(bg_ptr
, TSHUT_CONFIG
)) {
946 /* Set initial Tshut thresholds */
947 RMW_BITS(bg_ptr
, i
, tshut_threshold
,
948 tshut_hot_mask
, ts_data
->tshut_hot
);
949 RMW_BITS(bg_ptr
, i
, tshut_threshold
,
950 tshut_cold_mask
, ts_data
->tshut_cold
);
954 if (OMAP_BANDGAP_HAS(bg_ptr
, MODE_CONFIG
))
955 enable_continuous_mode(bg_ptr
);
957 /* Set .250 seconds time as default counter */
958 if (OMAP_BANDGAP_HAS(bg_ptr
, COUNTER
))
959 for (i
= 0; i
< bg_ptr
->conf
->sensor_count
; i
++)
960 RMW_BITS(bg_ptr
, i
, bgap_counter
, counter_mask
,
961 bg_ptr
->clk_rate
/ 4);
963 /* Every thing is good? Then expose the sensors */
964 for (i
= 0; i
< bg_ptr
->conf
->sensor_count
; i
++) {
967 if (bg_ptr
->conf
->sensors
[i
].register_cooling
)
968 bg_ptr
->conf
->sensors
[i
].register_cooling(bg_ptr
, i
);
970 domain
= bg_ptr
->conf
->sensors
[i
].domain
;
971 if (bg_ptr
->conf
->expose_sensor
)
972 bg_ptr
->conf
->expose_sensor(bg_ptr
, i
, domain
);
976 * Enable the Interrupts once everything is set. Otherwise irq handler
977 * might be called as soon as it is enabled where as rest of framework
978 * is still getting initialised.
980 if (OMAP_BANDGAP_HAS(bg_ptr
, TALERT
)) {
981 ret
= omap_bandgap_talert_init(bg_ptr
, pdev
);
983 dev_err(&pdev
->dev
, "failed to initialize Talert IRQ\n");
984 i
= bg_ptr
->conf
->sensor_count
;
992 if (OMAP_BANDGAP_HAS(bg_ptr
, CLK_CTRL
))
993 clk_disable_unprepare(bg_ptr
->fclock
);
995 clk_put(bg_ptr
->fclock
);
996 clk_put(bg_ptr
->div_clk
);
998 if (OMAP_BANDGAP_HAS(bg_ptr
, TSHUT
)) {
999 free_irq(gpio_to_irq(bg_ptr
->tshut_gpio
), NULL
);
1000 gpio_free(bg_ptr
->tshut_gpio
);
1007 int omap_bandgap_remove(struct platform_device
*pdev
)
1009 struct omap_bandgap
*bg_ptr
= platform_get_drvdata(pdev
);
1012 /* First thing is to remove sensor interfaces */
1013 for (i
= 0; i
< bg_ptr
->conf
->sensor_count
; i
++) {
1014 if (bg_ptr
->conf
->sensors
[i
].register_cooling
)
1015 bg_ptr
->conf
->sensors
[i
].unregister_cooling(bg_ptr
, i
);
1017 if (bg_ptr
->conf
->remove_sensor
)
1018 bg_ptr
->conf
->remove_sensor(bg_ptr
, i
);
1021 omap_bandgap_power(bg_ptr
, false);
1023 if (OMAP_BANDGAP_HAS(bg_ptr
, CLK_CTRL
))
1024 clk_disable_unprepare(bg_ptr
->fclock
);
1025 clk_put(bg_ptr
->fclock
);
1026 clk_put(bg_ptr
->div_clk
);
1028 if (OMAP_BANDGAP_HAS(bg_ptr
, TALERT
))
1029 free_irq(bg_ptr
->irq
, bg_ptr
);
1031 if (OMAP_BANDGAP_HAS(bg_ptr
, TSHUT
)) {
1032 free_irq(gpio_to_irq(bg_ptr
->tshut_gpio
), NULL
);
1033 gpio_free(bg_ptr
->tshut_gpio
);
1040 static int omap_bandgap_save_ctxt(struct omap_bandgap
*bg_ptr
)
1044 for (i
= 0; i
< bg_ptr
->conf
->sensor_count
; i
++) {
1045 struct temp_sensor_registers
*tsr
;
1046 struct temp_sensor_regval
*rval
;
1048 rval
= &bg_ptr
->conf
->sensors
[i
].regval
;
1049 tsr
= bg_ptr
->conf
->sensors
[i
].registers
;
1051 if (OMAP_BANDGAP_HAS(bg_ptr
, MODE_CONFIG
))
1052 rval
->bg_mode_ctrl
= omap_bandgap_readl(bg_ptr
,
1053 tsr
->bgap_mode_ctrl
);
1054 if (OMAP_BANDGAP_HAS(bg_ptr
, COUNTER
))
1055 rval
->bg_counter
= omap_bandgap_readl(bg_ptr
,
1057 if (OMAP_BANDGAP_HAS(bg_ptr
, TALERT
)) {
1058 rval
->bg_threshold
= omap_bandgap_readl(bg_ptr
,
1059 tsr
->bgap_threshold
);
1060 rval
->bg_ctrl
= omap_bandgap_readl(bg_ptr
,
1061 tsr
->bgap_mask_ctrl
);
1064 if (OMAP_BANDGAP_HAS(bg_ptr
, TSHUT_CONFIG
))
1065 rval
->tshut_threshold
= omap_bandgap_readl(bg_ptr
,
1066 tsr
->tshut_threshold
);
1072 static int omap_bandgap_restore_ctxt(struct omap_bandgap
*bg_ptr
)
1076 for (i
= 0; i
< bg_ptr
->conf
->sensor_count
; i
++) {
1077 struct temp_sensor_registers
*tsr
;
1078 struct temp_sensor_regval
*rval
;
1081 rval
= &bg_ptr
->conf
->sensors
[i
].regval
;
1082 tsr
= bg_ptr
->conf
->sensors
[i
].registers
;
1084 if (OMAP_BANDGAP_HAS(bg_ptr
, COUNTER
))
1085 val
= omap_bandgap_readl(bg_ptr
, tsr
->bgap_counter
);
1087 if (OMAP_BANDGAP_HAS(bg_ptr
, TSHUT_CONFIG
))
1088 omap_bandgap_writel(bg_ptr
, rval
->tshut_threshold
,
1089 tsr
->tshut_threshold
);
1090 /* Force immediate temperature measurement and update
1091 * of the DTEMP field
1093 omap_bandgap_force_single_read(bg_ptr
, i
);
1095 if (OMAP_BANDGAP_HAS(bg_ptr
, COUNTER
))
1096 omap_bandgap_writel(bg_ptr
, rval
->bg_counter
,
1098 if (OMAP_BANDGAP_HAS(bg_ptr
, MODE_CONFIG
))
1099 omap_bandgap_writel(bg_ptr
, rval
->bg_mode_ctrl
,
1100 tsr
->bgap_mode_ctrl
);
1101 if (OMAP_BANDGAP_HAS(bg_ptr
, TALERT
)) {
1102 omap_bandgap_writel(bg_ptr
, rval
->bg_threshold
,
1103 tsr
->bgap_threshold
);
1104 omap_bandgap_writel(bg_ptr
, rval
->bg_ctrl
,
1105 tsr
->bgap_mask_ctrl
);
1112 static int omap_bandgap_suspend(struct device
*dev
)
1114 struct omap_bandgap
*bg_ptr
= dev_get_drvdata(dev
);
1117 err
= omap_bandgap_save_ctxt(bg_ptr
);
1118 omap_bandgap_power(bg_ptr
, false);
1120 if (OMAP_BANDGAP_HAS(bg_ptr
, CLK_CTRL
))
1121 clk_disable_unprepare(bg_ptr
->fclock
);
1126 static int omap_bandgap_resume(struct device
*dev
)
1128 struct omap_bandgap
*bg_ptr
= dev_get_drvdata(dev
);
1130 if (OMAP_BANDGAP_HAS(bg_ptr
, CLK_CTRL
))
1131 clk_prepare_enable(bg_ptr
->fclock
);
1133 omap_bandgap_power(bg_ptr
, true);
1135 return omap_bandgap_restore_ctxt(bg_ptr
);
1137 static const struct dev_pm_ops omap_bandgap_dev_pm_ops
= {
1138 SET_SYSTEM_SLEEP_PM_OPS(omap_bandgap_suspend
,
1139 omap_bandgap_resume
)
1142 #define DEV_PM_OPS (&omap_bandgap_dev_pm_ops)
1144 #define DEV_PM_OPS NULL
1147 static const struct of_device_id of_omap_bandgap_match
[] = {
1148 #ifdef CONFIG_OMAP4_THERMAL
1150 .compatible
= "ti,omap4430-bandgap",
1151 .data
= (void *)&omap4430_data
,
1154 .compatible
= "ti,omap4460-bandgap",
1155 .data
= (void *)&omap4460_data
,
1158 .compatible
= "ti,omap4470-bandgap",
1159 .data
= (void *)&omap4470_data
,
1162 #ifdef CONFIG_OMAP5_THERMAL
1164 .compatible
= "ti,omap5430-bandgap",
1165 .data
= (void *)&omap5430_data
,
1171 MODULE_DEVICE_TABLE(of
, of_omap_bandgap_match
);
1173 static struct platform_driver omap_bandgap_sensor_driver
= {
1174 .probe
= omap_bandgap_probe
,
1175 .remove
= omap_bandgap_remove
,
1177 .name
= "omap-bandgap",
1179 .of_match_table
= of_omap_bandgap_match
,
1183 module_platform_driver(omap_bandgap_sensor_driver
);
1185 MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1186 MODULE_LICENSE("GPL v2");
1187 MODULE_ALIAS("platform:omap-bandgap");
1188 MODULE_AUTHOR("Texas Instrument Inc.");