blob: 2d5ba97ade08730376747b40a40913a0da7fb34a [file] [log] [blame]
Caesar Wangcbac8f632014-11-24 12:58:59 +08001/*
Caesar Wang678065d2016-04-18 11:35:58 +08002 * Copyright (c) 2014-2016, Fuzhou Rockchip Electronics Co., Ltd
Caesar Wang20f0af72015-11-09 12:48:59 +08003 * Caesar Wang <wxt@rock-chips.com>
4 *
Caesar Wangcbac8f632014-11-24 12:58:59 +08005 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/clk.h>
16#include <linux/delay.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/module.h>
20#include <linux/of.h>
21#include <linux/of_address.h>
22#include <linux/of_irq.h>
23#include <linux/platform_device.h>
Caesar Wangb9484762016-04-18 11:35:56 +080024#include <linux/regmap.h>
Caesar Wangcbac8f632014-11-24 12:58:59 +080025#include <linux/reset.h>
26#include <linux/thermal.h>
Caesar Wangb9484762016-04-18 11:35:56 +080027#include <linux/mfd/syscon.h>
Caesar Wangc9708722015-11-11 19:43:11 -080028#include <linux/pinctrl/consumer.h>
Caesar Wangcbac8f632014-11-24 12:58:59 +080029
30/**
31 * If the temperature over a period of time High,
32 * the resulting TSHUT gave CRU module,let it reset the entire chip,
33 * or via GPIO give PMIC.
34 */
35enum tshut_mode {
36 TSHUT_MODE_CRU = 0,
37 TSHUT_MODE_GPIO,
38};
39
40/**
Caesar Wang13c1cfd2015-12-03 16:48:39 +080041 * The system Temperature Sensors tshut(tshut) polarity
Caesar Wangcbac8f632014-11-24 12:58:59 +080042 * the bit 8 is tshut polarity.
43 * 0: low active, 1: high active
44 */
45enum tshut_polarity {
46 TSHUT_LOW_ACTIVE = 0,
47 TSHUT_HIGH_ACTIVE,
48};
49
50/**
Caesar Wang1d98b612015-11-05 13:17:58 +080051 * The system has two Temperature Sensors.
52 * sensor0 is for CPU, and sensor1 is for GPU.
Caesar Wangcbac8f632014-11-24 12:58:59 +080053 */
54enum sensor_id {
Caesar Wang1d98b612015-11-05 13:17:58 +080055 SENSOR_CPU = 0,
Caesar Wangcbac8f632014-11-24 12:58:59 +080056 SENSOR_GPU,
57};
58
Caesar Wang1d98b612015-11-05 13:17:58 +080059/**
Caesar Wang13c1cfd2015-12-03 16:48:39 +080060 * The conversion table has the adc value and temperature.
Caesar Wang952418a2016-02-15 15:33:30 +080061 * ADC_DECREMENT: the adc value is of diminishing.(e.g. rk3288_code_table)
62 * ADC_INCREMENT: the adc value is incremental.(e.g. rk3368_code_table)
Caesar Wang13c1cfd2015-12-03 16:48:39 +080063 */
Caesar Wang020ba952015-11-09 12:48:57 +080064enum adc_sort_mode {
65 ADC_DECREMENT = 0,
66 ADC_INCREMENT,
67};
68
69/**
Caesar Wang1d98b612015-11-05 13:17:58 +080070 * The max sensors is two in rockchip SoCs.
71 * Two sensors: CPU and GPU sensor.
72 */
73#define SOC_MAX_SENSORS 2
74
Caesar Wang13c1cfd2015-12-03 16:48:39 +080075/**
Caesar Wang678065d2016-04-18 11:35:58 +080076 * struct chip_tsadc_table - hold information about chip-specific differences
Caesar Wang13c1cfd2015-12-03 16:48:39 +080077 * @id: conversion table
78 * @length: size of conversion table
79 * @data_mask: mask to apply on data inputs
80 * @mode: sort mode of this adc variant (incrementing or decrementing)
81 */
Caesar Wangce741102015-11-09 12:48:56 +080082struct chip_tsadc_table {
83 const struct tsadc_table *id;
Caesar Wangce741102015-11-09 12:48:56 +080084 unsigned int length;
Caesar Wangce741102015-11-09 12:48:56 +080085 u32 data_mask;
Caesar Wang020ba952015-11-09 12:48:57 +080086 enum adc_sort_mode mode;
Caesar Wangce741102015-11-09 12:48:56 +080087};
88
Caesar Wang678065d2016-04-18 11:35:58 +080089/**
90 * struct rockchip_tsadc_chip - hold the private data of tsadc chip
91 * @chn_id[SOC_MAX_SENSORS]: the sensor id of chip correspond to the channel
92 * @chn_num: the channel number of tsadc chip
93 * @tshut_temp: the hardware-controlled shutdown temperature value
94 * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
95 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
96 * @initialize: SoC special initialize tsadc controller method
97 * @irq_ack: clear the interrupt
98 * @get_temp: get the temperature
Caesar Wang14848502016-06-22 16:42:05 +080099 * @set_alarm_temp: set the high temperature interrupt
Caesar Wang678065d2016-04-18 11:35:58 +0800100 * @set_tshut_temp: set the hardware-controlled shutdown temperature
101 * @set_tshut_mode: set the hardware-controlled shutdown mode
102 * @table: the chip-specific conversion table
103 */
Caesar Wangcbac8f632014-11-24 12:58:59 +0800104struct rockchip_tsadc_chip {
Caesar Wang1d98b612015-11-05 13:17:58 +0800105 /* The sensor id of chip correspond to the ADC channel */
106 int chn_id[SOC_MAX_SENSORS];
107 int chn_num;
108
Caesar Wangcbac8f632014-11-24 12:58:59 +0800109 /* The hardware-controlled tshut property */
Caesar Wang437df212015-11-09 12:48:58 +0800110 int tshut_temp;
Caesar Wangcbac8f632014-11-24 12:58:59 +0800111 enum tshut_mode tshut_mode;
112 enum tshut_polarity tshut_polarity;
113
114 /* Chip-wide methods */
Caesar Wangb9484762016-04-18 11:35:56 +0800115 void (*initialize)(struct regmap *grf,
116 void __iomem *reg, enum tshut_polarity p);
Caesar Wangcbac8f632014-11-24 12:58:59 +0800117 void (*irq_ack)(void __iomem *reg);
118 void (*control)(void __iomem *reg, bool on);
119
120 /* Per-sensor methods */
Caesar Wangce741102015-11-09 12:48:56 +0800121 int (*get_temp)(struct chip_tsadc_table table,
122 int chn, void __iomem *reg, int *temp);
Caesar Wang14848502016-06-22 16:42:05 +0800123 void (*set_alarm_temp)(struct chip_tsadc_table table,
124 int chn, void __iomem *reg, int temp);
Caesar Wangce741102015-11-09 12:48:56 +0800125 void (*set_tshut_temp)(struct chip_tsadc_table table,
Caesar Wang437df212015-11-09 12:48:58 +0800126 int chn, void __iomem *reg, int temp);
Caesar Wangcbac8f632014-11-24 12:58:59 +0800127 void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m);
Caesar Wangce741102015-11-09 12:48:56 +0800128
129 /* Per-table methods */
130 struct chip_tsadc_table table;
Caesar Wangcbac8f632014-11-24 12:58:59 +0800131};
132
Caesar Wang678065d2016-04-18 11:35:58 +0800133/**
134 * struct rockchip_thermal_sensor - hold the information of thermal sensor
135 * @thermal: pointer to the platform/configuration data
136 * @tzd: pointer to a thermal zone
137 * @id: identifier of the thermal sensor
138 */
Caesar Wangcbac8f632014-11-24 12:58:59 +0800139struct rockchip_thermal_sensor {
140 struct rockchip_thermal_data *thermal;
141 struct thermal_zone_device *tzd;
Caesar Wang1d98b612015-11-05 13:17:58 +0800142 int id;
Caesar Wangcbac8f632014-11-24 12:58:59 +0800143};
144
Caesar Wang678065d2016-04-18 11:35:58 +0800145/**
146 * struct rockchip_thermal_data - hold the private data of thermal driver
147 * @chip: pointer to the platform/configuration data
148 * @pdev: platform device of thermal
149 * @reset: the reset controller of tsadc
150 * @sensors[SOC_MAX_SENSORS]: the thermal sensor
151 * @clk: the controller clock is divided by the exteral 24MHz
152 * @pclk: the advanced peripherals bus clock
153 * @grf: the general register file will be used to do static set by software
154 * @regs: the base address of tsadc controller
155 * @tshut_temp: the hardware-controlled shutdown temperature value
156 * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
157 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
158 */
Caesar Wangcbac8f632014-11-24 12:58:59 +0800159struct rockchip_thermal_data {
160 const struct rockchip_tsadc_chip *chip;
161 struct platform_device *pdev;
162 struct reset_control *reset;
163
Caesar Wang1d98b612015-11-05 13:17:58 +0800164 struct rockchip_thermal_sensor sensors[SOC_MAX_SENSORS];
Caesar Wangcbac8f632014-11-24 12:58:59 +0800165
166 struct clk *clk;
167 struct clk *pclk;
168
Caesar Wangb9484762016-04-18 11:35:56 +0800169 struct regmap *grf;
Caesar Wangcbac8f632014-11-24 12:58:59 +0800170 void __iomem *regs;
171
Caesar Wang437df212015-11-09 12:48:58 +0800172 int tshut_temp;
Caesar Wangcbac8f632014-11-24 12:58:59 +0800173 enum tshut_mode tshut_mode;
174 enum tshut_polarity tshut_polarity;
175};
176
Caesar Wang952418a2016-02-15 15:33:30 +0800177/**
178 * TSADC Sensor Register description:
179 *
180 * TSADCV2_* are used for RK3288 SoCs, the other chips can reuse it.
181 * TSADCV3_* are used for newer SoCs than RK3288. (e.g: RK3228, RK3399)
182 *
183 */
Caesar Wangb9484762016-04-18 11:35:56 +0800184#define TSADCV2_USER_CON 0x00
Caesar Wangcbac8f632014-11-24 12:58:59 +0800185#define TSADCV2_AUTO_CON 0x04
186#define TSADCV2_INT_EN 0x08
187#define TSADCV2_INT_PD 0x0c
188#define TSADCV2_DATA(chn) (0x20 + (chn) * 0x04)
Caesar Wang14848502016-06-22 16:42:05 +0800189#define TSADCV2_COMP_INT(chn) (0x30 + (chn) * 0x04)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800190#define TSADCV2_COMP_SHUT(chn) (0x40 + (chn) * 0x04)
191#define TSADCV2_HIGHT_INT_DEBOUNCE 0x60
192#define TSADCV2_HIGHT_TSHUT_DEBOUNCE 0x64
193#define TSADCV2_AUTO_PERIOD 0x68
194#define TSADCV2_AUTO_PERIOD_HT 0x6c
195
196#define TSADCV2_AUTO_EN BIT(0)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800197#define TSADCV2_AUTO_SRC_EN(chn) BIT(4 + (chn))
198#define TSADCV2_AUTO_TSHUT_POLARITY_HIGH BIT(8)
Caesar Wang678065d2016-04-18 11:35:58 +0800199
Caesar Wang7ea38c62016-02-15 15:33:31 +0800200#define TSADCV3_AUTO_Q_SEL_EN BIT(1)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800201
202#define TSADCV2_INT_SRC_EN(chn) BIT(chn)
203#define TSADCV2_SHUT_2GPIO_SRC_EN(chn) BIT(4 + (chn))
204#define TSADCV2_SHUT_2CRU_SRC_EN(chn) BIT(8 + (chn))
205
Dmitry Torokhov452e01b2015-08-07 14:00:52 -0700206#define TSADCV2_INT_PD_CLEAR_MASK ~BIT(8)
Caesar Wang952418a2016-02-15 15:33:30 +0800207#define TSADCV3_INT_PD_CLEAR_MASK ~BIT(16)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800208
209#define TSADCV2_DATA_MASK 0xfff
Caesar Wang20f0af72015-11-09 12:48:59 +0800210#define TSADCV3_DATA_MASK 0x3ff
211
Caesar Wangcbac8f632014-11-24 12:58:59 +0800212#define TSADCV2_HIGHT_INT_DEBOUNCE_COUNT 4
213#define TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT 4
Caesar Wang46667872016-06-22 18:13:56 +0800214#define TSADCV2_AUTO_PERIOD_TIME 250 /* 250ms */
215#define TSADCV2_AUTO_PERIOD_HT_TIME 50 /* 50ms */
216#define TSADCV3_AUTO_PERIOD_TIME 187500 /* 250ms */
217#define TSADCV3_AUTO_PERIOD_HT_TIME 37500 /* 50ms */
218
Caesar Wangb9484762016-04-18 11:35:56 +0800219#define TSADCV2_USER_INTER_PD_SOC 0x340 /* 13 clocks */
220
221#define GRF_SARADC_TESTBIT 0x0e644
222#define GRF_TSADC_TESTBIT_L 0x0e648
223#define GRF_TSADC_TESTBIT_H 0x0e64c
224
225#define GRF_TSADC_TSEN_PD_ON (0x30003 << 0)
226#define GRF_TSADC_TSEN_PD_OFF (0x30000 << 0)
227#define GRF_SARADC_TESTBIT_ON (0x10001 << 2)
228#define GRF_TSADC_TESTBIT_H_ON (0x10001 << 2)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800229
Caesar Wang678065d2016-04-18 11:35:58 +0800230/**
231 * struct tsadc_table - code to temperature conversion table
232 * @code: the value of adc channel
233 * @temp: the temperature
234 * Note:
235 * code to temperature mapping of the temperature sensor is a piece wise linear
236 * curve.Any temperature, code faling between to 2 give temperatures can be
237 * linearly interpolated.
238 * Code to Temperature mapping should be updated based on manufacturer results.
239 */
Caesar Wangcbac8f632014-11-24 12:58:59 +0800240struct tsadc_table {
Dmitry Torokhovd9a241c2015-08-07 13:59:23 -0700241 u32 code;
Caesar Wang437df212015-11-09 12:48:58 +0800242 int temp;
Caesar Wangcbac8f632014-11-24 12:58:59 +0800243};
244
Caesar Wang952418a2016-02-15 15:33:30 +0800245static const struct tsadc_table rk3228_code_table[] = {
Caesar Wang7ea38c62016-02-15 15:33:31 +0800246 {0, -40000},
247 {588, -40000},
248 {593, -35000},
249 {598, -30000},
250 {603, -25000},
251 {608, -20000},
252 {613, -15000},
253 {618, -10000},
254 {623, -5000},
255 {629, 0},
256 {634, 5000},
257 {639, 10000},
258 {644, 15000},
259 {649, 20000},
260 {654, 25000},
261 {660, 30000},
262 {665, 35000},
263 {670, 40000},
264 {675, 45000},
265 {681, 50000},
266 {686, 55000},
267 {691, 60000},
268 {696, 65000},
269 {702, 70000},
270 {707, 75000},
271 {712, 80000},
272 {717, 85000},
273 {723, 90000},
274 {728, 95000},
275 {733, 100000},
276 {738, 105000},
277 {744, 110000},
278 {749, 115000},
279 {754, 120000},
280 {760, 125000},
281 {TSADCV2_DATA_MASK, 125000},
Caesar Wang7b02a5e2015-12-03 16:48:42 +0800282};
283
Caesar Wang952418a2016-02-15 15:33:30 +0800284static const struct tsadc_table rk3288_code_table[] = {
Caesar Wangcbac8f632014-11-24 12:58:59 +0800285 {TSADCV2_DATA_MASK, -40000},
286 {3800, -40000},
287 {3792, -35000},
288 {3783, -30000},
289 {3774, -25000},
290 {3765, -20000},
291 {3756, -15000},
292 {3747, -10000},
293 {3737, -5000},
294 {3728, 0},
295 {3718, 5000},
296 {3708, 10000},
297 {3698, 15000},
298 {3688, 20000},
299 {3678, 25000},
300 {3667, 30000},
301 {3656, 35000},
302 {3645, 40000},
303 {3634, 45000},
304 {3623, 50000},
305 {3611, 55000},
306 {3600, 60000},
307 {3588, 65000},
308 {3575, 70000},
309 {3563, 75000},
310 {3550, 80000},
311 {3537, 85000},
312 {3524, 90000},
313 {3510, 95000},
314 {3496, 100000},
315 {3482, 105000},
316 {3467, 110000},
317 {3452, 115000},
318 {3437, 120000},
319 {3421, 125000},
Caesar Wangcbac8f632014-11-24 12:58:59 +0800320};
321
Caesar Wang952418a2016-02-15 15:33:30 +0800322static const struct tsadc_table rk3368_code_table[] = {
Caesar Wang20f0af72015-11-09 12:48:59 +0800323 {0, -40000},
324 {106, -40000},
325 {108, -35000},
326 {110, -30000},
327 {112, -25000},
328 {114, -20000},
329 {116, -15000},
330 {118, -10000},
331 {120, -5000},
332 {122, 0},
333 {124, 5000},
334 {126, 10000},
335 {128, 15000},
336 {130, 20000},
337 {132, 25000},
338 {134, 30000},
339 {136, 35000},
340 {138, 40000},
341 {140, 45000},
342 {142, 50000},
343 {144, 55000},
344 {146, 60000},
345 {148, 65000},
346 {150, 70000},
347 {152, 75000},
348 {154, 80000},
349 {156, 85000},
350 {158, 90000},
351 {160, 95000},
352 {162, 100000},
353 {163, 105000},
354 {165, 110000},
355 {167, 115000},
356 {169, 120000},
357 {171, 125000},
358 {TSADCV3_DATA_MASK, 125000},
359};
360
Caesar Wang952418a2016-02-15 15:33:30 +0800361static const struct tsadc_table rk3399_code_table[] = {
Caesar Wang7ea38c62016-02-15 15:33:31 +0800362 {0, -40000},
Caesar Wangf762a352016-04-18 11:35:55 +0800363 {402, -40000},
364 {410, -35000},
365 {419, -30000},
366 {427, -25000},
367 {436, -20000},
368 {444, -15000},
369 {453, -10000},
370 {461, -5000},
371 {470, 0},
372 {478, 5000},
373 {487, 10000},
374 {496, 15000},
375 {504, 20000},
376 {513, 25000},
377 {521, 30000},
378 {530, 35000},
379 {538, 40000},
380 {547, 45000},
381 {555, 50000},
382 {564, 55000},
383 {573, 60000},
384 {581, 65000},
385 {590, 70000},
386 {599, 75000},
387 {607, 80000},
388 {616, 85000},
389 {624, 90000},
390 {633, 95000},
391 {642, 100000},
392 {650, 105000},
393 {659, 110000},
394 {668, 115000},
395 {677, 120000},
396 {685, 125000},
Caesar Wang7ea38c62016-02-15 15:33:31 +0800397 {TSADCV3_DATA_MASK, 125000},
Caesar Wangb0d70332015-12-03 16:48:43 +0800398};
399
Caesar Wangce741102015-11-09 12:48:56 +0800400static u32 rk_tsadcv2_temp_to_code(struct chip_tsadc_table table,
Caesar Wang437df212015-11-09 12:48:58 +0800401 int temp)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800402{
403 int high, low, mid;
404
405 low = 0;
Caesar Wangce741102015-11-09 12:48:56 +0800406 high = table.length - 1;
Caesar Wangcbac8f632014-11-24 12:58:59 +0800407 mid = (high + low) / 2;
408
Caesar Wangce741102015-11-09 12:48:56 +0800409 if (temp < table.id[low].temp || temp > table.id[high].temp)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800410 return 0;
411
412 while (low <= high) {
Caesar Wangce741102015-11-09 12:48:56 +0800413 if (temp == table.id[mid].temp)
414 return table.id[mid].code;
415 else if (temp < table.id[mid].temp)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800416 high = mid - 1;
417 else
418 low = mid + 1;
419 mid = (low + high) / 2;
420 }
421
422 return 0;
423}
424
Caesar Wangce741102015-11-09 12:48:56 +0800425static int rk_tsadcv2_code_to_temp(struct chip_tsadc_table table, u32 code,
426 int *temp)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800427{
Dmitry Torokhovd9a241c2015-08-07 13:59:23 -0700428 unsigned int low = 1;
Caesar Wangce741102015-11-09 12:48:56 +0800429 unsigned int high = table.length - 1;
Caesar Wang1e9a1ae2015-01-25 10:11:11 +0800430 unsigned int mid = (low + high) / 2;
431 unsigned int num;
432 unsigned long denom;
Caesar Wangcbac8f632014-11-24 12:58:59 +0800433
Caesar Wangce741102015-11-09 12:48:56 +0800434 WARN_ON(table.length < 2);
Caesar Wangcbac8f632014-11-24 12:58:59 +0800435
Caesar Wang020ba952015-11-09 12:48:57 +0800436 switch (table.mode) {
437 case ADC_DECREMENT:
438 code &= table.data_mask;
439 if (code < table.id[high].code)
440 return -EAGAIN; /* Incorrect reading */
Dmitry Torokhovd9a241c2015-08-07 13:59:23 -0700441
Caesar Wang020ba952015-11-09 12:48:57 +0800442 while (low <= high) {
443 if (code >= table.id[mid].code &&
444 code < table.id[mid - 1].code)
445 break;
446 else if (code < table.id[mid].code)
447 low = mid + 1;
448 else
449 high = mid - 1;
450
451 mid = (low + high) / 2;
452 }
453 break;
454 case ADC_INCREMENT:
455 code &= table.data_mask;
456 if (code < table.id[low].code)
457 return -EAGAIN; /* Incorrect reading */
458
459 while (low <= high) {
Caesar Wanga87dd792016-04-18 11:35:54 +0800460 if (code <= table.id[mid].code &&
461 code > table.id[mid - 1].code)
Caesar Wang020ba952015-11-09 12:48:57 +0800462 break;
463 else if (code > table.id[mid].code)
464 low = mid + 1;
465 else
466 high = mid - 1;
467
468 mid = (low + high) / 2;
469 }
470 break;
471 default:
472 pr_err("Invalid the conversion table\n");
Caesar Wangcbac8f632014-11-24 12:58:59 +0800473 }
474
Caesar Wang1e9a1ae2015-01-25 10:11:11 +0800475 /*
476 * The 5C granularity provided by the table is too much. Let's
477 * assume that the relationship between sensor readings and
478 * temperature between 2 table entries is linear and interpolate
479 * to produce less granular result.
480 */
Elaine Zhang1d37a032016-02-15 15:33:29 +0800481 num = table.id[mid].temp - table.id[mid - 1].temp;
Caesar Wang020ba952015-11-09 12:48:57 +0800482 num *= abs(table.id[mid - 1].code - code);
483 denom = abs(table.id[mid - 1].code - table.id[mid].code);
Caesar Wangce741102015-11-09 12:48:56 +0800484 *temp = table.id[mid - 1].temp + (num / denom);
Dmitry Torokhovd9a241c2015-08-07 13:59:23 -0700485
486 return 0;
Caesar Wangcbac8f632014-11-24 12:58:59 +0800487}
488
489/**
Caesar Wang144c5562015-11-05 13:17:59 +0800490 * rk_tsadcv2_initialize - initialize TASDC Controller.
491 *
492 * (1) Set TSADC_V2_AUTO_PERIOD:
493 * Configure the interleave between every two accessing of
494 * TSADC in normal operation.
495 *
496 * (2) Set TSADCV2_AUTO_PERIOD_HT:
497 * Configure the interleave between every two accessing of
498 * TSADC after the temperature is higher than COM_SHUT or COM_INT.
499 *
500 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
501 * If the temperature is higher than COMP_INT or COMP_SHUT for
502 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
Caesar Wangcbac8f632014-11-24 12:58:59 +0800503 */
Caesar Wangb9484762016-04-18 11:35:56 +0800504static void rk_tsadcv2_initialize(struct regmap *grf, void __iomem *regs,
Caesar Wangcbac8f632014-11-24 12:58:59 +0800505 enum tshut_polarity tshut_polarity)
506{
507 if (tshut_polarity == TSHUT_HIGH_ACTIVE)
Dmitry Torokhov452e01b2015-08-07 14:00:52 -0700508 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
Caesar Wangcbac8f632014-11-24 12:58:59 +0800509 regs + TSADCV2_AUTO_CON);
510 else
Dmitry Torokhov452e01b2015-08-07 14:00:52 -0700511 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
Caesar Wangcbac8f632014-11-24 12:58:59 +0800512 regs + TSADCV2_AUTO_CON);
513
514 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
515 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
516 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
517 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
518 regs + TSADCV2_AUTO_PERIOD_HT);
519 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
520 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
Caesar Wangb9484762016-04-18 11:35:56 +0800521
522 if (IS_ERR(grf)) {
523 pr_warn("%s: Missing rockchip,grf property\n", __func__);
524 return;
525 }
526}
527
528/**
529 * rk_tsadcv3_initialize - initialize TASDC Controller.
Caesar Wang678065d2016-04-18 11:35:58 +0800530 *
Caesar Wangb9484762016-04-18 11:35:56 +0800531 * (1) The tsadc control power sequence.
532 *
533 * (2) Set TSADC_V2_AUTO_PERIOD:
534 * Configure the interleave between every two accessing of
535 * TSADC in normal operation.
536 *
537 * (2) Set TSADCV2_AUTO_PERIOD_HT:
538 * Configure the interleave between every two accessing of
539 * TSADC after the temperature is higher than COM_SHUT or COM_INT.
540 *
541 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
542 * If the temperature is higher than COMP_INT or COMP_SHUT for
543 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
544 */
545static void rk_tsadcv3_initialize(struct regmap *grf, void __iomem *regs,
546 enum tshut_polarity tshut_polarity)
547{
548 /* The tsadc control power sequence */
549 if (IS_ERR(grf)) {
550 /* Set interleave value to workround ic time sync issue */
551 writel_relaxed(TSADCV2_USER_INTER_PD_SOC, regs +
552 TSADCV2_USER_CON);
Caesar Wang46667872016-06-22 18:13:56 +0800553
554 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME,
555 regs + TSADCV2_AUTO_PERIOD);
556 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
557 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
558 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
559 regs + TSADCV2_AUTO_PERIOD_HT);
560 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
561 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
562
Caesar Wangb9484762016-04-18 11:35:56 +0800563 } else {
564 regmap_write(grf, GRF_TSADC_TESTBIT_L, GRF_TSADC_TSEN_PD_ON);
565 mdelay(10);
566 regmap_write(grf, GRF_TSADC_TESTBIT_L, GRF_TSADC_TSEN_PD_OFF);
Caesar Wang2fe5c1b2016-05-03 10:23:50 +0800567 usleep_range(15, 100); /* The spec note says at least 15 us */
Caesar Wangb9484762016-04-18 11:35:56 +0800568 regmap_write(grf, GRF_SARADC_TESTBIT, GRF_SARADC_TESTBIT_ON);
569 regmap_write(grf, GRF_TSADC_TESTBIT_H, GRF_TSADC_TESTBIT_H_ON);
Caesar Wang2fe5c1b2016-05-03 10:23:50 +0800570 usleep_range(90, 200); /* The spec note says at least 90 us */
Caesar Wang46667872016-06-22 18:13:56 +0800571
572 writel_relaxed(TSADCV3_AUTO_PERIOD_TIME,
573 regs + TSADCV2_AUTO_PERIOD);
574 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
575 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
576 writel_relaxed(TSADCV3_AUTO_PERIOD_HT_TIME,
577 regs + TSADCV2_AUTO_PERIOD_HT);
578 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
579 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
Caesar Wangb9484762016-04-18 11:35:56 +0800580 }
581
582 if (tshut_polarity == TSHUT_HIGH_ACTIVE)
583 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
584 regs + TSADCV2_AUTO_CON);
585 else
586 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
587 regs + TSADCV2_AUTO_CON);
Caesar Wangcbac8f632014-11-24 12:58:59 +0800588}
589
590static void rk_tsadcv2_irq_ack(void __iomem *regs)
591{
592 u32 val;
593
594 val = readl_relaxed(regs + TSADCV2_INT_PD);
Dmitry Torokhov452e01b2015-08-07 14:00:52 -0700595 writel_relaxed(val & TSADCV2_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
Caesar Wangcbac8f632014-11-24 12:58:59 +0800596}
597
Caesar Wang952418a2016-02-15 15:33:30 +0800598static void rk_tsadcv3_irq_ack(void __iomem *regs)
599{
600 u32 val;
601
602 val = readl_relaxed(regs + TSADCV2_INT_PD);
603 writel_relaxed(val & TSADCV3_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
604}
605
Caesar Wangcbac8f632014-11-24 12:58:59 +0800606static void rk_tsadcv2_control(void __iomem *regs, bool enable)
607{
608 u32 val;
609
610 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
611 if (enable)
612 val |= TSADCV2_AUTO_EN;
613 else
614 val &= ~TSADCV2_AUTO_EN;
615
616 writel_relaxed(val, regs + TSADCV2_AUTO_CON);
617}
618
Caesar Wang7ea38c62016-02-15 15:33:31 +0800619/**
Caesar Wang678065d2016-04-18 11:35:58 +0800620 * rk_tsadcv3_control - the tsadc controller is enabled or disabled.
621 *
622 * NOTE: TSADC controller works at auto mode, and some SoCs need set the
623 * tsadc_q_sel bit on TSADCV2_AUTO_CON[1]. The (1024 - tsadc_q) as output
624 * adc value if setting this bit to enable.
Caesar Wang7ea38c62016-02-15 15:33:31 +0800625 */
626static void rk_tsadcv3_control(void __iomem *regs, bool enable)
627{
628 u32 val;
629
630 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
631 if (enable)
632 val |= TSADCV2_AUTO_EN | TSADCV3_AUTO_Q_SEL_EN;
633 else
634 val &= ~TSADCV2_AUTO_EN;
635
636 writel_relaxed(val, regs + TSADCV2_AUTO_CON);
637}
638
Caesar Wangce741102015-11-09 12:48:56 +0800639static int rk_tsadcv2_get_temp(struct chip_tsadc_table table,
640 int chn, void __iomem *regs, int *temp)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800641{
642 u32 val;
643
Caesar Wangcbac8f632014-11-24 12:58:59 +0800644 val = readl_relaxed(regs + TSADCV2_DATA(chn));
Caesar Wangcbac8f632014-11-24 12:58:59 +0800645
Caesar Wangce741102015-11-09 12:48:56 +0800646 return rk_tsadcv2_code_to_temp(table, val, temp);
Caesar Wangcbac8f632014-11-24 12:58:59 +0800647}
648
Caesar Wang14848502016-06-22 16:42:05 +0800649static void rk_tsadcv2_alarm_temp(struct chip_tsadc_table table,
650 int chn, void __iomem *regs, int temp)
651{
652 u32 alarm_value, int_en;
653
654 alarm_value = rk_tsadcv2_temp_to_code(table, temp);
655 writel_relaxed(alarm_value & table.data_mask,
656 regs + TSADCV2_COMP_INT(chn));
657
658 int_en = readl_relaxed(regs + TSADCV2_INT_EN);
659 int_en |= TSADCV2_INT_SRC_EN(chn);
660 writel_relaxed(int_en, regs + TSADCV2_INT_EN);
661}
662
Caesar Wangce741102015-11-09 12:48:56 +0800663static void rk_tsadcv2_tshut_temp(struct chip_tsadc_table table,
Caesar Wang437df212015-11-09 12:48:58 +0800664 int chn, void __iomem *regs, int temp)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800665{
666 u32 tshut_value, val;
667
Caesar Wangce741102015-11-09 12:48:56 +0800668 tshut_value = rk_tsadcv2_temp_to_code(table, temp);
Caesar Wangcbac8f632014-11-24 12:58:59 +0800669 writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn));
670
671 /* TSHUT will be valid */
672 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
673 writel_relaxed(val | TSADCV2_AUTO_SRC_EN(chn), regs + TSADCV2_AUTO_CON);
674}
675
676static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs,
677 enum tshut_mode mode)
678{
679 u32 val;
680
681 val = readl_relaxed(regs + TSADCV2_INT_EN);
682 if (mode == TSHUT_MODE_GPIO) {
683 val &= ~TSADCV2_SHUT_2CRU_SRC_EN(chn);
684 val |= TSADCV2_SHUT_2GPIO_SRC_EN(chn);
685 } else {
686 val &= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn);
687 val |= TSADCV2_SHUT_2CRU_SRC_EN(chn);
688 }
689
690 writel_relaxed(val, regs + TSADCV2_INT_EN);
691}
692
Caesar Wang7b02a5e2015-12-03 16:48:42 +0800693static const struct rockchip_tsadc_chip rk3228_tsadc_data = {
694 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
695 .chn_num = 1, /* one channel for tsadc */
696
697 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
698 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
699 .tshut_temp = 95000,
700
701 .initialize = rk_tsadcv2_initialize,
Caesar Wang952418a2016-02-15 15:33:30 +0800702 .irq_ack = rk_tsadcv3_irq_ack,
Caesar Wang7ea38c62016-02-15 15:33:31 +0800703 .control = rk_tsadcv3_control,
Caesar Wang7b02a5e2015-12-03 16:48:42 +0800704 .get_temp = rk_tsadcv2_get_temp,
Caesar Wang14848502016-06-22 16:42:05 +0800705 .set_alarm_temp = rk_tsadcv2_alarm_temp,
Caesar Wang7b02a5e2015-12-03 16:48:42 +0800706 .set_tshut_temp = rk_tsadcv2_tshut_temp,
707 .set_tshut_mode = rk_tsadcv2_tshut_mode,
708
709 .table = {
Caesar Wang952418a2016-02-15 15:33:30 +0800710 .id = rk3228_code_table,
711 .length = ARRAY_SIZE(rk3228_code_table),
Caesar Wang7b02a5e2015-12-03 16:48:42 +0800712 .data_mask = TSADCV3_DATA_MASK,
Caesar Wang7ea38c62016-02-15 15:33:31 +0800713 .mode = ADC_INCREMENT,
Caesar Wang7b02a5e2015-12-03 16:48:42 +0800714 },
715};
716
Caesar Wangcbac8f632014-11-24 12:58:59 +0800717static const struct rockchip_tsadc_chip rk3288_tsadc_data = {
Caesar Wang1d98b612015-11-05 13:17:58 +0800718 .chn_id[SENSOR_CPU] = 1, /* cpu sensor is channel 1 */
719 .chn_id[SENSOR_GPU] = 2, /* gpu sensor is channel 2 */
720 .chn_num = 2, /* two channels for tsadc */
721
Caesar Wangcbac8f632014-11-24 12:58:59 +0800722 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
723 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
724 .tshut_temp = 95000,
725
726 .initialize = rk_tsadcv2_initialize,
727 .irq_ack = rk_tsadcv2_irq_ack,
728 .control = rk_tsadcv2_control,
729 .get_temp = rk_tsadcv2_get_temp,
Caesar Wang14848502016-06-22 16:42:05 +0800730 .set_alarm_temp = rk_tsadcv2_alarm_temp,
Caesar Wangcbac8f632014-11-24 12:58:59 +0800731 .set_tshut_temp = rk_tsadcv2_tshut_temp,
732 .set_tshut_mode = rk_tsadcv2_tshut_mode,
Caesar Wangce741102015-11-09 12:48:56 +0800733
734 .table = {
Caesar Wang952418a2016-02-15 15:33:30 +0800735 .id = rk3288_code_table,
736 .length = ARRAY_SIZE(rk3288_code_table),
Caesar Wangce741102015-11-09 12:48:56 +0800737 .data_mask = TSADCV2_DATA_MASK,
Caesar Wang020ba952015-11-09 12:48:57 +0800738 .mode = ADC_DECREMENT,
Caesar Wangce741102015-11-09 12:48:56 +0800739 },
Caesar Wangcbac8f632014-11-24 12:58:59 +0800740};
741
Elaine Zhang1cd602692016-04-18 11:35:57 +0800742static const struct rockchip_tsadc_chip rk3366_tsadc_data = {
743 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
744 .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
745 .chn_num = 2, /* two channels for tsadc */
746
747 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
748 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
749 .tshut_temp = 95000,
750
751 .initialize = rk_tsadcv3_initialize,
752 .irq_ack = rk_tsadcv3_irq_ack,
753 .control = rk_tsadcv3_control,
754 .get_temp = rk_tsadcv2_get_temp,
Caesar Wang14848502016-06-22 16:42:05 +0800755 .set_alarm_temp = rk_tsadcv2_alarm_temp,
Elaine Zhang1cd602692016-04-18 11:35:57 +0800756 .set_tshut_temp = rk_tsadcv2_tshut_temp,
757 .set_tshut_mode = rk_tsadcv2_tshut_mode,
758
759 .table = {
760 .id = rk3228_code_table,
761 .length = ARRAY_SIZE(rk3228_code_table),
762 .data_mask = TSADCV3_DATA_MASK,
763 .mode = ADC_INCREMENT,
764 },
765};
766
Caesar Wang20f0af72015-11-09 12:48:59 +0800767static const struct rockchip_tsadc_chip rk3368_tsadc_data = {
768 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
769 .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
770 .chn_num = 2, /* two channels for tsadc */
771
772 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
773 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
774 .tshut_temp = 95000,
775
776 .initialize = rk_tsadcv2_initialize,
777 .irq_ack = rk_tsadcv2_irq_ack,
778 .control = rk_tsadcv2_control,
779 .get_temp = rk_tsadcv2_get_temp,
Caesar Wang14848502016-06-22 16:42:05 +0800780 .set_alarm_temp = rk_tsadcv2_alarm_temp,
Caesar Wang20f0af72015-11-09 12:48:59 +0800781 .set_tshut_temp = rk_tsadcv2_tshut_temp,
782 .set_tshut_mode = rk_tsadcv2_tshut_mode,
783
784 .table = {
Caesar Wang952418a2016-02-15 15:33:30 +0800785 .id = rk3368_code_table,
786 .length = ARRAY_SIZE(rk3368_code_table),
Caesar Wang20f0af72015-11-09 12:48:59 +0800787 .data_mask = TSADCV3_DATA_MASK,
788 .mode = ADC_INCREMENT,
789 },
790};
791
Caesar Wangb0d70332015-12-03 16:48:43 +0800792static const struct rockchip_tsadc_chip rk3399_tsadc_data = {
793 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
794 .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
795 .chn_num = 2, /* two channels for tsadc */
796
797 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
798 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
799 .tshut_temp = 95000,
800
Caesar Wangb9484762016-04-18 11:35:56 +0800801 .initialize = rk_tsadcv3_initialize,
Caesar Wang952418a2016-02-15 15:33:30 +0800802 .irq_ack = rk_tsadcv3_irq_ack,
Caesar Wang7ea38c62016-02-15 15:33:31 +0800803 .control = rk_tsadcv3_control,
Caesar Wangb0d70332015-12-03 16:48:43 +0800804 .get_temp = rk_tsadcv2_get_temp,
Caesar Wang14848502016-06-22 16:42:05 +0800805 .set_alarm_temp = rk_tsadcv2_alarm_temp,
Caesar Wangb0d70332015-12-03 16:48:43 +0800806 .set_tshut_temp = rk_tsadcv2_tshut_temp,
807 .set_tshut_mode = rk_tsadcv2_tshut_mode,
808
809 .table = {
Caesar Wang952418a2016-02-15 15:33:30 +0800810 .id = rk3399_code_table,
811 .length = ARRAY_SIZE(rk3399_code_table),
Caesar Wangb0d70332015-12-03 16:48:43 +0800812 .data_mask = TSADCV3_DATA_MASK,
Caesar Wang7ea38c62016-02-15 15:33:31 +0800813 .mode = ADC_INCREMENT,
Caesar Wangb0d70332015-12-03 16:48:43 +0800814 },
815};
816
Caesar Wangcbac8f632014-11-24 12:58:59 +0800817static const struct of_device_id of_rockchip_thermal_match[] = {
818 {
Caesar Wang7b02a5e2015-12-03 16:48:42 +0800819 .compatible = "rockchip,rk3228-tsadc",
820 .data = (void *)&rk3228_tsadc_data,
821 },
822 {
Caesar Wangcbac8f632014-11-24 12:58:59 +0800823 .compatible = "rockchip,rk3288-tsadc",
824 .data = (void *)&rk3288_tsadc_data,
825 },
Caesar Wang20f0af72015-11-09 12:48:59 +0800826 {
Elaine Zhang1cd602692016-04-18 11:35:57 +0800827 .compatible = "rockchip,rk3366-tsadc",
828 .data = (void *)&rk3366_tsadc_data,
829 },
830 {
Caesar Wang20f0af72015-11-09 12:48:59 +0800831 .compatible = "rockchip,rk3368-tsadc",
832 .data = (void *)&rk3368_tsadc_data,
833 },
Caesar Wangb0d70332015-12-03 16:48:43 +0800834 {
835 .compatible = "rockchip,rk3399-tsadc",
836 .data = (void *)&rk3399_tsadc_data,
837 },
Caesar Wangcbac8f632014-11-24 12:58:59 +0800838 { /* end */ },
839};
840MODULE_DEVICE_TABLE(of, of_rockchip_thermal_match);
841
842static void
843rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor *sensor, bool on)
844{
845 struct thermal_zone_device *tzd = sensor->tzd;
846
847 tzd->ops->set_mode(tzd,
848 on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
849}
850
851static irqreturn_t rockchip_thermal_alarm_irq_thread(int irq, void *dev)
852{
853 struct rockchip_thermal_data *thermal = dev;
854 int i;
855
856 dev_dbg(&thermal->pdev->dev, "thermal alarm\n");
857
858 thermal->chip->irq_ack(thermal->regs);
859
Caesar Wang1d98b612015-11-05 13:17:58 +0800860 for (i = 0; i < thermal->chip->chn_num; i++)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800861 thermal_zone_device_update(thermal->sensors[i].tzd);
862
863 return IRQ_HANDLED;
864}
865
Caesar Wang14848502016-06-22 16:42:05 +0800866static int rockchip_thermal_set_trips(void *_sensor, int low, int high)
867{
868 struct rockchip_thermal_sensor *sensor = _sensor;
869 struct rockchip_thermal_data *thermal = sensor->thermal;
870 const struct rockchip_tsadc_chip *tsadc = thermal->chip;
871
872 dev_dbg(&thermal->pdev->dev, "%s: sensor %d: low: %d, high %d\n",
873 __func__, sensor->id, low, high);
874
875 tsadc->set_alarm_temp(tsadc->table,
876 sensor->id, thermal->regs, high);
877
878 return 0;
879}
880
Sascha Hauer17e83512015-07-24 08:12:54 +0200881static int rockchip_thermal_get_temp(void *_sensor, int *out_temp)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800882{
883 struct rockchip_thermal_sensor *sensor = _sensor;
884 struct rockchip_thermal_data *thermal = sensor->thermal;
885 const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip;
886 int retval;
887
Caesar Wangce741102015-11-09 12:48:56 +0800888 retval = tsadc->get_temp(tsadc->table,
889 sensor->id, thermal->regs, out_temp);
Sascha Hauer17e83512015-07-24 08:12:54 +0200890 dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %d, retval: %d\n",
Caesar Wangcbac8f632014-11-24 12:58:59 +0800891 sensor->id, *out_temp, retval);
892
893 return retval;
894}
895
896static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops = {
897 .get_temp = rockchip_thermal_get_temp,
Caesar Wang14848502016-06-22 16:42:05 +0800898 .set_trips = rockchip_thermal_set_trips,
Caesar Wangcbac8f632014-11-24 12:58:59 +0800899};
900
901static int rockchip_configure_from_dt(struct device *dev,
902 struct device_node *np,
903 struct rockchip_thermal_data *thermal)
904{
905 u32 shut_temp, tshut_mode, tshut_polarity;
906
907 if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) {
908 dev_warn(dev,
Caesar Wang437df212015-11-09 12:48:58 +0800909 "Missing tshut temp property, using default %d\n",
Caesar Wangcbac8f632014-11-24 12:58:59 +0800910 thermal->chip->tshut_temp);
911 thermal->tshut_temp = thermal->chip->tshut_temp;
912 } else {
Caesar Wang43b4eb92016-02-15 15:33:28 +0800913 if (shut_temp > INT_MAX) {
914 dev_err(dev, "Invalid tshut temperature specified: %d\n",
915 shut_temp);
916 return -ERANGE;
917 }
Caesar Wangcbac8f632014-11-24 12:58:59 +0800918 thermal->tshut_temp = shut_temp;
919 }
920
Caesar Wangcbac8f632014-11-24 12:58:59 +0800921 if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) {
922 dev_warn(dev,
923 "Missing tshut mode property, using default (%s)\n",
924 thermal->chip->tshut_mode == TSHUT_MODE_GPIO ?
925 "gpio" : "cru");
926 thermal->tshut_mode = thermal->chip->tshut_mode;
927 } else {
928 thermal->tshut_mode = tshut_mode;
929 }
930
931 if (thermal->tshut_mode > 1) {
932 dev_err(dev, "Invalid tshut mode specified: %d\n",
933 thermal->tshut_mode);
934 return -EINVAL;
935 }
936
937 if (of_property_read_u32(np, "rockchip,hw-tshut-polarity",
938 &tshut_polarity)) {
939 dev_warn(dev,
940 "Missing tshut-polarity property, using default (%s)\n",
941 thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ?
942 "low" : "high");
943 thermal->tshut_polarity = thermal->chip->tshut_polarity;
944 } else {
945 thermal->tshut_polarity = tshut_polarity;
946 }
947
948 if (thermal->tshut_polarity > 1) {
949 dev_err(dev, "Invalid tshut-polarity specified: %d\n",
950 thermal->tshut_polarity);
951 return -EINVAL;
952 }
953
Caesar Wangb9484762016-04-18 11:35:56 +0800954 /* The tsadc wont to handle the error in here since some SoCs didn't
955 * need this property.
956 */
957 thermal->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
958
Caesar Wangcbac8f632014-11-24 12:58:59 +0800959 return 0;
960}
961
962static int
963rockchip_thermal_register_sensor(struct platform_device *pdev,
964 struct rockchip_thermal_data *thermal,
965 struct rockchip_thermal_sensor *sensor,
Caesar Wang1d98b612015-11-05 13:17:58 +0800966 int id)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800967{
968 const struct rockchip_tsadc_chip *tsadc = thermal->chip;
969 int error;
970
971 tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode);
Caesar Wangce741102015-11-09 12:48:56 +0800972 tsadc->set_tshut_temp(tsadc->table, id, thermal->regs,
973 thermal->tshut_temp);
Caesar Wangcbac8f632014-11-24 12:58:59 +0800974
975 sensor->thermal = thermal;
976 sensor->id = id;
Eduardo Valentin2633ad12016-03-09 13:10:28 -0800977 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev, id,
978 sensor, &rockchip_of_thermal_ops);
Caesar Wangcbac8f632014-11-24 12:58:59 +0800979 if (IS_ERR(sensor->tzd)) {
980 error = PTR_ERR(sensor->tzd);
981 dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
982 id, error);
983 return error;
984 }
985
986 return 0;
987}
988
Caesar Wang13c1cfd2015-12-03 16:48:39 +0800989/**
Caesar Wangcbac8f632014-11-24 12:58:59 +0800990 * Reset TSADC Controller, reset all tsadc registers.
991 */
992static void rockchip_thermal_reset_controller(struct reset_control *reset)
993{
994 reset_control_assert(reset);
995 usleep_range(10, 20);
996 reset_control_deassert(reset);
997}
998
999static int rockchip_thermal_probe(struct platform_device *pdev)
1000{
1001 struct device_node *np = pdev->dev.of_node;
1002 struct rockchip_thermal_data *thermal;
1003 const struct of_device_id *match;
1004 struct resource *res;
1005 int irq;
Eduardo Valentin2633ad12016-03-09 13:10:28 -08001006 int i;
Caesar Wangcbac8f632014-11-24 12:58:59 +08001007 int error;
1008
1009 match = of_match_node(of_rockchip_thermal_match, np);
1010 if (!match)
1011 return -ENXIO;
1012
1013 irq = platform_get_irq(pdev, 0);
1014 if (irq < 0) {
1015 dev_err(&pdev->dev, "no irq resource?\n");
1016 return -EINVAL;
1017 }
1018
1019 thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data),
1020 GFP_KERNEL);
1021 if (!thermal)
1022 return -ENOMEM;
1023
1024 thermal->pdev = pdev;
1025
1026 thermal->chip = (const struct rockchip_tsadc_chip *)match->data;
1027 if (!thermal->chip)
1028 return -EINVAL;
1029
1030 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1031 thermal->regs = devm_ioremap_resource(&pdev->dev, res);
1032 if (IS_ERR(thermal->regs))
1033 return PTR_ERR(thermal->regs);
1034
1035 thermal->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb");
1036 if (IS_ERR(thermal->reset)) {
1037 error = PTR_ERR(thermal->reset);
1038 dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
1039 return error;
1040 }
1041
1042 thermal->clk = devm_clk_get(&pdev->dev, "tsadc");
1043 if (IS_ERR(thermal->clk)) {
1044 error = PTR_ERR(thermal->clk);
1045 dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
1046 return error;
1047 }
1048
1049 thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
1050 if (IS_ERR(thermal->pclk)) {
Dan Carpenter0d0a2bf2015-04-21 12:34:10 +03001051 error = PTR_ERR(thermal->pclk);
Caesar Wangcbac8f632014-11-24 12:58:59 +08001052 dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
1053 error);
1054 return error;
1055 }
1056
1057 error = clk_prepare_enable(thermal->clk);
1058 if (error) {
1059 dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
1060 error);
1061 return error;
1062 }
1063
1064 error = clk_prepare_enable(thermal->pclk);
1065 if (error) {
1066 dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
1067 goto err_disable_clk;
1068 }
1069
1070 rockchip_thermal_reset_controller(thermal->reset);
1071
1072 error = rockchip_configure_from_dt(&pdev->dev, np, thermal);
1073 if (error) {
1074 dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
1075 error);
1076 goto err_disable_pclk;
1077 }
1078
Caesar Wangb9484762016-04-18 11:35:56 +08001079 thermal->chip->initialize(thermal->grf, thermal->regs,
1080 thermal->tshut_polarity);
Caesar Wangcbac8f632014-11-24 12:58:59 +08001081
Caesar Wang1d98b612015-11-05 13:17:58 +08001082 for (i = 0; i < thermal->chip->chn_num; i++) {
1083 error = rockchip_thermal_register_sensor(pdev, thermal,
1084 &thermal->sensors[i],
1085 thermal->chip->chn_id[i]);
1086 if (error) {
1087 dev_err(&pdev->dev,
1088 "failed to register sensor[%d] : error = %d\n",
1089 i, error);
Caesar Wang1d98b612015-11-05 13:17:58 +08001090 goto err_disable_pclk;
1091 }
Caesar Wangcbac8f632014-11-24 12:58:59 +08001092 }
1093
1094 error = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1095 &rockchip_thermal_alarm_irq_thread,
1096 IRQF_ONESHOT,
1097 "rockchip_thermal", thermal);
1098 if (error) {
1099 dev_err(&pdev->dev,
1100 "failed to request tsadc irq: %d\n", error);
Eduardo Valentin2633ad12016-03-09 13:10:28 -08001101 goto err_disable_pclk;
Caesar Wangcbac8f632014-11-24 12:58:59 +08001102 }
1103
1104 thermal->chip->control(thermal->regs, true);
1105
Caesar Wang1d98b612015-11-05 13:17:58 +08001106 for (i = 0; i < thermal->chip->chn_num; i++)
Caesar Wangcbac8f632014-11-24 12:58:59 +08001107 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
1108
1109 platform_set_drvdata(pdev, thermal);
1110
1111 return 0;
1112
Caesar Wangcbac8f632014-11-24 12:58:59 +08001113err_disable_pclk:
1114 clk_disable_unprepare(thermal->pclk);
1115err_disable_clk:
1116 clk_disable_unprepare(thermal->clk);
1117
1118 return error;
1119}
1120
1121static int rockchip_thermal_remove(struct platform_device *pdev)
1122{
1123 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
1124 int i;
1125
Caesar Wang1d98b612015-11-05 13:17:58 +08001126 for (i = 0; i < thermal->chip->chn_num; i++) {
Caesar Wangcbac8f632014-11-24 12:58:59 +08001127 struct rockchip_thermal_sensor *sensor = &thermal->sensors[i];
1128
1129 rockchip_thermal_toggle_sensor(sensor, false);
Caesar Wangcbac8f632014-11-24 12:58:59 +08001130 }
1131
1132 thermal->chip->control(thermal->regs, false);
1133
1134 clk_disable_unprepare(thermal->pclk);
1135 clk_disable_unprepare(thermal->clk);
1136
1137 return 0;
1138}
1139
1140static int __maybe_unused rockchip_thermal_suspend(struct device *dev)
1141{
1142 struct platform_device *pdev = to_platform_device(dev);
1143 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
1144 int i;
1145
Caesar Wang1d98b612015-11-05 13:17:58 +08001146 for (i = 0; i < thermal->chip->chn_num; i++)
Caesar Wangcbac8f632014-11-24 12:58:59 +08001147 rockchip_thermal_toggle_sensor(&thermal->sensors[i], false);
1148
1149 thermal->chip->control(thermal->regs, false);
1150
1151 clk_disable(thermal->pclk);
1152 clk_disable(thermal->clk);
1153
Caesar Wang7e38a5b2015-10-23 19:25:27 +08001154 pinctrl_pm_select_sleep_state(dev);
1155
Caesar Wangcbac8f632014-11-24 12:58:59 +08001156 return 0;
1157}
1158
1159static int __maybe_unused rockchip_thermal_resume(struct device *dev)
1160{
1161 struct platform_device *pdev = to_platform_device(dev);
1162 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
1163 int i;
1164 int error;
1165
1166 error = clk_enable(thermal->clk);
1167 if (error)
1168 return error;
1169
1170 error = clk_enable(thermal->pclk);
Shawn Linab5b52f2016-04-18 11:35:53 +08001171 if (error) {
1172 clk_disable(thermal->clk);
Caesar Wangcbac8f632014-11-24 12:58:59 +08001173 return error;
Shawn Linab5b52f2016-04-18 11:35:53 +08001174 }
Caesar Wangcbac8f632014-11-24 12:58:59 +08001175
1176 rockchip_thermal_reset_controller(thermal->reset);
1177
Caesar Wangb9484762016-04-18 11:35:56 +08001178 thermal->chip->initialize(thermal->grf, thermal->regs,
1179 thermal->tshut_polarity);
Caesar Wangcbac8f632014-11-24 12:58:59 +08001180
Caesar Wang1d98b612015-11-05 13:17:58 +08001181 for (i = 0; i < thermal->chip->chn_num; i++) {
1182 int id = thermal->sensors[i].id;
Caesar Wangcbac8f632014-11-24 12:58:59 +08001183
1184 thermal->chip->set_tshut_mode(id, thermal->regs,
1185 thermal->tshut_mode);
Caesar Wangce741102015-11-09 12:48:56 +08001186 thermal->chip->set_tshut_temp(thermal->chip->table,
1187 id, thermal->regs,
Caesar Wangcbac8f632014-11-24 12:58:59 +08001188 thermal->tshut_temp);
1189 }
1190
1191 thermal->chip->control(thermal->regs, true);
1192
Caesar Wang1d98b612015-11-05 13:17:58 +08001193 for (i = 0; i < thermal->chip->chn_num; i++)
Caesar Wangcbac8f632014-11-24 12:58:59 +08001194 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
1195
Caesar Wang7e38a5b2015-10-23 19:25:27 +08001196 pinctrl_pm_select_default_state(dev);
1197
Caesar Wangcbac8f632014-11-24 12:58:59 +08001198 return 0;
1199}
1200
1201static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops,
1202 rockchip_thermal_suspend, rockchip_thermal_resume);
1203
1204static struct platform_driver rockchip_thermal_driver = {
1205 .driver = {
1206 .name = "rockchip-thermal",
Caesar Wangcbac8f632014-11-24 12:58:59 +08001207 .pm = &rockchip_thermal_pm_ops,
1208 .of_match_table = of_rockchip_thermal_match,
1209 },
1210 .probe = rockchip_thermal_probe,
1211 .remove = rockchip_thermal_remove,
1212};
1213
1214module_platform_driver(rockchip_thermal_driver);
1215
1216MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
1217MODULE_AUTHOR("Rockchip, Inc.");
1218MODULE_LICENSE("GPL v2");
1219MODULE_ALIAS("platform:rockchip-thermal");