2 * R-Car Gen3 THS thermal sensor driver
3 * Based on rcar_thermal.c and work from Hien Dang and Khiem Nguyen.
5 * Copyright (C) 2016 Renesas Electronics Corporation.
6 * Copyright (C) 2016 Sang Engineering
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.
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.
18 #include <linux/delay.h>
19 #include <linux/err.h>
20 #include <linux/interrupt.h>
22 #include <linux/module.h>
23 #include <linux/of_device.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/spinlock.h>
27 #include <linux/sys_soc.h>
28 #include <linux/thermal.h>
30 #include "thermal_core.h"
32 /* Register offsets */
33 #define REG_GEN3_IRQSTR 0x04
34 #define REG_GEN3_IRQMSK 0x08
35 #define REG_GEN3_IRQCTL 0x0C
36 #define REG_GEN3_IRQEN 0x10
37 #define REG_GEN3_IRQTEMP1 0x14
38 #define REG_GEN3_IRQTEMP2 0x18
39 #define REG_GEN3_IRQTEMP3 0x1C
40 #define REG_GEN3_CTSR 0x20
41 #define REG_GEN3_THCTR 0x20
42 #define REG_GEN3_TEMP 0x28
43 #define REG_GEN3_THCODE1 0x50
44 #define REG_GEN3_THCODE2 0x54
45 #define REG_GEN3_THCODE3 0x58
47 /* IRQ{STR,MSK,EN} bits */
48 #define IRQ_TEMP1 BIT(0)
49 #define IRQ_TEMP2 BIT(1)
50 #define IRQ_TEMP3 BIT(2)
51 #define IRQ_TEMPD1 BIT(3)
52 #define IRQ_TEMPD2 BIT(4)
53 #define IRQ_TEMPD3 BIT(5)
56 #define CTSR_PONM BIT(8)
57 #define CTSR_AOUT BIT(7)
58 #define CTSR_THBGR BIT(5)
59 #define CTSR_VMEN BIT(4)
60 #define CTSR_VMST BIT(1)
61 #define CTSR_THSST BIT(0)
64 #define THCTR_PONM BIT(6)
65 #define THCTR_THSST BIT(0)
67 #define CTEMP_MASK 0xFFF
69 #define MCELSIUS(temp) ((temp) * 1000)
70 #define GEN3_FUSE_MASK 0xFFF
74 /* Structure for thermal temperature calculation */
75 struct equation_coefs {
82 struct rcar_gen3_thermal_tsc {
84 struct thermal_zone_device *zone;
85 struct equation_coefs coef;
90 struct rcar_gen3_thermal_priv {
91 struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
92 unsigned int num_tscs;
93 spinlock_t lock; /* Protect interrupts on and off */
94 void (*thermal_init)(struct rcar_gen3_thermal_tsc *tsc);
97 static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
100 return ioread32(tsc->base + reg);
103 static inline void rcar_gen3_thermal_write(struct rcar_gen3_thermal_tsc *tsc,
106 iowrite32(data, tsc->base + reg);
110 * Linear approximation for temperature
112 * [reg] = [temp] * a + b => [temp] = ([reg] - b) / a
114 * The constants a and b are calculated using two triplets of int values PTAT
115 * and THCODE. PTAT and THCODE can either be read from hardware or use hard
116 * coded values from driver. The formula to calculate a and b are taken from
117 * BSP and sparsely documented and understood.
119 * Examining the linear formula and the formula used to calculate constants a
120 * and b while knowing that the span for PTAT and THCODE values are between
121 * 0x000 and 0xfff the largest integer possible is 0xfff * 0xfff == 0xffe001.
122 * Integer also needs to be signed so that leaves 7 bits for binary
123 * fixed point scaling.
126 #define FIXPT_SHIFT 7
127 #define FIXPT_INT(_x) ((_x) << FIXPT_SHIFT)
128 #define INT_FIXPT(_x) ((_x) >> FIXPT_SHIFT)
129 #define FIXPT_DIV(_a, _b) DIV_ROUND_CLOSEST(((_a) << FIXPT_SHIFT), (_b))
130 #define FIXPT_TO_MCELSIUS(_x) ((_x) * 1000 >> FIXPT_SHIFT)
132 #define RCAR3_THERMAL_GRAN 500 /* mili Celsius */
134 /* no idea where these constants come from */
138 static void rcar_gen3_thermal_calc_coefs(struct equation_coefs *coef,
139 int *ptat, int *thcode)
143 /* TODO: Find documentation and document constant calculation formula */
146 * Division is not scaled in BSP and if scaled it might overflow
147 * the dividend (4095 * 4095 << 14 > INT_MAX) so keep it unscaled
149 tj_2 = (FIXPT_INT((ptat[1] - ptat[2]) * 157)
150 / (ptat[0] - ptat[2])) - FIXPT_INT(41);
152 coef->a1 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[2]),
153 tj_2 - FIXPT_INT(TJ_3));
154 coef->b1 = FIXPT_INT(thcode[2]) - coef->a1 * TJ_3;
156 coef->a2 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[0]),
157 tj_2 - FIXPT_INT(TJ_1));
158 coef->b2 = FIXPT_INT(thcode[0]) - coef->a2 * TJ_1;
161 static int rcar_gen3_thermal_round(int temp)
163 int result, round_offs;
165 round_offs = temp >= 0 ? RCAR3_THERMAL_GRAN / 2 :
166 -RCAR3_THERMAL_GRAN / 2;
167 result = (temp + round_offs) / RCAR3_THERMAL_GRAN;
168 return result * RCAR3_THERMAL_GRAN;
171 static int rcar_gen3_thermal_get_temp(void *devdata, int *temp)
173 struct rcar_gen3_thermal_tsc *tsc = devdata;
174 int mcelsius, val1, val2;
177 /* Read register and convert to mili Celsius */
178 reg = rcar_gen3_thermal_read(tsc, REG_GEN3_TEMP) & CTEMP_MASK;
180 val1 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b1, tsc->coef.a1);
181 val2 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b2, tsc->coef.a2);
182 mcelsius = FIXPT_TO_MCELSIUS((val1 + val2) / 2);
184 /* Make sure we are inside specifications */
185 if ((mcelsius < MCELSIUS(-40)) || (mcelsius > MCELSIUS(125)))
188 /* Round value to device granularity setting */
189 *temp = rcar_gen3_thermal_round(mcelsius);
194 static int rcar_gen3_thermal_mcelsius_to_temp(struct rcar_gen3_thermal_tsc *tsc,
197 int celsius, val1, val2;
199 celsius = DIV_ROUND_CLOSEST(mcelsius, 1000);
200 val1 = celsius * tsc->coef.a1 + tsc->coef.b1;
201 val2 = celsius * tsc->coef.a2 + tsc->coef.b2;
203 return INT_FIXPT((val1 + val2) / 2);
206 static int rcar_gen3_thermal_set_trips(void *devdata, int low, int high)
208 struct rcar_gen3_thermal_tsc *tsc = devdata;
210 low = clamp_val(low, -40000, 120000);
211 high = clamp_val(high, -40000, 120000);
213 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP1,
214 rcar_gen3_thermal_mcelsius_to_temp(tsc, low));
216 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP2,
217 rcar_gen3_thermal_mcelsius_to_temp(tsc, high));
225 static const struct thermal_zone_of_device_ops rcar_gen3_tz_of_ops = {
226 .get_temp = rcar_gen3_thermal_get_temp,
227 .set_trips = rcar_gen3_thermal_set_trips,
230 static void rcar_thermal_irq_set(struct rcar_gen3_thermal_priv *priv, bool on)
233 u32 val = on ? IRQ_TEMPD1 | IRQ_TEMP2 : 0;
235 for (i = 0; i < priv->num_tscs; i++)
236 rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, val);
239 static irqreturn_t rcar_gen3_thermal_irq(int irq, void *data)
241 struct rcar_gen3_thermal_priv *priv = data;
243 int i, ret = IRQ_HANDLED;
245 spin_lock(&priv->lock);
246 for (i = 0; i < priv->num_tscs; i++) {
247 status = rcar_gen3_thermal_read(priv->tscs[i], REG_GEN3_IRQSTR);
248 rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQSTR, 0);
250 ret = IRQ_WAKE_THREAD;
253 if (ret == IRQ_WAKE_THREAD)
254 rcar_thermal_irq_set(priv, false);
256 spin_unlock(&priv->lock);
261 static irqreturn_t rcar_gen3_thermal_irq_thread(int irq, void *data)
263 struct rcar_gen3_thermal_priv *priv = data;
267 for (i = 0; i < priv->num_tscs; i++)
268 thermal_zone_device_update(priv->tscs[i]->zone,
269 THERMAL_EVENT_UNSPECIFIED);
271 spin_lock_irqsave(&priv->lock, flags);
272 rcar_thermal_irq_set(priv, true);
273 spin_unlock_irqrestore(&priv->lock, flags);
278 static const struct soc_device_attribute r8a7795es1[] = {
279 { .soc_id = "r8a7795", .revision = "ES1.*" },
283 static void rcar_gen3_thermal_init_r8a7795es1(struct rcar_gen3_thermal_tsc *tsc)
285 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_THBGR);
286 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, 0x0);
288 usleep_range(1000, 2000);
290 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_PONM);
292 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F);
293 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0);
294 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN, IRQ_TEMPD1 | IRQ_TEMP2);
296 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
297 CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN);
299 usleep_range(100, 200);
301 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
302 CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN |
303 CTSR_VMST | CTSR_THSST);
305 usleep_range(1000, 2000);
308 static void rcar_gen3_thermal_init(struct rcar_gen3_thermal_tsc *tsc)
312 reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
313 reg_val &= ~THCTR_PONM;
314 rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
316 usleep_range(1000, 2000);
318 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F);
319 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0);
320 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN, IRQ_TEMPD1 | IRQ_TEMP2);
322 reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
323 reg_val |= THCTR_THSST;
324 rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
326 usleep_range(1000, 2000);
329 static const struct of_device_id rcar_gen3_thermal_dt_ids[] = {
330 { .compatible = "renesas,r8a7795-thermal", },
331 { .compatible = "renesas,r8a7796-thermal", },
332 { .compatible = "renesas,r8a77965-thermal", },
335 MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
337 static int rcar_gen3_thermal_remove(struct platform_device *pdev)
339 struct device *dev = &pdev->dev;
342 pm_runtime_disable(dev);
347 static int rcar_gen3_thermal_probe(struct platform_device *pdev)
349 struct rcar_gen3_thermal_priv *priv;
350 struct device *dev = &pdev->dev;
351 struct resource *res;
352 struct thermal_zone_device *zone;
356 /* default values if FUSEs are missing */
357 /* TODO: Read values from hardware on supported platforms */
358 int ptat[3] = { 2631, 1509, 435 };
359 int thcode[TSC_MAX_NUM][3] = {
360 { 3397, 2800, 2221 },
361 { 3393, 2795, 2216 },
362 { 3389, 2805, 2237 },
365 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
369 priv->thermal_init = rcar_gen3_thermal_init;
370 if (soc_device_match(r8a7795es1))
371 priv->thermal_init = rcar_gen3_thermal_init_r8a7795es1;
373 spin_lock_init(&priv->lock);
375 platform_set_drvdata(pdev, priv);
378 * Request 2 (of the 3 possible) IRQs, the driver only needs to
379 * to trigger on the low and high trip points of the current
380 * temp window at this point.
382 for (i = 0; i < 2; i++) {
383 irq = platform_get_irq(pdev, i);
387 irqname = devm_kasprintf(dev, GFP_KERNEL, "%s:ch%d",
392 ret = devm_request_threaded_irq(dev, irq, rcar_gen3_thermal_irq,
393 rcar_gen3_thermal_irq_thread,
394 IRQF_SHARED, irqname, priv);
399 pm_runtime_enable(dev);
400 pm_runtime_get_sync(dev);
402 for (i = 0; i < TSC_MAX_NUM; i++) {
403 struct rcar_gen3_thermal_tsc *tsc;
405 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
409 tsc = devm_kzalloc(dev, sizeof(*tsc), GFP_KERNEL);
412 goto error_unregister;
415 tsc->base = devm_ioremap_resource(dev, res);
416 if (IS_ERR(tsc->base)) {
417 ret = PTR_ERR(tsc->base);
418 goto error_unregister;
423 priv->thermal_init(tsc);
424 rcar_gen3_thermal_calc_coefs(&tsc->coef, ptat, thcode[i]);
426 zone = devm_thermal_zone_of_sensor_register(dev, i, tsc,
427 &rcar_gen3_tz_of_ops);
429 dev_err(dev, "Can't register thermal zone\n");
431 goto error_unregister;
435 ret = of_thermal_get_ntrips(tsc->zone);
437 goto error_unregister;
439 dev_info(dev, "TSC%d: Loaded %d trip points\n", i, ret);
444 if (!priv->num_tscs) {
446 goto error_unregister;
449 rcar_thermal_irq_set(priv, true);
454 rcar_gen3_thermal_remove(pdev);
459 static int __maybe_unused rcar_gen3_thermal_suspend(struct device *dev)
461 struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
463 rcar_thermal_irq_set(priv, false);
468 static int __maybe_unused rcar_gen3_thermal_resume(struct device *dev)
470 struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
473 for (i = 0; i < priv->num_tscs; i++) {
474 struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
476 priv->thermal_init(tsc);
477 rcar_gen3_thermal_set_trips(tsc, tsc->low, tsc->high);
480 rcar_thermal_irq_set(priv, true);
485 static SIMPLE_DEV_PM_OPS(rcar_gen3_thermal_pm_ops, rcar_gen3_thermal_suspend,
486 rcar_gen3_thermal_resume);
488 static struct platform_driver rcar_gen3_thermal_driver = {
490 .name = "rcar_gen3_thermal",
491 .pm = &rcar_gen3_thermal_pm_ops,
492 .of_match_table = rcar_gen3_thermal_dt_ids,
494 .probe = rcar_gen3_thermal_probe,
495 .remove = rcar_gen3_thermal_remove,
497 module_platform_driver(rcar_gen3_thermal_driver);
499 MODULE_LICENSE("GPL v2");
500 MODULE_DESCRIPTION("R-Car Gen3 THS thermal sensor driver");
501 MODULE_AUTHOR("Wolfram Sang <wsa+renesas@sang-engineering.com>");