rcar_thermal.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. 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_of_get_temp(void *data, int *temp)
  236. {
  237. struct rcar_thermal_priv *priv = data;
  238. return rcar_thermal_get_current_temp(priv, temp);
  239. }
  240. static int rcar_thermal_get_temp(struct thermal_zone_device *zone, int *temp)
  241. {
  242. struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
  243. return rcar_thermal_get_current_temp(priv, temp);
  244. }
  245. static int rcar_thermal_get_trip_type(struct thermal_zone_device *zone,
  246. int trip, enum thermal_trip_type *type)
  247. {
  248. struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
  249. struct device *dev = rcar_priv_to_dev(priv);
  250. /* see rcar_thermal_get_temp() */
  251. switch (trip) {
  252. case 0: /* +90 <= temp */
  253. *type = THERMAL_TRIP_CRITICAL;
  254. break;
  255. default:
  256. dev_err(dev, "rcar driver trip error\n");
  257. return -EINVAL;
  258. }
  259. return 0;
  260. }
  261. static int rcar_thermal_get_trip_temp(struct thermal_zone_device *zone,
  262. int trip, int *temp)
  263. {
  264. struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
  265. struct device *dev = rcar_priv_to_dev(priv);
  266. /* see rcar_thermal_get_temp() */
  267. switch (trip) {
  268. case 0: /* +90 <= temp */
  269. *temp = MCELSIUS(90);
  270. break;
  271. default:
  272. dev_err(dev, "rcar driver trip error\n");
  273. return -EINVAL;
  274. }
  275. return 0;
  276. }
  277. static int rcar_thermal_notify(struct thermal_zone_device *zone,
  278. int trip, enum thermal_trip_type type)
  279. {
  280. struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
  281. struct device *dev = rcar_priv_to_dev(priv);
  282. switch (type) {
  283. case THERMAL_TRIP_CRITICAL:
  284. /* FIXME */
  285. dev_warn(dev, "Thermal reached to critical temperature\n");
  286. break;
  287. default:
  288. break;
  289. }
  290. return 0;
  291. }
  292. static const struct thermal_zone_of_device_ops rcar_thermal_zone_of_ops = {
  293. .get_temp = rcar_thermal_of_get_temp,
  294. };
  295. static struct thermal_zone_device_ops rcar_thermal_zone_ops = {
  296. .get_temp = rcar_thermal_get_temp,
  297. .get_trip_type = rcar_thermal_get_trip_type,
  298. .get_trip_temp = rcar_thermal_get_trip_temp,
  299. .notify = rcar_thermal_notify,
  300. };
  301. /*
  302. * interrupt
  303. */
  304. #define rcar_thermal_irq_enable(p) _rcar_thermal_irq_ctrl(p, 1)
  305. #define rcar_thermal_irq_disable(p) _rcar_thermal_irq_ctrl(p, 0)
  306. static void _rcar_thermal_irq_ctrl(struct rcar_thermal_priv *priv, int enable)
  307. {
  308. struct rcar_thermal_common *common = priv->common;
  309. unsigned long flags;
  310. u32 mask = 0x3 << rcar_id_to_shift(priv); /* enable Rising/Falling */
  311. if (!rcar_has_irq_support(priv))
  312. return;
  313. spin_lock_irqsave(&common->lock, flags);
  314. rcar_thermal_common_bset(common, INTMSK, mask, enable ? 0 : mask);
  315. spin_unlock_irqrestore(&common->lock, flags);
  316. }
  317. static void rcar_thermal_work(struct work_struct *work)
  318. {
  319. struct rcar_thermal_priv *priv;
  320. int ret;
  321. priv = container_of(work, struct rcar_thermal_priv, work.work);
  322. ret = rcar_thermal_update_temp(priv);
  323. if (ret < 0)
  324. return;
  325. rcar_thermal_irq_enable(priv);
  326. thermal_zone_device_update(priv->zone, THERMAL_EVENT_UNSPECIFIED);
  327. }
  328. static u32 rcar_thermal_had_changed(struct rcar_thermal_priv *priv, u32 status)
  329. {
  330. struct device *dev = rcar_priv_to_dev(priv);
  331. status = (status >> rcar_id_to_shift(priv)) & 0x3;
  332. if (status) {
  333. dev_dbg(dev, "thermal%d %s%s\n",
  334. priv->id,
  335. (status & 0x2) ? "Rising " : "",
  336. (status & 0x1) ? "Falling" : "");
  337. }
  338. return status;
  339. }
  340. static irqreturn_t rcar_thermal_irq(int irq, void *data)
  341. {
  342. struct rcar_thermal_common *common = data;
  343. struct rcar_thermal_priv *priv;
  344. unsigned long flags;
  345. u32 status, mask;
  346. spin_lock_irqsave(&common->lock, flags);
  347. mask = rcar_thermal_common_read(common, INTMSK);
  348. status = rcar_thermal_common_read(common, STR);
  349. rcar_thermal_common_write(common, STR, 0x000F0F0F & mask);
  350. spin_unlock_irqrestore(&common->lock, flags);
  351. status = status & ~mask;
  352. /*
  353. * check the status
  354. */
  355. rcar_thermal_for_each_priv(priv, common) {
  356. if (rcar_thermal_had_changed(priv, status)) {
  357. rcar_thermal_irq_disable(priv);
  358. queue_delayed_work(system_freezable_wq, &priv->work,
  359. msecs_to_jiffies(300));
  360. }
  361. }
  362. return IRQ_HANDLED;
  363. }
  364. /*
  365. * platform functions
  366. */
  367. static int rcar_thermal_remove(struct platform_device *pdev)
  368. {
  369. struct rcar_thermal_common *common = platform_get_drvdata(pdev);
  370. struct device *dev = &pdev->dev;
  371. struct rcar_thermal_priv *priv;
  372. rcar_thermal_for_each_priv(priv, common) {
  373. rcar_thermal_irq_disable(priv);
  374. cancel_delayed_work_sync(&priv->work);
  375. if (priv->chip->use_of_thermal)
  376. thermal_remove_hwmon_sysfs(priv->zone);
  377. else
  378. thermal_zone_device_unregister(priv->zone);
  379. }
  380. pm_runtime_put(dev);
  381. pm_runtime_disable(dev);
  382. return 0;
  383. }
  384. static int rcar_thermal_probe(struct platform_device *pdev)
  385. {
  386. struct rcar_thermal_common *common;
  387. struct rcar_thermal_priv *priv;
  388. struct device *dev = &pdev->dev;
  389. struct resource *res, *irq;
  390. const struct rcar_thermal_chip *chip = of_device_get_match_data(dev);
  391. int mres = 0;
  392. int i;
  393. int ret = -ENODEV;
  394. int idle = IDLE_INTERVAL;
  395. u32 enr_bits = 0;
  396. common = devm_kzalloc(dev, sizeof(*common), GFP_KERNEL);
  397. if (!common)
  398. return -ENOMEM;
  399. platform_set_drvdata(pdev, common);
  400. INIT_LIST_HEAD(&common->head);
  401. spin_lock_init(&common->lock);
  402. common->dev = dev;
  403. pm_runtime_enable(dev);
  404. pm_runtime_get_sync(dev);
  405. for (i = 0; i < chip->nirqs; i++) {
  406. irq = platform_get_resource(pdev, IORESOURCE_IRQ, i);
  407. if (!irq)
  408. continue;
  409. if (!common->base) {
  410. /*
  411. * platform has IRQ support.
  412. * Then, driver uses common registers
  413. * rcar_has_irq_support() will be enabled
  414. */
  415. res = platform_get_resource(pdev, IORESOURCE_MEM,
  416. mres++);
  417. common->base = devm_ioremap_resource(dev, res);
  418. if (IS_ERR(common->base)) {
  419. ret = PTR_ERR(common->base);
  420. goto error_unregister;
  421. }
  422. idle = 0; /* polling delay is not needed */
  423. }
  424. ret = devm_request_irq(dev, irq->start, rcar_thermal_irq,
  425. IRQF_SHARED, dev_name(dev), common);
  426. if (ret) {
  427. dev_err(dev, "irq request failed\n ");
  428. goto error_unregister;
  429. }
  430. /* update ENR bits */
  431. if (chip->irq_per_ch)
  432. enr_bits |= 1 << i;
  433. }
  434. for (i = 0;; i++) {
  435. res = platform_get_resource(pdev, IORESOURCE_MEM, mres++);
  436. if (!res)
  437. break;
  438. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  439. if (!priv) {
  440. ret = -ENOMEM;
  441. goto error_unregister;
  442. }
  443. priv->base = devm_ioremap_resource(dev, res);
  444. if (IS_ERR(priv->base)) {
  445. ret = PTR_ERR(priv->base);
  446. goto error_unregister;
  447. }
  448. priv->common = common;
  449. priv->id = i;
  450. priv->chip = chip;
  451. mutex_init(&priv->lock);
  452. INIT_LIST_HEAD(&priv->list);
  453. INIT_DELAYED_WORK(&priv->work, rcar_thermal_work);
  454. ret = rcar_thermal_update_temp(priv);
  455. if (ret < 0)
  456. goto error_unregister;
  457. if (chip->use_of_thermal) {
  458. priv->zone = devm_thermal_zone_of_sensor_register(
  459. dev, i, priv,
  460. &rcar_thermal_zone_of_ops);
  461. } else {
  462. priv->zone = thermal_zone_device_register(
  463. "rcar_thermal",
  464. 1, 0, priv,
  465. &rcar_thermal_zone_ops, NULL, 0,
  466. idle);
  467. ret = thermal_zone_device_enable(priv->zone);
  468. if (ret) {
  469. thermal_zone_device_unregister(priv->zone);
  470. priv->zone = ERR_PTR(ret);
  471. }
  472. }
  473. if (IS_ERR(priv->zone)) {
  474. dev_err(dev, "can't register thermal zone\n");
  475. ret = PTR_ERR(priv->zone);
  476. priv->zone = NULL;
  477. goto error_unregister;
  478. }
  479. if (chip->use_of_thermal) {
  480. /*
  481. * thermal_zone doesn't enable hwmon as default,
  482. * but, enable it here to keep compatible
  483. */
  484. priv->zone->tzp->no_hwmon = false;
  485. ret = thermal_add_hwmon_sysfs(priv->zone);
  486. if (ret)
  487. goto error_unregister;
  488. }
  489. rcar_thermal_irq_enable(priv);
  490. list_move_tail(&priv->list, &common->head);
  491. /* update ENR bits */
  492. if (!chip->irq_per_ch)
  493. enr_bits |= 3 << (i * 8);
  494. }
  495. if (common->base && enr_bits)
  496. rcar_thermal_common_write(common, ENR, enr_bits);
  497. dev_info(dev, "%d sensor probed\n", i);
  498. return 0;
  499. error_unregister:
  500. rcar_thermal_remove(pdev);
  501. return ret;
  502. }
  503. #ifdef CONFIG_PM_SLEEP
  504. static int rcar_thermal_suspend(struct device *dev)
  505. {
  506. struct rcar_thermal_common *common = dev_get_drvdata(dev);
  507. struct rcar_thermal_priv *priv = list_first_entry(&common->head,
  508. typeof(*priv), list);
  509. if (priv->chip->needs_suspend_resume) {
  510. rcar_thermal_common_write(common, ENR, 0);
  511. rcar_thermal_irq_disable(priv);
  512. rcar_thermal_bset(priv, THSCR, CPCTL, 0);
  513. }
  514. return 0;
  515. }
  516. static int rcar_thermal_resume(struct device *dev)
  517. {
  518. struct rcar_thermal_common *common = dev_get_drvdata(dev);
  519. struct rcar_thermal_priv *priv = list_first_entry(&common->head,
  520. typeof(*priv), list);
  521. int ret;
  522. if (priv->chip->needs_suspend_resume) {
  523. ret = rcar_thermal_update_temp(priv);
  524. if (ret < 0)
  525. return ret;
  526. rcar_thermal_irq_enable(priv);
  527. rcar_thermal_common_write(common, ENR, 0x03);
  528. }
  529. return 0;
  530. }
  531. #endif
  532. static SIMPLE_DEV_PM_OPS(rcar_thermal_pm_ops, rcar_thermal_suspend,
  533. rcar_thermal_resume);
  534. static struct platform_driver rcar_thermal_driver = {
  535. .driver = {
  536. .name = "rcar_thermal",
  537. .pm = &rcar_thermal_pm_ops,
  538. .of_match_table = rcar_thermal_dt_ids,
  539. },
  540. .probe = rcar_thermal_probe,
  541. .remove = rcar_thermal_remove,
  542. };
  543. module_platform_driver(rcar_thermal_driver);
  544. MODULE_LICENSE("GPL v2");
  545. MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver");
  546. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");