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