rcar_thermal.c 15 KB

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