rcar_thermal.c 15 KB

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