blob: cacc12b44ca15f9cfa66952d76c4703b36a3ccec [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 Wang1d98b6182015-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 Wang1d98b6182015-11-05 13:17:58 +080055 SENSOR_CPU = 0,
Caesar Wangcbac8f632014-11-24 12:58:59 +080056 SENSOR_GPU,
57};
58
Caesar Wang1d98b6182015-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 Wang1d98b6182015-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 Wang1d98b6182015-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 */
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800121 int (*get_temp)(const struct chip_tsadc_table *table,
Caesar Wangce741102015-11-09 12:48:56 +0800122 int chn, void __iomem *reg, int *temp);
Caesar Wangd3530492016-12-12 19:05:33 +0800123 int (*set_alarm_temp)(const struct chip_tsadc_table *table,
124 int chn, void __iomem *reg, int temp);
125 int (*set_tshut_temp)(const struct chip_tsadc_table *table,
126 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 Wang1d98b6182015-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 Wang1d98b6182015-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 */
Rocky Hao5ef62de2016-07-27 22:10:43 +0800216#define TSADCV3_AUTO_PERIOD_TIME 1875 /* 2.5ms */
217#define TSADCV3_AUTO_PERIOD_HT_TIME 1875 /* 2.5ms */
Caesar Wang46667872016-06-22 18:13:56 +0800218
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
Caesar Wangb9484762016-04-18 11:35:56 +0800225#define GRF_SARADC_TESTBIT_ON (0x10001 << 2)
226#define GRF_TSADC_TESTBIT_H_ON (0x10001 << 2)
Rocky Hao23f75e42016-07-27 22:10:42 +0800227#define GRF_TSADC_VCM_EN_L (0x10001 << 7)
228#define GRF_TSADC_VCM_EN_H (0x10001 << 7)
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 Wangcadf29d2016-12-12 19:05:34 +0800320 {0, 125000},
Caesar Wangcbac8f632014-11-24 12:58:59 +0800321};
322
Caesar Wang952418a2016-02-15 15:33:30 +0800323static const struct tsadc_table rk3368_code_table[] = {
Caesar Wang20f0af72015-11-09 12:48:59 +0800324 {0, -40000},
325 {106, -40000},
326 {108, -35000},
327 {110, -30000},
328 {112, -25000},
329 {114, -20000},
330 {116, -15000},
331 {118, -10000},
332 {120, -5000},
333 {122, 0},
334 {124, 5000},
335 {126, 10000},
336 {128, 15000},
337 {130, 20000},
338 {132, 25000},
339 {134, 30000},
340 {136, 35000},
341 {138, 40000},
342 {140, 45000},
343 {142, 50000},
344 {144, 55000},
345 {146, 60000},
346 {148, 65000},
347 {150, 70000},
348 {152, 75000},
349 {154, 80000},
350 {156, 85000},
351 {158, 90000},
352 {160, 95000},
353 {162, 100000},
354 {163, 105000},
355 {165, 110000},
356 {167, 115000},
357 {169, 120000},
358 {171, 125000},
359 {TSADCV3_DATA_MASK, 125000},
360};
361
Caesar Wang952418a2016-02-15 15:33:30 +0800362static const struct tsadc_table rk3399_code_table[] = {
Caesar Wang7ea38c62016-02-15 15:33:31 +0800363 {0, -40000},
Caesar Wangf762a352016-04-18 11:35:55 +0800364 {402, -40000},
365 {410, -35000},
366 {419, -30000},
367 {427, -25000},
368 {436, -20000},
369 {444, -15000},
370 {453, -10000},
371 {461, -5000},
372 {470, 0},
373 {478, 5000},
374 {487, 10000},
375 {496, 15000},
376 {504, 20000},
377 {513, 25000},
378 {521, 30000},
379 {530, 35000},
380 {538, 40000},
381 {547, 45000},
382 {555, 50000},
383 {564, 55000},
384 {573, 60000},
385 {581, 65000},
386 {590, 70000},
387 {599, 75000},
388 {607, 80000},
389 {616, 85000},
390 {624, 90000},
391 {633, 95000},
392 {642, 100000},
393 {650, 105000},
394 {659, 110000},
395 {668, 115000},
396 {677, 120000},
397 {685, 125000},
Caesar Wang7ea38c62016-02-15 15:33:31 +0800398 {TSADCV3_DATA_MASK, 125000},
Caesar Wangb0d70332015-12-03 16:48:43 +0800399};
400
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800401static u32 rk_tsadcv2_temp_to_code(const struct chip_tsadc_table *table,
Caesar Wang437df212015-11-09 12:48:58 +0800402 int temp)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800403{
404 int high, low, mid;
Caesar Wangcadf29d2016-12-12 19:05:34 +0800405 unsigned long num;
406 unsigned int denom;
Caesar Wangd3530492016-12-12 19:05:33 +0800407 u32 error = table->data_mask;
Caesar Wangcbac8f632014-11-24 12:58:59 +0800408
409 low = 0;
Caesar Wangcadf29d2016-12-12 19:05:34 +0800410 high = (table->length - 1) - 1; /* ignore the last check for table */
Caesar Wangcbac8f632014-11-24 12:58:59 +0800411 mid = (high + low) / 2;
412
Caesar Wang1f09ba82016-06-22 18:13:57 +0800413 /* Return mask code data when the temp is over table range */
Caesar Wangd3530492016-12-12 19:05:33 +0800414 if (temp < table->id[low].temp || temp > table->id[high].temp)
Caesar Wang1f09ba82016-06-22 18:13:57 +0800415 goto exit;
Caesar Wangcbac8f632014-11-24 12:58:59 +0800416
417 while (low <= high) {
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800418 if (temp == table->id[mid].temp)
419 return table->id[mid].code;
420 else if (temp < table->id[mid].temp)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800421 high = mid - 1;
422 else
423 low = mid + 1;
424 mid = (low + high) / 2;
425 }
426
Caesar Wangcadf29d2016-12-12 19:05:34 +0800427 /*
428 * The conversion code granularity provided by the table. Let's
429 * assume that the relationship between temperature and
430 * analog value between 2 table entries is linear and interpolate
431 * to produce less granular result.
432 */
433 num = abs(table->id[mid + 1].code - table->id[mid].code);
434 num *= temp - table->id[mid].temp;
435 denom = table->id[mid + 1].temp - table->id[mid].temp;
436
437 switch (table->mode) {
438 case ADC_DECREMENT:
439 return table->id[mid].code - (num / denom);
440 case ADC_INCREMENT:
441 return table->id[mid].code + (num / denom);
442 default:
443 pr_err("%s: unknown table mode: %d\n", __func__, table->mode);
444 return error;
445 }
446
Caesar Wang1f09ba82016-06-22 18:13:57 +0800447exit:
Brian Norrise6ed1b42016-12-12 19:05:31 +0800448 pr_err("%s: invalid temperature, temp=%d error=%d\n",
449 __func__, temp, error);
Caesar Wang1f09ba82016-06-22 18:13:57 +0800450 return error;
Caesar Wangcbac8f632014-11-24 12:58:59 +0800451}
452
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800453static int rk_tsadcv2_code_to_temp(const struct chip_tsadc_table *table,
454 u32 code, int *temp)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800455{
Dmitry Torokhovd9a241c2015-08-07 13:59:23 -0700456 unsigned int low = 1;
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800457 unsigned int high = table->length - 1;
Caesar Wang1e9a1aea2015-01-25 10:11:11 +0800458 unsigned int mid = (low + high) / 2;
459 unsigned int num;
460 unsigned long denom;
Caesar Wangcbac8f632014-11-24 12:58:59 +0800461
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800462 WARN_ON(table->length < 2);
Caesar Wangcbac8f632014-11-24 12:58:59 +0800463
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800464 switch (table->mode) {
Caesar Wang020ba952015-11-09 12:48:57 +0800465 case ADC_DECREMENT:
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800466 code &= table->data_mask;
467 if (code < table->id[high].code)
Caesar Wang020ba952015-11-09 12:48:57 +0800468 return -EAGAIN; /* Incorrect reading */
Dmitry Torokhovd9a241c2015-08-07 13:59:23 -0700469
Caesar Wang020ba952015-11-09 12:48:57 +0800470 while (low <= high) {
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800471 if (code >= table->id[mid].code &&
472 code < table->id[mid - 1].code)
Caesar Wang020ba952015-11-09 12:48:57 +0800473 break;
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800474 else if (code < table->id[mid].code)
Caesar Wang020ba952015-11-09 12:48:57 +0800475 low = mid + 1;
476 else
477 high = mid - 1;
478
479 mid = (low + high) / 2;
480 }
481 break;
482 case ADC_INCREMENT:
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800483 code &= table->data_mask;
484 if (code < table->id[low].code)
Caesar Wang020ba952015-11-09 12:48:57 +0800485 return -EAGAIN; /* Incorrect reading */
486
487 while (low <= high) {
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800488 if (code <= table->id[mid].code &&
489 code > table->id[mid - 1].code)
Caesar Wang020ba952015-11-09 12:48:57 +0800490 break;
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800491 else if (code > table->id[mid].code)
Caesar Wang020ba952015-11-09 12:48:57 +0800492 low = mid + 1;
493 else
494 high = mid - 1;
495
496 mid = (low + high) / 2;
497 }
498 break;
499 default:
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800500 pr_err("%s: unknown table mode: %d\n", __func__, table->mode);
Brian Norrise6ed1b42016-12-12 19:05:31 +0800501 return -EINVAL;
Caesar Wangcbac8f632014-11-24 12:58:59 +0800502 }
503
Caesar Wang1e9a1aea2015-01-25 10:11:11 +0800504 /*
505 * The 5C granularity provided by the table is too much. Let's
506 * assume that the relationship between sensor readings and
507 * temperature between 2 table entries is linear and interpolate
508 * to produce less granular result.
509 */
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800510 num = table->id[mid].temp - table->id[mid - 1].temp;
511 num *= abs(table->id[mid - 1].code - code);
512 denom = abs(table->id[mid - 1].code - table->id[mid].code);
513 *temp = table->id[mid - 1].temp + (num / denom);
Dmitry Torokhovd9a241c2015-08-07 13:59:23 -0700514
515 return 0;
Caesar Wangcbac8f632014-11-24 12:58:59 +0800516}
517
518/**
Caesar Wang144c5562015-11-05 13:17:59 +0800519 * rk_tsadcv2_initialize - initialize TASDC Controller.
520 *
521 * (1) Set TSADC_V2_AUTO_PERIOD:
522 * Configure the interleave between every two accessing of
523 * TSADC in normal operation.
524 *
525 * (2) Set TSADCV2_AUTO_PERIOD_HT:
526 * Configure the interleave between every two accessing of
527 * TSADC after the temperature is higher than COM_SHUT or COM_INT.
528 *
529 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
530 * If the temperature is higher than COMP_INT or COMP_SHUT for
531 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
Caesar Wangcbac8f632014-11-24 12:58:59 +0800532 */
Caesar Wangb9484762016-04-18 11:35:56 +0800533static void rk_tsadcv2_initialize(struct regmap *grf, void __iomem *regs,
Caesar Wangcbac8f632014-11-24 12:58:59 +0800534 enum tshut_polarity tshut_polarity)
535{
536 if (tshut_polarity == TSHUT_HIGH_ACTIVE)
Dmitry Torokhov452e01b2015-08-07 14:00:52 -0700537 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
Caesar Wangcbac8f632014-11-24 12:58:59 +0800538 regs + TSADCV2_AUTO_CON);
539 else
Dmitry Torokhov452e01b2015-08-07 14:00:52 -0700540 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
Caesar Wangcbac8f632014-11-24 12:58:59 +0800541 regs + TSADCV2_AUTO_CON);
542
543 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
544 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
545 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
546 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
547 regs + TSADCV2_AUTO_PERIOD_HT);
548 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
549 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
Caesar Wangb9484762016-04-18 11:35:56 +0800550}
551
552/**
553 * rk_tsadcv3_initialize - initialize TASDC Controller.
Caesar Wang678065d2016-04-18 11:35:58 +0800554 *
Caesar Wangb9484762016-04-18 11:35:56 +0800555 * (1) The tsadc control power sequence.
556 *
557 * (2) Set TSADC_V2_AUTO_PERIOD:
558 * Configure the interleave between every two accessing of
559 * TSADC in normal operation.
560 *
561 * (2) Set TSADCV2_AUTO_PERIOD_HT:
562 * Configure the interleave between every two accessing of
563 * TSADC after the temperature is higher than COM_SHUT or COM_INT.
564 *
565 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
566 * If the temperature is higher than COMP_INT or COMP_SHUT for
567 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
568 */
569static void rk_tsadcv3_initialize(struct regmap *grf, void __iomem *regs,
570 enum tshut_polarity tshut_polarity)
571{
572 /* The tsadc control power sequence */
573 if (IS_ERR(grf)) {
574 /* Set interleave value to workround ic time sync issue */
575 writel_relaxed(TSADCV2_USER_INTER_PD_SOC, regs +
576 TSADCV2_USER_CON);
Caesar Wang46667872016-06-22 18:13:56 +0800577
578 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME,
579 regs + TSADCV2_AUTO_PERIOD);
580 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
581 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
582 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
583 regs + TSADCV2_AUTO_PERIOD_HT);
584 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
585 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
586
Caesar Wangb9484762016-04-18 11:35:56 +0800587 } else {
Rocky Hao23f75e42016-07-27 22:10:42 +0800588 /* Enable the voltage common mode feature */
589 regmap_write(grf, GRF_TSADC_TESTBIT_L, GRF_TSADC_VCM_EN_L);
590 regmap_write(grf, GRF_TSADC_TESTBIT_H, GRF_TSADC_VCM_EN_H);
591
Caesar Wang2fe5c1b2016-05-03 10:23:50 +0800592 usleep_range(15, 100); /* The spec note says at least 15 us */
Caesar Wangb9484762016-04-18 11:35:56 +0800593 regmap_write(grf, GRF_SARADC_TESTBIT, GRF_SARADC_TESTBIT_ON);
594 regmap_write(grf, GRF_TSADC_TESTBIT_H, GRF_TSADC_TESTBIT_H_ON);
Caesar Wang2fe5c1b2016-05-03 10:23:50 +0800595 usleep_range(90, 200); /* The spec note says at least 90 us */
Caesar Wang46667872016-06-22 18:13:56 +0800596
597 writel_relaxed(TSADCV3_AUTO_PERIOD_TIME,
598 regs + TSADCV2_AUTO_PERIOD);
599 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
600 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
601 writel_relaxed(TSADCV3_AUTO_PERIOD_HT_TIME,
602 regs + TSADCV2_AUTO_PERIOD_HT);
603 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
604 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
Caesar Wangb9484762016-04-18 11:35:56 +0800605 }
606
607 if (tshut_polarity == TSHUT_HIGH_ACTIVE)
608 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
609 regs + TSADCV2_AUTO_CON);
610 else
611 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
612 regs + TSADCV2_AUTO_CON);
Caesar Wangcbac8f632014-11-24 12:58:59 +0800613}
614
615static void rk_tsadcv2_irq_ack(void __iomem *regs)
616{
617 u32 val;
618
619 val = readl_relaxed(regs + TSADCV2_INT_PD);
Dmitry Torokhov452e01b2015-08-07 14:00:52 -0700620 writel_relaxed(val & TSADCV2_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
Caesar Wangcbac8f632014-11-24 12:58:59 +0800621}
622
Caesar Wang952418a2016-02-15 15:33:30 +0800623static void rk_tsadcv3_irq_ack(void __iomem *regs)
624{
625 u32 val;
626
627 val = readl_relaxed(regs + TSADCV2_INT_PD);
628 writel_relaxed(val & TSADCV3_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
629}
630
Caesar Wangcbac8f632014-11-24 12:58:59 +0800631static void rk_tsadcv2_control(void __iomem *regs, bool enable)
632{
633 u32 val;
634
635 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
636 if (enable)
637 val |= TSADCV2_AUTO_EN;
638 else
639 val &= ~TSADCV2_AUTO_EN;
640
641 writel_relaxed(val, regs + TSADCV2_AUTO_CON);
642}
643
Caesar Wang7ea38c62016-02-15 15:33:31 +0800644/**
Caesar Wang678065d2016-04-18 11:35:58 +0800645 * rk_tsadcv3_control - the tsadc controller is enabled or disabled.
646 *
647 * NOTE: TSADC controller works at auto mode, and some SoCs need set the
648 * tsadc_q_sel bit on TSADCV2_AUTO_CON[1]. The (1024 - tsadc_q) as output
649 * adc value if setting this bit to enable.
Caesar Wang7ea38c62016-02-15 15:33:31 +0800650 */
651static void rk_tsadcv3_control(void __iomem *regs, bool enable)
652{
653 u32 val;
654
655 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
656 if (enable)
657 val |= TSADCV2_AUTO_EN | TSADCV3_AUTO_Q_SEL_EN;
658 else
659 val &= ~TSADCV2_AUTO_EN;
660
661 writel_relaxed(val, regs + TSADCV2_AUTO_CON);
662}
663
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800664static int rk_tsadcv2_get_temp(const struct chip_tsadc_table *table,
Caesar Wangce741102015-11-09 12:48:56 +0800665 int chn, void __iomem *regs, int *temp)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800666{
667 u32 val;
668
Caesar Wangcbac8f632014-11-24 12:58:59 +0800669 val = readl_relaxed(regs + TSADCV2_DATA(chn));
Caesar Wangcbac8f632014-11-24 12:58:59 +0800670
Caesar Wangce741102015-11-09 12:48:56 +0800671 return rk_tsadcv2_code_to_temp(table, val, temp);
Caesar Wangcbac8f632014-11-24 12:58:59 +0800672}
673
Caesar Wangd3530492016-12-12 19:05:33 +0800674static int rk_tsadcv2_alarm_temp(const struct chip_tsadc_table *table,
675 int chn, void __iomem *regs, int temp)
Caesar Wang14848502016-06-22 16:42:05 +0800676{
677 u32 alarm_value, int_en;
678
Caesar Wang1f09ba82016-06-22 18:13:57 +0800679 /* Make sure the value is valid */
Caesar Wang14848502016-06-22 16:42:05 +0800680 alarm_value = rk_tsadcv2_temp_to_code(table, temp);
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800681 if (alarm_value == table->data_mask)
Caesar Wangd3530492016-12-12 19:05:33 +0800682 return -ERANGE;
Caesar Wang1f09ba82016-06-22 18:13:57 +0800683
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800684 writel_relaxed(alarm_value & table->data_mask,
Caesar Wang14848502016-06-22 16:42:05 +0800685 regs + TSADCV2_COMP_INT(chn));
686
687 int_en = readl_relaxed(regs + TSADCV2_INT_EN);
688 int_en |= TSADCV2_INT_SRC_EN(chn);
689 writel_relaxed(int_en, regs + TSADCV2_INT_EN);
Caesar Wangd3530492016-12-12 19:05:33 +0800690
691 return 0;
Caesar Wang14848502016-06-22 16:42:05 +0800692}
693
Caesar Wangd3530492016-12-12 19:05:33 +0800694static int rk_tsadcv2_tshut_temp(const struct chip_tsadc_table *table,
695 int chn, void __iomem *regs, int temp)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800696{
697 u32 tshut_value, val;
698
Caesar Wang1f09ba82016-06-22 18:13:57 +0800699 /* Make sure the value is valid */
Caesar Wangce741102015-11-09 12:48:56 +0800700 tshut_value = rk_tsadcv2_temp_to_code(table, temp);
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800701 if (tshut_value == table->data_mask)
Caesar Wangd3530492016-12-12 19:05:33 +0800702 return -ERANGE;
Caesar Wang1f09ba82016-06-22 18:13:57 +0800703
Caesar Wangcbac8f632014-11-24 12:58:59 +0800704 writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn));
705
706 /* TSHUT will be valid */
707 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
708 writel_relaxed(val | TSADCV2_AUTO_SRC_EN(chn), regs + TSADCV2_AUTO_CON);
Caesar Wangd3530492016-12-12 19:05:33 +0800709
710 return 0;
Caesar Wangcbac8f632014-11-24 12:58:59 +0800711}
712
713static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs,
714 enum tshut_mode mode)
715{
716 u32 val;
717
718 val = readl_relaxed(regs + TSADCV2_INT_EN);
719 if (mode == TSHUT_MODE_GPIO) {
720 val &= ~TSADCV2_SHUT_2CRU_SRC_EN(chn);
721 val |= TSADCV2_SHUT_2GPIO_SRC_EN(chn);
722 } else {
723 val &= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn);
724 val |= TSADCV2_SHUT_2CRU_SRC_EN(chn);
725 }
726
727 writel_relaxed(val, regs + TSADCV2_INT_EN);
728}
729
Caesar Wang7b02a5e2015-12-03 16:48:42 +0800730static const struct rockchip_tsadc_chip rk3228_tsadc_data = {
731 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
732 .chn_num = 1, /* one channel for tsadc */
733
734 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
735 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
736 .tshut_temp = 95000,
737
738 .initialize = rk_tsadcv2_initialize,
Caesar Wang952418a2016-02-15 15:33:30 +0800739 .irq_ack = rk_tsadcv3_irq_ack,
Caesar Wang7ea38c62016-02-15 15:33:31 +0800740 .control = rk_tsadcv3_control,
Caesar Wang7b02a5e2015-12-03 16:48:42 +0800741 .get_temp = rk_tsadcv2_get_temp,
Caesar Wang14848502016-06-22 16:42:05 +0800742 .set_alarm_temp = rk_tsadcv2_alarm_temp,
Caesar Wang7b02a5e2015-12-03 16:48:42 +0800743 .set_tshut_temp = rk_tsadcv2_tshut_temp,
744 .set_tshut_mode = rk_tsadcv2_tshut_mode,
745
746 .table = {
Caesar Wang952418a2016-02-15 15:33:30 +0800747 .id = rk3228_code_table,
748 .length = ARRAY_SIZE(rk3228_code_table),
Caesar Wang7b02a5e2015-12-03 16:48:42 +0800749 .data_mask = TSADCV3_DATA_MASK,
Caesar Wang7ea38c62016-02-15 15:33:31 +0800750 .mode = ADC_INCREMENT,
Caesar Wang7b02a5e2015-12-03 16:48:42 +0800751 },
752};
753
Caesar Wangcbac8f632014-11-24 12:58:59 +0800754static const struct rockchip_tsadc_chip rk3288_tsadc_data = {
Caesar Wang1d98b6182015-11-05 13:17:58 +0800755 .chn_id[SENSOR_CPU] = 1, /* cpu sensor is channel 1 */
756 .chn_id[SENSOR_GPU] = 2, /* gpu sensor is channel 2 */
757 .chn_num = 2, /* two channels for tsadc */
758
Caesar Wangcbac8f632014-11-24 12:58:59 +0800759 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
760 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
761 .tshut_temp = 95000,
762
763 .initialize = rk_tsadcv2_initialize,
764 .irq_ack = rk_tsadcv2_irq_ack,
765 .control = rk_tsadcv2_control,
766 .get_temp = rk_tsadcv2_get_temp,
Caesar Wang14848502016-06-22 16:42:05 +0800767 .set_alarm_temp = rk_tsadcv2_alarm_temp,
Caesar Wangcbac8f632014-11-24 12:58:59 +0800768 .set_tshut_temp = rk_tsadcv2_tshut_temp,
769 .set_tshut_mode = rk_tsadcv2_tshut_mode,
Caesar Wangce741102015-11-09 12:48:56 +0800770
771 .table = {
Caesar Wang952418a2016-02-15 15:33:30 +0800772 .id = rk3288_code_table,
773 .length = ARRAY_SIZE(rk3288_code_table),
Caesar Wangce741102015-11-09 12:48:56 +0800774 .data_mask = TSADCV2_DATA_MASK,
Caesar Wang020ba952015-11-09 12:48:57 +0800775 .mode = ADC_DECREMENT,
Caesar Wangce741102015-11-09 12:48:56 +0800776 },
Caesar Wangcbac8f632014-11-24 12:58:59 +0800777};
778
Elaine Zhang1cd602692016-04-18 11:35:57 +0800779static const struct rockchip_tsadc_chip rk3366_tsadc_data = {
780 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
781 .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
782 .chn_num = 2, /* two channels for tsadc */
783
784 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
785 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
786 .tshut_temp = 95000,
787
788 .initialize = rk_tsadcv3_initialize,
789 .irq_ack = rk_tsadcv3_irq_ack,
790 .control = rk_tsadcv3_control,
791 .get_temp = rk_tsadcv2_get_temp,
Caesar Wang14848502016-06-22 16:42:05 +0800792 .set_alarm_temp = rk_tsadcv2_alarm_temp,
Elaine Zhang1cd602692016-04-18 11:35:57 +0800793 .set_tshut_temp = rk_tsadcv2_tshut_temp,
794 .set_tshut_mode = rk_tsadcv2_tshut_mode,
795
796 .table = {
797 .id = rk3228_code_table,
798 .length = ARRAY_SIZE(rk3228_code_table),
799 .data_mask = TSADCV3_DATA_MASK,
800 .mode = ADC_INCREMENT,
801 },
802};
803
Caesar Wang20f0af72015-11-09 12:48:59 +0800804static const struct rockchip_tsadc_chip rk3368_tsadc_data = {
805 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
806 .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
807 .chn_num = 2, /* two channels for tsadc */
808
809 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
810 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
811 .tshut_temp = 95000,
812
813 .initialize = rk_tsadcv2_initialize,
814 .irq_ack = rk_tsadcv2_irq_ack,
815 .control = rk_tsadcv2_control,
816 .get_temp = rk_tsadcv2_get_temp,
Caesar Wang14848502016-06-22 16:42:05 +0800817 .set_alarm_temp = rk_tsadcv2_alarm_temp,
Caesar Wang20f0af72015-11-09 12:48:59 +0800818 .set_tshut_temp = rk_tsadcv2_tshut_temp,
819 .set_tshut_mode = rk_tsadcv2_tshut_mode,
820
821 .table = {
Caesar Wang952418a2016-02-15 15:33:30 +0800822 .id = rk3368_code_table,
823 .length = ARRAY_SIZE(rk3368_code_table),
Caesar Wang20f0af72015-11-09 12:48:59 +0800824 .data_mask = TSADCV3_DATA_MASK,
825 .mode = ADC_INCREMENT,
826 },
827};
828
Caesar Wangb0d70332015-12-03 16:48:43 +0800829static const struct rockchip_tsadc_chip rk3399_tsadc_data = {
830 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
831 .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
832 .chn_num = 2, /* two channels for tsadc */
833
834 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
835 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
836 .tshut_temp = 95000,
837
Caesar Wangb9484762016-04-18 11:35:56 +0800838 .initialize = rk_tsadcv3_initialize,
Caesar Wang952418a2016-02-15 15:33:30 +0800839 .irq_ack = rk_tsadcv3_irq_ack,
Caesar Wang7ea38c62016-02-15 15:33:31 +0800840 .control = rk_tsadcv3_control,
Caesar Wangb0d70332015-12-03 16:48:43 +0800841 .get_temp = rk_tsadcv2_get_temp,
Caesar Wang14848502016-06-22 16:42:05 +0800842 .set_alarm_temp = rk_tsadcv2_alarm_temp,
Caesar Wangb0d70332015-12-03 16:48:43 +0800843 .set_tshut_temp = rk_tsadcv2_tshut_temp,
844 .set_tshut_mode = rk_tsadcv2_tshut_mode,
845
846 .table = {
Caesar Wang952418a2016-02-15 15:33:30 +0800847 .id = rk3399_code_table,
848 .length = ARRAY_SIZE(rk3399_code_table),
Caesar Wangb0d70332015-12-03 16:48:43 +0800849 .data_mask = TSADCV3_DATA_MASK,
Caesar Wang7ea38c62016-02-15 15:33:31 +0800850 .mode = ADC_INCREMENT,
Caesar Wangb0d70332015-12-03 16:48:43 +0800851 },
852};
853
Caesar Wangcbac8f632014-11-24 12:58:59 +0800854static const struct of_device_id of_rockchip_thermal_match[] = {
855 {
Caesar Wang7b02a5e2015-12-03 16:48:42 +0800856 .compatible = "rockchip,rk3228-tsadc",
857 .data = (void *)&rk3228_tsadc_data,
858 },
859 {
Caesar Wangcbac8f632014-11-24 12:58:59 +0800860 .compatible = "rockchip,rk3288-tsadc",
861 .data = (void *)&rk3288_tsadc_data,
862 },
Caesar Wang20f0af72015-11-09 12:48:59 +0800863 {
Elaine Zhang1cd602692016-04-18 11:35:57 +0800864 .compatible = "rockchip,rk3366-tsadc",
865 .data = (void *)&rk3366_tsadc_data,
866 },
867 {
Caesar Wang20f0af72015-11-09 12:48:59 +0800868 .compatible = "rockchip,rk3368-tsadc",
869 .data = (void *)&rk3368_tsadc_data,
870 },
Caesar Wangb0d70332015-12-03 16:48:43 +0800871 {
872 .compatible = "rockchip,rk3399-tsadc",
873 .data = (void *)&rk3399_tsadc_data,
874 },
Caesar Wangcbac8f632014-11-24 12:58:59 +0800875 { /* end */ },
876};
877MODULE_DEVICE_TABLE(of, of_rockchip_thermal_match);
878
879static void
880rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor *sensor, bool on)
881{
882 struct thermal_zone_device *tzd = sensor->tzd;
883
884 tzd->ops->set_mode(tzd,
885 on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
886}
887
888static irqreturn_t rockchip_thermal_alarm_irq_thread(int irq, void *dev)
889{
890 struct rockchip_thermal_data *thermal = dev;
891 int i;
892
893 dev_dbg(&thermal->pdev->dev, "thermal alarm\n");
894
895 thermal->chip->irq_ack(thermal->regs);
896
Caesar Wang1d98b6182015-11-05 13:17:58 +0800897 for (i = 0; i < thermal->chip->chn_num; i++)
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700898 thermal_zone_device_update(thermal->sensors[i].tzd,
899 THERMAL_EVENT_UNSPECIFIED);
Caesar Wangcbac8f632014-11-24 12:58:59 +0800900
901 return IRQ_HANDLED;
902}
903
Caesar Wang14848502016-06-22 16:42:05 +0800904static int rockchip_thermal_set_trips(void *_sensor, int low, int high)
905{
906 struct rockchip_thermal_sensor *sensor = _sensor;
907 struct rockchip_thermal_data *thermal = sensor->thermal;
908 const struct rockchip_tsadc_chip *tsadc = thermal->chip;
909
910 dev_dbg(&thermal->pdev->dev, "%s: sensor %d: low: %d, high %d\n",
911 __func__, sensor->id, low, high);
912
Caesar Wangd3530492016-12-12 19:05:33 +0800913 return tsadc->set_alarm_temp(&tsadc->table,
914 sensor->id, thermal->regs, high);
Caesar Wang14848502016-06-22 16:42:05 +0800915}
916
Sascha Hauer17e83512015-07-24 08:12:54 +0200917static int rockchip_thermal_get_temp(void *_sensor, int *out_temp)
Caesar Wangcbac8f632014-11-24 12:58:59 +0800918{
919 struct rockchip_thermal_sensor *sensor = _sensor;
920 struct rockchip_thermal_data *thermal = sensor->thermal;
921 const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip;
922 int retval;
923
Brian Norriscdd8b3f2016-12-12 19:05:32 +0800924 retval = tsadc->get_temp(&tsadc->table,
Caesar Wangce741102015-11-09 12:48:56 +0800925 sensor->id, thermal->regs, out_temp);
Sascha Hauer17e83512015-07-24 08:12:54 +0200926 dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %d, retval: %d\n",
Caesar Wangcbac8f632014-11-24 12:58:59 +0800927 sensor->id, *out_temp, retval);
928
929 return retval;
930}
931
932static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops = {
933 .get_temp = rockchip_thermal_get_temp,
Caesar Wang14848502016-06-22 16:42:05 +0800934 .set_trips = rockchip_thermal_set_trips,
Caesar Wangcbac8f632014-11-24 12:58:59 +0800935};
936
937static int rockchip_configure_from_dt(struct device *dev,
938 struct device_node *np,
939 struct rockchip_thermal_data *thermal)
940{
941 u32 shut_temp, tshut_mode, tshut_polarity;
942
943 if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) {
944 dev_warn(dev,
Caesar Wang437df212015-11-09 12:48:58 +0800945 "Missing tshut temp property, using default %d\n",
Caesar Wangcbac8f632014-11-24 12:58:59 +0800946 thermal->chip->tshut_temp);
947 thermal->tshut_temp = thermal->chip->tshut_temp;
948 } else {
Caesar Wang43b4eb92016-02-15 15:33:28 +0800949 if (shut_temp > INT_MAX) {
950 dev_err(dev, "Invalid tshut temperature specified: %d\n",
951 shut_temp);
952 return -ERANGE;
953 }
Caesar Wangcbac8f632014-11-24 12:58:59 +0800954 thermal->tshut_temp = shut_temp;
955 }
956
Caesar Wangcbac8f632014-11-24 12:58:59 +0800957 if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) {
958 dev_warn(dev,
959 "Missing tshut mode property, using default (%s)\n",
960 thermal->chip->tshut_mode == TSHUT_MODE_GPIO ?
961 "gpio" : "cru");
962 thermal->tshut_mode = thermal->chip->tshut_mode;
963 } else {
964 thermal->tshut_mode = tshut_mode;
965 }
966
967 if (thermal->tshut_mode > 1) {
968 dev_err(dev, "Invalid tshut mode specified: %d\n",
969 thermal->tshut_mode);
970 return -EINVAL;
971 }
972
973 if (of_property_read_u32(np, "rockchip,hw-tshut-polarity",
974 &tshut_polarity)) {
975 dev_warn(dev,
976 "Missing tshut-polarity property, using default (%s)\n",
977 thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ?
978 "low" : "high");
979 thermal->tshut_polarity = thermal->chip->tshut_polarity;
980 } else {
981 thermal->tshut_polarity = tshut_polarity;
982 }
983
984 if (thermal->tshut_polarity > 1) {
985 dev_err(dev, "Invalid tshut-polarity specified: %d\n",
986 thermal->tshut_polarity);
987 return -EINVAL;
988 }
989
Caesar Wangb9484762016-04-18 11:35:56 +0800990 /* The tsadc wont to handle the error in here since some SoCs didn't
991 * need this property.
992 */
993 thermal->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
Shawn Lince62aba2016-10-11 10:07:34 +0800994 if (IS_ERR(thermal->grf))
995 dev_warn(dev, "Missing rockchip,grf property\n");
Caesar Wangb9484762016-04-18 11:35:56 +0800996
Caesar Wangcbac8f632014-11-24 12:58:59 +0800997 return 0;
998}
999
1000static int
1001rockchip_thermal_register_sensor(struct platform_device *pdev,
1002 struct rockchip_thermal_data *thermal,
1003 struct rockchip_thermal_sensor *sensor,
Caesar Wang1d98b6182015-11-05 13:17:58 +08001004 int id)
Caesar Wangcbac8f632014-11-24 12:58:59 +08001005{
1006 const struct rockchip_tsadc_chip *tsadc = thermal->chip;
1007 int error;
1008
1009 tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode);
Caesar Wangd3530492016-12-12 19:05:33 +08001010
1011 error = tsadc->set_tshut_temp(&tsadc->table, id, thermal->regs,
Caesar Wangce741102015-11-09 12:48:56 +08001012 thermal->tshut_temp);
Caesar Wangd3530492016-12-12 19:05:33 +08001013 if (error)
1014 dev_err(&pdev->dev, "%s: invalid tshut=%d, error=%d\n",
1015 __func__, thermal->tshut_temp, error);
Caesar Wangcbac8f632014-11-24 12:58:59 +08001016
1017 sensor->thermal = thermal;
1018 sensor->id = id;
Eduardo Valentin2633ad12016-03-09 13:10:28 -08001019 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev, id,
1020 sensor, &rockchip_of_thermal_ops);
Caesar Wangcbac8f632014-11-24 12:58:59 +08001021 if (IS_ERR(sensor->tzd)) {
1022 error = PTR_ERR(sensor->tzd);
1023 dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
1024 id, error);
1025 return error;
1026 }
1027
1028 return 0;
1029}
1030
Caesar Wang13c1cfd2015-12-03 16:48:39 +08001031/**
Caesar Wangcbac8f632014-11-24 12:58:59 +08001032 * Reset TSADC Controller, reset all tsadc registers.
1033 */
1034static void rockchip_thermal_reset_controller(struct reset_control *reset)
1035{
1036 reset_control_assert(reset);
1037 usleep_range(10, 20);
1038 reset_control_deassert(reset);
1039}
1040
1041static int rockchip_thermal_probe(struct platform_device *pdev)
1042{
1043 struct device_node *np = pdev->dev.of_node;
1044 struct rockchip_thermal_data *thermal;
1045 const struct of_device_id *match;
1046 struct resource *res;
1047 int irq;
Eduardo Valentin2633ad12016-03-09 13:10:28 -08001048 int i;
Caesar Wangcbac8f632014-11-24 12:58:59 +08001049 int error;
1050
1051 match = of_match_node(of_rockchip_thermal_match, np);
1052 if (!match)
1053 return -ENXIO;
1054
1055 irq = platform_get_irq(pdev, 0);
1056 if (irq < 0) {
1057 dev_err(&pdev->dev, "no irq resource?\n");
1058 return -EINVAL;
1059 }
1060
1061 thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data),
1062 GFP_KERNEL);
1063 if (!thermal)
1064 return -ENOMEM;
1065
1066 thermal->pdev = pdev;
1067
1068 thermal->chip = (const struct rockchip_tsadc_chip *)match->data;
1069 if (!thermal->chip)
1070 return -EINVAL;
1071
1072 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1073 thermal->regs = devm_ioremap_resource(&pdev->dev, res);
1074 if (IS_ERR(thermal->regs))
1075 return PTR_ERR(thermal->regs);
1076
1077 thermal->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb");
1078 if (IS_ERR(thermal->reset)) {
1079 error = PTR_ERR(thermal->reset);
1080 dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
1081 return error;
1082 }
1083
1084 thermal->clk = devm_clk_get(&pdev->dev, "tsadc");
1085 if (IS_ERR(thermal->clk)) {
1086 error = PTR_ERR(thermal->clk);
1087 dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
1088 return error;
1089 }
1090
1091 thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
1092 if (IS_ERR(thermal->pclk)) {
Dan Carpenter0d0a2bf2015-04-21 12:34:10 +03001093 error = PTR_ERR(thermal->pclk);
Caesar Wangcbac8f632014-11-24 12:58:59 +08001094 dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
1095 error);
1096 return error;
1097 }
1098
1099 error = clk_prepare_enable(thermal->clk);
1100 if (error) {
1101 dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
1102 error);
1103 return error;
1104 }
1105
1106 error = clk_prepare_enable(thermal->pclk);
1107 if (error) {
1108 dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
1109 goto err_disable_clk;
1110 }
1111
1112 rockchip_thermal_reset_controller(thermal->reset);
1113
1114 error = rockchip_configure_from_dt(&pdev->dev, np, thermal);
1115 if (error) {
1116 dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
1117 error);
1118 goto err_disable_pclk;
1119 }
1120
Caesar Wangb9484762016-04-18 11:35:56 +08001121 thermal->chip->initialize(thermal->grf, thermal->regs,
1122 thermal->tshut_polarity);
Caesar Wangcbac8f632014-11-24 12:58:59 +08001123
Caesar Wang1d98b6182015-11-05 13:17:58 +08001124 for (i = 0; i < thermal->chip->chn_num; i++) {
1125 error = rockchip_thermal_register_sensor(pdev, thermal,
1126 &thermal->sensors[i],
1127 thermal->chip->chn_id[i]);
1128 if (error) {
1129 dev_err(&pdev->dev,
1130 "failed to register sensor[%d] : error = %d\n",
1131 i, error);
Caesar Wang1d98b6182015-11-05 13:17:58 +08001132 goto err_disable_pclk;
1133 }
Caesar Wangcbac8f632014-11-24 12:58:59 +08001134 }
1135
1136 error = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1137 &rockchip_thermal_alarm_irq_thread,
1138 IRQF_ONESHOT,
1139 "rockchip_thermal", thermal);
1140 if (error) {
1141 dev_err(&pdev->dev,
1142 "failed to request tsadc irq: %d\n", error);
Eduardo Valentin2633ad12016-03-09 13:10:28 -08001143 goto err_disable_pclk;
Caesar Wangcbac8f632014-11-24 12:58:59 +08001144 }
1145
1146 thermal->chip->control(thermal->regs, true);
1147
Caesar Wang1d98b6182015-11-05 13:17:58 +08001148 for (i = 0; i < thermal->chip->chn_num; i++)
Caesar Wangcbac8f632014-11-24 12:58:59 +08001149 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
1150
1151 platform_set_drvdata(pdev, thermal);
1152
1153 return 0;
1154
Caesar Wangcbac8f632014-11-24 12:58:59 +08001155err_disable_pclk:
1156 clk_disable_unprepare(thermal->pclk);
1157err_disable_clk:
1158 clk_disable_unprepare(thermal->clk);
1159
1160 return error;
1161}
1162
1163static int rockchip_thermal_remove(struct platform_device *pdev)
1164{
1165 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
1166 int i;
1167
Caesar Wang1d98b6182015-11-05 13:17:58 +08001168 for (i = 0; i < thermal->chip->chn_num; i++) {
Caesar Wangcbac8f632014-11-24 12:58:59 +08001169 struct rockchip_thermal_sensor *sensor = &thermal->sensors[i];
1170
1171 rockchip_thermal_toggle_sensor(sensor, false);
Caesar Wangcbac8f632014-11-24 12:58:59 +08001172 }
1173
1174 thermal->chip->control(thermal->regs, false);
1175
1176 clk_disable_unprepare(thermal->pclk);
1177 clk_disable_unprepare(thermal->clk);
1178
1179 return 0;
1180}
1181
1182static int __maybe_unused rockchip_thermal_suspend(struct device *dev)
1183{
1184 struct platform_device *pdev = to_platform_device(dev);
1185 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
1186 int i;
1187
Caesar Wang1d98b6182015-11-05 13:17:58 +08001188 for (i = 0; i < thermal->chip->chn_num; i++)
Caesar Wangcbac8f632014-11-24 12:58:59 +08001189 rockchip_thermal_toggle_sensor(&thermal->sensors[i], false);
1190
1191 thermal->chip->control(thermal->regs, false);
1192
1193 clk_disable(thermal->pclk);
1194 clk_disable(thermal->clk);
1195
Caesar Wang7e38a5b2015-10-23 19:25:27 +08001196 pinctrl_pm_select_sleep_state(dev);
1197
Caesar Wangcbac8f632014-11-24 12:58:59 +08001198 return 0;
1199}
1200
1201static int __maybe_unused rockchip_thermal_resume(struct device *dev)
1202{
1203 struct platform_device *pdev = to_platform_device(dev);
1204 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
1205 int i;
1206 int error;
1207
1208 error = clk_enable(thermal->clk);
1209 if (error)
1210 return error;
1211
1212 error = clk_enable(thermal->pclk);
Shawn Linab5b52f2016-04-18 11:35:53 +08001213 if (error) {
1214 clk_disable(thermal->clk);
Caesar Wangcbac8f632014-11-24 12:58:59 +08001215 return error;
Shawn Linab5b52f2016-04-18 11:35:53 +08001216 }
Caesar Wangcbac8f632014-11-24 12:58:59 +08001217
1218 rockchip_thermal_reset_controller(thermal->reset);
1219
Caesar Wangb9484762016-04-18 11:35:56 +08001220 thermal->chip->initialize(thermal->grf, thermal->regs,
1221 thermal->tshut_polarity);
Caesar Wangcbac8f632014-11-24 12:58:59 +08001222
Caesar Wang1d98b6182015-11-05 13:17:58 +08001223 for (i = 0; i < thermal->chip->chn_num; i++) {
1224 int id = thermal->sensors[i].id;
Caesar Wangcbac8f632014-11-24 12:58:59 +08001225
1226 thermal->chip->set_tshut_mode(id, thermal->regs,
1227 thermal->tshut_mode);
Caesar Wangd3530492016-12-12 19:05:33 +08001228
1229 error = thermal->chip->set_tshut_temp(&thermal->chip->table,
Caesar Wangce741102015-11-09 12:48:56 +08001230 id, thermal->regs,
Caesar Wangcbac8f632014-11-24 12:58:59 +08001231 thermal->tshut_temp);
Caesar Wangd3530492016-12-12 19:05:33 +08001232 if (error)
1233 dev_err(&pdev->dev, "%s: invalid tshut=%d, error=%d\n",
1234 __func__, thermal->tshut_temp, error);
Caesar Wangcbac8f632014-11-24 12:58:59 +08001235 }
1236
1237 thermal->chip->control(thermal->regs, true);
1238
Caesar Wang1d98b6182015-11-05 13:17:58 +08001239 for (i = 0; i < thermal->chip->chn_num; i++)
Caesar Wangcbac8f632014-11-24 12:58:59 +08001240 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
1241
Caesar Wang7e38a5b2015-10-23 19:25:27 +08001242 pinctrl_pm_select_default_state(dev);
1243
Caesar Wangcbac8f632014-11-24 12:58:59 +08001244 return 0;
1245}
1246
1247static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops,
1248 rockchip_thermal_suspend, rockchip_thermal_resume);
1249
1250static struct platform_driver rockchip_thermal_driver = {
1251 .driver = {
1252 .name = "rockchip-thermal",
Caesar Wangcbac8f632014-11-24 12:58:59 +08001253 .pm = &rockchip_thermal_pm_ops,
1254 .of_match_table = of_rockchip_thermal_match,
1255 },
1256 .probe = rockchip_thermal_probe,
1257 .remove = rockchip_thermal_remove,
1258};
1259
1260module_platform_driver(rockchip_thermal_driver);
1261
1262MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
1263MODULE_AUTHOR("Rockchip, Inc.");
1264MODULE_LICENSE("GPL v2");
1265MODULE_ALIAS("platform:rockchip-thermal");