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