rcar_thermal.c 13 KB

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