rcar_thermal.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 CPTAP 0xf
  34. /* THSSR */
  35. #define CTEMP 0x3f
  36. struct rcar_thermal_priv {
  37. void __iomem *base;
  38. struct device *dev;
  39. spinlock_t lock;
  40. u32 comp;
  41. };
  42. #define MCELSIUS(temp) ((temp) * 1000)
  43. #define rcar_zone_to_priv(zone) ((zone)->devdata)
  44. /*
  45. * basic functions
  46. */
  47. static u32 rcar_thermal_read(struct rcar_thermal_priv *priv, u32 reg)
  48. {
  49. unsigned long flags;
  50. u32 ret;
  51. spin_lock_irqsave(&priv->lock, flags);
  52. ret = ioread32(priv->base + reg);
  53. spin_unlock_irqrestore(&priv->lock, flags);
  54. return ret;
  55. }
  56. #if 0 /* no user at this point */
  57. static void rcar_thermal_write(struct rcar_thermal_priv *priv,
  58. u32 reg, u32 data)
  59. {
  60. unsigned long flags;
  61. spin_lock_irqsave(&priv->lock, flags);
  62. iowrite32(data, priv->base + reg);
  63. spin_unlock_irqrestore(&priv->lock, flags);
  64. }
  65. #endif
  66. static void rcar_thermal_bset(struct rcar_thermal_priv *priv, u32 reg,
  67. u32 mask, u32 data)
  68. {
  69. unsigned long flags;
  70. u32 val;
  71. spin_lock_irqsave(&priv->lock, flags);
  72. val = ioread32(priv->base + reg);
  73. val &= ~mask;
  74. val |= (data & mask);
  75. iowrite32(val, priv->base + reg);
  76. spin_unlock_irqrestore(&priv->lock, flags);
  77. }
  78. /*
  79. * zone device functions
  80. */
  81. static int rcar_thermal_get_temp(struct thermal_zone_device *zone,
  82. unsigned long *temp)
  83. {
  84. struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
  85. int val, min, max, tmp;
  86. tmp = -200; /* default */
  87. while (1) {
  88. if (priv->comp < 1 || priv->comp > 12) {
  89. dev_err(priv->dev,
  90. "THSSR invalid data (%d)\n", priv->comp);
  91. priv->comp = 4; /* for next thermal */
  92. return -EINVAL;
  93. }
  94. /*
  95. * THS comparator offset and the reference temperature
  96. *
  97. * Comparator | reference | Temperature field
  98. * offset | temperature | measurement
  99. * | (degrees C) | (degrees C)
  100. * -------------+---------------+-------------------
  101. * 1 | -45 | -45 to -30
  102. * 2 | -30 | -30 to -15
  103. * 3 | -15 | -15 to 0
  104. * 4 | 0 | 0 to +15
  105. * 5 | +15 | +15 to +30
  106. * 6 | +30 | +30 to +45
  107. * 7 | +45 | +45 to +60
  108. * 8 | +60 | +60 to +75
  109. * 9 | +75 | +75 to +90
  110. * 10 | +90 | +90 to +105
  111. * 11 | +105 | +105 to +120
  112. * 12 | +120 | +120 to +135
  113. */
  114. /* calculate thermal limitation */
  115. min = (priv->comp * 15) - 60;
  116. max = min + 15;
  117. /*
  118. * we need to wait 300us after changing comparator offset
  119. * to get stable temperature.
  120. * see "Usage Notes" on datasheet
  121. */
  122. rcar_thermal_bset(priv, THSCR, CPTAP, priv->comp);
  123. udelay(300);
  124. /* calculate current temperature */
  125. val = rcar_thermal_read(priv, THSSR) & CTEMP;
  126. val = (val * 5) - 65;
  127. dev_dbg(priv->dev, "comp/min/max/val = %d/%d/%d/%d\n",
  128. priv->comp, min, max, val);
  129. /*
  130. * If val is same as min/max, then,
  131. * it should try again on next comparator.
  132. * But the val might be correct temperature.
  133. * Keep it on "tmp" and compare with next val.
  134. */
  135. if (tmp == val)
  136. break;
  137. if (val <= min) {
  138. tmp = min;
  139. priv->comp--; /* try again */
  140. } else if (val >= max) {
  141. tmp = max;
  142. priv->comp++; /* try again */
  143. } else {
  144. tmp = val;
  145. break;
  146. }
  147. }
  148. *temp = MCELSIUS(tmp);
  149. return 0;
  150. }
  151. static int rcar_thermal_get_trip_type(struct thermal_zone_device *zone,
  152. int trip, enum thermal_trip_type *type)
  153. {
  154. struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
  155. /* see rcar_thermal_get_temp() */
  156. switch (trip) {
  157. case 0: /* +90 <= temp */
  158. *type = THERMAL_TRIP_CRITICAL;
  159. break;
  160. default:
  161. dev_err(priv->dev, "rcar driver trip error\n");
  162. return -EINVAL;
  163. }
  164. return 0;
  165. }
  166. static int rcar_thermal_get_trip_temp(struct thermal_zone_device *zone,
  167. int trip, unsigned long *temp)
  168. {
  169. struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
  170. /* see rcar_thermal_get_temp() */
  171. switch (trip) {
  172. case 0: /* +90 <= temp */
  173. *temp = MCELSIUS(90);
  174. break;
  175. default:
  176. dev_err(priv->dev, "rcar driver trip error\n");
  177. return -EINVAL;
  178. }
  179. return 0;
  180. }
  181. static int rcar_thermal_notify(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. switch (type) {
  186. case THERMAL_TRIP_CRITICAL:
  187. /* FIXME */
  188. dev_warn(priv->dev,
  189. "Thermal reached to critical temperature\n");
  190. machine_power_off();
  191. break;
  192. default:
  193. break;
  194. }
  195. return 0;
  196. }
  197. static struct thermal_zone_device_ops rcar_thermal_zone_ops = {
  198. .get_temp = rcar_thermal_get_temp,
  199. .get_trip_type = rcar_thermal_get_trip_type,
  200. .get_trip_temp = rcar_thermal_get_trip_temp,
  201. .notify = rcar_thermal_notify,
  202. };
  203. /*
  204. * platform functions
  205. */
  206. static int rcar_thermal_probe(struct platform_device *pdev)
  207. {
  208. struct thermal_zone_device *zone;
  209. struct rcar_thermal_priv *priv;
  210. struct resource *res;
  211. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  212. if (!res) {
  213. dev_err(&pdev->dev, "Could not get platform resource\n");
  214. return -ENODEV;
  215. }
  216. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  217. if (!priv) {
  218. dev_err(&pdev->dev, "Could not allocate priv\n");
  219. return -ENOMEM;
  220. }
  221. priv->comp = 4; /* basic setup */
  222. priv->dev = &pdev->dev;
  223. spin_lock_init(&priv->lock);
  224. priv->base = devm_ioremap_nocache(&pdev->dev,
  225. res->start, resource_size(res));
  226. if (!priv->base) {
  227. dev_err(&pdev->dev, "Unable to ioremap thermal register\n");
  228. return -ENOMEM;
  229. }
  230. zone = thermal_zone_device_register("rcar_thermal", 1, 0, priv,
  231. &rcar_thermal_zone_ops, NULL, 0,
  232. IDLE_INTERVAL);
  233. if (IS_ERR(zone)) {
  234. dev_err(&pdev->dev, "thermal zone device is NULL\n");
  235. return PTR_ERR(zone);
  236. }
  237. platform_set_drvdata(pdev, zone);
  238. dev_info(&pdev->dev, "proved\n");
  239. return 0;
  240. }
  241. static int rcar_thermal_remove(struct platform_device *pdev)
  242. {
  243. struct thermal_zone_device *zone = platform_get_drvdata(pdev);
  244. thermal_zone_device_unregister(zone);
  245. platform_set_drvdata(pdev, NULL);
  246. return 0;
  247. }
  248. static struct platform_driver rcar_thermal_driver = {
  249. .driver = {
  250. .name = "rcar_thermal",
  251. },
  252. .probe = rcar_thermal_probe,
  253. .remove = rcar_thermal_remove,
  254. };
  255. module_platform_driver(rcar_thermal_driver);
  256. MODULE_LICENSE("GPL");
  257. MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver");
  258. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");