rcar_gen3_thermal.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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/spinlock.h>
  18. #include <linux/sys_soc.h>
  19. #include <linux/thermal.h>
  20. #include "thermal_core.h"
  21. /* Register offsets */
  22. #define REG_GEN3_IRQSTR 0x04
  23. #define REG_GEN3_IRQMSK 0x08
  24. #define REG_GEN3_IRQCTL 0x0C
  25. #define REG_GEN3_IRQEN 0x10
  26. #define REG_GEN3_IRQTEMP1 0x14
  27. #define REG_GEN3_IRQTEMP2 0x18
  28. #define REG_GEN3_IRQTEMP3 0x1C
  29. #define REG_GEN3_CTSR 0x20
  30. #define REG_GEN3_THCTR 0x20
  31. #define REG_GEN3_TEMP 0x28
  32. #define REG_GEN3_THCODE1 0x50
  33. #define REG_GEN3_THCODE2 0x54
  34. #define REG_GEN3_THCODE3 0x58
  35. /* IRQ{STR,MSK,EN} bits */
  36. #define IRQ_TEMP1 BIT(0)
  37. #define IRQ_TEMP2 BIT(1)
  38. #define IRQ_TEMP3 BIT(2)
  39. #define IRQ_TEMPD1 BIT(3)
  40. #define IRQ_TEMPD2 BIT(4)
  41. #define IRQ_TEMPD3 BIT(5)
  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. int low;
  68. int high;
  69. };
  70. struct rcar_gen3_thermal_priv {
  71. struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
  72. unsigned int num_tscs;
  73. spinlock_t lock; /* Protect interrupts on and off */
  74. void (*thermal_init)(struct rcar_gen3_thermal_tsc *tsc);
  75. };
  76. static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
  77. u32 reg)
  78. {
  79. return ioread32(tsc->base + reg);
  80. }
  81. static inline void rcar_gen3_thermal_write(struct rcar_gen3_thermal_tsc *tsc,
  82. u32 reg, u32 data)
  83. {
  84. iowrite32(data, tsc->base + reg);
  85. }
  86. /*
  87. * Linear approximation for temperature
  88. *
  89. * [reg] = [temp] * a + b => [temp] = ([reg] - b) / a
  90. *
  91. * The constants a and b are calculated using two triplets of int values PTAT
  92. * and THCODE. PTAT and THCODE can either be read from hardware or use hard
  93. * coded values from driver. The formula to calculate a and b are taken from
  94. * BSP and sparsely documented and understood.
  95. *
  96. * Examining the linear formula and the formula used to calculate constants a
  97. * and b while knowing that the span for PTAT and THCODE values are between
  98. * 0x000 and 0xfff the largest integer possible is 0xfff * 0xfff == 0xffe001.
  99. * Integer also needs to be signed so that leaves 7 bits for binary
  100. * fixed point scaling.
  101. */
  102. #define FIXPT_SHIFT 7
  103. #define FIXPT_INT(_x) ((_x) << FIXPT_SHIFT)
  104. #define INT_FIXPT(_x) ((_x) >> FIXPT_SHIFT)
  105. #define FIXPT_DIV(_a, _b) DIV_ROUND_CLOSEST(((_a) << FIXPT_SHIFT), (_b))
  106. #define FIXPT_TO_MCELSIUS(_x) ((_x) * 1000 >> FIXPT_SHIFT)
  107. #define RCAR3_THERMAL_GRAN 500 /* mili Celsius */
  108. /* no idea where these constants come from */
  109. #define TJ_1 116
  110. #define TJ_3 -41
  111. static void rcar_gen3_thermal_calc_coefs(struct equation_coefs *coef,
  112. int *ptat, int *thcode)
  113. {
  114. int tj_2;
  115. /* TODO: Find documentation and document constant calculation formula */
  116. /*
  117. * Division is not scaled in BSP and if scaled it might overflow
  118. * the dividend (4095 * 4095 << 14 > INT_MAX) so keep it unscaled
  119. */
  120. tj_2 = (FIXPT_INT((ptat[1] - ptat[2]) * 157)
  121. / (ptat[0] - ptat[2])) - FIXPT_INT(41);
  122. coef->a1 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[2]),
  123. tj_2 - FIXPT_INT(TJ_3));
  124. coef->b1 = FIXPT_INT(thcode[2]) - coef->a1 * TJ_3;
  125. coef->a2 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[0]),
  126. tj_2 - FIXPT_INT(TJ_1));
  127. coef->b2 = FIXPT_INT(thcode[0]) - coef->a2 * TJ_1;
  128. }
  129. static int rcar_gen3_thermal_round(int temp)
  130. {
  131. int result, round_offs;
  132. round_offs = temp >= 0 ? RCAR3_THERMAL_GRAN / 2 :
  133. -RCAR3_THERMAL_GRAN / 2;
  134. result = (temp + round_offs) / RCAR3_THERMAL_GRAN;
  135. return result * RCAR3_THERMAL_GRAN;
  136. }
  137. static int rcar_gen3_thermal_get_temp(void *devdata, int *temp)
  138. {
  139. struct rcar_gen3_thermal_tsc *tsc = devdata;
  140. int mcelsius, val1, val2;
  141. u32 reg;
  142. /* Read register and convert to mili Celsius */
  143. reg = rcar_gen3_thermal_read(tsc, REG_GEN3_TEMP) & CTEMP_MASK;
  144. val1 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b1, tsc->coef.a1);
  145. val2 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b2, tsc->coef.a2);
  146. mcelsius = FIXPT_TO_MCELSIUS((val1 + val2) / 2);
  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 int rcar_gen3_thermal_mcelsius_to_temp(struct rcar_gen3_thermal_tsc *tsc,
  155. int mcelsius)
  156. {
  157. int celsius, val1, val2;
  158. celsius = DIV_ROUND_CLOSEST(mcelsius, 1000);
  159. val1 = celsius * tsc->coef.a1 + tsc->coef.b1;
  160. val2 = celsius * tsc->coef.a2 + tsc->coef.b2;
  161. return INT_FIXPT((val1 + val2) / 2);
  162. }
  163. static int rcar_gen3_thermal_set_trips(void *devdata, int low, int high)
  164. {
  165. struct rcar_gen3_thermal_tsc *tsc = devdata;
  166. low = clamp_val(low, -40000, 120000);
  167. high = clamp_val(high, -40000, 120000);
  168. rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP1,
  169. rcar_gen3_thermal_mcelsius_to_temp(tsc, low));
  170. rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP2,
  171. rcar_gen3_thermal_mcelsius_to_temp(tsc, high));
  172. tsc->low = low;
  173. tsc->high = high;
  174. return 0;
  175. }
  176. static const struct thermal_zone_of_device_ops rcar_gen3_tz_of_ops = {
  177. .get_temp = rcar_gen3_thermal_get_temp,
  178. .set_trips = rcar_gen3_thermal_set_trips,
  179. };
  180. static void rcar_thermal_irq_set(struct rcar_gen3_thermal_priv *priv, bool on)
  181. {
  182. unsigned int i;
  183. u32 val = on ? IRQ_TEMPD1 | IRQ_TEMP2 : 0;
  184. for (i = 0; i < priv->num_tscs; i++)
  185. rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, val);
  186. }
  187. static irqreturn_t rcar_gen3_thermal_irq(int irq, void *data)
  188. {
  189. struct rcar_gen3_thermal_priv *priv = data;
  190. u32 status;
  191. int i, ret = IRQ_HANDLED;
  192. spin_lock(&priv->lock);
  193. for (i = 0; i < priv->num_tscs; i++) {
  194. status = rcar_gen3_thermal_read(priv->tscs[i], REG_GEN3_IRQSTR);
  195. rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQSTR, 0);
  196. if (status)
  197. ret = IRQ_WAKE_THREAD;
  198. }
  199. if (ret == IRQ_WAKE_THREAD)
  200. rcar_thermal_irq_set(priv, false);
  201. spin_unlock(&priv->lock);
  202. return ret;
  203. }
  204. static irqreturn_t rcar_gen3_thermal_irq_thread(int irq, void *data)
  205. {
  206. struct rcar_gen3_thermal_priv *priv = data;
  207. unsigned long flags;
  208. int i;
  209. for (i = 0; i < priv->num_tscs; i++)
  210. thermal_zone_device_update(priv->tscs[i]->zone,
  211. THERMAL_EVENT_UNSPECIFIED);
  212. spin_lock_irqsave(&priv->lock, flags);
  213. rcar_thermal_irq_set(priv, true);
  214. spin_unlock_irqrestore(&priv->lock, flags);
  215. return IRQ_HANDLED;
  216. }
  217. static const struct soc_device_attribute r8a7795es1[] = {
  218. { .soc_id = "r8a7795", .revision = "ES1.*" },
  219. { /* sentinel */ }
  220. };
  221. static void rcar_gen3_thermal_init_r8a7795es1(struct rcar_gen3_thermal_tsc *tsc)
  222. {
  223. rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_THBGR);
  224. rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, 0x0);
  225. usleep_range(1000, 2000);
  226. rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_PONM);
  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. rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
  231. CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN);
  232. usleep_range(100, 200);
  233. rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
  234. CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN |
  235. CTSR_VMST | CTSR_THSST);
  236. usleep_range(1000, 2000);
  237. }
  238. static void rcar_gen3_thermal_init(struct rcar_gen3_thermal_tsc *tsc)
  239. {
  240. u32 reg_val;
  241. reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
  242. reg_val &= ~THCTR_PONM;
  243. rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
  244. usleep_range(1000, 2000);
  245. rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F);
  246. rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0);
  247. rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN, IRQ_TEMPD1 | IRQ_TEMP2);
  248. reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
  249. reg_val |= THCTR_THSST;
  250. rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
  251. usleep_range(1000, 2000);
  252. }
  253. static const struct of_device_id rcar_gen3_thermal_dt_ids[] = {
  254. { .compatible = "renesas,r8a774a1-thermal", },
  255. { .compatible = "renesas,r8a7795-thermal", },
  256. { .compatible = "renesas,r8a7796-thermal", },
  257. { .compatible = "renesas,r8a77965-thermal", },
  258. { .compatible = "renesas,r8a77980-thermal", },
  259. {},
  260. };
  261. MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
  262. static int rcar_gen3_thermal_remove(struct platform_device *pdev)
  263. {
  264. struct device *dev = &pdev->dev;
  265. pm_runtime_put(dev);
  266. pm_runtime_disable(dev);
  267. return 0;
  268. }
  269. static int rcar_gen3_thermal_probe(struct platform_device *pdev)
  270. {
  271. struct rcar_gen3_thermal_priv *priv;
  272. struct device *dev = &pdev->dev;
  273. struct resource *res;
  274. struct thermal_zone_device *zone;
  275. int ret, irq, i;
  276. char *irqname;
  277. /* default values if FUSEs are missing */
  278. /* TODO: Read values from hardware on supported platforms */
  279. int ptat[3] = { 2631, 1509, 435 };
  280. int thcode[TSC_MAX_NUM][3] = {
  281. { 3397, 2800, 2221 },
  282. { 3393, 2795, 2216 },
  283. { 3389, 2805, 2237 },
  284. };
  285. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  286. if (!priv)
  287. return -ENOMEM;
  288. priv->thermal_init = rcar_gen3_thermal_init;
  289. if (soc_device_match(r8a7795es1))
  290. priv->thermal_init = rcar_gen3_thermal_init_r8a7795es1;
  291. spin_lock_init(&priv->lock);
  292. platform_set_drvdata(pdev, priv);
  293. /*
  294. * Request 2 (of the 3 possible) IRQs, the driver only needs to
  295. * to trigger on the low and high trip points of the current
  296. * temp window at this point.
  297. */
  298. for (i = 0; i < 2; i++) {
  299. irq = platform_get_irq(pdev, i);
  300. if (irq < 0)
  301. return irq;
  302. irqname = devm_kasprintf(dev, GFP_KERNEL, "%s:ch%d",
  303. dev_name(dev), i);
  304. if (!irqname)
  305. return -ENOMEM;
  306. ret = devm_request_threaded_irq(dev, irq, rcar_gen3_thermal_irq,
  307. rcar_gen3_thermal_irq_thread,
  308. IRQF_SHARED, irqname, priv);
  309. if (ret)
  310. return ret;
  311. }
  312. pm_runtime_enable(dev);
  313. pm_runtime_get_sync(dev);
  314. for (i = 0; i < TSC_MAX_NUM; i++) {
  315. struct rcar_gen3_thermal_tsc *tsc;
  316. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  317. if (!res)
  318. break;
  319. tsc = devm_kzalloc(dev, sizeof(*tsc), GFP_KERNEL);
  320. if (!tsc) {
  321. ret = -ENOMEM;
  322. goto error_unregister;
  323. }
  324. tsc->base = devm_ioremap_resource(dev, res);
  325. if (IS_ERR(tsc->base)) {
  326. ret = PTR_ERR(tsc->base);
  327. goto error_unregister;
  328. }
  329. priv->tscs[i] = tsc;
  330. priv->thermal_init(tsc);
  331. rcar_gen3_thermal_calc_coefs(&tsc->coef, ptat, thcode[i]);
  332. zone = devm_thermal_zone_of_sensor_register(dev, i, tsc,
  333. &rcar_gen3_tz_of_ops);
  334. if (IS_ERR(zone)) {
  335. dev_err(dev, "Can't register thermal zone\n");
  336. ret = PTR_ERR(zone);
  337. goto error_unregister;
  338. }
  339. tsc->zone = zone;
  340. ret = of_thermal_get_ntrips(tsc->zone);
  341. if (ret < 0)
  342. goto error_unregister;
  343. dev_info(dev, "TSC%d: Loaded %d trip points\n", i, ret);
  344. }
  345. priv->num_tscs = i;
  346. if (!priv->num_tscs) {
  347. ret = -ENODEV;
  348. goto error_unregister;
  349. }
  350. rcar_thermal_irq_set(priv, true);
  351. return 0;
  352. error_unregister:
  353. rcar_gen3_thermal_remove(pdev);
  354. return ret;
  355. }
  356. static int __maybe_unused rcar_gen3_thermal_suspend(struct device *dev)
  357. {
  358. struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
  359. rcar_thermal_irq_set(priv, false);
  360. return 0;
  361. }
  362. static int __maybe_unused rcar_gen3_thermal_resume(struct device *dev)
  363. {
  364. struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
  365. unsigned int i;
  366. for (i = 0; i < priv->num_tscs; i++) {
  367. struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
  368. priv->thermal_init(tsc);
  369. rcar_gen3_thermal_set_trips(tsc, tsc->low, tsc->high);
  370. }
  371. rcar_thermal_irq_set(priv, true);
  372. return 0;
  373. }
  374. static SIMPLE_DEV_PM_OPS(rcar_gen3_thermal_pm_ops, rcar_gen3_thermal_suspend,
  375. rcar_gen3_thermal_resume);
  376. static struct platform_driver rcar_gen3_thermal_driver = {
  377. .driver = {
  378. .name = "rcar_gen3_thermal",
  379. .pm = &rcar_gen3_thermal_pm_ops,
  380. .of_match_table = rcar_gen3_thermal_dt_ids,
  381. },
  382. .probe = rcar_gen3_thermal_probe,
  383. .remove = rcar_gen3_thermal_remove,
  384. };
  385. module_platform_driver(rcar_gen3_thermal_driver);
  386. MODULE_LICENSE("GPL v2");
  387. MODULE_DESCRIPTION("R-Car Gen3 THS thermal sensor driver");
  388. MODULE_AUTHOR("Wolfram Sang <wsa+renesas@sang-engineering.com>");