rcar_thermal.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * R-Car THS/TSC thermal sensor driver
  4. *
  5. * Copyright (C) 2012 Renesas Solutions Corp.
  6. * Kuninori Morimoto <[email protected]>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/err.h>
  10. #include <linux/irq.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/of_device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/reboot.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/thermal.h>
  21. #include "thermal_hwmon.h"
  22. #define IDLE_INTERVAL 5000
  23. #define COMMON_STR 0x00
  24. #define COMMON_ENR 0x04
  25. #define COMMON_INTMSK 0x0c
  26. #define REG_POSNEG 0x20
  27. #define REG_FILONOFF 0x28
  28. #define REG_THSCR 0x2c
  29. #define REG_THSSR 0x30
  30. #define REG_INTCTRL 0x34
  31. /* THSCR */
  32. #define CPCTL (1 << 12)
  33. /* THSSR */
  34. #define CTEMP 0x3f
  35. struct rcar_thermal_common {
  36. void __iomem *base;
  37. struct device *dev;
  38. struct list_head head;
  39. spinlock_t lock;
  40. };
  41. struct rcar_thermal_chip {
  42. unsigned int use_of_thermal : 1;
  43. unsigned int has_filonoff : 1;
  44. unsigned int irq_per_ch : 1;
  45. unsigned int needs_suspend_resume : 1;
  46. unsigned int nirqs;
  47. unsigned int ctemp_bands;
  48. };
  49. static const struct rcar_thermal_chip rcar_thermal = {
  50. .use_of_thermal = 0,
  51. .has_filonoff = 1,
  52. .irq_per_ch = 0,
  53. .needs_suspend_resume = 0,
  54. .nirqs = 1,
  55. .ctemp_bands = 1,
  56. };
  57. static const struct rcar_thermal_chip rcar_gen2_thermal = {
  58. .use_of_thermal = 1,
  59. .has_filonoff = 1,
  60. .irq_per_ch = 0,
  61. .needs_suspend_resume = 0,
  62. .nirqs = 1,
  63. .ctemp_bands = 1,
  64. };
  65. static const struct rcar_thermal_chip rcar_gen3_thermal = {
  66. .use_of_thermal = 1,
  67. .has_filonoff = 0,
  68. .irq_per_ch = 1,
  69. .needs_suspend_resume = 1,
  70. /*
  71. * The Gen3 chip has 3 interrupts, but this driver uses only 2
  72. * interrupts to detect a temperature change, rise or fall.
  73. */
  74. .nirqs = 2,
  75. .ctemp_bands = 2,
  76. };
  77. struct rcar_thermal_priv {
  78. void __iomem *base;
  79. struct rcar_thermal_common *common;
  80. struct thermal_zone_device *zone;
  81. const struct rcar_thermal_chip *chip;
  82. struct delayed_work work;
  83. struct mutex lock;
  84. struct list_head list;
  85. int id;
  86. };
  87. #define rcar_thermal_for_each_priv(pos, common) \
  88. list_for_each_entry(pos, &common->head, list)
  89. #define MCELSIUS(temp) ((temp) * 1000)
  90. #define rcar_zone_to_priv(zone) ((zone)->devdata)
  91. #define rcar_priv_to_dev(priv) ((priv)->common->dev)
  92. #define rcar_has_irq_support(priv) ((priv)->common->base)
  93. #define rcar_id_to_shift(priv) ((priv)->id * 8)
  94. static const struct of_device_id rcar_thermal_dt_ids[] = {
  95. {
  96. .compatible = "renesas,rcar-thermal",
  97. .data = &rcar_thermal,
  98. },
  99. {
  100. .compatible = "renesas,rcar-gen2-thermal",
  101. .data = &rcar_gen2_thermal,
  102. },
  103. {
  104. .compatible = "renesas,thermal-r8a774c0",
  105. .data = &rcar_gen3_thermal,
  106. },
  107. {
  108. .compatible = "renesas,thermal-r8a77970",
  109. .data = &rcar_gen3_thermal,
  110. },
  111. {
  112. .compatible = "renesas,thermal-r8a77990",
  113. .data = &rcar_gen3_thermal,
  114. },
  115. {
  116. .compatible = "renesas,thermal-r8a77995",
  117. .data = &rcar_gen3_thermal,
  118. },
  119. {},
  120. };
  121. MODULE_DEVICE_TABLE(of, rcar_thermal_dt_ids);
  122. /*
  123. * basic functions
  124. */
  125. #define rcar_thermal_common_read(c, r) \
  126. _rcar_thermal_common_read(c, COMMON_ ##r)
  127. static u32 _rcar_thermal_common_read(struct rcar_thermal_common *common,
  128. u32 reg)
  129. {
  130. return ioread32(common->base + reg);
  131. }
  132. #define rcar_thermal_common_write(c, r, d) \
  133. _rcar_thermal_common_write(c, COMMON_ ##r, d)
  134. static void _rcar_thermal_common_write(struct rcar_thermal_common *common,
  135. u32 reg, u32 data)
  136. {
  137. iowrite32(data, common->base + reg);
  138. }
  139. #define rcar_thermal_common_bset(c, r, m, d) \
  140. _rcar_thermal_common_bset(c, COMMON_ ##r, m, d)
  141. static void _rcar_thermal_common_bset(struct rcar_thermal_common *common,
  142. u32 reg, u32 mask, u32 data)
  143. {
  144. u32 val;
  145. val = ioread32(common->base + reg);
  146. val &= ~mask;
  147. val |= (data & mask);
  148. iowrite32(val, common->base + reg);
  149. }
  150. #define rcar_thermal_read(p, r) _rcar_thermal_read(p, REG_ ##r)
  151. static u32 _rcar_thermal_read(struct rcar_thermal_priv *priv, u32 reg)
  152. {
  153. return ioread32(priv->base + reg);
  154. }
  155. #define rcar_thermal_write(p, r, d) _rcar_thermal_write(p, REG_ ##r, d)
  156. static void _rcar_thermal_write(struct rcar_thermal_priv *priv,
  157. u32 reg, u32 data)
  158. {
  159. iowrite32(data, priv->base + reg);
  160. }
  161. #define rcar_thermal_bset(p, r, m, d) _rcar_thermal_bset(p, REG_ ##r, m, d)
  162. static void _rcar_thermal_bset(struct rcar_thermal_priv *priv, u32 reg,
  163. u32 mask, u32 data)
  164. {
  165. u32 val;
  166. val = ioread32(priv->base + reg);
  167. val &= ~mask;
  168. val |= (data & mask);
  169. iowrite32(val, priv->base + reg);
  170. }
  171. /*
  172. * zone device functions
  173. */
  174. static int rcar_thermal_update_temp(struct rcar_thermal_priv *priv)
  175. {
  176. struct device *dev = rcar_priv_to_dev(priv);
  177. int old, new, ctemp = -EINVAL;
  178. unsigned int i;
  179. mutex_lock(&priv->lock);
  180. /*
  181. * TSC decides a value of CPTAP automatically,
  182. * and this is the conditions which validate interrupt.
  183. */
  184. rcar_thermal_bset(priv, THSCR, CPCTL, CPCTL);
  185. old = ~0;
  186. for (i = 0; i < 128; i++) {
  187. /*
  188. * we need to wait 300us after changing comparator offset
  189. * to get stable temperature.
  190. * see "Usage Notes" on datasheet
  191. */
  192. usleep_range(300, 400);
  193. new = rcar_thermal_read(priv, THSSR) & CTEMP;
  194. if (new == old) {
  195. ctemp = new;
  196. break;
  197. }
  198. old = new;
  199. }
  200. if (ctemp < 0) {
  201. dev_err(dev, "thermal sensor was broken\n");
  202. goto err_out_unlock;
  203. }
  204. /*
  205. * enable IRQ
  206. */
  207. if (rcar_has_irq_support(priv)) {
  208. if (priv->chip->has_filonoff)
  209. rcar_thermal_write(priv, FILONOFF, 0);
  210. /* enable Rising/Falling edge interrupt */
  211. rcar_thermal_write(priv, POSNEG, 0x1);
  212. rcar_thermal_write(priv, INTCTRL, (((ctemp - 0) << 8) |
  213. ((ctemp - 1) << 0)));
  214. }
  215. err_out_unlock:
  216. mutex_unlock(&priv->lock);
  217. return ctemp;
  218. }
  219. static int rcar_thermal_get_current_temp(struct rcar_thermal_priv *priv,
  220. int *temp)
  221. {
  222. int ctemp;
  223. ctemp = rcar_thermal_update_temp(priv);
  224. if (ctemp < 0)
  225. return ctemp;
  226. /* Guaranteed operating range is -45C to 125C. */
  227. if (priv->chip->ctemp_bands == 1)
  228. *temp = MCELSIUS((ctemp * 5) - 65);
  229. else if (ctemp < 24)
  230. *temp = MCELSIUS(((ctemp * 55) - 720) / 10);
  231. else
  232. *temp = MCELSIUS((ctemp * 5) - 60);
  233. return 0;
  234. }
  235. static int rcar_thermal_get_temp(struct thermal_zone_device *zone, int *temp)
  236. {
  237. struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
  238. return rcar_thermal_get_current_temp(priv, temp);
  239. }
  240. static int rcar_thermal_get_trip_type(struct thermal_zone_device *zone,
  241. int trip, enum thermal_trip_type *type)
  242. {
  243. struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
  244. struct device *dev = rcar_priv_to_dev(priv);
  245. /* see rcar_thermal_get_temp() */
  246. switch (trip) {
  247. case 0: /* +90 <= temp */
  248. *type = THERMAL_TRIP_CRITICAL;
  249. break;
  250. default:
  251. dev_err(dev, "rcar driver trip error\n");
  252. return -EINVAL;
  253. }
  254. return 0;
  255. }
  256. static int rcar_thermal_get_trip_temp(struct thermal_zone_device *zone,
  257. int trip, int *temp)
  258. {
  259. struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
  260. struct device *dev = rcar_priv_to_dev(priv);
  261. /* see rcar_thermal_get_temp() */
  262. switch (trip) {
  263. case 0: /* +90 <= temp */
  264. *temp = MCELSIUS(90);
  265. break;
  266. default:
  267. dev_err(dev, "rcar driver trip error\n");
  268. return -EINVAL;
  269. }
  270. return 0;
  271. }
  272. static const struct thermal_zone_device_ops rcar_thermal_zone_of_ops = {
  273. .get_temp = rcar_thermal_get_temp,
  274. };
  275. static struct thermal_zone_device_ops rcar_thermal_zone_ops = {
  276. .get_temp = rcar_thermal_get_temp,
  277. .get_trip_type = rcar_thermal_get_trip_type,
  278. .get_trip_temp = rcar_thermal_get_trip_temp,
  279. };
  280. /*
  281. * interrupt
  282. */
  283. #define rcar_thermal_irq_enable(p) _rcar_thermal_irq_ctrl(p, 1)
  284. #define rcar_thermal_irq_disable(p) _rcar_thermal_irq_ctrl(p, 0)
  285. static void _rcar_thermal_irq_ctrl(struct rcar_thermal_priv *priv, int enable)
  286. {
  287. struct rcar_thermal_common *common = priv->common;
  288. unsigned long flags;
  289. u32 mask = 0x3 << rcar_id_to_shift(priv); /* enable Rising/Falling */
  290. if (!rcar_has_irq_support(priv))
  291. return;
  292. spin_lock_irqsave(&common->lock, flags);
  293. rcar_thermal_common_bset(common, INTMSK, mask, enable ? 0 : mask);
  294. spin_unlock_irqrestore(&common->lock, flags);
  295. }
  296. static void rcar_thermal_work(struct work_struct *work)
  297. {
  298. struct rcar_thermal_priv *priv;
  299. int ret;
  300. priv = container_of(work, struct rcar_thermal_priv, work.work);
  301. ret = rcar_thermal_update_temp(priv);
  302. if (ret < 0)
  303. return;
  304. rcar_thermal_irq_enable(priv);
  305. thermal_zone_device_update(priv->zone, THERMAL_EVENT_UNSPECIFIED);
  306. }
  307. static u32 rcar_thermal_had_changed(struct rcar_thermal_priv *priv, u32 status)
  308. {
  309. struct device *dev = rcar_priv_to_dev(priv);
  310. status = (status >> rcar_id_to_shift(priv)) & 0x3;
  311. if (status) {
  312. dev_dbg(dev, "thermal%d %s%s\n",
  313. priv->id,
  314. (status & 0x2) ? "Rising " : "",
  315. (status & 0x1) ? "Falling" : "");
  316. }
  317. return status;
  318. }
  319. static irqreturn_t rcar_thermal_irq(int irq, void *data)
  320. {
  321. struct rcar_thermal_common *common = data;
  322. struct rcar_thermal_priv *priv;
  323. u32 status, mask;
  324. spin_lock(&common->lock);
  325. mask = rcar_thermal_common_read(common, INTMSK);
  326. status = rcar_thermal_common_read(common, STR);
  327. rcar_thermal_common_write(common, STR, 0x000F0F0F & mask);
  328. spin_unlock(&common->lock);
  329. status = status & ~mask;
  330. /*
  331. * check the status
  332. */
  333. rcar_thermal_for_each_priv(priv, common) {
  334. if (rcar_thermal_had_changed(priv, status)) {
  335. rcar_thermal_irq_disable(priv);
  336. queue_delayed_work(system_freezable_wq, &priv->work,
  337. msecs_to_jiffies(300));
  338. }
  339. }
  340. return IRQ_HANDLED;
  341. }
  342. /*
  343. * platform functions
  344. */
  345. static int rcar_thermal_remove(struct platform_device *pdev)
  346. {
  347. struct rcar_thermal_common *common = platform_get_drvdata(pdev);
  348. struct device *dev = &pdev->dev;
  349. struct rcar_thermal_priv *priv;
  350. rcar_thermal_for_each_priv(priv, common) {
  351. rcar_thermal_irq_disable(priv);
  352. cancel_delayed_work_sync(&priv->work);
  353. if (priv->chip->use_of_thermal)
  354. thermal_remove_hwmon_sysfs(priv->zone);
  355. else
  356. thermal_zone_device_unregister(priv->zone);
  357. }
  358. pm_runtime_put(dev);
  359. pm_runtime_disable(dev);
  360. return 0;
  361. }
  362. static int rcar_thermal_probe(struct platform_device *pdev)
  363. {
  364. struct rcar_thermal_common *common;
  365. struct rcar_thermal_priv *priv;
  366. struct device *dev = &pdev->dev;
  367. struct resource *res;
  368. const struct rcar_thermal_chip *chip = of_device_get_match_data(dev);
  369. int mres = 0;
  370. int i;
  371. int ret = -ENODEV;
  372. int idle = IDLE_INTERVAL;
  373. u32 enr_bits = 0;
  374. common = devm_kzalloc(dev, sizeof(*common), GFP_KERNEL);
  375. if (!common)
  376. return -ENOMEM;
  377. platform_set_drvdata(pdev, common);
  378. INIT_LIST_HEAD(&common->head);
  379. spin_lock_init(&common->lock);
  380. common->dev = dev;
  381. pm_runtime_enable(dev);
  382. pm_runtime_get_sync(dev);
  383. for (i = 0; i < chip->nirqs; i++) {
  384. int irq;
  385. ret = platform_get_irq_optional(pdev, i);
  386. if (ret < 0 && ret != -ENXIO)
  387. goto error_unregister;
  388. if (ret > 0)
  389. irq = ret;
  390. else
  391. break;
  392. if (!common->base) {
  393. /*
  394. * platform has IRQ support.
  395. * Then, driver uses common registers
  396. * rcar_has_irq_support() will be enabled
  397. */
  398. res = platform_get_resource(pdev, IORESOURCE_MEM,
  399. mres++);
  400. common->base = devm_ioremap_resource(dev, res);
  401. if (IS_ERR(common->base)) {
  402. ret = PTR_ERR(common->base);
  403. goto error_unregister;
  404. }
  405. idle = 0; /* polling delay is not needed */
  406. }
  407. ret = devm_request_irq(dev, irq, rcar_thermal_irq,
  408. IRQF_SHARED, dev_name(dev), common);
  409. if (ret) {
  410. dev_err(dev, "irq request failed\n ");
  411. goto error_unregister;
  412. }
  413. /* update ENR bits */
  414. if (chip->irq_per_ch)
  415. enr_bits |= 1 << i;
  416. }
  417. for (i = 0;; i++) {
  418. res = platform_get_resource(pdev, IORESOURCE_MEM, mres++);
  419. if (!res)
  420. break;
  421. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  422. if (!priv) {
  423. ret = -ENOMEM;
  424. goto error_unregister;
  425. }
  426. priv->base = devm_ioremap_resource(dev, res);
  427. if (IS_ERR(priv->base)) {
  428. ret = PTR_ERR(priv->base);
  429. goto error_unregister;
  430. }
  431. priv->common = common;
  432. priv->id = i;
  433. priv->chip = chip;
  434. mutex_init(&priv->lock);
  435. INIT_LIST_HEAD(&priv->list);
  436. INIT_DELAYED_WORK(&priv->work, rcar_thermal_work);
  437. ret = rcar_thermal_update_temp(priv);
  438. if (ret < 0)
  439. goto error_unregister;
  440. if (chip->use_of_thermal) {
  441. priv->zone = devm_thermal_of_zone_register(
  442. dev, i, priv,
  443. &rcar_thermal_zone_of_ops);
  444. } else {
  445. priv->zone = thermal_zone_device_register(
  446. "rcar_thermal",
  447. 1, 0, priv,
  448. &rcar_thermal_zone_ops, NULL, 0,
  449. idle);
  450. ret = thermal_zone_device_enable(priv->zone);
  451. if (ret) {
  452. thermal_zone_device_unregister(priv->zone);
  453. priv->zone = ERR_PTR(ret);
  454. }
  455. }
  456. if (IS_ERR(priv->zone)) {
  457. dev_err(dev, "can't register thermal zone\n");
  458. ret = PTR_ERR(priv->zone);
  459. priv->zone = NULL;
  460. goto error_unregister;
  461. }
  462. if (chip->use_of_thermal) {
  463. /*
  464. * thermal_zone doesn't enable hwmon as default,
  465. * but, enable it here to keep compatible
  466. */
  467. priv->zone->tzp->no_hwmon = false;
  468. ret = thermal_add_hwmon_sysfs(priv->zone);
  469. if (ret)
  470. goto error_unregister;
  471. }
  472. rcar_thermal_irq_enable(priv);
  473. list_move_tail(&priv->list, &common->head);
  474. /* update ENR bits */
  475. if (!chip->irq_per_ch)
  476. enr_bits |= 3 << (i * 8);
  477. }
  478. if (common->base && enr_bits)
  479. rcar_thermal_common_write(common, ENR, enr_bits);
  480. dev_info(dev, "%d sensor probed\n", i);
  481. return 0;
  482. error_unregister:
  483. rcar_thermal_remove(pdev);
  484. return ret;
  485. }
  486. #ifdef CONFIG_PM_SLEEP
  487. static int rcar_thermal_suspend(struct device *dev)
  488. {
  489. struct rcar_thermal_common *common = dev_get_drvdata(dev);
  490. struct rcar_thermal_priv *priv = list_first_entry(&common->head,
  491. typeof(*priv), list);
  492. if (priv->chip->needs_suspend_resume) {
  493. rcar_thermal_common_write(common, ENR, 0);
  494. rcar_thermal_irq_disable(priv);
  495. rcar_thermal_bset(priv, THSCR, CPCTL, 0);
  496. }
  497. return 0;
  498. }
  499. static int rcar_thermal_resume(struct device *dev)
  500. {
  501. struct rcar_thermal_common *common = dev_get_drvdata(dev);
  502. struct rcar_thermal_priv *priv = list_first_entry(&common->head,
  503. typeof(*priv), list);
  504. int ret;
  505. if (priv->chip->needs_suspend_resume) {
  506. ret = rcar_thermal_update_temp(priv);
  507. if (ret < 0)
  508. return ret;
  509. rcar_thermal_irq_enable(priv);
  510. rcar_thermal_common_write(common, ENR, 0x03);
  511. }
  512. return 0;
  513. }
  514. #endif
  515. static SIMPLE_DEV_PM_OPS(rcar_thermal_pm_ops, rcar_thermal_suspend,
  516. rcar_thermal_resume);
  517. static struct platform_driver rcar_thermal_driver = {
  518. .driver = {
  519. .name = "rcar_thermal",
  520. .pm = &rcar_thermal_pm_ops,
  521. .of_match_table = rcar_thermal_dt_ids,
  522. },
  523. .probe = rcar_thermal_probe,
  524. .remove = rcar_thermal_remove,
  525. };
  526. module_platform_driver(rcar_thermal_driver);
  527. MODULE_LICENSE("GPL v2");
  528. MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver");
  529. MODULE_AUTHOR("Kuninori Morimoto <[email protected]>");