hisi_thermal.c 16 KB

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