uniphier_thermal.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. * uniphier_thermal.c - Socionext UniPhier thermal driver
  3. *
  4. * Copyright 2014 Panasonic Corporation
  5. * Copyright 2016-2017 Socionext Inc.
  6. * All rights reserved.
  7. *
  8. * Author:
  9. * Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 of
  13. * the License as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/bitops.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/mfd/syscon.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/regmap.h>
  28. #include <linux/thermal.h>
  29. #include "thermal_core.h"
  30. /*
  31. * block registers
  32. * addresses are the offset from .block_base
  33. */
  34. #define PVTCTLEN 0x0000
  35. #define PVTCTLEN_EN BIT(0)
  36. #define PVTCTLMODE 0x0004
  37. #define PVTCTLMODE_MASK 0xf
  38. #define PVTCTLMODE_TEMPMON 0x5
  39. #define EMONREPEAT 0x0040
  40. #define EMONREPEAT_ENDLESS BIT(24)
  41. #define EMONREPEAT_PERIOD GENMASK(3, 0)
  42. #define EMONREPEAT_PERIOD_1000000 0x9
  43. /*
  44. * common registers
  45. * addresses are the offset from .map_base
  46. */
  47. #define PVTCTLSEL 0x0900
  48. #define PVTCTLSEL_MASK GENMASK(2, 0)
  49. #define PVTCTLSEL_MONITOR 0
  50. #define SETALERT0 0x0910
  51. #define SETALERT1 0x0914
  52. #define SETALERT2 0x0918
  53. #define SETALERT_TEMP_OVF (GENMASK(7, 0) << 16)
  54. #define SETALERT_TEMP_OVF_VALUE(val) (((val) & GENMASK(7, 0)) << 16)
  55. #define SETALERT_EN BIT(0)
  56. #define PMALERTINTCTL 0x0920
  57. #define PMALERTINTCTL_CLR(ch) BIT(4 * (ch) + 2)
  58. #define PMALERTINTCTL_SET(ch) BIT(4 * (ch) + 1)
  59. #define PMALERTINTCTL_EN(ch) BIT(4 * (ch) + 0)
  60. #define PMALERTINTCTL_MASK (GENMASK(10, 8) | GENMASK(6, 4) | \
  61. GENMASK(2, 0))
  62. #define TMOD 0x0928
  63. #define TMOD_WIDTH 9
  64. #define TMODCOEF 0x0e5c
  65. #define TMODSETUP0_EN BIT(30)
  66. #define TMODSETUP0_VAL(val) (((val) & GENMASK(13, 0)) << 16)
  67. #define TMODSETUP1_EN BIT(15)
  68. #define TMODSETUP1_VAL(val) ((val) & GENMASK(14, 0))
  69. /* SoC critical temperature */
  70. #define CRITICAL_TEMP_LIMIT (120 * 1000)
  71. /* Max # of alert channels */
  72. #define ALERT_CH_NUM 3
  73. /* SoC specific thermal sensor data */
  74. struct uniphier_tm_soc_data {
  75. u32 map_base;
  76. u32 block_base;
  77. u32 tmod_setup_addr;
  78. };
  79. struct uniphier_tm_dev {
  80. struct regmap *regmap;
  81. struct device *dev;
  82. bool alert_en[ALERT_CH_NUM];
  83. struct thermal_zone_device *tz_dev;
  84. const struct uniphier_tm_soc_data *data;
  85. };
  86. static int uniphier_tm_initialize_sensor(struct uniphier_tm_dev *tdev)
  87. {
  88. struct regmap *map = tdev->regmap;
  89. u32 val;
  90. u32 tmod_calib[2];
  91. int ret;
  92. /* stop PVT */
  93. regmap_write_bits(map, tdev->data->block_base + PVTCTLEN,
  94. PVTCTLEN_EN, 0);
  95. /*
  96. * Since SoC has a calibrated value that was set in advance,
  97. * TMODCOEF shows non-zero and PVT refers the value internally.
  98. *
  99. * If TMODCOEF shows zero, the boards don't have the calibrated
  100. * value, and the driver has to set default value from DT.
  101. */
  102. ret = regmap_read(map, tdev->data->map_base + TMODCOEF, &val);
  103. if (ret)
  104. return ret;
  105. if (!val) {
  106. /* look for the default values in DT */
  107. ret = of_property_read_u32_array(tdev->dev->of_node,
  108. "socionext,tmod-calibration",
  109. tmod_calib,
  110. ARRAY_SIZE(tmod_calib));
  111. if (ret)
  112. return ret;
  113. regmap_write(map, tdev->data->tmod_setup_addr,
  114. TMODSETUP0_EN | TMODSETUP0_VAL(tmod_calib[0]) |
  115. TMODSETUP1_EN | TMODSETUP1_VAL(tmod_calib[1]));
  116. }
  117. /* select temperature mode */
  118. regmap_write_bits(map, tdev->data->block_base + PVTCTLMODE,
  119. PVTCTLMODE_MASK, PVTCTLMODE_TEMPMON);
  120. /* set monitoring period */
  121. regmap_write_bits(map, tdev->data->block_base + EMONREPEAT,
  122. EMONREPEAT_ENDLESS | EMONREPEAT_PERIOD,
  123. EMONREPEAT_ENDLESS | EMONREPEAT_PERIOD_1000000);
  124. /* set monitor mode */
  125. regmap_write_bits(map, tdev->data->map_base + PVTCTLSEL,
  126. PVTCTLSEL_MASK, PVTCTLSEL_MONITOR);
  127. return 0;
  128. }
  129. static void uniphier_tm_set_alert(struct uniphier_tm_dev *tdev, u32 ch,
  130. u32 temp)
  131. {
  132. struct regmap *map = tdev->regmap;
  133. /* set alert temperature */
  134. regmap_write_bits(map, tdev->data->map_base + SETALERT0 + (ch << 2),
  135. SETALERT_EN | SETALERT_TEMP_OVF,
  136. SETALERT_EN |
  137. SETALERT_TEMP_OVF_VALUE(temp / 1000));
  138. }
  139. static void uniphier_tm_enable_sensor(struct uniphier_tm_dev *tdev)
  140. {
  141. struct regmap *map = tdev->regmap;
  142. int i;
  143. u32 bits = 0;
  144. for (i = 0; i < ALERT_CH_NUM; i++)
  145. if (tdev->alert_en[i])
  146. bits |= PMALERTINTCTL_EN(i);
  147. /* enable alert interrupt */
  148. regmap_write_bits(map, tdev->data->map_base + PMALERTINTCTL,
  149. PMALERTINTCTL_MASK, bits);
  150. /* start PVT */
  151. regmap_write_bits(map, tdev->data->block_base + PVTCTLEN,
  152. PVTCTLEN_EN, PVTCTLEN_EN);
  153. usleep_range(700, 1500); /* The spec note says at least 700us */
  154. }
  155. static void uniphier_tm_disable_sensor(struct uniphier_tm_dev *tdev)
  156. {
  157. struct regmap *map = tdev->regmap;
  158. /* disable alert interrupt */
  159. regmap_write_bits(map, tdev->data->map_base + PMALERTINTCTL,
  160. PMALERTINTCTL_MASK, 0);
  161. /* stop PVT */
  162. regmap_write_bits(map, tdev->data->block_base + PVTCTLEN,
  163. PVTCTLEN_EN, 0);
  164. usleep_range(1000, 2000); /* The spec note says at least 1ms */
  165. }
  166. static int uniphier_tm_get_temp(void *data, int *out_temp)
  167. {
  168. struct uniphier_tm_dev *tdev = data;
  169. struct regmap *map = tdev->regmap;
  170. int ret;
  171. u32 temp;
  172. ret = regmap_read(map, tdev->data->map_base + TMOD, &temp);
  173. if (ret)
  174. return ret;
  175. /* MSB of the TMOD field is a sign bit */
  176. *out_temp = sign_extend32(temp, TMOD_WIDTH - 1) * 1000;
  177. return 0;
  178. }
  179. static const struct thermal_zone_of_device_ops uniphier_of_thermal_ops = {
  180. .get_temp = uniphier_tm_get_temp,
  181. };
  182. static void uniphier_tm_irq_clear(struct uniphier_tm_dev *tdev)
  183. {
  184. u32 mask = 0, bits = 0;
  185. int i;
  186. for (i = 0; i < ALERT_CH_NUM; i++) {
  187. mask |= (PMALERTINTCTL_CLR(i) | PMALERTINTCTL_SET(i));
  188. bits |= PMALERTINTCTL_CLR(i);
  189. }
  190. /* clear alert interrupt */
  191. regmap_write_bits(tdev->regmap,
  192. tdev->data->map_base + PMALERTINTCTL, mask, bits);
  193. }
  194. static irqreturn_t uniphier_tm_alarm_irq(int irq, void *_tdev)
  195. {
  196. struct uniphier_tm_dev *tdev = _tdev;
  197. disable_irq_nosync(irq);
  198. uniphier_tm_irq_clear(tdev);
  199. return IRQ_WAKE_THREAD;
  200. }
  201. static irqreturn_t uniphier_tm_alarm_irq_thread(int irq, void *_tdev)
  202. {
  203. struct uniphier_tm_dev *tdev = _tdev;
  204. thermal_zone_device_update(tdev->tz_dev, THERMAL_EVENT_UNSPECIFIED);
  205. return IRQ_HANDLED;
  206. }
  207. static int uniphier_tm_probe(struct platform_device *pdev)
  208. {
  209. struct device *dev = &pdev->dev;
  210. struct regmap *regmap;
  211. struct device_node *parent;
  212. struct uniphier_tm_dev *tdev;
  213. const struct thermal_trip *trips;
  214. int i, ret, irq, ntrips, crit_temp = INT_MAX;
  215. tdev = devm_kzalloc(dev, sizeof(*tdev), GFP_KERNEL);
  216. if (!tdev)
  217. return -ENOMEM;
  218. tdev->dev = dev;
  219. tdev->data = of_device_get_match_data(dev);
  220. if (WARN_ON(!tdev->data))
  221. return -EINVAL;
  222. irq = platform_get_irq(pdev, 0);
  223. if (irq < 0)
  224. return irq;
  225. /* get regmap from syscon node */
  226. parent = of_get_parent(dev->of_node); /* parent should be syscon node */
  227. regmap = syscon_node_to_regmap(parent);
  228. of_node_put(parent);
  229. if (IS_ERR(regmap)) {
  230. dev_err(dev, "failed to get regmap (error %ld)\n",
  231. PTR_ERR(regmap));
  232. return PTR_ERR(regmap);
  233. }
  234. tdev->regmap = regmap;
  235. ret = uniphier_tm_initialize_sensor(tdev);
  236. if (ret) {
  237. dev_err(dev, "failed to initialize sensor\n");
  238. return ret;
  239. }
  240. ret = devm_request_threaded_irq(dev, irq, uniphier_tm_alarm_irq,
  241. uniphier_tm_alarm_irq_thread,
  242. 0, "thermal", tdev);
  243. if (ret)
  244. return ret;
  245. platform_set_drvdata(pdev, tdev);
  246. tdev->tz_dev = devm_thermal_zone_of_sensor_register(dev, 0, tdev,
  247. &uniphier_of_thermal_ops);
  248. if (IS_ERR(tdev->tz_dev)) {
  249. dev_err(dev, "failed to register sensor device\n");
  250. return PTR_ERR(tdev->tz_dev);
  251. }
  252. /* get trip points */
  253. trips = of_thermal_get_trip_points(tdev->tz_dev);
  254. ntrips = of_thermal_get_ntrips(tdev->tz_dev);
  255. if (ntrips > ALERT_CH_NUM) {
  256. dev_err(dev, "thermal zone has too many trips\n");
  257. return -E2BIG;
  258. }
  259. /* set alert temperatures */
  260. for (i = 0; i < ntrips; i++) {
  261. if (trips[i].type == THERMAL_TRIP_CRITICAL &&
  262. trips[i].temperature < crit_temp)
  263. crit_temp = trips[i].temperature;
  264. uniphier_tm_set_alert(tdev, i, trips[i].temperature);
  265. tdev->alert_en[i] = true;
  266. }
  267. if (crit_temp > CRITICAL_TEMP_LIMIT) {
  268. dev_err(dev, "critical trip is over limit(>%d), or not set\n",
  269. CRITICAL_TEMP_LIMIT);
  270. return -EINVAL;
  271. }
  272. uniphier_tm_enable_sensor(tdev);
  273. return 0;
  274. }
  275. static int uniphier_tm_remove(struct platform_device *pdev)
  276. {
  277. struct uniphier_tm_dev *tdev = platform_get_drvdata(pdev);
  278. /* disable sensor */
  279. uniphier_tm_disable_sensor(tdev);
  280. return 0;
  281. }
  282. static const struct uniphier_tm_soc_data uniphier_pxs2_tm_data = {
  283. .map_base = 0xe000,
  284. .block_base = 0xe000,
  285. .tmod_setup_addr = 0xe904,
  286. };
  287. static const struct uniphier_tm_soc_data uniphier_ld20_tm_data = {
  288. .map_base = 0xe000,
  289. .block_base = 0xe800,
  290. .tmod_setup_addr = 0xe938,
  291. };
  292. static const struct of_device_id uniphier_tm_dt_ids[] = {
  293. {
  294. .compatible = "socionext,uniphier-pxs2-thermal",
  295. .data = &uniphier_pxs2_tm_data,
  296. },
  297. {
  298. .compatible = "socionext,uniphier-ld20-thermal",
  299. .data = &uniphier_ld20_tm_data,
  300. },
  301. {
  302. .compatible = "socionext,uniphier-pxs3-thermal",
  303. .data = &uniphier_ld20_tm_data,
  304. },
  305. { /* sentinel */ }
  306. };
  307. MODULE_DEVICE_TABLE(of, uniphier_tm_dt_ids);
  308. static struct platform_driver uniphier_tm_driver = {
  309. .probe = uniphier_tm_probe,
  310. .remove = uniphier_tm_remove,
  311. .driver = {
  312. .name = "uniphier-thermal",
  313. .of_match_table = uniphier_tm_dt_ids,
  314. },
  315. };
  316. module_platform_driver(uniphier_tm_driver);
  317. MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
  318. MODULE_DESCRIPTION("UniPhier thermal driver");
  319. MODULE_LICENSE("GPL v2");