thermal: rockchip: rk3368: fix efuse value of temp ajust code issue
[firefly-linux-kernel-4.4.55.git] / drivers / thermal / rk3368_thermal.c
1 /*
2  * Copyright (c) 2017, 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/regulator/consumer.h>
33 #include <linux/slab.h>
34 #include <linux/mutex.h>
35 #include <linux/nvmem-consumer.h>
36
37 /**
38  * If the temperature over a period of time High,
39  * the resulting TSHUT gave CRU module,let it reset the entire chip,
40  * or via GPIO give PMIC.
41  */
42 enum tshut_mode {
43         TSHUT_MODE_CRU = 0,
44         TSHUT_MODE_GPIO,
45 };
46
47 enum tsadc_mode {
48         TSADC_AUTO_MODE = 0,
49         TSHUT_USER_MODE,
50 };
51
52 /**
53  * the system Temperature Sensors tshut(tshut) polarity
54  * the bit 8 is tshut polarity.
55  * 0: low active, 1: high active
56  */
57 enum tshut_polarity {
58         TSHUT_LOW_ACTIVE = 0,
59         TSHUT_HIGH_ACTIVE,
60 };
61
62 #define NUM_SENSORS     2
63
64 /* TSADC V2 Sensor info define: */
65 #define TSADCV2_USER_CON                        0x00
66 #define TSADCV2_AUTO_CON                        0x04
67 #define TSADCV2_INT_EN                          0x08
68 #define TSADCV2_INT_PD                          0x0c
69 #define TSADCV2_DATA(chn)                       (0x20 + (chn) * 0x04)
70
71 #define TSADC_CLK_CYCLE_TIME        32  /* usec */
72 #define TSADCV3_DATA_MASK                       0x3ff
73
74 /**
75  * The conversion table has the adc value and temperature.
76  * ADC_DECREMENT: the adc value is of diminishing.(e.g. rk3288_code_table)
77  * ADC_INCREMENT: the adc value is incremental.(e.g. rk3368_code_table)
78  */
79 enum adc_sort_mode {
80         ADC_DECREMENT = 0,
81         ADC_INCREMENT,
82 };
83
84 #define TIME_OUT_TOTAL 2000
85 #define INVALID_EFUSE_VALUE           0xff
86
87 enum {
88         ACCESS_FORBIDDEN = 0,
89 };
90
91 #define MIN_TEMP (-40000)
92 #define MAX_TEMP (125000)
93
94 #define BASE (1024)
95 #define BASE_SHIFT (10)
96 #define START_BOUNDING_COUNT (100)
97 #define HIGHER_BOUNDING_TEMP (30)
98 #define LOWER_BOUNDING_TEMP (15)
99
100 /**
101  * struct tsadc_table - hold information about code and temp mapping
102  * @code: raw code from tsadc ip
103  * @temp: the mapping temperature
104  */
105
106 struct tsadc_table {
107         unsigned long code;
108         int temp;
109 };
110
111 /**
112  * struct chip_tsadc_table - hold information about chip-specific differences
113  * @id: conversion table
114  * @length: size of conversion table
115  * @data_mask: mask to apply on data inputs
116  * @mode: sort mode of this adc variant (incrementing or decrementing)
117  */
118 struct chip_tsadc_table {
119         const struct tsadc_table *id;
120         unsigned int length;
121         u32 data_mask;
122         enum adc_sort_mode mode;
123 };
124
125 /**
126  * struct rk3368_tsadc_chip - hold the private data of tsadc chip
127  * @chn_id[SOC_MAX_SENSORS]: the sensor id of chip correspond to the channel
128  * @chn_num: the channel number of tsadc chip
129  * @tshut_temp: the hardware-controlled shutdown temperature value
130  * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
131  * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
132  * @chip_tsadc_table: the chip-specific conversion table
133  * @get_temp: get the temperature
134  * @set_alarm_temp: set the high temperature interrupt
135  * @set_tshut_temp: set the hardware-controlled shutdown temperature
136  * @set_tshut_mode: set the hardware-controlled shutdown mode
137  */
138 struct rk3368_tsadc_chip {
139         int chn_id[NUM_SENSORS];
140         int chn_num;
141         long hw_shut_temp;
142         enum tshut_mode tshut_mode;
143         enum tsadc_mode mode;
144         enum tshut_polarity tshut_polarity;
145
146         const struct chip_tsadc_table *temp_table;
147
148         /* Per-sensor methods */
149         int (*get_temp)(const struct chip_tsadc_table *table,
150                         int chn, void __iomem *reg, int *temp);
151         void (*set_alarm_temp)(const struct chip_tsadc_table *table,
152                                int chn, void __iomem *reg, int temp);
153         void (*set_tshut_temp)(const struct chip_tsadc_table *table,
154                                int chn, void __iomem *reg, int temp);
155         void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m);
156 };
157
158 /**
159  * struct rk3368_thermal_sensor - hold the information of thermal sensor
160  * @ctx:  pointer to the platform/configuration data
161  * @tzd: pointer to a thermal zone
162  * @id: identifier of the thermal sensor
163  */
164 struct rk3368_thermal_sensor {
165         struct rk3368_thermal_data *ctx;
166         struct thermal_zone_device *tzd;
167         int id;
168 };
169
170 /**
171  * struct rk3368_thermal_data - hold the private data of thermal driver
172  * @chip: pointer to the platform/configuration data
173  * @pdev: platform device of thermal
174  * @reset: the reset controller of tsadc
175  * @sensors[SOC_MAX_SENSORS]: the thermal sensor
176  * @clk: the controller clock is divided by the external 24MHz
177  * @pclk: the advanced peripherals bus clock
178  * @regs: the base address of tsadc controller
179  * @tshut_temp: the hardware-controlled shutdown temperature value
180  * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
181  * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
182  * @cpu_temp_adjust: efuse value used to ajust the temperature
183  * @gpu_temp_adjust: efuse value used to ajust the temperature
184  * @cpu_temp: the current cpu's temperature
185  * @logout: switch to control log output or not
186  * @rk3368_thermal_kobj: node in sys fs
187  */
188 struct rk3368_thermal_data {
189         const struct rk3368_tsadc_chip *chip;
190         struct platform_device *pdev;
191         struct reset_control *reset;
192
193         struct rk3368_thermal_sensor sensors[NUM_SENSORS];
194
195         struct clk *clk;
196         struct clk *pclk;
197         void __iomem *regs;
198
199         long hw_shut_temp;
200         enum tshut_mode tshut_mode;
201         enum tshut_polarity tshut_polarity;
202
203         int cpu_temp_adjust;
204         int gpu_temp_adjust;
205         int cpu_temp;
206         bool logout;
207         struct kobject *rk3368_thermal_kobj;
208         struct regulator *ref_regulator;
209         int regulator_uv;
210         struct notifier_block tsadc_nb;
211 };
212
213 static struct rk3368_thermal_data *thermal_ctx;
214
215 static DEFINE_MUTEX(thermal_reg_mutex);
216
217 static const struct tsadc_table code_table_3368[] = {
218         {0, MIN_TEMP},
219         {106, MIN_TEMP},
220         {108, -35000},
221         {110, -30000},
222         {112, -25000},
223         {114, -20000},
224         {116, -15000},
225         {118, -10000},
226         {120, -5000},
227         {122, 0},
228         {124, 5000},
229         {126, 10000},
230         {128, 15000},
231         {130, 20000},
232         {132, 25000},
233         {134, 30000},
234         {136, 35000},
235         {138, 40000},
236         {140, 45000},
237         {142, 50000},
238         {144, 55000},
239         {146, 60000},
240         {148, 65000},
241         {150, 70000},
242         {152, 75000},
243         {154, 80000},
244         {156, 85000},
245         {158, 90000},
246         {160, 95000},
247         {162, 100000},
248         {163, 105000},
249         {165, 110000},
250         {167, 115000},
251         {169, 120000},
252         {171, MAX_TEMP},
253         {TSADCV3_DATA_MASK, MAX_TEMP},
254 };
255
256 static const struct chip_tsadc_table tsadc_table_3368 = {
257         .id = code_table_3368,
258         .length = ARRAY_SIZE(code_table_3368),
259         .data_mask = TSADCV3_DATA_MASK,
260         .mode = ADC_INCREMENT,
261 };
262
263 static int rk3368_get_ajust_code(struct device_node *np, int *ajust_code)
264 {
265         struct nvmem_cell *cell;
266         unsigned char *buf;
267         size_t len;
268
269         cell = of_nvmem_cell_get(np, "temp_adjust");
270         if (IS_ERR(cell)) {
271                 pr_err("avs failed to get temp_adjust cell\n");
272                 return PTR_ERR(cell);
273         }
274
275         buf = (unsigned char *)nvmem_cell_read(cell, &len);
276
277         nvmem_cell_put(cell);
278
279         if (IS_ERR(buf))
280                 return PTR_ERR(buf);
281
282         if (buf[0] == INVALID_EFUSE_VALUE)
283                 return -EINVAL;
284
285         if (buf[0] & 0x80)
286                 *ajust_code = -(buf[0] & 0x7f);
287         else
288                 *ajust_code = buf[0];
289
290         kfree(buf);
291
292         return 0;
293 }
294
295 static struct rk3368_thermal_data *rk3368_thermal_get_data(void)
296 {
297         WARN_ON(!thermal_ctx);
298         return thermal_ctx;
299 }
300
301 static int rk3368_temp_to_code(const struct chip_tsadc_table *tmp_table,
302                                  long temp, u32 *code)
303 {
304         unsigned int low = 1;
305         unsigned int high = tmp_table->length - 1;
306         unsigned int mid = (low + high) / 2;
307         unsigned int num;
308         unsigned long denom;
309         *code = tmp_table->data_mask;
310
311         WARN_ON(tmp_table->length < 2);
312
313         if (temp < tmp_table->id[low].temp)
314                 return -EAGAIN; /* Incorrect reading */
315
316         while (low <= high) {
317                 if (temp == tmp_table->id[mid].temp) {
318                         *code = tmp_table->id[mid].code;
319                         break;
320                 } else if (temp > tmp_table->id[mid].temp) {
321                         low = mid + 1;
322                 } else {
323                         high = mid - 1;
324                 }
325
326                 mid = (low + high) / 2;
327         }
328         /*
329          * The 5C granularity provided by the table is too much. Let's
330          * assume that the relationship between sensor readings and
331          * temperature between 2 table entries is linear and interpolate
332          * to produce less granular result.
333          */
334         if (*code == tmp_table->data_mask) {
335                 num = abs(tmp_table->id[low].code - tmp_table->id[high].code);
336                 num *= abs(tmp_table->id[high].temp - temp);
337                 denom = abs(tmp_table->id[high].temp - tmp_table->id[low].temp);
338                 *code = tmp_table->id[high].code + (num / denom);
339         }
340
341         return 0;
342 }
343
344 static int rk3368_code_to_temp(const struct chip_tsadc_table *tmp_table,
345                                  u32 code, int *temp)
346 {
347         unsigned int low = 1;
348         unsigned int high = tmp_table->length - 1;
349         unsigned int mid = (low + high) / 2;
350         unsigned int num;
351         unsigned long denom;
352         *temp = INVALID_TEMP;
353
354         WARN_ON(tmp_table->length < 2);
355
356         switch (tmp_table->mode) {
357         case ADC_DECREMENT:
358                 code &= tmp_table->data_mask;
359                 if (code < tmp_table->id[high].code)
360                         return -EAGAIN; /* Incorrect reading */
361
362                 while (low <= high) {
363                         if (code == tmp_table->id[mid].code) {
364                                 *temp = tmp_table->id[mid].temp;
365                                 break;
366                         } else if (code < tmp_table->id[mid].code) {
367                                 low = mid + 1;
368                         } else {
369                                 high = mid - 1;
370                         }
371
372                         mid = (low + high) / 2;
373                 }
374                 break;
375         case ADC_INCREMENT:
376                 code &= tmp_table->data_mask;
377                 if (code < tmp_table->id[low].code)
378                         return -EAGAIN; /* Incorrect reading */
379
380                 while (low <= high) {
381                         if (code == tmp_table->id[mid].code) {
382                                 *temp = tmp_table->id[mid].temp;
383                                 break;
384                         } else if (code > tmp_table->id[mid].code) {
385                                 low = mid + 1;
386                         } else {
387                                 high = mid - 1;
388                         }
389
390                         mid = (low + high) / 2;
391                 }
392                 break;
393         default:
394                 pr_err("Invalid the conversion table\n");
395         }
396
397         /*
398          * The 5C granularity provided by the table is too much. Let's
399          * assume that the relationship between sensor readings and
400          * temperature between 2 table entries is linear and interpolate
401          * to produce less granular result.
402          */
403         if (*temp == INVALID_TEMP) {
404                 num = abs(tmp_table->id[low].temp - tmp_table->id[high].temp);
405                 num *= abs(tmp_table->id[high].code - code);
406                 denom = abs(tmp_table->id[high].code - tmp_table->id[low].code);
407                 *temp = tmp_table->id[high].temp + (num / denom);
408         }
409
410         return 0;
411 }
412
413 static const struct rk3368_tsadc_chip rk3368_tsadc_data = {
414         .tshut_mode = TSHUT_MODE_GPIO,  /* default TSHUT via GPIO give PMIC */
415         .tshut_polarity = TSHUT_LOW_ACTIVE,     /* default TSHUT LOW ACTIVE */
416         .hw_shut_temp = 125000,
417         .mode = TSHUT_USER_MODE,
418         .chn_num = 2,
419         .chn_id[0] = 0,
420         .chn_id[1] = 1,
421         .temp_table = &tsadc_table_3368,
422 };
423
424 static int rk3368_configure_from_dt(struct device *dev,
425                                       struct device_node *np,
426                                       struct rk3368_thermal_data *thermal)
427 {
428         u32 shut_temp;
429         u32 rate;
430         int ret;
431
432         if (of_property_read_u32(np, "clock-frequency", &rate)) {
433                 dev_err(dev, "Missing clock-frequency property in the DT.\n");
434                 return -EINVAL;
435         }
436         ret = clk_set_rate(thermal->clk, rate);
437
438         if (of_property_read_u32(np, "hw-shut-temp", &shut_temp)) {
439                 dev_warn(dev,
440                          "Missing tshut temp property, using default %ld\n",
441                          thermal->chip->hw_shut_temp);
442                 thermal->hw_shut_temp = thermal->chip->hw_shut_temp;
443         } else {
444                 thermal->hw_shut_temp = shut_temp;
445         }
446
447         if (thermal->hw_shut_temp > INT_MAX) {
448                 dev_err(dev, "Invalid tshut temperature specified: %ld\n",
449                         thermal->hw_shut_temp);
450                 return -ERANGE;
451         }
452
453         return 0;
454 }
455
456 static int predict_temp(int temp)
457 {
458         int cov_q = 18;
459         int cov_r = 542;
460
461         int gain;
462         int temp_mid;
463         int temp_now;
464         int prob_mid;
465         int prob_now;
466         static int temp_last = 25;
467         static int prob_last = 20;
468         static int bounding_cnt;
469
470         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
471
472         if (!ctx)
473                 return INVALID_TEMP;
474
475         if (bounding_cnt++ > START_BOUNDING_COUNT) {
476                 bounding_cnt = START_BOUNDING_COUNT;
477                 if (temp - temp_last > HIGHER_BOUNDING_TEMP)
478                         temp = temp_last + HIGHER_BOUNDING_TEMP / 3;
479                 if (temp_last - temp > LOWER_BOUNDING_TEMP)
480                         temp = temp_last - LOWER_BOUNDING_TEMP / 3;
481         }
482
483         temp_mid = temp_last;
484         prob_mid = prob_last + cov_q;
485         gain = (prob_mid * BASE) / (prob_mid + cov_r);
486
487         temp_now = temp_mid + (gain * (temp - temp_mid) >> BASE_SHIFT);
488         prob_now = ((BASE - gain) * prob_mid) >> BASE_SHIFT;
489
490         prob_last = prob_now;
491         temp_last = temp_now;
492
493         if (ctx->logout)
494                 pr_info("prob_now %d, temp_last %d, temp %d gain %d", prob_now,
495                         temp_now, temp, gain);
496
497         return temp_last;
498 }
499
500 static int get_raw_code_internal(void)
501 {
502         u32 val_cpu_pd;
503         int val_cpu;
504         int i;
505         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
506
507         if (!ctx)
508                 return INVALID_TEMP;
509
510         /* power up, channel 0 */
511         writel_relaxed(0x18, ctx->regs + TSADCV2_USER_CON);
512
513         udelay(TSADC_CLK_CYCLE_TIME * 2);
514         /* start working */
515         writel_relaxed(0x38, ctx->regs + TSADCV2_USER_CON);
516         udelay(TSADC_CLK_CYCLE_TIME * 13);
517
518         /* try 50 times */
519         for (i = 0; i < 50; i++) {
520                 udelay(TSADC_CLK_CYCLE_TIME);
521                 val_cpu_pd = readl_relaxed(ctx->regs + TSADCV2_INT_PD);
522
523                 if ((val_cpu_pd & 0x100) == 0x100) {
524                         udelay(1);
525                         /*clear eoc inter */
526                         writel_relaxed(0x100, ctx->regs + TSADCV2_INT_PD);
527                         /*read adc data */
528                         val_cpu = readl_relaxed(ctx->regs + TSADCV2_DATA(0));
529                         break;
530                 }
531         }
532         /*power down, channel 0 */
533         writel_relaxed(0x0, ctx->regs + TSADCV2_USER_CON);
534
535         return val_cpu;
536 }
537
538 #define RAW_CODE_MIN (50)
539 #define RAW_CODE_MAX (225)
540
541 static int rk3368_get_raw_code(void)
542 {
543         static int old_data = 130;
544         int tsadc_data = 0;
545
546         tsadc_data = get_raw_code_internal();
547
548         if ((tsadc_data < RAW_CODE_MIN) || (tsadc_data > RAW_CODE_MAX))
549                 tsadc_data = old_data;
550         else
551                 old_data = tsadc_data;
552
553         return tsadc_data;
554 }
555
556 static int rk3368_convert_code_2_temp(int tsadc_data, int voltage)
557 {
558         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
559         const struct rk3368_tsadc_chip *tsadc;
560         int out_temp;
561         static int old_temp;
562         int data_adjust;
563
564         u32 code_temp;
565         u32 tmp_code1;
566         u32 tmp_code2;
567
568         if (!ctx)
569                 return INVALID_TEMP;
570
571         tsadc = ctx->chip;
572
573         rk3368_temp_to_code(tsadc->temp_table,
574                               ctx->cpu_temp_adjust * 1000, &tmp_code1);
575         rk3368_temp_to_code(tsadc->temp_table, 0, &tmp_code2);
576         data_adjust = tmp_code1 - tmp_code2;
577         code_temp =
578             ((tsadc_data * voltage - data_adjust * 1000000) + 500000) / 1000000;
579         rk3368_code_to_temp(tsadc->temp_table, code_temp, &out_temp);
580
581         if (ctx->logout)
582                 pr_info("cpu code temp:[%d, %d], voltage: %d\n",
583                         tsadc_data, out_temp / 1000, voltage);
584
585         if ((out_temp < MIN_TEMP) || (out_temp > MAX_TEMP))
586                 out_temp = old_temp;
587         else
588                 old_temp = out_temp;
589
590         ctx->cpu_temp = out_temp / 1000;
591         return out_temp;
592 }
593
594 static int rk3368_thermal_set_trips(void *_sensor, int low, int high)
595 {
596         return 0;
597 }
598
599 static int rk3368_thermal_get_temp(void *_sensor, int *out_temp)
600 {
601         int raw_code;
602         int temp;
603         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
604         struct platform_device *pdev;
605
606         if (!ctx)
607                 return INVALID_TEMP;
608
609         pdev = ctx->pdev;
610
611         mutex_lock(&thermal_reg_mutex);
612         raw_code = rk3368_get_raw_code();
613         temp = rk3368_convert_code_2_temp(raw_code, ctx->regulator_uv);
614         *out_temp = predict_temp(temp / 1000) * 1000;
615         mutex_unlock(&thermal_reg_mutex);
616
617         return 0;
618 }
619
620 static const struct thermal_zone_of_device_ops rk3368_of_thermal_ops = {
621         .get_temp = rk3368_thermal_get_temp,
622         .set_trips = rk3368_thermal_set_trips,
623 };
624
625 static int
626 rk3368_thermal_register_sensor(struct platform_device *pdev,
627                                  struct rk3368_thermal_data *ctx,
628                                  struct rk3368_thermal_sensor *sensor, int id)
629 {
630         int error;
631
632         sensor->ctx = ctx;
633         sensor->id = id;
634         sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev, id,
635                                                            sensor,
636                                                            &rk3368_of_thermal_ops);
637         if (IS_ERR(sensor->tzd)) {
638                 error = PTR_ERR(sensor->tzd);
639                 dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
640                         id, error);
641                 return error;
642         }
643
644         return 0;
645 }
646
647 /*
648  * Reset TSADC Controller, reset all tsadc registers.
649  */
650 static void rk3368_thermal_reset_controller(struct reset_control *reset)
651 {
652         reset_control_assert(reset);
653         udelay(10);
654         reset_control_deassert(reset);
655 }
656
657 static ssize_t rk3368_thermal_temp_adjust_test_store(struct kobject *kobj,
658                                                        struct kobj_attribute
659                                                        *attr, const char *buf,
660                                                        size_t n)
661 {
662         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
663         int getdata;
664         char cmd;
665         const char *buftmp = buf;
666         int ret;
667
668         if (!ctx)
669                 return n;
670
671         ret = sscanf(buftmp, "%c ", &cmd);
672         if (ret != 1)
673                 return -EINVAL;
674
675         switch (cmd) {
676         case 'c':
677                 ret = sscanf(buftmp, "%c %d", &cmd, &getdata);
678                 if (ret != 2)
679                         return -EINVAL;
680                 ctx->cpu_temp_adjust = getdata;
681                 pr_info("get cpu_temp_adjust value = %d\n", getdata);
682
683                 break;
684         case 'g':
685                 ret = sscanf(buftmp, "%c %d", &cmd, &getdata);
686                 if (ret != 2)
687                         return -EINVAL;
688                 ctx->gpu_temp_adjust = getdata;
689                 pr_info("get gpu_temp_adjust value = %d\n", getdata);
690
691                 break;
692         default:
693                 pr_info("Unknown command\n");
694                 break;
695         }
696
697         return n;
698 }
699
700 static ssize_t rk3368_thermal_temp_adjust_test_show(struct kobject *kobj,
701                                                       struct kobj_attribute
702                                                       *attr, char *buf)
703 {
704         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
705         char *str = buf;
706
707         if (!ctx)
708                 return 0;
709
710         str +=
711             sprintf(str, "rk3368_thermal: cpu:%d, gpu:%d\n",
712                     ctx->cpu_temp_adjust, ctx->gpu_temp_adjust);
713         return (str - buf);
714 }
715
716 static ssize_t rk3368_thermal_temp_test_store(struct kobject *kobj,
717                                                 struct kobj_attribute *attr,
718                                                 const char *buf, size_t n)
719 {
720         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
721         char cmd;
722         const char *buftmp = buf;
723         int ret;
724
725         if (!ctx)
726                 return n;
727
728         ret = sscanf(buftmp, "%c", &cmd);
729         if (ret != 1)
730                 return -EINVAL;
731
732         switch (cmd) {
733         case 't':
734                 ctx->logout = true;
735                 break;
736         case 'f':
737                 ctx->logout = false;
738                 break;
739         default:
740                 pr_info("Unknown command\n");
741                 break;
742         }
743
744         return n;
745 }
746
747 static ssize_t rk3368_thermal_temp_test_show(struct kobject *kobj,
748                                                struct kobj_attribute *attr,
749                                                char *buf)
750 {
751         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
752         char *str = buf;
753
754         if (!ctx)
755                 return 0;
756
757         str += sprintf(str, "current cpu_temp:%d\n", ctx->cpu_temp);
758         return (str - buf);
759 }
760
761 struct rk3368_thermal_attribute {
762         struct attribute attr;
763         ssize_t (*show) (struct kobject *kobj, struct kobj_attribute *attr,
764                          char *buf);
765         ssize_t (*store) (struct kobject *kobj, struct kobj_attribute *attr,
766                           const char *buf, size_t n);
767 };
768
769 static struct rk3368_thermal_attribute rk3368_thermal_attrs[] = {
770         /*node_name permission show_func store_func */
771         __ATTR(temp_adjust, 0644,
772                rk3368_thermal_temp_adjust_test_show,
773                rk3368_thermal_temp_adjust_test_store),
774         __ATTR(temp, 0644, rk3368_thermal_temp_test_show,
775                rk3368_thermal_temp_test_store),
776 };
777
778 static void rk3368_dump_temperature(void)
779 {
780         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
781         struct platform_device *pdev;
782
783         if (!ctx)
784                 return;
785
786         pdev = ctx->pdev;
787
788         if (ctx->cpu_temp != INVALID_TEMP)
789                 dev_warn(&pdev->dev, "cpu channal temperature(%d C)\n",
790                          ctx->cpu_temp);
791
792         if (ctx->regs) {
793                 pr_warn("THERMAL REGS:\n");
794                 print_hex_dump(KERN_WARNING, "", DUMP_PREFIX_OFFSET,
795                                32, 4, ctx->regs, 0x88, false);
796         }
797 }
798 EXPORT_SYMBOL_GPL(rk3368_dump_temperature);
799
800 static int rk3368_thermal_panic(struct notifier_block *this,
801                                   unsigned long ev, void *ptr)
802 {
803         rk3368_dump_temperature();
804         return NOTIFY_DONE;
805 }
806
807 static struct notifier_block rk3368_thermal_panic_block = {
808         .notifier_call = rk3368_thermal_panic,
809 };
810
811 static int rk3368_thermal_notify(struct notifier_block *nb,
812                                    unsigned long event, void *data)
813 {
814         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
815         struct platform_device *pdev;
816
817         if (!ctx)
818                 return NOTIFY_OK;
819
820         pdev = ctx->pdev;
821
822         if (event & REGULATOR_EVENT_PRE_VOLTAGE_CHANGE) {
823                 mutex_lock(&thermal_reg_mutex);
824         } else if (event & (REGULATOR_EVENT_VOLTAGE_CHANGE |
825                             REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE)) {
826                 ctx->regulator_uv = (unsigned long)data;
827                 mutex_unlock(&thermal_reg_mutex);
828         } else {
829                 return NOTIFY_OK;
830         }
831         return NOTIFY_OK;
832 }
833
834 static const struct of_device_id of_rk3368_thermal_match[] = {
835         {
836          .compatible = "rockchip,rk3368-tsadc-legacy",
837          .data = (void *)&rk3368_tsadc_data,
838          },
839
840         { /* end */ },
841 };
842 MODULE_DEVICE_TABLE(of, of_rk3368_thermal_match);
843
844 static int rk3368_thermal_probe(struct platform_device *pdev)
845 {
846         struct device_node *np = pdev->dev.of_node;
847         struct rk3368_thermal_data *ctx;
848         const struct of_device_id *match;
849         struct resource *res;
850         int irq;
851         int i, j;
852         int error;
853         int uv;
854         int ajust_code = 0;
855
856         match = of_match_node(of_rk3368_thermal_match, np);
857         if (!match)
858                 return -ENXIO;
859
860         irq = platform_get_irq(pdev, 0);
861         if (irq < 0) {
862                 dev_err(&pdev->dev, "no irq resource?\n");
863                 return -EINVAL;
864         }
865
866         ctx = devm_kzalloc(&pdev->dev, sizeof(struct rk3368_thermal_data),
867                            GFP_KERNEL);
868         if (!ctx)
869                 return -ENOMEM;
870
871         ctx->pdev = pdev;
872
873         ctx->chip = (const struct rk3368_tsadc_chip *)match->data;
874         if (!ctx->chip)
875                 return -EINVAL;
876
877         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
878         ctx->regs = devm_ioremap_resource(&pdev->dev, res);
879         if (IS_ERR(ctx->regs))
880                 return PTR_ERR(ctx->regs);
881
882         ctx->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb");
883         if (IS_ERR(ctx->reset)) {
884                 error = PTR_ERR(ctx->reset);
885                 dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
886                 return error;
887         }
888
889         ctx->clk = devm_clk_get(&pdev->dev, "tsadc");
890         if (IS_ERR(ctx->clk)) {
891                 error = PTR_ERR(ctx->clk);
892                 dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
893                 return error;
894         }
895
896         ctx->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
897         if (IS_ERR(ctx->pclk)) {
898                 error = PTR_ERR(ctx->pclk);
899                 dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
900                         error);
901                 return error;
902         }
903
904         error = clk_prepare_enable(ctx->clk);
905         if (error) {
906                 dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
907                         error);
908                 return error;
909         }
910
911         error = clk_prepare_enable(ctx->pclk);
912         if (error) {
913                 dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
914                 goto err_disable_clk;
915         }
916
917         rk3368_thermal_reset_controller(ctx->reset);
918
919         error = rk3368_configure_from_dt(&pdev->dev, np, ctx);
920         if (error) {
921                 dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
922                         error);
923                 goto err_disable_pclk;
924         }
925
926         thermal_ctx = ctx;
927         ctx->ref_regulator = devm_regulator_get_optional(&pdev->dev, "tsadc");
928
929         if (IS_ERR(ctx->ref_regulator)) {
930                 error = PTR_ERR(ctx->ref_regulator);
931
932                 if (error != -EPROBE_DEFER)
933                         dev_err(&pdev->dev,
934                                 "couldn't get regulator tsadc-supply\n");
935                 goto err_disable_pclk;
936         }
937
938         ctx->tsadc_nb.notifier_call = rk3368_thermal_notify;
939
940         /* register regulator notifier */
941         error =
942             regulator_register_notifier(ctx->ref_regulator, &ctx->tsadc_nb);
943         if (error) {
944                 dev_err(&pdev->dev, "regulator notifier request failed\n");
945                 goto err_disable_pclk;
946         }
947
948         uv = regulator_get_voltage(ctx->ref_regulator);
949         if (uv <= 0) {
950                 dev_WARN(&pdev->dev, "regulator get failed\n");
951                 uv = 1000000;
952         }
953
954         mutex_lock(&thermal_reg_mutex);
955         if (!ctx->regulator_uv)
956                 ctx->regulator_uv = uv;
957         mutex_unlock(&thermal_reg_mutex);
958
959         rk3368_get_ajust_code(np, &ajust_code);
960
961         ctx->cpu_temp_adjust = (int)ajust_code;
962
963         for (i = 0; i < ctx->chip->chn_num; i++) {
964                 error = rk3368_thermal_register_sensor(pdev, ctx,
965                                                          &ctx->sensors[i],
966                                                          ctx->chip->chn_id[i]);
967                 if (error) {
968                         dev_err(&pdev->dev,
969                                 "failed to register thermal sensor %d : error= %d\n",
970                                 i, error);
971                         for (j = 0; j < i; j++)
972                                 thermal_zone_of_sensor_unregister(&pdev->dev,
973                                                                   ctx->sensors[j].tzd);
974                         goto err_unreg_notifier;
975                 }
976         }
977
978         ctx->rk3368_thermal_kobj =
979             kobject_create_and_add("rk3368_thermal", NULL);
980         if (!ctx->rk3368_thermal_kobj) {
981                 error = -ENOMEM;
982                 dev_err(&pdev->dev,
983                         "failed to creat debug node : error= %d\n", error);
984                 goto err_unreg_notifier;
985         }
986
987         for (i = 0; i < ARRAY_SIZE(rk3368_thermal_attrs); i++) {
988                 error =
989                     sysfs_create_file(ctx->rk3368_thermal_kobj,
990                                       &rk3368_thermal_attrs[i].attr);
991                 if (error) {
992                         dev_err(&pdev->dev,
993                                 "failed to register thermal sensor %d : error= %d\n",
994                                 i, error);
995                         for (j = 0; j < i; j++)
996                                 sysfs_remove_file(ctx->rk3368_thermal_kobj,
997                                                   &rk3368_thermal_attrs[j].attr);
998
999                         goto err_unreg_notifier;
1000                 }
1001         }
1002
1003         platform_set_drvdata(pdev, ctx);
1004
1005         atomic_notifier_chain_register(&panic_notifier_list,
1006                                        &rk3368_thermal_panic_block);
1007
1008         ctx->cpu_temp = INVALID_TEMP;
1009
1010         pr_info("rk3368 tsadc probed successfully\n");
1011
1012         return 0;
1013
1014 err_unreg_notifier:
1015         regulator_unregister_notifier(ctx->ref_regulator, &ctx->tsadc_nb);
1016
1017 err_disable_pclk:
1018         clk_disable_unprepare(ctx->pclk);
1019 err_disable_clk:
1020         clk_disable_unprepare(ctx->clk);
1021
1022         return error;
1023 }
1024
1025 static int rk3368_thermal_remove(struct platform_device *pdev)
1026 {
1027         struct rk3368_thermal_data *ctx = platform_get_drvdata(pdev);
1028         int i;
1029
1030         for (i = 0; i < ctx->chip->chn_num; i++) {
1031                 struct rk3368_thermal_sensor *sensor = &ctx->sensors[i];
1032
1033                 thermal_zone_of_sensor_unregister(&pdev->dev, sensor->tzd);
1034         }
1035         clk_disable_unprepare(ctx->pclk);
1036         clk_disable_unprepare(ctx->clk);
1037
1038         return 0;
1039 }
1040
1041 static int __maybe_unused rk3368_thermal_suspend(struct device *dev)
1042 {
1043         struct platform_device *pdev = to_platform_device(dev);
1044         struct rk3368_thermal_data *ctx = platform_get_drvdata(pdev);
1045
1046         clk_disable(ctx->pclk);
1047         clk_disable(ctx->clk);
1048         return 0;
1049 }
1050
1051 static int __maybe_unused rk3368_thermal_resume(struct device *dev)
1052 {
1053         struct platform_device *pdev = to_platform_device(dev);
1054         struct rk3368_thermal_data *ctx = platform_get_drvdata(pdev);
1055         int error;
1056
1057         error = clk_enable(ctx->clk);
1058         if (error)
1059                 return error;
1060
1061         error = clk_enable(ctx->pclk);
1062         if (error) {
1063                 clk_disable(ctx->clk);
1064                 return error;
1065         }
1066
1067         rk3368_thermal_reset_controller(ctx->reset);
1068
1069         return 0;
1070 }
1071
1072 static SIMPLE_DEV_PM_OPS(rk3368_thermal_pm_ops,
1073                          rk3368_thermal_suspend, rk3368_thermal_resume);
1074
1075 static struct platform_driver rk3368_thermal_driver = {
1076         .driver = {
1077                 .name = "rk3368-thermal",
1078                 .pm = &rk3368_thermal_pm_ops,
1079                 .of_match_table = of_rk3368_thermal_match,
1080         },
1081         .probe = rk3368_thermal_probe,
1082         .remove = rk3368_thermal_remove,
1083 };
1084
1085 /* rk3368 thermal needs a clock source of 32k from rk818, so this init process
1086  * is postponed
1087  */
1088 static int __init rk3368_thermal_init_driver(void)
1089 {
1090         return platform_driver_register(&rk3368_thermal_driver);
1091 }
1092 late_initcall(rk3368_thermal_init_driver);
1093
1094 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
1095 MODULE_AUTHOR("Rockchip, Inc.");
1096 MODULE_LICENSE("GPL v2");
1097 MODULE_ALIAS("platform:rk3368-thermal");