blob: 7aed5337bdd35b3b3214372e7d5f9a01cf26789c [file] [log] [blame]
Kuninori Morimotod3165222018-08-20 11:42:35 -07001// SPDX-License-Identifier: GPL-2.0
Wolfram Sang564e73d2016-12-22 11:38:21 +01002/*
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
Wolfram Sang564e73d2016-12-22 11:38:21 +01008 */
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>
Wolfram Sang564e73d2016-12-22 11:38:21 +010014#include <linux/of_device.h>
15#include <linux/platform_device.h>
16#include <linux/pm_runtime.h>
Niklas Söderlund7d4b2692017-03-29 20:43:54 +020017#include <linux/spinlock.h>
Niklas Söderlundd668c802017-10-17 13:36:13 +020018#include <linux/sys_soc.h>
Wolfram Sang564e73d2016-12-22 11:38:21 +010019#include <linux/thermal.h>
20
Niklas Söderlund7d4b2692017-03-29 20:43:54 +020021#include "thermal_core.h"
22
Wolfram Sang564e73d2016-12-22 11:38:21 +010023/* Register offsets */
24#define REG_GEN3_IRQSTR 0x04
25#define REG_GEN3_IRQMSK 0x08
26#define REG_GEN3_IRQCTL 0x0C
27#define REG_GEN3_IRQEN 0x10
28#define REG_GEN3_IRQTEMP1 0x14
29#define REG_GEN3_IRQTEMP2 0x18
30#define REG_GEN3_IRQTEMP3 0x1C
31#define REG_GEN3_CTSR 0x20
32#define REG_GEN3_THCTR 0x20
33#define REG_GEN3_TEMP 0x28
34#define REG_GEN3_THCODE1 0x50
35#define REG_GEN3_THCODE2 0x54
36#define REG_GEN3_THCODE3 0x58
37
Niklas Söderlund7d4b2692017-03-29 20:43:54 +020038/* IRQ{STR,MSK,EN} bits */
39#define IRQ_TEMP1 BIT(0)
40#define IRQ_TEMP2 BIT(1)
41#define IRQ_TEMP3 BIT(2)
42#define IRQ_TEMPD1 BIT(3)
43#define IRQ_TEMPD2 BIT(4)
44#define IRQ_TEMPD3 BIT(5)
45
Wolfram Sang564e73d2016-12-22 11:38:21 +010046/* CTSR bits */
47#define CTSR_PONM BIT(8)
48#define CTSR_AOUT BIT(7)
49#define CTSR_THBGR BIT(5)
50#define CTSR_VMEN BIT(4)
51#define CTSR_VMST BIT(1)
52#define CTSR_THSST BIT(0)
53
54/* THCTR bits */
55#define THCTR_PONM BIT(6)
56#define THCTR_THSST BIT(0)
57
58#define CTEMP_MASK 0xFFF
59
60#define MCELSIUS(temp) ((temp) * 1000)
61#define GEN3_FUSE_MASK 0xFFF
62
63#define TSC_MAX_NUM 3
64
65/* Structure for thermal temperature calculation */
66struct equation_coefs {
67 int a1;
68 int b1;
69 int a2;
70 int b2;
71};
72
73struct rcar_gen3_thermal_tsc {
74 void __iomem *base;
75 struct thermal_zone_device *zone;
76 struct equation_coefs coef;
Niklas Söderlund75f78d62017-03-29 20:43:56 +020077 int low;
78 int high;
Wolfram Sang564e73d2016-12-22 11:38:21 +010079};
80
81struct rcar_gen3_thermal_priv {
82 struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
Niklas Söderlund97dad1f2017-03-29 20:43:53 +020083 unsigned int num_tscs;
Niklas Söderlund7d4b2692017-03-29 20:43:54 +020084 spinlock_t lock; /* Protect interrupts on and off */
Wolfram Sang564e73d2016-12-22 11:38:21 +010085 void (*thermal_init)(struct rcar_gen3_thermal_tsc *tsc);
86};
87
88static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
89 u32 reg)
90{
91 return ioread32(tsc->base + reg);
92}
93
94static inline void rcar_gen3_thermal_write(struct rcar_gen3_thermal_tsc *tsc,
95 u32 reg, u32 data)
96{
97 iowrite32(data, tsc->base + reg);
98}
99
100/*
101 * Linear approximation for temperature
102 *
103 * [reg] = [temp] * a + b => [temp] = ([reg] - b) / a
104 *
105 * The constants a and b are calculated using two triplets of int values PTAT
106 * and THCODE. PTAT and THCODE can either be read from hardware or use hard
107 * coded values from driver. The formula to calculate a and b are taken from
108 * BSP and sparsely documented and understood.
109 *
110 * Examining the linear formula and the formula used to calculate constants a
111 * and b while knowing that the span for PTAT and THCODE values are between
112 * 0x000 and 0xfff the largest integer possible is 0xfff * 0xfff == 0xffe001.
113 * Integer also needs to be signed so that leaves 7 bits for binary
114 * fixed point scaling.
115 */
116
117#define FIXPT_SHIFT 7
118#define FIXPT_INT(_x) ((_x) << FIXPT_SHIFT)
Niklas Söderlund7d4b2692017-03-29 20:43:54 +0200119#define INT_FIXPT(_x) ((_x) >> FIXPT_SHIFT)
Wolfram Sang564e73d2016-12-22 11:38:21 +0100120#define FIXPT_DIV(_a, _b) DIV_ROUND_CLOSEST(((_a) << FIXPT_SHIFT), (_b))
121#define FIXPT_TO_MCELSIUS(_x) ((_x) * 1000 >> FIXPT_SHIFT)
122
123#define RCAR3_THERMAL_GRAN 500 /* mili Celsius */
124
125/* no idea where these constants come from */
Hien Dangfc66ddf2018-04-17 22:57:46 +0200126#define TJ_1 116
Wolfram Sang564e73d2016-12-22 11:38:21 +0100127#define TJ_3 -41
128
129static void rcar_gen3_thermal_calc_coefs(struct equation_coefs *coef,
130 int *ptat, int *thcode)
131{
132 int tj_2;
133
134 /* TODO: Find documentation and document constant calculation formula */
135
136 /*
137 * Division is not scaled in BSP and if scaled it might overflow
138 * the dividend (4095 * 4095 << 14 > INT_MAX) so keep it unscaled
139 */
Hien Dangfc66ddf2018-04-17 22:57:46 +0200140 tj_2 = (FIXPT_INT((ptat[1] - ptat[2]) * 157)
Wolfram Sang564e73d2016-12-22 11:38:21 +0100141 / (ptat[0] - ptat[2])) - FIXPT_INT(41);
142
143 coef->a1 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[2]),
144 tj_2 - FIXPT_INT(TJ_3));
145 coef->b1 = FIXPT_INT(thcode[2]) - coef->a1 * TJ_3;
146
147 coef->a2 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[0]),
148 tj_2 - FIXPT_INT(TJ_1));
149 coef->b2 = FIXPT_INT(thcode[0]) - coef->a2 * TJ_1;
150}
151
152static int rcar_gen3_thermal_round(int temp)
153{
154 int result, round_offs;
155
156 round_offs = temp >= 0 ? RCAR3_THERMAL_GRAN / 2 :
157 -RCAR3_THERMAL_GRAN / 2;
158 result = (temp + round_offs) / RCAR3_THERMAL_GRAN;
159 return result * RCAR3_THERMAL_GRAN;
160}
161
162static int rcar_gen3_thermal_get_temp(void *devdata, int *temp)
163{
164 struct rcar_gen3_thermal_tsc *tsc = devdata;
165 int mcelsius, val1, val2;
166 u32 reg;
167
168 /* Read register and convert to mili Celsius */
Wolfram Sang564e73d2016-12-22 11:38:21 +0100169 reg = rcar_gen3_thermal_read(tsc, REG_GEN3_TEMP) & CTEMP_MASK;
170
171 val1 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b1, tsc->coef.a1);
172 val2 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b2, tsc->coef.a2);
173 mcelsius = FIXPT_TO_MCELSIUS((val1 + val2) / 2);
174
Wolfram Sang564e73d2016-12-22 11:38:21 +0100175 /* Make sure we are inside specifications */
176 if ((mcelsius < MCELSIUS(-40)) || (mcelsius > MCELSIUS(125)))
177 return -EIO;
178
179 /* Round value to device granularity setting */
180 *temp = rcar_gen3_thermal_round(mcelsius);
181
182 return 0;
183}
184
Niklas Söderlund7d4b2692017-03-29 20:43:54 +0200185static int rcar_gen3_thermal_mcelsius_to_temp(struct rcar_gen3_thermal_tsc *tsc,
186 int mcelsius)
187{
188 int celsius, val1, val2;
189
190 celsius = DIV_ROUND_CLOSEST(mcelsius, 1000);
191 val1 = celsius * tsc->coef.a1 + tsc->coef.b1;
192 val2 = celsius * tsc->coef.a2 + tsc->coef.b2;
193
194 return INT_FIXPT((val1 + val2) / 2);
195}
196
197static int rcar_gen3_thermal_set_trips(void *devdata, int low, int high)
198{
199 struct rcar_gen3_thermal_tsc *tsc = devdata;
200
Niklas Söderlund270ba432018-04-17 22:57:47 +0200201 low = clamp_val(low, -40000, 120000);
202 high = clamp_val(high, -40000, 120000);
Niklas Söderlund7d4b2692017-03-29 20:43:54 +0200203
204 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP1,
205 rcar_gen3_thermal_mcelsius_to_temp(tsc, low));
206
207 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP2,
208 rcar_gen3_thermal_mcelsius_to_temp(tsc, high));
209
Niklas Söderlund75f78d62017-03-29 20:43:56 +0200210 tsc->low = low;
211 tsc->high = high;
212
Niklas Söderlund7d4b2692017-03-29 20:43:54 +0200213 return 0;
214}
215
Julia Lawalla0def102017-08-08 17:08:57 +0200216static const struct thermal_zone_of_device_ops rcar_gen3_tz_of_ops = {
Wolfram Sang564e73d2016-12-22 11:38:21 +0100217 .get_temp = rcar_gen3_thermal_get_temp,
Niklas Söderlund7d4b2692017-03-29 20:43:54 +0200218 .set_trips = rcar_gen3_thermal_set_trips,
Wolfram Sang564e73d2016-12-22 11:38:21 +0100219};
220
Niklas Söderlund7d4b2692017-03-29 20:43:54 +0200221static void rcar_thermal_irq_set(struct rcar_gen3_thermal_priv *priv, bool on)
222{
223 unsigned int i;
224 u32 val = on ? IRQ_TEMPD1 | IRQ_TEMP2 : 0;
225
226 for (i = 0; i < priv->num_tscs; i++)
227 rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, val);
228}
229
230static irqreturn_t rcar_gen3_thermal_irq(int irq, void *data)
231{
232 struct rcar_gen3_thermal_priv *priv = data;
233 u32 status;
234 int i, ret = IRQ_HANDLED;
235
236 spin_lock(&priv->lock);
237 for (i = 0; i < priv->num_tscs; i++) {
238 status = rcar_gen3_thermal_read(priv->tscs[i], REG_GEN3_IRQSTR);
239 rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQSTR, 0);
240 if (status)
241 ret = IRQ_WAKE_THREAD;
242 }
243
244 if (ret == IRQ_WAKE_THREAD)
245 rcar_thermal_irq_set(priv, false);
246
247 spin_unlock(&priv->lock);
248
249 return ret;
250}
251
252static irqreturn_t rcar_gen3_thermal_irq_thread(int irq, void *data)
253{
254 struct rcar_gen3_thermal_priv *priv = data;
255 unsigned long flags;
256 int i;
257
258 for (i = 0; i < priv->num_tscs; i++)
259 thermal_zone_device_update(priv->tscs[i]->zone,
260 THERMAL_EVENT_UNSPECIFIED);
261
262 spin_lock_irqsave(&priv->lock, flags);
263 rcar_thermal_irq_set(priv, true);
264 spin_unlock_irqrestore(&priv->lock, flags);
265
266 return IRQ_HANDLED;
267}
268
Niklas Söderlundd668c802017-10-17 13:36:13 +0200269static const struct soc_device_attribute r8a7795es1[] = {
270 { .soc_id = "r8a7795", .revision = "ES1.*" },
271 { /* sentinel */ }
272};
273
274static void rcar_gen3_thermal_init_r8a7795es1(struct rcar_gen3_thermal_tsc *tsc)
Wolfram Sang564e73d2016-12-22 11:38:21 +0100275{
276 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_THBGR);
277 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, 0x0);
278
279 usleep_range(1000, 2000);
280
281 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_PONM);
Niklas Söderlund7d4b2692017-03-29 20:43:54 +0200282
Wolfram Sang564e73d2016-12-22 11:38:21 +0100283 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F);
Niklas Söderlund7d4b2692017-03-29 20:43:54 +0200284 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0);
285 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN, IRQ_TEMPD1 | IRQ_TEMP2);
286
Wolfram Sang564e73d2016-12-22 11:38:21 +0100287 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
288 CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN);
289
290 usleep_range(100, 200);
291
292 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
293 CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN |
294 CTSR_VMST | CTSR_THSST);
295
296 usleep_range(1000, 2000);
297}
298
Niklas Söderlundd668c802017-10-17 13:36:13 +0200299static void rcar_gen3_thermal_init(struct rcar_gen3_thermal_tsc *tsc)
Wolfram Sang564e73d2016-12-22 11:38:21 +0100300{
301 u32 reg_val;
302
303 reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
304 reg_val &= ~THCTR_PONM;
305 rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
306
307 usleep_range(1000, 2000);
308
309 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F);
Niklas Söderlund7d4b2692017-03-29 20:43:54 +0200310 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0);
311 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN, IRQ_TEMPD1 | IRQ_TEMP2);
312
Wolfram Sang564e73d2016-12-22 11:38:21 +0100313 reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
314 reg_val |= THCTR_THSST;
315 rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
Niklas Söderlund78aefd22017-03-29 20:43:50 +0200316
317 usleep_range(1000, 2000);
Wolfram Sang564e73d2016-12-22 11:38:21 +0100318}
319
Wolfram Sang564e73d2016-12-22 11:38:21 +0100320static const struct of_device_id rcar_gen3_thermal_dt_ids[] = {
Niklas Söderlundd668c802017-10-17 13:36:13 +0200321 { .compatible = "renesas,r8a7795-thermal", },
322 { .compatible = "renesas,r8a7796-thermal", },
Niklas Söderlund2e7db3e2018-04-26 21:42:02 +0200323 { .compatible = "renesas,r8a77965-thermal", },
Wolfram Sang564e73d2016-12-22 11:38:21 +0100324 {},
325};
326MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
327
328static int rcar_gen3_thermal_remove(struct platform_device *pdev)
329{
330 struct device *dev = &pdev->dev;
331
332 pm_runtime_put(dev);
333 pm_runtime_disable(dev);
334
335 return 0;
336}
337
338static int rcar_gen3_thermal_probe(struct platform_device *pdev)
339{
340 struct rcar_gen3_thermal_priv *priv;
341 struct device *dev = &pdev->dev;
342 struct resource *res;
343 struct thermal_zone_device *zone;
Niklas Söderlund7d4b2692017-03-29 20:43:54 +0200344 int ret, irq, i;
345 char *irqname;
Wolfram Sang564e73d2016-12-22 11:38:21 +0100346
347 /* default values if FUSEs are missing */
348 /* TODO: Read values from hardware on supported platforms */
Hien Dangfc66ddf2018-04-17 22:57:46 +0200349 int ptat[3] = { 2631, 1509, 435 };
Wolfram Sang564e73d2016-12-22 11:38:21 +0100350 int thcode[TSC_MAX_NUM][3] = {
Hien Dangfc66ddf2018-04-17 22:57:46 +0200351 { 3397, 2800, 2221 },
352 { 3393, 2795, 2216 },
353 { 3389, 2805, 2237 },
Wolfram Sang564e73d2016-12-22 11:38:21 +0100354 };
355
356 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
357 if (!priv)
358 return -ENOMEM;
359
Niklas Söderlundd668c802017-10-17 13:36:13 +0200360 priv->thermal_init = rcar_gen3_thermal_init;
361 if (soc_device_match(r8a7795es1))
362 priv->thermal_init = rcar_gen3_thermal_init_r8a7795es1;
Niklas Söderlundcc4d0722017-03-29 20:43:55 +0200363
Niklas Söderlund7d4b2692017-03-29 20:43:54 +0200364 spin_lock_init(&priv->lock);
365
Wolfram Sang564e73d2016-12-22 11:38:21 +0100366 platform_set_drvdata(pdev, priv);
367
Niklas Söderlund7d4b2692017-03-29 20:43:54 +0200368 /*
369 * Request 2 (of the 3 possible) IRQs, the driver only needs to
370 * to trigger on the low and high trip points of the current
371 * temp window at this point.
372 */
373 for (i = 0; i < 2; i++) {
374 irq = platform_get_irq(pdev, i);
375 if (irq < 0)
376 return irq;
377
378 irqname = devm_kasprintf(dev, GFP_KERNEL, "%s:ch%d",
379 dev_name(dev), i);
380 if (!irqname)
381 return -ENOMEM;
382
383 ret = devm_request_threaded_irq(dev, irq, rcar_gen3_thermal_irq,
384 rcar_gen3_thermal_irq_thread,
385 IRQF_SHARED, irqname, priv);
386 if (ret)
387 return ret;
388 }
389
Wolfram Sang564e73d2016-12-22 11:38:21 +0100390 pm_runtime_enable(dev);
391 pm_runtime_get_sync(dev);
392
393 for (i = 0; i < TSC_MAX_NUM; i++) {
394 struct rcar_gen3_thermal_tsc *tsc;
395
Niklas Söderlundd51546c2017-03-29 20:43:52 +0200396 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
397 if (!res)
398 break;
399
Wolfram Sang564e73d2016-12-22 11:38:21 +0100400 tsc = devm_kzalloc(dev, sizeof(*tsc), GFP_KERNEL);
401 if (!tsc) {
402 ret = -ENOMEM;
403 goto error_unregister;
404 }
405
Wolfram Sang564e73d2016-12-22 11:38:21 +0100406 tsc->base = devm_ioremap_resource(dev, res);
407 if (IS_ERR(tsc->base)) {
408 ret = PTR_ERR(tsc->base);
409 goto error_unregister;
410 }
411
412 priv->tscs[i] = tsc;
Wolfram Sang564e73d2016-12-22 11:38:21 +0100413
Niklas Söderlundd668c802017-10-17 13:36:13 +0200414 priv->thermal_init(tsc);
Wolfram Sang564e73d2016-12-22 11:38:21 +0100415 rcar_gen3_thermal_calc_coefs(&tsc->coef, ptat, thcode[i]);
416
417 zone = devm_thermal_zone_of_sensor_register(dev, i, tsc,
418 &rcar_gen3_tz_of_ops);
419 if (IS_ERR(zone)) {
420 dev_err(dev, "Can't register thermal zone\n");
421 ret = PTR_ERR(zone);
422 goto error_unregister;
423 }
424 tsc->zone = zone;
Niklas Söderlund7d4b2692017-03-29 20:43:54 +0200425
426 ret = of_thermal_get_ntrips(tsc->zone);
427 if (ret < 0)
428 goto error_unregister;
429
430 dev_info(dev, "TSC%d: Loaded %d trip points\n", i, ret);
Wolfram Sang564e73d2016-12-22 11:38:21 +0100431 }
432
Niklas Söderlund97dad1f2017-03-29 20:43:53 +0200433 priv->num_tscs = i;
434
435 if (!priv->num_tscs) {
436 ret = -ENODEV;
437 goto error_unregister;
438 }
439
Niklas Söderlund7d4b2692017-03-29 20:43:54 +0200440 rcar_thermal_irq_set(priv, true);
441
Wolfram Sang564e73d2016-12-22 11:38:21 +0100442 return 0;
443
444error_unregister:
445 rcar_gen3_thermal_remove(pdev);
446
447 return ret;
448}
449
Niklas Söderlund75f78d62017-03-29 20:43:56 +0200450static int __maybe_unused rcar_gen3_thermal_suspend(struct device *dev)
451{
452 struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
453
454 rcar_thermal_irq_set(priv, false);
455
456 return 0;
457}
458
459static int __maybe_unused rcar_gen3_thermal_resume(struct device *dev)
460{
461 struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
462 unsigned int i;
463
464 for (i = 0; i < priv->num_tscs; i++) {
465 struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
466
Niklas Söderlundd668c802017-10-17 13:36:13 +0200467 priv->thermal_init(tsc);
Niklas Söderlund75f78d62017-03-29 20:43:56 +0200468 rcar_gen3_thermal_set_trips(tsc, tsc->low, tsc->high);
469 }
470
471 rcar_thermal_irq_set(priv, true);
472
473 return 0;
474}
475
476static SIMPLE_DEV_PM_OPS(rcar_gen3_thermal_pm_ops, rcar_gen3_thermal_suspend,
477 rcar_gen3_thermal_resume);
478
Wolfram Sang564e73d2016-12-22 11:38:21 +0100479static struct platform_driver rcar_gen3_thermal_driver = {
480 .driver = {
481 .name = "rcar_gen3_thermal",
Niklas Söderlund75f78d62017-03-29 20:43:56 +0200482 .pm = &rcar_gen3_thermal_pm_ops,
Wolfram Sang564e73d2016-12-22 11:38:21 +0100483 .of_match_table = rcar_gen3_thermal_dt_ids,
484 },
485 .probe = rcar_gen3_thermal_probe,
486 .remove = rcar_gen3_thermal_remove,
487};
488module_platform_driver(rcar_gen3_thermal_driver);
489
490MODULE_LICENSE("GPL v2");
491MODULE_DESCRIPTION("R-Car Gen3 THS thermal sensor driver");
492MODULE_AUTHOR("Wolfram Sang <wsa+renesas@sang-engineering.com>");