thermal: rockchip: add rk3368 support
[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, u8 *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         *ajust_code = buf[0];
286         kfree(buf);
287
288         return 0;
289 }
290
291 static struct rk3368_thermal_data *rk3368_thermal_get_data(void)
292 {
293         WARN_ON(!thermal_ctx);
294         return thermal_ctx;
295 }
296
297 static int rk3368_temp_to_code(const struct chip_tsadc_table *tmp_table,
298                                  long temp, u32 *code)
299 {
300         unsigned int low = 1;
301         unsigned int high = tmp_table->length - 1;
302         unsigned int mid = (low + high) / 2;
303         unsigned int num;
304         unsigned long denom;
305         *code = tmp_table->data_mask;
306
307         WARN_ON(tmp_table->length < 2);
308
309         if (temp < tmp_table->id[low].temp)
310                 return -EAGAIN; /* Incorrect reading */
311
312         while (low <= high) {
313                 if (temp == tmp_table->id[mid].temp) {
314                         *code = tmp_table->id[mid].code;
315                         break;
316                 } else if (temp > tmp_table->id[mid].temp) {
317                         low = mid + 1;
318                 } else {
319                         high = mid - 1;
320                 }
321
322                 mid = (low + high) / 2;
323         }
324         /*
325          * The 5C granularity provided by the table is too much. Let's
326          * assume that the relationship between sensor readings and
327          * temperature between 2 table entries is linear and interpolate
328          * to produce less granular result.
329          */
330         if (*code == tmp_table->data_mask) {
331                 num = abs(tmp_table->id[low].code - tmp_table->id[high].code);
332                 num *= abs(tmp_table->id[high].temp - temp);
333                 denom = abs(tmp_table->id[high].temp - tmp_table->id[low].temp);
334                 *code = tmp_table->id[high].code + (num / denom);
335         }
336
337         return 0;
338 }
339
340 static int rk3368_code_to_temp(const struct chip_tsadc_table *tmp_table,
341                                  u32 code, int *temp)
342 {
343         unsigned int low = 1;
344         unsigned int high = tmp_table->length - 1;
345         unsigned int mid = (low + high) / 2;
346         unsigned int num;
347         unsigned long denom;
348         *temp = INVALID_TEMP;
349
350         WARN_ON(tmp_table->length < 2);
351
352         switch (tmp_table->mode) {
353         case ADC_DECREMENT:
354                 code &= tmp_table->data_mask;
355                 if (code < tmp_table->id[high].code)
356                         return -EAGAIN; /* Incorrect reading */
357
358                 while (low <= high) {
359                         if (code == tmp_table->id[mid].code) {
360                                 *temp = tmp_table->id[mid].temp;
361                                 break;
362                         } else if (code < tmp_table->id[mid].code) {
363                                 low = mid + 1;
364                         } else {
365                                 high = mid - 1;
366                         }
367
368                         mid = (low + high) / 2;
369                 }
370                 break;
371         case ADC_INCREMENT:
372                 code &= tmp_table->data_mask;
373                 if (code < tmp_table->id[low].code)
374                         return -EAGAIN; /* Incorrect reading */
375
376                 while (low <= high) {
377                         if (code == tmp_table->id[mid].code) {
378                                 *temp = tmp_table->id[mid].temp;
379                                 break;
380                         } else if (code > tmp_table->id[mid].code) {
381                                 low = mid + 1;
382                         } else {
383                                 high = mid - 1;
384                         }
385
386                         mid = (low + high) / 2;
387                 }
388                 break;
389         default:
390                 pr_err("Invalid the conversion table\n");
391         }
392
393         /*
394          * The 5C granularity provided by the table is too much. Let's
395          * assume that the relationship between sensor readings and
396          * temperature between 2 table entries is linear and interpolate
397          * to produce less granular result.
398          */
399         if (*temp == INVALID_TEMP) {
400                 num = abs(tmp_table->id[low].temp - tmp_table->id[high].temp);
401                 num *= abs(tmp_table->id[high].code - code);
402                 denom = abs(tmp_table->id[high].code - tmp_table->id[low].code);
403                 *temp = tmp_table->id[high].temp + (num / denom);
404         }
405
406         return 0;
407 }
408
409 static const struct rk3368_tsadc_chip rk3368_tsadc_data = {
410         .tshut_mode = TSHUT_MODE_GPIO,  /* default TSHUT via GPIO give PMIC */
411         .tshut_polarity = TSHUT_LOW_ACTIVE,     /* default TSHUT LOW ACTIVE */
412         .hw_shut_temp = 125000,
413         .mode = TSHUT_USER_MODE,
414         .chn_num = 2,
415         .chn_id[0] = 0,
416         .chn_id[1] = 1,
417         .temp_table = &tsadc_table_3368,
418 };
419
420 static int rk3368_configure_from_dt(struct device *dev,
421                                       struct device_node *np,
422                                       struct rk3368_thermal_data *thermal)
423 {
424         u32 shut_temp;
425         u32 rate;
426         int ret;
427
428         if (of_property_read_u32(np, "clock-frequency", &rate)) {
429                 dev_err(dev, "Missing clock-frequency property in the DT.\n");
430                 return -EINVAL;
431         }
432         ret = clk_set_rate(thermal->clk, rate);
433
434         if (of_property_read_u32(np, "hw-shut-temp", &shut_temp)) {
435                 dev_warn(dev,
436                          "Missing tshut temp property, using default %ld\n",
437                          thermal->chip->hw_shut_temp);
438                 thermal->hw_shut_temp = thermal->chip->hw_shut_temp;
439         } else {
440                 thermal->hw_shut_temp = shut_temp;
441         }
442
443         if (thermal->hw_shut_temp > INT_MAX) {
444                 dev_err(dev, "Invalid tshut temperature specified: %ld\n",
445                         thermal->hw_shut_temp);
446                 return -ERANGE;
447         }
448
449         return 0;
450 }
451
452 static int predict_temp(int temp)
453 {
454         int cov_q = 18;
455         int cov_r = 542;
456
457         int gain;
458         int temp_mid;
459         int temp_now;
460         int prob_mid;
461         int prob_now;
462         static int temp_last = 25;
463         static int prob_last = 20;
464         static int bounding_cnt;
465
466         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
467
468         if (!ctx)
469                 return INVALID_TEMP;
470
471         if (bounding_cnt++ > START_BOUNDING_COUNT) {
472                 bounding_cnt = START_BOUNDING_COUNT;
473                 if (temp - temp_last > HIGHER_BOUNDING_TEMP)
474                         temp = temp_last + HIGHER_BOUNDING_TEMP / 3;
475                 if (temp_last - temp > LOWER_BOUNDING_TEMP)
476                         temp = temp_last - LOWER_BOUNDING_TEMP / 3;
477         }
478
479         temp_mid = temp_last;
480         prob_mid = prob_last + cov_q;
481         gain = (prob_mid * BASE) / (prob_mid + cov_r);
482
483         temp_now = temp_mid + (gain * (temp - temp_mid) >> BASE_SHIFT);
484         prob_now = ((BASE - gain) * prob_mid) >> BASE_SHIFT;
485
486         prob_last = prob_now;
487         temp_last = temp_now;
488
489         if (ctx->logout)
490                 pr_info("prob_now %d, temp_last %d, temp %d gain %d", prob_now,
491                         temp_now, temp, gain);
492
493         return temp_last;
494 }
495
496 static int get_raw_code_internal(void)
497 {
498         u32 val_cpu_pd;
499         int val_cpu;
500         int i;
501         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
502
503         if (!ctx)
504                 return INVALID_TEMP;
505
506         /* power up, channel 0 */
507         writel_relaxed(0x18, ctx->regs + TSADCV2_USER_CON);
508
509         udelay(TSADC_CLK_CYCLE_TIME * 2);
510         /* start working */
511         writel_relaxed(0x38, ctx->regs + TSADCV2_USER_CON);
512         udelay(TSADC_CLK_CYCLE_TIME * 13);
513
514         /* try 50 times */
515         for (i = 0; i < 50; i++) {
516                 udelay(TSADC_CLK_CYCLE_TIME);
517                 val_cpu_pd = readl_relaxed(ctx->regs + TSADCV2_INT_PD);
518
519                 if ((val_cpu_pd & 0x100) == 0x100) {
520                         udelay(1);
521                         /*clear eoc inter */
522                         writel_relaxed(0x100, ctx->regs + TSADCV2_INT_PD);
523                         /*read adc data */
524                         val_cpu = readl_relaxed(ctx->regs + TSADCV2_DATA(0));
525                         break;
526                 }
527         }
528         /*power down, channel 0 */
529         writel_relaxed(0x0, ctx->regs + TSADCV2_USER_CON);
530
531         return val_cpu;
532 }
533
534 #define RAW_CODE_MIN (50)
535 #define RAW_CODE_MAX (225)
536
537 static int rk3368_get_raw_code(void)
538 {
539         static int old_data = 130;
540         int tsadc_data = 0;
541
542         tsadc_data = get_raw_code_internal();
543
544         if ((tsadc_data < RAW_CODE_MIN) || (tsadc_data > RAW_CODE_MAX))
545                 tsadc_data = old_data;
546         else
547                 old_data = tsadc_data;
548
549         return tsadc_data;
550 }
551
552 static int rk3368_convert_code_2_temp(int tsadc_data, int voltage)
553 {
554         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
555         const struct rk3368_tsadc_chip *tsadc;
556         int out_temp;
557         static int old_temp;
558         int data_adjust;
559
560         u32 code_temp;
561         u32 tmp_code1;
562         u32 tmp_code2;
563
564         if (!ctx)
565                 return INVALID_TEMP;
566
567         tsadc = ctx->chip;
568
569         rk3368_temp_to_code(tsadc->temp_table,
570                               ctx->cpu_temp_adjust * 1000, &tmp_code1);
571         rk3368_temp_to_code(tsadc->temp_table, 0, &tmp_code2);
572         data_adjust = tmp_code1 - tmp_code2;
573         code_temp =
574             ((tsadc_data * voltage - data_adjust * 1000000) + 500000) / 1000000;
575         rk3368_code_to_temp(tsadc->temp_table, code_temp, &out_temp);
576
577         if (ctx->logout)
578                 pr_info("cpu code temp:[%d, %d], voltage: %d\n",
579                         tsadc_data, out_temp / 1000, voltage);
580
581         if ((out_temp < MIN_TEMP) || (out_temp > MAX_TEMP))
582                 out_temp = old_temp;
583         else
584                 old_temp = out_temp;
585
586         ctx->cpu_temp = out_temp / 1000;
587         return out_temp;
588 }
589
590 static int rk3368_thermal_set_trips(void *_sensor, int low, int high)
591 {
592         return 0;
593 }
594
595 static int rk3368_thermal_get_temp(void *_sensor, int *out_temp)
596 {
597         int raw_code;
598         int temp;
599         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
600         struct platform_device *pdev;
601
602         if (!ctx)
603                 return INVALID_TEMP;
604
605         pdev = ctx->pdev;
606
607         mutex_lock(&thermal_reg_mutex);
608         raw_code = rk3368_get_raw_code();
609         temp = rk3368_convert_code_2_temp(raw_code, ctx->regulator_uv);
610         *out_temp = predict_temp(temp / 1000) * 1000;
611         mutex_unlock(&thermal_reg_mutex);
612
613         return 0;
614 }
615
616 static const struct thermal_zone_of_device_ops rk3368_of_thermal_ops = {
617         .get_temp = rk3368_thermal_get_temp,
618         .set_trips = rk3368_thermal_set_trips,
619 };
620
621 static int
622 rk3368_thermal_register_sensor(struct platform_device *pdev,
623                                  struct rk3368_thermal_data *ctx,
624                                  struct rk3368_thermal_sensor *sensor, int id)
625 {
626         int error;
627
628         sensor->ctx = ctx;
629         sensor->id = id;
630         sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev, id,
631                                                            sensor,
632                                                            &rk3368_of_thermal_ops);
633         if (IS_ERR(sensor->tzd)) {
634                 error = PTR_ERR(sensor->tzd);
635                 dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
636                         id, error);
637                 return error;
638         }
639
640         return 0;
641 }
642
643 /*
644  * Reset TSADC Controller, reset all tsadc registers.
645  */
646 static void rk3368_thermal_reset_controller(struct reset_control *reset)
647 {
648         reset_control_assert(reset);
649         udelay(10);
650         reset_control_deassert(reset);
651 }
652
653 static ssize_t rk3368_thermal_temp_adjust_test_store(struct kobject *kobj,
654                                                        struct kobj_attribute
655                                                        *attr, const char *buf,
656                                                        size_t n)
657 {
658         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
659         int getdata;
660         char cmd;
661         const char *buftmp = buf;
662         int ret;
663
664         if (!ctx)
665                 return n;
666
667         ret = sscanf(buftmp, "%c ", &cmd);
668         if (ret != 1)
669                 return -EINVAL;
670
671         switch (cmd) {
672         case 'c':
673                 ret = sscanf(buftmp, "%c %d", &cmd, &getdata);
674                 if (ret != 2)
675                         return -EINVAL;
676                 ctx->cpu_temp_adjust = getdata;
677                 pr_info("get cpu_temp_adjust value = %d\n", getdata);
678
679                 break;
680         case 'g':
681                 ret = sscanf(buftmp, "%c %d", &cmd, &getdata);
682                 if (ret != 2)
683                         return -EINVAL;
684                 ctx->gpu_temp_adjust = getdata;
685                 pr_info("get gpu_temp_adjust value = %d\n", getdata);
686
687                 break;
688         default:
689                 pr_info("Unknown command\n");
690                 break;
691         }
692
693         return n;
694 }
695
696 static ssize_t rk3368_thermal_temp_adjust_test_show(struct kobject *kobj,
697                                                       struct kobj_attribute
698                                                       *attr, char *buf)
699 {
700         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
701         char *str = buf;
702
703         if (!ctx)
704                 return 0;
705
706         str +=
707             sprintf(str, "rk3368_thermal: cpu:%d, gpu:%d\n",
708                     ctx->cpu_temp_adjust, ctx->gpu_temp_adjust);
709         return (str - buf);
710 }
711
712 static ssize_t rk3368_thermal_temp_test_store(struct kobject *kobj,
713                                                 struct kobj_attribute *attr,
714                                                 const char *buf, size_t n)
715 {
716         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
717         char cmd;
718         const char *buftmp = buf;
719         int ret;
720
721         if (!ctx)
722                 return n;
723
724         ret = sscanf(buftmp, "%c", &cmd);
725         if (ret != 1)
726                 return -EINVAL;
727
728         switch (cmd) {
729         case 't':
730                 ctx->logout = true;
731                 break;
732         case 'f':
733                 ctx->logout = false;
734                 break;
735         default:
736                 pr_info("Unknown command\n");
737                 break;
738         }
739
740         return n;
741 }
742
743 static ssize_t rk3368_thermal_temp_test_show(struct kobject *kobj,
744                                                struct kobj_attribute *attr,
745                                                char *buf)
746 {
747         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
748         char *str = buf;
749
750         if (!ctx)
751                 return 0;
752
753         str += sprintf(str, "current cpu_temp:%d\n", ctx->cpu_temp);
754         return (str - buf);
755 }
756
757 struct rk3368_thermal_attribute {
758         struct attribute attr;
759         ssize_t (*show) (struct kobject *kobj, struct kobj_attribute *attr,
760                          char *buf);
761         ssize_t (*store) (struct kobject *kobj, struct kobj_attribute *attr,
762                           const char *buf, size_t n);
763 };
764
765 static struct rk3368_thermal_attribute rk3368_thermal_attrs[] = {
766         /*node_name permission show_func store_func */
767         __ATTR(temp_adjust, 0644,
768                rk3368_thermal_temp_adjust_test_show,
769                rk3368_thermal_temp_adjust_test_store),
770         __ATTR(temp, 0644, rk3368_thermal_temp_test_show,
771                rk3368_thermal_temp_test_store),
772 };
773
774 static void rk3368_dump_temperature(void)
775 {
776         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
777         struct platform_device *pdev;
778
779         if (!ctx)
780                 return;
781
782         pdev = ctx->pdev;
783
784         if (ctx->cpu_temp != INVALID_TEMP)
785                 dev_warn(&pdev->dev, "cpu channal temperature(%d C)\n",
786                          ctx->cpu_temp);
787
788         if (ctx->regs) {
789                 pr_warn("THERMAL REGS:\n");
790                 print_hex_dump(KERN_WARNING, "", DUMP_PREFIX_OFFSET,
791                                32, 4, ctx->regs, 0x88, false);
792         }
793 }
794 EXPORT_SYMBOL_GPL(rk3368_dump_temperature);
795
796 static int rk3368_thermal_panic(struct notifier_block *this,
797                                   unsigned long ev, void *ptr)
798 {
799         rk3368_dump_temperature();
800         return NOTIFY_DONE;
801 }
802
803 static struct notifier_block rk3368_thermal_panic_block = {
804         .notifier_call = rk3368_thermal_panic,
805 };
806
807 static int rk3368_thermal_notify(struct notifier_block *nb,
808                                    unsigned long event, void *data)
809 {
810         struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
811         struct platform_device *pdev;
812
813         if (!ctx)
814                 return NOTIFY_OK;
815
816         pdev = ctx->pdev;
817
818         if (event & REGULATOR_EVENT_PRE_VOLTAGE_CHANGE) {
819                 mutex_lock(&thermal_reg_mutex);
820         } else if (event & (REGULATOR_EVENT_VOLTAGE_CHANGE |
821                             REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE)) {
822                 ctx->regulator_uv = (unsigned long)data;
823                 mutex_unlock(&thermal_reg_mutex);
824         } else {
825                 return NOTIFY_OK;
826         }
827         return NOTIFY_OK;
828 }
829
830 static const struct of_device_id of_rk3368_thermal_match[] = {
831         {
832          .compatible = "rockchip,rk3368-tsadc-legacy",
833          .data = (void *)&rk3368_tsadc_data,
834          },
835
836         { /* end */ },
837 };
838 MODULE_DEVICE_TABLE(of, of_rk3368_thermal_match);
839
840 static int rk3368_thermal_probe(struct platform_device *pdev)
841 {
842         struct device_node *np = pdev->dev.of_node;
843         struct rk3368_thermal_data *ctx;
844         const struct of_device_id *match;
845         struct resource *res;
846         int irq;
847         int i, j;
848         int error;
849         int uv;
850         u8 ajust_code = 0;
851
852         match = of_match_node(of_rk3368_thermal_match, np);
853         if (!match)
854                 return -ENXIO;
855
856         irq = platform_get_irq(pdev, 0);
857         if (irq < 0) {
858                 dev_err(&pdev->dev, "no irq resource?\n");
859                 return -EINVAL;
860         }
861
862         ctx = devm_kzalloc(&pdev->dev, sizeof(struct rk3368_thermal_data),
863                            GFP_KERNEL);
864         if (!ctx)
865                 return -ENOMEM;
866
867         ctx->pdev = pdev;
868
869         ctx->chip = (const struct rk3368_tsadc_chip *)match->data;
870         if (!ctx->chip)
871                 return -EINVAL;
872
873         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
874         ctx->regs = devm_ioremap_resource(&pdev->dev, res);
875         if (IS_ERR(ctx->regs))
876                 return PTR_ERR(ctx->regs);
877
878         ctx->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb");
879         if (IS_ERR(ctx->reset)) {
880                 error = PTR_ERR(ctx->reset);
881                 dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
882                 return error;
883         }
884
885         ctx->clk = devm_clk_get(&pdev->dev, "tsadc");
886         if (IS_ERR(ctx->clk)) {
887                 error = PTR_ERR(ctx->clk);
888                 dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
889                 return error;
890         }
891
892         ctx->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
893         if (IS_ERR(ctx->pclk)) {
894                 error = PTR_ERR(ctx->pclk);
895                 dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
896                         error);
897                 return error;
898         }
899
900         error = clk_prepare_enable(ctx->clk);
901         if (error) {
902                 dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
903                         error);
904                 return error;
905         }
906
907         error = clk_prepare_enable(ctx->pclk);
908         if (error) {
909                 dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
910                 goto err_disable_clk;
911         }
912
913         rk3368_thermal_reset_controller(ctx->reset);
914
915         error = rk3368_configure_from_dt(&pdev->dev, np, ctx);
916         if (error) {
917                 dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
918                         error);
919                 goto err_disable_pclk;
920         }
921
922         thermal_ctx = ctx;
923         ctx->ref_regulator = devm_regulator_get_optional(&pdev->dev, "tsadc");
924
925         if (IS_ERR(ctx->ref_regulator)) {
926                 error = PTR_ERR(ctx->ref_regulator);
927
928                 if (error != -EPROBE_DEFER)
929                         dev_err(&pdev->dev,
930                                 "couldn't get regulator tsadc-supply\n");
931                 goto err_disable_pclk;
932         }
933
934         ctx->tsadc_nb.notifier_call = rk3368_thermal_notify;
935
936         /* register regulator notifier */
937         error =
938             regulator_register_notifier(ctx->ref_regulator, &ctx->tsadc_nb);
939         if (error) {
940                 dev_err(&pdev->dev, "regulator notifier request failed\n");
941                 goto err_disable_pclk;
942         }
943
944         uv = regulator_get_voltage(ctx->ref_regulator);
945         if (uv <= 0) {
946                 dev_WARN(&pdev->dev, "regulator get failed\n");
947                 uv = 1000000;
948         }
949
950         mutex_lock(&thermal_reg_mutex);
951         if (!ctx->regulator_uv)
952                 ctx->regulator_uv = uv;
953         mutex_unlock(&thermal_reg_mutex);
954
955         rk3368_get_ajust_code(np, &ajust_code);
956
957         ctx->cpu_temp_adjust = (int)ajust_code;
958
959         for (i = 0; i < ctx->chip->chn_num; i++) {
960                 error = rk3368_thermal_register_sensor(pdev, ctx,
961                                                          &ctx->sensors[i],
962                                                          ctx->chip->chn_id[i]);
963                 if (error) {
964                         dev_err(&pdev->dev,
965                                 "failed to register thermal sensor %d : error= %d\n",
966                                 i, error);
967                         for (j = 0; j < i; j++)
968                                 thermal_zone_of_sensor_unregister(&pdev->dev,
969                                                                   ctx->sensors[j].tzd);
970                         goto err_unreg_notifier;
971                 }
972         }
973
974         ctx->rk3368_thermal_kobj =
975             kobject_create_and_add("rk3368_thermal", NULL);
976         if (!ctx->rk3368_thermal_kobj) {
977                 error = -ENOMEM;
978                 dev_err(&pdev->dev,
979                         "failed to creat debug node : error= %d\n", error);
980                 goto err_unreg_notifier;
981         }
982
983         for (i = 0; i < ARRAY_SIZE(rk3368_thermal_attrs); i++) {
984                 error =
985                     sysfs_create_file(ctx->rk3368_thermal_kobj,
986                                       &rk3368_thermal_attrs[i].attr);
987                 if (error) {
988                         dev_err(&pdev->dev,
989                                 "failed to register thermal sensor %d : error= %d\n",
990                                 i, error);
991                         for (j = 0; j < i; j++)
992                                 sysfs_remove_file(ctx->rk3368_thermal_kobj,
993                                                   &rk3368_thermal_attrs[j].attr);
994
995                         goto err_unreg_notifier;
996                 }
997         }
998
999         platform_set_drvdata(pdev, ctx);
1000
1001         atomic_notifier_chain_register(&panic_notifier_list,
1002                                        &rk3368_thermal_panic_block);
1003
1004         ctx->cpu_temp = INVALID_TEMP;
1005
1006         pr_info("rk3368 tsadc probed successfully\n");
1007
1008         return 0;
1009
1010 err_unreg_notifier:
1011         regulator_unregister_notifier(ctx->ref_regulator, &ctx->tsadc_nb);
1012
1013 err_disable_pclk:
1014         clk_disable_unprepare(ctx->pclk);
1015 err_disable_clk:
1016         clk_disable_unprepare(ctx->clk);
1017
1018         return error;
1019 }
1020
1021 static int rk3368_thermal_remove(struct platform_device *pdev)
1022 {
1023         struct rk3368_thermal_data *ctx = platform_get_drvdata(pdev);
1024         int i;
1025
1026         for (i = 0; i < ctx->chip->chn_num; i++) {
1027                 struct rk3368_thermal_sensor *sensor = &ctx->sensors[i];
1028
1029                 thermal_zone_of_sensor_unregister(&pdev->dev, sensor->tzd);
1030         }
1031         clk_disable_unprepare(ctx->pclk);
1032         clk_disable_unprepare(ctx->clk);
1033
1034         return 0;
1035 }
1036
1037 static int __maybe_unused rk3368_thermal_suspend(struct device *dev)
1038 {
1039         struct platform_device *pdev = to_platform_device(dev);
1040         struct rk3368_thermal_data *ctx = platform_get_drvdata(pdev);
1041
1042         clk_disable(ctx->pclk);
1043         clk_disable(ctx->clk);
1044         return 0;
1045 }
1046
1047 static int __maybe_unused rk3368_thermal_resume(struct device *dev)
1048 {
1049         struct platform_device *pdev = to_platform_device(dev);
1050         struct rk3368_thermal_data *ctx = platform_get_drvdata(pdev);
1051         int error;
1052
1053         error = clk_enable(ctx->clk);
1054         if (error)
1055                 return error;
1056
1057         error = clk_enable(ctx->pclk);
1058         if (error) {
1059                 clk_disable(ctx->clk);
1060                 return error;
1061         }
1062
1063         rk3368_thermal_reset_controller(ctx->reset);
1064
1065         return 0;
1066 }
1067
1068 static SIMPLE_DEV_PM_OPS(rk3368_thermal_pm_ops,
1069                          rk3368_thermal_suspend, rk3368_thermal_resume);
1070
1071 static struct platform_driver rk3368_thermal_driver = {
1072         .driver = {
1073                 .name = "rk3368-thermal",
1074                 .pm = &rk3368_thermal_pm_ops,
1075                 .of_match_table = of_rk3368_thermal_match,
1076         },
1077         .probe = rk3368_thermal_probe,
1078         .remove = rk3368_thermal_remove,
1079 };
1080
1081 /* rk3368 thermal needs a clock source of 32k from rk818, so this init process
1082  * is postponed
1083  */
1084 static int __init rk3368_thermal_init_driver(void)
1085 {
1086         return platform_driver_register(&rk3368_thermal_driver);
1087 }
1088 late_initcall(rk3368_thermal_init_driver);
1089
1090 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
1091 MODULE_AUTHOR("Rockchip, Inc.");
1092 MODULE_LICENSE("GPL v2");
1093 MODULE_ALIAS("platform:rk3368-thermal");