rcar_thermal.c 5.8 KB

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