rcar_thermal.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_priv {
  37. void __iomem *base;
  38. struct device *dev;
  39. spinlock_t lock;
  40. };
  41. #define MCELSIUS(temp) ((temp) * 1000)
  42. #define rcar_zone_to_priv(zone) ((zone)->devdata)
  43. #define rcar_priv_to_dev(priv) ((priv)->dev)
  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. struct device *dev = rcar_priv_to_dev(priv);
  86. int i;
  87. int ctemp, old, new;
  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. return 0;
  115. }
  116. static int rcar_thermal_get_trip_type(struct thermal_zone_device *zone,
  117. int trip, enum thermal_trip_type *type)
  118. {
  119. struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
  120. /* see rcar_thermal_get_temp() */
  121. switch (trip) {
  122. case 0: /* +90 <= temp */
  123. *type = THERMAL_TRIP_CRITICAL;
  124. break;
  125. default:
  126. dev_err(priv->dev, "rcar driver trip error\n");
  127. return -EINVAL;
  128. }
  129. return 0;
  130. }
  131. static int rcar_thermal_get_trip_temp(struct thermal_zone_device *zone,
  132. int trip, unsigned long *temp)
  133. {
  134. struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
  135. /* see rcar_thermal_get_temp() */
  136. switch (trip) {
  137. case 0: /* +90 <= temp */
  138. *temp = MCELSIUS(90);
  139. break;
  140. default:
  141. dev_err(priv->dev, "rcar driver trip error\n");
  142. return -EINVAL;
  143. }
  144. return 0;
  145. }
  146. static int rcar_thermal_notify(struct thermal_zone_device *zone,
  147. int trip, enum thermal_trip_type type)
  148. {
  149. struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
  150. switch (type) {
  151. case THERMAL_TRIP_CRITICAL:
  152. /* FIXME */
  153. dev_warn(priv->dev,
  154. "Thermal reached to critical temperature\n");
  155. machine_power_off();
  156. break;
  157. default:
  158. break;
  159. }
  160. return 0;
  161. }
  162. static struct thermal_zone_device_ops rcar_thermal_zone_ops = {
  163. .get_temp = rcar_thermal_get_temp,
  164. .get_trip_type = rcar_thermal_get_trip_type,
  165. .get_trip_temp = rcar_thermal_get_trip_temp,
  166. .notify = rcar_thermal_notify,
  167. };
  168. /*
  169. * platform functions
  170. */
  171. static int rcar_thermal_probe(struct platform_device *pdev)
  172. {
  173. struct thermal_zone_device *zone;
  174. struct rcar_thermal_priv *priv;
  175. struct resource *res;
  176. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  177. if (!res) {
  178. dev_err(&pdev->dev, "Could not get platform resource\n");
  179. return -ENODEV;
  180. }
  181. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  182. if (!priv) {
  183. dev_err(&pdev->dev, "Could not allocate priv\n");
  184. return -ENOMEM;
  185. }
  186. priv->dev = &pdev->dev;
  187. spin_lock_init(&priv->lock);
  188. priv->base = devm_ioremap_nocache(&pdev->dev,
  189. res->start, resource_size(res));
  190. if (!priv->base) {
  191. dev_err(&pdev->dev, "Unable to ioremap thermal register\n");
  192. return -ENOMEM;
  193. }
  194. zone = thermal_zone_device_register("rcar_thermal", 1, 0, priv,
  195. &rcar_thermal_zone_ops, NULL, 0,
  196. IDLE_INTERVAL);
  197. if (IS_ERR(zone)) {
  198. dev_err(&pdev->dev, "thermal zone device is NULL\n");
  199. return PTR_ERR(zone);
  200. }
  201. platform_set_drvdata(pdev, zone);
  202. dev_info(&pdev->dev, "proved\n");
  203. return 0;
  204. }
  205. static int rcar_thermal_remove(struct platform_device *pdev)
  206. {
  207. struct thermal_zone_device *zone = platform_get_drvdata(pdev);
  208. thermal_zone_device_unregister(zone);
  209. platform_set_drvdata(pdev, NULL);
  210. return 0;
  211. }
  212. static struct platform_driver rcar_thermal_driver = {
  213. .driver = {
  214. .name = "rcar_thermal",
  215. },
  216. .probe = rcar_thermal_probe,
  217. .remove = rcar_thermal_remove,
  218. };
  219. module_platform_driver(rcar_thermal_driver);
  220. MODULE_LICENSE("GPL");
  221. MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver");
  222. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");