blob: c4111a98f1a79570c9eb98e7d0fcf9dbcbf2ec67 [file] [log] [blame]
kongxinwei9a5238a2015-05-20 19:16:37 +08001/*
2 * Hisilicon thermal sensor driver
3 *
4 * Copyright (c) 2014-2015 Hisilicon Limited.
5 * Copyright (c) 2014-2015 Linaro Limited.
6 *
7 * Xinwei Kong <kong.kongxinwei@hisilicon.com>
8 * Leo Yan <leo.yan@linaro.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
15 * kind, whether express or implied; without even the implied warranty
16 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#include <linux/cpufreq.h>
21#include <linux/delay.h>
22#include <linux/interrupt.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <linux/io.h>
Kevin Wangtaoa160a462017-10-22 10:54:34 +020026#include <linux/of_device.h>
kongxinwei9a5238a2015-05-20 19:16:37 +080027
28#include "thermal_core.h"
29
Kevin Wangtao5ed82b72017-10-22 10:54:33 +020030#define HI6220_TEMP0_LAG (0x0)
31#define HI6220_TEMP0_TH (0x4)
32#define HI6220_TEMP0_RST_TH (0x8)
33#define HI6220_TEMP0_CFG (0xC)
Kevin Wangtaoa160a462017-10-22 10:54:34 +020034#define HI6220_TEMP0_CFG_SS_MSK (0xF000)
Kevin Wangtao5ed82b72017-10-22 10:54:33 +020035#define HI6220_TEMP0_CFG_HDAK_MSK (0x30)
36#define HI6220_TEMP0_EN (0x10)
37#define HI6220_TEMP0_INT_EN (0x14)
38#define HI6220_TEMP0_INT_CLR (0x18)
39#define HI6220_TEMP0_RST_MSK (0x1C)
40#define HI6220_TEMP0_VALUE (0x28)
kongxinwei9a5238a2015-05-20 19:16:37 +080041
Kevin Wangtao2bb60a82017-10-22 10:54:35 +020042#define HI3660_OFFSET(chan) ((chan) * 0x40)
43#define HI3660_TEMP(chan) (HI3660_OFFSET(chan) + 0x1C)
44#define HI3660_TH(chan) (HI3660_OFFSET(chan) + 0x20)
45#define HI3660_LAG(chan) (HI3660_OFFSET(chan) + 0x28)
46#define HI3660_INT_EN(chan) (HI3660_OFFSET(chan) + 0x2C)
47#define HI3660_INT_CLR(chan) (HI3660_OFFSET(chan) + 0x30)
48
Kevin Wangtao5ed82b72017-10-22 10:54:33 +020049#define HI6220_TEMP_BASE (-60000)
50#define HI6220_TEMP_RESET (100000)
51#define HI6220_TEMP_STEP (785)
Kevin Wangtaoa160a462017-10-22 10:54:34 +020052#define HI6220_TEMP_LAG (3500)
kongxinwei9a5238a2015-05-20 19:16:37 +080053
Kevin Wangtao2bb60a82017-10-22 10:54:35 +020054#define HI3660_TEMP_BASE (-63780)
55#define HI3660_TEMP_STEP (205)
56#define HI3660_TEMP_LAG (4000)
57
Daniel Lezcanoa849eec2018-09-25 11:03:05 +020058#define HI6220_CLUSTER0_SENSOR 2
Daniel Lezcanoce8c0702018-09-25 11:03:10 +020059#define HI6220_CLUSTER1_SENSOR 1
60
61#define HI3660_LITTLE_SENSOR 0
Daniel Lezcanoa849eec2018-09-25 11:03:05 +020062#define HI3660_BIG_SENSOR 1
Daniel Lezcanoce8c0702018-09-25 11:03:10 +020063#define HI3660_G3D_SENSOR 2
64#define HI3660_MODEM_SENSOR 3
kongxinwei9a5238a2015-05-20 19:16:37 +080065
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +020066struct hisi_thermal_data;
67
kongxinwei9a5238a2015-05-20 19:16:37 +080068struct hisi_thermal_sensor {
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +020069 struct hisi_thermal_data *data;
kongxinwei9a5238a2015-05-20 19:16:37 +080070 struct thermal_zone_device *tzd;
Daniel Lezcano2cffaef2018-09-25 11:03:07 +020071 const char *irq_name;
kongxinwei9a5238a2015-05-20 19:16:37 +080072 uint32_t id;
73 uint32_t thres_temp;
74};
75
Daniel Lezcanoc90aaec2018-09-25 11:02:59 +020076struct hisi_thermal_ops {
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +020077 int (*get_temp)(struct hisi_thermal_sensor *sensor);
78 int (*enable_sensor)(struct hisi_thermal_sensor *sensor);
79 int (*disable_sensor)(struct hisi_thermal_sensor *sensor);
80 int (*irq_handler)(struct hisi_thermal_sensor *sensor);
Daniel Lezcanoc90aaec2018-09-25 11:02:59 +020081 int (*probe)(struct hisi_thermal_data *data);
82};
83
84struct hisi_thermal_data {
85 const struct hisi_thermal_ops *ops;
Daniel Lezcano8c0ffc82018-09-25 11:03:03 +020086 struct hisi_thermal_sensor *sensor;
kongxinwei9a5238a2015-05-20 19:16:37 +080087 struct platform_device *pdev;
88 struct clk *clk;
kongxinwei9a5238a2015-05-20 19:16:37 +080089 void __iomem *regs;
Daniel Lezcano8c0ffc82018-09-25 11:03:03 +020090 int nr_sensors;
kongxinwei9a5238a2015-05-20 19:16:37 +080091};
92
Daniel Lezcano48880b92017-10-19 19:05:46 +020093/*
94 * The temperature computation on the tsensor is as follow:
95 * Unit: millidegree Celsius
Kevin Wangtaoe42bbe12017-10-19 19:05:57 +020096 * Step: 200/255 (0.7843)
Daniel Lezcano48880b92017-10-19 19:05:46 +020097 * Temperature base: -60°C
98 *
Kevin Wangtaoe42bbe12017-10-19 19:05:57 +020099 * The register is programmed in temperature steps, every step is 785
Daniel Lezcano48880b92017-10-19 19:05:46 +0200100 * millidegree and begins at -60 000 m°C
101 *
102 * The temperature from the steps:
103 *
Kevin Wangtaoe42bbe12017-10-19 19:05:57 +0200104 * Temp = TempBase + (steps x 785)
Daniel Lezcano48880b92017-10-19 19:05:46 +0200105 *
106 * and the steps from the temperature:
107 *
Kevin Wangtaoe42bbe12017-10-19 19:05:57 +0200108 * steps = (Temp - TempBase) / 785
Daniel Lezcano48880b92017-10-19 19:05:46 +0200109 *
110 */
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200111static inline int hi6220_thermal_step_to_temp(int step)
kongxinwei9a5238a2015-05-20 19:16:37 +0800112{
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200113 return HI6220_TEMP_BASE + (step * HI6220_TEMP_STEP);
kongxinwei9a5238a2015-05-20 19:16:37 +0800114}
115
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200116static inline int hi6220_thermal_temp_to_step(int temp)
kongxinwei9a5238a2015-05-20 19:16:37 +0800117{
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200118 return DIV_ROUND_UP(temp - HI6220_TEMP_BASE, HI6220_TEMP_STEP);
Daniel Lezcanodb2b0332017-10-19 19:05:47 +0200119}
120
Daniel Lezcano10d7e9a2017-10-19 19:05:51 +0200121/*
Kevin Wangtao2bb60a82017-10-22 10:54:35 +0200122 * for Hi3660,
123 * Step: 189/922 (0.205)
124 * Temperature base: -63.780°C
125 *
126 * The register is programmed in temperature steps, every step is 205
127 * millidegree and begins at -63 780 m°C
128 */
129static inline int hi3660_thermal_step_to_temp(int step)
130{
131 return HI3660_TEMP_BASE + step * HI3660_TEMP_STEP;
132}
133
134static inline int hi3660_thermal_temp_to_step(int temp)
135{
136 return DIV_ROUND_UP(temp - HI3660_TEMP_BASE, HI3660_TEMP_STEP);
137}
138
139/*
Daniel Lezcano10d7e9a2017-10-19 19:05:51 +0200140 * The lag register contains 5 bits encoding the temperature in steps.
141 *
142 * Each time the temperature crosses the threshold boundary, an
143 * interrupt is raised. It could be when the temperature is going
144 * above the threshold or below. However, if the temperature is
145 * fluctuating around this value due to the load, we can receive
146 * several interrupts which may not desired.
147 *
148 * We can setup a temperature representing the delta between the
149 * threshold and the current temperature when the temperature is
150 * decreasing.
151 *
152 * For instance: the lag register is 5°C, the threshold is 65°C, when
153 * the temperature reaches 65°C an interrupt is raised and when the
154 * temperature decrease to 65°C - 5°C another interrupt is raised.
155 *
156 * A very short lag can lead to an interrupt storm, a long lag
157 * increase the latency to react to the temperature changes. In our
158 * case, that is not really a problem as we are polling the
159 * temperature.
160 *
161 * [0:4] : lag register
162 *
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200163 * The temperature is coded in steps, cf. HI6220_TEMP_STEP.
Daniel Lezcano10d7e9a2017-10-19 19:05:51 +0200164 *
165 * Min : 0x00 : 0.0 °C
166 * Max : 0x1F : 24.3 °C
167 *
168 * The 'value' parameter is in milliCelsius.
169 */
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200170static inline void hi6220_thermal_set_lag(void __iomem *addr, int value)
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200171{
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200172 writel(DIV_ROUND_UP(value, HI6220_TEMP_STEP) & 0x1F,
173 addr + HI6220_TEMP0_LAG);
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200174}
175
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200176static inline void hi6220_thermal_alarm_clear(void __iomem *addr, int value)
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200177{
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200178 writel(value, addr + HI6220_TEMP0_INT_CLR);
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200179}
180
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200181static inline void hi6220_thermal_alarm_enable(void __iomem *addr, int value)
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200182{
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200183 writel(value, addr + HI6220_TEMP0_INT_EN);
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200184}
185
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200186static inline void hi6220_thermal_alarm_set(void __iomem *addr, int temp)
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200187{
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200188 writel(hi6220_thermal_temp_to_step(temp) | 0x0FFFFFF00,
189 addr + HI6220_TEMP0_TH);
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200190}
191
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200192static inline void hi6220_thermal_reset_set(void __iomem *addr, int temp)
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200193{
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200194 writel(hi6220_thermal_temp_to_step(temp), addr + HI6220_TEMP0_RST_TH);
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200195}
196
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200197static inline void hi6220_thermal_reset_enable(void __iomem *addr, int value)
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200198{
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200199 writel(value, addr + HI6220_TEMP0_RST_MSK);
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200200}
201
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200202static inline void hi6220_thermal_enable(void __iomem *addr, int value)
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200203{
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200204 writel(value, addr + HI6220_TEMP0_EN);
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200205}
206
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200207static inline int hi6220_thermal_get_temperature(void __iomem *addr)
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200208{
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200209 return hi6220_thermal_step_to_temp(readl(addr + HI6220_TEMP0_VALUE));
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200210}
211
Daniel Lezcanob4243152017-10-19 19:05:50 +0200212/*
Kevin Wangtao2bb60a82017-10-22 10:54:35 +0200213 * [0:6] lag register
214 *
215 * The temperature is coded in steps, cf. HI3660_TEMP_STEP.
216 *
217 * Min : 0x00 : 0.0 °C
218 * Max : 0x7F : 26.0 °C
219 *
220 */
221static inline void hi3660_thermal_set_lag(void __iomem *addr,
222 int id, int value)
223{
224 writel(DIV_ROUND_UP(value, HI3660_TEMP_STEP) & 0x7F,
225 addr + HI3660_LAG(id));
226}
227
228static inline void hi3660_thermal_alarm_clear(void __iomem *addr,
229 int id, int value)
230{
231 writel(value, addr + HI3660_INT_CLR(id));
232}
233
234static inline void hi3660_thermal_alarm_enable(void __iomem *addr,
235 int id, int value)
236{
237 writel(value, addr + HI3660_INT_EN(id));
238}
239
240static inline void hi3660_thermal_alarm_set(void __iomem *addr,
241 int id, int value)
242{
243 writel(value, addr + HI3660_TH(id));
244}
245
246static inline int hi3660_thermal_get_temperature(void __iomem *addr, int id)
247{
248 return hi3660_thermal_step_to_temp(readl(addr + HI3660_TEMP(id)));
249}
250
251/*
Daniel Lezcanob4243152017-10-19 19:05:50 +0200252 * Temperature configuration register - Sensor selection
253 *
254 * Bits [19:12]
255 *
256 * 0x0: local sensor (default)
257 * 0x1: remote sensor 1 (ACPU cluster 1)
258 * 0x2: remote sensor 2 (ACPU cluster 0)
259 * 0x3: remote sensor 3 (G3D)
260 */
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200261static inline void hi6220_thermal_sensor_select(void __iomem *addr, int sensor)
Daniel Lezcanob4243152017-10-19 19:05:50 +0200262{
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200263 writel((readl(addr + HI6220_TEMP0_CFG) & ~HI6220_TEMP0_CFG_SS_MSK) |
264 (sensor << 12), addr + HI6220_TEMP0_CFG);
Daniel Lezcanob4243152017-10-19 19:05:50 +0200265}
266
267/*
268 * Temperature configuration register - Hdak conversion polling interval
269 *
270 * Bits [5:4]
271 *
272 * 0x0 : 0.768 ms
273 * 0x1 : 6.144 ms
274 * 0x2 : 49.152 ms
275 * 0x3 : 393.216 ms
276 */
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200277static inline void hi6220_thermal_hdak_set(void __iomem *addr, int value)
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200278{
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200279 writel((readl(addr + HI6220_TEMP0_CFG) & ~HI6220_TEMP0_CFG_HDAK_MSK) |
280 (value << 4), addr + HI6220_TEMP0_CFG);
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200281}
282
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200283static int hi6220_thermal_irq_handler(struct hisi_thermal_sensor *sensor)
Kevin Wangtaoa160a462017-10-22 10:54:34 +0200284{
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200285 struct hisi_thermal_data *data = sensor->data;
286
Kevin Wangtaoa160a462017-10-22 10:54:34 +0200287 hi6220_thermal_alarm_clear(data->regs, 1);
288 return 0;
289}
290
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200291static int hi3660_thermal_irq_handler(struct hisi_thermal_sensor *sensor)
Kevin Wangtao2bb60a82017-10-22 10:54:35 +0200292{
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200293 struct hisi_thermal_data *data = sensor->data;
294
295 hi3660_thermal_alarm_clear(data->regs, sensor->id, 1);
Kevin Wangtao2bb60a82017-10-22 10:54:35 +0200296 return 0;
297}
298
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200299static int hi6220_thermal_get_temp(struct hisi_thermal_sensor *sensor)
Kevin Wangtaoa160a462017-10-22 10:54:34 +0200300{
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200301 struct hisi_thermal_data *data = sensor->data;
302
Kevin Wangtaoa160a462017-10-22 10:54:34 +0200303 return hi6220_thermal_get_temperature(data->regs);
304}
305
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200306static int hi3660_thermal_get_temp(struct hisi_thermal_sensor *sensor)
Kevin Wangtao2bb60a82017-10-22 10:54:35 +0200307{
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200308 struct hisi_thermal_data *data = sensor->data;
309
310 return hi3660_thermal_get_temperature(data->regs, sensor->id);
Kevin Wangtao2bb60a82017-10-22 10:54:35 +0200311}
312
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200313static int hi6220_thermal_disable_sensor(struct hisi_thermal_sensor *sensor)
kongxinwei9a5238a2015-05-20 19:16:37 +0800314{
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200315 struct hisi_thermal_data *data = sensor->data;
316
kongxinwei9a5238a2015-05-20 19:16:37 +0800317 /* disable sensor module */
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200318 hi6220_thermal_enable(data->regs, 0);
319 hi6220_thermal_alarm_enable(data->regs, 0);
320 hi6220_thermal_reset_enable(data->regs, 0);
Kevin Wangtao943c0f62017-10-19 19:05:56 +0200321
322 clk_disable_unprepare(data->clk);
Kevin Wangtaoa160a462017-10-22 10:54:34 +0200323
324 return 0;
kongxinwei9a5238a2015-05-20 19:16:37 +0800325}
326
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200327static int hi3660_thermal_disable_sensor(struct hisi_thermal_sensor *sensor)
Kevin Wangtao2bb60a82017-10-22 10:54:35 +0200328{
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200329 struct hisi_thermal_data *data = sensor->data;
330
Kevin Wangtao2bb60a82017-10-22 10:54:35 +0200331 /* disable sensor module */
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200332 hi3660_thermal_alarm_enable(data->regs, sensor->id, 0);
Kevin Wangtao2bb60a82017-10-22 10:54:35 +0200333 return 0;
334}
335
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200336static int hi6220_thermal_enable_sensor(struct hisi_thermal_sensor *sensor)
Kevin Wangtaoa0678da82017-10-22 10:54:32 +0200337{
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200338 struct hisi_thermal_data *data = sensor->data;
Kevin Wangtaoa0678da82017-10-22 10:54:32 +0200339 int ret;
340
341 /* enable clock for tsensor */
342 ret = clk_prepare_enable(data->clk);
343 if (ret)
344 return ret;
345
346 /* disable module firstly */
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200347 hi6220_thermal_reset_enable(data->regs, 0);
348 hi6220_thermal_enable(data->regs, 0);
Kevin Wangtaoa0678da82017-10-22 10:54:32 +0200349
350 /* select sensor id */
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200351 hi6220_thermal_sensor_select(data->regs, sensor->id);
Kevin Wangtaoa0678da82017-10-22 10:54:32 +0200352
353 /* setting the hdak time */
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200354 hi6220_thermal_hdak_set(data->regs, 0);
Kevin Wangtaoa0678da82017-10-22 10:54:32 +0200355
356 /* setting lag value between current temp and the threshold */
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200357 hi6220_thermal_set_lag(data->regs, HI6220_TEMP_LAG);
Kevin Wangtaoa0678da82017-10-22 10:54:32 +0200358
359 /* enable for interrupt */
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200360 hi6220_thermal_alarm_set(data->regs, sensor->thres_temp);
Kevin Wangtaoa0678da82017-10-22 10:54:32 +0200361
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200362 hi6220_thermal_reset_set(data->regs, HI6220_TEMP_RESET);
Kevin Wangtaoa0678da82017-10-22 10:54:32 +0200363
364 /* enable module */
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200365 hi6220_thermal_reset_enable(data->regs, 1);
366 hi6220_thermal_enable(data->regs, 1);
Kevin Wangtaoa0678da82017-10-22 10:54:32 +0200367
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200368 hi6220_thermal_alarm_clear(data->regs, 0);
369 hi6220_thermal_alarm_enable(data->regs, 1);
Kevin Wangtaoa0678da82017-10-22 10:54:32 +0200370
371 return 0;
372}
373
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200374static int hi3660_thermal_enable_sensor(struct hisi_thermal_sensor *sensor)
Kevin Wangtao2bb60a82017-10-22 10:54:35 +0200375{
376 unsigned int value;
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200377 struct hisi_thermal_data *data = sensor->data;
Kevin Wangtao2bb60a82017-10-22 10:54:35 +0200378
379 /* disable interrupt */
380 hi3660_thermal_alarm_enable(data->regs, sensor->id, 0);
381
382 /* setting lag value between current temp and the threshold */
383 hi3660_thermal_set_lag(data->regs, sensor->id, HI3660_TEMP_LAG);
384
385 /* set interrupt threshold */
386 value = hi3660_thermal_temp_to_step(sensor->thres_temp);
387 hi3660_thermal_alarm_set(data->regs, sensor->id, value);
388
389 /* enable interrupt */
390 hi3660_thermal_alarm_clear(data->regs, sensor->id, 1);
391 hi3660_thermal_alarm_enable(data->regs, sensor->id, 1);
392
393 return 0;
394}
395
Kevin Wangtaoa160a462017-10-22 10:54:34 +0200396static int hi6220_thermal_probe(struct hisi_thermal_data *data)
397{
398 struct platform_device *pdev = data->pdev;
399 struct device *dev = &pdev->dev;
Kevin Wangtaoa160a462017-10-22 10:54:34 +0200400 int ret;
401
Kevin Wangtaoa160a462017-10-22 10:54:34 +0200402 data->clk = devm_clk_get(dev, "thermal_clk");
403 if (IS_ERR(data->clk)) {
404 ret = PTR_ERR(data->clk);
405 if (ret != -EPROBE_DEFER)
406 dev_err(dev, "failed to get thermal clk: %d\n", ret);
407 return ret;
408 }
409
Daniel Lezcano8c0ffc82018-09-25 11:03:03 +0200410 data->sensor = devm_kzalloc(dev, sizeof(*data->sensor), GFP_KERNEL);
411 if (!data->sensor)
412 return -ENOMEM;
413
Daniel Lezcanoa849eec2018-09-25 11:03:05 +0200414 data->sensor[0].id = HI6220_CLUSTER0_SENSOR;
Daniel Lezcano2cffaef2018-09-25 11:03:07 +0200415 data->sensor[0].irq_name = "tsensor_intr";
Daniel Lezcano8c0ffc82018-09-25 11:03:03 +0200416 data->sensor[0].data = data;
417 data->nr_sensors = 1;
Kevin Wangtaoa160a462017-10-22 10:54:34 +0200418
419 return 0;
420}
421
Kevin Wangtao2bb60a82017-10-22 10:54:35 +0200422static int hi3660_thermal_probe(struct hisi_thermal_data *data)
423{
Daniel Lezcano8c0ffc82018-09-25 11:03:03 +0200424 struct platform_device *pdev = data->pdev;
425 struct device *dev = &pdev->dev;
426
Daniel Lezcano8c6c3682018-09-25 11:03:12 +0200427 data->nr_sensors = 2;
428
429 data->sensor = devm_kzalloc(dev, sizeof(*data->sensor) *
430 data->nr_sensors, GFP_KERNEL);
Daniel Lezcano8c0ffc82018-09-25 11:03:03 +0200431 if (!data->sensor)
432 return -ENOMEM;
433
Daniel Lezcanoa849eec2018-09-25 11:03:05 +0200434 data->sensor[0].id = HI3660_BIG_SENSOR;
Daniel Lezcano2cffaef2018-09-25 11:03:07 +0200435 data->sensor[0].irq_name = "tsensor_a73";
Daniel Lezcano8c0ffc82018-09-25 11:03:03 +0200436 data->sensor[0].data = data;
Daniel Lezcano8c6c3682018-09-25 11:03:12 +0200437
438 data->sensor[1].id = HI3660_LITTLE_SENSOR;
439 data->sensor[1].irq_name = "tsensor_a53";
440 data->sensor[1].data = data;
Kevin Wangtao2bb60a82017-10-22 10:54:35 +0200441
442 return 0;
443}
444
Daniel Lezcano81d7cb72017-10-19 19:05:54 +0200445static int hisi_thermal_get_temp(void *__data, int *temp)
kongxinwei9a5238a2015-05-20 19:16:37 +0800446{
Daniel Lezcano49e778d2018-09-25 11:03:01 +0200447 struct hisi_thermal_sensor *sensor = __data;
448 struct hisi_thermal_data *data = sensor->data;
kongxinwei9a5238a2015-05-20 19:16:37 +0800449
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200450 *temp = data->ops->get_temp(sensor);
kongxinwei9a5238a2015-05-20 19:16:37 +0800451
Daniel Lezcano8c6c3682018-09-25 11:03:12 +0200452 dev_dbg(&data->pdev->dev, "tzd=%p, id=%d, temp=%d, thres=%d\n",
453 sensor->tzd, sensor->id, *temp, sensor->thres_temp);
kongxinwei9a5238a2015-05-20 19:16:37 +0800454
455 return 0;
456}
457
Julia Lawall3fe156f2017-08-08 17:08:55 +0200458static const struct thermal_zone_of_device_ops hisi_of_thermal_ops = {
kongxinwei9a5238a2015-05-20 19:16:37 +0800459 .get_temp = hisi_thermal_get_temp,
460};
461
kongxinwei9a5238a2015-05-20 19:16:37 +0800462static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev)
463{
Daniel Lezcano7edc5e42018-09-25 11:03:04 +0200464 struct hisi_thermal_sensor *sensor = dev;
465 struct hisi_thermal_data *data = sensor->data;
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200466 int temp = 0;
kongxinwei9a5238a2015-05-20 19:16:37 +0800467
Daniel Lezcano9c9ae8d2018-09-25 11:03:00 +0200468 data->ops->irq_handler(sensor);
kongxinwei9a5238a2015-05-20 19:16:37 +0800469
Daniel Lezcano49e778d2018-09-25 11:03:01 +0200470 hisi_thermal_get_temp(sensor, &temp);
Daniel Lezcano10d7e9a2017-10-19 19:05:51 +0200471
472 if (temp >= sensor->thres_temp) {
Daniel Lezcano7edc5e42018-09-25 11:03:04 +0200473 dev_crit(&data->pdev->dev,
474 "sensor <%d> THERMAL ALARM: %d > %d\n",
475 sensor->id, temp, sensor->thres_temp);
Daniel Lezcano10d7e9a2017-10-19 19:05:51 +0200476
Daniel Lezcano7edc5e42018-09-25 11:03:04 +0200477 thermal_zone_device_update(sensor->tzd,
Daniel Lezcano10d7e9a2017-10-19 19:05:51 +0200478 THERMAL_EVENT_UNSPECIFIED);
479
Kevin Wangtao5ed82b72017-10-22 10:54:33 +0200480 } else {
Daniel Lezcano7edc5e42018-09-25 11:03:04 +0200481 dev_crit(&data->pdev->dev,
482 "sensor <%d> THERMAL ALARM stopped: %d < %d\n",
483 sensor->id, temp, sensor->thres_temp);
Daniel Lezcano10d7e9a2017-10-19 19:05:51 +0200484 }
kongxinwei9a5238a2015-05-20 19:16:37 +0800485
486 return IRQ_HANDLED;
487}
488
489static int hisi_thermal_register_sensor(struct platform_device *pdev,
Kevin Wangtaoa160a462017-10-22 10:54:34 +0200490 struct hisi_thermal_sensor *sensor)
kongxinwei9a5238a2015-05-20 19:16:37 +0800491{
492 int ret, i;
493 const struct thermal_trip *trip;
494
Eduardo Valentin44a520d2016-03-09 13:07:13 -0800495 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev,
Daniel Lezcano49e778d2018-09-25 11:03:01 +0200496 sensor->id, sensor,
Daniel Lezcano81d7cb72017-10-19 19:05:54 +0200497 &hisi_of_thermal_ops);
kongxinwei9a5238a2015-05-20 19:16:37 +0800498 if (IS_ERR(sensor->tzd)) {
499 ret = PTR_ERR(sensor->tzd);
Leo Yan439dc962016-03-29 19:27:12 +0800500 sensor->tzd = NULL;
kongxinwei9a5238a2015-05-20 19:16:37 +0800501 dev_err(&pdev->dev, "failed to register sensor id %d: %d\n",
502 sensor->id, ret);
503 return ret;
504 }
505
506 trip = of_thermal_get_trip_points(sensor->tzd);
507
508 for (i = 0; i < of_thermal_get_ntrips(sensor->tzd); i++) {
509 if (trip[i].type == THERMAL_TRIP_PASSIVE) {
Kevin Wangtaoe42bbe12017-10-19 19:05:57 +0200510 sensor->thres_temp = trip[i].temperature;
kongxinwei9a5238a2015-05-20 19:16:37 +0800511 break;
512 }
513 }
514
515 return 0;
516}
517
Daniel Lezcanoc90aaec2018-09-25 11:02:59 +0200518static const struct hisi_thermal_ops hi6220_ops = {
519 .get_temp = hi6220_thermal_get_temp,
520 .enable_sensor = hi6220_thermal_enable_sensor,
521 .disable_sensor = hi6220_thermal_disable_sensor,
522 .irq_handler = hi6220_thermal_irq_handler,
523 .probe = hi6220_thermal_probe,
524};
525
526static const struct hisi_thermal_ops hi3660_ops = {
527 .get_temp = hi3660_thermal_get_temp,
528 .enable_sensor = hi3660_thermal_enable_sensor,
529 .disable_sensor = hi3660_thermal_disable_sensor,
530 .irq_handler = hi3660_thermal_irq_handler,
531 .probe = hi3660_thermal_probe,
532};
533
kongxinwei9a5238a2015-05-20 19:16:37 +0800534static const struct of_device_id of_hisi_thermal_match[] = {
Kevin Wangtao2bb60a82017-10-22 10:54:35 +0200535 {
536 .compatible = "hisilicon,tsensor",
Daniel Lezcanoc90aaec2018-09-25 11:02:59 +0200537 .data = &hi6220_ops,
Kevin Wangtao2bb60a82017-10-22 10:54:35 +0200538 },
539 {
540 .compatible = "hisilicon,hi3660-tsensor",
Daniel Lezcanoc90aaec2018-09-25 11:02:59 +0200541 .data = &hi3660_ops,
Kevin Wangtao2bb60a82017-10-22 10:54:35 +0200542 },
kongxinwei9a5238a2015-05-20 19:16:37 +0800543 { /* end */ }
544};
545MODULE_DEVICE_TABLE(of, of_hisi_thermal_match);
546
547static void hisi_thermal_toggle_sensor(struct hisi_thermal_sensor *sensor,
548 bool on)
549{
550 struct thermal_zone_device *tzd = sensor->tzd;
551
552 tzd->ops->set_mode(tzd,
553 on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
554}
555
556static int hisi_thermal_probe(struct platform_device *pdev)
557{
558 struct hisi_thermal_data *data;
Kevin Wangtaoa160a462017-10-22 10:54:34 +0200559 struct device *dev = &pdev->dev;
Daniel Lezcano9bb4ec82018-09-25 11:03:02 +0200560 struct resource *res;
Daniel Lezcano7edc5e42018-09-25 11:03:04 +0200561 int i, ret;
kongxinwei9a5238a2015-05-20 19:16:37 +0800562
Kevin Wangtaoa160a462017-10-22 10:54:34 +0200563 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
kongxinwei9a5238a2015-05-20 19:16:37 +0800564 if (!data)
565 return -ENOMEM;
566
kongxinwei9a5238a2015-05-20 19:16:37 +0800567 data->pdev = pdev;
kongxinwei9a5238a2015-05-20 19:16:37 +0800568 platform_set_drvdata(pdev, data);
Daniel Lezcanoc90aaec2018-09-25 11:02:59 +0200569 data->ops = of_device_get_match_data(dev);
kongxinwei9a5238a2015-05-20 19:16:37 +0800570
Daniel Lezcano9bb4ec82018-09-25 11:03:02 +0200571 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
572 data->regs = devm_ioremap_resource(dev, res);
573 if (IS_ERR(data->regs)) {
574 dev_err(dev, "failed to get io address\n");
575 return PTR_ERR(data->regs);
576 }
577
Daniel Lezcanoc90aaec2018-09-25 11:02:59 +0200578 ret = data->ops->probe(data);
Kevin Wangtaoa160a462017-10-22 10:54:34 +0200579 if (ret)
580 return ret;
581
Daniel Lezcano7edc5e42018-09-25 11:03:04 +0200582 for (i = 0; i < data->nr_sensors; i++) {
583 struct hisi_thermal_sensor *sensor = &data->sensor[i];
kongxinwei9a5238a2015-05-20 19:16:37 +0800584
Daniel Lezcano7edc5e42018-09-25 11:03:04 +0200585 ret = hisi_thermal_register_sensor(pdev, sensor);
586 if (ret) {
587 dev_err(dev, "failed to register thermal sensor: %d\n",
588 ret);
589 return ret;
590 }
Daniel Lezcanoff4ec292017-10-19 19:05:44 +0200591
Daniel Lezcanoa18e83e2018-09-25 11:03:09 +0200592 ret = platform_get_irq_byname(pdev, sensor->irq_name);
593 if (ret < 0)
594 return ret;
Daniel Lezcano7edc5e42018-09-25 11:03:04 +0200595
Daniel Lezcanoa18e83e2018-09-25 11:03:09 +0200596 ret = devm_request_threaded_irq(dev, ret, NULL,
Daniel Lezcano7edc5e42018-09-25 11:03:04 +0200597 hisi_thermal_alarm_irq_thread,
Daniel Lezcano2cffaef2018-09-25 11:03:07 +0200598 IRQF_ONESHOT, sensor->irq_name,
Daniel Lezcano7edc5e42018-09-25 11:03:04 +0200599 sensor);
Kevin Wangtaoa160a462017-10-22 10:54:34 +0200600 if (ret < 0) {
Daniel Lezcanoa18e83e2018-09-25 11:03:09 +0200601 dev_err(dev, "Failed to request alarm irq: %d\n", ret);
Kevin Wangtaoa160a462017-10-22 10:54:34 +0200602 return ret;
603 }
Daniel Lezcano2cb4de72017-10-19 19:05:45 +0200604
Daniel Lezcano7edc5e42018-09-25 11:03:04 +0200605 ret = data->ops->enable_sensor(sensor);
606 if (ret) {
607 dev_err(dev, "Failed to setup the sensor: %d\n", ret);
608 return ret;
609 }
610
611 hisi_thermal_toggle_sensor(sensor, true);
612 }
Daniel Lezcanoc176b102017-10-19 19:05:43 +0200613
kongxinwei9a5238a2015-05-20 19:16:37 +0800614 return 0;
kongxinwei9a5238a2015-05-20 19:16:37 +0800615}
616
617static int hisi_thermal_remove(struct platform_device *pdev)
618{
619 struct hisi_thermal_data *data = platform_get_drvdata(pdev);
Daniel Lezcano7edc5e42018-09-25 11:03:04 +0200620 int i;
kongxinwei9a5238a2015-05-20 19:16:37 +0800621
Daniel Lezcano7edc5e42018-09-25 11:03:04 +0200622 for (i = 0; i < data->nr_sensors; i++) {
623 struct hisi_thermal_sensor *sensor = &data->sensor[i];
Kevin Wangtaoa160a462017-10-22 10:54:34 +0200624
Daniel Lezcano7edc5e42018-09-25 11:03:04 +0200625 hisi_thermal_toggle_sensor(sensor, false);
626 data->ops->disable_sensor(sensor);
627 }
kongxinwei9a5238a2015-05-20 19:16:37 +0800628
629 return 0;
630}
631
632#ifdef CONFIG_PM_SLEEP
633static int hisi_thermal_suspend(struct device *dev)
634{
635 struct hisi_thermal_data *data = dev_get_drvdata(dev);
Daniel Lezcano7edc5e42018-09-25 11:03:04 +0200636 int i;
kongxinwei9a5238a2015-05-20 19:16:37 +0800637
Daniel Lezcano7edc5e42018-09-25 11:03:04 +0200638 for (i = 0; i < data->nr_sensors; i++)
639 data->ops->disable_sensor(&data->sensor[i]);
kongxinwei9a5238a2015-05-20 19:16:37 +0800640
kongxinwei9a5238a2015-05-20 19:16:37 +0800641 return 0;
642}
643
644static int hisi_thermal_resume(struct device *dev)
645{
646 struct hisi_thermal_data *data = dev_get_drvdata(dev);
Daniel Lezcano7edc5e42018-09-25 11:03:04 +0200647 int i, ret = 0;
kongxinwei9a5238a2015-05-20 19:16:37 +0800648
Daniel Lezcano7edc5e42018-09-25 11:03:04 +0200649 for (i = 0; i < data->nr_sensors; i++)
650 ret |= data->ops->enable_sensor(&data->sensor[i]);
651
652 return ret;
kongxinwei9a5238a2015-05-20 19:16:37 +0800653}
654#endif
655
656static SIMPLE_DEV_PM_OPS(hisi_thermal_pm_ops,
657 hisi_thermal_suspend, hisi_thermal_resume);
658
659static struct platform_driver hisi_thermal_driver = {
660 .driver = {
661 .name = "hisi_thermal",
kongxinwei9a5238a2015-05-20 19:16:37 +0800662 .pm = &hisi_thermal_pm_ops,
663 .of_match_table = of_hisi_thermal_match,
664 },
665 .probe = hisi_thermal_probe,
666 .remove = hisi_thermal_remove,
667};
668
669module_platform_driver(hisi_thermal_driver);
670
671MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
672MODULE_AUTHOR("Leo Yan <leo.yan@linaro.org>");
673MODULE_DESCRIPTION("Hisilicon thermal driver");
674MODULE_LICENSE("GPL v2");