rcar_thermal.c 13 KB

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