39e2eec57fff236c40a9ed7003dd6ba32407f56a
[firefly-linux-kernel-4.4.55.git] / drivers / thermal / rockchip_thermal.c
1 /*
2  * Copyright (c) 2014-2016, Fuzhou Rockchip Electronics Co., Ltd
3  * Caesar Wang <wxt@rock-chips.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  */
14
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/module.h>
20 #include <linux/of.h>
21 #include <linux/of_address.h>
22 #include <linux/of_irq.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
25 #include <linux/reset.h>
26 #include <linux/thermal.h>
27 #include <linux/mfd/syscon.h>
28 #include <linux/pinctrl/consumer.h>
29
30 /**
31  * If the temperature over a period of time High,
32  * the resulting TSHUT gave CRU module,let it reset the entire chip,
33  * or via GPIO give PMIC.
34  */
35 enum tshut_mode {
36         TSHUT_MODE_CRU = 0,
37         TSHUT_MODE_GPIO,
38 };
39
40 /**
41  * The system Temperature Sensors tshut(tshut) polarity
42  * the bit 8 is tshut polarity.
43  * 0: low active, 1: high active
44  */
45 enum tshut_polarity {
46         TSHUT_LOW_ACTIVE = 0,
47         TSHUT_HIGH_ACTIVE,
48 };
49
50 /**
51  * The system has two Temperature Sensors.
52  * sensor0 is for CPU, and sensor1 is for GPU.
53  */
54 enum sensor_id {
55         SENSOR_CPU = 0,
56         SENSOR_GPU,
57 };
58
59 /**
60  * The conversion table has the adc value and temperature.
61  * ADC_DECREMENT: the adc value is of diminishing.(e.g. rk3288_code_table)
62  * ADC_INCREMENT: the adc value is incremental.(e.g. rk3368_code_table)
63  */
64 enum adc_sort_mode {
65         ADC_DECREMENT = 0,
66         ADC_INCREMENT,
67 };
68
69 /**
70  * The max sensors is two in rockchip SoCs.
71  * Two sensors: CPU and GPU sensor.
72  */
73 #define SOC_MAX_SENSORS 2
74
75 /**
76  * struct chip_tsadc_table - hold information about chip-specific differences
77  * @id: conversion table
78  * @length: size of conversion table
79  * @data_mask: mask to apply on data inputs
80  * @mode: sort mode of this adc variant (incrementing or decrementing)
81  */
82 struct chip_tsadc_table {
83         const struct tsadc_table *id;
84         unsigned int length;
85         u32 data_mask;
86         enum adc_sort_mode mode;
87 };
88
89 /**
90  * struct rockchip_tsadc_chip - hold the private data of tsadc chip
91  * @chn_id[SOC_MAX_SENSORS]: the sensor id of chip correspond to the channel
92  * @chn_num: the channel number of tsadc chip
93  * @tshut_temp: the hardware-controlled shutdown temperature value
94  * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
95  * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
96  * @initialize: SoC special initialize tsadc controller method
97  * @irq_ack: clear the interrupt
98  * @get_temp: get the temperature
99  * @set_alarm_temp: set the high temperature interrupt
100  * @set_tshut_temp: set the hardware-controlled shutdown temperature
101  * @set_tshut_mode: set the hardware-controlled shutdown mode
102  * @table: the chip-specific conversion table
103  */
104 struct rockchip_tsadc_chip {
105         /* The sensor id of chip correspond to the ADC channel */
106         int chn_id[SOC_MAX_SENSORS];
107         int chn_num;
108
109         /* The hardware-controlled tshut property */
110         int tshut_temp;
111         enum tshut_mode tshut_mode;
112         enum tshut_polarity tshut_polarity;
113
114         /* Chip-wide methods */
115         void (*initialize)(struct regmap *grf,
116                            void __iomem *reg, enum tshut_polarity p);
117         void (*irq_ack)(void __iomem *reg);
118         void (*control)(void __iomem *reg, bool on);
119
120         /* Per-sensor methods */
121         int (*get_temp)(struct chip_tsadc_table table,
122                         int chn, void __iomem *reg, int *temp);
123         void (*set_alarm_temp)(struct chip_tsadc_table table,
124                                int chn, void __iomem *reg, int temp);
125         void (*set_tshut_temp)(struct chip_tsadc_table table,
126                                int chn, void __iomem *reg, int temp);
127         void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m);
128
129         /* Per-table methods */
130         struct chip_tsadc_table table;
131 };
132
133 /**
134  * struct rockchip_thermal_sensor - hold the information of thermal sensor
135  * @thermal:  pointer to the platform/configuration data
136  * @tzd: pointer to a thermal zone
137  * @id: identifier of the thermal sensor
138  */
139 struct rockchip_thermal_sensor {
140         struct rockchip_thermal_data *thermal;
141         struct thermal_zone_device *tzd;
142         int id;
143 };
144
145 /**
146  * struct rockchip_thermal_data - hold the private data of thermal driver
147  * @chip: pointer to the platform/configuration data
148  * @pdev: platform device of thermal
149  * @reset: the reset controller of tsadc
150  * @sensors[SOC_MAX_SENSORS]: the thermal sensor
151  * @clk: the controller clock is divided by the exteral 24MHz
152  * @pclk: the advanced peripherals bus clock
153  * @grf: the general register file will be used to do static set by software
154  * @regs: the base address of tsadc controller
155  * @tshut_temp: the hardware-controlled shutdown temperature value
156  * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
157  * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
158  */
159 struct rockchip_thermal_data {
160         const struct rockchip_tsadc_chip *chip;
161         struct platform_device *pdev;
162         struct reset_control *reset;
163
164         struct rockchip_thermal_sensor sensors[SOC_MAX_SENSORS];
165
166         struct clk *clk;
167         struct clk *pclk;
168
169         struct regmap *grf;
170         void __iomem *regs;
171
172         int tshut_temp;
173         enum tshut_mode tshut_mode;
174         enum tshut_polarity tshut_polarity;
175 };
176
177 /**
178  * TSADC Sensor Register description:
179  *
180  * TSADCV2_* are used for RK3288 SoCs, the other chips can reuse it.
181  * TSADCV3_* are used for newer SoCs than RK3288. (e.g: RK3228, RK3399)
182  *
183  */
184 #define TSADCV2_USER_CON                        0x00
185 #define TSADCV2_AUTO_CON                        0x04
186 #define TSADCV2_INT_EN                          0x08
187 #define TSADCV2_INT_PD                          0x0c
188 #define TSADCV2_DATA(chn)                       (0x20 + (chn) * 0x04)
189 #define TSADCV2_COMP_INT(chn)                   (0x30 + (chn) * 0x04)
190 #define TSADCV2_COMP_SHUT(chn)                  (0x40 + (chn) * 0x04)
191 #define TSADCV2_HIGHT_INT_DEBOUNCE              0x60
192 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE            0x64
193 #define TSADCV2_AUTO_PERIOD                     0x68
194 #define TSADCV2_AUTO_PERIOD_HT                  0x6c
195
196 #define TSADCV2_AUTO_EN                         BIT(0)
197 #define TSADCV2_AUTO_SRC_EN(chn)                BIT(4 + (chn))
198 #define TSADCV2_AUTO_TSHUT_POLARITY_HIGH        BIT(8)
199
200 #define TSADCV3_AUTO_Q_SEL_EN                   BIT(1)
201
202 #define TSADCV2_INT_SRC_EN(chn)                 BIT(chn)
203 #define TSADCV2_SHUT_2GPIO_SRC_EN(chn)          BIT(4 + (chn))
204 #define TSADCV2_SHUT_2CRU_SRC_EN(chn)           BIT(8 + (chn))
205
206 #define TSADCV2_INT_PD_CLEAR_MASK               ~BIT(8)
207 #define TSADCV3_INT_PD_CLEAR_MASK               ~BIT(16)
208
209 #define TSADCV2_DATA_MASK                       0xfff
210 #define TSADCV3_DATA_MASK                       0x3ff
211
212 #define TSADCV2_HIGHT_INT_DEBOUNCE_COUNT        4
213 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT      4
214 #define TSADCV2_AUTO_PERIOD_TIME                250 /* msec */
215 #define TSADCV2_AUTO_PERIOD_HT_TIME             50  /* msec */
216 #define TSADCV2_USER_INTER_PD_SOC               0x340 /* 13 clocks */
217
218 #define GRF_SARADC_TESTBIT                      0x0e644
219 #define GRF_TSADC_TESTBIT_L                     0x0e648
220 #define GRF_TSADC_TESTBIT_H                     0x0e64c
221
222 #define GRF_TSADC_TSEN_PD_ON                    (0x30003 << 0)
223 #define GRF_TSADC_TSEN_PD_OFF                   (0x30000 << 0)
224 #define GRF_SARADC_TESTBIT_ON                   (0x10001 << 2)
225 #define GRF_TSADC_TESTBIT_H_ON                  (0x10001 << 2)
226
227 /**
228  * struct tsadc_table - code to temperature conversion table
229  * @code: the value of adc channel
230  * @temp: the temperature
231  * Note:
232  * code to temperature mapping of the temperature sensor is a piece wise linear
233  * curve.Any temperature, code faling between to 2 give temperatures can be
234  * linearly interpolated.
235  * Code to Temperature mapping should be updated based on manufacturer results.
236  */
237 struct tsadc_table {
238         u32 code;
239         int temp;
240 };
241
242 static const struct tsadc_table rk3228_code_table[] = {
243         {0, -40000},
244         {588, -40000},
245         {593, -35000},
246         {598, -30000},
247         {603, -25000},
248         {608, -20000},
249         {613, -15000},
250         {618, -10000},
251         {623, -5000},
252         {629, 0},
253         {634, 5000},
254         {639, 10000},
255         {644, 15000},
256         {649, 20000},
257         {654, 25000},
258         {660, 30000},
259         {665, 35000},
260         {670, 40000},
261         {675, 45000},
262         {681, 50000},
263         {686, 55000},
264         {691, 60000},
265         {696, 65000},
266         {702, 70000},
267         {707, 75000},
268         {712, 80000},
269         {717, 85000},
270         {723, 90000},
271         {728, 95000},
272         {733, 100000},
273         {738, 105000},
274         {744, 110000},
275         {749, 115000},
276         {754, 120000},
277         {760, 125000},
278         {TSADCV2_DATA_MASK, 125000},
279 };
280
281 static const struct tsadc_table rk3288_code_table[] = {
282         {TSADCV2_DATA_MASK, -40000},
283         {3800, -40000},
284         {3792, -35000},
285         {3783, -30000},
286         {3774, -25000},
287         {3765, -20000},
288         {3756, -15000},
289         {3747, -10000},
290         {3737, -5000},
291         {3728, 0},
292         {3718, 5000},
293         {3708, 10000},
294         {3698, 15000},
295         {3688, 20000},
296         {3678, 25000},
297         {3667, 30000},
298         {3656, 35000},
299         {3645, 40000},
300         {3634, 45000},
301         {3623, 50000},
302         {3611, 55000},
303         {3600, 60000},
304         {3588, 65000},
305         {3575, 70000},
306         {3563, 75000},
307         {3550, 80000},
308         {3537, 85000},
309         {3524, 90000},
310         {3510, 95000},
311         {3496, 100000},
312         {3482, 105000},
313         {3467, 110000},
314         {3452, 115000},
315         {3437, 120000},
316         {3421, 125000},
317 };
318
319 static const struct tsadc_table rk3368_code_table[] = {
320         {0, -40000},
321         {106, -40000},
322         {108, -35000},
323         {110, -30000},
324         {112, -25000},
325         {114, -20000},
326         {116, -15000},
327         {118, -10000},
328         {120, -5000},
329         {122, 0},
330         {124, 5000},
331         {126, 10000},
332         {128, 15000},
333         {130, 20000},
334         {132, 25000},
335         {134, 30000},
336         {136, 35000},
337         {138, 40000},
338         {140, 45000},
339         {142, 50000},
340         {144, 55000},
341         {146, 60000},
342         {148, 65000},
343         {150, 70000},
344         {152, 75000},
345         {154, 80000},
346         {156, 85000},
347         {158, 90000},
348         {160, 95000},
349         {162, 100000},
350         {163, 105000},
351         {165, 110000},
352         {167, 115000},
353         {169, 120000},
354         {171, 125000},
355         {TSADCV3_DATA_MASK, 125000},
356 };
357
358 static const struct tsadc_table rk3399_code_table[] = {
359         {0, -40000},
360         {402, -40000},
361         {410, -35000},
362         {419, -30000},
363         {427, -25000},
364         {436, -20000},
365         {444, -15000},
366         {453, -10000},
367         {461, -5000},
368         {470, 0},
369         {478, 5000},
370         {487, 10000},
371         {496, 15000},
372         {504, 20000},
373         {513, 25000},
374         {521, 30000},
375         {530, 35000},
376         {538, 40000},
377         {547, 45000},
378         {555, 50000},
379         {564, 55000},
380         {573, 60000},
381         {581, 65000},
382         {590, 70000},
383         {599, 75000},
384         {607, 80000},
385         {616, 85000},
386         {624, 90000},
387         {633, 95000},
388         {642, 100000},
389         {650, 105000},
390         {659, 110000},
391         {668, 115000},
392         {677, 120000},
393         {685, 125000},
394         {TSADCV3_DATA_MASK, 125000},
395 };
396
397 static u32 rk_tsadcv2_temp_to_code(struct chip_tsadc_table table,
398                                    int temp)
399 {
400         int high, low, mid;
401
402         low = 0;
403         high = table.length - 1;
404         mid = (high + low) / 2;
405
406         if (temp < table.id[low].temp || temp > table.id[high].temp)
407                 return 0;
408
409         while (low <= high) {
410                 if (temp == table.id[mid].temp)
411                         return table.id[mid].code;
412                 else if (temp < table.id[mid].temp)
413                         high = mid - 1;
414                 else
415                         low = mid + 1;
416                 mid = (low + high) / 2;
417         }
418
419         return 0;
420 }
421
422 static int rk_tsadcv2_code_to_temp(struct chip_tsadc_table table, u32 code,
423                                    int *temp)
424 {
425         unsigned int low = 1;
426         unsigned int high = table.length - 1;
427         unsigned int mid = (low + high) / 2;
428         unsigned int num;
429         unsigned long denom;
430
431         WARN_ON(table.length < 2);
432
433         switch (table.mode) {
434         case ADC_DECREMENT:
435                 code &= table.data_mask;
436                 if (code < table.id[high].code)
437                         return -EAGAIN;         /* Incorrect reading */
438
439                 while (low <= high) {
440                         if (code >= table.id[mid].code &&
441                             code < table.id[mid - 1].code)
442                                 break;
443                         else if (code < table.id[mid].code)
444                                 low = mid + 1;
445                         else
446                                 high = mid - 1;
447
448                         mid = (low + high) / 2;
449                 }
450                 break;
451         case ADC_INCREMENT:
452                 code &= table.data_mask;
453                 if (code < table.id[low].code)
454                         return -EAGAIN;         /* Incorrect reading */
455
456                 while (low <= high) {
457                         if (code <= table.id[mid].code &&
458                             code > table.id[mid - 1].code)
459                                 break;
460                         else if (code > table.id[mid].code)
461                                 low = mid + 1;
462                         else
463                                 high = mid - 1;
464
465                         mid = (low + high) / 2;
466                 }
467                 break;
468         default:
469                 pr_err("Invalid the conversion table\n");
470         }
471
472         /*
473          * The 5C granularity provided by the table is too much. Let's
474          * assume that the relationship between sensor readings and
475          * temperature between 2 table entries is linear and interpolate
476          * to produce less granular result.
477          */
478         num = table.id[mid].temp - table.id[mid - 1].temp;
479         num *= abs(table.id[mid - 1].code - code);
480         denom = abs(table.id[mid - 1].code - table.id[mid].code);
481         *temp = table.id[mid - 1].temp + (num / denom);
482
483         return 0;
484 }
485
486 /**
487  * rk_tsadcv2_initialize - initialize TASDC Controller.
488  *
489  * (1) Set TSADC_V2_AUTO_PERIOD:
490  *     Configure the interleave between every two accessing of
491  *     TSADC in normal operation.
492  *
493  * (2) Set TSADCV2_AUTO_PERIOD_HT:
494  *     Configure the interleave between every two accessing of
495  *     TSADC after the temperature is higher than COM_SHUT or COM_INT.
496  *
497  * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
498  *     If the temperature is higher than COMP_INT or COMP_SHUT for
499  *     "debounce" times, TSADC controller will generate interrupt or TSHUT.
500  */
501 static void rk_tsadcv2_initialize(struct regmap *grf, void __iomem *regs,
502                                   enum tshut_polarity tshut_polarity)
503 {
504         if (tshut_polarity == TSHUT_HIGH_ACTIVE)
505                 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
506                                regs + TSADCV2_AUTO_CON);
507         else
508                 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
509                                regs + TSADCV2_AUTO_CON);
510
511         writel_relaxed(TSADCV2_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
512         writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
513                        regs + TSADCV2_HIGHT_INT_DEBOUNCE);
514         writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
515                        regs + TSADCV2_AUTO_PERIOD_HT);
516         writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
517                        regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
518
519         if (IS_ERR(grf)) {
520                 pr_warn("%s: Missing rockchip,grf property\n", __func__);
521                 return;
522         }
523 }
524
525 /**
526  * rk_tsadcv3_initialize - initialize TASDC Controller.
527  *
528  * (1) The tsadc control power sequence.
529  *
530  * (2) Set TSADC_V2_AUTO_PERIOD:
531  *     Configure the interleave between every two accessing of
532  *     TSADC in normal operation.
533  *
534  * (2) Set TSADCV2_AUTO_PERIOD_HT:
535  *     Configure the interleave between every two accessing of
536  *     TSADC after the temperature is higher than COM_SHUT or COM_INT.
537  *
538  * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
539  *     If the temperature is higher than COMP_INT or COMP_SHUT for
540  *     "debounce" times, TSADC controller will generate interrupt or TSHUT.
541  */
542 static void rk_tsadcv3_initialize(struct regmap *grf, void __iomem *regs,
543                                   enum tshut_polarity tshut_polarity)
544 {
545         /* The tsadc control power sequence */
546         if (IS_ERR(grf)) {
547                 /* Set interleave value to workround ic time sync issue */
548                 writel_relaxed(TSADCV2_USER_INTER_PD_SOC, regs +
549                                TSADCV2_USER_CON);
550         } else {
551                 regmap_write(grf, GRF_TSADC_TESTBIT_L, GRF_TSADC_TSEN_PD_ON);
552                 mdelay(10);
553                 regmap_write(grf, GRF_TSADC_TESTBIT_L, GRF_TSADC_TSEN_PD_OFF);
554                 udelay(100); /* The spec note says at least 15 us */
555                 regmap_write(grf, GRF_SARADC_TESTBIT, GRF_SARADC_TESTBIT_ON);
556                 regmap_write(grf, GRF_TSADC_TESTBIT_H, GRF_TSADC_TESTBIT_H_ON);
557                 udelay(200); /* The spec note says at least 90 us */
558         }
559
560         if (tshut_polarity == TSHUT_HIGH_ACTIVE)
561                 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
562                                regs + TSADCV2_AUTO_CON);
563         else
564                 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
565                                regs + TSADCV2_AUTO_CON);
566
567         writel_relaxed(TSADCV2_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
568         writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
569                        regs + TSADCV2_HIGHT_INT_DEBOUNCE);
570         writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
571                        regs + TSADCV2_AUTO_PERIOD_HT);
572         writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
573                        regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
574 }
575
576 static void rk_tsadcv2_irq_ack(void __iomem *regs)
577 {
578         u32 val;
579
580         val = readl_relaxed(regs + TSADCV2_INT_PD);
581         writel_relaxed(val & TSADCV2_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
582 }
583
584 static void rk_tsadcv3_irq_ack(void __iomem *regs)
585 {
586         u32 val;
587
588         val = readl_relaxed(regs + TSADCV2_INT_PD);
589         writel_relaxed(val & TSADCV3_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
590 }
591
592 static void rk_tsadcv2_control(void __iomem *regs, bool enable)
593 {
594         u32 val;
595
596         val = readl_relaxed(regs + TSADCV2_AUTO_CON);
597         if (enable)
598                 val |= TSADCV2_AUTO_EN;
599         else
600                 val &= ~TSADCV2_AUTO_EN;
601
602         writel_relaxed(val, regs + TSADCV2_AUTO_CON);
603 }
604
605 /**
606  * rk_tsadcv3_control - the tsadc controller is enabled or disabled.
607  *
608  * NOTE: TSADC controller works at auto mode, and some SoCs need set the
609  * tsadc_q_sel bit on TSADCV2_AUTO_CON[1]. The (1024 - tsadc_q) as output
610  * adc value if setting this bit to enable.
611  */
612 static void rk_tsadcv3_control(void __iomem *regs, bool enable)
613 {
614         u32 val;
615
616         val = readl_relaxed(regs + TSADCV2_AUTO_CON);
617         if (enable)
618                 val |= TSADCV2_AUTO_EN | TSADCV3_AUTO_Q_SEL_EN;
619         else
620                 val &= ~TSADCV2_AUTO_EN;
621
622         writel_relaxed(val, regs + TSADCV2_AUTO_CON);
623 }
624
625 static int rk_tsadcv2_get_temp(struct chip_tsadc_table table,
626                                int chn, void __iomem *regs, int *temp)
627 {
628         u32 val;
629
630         val = readl_relaxed(regs + TSADCV2_DATA(chn));
631
632         return rk_tsadcv2_code_to_temp(table, val, temp);
633 }
634
635 static void rk_tsadcv2_alarm_temp(struct chip_tsadc_table table,
636                                   int chn, void __iomem *regs, int temp)
637 {
638         u32 alarm_value, int_en;
639
640         alarm_value = rk_tsadcv2_temp_to_code(table, temp);
641         writel_relaxed(alarm_value & table.data_mask,
642                        regs + TSADCV2_COMP_INT(chn));
643
644         int_en = readl_relaxed(regs + TSADCV2_INT_EN);
645         int_en |= TSADCV2_INT_SRC_EN(chn);
646         writel_relaxed(int_en, regs + TSADCV2_INT_EN);
647 }
648
649 static void rk_tsadcv2_tshut_temp(struct chip_tsadc_table table,
650                                   int chn, void __iomem *regs, int temp)
651 {
652         u32 tshut_value, val;
653
654         tshut_value = rk_tsadcv2_temp_to_code(table, temp);
655         writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn));
656
657         /* TSHUT will be valid */
658         val = readl_relaxed(regs + TSADCV2_AUTO_CON);
659         writel_relaxed(val | TSADCV2_AUTO_SRC_EN(chn), regs + TSADCV2_AUTO_CON);
660 }
661
662 static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs,
663                                   enum tshut_mode mode)
664 {
665         u32 val;
666
667         val = readl_relaxed(regs + TSADCV2_INT_EN);
668         if (mode == TSHUT_MODE_GPIO) {
669                 val &= ~TSADCV2_SHUT_2CRU_SRC_EN(chn);
670                 val |= TSADCV2_SHUT_2GPIO_SRC_EN(chn);
671         } else {
672                 val &= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn);
673                 val |= TSADCV2_SHUT_2CRU_SRC_EN(chn);
674         }
675
676         writel_relaxed(val, regs + TSADCV2_INT_EN);
677 }
678
679 static const struct rockchip_tsadc_chip rk3228_tsadc_data = {
680         .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
681         .chn_num = 1, /* one channel for tsadc */
682
683         .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
684         .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
685         .tshut_temp = 95000,
686
687         .initialize = rk_tsadcv2_initialize,
688         .irq_ack = rk_tsadcv3_irq_ack,
689         .control = rk_tsadcv3_control,
690         .get_temp = rk_tsadcv2_get_temp,
691         .set_alarm_temp = rk_tsadcv2_alarm_temp,
692         .set_tshut_temp = rk_tsadcv2_tshut_temp,
693         .set_tshut_mode = rk_tsadcv2_tshut_mode,
694
695         .table = {
696                 .id = rk3228_code_table,
697                 .length = ARRAY_SIZE(rk3228_code_table),
698                 .data_mask = TSADCV3_DATA_MASK,
699                 .mode = ADC_INCREMENT,
700         },
701 };
702
703 static const struct rockchip_tsadc_chip rk3288_tsadc_data = {
704         .chn_id[SENSOR_CPU] = 1, /* cpu sensor is channel 1 */
705         .chn_id[SENSOR_GPU] = 2, /* gpu sensor is channel 2 */
706         .chn_num = 2, /* two channels for tsadc */
707
708         .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
709         .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
710         .tshut_temp = 95000,
711
712         .initialize = rk_tsadcv2_initialize,
713         .irq_ack = rk_tsadcv2_irq_ack,
714         .control = rk_tsadcv2_control,
715         .get_temp = rk_tsadcv2_get_temp,
716         .set_alarm_temp = rk_tsadcv2_alarm_temp,
717         .set_tshut_temp = rk_tsadcv2_tshut_temp,
718         .set_tshut_mode = rk_tsadcv2_tshut_mode,
719
720         .table = {
721                 .id = rk3288_code_table,
722                 .length = ARRAY_SIZE(rk3288_code_table),
723                 .data_mask = TSADCV2_DATA_MASK,
724                 .mode = ADC_DECREMENT,
725         },
726 };
727
728 static const struct rockchip_tsadc_chip rk3366_tsadc_data = {
729         .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
730         .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
731         .chn_num = 2, /* two channels for tsadc */
732
733         .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
734         .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
735         .tshut_temp = 95000,
736
737         .initialize = rk_tsadcv3_initialize,
738         .irq_ack = rk_tsadcv3_irq_ack,
739         .control = rk_tsadcv3_control,
740         .get_temp = rk_tsadcv2_get_temp,
741         .set_alarm_temp = rk_tsadcv2_alarm_temp,
742         .set_tshut_temp = rk_tsadcv2_tshut_temp,
743         .set_tshut_mode = rk_tsadcv2_tshut_mode,
744
745         .table = {
746                 .id = rk3228_code_table,
747                 .length = ARRAY_SIZE(rk3228_code_table),
748                 .data_mask = TSADCV3_DATA_MASK,
749                 .mode = ADC_INCREMENT,
750         },
751 };
752
753 static const struct rockchip_tsadc_chip rk3368_tsadc_data = {
754         .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
755         .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
756         .chn_num = 2, /* two channels for tsadc */
757
758         .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
759         .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
760         .tshut_temp = 95000,
761
762         .initialize = rk_tsadcv2_initialize,
763         .irq_ack = rk_tsadcv2_irq_ack,
764         .control = rk_tsadcv2_control,
765         .get_temp = rk_tsadcv2_get_temp,
766         .set_alarm_temp = rk_tsadcv2_alarm_temp,
767         .set_tshut_temp = rk_tsadcv2_tshut_temp,
768         .set_tshut_mode = rk_tsadcv2_tshut_mode,
769
770         .table = {
771                 .id = rk3368_code_table,
772                 .length = ARRAY_SIZE(rk3368_code_table),
773                 .data_mask = TSADCV3_DATA_MASK,
774                 .mode = ADC_INCREMENT,
775         },
776 };
777
778 static const struct rockchip_tsadc_chip rk3399_tsadc_data = {
779         .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
780         .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
781         .chn_num = 2, /* two channels for tsadc */
782
783         .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
784         .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
785         .tshut_temp = 95000,
786
787         .initialize = rk_tsadcv3_initialize,
788         .irq_ack = rk_tsadcv3_irq_ack,
789         .control = rk_tsadcv3_control,
790         .get_temp = rk_tsadcv2_get_temp,
791         .set_alarm_temp = rk_tsadcv2_alarm_temp,
792         .set_tshut_temp = rk_tsadcv2_tshut_temp,
793         .set_tshut_mode = rk_tsadcv2_tshut_mode,
794
795         .table = {
796                 .id = rk3399_code_table,
797                 .length = ARRAY_SIZE(rk3399_code_table),
798                 .data_mask = TSADCV3_DATA_MASK,
799                 .mode = ADC_INCREMENT,
800         },
801 };
802
803 static const struct of_device_id of_rockchip_thermal_match[] = {
804         {
805                 .compatible = "rockchip,rk3228-tsadc",
806                 .data = (void *)&rk3228_tsadc_data,
807         },
808         {
809                 .compatible = "rockchip,rk3288-tsadc",
810                 .data = (void *)&rk3288_tsadc_data,
811         },
812         {
813                 .compatible = "rockchip,rk3366-tsadc",
814                 .data = (void *)&rk3366_tsadc_data,
815         },
816         {
817                 .compatible = "rockchip,rk3368-tsadc",
818                 .data = (void *)&rk3368_tsadc_data,
819         },
820         {
821                 .compatible = "rockchip,rk3399-tsadc",
822                 .data = (void *)&rk3399_tsadc_data,
823         },
824         { /* end */ },
825 };
826 MODULE_DEVICE_TABLE(of, of_rockchip_thermal_match);
827
828 static void
829 rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor *sensor, bool on)
830 {
831         struct thermal_zone_device *tzd = sensor->tzd;
832
833         tzd->ops->set_mode(tzd,
834                 on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
835 }
836
837 static irqreturn_t rockchip_thermal_alarm_irq_thread(int irq, void *dev)
838 {
839         struct rockchip_thermal_data *thermal = dev;
840         int i;
841
842         dev_dbg(&thermal->pdev->dev, "thermal alarm\n");
843
844         thermal->chip->irq_ack(thermal->regs);
845
846         for (i = 0; i < thermal->chip->chn_num; i++)
847                 thermal_zone_device_update(thermal->sensors[i].tzd);
848
849         return IRQ_HANDLED;
850 }
851
852 static int rockchip_thermal_set_trips(void *_sensor, int low, int high)
853 {
854         struct rockchip_thermal_sensor *sensor = _sensor;
855         struct rockchip_thermal_data *thermal = sensor->thermal;
856         const struct rockchip_tsadc_chip *tsadc = thermal->chip;
857
858         dev_dbg(&thermal->pdev->dev, "%s: sensor %d: low: %d, high %d\n",
859                 __func__, sensor->id, low, high);
860
861         tsadc->set_alarm_temp(tsadc->table,
862                               sensor->id, thermal->regs, high);
863
864         return 0;
865 }
866
867 static int rockchip_thermal_get_temp(void *_sensor, int *out_temp)
868 {
869         struct rockchip_thermal_sensor *sensor = _sensor;
870         struct rockchip_thermal_data *thermal = sensor->thermal;
871         const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip;
872         int retval;
873
874         retval = tsadc->get_temp(tsadc->table,
875                                  sensor->id, thermal->regs, out_temp);
876         dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %d, retval: %d\n",
877                 sensor->id, *out_temp, retval);
878
879         return retval;
880 }
881
882 static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops = {
883         .get_temp = rockchip_thermal_get_temp,
884         .set_trips = rockchip_thermal_set_trips,
885 };
886
887 static int rockchip_configure_from_dt(struct device *dev,
888                                       struct device_node *np,
889                                       struct rockchip_thermal_data *thermal)
890 {
891         u32 shut_temp, tshut_mode, tshut_polarity;
892
893         if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) {
894                 dev_warn(dev,
895                          "Missing tshut temp property, using default %d\n",
896                          thermal->chip->tshut_temp);
897                 thermal->tshut_temp = thermal->chip->tshut_temp;
898         } else {
899                 if (shut_temp > INT_MAX) {
900                         dev_err(dev, "Invalid tshut temperature specified: %d\n",
901                                 shut_temp);
902                         return -ERANGE;
903                 }
904                 thermal->tshut_temp = shut_temp;
905         }
906
907         if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) {
908                 dev_warn(dev,
909                          "Missing tshut mode property, using default (%s)\n",
910                          thermal->chip->tshut_mode == TSHUT_MODE_GPIO ?
911                                 "gpio" : "cru");
912                 thermal->tshut_mode = thermal->chip->tshut_mode;
913         } else {
914                 thermal->tshut_mode = tshut_mode;
915         }
916
917         if (thermal->tshut_mode > 1) {
918                 dev_err(dev, "Invalid tshut mode specified: %d\n",
919                         thermal->tshut_mode);
920                 return -EINVAL;
921         }
922
923         if (of_property_read_u32(np, "rockchip,hw-tshut-polarity",
924                                  &tshut_polarity)) {
925                 dev_warn(dev,
926                          "Missing tshut-polarity property, using default (%s)\n",
927                          thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ?
928                                 "low" : "high");
929                 thermal->tshut_polarity = thermal->chip->tshut_polarity;
930         } else {
931                 thermal->tshut_polarity = tshut_polarity;
932         }
933
934         if (thermal->tshut_polarity > 1) {
935                 dev_err(dev, "Invalid tshut-polarity specified: %d\n",
936                         thermal->tshut_polarity);
937                 return -EINVAL;
938         }
939
940         /* The tsadc wont to handle the error in here since some SoCs didn't
941          * need this property.
942          */
943         thermal->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
944
945         return 0;
946 }
947
948 static int
949 rockchip_thermal_register_sensor(struct platform_device *pdev,
950                                  struct rockchip_thermal_data *thermal,
951                                  struct rockchip_thermal_sensor *sensor,
952                                  int id)
953 {
954         const struct rockchip_tsadc_chip *tsadc = thermal->chip;
955         int error;
956
957         tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode);
958         tsadc->set_tshut_temp(tsadc->table, id, thermal->regs,
959                               thermal->tshut_temp);
960
961         sensor->thermal = thermal;
962         sensor->id = id;
963         sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev, id,
964                                         sensor, &rockchip_of_thermal_ops);
965         if (IS_ERR(sensor->tzd)) {
966                 error = PTR_ERR(sensor->tzd);
967                 dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
968                         id, error);
969                 return error;
970         }
971
972         return 0;
973 }
974
975 /**
976  * Reset TSADC Controller, reset all tsadc registers.
977  */
978 static void rockchip_thermal_reset_controller(struct reset_control *reset)
979 {
980         reset_control_assert(reset);
981         usleep_range(10, 20);
982         reset_control_deassert(reset);
983 }
984
985 static int rockchip_thermal_probe(struct platform_device *pdev)
986 {
987         struct device_node *np = pdev->dev.of_node;
988         struct rockchip_thermal_data *thermal;
989         const struct of_device_id *match;
990         struct resource *res;
991         int irq;
992         int i;
993         int error;
994
995         match = of_match_node(of_rockchip_thermal_match, np);
996         if (!match)
997                 return -ENXIO;
998
999         irq = platform_get_irq(pdev, 0);
1000         if (irq < 0) {
1001                 dev_err(&pdev->dev, "no irq resource?\n");
1002                 return -EINVAL;
1003         }
1004
1005         thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data),
1006                                GFP_KERNEL);
1007         if (!thermal)
1008                 return -ENOMEM;
1009
1010         thermal->pdev = pdev;
1011
1012         thermal->chip = (const struct rockchip_tsadc_chip *)match->data;
1013         if (!thermal->chip)
1014                 return -EINVAL;
1015
1016         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1017         thermal->regs = devm_ioremap_resource(&pdev->dev, res);
1018         if (IS_ERR(thermal->regs))
1019                 return PTR_ERR(thermal->regs);
1020
1021         thermal->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb");
1022         if (IS_ERR(thermal->reset)) {
1023                 error = PTR_ERR(thermal->reset);
1024                 dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
1025                 return error;
1026         }
1027
1028         thermal->clk = devm_clk_get(&pdev->dev, "tsadc");
1029         if (IS_ERR(thermal->clk)) {
1030                 error = PTR_ERR(thermal->clk);
1031                 dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
1032                 return error;
1033         }
1034
1035         thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
1036         if (IS_ERR(thermal->pclk)) {
1037                 error = PTR_ERR(thermal->pclk);
1038                 dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
1039                         error);
1040                 return error;
1041         }
1042
1043         error = clk_prepare_enable(thermal->clk);
1044         if (error) {
1045                 dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
1046                         error);
1047                 return error;
1048         }
1049
1050         error = clk_prepare_enable(thermal->pclk);
1051         if (error) {
1052                 dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
1053                 goto err_disable_clk;
1054         }
1055
1056         rockchip_thermal_reset_controller(thermal->reset);
1057
1058         error = rockchip_configure_from_dt(&pdev->dev, np, thermal);
1059         if (error) {
1060                 dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
1061                         error);
1062                 goto err_disable_pclk;
1063         }
1064
1065         thermal->chip->initialize(thermal->grf, thermal->regs,
1066                                   thermal->tshut_polarity);
1067
1068         for (i = 0; i < thermal->chip->chn_num; i++) {
1069                 error = rockchip_thermal_register_sensor(pdev, thermal,
1070                                                 &thermal->sensors[i],
1071                                                 thermal->chip->chn_id[i]);
1072                 if (error) {
1073                         dev_err(&pdev->dev,
1074                                 "failed to register sensor[%d] : error = %d\n",
1075                                 i, error);
1076                         goto err_disable_pclk;
1077                 }
1078         }
1079
1080         error = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1081                                           &rockchip_thermal_alarm_irq_thread,
1082                                           IRQF_ONESHOT,
1083                                           "rockchip_thermal", thermal);
1084         if (error) {
1085                 dev_err(&pdev->dev,
1086                         "failed to request tsadc irq: %d\n", error);
1087                 goto err_disable_pclk;
1088         }
1089
1090         thermal->chip->control(thermal->regs, true);
1091
1092         for (i = 0; i < thermal->chip->chn_num; i++)
1093                 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
1094
1095         platform_set_drvdata(pdev, thermal);
1096
1097         return 0;
1098
1099 err_disable_pclk:
1100         clk_disable_unprepare(thermal->pclk);
1101 err_disable_clk:
1102         clk_disable_unprepare(thermal->clk);
1103
1104         return error;
1105 }
1106
1107 static int rockchip_thermal_remove(struct platform_device *pdev)
1108 {
1109         struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
1110         int i;
1111
1112         for (i = 0; i < thermal->chip->chn_num; i++) {
1113                 struct rockchip_thermal_sensor *sensor = &thermal->sensors[i];
1114
1115                 rockchip_thermal_toggle_sensor(sensor, false);
1116         }
1117
1118         thermal->chip->control(thermal->regs, false);
1119
1120         clk_disable_unprepare(thermal->pclk);
1121         clk_disable_unprepare(thermal->clk);
1122
1123         return 0;
1124 }
1125
1126 static int __maybe_unused rockchip_thermal_suspend(struct device *dev)
1127 {
1128         struct platform_device *pdev = to_platform_device(dev);
1129         struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
1130         int i;
1131
1132         for (i = 0; i < thermal->chip->chn_num; i++)
1133                 rockchip_thermal_toggle_sensor(&thermal->sensors[i], false);
1134
1135         thermal->chip->control(thermal->regs, false);
1136
1137         clk_disable(thermal->pclk);
1138         clk_disable(thermal->clk);
1139
1140         pinctrl_pm_select_sleep_state(dev);
1141
1142         return 0;
1143 }
1144
1145 static int __maybe_unused rockchip_thermal_resume(struct device *dev)
1146 {
1147         struct platform_device *pdev = to_platform_device(dev);
1148         struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
1149         int i;
1150         int error;
1151
1152         error = clk_enable(thermal->clk);
1153         if (error)
1154                 return error;
1155
1156         error = clk_enable(thermal->pclk);
1157         if (error) {
1158                 clk_disable(thermal->clk);
1159                 return error;
1160         }
1161
1162         rockchip_thermal_reset_controller(thermal->reset);
1163
1164         thermal->chip->initialize(thermal->grf, thermal->regs,
1165                                   thermal->tshut_polarity);
1166
1167         for (i = 0; i < thermal->chip->chn_num; i++) {
1168                 int id = thermal->sensors[i].id;
1169
1170                 thermal->chip->set_tshut_mode(id, thermal->regs,
1171                                               thermal->tshut_mode);
1172                 thermal->chip->set_tshut_temp(thermal->chip->table,
1173                                               id, thermal->regs,
1174                                               thermal->tshut_temp);
1175         }
1176
1177         thermal->chip->control(thermal->regs, true);
1178
1179         for (i = 0; i < thermal->chip->chn_num; i++)
1180                 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
1181
1182         pinctrl_pm_select_default_state(dev);
1183
1184         return 0;
1185 }
1186
1187 static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops,
1188                          rockchip_thermal_suspend, rockchip_thermal_resume);
1189
1190 static struct platform_driver rockchip_thermal_driver = {
1191         .driver = {
1192                 .name = "rockchip-thermal",
1193                 .pm = &rockchip_thermal_pm_ops,
1194                 .of_match_table = of_rockchip_thermal_match,
1195         },
1196         .probe = rockchip_thermal_probe,
1197         .remove = rockchip_thermal_remove,
1198 };
1199
1200 module_platform_driver(rockchip_thermal_driver);
1201
1202 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
1203 MODULE_AUTHOR("Rockchip, Inc.");
1204 MODULE_LICENSE("GPL v2");
1205 MODULE_ALIAS("platform:rockchip-thermal");