]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/ti-soc-thermal/ti-bandgap.c
staging: ti-soc-thermal: return error in case power switch is not supported
[karo-tx-linux.git] / drivers / staging / ti-soc-thermal / ti-bandgap.c
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/io.h>
42
43 #include "ti-bandgap.h"
44
45 /***   Helper functions to access registers and their bitfields   ***/
46
47 /**
48  * ti_bandgap_readl() - simple read helper function
49  * @bgp: pointer to ti_bandgap structure
50  * @reg: desired register (offset) to be read
51  *
52  * Helper function to read bandgap registers. It uses the io remapped area.
53  * Returns the register value.
54  */
55 static u32 ti_bandgap_readl(struct ti_bandgap *bgp, u32 reg)
56 {
57         return readl(bgp->base + reg);
58 }
59
60 /**
61  * ti_bandgap_writel() - simple write helper function
62  * @bgp: pointer to ti_bandgap structure
63  * @val: desired register value to be written
64  * @reg: desired register (offset) to be written
65  *
66  * Helper function to write bandgap registers. It uses the io remapped area.
67  */
68 static void ti_bandgap_writel(struct ti_bandgap *bgp, u32 val, u32 reg)
69 {
70         writel(val, bgp->base + reg);
71 }
72
73 /**
74  * DOC: macro to update bits.
75  *
76  * RMW_BITS() - used to read, modify and update bandgap bitfields.
77  *            The value passed will be shifted.
78  */
79 #define RMW_BITS(bgp, id, reg, mask, val)                       \
80 do {                                                            \
81         struct temp_sensor_registers *t;                        \
82         u32 r;                                                  \
83                                                                 \
84         t = bgp->conf->sensors[(id)].registers;         \
85         r = ti_bandgap_readl(bgp, t->reg);                      \
86         r &= ~t->mask;                                          \
87         r |= (val) << __ffs(t->mask);                           \
88         ti_bandgap_writel(bgp, r, t->reg);                      \
89 } while (0)
90
91 /***   Basic helper functions   ***/
92
93 /**
94  * ti_bandgap_power() - controls the power state of a bandgap device
95  * @bgp: pointer to ti_bandgap structure
96  * @on: desired power state (1 - on, 0 - off)
97  *
98  * Used to power on/off a bandgap device instance. Only used on those
99  * that features tempsoff bit.
100  */
101 static int ti_bandgap_power(struct ti_bandgap *bgp, bool on)
102 {
103         int i, ret = 0;
104
105         if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH)) {
106                 ret = -ENOTSUPP;
107                 goto exit;
108         }
109
110         for (i = 0; i < bgp->conf->sensor_count; i++)
111                 /* active on 0 */
112                 RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
113
114 exit:
115         return ret;
116 }
117
118 /**
119  * ti_bandgap_read_temp() - helper function to read sensor temperature
120  * @bgp: pointer to ti_bandgap structure
121  * @id: bandgap sensor id
122  *
123  * Function to concentrate the steps to read sensor temperature register.
124  * This function is desired because, depending on bandgap device version,
125  * it might be needed to freeze the bandgap state machine, before fetching
126  * the register value.
127  */
128 static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
129 {
130         struct temp_sensor_registers *tsr;
131         u32 temp, reg;
132
133         tsr = bgp->conf->sensors[id].registers;
134         reg = tsr->temp_sensor_ctrl;
135
136         if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
137                 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
138                 /*
139                  * In case we cannot read from cur_dtemp / dtemp_0,
140                  * then we read from the last valid temp read
141                  */
142                 reg = tsr->ctrl_dtemp_1;
143         }
144
145         /* read temperature */
146         temp = ti_bandgap_readl(bgp, reg);
147         temp &= tsr->bgap_dtemp_mask;
148
149         if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
150                 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
151
152         return temp;
153 }
154
155 /***   IRQ handlers   ***/
156
157 /**
158  * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
159  * @irq: IRQ number
160  * @data: private data (struct ti_bandgap *)
161  *
162  * This is the Talert handler. Use it only if bandgap device features
163  * HAS(TALERT). This handler goes over all sensors and checks their
164  * conditions and acts accordingly. In case there are events pending,
165  * it will reset the event mask to wait for the opposite event (next event).
166  * Every time there is a new event, it will be reported to thermal layer.
167  */
168 static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
169 {
170         struct ti_bandgap *bgp = data;
171         struct temp_sensor_registers *tsr;
172         u32 t_hot = 0, t_cold = 0, ctrl;
173         int i;
174
175         spin_lock(&bgp->lock);
176         for (i = 0; i < bgp->conf->sensor_count; i++) {
177                 tsr = bgp->conf->sensors[i].registers;
178                 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
179
180                 /* Read the status of t_hot */
181                 t_hot = ctrl & tsr->status_hot_mask;
182
183                 /* Read the status of t_cold */
184                 t_cold = ctrl & tsr->status_cold_mask;
185
186                 if (!t_cold && !t_hot)
187                         continue;
188
189                 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
190                 /*
191                  * One TALERT interrupt: Two sources
192                  * If the interrupt is due to t_hot then mask t_hot and
193                  * and unmask t_cold else mask t_cold and unmask t_hot
194                  */
195                 if (t_hot) {
196                         ctrl &= ~tsr->mask_hot_mask;
197                         ctrl |= tsr->mask_cold_mask;
198                 } else if (t_cold) {
199                         ctrl &= ~tsr->mask_cold_mask;
200                         ctrl |= tsr->mask_hot_mask;
201                 }
202
203                 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
204
205                 dev_dbg(bgp->dev,
206                         "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
207                         __func__, bgp->conf->sensors[i].domain,
208                         t_hot, t_cold);
209
210                 /* report temperature to whom may concern */
211                 if (bgp->conf->report_temperature)
212                         bgp->conf->report_temperature(bgp, i);
213         }
214         spin_unlock(&bgp->lock);
215
216         return IRQ_HANDLED;
217 }
218
219 /**
220  * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
221  * @irq: IRQ number
222  * @data: private data (unused)
223  *
224  * This is the Tshut handler. Use it only if bandgap device features
225  * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
226  * the system.
227  */
228 static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
229 {
230         pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
231                  __func__);
232
233         orderly_poweroff(true);
234
235         return IRQ_HANDLED;
236 }
237
238 /***   Helper functions which manipulate conversion ADC <-> mi Celsius   ***/
239
240 /**
241  * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
242  * @bgp: struct ti_bandgap pointer
243  * @adc_val: value in ADC representation
244  * @t: address where to write the resulting temperature in mCelsius
245  *
246  * Simple conversion from ADC representation to mCelsius. In case the ADC value
247  * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
248  * The conversion table is indexed by the ADC values.
249  */
250 static
251 int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
252 {
253         const struct ti_bandgap_data *conf = bgp->conf;
254         int ret = 0;
255
256         /* look up for temperature in the table and return the temperature */
257         if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val) {
258                 ret = -ERANGE;
259                 goto exit;
260         }
261
262         *t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
263
264 exit:
265         return ret;
266 }
267
268 /**
269  * ti_bandgap_mcelsius_to_adc() - converts a mCelsius value to ADC scale
270  * @bgp: struct ti_bandgap pointer
271  * @temp: value in mCelsius
272  * @adc: address where to write the resulting temperature in ADC representation
273  *
274  * Simple conversion from mCelsius to ADC values. In case the temp value
275  * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
276  * The conversion table is indexed by the ADC values.
277  */
278 static
279 int ti_bandgap_mcelsius_to_adc(struct ti_bandgap *bgp, long temp, int *adc)
280 {
281         const struct ti_bandgap_data *conf = bgp->conf;
282         const int *conv_table = bgp->conf->conv_table;
283         int high, low, mid, ret = 0;
284
285         low = 0;
286         high = conf->adc_end_val - conf->adc_start_val;
287         mid = (high + low) / 2;
288
289         if (temp < conv_table[low] || temp > conv_table[high]) {
290                 ret = -ERANGE;
291                 goto exit;
292         }
293
294         while (low < high) {
295                 if (temp < conv_table[mid])
296                         high = mid - 1;
297                 else
298                         low = mid + 1;
299                 mid = (low + high) / 2;
300         }
301
302         *adc = conf->adc_start_val + low;
303
304 exit:
305         return ret;
306 }
307
308 /**
309  * ti_bandgap_add_hyst() - add hysteresis (in mCelsius) to an ADC value
310  * @bgp: struct ti_bandgap pointer
311  * @adc_val: temperature value in ADC representation
312  * @hyst_val: hysteresis value in mCelsius
313  * @sum: address where to write the resulting temperature (in ADC scale)
314  *
315  * Adds an hysteresis value (in mCelsius) to a ADC temperature value.
316  * Returns 0 on success, -ERANGE otherwise.
317  */
318 static
319 int ti_bandgap_add_hyst(struct ti_bandgap *bgp, int adc_val, int hyst_val,
320                         u32 *sum)
321 {
322         int temp, ret;
323
324         /*
325          * Need to add in the mcelsius domain, so we have a temperature
326          * the conv_table range
327          */
328         ret = ti_bandgap_adc_to_mcelsius(bgp, adc_val, &temp);
329         if (ret < 0)
330                 goto exit;
331
332         temp += hyst_val;
333
334         ret = ti_bandgap_mcelsius_to_adc(bgp, temp, sum);
335
336 exit:
337         return ret;
338 }
339
340 /***   Helper functions handling device Alert/Shutdown signals   ***/
341
342 /**
343  * ti_bandgap_unmask_interrupts() - unmasks the events of thot & tcold
344  * @bgp: struct ti_bandgap pointer
345  * @id: bandgap sensor id
346  * @t_hot: hot temperature value to trigger alert signal
347  * @t_cold: cold temperature value to trigger alert signal
348  *
349  * Checks the requested t_hot and t_cold values and configures the IRQ event
350  * masks accordingly. Call this function only if bandgap features HAS(TALERT).
351  */
352 static void ti_bandgap_unmask_interrupts(struct ti_bandgap *bgp, int id,
353                                          u32 t_hot, u32 t_cold)
354 {
355         struct temp_sensor_registers *tsr;
356         u32 temp, reg_val;
357
358         /* Read the current on die temperature */
359         temp = ti_bandgap_read_temp(bgp, id);
360
361         tsr = bgp->conf->sensors[id].registers;
362         reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
363
364         if (temp < t_hot)
365                 reg_val |= tsr->mask_hot_mask;
366         else
367                 reg_val &= ~tsr->mask_hot_mask;
368
369         if (t_cold < temp)
370                 reg_val |= tsr->mask_cold_mask;
371         else
372                 reg_val &= ~tsr->mask_cold_mask;
373         ti_bandgap_writel(bgp, reg_val, tsr->bgap_mask_ctrl);
374 }
375
376 /**
377  * ti_bandgap_update_alert_threshold() - sequence to update thresholds
378  * @bgp: struct ti_bandgap pointer
379  * @id: bandgap sensor id
380  * @val: value (ADC) of a new threshold
381  * @hot: desired threshold to be updated. true if threshold hot, false if
382  *       threshold cold
383  *
384  * It will program the required thresholds (hot and cold) for TALERT signal.
385  * This function can be used to update t_hot or t_cold, depending on @hot value.
386  * It checks the resulting t_hot and t_cold values, based on the new passed @val
387  * and configures the thresholds so that t_hot is always greater than t_cold.
388  * Call this function only if bandgap features HAS(TALERT).
389  */
390 static int ti_bandgap_update_alert_threshold(struct ti_bandgap *bgp, int id,
391                                              int val, bool hot)
392 {
393         struct temp_sensor_data *ts_data = bgp->conf->sensors[id].ts_data;
394         struct temp_sensor_registers *tsr;
395         u32 thresh_val, reg_val, t_hot, t_cold;
396         int err = 0;
397
398         tsr = bgp->conf->sensors[id].registers;
399
400         /* obtain the current value */
401         thresh_val = ti_bandgap_readl(bgp, tsr->bgap_threshold);
402         t_cold = (thresh_val & tsr->threshold_tcold_mask) >>
403                 __ffs(tsr->threshold_tcold_mask);
404         t_hot = (thresh_val & tsr->threshold_thot_mask) >>
405                 __ffs(tsr->threshold_thot_mask);
406         if (hot)
407                 t_hot = val;
408         else
409                 t_cold = val;
410
411         if (t_cold > t_hot) {
412                 if (hot)
413                         err = ti_bandgap_add_hyst(bgp, t_hot,
414                                                   -ts_data->hyst_val,
415                                                   &t_cold);
416                 else
417                         err = ti_bandgap_add_hyst(bgp, t_cold,
418                                                   ts_data->hyst_val,
419                                                   &t_hot);
420         }
421
422         /* write the new threshold values */
423         reg_val = thresh_val &
424                   ~(tsr->threshold_thot_mask | tsr->threshold_tcold_mask);
425         reg_val |= (t_hot << __ffs(tsr->threshold_thot_mask)) |
426                    (t_cold << __ffs(tsr->threshold_tcold_mask));
427         ti_bandgap_writel(bgp, reg_val, tsr->bgap_threshold);
428
429         if (err) {
430                 dev_err(bgp->dev, "failed to reprogram thot threshold\n");
431                 err = -EIO;
432                 goto exit;
433         }
434
435         ti_bandgap_unmask_interrupts(bgp, id, t_hot, t_cold);
436 exit:
437         return err;
438 }
439
440 /**
441  * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
442  * @bgp: struct ti_bandgap pointer
443  * @id: bandgap sensor id
444  *
445  * Checks if the bandgap pointer is valid and if the sensor id is also
446  * applicable.
447  */
448 static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
449 {
450         int ret = 0;
451
452         if (IS_ERR_OR_NULL(bgp)) {
453                 pr_err("%s: invalid bandgap pointer\n", __func__);
454                 ret = -EINVAL;
455                 goto exit;
456         }
457
458         if ((id < 0) || (id >= bgp->conf->sensor_count)) {
459                 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
460                         __func__, id);
461                 ret = -ERANGE;
462         }
463
464 exit:
465         return ret;
466 }
467
468 /**
469  * _ti_bandgap_write_threshold() - helper to update TALERT t_cold or t_hot
470  * @bgp: struct ti_bandgap pointer
471  * @id: bandgap sensor id
472  * @val: value (mCelsius) of a new threshold
473  * @hot: desired threshold to be updated. true if threshold hot, false if
474  *       threshold cold
475  *
476  * It will update the required thresholds (hot and cold) for TALERT signal.
477  * This function can be used to update t_hot or t_cold, depending on @hot value.
478  * Validates the mCelsius range and update the requested threshold.
479  * Call this function only if bandgap features HAS(TALERT).
480  */
481 static int _ti_bandgap_write_threshold(struct ti_bandgap *bgp, int id, int val,
482                                        bool hot)
483 {
484         struct temp_sensor_data *ts_data;
485         struct temp_sensor_registers *tsr;
486         u32 adc_val;
487         int ret;
488
489         ret = ti_bandgap_validate(bgp, id);
490         if (ret)
491                 goto exit;
492
493         if (!TI_BANDGAP_HAS(bgp, TALERT)) {
494                 ret = -ENOTSUPP;
495                 goto exit;
496         }
497
498         ts_data = bgp->conf->sensors[id].ts_data;
499         tsr = bgp->conf->sensors[id].registers;
500         if (hot) {
501                 if (val < ts_data->min_temp + ts_data->hyst_val)
502                         ret = -EINVAL;
503         } else {
504                 if (val > ts_data->max_temp + ts_data->hyst_val)
505                         ret = -EINVAL;
506         }
507
508         if (ret)
509                 goto exit;
510
511         ret = ti_bandgap_mcelsius_to_adc(bgp, val, &adc_val);
512         if (ret < 0)
513                 goto exit;
514
515         spin_lock(&bgp->lock);
516         ret = ti_bandgap_update_alert_threshold(bgp, id, adc_val, hot);
517         spin_unlock(&bgp->lock);
518
519 exit:
520         return ret;
521 }
522
523 /**
524  * _ti_bandgap_read_threshold() - helper to read TALERT t_cold or t_hot
525  * @bgp: struct ti_bandgap pointer
526  * @id: bandgap sensor id
527  * @val: value (mCelsius) of a threshold
528  * @hot: desired threshold to be read. true if threshold hot, false if
529  *       threshold cold
530  *
531  * It will fetch the required thresholds (hot and cold) for TALERT signal.
532  * This function can be used to read t_hot or t_cold, depending on @hot value.
533  * Call this function only if bandgap features HAS(TALERT).
534  */
535 static int _ti_bandgap_read_threshold(struct ti_bandgap *bgp, int id,
536                                       int *val, bool hot)
537 {
538         struct temp_sensor_registers *tsr;
539         u32 temp, mask;
540         int ret = 0;
541
542         ret = ti_bandgap_validate(bgp, id);
543         if (ret)
544                 goto exit;
545
546         if (!TI_BANDGAP_HAS(bgp, TALERT)) {
547                 ret = -ENOTSUPP;
548                 goto exit;
549         }
550
551         tsr = bgp->conf->sensors[id].registers;
552         if (hot)
553                 mask = tsr->threshold_thot_mask;
554         else
555                 mask = tsr->threshold_tcold_mask;
556
557         temp = ti_bandgap_readl(bgp, tsr->bgap_threshold);
558         temp = (temp & mask) >> __ffs(mask);
559         ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
560         if (ret) {
561                 dev_err(bgp->dev, "failed to read thot\n");
562                 ret = -EIO;
563                 goto exit;
564         }
565
566         *val = temp;
567
568 exit:
569         return ret;
570 }
571
572 /***   Exposed APIs   ***/
573
574 /**
575  * ti_bandgap_read_thot() - reads sensor current thot
576  * @bgp: pointer to bandgap instance
577  * @id: sensor id
578  * @thot: resulting current thot value
579  *
580  * returns 0 on success or the proper error code
581  */
582 int ti_bandgap_read_thot(struct ti_bandgap *bgp, int id, int *thot)
583 {
584         return _ti_bandgap_read_threshold(bgp, id, thot, true);
585 }
586
587 /**
588  * ti_bandgap_write_thot() - sets sensor current thot
589  * @bgp: pointer to bandgap instance
590  * @id: sensor id
591  * @val: desired thot value
592  *
593  * returns 0 on success or the proper error code
594  */
595 int ti_bandgap_write_thot(struct ti_bandgap *bgp, int id, int val)
596 {
597         return _ti_bandgap_write_threshold(bgp, id, val, true);
598 }
599
600 /**
601  * ti_bandgap_read_tcold() - reads sensor current tcold
602  * @bgp: pointer to bandgap instance
603  * @id: sensor id
604  * @tcold: resulting current tcold value
605  *
606  * returns 0 on success or the proper error code
607  */
608 int ti_bandgap_read_tcold(struct ti_bandgap *bgp, int id, int *tcold)
609 {
610         return _ti_bandgap_read_threshold(bgp, id, tcold, false);
611 }
612
613 /**
614  * ti_bandgap_write_tcold() - sets the sensor tcold
615  * @bgp: pointer to bandgap instance
616  * @id: sensor id
617  * @val: desired tcold value
618  *
619  * returns 0 on success or the proper error code
620  */
621 int ti_bandgap_write_tcold(struct ti_bandgap *bgp, int id, int val)
622 {
623         return _ti_bandgap_write_threshold(bgp, id, val, false);
624 }
625
626 /**
627  * ti_bandgap_read_update_interval() - read the sensor update interval
628  * @bgp: pointer to bandgap instance
629  * @id: sensor id
630  * @interval: resulting update interval in miliseconds
631  *
632  * returns 0 on success or the proper error code
633  */
634 int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
635                                     int *interval)
636 {
637         struct temp_sensor_registers *tsr;
638         u32 time;
639         int ret;
640
641         ret = ti_bandgap_validate(bgp, id);
642         if (ret)
643                 return ret;
644
645         if (!TI_BANDGAP_HAS(bgp, COUNTER))
646                 return -ENOTSUPP;
647
648         tsr = bgp->conf->sensors[id].registers;
649         time = ti_bandgap_readl(bgp, tsr->bgap_counter);
650         time = (time & tsr->counter_mask) >> __ffs(tsr->counter_mask);
651         time = time * 1000 / bgp->clk_rate;
652
653         *interval = time;
654
655         return 0;
656 }
657
658 /**
659  * ti_bandgap_write_update_interval() - set the update interval
660  * @bgp: pointer to bandgap instance
661  * @id: sensor id
662  * @interval: desired update interval in miliseconds
663  *
664  * returns 0 on success or the proper error code
665  */
666 int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
667                                      int id, u32 interval)
668 {
669         int ret = ti_bandgap_validate(bgp, id);
670         if (ret)
671                 return ret;
672
673         if (!TI_BANDGAP_HAS(bgp, COUNTER))
674                 return -ENOTSUPP;
675
676         interval = interval * bgp->clk_rate / 1000;
677         spin_lock(&bgp->lock);
678         RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
679         spin_unlock(&bgp->lock);
680
681         return 0;
682 }
683
684 /**
685  * ti_bandgap_read_temperature() - report current temperature
686  * @bgp: pointer to bandgap instance
687  * @id: sensor id
688  * @temperature: resulting temperature
689  *
690  * returns 0 on success or the proper error code
691  */
692 int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
693                                 int *temperature)
694 {
695         u32 temp;
696         int ret;
697
698         ret = ti_bandgap_validate(bgp, id);
699         if (ret)
700                 return ret;
701
702         spin_lock(&bgp->lock);
703         temp = ti_bandgap_read_temp(bgp, id);
704         spin_unlock(&bgp->lock);
705
706         ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
707         if (ret)
708                 return -EIO;
709
710         *temperature = temp;
711
712         return 0;
713 }
714
715 /**
716  * ti_bandgap_set_sensor_data() - helper function to store thermal
717  * framework related data.
718  * @bgp: pointer to bandgap instance
719  * @id: sensor id
720  * @data: thermal framework related data to be stored
721  *
722  * returns 0 on success or the proper error code
723  */
724 int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
725 {
726         int ret = ti_bandgap_validate(bgp, id);
727         if (ret)
728                 return ret;
729
730         bgp->regval[id].data = data;
731
732         return 0;
733 }
734
735 /**
736  * ti_bandgap_get_sensor_data() - helper function to get thermal
737  * framework related data.
738  * @bgp: pointer to bandgap instance
739  * @id: sensor id
740  *
741  * returns data stored by set function with sensor id on success or NULL
742  */
743 void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
744 {
745         int ret = ti_bandgap_validate(bgp, id);
746         if (ret)
747                 return ERR_PTR(ret);
748
749         return bgp->regval[id].data;
750 }
751
752 /***   Helper functions used during device initialization   ***/
753
754 /**
755  * ti_bandgap_force_single_read() - executes 1 single ADC conversion
756  * @bgp: pointer to struct ti_bandgap
757  * @id: sensor id which it is desired to read 1 temperature
758  *
759  * Used to initialize the conversion state machine and set it to a valid
760  * state. Called during device initialization and context restore events.
761  */
762 static int
763 ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
764 {
765         u32 temp = 0, counter = 1000;
766
767         /* Select single conversion mode */
768         if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
769                 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
770
771         /* Start of Conversion = 1 */
772         RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
773         /* Wait until DTEMP is updated */
774         temp = ti_bandgap_read_temp(bgp, id);
775
776         while ((temp == 0) && --counter)
777                 temp = ti_bandgap_read_temp(bgp, id);
778         /* REVISIT: Check correct condition for end of conversion */
779
780         /* Start of Conversion = 0 */
781         RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
782
783         return 0;
784 }
785
786 /**
787  * ti_bandgap_set_continous_mode() - One time enabling of continuous mode
788  * @bgp: pointer to struct ti_bandgap
789  *
790  * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
791  * be used for junction temperature monitoring, it is desirable that the
792  * sensors are operational all the time, so that alerts are generated
793  * properly.
794  */
795 static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
796 {
797         int i;
798
799         for (i = 0; i < bgp->conf->sensor_count; i++) {
800                 /* Perform a single read just before enabling continuous */
801                 ti_bandgap_force_single_read(bgp, i);
802                 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
803         }
804
805         return 0;
806 }
807
808 /**
809  * ti_bandgap_tshut_init() - setup and initialize tshut handling
810  * @bgp: pointer to struct ti_bandgap
811  * @pdev: pointer to device struct platform_device
812  *
813  * Call this function only in case the bandgap features HAS(TSHUT).
814  * In this case, the driver needs to handle the TSHUT signal as an IRQ.
815  * The IRQ is wired as a GPIO, and for this purpose, it is required
816  * to specify which GPIO line is used. TSHUT IRQ is fired anytime
817  * one of the bandgap sensors violates the TSHUT high/hot threshold.
818  * And in that case, the system must go off.
819  */
820 static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
821                                  struct platform_device *pdev)
822 {
823         int gpio_nr = bgp->tshut_gpio;
824         int status;
825
826         /* Request for gpio_86 line */
827         status = gpio_request(gpio_nr, "tshut");
828         if (status < 0) {
829                 dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86);
830                 return status;
831         }
832         status = gpio_direction_input(gpio_nr);
833         if (status) {
834                 dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr);
835                 return status;
836         }
837
838         status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler,
839                              IRQF_TRIGGER_RISING, "tshut", NULL);
840         if (status) {
841                 gpio_free(gpio_nr);
842                 dev_err(bgp->dev, "request irq failed for TSHUT");
843         }
844
845         return 0;
846 }
847
848 /**
849  * ti_bandgap_alert_init() - setup and initialize talert handling
850  * @bgp: pointer to struct ti_bandgap
851  * @pdev: pointer to device struct platform_device
852  *
853  * Call this function only in case the bandgap features HAS(TALERT).
854  * In this case, the driver needs to handle the TALERT signals as an IRQs.
855  * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
856  * are violated. In these situation, the driver must reprogram the thresholds,
857  * accordingly to specified policy.
858  */
859 static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
860                                   struct platform_device *pdev)
861 {
862         int ret;
863
864         bgp->irq = platform_get_irq(pdev, 0);
865         if (bgp->irq < 0) {
866                 dev_err(&pdev->dev, "get_irq failed\n");
867                 return bgp->irq;
868         }
869         ret = request_threaded_irq(bgp->irq, NULL,
870                                    ti_bandgap_talert_irq_handler,
871                                    IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
872                                    "talert", bgp);
873         if (ret) {
874                 dev_err(&pdev->dev, "Request threaded irq failed.\n");
875                 return ret;
876         }
877
878         return 0;
879 }
880
881 static const struct of_device_id of_ti_bandgap_match[];
882 /**
883  * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
884  * @pdev: pointer to device struct platform_device
885  *
886  * Used to read the device tree properties accordingly to the bandgap
887  * matching version. Based on bandgap version and its capabilities it
888  * will build a struct ti_bandgap out of the required DT entries.
889  */
890 static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
891 {
892         struct device_node *node = pdev->dev.of_node;
893         const struct of_device_id *of_id;
894         struct ti_bandgap *bgp;
895         struct resource *res;
896         u32 prop;
897         int i;
898
899         /* just for the sake */
900         if (!node) {
901                 dev_err(&pdev->dev, "no platform information available\n");
902                 return ERR_PTR(-EINVAL);
903         }
904
905         bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
906         if (!bgp) {
907                 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
908                 return ERR_PTR(-ENOMEM);
909         }
910
911         of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
912         if (of_id)
913                 bgp->conf = of_id->data;
914
915         /* register shadow for context save and restore */
916         bgp->regval = devm_kzalloc(&pdev->dev, sizeof(*bgp->regval) *
917                                    bgp->conf->sensor_count, GFP_KERNEL);
918         if (!bgp) {
919                 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
920                 return ERR_PTR(-ENOMEM);
921         }
922
923         i = 0;
924         do {
925                 void __iomem *chunk;
926
927                 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
928                 if (!res)
929                         break;
930                 chunk = devm_ioremap_resource(&pdev->dev, res);
931                 if (i == 0)
932                         bgp->base = chunk;
933                 if (IS_ERR(chunk))
934                         return ERR_CAST(chunk);
935
936                 i++;
937         } while (res);
938
939         if (TI_BANDGAP_HAS(bgp, TSHUT)) {
940                 if (of_property_read_u32(node, "ti,tshut-gpio", &prop) < 0) {
941                         dev_err(&pdev->dev, "missing tshut gpio in device tree\n");
942                         return ERR_PTR(-EINVAL);
943                 }
944                 bgp->tshut_gpio = prop;
945                 if (!gpio_is_valid(bgp->tshut_gpio)) {
946                         dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
947                                 bgp->tshut_gpio);
948                         return ERR_PTR(-EINVAL);
949                 }
950         }
951
952         return bgp;
953 }
954
955 /***   Device driver call backs   ***/
956
957 static
958 int ti_bandgap_probe(struct platform_device *pdev)
959 {
960         struct ti_bandgap *bgp;
961         int clk_rate, ret = 0, i;
962
963         bgp = ti_bandgap_build(pdev);
964         if (IS_ERR_OR_NULL(bgp)) {
965                 dev_err(&pdev->dev, "failed to fetch platform data\n");
966                 return PTR_ERR(bgp);
967         }
968         bgp->dev = &pdev->dev;
969
970         if (TI_BANDGAP_HAS(bgp, TSHUT)) {
971                 ret = ti_bandgap_tshut_init(bgp, pdev);
972                 if (ret) {
973                         dev_err(&pdev->dev,
974                                 "failed to initialize system tshut IRQ\n");
975                         return ret;
976                 }
977         }
978
979         bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
980         ret = IS_ERR_OR_NULL(bgp->fclock);
981         if (ret) {
982                 dev_err(&pdev->dev, "failed to request fclock reference\n");
983                 goto free_irqs;
984         }
985
986         bgp->div_clk = clk_get(NULL,  bgp->conf->div_ck_name);
987         ret = IS_ERR_OR_NULL(bgp->div_clk);
988         if (ret) {
989                 dev_err(&pdev->dev,
990                         "failed to request div_ts_ck clock ref\n");
991                 goto free_irqs;
992         }
993
994         for (i = 0; i < bgp->conf->sensor_count; i++) {
995                 struct temp_sensor_registers *tsr;
996                 u32 val;
997
998                 tsr = bgp->conf->sensors[i].registers;
999                 /*
1000                  * check if the efuse has a non-zero value if not
1001                  * it is an untrimmed sample and the temperatures
1002                  * may not be accurate
1003                  */
1004                 val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
1005                 if (ret || !val)
1006                         dev_info(&pdev->dev,
1007                                  "Non-trimmed BGAP, Temp not accurate\n");
1008         }
1009
1010         clk_rate = clk_round_rate(bgp->div_clk,
1011                                   bgp->conf->sensors[0].ts_data->max_freq);
1012         if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
1013             clk_rate == 0xffffffff) {
1014                 ret = -ENODEV;
1015                 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
1016                 goto put_clks;
1017         }
1018
1019         ret = clk_set_rate(bgp->div_clk, clk_rate);
1020         if (ret)
1021                 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
1022
1023         bgp->clk_rate = clk_rate;
1024         if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1025                 clk_prepare_enable(bgp->fclock);
1026
1027
1028         spin_lock_init(&bgp->lock);
1029         bgp->dev = &pdev->dev;
1030         platform_set_drvdata(pdev, bgp);
1031
1032         ti_bandgap_power(bgp, true);
1033
1034         /* Set default counter to 1 for now */
1035         if (TI_BANDGAP_HAS(bgp, COUNTER))
1036                 for (i = 0; i < bgp->conf->sensor_count; i++)
1037                         RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
1038
1039         /* Set default thresholds for alert and shutdown */
1040         for (i = 0; i < bgp->conf->sensor_count; i++) {
1041                 struct temp_sensor_data *ts_data;
1042
1043                 ts_data = bgp->conf->sensors[i].ts_data;
1044
1045                 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1046                         /* Set initial Talert thresholds */
1047                         RMW_BITS(bgp, i, bgap_threshold,
1048                                  threshold_tcold_mask, ts_data->t_cold);
1049                         RMW_BITS(bgp, i, bgap_threshold,
1050                                  threshold_thot_mask, ts_data->t_hot);
1051                         /* Enable the alert events */
1052                         RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
1053                         RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
1054                 }
1055
1056                 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
1057                         /* Set initial Tshut thresholds */
1058                         RMW_BITS(bgp, i, tshut_threshold,
1059                                  tshut_hot_mask, ts_data->tshut_hot);
1060                         RMW_BITS(bgp, i, tshut_threshold,
1061                                  tshut_cold_mask, ts_data->tshut_cold);
1062                 }
1063         }
1064
1065         if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1066                 ti_bandgap_set_continuous_mode(bgp);
1067
1068         /* Set .250 seconds time as default counter */
1069         if (TI_BANDGAP_HAS(bgp, COUNTER))
1070                 for (i = 0; i < bgp->conf->sensor_count; i++)
1071                         RMW_BITS(bgp, i, bgap_counter, counter_mask,
1072                                  bgp->clk_rate / 4);
1073
1074         /* Every thing is good? Then expose the sensors */
1075         for (i = 0; i < bgp->conf->sensor_count; i++) {
1076                 char *domain;
1077
1078                 if (bgp->conf->sensors[i].register_cooling)
1079                         bgp->conf->sensors[i].register_cooling(bgp, i);
1080
1081                 domain = bgp->conf->sensors[i].domain;
1082                 if (bgp->conf->expose_sensor)
1083                         bgp->conf->expose_sensor(bgp, i, domain);
1084         }
1085
1086         /*
1087          * Enable the Interrupts once everything is set. Otherwise irq handler
1088          * might be called as soon as it is enabled where as rest of framework
1089          * is still getting initialised.
1090          */
1091         if (TI_BANDGAP_HAS(bgp, TALERT)) {
1092                 ret = ti_bandgap_talert_init(bgp, pdev);
1093                 if (ret) {
1094                         dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
1095                         i = bgp->conf->sensor_count;
1096                         goto disable_clk;
1097                 }
1098         }
1099
1100         return 0;
1101
1102 disable_clk:
1103         if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1104                 clk_disable_unprepare(bgp->fclock);
1105 put_clks:
1106         clk_put(bgp->fclock);
1107         clk_put(bgp->div_clk);
1108 free_irqs:
1109         if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1110                 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1111                 gpio_free(bgp->tshut_gpio);
1112         }
1113
1114         return ret;
1115 }
1116
1117 static
1118 int ti_bandgap_remove(struct platform_device *pdev)
1119 {
1120         struct ti_bandgap *bgp = platform_get_drvdata(pdev);
1121         int i;
1122
1123         /* First thing is to remove sensor interfaces */
1124         for (i = 0; i < bgp->conf->sensor_count; i++) {
1125                 if (bgp->conf->sensors[i].register_cooling)
1126                         bgp->conf->sensors[i].unregister_cooling(bgp, i);
1127
1128                 if (bgp->conf->remove_sensor)
1129                         bgp->conf->remove_sensor(bgp, i);
1130         }
1131
1132         ti_bandgap_power(bgp, false);
1133
1134         if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1135                 clk_disable_unprepare(bgp->fclock);
1136         clk_put(bgp->fclock);
1137         clk_put(bgp->div_clk);
1138
1139         if (TI_BANDGAP_HAS(bgp, TALERT))
1140                 free_irq(bgp->irq, bgp);
1141
1142         if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1143                 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1144                 gpio_free(bgp->tshut_gpio);
1145         }
1146
1147         return 0;
1148 }
1149
1150 #ifdef CONFIG_PM
1151 static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
1152 {
1153         int i;
1154
1155         for (i = 0; i < bgp->conf->sensor_count; i++) {
1156                 struct temp_sensor_registers *tsr;
1157                 struct temp_sensor_regval *rval;
1158
1159                 rval = &bgp->regval[i];
1160                 tsr = bgp->conf->sensors[i].registers;
1161
1162                 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1163                         rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
1164                                                         tsr->bgap_mode_ctrl);
1165                 if (TI_BANDGAP_HAS(bgp, COUNTER))
1166                         rval->bg_counter = ti_bandgap_readl(bgp,
1167                                                         tsr->bgap_counter);
1168                 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1169                         rval->bg_threshold = ti_bandgap_readl(bgp,
1170                                                         tsr->bgap_threshold);
1171                         rval->bg_ctrl = ti_bandgap_readl(bgp,
1172                                                    tsr->bgap_mask_ctrl);
1173                 }
1174
1175                 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1176                         rval->tshut_threshold = ti_bandgap_readl(bgp,
1177                                                    tsr->tshut_threshold);
1178         }
1179
1180         return 0;
1181 }
1182
1183 static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
1184 {
1185         int i;
1186
1187         for (i = 0; i < bgp->conf->sensor_count; i++) {
1188                 struct temp_sensor_registers *tsr;
1189                 struct temp_sensor_regval *rval;
1190                 u32 val = 0;
1191
1192                 rval = &bgp->regval[i];
1193                 tsr = bgp->conf->sensors[i].registers;
1194
1195                 if (TI_BANDGAP_HAS(bgp, COUNTER))
1196                         val = ti_bandgap_readl(bgp, tsr->bgap_counter);
1197
1198                 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1199                         ti_bandgap_writel(bgp, rval->tshut_threshold,
1200                                           tsr->tshut_threshold);
1201                 /* Force immediate temperature measurement and update
1202                  * of the DTEMP field
1203                  */
1204                 ti_bandgap_force_single_read(bgp, i);
1205
1206                 if (TI_BANDGAP_HAS(bgp, COUNTER))
1207                         ti_bandgap_writel(bgp, rval->bg_counter,
1208                                           tsr->bgap_counter);
1209                 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1210                         ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1211                                           tsr->bgap_mode_ctrl);
1212                 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1213                         ti_bandgap_writel(bgp, rval->bg_threshold,
1214                                           tsr->bgap_threshold);
1215                         ti_bandgap_writel(bgp, rval->bg_ctrl,
1216                                           tsr->bgap_mask_ctrl);
1217                 }
1218         }
1219
1220         return 0;
1221 }
1222
1223 static int ti_bandgap_suspend(struct device *dev)
1224 {
1225         struct ti_bandgap *bgp = dev_get_drvdata(dev);
1226         int err;
1227
1228         err = ti_bandgap_save_ctxt(bgp);
1229         ti_bandgap_power(bgp, false);
1230
1231         if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1232                 clk_disable_unprepare(bgp->fclock);
1233
1234         return err;
1235 }
1236
1237 static int ti_bandgap_resume(struct device *dev)
1238 {
1239         struct ti_bandgap *bgp = dev_get_drvdata(dev);
1240
1241         if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1242                 clk_prepare_enable(bgp->fclock);
1243
1244         ti_bandgap_power(bgp, true);
1245
1246         return ti_bandgap_restore_ctxt(bgp);
1247 }
1248 static const struct dev_pm_ops ti_bandgap_dev_pm_ops = {
1249         SET_SYSTEM_SLEEP_PM_OPS(ti_bandgap_suspend,
1250                                 ti_bandgap_resume)
1251 };
1252
1253 #define DEV_PM_OPS      (&ti_bandgap_dev_pm_ops)
1254 #else
1255 #define DEV_PM_OPS      NULL
1256 #endif
1257
1258 static const struct of_device_id of_ti_bandgap_match[] = {
1259 #ifdef CONFIG_OMAP4_THERMAL
1260         {
1261                 .compatible = "ti,omap4430-bandgap",
1262                 .data = (void *)&omap4430_data,
1263         },
1264         {
1265                 .compatible = "ti,omap4460-bandgap",
1266                 .data = (void *)&omap4460_data,
1267         },
1268         {
1269                 .compatible = "ti,omap4470-bandgap",
1270                 .data = (void *)&omap4470_data,
1271         },
1272 #endif
1273 #ifdef CONFIG_OMAP5_THERMAL
1274         {
1275                 .compatible = "ti,omap5430-bandgap",
1276                 .data = (void *)&omap5430_data,
1277         },
1278 #endif
1279         /* Sentinel */
1280         { },
1281 };
1282 MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
1283
1284 static struct platform_driver ti_bandgap_sensor_driver = {
1285         .probe = ti_bandgap_probe,
1286         .remove = ti_bandgap_remove,
1287         .driver = {
1288                         .name = "ti-soc-thermal",
1289                         .pm = DEV_PM_OPS,
1290                         .of_match_table = of_ti_bandgap_match,
1291         },
1292 };
1293
1294 module_platform_driver(ti_bandgap_sensor_driver);
1295
1296 MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1297 MODULE_LICENSE("GPL v2");
1298 MODULE_ALIAS("platform:ti-soc-thermal");
1299 MODULE_AUTHOR("Texas Instrument Inc.");