rcar_gen3_thermal.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * R-Car Gen3 THS thermal sensor driver
  3. * Based on rcar_thermal.c and work from Hien Dang and Khiem Nguyen.
  4. *
  5. * Copyright (C) 2016 Renesas Electronics Corporation.
  6. * Copyright (C) 2016 Sang Engineering
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. */
  18. #include <linux/delay.h>
  19. #include <linux/err.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/module.h>
  23. #include <linux/mutex.h>
  24. #include <linux/of_device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/thermal.h>
  28. /* Register offsets */
  29. #define REG_GEN3_IRQSTR 0x04
  30. #define REG_GEN3_IRQMSK 0x08
  31. #define REG_GEN3_IRQCTL 0x0C
  32. #define REG_GEN3_IRQEN 0x10
  33. #define REG_GEN3_IRQTEMP1 0x14
  34. #define REG_GEN3_IRQTEMP2 0x18
  35. #define REG_GEN3_IRQTEMP3 0x1C
  36. #define REG_GEN3_CTSR 0x20
  37. #define REG_GEN3_THCTR 0x20
  38. #define REG_GEN3_TEMP 0x28
  39. #define REG_GEN3_THCODE1 0x50
  40. #define REG_GEN3_THCODE2 0x54
  41. #define REG_GEN3_THCODE3 0x58
  42. /* CTSR bits */
  43. #define CTSR_PONM BIT(8)
  44. #define CTSR_AOUT BIT(7)
  45. #define CTSR_THBGR BIT(5)
  46. #define CTSR_VMEN BIT(4)
  47. #define CTSR_VMST BIT(1)
  48. #define CTSR_THSST BIT(0)
  49. /* THCTR bits */
  50. #define THCTR_PONM BIT(6)
  51. #define THCTR_THSST BIT(0)
  52. #define CTEMP_MASK 0xFFF
  53. #define MCELSIUS(temp) ((temp) * 1000)
  54. #define GEN3_FUSE_MASK 0xFFF
  55. #define TSC_MAX_NUM 3
  56. /* Structure for thermal temperature calculation */
  57. struct equation_coefs {
  58. int a1;
  59. int b1;
  60. int a2;
  61. int b2;
  62. };
  63. struct rcar_gen3_thermal_tsc {
  64. void __iomem *base;
  65. struct thermal_zone_device *zone;
  66. struct equation_coefs coef;
  67. struct mutex lock;
  68. };
  69. struct rcar_gen3_thermal_priv {
  70. struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
  71. };
  72. struct rcar_gen3_thermal_data {
  73. void (*thermal_init)(struct rcar_gen3_thermal_tsc *tsc);
  74. };
  75. static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
  76. u32 reg)
  77. {
  78. return ioread32(tsc->base + reg);
  79. }
  80. static inline void rcar_gen3_thermal_write(struct rcar_gen3_thermal_tsc *tsc,
  81. u32 reg, u32 data)
  82. {
  83. iowrite32(data, tsc->base + reg);
  84. }
  85. /*
  86. * Linear approximation for temperature
  87. *
  88. * [reg] = [temp] * a + b => [temp] = ([reg] - b) / a
  89. *
  90. * The constants a and b are calculated using two triplets of int values PTAT
  91. * and THCODE. PTAT and THCODE can either be read from hardware or use hard
  92. * coded values from driver. The formula to calculate a and b are taken from
  93. * BSP and sparsely documented and understood.
  94. *
  95. * Examining the linear formula and the formula used to calculate constants a
  96. * and b while knowing that the span for PTAT and THCODE values are between
  97. * 0x000 and 0xfff the largest integer possible is 0xfff * 0xfff == 0xffe001.
  98. * Integer also needs to be signed so that leaves 7 bits for binary
  99. * fixed point scaling.
  100. */
  101. #define FIXPT_SHIFT 7
  102. #define FIXPT_INT(_x) ((_x) << FIXPT_SHIFT)
  103. #define FIXPT_DIV(_a, _b) DIV_ROUND_CLOSEST(((_a) << FIXPT_SHIFT), (_b))
  104. #define FIXPT_TO_MCELSIUS(_x) ((_x) * 1000 >> FIXPT_SHIFT)
  105. #define RCAR3_THERMAL_GRAN 500 /* mili Celsius */
  106. /* no idea where these constants come from */
  107. #define TJ_1 96
  108. #define TJ_3 -41
  109. static void rcar_gen3_thermal_calc_coefs(struct equation_coefs *coef,
  110. int *ptat, int *thcode)
  111. {
  112. int tj_2;
  113. /* TODO: Find documentation and document constant calculation formula */
  114. /*
  115. * Division is not scaled in BSP and if scaled it might overflow
  116. * the dividend (4095 * 4095 << 14 > INT_MAX) so keep it unscaled
  117. */
  118. tj_2 = (FIXPT_INT((ptat[1] - ptat[2]) * 137)
  119. / (ptat[0] - ptat[2])) - FIXPT_INT(41);
  120. coef->a1 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[2]),
  121. tj_2 - FIXPT_INT(TJ_3));
  122. coef->b1 = FIXPT_INT(thcode[2]) - coef->a1 * TJ_3;
  123. coef->a2 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[0]),
  124. tj_2 - FIXPT_INT(TJ_1));
  125. coef->b2 = FIXPT_INT(thcode[0]) - coef->a2 * TJ_1;
  126. }
  127. static int rcar_gen3_thermal_round(int temp)
  128. {
  129. int result, round_offs;
  130. round_offs = temp >= 0 ? RCAR3_THERMAL_GRAN / 2 :
  131. -RCAR3_THERMAL_GRAN / 2;
  132. result = (temp + round_offs) / RCAR3_THERMAL_GRAN;
  133. return result * RCAR3_THERMAL_GRAN;
  134. }
  135. static int rcar_gen3_thermal_get_temp(void *devdata, int *temp)
  136. {
  137. struct rcar_gen3_thermal_tsc *tsc = devdata;
  138. int mcelsius, val1, val2;
  139. u32 reg;
  140. /* Read register and convert to mili Celsius */
  141. mutex_lock(&tsc->lock);
  142. reg = rcar_gen3_thermal_read(tsc, REG_GEN3_TEMP) & CTEMP_MASK;
  143. val1 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b1, tsc->coef.a1);
  144. val2 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b2, tsc->coef.a2);
  145. mcelsius = FIXPT_TO_MCELSIUS((val1 + val2) / 2);
  146. mutex_unlock(&tsc->lock);
  147. /* Make sure we are inside specifications */
  148. if ((mcelsius < MCELSIUS(-40)) || (mcelsius > MCELSIUS(125)))
  149. return -EIO;
  150. /* Round value to device granularity setting */
  151. *temp = rcar_gen3_thermal_round(mcelsius);
  152. return 0;
  153. }
  154. static struct thermal_zone_of_device_ops rcar_gen3_tz_of_ops = {
  155. .get_temp = rcar_gen3_thermal_get_temp,
  156. };
  157. static void r8a7795_thermal_init(struct rcar_gen3_thermal_tsc *tsc)
  158. {
  159. rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_THBGR);
  160. rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, 0x0);
  161. usleep_range(1000, 2000);
  162. rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_PONM);
  163. rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F);
  164. rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
  165. CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN);
  166. usleep_range(100, 200);
  167. rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
  168. CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN |
  169. CTSR_VMST | CTSR_THSST);
  170. usleep_range(1000, 2000);
  171. }
  172. static void r8a7796_thermal_init(struct rcar_gen3_thermal_tsc *tsc)
  173. {
  174. u32 reg_val;
  175. reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
  176. reg_val &= ~THCTR_PONM;
  177. rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
  178. usleep_range(1000, 2000);
  179. rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F);
  180. reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
  181. reg_val |= THCTR_THSST;
  182. rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
  183. }
  184. static const struct rcar_gen3_thermal_data r8a7795_data = {
  185. .thermal_init = r8a7795_thermal_init,
  186. };
  187. static const struct rcar_gen3_thermal_data r8a7796_data = {
  188. .thermal_init = r8a7796_thermal_init,
  189. };
  190. static const struct of_device_id rcar_gen3_thermal_dt_ids[] = {
  191. { .compatible = "renesas,r8a7795-thermal", .data = &r8a7795_data},
  192. { .compatible = "renesas,r8a7796-thermal", .data = &r8a7796_data},
  193. {},
  194. };
  195. MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
  196. static int rcar_gen3_thermal_remove(struct platform_device *pdev)
  197. {
  198. struct device *dev = &pdev->dev;
  199. pm_runtime_put(dev);
  200. pm_runtime_disable(dev);
  201. return 0;
  202. }
  203. static int rcar_gen3_thermal_probe(struct platform_device *pdev)
  204. {
  205. struct rcar_gen3_thermal_priv *priv;
  206. struct device *dev = &pdev->dev;
  207. struct resource *res;
  208. struct thermal_zone_device *zone;
  209. int ret, i;
  210. const struct rcar_gen3_thermal_data *match_data =
  211. of_device_get_match_data(dev);
  212. /* default values if FUSEs are missing */
  213. /* TODO: Read values from hardware on supported platforms */
  214. int ptat[3] = { 2351, 1509, 435 };
  215. int thcode[TSC_MAX_NUM][3] = {
  216. { 3248, 2800, 2221 },
  217. { 3245, 2795, 2216 },
  218. { 3250, 2805, 2237 },
  219. };
  220. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  221. if (!priv)
  222. return -ENOMEM;
  223. platform_set_drvdata(pdev, priv);
  224. pm_runtime_enable(dev);
  225. pm_runtime_get_sync(dev);
  226. for (i = 0; i < TSC_MAX_NUM; i++) {
  227. struct rcar_gen3_thermal_tsc *tsc;
  228. tsc = devm_kzalloc(dev, sizeof(*tsc), GFP_KERNEL);
  229. if (!tsc) {
  230. ret = -ENOMEM;
  231. goto error_unregister;
  232. }
  233. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  234. if (!res)
  235. break;
  236. tsc->base = devm_ioremap_resource(dev, res);
  237. if (IS_ERR(tsc->base)) {
  238. ret = PTR_ERR(tsc->base);
  239. goto error_unregister;
  240. }
  241. priv->tscs[i] = tsc;
  242. mutex_init(&tsc->lock);
  243. match_data->thermal_init(tsc);
  244. rcar_gen3_thermal_calc_coefs(&tsc->coef, ptat, thcode[i]);
  245. zone = devm_thermal_zone_of_sensor_register(dev, i, tsc,
  246. &rcar_gen3_tz_of_ops);
  247. if (IS_ERR(zone)) {
  248. dev_err(dev, "Can't register thermal zone\n");
  249. ret = PTR_ERR(zone);
  250. goto error_unregister;
  251. }
  252. tsc->zone = zone;
  253. }
  254. return 0;
  255. error_unregister:
  256. rcar_gen3_thermal_remove(pdev);
  257. return ret;
  258. }
  259. static struct platform_driver rcar_gen3_thermal_driver = {
  260. .driver = {
  261. .name = "rcar_gen3_thermal",
  262. .of_match_table = rcar_gen3_thermal_dt_ids,
  263. },
  264. .probe = rcar_gen3_thermal_probe,
  265. .remove = rcar_gen3_thermal_remove,
  266. };
  267. module_platform_driver(rcar_gen3_thermal_driver);
  268. MODULE_LICENSE("GPL v2");
  269. MODULE_DESCRIPTION("R-Car Gen3 THS thermal sensor driver");
  270. MODULE_AUTHOR("Wolfram Sang <wsa+renesas@sang-engineering.com>");