Linux 4.19.133
[linux/fpc-iii.git] / drivers / thermal / rcar_gen3_thermal.c
blob8f553453dd7fcb552698c5e802ccd88e7e04fae4
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.
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>
20 #include "thermal_core.h"
22 /* Register offsets */
23 #define REG_GEN3_IRQSTR 0x04
24 #define REG_GEN3_IRQMSK 0x08
25 #define REG_GEN3_IRQCTL 0x0C
26 #define REG_GEN3_IRQEN 0x10
27 #define REG_GEN3_IRQTEMP1 0x14
28 #define REG_GEN3_IRQTEMP2 0x18
29 #define REG_GEN3_IRQTEMP3 0x1C
30 #define REG_GEN3_CTSR 0x20
31 #define REG_GEN3_THCTR 0x20
32 #define REG_GEN3_TEMP 0x28
33 #define REG_GEN3_THCODE1 0x50
34 #define REG_GEN3_THCODE2 0x54
35 #define REG_GEN3_THCODE3 0x58
37 /* IRQ{STR,MSK,EN} bits */
38 #define IRQ_TEMP1 BIT(0)
39 #define IRQ_TEMP2 BIT(1)
40 #define IRQ_TEMP3 BIT(2)
41 #define IRQ_TEMPD1 BIT(3)
42 #define IRQ_TEMPD2 BIT(4)
43 #define IRQ_TEMPD3 BIT(5)
45 /* CTSR bits */
46 #define CTSR_PONM BIT(8)
47 #define CTSR_AOUT BIT(7)
48 #define CTSR_THBGR BIT(5)
49 #define CTSR_VMEN BIT(4)
50 #define CTSR_VMST BIT(1)
51 #define CTSR_THSST BIT(0)
53 /* THCTR bits */
54 #define THCTR_PONM BIT(6)
55 #define THCTR_THSST BIT(0)
57 #define CTEMP_MASK 0xFFF
59 #define MCELSIUS(temp) ((temp) * 1000)
60 #define GEN3_FUSE_MASK 0xFFF
62 #define TSC_MAX_NUM 3
64 /* Structure for thermal temperature calculation */
65 struct equation_coefs {
66 int a1;
67 int b1;
68 int a2;
69 int b2;
72 struct rcar_gen3_thermal_tsc {
73 void __iomem *base;
74 struct thermal_zone_device *zone;
75 struct equation_coefs coef;
76 int low;
77 int high;
80 struct rcar_gen3_thermal_priv {
81 struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
82 unsigned int num_tscs;
83 void (*thermal_init)(struct rcar_gen3_thermal_tsc *tsc);
86 static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
87 u32 reg)
89 return ioread32(tsc->base + reg);
92 static inline void rcar_gen3_thermal_write(struct rcar_gen3_thermal_tsc *tsc,
93 u32 reg, u32 data)
95 iowrite32(data, tsc->base + reg);
99 * Linear approximation for temperature
101 * [reg] = [temp] * a + b => [temp] = ([reg] - b) / a
103 * The constants a and b are calculated using two triplets of int values PTAT
104 * and THCODE. PTAT and THCODE can either be read from hardware or use hard
105 * coded values from driver. The formula to calculate a and b are taken from
106 * BSP and sparsely documented and understood.
108 * Examining the linear formula and the formula used to calculate constants a
109 * and b while knowing that the span for PTAT and THCODE values are between
110 * 0x000 and 0xfff the largest integer possible is 0xfff * 0xfff == 0xffe001.
111 * Integer also needs to be signed so that leaves 7 bits for binary
112 * fixed point scaling.
115 #define FIXPT_SHIFT 7
116 #define FIXPT_INT(_x) ((_x) << FIXPT_SHIFT)
117 #define INT_FIXPT(_x) ((_x) >> FIXPT_SHIFT)
118 #define FIXPT_DIV(_a, _b) DIV_ROUND_CLOSEST(((_a) << FIXPT_SHIFT), (_b))
119 #define FIXPT_TO_MCELSIUS(_x) ((_x) * 1000 >> FIXPT_SHIFT)
121 #define RCAR3_THERMAL_GRAN 500 /* mili Celsius */
123 /* no idea where these constants come from */
124 #define TJ_1 116
125 #define TJ_3 -41
127 static void rcar_gen3_thermal_calc_coefs(struct equation_coefs *coef,
128 int *ptat, int *thcode)
130 int tj_2;
132 /* TODO: Find documentation and document constant calculation formula */
135 * Division is not scaled in BSP and if scaled it might overflow
136 * the dividend (4095 * 4095 << 14 > INT_MAX) so keep it unscaled
138 tj_2 = (FIXPT_INT((ptat[1] - ptat[2]) * 157)
139 / (ptat[0] - ptat[2])) - FIXPT_INT(41);
141 coef->a1 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[2]),
142 tj_2 - FIXPT_INT(TJ_3));
143 coef->b1 = FIXPT_INT(thcode[2]) - coef->a1 * TJ_3;
145 coef->a2 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[0]),
146 tj_2 - FIXPT_INT(TJ_1));
147 coef->b2 = FIXPT_INT(thcode[0]) - coef->a2 * TJ_1;
150 static int rcar_gen3_thermal_round(int temp)
152 int result, round_offs;
154 round_offs = temp >= 0 ? RCAR3_THERMAL_GRAN / 2 :
155 -RCAR3_THERMAL_GRAN / 2;
156 result = (temp + round_offs) / RCAR3_THERMAL_GRAN;
157 return result * RCAR3_THERMAL_GRAN;
160 static int rcar_gen3_thermal_get_temp(void *devdata, int *temp)
162 struct rcar_gen3_thermal_tsc *tsc = devdata;
163 int mcelsius, val1, val2;
164 u32 reg;
166 /* Read register and convert to mili Celsius */
167 reg = rcar_gen3_thermal_read(tsc, REG_GEN3_TEMP) & CTEMP_MASK;
169 val1 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b1, tsc->coef.a1);
170 val2 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b2, tsc->coef.a2);
171 mcelsius = FIXPT_TO_MCELSIUS((val1 + val2) / 2);
173 /* Make sure we are inside specifications */
174 if ((mcelsius < MCELSIUS(-40)) || (mcelsius > MCELSIUS(125)))
175 return -EIO;
177 /* Round value to device granularity setting */
178 *temp = rcar_gen3_thermal_round(mcelsius);
180 return 0;
183 static int rcar_gen3_thermal_mcelsius_to_temp(struct rcar_gen3_thermal_tsc *tsc,
184 int mcelsius)
186 int celsius, val1, val2;
188 celsius = DIV_ROUND_CLOSEST(mcelsius, 1000);
189 val1 = celsius * tsc->coef.a1 + tsc->coef.b1;
190 val2 = celsius * tsc->coef.a2 + tsc->coef.b2;
192 return INT_FIXPT((val1 + val2) / 2);
195 static int rcar_gen3_thermal_set_trips(void *devdata, int low, int high)
197 struct rcar_gen3_thermal_tsc *tsc = devdata;
199 low = clamp_val(low, -40000, 120000);
200 high = clamp_val(high, -40000, 120000);
202 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP1,
203 rcar_gen3_thermal_mcelsius_to_temp(tsc, low));
205 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP2,
206 rcar_gen3_thermal_mcelsius_to_temp(tsc, high));
208 tsc->low = low;
209 tsc->high = high;
211 return 0;
214 static const struct thermal_zone_of_device_ops rcar_gen3_tz_of_ops = {
215 .get_temp = rcar_gen3_thermal_get_temp,
216 .set_trips = rcar_gen3_thermal_set_trips,
219 static void rcar_thermal_irq_set(struct rcar_gen3_thermal_priv *priv, bool on)
221 unsigned int i;
222 u32 val = on ? IRQ_TEMPD1 | IRQ_TEMP2 : 0;
224 for (i = 0; i < priv->num_tscs; i++)
225 rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, val);
228 static irqreturn_t rcar_gen3_thermal_irq(int irq, void *data)
230 struct rcar_gen3_thermal_priv *priv = data;
231 u32 status;
232 int i;
234 for (i = 0; i < priv->num_tscs; i++) {
235 status = rcar_gen3_thermal_read(priv->tscs[i], REG_GEN3_IRQSTR);
236 rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQSTR, 0);
237 if (status)
238 thermal_zone_device_update(priv->tscs[i]->zone,
239 THERMAL_EVENT_UNSPECIFIED);
242 return IRQ_HANDLED;
245 static const struct soc_device_attribute r8a7795es1[] = {
246 { .soc_id = "r8a7795", .revision = "ES1.*" },
247 { /* sentinel */ }
250 static void rcar_gen3_thermal_init_r8a7795es1(struct rcar_gen3_thermal_tsc *tsc)
252 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_THBGR);
253 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, 0x0);
255 usleep_range(1000, 2000);
257 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_PONM);
259 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F);
260 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0);
261 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN, IRQ_TEMPD1 | IRQ_TEMP2);
263 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
264 CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN);
266 usleep_range(100, 200);
268 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
269 CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN |
270 CTSR_VMST | CTSR_THSST);
272 usleep_range(1000, 2000);
275 static void rcar_gen3_thermal_init(struct rcar_gen3_thermal_tsc *tsc)
277 u32 reg_val;
279 reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
280 reg_val &= ~THCTR_PONM;
281 rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
283 usleep_range(1000, 2000);
285 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F);
286 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0);
287 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN, IRQ_TEMPD1 | IRQ_TEMP2);
289 reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
290 reg_val |= THCTR_THSST;
291 rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
293 usleep_range(1000, 2000);
296 static const struct of_device_id rcar_gen3_thermal_dt_ids[] = {
297 { .compatible = "renesas,r8a7795-thermal", },
298 { .compatible = "renesas,r8a7796-thermal", },
299 { .compatible = "renesas,r8a77965-thermal", },
302 MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
304 static int rcar_gen3_thermal_remove(struct platform_device *pdev)
306 struct device *dev = &pdev->dev;
307 struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
309 rcar_thermal_irq_set(priv, false);
311 pm_runtime_put(dev);
312 pm_runtime_disable(dev);
314 return 0;
317 static int rcar_gen3_thermal_probe(struct platform_device *pdev)
319 struct rcar_gen3_thermal_priv *priv;
320 struct device *dev = &pdev->dev;
321 struct resource *res;
322 struct thermal_zone_device *zone;
323 int ret, irq, i;
324 char *irqname;
326 /* default values if FUSEs are missing */
327 /* TODO: Read values from hardware on supported platforms */
328 int ptat[3] = { 2631, 1509, 435 };
329 int thcode[TSC_MAX_NUM][3] = {
330 { 3397, 2800, 2221 },
331 { 3393, 2795, 2216 },
332 { 3389, 2805, 2237 },
335 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
336 if (!priv)
337 return -ENOMEM;
339 priv->thermal_init = rcar_gen3_thermal_init;
340 if (soc_device_match(r8a7795es1))
341 priv->thermal_init = rcar_gen3_thermal_init_r8a7795es1;
343 platform_set_drvdata(pdev, priv);
346 * Request 2 (of the 3 possible) IRQs, the driver only needs to
347 * to trigger on the low and high trip points of the current
348 * temp window at this point.
350 for (i = 0; i < 2; i++) {
351 irq = platform_get_irq(pdev, i);
352 if (irq < 0)
353 return irq;
355 irqname = devm_kasprintf(dev, GFP_KERNEL, "%s:ch%d",
356 dev_name(dev), i);
357 if (!irqname)
358 return -ENOMEM;
360 ret = devm_request_threaded_irq(dev, irq, NULL,
361 rcar_gen3_thermal_irq,
362 IRQF_ONESHOT, irqname, priv);
363 if (ret)
364 return ret;
367 pm_runtime_enable(dev);
368 pm_runtime_get_sync(dev);
370 for (i = 0; i < TSC_MAX_NUM; i++) {
371 struct rcar_gen3_thermal_tsc *tsc;
373 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
374 if (!res)
375 break;
377 tsc = devm_kzalloc(dev, sizeof(*tsc), GFP_KERNEL);
378 if (!tsc) {
379 ret = -ENOMEM;
380 goto error_unregister;
383 tsc->base = devm_ioremap_resource(dev, res);
384 if (IS_ERR(tsc->base)) {
385 ret = PTR_ERR(tsc->base);
386 goto error_unregister;
389 priv->tscs[i] = tsc;
391 priv->thermal_init(tsc);
392 rcar_gen3_thermal_calc_coefs(&tsc->coef, ptat, thcode[i]);
394 zone = devm_thermal_zone_of_sensor_register(dev, i, tsc,
395 &rcar_gen3_tz_of_ops);
396 if (IS_ERR(zone)) {
397 dev_err(dev, "Can't register thermal zone\n");
398 ret = PTR_ERR(zone);
399 goto error_unregister;
401 tsc->zone = zone;
403 ret = of_thermal_get_ntrips(tsc->zone);
404 if (ret < 0)
405 goto error_unregister;
407 dev_info(dev, "TSC%d: Loaded %d trip points\n", i, ret);
410 priv->num_tscs = i;
412 if (!priv->num_tscs) {
413 ret = -ENODEV;
414 goto error_unregister;
417 rcar_thermal_irq_set(priv, true);
419 return 0;
421 error_unregister:
422 rcar_gen3_thermal_remove(pdev);
424 return ret;
427 static int __maybe_unused rcar_gen3_thermal_suspend(struct device *dev)
429 struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
431 rcar_thermal_irq_set(priv, false);
433 return 0;
436 static int __maybe_unused rcar_gen3_thermal_resume(struct device *dev)
438 struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
439 unsigned int i;
441 for (i = 0; i < priv->num_tscs; i++) {
442 struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
444 priv->thermal_init(tsc);
445 rcar_gen3_thermal_set_trips(tsc, tsc->low, tsc->high);
448 rcar_thermal_irq_set(priv, true);
450 return 0;
453 static SIMPLE_DEV_PM_OPS(rcar_gen3_thermal_pm_ops, rcar_gen3_thermal_suspend,
454 rcar_gen3_thermal_resume);
456 static struct platform_driver rcar_gen3_thermal_driver = {
457 .driver = {
458 .name = "rcar_gen3_thermal",
459 .pm = &rcar_gen3_thermal_pm_ops,
460 .of_match_table = rcar_gen3_thermal_dt_ids,
462 .probe = rcar_gen3_thermal_probe,
463 .remove = rcar_gen3_thermal_remove,
465 module_platform_driver(rcar_gen3_thermal_driver);
467 MODULE_LICENSE("GPL v2");
468 MODULE_DESCRIPTION("R-Car Gen3 THS thermal sensor driver");
469 MODULE_AUTHOR("Wolfram Sang <wsa+renesas@sang-engineering.com>");