1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun // Copyright (C) 2020 Spreadtrum Communications Inc.
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun #include <linux/clk.h>
5*4882a593Smuzhiyun #include <linux/io.h>
6*4882a593Smuzhiyun #include <linux/iopoll.h>
7*4882a593Smuzhiyun #include <linux/module.h>
8*4882a593Smuzhiyun #include <linux/nvmem-consumer.h>
9*4882a593Smuzhiyun #include <linux/of_device.h>
10*4882a593Smuzhiyun #include <linux/platform_device.h>
11*4882a593Smuzhiyun #include <linux/slab.h>
12*4882a593Smuzhiyun #include <linux/thermal.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #define SPRD_THM_CTL 0x0
15*4882a593Smuzhiyun #define SPRD_THM_INT_EN 0x4
16*4882a593Smuzhiyun #define SPRD_THM_INT_STS 0x8
17*4882a593Smuzhiyun #define SPRD_THM_INT_RAW_STS 0xc
18*4882a593Smuzhiyun #define SPRD_THM_DET_PERIOD 0x10
19*4882a593Smuzhiyun #define SPRD_THM_INT_CLR 0x14
20*4882a593Smuzhiyun #define SPRD_THM_INT_CLR_ST 0x18
21*4882a593Smuzhiyun #define SPRD_THM_MON_PERIOD 0x4c
22*4882a593Smuzhiyun #define SPRD_THM_MON_CTL 0x50
23*4882a593Smuzhiyun #define SPRD_THM_INTERNAL_STS1 0x54
24*4882a593Smuzhiyun #define SPRD_THM_RAW_READ_MSK 0x3ff
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #define SPRD_THM_OFFSET(id) ((id) * 0x4)
27*4882a593Smuzhiyun #define SPRD_THM_TEMP(id) (SPRD_THM_OFFSET(id) + 0x5c)
28*4882a593Smuzhiyun #define SPRD_THM_THRES(id) (SPRD_THM_OFFSET(id) + 0x2c)
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #define SPRD_THM_SEN(id) BIT((id) + 2)
31*4882a593Smuzhiyun #define SPRD_THM_SEN_OVERHEAT_EN(id) BIT((id) + 8)
32*4882a593Smuzhiyun #define SPRD_THM_SEN_OVERHEAT_ALARM_EN(id) BIT((id) + 0)
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /* bits definitions for register THM_CTL */
35*4882a593Smuzhiyun #define SPRD_THM_SET_RDY_ST BIT(13)
36*4882a593Smuzhiyun #define SPRD_THM_SET_RDY BIT(12)
37*4882a593Smuzhiyun #define SPRD_THM_MON_EN BIT(1)
38*4882a593Smuzhiyun #define SPRD_THM_EN BIT(0)
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /* bits definitions for register THM_INT_CTL */
41*4882a593Smuzhiyun #define SPRD_THM_BIT_INT_EN BIT(26)
42*4882a593Smuzhiyun #define SPRD_THM_OVERHEAT_EN BIT(25)
43*4882a593Smuzhiyun #define SPRD_THM_OTP_TRIP_SHIFT 10
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun /* bits definitions for register SPRD_THM_INTERNAL_STS1 */
46*4882a593Smuzhiyun #define SPRD_THM_TEMPER_RDY BIT(0)
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun #define SPRD_THM_DET_PERIOD_DATA 0x800
49*4882a593Smuzhiyun #define SPRD_THM_DET_PERIOD_MASK GENMASK(19, 0)
50*4882a593Smuzhiyun #define SPRD_THM_MON_MODE 0x7
51*4882a593Smuzhiyun #define SPRD_THM_MON_MODE_MASK GENMASK(3, 0)
52*4882a593Smuzhiyun #define SPRD_THM_MON_PERIOD_DATA 0x10
53*4882a593Smuzhiyun #define SPRD_THM_MON_PERIOD_MASK GENMASK(15, 0)
54*4882a593Smuzhiyun #define SPRD_THM_THRES_MASK GENMASK(19, 0)
55*4882a593Smuzhiyun #define SPRD_THM_INT_CLR_MASK GENMASK(24, 0)
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /* thermal sensor calibration parameters */
58*4882a593Smuzhiyun #define SPRD_THM_TEMP_LOW -40000
59*4882a593Smuzhiyun #define SPRD_THM_TEMP_HIGH 120000
60*4882a593Smuzhiyun #define SPRD_THM_OTP_TEMP 120000
61*4882a593Smuzhiyun #define SPRD_THM_HOT_TEMP 75000
62*4882a593Smuzhiyun #define SPRD_THM_RAW_DATA_LOW 0
63*4882a593Smuzhiyun #define SPRD_THM_RAW_DATA_HIGH 1000
64*4882a593Smuzhiyun #define SPRD_THM_SEN_NUM 8
65*4882a593Smuzhiyun #define SPRD_THM_DT_OFFSET 24
66*4882a593Smuzhiyun #define SPRD_THM_RATION_OFFSET 17
67*4882a593Smuzhiyun #define SPRD_THM_RATION_SIGN 16
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun #define SPRD_THM_RDYST_POLLING_TIME 10
70*4882a593Smuzhiyun #define SPRD_THM_RDYST_TIMEOUT 700
71*4882a593Smuzhiyun #define SPRD_THM_TEMP_READY_POLL_TIME 10000
72*4882a593Smuzhiyun #define SPRD_THM_TEMP_READY_TIMEOUT 600000
73*4882a593Smuzhiyun #define SPRD_THM_MAX_SENSOR 8
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun struct sprd_thermal_sensor {
76*4882a593Smuzhiyun struct thermal_zone_device *tzd;
77*4882a593Smuzhiyun struct sprd_thermal_data *data;
78*4882a593Smuzhiyun struct device *dev;
79*4882a593Smuzhiyun int cal_slope;
80*4882a593Smuzhiyun int cal_offset;
81*4882a593Smuzhiyun int id;
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun struct sprd_thermal_data {
85*4882a593Smuzhiyun const struct sprd_thm_variant_data *var_data;
86*4882a593Smuzhiyun struct sprd_thermal_sensor *sensor[SPRD_THM_MAX_SENSOR];
87*4882a593Smuzhiyun struct clk *clk;
88*4882a593Smuzhiyun void __iomem *base;
89*4882a593Smuzhiyun u32 ratio_off;
90*4882a593Smuzhiyun int ratio_sign;
91*4882a593Smuzhiyun int nr_sensors;
92*4882a593Smuzhiyun };
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /*
95*4882a593Smuzhiyun * The conversion between ADC and temperature is based on linear relationship,
96*4882a593Smuzhiyun * and use idea_k to specify the slope and ideal_b to specify the offset.
97*4882a593Smuzhiyun *
98*4882a593Smuzhiyun * Since different Spreadtrum SoCs have different ideal_k and ideal_b,
99*4882a593Smuzhiyun * we should save ideal_k and ideal_b in the device data structure.
100*4882a593Smuzhiyun */
101*4882a593Smuzhiyun struct sprd_thm_variant_data {
102*4882a593Smuzhiyun u32 ideal_k;
103*4882a593Smuzhiyun u32 ideal_b;
104*4882a593Smuzhiyun };
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun static const struct sprd_thm_variant_data ums512_data = {
107*4882a593Smuzhiyun .ideal_k = 262,
108*4882a593Smuzhiyun .ideal_b = 66400,
109*4882a593Smuzhiyun };
110*4882a593Smuzhiyun
sprd_thm_update_bits(void __iomem * reg,u32 mask,u32 val)111*4882a593Smuzhiyun static inline void sprd_thm_update_bits(void __iomem *reg, u32 mask, u32 val)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun u32 tmp, orig;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun orig = readl(reg);
116*4882a593Smuzhiyun tmp = orig & ~mask;
117*4882a593Smuzhiyun tmp |= val & mask;
118*4882a593Smuzhiyun writel(tmp, reg);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
sprd_thm_cal_read(struct device_node * np,const char * cell_id,u32 * val)121*4882a593Smuzhiyun static int sprd_thm_cal_read(struct device_node *np, const char *cell_id,
122*4882a593Smuzhiyun u32 *val)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun struct nvmem_cell *cell;
125*4882a593Smuzhiyun void *buf;
126*4882a593Smuzhiyun size_t len;
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun cell = of_nvmem_cell_get(np, cell_id);
129*4882a593Smuzhiyun if (IS_ERR(cell))
130*4882a593Smuzhiyun return PTR_ERR(cell);
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun buf = nvmem_cell_read(cell, &len);
133*4882a593Smuzhiyun nvmem_cell_put(cell);
134*4882a593Smuzhiyun if (IS_ERR(buf))
135*4882a593Smuzhiyun return PTR_ERR(buf);
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun if (len > sizeof(u32)) {
138*4882a593Smuzhiyun kfree(buf);
139*4882a593Smuzhiyun return -EINVAL;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun memcpy(val, buf, len);
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun kfree(buf);
145*4882a593Smuzhiyun return 0;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
sprd_thm_sensor_calibration(struct device_node * np,struct sprd_thermal_data * thm,struct sprd_thermal_sensor * sen)148*4882a593Smuzhiyun static int sprd_thm_sensor_calibration(struct device_node *np,
149*4882a593Smuzhiyun struct sprd_thermal_data *thm,
150*4882a593Smuzhiyun struct sprd_thermal_sensor *sen)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun int ret;
153*4882a593Smuzhiyun /*
154*4882a593Smuzhiyun * According to thermal datasheet, the default calibration offset is 64,
155*4882a593Smuzhiyun * and the default ratio is 1000.
156*4882a593Smuzhiyun */
157*4882a593Smuzhiyun int dt_offset = 64, ratio = 1000;
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun ret = sprd_thm_cal_read(np, "sen_delta_cal", &dt_offset);
160*4882a593Smuzhiyun if (ret)
161*4882a593Smuzhiyun return ret;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun ratio += thm->ratio_sign * thm->ratio_off;
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /*
166*4882a593Smuzhiyun * According to the ideal slope K and ideal offset B, combined with
167*4882a593Smuzhiyun * calibration value of thermal from efuse, then calibrate the real
168*4882a593Smuzhiyun * slope k and offset b:
169*4882a593Smuzhiyun * k_cal = (k * ratio) / 1000.
170*4882a593Smuzhiyun * b_cal = b + (dt_offset - 64) * 500.
171*4882a593Smuzhiyun */
172*4882a593Smuzhiyun sen->cal_slope = (thm->var_data->ideal_k * ratio) / 1000;
173*4882a593Smuzhiyun sen->cal_offset = thm->var_data->ideal_b + (dt_offset - 128) * 250;
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun return 0;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
sprd_thm_rawdata_to_temp(struct sprd_thermal_sensor * sen,u32 rawdata)178*4882a593Smuzhiyun static int sprd_thm_rawdata_to_temp(struct sprd_thermal_sensor *sen,
179*4882a593Smuzhiyun u32 rawdata)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun clamp(rawdata, (u32)SPRD_THM_RAW_DATA_LOW, (u32)SPRD_THM_RAW_DATA_HIGH);
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun /*
184*4882a593Smuzhiyun * According to the thermal datasheet, the formula of converting
185*4882a593Smuzhiyun * adc value to the temperature value should be:
186*4882a593Smuzhiyun * T_final = k_cal * x - b_cal.
187*4882a593Smuzhiyun */
188*4882a593Smuzhiyun return sen->cal_slope * rawdata - sen->cal_offset;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
sprd_thm_temp_to_rawdata(int temp,struct sprd_thermal_sensor * sen)191*4882a593Smuzhiyun static int sprd_thm_temp_to_rawdata(int temp, struct sprd_thermal_sensor *sen)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun u32 val;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun clamp(temp, (int)SPRD_THM_TEMP_LOW, (int)SPRD_THM_TEMP_HIGH);
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun /*
198*4882a593Smuzhiyun * According to the thermal datasheet, the formula of converting
199*4882a593Smuzhiyun * adc value to the temperature value should be:
200*4882a593Smuzhiyun * T_final = k_cal * x - b_cal.
201*4882a593Smuzhiyun */
202*4882a593Smuzhiyun val = (temp + sen->cal_offset) / sen->cal_slope;
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun return clamp(val, val, (u32)(SPRD_THM_RAW_DATA_HIGH - 1));
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
sprd_thm_read_temp(void * devdata,int * temp)207*4882a593Smuzhiyun static int sprd_thm_read_temp(void *devdata, int *temp)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun struct sprd_thermal_sensor *sen = devdata;
210*4882a593Smuzhiyun u32 data;
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun data = readl(sen->data->base + SPRD_THM_TEMP(sen->id)) &
213*4882a593Smuzhiyun SPRD_THM_RAW_READ_MSK;
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun *temp = sprd_thm_rawdata_to_temp(sen, data);
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun return 0;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun static const struct thermal_zone_of_device_ops sprd_thm_ops = {
221*4882a593Smuzhiyun .get_temp = sprd_thm_read_temp,
222*4882a593Smuzhiyun };
223*4882a593Smuzhiyun
sprd_thm_poll_ready_status(struct sprd_thermal_data * thm)224*4882a593Smuzhiyun static int sprd_thm_poll_ready_status(struct sprd_thermal_data *thm)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun u32 val;
227*4882a593Smuzhiyun int ret;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun /*
230*4882a593Smuzhiyun * Wait for thermal ready status before configuring thermal parameters.
231*4882a593Smuzhiyun */
232*4882a593Smuzhiyun ret = readl_poll_timeout(thm->base + SPRD_THM_CTL, val,
233*4882a593Smuzhiyun !(val & SPRD_THM_SET_RDY_ST),
234*4882a593Smuzhiyun SPRD_THM_RDYST_POLLING_TIME,
235*4882a593Smuzhiyun SPRD_THM_RDYST_TIMEOUT);
236*4882a593Smuzhiyun if (ret)
237*4882a593Smuzhiyun return ret;
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun sprd_thm_update_bits(thm->base + SPRD_THM_CTL, SPRD_THM_MON_EN,
240*4882a593Smuzhiyun SPRD_THM_MON_EN);
241*4882a593Smuzhiyun sprd_thm_update_bits(thm->base + SPRD_THM_CTL, SPRD_THM_SET_RDY,
242*4882a593Smuzhiyun SPRD_THM_SET_RDY);
243*4882a593Smuzhiyun return 0;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
sprd_thm_wait_temp_ready(struct sprd_thermal_data * thm)246*4882a593Smuzhiyun static int sprd_thm_wait_temp_ready(struct sprd_thermal_data *thm)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun u32 val;
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun /* Wait for first temperature data ready before reading temperature */
251*4882a593Smuzhiyun return readl_poll_timeout(thm->base + SPRD_THM_INTERNAL_STS1, val,
252*4882a593Smuzhiyun !(val & SPRD_THM_TEMPER_RDY),
253*4882a593Smuzhiyun SPRD_THM_TEMP_READY_POLL_TIME,
254*4882a593Smuzhiyun SPRD_THM_TEMP_READY_TIMEOUT);
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun
sprd_thm_set_ready(struct sprd_thermal_data * thm)257*4882a593Smuzhiyun static int sprd_thm_set_ready(struct sprd_thermal_data *thm)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun int ret;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun ret = sprd_thm_poll_ready_status(thm);
262*4882a593Smuzhiyun if (ret)
263*4882a593Smuzhiyun return ret;
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun /*
266*4882a593Smuzhiyun * Clear interrupt status, enable thermal interrupt and enable thermal.
267*4882a593Smuzhiyun *
268*4882a593Smuzhiyun * The SPRD thermal controller integrates a hardware interrupt signal,
269*4882a593Smuzhiyun * which means if the temperature is overheat, it will generate an
270*4882a593Smuzhiyun * interrupt and notify the event to PMIC automatically to shutdown the
271*4882a593Smuzhiyun * system. So here we should enable the interrupt bits, though we have
272*4882a593Smuzhiyun * not registered an irq handler.
273*4882a593Smuzhiyun */
274*4882a593Smuzhiyun writel(SPRD_THM_INT_CLR_MASK, thm->base + SPRD_THM_INT_CLR);
275*4882a593Smuzhiyun sprd_thm_update_bits(thm->base + SPRD_THM_INT_EN,
276*4882a593Smuzhiyun SPRD_THM_BIT_INT_EN, SPRD_THM_BIT_INT_EN);
277*4882a593Smuzhiyun sprd_thm_update_bits(thm->base + SPRD_THM_CTL,
278*4882a593Smuzhiyun SPRD_THM_EN, SPRD_THM_EN);
279*4882a593Smuzhiyun return 0;
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun
sprd_thm_sensor_init(struct sprd_thermal_data * thm,struct sprd_thermal_sensor * sen)282*4882a593Smuzhiyun static void sprd_thm_sensor_init(struct sprd_thermal_data *thm,
283*4882a593Smuzhiyun struct sprd_thermal_sensor *sen)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun u32 otp_rawdata, hot_rawdata;
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun otp_rawdata = sprd_thm_temp_to_rawdata(SPRD_THM_OTP_TEMP, sen);
288*4882a593Smuzhiyun hot_rawdata = sprd_thm_temp_to_rawdata(SPRD_THM_HOT_TEMP, sen);
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun /* Enable the sensor' overheat temperature protection interrupt */
291*4882a593Smuzhiyun sprd_thm_update_bits(thm->base + SPRD_THM_INT_EN,
292*4882a593Smuzhiyun SPRD_THM_SEN_OVERHEAT_ALARM_EN(sen->id),
293*4882a593Smuzhiyun SPRD_THM_SEN_OVERHEAT_ALARM_EN(sen->id));
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /* Set the sensor' overheat and hot threshold temperature */
296*4882a593Smuzhiyun sprd_thm_update_bits(thm->base + SPRD_THM_THRES(sen->id),
297*4882a593Smuzhiyun SPRD_THM_THRES_MASK,
298*4882a593Smuzhiyun (otp_rawdata << SPRD_THM_OTP_TRIP_SHIFT) |
299*4882a593Smuzhiyun hot_rawdata);
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun /* Enable the corresponding sensor */
302*4882a593Smuzhiyun sprd_thm_update_bits(thm->base + SPRD_THM_CTL, SPRD_THM_SEN(sen->id),
303*4882a593Smuzhiyun SPRD_THM_SEN(sen->id));
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
sprd_thm_para_config(struct sprd_thermal_data * thm)306*4882a593Smuzhiyun static void sprd_thm_para_config(struct sprd_thermal_data *thm)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun /* Set the period of two valid temperature detection action */
309*4882a593Smuzhiyun sprd_thm_update_bits(thm->base + SPRD_THM_DET_PERIOD,
310*4882a593Smuzhiyun SPRD_THM_DET_PERIOD_MASK, SPRD_THM_DET_PERIOD);
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun /* Set the sensors' monitor mode */
313*4882a593Smuzhiyun sprd_thm_update_bits(thm->base + SPRD_THM_MON_CTL,
314*4882a593Smuzhiyun SPRD_THM_MON_MODE_MASK, SPRD_THM_MON_MODE);
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun /* Set the sensors' monitor period */
317*4882a593Smuzhiyun sprd_thm_update_bits(thm->base + SPRD_THM_MON_PERIOD,
318*4882a593Smuzhiyun SPRD_THM_MON_PERIOD_MASK, SPRD_THM_MON_PERIOD);
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun
sprd_thm_toggle_sensor(struct sprd_thermal_sensor * sen,bool on)321*4882a593Smuzhiyun static void sprd_thm_toggle_sensor(struct sprd_thermal_sensor *sen, bool on)
322*4882a593Smuzhiyun {
323*4882a593Smuzhiyun struct thermal_zone_device *tzd = sen->tzd;
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun if (on)
326*4882a593Smuzhiyun thermal_zone_device_enable(tzd);
327*4882a593Smuzhiyun else
328*4882a593Smuzhiyun thermal_zone_device_disable(tzd);
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun
sprd_thm_probe(struct platform_device * pdev)331*4882a593Smuzhiyun static int sprd_thm_probe(struct platform_device *pdev)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun struct device_node *np = pdev->dev.of_node;
334*4882a593Smuzhiyun struct device_node *sen_child;
335*4882a593Smuzhiyun struct sprd_thermal_data *thm;
336*4882a593Smuzhiyun struct sprd_thermal_sensor *sen;
337*4882a593Smuzhiyun const struct sprd_thm_variant_data *pdata;
338*4882a593Smuzhiyun int ret, i;
339*4882a593Smuzhiyun u32 val;
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun pdata = of_device_get_match_data(&pdev->dev);
342*4882a593Smuzhiyun if (!pdata) {
343*4882a593Smuzhiyun dev_err(&pdev->dev, "No matching driver data found\n");
344*4882a593Smuzhiyun return -EINVAL;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun thm = devm_kzalloc(&pdev->dev, sizeof(*thm), GFP_KERNEL);
348*4882a593Smuzhiyun if (!thm)
349*4882a593Smuzhiyun return -ENOMEM;
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun thm->var_data = pdata;
352*4882a593Smuzhiyun thm->base = devm_platform_ioremap_resource(pdev, 0);
353*4882a593Smuzhiyun if (IS_ERR(thm->base))
354*4882a593Smuzhiyun return PTR_ERR(thm->base);
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun thm->nr_sensors = of_get_child_count(np);
357*4882a593Smuzhiyun if (thm->nr_sensors == 0 || thm->nr_sensors > SPRD_THM_MAX_SENSOR) {
358*4882a593Smuzhiyun dev_err(&pdev->dev, "incorrect sensor count\n");
359*4882a593Smuzhiyun return -EINVAL;
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun thm->clk = devm_clk_get(&pdev->dev, "enable");
363*4882a593Smuzhiyun if (IS_ERR(thm->clk)) {
364*4882a593Smuzhiyun dev_err(&pdev->dev, "failed to get enable clock\n");
365*4882a593Smuzhiyun return PTR_ERR(thm->clk);
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun ret = clk_prepare_enable(thm->clk);
369*4882a593Smuzhiyun if (ret)
370*4882a593Smuzhiyun return ret;
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun sprd_thm_para_config(thm);
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun ret = sprd_thm_cal_read(np, "thm_sign_cal", &val);
375*4882a593Smuzhiyun if (ret)
376*4882a593Smuzhiyun goto disable_clk;
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun if (val > 0)
379*4882a593Smuzhiyun thm->ratio_sign = -1;
380*4882a593Smuzhiyun else
381*4882a593Smuzhiyun thm->ratio_sign = 1;
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun ret = sprd_thm_cal_read(np, "thm_ratio_cal", &thm->ratio_off);
384*4882a593Smuzhiyun if (ret)
385*4882a593Smuzhiyun goto disable_clk;
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun for_each_child_of_node(np, sen_child) {
388*4882a593Smuzhiyun sen = devm_kzalloc(&pdev->dev, sizeof(*sen), GFP_KERNEL);
389*4882a593Smuzhiyun if (!sen) {
390*4882a593Smuzhiyun ret = -ENOMEM;
391*4882a593Smuzhiyun goto of_put;
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun sen->data = thm;
395*4882a593Smuzhiyun sen->dev = &pdev->dev;
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun ret = of_property_read_u32(sen_child, "reg", &sen->id);
398*4882a593Smuzhiyun if (ret) {
399*4882a593Smuzhiyun dev_err(&pdev->dev, "get sensor reg failed");
400*4882a593Smuzhiyun goto of_put;
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun ret = sprd_thm_sensor_calibration(sen_child, thm, sen);
404*4882a593Smuzhiyun if (ret) {
405*4882a593Smuzhiyun dev_err(&pdev->dev, "efuse cal analysis failed");
406*4882a593Smuzhiyun goto of_put;
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun sprd_thm_sensor_init(thm, sen);
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun sen->tzd = devm_thermal_zone_of_sensor_register(sen->dev,
412*4882a593Smuzhiyun sen->id,
413*4882a593Smuzhiyun sen,
414*4882a593Smuzhiyun &sprd_thm_ops);
415*4882a593Smuzhiyun if (IS_ERR(sen->tzd)) {
416*4882a593Smuzhiyun dev_err(&pdev->dev, "register thermal zone failed %d\n",
417*4882a593Smuzhiyun sen->id);
418*4882a593Smuzhiyun ret = PTR_ERR(sen->tzd);
419*4882a593Smuzhiyun goto of_put;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun thm->sensor[sen->id] = sen;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun /* sen_child set to NULL at this point */
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun ret = sprd_thm_set_ready(thm);
427*4882a593Smuzhiyun if (ret)
428*4882a593Smuzhiyun goto of_put;
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun ret = sprd_thm_wait_temp_ready(thm);
431*4882a593Smuzhiyun if (ret)
432*4882a593Smuzhiyun goto of_put;
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun for (i = 0; i < thm->nr_sensors; i++)
435*4882a593Smuzhiyun sprd_thm_toggle_sensor(thm->sensor[i], true);
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun platform_set_drvdata(pdev, thm);
438*4882a593Smuzhiyun return 0;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun of_put:
441*4882a593Smuzhiyun of_node_put(sen_child);
442*4882a593Smuzhiyun disable_clk:
443*4882a593Smuzhiyun clk_disable_unprepare(thm->clk);
444*4882a593Smuzhiyun return ret;
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
sprd_thm_hw_suspend(struct sprd_thermal_data * thm)448*4882a593Smuzhiyun static void sprd_thm_hw_suspend(struct sprd_thermal_data *thm)
449*4882a593Smuzhiyun {
450*4882a593Smuzhiyun int i;
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun for (i = 0; i < thm->nr_sensors; i++) {
453*4882a593Smuzhiyun sprd_thm_update_bits(thm->base + SPRD_THM_CTL,
454*4882a593Smuzhiyun SPRD_THM_SEN(thm->sensor[i]->id), 0);
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun sprd_thm_update_bits(thm->base + SPRD_THM_CTL,
458*4882a593Smuzhiyun SPRD_THM_EN, 0x0);
459*4882a593Smuzhiyun }
460*4882a593Smuzhiyun
sprd_thm_suspend(struct device * dev)461*4882a593Smuzhiyun static int sprd_thm_suspend(struct device *dev)
462*4882a593Smuzhiyun {
463*4882a593Smuzhiyun struct sprd_thermal_data *thm = dev_get_drvdata(dev);
464*4882a593Smuzhiyun int i;
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun for (i = 0; i < thm->nr_sensors; i++)
467*4882a593Smuzhiyun sprd_thm_toggle_sensor(thm->sensor[i], false);
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun sprd_thm_hw_suspend(thm);
470*4882a593Smuzhiyun clk_disable_unprepare(thm->clk);
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun return 0;
473*4882a593Smuzhiyun }
474*4882a593Smuzhiyun
sprd_thm_hw_resume(struct sprd_thermal_data * thm)475*4882a593Smuzhiyun static int sprd_thm_hw_resume(struct sprd_thermal_data *thm)
476*4882a593Smuzhiyun {
477*4882a593Smuzhiyun int ret, i;
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun for (i = 0; i < thm->nr_sensors; i++) {
480*4882a593Smuzhiyun sprd_thm_update_bits(thm->base + SPRD_THM_CTL,
481*4882a593Smuzhiyun SPRD_THM_SEN(thm->sensor[i]->id),
482*4882a593Smuzhiyun SPRD_THM_SEN(thm->sensor[i]->id));
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun ret = sprd_thm_poll_ready_status(thm);
486*4882a593Smuzhiyun if (ret)
487*4882a593Smuzhiyun return ret;
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun writel(SPRD_THM_INT_CLR_MASK, thm->base + SPRD_THM_INT_CLR);
490*4882a593Smuzhiyun sprd_thm_update_bits(thm->base + SPRD_THM_CTL,
491*4882a593Smuzhiyun SPRD_THM_EN, SPRD_THM_EN);
492*4882a593Smuzhiyun return sprd_thm_wait_temp_ready(thm);
493*4882a593Smuzhiyun }
494*4882a593Smuzhiyun
sprd_thm_resume(struct device * dev)495*4882a593Smuzhiyun static int sprd_thm_resume(struct device *dev)
496*4882a593Smuzhiyun {
497*4882a593Smuzhiyun struct sprd_thermal_data *thm = dev_get_drvdata(dev);
498*4882a593Smuzhiyun int ret, i;
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun ret = clk_prepare_enable(thm->clk);
501*4882a593Smuzhiyun if (ret)
502*4882a593Smuzhiyun return ret;
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun ret = sprd_thm_hw_resume(thm);
505*4882a593Smuzhiyun if (ret)
506*4882a593Smuzhiyun goto disable_clk;
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun for (i = 0; i < thm->nr_sensors; i++)
509*4882a593Smuzhiyun sprd_thm_toggle_sensor(thm->sensor[i], true);
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun return 0;
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun disable_clk:
514*4882a593Smuzhiyun clk_disable_unprepare(thm->clk);
515*4882a593Smuzhiyun return ret;
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun #endif
518*4882a593Smuzhiyun
sprd_thm_remove(struct platform_device * pdev)519*4882a593Smuzhiyun static int sprd_thm_remove(struct platform_device *pdev)
520*4882a593Smuzhiyun {
521*4882a593Smuzhiyun struct sprd_thermal_data *thm = platform_get_drvdata(pdev);
522*4882a593Smuzhiyun int i;
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun for (i = 0; i < thm->nr_sensors; i++) {
525*4882a593Smuzhiyun sprd_thm_toggle_sensor(thm->sensor[i], false);
526*4882a593Smuzhiyun devm_thermal_zone_of_sensor_unregister(&pdev->dev,
527*4882a593Smuzhiyun thm->sensor[i]->tzd);
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun clk_disable_unprepare(thm->clk);
531*4882a593Smuzhiyun return 0;
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun static const struct of_device_id sprd_thermal_of_match[] = {
535*4882a593Smuzhiyun { .compatible = "sprd,ums512-thermal", .data = &ums512_data },
536*4882a593Smuzhiyun { },
537*4882a593Smuzhiyun };
538*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, sprd_thermal_of_match);
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun static const struct dev_pm_ops sprd_thermal_pm_ops = {
541*4882a593Smuzhiyun SET_SYSTEM_SLEEP_PM_OPS(sprd_thm_suspend, sprd_thm_resume)
542*4882a593Smuzhiyun };
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun static struct platform_driver sprd_thermal_driver = {
545*4882a593Smuzhiyun .probe = sprd_thm_probe,
546*4882a593Smuzhiyun .remove = sprd_thm_remove,
547*4882a593Smuzhiyun .driver = {
548*4882a593Smuzhiyun .name = "sprd-thermal",
549*4882a593Smuzhiyun .pm = &sprd_thermal_pm_ops,
550*4882a593Smuzhiyun .of_match_table = sprd_thermal_of_match,
551*4882a593Smuzhiyun },
552*4882a593Smuzhiyun };
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun module_platform_driver(sprd_thermal_driver);
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun MODULE_AUTHOR("Freeman Liu <freeman.liu@unisoc.com>");
557*4882a593Smuzhiyun MODULE_DESCRIPTION("Spreadtrum thermal driver");
558*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
559