sprd_thermal.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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.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 = thermal_zone_device_priv(tz);
  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_enabled(&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. sprd_thm_para_config(thm);
  306. ret = sprd_thm_cal_read(np, "thm_sign_cal", &val);
  307. if (ret)
  308. return ret;
  309. if (val > 0)
  310. thm->ratio_sign = -1;
  311. else
  312. thm->ratio_sign = 1;
  313. ret = sprd_thm_cal_read(np, "thm_ratio_cal", &thm->ratio_off);
  314. if (ret)
  315. return ret;
  316. for_each_child_of_node(np, sen_child) {
  317. sen = devm_kzalloc(&pdev->dev, sizeof(*sen), GFP_KERNEL);
  318. if (!sen) {
  319. ret = -ENOMEM;
  320. goto of_put;
  321. }
  322. sen->data = thm;
  323. sen->dev = &pdev->dev;
  324. ret = of_property_read_u32(sen_child, "reg", &sen->id);
  325. if (ret) {
  326. dev_err(&pdev->dev, "get sensor reg failed");
  327. goto of_put;
  328. }
  329. ret = sprd_thm_sensor_calibration(sen_child, thm, sen);
  330. if (ret) {
  331. dev_err(&pdev->dev, "efuse cal analysis failed");
  332. goto of_put;
  333. }
  334. sprd_thm_sensor_init(thm, sen);
  335. sen->tzd = devm_thermal_of_zone_register(sen->dev,
  336. sen->id,
  337. sen,
  338. &sprd_thm_ops);
  339. if (IS_ERR(sen->tzd)) {
  340. dev_err(&pdev->dev, "register thermal zone failed %d\n",
  341. sen->id);
  342. ret = PTR_ERR(sen->tzd);
  343. goto of_put;
  344. }
  345. thm->sensor[sen->id] = sen;
  346. }
  347. /* sen_child set to NULL at this point */
  348. ret = sprd_thm_set_ready(thm);
  349. if (ret)
  350. goto of_put;
  351. ret = sprd_thm_wait_temp_ready(thm);
  352. if (ret)
  353. goto of_put;
  354. for (i = 0; i < thm->nr_sensors; i++)
  355. sprd_thm_toggle_sensor(thm->sensor[i], true);
  356. platform_set_drvdata(pdev, thm);
  357. return 0;
  358. of_put:
  359. of_node_put(sen_child);
  360. return ret;
  361. }
  362. #ifdef CONFIG_PM_SLEEP
  363. static void sprd_thm_hw_suspend(struct sprd_thermal_data *thm)
  364. {
  365. int i;
  366. for (i = 0; i < thm->nr_sensors; i++) {
  367. sprd_thm_update_bits(thm->base + SPRD_THM_CTL,
  368. SPRD_THM_SEN(thm->sensor[i]->id), 0);
  369. }
  370. sprd_thm_update_bits(thm->base + SPRD_THM_CTL,
  371. SPRD_THM_EN, 0x0);
  372. }
  373. static int sprd_thm_suspend(struct device *dev)
  374. {
  375. struct sprd_thermal_data *thm = dev_get_drvdata(dev);
  376. int i;
  377. for (i = 0; i < thm->nr_sensors; i++)
  378. sprd_thm_toggle_sensor(thm->sensor[i], false);
  379. sprd_thm_hw_suspend(thm);
  380. clk_disable_unprepare(thm->clk);
  381. return 0;
  382. }
  383. static int sprd_thm_hw_resume(struct sprd_thermal_data *thm)
  384. {
  385. int ret, i;
  386. for (i = 0; i < thm->nr_sensors; i++) {
  387. sprd_thm_update_bits(thm->base + SPRD_THM_CTL,
  388. SPRD_THM_SEN(thm->sensor[i]->id),
  389. SPRD_THM_SEN(thm->sensor[i]->id));
  390. }
  391. ret = sprd_thm_poll_ready_status(thm);
  392. if (ret)
  393. return ret;
  394. writel(SPRD_THM_INT_CLR_MASK, thm->base + SPRD_THM_INT_CLR);
  395. sprd_thm_update_bits(thm->base + SPRD_THM_CTL,
  396. SPRD_THM_EN, SPRD_THM_EN);
  397. return sprd_thm_wait_temp_ready(thm);
  398. }
  399. static int sprd_thm_resume(struct device *dev)
  400. {
  401. struct sprd_thermal_data *thm = dev_get_drvdata(dev);
  402. int ret, i;
  403. ret = clk_prepare_enable(thm->clk);
  404. if (ret)
  405. return ret;
  406. ret = sprd_thm_hw_resume(thm);
  407. if (ret)
  408. goto disable_clk;
  409. for (i = 0; i < thm->nr_sensors; i++)
  410. sprd_thm_toggle_sensor(thm->sensor[i], true);
  411. return 0;
  412. disable_clk:
  413. clk_disable_unprepare(thm->clk);
  414. return ret;
  415. }
  416. #endif
  417. static void sprd_thm_remove(struct platform_device *pdev)
  418. {
  419. struct sprd_thermal_data *thm = platform_get_drvdata(pdev);
  420. int i;
  421. for (i = 0; i < thm->nr_sensors; i++) {
  422. sprd_thm_toggle_sensor(thm->sensor[i], false);
  423. devm_thermal_of_zone_unregister(&pdev->dev,
  424. thm->sensor[i]->tzd);
  425. }
  426. }
  427. static const struct of_device_id sprd_thermal_of_match[] = {
  428. { .compatible = "sprd,ums512-thermal", .data = &ums512_data },
  429. { },
  430. };
  431. MODULE_DEVICE_TABLE(of, sprd_thermal_of_match);
  432. static const struct dev_pm_ops sprd_thermal_pm_ops = {
  433. SET_SYSTEM_SLEEP_PM_OPS(sprd_thm_suspend, sprd_thm_resume)
  434. };
  435. static struct platform_driver sprd_thermal_driver = {
  436. .probe = sprd_thm_probe,
  437. .remove_new = sprd_thm_remove,
  438. .driver = {
  439. .name = "sprd-thermal",
  440. .pm = &sprd_thermal_pm_ops,
  441. .of_match_table = sprd_thermal_of_match,
  442. },
  443. };
  444. module_platform_driver(sprd_thermal_driver);
  445. MODULE_AUTHOR("Freeman Liu <freeman.liu@unisoc.com>");
  446. MODULE_DESCRIPTION("Spreadtrum thermal driver");
  447. MODULE_LICENSE("GPL v2");