1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2020 Spreadtrum Communications Inc.
6 #include <linux/iopoll.h>
7 #include <linux/module.h>
8 #include <linux/nvmem-consumer.h>
9 #include <linux/of_device.h>
10 #include <linux/platform_device.h>
11 #include <linux/slab.h>
12 #include <linux/thermal.h>
14 #define SPRD_THM_CTL 0x0
15 #define SPRD_THM_INT_EN 0x4
16 #define SPRD_THM_INT_STS 0x8
17 #define SPRD_THM_INT_RAW_STS 0xc
18 #define SPRD_THM_DET_PERIOD 0x10
19 #define SPRD_THM_INT_CLR 0x14
20 #define SPRD_THM_INT_CLR_ST 0x18
21 #define SPRD_THM_MON_PERIOD 0x4c
22 #define SPRD_THM_MON_CTL 0x50
23 #define SPRD_THM_INTERNAL_STS1 0x54
24 #define SPRD_THM_RAW_READ_MSK 0x3ff
26 #define SPRD_THM_OFFSET(id) ((id) * 0x4)
27 #define SPRD_THM_TEMP(id) (SPRD_THM_OFFSET(id) + 0x5c)
28 #define SPRD_THM_THRES(id) (SPRD_THM_OFFSET(id) + 0x2c)
30 #define SPRD_THM_SEN(id) BIT((id) + 2)
31 #define SPRD_THM_SEN_OVERHEAT_EN(id) BIT((id) + 8)
32 #define SPRD_THM_SEN_OVERHEAT_ALARM_EN(id) BIT((id) + 0)
34 /* bits definitions for register THM_CTL */
35 #define SPRD_THM_SET_RDY_ST BIT(13)
36 #define SPRD_THM_SET_RDY BIT(12)
37 #define SPRD_THM_MON_EN BIT(1)
38 #define SPRD_THM_EN BIT(0)
40 /* bits definitions for register THM_INT_CTL */
41 #define SPRD_THM_BIT_INT_EN BIT(26)
42 #define SPRD_THM_OVERHEAT_EN BIT(25)
43 #define SPRD_THM_OTP_TRIP_SHIFT 10
45 /* bits definitions for register SPRD_THM_INTERNAL_STS1 */
46 #define SPRD_THM_TEMPER_RDY BIT(0)
48 #define SPRD_THM_DET_PERIOD_DATA 0x800
49 #define SPRD_THM_DET_PERIOD_MASK GENMASK(19, 0)
50 #define SPRD_THM_MON_MODE 0x7
51 #define SPRD_THM_MON_MODE_MASK GENMASK(3, 0)
52 #define SPRD_THM_MON_PERIOD_DATA 0x10
53 #define SPRD_THM_MON_PERIOD_MASK GENMASK(15, 0)
54 #define SPRD_THM_THRES_MASK GENMASK(19, 0)
55 #define SPRD_THM_INT_CLR_MASK GENMASK(24, 0)
57 /* thermal sensor calibration parameters */
58 #define SPRD_THM_TEMP_LOW -40000
59 #define SPRD_THM_TEMP_HIGH 120000
60 #define SPRD_THM_OTP_TEMP 120000
61 #define SPRD_THM_HOT_TEMP 75000
62 #define SPRD_THM_RAW_DATA_LOW 0
63 #define SPRD_THM_RAW_DATA_HIGH 1000
64 #define SPRD_THM_SEN_NUM 8
65 #define SPRD_THM_DT_OFFSET 24
66 #define SPRD_THM_RATION_OFFSET 17
67 #define SPRD_THM_RATION_SIGN 16
69 #define SPRD_THM_RDYST_POLLING_TIME 10
70 #define SPRD_THM_RDYST_TIMEOUT 700
71 #define SPRD_THM_TEMP_READY_POLL_TIME 10000
72 #define SPRD_THM_TEMP_READY_TIMEOUT 600000
73 #define SPRD_THM_MAX_SENSOR 8
75 struct sprd_thermal_sensor {
76 struct thermal_zone_device *tzd;
77 struct sprd_thermal_data *data;
84 struct sprd_thermal_data {
85 const struct sprd_thm_variant_data *var_data;
86 struct sprd_thermal_sensor *sensor[SPRD_THM_MAX_SENSOR];
95 * The conversion between ADC and temperature is based on linear relationship,
96 * and use idea_k to specify the slope and ideal_b to specify the offset.
98 * Since different Spreadtrum SoCs have different ideal_k and ideal_b,
99 * we should save ideal_k and ideal_b in the device data structure.
101 struct sprd_thm_variant_data {
106 static const struct sprd_thm_variant_data ums512_data = {
111 static inline void sprd_thm_update_bits(void __iomem *reg, u32 mask, u32 val)
121 static int sprd_thm_cal_read(struct device_node *np, const char *cell_id,
124 struct nvmem_cell *cell;
128 cell = of_nvmem_cell_get(np, cell_id);
130 return PTR_ERR(cell);
132 buf = nvmem_cell_read(cell, &len);
133 nvmem_cell_put(cell);
137 if (len > sizeof(u32)) {
142 memcpy(val, buf, len);
148 static int sprd_thm_sensor_calibration(struct device_node *np,
149 struct sprd_thermal_data *thm,
150 struct sprd_thermal_sensor *sen)
154 * According to thermal datasheet, the default calibration offset is 64,
155 * and the default ratio is 1000.
157 int dt_offset = 64, ratio = 1000;
159 ret = sprd_thm_cal_read(np, "sen_delta_cal", &dt_offset);
163 ratio += thm->ratio_sign * thm->ratio_off;
166 * According to the ideal slope K and ideal offset B, combined with
167 * calibration value of thermal from efuse, then calibrate the real
168 * slope k and offset b:
169 * k_cal = (k * ratio) / 1000.
170 * b_cal = b + (dt_offset - 64) * 500.
172 sen->cal_slope = (thm->var_data->ideal_k * ratio) / 1000;
173 sen->cal_offset = thm->var_data->ideal_b + (dt_offset - 128) * 250;
178 static int sprd_thm_rawdata_to_temp(struct sprd_thermal_sensor *sen,
181 clamp(rawdata, (u32)SPRD_THM_RAW_DATA_LOW, (u32)SPRD_THM_RAW_DATA_HIGH);
184 * According to the thermal datasheet, the formula of converting
185 * adc value to the temperature value should be:
186 * T_final = k_cal * x - b_cal.
188 return sen->cal_slope * rawdata - sen->cal_offset;
191 static int sprd_thm_temp_to_rawdata(int temp, struct sprd_thermal_sensor *sen)
195 clamp(temp, (int)SPRD_THM_TEMP_LOW, (int)SPRD_THM_TEMP_HIGH);
198 * According to the thermal datasheet, the formula of converting
199 * adc value to the temperature value should be:
200 * T_final = k_cal * x - b_cal.
202 val = (temp + sen->cal_offset) / sen->cal_slope;
204 return clamp(val, val, (u32)(SPRD_THM_RAW_DATA_HIGH - 1));
207 static int sprd_thm_read_temp(void *devdata, int *temp)
209 struct sprd_thermal_sensor *sen = devdata;
212 data = readl(sen->data->base + SPRD_THM_TEMP(sen->id)) &
213 SPRD_THM_RAW_READ_MSK;
215 *temp = sprd_thm_rawdata_to_temp(sen, data);
220 static const struct thermal_zone_of_device_ops sprd_thm_ops = {
221 .get_temp = sprd_thm_read_temp,
224 static int sprd_thm_poll_ready_status(struct sprd_thermal_data *thm)
230 * Wait for thermal ready status before configuring thermal parameters.
232 ret = readl_poll_timeout(thm->base + SPRD_THM_CTL, val,
233 !(val & SPRD_THM_SET_RDY_ST),
234 SPRD_THM_RDYST_POLLING_TIME,
235 SPRD_THM_RDYST_TIMEOUT);
239 sprd_thm_update_bits(thm->base + SPRD_THM_CTL, SPRD_THM_MON_EN,
241 sprd_thm_update_bits(thm->base + SPRD_THM_CTL, SPRD_THM_SET_RDY,
246 static int sprd_thm_wait_temp_ready(struct sprd_thermal_data *thm)
250 /* Wait for first temperature data ready before reading temperature */
251 return readl_poll_timeout(thm->base + SPRD_THM_INTERNAL_STS1, val,
252 !(val & SPRD_THM_TEMPER_RDY),
253 SPRD_THM_TEMP_READY_POLL_TIME,
254 SPRD_THM_TEMP_READY_TIMEOUT);
257 static int sprd_thm_set_ready(struct sprd_thermal_data *thm)
261 ret = sprd_thm_poll_ready_status(thm);
266 * Clear interrupt status, enable thermal interrupt and enable thermal.
268 * The SPRD thermal controller integrates a hardware interrupt signal,
269 * which means if the temperature is overheat, it will generate an
270 * interrupt and notify the event to PMIC automatically to shutdown the
271 * system. So here we should enable the interrupt bits, though we have
272 * not registered an irq handler.
274 writel(SPRD_THM_INT_CLR_MASK, thm->base + SPRD_THM_INT_CLR);
275 sprd_thm_update_bits(thm->base + SPRD_THM_INT_EN,
276 SPRD_THM_BIT_INT_EN, SPRD_THM_BIT_INT_EN);
277 sprd_thm_update_bits(thm->base + SPRD_THM_CTL,
278 SPRD_THM_EN, SPRD_THM_EN);
282 static void sprd_thm_sensor_init(struct sprd_thermal_data *thm,
283 struct sprd_thermal_sensor *sen)
285 u32 otp_rawdata, hot_rawdata;
287 otp_rawdata = sprd_thm_temp_to_rawdata(SPRD_THM_OTP_TEMP, sen);
288 hot_rawdata = sprd_thm_temp_to_rawdata(SPRD_THM_HOT_TEMP, sen);
290 /* Enable the sensor' overheat temperature protection interrupt */
291 sprd_thm_update_bits(thm->base + SPRD_THM_INT_EN,
292 SPRD_THM_SEN_OVERHEAT_ALARM_EN(sen->id),
293 SPRD_THM_SEN_OVERHEAT_ALARM_EN(sen->id));
295 /* Set the sensor' overheat and hot threshold temperature */
296 sprd_thm_update_bits(thm->base + SPRD_THM_THRES(sen->id),
298 (otp_rawdata << SPRD_THM_OTP_TRIP_SHIFT) |
301 /* Enable the corresponding sensor */
302 sprd_thm_update_bits(thm->base + SPRD_THM_CTL, SPRD_THM_SEN(sen->id),
303 SPRD_THM_SEN(sen->id));
306 static void sprd_thm_para_config(struct sprd_thermal_data *thm)
308 /* Set the period of two valid temperature detection action */
309 sprd_thm_update_bits(thm->base + SPRD_THM_DET_PERIOD,
310 SPRD_THM_DET_PERIOD_MASK, SPRD_THM_DET_PERIOD);
312 /* Set the sensors' monitor mode */
313 sprd_thm_update_bits(thm->base + SPRD_THM_MON_CTL,
314 SPRD_THM_MON_MODE_MASK, SPRD_THM_MON_MODE);
316 /* Set the sensors' monitor period */
317 sprd_thm_update_bits(thm->base + SPRD_THM_MON_PERIOD,
318 SPRD_THM_MON_PERIOD_MASK, SPRD_THM_MON_PERIOD);
321 static void sprd_thm_toggle_sensor(struct sprd_thermal_sensor *sen, bool on)
323 struct thermal_zone_device *tzd = sen->tzd;
326 thermal_zone_device_enable(tzd);
328 thermal_zone_device_disable(tzd);
331 static int sprd_thm_probe(struct platform_device *pdev)
333 struct device_node *np = pdev->dev.of_node;
334 struct device_node *sen_child;
335 struct sprd_thermal_data *thm;
336 struct sprd_thermal_sensor *sen;
337 const struct sprd_thm_variant_data *pdata;
341 pdata = of_device_get_match_data(&pdev->dev);
343 dev_err(&pdev->dev, "No matching driver data found\n");
347 thm = devm_kzalloc(&pdev->dev, sizeof(*thm), GFP_KERNEL);
351 thm->var_data = pdata;
352 thm->base = devm_platform_ioremap_resource(pdev, 0);
353 if (IS_ERR(thm->base))
354 return PTR_ERR(thm->base);
356 thm->nr_sensors = of_get_child_count(np);
357 if (thm->nr_sensors == 0 || thm->nr_sensors > SPRD_THM_MAX_SENSOR) {
358 dev_err(&pdev->dev, "incorrect sensor count\n");
362 thm->clk = devm_clk_get(&pdev->dev, "enable");
363 if (IS_ERR(thm->clk)) {
364 dev_err(&pdev->dev, "failed to get enable clock\n");
365 return PTR_ERR(thm->clk);
368 ret = clk_prepare_enable(thm->clk);
372 sprd_thm_para_config(thm);
374 ret = sprd_thm_cal_read(np, "thm_sign_cal", &val);
379 thm->ratio_sign = -1;
383 ret = sprd_thm_cal_read(np, "thm_ratio_cal", &thm->ratio_off);
387 for_each_child_of_node(np, sen_child) {
388 sen = devm_kzalloc(&pdev->dev, sizeof(*sen), GFP_KERNEL);
395 sen->dev = &pdev->dev;
397 ret = of_property_read_u32(sen_child, "reg", &sen->id);
399 dev_err(&pdev->dev, "get sensor reg failed");
403 ret = sprd_thm_sensor_calibration(sen_child, thm, sen);
405 dev_err(&pdev->dev, "efuse cal analysis failed");
409 sprd_thm_sensor_init(thm, sen);
411 sen->tzd = devm_thermal_zone_of_sensor_register(sen->dev,
415 if (IS_ERR(sen->tzd)) {
416 dev_err(&pdev->dev, "register thermal zone failed %d\n",
418 ret = PTR_ERR(sen->tzd);
422 thm->sensor[sen->id] = sen;
424 /* sen_child set to NULL at this point */
426 ret = sprd_thm_set_ready(thm);
430 ret = sprd_thm_wait_temp_ready(thm);
434 for (i = 0; i < thm->nr_sensors; i++)
435 sprd_thm_toggle_sensor(thm->sensor[i], true);
437 platform_set_drvdata(pdev, thm);
441 of_node_put(sen_child);
443 clk_disable_unprepare(thm->clk);
447 #ifdef CONFIG_PM_SLEEP
448 static void sprd_thm_hw_suspend(struct sprd_thermal_data *thm)
452 for (i = 0; i < thm->nr_sensors; i++) {
453 sprd_thm_update_bits(thm->base + SPRD_THM_CTL,
454 SPRD_THM_SEN(thm->sensor[i]->id), 0);
457 sprd_thm_update_bits(thm->base + SPRD_THM_CTL,
461 static int sprd_thm_suspend(struct device *dev)
463 struct sprd_thermal_data *thm = dev_get_drvdata(dev);
466 for (i = 0; i < thm->nr_sensors; i++)
467 sprd_thm_toggle_sensor(thm->sensor[i], false);
469 sprd_thm_hw_suspend(thm);
470 clk_disable_unprepare(thm->clk);
475 static int sprd_thm_hw_resume(struct sprd_thermal_data *thm)
479 for (i = 0; i < thm->nr_sensors; i++) {
480 sprd_thm_update_bits(thm->base + SPRD_THM_CTL,
481 SPRD_THM_SEN(thm->sensor[i]->id),
482 SPRD_THM_SEN(thm->sensor[i]->id));
485 ret = sprd_thm_poll_ready_status(thm);
489 writel(SPRD_THM_INT_CLR_MASK, thm->base + SPRD_THM_INT_CLR);
490 sprd_thm_update_bits(thm->base + SPRD_THM_CTL,
491 SPRD_THM_EN, SPRD_THM_EN);
492 return sprd_thm_wait_temp_ready(thm);
495 static int sprd_thm_resume(struct device *dev)
497 struct sprd_thermal_data *thm = dev_get_drvdata(dev);
500 ret = clk_prepare_enable(thm->clk);
504 ret = sprd_thm_hw_resume(thm);
508 for (i = 0; i < thm->nr_sensors; i++)
509 sprd_thm_toggle_sensor(thm->sensor[i], true);
514 clk_disable_unprepare(thm->clk);
519 static int sprd_thm_remove(struct platform_device *pdev)
521 struct sprd_thermal_data *thm = platform_get_drvdata(pdev);
524 for (i = 0; i < thm->nr_sensors; i++) {
525 sprd_thm_toggle_sensor(thm->sensor[i], false);
526 devm_thermal_zone_of_sensor_unregister(&pdev->dev,
527 thm->sensor[i]->tzd);
530 clk_disable_unprepare(thm->clk);
534 static const struct of_device_id sprd_thermal_of_match[] = {
535 { .compatible = "sprd,ums512-thermal", .data = &ums512_data },
538 MODULE_DEVICE_TABLE(of, sprd_thermal_of_match);
540 static const struct dev_pm_ops sprd_thermal_pm_ops = {
541 SET_SYSTEM_SLEEP_PM_OPS(sprd_thm_suspend, sprd_thm_resume)
544 static struct platform_driver sprd_thermal_driver = {
545 .probe = sprd_thm_probe,
546 .remove = sprd_thm_remove,
548 .name = "sprd-thermal",
549 .pm = &sprd_thermal_pm_ops,
550 .of_match_table = sprd_thermal_of_match,
554 module_platform_driver(sprd_thermal_driver);
556 MODULE_AUTHOR("Freeman Liu <freeman.liu@unisoc.com>");
557 MODULE_DESCRIPTION("Spreadtrum thermal driver");
558 MODULE_LICENSE("GPL v2");