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