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