rcar_thermal.c 6.0 KB

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