blob: 1e93f60b6d7445d68b3f7540f7cd2c3207a60737 [file] [log] [blame]
Kuninori Morimotoc9545792018-07-30 07:56:06 +00001// SPDX-License-Identifier: GPL-2.0
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +10002/*
3 * R-Car THS/TSC thermal sensor driver
4 *
5 * Copyright (C) 2012 Renesas Solutions Corp.
6 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +10007 */
8#include <linux/delay.h>
9#include <linux/err.h>
Kuninori Morimotoe0a51722013-01-31 09:04:48 +000010#include <linux/irq.h>
11#include <linux/interrupt.h>
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100012#include <linux/io.h>
13#include <linux/module.h>
Rob Herringf6a756e2023-07-14 11:50:07 -060014#include <linux/of.h>
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100015#include <linux/platform_device.h>
Kuninori Morimoto51d45d22013-03-26 06:08:52 +000016#include <linux/pm_runtime.h>
kuninori.morimoto.gx@renesas.comd2a73e22012-12-02 18:48:41 -080017#include <linux/reboot.h>
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100018#include <linux/slab.h>
19#include <linux/spinlock.h>
20#include <linux/thermal.h>
21
Niklas Söderlund9d617942024-05-06 17:40:10 +020022#include "../thermal_hwmon.h"
Kuninori Morimoto64a411e2016-07-19 10:01:37 +000023
kuninori.morimoto.gx@renesas.comd2a73e22012-12-02 18:48:41 -080024#define IDLE_INTERVAL 5000
25
Kuninori Morimotoe0a51722013-01-31 09:04:48 +000026#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
Kuninori Morimotoe9137a52013-01-31 09:03:46 +000032#define REG_THSCR 0x2c
33#define REG_THSSR 0x30
Kuninori Morimotoe0a51722013-01-31 09:04:48 +000034#define REG_INTCTRL 0x34
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100035
36/* THSCR */
Kuninori Morimotof8f53e12013-01-31 09:03:11 +000037#define CPCTL (1 << 12)
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100038
39/* THSSR */
40#define CTEMP 0x3f
41
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +000042struct rcar_thermal_common {
43 void __iomem *base;
44 struct device *dev;
45 struct list_head head;
Kuninori Morimotoe0a51722013-01-31 09:04:48 +000046 spinlock_t lock;
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +000047};
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100048
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +090049struct 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;
Yoshihiro Kaneko20386f02019-05-08 13:08:45 +020055 unsigned int ctemp_bands;
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +090056};
57
58static const struct rcar_thermal_chip rcar_thermal = {
59 .use_of_thermal = 0,
60 .has_filonoff = 1,
61 .irq_per_ch = 0,
62 .needs_suspend_resume = 0,
63 .nirqs = 1,
Yoshihiro Kaneko20386f02019-05-08 13:08:45 +020064 .ctemp_bands = 1,
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +090065};
66
67static const struct rcar_thermal_chip rcar_gen2_thermal = {
68 .use_of_thermal = 1,
69 .has_filonoff = 1,
70 .irq_per_ch = 0,
71 .needs_suspend_resume = 0,
72 .nirqs = 1,
Yoshihiro Kaneko20386f02019-05-08 13:08:45 +020073 .ctemp_bands = 1,
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +090074};
75
76static const struct rcar_thermal_chip rcar_gen3_thermal = {
77 .use_of_thermal = 1,
78 .has_filonoff = 0,
79 .irq_per_ch = 1,
80 .needs_suspend_resume = 1,
81 /*
82 * The Gen3 chip has 3 interrupts, but this driver uses only 2
83 * interrupts to detect a temperature change, rise or fall.
84 */
85 .nirqs = 2,
Yoshihiro Kaneko20386f02019-05-08 13:08:45 +020086 .ctemp_bands = 2,
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +090087};
88
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100089struct rcar_thermal_priv {
90 void __iomem *base;
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +000091 struct rcar_thermal_common *common;
92 struct thermal_zone_device *zone;
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +090093 const struct rcar_thermal_chip *chip;
Kuninori Morimotoe0a51722013-01-31 09:04:48 +000094 struct delayed_work work;
Kuninori Morimotob2bbc6a2013-01-31 09:03:22 +000095 struct mutex lock;
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +000096 struct list_head list;
Kuninori Morimotoe0a51722013-01-31 09:04:48 +000097 int id;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100098};
99
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000100#define rcar_thermal_for_each_priv(pos, common) \
101 list_for_each_entry(pos, &common->head, list)
102
Kuninori Morimotoc4997032012-11-26 02:32:06 +0000103#define MCELSIUS(temp) ((temp) * 1000)
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000104#define rcar_priv_to_dev(priv) ((priv)->common->dev)
105#define rcar_has_irq_support(priv) ((priv)->common->base)
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000106#define rcar_id_to_shift(priv) ((priv)->id * 8)
107
Kuninori Morimotoca1e4552015-12-15 01:17:07 +0000108static const struct of_device_id rcar_thermal_dt_ids[] = {
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +0900109 {
110 .compatible = "renesas,rcar-thermal",
111 .data = &rcar_thermal,
112 },
113 {
114 .compatible = "renesas,rcar-gen2-thermal",
115 .data = &rcar_gen2_thermal,
116 },
117 {
Fabrizio Castrob8d3d112018-12-13 20:23:10 +0000118 .compatible = "renesas,thermal-r8a774c0",
119 .data = &rcar_gen3_thermal,
120 },
121 {
Sergei Shtylyov92ca3662018-10-05 00:03:13 +0300122 .compatible = "renesas,thermal-r8a77970",
123 .data = &rcar_gen3_thermal,
124 },
125 {
Yoshihiro Kanekoe36e1302018-12-17 23:50:21 +0900126 .compatible = "renesas,thermal-r8a77990",
127 .data = &rcar_gen3_thermal,
128 },
129 {
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +0900130 .compatible = "renesas,thermal-r8a77995",
131 .data = &rcar_gen3_thermal,
132 },
Kuninori Morimotoca1e4552015-12-15 01:17:07 +0000133 {},
134};
135MODULE_DEVICE_TABLE(of, rcar_thermal_dt_ids);
136
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000137/*
138 * basic functions
139 */
Kuninori Morimotoe9137a52013-01-31 09:03:46 +0000140#define rcar_thermal_common_read(c, r) \
141 _rcar_thermal_common_read(c, COMMON_ ##r)
142static u32 _rcar_thermal_common_read(struct rcar_thermal_common *common,
143 u32 reg)
144{
145 return ioread32(common->base + reg);
146}
147
148#define rcar_thermal_common_write(c, r, d) \
149 _rcar_thermal_common_write(c, COMMON_ ##r, d)
150static void _rcar_thermal_common_write(struct rcar_thermal_common *common,
151 u32 reg, u32 data)
152{
153 iowrite32(data, common->base + reg);
154}
155
156#define rcar_thermal_common_bset(c, r, m, d) \
157 _rcar_thermal_common_bset(c, COMMON_ ##r, m, d)
158static void _rcar_thermal_common_bset(struct rcar_thermal_common *common,
159 u32 reg, u32 mask, u32 data)
160{
161 u32 val;
162
163 val = ioread32(common->base + reg);
164 val &= ~mask;
165 val |= (data & mask);
166 iowrite32(val, common->base + reg);
167}
Kuninori Morimotoe9137a52013-01-31 09:03:46 +0000168
169#define rcar_thermal_read(p, r) _rcar_thermal_read(p, REG_ ##r)
170static u32 _rcar_thermal_read(struct rcar_thermal_priv *priv, u32 reg)
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000171{
Kuninori Morimotob2bbc6a2013-01-31 09:03:22 +0000172 return ioread32(priv->base + reg);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000173}
174
Kuninori Morimotoe9137a52013-01-31 09:03:46 +0000175#define rcar_thermal_write(p, r, d) _rcar_thermal_write(p, REG_ ##r, d)
176static void _rcar_thermal_write(struct rcar_thermal_priv *priv,
177 u32 reg, u32 data)
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000178{
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000179 iowrite32(data, priv->base + reg);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000180}
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000181
Kuninori Morimotoe9137a52013-01-31 09:03:46 +0000182#define rcar_thermal_bset(p, r, m, d) _rcar_thermal_bset(p, REG_ ##r, m, d)
183static void _rcar_thermal_bset(struct rcar_thermal_priv *priv, u32 reg,
184 u32 mask, u32 data)
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000185{
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000186 u32 val;
187
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000188 val = ioread32(priv->base + reg);
189 val &= ~mask;
190 val |= (data & mask);
191 iowrite32(val, priv->base + reg);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000192}
193
194/*
195 * zone device functions
196 */
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000197static int rcar_thermal_update_temp(struct rcar_thermal_priv *priv)
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000198{
Kuninori Morimotof8f53e12013-01-31 09:03:11 +0000199 struct device *dev = rcar_priv_to_dev(priv);
Niklas Söderlundb03628b2020-05-14 17:25:05 +0200200 int old, new, ctemp = -EINVAL;
201 unsigned int i;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000202
Kuninori Morimotob2bbc6a2013-01-31 09:03:22 +0000203 mutex_lock(&priv->lock);
204
Kuninori Morimotof8f53e12013-01-31 09:03:11 +0000205 /*
206 * TSC decides a value of CPTAP automatically,
207 * and this is the conditions which validate interrupt.
208 */
209 rcar_thermal_bset(priv, THSCR, CPCTL, CPCTL);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000210
Kuninori Morimotof8f53e12013-01-31 09:03:11 +0000211 old = ~0;
212 for (i = 0; i < 128; i++) {
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000213 /*
214 * we need to wait 300us after changing comparator offset
215 * to get stable temperature.
216 * see "Usage Notes" on datasheet
217 */
Geert Uytterhoeven263c8c42020-01-15 13:54:17 +0100218 usleep_range(300, 400);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000219
Kuninori Morimotof8f53e12013-01-31 09:03:11 +0000220 new = rcar_thermal_read(priv, THSSR) & CTEMP;
221 if (new == old) {
222 ctemp = new;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000223 break;
224 }
Kuninori Morimotof8f53e12013-01-31 09:03:11 +0000225 old = new;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000226 }
227
Niklas Söderlundb03628b2020-05-14 17:25:05 +0200228 if (ctemp < 0) {
Kuninori Morimotof8f53e12013-01-31 09:03:11 +0000229 dev_err(dev, "thermal sensor was broken\n");
Wei Yongjunf0e68fc2013-02-22 13:22:39 +0000230 goto err_out_unlock;
Kuninori Morimotof8f53e12013-01-31 09:03:11 +0000231 }
232
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000233 /*
234 * enable IRQ
235 */
236 if (rcar_has_irq_support(priv)) {
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +0900237 if (priv->chip->has_filonoff)
238 rcar_thermal_write(priv, FILONOFF, 0);
Kuninori Morimotof8f53e12013-01-31 09:03:11 +0000239
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000240 /* enable Rising/Falling edge interrupt */
241 rcar_thermal_write(priv, POSNEG, 0x1);
242 rcar_thermal_write(priv, INTCTRL, (((ctemp - 0) << 8) |
243 ((ctemp - 1) << 0)));
244 }
245
Wei Yongjunf0e68fc2013-02-22 13:22:39 +0000246err_out_unlock:
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000247 mutex_unlock(&priv->lock);
Niklas Söderlund57ed7372020-03-10 18:00:28 +0100248
Niklas Söderlundb03628b2020-05-14 17:25:05 +0200249 return ctemp;
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000250}
251
Kuninori Morimoto8b477ea2016-01-28 02:45:08 +0000252static int rcar_thermal_get_current_temp(struct rcar_thermal_priv *priv,
253 int *temp)
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000254{
Niklas Söderlund0fa04202020-03-10 18:00:29 +0100255 int ctemp;
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000256
Niklas Söderlund57ed7372020-03-10 18:00:28 +0100257 ctemp = rcar_thermal_update_temp(priv);
258 if (ctemp < 0)
259 return ctemp;
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000260
Niklas Söderlunddff6d4f2020-01-17 17:05:53 +0100261 /* Guaranteed operating range is -45C to 125C. */
Kuninori Morimoto5440c402015-12-15 01:18:13 +0000262
Niklas Söderlund0fa04202020-03-10 18:00:29 +0100263 if (priv->chip->ctemp_bands == 1)
264 *temp = MCELSIUS((ctemp * 5) - 65);
265 else if (ctemp < 24)
266 *temp = MCELSIUS(((ctemp * 55) - 720) / 10);
267 else
268 *temp = MCELSIUS((ctemp * 5) - 60);
Kuninori Morimoto5440c402015-12-15 01:18:13 +0000269
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000270 return 0;
271}
272
Kuninori Morimoto8b477ea2016-01-28 02:45:08 +0000273static int rcar_thermal_get_temp(struct thermal_zone_device *zone, int *temp)
274{
Daniel Lezcano5f68d072023-03-01 21:14:30 +0100275 struct rcar_thermal_priv *priv = thermal_zone_device_priv(zone);
Kuninori Morimoto8b477ea2016-01-28 02:45:08 +0000276
277 return rcar_thermal_get_current_temp(priv, temp);
278}
279
Daniel Lezcano0b6a3e42022-10-03 11:25:55 +0200280static struct thermal_zone_device_ops rcar_thermal_zone_ops = {
Daniel Lezcano2ebd4f22022-08-05 00:43:33 +0200281 .get_temp = rcar_thermal_get_temp,
Kuninori Morimoto8b477ea2016-01-28 02:45:08 +0000282};
283
Daniel Lezcano0b6a3e42022-10-03 11:25:55 +0200284static struct thermal_trip trips[] = {
285 { .type = THERMAL_TRIP_CRITICAL, .temperature = 90000 }
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000286};
287
288/*
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000289 * interrupt
290 */
291#define rcar_thermal_irq_enable(p) _rcar_thermal_irq_ctrl(p, 1)
292#define rcar_thermal_irq_disable(p) _rcar_thermal_irq_ctrl(p, 0)
293static void _rcar_thermal_irq_ctrl(struct rcar_thermal_priv *priv, int enable)
294{
295 struct rcar_thermal_common *common = priv->common;
296 unsigned long flags;
297 u32 mask = 0x3 << rcar_id_to_shift(priv); /* enable Rising/Falling */
298
Kuninori Morimotoffbcdf82015-12-15 01:17:56 +0000299 if (!rcar_has_irq_support(priv))
300 return;
301
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000302 spin_lock_irqsave(&common->lock, flags);
303
304 rcar_thermal_common_bset(common, INTMSK, mask, enable ? 0 : mask);
305
306 spin_unlock_irqrestore(&common->lock, flags);
307}
308
309static void rcar_thermal_work(struct work_struct *work)
310{
311 struct rcar_thermal_priv *priv;
Kuninori Morimotoa1ade562015-12-15 01:17:40 +0000312 int ret;
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000313
314 priv = container_of(work, struct rcar_thermal_priv, work.work);
315
Kuninori Morimotoa1ade562015-12-15 01:17:40 +0000316 ret = rcar_thermal_update_temp(priv);
317 if (ret < 0)
318 return;
319
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000320 rcar_thermal_irq_enable(priv);
Patrick Titiano94771652014-02-28 14:10:04 +0100321
Niklas Söderlund7617e772020-03-10 18:00:27 +0100322 thermal_zone_device_update(priv->zone, THERMAL_EVENT_UNSPECIFIED);
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000323}
324
325static u32 rcar_thermal_had_changed(struct rcar_thermal_priv *priv, u32 status)
326{
327 struct device *dev = rcar_priv_to_dev(priv);
328
329 status = (status >> rcar_id_to_shift(priv)) & 0x3;
330
Patrick Titiano206c0cb2014-02-28 14:10:03 +0100331 if (status) {
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000332 dev_dbg(dev, "thermal%d %s%s\n",
333 priv->id,
334 (status & 0x2) ? "Rising " : "",
335 (status & 0x1) ? "Falling" : "");
336 }
337
338 return status;
339}
340
341static irqreturn_t rcar_thermal_irq(int irq, void *data)
342{
343 struct rcar_thermal_common *common = data;
344 struct rcar_thermal_priv *priv;
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000345 u32 status, mask;
346
Tian Tao4eb7d0c2020-10-27 09:06:30 +0800347 spin_lock(&common->lock);
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000348
349 mask = rcar_thermal_common_read(common, INTMSK);
350 status = rcar_thermal_common_read(common, STR);
351 rcar_thermal_common_write(common, STR, 0x000F0F0F & mask);
352
Tian Tao4eb7d0c2020-10-27 09:06:30 +0800353 spin_unlock(&common->lock);
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000354
355 status = status & ~mask;
356
357 /*
358 * check the status
359 */
360 rcar_thermal_for_each_priv(priv, common) {
361 if (rcar_thermal_had_changed(priv, status)) {
362 rcar_thermal_irq_disable(priv);
Geert Uytterhoeven3a313862018-10-12 09:20:15 +0200363 queue_delayed_work(system_freezable_wq, &priv->work,
364 msecs_to_jiffies(300));
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000365 }
366 }
367
368 return IRQ_HANDLED;
369}
370
371/*
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000372 * platform functions
373 */
Uwe Kleine-König03a5a752023-09-27 21:37:26 +0200374static void rcar_thermal_remove(struct platform_device *pdev)
Kuninori Morimoto84f0e492015-11-10 02:12:06 +0000375{
376 struct rcar_thermal_common *common = platform_get_drvdata(pdev);
377 struct device *dev = &pdev->dev;
378 struct rcar_thermal_priv *priv;
379
380 rcar_thermal_for_each_priv(priv, common) {
Kuninori Morimotoffbcdf82015-12-15 01:17:56 +0000381 rcar_thermal_irq_disable(priv);
Geert Uytterhoeven697ee782018-10-12 09:20:16 +0200382 cancel_delayed_work_sync(&priv->work);
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +0900383 if (priv->chip->use_of_thermal)
Kuninori Morimoto64a411e2016-07-19 10:01:37 +0000384 thermal_remove_hwmon_sysfs(priv->zone);
Bui Duc Phucd4b23c5c2016-08-22 03:19:49 +0000385 else
386 thermal_zone_device_unregister(priv->zone);
Kuninori Morimoto84f0e492015-11-10 02:12:06 +0000387 }
388
389 pm_runtime_put(dev);
390 pm_runtime_disable(dev);
Kuninori Morimoto84f0e492015-11-10 02:12:06 +0000391}
392
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000393static int rcar_thermal_probe(struct platform_device *pdev)
394{
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000395 struct rcar_thermal_common *common;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000396 struct rcar_thermal_priv *priv;
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000397 struct device *dev = &pdev->dev;
Lad Prabhakar3277e022022-01-10 14:40:39 +0000398 struct resource *res;
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +0900399 const struct rcar_thermal_chip *chip = of_device_get_match_data(dev);
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000400 int mres = 0;
401 int i;
Devendra Nagafb84d992013-03-04 16:52:47 +0000402 int ret = -ENODEV;
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000403 int idle = IDLE_INTERVAL;
Yoshihiro Shimoda11313742015-01-07 10:13:10 +0900404 u32 enr_bits = 0;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000405
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000406 common = devm_kzalloc(dev, sizeof(*common), GFP_KERNEL);
Jingoo Hanb0a60d82014-05-07 15:03:25 +0900407 if (!common)
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000408 return -ENOMEM;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000409
Kuninori Morimoto84f0e492015-11-10 02:12:06 +0000410 platform_set_drvdata(pdev, common);
411
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000412 INIT_LIST_HEAD(&common->head);
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000413 spin_lock_init(&common->lock);
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000414 common->dev = dev;
415
Kuninori Morimoto51d45d22013-03-26 06:08:52 +0000416 pm_runtime_enable(dev);
417 pm_runtime_get_sync(dev);
418
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +0900419 for (i = 0; i < chip->nirqs; i++) {
Lad Prabhakar3277e022022-01-10 14:40:39 +0000420 int irq;
421
422 ret = platform_get_irq_optional(pdev, i);
423 if (ret < 0 && ret != -ENXIO)
424 goto error_unregister;
425 if (ret > 0)
426 irq = ret;
427 else
428 break;
429
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +0900430 if (!common->base) {
431 /*
432 * platform has IRQ support.
433 * Then, driver uses common registers
434 * rcar_has_irq_support() will be enabled
435 */
436 res = platform_get_resource(pdev, IORESOURCE_MEM,
437 mres++);
438 common->base = devm_ioremap_resource(dev, res);
Niklas Söderlund39056e82020-03-10 12:47:09 +0100439 if (IS_ERR(common->base)) {
440 ret = PTR_ERR(common->base);
441 goto error_unregister;
442 }
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000443
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +0900444 idle = 0; /* polling delay is not needed */
445 }
446
Lad Prabhakar3277e022022-01-10 14:40:39 +0000447 ret = devm_request_irq(dev, irq, rcar_thermal_irq,
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +0900448 IRQF_SHARED, dev_name(dev), common);
449 if (ret) {
450 dev_err(dev, "irq request failed\n ");
451 goto error_unregister;
452 }
453
454 /* update ENR bits */
455 if (chip->irq_per_ch)
456 enr_bits |= 1 << i;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000457 }
458
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000459 for (i = 0;; i++) {
460 res = platform_get_resource(pdev, IORESOURCE_MEM, mres++);
461 if (!res)
462 break;
463
464 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
465 if (!priv) {
Kuninori Morimoto1dc20822013-03-26 06:08:10 +0000466 ret = -ENOMEM;
467 goto error_unregister;
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000468 }
469
Sachin Kamat50955262013-03-04 06:45:33 +0000470 priv->base = devm_ioremap_resource(dev, res);
Kuninori Morimoto1dc20822013-03-26 06:08:10 +0000471 if (IS_ERR(priv->base)) {
472 ret = PTR_ERR(priv->base);
473 goto error_unregister;
474 }
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000475
476 priv->common = common;
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000477 priv->id = i;
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +0900478 priv->chip = chip;
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000479 mutex_init(&priv->lock);
480 INIT_LIST_HEAD(&priv->list);
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000481 INIT_DELAYED_WORK(&priv->work, rcar_thermal_work);
Kuninori Morimotoa1ade562015-12-15 01:17:40 +0000482 ret = rcar_thermal_update_temp(priv);
483 if (ret < 0)
484 goto error_unregister;
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000485
Geert Uytterhoeven392573b2020-08-19 11:27:16 +0200486 if (chip->use_of_thermal) {
Daniel Lezcano2ebd4f22022-08-05 00:43:33 +0200487 priv->zone = devm_thermal_of_zone_register(
Kuninori Morimoto8b477ea2016-01-28 02:45:08 +0000488 dev, i, priv,
Daniel Lezcano0b6a3e42022-10-03 11:25:55 +0200489 &rcar_thermal_zone_ops);
Geert Uytterhoeven392573b2020-08-19 11:27:16 +0200490 } else {
Daniel Lezcano0b6a3e42022-10-03 11:25:55 +0200491 priv->zone = thermal_zone_device_register_with_trips(
Rafael J. Wysocki4a62d582024-02-22 19:09:16 +0100492 "rcar_thermal", trips, ARRAY_SIZE(trips), priv,
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000493 &rcar_thermal_zone_ops, NULL, 0,
Kuninori Morimotoe0a51722013-01-31 09:04:48 +0000494 idle);
Andrzej Pietrasiewiczbbcf90c2020-06-29 14:29:22 +0200495
496 ret = thermal_zone_device_enable(priv->zone);
497 if (ret) {
498 thermal_zone_device_unregister(priv->zone);
499 priv->zone = ERR_PTR(ret);
500 }
501 }
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000502 if (IS_ERR(priv->zone)) {
503 dev_err(dev, "can't register thermal zone\n");
Devendra Nagafb84d992013-03-04 16:52:47 +0000504 ret = PTR_ERR(priv->zone);
Dirk Behme87260d32016-04-21 12:24:55 +0200505 priv->zone = NULL;
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000506 goto error_unregister;
507 }
508
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +0900509 if (chip->use_of_thermal) {
Kuninori Morimoto64a411e2016-07-19 10:01:37 +0000510 ret = thermal_add_hwmon_sysfs(priv->zone);
511 if (ret)
512 goto error_unregister;
513 }
514
Kuninori Morimotoffbcdf82015-12-15 01:17:56 +0000515 rcar_thermal_irq_enable(priv);
Kuninori Morimoto1dc20822013-03-26 06:08:10 +0000516
517 list_move_tail(&priv->list, &common->head);
Yoshihiro Shimoda11313742015-01-07 10:13:10 +0900518
519 /* update ENR bits */
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +0900520 if (!chip->irq_per_ch)
521 enr_bits |= 3 << (i * 8);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000522 }
523
Simon Horman542cdf42018-07-24 13:14:13 +0200524 if (common->base && enr_bits)
Yoshihiro Shimoda11313742015-01-07 10:13:10 +0900525 rcar_thermal_common_write(common, ENR, enr_bits);
526
Laurent Pinchart3db46c92013-05-14 23:00:32 +0000527 dev_info(dev, "%d sensor probed\n", i);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000528
529 return 0;
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000530
531error_unregister:
Kuninori Morimoto84f0e492015-11-10 02:12:06 +0000532 rcar_thermal_remove(pdev);
Kuninori Morimoto51d45d22013-03-26 06:08:52 +0000533
Devendra Nagafb84d992013-03-04 16:52:47 +0000534 return ret;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000535}
536
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +0900537#ifdef CONFIG_PM_SLEEP
538static int rcar_thermal_suspend(struct device *dev)
539{
540 struct rcar_thermal_common *common = dev_get_drvdata(dev);
541 struct rcar_thermal_priv *priv = list_first_entry(&common->head,
542 typeof(*priv), list);
543
544 if (priv->chip->needs_suspend_resume) {
545 rcar_thermal_common_write(common, ENR, 0);
546 rcar_thermal_irq_disable(priv);
547 rcar_thermal_bset(priv, THSCR, CPCTL, 0);
548 }
549
550 return 0;
551}
552
553static int rcar_thermal_resume(struct device *dev)
554{
555 struct rcar_thermal_common *common = dev_get_drvdata(dev);
556 struct rcar_thermal_priv *priv = list_first_entry(&common->head,
557 typeof(*priv), list);
558 int ret;
559
560 if (priv->chip->needs_suspend_resume) {
561 ret = rcar_thermal_update_temp(priv);
562 if (ret < 0)
563 return ret;
564 rcar_thermal_irq_enable(priv);
565 rcar_thermal_common_write(common, ENR, 0x03);
566 }
567
568 return 0;
569}
570#endif
571
572static SIMPLE_DEV_PM_OPS(rcar_thermal_pm_ops, rcar_thermal_suspend,
573 rcar_thermal_resume);
574
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000575static struct platform_driver rcar_thermal_driver = {
576 .driver = {
577 .name = "rcar_thermal",
Yoshihiro Kaneko1969d9d2018-05-20 18:26:17 +0900578 .pm = &rcar_thermal_pm_ops,
Kuninori Morimoto76cc1882013-01-31 09:05:26 +0000579 .of_match_table = rcar_thermal_dt_ids,
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000580 },
581 .probe = rcar_thermal_probe,
Uwe Kleine-König03a5a752023-09-27 21:37:26 +0200582 .remove_new = rcar_thermal_remove,
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000583};
584module_platform_driver(rcar_thermal_driver);
585
Kuninori Morimotoc9545792018-07-30 07:56:06 +0000586MODULE_LICENSE("GPL v2");
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000587MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver");
588MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");