1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Hisilicon thermal sensor driver
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (c) 2014-2015 Hisilicon Limited.
5*4882a593Smuzhiyun * Copyright (c) 2014-2015 Linaro Limited.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Xinwei Kong <kong.kongxinwei@hisilicon.com>
8*4882a593Smuzhiyun * Leo Yan <leo.yan@linaro.org>
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify
11*4882a593Smuzhiyun * it under the terms of the GNU General Public License version 2 as
12*4882a593Smuzhiyun * published by the Free Software Foundation.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * This program is distributed "as is" WITHOUT ANY WARRANTY of any
15*4882a593Smuzhiyun * kind, whether express or implied; without even the implied warranty
16*4882a593Smuzhiyun * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17*4882a593Smuzhiyun * GNU General Public License for more details.
18*4882a593Smuzhiyun */
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #include <linux/cpufreq.h>
21*4882a593Smuzhiyun #include <linux/delay.h>
22*4882a593Smuzhiyun #include <linux/interrupt.h>
23*4882a593Smuzhiyun #include <linux/module.h>
24*4882a593Smuzhiyun #include <linux/platform_device.h>
25*4882a593Smuzhiyun #include <linux/io.h>
26*4882a593Smuzhiyun #include <linux/of_device.h>
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #include "thermal_core.h"
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #define HI6220_TEMP0_LAG (0x0)
31*4882a593Smuzhiyun #define HI6220_TEMP0_TH (0x4)
32*4882a593Smuzhiyun #define HI6220_TEMP0_RST_TH (0x8)
33*4882a593Smuzhiyun #define HI6220_TEMP0_CFG (0xC)
34*4882a593Smuzhiyun #define HI6220_TEMP0_CFG_SS_MSK (0xF000)
35*4882a593Smuzhiyun #define HI6220_TEMP0_CFG_HDAK_MSK (0x30)
36*4882a593Smuzhiyun #define HI6220_TEMP0_EN (0x10)
37*4882a593Smuzhiyun #define HI6220_TEMP0_INT_EN (0x14)
38*4882a593Smuzhiyun #define HI6220_TEMP0_INT_CLR (0x18)
39*4882a593Smuzhiyun #define HI6220_TEMP0_RST_MSK (0x1C)
40*4882a593Smuzhiyun #define HI6220_TEMP0_VALUE (0x28)
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun #define HI3660_OFFSET(chan) ((chan) * 0x40)
43*4882a593Smuzhiyun #define HI3660_TEMP(chan) (HI3660_OFFSET(chan) + 0x1C)
44*4882a593Smuzhiyun #define HI3660_TH(chan) (HI3660_OFFSET(chan) + 0x20)
45*4882a593Smuzhiyun #define HI3660_LAG(chan) (HI3660_OFFSET(chan) + 0x28)
46*4882a593Smuzhiyun #define HI3660_INT_EN(chan) (HI3660_OFFSET(chan) + 0x2C)
47*4882a593Smuzhiyun #define HI3660_INT_CLR(chan) (HI3660_OFFSET(chan) + 0x30)
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #define HI6220_TEMP_BASE (-60000)
50*4882a593Smuzhiyun #define HI6220_TEMP_RESET (100000)
51*4882a593Smuzhiyun #define HI6220_TEMP_STEP (785)
52*4882a593Smuzhiyun #define HI6220_TEMP_LAG (3500)
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun #define HI3660_TEMP_BASE (-63780)
55*4882a593Smuzhiyun #define HI3660_TEMP_STEP (205)
56*4882a593Smuzhiyun #define HI3660_TEMP_LAG (4000)
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun #define HI6220_CLUSTER0_SENSOR 2
59*4882a593Smuzhiyun #define HI6220_CLUSTER1_SENSOR 1
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun #define HI3660_LITTLE_SENSOR 0
62*4882a593Smuzhiyun #define HI3660_BIG_SENSOR 1
63*4882a593Smuzhiyun #define HI3660_G3D_SENSOR 2
64*4882a593Smuzhiyun #define HI3660_MODEM_SENSOR 3
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun struct hisi_thermal_data;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun struct hisi_thermal_sensor {
69*4882a593Smuzhiyun struct hisi_thermal_data *data;
70*4882a593Smuzhiyun struct thermal_zone_device *tzd;
71*4882a593Smuzhiyun const char *irq_name;
72*4882a593Smuzhiyun uint32_t id;
73*4882a593Smuzhiyun uint32_t thres_temp;
74*4882a593Smuzhiyun };
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun struct hisi_thermal_ops {
77*4882a593Smuzhiyun int (*get_temp)(struct hisi_thermal_sensor *sensor);
78*4882a593Smuzhiyun int (*enable_sensor)(struct hisi_thermal_sensor *sensor);
79*4882a593Smuzhiyun int (*disable_sensor)(struct hisi_thermal_sensor *sensor);
80*4882a593Smuzhiyun int (*irq_handler)(struct hisi_thermal_sensor *sensor);
81*4882a593Smuzhiyun int (*probe)(struct hisi_thermal_data *data);
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun struct hisi_thermal_data {
85*4882a593Smuzhiyun const struct hisi_thermal_ops *ops;
86*4882a593Smuzhiyun struct hisi_thermal_sensor *sensor;
87*4882a593Smuzhiyun struct platform_device *pdev;
88*4882a593Smuzhiyun struct clk *clk;
89*4882a593Smuzhiyun void __iomem *regs;
90*4882a593Smuzhiyun int nr_sensors;
91*4882a593Smuzhiyun };
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /*
94*4882a593Smuzhiyun * The temperature computation on the tsensor is as follow:
95*4882a593Smuzhiyun * Unit: millidegree Celsius
96*4882a593Smuzhiyun * Step: 200/255 (0.7843)
97*4882a593Smuzhiyun * Temperature base: -60°C
98*4882a593Smuzhiyun *
99*4882a593Smuzhiyun * The register is programmed in temperature steps, every step is 785
100*4882a593Smuzhiyun * millidegree and begins at -60 000 m°C
101*4882a593Smuzhiyun *
102*4882a593Smuzhiyun * The temperature from the steps:
103*4882a593Smuzhiyun *
104*4882a593Smuzhiyun * Temp = TempBase + (steps x 785)
105*4882a593Smuzhiyun *
106*4882a593Smuzhiyun * and the steps from the temperature:
107*4882a593Smuzhiyun *
108*4882a593Smuzhiyun * steps = (Temp - TempBase) / 785
109*4882a593Smuzhiyun *
110*4882a593Smuzhiyun */
hi6220_thermal_step_to_temp(int step)111*4882a593Smuzhiyun static inline int hi6220_thermal_step_to_temp(int step)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun return HI6220_TEMP_BASE + (step * HI6220_TEMP_STEP);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
hi6220_thermal_temp_to_step(int temp)116*4882a593Smuzhiyun static inline int hi6220_thermal_temp_to_step(int temp)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun return DIV_ROUND_UP(temp - HI6220_TEMP_BASE, HI6220_TEMP_STEP);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /*
122*4882a593Smuzhiyun * for Hi3660,
123*4882a593Smuzhiyun * Step: 189/922 (0.205)
124*4882a593Smuzhiyun * Temperature base: -63.780°C
125*4882a593Smuzhiyun *
126*4882a593Smuzhiyun * The register is programmed in temperature steps, every step is 205
127*4882a593Smuzhiyun * millidegree and begins at -63 780 m°C
128*4882a593Smuzhiyun */
hi3660_thermal_step_to_temp(int step)129*4882a593Smuzhiyun static inline int hi3660_thermal_step_to_temp(int step)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun return HI3660_TEMP_BASE + step * HI3660_TEMP_STEP;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun
hi3660_thermal_temp_to_step(int temp)134*4882a593Smuzhiyun static inline int hi3660_thermal_temp_to_step(int temp)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun return DIV_ROUND_UP(temp - HI3660_TEMP_BASE, HI3660_TEMP_STEP);
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /*
140*4882a593Smuzhiyun * The lag register contains 5 bits encoding the temperature in steps.
141*4882a593Smuzhiyun *
142*4882a593Smuzhiyun * Each time the temperature crosses the threshold boundary, an
143*4882a593Smuzhiyun * interrupt is raised. It could be when the temperature is going
144*4882a593Smuzhiyun * above the threshold or below. However, if the temperature is
145*4882a593Smuzhiyun * fluctuating around this value due to the load, we can receive
146*4882a593Smuzhiyun * several interrupts which may not desired.
147*4882a593Smuzhiyun *
148*4882a593Smuzhiyun * We can setup a temperature representing the delta between the
149*4882a593Smuzhiyun * threshold and the current temperature when the temperature is
150*4882a593Smuzhiyun * decreasing.
151*4882a593Smuzhiyun *
152*4882a593Smuzhiyun * For instance: the lag register is 5°C, the threshold is 65°C, when
153*4882a593Smuzhiyun * the temperature reaches 65°C an interrupt is raised and when the
154*4882a593Smuzhiyun * temperature decrease to 65°C - 5°C another interrupt is raised.
155*4882a593Smuzhiyun *
156*4882a593Smuzhiyun * A very short lag can lead to an interrupt storm, a long lag
157*4882a593Smuzhiyun * increase the latency to react to the temperature changes. In our
158*4882a593Smuzhiyun * case, that is not really a problem as we are polling the
159*4882a593Smuzhiyun * temperature.
160*4882a593Smuzhiyun *
161*4882a593Smuzhiyun * [0:4] : lag register
162*4882a593Smuzhiyun *
163*4882a593Smuzhiyun * The temperature is coded in steps, cf. HI6220_TEMP_STEP.
164*4882a593Smuzhiyun *
165*4882a593Smuzhiyun * Min : 0x00 : 0.0 °C
166*4882a593Smuzhiyun * Max : 0x1F : 24.3 °C
167*4882a593Smuzhiyun *
168*4882a593Smuzhiyun * The 'value' parameter is in milliCelsius.
169*4882a593Smuzhiyun */
hi6220_thermal_set_lag(void __iomem * addr,int value)170*4882a593Smuzhiyun static inline void hi6220_thermal_set_lag(void __iomem *addr, int value)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun writel(DIV_ROUND_UP(value, HI6220_TEMP_STEP) & 0x1F,
173*4882a593Smuzhiyun addr + HI6220_TEMP0_LAG);
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
hi6220_thermal_alarm_clear(void __iomem * addr,int value)176*4882a593Smuzhiyun static inline void hi6220_thermal_alarm_clear(void __iomem *addr, int value)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun writel(value, addr + HI6220_TEMP0_INT_CLR);
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
hi6220_thermal_alarm_enable(void __iomem * addr,int value)181*4882a593Smuzhiyun static inline void hi6220_thermal_alarm_enable(void __iomem *addr, int value)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun writel(value, addr + HI6220_TEMP0_INT_EN);
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
hi6220_thermal_alarm_set(void __iomem * addr,int temp)186*4882a593Smuzhiyun static inline void hi6220_thermal_alarm_set(void __iomem *addr, int temp)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun writel(hi6220_thermal_temp_to_step(temp) | 0x0FFFFFF00,
189*4882a593Smuzhiyun addr + HI6220_TEMP0_TH);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun
hi6220_thermal_reset_set(void __iomem * addr,int temp)192*4882a593Smuzhiyun static inline void hi6220_thermal_reset_set(void __iomem *addr, int temp)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun writel(hi6220_thermal_temp_to_step(temp), addr + HI6220_TEMP0_RST_TH);
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun
hi6220_thermal_reset_enable(void __iomem * addr,int value)197*4882a593Smuzhiyun static inline void hi6220_thermal_reset_enable(void __iomem *addr, int value)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun writel(value, addr + HI6220_TEMP0_RST_MSK);
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
hi6220_thermal_enable(void __iomem * addr,int value)202*4882a593Smuzhiyun static inline void hi6220_thermal_enable(void __iomem *addr, int value)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun writel(value, addr + HI6220_TEMP0_EN);
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
hi6220_thermal_get_temperature(void __iomem * addr)207*4882a593Smuzhiyun static inline int hi6220_thermal_get_temperature(void __iomem *addr)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun return hi6220_thermal_step_to_temp(readl(addr + HI6220_TEMP0_VALUE));
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun /*
213*4882a593Smuzhiyun * [0:6] lag register
214*4882a593Smuzhiyun *
215*4882a593Smuzhiyun * The temperature is coded in steps, cf. HI3660_TEMP_STEP.
216*4882a593Smuzhiyun *
217*4882a593Smuzhiyun * Min : 0x00 : 0.0 °C
218*4882a593Smuzhiyun * Max : 0x7F : 26.0 °C
219*4882a593Smuzhiyun *
220*4882a593Smuzhiyun */
hi3660_thermal_set_lag(void __iomem * addr,int id,int value)221*4882a593Smuzhiyun static inline void hi3660_thermal_set_lag(void __iomem *addr,
222*4882a593Smuzhiyun int id, int value)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun writel(DIV_ROUND_UP(value, HI3660_TEMP_STEP) & 0x7F,
225*4882a593Smuzhiyun addr + HI3660_LAG(id));
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun
hi3660_thermal_alarm_clear(void __iomem * addr,int id,int value)228*4882a593Smuzhiyun static inline void hi3660_thermal_alarm_clear(void __iomem *addr,
229*4882a593Smuzhiyun int id, int value)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun writel(value, addr + HI3660_INT_CLR(id));
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun
hi3660_thermal_alarm_enable(void __iomem * addr,int id,int value)234*4882a593Smuzhiyun static inline void hi3660_thermal_alarm_enable(void __iomem *addr,
235*4882a593Smuzhiyun int id, int value)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun writel(value, addr + HI3660_INT_EN(id));
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun
hi3660_thermal_alarm_set(void __iomem * addr,int id,int value)240*4882a593Smuzhiyun static inline void hi3660_thermal_alarm_set(void __iomem *addr,
241*4882a593Smuzhiyun int id, int value)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun writel(value, addr + HI3660_TH(id));
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
hi3660_thermal_get_temperature(void __iomem * addr,int id)246*4882a593Smuzhiyun static inline int hi3660_thermal_get_temperature(void __iomem *addr, int id)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun return hi3660_thermal_step_to_temp(readl(addr + HI3660_TEMP(id)));
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /*
252*4882a593Smuzhiyun * Temperature configuration register - Sensor selection
253*4882a593Smuzhiyun *
254*4882a593Smuzhiyun * Bits [19:12]
255*4882a593Smuzhiyun *
256*4882a593Smuzhiyun * 0x0: local sensor (default)
257*4882a593Smuzhiyun * 0x1: remote sensor 1 (ACPU cluster 1)
258*4882a593Smuzhiyun * 0x2: remote sensor 2 (ACPU cluster 0)
259*4882a593Smuzhiyun * 0x3: remote sensor 3 (G3D)
260*4882a593Smuzhiyun */
hi6220_thermal_sensor_select(void __iomem * addr,int sensor)261*4882a593Smuzhiyun static inline void hi6220_thermal_sensor_select(void __iomem *addr, int sensor)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun writel((readl(addr + HI6220_TEMP0_CFG) & ~HI6220_TEMP0_CFG_SS_MSK) |
264*4882a593Smuzhiyun (sensor << 12), addr + HI6220_TEMP0_CFG);
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun /*
268*4882a593Smuzhiyun * Temperature configuration register - Hdak conversion polling interval
269*4882a593Smuzhiyun *
270*4882a593Smuzhiyun * Bits [5:4]
271*4882a593Smuzhiyun *
272*4882a593Smuzhiyun * 0x0 : 0.768 ms
273*4882a593Smuzhiyun * 0x1 : 6.144 ms
274*4882a593Smuzhiyun * 0x2 : 49.152 ms
275*4882a593Smuzhiyun * 0x3 : 393.216 ms
276*4882a593Smuzhiyun */
hi6220_thermal_hdak_set(void __iomem * addr,int value)277*4882a593Smuzhiyun static inline void hi6220_thermal_hdak_set(void __iomem *addr, int value)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun writel((readl(addr + HI6220_TEMP0_CFG) & ~HI6220_TEMP0_CFG_HDAK_MSK) |
280*4882a593Smuzhiyun (value << 4), addr + HI6220_TEMP0_CFG);
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun
hi6220_thermal_irq_handler(struct hisi_thermal_sensor * sensor)283*4882a593Smuzhiyun static int hi6220_thermal_irq_handler(struct hisi_thermal_sensor *sensor)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun struct hisi_thermal_data *data = sensor->data;
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun hi6220_thermal_alarm_clear(data->regs, 1);
288*4882a593Smuzhiyun return 0;
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun
hi3660_thermal_irq_handler(struct hisi_thermal_sensor * sensor)291*4882a593Smuzhiyun static int hi3660_thermal_irq_handler(struct hisi_thermal_sensor *sensor)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun struct hisi_thermal_data *data = sensor->data;
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun hi3660_thermal_alarm_clear(data->regs, sensor->id, 1);
296*4882a593Smuzhiyun return 0;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun
hi6220_thermal_get_temp(struct hisi_thermal_sensor * sensor)299*4882a593Smuzhiyun static int hi6220_thermal_get_temp(struct hisi_thermal_sensor *sensor)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun struct hisi_thermal_data *data = sensor->data;
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun return hi6220_thermal_get_temperature(data->regs);
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
hi3660_thermal_get_temp(struct hisi_thermal_sensor * sensor)306*4882a593Smuzhiyun static int hi3660_thermal_get_temp(struct hisi_thermal_sensor *sensor)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun struct hisi_thermal_data *data = sensor->data;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun return hi3660_thermal_get_temperature(data->regs, sensor->id);
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun
hi6220_thermal_disable_sensor(struct hisi_thermal_sensor * sensor)313*4882a593Smuzhiyun static int hi6220_thermal_disable_sensor(struct hisi_thermal_sensor *sensor)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun struct hisi_thermal_data *data = sensor->data;
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun /* disable sensor module */
318*4882a593Smuzhiyun hi6220_thermal_enable(data->regs, 0);
319*4882a593Smuzhiyun hi6220_thermal_alarm_enable(data->regs, 0);
320*4882a593Smuzhiyun hi6220_thermal_reset_enable(data->regs, 0);
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun clk_disable_unprepare(data->clk);
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun return 0;
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun
hi3660_thermal_disable_sensor(struct hisi_thermal_sensor * sensor)327*4882a593Smuzhiyun static int hi3660_thermal_disable_sensor(struct hisi_thermal_sensor *sensor)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun struct hisi_thermal_data *data = sensor->data;
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun /* disable sensor module */
332*4882a593Smuzhiyun hi3660_thermal_alarm_enable(data->regs, sensor->id, 0);
333*4882a593Smuzhiyun return 0;
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun
hi6220_thermal_enable_sensor(struct hisi_thermal_sensor * sensor)336*4882a593Smuzhiyun static int hi6220_thermal_enable_sensor(struct hisi_thermal_sensor *sensor)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun struct hisi_thermal_data *data = sensor->data;
339*4882a593Smuzhiyun int ret;
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun /* enable clock for tsensor */
342*4882a593Smuzhiyun ret = clk_prepare_enable(data->clk);
343*4882a593Smuzhiyun if (ret)
344*4882a593Smuzhiyun return ret;
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun /* disable module firstly */
347*4882a593Smuzhiyun hi6220_thermal_reset_enable(data->regs, 0);
348*4882a593Smuzhiyun hi6220_thermal_enable(data->regs, 0);
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun /* select sensor id */
351*4882a593Smuzhiyun hi6220_thermal_sensor_select(data->regs, sensor->id);
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun /* setting the hdak time */
354*4882a593Smuzhiyun hi6220_thermal_hdak_set(data->regs, 0);
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun /* setting lag value between current temp and the threshold */
357*4882a593Smuzhiyun hi6220_thermal_set_lag(data->regs, HI6220_TEMP_LAG);
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun /* enable for interrupt */
360*4882a593Smuzhiyun hi6220_thermal_alarm_set(data->regs, sensor->thres_temp);
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun hi6220_thermal_reset_set(data->regs, HI6220_TEMP_RESET);
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun /* enable module */
365*4882a593Smuzhiyun hi6220_thermal_reset_enable(data->regs, 1);
366*4882a593Smuzhiyun hi6220_thermal_enable(data->regs, 1);
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun hi6220_thermal_alarm_clear(data->regs, 0);
369*4882a593Smuzhiyun hi6220_thermal_alarm_enable(data->regs, 1);
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun return 0;
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun
hi3660_thermal_enable_sensor(struct hisi_thermal_sensor * sensor)374*4882a593Smuzhiyun static int hi3660_thermal_enable_sensor(struct hisi_thermal_sensor *sensor)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun unsigned int value;
377*4882a593Smuzhiyun struct hisi_thermal_data *data = sensor->data;
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun /* disable interrupt */
380*4882a593Smuzhiyun hi3660_thermal_alarm_enable(data->regs, sensor->id, 0);
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun /* setting lag value between current temp and the threshold */
383*4882a593Smuzhiyun hi3660_thermal_set_lag(data->regs, sensor->id, HI3660_TEMP_LAG);
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun /* set interrupt threshold */
386*4882a593Smuzhiyun value = hi3660_thermal_temp_to_step(sensor->thres_temp);
387*4882a593Smuzhiyun hi3660_thermal_alarm_set(data->regs, sensor->id, value);
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun /* enable interrupt */
390*4882a593Smuzhiyun hi3660_thermal_alarm_clear(data->regs, sensor->id, 1);
391*4882a593Smuzhiyun hi3660_thermal_alarm_enable(data->regs, sensor->id, 1);
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun return 0;
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun
hi6220_thermal_probe(struct hisi_thermal_data * data)396*4882a593Smuzhiyun static int hi6220_thermal_probe(struct hisi_thermal_data *data)
397*4882a593Smuzhiyun {
398*4882a593Smuzhiyun struct platform_device *pdev = data->pdev;
399*4882a593Smuzhiyun struct device *dev = &pdev->dev;
400*4882a593Smuzhiyun int ret;
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun data->clk = devm_clk_get(dev, "thermal_clk");
403*4882a593Smuzhiyun if (IS_ERR(data->clk)) {
404*4882a593Smuzhiyun ret = PTR_ERR(data->clk);
405*4882a593Smuzhiyun if (ret != -EPROBE_DEFER)
406*4882a593Smuzhiyun dev_err(dev, "failed to get thermal clk: %d\n", ret);
407*4882a593Smuzhiyun return ret;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun data->sensor = devm_kzalloc(dev, sizeof(*data->sensor), GFP_KERNEL);
411*4882a593Smuzhiyun if (!data->sensor)
412*4882a593Smuzhiyun return -ENOMEM;
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun data->sensor[0].id = HI6220_CLUSTER0_SENSOR;
415*4882a593Smuzhiyun data->sensor[0].irq_name = "tsensor_intr";
416*4882a593Smuzhiyun data->sensor[0].data = data;
417*4882a593Smuzhiyun data->nr_sensors = 1;
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun return 0;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun
hi3660_thermal_probe(struct hisi_thermal_data * data)422*4882a593Smuzhiyun static int hi3660_thermal_probe(struct hisi_thermal_data *data)
423*4882a593Smuzhiyun {
424*4882a593Smuzhiyun struct platform_device *pdev = data->pdev;
425*4882a593Smuzhiyun struct device *dev = &pdev->dev;
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun data->nr_sensors = 1;
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun data->sensor = devm_kzalloc(dev, sizeof(*data->sensor) *
430*4882a593Smuzhiyun data->nr_sensors, GFP_KERNEL);
431*4882a593Smuzhiyun if (!data->sensor)
432*4882a593Smuzhiyun return -ENOMEM;
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun data->sensor[0].id = HI3660_BIG_SENSOR;
435*4882a593Smuzhiyun data->sensor[0].irq_name = "tsensor_a73";
436*4882a593Smuzhiyun data->sensor[0].data = data;
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun data->sensor[1].id = HI3660_LITTLE_SENSOR;
439*4882a593Smuzhiyun data->sensor[1].irq_name = "tsensor_a53";
440*4882a593Smuzhiyun data->sensor[1].data = data;
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun return 0;
443*4882a593Smuzhiyun }
444*4882a593Smuzhiyun
hisi_thermal_get_temp(void * __data,int * temp)445*4882a593Smuzhiyun static int hisi_thermal_get_temp(void *__data, int *temp)
446*4882a593Smuzhiyun {
447*4882a593Smuzhiyun struct hisi_thermal_sensor *sensor = __data;
448*4882a593Smuzhiyun struct hisi_thermal_data *data = sensor->data;
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun *temp = data->ops->get_temp(sensor);
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun dev_dbg(&data->pdev->dev, "tzd=%p, id=%d, temp=%d, thres=%d\n",
453*4882a593Smuzhiyun sensor->tzd, sensor->id, *temp, sensor->thres_temp);
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun return 0;
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun static const struct thermal_zone_of_device_ops hisi_of_thermal_ops = {
459*4882a593Smuzhiyun .get_temp = hisi_thermal_get_temp,
460*4882a593Smuzhiyun };
461*4882a593Smuzhiyun
hisi_thermal_alarm_irq_thread(int irq,void * dev)462*4882a593Smuzhiyun static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev)
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun struct hisi_thermal_sensor *sensor = dev;
465*4882a593Smuzhiyun struct hisi_thermal_data *data = sensor->data;
466*4882a593Smuzhiyun int temp = 0;
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun data->ops->irq_handler(sensor);
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun hisi_thermal_get_temp(sensor, &temp);
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun if (temp >= sensor->thres_temp) {
473*4882a593Smuzhiyun dev_crit(&data->pdev->dev,
474*4882a593Smuzhiyun "sensor <%d> THERMAL ALARM: %d > %d\n",
475*4882a593Smuzhiyun sensor->id, temp, sensor->thres_temp);
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun thermal_zone_device_update(sensor->tzd,
478*4882a593Smuzhiyun THERMAL_EVENT_UNSPECIFIED);
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun } else {
481*4882a593Smuzhiyun dev_crit(&data->pdev->dev,
482*4882a593Smuzhiyun "sensor <%d> THERMAL ALARM stopped: %d < %d\n",
483*4882a593Smuzhiyun sensor->id, temp, sensor->thres_temp);
484*4882a593Smuzhiyun }
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun return IRQ_HANDLED;
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun
hisi_thermal_register_sensor(struct platform_device * pdev,struct hisi_thermal_sensor * sensor)489*4882a593Smuzhiyun static int hisi_thermal_register_sensor(struct platform_device *pdev,
490*4882a593Smuzhiyun struct hisi_thermal_sensor *sensor)
491*4882a593Smuzhiyun {
492*4882a593Smuzhiyun int ret, i;
493*4882a593Smuzhiyun const struct thermal_trip *trip;
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev,
496*4882a593Smuzhiyun sensor->id, sensor,
497*4882a593Smuzhiyun &hisi_of_thermal_ops);
498*4882a593Smuzhiyun if (IS_ERR(sensor->tzd)) {
499*4882a593Smuzhiyun ret = PTR_ERR(sensor->tzd);
500*4882a593Smuzhiyun sensor->tzd = NULL;
501*4882a593Smuzhiyun dev_err(&pdev->dev, "failed to register sensor id %d: %d\n",
502*4882a593Smuzhiyun sensor->id, ret);
503*4882a593Smuzhiyun return ret;
504*4882a593Smuzhiyun }
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun trip = of_thermal_get_trip_points(sensor->tzd);
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun for (i = 0; i < of_thermal_get_ntrips(sensor->tzd); i++) {
509*4882a593Smuzhiyun if (trip[i].type == THERMAL_TRIP_PASSIVE) {
510*4882a593Smuzhiyun sensor->thres_temp = trip[i].temperature;
511*4882a593Smuzhiyun break;
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun return 0;
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun static const struct hisi_thermal_ops hi6220_ops = {
519*4882a593Smuzhiyun .get_temp = hi6220_thermal_get_temp,
520*4882a593Smuzhiyun .enable_sensor = hi6220_thermal_enable_sensor,
521*4882a593Smuzhiyun .disable_sensor = hi6220_thermal_disable_sensor,
522*4882a593Smuzhiyun .irq_handler = hi6220_thermal_irq_handler,
523*4882a593Smuzhiyun .probe = hi6220_thermal_probe,
524*4882a593Smuzhiyun };
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun static const struct hisi_thermal_ops hi3660_ops = {
527*4882a593Smuzhiyun .get_temp = hi3660_thermal_get_temp,
528*4882a593Smuzhiyun .enable_sensor = hi3660_thermal_enable_sensor,
529*4882a593Smuzhiyun .disable_sensor = hi3660_thermal_disable_sensor,
530*4882a593Smuzhiyun .irq_handler = hi3660_thermal_irq_handler,
531*4882a593Smuzhiyun .probe = hi3660_thermal_probe,
532*4882a593Smuzhiyun };
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun static const struct of_device_id of_hisi_thermal_match[] = {
535*4882a593Smuzhiyun {
536*4882a593Smuzhiyun .compatible = "hisilicon,tsensor",
537*4882a593Smuzhiyun .data = &hi6220_ops,
538*4882a593Smuzhiyun },
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun .compatible = "hisilicon,hi3660-tsensor",
541*4882a593Smuzhiyun .data = &hi3660_ops,
542*4882a593Smuzhiyun },
543*4882a593Smuzhiyun { /* end */ }
544*4882a593Smuzhiyun };
545*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, of_hisi_thermal_match);
546*4882a593Smuzhiyun
hisi_thermal_toggle_sensor(struct hisi_thermal_sensor * sensor,bool on)547*4882a593Smuzhiyun static void hisi_thermal_toggle_sensor(struct hisi_thermal_sensor *sensor,
548*4882a593Smuzhiyun bool on)
549*4882a593Smuzhiyun {
550*4882a593Smuzhiyun struct thermal_zone_device *tzd = sensor->tzd;
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun if (on)
553*4882a593Smuzhiyun thermal_zone_device_enable(tzd);
554*4882a593Smuzhiyun else
555*4882a593Smuzhiyun thermal_zone_device_disable(tzd);
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun
hisi_thermal_probe(struct platform_device * pdev)558*4882a593Smuzhiyun static int hisi_thermal_probe(struct platform_device *pdev)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun struct hisi_thermal_data *data;
561*4882a593Smuzhiyun struct device *dev = &pdev->dev;
562*4882a593Smuzhiyun struct resource *res;
563*4882a593Smuzhiyun int i, ret;
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
566*4882a593Smuzhiyun if (!data)
567*4882a593Smuzhiyun return -ENOMEM;
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun data->pdev = pdev;
570*4882a593Smuzhiyun platform_set_drvdata(pdev, data);
571*4882a593Smuzhiyun data->ops = of_device_get_match_data(dev);
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
574*4882a593Smuzhiyun data->regs = devm_ioremap_resource(dev, res);
575*4882a593Smuzhiyun if (IS_ERR(data->regs)) {
576*4882a593Smuzhiyun dev_err(dev, "failed to get io address\n");
577*4882a593Smuzhiyun return PTR_ERR(data->regs);
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun ret = data->ops->probe(data);
581*4882a593Smuzhiyun if (ret)
582*4882a593Smuzhiyun return ret;
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun for (i = 0; i < data->nr_sensors; i++) {
585*4882a593Smuzhiyun struct hisi_thermal_sensor *sensor = &data->sensor[i];
586*4882a593Smuzhiyun
587*4882a593Smuzhiyun ret = hisi_thermal_register_sensor(pdev, sensor);
588*4882a593Smuzhiyun if (ret) {
589*4882a593Smuzhiyun dev_err(dev, "failed to register thermal sensor: %d\n",
590*4882a593Smuzhiyun ret);
591*4882a593Smuzhiyun return ret;
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun ret = platform_get_irq(pdev, 0);
595*4882a593Smuzhiyun if (ret < 0)
596*4882a593Smuzhiyun return ret;
597*4882a593Smuzhiyun
598*4882a593Smuzhiyun ret = devm_request_threaded_irq(dev, ret, NULL,
599*4882a593Smuzhiyun hisi_thermal_alarm_irq_thread,
600*4882a593Smuzhiyun IRQF_ONESHOT, sensor->irq_name,
601*4882a593Smuzhiyun sensor);
602*4882a593Smuzhiyun if (ret < 0) {
603*4882a593Smuzhiyun dev_err(dev, "Failed to request alarm irq: %d\n", ret);
604*4882a593Smuzhiyun return ret;
605*4882a593Smuzhiyun }
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun ret = data->ops->enable_sensor(sensor);
608*4882a593Smuzhiyun if (ret) {
609*4882a593Smuzhiyun dev_err(dev, "Failed to setup the sensor: %d\n", ret);
610*4882a593Smuzhiyun return ret;
611*4882a593Smuzhiyun }
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun hisi_thermal_toggle_sensor(sensor, true);
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun return 0;
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun
hisi_thermal_remove(struct platform_device * pdev)619*4882a593Smuzhiyun static int hisi_thermal_remove(struct platform_device *pdev)
620*4882a593Smuzhiyun {
621*4882a593Smuzhiyun struct hisi_thermal_data *data = platform_get_drvdata(pdev);
622*4882a593Smuzhiyun int i;
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun for (i = 0; i < data->nr_sensors; i++) {
625*4882a593Smuzhiyun struct hisi_thermal_sensor *sensor = &data->sensor[i];
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun hisi_thermal_toggle_sensor(sensor, false);
628*4882a593Smuzhiyun data->ops->disable_sensor(sensor);
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun return 0;
632*4882a593Smuzhiyun }
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
hisi_thermal_suspend(struct device * dev)635*4882a593Smuzhiyun static int hisi_thermal_suspend(struct device *dev)
636*4882a593Smuzhiyun {
637*4882a593Smuzhiyun struct hisi_thermal_data *data = dev_get_drvdata(dev);
638*4882a593Smuzhiyun int i;
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun for (i = 0; i < data->nr_sensors; i++)
641*4882a593Smuzhiyun data->ops->disable_sensor(&data->sensor[i]);
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun return 0;
644*4882a593Smuzhiyun }
645*4882a593Smuzhiyun
hisi_thermal_resume(struct device * dev)646*4882a593Smuzhiyun static int hisi_thermal_resume(struct device *dev)
647*4882a593Smuzhiyun {
648*4882a593Smuzhiyun struct hisi_thermal_data *data = dev_get_drvdata(dev);
649*4882a593Smuzhiyun int i, ret = 0;
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun for (i = 0; i < data->nr_sensors; i++)
652*4882a593Smuzhiyun ret |= data->ops->enable_sensor(&data->sensor[i]);
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun return ret;
655*4882a593Smuzhiyun }
656*4882a593Smuzhiyun #endif
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(hisi_thermal_pm_ops,
659*4882a593Smuzhiyun hisi_thermal_suspend, hisi_thermal_resume);
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun static struct platform_driver hisi_thermal_driver = {
662*4882a593Smuzhiyun .driver = {
663*4882a593Smuzhiyun .name = "hisi_thermal",
664*4882a593Smuzhiyun .pm = &hisi_thermal_pm_ops,
665*4882a593Smuzhiyun .of_match_table = of_hisi_thermal_match,
666*4882a593Smuzhiyun },
667*4882a593Smuzhiyun .probe = hisi_thermal_probe,
668*4882a593Smuzhiyun .remove = hisi_thermal_remove,
669*4882a593Smuzhiyun };
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun module_platform_driver(hisi_thermal_driver);
672*4882a593Smuzhiyun
673*4882a593Smuzhiyun MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
674*4882a593Smuzhiyun MODULE_AUTHOR("Leo Yan <leo.yan@linaro.org>");
675*4882a593Smuzhiyun MODULE_DESCRIPTION("Hisilicon thermal driver");
676*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
677