rcar_gen3_thermal.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * R-Car Gen3 THS thermal sensor driver
  4. * Based on rcar_thermal.c and work from Hien Dang and Khiem Nguyen.
  5. *
  6. * Copyright (C) 2016 Renesas Electronics Corporation.
  7. * Copyright (C) 2016 Sang Engineering
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/err.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/of_device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/sys_soc.h>
  18. #include <linux/thermal.h>
  19. #include "thermal_core.h"
  20. /* Register offsets */
  21. #define REG_GEN3_IRQSTR 0x04
  22. #define REG_GEN3_IRQMSK 0x08
  23. #define REG_GEN3_IRQCTL 0x0C
  24. #define REG_GEN3_IRQEN 0x10
  25. #define REG_GEN3_IRQTEMP1 0x14
  26. #define REG_GEN3_IRQTEMP2 0x18
  27. #define REG_GEN3_IRQTEMP3 0x1C
  28. #define REG_GEN3_CTSR 0x20
  29. #define REG_GEN3_THCTR 0x20
  30. #define REG_GEN3_TEMP 0x28
  31. #define REG_GEN3_THCODE1 0x50
  32. #define REG_GEN3_THCODE2 0x54
  33. #define REG_GEN3_THCODE3 0x58
  34. /* IRQ{STR,MSK,EN} bits */
  35. #define IRQ_TEMP1 BIT(0)
  36. #define IRQ_TEMP2 BIT(1)
  37. #define IRQ_TEMP3 BIT(2)
  38. #define IRQ_TEMPD1 BIT(3)
  39. #define IRQ_TEMPD2 BIT(4)
  40. #define IRQ_TEMPD3 BIT(5)
  41. /* CTSR bits */
  42. #define CTSR_PONM BIT(8)
  43. #define CTSR_AOUT BIT(7)
  44. #define CTSR_THBGR BIT(5)
  45. #define CTSR_VMEN BIT(4)
  46. #define CTSR_VMST BIT(1)
  47. #define CTSR_THSST BIT(0)
  48. /* THCTR bits */
  49. #define THCTR_PONM BIT(6)
  50. #define THCTR_THSST BIT(0)
  51. #define CTEMP_MASK 0xFFF
  52. #define MCELSIUS(temp) ((temp) * 1000)
  53. #define GEN3_FUSE_MASK 0xFFF
  54. #define TSC_MAX_NUM 3
  55. /* Structure for thermal temperature calculation */
  56. struct equation_coefs {
  57. int a1;
  58. int b1;
  59. int a2;
  60. int b2;
  61. };
  62. struct rcar_gen3_thermal_tsc {
  63. void __iomem *base;
  64. struct thermal_zone_device *zone;
  65. struct equation_coefs coef;
  66. int low;
  67. int high;
  68. };
  69. struct rcar_gen3_thermal_priv {
  70. struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
  71. unsigned int num_tscs;
  72. void (*thermal_init)(struct rcar_gen3_thermal_tsc *tsc);
  73. };
  74. static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
  75. u32 reg)
  76. {
  77. return ioread32(tsc->base + reg);
  78. }
  79. static inline void rcar_gen3_thermal_write(struct rcar_gen3_thermal_tsc *tsc,
  80. u32 reg, u32 data)
  81. {
  82. iowrite32(data, tsc->base + reg);
  83. }
  84. /*
  85. * Linear approximation for temperature
  86. *
  87. * [reg] = [temp] * a + b => [temp] = ([reg] - b) / a
  88. *
  89. * The constants a and b are calculated using two triplets of int values PTAT
  90. * and THCODE. PTAT and THCODE can either be read from hardware or use hard
  91. * coded values from driver. The formula to calculate a and b are taken from
  92. * BSP and sparsely documented and understood.
  93. *
  94. * Examining the linear formula and the formula used to calculate constants a
  95. * and b while knowing that the span for PTAT and THCODE values are between
  96. * 0x000 and 0xfff the largest integer possible is 0xfff * 0xfff == 0xffe001.
  97. * Integer also needs to be signed so that leaves 7 bits for binary
  98. * fixed point scaling.
  99. */
  100. #define FIXPT_SHIFT 7
  101. #define FIXPT_INT(_x) ((_x) << FIXPT_SHIFT)
  102. #define INT_FIXPT(_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 116
  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]) * 157)
  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. reg = rcar_gen3_thermal_read(tsc, REG_GEN3_TEMP) & CTEMP_MASK;
  142. val1 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b1, tsc->coef.a1);
  143. val2 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b2, tsc->coef.a2);
  144. mcelsius = FIXPT_TO_MCELSIUS((val1 + val2) / 2);
  145. /* Make sure we are inside specifications */
  146. if ((mcelsius < MCELSIUS(-40)) || (mcelsius > MCELSIUS(125)))
  147. return -EIO;
  148. /* Round value to device granularity setting */
  149. *temp = rcar_gen3_thermal_round(mcelsius);
  150. return 0;
  151. }
  152. static int rcar_gen3_thermal_mcelsius_to_temp(struct rcar_gen3_thermal_tsc *tsc,
  153. int mcelsius)
  154. {
  155. int celsius, val1, val2;
  156. celsius = DIV_ROUND_CLOSEST(mcelsius, 1000);
  157. val1 = celsius * tsc->coef.a1 + tsc->coef.b1;
  158. val2 = celsius * tsc->coef.a2 + tsc->coef.b2;
  159. return INT_FIXPT((val1 + val2) / 2);
  160. }
  161. static int rcar_gen3_thermal_set_trips(void *devdata, int low, int high)
  162. {
  163. struct rcar_gen3_thermal_tsc *tsc = devdata;
  164. low = clamp_val(low, -40000, 120000);
  165. high = clamp_val(high, -40000, 120000);
  166. rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP1,
  167. rcar_gen3_thermal_mcelsius_to_temp(tsc, low));
  168. rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP2,
  169. rcar_gen3_thermal_mcelsius_to_temp(tsc, high));
  170. tsc->low = low;
  171. tsc->high = high;
  172. return 0;
  173. }
  174. static const struct thermal_zone_of_device_ops rcar_gen3_tz_of_ops = {
  175. .get_temp = rcar_gen3_thermal_get_temp,
  176. .set_trips = rcar_gen3_thermal_set_trips,
  177. };
  178. static void rcar_thermal_irq_set(struct rcar_gen3_thermal_priv *priv, bool on)
  179. {
  180. unsigned int i;
  181. u32 val = on ? IRQ_TEMPD1 | IRQ_TEMP2 : 0;
  182. for (i = 0; i < priv->num_tscs; i++)
  183. rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, val);
  184. }
  185. static irqreturn_t rcar_gen3_thermal_irq(int irq, void *data)
  186. {
  187. struct rcar_gen3_thermal_priv *priv = data;
  188. u32 status;
  189. int i;
  190. for (i = 0; i < priv->num_tscs; i++) {
  191. status = rcar_gen3_thermal_read(priv->tscs[i], REG_GEN3_IRQSTR);
  192. rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQSTR, 0);
  193. if (status)
  194. thermal_zone_device_update(priv->tscs[i]->zone,
  195. THERMAL_EVENT_UNSPECIFIED);
  196. }
  197. return IRQ_HANDLED;
  198. }
  199. static const struct soc_device_attribute r8a7795es1[] = {
  200. { .soc_id = "r8a7795", .revision = "ES1.*" },
  201. { /* sentinel */ }
  202. };
  203. static void rcar_gen3_thermal_init_r8a7795es1(struct rcar_gen3_thermal_tsc *tsc)
  204. {
  205. rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_THBGR);
  206. rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, 0x0);
  207. usleep_range(1000, 2000);
  208. rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_PONM);
  209. rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F);
  210. rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0);
  211. rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN, IRQ_TEMPD1 | IRQ_TEMP2);
  212. rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
  213. CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN);
  214. usleep_range(100, 200);
  215. rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
  216. CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN |
  217. CTSR_VMST | CTSR_THSST);
  218. usleep_range(1000, 2000);
  219. }
  220. static void rcar_gen3_thermal_init(struct rcar_gen3_thermal_tsc *tsc)
  221. {
  222. u32 reg_val;
  223. reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
  224. reg_val &= ~THCTR_PONM;
  225. rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
  226. usleep_range(1000, 2000);
  227. rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F);
  228. rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0);
  229. rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN, IRQ_TEMPD1 | IRQ_TEMP2);
  230. reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
  231. reg_val |= THCTR_THSST;
  232. rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
  233. usleep_range(1000, 2000);
  234. }
  235. static const struct of_device_id rcar_gen3_thermal_dt_ids[] = {
  236. { .compatible = "renesas,r8a7795-thermal", },
  237. { .compatible = "renesas,r8a7796-thermal", },
  238. { .compatible = "renesas,r8a77965-thermal", },
  239. {},
  240. };
  241. MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
  242. static int rcar_gen3_thermal_remove(struct platform_device *pdev)
  243. {
  244. struct device *dev = &pdev->dev;
  245. struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
  246. rcar_thermal_irq_set(priv, false);
  247. pm_runtime_put(dev);
  248. pm_runtime_disable(dev);
  249. return 0;
  250. }
  251. static int rcar_gen3_thermal_probe(struct platform_device *pdev)
  252. {
  253. struct rcar_gen3_thermal_priv *priv;
  254. struct device *dev = &pdev->dev;
  255. struct resource *res;
  256. struct thermal_zone_device *zone;
  257. int ret, irq, i;
  258. char *irqname;
  259. /* default values if FUSEs are missing */
  260. /* TODO: Read values from hardware on supported platforms */
  261. int ptat[3] = { 2631, 1509, 435 };
  262. int thcode[TSC_MAX_NUM][3] = {
  263. { 3397, 2800, 2221 },
  264. { 3393, 2795, 2216 },
  265. { 3389, 2805, 2237 },
  266. };
  267. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  268. if (!priv)
  269. return -ENOMEM;
  270. priv->thermal_init = rcar_gen3_thermal_init;
  271. if (soc_device_match(r8a7795es1))
  272. priv->thermal_init = rcar_gen3_thermal_init_r8a7795es1;
  273. platform_set_drvdata(pdev, priv);
  274. /*
  275. * Request 2 (of the 3 possible) IRQs, the driver only needs to
  276. * to trigger on the low and high trip points of the current
  277. * temp window at this point.
  278. */
  279. for (i = 0; i < 2; i++) {
  280. irq = platform_get_irq(pdev, i);
  281. if (irq < 0)
  282. return irq;
  283. irqname = devm_kasprintf(dev, GFP_KERNEL, "%s:ch%d",
  284. dev_name(dev), i);
  285. if (!irqname)
  286. return -ENOMEM;
  287. ret = devm_request_threaded_irq(dev, irq, NULL,
  288. rcar_gen3_thermal_irq,
  289. IRQF_ONESHOT, irqname, priv);
  290. if (ret)
  291. return ret;
  292. }
  293. pm_runtime_enable(dev);
  294. pm_runtime_get_sync(dev);
  295. for (i = 0; i < TSC_MAX_NUM; i++) {
  296. struct rcar_gen3_thermal_tsc *tsc;
  297. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  298. if (!res)
  299. break;
  300. tsc = devm_kzalloc(dev, sizeof(*tsc), GFP_KERNEL);
  301. if (!tsc) {
  302. ret = -ENOMEM;
  303. goto error_unregister;
  304. }
  305. tsc->base = devm_ioremap_resource(dev, res);
  306. if (IS_ERR(tsc->base)) {
  307. ret = PTR_ERR(tsc->base);
  308. goto error_unregister;
  309. }
  310. priv->tscs[i] = tsc;
  311. priv->thermal_init(tsc);
  312. rcar_gen3_thermal_calc_coefs(&tsc->coef, ptat, thcode[i]);
  313. zone = devm_thermal_zone_of_sensor_register(dev, i, tsc,
  314. &rcar_gen3_tz_of_ops);
  315. if (IS_ERR(zone)) {
  316. dev_err(dev, "Can't register thermal zone\n");
  317. ret = PTR_ERR(zone);
  318. goto error_unregister;
  319. }
  320. tsc->zone = zone;
  321. ret = of_thermal_get_ntrips(tsc->zone);
  322. if (ret < 0)
  323. goto error_unregister;
  324. dev_info(dev, "TSC%d: Loaded %d trip points\n", i, ret);
  325. }
  326. priv->num_tscs = i;
  327. if (!priv->num_tscs) {
  328. ret = -ENODEV;
  329. goto error_unregister;
  330. }
  331. rcar_thermal_irq_set(priv, true);
  332. return 0;
  333. error_unregister:
  334. rcar_gen3_thermal_remove(pdev);
  335. return ret;
  336. }
  337. static int __maybe_unused rcar_gen3_thermal_suspend(struct device *dev)
  338. {
  339. struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
  340. rcar_thermal_irq_set(priv, false);
  341. return 0;
  342. }
  343. static int __maybe_unused rcar_gen3_thermal_resume(struct device *dev)
  344. {
  345. struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
  346. unsigned int i;
  347. for (i = 0; i < priv->num_tscs; i++) {
  348. struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
  349. priv->thermal_init(tsc);
  350. rcar_gen3_thermal_set_trips(tsc, tsc->low, tsc->high);
  351. }
  352. rcar_thermal_irq_set(priv, true);
  353. return 0;
  354. }
  355. static SIMPLE_DEV_PM_OPS(rcar_gen3_thermal_pm_ops, rcar_gen3_thermal_suspend,
  356. rcar_gen3_thermal_resume);
  357. static struct platform_driver rcar_gen3_thermal_driver = {
  358. .driver = {
  359. .name = "rcar_gen3_thermal",
  360. .pm = &rcar_gen3_thermal_pm_ops,
  361. .of_match_table = rcar_gen3_thermal_dt_ids,
  362. },
  363. .probe = rcar_gen3_thermal_probe,
  364. .remove = rcar_gen3_thermal_remove,
  365. };
  366. module_platform_driver(rcar_gen3_thermal_driver);
  367. MODULE_LICENSE("GPL v2");
  368. MODULE_DESCRIPTION("R-Car Gen3 THS thermal sensor driver");
  369. MODULE_AUTHOR("Wolfram Sang <wsa+renesas@sang-engineering.com>");