sprd_thermal.c 14 KB

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