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