rcar_thermal.c 12 KB

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