uniphier_thermal.c 9.5 KB

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