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 
20 #include "thermal_core.h"
21 #include "thermal_hwmon.h"
22 
23 /* 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 
38 /* 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 
46 /* 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 */
66 struct equation_coefs {
67 	int a1;
68 	int b1;
69 	int a2;
70 	int b2;
71 };
72 
73 struct rcar_gen3_thermal_tsc {
74 	void __iomem *base;
75 	struct thermal_zone_device *zone;
76 	struct equation_coefs coef;
77 	int low;
78 	int high;
79 };
80 
81 struct rcar_gen3_thermal_priv {
82 	struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
83 	unsigned int num_tscs;
84 	void (*thermal_init)(struct rcar_gen3_thermal_tsc *tsc);
85 };
86 
87 static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
88 					 u32 reg)
89 {
90 	return ioread32(tsc->base + reg);
91 }
92 
93 static inline void rcar_gen3_thermal_write(struct rcar_gen3_thermal_tsc *tsc,
94 					   u32 reg, u32 data)
95 {
96 	iowrite32(data, tsc->base + reg);
97 }
98 
99 /*
100  * Linear approximation for temperature
101  *
102  * [reg] = [temp] * a + b => [temp] = ([reg] - b) / a
103  *
104  * The constants a and b are calculated using two triplets of int values PTAT
105  * and THCODE. PTAT and THCODE can either be read from hardware or use hard
106  * coded values from driver. The formula to calculate a and b are taken from
107  * BSP and sparsely documented and understood.
108  *
109  * Examining the linear formula and the formula used to calculate constants a
110  * and b while knowing that the span for PTAT and THCODE values are between
111  * 0x000 and 0xfff the largest integer possible is 0xfff * 0xfff == 0xffe001.
112  * Integer also needs to be signed so that leaves 7 bits for binary
113  * fixed point scaling.
114  */
115 
116 #define FIXPT_SHIFT 7
117 #define FIXPT_INT(_x) ((_x) << FIXPT_SHIFT)
118 #define INT_FIXPT(_x) ((_x) >> FIXPT_SHIFT)
119 #define FIXPT_DIV(_a, _b) DIV_ROUND_CLOSEST(((_a) << FIXPT_SHIFT), (_b))
120 #define FIXPT_TO_MCELSIUS(_x) ((_x) * 1000 >> FIXPT_SHIFT)
121 
122 #define RCAR3_THERMAL_GRAN 500 /* mili Celsius */
123 
124 /* no idea where these constants come from */
125 #define TJ_1 116
126 #define TJ_3 -41
127 
128 static void rcar_gen3_thermal_calc_coefs(struct equation_coefs *coef,
129 					 int *ptat, int *thcode)
130 {
131 	int tj_2;
132 
133 	/* TODO: Find documentation and document constant calculation formula */
134 
135 	/*
136 	 * Division is not scaled in BSP and if scaled it might overflow
137 	 * the dividend (4095 * 4095 << 14 > INT_MAX) so keep it unscaled
138 	 */
139 	tj_2 = (FIXPT_INT((ptat[1] - ptat[2]) * 157)
140 		/ (ptat[0] - ptat[2])) - FIXPT_INT(41);
141 
142 	coef->a1 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[2]),
143 			     tj_2 - FIXPT_INT(TJ_3));
144 	coef->b1 = FIXPT_INT(thcode[2]) - coef->a1 * TJ_3;
145 
146 	coef->a2 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[0]),
147 			     tj_2 - FIXPT_INT(TJ_1));
148 	coef->b2 = FIXPT_INT(thcode[0]) - coef->a2 * TJ_1;
149 }
150 
151 static int rcar_gen3_thermal_round(int temp)
152 {
153 	int result, round_offs;
154 
155 	round_offs = temp >= 0 ? RCAR3_THERMAL_GRAN / 2 :
156 		-RCAR3_THERMAL_GRAN / 2;
157 	result = (temp + round_offs) / RCAR3_THERMAL_GRAN;
158 	return result * RCAR3_THERMAL_GRAN;
159 }
160 
161 static int rcar_gen3_thermal_get_temp(void *devdata, int *temp)
162 {
163 	struct rcar_gen3_thermal_tsc *tsc = devdata;
164 	int mcelsius, val1, val2;
165 	u32 reg;
166 
167 	/* Read register and convert to mili Celsius */
168 	reg = rcar_gen3_thermal_read(tsc, REG_GEN3_TEMP) & CTEMP_MASK;
169 
170 	val1 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b1, tsc->coef.a1);
171 	val2 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b2, tsc->coef.a2);
172 	mcelsius = FIXPT_TO_MCELSIUS((val1 + val2) / 2);
173 
174 	/* Make sure we are inside specifications */
175 	if ((mcelsius < MCELSIUS(-40)) || (mcelsius > MCELSIUS(125)))
176 		return -EIO;
177 
178 	/* Round value to device granularity setting */
179 	*temp = rcar_gen3_thermal_round(mcelsius);
180 
181 	return 0;
182 }
183 
184 static int rcar_gen3_thermal_mcelsius_to_temp(struct rcar_gen3_thermal_tsc *tsc,
185 					      int mcelsius)
186 {
187 	int celsius, val1, val2;
188 
189 	celsius = DIV_ROUND_CLOSEST(mcelsius, 1000);
190 	val1 = celsius * tsc->coef.a1 + tsc->coef.b1;
191 	val2 = celsius * tsc->coef.a2 + tsc->coef.b2;
192 
193 	return INT_FIXPT((val1 + val2) / 2);
194 }
195 
196 static int rcar_gen3_thermal_set_trips(void *devdata, int low, int high)
197 {
198 	struct rcar_gen3_thermal_tsc *tsc = devdata;
199 
200 	low = clamp_val(low, -40000, 120000);
201 	high = clamp_val(high, -40000, 120000);
202 
203 	rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP1,
204 				rcar_gen3_thermal_mcelsius_to_temp(tsc, low));
205 
206 	rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP2,
207 				rcar_gen3_thermal_mcelsius_to_temp(tsc, high));
208 
209 	tsc->low = low;
210 	tsc->high = high;
211 
212 	return 0;
213 }
214 
215 static const struct thermal_zone_of_device_ops rcar_gen3_tz_of_ops = {
216 	.get_temp	= rcar_gen3_thermal_get_temp,
217 	.set_trips	= rcar_gen3_thermal_set_trips,
218 };
219 
220 static void rcar_thermal_irq_set(struct rcar_gen3_thermal_priv *priv, bool on)
221 {
222 	unsigned int i;
223 	u32 val = on ? IRQ_TEMPD1 | IRQ_TEMP2 : 0;
224 
225 	for (i = 0; i < priv->num_tscs; i++)
226 		rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, val);
227 }
228 
229 static irqreturn_t rcar_gen3_thermal_irq(int irq, void *data)
230 {
231 	struct rcar_gen3_thermal_priv *priv = data;
232 	u32 status;
233 	int i;
234 
235 	for (i = 0; i < priv->num_tscs; i++) {
236 		status = rcar_gen3_thermal_read(priv->tscs[i], REG_GEN3_IRQSTR);
237 		rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQSTR, 0);
238 		if (status)
239 			thermal_zone_device_update(priv->tscs[i]->zone,
240 						   THERMAL_EVENT_UNSPECIFIED);
241 	}
242 
243 	return IRQ_HANDLED;
244 }
245 
246 static const struct soc_device_attribute r8a7795es1[] = {
247 	{ .soc_id = "r8a7795", .revision = "ES1.*" },
248 	{ /* sentinel */ }
249 };
250 
251 static void rcar_gen3_thermal_init_r8a7795es1(struct rcar_gen3_thermal_tsc *tsc)
252 {
253 	rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,  CTSR_THBGR);
254 	rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,  0x0);
255 
256 	usleep_range(1000, 2000);
257 
258 	rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_PONM);
259 
260 	rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F);
261 	rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0);
262 	rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN, IRQ_TEMPD1 | IRQ_TEMP2);
263 
264 	rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
265 				CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN);
266 
267 	usleep_range(100, 200);
268 
269 	rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
270 				CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN |
271 				CTSR_VMST | CTSR_THSST);
272 
273 	usleep_range(1000, 2000);
274 }
275 
276 static void rcar_gen3_thermal_init(struct rcar_gen3_thermal_tsc *tsc)
277 {
278 	u32 reg_val;
279 
280 	reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
281 	reg_val &= ~THCTR_PONM;
282 	rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
283 
284 	usleep_range(1000, 2000);
285 
286 	rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0);
287 	rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0);
288 	rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN, IRQ_TEMPD1 | IRQ_TEMP2);
289 
290 	reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
291 	reg_val |= THCTR_THSST;
292 	rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
293 
294 	usleep_range(1000, 2000);
295 }
296 
297 static const struct of_device_id rcar_gen3_thermal_dt_ids[] = {
298 	{ .compatible = "renesas,r8a774a1-thermal", },
299 	{ .compatible = "renesas,r8a7795-thermal", },
300 	{ .compatible = "renesas,r8a7796-thermal", },
301 	{ .compatible = "renesas,r8a77965-thermal", },
302 	{ .compatible = "renesas,r8a77980-thermal", },
303 	{},
304 };
305 MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
306 
307 static int rcar_gen3_thermal_remove(struct platform_device *pdev)
308 {
309 	struct device *dev = &pdev->dev;
310 	struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
311 
312 	rcar_thermal_irq_set(priv, false);
313 
314 	pm_runtime_put(dev);
315 	pm_runtime_disable(dev);
316 
317 	return 0;
318 }
319 
320 static void rcar_gen3_hwmon_action(void *data)
321 {
322 	struct thermal_zone_device *zone = data;
323 
324 	thermal_remove_hwmon_sysfs(zone);
325 }
326 
327 static int rcar_gen3_thermal_probe(struct platform_device *pdev)
328 {
329 	struct rcar_gen3_thermal_priv *priv;
330 	struct device *dev = &pdev->dev;
331 	struct resource *res;
332 	struct thermal_zone_device *zone;
333 	int ret, irq, i;
334 	char *irqname;
335 
336 	/* default values if FUSEs are missing */
337 	/* TODO: Read values from hardware on supported platforms */
338 	int ptat[3] = { 2631, 1509, 435 };
339 	int thcode[TSC_MAX_NUM][3] = {
340 		{ 3397, 2800, 2221 },
341 		{ 3393, 2795, 2216 },
342 		{ 3389, 2805, 2237 },
343 	};
344 
345 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
346 	if (!priv)
347 		return -ENOMEM;
348 
349 	priv->thermal_init = rcar_gen3_thermal_init;
350 	if (soc_device_match(r8a7795es1))
351 		priv->thermal_init = rcar_gen3_thermal_init_r8a7795es1;
352 
353 	platform_set_drvdata(pdev, priv);
354 
355 	/*
356 	 * Request 2 (of the 3 possible) IRQs, the driver only needs to
357 	 * to trigger on the low and high trip points of the current
358 	 * temp window at this point.
359 	 */
360 	for (i = 0; i < 2; i++) {
361 		irq = platform_get_irq(pdev, i);
362 		if (irq < 0)
363 			return irq;
364 
365 		irqname = devm_kasprintf(dev, GFP_KERNEL, "%s:ch%d",
366 					 dev_name(dev), i);
367 		if (!irqname)
368 			return -ENOMEM;
369 
370 		ret = devm_request_threaded_irq(dev, irq, NULL,
371 						rcar_gen3_thermal_irq,
372 						IRQF_ONESHOT, irqname, priv);
373 		if (ret)
374 			return ret;
375 	}
376 
377 	pm_runtime_enable(dev);
378 	pm_runtime_get_sync(dev);
379 
380 	for (i = 0; i < TSC_MAX_NUM; i++) {
381 		struct rcar_gen3_thermal_tsc *tsc;
382 
383 		res = platform_get_resource(pdev, IORESOURCE_MEM, i);
384 		if (!res)
385 			break;
386 
387 		tsc = devm_kzalloc(dev, sizeof(*tsc), GFP_KERNEL);
388 		if (!tsc) {
389 			ret = -ENOMEM;
390 			goto error_unregister;
391 		}
392 
393 		tsc->base = devm_ioremap_resource(dev, res);
394 		if (IS_ERR(tsc->base)) {
395 			ret = PTR_ERR(tsc->base);
396 			goto error_unregister;
397 		}
398 
399 		priv->tscs[i] = tsc;
400 
401 		priv->thermal_init(tsc);
402 		rcar_gen3_thermal_calc_coefs(&tsc->coef, ptat, thcode[i]);
403 
404 		zone = devm_thermal_zone_of_sensor_register(dev, i, tsc,
405 							    &rcar_gen3_tz_of_ops);
406 		if (IS_ERR(zone)) {
407 			dev_err(dev, "Can't register thermal zone\n");
408 			ret = PTR_ERR(zone);
409 			goto error_unregister;
410 		}
411 		tsc->zone = zone;
412 
413 		tsc->zone->tzp->no_hwmon = false;
414 		ret = thermal_add_hwmon_sysfs(tsc->zone);
415 		if (ret)
416 			goto error_unregister;
417 
418 		ret = devm_add_action(dev, rcar_gen3_hwmon_action, zone);
419 		if (ret) {
420 			rcar_gen3_hwmon_action(zone);
421 			goto error_unregister;
422 		}
423 
424 		ret = of_thermal_get_ntrips(tsc->zone);
425 		if (ret < 0)
426 			goto error_unregister;
427 
428 		dev_info(dev, "TSC%d: Loaded %d trip points\n", i, ret);
429 	}
430 
431 	priv->num_tscs = i;
432 
433 	if (!priv->num_tscs) {
434 		ret = -ENODEV;
435 		goto error_unregister;
436 	}
437 
438 	rcar_thermal_irq_set(priv, true);
439 
440 	return 0;
441 
442 error_unregister:
443 	rcar_gen3_thermal_remove(pdev);
444 
445 	return ret;
446 }
447 
448 static int __maybe_unused rcar_gen3_thermal_suspend(struct device *dev)
449 {
450 	struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
451 
452 	rcar_thermal_irq_set(priv, false);
453 
454 	return 0;
455 }
456 
457 static int __maybe_unused rcar_gen3_thermal_resume(struct device *dev)
458 {
459 	struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
460 	unsigned int i;
461 
462 	for (i = 0; i < priv->num_tscs; i++) {
463 		struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
464 
465 		priv->thermal_init(tsc);
466 		rcar_gen3_thermal_set_trips(tsc, tsc->low, tsc->high);
467 	}
468 
469 	rcar_thermal_irq_set(priv, true);
470 
471 	return 0;
472 }
473 
474 static SIMPLE_DEV_PM_OPS(rcar_gen3_thermal_pm_ops, rcar_gen3_thermal_suspend,
475 			 rcar_gen3_thermal_resume);
476 
477 static struct platform_driver rcar_gen3_thermal_driver = {
478 	.driver	= {
479 		.name	= "rcar_gen3_thermal",
480 		.pm = &rcar_gen3_thermal_pm_ops,
481 		.of_match_table = rcar_gen3_thermal_dt_ids,
482 	},
483 	.probe		= rcar_gen3_thermal_probe,
484 	.remove		= rcar_gen3_thermal_remove,
485 };
486 module_platform_driver(rcar_gen3_thermal_driver);
487 
488 MODULE_LICENSE("GPL v2");
489 MODULE_DESCRIPTION("R-Car Gen3 THS thermal sensor driver");
490 MODULE_AUTHOR("Wolfram Sang <wsa+renesas@sang-engineering.com>");
491