rcar_thermal.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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/io.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/reboot.h>
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/thermal.h>
  29. #define IDLE_INTERVAL 5000
  30. #define THSCR 0x2c
  31. #define THSSR 0x30
  32. /* THSCR */
  33. #define CPCTL (1 << 12)
  34. /* THSSR */
  35. #define CTEMP 0x3f
  36. struct rcar_thermal_common {
  37. void __iomem *base;
  38. struct device *dev;
  39. struct list_head head;
  40. };
  41. struct rcar_thermal_priv {
  42. void __iomem *base;
  43. struct rcar_thermal_common *common;
  44. struct thermal_zone_device *zone;
  45. struct mutex lock;
  46. struct list_head list;
  47. };
  48. #define rcar_thermal_for_each_priv(pos, common) \
  49. list_for_each_entry(pos, &common->head, list)
  50. #define MCELSIUS(temp) ((temp) * 1000)
  51. #define rcar_zone_to_priv(zone) ((zone)->devdata)
  52. #define rcar_priv_to_dev(priv) ((priv)->common->dev)
  53. #define rcar_has_irq_support(priv) ((priv)->common->base)
  54. /*
  55. * basic functions
  56. */
  57. static u32 rcar_thermal_read(struct rcar_thermal_priv *priv, u32 reg)
  58. {
  59. return ioread32(priv->base + reg);
  60. }
  61. #if 0 /* no user at this point */
  62. static void rcar_thermal_write(struct rcar_thermal_priv *priv,
  63. u32 reg, u32 data)
  64. {
  65. iowrite32(data, priv->base + reg);
  66. }
  67. #endif
  68. static void rcar_thermal_bset(struct rcar_thermal_priv *priv, u32 reg,
  69. u32 mask, u32 data)
  70. {
  71. u32 val;
  72. val = ioread32(priv->base + reg);
  73. val &= ~mask;
  74. val |= (data & mask);
  75. iowrite32(val, priv->base + reg);
  76. }
  77. /*
  78. * zone device functions
  79. */
  80. static int rcar_thermal_get_temp(struct thermal_zone_device *zone,
  81. unsigned long *temp)
  82. {
  83. struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
  84. struct device *dev = rcar_priv_to_dev(priv);
  85. int i;
  86. int ctemp, old, new;
  87. mutex_lock(&priv->lock);
  88. /*
  89. * TSC decides a value of CPTAP automatically,
  90. * and this is the conditions which validate interrupt.
  91. */
  92. rcar_thermal_bset(priv, THSCR, CPCTL, CPCTL);
  93. ctemp = 0;
  94. old = ~0;
  95. for (i = 0; i < 128; i++) {
  96. /*
  97. * we need to wait 300us after changing comparator offset
  98. * to get stable temperature.
  99. * see "Usage Notes" on datasheet
  100. */
  101. udelay(300);
  102. new = rcar_thermal_read(priv, THSSR) & CTEMP;
  103. if (new == old) {
  104. ctemp = new;
  105. break;
  106. }
  107. old = new;
  108. }
  109. if (!ctemp) {
  110. dev_err(dev, "thermal sensor was broken\n");
  111. return -EINVAL;
  112. }
  113. *temp = MCELSIUS((ctemp * 5) - 65);
  114. mutex_unlock(&priv->lock);
  115. return 0;
  116. }
  117. static int rcar_thermal_get_trip_type(struct thermal_zone_device *zone,
  118. int trip, enum thermal_trip_type *type)
  119. {
  120. struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
  121. struct device *dev = rcar_priv_to_dev(priv);
  122. /* see rcar_thermal_get_temp() */
  123. switch (trip) {
  124. case 0: /* +90 <= temp */
  125. *type = THERMAL_TRIP_CRITICAL;
  126. break;
  127. default:
  128. dev_err(dev, "rcar driver trip error\n");
  129. return -EINVAL;
  130. }
  131. return 0;
  132. }
  133. static int rcar_thermal_get_trip_temp(struct thermal_zone_device *zone,
  134. int trip, unsigned long *temp)
  135. {
  136. struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
  137. struct device *dev = rcar_priv_to_dev(priv);
  138. /* see rcar_thermal_get_temp() */
  139. switch (trip) {
  140. case 0: /* +90 <= temp */
  141. *temp = MCELSIUS(90);
  142. break;
  143. default:
  144. dev_err(dev, "rcar driver trip error\n");
  145. return -EINVAL;
  146. }
  147. return 0;
  148. }
  149. static int rcar_thermal_notify(struct thermal_zone_device *zone,
  150. int trip, enum thermal_trip_type type)
  151. {
  152. struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
  153. struct device *dev = rcar_priv_to_dev(priv);
  154. switch (type) {
  155. case THERMAL_TRIP_CRITICAL:
  156. /* FIXME */
  157. dev_warn(dev, "Thermal reached to critical temperature\n");
  158. machine_power_off();
  159. break;
  160. default:
  161. break;
  162. }
  163. return 0;
  164. }
  165. static struct thermal_zone_device_ops rcar_thermal_zone_ops = {
  166. .get_temp = rcar_thermal_get_temp,
  167. .get_trip_type = rcar_thermal_get_trip_type,
  168. .get_trip_temp = rcar_thermal_get_trip_temp,
  169. .notify = rcar_thermal_notify,
  170. };
  171. /*
  172. * platform functions
  173. */
  174. static int rcar_thermal_probe(struct platform_device *pdev)
  175. {
  176. struct rcar_thermal_common *common;
  177. struct rcar_thermal_priv *priv;
  178. struct device *dev = &pdev->dev;
  179. struct resource *res, *irq;
  180. int mres = 0;
  181. int i;
  182. common = devm_kzalloc(dev, sizeof(*common), GFP_KERNEL);
  183. if (!common) {
  184. dev_err(dev, "Could not allocate common\n");
  185. return -ENOMEM;
  186. }
  187. INIT_LIST_HEAD(&common->head);
  188. common->dev = dev;
  189. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  190. if (irq) {
  191. /*
  192. * platform has IRQ support.
  193. * Then, drier use common register
  194. */
  195. res = platform_get_resource(pdev, IORESOURCE_MEM, mres++);
  196. if (!res) {
  197. dev_err(dev, "Could not get platform resource\n");
  198. return -ENODEV;
  199. }
  200. /*
  201. * rcar_has_irq_support() will be enabled
  202. */
  203. common->base = devm_request_and_ioremap(dev, res);
  204. if (!common->base) {
  205. dev_err(dev, "Unable to ioremap thermal register\n");
  206. return -ENOMEM;
  207. }
  208. }
  209. for (i = 0;; i++) {
  210. res = platform_get_resource(pdev, IORESOURCE_MEM, mres++);
  211. if (!res)
  212. break;
  213. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  214. if (!priv) {
  215. dev_err(dev, "Could not allocate priv\n");
  216. return -ENOMEM;
  217. }
  218. priv->base = devm_request_and_ioremap(dev, res);
  219. if (!priv->base) {
  220. dev_err(dev, "Unable to ioremap priv register\n");
  221. return -ENOMEM;
  222. }
  223. priv->common = common;
  224. mutex_init(&priv->lock);
  225. INIT_LIST_HEAD(&priv->list);
  226. priv->zone = thermal_zone_device_register("rcar_thermal",
  227. 1, 0, priv,
  228. &rcar_thermal_zone_ops, NULL, 0,
  229. IDLE_INTERVAL);
  230. if (IS_ERR(priv->zone)) {
  231. dev_err(dev, "can't register thermal zone\n");
  232. goto error_unregister;
  233. }
  234. list_move_tail(&priv->list, &common->head);
  235. }
  236. platform_set_drvdata(pdev, common);
  237. dev_info(dev, "%d sensor proved\n", i);
  238. return 0;
  239. error_unregister:
  240. rcar_thermal_for_each_priv(priv, common)
  241. thermal_zone_device_unregister(priv->zone);
  242. return -ENODEV;
  243. }
  244. static int rcar_thermal_remove(struct platform_device *pdev)
  245. {
  246. struct rcar_thermal_common *common = platform_get_drvdata(pdev);
  247. struct rcar_thermal_priv *priv;
  248. rcar_thermal_for_each_priv(priv, common)
  249. thermal_zone_device_unregister(priv->zone);
  250. platform_set_drvdata(pdev, NULL);
  251. return 0;
  252. }
  253. static struct platform_driver rcar_thermal_driver = {
  254. .driver = {
  255. .name = "rcar_thermal",
  256. },
  257. .probe = rcar_thermal_probe,
  258. .remove = rcar_thermal_remove,
  259. };
  260. module_platform_driver(rcar_thermal_driver);
  261. MODULE_LICENSE("GPL");
  262. MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver");
  263. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");