rcar_thermal.c 15 KB

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