1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  R-Car THS/TSC thermal sensor driver
4  *
5  * Copyright (C) 2012 Renesas Solutions Corp.
6  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7  */
8 #include <linux/delay.h>
9 #include <linux/err.h>
10 #include <linux/irq.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/reboot.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
20 #include <linux/thermal.h>
21 
22 #include "thermal_hwmon.h"
23 
24 #define IDLE_INTERVAL	5000
25 
26 #define COMMON_STR	0x00
27 #define COMMON_ENR	0x04
28 #define COMMON_INTMSK	0x0c
29 
30 #define REG_POSNEG	0x20
31 #define REG_FILONOFF	0x28
32 #define REG_THSCR	0x2c
33 #define REG_THSSR	0x30
34 #define REG_INTCTRL	0x34
35 
36 /* THSCR */
37 #define CPCTL	(1 << 12)
38 
39 /* THSSR */
40 #define CTEMP	0x3f
41 
42 struct rcar_thermal_common {
43 	void __iomem *base;
44 	struct device *dev;
45 	struct list_head head;
46 	spinlock_t lock;
47 };
48 
49 struct rcar_thermal_chip {
50 	unsigned int use_of_thermal : 1;
51 	unsigned int has_filonoff : 1;
52 	unsigned int irq_per_ch : 1;
53 	unsigned int needs_suspend_resume : 1;
54 	unsigned int nirqs;
55 };
56 
57 static const struct rcar_thermal_chip rcar_thermal = {
58 	.use_of_thermal = 0,
59 	.has_filonoff = 1,
60 	.irq_per_ch = 0,
61 	.needs_suspend_resume = 0,
62 	.nirqs = 1,
63 };
64 
65 static const struct rcar_thermal_chip rcar_gen2_thermal = {
66 	.use_of_thermal = 1,
67 	.has_filonoff = 1,
68 	.irq_per_ch = 0,
69 	.needs_suspend_resume = 0,
70 	.nirqs = 1,
71 };
72 
73 static const struct rcar_thermal_chip rcar_gen3_thermal = {
74 	.use_of_thermal = 1,
75 	.has_filonoff = 0,
76 	.irq_per_ch = 1,
77 	.needs_suspend_resume = 1,
78 	/*
79 	 * The Gen3 chip has 3 interrupts, but this driver uses only 2
80 	 * interrupts to detect a temperature change, rise or fall.
81 	 */
82 	.nirqs = 2,
83 };
84 
85 struct rcar_thermal_priv {
86 	void __iomem *base;
87 	struct rcar_thermal_common *common;
88 	struct thermal_zone_device *zone;
89 	const struct rcar_thermal_chip *chip;
90 	struct delayed_work work;
91 	struct mutex lock;
92 	struct list_head list;
93 	int id;
94 	u32 ctemp;
95 };
96 
97 #define rcar_thermal_for_each_priv(pos, common)	\
98 	list_for_each_entry(pos, &common->head, list)
99 
100 #define MCELSIUS(temp)			((temp) * 1000)
101 #define rcar_zone_to_priv(zone)		((zone)->devdata)
102 #define rcar_priv_to_dev(priv)		((priv)->common->dev)
103 #define rcar_has_irq_support(priv)	((priv)->common->base)
104 #define rcar_id_to_shift(priv)		((priv)->id * 8)
105 
106 static const struct of_device_id rcar_thermal_dt_ids[] = {
107 	{
108 		.compatible = "renesas,rcar-thermal",
109 		.data = &rcar_thermal,
110 	},
111 	{
112 		.compatible = "renesas,rcar-gen2-thermal",
113 		 .data = &rcar_gen2_thermal,
114 	},
115 	{
116 		.compatible = "renesas,thermal-r8a77995",
117 		.data = &rcar_gen3_thermal,
118 	},
119 	{},
120 };
121 MODULE_DEVICE_TABLE(of, rcar_thermal_dt_ids);
122 
123 /*
124  *		basic functions
125  */
126 #define rcar_thermal_common_read(c, r) \
127 	_rcar_thermal_common_read(c, COMMON_ ##r)
128 static u32 _rcar_thermal_common_read(struct rcar_thermal_common *common,
129 				     u32 reg)
130 {
131 	return ioread32(common->base + reg);
132 }
133 
134 #define rcar_thermal_common_write(c, r, d) \
135 	_rcar_thermal_common_write(c, COMMON_ ##r, d)
136 static void _rcar_thermal_common_write(struct rcar_thermal_common *common,
137 				       u32 reg, u32 data)
138 {
139 	iowrite32(data, common->base + reg);
140 }
141 
142 #define rcar_thermal_common_bset(c, r, m, d) \
143 	_rcar_thermal_common_bset(c, COMMON_ ##r, m, d)
144 static void _rcar_thermal_common_bset(struct rcar_thermal_common *common,
145 				      u32 reg, u32 mask, u32 data)
146 {
147 	u32 val;
148 
149 	val = ioread32(common->base + reg);
150 	val &= ~mask;
151 	val |= (data & mask);
152 	iowrite32(val, common->base + reg);
153 }
154 
155 #define rcar_thermal_read(p, r) _rcar_thermal_read(p, REG_ ##r)
156 static u32 _rcar_thermal_read(struct rcar_thermal_priv *priv, u32 reg)
157 {
158 	return ioread32(priv->base + reg);
159 }
160 
161 #define rcar_thermal_write(p, r, d) _rcar_thermal_write(p, REG_ ##r, d)
162 static void _rcar_thermal_write(struct rcar_thermal_priv *priv,
163 				u32 reg, u32 data)
164 {
165 	iowrite32(data, priv->base + reg);
166 }
167 
168 #define rcar_thermal_bset(p, r, m, d) _rcar_thermal_bset(p, REG_ ##r, m, d)
169 static void _rcar_thermal_bset(struct rcar_thermal_priv *priv, u32 reg,
170 			       u32 mask, u32 data)
171 {
172 	u32 val;
173 
174 	val = ioread32(priv->base + reg);
175 	val &= ~mask;
176 	val |= (data & mask);
177 	iowrite32(val, priv->base + reg);
178 }
179 
180 /*
181  *		zone device functions
182  */
183 static int rcar_thermal_update_temp(struct rcar_thermal_priv *priv)
184 {
185 	struct device *dev = rcar_priv_to_dev(priv);
186 	int i;
187 	u32 ctemp, old, new;
188 	int ret = -EINVAL;
189 
190 	mutex_lock(&priv->lock);
191 
192 	/*
193 	 * TSC decides a value of CPTAP automatically,
194 	 * and this is the conditions which validate interrupt.
195 	 */
196 	rcar_thermal_bset(priv, THSCR, CPCTL, CPCTL);
197 
198 	ctemp = 0;
199 	old = ~0;
200 	for (i = 0; i < 128; i++) {
201 		/*
202 		 * we need to wait 300us after changing comparator offset
203 		 * to get stable temperature.
204 		 * see "Usage Notes" on datasheet
205 		 */
206 		udelay(300);
207 
208 		new = rcar_thermal_read(priv, THSSR) & CTEMP;
209 		if (new == old) {
210 			ctemp = new;
211 			break;
212 		}
213 		old = new;
214 	}
215 
216 	if (!ctemp) {
217 		dev_err(dev, "thermal sensor was broken\n");
218 		goto err_out_unlock;
219 	}
220 
221 	/*
222 	 * enable IRQ
223 	 */
224 	if (rcar_has_irq_support(priv)) {
225 		if (priv->chip->has_filonoff)
226 			rcar_thermal_write(priv, FILONOFF, 0);
227 
228 		/* enable Rising/Falling edge interrupt */
229 		rcar_thermal_write(priv, POSNEG,  0x1);
230 		rcar_thermal_write(priv, INTCTRL, (((ctemp - 0) << 8) |
231 						   ((ctemp - 1) << 0)));
232 	}
233 
234 	dev_dbg(dev, "thermal%d  %d -> %d\n", priv->id, priv->ctemp, ctemp);
235 
236 	priv->ctemp = ctemp;
237 	ret = 0;
238 err_out_unlock:
239 	mutex_unlock(&priv->lock);
240 	return ret;
241 }
242 
243 static int rcar_thermal_get_current_temp(struct rcar_thermal_priv *priv,
244 					 int *temp)
245 {
246 	int tmp;
247 	int ret;
248 
249 	ret = rcar_thermal_update_temp(priv);
250 	if (ret < 0)
251 		return ret;
252 
253 	mutex_lock(&priv->lock);
254 	tmp =  MCELSIUS((priv->ctemp * 5) - 65);
255 	mutex_unlock(&priv->lock);
256 
257 	if ((tmp < MCELSIUS(-45)) || (tmp > MCELSIUS(125))) {
258 		struct device *dev = rcar_priv_to_dev(priv);
259 
260 		dev_err(dev, "it couldn't measure temperature correctly\n");
261 		return -EIO;
262 	}
263 
264 	*temp = tmp;
265 
266 	return 0;
267 }
268 
269 static int rcar_thermal_of_get_temp(void *data, int *temp)
270 {
271 	struct rcar_thermal_priv *priv = data;
272 
273 	return rcar_thermal_get_current_temp(priv, temp);
274 }
275 
276 static int rcar_thermal_get_temp(struct thermal_zone_device *zone, int *temp)
277 {
278 	struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
279 
280 	return rcar_thermal_get_current_temp(priv, temp);
281 }
282 
283 static int rcar_thermal_get_trip_type(struct thermal_zone_device *zone,
284 				      int trip, enum thermal_trip_type *type)
285 {
286 	struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
287 	struct device *dev = rcar_priv_to_dev(priv);
288 
289 	/* see rcar_thermal_get_temp() */
290 	switch (trip) {
291 	case 0: /* +90 <= temp */
292 		*type = THERMAL_TRIP_CRITICAL;
293 		break;
294 	default:
295 		dev_err(dev, "rcar driver trip error\n");
296 		return -EINVAL;
297 	}
298 
299 	return 0;
300 }
301 
302 static int rcar_thermal_get_trip_temp(struct thermal_zone_device *zone,
303 				      int trip, int *temp)
304 {
305 	struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
306 	struct device *dev = rcar_priv_to_dev(priv);
307 
308 	/* see rcar_thermal_get_temp() */
309 	switch (trip) {
310 	case 0: /* +90 <= temp */
311 		*temp = MCELSIUS(90);
312 		break;
313 	default:
314 		dev_err(dev, "rcar driver trip error\n");
315 		return -EINVAL;
316 	}
317 
318 	return 0;
319 }
320 
321 static int rcar_thermal_notify(struct thermal_zone_device *zone,
322 			       int trip, enum thermal_trip_type type)
323 {
324 	struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
325 	struct device *dev = rcar_priv_to_dev(priv);
326 
327 	switch (type) {
328 	case THERMAL_TRIP_CRITICAL:
329 		/* FIXME */
330 		dev_warn(dev, "Thermal reached to critical temperature\n");
331 		break;
332 	default:
333 		break;
334 	}
335 
336 	return 0;
337 }
338 
339 static const struct thermal_zone_of_device_ops rcar_thermal_zone_of_ops = {
340 	.get_temp	= rcar_thermal_of_get_temp,
341 };
342 
343 static struct thermal_zone_device_ops rcar_thermal_zone_ops = {
344 	.get_temp	= rcar_thermal_get_temp,
345 	.get_trip_type	= rcar_thermal_get_trip_type,
346 	.get_trip_temp	= rcar_thermal_get_trip_temp,
347 	.notify		= rcar_thermal_notify,
348 };
349 
350 /*
351  *		interrupt
352  */
353 #define rcar_thermal_irq_enable(p)	_rcar_thermal_irq_ctrl(p, 1)
354 #define rcar_thermal_irq_disable(p)	_rcar_thermal_irq_ctrl(p, 0)
355 static void _rcar_thermal_irq_ctrl(struct rcar_thermal_priv *priv, int enable)
356 {
357 	struct rcar_thermal_common *common = priv->common;
358 	unsigned long flags;
359 	u32 mask = 0x3 << rcar_id_to_shift(priv); /* enable Rising/Falling */
360 
361 	if (!rcar_has_irq_support(priv))
362 		return;
363 
364 	spin_lock_irqsave(&common->lock, flags);
365 
366 	rcar_thermal_common_bset(common, INTMSK, mask, enable ? 0 : mask);
367 
368 	spin_unlock_irqrestore(&common->lock, flags);
369 }
370 
371 static void rcar_thermal_work(struct work_struct *work)
372 {
373 	struct rcar_thermal_priv *priv;
374 	int cctemp, nctemp;
375 	int ret;
376 
377 	priv = container_of(work, struct rcar_thermal_priv, work.work);
378 
379 	ret = rcar_thermal_get_current_temp(priv, &cctemp);
380 	if (ret < 0)
381 		return;
382 
383 	ret = rcar_thermal_update_temp(priv);
384 	if (ret < 0)
385 		return;
386 
387 	rcar_thermal_irq_enable(priv);
388 
389 	ret = rcar_thermal_get_current_temp(priv, &nctemp);
390 	if (ret < 0)
391 		return;
392 
393 	if (nctemp != cctemp)
394 		thermal_zone_device_update(priv->zone,
395 					   THERMAL_EVENT_UNSPECIFIED);
396 }
397 
398 static u32 rcar_thermal_had_changed(struct rcar_thermal_priv *priv, u32 status)
399 {
400 	struct device *dev = rcar_priv_to_dev(priv);
401 
402 	status = (status >> rcar_id_to_shift(priv)) & 0x3;
403 
404 	if (status) {
405 		dev_dbg(dev, "thermal%d %s%s\n",
406 			priv->id,
407 			(status & 0x2) ? "Rising " : "",
408 			(status & 0x1) ? "Falling" : "");
409 	}
410 
411 	return status;
412 }
413 
414 static irqreturn_t rcar_thermal_irq(int irq, void *data)
415 {
416 	struct rcar_thermal_common *common = data;
417 	struct rcar_thermal_priv *priv;
418 	unsigned long flags;
419 	u32 status, mask;
420 
421 	spin_lock_irqsave(&common->lock, flags);
422 
423 	mask	= rcar_thermal_common_read(common, INTMSK);
424 	status	= rcar_thermal_common_read(common, STR);
425 	rcar_thermal_common_write(common, STR, 0x000F0F0F & mask);
426 
427 	spin_unlock_irqrestore(&common->lock, flags);
428 
429 	status = status & ~mask;
430 
431 	/*
432 	 * check the status
433 	 */
434 	rcar_thermal_for_each_priv(priv, common) {
435 		if (rcar_thermal_had_changed(priv, status)) {
436 			rcar_thermal_irq_disable(priv);
437 			schedule_delayed_work(&priv->work,
438 					      msecs_to_jiffies(300));
439 		}
440 	}
441 
442 	return IRQ_HANDLED;
443 }
444 
445 /*
446  *		platform functions
447  */
448 static int rcar_thermal_remove(struct platform_device *pdev)
449 {
450 	struct rcar_thermal_common *common = platform_get_drvdata(pdev);
451 	struct device *dev = &pdev->dev;
452 	struct rcar_thermal_priv *priv;
453 
454 	rcar_thermal_for_each_priv(priv, common) {
455 		rcar_thermal_irq_disable(priv);
456 		if (priv->chip->use_of_thermal)
457 			thermal_remove_hwmon_sysfs(priv->zone);
458 		else
459 			thermal_zone_device_unregister(priv->zone);
460 	}
461 
462 	pm_runtime_put(dev);
463 	pm_runtime_disable(dev);
464 
465 	return 0;
466 }
467 
468 static int rcar_thermal_probe(struct platform_device *pdev)
469 {
470 	struct rcar_thermal_common *common;
471 	struct rcar_thermal_priv *priv;
472 	struct device *dev = &pdev->dev;
473 	struct resource *res, *irq;
474 	const struct rcar_thermal_chip *chip = of_device_get_match_data(dev);
475 	int mres = 0;
476 	int i;
477 	int ret = -ENODEV;
478 	int idle = IDLE_INTERVAL;
479 	u32 enr_bits = 0;
480 
481 	common = devm_kzalloc(dev, sizeof(*common), GFP_KERNEL);
482 	if (!common)
483 		return -ENOMEM;
484 
485 	platform_set_drvdata(pdev, common);
486 
487 	INIT_LIST_HEAD(&common->head);
488 	spin_lock_init(&common->lock);
489 	common->dev = dev;
490 
491 	pm_runtime_enable(dev);
492 	pm_runtime_get_sync(dev);
493 
494 	for (i = 0; i < chip->nirqs; i++) {
495 		irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
496 		if (!irq)
497 			continue;
498 		if (!common->base) {
499 			/*
500 			 * platform has IRQ support.
501 			 * Then, driver uses common registers
502 			 * rcar_has_irq_support() will be enabled
503 			 */
504 			res = platform_get_resource(pdev, IORESOURCE_MEM,
505 						    mres++);
506 			common->base = devm_ioremap_resource(dev, res);
507 			if (IS_ERR(common->base))
508 				return PTR_ERR(common->base);
509 
510 			idle = 0; /* polling delay is not needed */
511 		}
512 
513 		ret = devm_request_irq(dev, irq->start, rcar_thermal_irq,
514 				       IRQF_SHARED, dev_name(dev), common);
515 		if (ret) {
516 			dev_err(dev, "irq request failed\n ");
517 			goto error_unregister;
518 		}
519 
520 		/* update ENR bits */
521 		if (chip->irq_per_ch)
522 			enr_bits |= 1 << i;
523 	}
524 
525 	for (i = 0;; i++) {
526 		res = platform_get_resource(pdev, IORESOURCE_MEM, mres++);
527 		if (!res)
528 			break;
529 
530 		priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
531 		if (!priv) {
532 			ret = -ENOMEM;
533 			goto error_unregister;
534 		}
535 
536 		priv->base = devm_ioremap_resource(dev, res);
537 		if (IS_ERR(priv->base)) {
538 			ret = PTR_ERR(priv->base);
539 			goto error_unregister;
540 		}
541 
542 		priv->common = common;
543 		priv->id = i;
544 		priv->chip = chip;
545 		mutex_init(&priv->lock);
546 		INIT_LIST_HEAD(&priv->list);
547 		INIT_DELAYED_WORK(&priv->work, rcar_thermal_work);
548 		ret = rcar_thermal_update_temp(priv);
549 		if (ret < 0)
550 			goto error_unregister;
551 
552 		if (chip->use_of_thermal)
553 			priv->zone = devm_thermal_zone_of_sensor_register(
554 						dev, i, priv,
555 						&rcar_thermal_zone_of_ops);
556 		else
557 			priv->zone = thermal_zone_device_register(
558 						"rcar_thermal",
559 						1, 0, priv,
560 						&rcar_thermal_zone_ops, NULL, 0,
561 						idle);
562 		if (IS_ERR(priv->zone)) {
563 			dev_err(dev, "can't register thermal zone\n");
564 			ret = PTR_ERR(priv->zone);
565 			priv->zone = NULL;
566 			goto error_unregister;
567 		}
568 
569 		if (chip->use_of_thermal) {
570 			/*
571 			 * thermal_zone doesn't enable hwmon as default,
572 			 * but, enable it here to keep compatible
573 			 */
574 			priv->zone->tzp->no_hwmon = false;
575 			ret = thermal_add_hwmon_sysfs(priv->zone);
576 			if (ret)
577 				goto error_unregister;
578 		}
579 
580 		rcar_thermal_irq_enable(priv);
581 
582 		list_move_tail(&priv->list, &common->head);
583 
584 		/* update ENR bits */
585 		if (!chip->irq_per_ch)
586 			enr_bits |= 3 << (i * 8);
587 	}
588 
589 	if (common->base && enr_bits)
590 		rcar_thermal_common_write(common, ENR, enr_bits);
591 
592 	dev_info(dev, "%d sensor probed\n", i);
593 
594 	return 0;
595 
596 error_unregister:
597 	rcar_thermal_remove(pdev);
598 
599 	return ret;
600 }
601 
602 #ifdef CONFIG_PM_SLEEP
603 static int rcar_thermal_suspend(struct device *dev)
604 {
605 	struct rcar_thermal_common *common = dev_get_drvdata(dev);
606 	struct rcar_thermal_priv *priv = list_first_entry(&common->head,
607 							  typeof(*priv), list);
608 
609 	if (priv->chip->needs_suspend_resume) {
610 		rcar_thermal_common_write(common, ENR, 0);
611 		rcar_thermal_irq_disable(priv);
612 		rcar_thermal_bset(priv, THSCR, CPCTL, 0);
613 	}
614 
615 	return 0;
616 }
617 
618 static int rcar_thermal_resume(struct device *dev)
619 {
620 	struct rcar_thermal_common *common = dev_get_drvdata(dev);
621 	struct rcar_thermal_priv *priv = list_first_entry(&common->head,
622 							  typeof(*priv), list);
623 	int ret;
624 
625 	if (priv->chip->needs_suspend_resume) {
626 		ret = rcar_thermal_update_temp(priv);
627 		if (ret < 0)
628 			return ret;
629 		rcar_thermal_irq_enable(priv);
630 		rcar_thermal_common_write(common, ENR, 0x03);
631 	}
632 
633 	return 0;
634 }
635 #endif
636 
637 static SIMPLE_DEV_PM_OPS(rcar_thermal_pm_ops, rcar_thermal_suspend,
638 			 rcar_thermal_resume);
639 
640 static struct platform_driver rcar_thermal_driver = {
641 	.driver	= {
642 		.name	= "rcar_thermal",
643 		.pm = &rcar_thermal_pm_ops,
644 		.of_match_table = rcar_thermal_dt_ids,
645 	},
646 	.probe		= rcar_thermal_probe,
647 	.remove		= rcar_thermal_remove,
648 };
649 module_platform_driver(rcar_thermal_driver);
650 
651 MODULE_LICENSE("GPL v2");
652 MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver");
653 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
654