Loading...
1/*
2 * TI Bandgap temperature sensor driver
3 *
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>
9 *
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.
13 *
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.
18 *
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
22 * 02110-1301 USA
23 *
24 */
25
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>
41#include <linux/of_gpio.h>
42#include <linux/io.h>
43
44#include "ti-bandgap.h"
45
46/*** Helper functions to access registers and their bitfields ***/
47
48/**
49 * ti_bandgap_readl() - simple read helper function
50 * @bgp: pointer to ti_bandgap structure
51 * @reg: desired register (offset) to be read
52 *
53 * Helper function to read bandgap registers. It uses the io remapped area.
54 * Return: the register value.
55 */
56static u32 ti_bandgap_readl(struct ti_bandgap *bgp, u32 reg)
57{
58 return readl(bgp->base + reg);
59}
60
61/**
62 * ti_bandgap_writel() - simple write helper function
63 * @bgp: pointer to ti_bandgap structure
64 * @val: desired register value to be written
65 * @reg: desired register (offset) to be written
66 *
67 * Helper function to write bandgap registers. It uses the io remapped area.
68 */
69static void ti_bandgap_writel(struct ti_bandgap *bgp, u32 val, u32 reg)
70{
71 writel(val, bgp->base + reg);
72}
73
74/**
75 * DOC: macro to update bits.
76 *
77 * RMW_BITS() - used to read, modify and update bandgap bitfields.
78 * The value passed will be shifted.
79 */
80#define RMW_BITS(bgp, id, reg, mask, val) \
81do { \
82 struct temp_sensor_registers *t; \
83 u32 r; \
84 \
85 t = bgp->conf->sensors[(id)].registers; \
86 r = ti_bandgap_readl(bgp, t->reg); \
87 r &= ~t->mask; \
88 r |= (val) << __ffs(t->mask); \
89 ti_bandgap_writel(bgp, r, t->reg); \
90} while (0)
91
92/*** Basic helper functions ***/
93
94/**
95 * ti_bandgap_power() - controls the power state of a bandgap device
96 * @bgp: pointer to ti_bandgap structure
97 * @on: desired power state (1 - on, 0 - off)
98 *
99 * Used to power on/off a bandgap device instance. Only used on those
100 * that features tempsoff bit.
101 *
102 * Return: 0 on success, -ENOTSUPP if tempsoff is not supported.
103 */
104static int ti_bandgap_power(struct ti_bandgap *bgp, bool on)
105{
106 int i, ret = 0;
107
108 if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH)) {
109 ret = -ENOTSUPP;
110 goto exit;
111 }
112
113 for (i = 0; i < bgp->conf->sensor_count; i++)
114 /* active on 0 */
115 RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
116
117exit:
118 return ret;
119}
120
121/**
122 * ti_bandgap_read_temp() - helper function to read sensor temperature
123 * @bgp: pointer to ti_bandgap structure
124 * @id: bandgap sensor id
125 *
126 * Function to concentrate the steps to read sensor temperature register.
127 * This function is desired because, depending on bandgap device version,
128 * it might be needed to freeze the bandgap state machine, before fetching
129 * the register value.
130 *
131 * Return: temperature in ADC values.
132 */
133static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
134{
135 struct temp_sensor_registers *tsr;
136 u32 temp, reg;
137
138 tsr = bgp->conf->sensors[id].registers;
139 reg = tsr->temp_sensor_ctrl;
140
141 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
142 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
143 /*
144 * In case we cannot read from cur_dtemp / dtemp_0,
145 * then we read from the last valid temp read
146 */
147 reg = tsr->ctrl_dtemp_1;
148 }
149
150 /* read temperature */
151 temp = ti_bandgap_readl(bgp, reg);
152 temp &= tsr->bgap_dtemp_mask;
153
154 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
155 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
156
157 return temp;
158}
159
160/*** IRQ handlers ***/
161
162/**
163 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
164 * @irq: IRQ number
165 * @data: private data (struct ti_bandgap *)
166 *
167 * This is the Talert handler. Use it only if bandgap device features
168 * HAS(TALERT). This handler goes over all sensors and checks their
169 * conditions and acts accordingly. In case there are events pending,
170 * it will reset the event mask to wait for the opposite event (next event).
171 * Every time there is a new event, it will be reported to thermal layer.
172 *
173 * Return: IRQ_HANDLED
174 */
175static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
176{
177 struct ti_bandgap *bgp = data;
178 struct temp_sensor_registers *tsr;
179 u32 t_hot = 0, t_cold = 0, ctrl;
180 int i;
181
182 spin_lock(&bgp->lock);
183 for (i = 0; i < bgp->conf->sensor_count; i++) {
184 tsr = bgp->conf->sensors[i].registers;
185 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
186
187 /* Read the status of t_hot */
188 t_hot = ctrl & tsr->status_hot_mask;
189
190 /* Read the status of t_cold */
191 t_cold = ctrl & tsr->status_cold_mask;
192
193 if (!t_cold && !t_hot)
194 continue;
195
196 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
197 /*
198 * One TALERT interrupt: Two sources
199 * If the interrupt is due to t_hot then mask t_hot and
200 * and unmask t_cold else mask t_cold and unmask t_hot
201 */
202 if (t_hot) {
203 ctrl &= ~tsr->mask_hot_mask;
204 ctrl |= tsr->mask_cold_mask;
205 } else if (t_cold) {
206 ctrl &= ~tsr->mask_cold_mask;
207 ctrl |= tsr->mask_hot_mask;
208 }
209
210 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
211
212 dev_dbg(bgp->dev,
213 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
214 __func__, bgp->conf->sensors[i].domain,
215 t_hot, t_cold);
216
217 /* report temperature to whom may concern */
218 if (bgp->conf->report_temperature)
219 bgp->conf->report_temperature(bgp, i);
220 }
221 spin_unlock(&bgp->lock);
222
223 return IRQ_HANDLED;
224}
225
226/**
227 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
228 * @irq: IRQ number
229 * @data: private data (unused)
230 *
231 * This is the Tshut handler. Use it only if bandgap device features
232 * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
233 * the system.
234 *
235 * Return: IRQ_HANDLED
236 */
237static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
238{
239 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
240 __func__);
241
242 orderly_poweroff(true);
243
244 return IRQ_HANDLED;
245}
246
247/*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
248
249/**
250 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
251 * @bgp: struct ti_bandgap pointer
252 * @adc_val: value in ADC representation
253 * @t: address where to write the resulting temperature in mCelsius
254 *
255 * Simple conversion from ADC representation to mCelsius. In case the ADC value
256 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
257 * The conversion table is indexed by the ADC values.
258 *
259 * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val
260 * argument is out of the ADC conv table range.
261 */
262static
263int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
264{
265 const struct ti_bandgap_data *conf = bgp->conf;
266 int ret = 0;
267
268 /* look up for temperature in the table and return the temperature */
269 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val) {
270 ret = -ERANGE;
271 goto exit;
272 }
273
274 *t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
275
276exit:
277 return ret;
278}
279
280/**
281 * ti_bandgap_mcelsius_to_adc() - converts a mCelsius value to ADC scale
282 * @bgp: struct ti_bandgap pointer
283 * @temp: value in mCelsius
284 * @adc: address where to write the resulting temperature in ADC representation
285 *
286 * Simple conversion from mCelsius to ADC values. In case the temp value
287 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
288 * The conversion table is indexed by the ADC values.
289 *
290 * Return: 0 if conversion was successful, else -ERANGE in case the @temp
291 * argument is out of the ADC conv table range.
292 */
293static
294int ti_bandgap_mcelsius_to_adc(struct ti_bandgap *bgp, long temp, int *adc)
295{
296 const struct ti_bandgap_data *conf = bgp->conf;
297 const int *conv_table = bgp->conf->conv_table;
298 int high, low, mid, ret = 0;
299
300 low = 0;
301 high = conf->adc_end_val - conf->adc_start_val;
302 mid = (high + low) / 2;
303
304 if (temp < conv_table[low] || temp > conv_table[high]) {
305 ret = -ERANGE;
306 goto exit;
307 }
308
309 while (low < high) {
310 if (temp < conv_table[mid])
311 high = mid - 1;
312 else
313 low = mid + 1;
314 mid = (low + high) / 2;
315 }
316
317 *adc = conf->adc_start_val + low;
318
319exit:
320 return ret;
321}
322
323/**
324 * ti_bandgap_add_hyst() - add hysteresis (in mCelsius) to an ADC value
325 * @bgp: struct ti_bandgap pointer
326 * @adc_val: temperature value in ADC representation
327 * @hyst_val: hysteresis value in mCelsius
328 * @sum: address where to write the resulting temperature (in ADC scale)
329 *
330 * Adds an hysteresis value (in mCelsius) to a ADC temperature value.
331 *
332 * Return: 0 on success, -ERANGE otherwise.
333 */
334static
335int ti_bandgap_add_hyst(struct ti_bandgap *bgp, int adc_val, int hyst_val,
336 u32 *sum)
337{
338 int temp, ret;
339
340 /*
341 * Need to add in the mcelsius domain, so we have a temperature
342 * the conv_table range
343 */
344 ret = ti_bandgap_adc_to_mcelsius(bgp, adc_val, &temp);
345 if (ret < 0)
346 goto exit;
347
348 temp += hyst_val;
349
350 ret = ti_bandgap_mcelsius_to_adc(bgp, temp, sum);
351
352exit:
353 return ret;
354}
355
356/*** Helper functions handling device Alert/Shutdown signals ***/
357
358/**
359 * ti_bandgap_unmask_interrupts() - unmasks the events of thot & tcold
360 * @bgp: struct ti_bandgap pointer
361 * @id: bandgap sensor id
362 * @t_hot: hot temperature value to trigger alert signal
363 * @t_cold: cold temperature value to trigger alert signal
364 *
365 * Checks the requested t_hot and t_cold values and configures the IRQ event
366 * masks accordingly. Call this function only if bandgap features HAS(TALERT).
367 */
368static void ti_bandgap_unmask_interrupts(struct ti_bandgap *bgp, int id,
369 u32 t_hot, u32 t_cold)
370{
371 struct temp_sensor_registers *tsr;
372 u32 temp, reg_val;
373
374 /* Read the current on die temperature */
375 temp = ti_bandgap_read_temp(bgp, id);
376
377 tsr = bgp->conf->sensors[id].registers;
378 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
379
380 if (temp < t_hot)
381 reg_val |= tsr->mask_hot_mask;
382 else
383 reg_val &= ~tsr->mask_hot_mask;
384
385 if (t_cold < temp)
386 reg_val |= tsr->mask_cold_mask;
387 else
388 reg_val &= ~tsr->mask_cold_mask;
389 ti_bandgap_writel(bgp, reg_val, tsr->bgap_mask_ctrl);
390}
391
392/**
393 * ti_bandgap_update_alert_threshold() - sequence to update thresholds
394 * @bgp: struct ti_bandgap pointer
395 * @id: bandgap sensor id
396 * @val: value (ADC) of a new threshold
397 * @hot: desired threshold to be updated. true if threshold hot, false if
398 * threshold cold
399 *
400 * It will program the required thresholds (hot and cold) for TALERT signal.
401 * This function can be used to update t_hot or t_cold, depending on @hot value.
402 * It checks the resulting t_hot and t_cold values, based on the new passed @val
403 * and configures the thresholds so that t_hot is always greater than t_cold.
404 * Call this function only if bandgap features HAS(TALERT).
405 *
406 * Return: 0 if no error, else corresponding error
407 */
408static int ti_bandgap_update_alert_threshold(struct ti_bandgap *bgp, int id,
409 int val, bool hot)
410{
411 struct temp_sensor_data *ts_data = bgp->conf->sensors[id].ts_data;
412 struct temp_sensor_registers *tsr;
413 u32 thresh_val, reg_val, t_hot, t_cold;
414 int err = 0;
415
416 tsr = bgp->conf->sensors[id].registers;
417
418 /* obtain the current value */
419 thresh_val = ti_bandgap_readl(bgp, tsr->bgap_threshold);
420 t_cold = (thresh_val & tsr->threshold_tcold_mask) >>
421 __ffs(tsr->threshold_tcold_mask);
422 t_hot = (thresh_val & tsr->threshold_thot_mask) >>
423 __ffs(tsr->threshold_thot_mask);
424 if (hot)
425 t_hot = val;
426 else
427 t_cold = val;
428
429 if (t_cold > t_hot) {
430 if (hot)
431 err = ti_bandgap_add_hyst(bgp, t_hot,
432 -ts_data->hyst_val,
433 &t_cold);
434 else
435 err = ti_bandgap_add_hyst(bgp, t_cold,
436 ts_data->hyst_val,
437 &t_hot);
438 }
439
440 /* write the new threshold values */
441 reg_val = thresh_val &
442 ~(tsr->threshold_thot_mask | tsr->threshold_tcold_mask);
443 reg_val |= (t_hot << __ffs(tsr->threshold_thot_mask)) |
444 (t_cold << __ffs(tsr->threshold_tcold_mask));
445 ti_bandgap_writel(bgp, reg_val, tsr->bgap_threshold);
446
447 if (err) {
448 dev_err(bgp->dev, "failed to reprogram thot threshold\n");
449 err = -EIO;
450 goto exit;
451 }
452
453 ti_bandgap_unmask_interrupts(bgp, id, t_hot, t_cold);
454exit:
455 return err;
456}
457
458/**
459 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
460 * @bgp: struct ti_bandgap pointer
461 * @id: bandgap sensor id
462 *
463 * Checks if the bandgap pointer is valid and if the sensor id is also
464 * applicable.
465 *
466 * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
467 * @id cannot index @bgp sensors.
468 */
469static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
470{
471 int ret = 0;
472
473 if (!bgp || IS_ERR(bgp)) {
474 pr_err("%s: invalid bandgap pointer\n", __func__);
475 ret = -EINVAL;
476 goto exit;
477 }
478
479 if ((id < 0) || (id >= bgp->conf->sensor_count)) {
480 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
481 __func__, id);
482 ret = -ERANGE;
483 }
484
485exit:
486 return ret;
487}
488
489/**
490 * _ti_bandgap_write_threshold() - helper to update TALERT t_cold or t_hot
491 * @bgp: struct ti_bandgap pointer
492 * @id: bandgap sensor id
493 * @val: value (mCelsius) of a new threshold
494 * @hot: desired threshold to be updated. true if threshold hot, false if
495 * threshold cold
496 *
497 * It will update the required thresholds (hot and cold) for TALERT signal.
498 * This function can be used to update t_hot or t_cold, depending on @hot value.
499 * Validates the mCelsius range and update the requested threshold.
500 * Call this function only if bandgap features HAS(TALERT).
501 *
502 * Return: 0 if no error, else corresponding error value.
503 */
504static int _ti_bandgap_write_threshold(struct ti_bandgap *bgp, int id, int val,
505 bool hot)
506{
507 struct temp_sensor_data *ts_data;
508 struct temp_sensor_registers *tsr;
509 u32 adc_val;
510 int ret;
511
512 ret = ti_bandgap_validate(bgp, id);
513 if (ret)
514 goto exit;
515
516 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
517 ret = -ENOTSUPP;
518 goto exit;
519 }
520
521 ts_data = bgp->conf->sensors[id].ts_data;
522 tsr = bgp->conf->sensors[id].registers;
523 if (hot) {
524 if (val < ts_data->min_temp + ts_data->hyst_val)
525 ret = -EINVAL;
526 } else {
527 if (val > ts_data->max_temp + ts_data->hyst_val)
528 ret = -EINVAL;
529 }
530
531 if (ret)
532 goto exit;
533
534 ret = ti_bandgap_mcelsius_to_adc(bgp, val, &adc_val);
535 if (ret < 0)
536 goto exit;
537
538 spin_lock(&bgp->lock);
539 ret = ti_bandgap_update_alert_threshold(bgp, id, adc_val, hot);
540 spin_unlock(&bgp->lock);
541
542exit:
543 return ret;
544}
545
546/**
547 * _ti_bandgap_read_threshold() - helper to read TALERT t_cold or t_hot
548 * @bgp: struct ti_bandgap pointer
549 * @id: bandgap sensor id
550 * @val: value (mCelsius) of a threshold
551 * @hot: desired threshold to be read. true if threshold hot, false if
552 * threshold cold
553 *
554 * It will fetch the required thresholds (hot and cold) for TALERT signal.
555 * This function can be used to read t_hot or t_cold, depending on @hot value.
556 * Call this function only if bandgap features HAS(TALERT).
557 *
558 * Return: 0 if no error, -ENOTSUPP if it has no TALERT support, or the
559 * corresponding error value if some operation fails.
560 */
561static int _ti_bandgap_read_threshold(struct ti_bandgap *bgp, int id,
562 int *val, bool hot)
563{
564 struct temp_sensor_registers *tsr;
565 u32 temp, mask;
566 int ret = 0;
567
568 ret = ti_bandgap_validate(bgp, id);
569 if (ret)
570 goto exit;
571
572 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
573 ret = -ENOTSUPP;
574 goto exit;
575 }
576
577 tsr = bgp->conf->sensors[id].registers;
578 if (hot)
579 mask = tsr->threshold_thot_mask;
580 else
581 mask = tsr->threshold_tcold_mask;
582
583 temp = ti_bandgap_readl(bgp, tsr->bgap_threshold);
584 temp = (temp & mask) >> __ffs(mask);
585 ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
586 if (ret) {
587 dev_err(bgp->dev, "failed to read thot\n");
588 ret = -EIO;
589 goto exit;
590 }
591
592 *val = temp;
593
594exit:
595 return ret;
596}
597
598/*** Exposed APIs ***/
599
600/**
601 * ti_bandgap_read_thot() - reads sensor current thot
602 * @bgp: pointer to bandgap instance
603 * @id: sensor id
604 * @thot: resulting current thot value
605 *
606 * Return: 0 on success or the proper error code
607 */
608int ti_bandgap_read_thot(struct ti_bandgap *bgp, int id, int *thot)
609{
610 return _ti_bandgap_read_threshold(bgp, id, thot, true);
611}
612
613/**
614 * ti_bandgap_write_thot() - sets sensor current thot
615 * @bgp: pointer to bandgap instance
616 * @id: sensor id
617 * @val: desired thot value
618 *
619 * Return: 0 on success or the proper error code
620 */
621int ti_bandgap_write_thot(struct ti_bandgap *bgp, int id, int val)
622{
623 return _ti_bandgap_write_threshold(bgp, id, val, true);
624}
625
626/**
627 * ti_bandgap_read_tcold() - reads sensor current tcold
628 * @bgp: pointer to bandgap instance
629 * @id: sensor id
630 * @tcold: resulting current tcold value
631 *
632 * Return: 0 on success or the proper error code
633 */
634int ti_bandgap_read_tcold(struct ti_bandgap *bgp, int id, int *tcold)
635{
636 return _ti_bandgap_read_threshold(bgp, id, tcold, false);
637}
638
639/**
640 * ti_bandgap_write_tcold() - sets the sensor tcold
641 * @bgp: pointer to bandgap instance
642 * @id: sensor id
643 * @val: desired tcold value
644 *
645 * Return: 0 on success or the proper error code
646 */
647int ti_bandgap_write_tcold(struct ti_bandgap *bgp, int id, int val)
648{
649 return _ti_bandgap_write_threshold(bgp, id, val, false);
650}
651
652/**
653 * ti_bandgap_read_counter() - read the sensor counter
654 * @bgp: pointer to bandgap instance
655 * @id: sensor id
656 * @interval: resulting update interval in miliseconds
657 */
658static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id,
659 int *interval)
660{
661 struct temp_sensor_registers *tsr;
662 int time;
663
664 tsr = bgp->conf->sensors[id].registers;
665 time = ti_bandgap_readl(bgp, tsr->bgap_counter);
666 time = (time & tsr->counter_mask) >>
667 __ffs(tsr->counter_mask);
668 time = time * 1000 / bgp->clk_rate;
669 *interval = time;
670}
671
672/**
673 * ti_bandgap_read_counter_delay() - read the sensor counter delay
674 * @bgp: pointer to bandgap instance
675 * @id: sensor id
676 * @interval: resulting update interval in miliseconds
677 */
678static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id,
679 int *interval)
680{
681 struct temp_sensor_registers *tsr;
682 int reg_val;
683
684 tsr = bgp->conf->sensors[id].registers;
685
686 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
687 reg_val = (reg_val & tsr->mask_counter_delay_mask) >>
688 __ffs(tsr->mask_counter_delay_mask);
689 switch (reg_val) {
690 case 0:
691 *interval = 0;
692 break;
693 case 1:
694 *interval = 1;
695 break;
696 case 2:
697 *interval = 10;
698 break;
699 case 3:
700 *interval = 100;
701 break;
702 case 4:
703 *interval = 250;
704 break;
705 case 5:
706 *interval = 500;
707 break;
708 default:
709 dev_warn(bgp->dev, "Wrong counter delay value read from register %X",
710 reg_val);
711 }
712}
713
714/**
715 * ti_bandgap_read_update_interval() - read the sensor update interval
716 * @bgp: pointer to bandgap instance
717 * @id: sensor id
718 * @interval: resulting update interval in miliseconds
719 *
720 * Return: 0 on success or the proper error code
721 */
722int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
723 int *interval)
724{
725 int ret = 0;
726
727 ret = ti_bandgap_validate(bgp, id);
728 if (ret)
729 goto exit;
730
731 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
732 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
733 ret = -ENOTSUPP;
734 goto exit;
735 }
736
737 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
738 ti_bandgap_read_counter(bgp, id, interval);
739 goto exit;
740 }
741
742 ti_bandgap_read_counter_delay(bgp, id, interval);
743exit:
744 return ret;
745}
746
747/**
748 * ti_bandgap_write_counter_delay() - set the counter_delay
749 * @bgp: pointer to bandgap instance
750 * @id: sensor id
751 * @interval: desired update interval in miliseconds
752 *
753 * Return: 0 on success or the proper error code
754 */
755static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id,
756 u32 interval)
757{
758 int rval;
759
760 switch (interval) {
761 case 0: /* Immediate conversion */
762 rval = 0x0;
763 break;
764 case 1: /* Conversion after ever 1ms */
765 rval = 0x1;
766 break;
767 case 10: /* Conversion after ever 10ms */
768 rval = 0x2;
769 break;
770 case 100: /* Conversion after ever 100ms */
771 rval = 0x3;
772 break;
773 case 250: /* Conversion after ever 250ms */
774 rval = 0x4;
775 break;
776 case 500: /* Conversion after ever 500ms */
777 rval = 0x5;
778 break;
779 default:
780 dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval);
781 return -EINVAL;
782 }
783
784 spin_lock(&bgp->lock);
785 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval);
786 spin_unlock(&bgp->lock);
787
788 return 0;
789}
790
791/**
792 * ti_bandgap_write_counter() - set the bandgap sensor counter
793 * @bgp: pointer to bandgap instance
794 * @id: sensor id
795 * @interval: desired update interval in miliseconds
796 */
797static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id,
798 u32 interval)
799{
800 interval = interval * bgp->clk_rate / 1000;
801 spin_lock(&bgp->lock);
802 RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
803 spin_unlock(&bgp->lock);
804}
805
806/**
807 * ti_bandgap_write_update_interval() - set the update interval
808 * @bgp: pointer to bandgap instance
809 * @id: sensor id
810 * @interval: desired update interval in miliseconds
811 *
812 * Return: 0 on success or the proper error code
813 */
814int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
815 int id, u32 interval)
816{
817 int ret = ti_bandgap_validate(bgp, id);
818 if (ret)
819 goto exit;
820
821 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
822 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
823 ret = -ENOTSUPP;
824 goto exit;
825 }
826
827 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
828 ti_bandgap_write_counter(bgp, id, interval);
829 goto exit;
830 }
831
832 ret = ti_bandgap_write_counter_delay(bgp, id, interval);
833exit:
834 return ret;
835}
836
837/**
838 * ti_bandgap_read_temperature() - report current temperature
839 * @bgp: pointer to bandgap instance
840 * @id: sensor id
841 * @temperature: resulting temperature
842 *
843 * Return: 0 on success or the proper error code
844 */
845int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
846 int *temperature)
847{
848 u32 temp;
849 int ret;
850
851 ret = ti_bandgap_validate(bgp, id);
852 if (ret)
853 return ret;
854
855 spin_lock(&bgp->lock);
856 temp = ti_bandgap_read_temp(bgp, id);
857 spin_unlock(&bgp->lock);
858
859 ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
860 if (ret)
861 return -EIO;
862
863 *temperature = temp;
864
865 return 0;
866}
867
868/**
869 * ti_bandgap_set_sensor_data() - helper function to store thermal
870 * framework related data.
871 * @bgp: pointer to bandgap instance
872 * @id: sensor id
873 * @data: thermal framework related data to be stored
874 *
875 * Return: 0 on success or the proper error code
876 */
877int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
878{
879 int ret = ti_bandgap_validate(bgp, id);
880 if (ret)
881 return ret;
882
883 bgp->regval[id].data = data;
884
885 return 0;
886}
887
888/**
889 * ti_bandgap_get_sensor_data() - helper function to get thermal
890 * framework related data.
891 * @bgp: pointer to bandgap instance
892 * @id: sensor id
893 *
894 * Return: data stored by set function with sensor id on success or NULL
895 */
896void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
897{
898 int ret = ti_bandgap_validate(bgp, id);
899 if (ret)
900 return ERR_PTR(ret);
901
902 return bgp->regval[id].data;
903}
904
905/*** Helper functions used during device initialization ***/
906
907/**
908 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
909 * @bgp: pointer to struct ti_bandgap
910 * @id: sensor id which it is desired to read 1 temperature
911 *
912 * Used to initialize the conversion state machine and set it to a valid
913 * state. Called during device initialization and context restore events.
914 *
915 * Return: 0
916 */
917static int
918ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
919{
920 u32 temp = 0, counter = 1000;
921
922 /* Select single conversion mode */
923 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
924 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
925
926 /* Start of Conversion = 1 */
927 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
928 /* Wait until DTEMP is updated */
929 temp = ti_bandgap_read_temp(bgp, id);
930
931 while ((temp == 0) && --counter)
932 temp = ti_bandgap_read_temp(bgp, id);
933 /* REVISIT: Check correct condition for end of conversion */
934
935 /* Start of Conversion = 0 */
936 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
937
938 return 0;
939}
940
941/**
942 * ti_bandgap_set_continous_mode() - One time enabling of continuous mode
943 * @bgp: pointer to struct ti_bandgap
944 *
945 * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
946 * be used for junction temperature monitoring, it is desirable that the
947 * sensors are operational all the time, so that alerts are generated
948 * properly.
949 *
950 * Return: 0
951 */
952static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
953{
954 int i;
955
956 for (i = 0; i < bgp->conf->sensor_count; i++) {
957 /* Perform a single read just before enabling continuous */
958 ti_bandgap_force_single_read(bgp, i);
959 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
960 }
961
962 return 0;
963}
964
965/**
966 * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
967 * @bgp: pointer to struct ti_bandgap
968 * @id: id of the individual sensor
969 * @trend: Pointer to trend.
970 *
971 * This function needs to be called to fetch the temperature trend of a
972 * Particular sensor. The function computes the difference in temperature
973 * w.r.t time. For the bandgaps with built in history buffer the temperatures
974 * are read from the buffer and for those without the Buffer -ENOTSUPP is
975 * returned.
976 *
977 * Return: 0 if no error, else return corresponding error. If no
978 * error then the trend value is passed on to trend parameter
979 */
980int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend)
981{
982 struct temp_sensor_registers *tsr;
983 u32 temp1, temp2, reg1, reg2;
984 int t1, t2, interval, ret = 0;
985
986 ret = ti_bandgap_validate(bgp, id);
987 if (ret)
988 goto exit;
989
990 if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) ||
991 !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
992 ret = -ENOTSUPP;
993 goto exit;
994 }
995
996 spin_lock(&bgp->lock);
997
998 tsr = bgp->conf->sensors[id].registers;
999
1000 /* Freeze and read the last 2 valid readings */
1001 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
1002 reg1 = tsr->ctrl_dtemp_1;
1003 reg2 = tsr->ctrl_dtemp_2;
1004
1005 /* read temperature from history buffer */
1006 temp1 = ti_bandgap_readl(bgp, reg1);
1007 temp1 &= tsr->bgap_dtemp_mask;
1008
1009 temp2 = ti_bandgap_readl(bgp, reg2);
1010 temp2 &= tsr->bgap_dtemp_mask;
1011
1012 /* Convert from adc values to mCelsius temperature */
1013 ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1);
1014 if (ret)
1015 goto unfreeze;
1016
1017 ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2);
1018 if (ret)
1019 goto unfreeze;
1020
1021 /* Fetch the update interval */
1022 ret = ti_bandgap_read_update_interval(bgp, id, &interval);
1023 if (ret)
1024 goto unfreeze;
1025
1026 /* Set the interval to 1 ms if bandgap counter delay is not set */
1027 if (interval == 0)
1028 interval = 1;
1029
1030 *trend = (t1 - t2) / interval;
1031
1032 dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
1033 t1, t2, *trend);
1034
1035unfreeze:
1036 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
1037 spin_unlock(&bgp->lock);
1038exit:
1039 return ret;
1040}
1041
1042/**
1043 * ti_bandgap_tshut_init() - setup and initialize tshut handling
1044 * @bgp: pointer to struct ti_bandgap
1045 * @pdev: pointer to device struct platform_device
1046 *
1047 * Call this function only in case the bandgap features HAS(TSHUT).
1048 * In this case, the driver needs to handle the TSHUT signal as an IRQ.
1049 * The IRQ is wired as a GPIO, and for this purpose, it is required
1050 * to specify which GPIO line is used. TSHUT IRQ is fired anytime
1051 * one of the bandgap sensors violates the TSHUT high/hot threshold.
1052 * And in that case, the system must go off.
1053 *
1054 * Return: 0 if no error, else error status
1055 */
1056static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
1057 struct platform_device *pdev)
1058{
1059 int gpio_nr = bgp->tshut_gpio;
1060 int status;
1061
1062 /* Request for gpio_86 line */
1063 status = gpio_request(gpio_nr, "tshut");
1064 if (status < 0) {
1065 dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86);
1066 return status;
1067 }
1068 status = gpio_direction_input(gpio_nr);
1069 if (status) {
1070 dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr);
1071 return status;
1072 }
1073
1074 status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler,
1075 IRQF_TRIGGER_RISING, "tshut", NULL);
1076 if (status) {
1077 gpio_free(gpio_nr);
1078 dev_err(bgp->dev, "request irq failed for TSHUT");
1079 }
1080
1081 return 0;
1082}
1083
1084/**
1085 * ti_bandgap_alert_init() - setup and initialize talert handling
1086 * @bgp: pointer to struct ti_bandgap
1087 * @pdev: pointer to device struct platform_device
1088 *
1089 * Call this function only in case the bandgap features HAS(TALERT).
1090 * In this case, the driver needs to handle the TALERT signals as an IRQs.
1091 * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
1092 * are violated. In these situation, the driver must reprogram the thresholds,
1093 * accordingly to specified policy.
1094 *
1095 * Return: 0 if no error, else return corresponding error.
1096 */
1097static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
1098 struct platform_device *pdev)
1099{
1100 int ret;
1101
1102 bgp->irq = platform_get_irq(pdev, 0);
1103 if (bgp->irq < 0) {
1104 dev_err(&pdev->dev, "get_irq failed\n");
1105 return bgp->irq;
1106 }
1107 ret = request_threaded_irq(bgp->irq, NULL,
1108 ti_bandgap_talert_irq_handler,
1109 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
1110 "talert", bgp);
1111 if (ret) {
1112 dev_err(&pdev->dev, "Request threaded irq failed.\n");
1113 return ret;
1114 }
1115
1116 return 0;
1117}
1118
1119static const struct of_device_id of_ti_bandgap_match[];
1120/**
1121 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
1122 * @pdev: pointer to device struct platform_device
1123 *
1124 * Used to read the device tree properties accordingly to the bandgap
1125 * matching version. Based on bandgap version and its capabilities it
1126 * will build a struct ti_bandgap out of the required DT entries.
1127 *
1128 * Return: valid bandgap structure if successful, else returns ERR_PTR
1129 * return value must be verified with IS_ERR.
1130 */
1131static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
1132{
1133 struct device_node *node = pdev->dev.of_node;
1134 const struct of_device_id *of_id;
1135 struct ti_bandgap *bgp;
1136 struct resource *res;
1137 int i;
1138
1139 /* just for the sake */
1140 if (!node) {
1141 dev_err(&pdev->dev, "no platform information available\n");
1142 return ERR_PTR(-EINVAL);
1143 }
1144
1145 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
1146 if (!bgp) {
1147 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
1148 return ERR_PTR(-ENOMEM);
1149 }
1150
1151 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
1152 if (of_id)
1153 bgp->conf = of_id->data;
1154
1155 /* register shadow for context save and restore */
1156 bgp->regval = devm_kzalloc(&pdev->dev, sizeof(*bgp->regval) *
1157 bgp->conf->sensor_count, GFP_KERNEL);
1158 if (!bgp) {
1159 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
1160 return ERR_PTR(-ENOMEM);
1161 }
1162
1163 i = 0;
1164 do {
1165 void __iomem *chunk;
1166
1167 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
1168 if (!res)
1169 break;
1170 chunk = devm_ioremap_resource(&pdev->dev, res);
1171 if (i == 0)
1172 bgp->base = chunk;
1173 if (IS_ERR(chunk))
1174 return ERR_CAST(chunk);
1175
1176 i++;
1177 } while (res);
1178
1179 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1180 bgp->tshut_gpio = of_get_gpio(node, 0);
1181 if (!gpio_is_valid(bgp->tshut_gpio)) {
1182 dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
1183 bgp->tshut_gpio);
1184 return ERR_PTR(-EINVAL);
1185 }
1186 }
1187
1188 return bgp;
1189}
1190
1191/*** Device driver call backs ***/
1192
1193static
1194int ti_bandgap_probe(struct platform_device *pdev)
1195{
1196 struct ti_bandgap *bgp;
1197 int clk_rate, ret = 0, i;
1198
1199 bgp = ti_bandgap_build(pdev);
1200 if (IS_ERR(bgp)) {
1201 dev_err(&pdev->dev, "failed to fetch platform data\n");
1202 return PTR_ERR(bgp);
1203 }
1204 bgp->dev = &pdev->dev;
1205
1206 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1207 ret = ti_bandgap_tshut_init(bgp, pdev);
1208 if (ret) {
1209 dev_err(&pdev->dev,
1210 "failed to initialize system tshut IRQ\n");
1211 return ret;
1212 }
1213 }
1214
1215 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
1216 ret = IS_ERR(bgp->fclock);
1217 if (ret) {
1218 dev_err(&pdev->dev, "failed to request fclock reference\n");
1219 ret = PTR_ERR(bgp->fclock);
1220 goto free_irqs;
1221 }
1222
1223 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
1224 ret = IS_ERR(bgp->div_clk);
1225 if (ret) {
1226 dev_err(&pdev->dev,
1227 "failed to request div_ts_ck clock ref\n");
1228 ret = PTR_ERR(bgp->div_clk);
1229 goto free_irqs;
1230 }
1231
1232 for (i = 0; i < bgp->conf->sensor_count; i++) {
1233 struct temp_sensor_registers *tsr;
1234 u32 val;
1235
1236 tsr = bgp->conf->sensors[i].registers;
1237 /*
1238 * check if the efuse has a non-zero value if not
1239 * it is an untrimmed sample and the temperatures
1240 * may not be accurate
1241 */
1242 val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
1243 if (ret || !val)
1244 dev_info(&pdev->dev,
1245 "Non-trimmed BGAP, Temp not accurate\n");
1246 }
1247
1248 clk_rate = clk_round_rate(bgp->div_clk,
1249 bgp->conf->sensors[0].ts_data->max_freq);
1250 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
1251 clk_rate == 0xffffffff) {
1252 ret = -ENODEV;
1253 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
1254 goto put_clks;
1255 }
1256
1257 ret = clk_set_rate(bgp->div_clk, clk_rate);
1258 if (ret)
1259 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
1260
1261 bgp->clk_rate = clk_rate;
1262 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1263 clk_prepare_enable(bgp->fclock);
1264
1265
1266 spin_lock_init(&bgp->lock);
1267 bgp->dev = &pdev->dev;
1268 platform_set_drvdata(pdev, bgp);
1269
1270 ti_bandgap_power(bgp, true);
1271
1272 /* Set default counter to 1 for now */
1273 if (TI_BANDGAP_HAS(bgp, COUNTER))
1274 for (i = 0; i < bgp->conf->sensor_count; i++)
1275 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
1276
1277 /* Set default thresholds for alert and shutdown */
1278 for (i = 0; i < bgp->conf->sensor_count; i++) {
1279 struct temp_sensor_data *ts_data;
1280
1281 ts_data = bgp->conf->sensors[i].ts_data;
1282
1283 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1284 /* Set initial Talert thresholds */
1285 RMW_BITS(bgp, i, bgap_threshold,
1286 threshold_tcold_mask, ts_data->t_cold);
1287 RMW_BITS(bgp, i, bgap_threshold,
1288 threshold_thot_mask, ts_data->t_hot);
1289 /* Enable the alert events */
1290 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
1291 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
1292 }
1293
1294 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
1295 /* Set initial Tshut thresholds */
1296 RMW_BITS(bgp, i, tshut_threshold,
1297 tshut_hot_mask, ts_data->tshut_hot);
1298 RMW_BITS(bgp, i, tshut_threshold,
1299 tshut_cold_mask, ts_data->tshut_cold);
1300 }
1301 }
1302
1303 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1304 ti_bandgap_set_continuous_mode(bgp);
1305
1306 /* Set .250 seconds time as default counter */
1307 if (TI_BANDGAP_HAS(bgp, COUNTER))
1308 for (i = 0; i < bgp->conf->sensor_count; i++)
1309 RMW_BITS(bgp, i, bgap_counter, counter_mask,
1310 bgp->clk_rate / 4);
1311
1312 /* Every thing is good? Then expose the sensors */
1313 for (i = 0; i < bgp->conf->sensor_count; i++) {
1314 char *domain;
1315
1316 if (bgp->conf->sensors[i].register_cooling) {
1317 ret = bgp->conf->sensors[i].register_cooling(bgp, i);
1318 if (ret)
1319 goto remove_sensors;
1320 }
1321
1322 if (bgp->conf->expose_sensor) {
1323 domain = bgp->conf->sensors[i].domain;
1324 ret = bgp->conf->expose_sensor(bgp, i, domain);
1325 if (ret)
1326 goto remove_last_cooling;
1327 }
1328 }
1329
1330 /*
1331 * Enable the Interrupts once everything is set. Otherwise irq handler
1332 * might be called as soon as it is enabled where as rest of framework
1333 * is still getting initialised.
1334 */
1335 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1336 ret = ti_bandgap_talert_init(bgp, pdev);
1337 if (ret) {
1338 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
1339 i = bgp->conf->sensor_count;
1340 goto disable_clk;
1341 }
1342 }
1343
1344 return 0;
1345
1346remove_last_cooling:
1347 if (bgp->conf->sensors[i].unregister_cooling)
1348 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1349remove_sensors:
1350 for (i--; i >= 0; i--) {
1351 if (bgp->conf->sensors[i].unregister_cooling)
1352 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1353 if (bgp->conf->remove_sensor)
1354 bgp->conf->remove_sensor(bgp, i);
1355 }
1356 ti_bandgap_power(bgp, false);
1357disable_clk:
1358 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1359 clk_disable_unprepare(bgp->fclock);
1360put_clks:
1361 clk_put(bgp->fclock);
1362 clk_put(bgp->div_clk);
1363free_irqs:
1364 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1365 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1366 gpio_free(bgp->tshut_gpio);
1367 }
1368
1369 return ret;
1370}
1371
1372static
1373int ti_bandgap_remove(struct platform_device *pdev)
1374{
1375 struct ti_bandgap *bgp = platform_get_drvdata(pdev);
1376 int i;
1377
1378 /* First thing is to remove sensor interfaces */
1379 for (i = 0; i < bgp->conf->sensor_count; i++) {
1380 if (bgp->conf->sensors[i].unregister_cooling)
1381 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1382
1383 if (bgp->conf->remove_sensor)
1384 bgp->conf->remove_sensor(bgp, i);
1385 }
1386
1387 ti_bandgap_power(bgp, false);
1388
1389 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1390 clk_disable_unprepare(bgp->fclock);
1391 clk_put(bgp->fclock);
1392 clk_put(bgp->div_clk);
1393
1394 if (TI_BANDGAP_HAS(bgp, TALERT))
1395 free_irq(bgp->irq, bgp);
1396
1397 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1398 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1399 gpio_free(bgp->tshut_gpio);
1400 }
1401
1402 return 0;
1403}
1404
1405#ifdef CONFIG_PM
1406static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
1407{
1408 int i;
1409
1410 for (i = 0; i < bgp->conf->sensor_count; i++) {
1411 struct temp_sensor_registers *tsr;
1412 struct temp_sensor_regval *rval;
1413
1414 rval = &bgp->regval[i];
1415 tsr = bgp->conf->sensors[i].registers;
1416
1417 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1418 rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
1419 tsr->bgap_mode_ctrl);
1420 if (TI_BANDGAP_HAS(bgp, COUNTER))
1421 rval->bg_counter = ti_bandgap_readl(bgp,
1422 tsr->bgap_counter);
1423 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1424 rval->bg_threshold = ti_bandgap_readl(bgp,
1425 tsr->bgap_threshold);
1426 rval->bg_ctrl = ti_bandgap_readl(bgp,
1427 tsr->bgap_mask_ctrl);
1428 }
1429
1430 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1431 rval->tshut_threshold = ti_bandgap_readl(bgp,
1432 tsr->tshut_threshold);
1433 }
1434
1435 return 0;
1436}
1437
1438static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
1439{
1440 int i;
1441
1442 for (i = 0; i < bgp->conf->sensor_count; i++) {
1443 struct temp_sensor_registers *tsr;
1444 struct temp_sensor_regval *rval;
1445 u32 val = 0;
1446
1447 rval = &bgp->regval[i];
1448 tsr = bgp->conf->sensors[i].registers;
1449
1450 if (TI_BANDGAP_HAS(bgp, COUNTER))
1451 val = ti_bandgap_readl(bgp, tsr->bgap_counter);
1452
1453 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1454 ti_bandgap_writel(bgp, rval->tshut_threshold,
1455 tsr->tshut_threshold);
1456 /* Force immediate temperature measurement and update
1457 * of the DTEMP field
1458 */
1459 ti_bandgap_force_single_read(bgp, i);
1460
1461 if (TI_BANDGAP_HAS(bgp, COUNTER))
1462 ti_bandgap_writel(bgp, rval->bg_counter,
1463 tsr->bgap_counter);
1464 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1465 ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1466 tsr->bgap_mode_ctrl);
1467 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1468 ti_bandgap_writel(bgp, rval->bg_threshold,
1469 tsr->bgap_threshold);
1470 ti_bandgap_writel(bgp, rval->bg_ctrl,
1471 tsr->bgap_mask_ctrl);
1472 }
1473 }
1474
1475 return 0;
1476}
1477
1478static int ti_bandgap_suspend(struct device *dev)
1479{
1480 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1481 int err;
1482
1483 err = ti_bandgap_save_ctxt(bgp);
1484 ti_bandgap_power(bgp, false);
1485
1486 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1487 clk_disable_unprepare(bgp->fclock);
1488
1489 return err;
1490}
1491
1492static int ti_bandgap_resume(struct device *dev)
1493{
1494 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1495
1496 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1497 clk_prepare_enable(bgp->fclock);
1498
1499 ti_bandgap_power(bgp, true);
1500
1501 return ti_bandgap_restore_ctxt(bgp);
1502}
1503static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend,
1504 ti_bandgap_resume);
1505
1506#define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
1507#else
1508#define DEV_PM_OPS NULL
1509#endif
1510
1511static const struct of_device_id of_ti_bandgap_match[] = {
1512#ifdef CONFIG_OMAP4_THERMAL
1513 {
1514 .compatible = "ti,omap4430-bandgap",
1515 .data = (void *)&omap4430_data,
1516 },
1517 {
1518 .compatible = "ti,omap4460-bandgap",
1519 .data = (void *)&omap4460_data,
1520 },
1521 {
1522 .compatible = "ti,omap4470-bandgap",
1523 .data = (void *)&omap4470_data,
1524 },
1525#endif
1526#ifdef CONFIG_OMAP5_THERMAL
1527 {
1528 .compatible = "ti,omap5430-bandgap",
1529 .data = (void *)&omap5430_data,
1530 },
1531#endif
1532#ifdef CONFIG_DRA752_THERMAL
1533 {
1534 .compatible = "ti,dra752-bandgap",
1535 .data = (void *)&dra752_data,
1536 },
1537#endif
1538 /* Sentinel */
1539 { },
1540};
1541MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
1542
1543static struct platform_driver ti_bandgap_sensor_driver = {
1544 .probe = ti_bandgap_probe,
1545 .remove = ti_bandgap_remove,
1546 .driver = {
1547 .name = "ti-soc-thermal",
1548 .pm = DEV_PM_OPS,
1549 .of_match_table = of_ti_bandgap_match,
1550 },
1551};
1552
1553module_platform_driver(ti_bandgap_sensor_driver);
1554
1555MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1556MODULE_LICENSE("GPL v2");
1557MODULE_ALIAS("platform:ti-soc-thermal");
1558MODULE_AUTHOR("Texas Instrument Inc.");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * TI Bandgap temperature sensor driver
4 *
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>
10 */
11
12#include <linux/clk.h>
13#include <linux/cpu_pm.h>
14#include <linux/device.h>
15#include <linux/err.h>
16#include <linux/export.h>
17#include <linux/gpio/consumer.h>
18#include <linux/init.h>
19#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/iopoll.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/of.h>
25#include <linux/of_device.h>
26#include <linux/of_irq.h>
27#include <linux/of_platform.h>
28#include <linux/platform_device.h>
29#include <linux/pm.h>
30#include <linux/pm_runtime.h>
31#include <linux/reboot.h>
32#include <linux/spinlock.h>
33#include <linux/sys_soc.h>
34#include <linux/types.h>
35
36#include "ti-bandgap.h"
37
38static int ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id);
39#ifdef CONFIG_PM_SLEEP
40static int bandgap_omap_cpu_notifier(struct notifier_block *nb,
41 unsigned long cmd, void *v);
42#endif
43
44/*** Helper functions to access registers and their bitfields ***/
45
46/**
47 * ti_bandgap_readl() - simple read helper function
48 * @bgp: pointer to ti_bandgap structure
49 * @reg: desired register (offset) to be read
50 *
51 * Helper function to read bandgap registers. It uses the io remapped area.
52 * Return: the register value.
53 */
54static u32 ti_bandgap_readl(struct ti_bandgap *bgp, u32 reg)
55{
56 return readl(bgp->base + reg);
57}
58
59/**
60 * ti_bandgap_writel() - simple write helper function
61 * @bgp: pointer to ti_bandgap structure
62 * @val: desired register value to be written
63 * @reg: desired register (offset) to be written
64 *
65 * Helper function to write bandgap registers. It uses the io remapped area.
66 */
67static void ti_bandgap_writel(struct ti_bandgap *bgp, u32 val, u32 reg)
68{
69 writel(val, bgp->base + reg);
70}
71
72/**
73 * DOC: macro to update bits.
74 *
75 * RMW_BITS() - used to read, modify and update bandgap bitfields.
76 * The value passed will be shifted.
77 */
78#define RMW_BITS(bgp, id, reg, mask, val) \
79do { \
80 struct temp_sensor_registers *t; \
81 u32 r; \
82 \
83 t = bgp->conf->sensors[(id)].registers; \
84 r = ti_bandgap_readl(bgp, t->reg); \
85 r &= ~t->mask; \
86 r |= (val) << __ffs(t->mask); \
87 ti_bandgap_writel(bgp, r, t->reg); \
88} while (0)
89
90/*** Basic helper functions ***/
91
92/**
93 * ti_bandgap_power() - controls the power state of a bandgap device
94 * @bgp: pointer to ti_bandgap structure
95 * @on: desired power state (1 - on, 0 - off)
96 *
97 * Used to power on/off a bandgap device instance. Only used on those
98 * that features tempsoff bit.
99 *
100 * Return: 0 on success, -ENOTSUPP if tempsoff is not supported.
101 */
102static int ti_bandgap_power(struct ti_bandgap *bgp, bool on)
103{
104 int i;
105
106 if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH))
107 return -ENOTSUPP;
108
109 for (i = 0; i < bgp->conf->sensor_count; i++)
110 /* active on 0 */
111 RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
112 return 0;
113}
114
115/**
116 * ti_errata814_bandgap_read_temp() - helper function to read dra7 sensor temperature
117 * @bgp: pointer to ti_bandgap structure
118 * @reg: desired register (offset) to be read
119 *
120 * Function to read dra7 bandgap sensor temperature. This is done separately
121 * so as to workaround the errata "Bandgap Temperature read Dtemp can be
122 * corrupted" - Errata ID: i814".
123 * Read accesses to registers listed below can be corrupted due to incorrect
124 * resynchronization between clock domains.
125 * Read access to registers below can be corrupted :
126 * CTRL_CORE_DTEMP_MPU/GPU/CORE/DSPEVE/IVA_n (n = 0 to 4)
127 * CTRL_CORE_TEMP_SENSOR_MPU/GPU/CORE/DSPEVE/IVA_n
128 *
129 * Return: the register value.
130 */
131static u32 ti_errata814_bandgap_read_temp(struct ti_bandgap *bgp, u32 reg)
132{
133 u32 val1, val2;
134
135 val1 = ti_bandgap_readl(bgp, reg);
136 val2 = ti_bandgap_readl(bgp, reg);
137
138 /* If both times we read the same value then that is right */
139 if (val1 == val2)
140 return val1;
141
142 /* if val1 and val2 are different read it third time */
143 return ti_bandgap_readl(bgp, reg);
144}
145
146/**
147 * ti_bandgap_read_temp() - helper function to read sensor temperature
148 * @bgp: pointer to ti_bandgap structure
149 * @id: bandgap sensor id
150 *
151 * Function to concentrate the steps to read sensor temperature register.
152 * This function is desired because, depending on bandgap device version,
153 * it might be needed to freeze the bandgap state machine, before fetching
154 * the register value.
155 *
156 * Return: temperature in ADC values.
157 */
158static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
159{
160 struct temp_sensor_registers *tsr;
161 u32 temp, reg;
162
163 tsr = bgp->conf->sensors[id].registers;
164 reg = tsr->temp_sensor_ctrl;
165
166 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
167 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
168 /*
169 * In case we cannot read from cur_dtemp / dtemp_0,
170 * then we read from the last valid temp read
171 */
172 reg = tsr->ctrl_dtemp_1;
173 }
174
175 /* read temperature */
176 if (TI_BANDGAP_HAS(bgp, ERRATA_814))
177 temp = ti_errata814_bandgap_read_temp(bgp, reg);
178 else
179 temp = ti_bandgap_readl(bgp, reg);
180
181 temp &= tsr->bgap_dtemp_mask;
182
183 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
184 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
185
186 return temp;
187}
188
189/*** IRQ handlers ***/
190
191/**
192 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
193 * @irq: IRQ number
194 * @data: private data (struct ti_bandgap *)
195 *
196 * This is the Talert handler. Use it only if bandgap device features
197 * HAS(TALERT). This handler goes over all sensors and checks their
198 * conditions and acts accordingly. In case there are events pending,
199 * it will reset the event mask to wait for the opposite event (next event).
200 * Every time there is a new event, it will be reported to thermal layer.
201 *
202 * Return: IRQ_HANDLED
203 */
204static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
205{
206 struct ti_bandgap *bgp = data;
207 struct temp_sensor_registers *tsr;
208 u32 t_hot = 0, t_cold = 0, ctrl;
209 int i;
210
211 spin_lock(&bgp->lock);
212 for (i = 0; i < bgp->conf->sensor_count; i++) {
213 tsr = bgp->conf->sensors[i].registers;
214 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
215
216 /* Read the status of t_hot */
217 t_hot = ctrl & tsr->status_hot_mask;
218
219 /* Read the status of t_cold */
220 t_cold = ctrl & tsr->status_cold_mask;
221
222 if (!t_cold && !t_hot)
223 continue;
224
225 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
226 /*
227 * One TALERT interrupt: Two sources
228 * If the interrupt is due to t_hot then mask t_hot and
229 * unmask t_cold else mask t_cold and unmask t_hot
230 */
231 if (t_hot) {
232 ctrl &= ~tsr->mask_hot_mask;
233 ctrl |= tsr->mask_cold_mask;
234 } else if (t_cold) {
235 ctrl &= ~tsr->mask_cold_mask;
236 ctrl |= tsr->mask_hot_mask;
237 }
238
239 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
240
241 dev_dbg(bgp->dev,
242 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
243 __func__, bgp->conf->sensors[i].domain,
244 t_hot, t_cold);
245
246 /* report temperature to whom may concern */
247 if (bgp->conf->report_temperature)
248 bgp->conf->report_temperature(bgp, i);
249 }
250 spin_unlock(&bgp->lock);
251
252 return IRQ_HANDLED;
253}
254
255/**
256 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
257 * @irq: IRQ number
258 * @data: private data (unused)
259 *
260 * This is the Tshut handler. Use it only if bandgap device features
261 * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
262 * the system.
263 *
264 * Return: IRQ_HANDLED
265 */
266static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
267{
268 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
269 __func__);
270
271 orderly_poweroff(true);
272
273 return IRQ_HANDLED;
274}
275
276/*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
277
278/**
279 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
280 * @bgp: struct ti_bandgap pointer
281 * @adc_val: value in ADC representation
282 * @t: address where to write the resulting temperature in mCelsius
283 *
284 * Simple conversion from ADC representation to mCelsius. In case the ADC value
285 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
286 * The conversion table is indexed by the ADC values.
287 *
288 * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val
289 * argument is out of the ADC conv table range.
290 */
291static
292int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
293{
294 const struct ti_bandgap_data *conf = bgp->conf;
295
296 /* look up for temperature in the table and return the temperature */
297 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val)
298 return -ERANGE;
299
300 *t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
301 return 0;
302}
303
304/**
305 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
306 * @bgp: struct ti_bandgap pointer
307 * @id: bandgap sensor id
308 *
309 * Checks if the bandgap pointer is valid and if the sensor id is also
310 * applicable.
311 *
312 * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
313 * @id cannot index @bgp sensors.
314 */
315static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
316{
317 if (IS_ERR_OR_NULL(bgp)) {
318 pr_err("%s: invalid bandgap pointer\n", __func__);
319 return -EINVAL;
320 }
321
322 if ((id < 0) || (id >= bgp->conf->sensor_count)) {
323 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
324 __func__, id);
325 return -ERANGE;
326 }
327
328 return 0;
329}
330
331/**
332 * ti_bandgap_read_counter() - read the sensor counter
333 * @bgp: pointer to bandgap instance
334 * @id: sensor id
335 * @interval: resulting update interval in miliseconds
336 */
337static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id,
338 int *interval)
339{
340 struct temp_sensor_registers *tsr;
341 int time;
342
343 tsr = bgp->conf->sensors[id].registers;
344 time = ti_bandgap_readl(bgp, tsr->bgap_counter);
345 time = (time & tsr->counter_mask) >>
346 __ffs(tsr->counter_mask);
347 time = time * 1000 / bgp->clk_rate;
348 *interval = time;
349}
350
351/**
352 * ti_bandgap_read_counter_delay() - read the sensor counter delay
353 * @bgp: pointer to bandgap instance
354 * @id: sensor id
355 * @interval: resulting update interval in miliseconds
356 */
357static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id,
358 int *interval)
359{
360 struct temp_sensor_registers *tsr;
361 int reg_val;
362
363 tsr = bgp->conf->sensors[id].registers;
364
365 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
366 reg_val = (reg_val & tsr->mask_counter_delay_mask) >>
367 __ffs(tsr->mask_counter_delay_mask);
368 switch (reg_val) {
369 case 0:
370 *interval = 0;
371 break;
372 case 1:
373 *interval = 1;
374 break;
375 case 2:
376 *interval = 10;
377 break;
378 case 3:
379 *interval = 100;
380 break;
381 case 4:
382 *interval = 250;
383 break;
384 case 5:
385 *interval = 500;
386 break;
387 default:
388 dev_warn(bgp->dev, "Wrong counter delay value read from register %X",
389 reg_val);
390 }
391}
392
393/**
394 * ti_bandgap_read_update_interval() - read the sensor update interval
395 * @bgp: pointer to bandgap instance
396 * @id: sensor id
397 * @interval: resulting update interval in miliseconds
398 *
399 * Return: 0 on success or the proper error code
400 */
401int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
402 int *interval)
403{
404 int ret = 0;
405
406 ret = ti_bandgap_validate(bgp, id);
407 if (ret)
408 goto exit;
409
410 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
411 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
412 ret = -ENOTSUPP;
413 goto exit;
414 }
415
416 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
417 ti_bandgap_read_counter(bgp, id, interval);
418 goto exit;
419 }
420
421 ti_bandgap_read_counter_delay(bgp, id, interval);
422exit:
423 return ret;
424}
425
426/**
427 * ti_bandgap_write_counter_delay() - set the counter_delay
428 * @bgp: pointer to bandgap instance
429 * @id: sensor id
430 * @interval: desired update interval in miliseconds
431 *
432 * Return: 0 on success or the proper error code
433 */
434static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id,
435 u32 interval)
436{
437 int rval;
438
439 switch (interval) {
440 case 0: /* Immediate conversion */
441 rval = 0x0;
442 break;
443 case 1: /* Conversion after ever 1ms */
444 rval = 0x1;
445 break;
446 case 10: /* Conversion after ever 10ms */
447 rval = 0x2;
448 break;
449 case 100: /* Conversion after ever 100ms */
450 rval = 0x3;
451 break;
452 case 250: /* Conversion after ever 250ms */
453 rval = 0x4;
454 break;
455 case 500: /* Conversion after ever 500ms */
456 rval = 0x5;
457 break;
458 default:
459 dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval);
460 return -EINVAL;
461 }
462
463 spin_lock(&bgp->lock);
464 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval);
465 spin_unlock(&bgp->lock);
466
467 return 0;
468}
469
470/**
471 * ti_bandgap_write_counter() - set the bandgap sensor counter
472 * @bgp: pointer to bandgap instance
473 * @id: sensor id
474 * @interval: desired update interval in miliseconds
475 */
476static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id,
477 u32 interval)
478{
479 interval = interval * bgp->clk_rate / 1000;
480 spin_lock(&bgp->lock);
481 RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
482 spin_unlock(&bgp->lock);
483}
484
485/**
486 * ti_bandgap_write_update_interval() - set the update interval
487 * @bgp: pointer to bandgap instance
488 * @id: sensor id
489 * @interval: desired update interval in miliseconds
490 *
491 * Return: 0 on success or the proper error code
492 */
493int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
494 int id, u32 interval)
495{
496 int ret = ti_bandgap_validate(bgp, id);
497 if (ret)
498 goto exit;
499
500 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
501 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
502 ret = -ENOTSUPP;
503 goto exit;
504 }
505
506 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
507 ti_bandgap_write_counter(bgp, id, interval);
508 goto exit;
509 }
510
511 ret = ti_bandgap_write_counter_delay(bgp, id, interval);
512exit:
513 return ret;
514}
515
516/**
517 * ti_bandgap_read_temperature() - report current temperature
518 * @bgp: pointer to bandgap instance
519 * @id: sensor id
520 * @temperature: resulting temperature
521 *
522 * Return: 0 on success or the proper error code
523 */
524int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
525 int *temperature)
526{
527 u32 temp;
528 int ret;
529
530 ret = ti_bandgap_validate(bgp, id);
531 if (ret)
532 return ret;
533
534 if (!TI_BANDGAP_HAS(bgp, MODE_CONFIG)) {
535 ret = ti_bandgap_force_single_read(bgp, id);
536 if (ret)
537 return ret;
538 }
539
540 spin_lock(&bgp->lock);
541 temp = ti_bandgap_read_temp(bgp, id);
542 spin_unlock(&bgp->lock);
543
544 ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
545 if (ret)
546 return -EIO;
547
548 *temperature = temp;
549
550 return 0;
551}
552
553/**
554 * ti_bandgap_set_sensor_data() - helper function to store thermal
555 * framework related data.
556 * @bgp: pointer to bandgap instance
557 * @id: sensor id
558 * @data: thermal framework related data to be stored
559 *
560 * Return: 0 on success or the proper error code
561 */
562int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
563{
564 int ret = ti_bandgap_validate(bgp, id);
565 if (ret)
566 return ret;
567
568 bgp->regval[id].data = data;
569
570 return 0;
571}
572
573/**
574 * ti_bandgap_get_sensor_data() - helper function to get thermal
575 * framework related data.
576 * @bgp: pointer to bandgap instance
577 * @id: sensor id
578 *
579 * Return: data stored by set function with sensor id on success or NULL
580 */
581void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
582{
583 int ret = ti_bandgap_validate(bgp, id);
584 if (ret)
585 return ERR_PTR(ret);
586
587 return bgp->regval[id].data;
588}
589
590/*** Helper functions used during device initialization ***/
591
592/**
593 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
594 * @bgp: pointer to struct ti_bandgap
595 * @id: sensor id which it is desired to read 1 temperature
596 *
597 * Used to initialize the conversion state machine and set it to a valid
598 * state. Called during device initialization and context restore events.
599 *
600 * Return: 0
601 */
602static int
603ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
604{
605 struct temp_sensor_registers *tsr = bgp->conf->sensors[id].registers;
606 void __iomem *temp_sensor_ctrl = bgp->base + tsr->temp_sensor_ctrl;
607 int error;
608 u32 val;
609
610 /* Select continuous or single conversion mode */
611 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG)) {
612 if (TI_BANDGAP_HAS(bgp, CONT_MODE_ONLY))
613 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 1);
614 else
615 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
616 }
617
618 /* Set Start of Conversion if available */
619 if (tsr->bgap_soc_mask) {
620 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
621
622 /* Wait for EOCZ going up */
623 error = readl_poll_timeout_atomic(temp_sensor_ctrl, val,
624 val & tsr->bgap_eocz_mask,
625 1, 1000);
626 if (error)
627 dev_warn(bgp->dev, "eocz timed out waiting high\n");
628
629 /* Clear Start of Conversion if available */
630 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
631 }
632
633 /* Wait for EOCZ going down, always needed even if no bgap_soc_mask */
634 error = readl_poll_timeout_atomic(temp_sensor_ctrl, val,
635 !(val & tsr->bgap_eocz_mask),
636 1, 1500);
637 if (error)
638 dev_warn(bgp->dev, "eocz timed out waiting low\n");
639
640 return 0;
641}
642
643/**
644 * ti_bandgap_set_continuous_mode() - One time enabling of continuous mode
645 * @bgp: pointer to struct ti_bandgap
646 *
647 * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
648 * be used for junction temperature monitoring, it is desirable that the
649 * sensors are operational all the time, so that alerts are generated
650 * properly.
651 *
652 * Return: 0
653 */
654static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
655{
656 int i;
657
658 for (i = 0; i < bgp->conf->sensor_count; i++) {
659 /* Perform a single read just before enabling continuous */
660 ti_bandgap_force_single_read(bgp, i);
661 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
662 }
663
664 return 0;
665}
666
667/**
668 * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
669 * @bgp: pointer to struct ti_bandgap
670 * @id: id of the individual sensor
671 * @trend: Pointer to trend.
672 *
673 * This function needs to be called to fetch the temperature trend of a
674 * Particular sensor. The function computes the difference in temperature
675 * w.r.t time. For the bandgaps with built in history buffer the temperatures
676 * are read from the buffer and for those without the Buffer -ENOTSUPP is
677 * returned.
678 *
679 * Return: 0 if no error, else return corresponding error. If no
680 * error then the trend value is passed on to trend parameter
681 */
682int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend)
683{
684 struct temp_sensor_registers *tsr;
685 u32 temp1, temp2, reg1, reg2;
686 int t1, t2, interval, ret = 0;
687
688 ret = ti_bandgap_validate(bgp, id);
689 if (ret)
690 goto exit;
691
692 if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) ||
693 !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
694 ret = -ENOTSUPP;
695 goto exit;
696 }
697
698 spin_lock(&bgp->lock);
699
700 tsr = bgp->conf->sensors[id].registers;
701
702 /* Freeze and read the last 2 valid readings */
703 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
704 reg1 = tsr->ctrl_dtemp_1;
705 reg2 = tsr->ctrl_dtemp_2;
706
707 /* read temperature from history buffer */
708 temp1 = ti_bandgap_readl(bgp, reg1);
709 temp1 &= tsr->bgap_dtemp_mask;
710
711 temp2 = ti_bandgap_readl(bgp, reg2);
712 temp2 &= tsr->bgap_dtemp_mask;
713
714 /* Convert from adc values to mCelsius temperature */
715 ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1);
716 if (ret)
717 goto unfreeze;
718
719 ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2);
720 if (ret)
721 goto unfreeze;
722
723 /* Fetch the update interval */
724 ret = ti_bandgap_read_update_interval(bgp, id, &interval);
725 if (ret)
726 goto unfreeze;
727
728 /* Set the interval to 1 ms if bandgap counter delay is not set */
729 if (interval == 0)
730 interval = 1;
731
732 *trend = (t1 - t2) / interval;
733
734 dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
735 t1, t2, *trend);
736
737unfreeze:
738 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
739 spin_unlock(&bgp->lock);
740exit:
741 return ret;
742}
743
744/**
745 * ti_bandgap_tshut_init() - setup and initialize tshut handling
746 * @bgp: pointer to struct ti_bandgap
747 * @pdev: pointer to device struct platform_device
748 *
749 * Call this function only in case the bandgap features HAS(TSHUT).
750 * In this case, the driver needs to handle the TSHUT signal as an IRQ.
751 * The IRQ is wired as a GPIO, and for this purpose, it is required
752 * to specify which GPIO line is used. TSHUT IRQ is fired anytime
753 * one of the bandgap sensors violates the TSHUT high/hot threshold.
754 * And in that case, the system must go off.
755 *
756 * Return: 0 if no error, else error status
757 */
758static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
759 struct platform_device *pdev)
760{
761 int status;
762
763 status = request_irq(gpiod_to_irq(bgp->tshut_gpiod),
764 ti_bandgap_tshut_irq_handler,
765 IRQF_TRIGGER_RISING, "tshut", NULL);
766 if (status)
767 dev_err(bgp->dev, "request irq failed for TSHUT");
768
769 return 0;
770}
771
772/**
773 * ti_bandgap_talert_init() - setup and initialize talert handling
774 * @bgp: pointer to struct ti_bandgap
775 * @pdev: pointer to device struct platform_device
776 *
777 * Call this function only in case the bandgap features HAS(TALERT).
778 * In this case, the driver needs to handle the TALERT signals as an IRQs.
779 * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
780 * are violated. In these situation, the driver must reprogram the thresholds,
781 * accordingly to specified policy.
782 *
783 * Return: 0 if no error, else return corresponding error.
784 */
785static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
786 struct platform_device *pdev)
787{
788 int ret;
789
790 bgp->irq = platform_get_irq(pdev, 0);
791 if (bgp->irq < 0)
792 return bgp->irq;
793
794 ret = request_threaded_irq(bgp->irq, NULL,
795 ti_bandgap_talert_irq_handler,
796 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
797 "talert", bgp);
798 if (ret) {
799 dev_err(&pdev->dev, "Request threaded irq failed.\n");
800 return ret;
801 }
802
803 return 0;
804}
805
806static const struct of_device_id of_ti_bandgap_match[];
807/**
808 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
809 * @pdev: pointer to device struct platform_device
810 *
811 * Used to read the device tree properties accordingly to the bandgap
812 * matching version. Based on bandgap version and its capabilities it
813 * will build a struct ti_bandgap out of the required DT entries.
814 *
815 * Return: valid bandgap structure if successful, else returns ERR_PTR
816 * return value must be verified with IS_ERR.
817 */
818static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
819{
820 struct device_node *node = pdev->dev.of_node;
821 const struct of_device_id *of_id;
822 struct ti_bandgap *bgp;
823 struct resource *res;
824 int i;
825
826 /* just for the sake */
827 if (!node) {
828 dev_err(&pdev->dev, "no platform information available\n");
829 return ERR_PTR(-EINVAL);
830 }
831
832 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
833 if (!bgp)
834 return ERR_PTR(-ENOMEM);
835
836 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
837 if (of_id)
838 bgp->conf = of_id->data;
839
840 /* register shadow for context save and restore */
841 bgp->regval = devm_kcalloc(&pdev->dev, bgp->conf->sensor_count,
842 sizeof(*bgp->regval), GFP_KERNEL);
843 if (!bgp->regval)
844 return ERR_PTR(-ENOMEM);
845
846 i = 0;
847 do {
848 void __iomem *chunk;
849
850 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
851 if (!res)
852 break;
853 chunk = devm_ioremap_resource(&pdev->dev, res);
854 if (i == 0)
855 bgp->base = chunk;
856 if (IS_ERR(chunk))
857 return ERR_CAST(chunk);
858
859 i++;
860 } while (res);
861
862 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
863 bgp->tshut_gpiod = devm_gpiod_get(&pdev->dev, NULL, GPIOD_IN);
864 if (IS_ERR(bgp->tshut_gpiod)) {
865 dev_err(&pdev->dev, "invalid gpio for tshut\n");
866 return ERR_CAST(bgp->tshut_gpiod);
867 }
868 }
869
870 return bgp;
871}
872
873/*
874 * List of SoCs on which the CPU PM notifier can cause erros on the DTEMP
875 * readout.
876 * Enabled notifier on these machines results in erroneous, random values which
877 * could trigger unexpected thermal shutdown.
878 */
879static const struct soc_device_attribute soc_no_cpu_notifier[] = {
880 { .machine = "OMAP4430" },
881 { /* sentinel */ }
882};
883
884/*** Device driver call backs ***/
885
886static
887int ti_bandgap_probe(struct platform_device *pdev)
888{
889 struct ti_bandgap *bgp;
890 int clk_rate, ret, i;
891
892 bgp = ti_bandgap_build(pdev);
893 if (IS_ERR(bgp)) {
894 dev_err(&pdev->dev, "failed to fetch platform data\n");
895 return PTR_ERR(bgp);
896 }
897 bgp->dev = &pdev->dev;
898
899 if (TI_BANDGAP_HAS(bgp, UNRELIABLE))
900 dev_warn(&pdev->dev,
901 "This OMAP thermal sensor is unreliable. You've been warned\n");
902
903 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
904 ret = ti_bandgap_tshut_init(bgp, pdev);
905 if (ret) {
906 dev_err(&pdev->dev,
907 "failed to initialize system tshut IRQ\n");
908 return ret;
909 }
910 }
911
912 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
913 if (IS_ERR(bgp->fclock)) {
914 dev_err(&pdev->dev, "failed to request fclock reference\n");
915 ret = PTR_ERR(bgp->fclock);
916 goto free_irqs;
917 }
918
919 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
920 if (IS_ERR(bgp->div_clk)) {
921 dev_err(&pdev->dev, "failed to request div_ts_ck clock ref\n");
922 ret = PTR_ERR(bgp->div_clk);
923 goto put_fclock;
924 }
925
926 for (i = 0; i < bgp->conf->sensor_count; i++) {
927 struct temp_sensor_registers *tsr;
928 u32 val;
929
930 tsr = bgp->conf->sensors[i].registers;
931 /*
932 * check if the efuse has a non-zero value if not
933 * it is an untrimmed sample and the temperatures
934 * may not be accurate
935 */
936 val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
937 if (!val)
938 dev_info(&pdev->dev,
939 "Non-trimmed BGAP, Temp not accurate\n");
940 }
941
942 clk_rate = clk_round_rate(bgp->div_clk,
943 bgp->conf->sensors[0].ts_data->max_freq);
944 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
945 clk_rate <= 0) {
946 ret = -ENODEV;
947 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
948 goto put_clks;
949 }
950
951 ret = clk_set_rate(bgp->div_clk, clk_rate);
952 if (ret)
953 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
954
955 bgp->clk_rate = clk_rate;
956 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
957 clk_prepare_enable(bgp->fclock);
958
959
960 spin_lock_init(&bgp->lock);
961 bgp->dev = &pdev->dev;
962 platform_set_drvdata(pdev, bgp);
963
964 ti_bandgap_power(bgp, true);
965
966 /* Set default counter to 1 for now */
967 if (TI_BANDGAP_HAS(bgp, COUNTER))
968 for (i = 0; i < bgp->conf->sensor_count; i++)
969 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
970
971 /* Set default thresholds for alert and shutdown */
972 for (i = 0; i < bgp->conf->sensor_count; i++) {
973 struct temp_sensor_data *ts_data;
974
975 ts_data = bgp->conf->sensors[i].ts_data;
976
977 if (TI_BANDGAP_HAS(bgp, TALERT)) {
978 /* Set initial Talert thresholds */
979 RMW_BITS(bgp, i, bgap_threshold,
980 threshold_tcold_mask, ts_data->t_cold);
981 RMW_BITS(bgp, i, bgap_threshold,
982 threshold_thot_mask, ts_data->t_hot);
983 /* Enable the alert events */
984 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
985 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
986 }
987
988 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
989 /* Set initial Tshut thresholds */
990 RMW_BITS(bgp, i, tshut_threshold,
991 tshut_hot_mask, ts_data->tshut_hot);
992 RMW_BITS(bgp, i, tshut_threshold,
993 tshut_cold_mask, ts_data->tshut_cold);
994 }
995 }
996
997 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
998 ti_bandgap_set_continuous_mode(bgp);
999
1000 /* Set .250 seconds time as default counter */
1001 if (TI_BANDGAP_HAS(bgp, COUNTER))
1002 for (i = 0; i < bgp->conf->sensor_count; i++)
1003 RMW_BITS(bgp, i, bgap_counter, counter_mask,
1004 bgp->clk_rate / 4);
1005
1006 /* Every thing is good? Then expose the sensors */
1007 for (i = 0; i < bgp->conf->sensor_count; i++) {
1008 char *domain;
1009
1010 if (bgp->conf->sensors[i].register_cooling) {
1011 ret = bgp->conf->sensors[i].register_cooling(bgp, i);
1012 if (ret)
1013 goto remove_sensors;
1014 }
1015
1016 if (bgp->conf->expose_sensor) {
1017 domain = bgp->conf->sensors[i].domain;
1018 ret = bgp->conf->expose_sensor(bgp, i, domain);
1019 if (ret)
1020 goto remove_last_cooling;
1021 }
1022 }
1023
1024 /*
1025 * Enable the Interrupts once everything is set. Otherwise irq handler
1026 * might be called as soon as it is enabled where as rest of framework
1027 * is still getting initialised.
1028 */
1029 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1030 ret = ti_bandgap_talert_init(bgp, pdev);
1031 if (ret) {
1032 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
1033 i = bgp->conf->sensor_count;
1034 goto disable_clk;
1035 }
1036 }
1037
1038#ifdef CONFIG_PM_SLEEP
1039 bgp->nb.notifier_call = bandgap_omap_cpu_notifier;
1040 if (!soc_device_match(soc_no_cpu_notifier))
1041 cpu_pm_register_notifier(&bgp->nb);
1042#endif
1043
1044 return 0;
1045
1046remove_last_cooling:
1047 if (bgp->conf->sensors[i].unregister_cooling)
1048 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1049remove_sensors:
1050 for (i--; i >= 0; i--) {
1051 if (bgp->conf->sensors[i].unregister_cooling)
1052 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1053 if (bgp->conf->remove_sensor)
1054 bgp->conf->remove_sensor(bgp, i);
1055 }
1056 ti_bandgap_power(bgp, false);
1057disable_clk:
1058 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1059 clk_disable_unprepare(bgp->fclock);
1060put_clks:
1061 clk_put(bgp->div_clk);
1062put_fclock:
1063 clk_put(bgp->fclock);
1064free_irqs:
1065 if (TI_BANDGAP_HAS(bgp, TSHUT))
1066 free_irq(gpiod_to_irq(bgp->tshut_gpiod), NULL);
1067
1068 return ret;
1069}
1070
1071static
1072void ti_bandgap_remove(struct platform_device *pdev)
1073{
1074 struct ti_bandgap *bgp = platform_get_drvdata(pdev);
1075 int i;
1076
1077 if (!soc_device_match(soc_no_cpu_notifier))
1078 cpu_pm_unregister_notifier(&bgp->nb);
1079
1080 /* Remove sensor interfaces */
1081 for (i = 0; i < bgp->conf->sensor_count; i++) {
1082 if (bgp->conf->sensors[i].unregister_cooling)
1083 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1084
1085 if (bgp->conf->remove_sensor)
1086 bgp->conf->remove_sensor(bgp, i);
1087 }
1088
1089 ti_bandgap_power(bgp, false);
1090
1091 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1092 clk_disable_unprepare(bgp->fclock);
1093 clk_put(bgp->fclock);
1094 clk_put(bgp->div_clk);
1095
1096 if (TI_BANDGAP_HAS(bgp, TALERT))
1097 free_irq(bgp->irq, bgp);
1098
1099 if (TI_BANDGAP_HAS(bgp, TSHUT))
1100 free_irq(gpiod_to_irq(bgp->tshut_gpiod), NULL);
1101}
1102
1103#ifdef CONFIG_PM_SLEEP
1104static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
1105{
1106 int i;
1107
1108 for (i = 0; i < bgp->conf->sensor_count; i++) {
1109 struct temp_sensor_registers *tsr;
1110 struct temp_sensor_regval *rval;
1111
1112 rval = &bgp->regval[i];
1113 tsr = bgp->conf->sensors[i].registers;
1114
1115 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1116 rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
1117 tsr->bgap_mode_ctrl);
1118 if (TI_BANDGAP_HAS(bgp, COUNTER))
1119 rval->bg_counter = ti_bandgap_readl(bgp,
1120 tsr->bgap_counter);
1121 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1122 rval->bg_threshold = ti_bandgap_readl(bgp,
1123 tsr->bgap_threshold);
1124 rval->bg_ctrl = ti_bandgap_readl(bgp,
1125 tsr->bgap_mask_ctrl);
1126 }
1127
1128 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1129 rval->tshut_threshold = ti_bandgap_readl(bgp,
1130 tsr->tshut_threshold);
1131 }
1132
1133 return 0;
1134}
1135
1136static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
1137{
1138 int i;
1139
1140 for (i = 0; i < bgp->conf->sensor_count; i++) {
1141 struct temp_sensor_registers *tsr;
1142 struct temp_sensor_regval *rval;
1143
1144 rval = &bgp->regval[i];
1145 tsr = bgp->conf->sensors[i].registers;
1146
1147 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1148 ti_bandgap_writel(bgp, rval->tshut_threshold,
1149 tsr->tshut_threshold);
1150 /* Force immediate temperature measurement and update
1151 * of the DTEMP field
1152 */
1153 ti_bandgap_force_single_read(bgp, i);
1154
1155 if (TI_BANDGAP_HAS(bgp, COUNTER))
1156 ti_bandgap_writel(bgp, rval->bg_counter,
1157 tsr->bgap_counter);
1158 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1159 ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1160 tsr->bgap_mode_ctrl);
1161 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1162 ti_bandgap_writel(bgp, rval->bg_threshold,
1163 tsr->bgap_threshold);
1164 ti_bandgap_writel(bgp, rval->bg_ctrl,
1165 tsr->bgap_mask_ctrl);
1166 }
1167 }
1168
1169 return 0;
1170}
1171
1172static int ti_bandgap_suspend(struct device *dev)
1173{
1174 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1175 int err;
1176
1177 err = ti_bandgap_save_ctxt(bgp);
1178 ti_bandgap_power(bgp, false);
1179
1180 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1181 clk_disable_unprepare(bgp->fclock);
1182
1183 bgp->is_suspended = true;
1184
1185 return err;
1186}
1187
1188static int bandgap_omap_cpu_notifier(struct notifier_block *nb,
1189 unsigned long cmd, void *v)
1190{
1191 struct ti_bandgap *bgp;
1192
1193 bgp = container_of(nb, struct ti_bandgap, nb);
1194
1195 spin_lock(&bgp->lock);
1196 switch (cmd) {
1197 case CPU_CLUSTER_PM_ENTER:
1198 if (bgp->is_suspended)
1199 break;
1200 ti_bandgap_save_ctxt(bgp);
1201 ti_bandgap_power(bgp, false);
1202 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1203 clk_disable(bgp->fclock);
1204 break;
1205 case CPU_CLUSTER_PM_ENTER_FAILED:
1206 case CPU_CLUSTER_PM_EXIT:
1207 if (bgp->is_suspended)
1208 break;
1209 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1210 clk_enable(bgp->fclock);
1211 ti_bandgap_power(bgp, true);
1212 ti_bandgap_restore_ctxt(bgp);
1213 break;
1214 }
1215 spin_unlock(&bgp->lock);
1216
1217 return NOTIFY_OK;
1218}
1219
1220static int ti_bandgap_resume(struct device *dev)
1221{
1222 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1223
1224 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1225 clk_prepare_enable(bgp->fclock);
1226
1227 ti_bandgap_power(bgp, true);
1228 bgp->is_suspended = false;
1229
1230 return ti_bandgap_restore_ctxt(bgp);
1231}
1232static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend,
1233 ti_bandgap_resume);
1234
1235#define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
1236#else
1237#define DEV_PM_OPS NULL
1238#endif
1239
1240static const struct of_device_id of_ti_bandgap_match[] = {
1241#ifdef CONFIG_OMAP3_THERMAL
1242 {
1243 .compatible = "ti,omap34xx-bandgap",
1244 .data = (void *)&omap34xx_data,
1245 },
1246 {
1247 .compatible = "ti,omap36xx-bandgap",
1248 .data = (void *)&omap36xx_data,
1249 },
1250#endif
1251#ifdef CONFIG_OMAP4_THERMAL
1252 {
1253 .compatible = "ti,omap4430-bandgap",
1254 .data = (void *)&omap4430_data,
1255 },
1256 {
1257 .compatible = "ti,omap4460-bandgap",
1258 .data = (void *)&omap4460_data,
1259 },
1260 {
1261 .compatible = "ti,omap4470-bandgap",
1262 .data = (void *)&omap4470_data,
1263 },
1264#endif
1265#ifdef CONFIG_OMAP5_THERMAL
1266 {
1267 .compatible = "ti,omap5430-bandgap",
1268 .data = (void *)&omap5430_data,
1269 },
1270#endif
1271#ifdef CONFIG_DRA752_THERMAL
1272 {
1273 .compatible = "ti,dra752-bandgap",
1274 .data = (void *)&dra752_data,
1275 },
1276#endif
1277 /* Sentinel */
1278 { },
1279};
1280MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
1281
1282static struct platform_driver ti_bandgap_sensor_driver = {
1283 .probe = ti_bandgap_probe,
1284 .remove = ti_bandgap_remove,
1285 .driver = {
1286 .name = "ti-soc-thermal",
1287 .pm = DEV_PM_OPS,
1288 .of_match_table = of_ti_bandgap_match,
1289 },
1290};
1291
1292module_platform_driver(ti_bandgap_sensor_driver);
1293
1294MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1295MODULE_LICENSE("GPL v2");
1296MODULE_ALIAS("platform:ti-soc-thermal");
1297MODULE_AUTHOR("Texas Instrument Inc.");