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