xref: /OK3568_Linux_fs/kernel/drivers/thermal/armada_thermal.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Marvell EBU Armada SoCs thermal sensor driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2013 Marvell
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun #include <linux/device.h>
8*4882a593Smuzhiyun #include <linux/err.h>
9*4882a593Smuzhiyun #include <linux/io.h>
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/of.h>
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/delay.h>
14*4882a593Smuzhiyun #include <linux/platform_device.h>
15*4882a593Smuzhiyun #include <linux/of_device.h>
16*4882a593Smuzhiyun #include <linux/thermal.h>
17*4882a593Smuzhiyun #include <linux/iopoll.h>
18*4882a593Smuzhiyun #include <linux/mfd/syscon.h>
19*4882a593Smuzhiyun #include <linux/regmap.h>
20*4882a593Smuzhiyun #include <linux/interrupt.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include "thermal_core.h"
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /* Thermal Manager Control and Status Register */
25*4882a593Smuzhiyun #define PMU_TDC0_SW_RST_MASK		(0x1 << 1)
26*4882a593Smuzhiyun #define PMU_TM_DISABLE_OFFS		0
27*4882a593Smuzhiyun #define PMU_TM_DISABLE_MASK		(0x1 << PMU_TM_DISABLE_OFFS)
28*4882a593Smuzhiyun #define PMU_TDC0_REF_CAL_CNT_OFFS	11
29*4882a593Smuzhiyun #define PMU_TDC0_REF_CAL_CNT_MASK	(0x1ff << PMU_TDC0_REF_CAL_CNT_OFFS)
30*4882a593Smuzhiyun #define PMU_TDC0_OTF_CAL_MASK		(0x1 << 30)
31*4882a593Smuzhiyun #define PMU_TDC0_START_CAL_MASK		(0x1 << 25)
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #define A375_UNIT_CONTROL_SHIFT		27
34*4882a593Smuzhiyun #define A375_UNIT_CONTROL_MASK		0x7
35*4882a593Smuzhiyun #define A375_READOUT_INVERT		BIT(15)
36*4882a593Smuzhiyun #define A375_HW_RESETn			BIT(8)
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /* Errata fields */
39*4882a593Smuzhiyun #define CONTROL0_TSEN_TC_TRIM_MASK	0x7
40*4882a593Smuzhiyun #define CONTROL0_TSEN_TC_TRIM_VAL	0x3
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #define CONTROL0_TSEN_START		BIT(0)
43*4882a593Smuzhiyun #define CONTROL0_TSEN_RESET		BIT(1)
44*4882a593Smuzhiyun #define CONTROL0_TSEN_ENABLE		BIT(2)
45*4882a593Smuzhiyun #define CONTROL0_TSEN_AVG_BYPASS	BIT(6)
46*4882a593Smuzhiyun #define CONTROL0_TSEN_CHAN_SHIFT	13
47*4882a593Smuzhiyun #define CONTROL0_TSEN_CHAN_MASK		0xF
48*4882a593Smuzhiyun #define CONTROL0_TSEN_OSR_SHIFT		24
49*4882a593Smuzhiyun #define CONTROL0_TSEN_OSR_MAX		0x3
50*4882a593Smuzhiyun #define CONTROL0_TSEN_MODE_SHIFT	30
51*4882a593Smuzhiyun #define CONTROL0_TSEN_MODE_EXTERNAL	0x2
52*4882a593Smuzhiyun #define CONTROL0_TSEN_MODE_MASK		0x3
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun #define CONTROL1_TSEN_AVG_MASK		0x7
55*4882a593Smuzhiyun #define CONTROL1_EXT_TSEN_SW_RESET	BIT(7)
56*4882a593Smuzhiyun #define CONTROL1_EXT_TSEN_HW_RESETn	BIT(8)
57*4882a593Smuzhiyun #define CONTROL1_TSEN_INT_EN		BIT(25)
58*4882a593Smuzhiyun #define CONTROL1_TSEN_SELECT_OFF	21
59*4882a593Smuzhiyun #define CONTROL1_TSEN_SELECT_MASK	0x3
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun #define STATUS_POLL_PERIOD_US		1000
62*4882a593Smuzhiyun #define STATUS_POLL_TIMEOUT_US		100000
63*4882a593Smuzhiyun #define OVERHEAT_INT_POLL_DELAY_MS	1000
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun struct armada_thermal_data;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun /* Marvell EBU Thermal Sensor Dev Structure */
68*4882a593Smuzhiyun struct armada_thermal_priv {
69*4882a593Smuzhiyun 	struct device *dev;
70*4882a593Smuzhiyun 	struct regmap *syscon;
71*4882a593Smuzhiyun 	char zone_name[THERMAL_NAME_LENGTH];
72*4882a593Smuzhiyun 	/* serialize temperature reads/updates */
73*4882a593Smuzhiyun 	struct mutex update_lock;
74*4882a593Smuzhiyun 	struct armada_thermal_data *data;
75*4882a593Smuzhiyun 	struct thermal_zone_device *overheat_sensor;
76*4882a593Smuzhiyun 	int interrupt_source;
77*4882a593Smuzhiyun 	int current_channel;
78*4882a593Smuzhiyun 	long current_threshold;
79*4882a593Smuzhiyun 	long current_hysteresis;
80*4882a593Smuzhiyun };
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun struct armada_thermal_data {
83*4882a593Smuzhiyun 	/* Initialize the thermal IC */
84*4882a593Smuzhiyun 	void (*init)(struct platform_device *pdev,
85*4882a593Smuzhiyun 		     struct armada_thermal_priv *priv);
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	/* Formula coeficients: temp = (b - m * reg) / div */
88*4882a593Smuzhiyun 	s64 coef_b;
89*4882a593Smuzhiyun 	s64 coef_m;
90*4882a593Smuzhiyun 	u32 coef_div;
91*4882a593Smuzhiyun 	bool inverted;
92*4882a593Smuzhiyun 	bool signed_sample;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	/* Register shift and mask to access the sensor temperature */
95*4882a593Smuzhiyun 	unsigned int temp_shift;
96*4882a593Smuzhiyun 	unsigned int temp_mask;
97*4882a593Smuzhiyun 	unsigned int thresh_shift;
98*4882a593Smuzhiyun 	unsigned int hyst_shift;
99*4882a593Smuzhiyun 	unsigned int hyst_mask;
100*4882a593Smuzhiyun 	u32 is_valid_bit;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	/* Syscon access */
103*4882a593Smuzhiyun 	unsigned int syscon_control0_off;
104*4882a593Smuzhiyun 	unsigned int syscon_control1_off;
105*4882a593Smuzhiyun 	unsigned int syscon_status_off;
106*4882a593Smuzhiyun 	unsigned int dfx_irq_cause_off;
107*4882a593Smuzhiyun 	unsigned int dfx_irq_mask_off;
108*4882a593Smuzhiyun 	unsigned int dfx_overheat_irq;
109*4882a593Smuzhiyun 	unsigned int dfx_server_irq_mask_off;
110*4882a593Smuzhiyun 	unsigned int dfx_server_irq_en;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	/* One sensor is in the thermal IC, the others are in the CPUs if any */
113*4882a593Smuzhiyun 	unsigned int cpu_nr;
114*4882a593Smuzhiyun };
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun struct armada_drvdata {
117*4882a593Smuzhiyun 	enum drvtype {
118*4882a593Smuzhiyun 		LEGACY,
119*4882a593Smuzhiyun 		SYSCON
120*4882a593Smuzhiyun 	} type;
121*4882a593Smuzhiyun 	union {
122*4882a593Smuzhiyun 		struct armada_thermal_priv *priv;
123*4882a593Smuzhiyun 		struct thermal_zone_device *tz;
124*4882a593Smuzhiyun 	} data;
125*4882a593Smuzhiyun };
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun /*
128*4882a593Smuzhiyun  * struct armada_thermal_sensor - hold the information of one thermal sensor
129*4882a593Smuzhiyun  * @thermal: pointer to the local private structure
130*4882a593Smuzhiyun  * @tzd: pointer to the thermal zone device
131*4882a593Smuzhiyun  * @id: identifier of the thermal sensor
132*4882a593Smuzhiyun  */
133*4882a593Smuzhiyun struct armada_thermal_sensor {
134*4882a593Smuzhiyun 	struct armada_thermal_priv *priv;
135*4882a593Smuzhiyun 	int id;
136*4882a593Smuzhiyun };
137*4882a593Smuzhiyun 
armadaxp_init(struct platform_device * pdev,struct armada_thermal_priv * priv)138*4882a593Smuzhiyun static void armadaxp_init(struct platform_device *pdev,
139*4882a593Smuzhiyun 			  struct armada_thermal_priv *priv)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun 	struct armada_thermal_data *data = priv->data;
142*4882a593Smuzhiyun 	u32 reg;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	regmap_read(priv->syscon, data->syscon_control1_off, &reg);
145*4882a593Smuzhiyun 	reg |= PMU_TDC0_OTF_CAL_MASK;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	/* Reference calibration value */
148*4882a593Smuzhiyun 	reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
149*4882a593Smuzhiyun 	reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS);
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	/* Reset the sensor */
152*4882a593Smuzhiyun 	reg |= PMU_TDC0_SW_RST_MASK;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->syscon_control1_off, reg);
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	reg &= ~PMU_TDC0_SW_RST_MASK;
157*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->syscon_control1_off, reg);
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	/* Enable the sensor */
160*4882a593Smuzhiyun 	regmap_read(priv->syscon, data->syscon_status_off, &reg);
161*4882a593Smuzhiyun 	reg &= ~PMU_TM_DISABLE_MASK;
162*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->syscon_status_off, reg);
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun 
armada370_init(struct platform_device * pdev,struct armada_thermal_priv * priv)165*4882a593Smuzhiyun static void armada370_init(struct platform_device *pdev,
166*4882a593Smuzhiyun 			   struct armada_thermal_priv *priv)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	struct armada_thermal_data *data = priv->data;
169*4882a593Smuzhiyun 	u32 reg;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	regmap_read(priv->syscon, data->syscon_control1_off, &reg);
172*4882a593Smuzhiyun 	reg |= PMU_TDC0_OTF_CAL_MASK;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	/* Reference calibration value */
175*4882a593Smuzhiyun 	reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
176*4882a593Smuzhiyun 	reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS);
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	/* Reset the sensor */
179*4882a593Smuzhiyun 	reg &= ~PMU_TDC0_START_CAL_MASK;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->syscon_control1_off, reg);
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	msleep(10);
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun 
armada375_init(struct platform_device * pdev,struct armada_thermal_priv * priv)186*4882a593Smuzhiyun static void armada375_init(struct platform_device *pdev,
187*4882a593Smuzhiyun 			   struct armada_thermal_priv *priv)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun 	struct armada_thermal_data *data = priv->data;
190*4882a593Smuzhiyun 	u32 reg;
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	regmap_read(priv->syscon, data->syscon_control1_off, &reg);
193*4882a593Smuzhiyun 	reg &= ~(A375_UNIT_CONTROL_MASK << A375_UNIT_CONTROL_SHIFT);
194*4882a593Smuzhiyun 	reg &= ~A375_READOUT_INVERT;
195*4882a593Smuzhiyun 	reg &= ~A375_HW_RESETn;
196*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->syscon_control1_off, reg);
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	msleep(20);
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	reg |= A375_HW_RESETn;
201*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->syscon_control1_off, reg);
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	msleep(50);
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun 
armada_wait_sensor_validity(struct armada_thermal_priv * priv)206*4882a593Smuzhiyun static int armada_wait_sensor_validity(struct armada_thermal_priv *priv)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun 	u32 reg;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	return regmap_read_poll_timeout(priv->syscon,
211*4882a593Smuzhiyun 					priv->data->syscon_status_off, reg,
212*4882a593Smuzhiyun 					reg & priv->data->is_valid_bit,
213*4882a593Smuzhiyun 					STATUS_POLL_PERIOD_US,
214*4882a593Smuzhiyun 					STATUS_POLL_TIMEOUT_US);
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
armada380_init(struct platform_device * pdev,struct armada_thermal_priv * priv)217*4882a593Smuzhiyun static void armada380_init(struct platform_device *pdev,
218*4882a593Smuzhiyun 			   struct armada_thermal_priv *priv)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun 	struct armada_thermal_data *data = priv->data;
221*4882a593Smuzhiyun 	u32 reg;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	/* Disable the HW/SW reset */
224*4882a593Smuzhiyun 	regmap_read(priv->syscon, data->syscon_control1_off, &reg);
225*4882a593Smuzhiyun 	reg |= CONTROL1_EXT_TSEN_HW_RESETn;
226*4882a593Smuzhiyun 	reg &= ~CONTROL1_EXT_TSEN_SW_RESET;
227*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->syscon_control1_off, reg);
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	/* Set Tsen Tc Trim to correct default value (errata #132698) */
230*4882a593Smuzhiyun 	regmap_read(priv->syscon, data->syscon_control0_off, &reg);
231*4882a593Smuzhiyun 	reg &= ~CONTROL0_TSEN_TC_TRIM_MASK;
232*4882a593Smuzhiyun 	reg |= CONTROL0_TSEN_TC_TRIM_VAL;
233*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->syscon_control0_off, reg);
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun 
armada_ap806_init(struct platform_device * pdev,struct armada_thermal_priv * priv)236*4882a593Smuzhiyun static void armada_ap806_init(struct platform_device *pdev,
237*4882a593Smuzhiyun 			      struct armada_thermal_priv *priv)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun 	struct armada_thermal_data *data = priv->data;
240*4882a593Smuzhiyun 	u32 reg;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	regmap_read(priv->syscon, data->syscon_control0_off, &reg);
243*4882a593Smuzhiyun 	reg &= ~CONTROL0_TSEN_RESET;
244*4882a593Smuzhiyun 	reg |= CONTROL0_TSEN_START | CONTROL0_TSEN_ENABLE;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	/* Sample every ~2ms */
247*4882a593Smuzhiyun 	reg |= CONTROL0_TSEN_OSR_MAX << CONTROL0_TSEN_OSR_SHIFT;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	/* Enable average (2 samples by default) */
250*4882a593Smuzhiyun 	reg &= ~CONTROL0_TSEN_AVG_BYPASS;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->syscon_control0_off, reg);
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun 
armada_cp110_init(struct platform_device * pdev,struct armada_thermal_priv * priv)255*4882a593Smuzhiyun static void armada_cp110_init(struct platform_device *pdev,
256*4882a593Smuzhiyun 			      struct armada_thermal_priv *priv)
257*4882a593Smuzhiyun {
258*4882a593Smuzhiyun 	struct armada_thermal_data *data = priv->data;
259*4882a593Smuzhiyun 	u32 reg;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	armada380_init(pdev, priv);
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	/* Sample every ~2ms */
264*4882a593Smuzhiyun 	regmap_read(priv->syscon, data->syscon_control0_off, &reg);
265*4882a593Smuzhiyun 	reg |= CONTROL0_TSEN_OSR_MAX << CONTROL0_TSEN_OSR_SHIFT;
266*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->syscon_control0_off, reg);
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	/* Average the output value over 2^1 = 2 samples */
269*4882a593Smuzhiyun 	regmap_read(priv->syscon, data->syscon_control1_off, &reg);
270*4882a593Smuzhiyun 	reg &= ~CONTROL1_TSEN_AVG_MASK;
271*4882a593Smuzhiyun 	reg |= 1;
272*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->syscon_control1_off, reg);
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
armada_is_valid(struct armada_thermal_priv * priv)275*4882a593Smuzhiyun static bool armada_is_valid(struct armada_thermal_priv *priv)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun 	u32 reg;
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	if (!priv->data->is_valid_bit)
280*4882a593Smuzhiyun 		return true;
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	regmap_read(priv->syscon, priv->data->syscon_status_off, &reg);
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	return reg & priv->data->is_valid_bit;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun 
armada_enable_overheat_interrupt(struct armada_thermal_priv * priv)287*4882a593Smuzhiyun static void armada_enable_overheat_interrupt(struct armada_thermal_priv *priv)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun 	struct armada_thermal_data *data = priv->data;
290*4882a593Smuzhiyun 	u32 reg;
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	/* Clear DFX temperature IRQ cause */
293*4882a593Smuzhiyun 	regmap_read(priv->syscon, data->dfx_irq_cause_off, &reg);
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	/* Enable DFX Temperature IRQ */
296*4882a593Smuzhiyun 	regmap_read(priv->syscon, data->dfx_irq_mask_off, &reg);
297*4882a593Smuzhiyun 	reg |= data->dfx_overheat_irq;
298*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->dfx_irq_mask_off, reg);
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	/* Enable DFX server IRQ */
301*4882a593Smuzhiyun 	regmap_read(priv->syscon, data->dfx_server_irq_mask_off, &reg);
302*4882a593Smuzhiyun 	reg |= data->dfx_server_irq_en;
303*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->dfx_server_irq_mask_off, reg);
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	/* Enable overheat interrupt */
306*4882a593Smuzhiyun 	regmap_read(priv->syscon, data->syscon_control1_off, &reg);
307*4882a593Smuzhiyun 	reg |= CONTROL1_TSEN_INT_EN;
308*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->syscon_control1_off, reg);
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun static void __maybe_unused
armada_disable_overheat_interrupt(struct armada_thermal_priv * priv)312*4882a593Smuzhiyun armada_disable_overheat_interrupt(struct armada_thermal_priv *priv)
313*4882a593Smuzhiyun {
314*4882a593Smuzhiyun 	struct armada_thermal_data *data = priv->data;
315*4882a593Smuzhiyun 	u32 reg;
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	regmap_read(priv->syscon, data->syscon_control1_off, &reg);
318*4882a593Smuzhiyun 	reg &= ~CONTROL1_TSEN_INT_EN;
319*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->syscon_control1_off, reg);
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun /* There is currently no board with more than one sensor per channel */
armada_select_channel(struct armada_thermal_priv * priv,int channel)323*4882a593Smuzhiyun static int armada_select_channel(struct armada_thermal_priv *priv, int channel)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun 	struct armada_thermal_data *data = priv->data;
326*4882a593Smuzhiyun 	u32 ctrl0;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	if (channel < 0 || channel > priv->data->cpu_nr)
329*4882a593Smuzhiyun 		return -EINVAL;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	if (priv->current_channel == channel)
332*4882a593Smuzhiyun 		return 0;
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	/* Stop the measurements */
335*4882a593Smuzhiyun 	regmap_read(priv->syscon, data->syscon_control0_off, &ctrl0);
336*4882a593Smuzhiyun 	ctrl0 &= ~CONTROL0_TSEN_START;
337*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->syscon_control0_off, ctrl0);
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	/* Reset the mode, internal sensor will be automatically selected */
340*4882a593Smuzhiyun 	ctrl0 &= ~(CONTROL0_TSEN_MODE_MASK << CONTROL0_TSEN_MODE_SHIFT);
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	/* Other channels are external and should be selected accordingly */
343*4882a593Smuzhiyun 	if (channel) {
344*4882a593Smuzhiyun 		/* Change the mode to external */
345*4882a593Smuzhiyun 		ctrl0 |= CONTROL0_TSEN_MODE_EXTERNAL <<
346*4882a593Smuzhiyun 			 CONTROL0_TSEN_MODE_SHIFT;
347*4882a593Smuzhiyun 		/* Select the sensor */
348*4882a593Smuzhiyun 		ctrl0 &= ~(CONTROL0_TSEN_CHAN_MASK << CONTROL0_TSEN_CHAN_SHIFT);
349*4882a593Smuzhiyun 		ctrl0 |= (channel - 1) << CONTROL0_TSEN_CHAN_SHIFT;
350*4882a593Smuzhiyun 	}
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	/* Actually set the mode/channel */
353*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->syscon_control0_off, ctrl0);
354*4882a593Smuzhiyun 	priv->current_channel = channel;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	/* Re-start the measurements */
357*4882a593Smuzhiyun 	ctrl0 |= CONTROL0_TSEN_START;
358*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->syscon_control0_off, ctrl0);
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	/*
361*4882a593Smuzhiyun 	 * The IP has a latency of ~15ms, so after updating the selected source,
362*4882a593Smuzhiyun 	 * we must absolutely wait for the sensor validity bit to ensure we read
363*4882a593Smuzhiyun 	 * actual data.
364*4882a593Smuzhiyun 	 */
365*4882a593Smuzhiyun 	if (armada_wait_sensor_validity(priv)) {
366*4882a593Smuzhiyun 		dev_err(priv->dev,
367*4882a593Smuzhiyun 			"Temperature sensor reading not valid\n");
368*4882a593Smuzhiyun 		return -EIO;
369*4882a593Smuzhiyun 	}
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	return 0;
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun 
armada_read_sensor(struct armada_thermal_priv * priv,int * temp)374*4882a593Smuzhiyun static int armada_read_sensor(struct armada_thermal_priv *priv, int *temp)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun 	u32 reg, div;
377*4882a593Smuzhiyun 	s64 sample, b, m;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	regmap_read(priv->syscon, priv->data->syscon_status_off, &reg);
380*4882a593Smuzhiyun 	reg = (reg >> priv->data->temp_shift) & priv->data->temp_mask;
381*4882a593Smuzhiyun 	if (priv->data->signed_sample)
382*4882a593Smuzhiyun 		/* The most significant bit is the sign bit */
383*4882a593Smuzhiyun 		sample = sign_extend32(reg, fls(priv->data->temp_mask) - 1);
384*4882a593Smuzhiyun 	else
385*4882a593Smuzhiyun 		sample = reg;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	/* Get formula coeficients */
388*4882a593Smuzhiyun 	b = priv->data->coef_b;
389*4882a593Smuzhiyun 	m = priv->data->coef_m;
390*4882a593Smuzhiyun 	div = priv->data->coef_div;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	if (priv->data->inverted)
393*4882a593Smuzhiyun 		*temp = div_s64((m * sample) - b, div);
394*4882a593Smuzhiyun 	else
395*4882a593Smuzhiyun 		*temp = div_s64(b - (m * sample), div);
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	return 0;
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun 
armada_get_temp_legacy(struct thermal_zone_device * thermal,int * temp)400*4882a593Smuzhiyun static int armada_get_temp_legacy(struct thermal_zone_device *thermal,
401*4882a593Smuzhiyun 				  int *temp)
402*4882a593Smuzhiyun {
403*4882a593Smuzhiyun 	struct armada_thermal_priv *priv = thermal->devdata;
404*4882a593Smuzhiyun 	int ret;
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	/* Valid check */
407*4882a593Smuzhiyun 	if (!armada_is_valid(priv)) {
408*4882a593Smuzhiyun 		dev_err(priv->dev,
409*4882a593Smuzhiyun 			"Temperature sensor reading not valid\n");
410*4882a593Smuzhiyun 		return -EIO;
411*4882a593Smuzhiyun 	}
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	/* Do the actual reading */
414*4882a593Smuzhiyun 	ret = armada_read_sensor(priv, temp);
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	return ret;
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun static struct thermal_zone_device_ops legacy_ops = {
420*4882a593Smuzhiyun 	.get_temp = armada_get_temp_legacy,
421*4882a593Smuzhiyun };
422*4882a593Smuzhiyun 
armada_get_temp(void * _sensor,int * temp)423*4882a593Smuzhiyun static int armada_get_temp(void *_sensor, int *temp)
424*4882a593Smuzhiyun {
425*4882a593Smuzhiyun 	struct armada_thermal_sensor *sensor = _sensor;
426*4882a593Smuzhiyun 	struct armada_thermal_priv *priv = sensor->priv;
427*4882a593Smuzhiyun 	int ret;
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	mutex_lock(&priv->update_lock);
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	/* Select the desired channel */
432*4882a593Smuzhiyun 	ret = armada_select_channel(priv, sensor->id);
433*4882a593Smuzhiyun 	if (ret)
434*4882a593Smuzhiyun 		goto unlock_mutex;
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	/* Do the actual reading */
437*4882a593Smuzhiyun 	ret = armada_read_sensor(priv, temp);
438*4882a593Smuzhiyun 	if (ret)
439*4882a593Smuzhiyun 		goto unlock_mutex;
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	/*
442*4882a593Smuzhiyun 	 * Select back the interrupt source channel from which a potential
443*4882a593Smuzhiyun 	 * critical trip point has been set.
444*4882a593Smuzhiyun 	 */
445*4882a593Smuzhiyun 	ret = armada_select_channel(priv, priv->interrupt_source);
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun unlock_mutex:
448*4882a593Smuzhiyun 	mutex_unlock(&priv->update_lock);
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	return ret;
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun static const struct thermal_zone_of_device_ops of_ops = {
454*4882a593Smuzhiyun 	.get_temp = armada_get_temp,
455*4882a593Smuzhiyun };
456*4882a593Smuzhiyun 
armada_mc_to_reg_temp(struct armada_thermal_data * data,unsigned int temp_mc)457*4882a593Smuzhiyun static unsigned int armada_mc_to_reg_temp(struct armada_thermal_data *data,
458*4882a593Smuzhiyun 					  unsigned int temp_mc)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun 	s64 b = data->coef_b;
461*4882a593Smuzhiyun 	s64 m = data->coef_m;
462*4882a593Smuzhiyun 	s64 div = data->coef_div;
463*4882a593Smuzhiyun 	unsigned int sample;
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	if (data->inverted)
466*4882a593Smuzhiyun 		sample = div_s64(((temp_mc * div) + b), m);
467*4882a593Smuzhiyun 	else
468*4882a593Smuzhiyun 		sample = div_s64((b - (temp_mc * div)), m);
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 	return sample & data->temp_mask;
471*4882a593Smuzhiyun }
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun /*
474*4882a593Smuzhiyun  * The documentation states:
475*4882a593Smuzhiyun  * high/low watermark = threshold +/- 0.4761 * 2^(hysteresis + 2)
476*4882a593Smuzhiyun  * which is the mathematical derivation for:
477*4882a593Smuzhiyun  * 0x0 <=> 1.9°C, 0x1 <=> 3.8°C, 0x2 <=> 7.6°C, 0x3 <=> 15.2°C
478*4882a593Smuzhiyun  */
479*4882a593Smuzhiyun static unsigned int hyst_levels_mc[] = {1900, 3800, 7600, 15200};
480*4882a593Smuzhiyun 
armada_mc_to_reg_hyst(struct armada_thermal_data * data,unsigned int hyst_mc)481*4882a593Smuzhiyun static unsigned int armada_mc_to_reg_hyst(struct armada_thermal_data *data,
482*4882a593Smuzhiyun 					  unsigned int hyst_mc)
483*4882a593Smuzhiyun {
484*4882a593Smuzhiyun 	int i;
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	/*
487*4882a593Smuzhiyun 	 * We will always take the smallest possible hysteresis to avoid risking
488*4882a593Smuzhiyun 	 * the hardware integrity by enlarging the threshold by +8°C in the
489*4882a593Smuzhiyun 	 * worst case.
490*4882a593Smuzhiyun 	 */
491*4882a593Smuzhiyun 	for (i = ARRAY_SIZE(hyst_levels_mc) - 1; i > 0; i--)
492*4882a593Smuzhiyun 		if (hyst_mc >= hyst_levels_mc[i])
493*4882a593Smuzhiyun 			break;
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	return i & data->hyst_mask;
496*4882a593Smuzhiyun }
497*4882a593Smuzhiyun 
armada_set_overheat_thresholds(struct armada_thermal_priv * priv,int thresh_mc,int hyst_mc)498*4882a593Smuzhiyun static void armada_set_overheat_thresholds(struct armada_thermal_priv *priv,
499*4882a593Smuzhiyun 					   int thresh_mc, int hyst_mc)
500*4882a593Smuzhiyun {
501*4882a593Smuzhiyun 	struct armada_thermal_data *data = priv->data;
502*4882a593Smuzhiyun 	unsigned int threshold = armada_mc_to_reg_temp(data, thresh_mc);
503*4882a593Smuzhiyun 	unsigned int hysteresis = armada_mc_to_reg_hyst(data, hyst_mc);
504*4882a593Smuzhiyun 	u32 ctrl1;
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	regmap_read(priv->syscon, data->syscon_control1_off, &ctrl1);
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	/* Set Threshold */
509*4882a593Smuzhiyun 	if (thresh_mc >= 0) {
510*4882a593Smuzhiyun 		ctrl1 &= ~(data->temp_mask << data->thresh_shift);
511*4882a593Smuzhiyun 		ctrl1 |= threshold << data->thresh_shift;
512*4882a593Smuzhiyun 		priv->current_threshold = thresh_mc;
513*4882a593Smuzhiyun 	}
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	/* Set Hysteresis */
516*4882a593Smuzhiyun 	if (hyst_mc >= 0) {
517*4882a593Smuzhiyun 		ctrl1 &= ~(data->hyst_mask << data->hyst_shift);
518*4882a593Smuzhiyun 		ctrl1 |= hysteresis << data->hyst_shift;
519*4882a593Smuzhiyun 		priv->current_hysteresis = hyst_mc;
520*4882a593Smuzhiyun 	}
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	regmap_write(priv->syscon, data->syscon_control1_off, ctrl1);
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun 
armada_overheat_isr(int irq,void * blob)525*4882a593Smuzhiyun static irqreturn_t armada_overheat_isr(int irq, void *blob)
526*4882a593Smuzhiyun {
527*4882a593Smuzhiyun 	/*
528*4882a593Smuzhiyun 	 * Disable the IRQ and continue in thread context (thermal core
529*4882a593Smuzhiyun 	 * notification and temperature monitoring).
530*4882a593Smuzhiyun 	 */
531*4882a593Smuzhiyun 	disable_irq_nosync(irq);
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	return IRQ_WAKE_THREAD;
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun 
armada_overheat_isr_thread(int irq,void * blob)536*4882a593Smuzhiyun static irqreturn_t armada_overheat_isr_thread(int irq, void *blob)
537*4882a593Smuzhiyun {
538*4882a593Smuzhiyun 	struct armada_thermal_priv *priv = blob;
539*4882a593Smuzhiyun 	int low_threshold = priv->current_threshold - priv->current_hysteresis;
540*4882a593Smuzhiyun 	int temperature;
541*4882a593Smuzhiyun 	u32 dummy;
542*4882a593Smuzhiyun 	int ret;
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	/* Notify the core in thread context */
545*4882a593Smuzhiyun 	thermal_zone_device_update(priv->overheat_sensor,
546*4882a593Smuzhiyun 				   THERMAL_EVENT_UNSPECIFIED);
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	/*
549*4882a593Smuzhiyun 	 * The overheat interrupt must be cleared by reading the DFX interrupt
550*4882a593Smuzhiyun 	 * cause _after_ the temperature has fallen down to the low threshold.
551*4882a593Smuzhiyun 	 * Otherwise future interrupts might not be served.
552*4882a593Smuzhiyun 	 */
553*4882a593Smuzhiyun 	do {
554*4882a593Smuzhiyun 		msleep(OVERHEAT_INT_POLL_DELAY_MS);
555*4882a593Smuzhiyun 		mutex_lock(&priv->update_lock);
556*4882a593Smuzhiyun 		ret = armada_read_sensor(priv, &temperature);
557*4882a593Smuzhiyun 		mutex_unlock(&priv->update_lock);
558*4882a593Smuzhiyun 		if (ret)
559*4882a593Smuzhiyun 			goto enable_irq;
560*4882a593Smuzhiyun 	} while (temperature >= low_threshold);
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	regmap_read(priv->syscon, priv->data->dfx_irq_cause_off, &dummy);
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	/* Notify the thermal core that the temperature is acceptable again */
565*4882a593Smuzhiyun 	thermal_zone_device_update(priv->overheat_sensor,
566*4882a593Smuzhiyun 				   THERMAL_EVENT_UNSPECIFIED);
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun enable_irq:
569*4882a593Smuzhiyun 	enable_irq(irq);
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 	return IRQ_HANDLED;
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun static const struct armada_thermal_data armadaxp_data = {
575*4882a593Smuzhiyun 	.init = armadaxp_init,
576*4882a593Smuzhiyun 	.temp_shift = 10,
577*4882a593Smuzhiyun 	.temp_mask = 0x1ff,
578*4882a593Smuzhiyun 	.coef_b = 3153000000ULL,
579*4882a593Smuzhiyun 	.coef_m = 10000000ULL,
580*4882a593Smuzhiyun 	.coef_div = 13825,
581*4882a593Smuzhiyun 	.syscon_status_off = 0xb0,
582*4882a593Smuzhiyun 	.syscon_control1_off = 0x2d0,
583*4882a593Smuzhiyun };
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun static const struct armada_thermal_data armada370_data = {
586*4882a593Smuzhiyun 	.init = armada370_init,
587*4882a593Smuzhiyun 	.is_valid_bit = BIT(9),
588*4882a593Smuzhiyun 	.temp_shift = 10,
589*4882a593Smuzhiyun 	.temp_mask = 0x1ff,
590*4882a593Smuzhiyun 	.coef_b = 3153000000ULL,
591*4882a593Smuzhiyun 	.coef_m = 10000000ULL,
592*4882a593Smuzhiyun 	.coef_div = 13825,
593*4882a593Smuzhiyun 	.syscon_status_off = 0x0,
594*4882a593Smuzhiyun 	.syscon_control1_off = 0x4,
595*4882a593Smuzhiyun };
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun static const struct armada_thermal_data armada375_data = {
598*4882a593Smuzhiyun 	.init = armada375_init,
599*4882a593Smuzhiyun 	.is_valid_bit = BIT(10),
600*4882a593Smuzhiyun 	.temp_shift = 0,
601*4882a593Smuzhiyun 	.temp_mask = 0x1ff,
602*4882a593Smuzhiyun 	.coef_b = 3171900000ULL,
603*4882a593Smuzhiyun 	.coef_m = 10000000ULL,
604*4882a593Smuzhiyun 	.coef_div = 13616,
605*4882a593Smuzhiyun 	.syscon_status_off = 0x78,
606*4882a593Smuzhiyun 	.syscon_control0_off = 0x7c,
607*4882a593Smuzhiyun 	.syscon_control1_off = 0x80,
608*4882a593Smuzhiyun };
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun static const struct armada_thermal_data armada380_data = {
611*4882a593Smuzhiyun 	.init = armada380_init,
612*4882a593Smuzhiyun 	.is_valid_bit = BIT(10),
613*4882a593Smuzhiyun 	.temp_shift = 0,
614*4882a593Smuzhiyun 	.temp_mask = 0x3ff,
615*4882a593Smuzhiyun 	.coef_b = 1172499100ULL,
616*4882a593Smuzhiyun 	.coef_m = 2000096ULL,
617*4882a593Smuzhiyun 	.coef_div = 4201,
618*4882a593Smuzhiyun 	.inverted = true,
619*4882a593Smuzhiyun 	.syscon_control0_off = 0x70,
620*4882a593Smuzhiyun 	.syscon_control1_off = 0x74,
621*4882a593Smuzhiyun 	.syscon_status_off = 0x78,
622*4882a593Smuzhiyun };
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun static const struct armada_thermal_data armada_ap806_data = {
625*4882a593Smuzhiyun 	.init = armada_ap806_init,
626*4882a593Smuzhiyun 	.is_valid_bit = BIT(16),
627*4882a593Smuzhiyun 	.temp_shift = 0,
628*4882a593Smuzhiyun 	.temp_mask = 0x3ff,
629*4882a593Smuzhiyun 	.thresh_shift = 3,
630*4882a593Smuzhiyun 	.hyst_shift = 19,
631*4882a593Smuzhiyun 	.hyst_mask = 0x3,
632*4882a593Smuzhiyun 	.coef_b = -150000LL,
633*4882a593Smuzhiyun 	.coef_m = 423ULL,
634*4882a593Smuzhiyun 	.coef_div = 1,
635*4882a593Smuzhiyun 	.inverted = true,
636*4882a593Smuzhiyun 	.signed_sample = true,
637*4882a593Smuzhiyun 	.syscon_control0_off = 0x84,
638*4882a593Smuzhiyun 	.syscon_control1_off = 0x88,
639*4882a593Smuzhiyun 	.syscon_status_off = 0x8C,
640*4882a593Smuzhiyun 	.dfx_irq_cause_off = 0x108,
641*4882a593Smuzhiyun 	.dfx_irq_mask_off = 0x10C,
642*4882a593Smuzhiyun 	.dfx_overheat_irq = BIT(22),
643*4882a593Smuzhiyun 	.dfx_server_irq_mask_off = 0x104,
644*4882a593Smuzhiyun 	.dfx_server_irq_en = BIT(1),
645*4882a593Smuzhiyun 	.cpu_nr = 4,
646*4882a593Smuzhiyun };
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun static const struct armada_thermal_data armada_cp110_data = {
649*4882a593Smuzhiyun 	.init = armada_cp110_init,
650*4882a593Smuzhiyun 	.is_valid_bit = BIT(10),
651*4882a593Smuzhiyun 	.temp_shift = 0,
652*4882a593Smuzhiyun 	.temp_mask = 0x3ff,
653*4882a593Smuzhiyun 	.thresh_shift = 16,
654*4882a593Smuzhiyun 	.hyst_shift = 26,
655*4882a593Smuzhiyun 	.hyst_mask = 0x3,
656*4882a593Smuzhiyun 	.coef_b = 1172499100ULL,
657*4882a593Smuzhiyun 	.coef_m = 2000096ULL,
658*4882a593Smuzhiyun 	.coef_div = 4201,
659*4882a593Smuzhiyun 	.inverted = true,
660*4882a593Smuzhiyun 	.syscon_control0_off = 0x70,
661*4882a593Smuzhiyun 	.syscon_control1_off = 0x74,
662*4882a593Smuzhiyun 	.syscon_status_off = 0x78,
663*4882a593Smuzhiyun 	.dfx_irq_cause_off = 0x108,
664*4882a593Smuzhiyun 	.dfx_irq_mask_off = 0x10C,
665*4882a593Smuzhiyun 	.dfx_overheat_irq = BIT(20),
666*4882a593Smuzhiyun 	.dfx_server_irq_mask_off = 0x104,
667*4882a593Smuzhiyun 	.dfx_server_irq_en = BIT(1),
668*4882a593Smuzhiyun };
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun static const struct of_device_id armada_thermal_id_table[] = {
671*4882a593Smuzhiyun 	{
672*4882a593Smuzhiyun 		.compatible = "marvell,armadaxp-thermal",
673*4882a593Smuzhiyun 		.data       = &armadaxp_data,
674*4882a593Smuzhiyun 	},
675*4882a593Smuzhiyun 	{
676*4882a593Smuzhiyun 		.compatible = "marvell,armada370-thermal",
677*4882a593Smuzhiyun 		.data       = &armada370_data,
678*4882a593Smuzhiyun 	},
679*4882a593Smuzhiyun 	{
680*4882a593Smuzhiyun 		.compatible = "marvell,armada375-thermal",
681*4882a593Smuzhiyun 		.data       = &armada375_data,
682*4882a593Smuzhiyun 	},
683*4882a593Smuzhiyun 	{
684*4882a593Smuzhiyun 		.compatible = "marvell,armada380-thermal",
685*4882a593Smuzhiyun 		.data       = &armada380_data,
686*4882a593Smuzhiyun 	},
687*4882a593Smuzhiyun 	{
688*4882a593Smuzhiyun 		.compatible = "marvell,armada-ap806-thermal",
689*4882a593Smuzhiyun 		.data       = &armada_ap806_data,
690*4882a593Smuzhiyun 	},
691*4882a593Smuzhiyun 	{
692*4882a593Smuzhiyun 		.compatible = "marvell,armada-cp110-thermal",
693*4882a593Smuzhiyun 		.data       = &armada_cp110_data,
694*4882a593Smuzhiyun 	},
695*4882a593Smuzhiyun 	{
696*4882a593Smuzhiyun 		/* sentinel */
697*4882a593Smuzhiyun 	},
698*4882a593Smuzhiyun };
699*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, armada_thermal_id_table);
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun static const struct regmap_config armada_thermal_regmap_config = {
702*4882a593Smuzhiyun 	.reg_bits = 32,
703*4882a593Smuzhiyun 	.reg_stride = 4,
704*4882a593Smuzhiyun 	.val_bits = 32,
705*4882a593Smuzhiyun 	.fast_io = true,
706*4882a593Smuzhiyun };
707*4882a593Smuzhiyun 
armada_thermal_probe_legacy(struct platform_device * pdev,struct armada_thermal_priv * priv)708*4882a593Smuzhiyun static int armada_thermal_probe_legacy(struct platform_device *pdev,
709*4882a593Smuzhiyun 				       struct armada_thermal_priv *priv)
710*4882a593Smuzhiyun {
711*4882a593Smuzhiyun 	struct armada_thermal_data *data = priv->data;
712*4882a593Smuzhiyun 	struct resource *res;
713*4882a593Smuzhiyun 	void __iomem *base;
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 	/* First memory region points towards the status register */
716*4882a593Smuzhiyun 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
717*4882a593Smuzhiyun 	base = devm_ioremap_resource(&pdev->dev, res);
718*4882a593Smuzhiyun 	if (IS_ERR(base))
719*4882a593Smuzhiyun 		return PTR_ERR(base);
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun 	/*
722*4882a593Smuzhiyun 	 * Fix up from the old individual DT register specification to
723*4882a593Smuzhiyun 	 * cover all the registers.  We do this by adjusting the ioremap()
724*4882a593Smuzhiyun 	 * result, which should be fine as ioremap() deals with pages.
725*4882a593Smuzhiyun 	 * However, validate that we do not cross a page boundary while
726*4882a593Smuzhiyun 	 * making this adjustment.
727*4882a593Smuzhiyun 	 */
728*4882a593Smuzhiyun 	if (((unsigned long)base & ~PAGE_MASK) < data->syscon_status_off)
729*4882a593Smuzhiyun 		return -EINVAL;
730*4882a593Smuzhiyun 	base -= data->syscon_status_off;
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 	priv->syscon = devm_regmap_init_mmio(&pdev->dev, base,
733*4882a593Smuzhiyun 					     &armada_thermal_regmap_config);
734*4882a593Smuzhiyun 	return PTR_ERR_OR_ZERO(priv->syscon);
735*4882a593Smuzhiyun }
736*4882a593Smuzhiyun 
armada_thermal_probe_syscon(struct platform_device * pdev,struct armada_thermal_priv * priv)737*4882a593Smuzhiyun static int armada_thermal_probe_syscon(struct platform_device *pdev,
738*4882a593Smuzhiyun 				       struct armada_thermal_priv *priv)
739*4882a593Smuzhiyun {
740*4882a593Smuzhiyun 	priv->syscon = syscon_node_to_regmap(pdev->dev.parent->of_node);
741*4882a593Smuzhiyun 	return PTR_ERR_OR_ZERO(priv->syscon);
742*4882a593Smuzhiyun }
743*4882a593Smuzhiyun 
armada_set_sane_name(struct platform_device * pdev,struct armada_thermal_priv * priv)744*4882a593Smuzhiyun static void armada_set_sane_name(struct platform_device *pdev,
745*4882a593Smuzhiyun 				 struct armada_thermal_priv *priv)
746*4882a593Smuzhiyun {
747*4882a593Smuzhiyun 	const char *name = dev_name(&pdev->dev);
748*4882a593Smuzhiyun 	char *insane_char;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	if (strlen(name) > THERMAL_NAME_LENGTH) {
751*4882a593Smuzhiyun 		/*
752*4882a593Smuzhiyun 		 * When inside a system controller, the device name has the
753*4882a593Smuzhiyun 		 * form: f06f8000.system-controller:ap-thermal so stripping
754*4882a593Smuzhiyun 		 * after the ':' should give us a shorter but meaningful name.
755*4882a593Smuzhiyun 		 */
756*4882a593Smuzhiyun 		name = strrchr(name, ':');
757*4882a593Smuzhiyun 		if (!name)
758*4882a593Smuzhiyun 			name = "armada_thermal";
759*4882a593Smuzhiyun 		else
760*4882a593Smuzhiyun 			name++;
761*4882a593Smuzhiyun 	}
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun 	/* Save the name locally */
764*4882a593Smuzhiyun 	strncpy(priv->zone_name, name, THERMAL_NAME_LENGTH - 1);
765*4882a593Smuzhiyun 	priv->zone_name[THERMAL_NAME_LENGTH - 1] = '\0';
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 	/* Then check there are no '-' or hwmon core will complain */
768*4882a593Smuzhiyun 	do {
769*4882a593Smuzhiyun 		insane_char = strpbrk(priv->zone_name, "-");
770*4882a593Smuzhiyun 		if (insane_char)
771*4882a593Smuzhiyun 			*insane_char = '_';
772*4882a593Smuzhiyun 	} while (insane_char);
773*4882a593Smuzhiyun }
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun /*
776*4882a593Smuzhiyun  * The IP can manage to trigger interrupts on overheat situation from all the
777*4882a593Smuzhiyun  * sensors. However, the interrupt source changes along with the last selected
778*4882a593Smuzhiyun  * source (ie. the last read sensor), which is an inconsistent behavior. Avoid
779*4882a593Smuzhiyun  * possible glitches by always selecting back only one channel (arbitrarily: the
780*4882a593Smuzhiyun  * first in the DT which has a critical trip point). We also disable sensor
781*4882a593Smuzhiyun  * switch during overheat situations.
782*4882a593Smuzhiyun  */
armada_configure_overheat_int(struct armada_thermal_priv * priv,struct thermal_zone_device * tz,int sensor_id)783*4882a593Smuzhiyun static int armada_configure_overheat_int(struct armada_thermal_priv *priv,
784*4882a593Smuzhiyun 					 struct thermal_zone_device *tz,
785*4882a593Smuzhiyun 					 int sensor_id)
786*4882a593Smuzhiyun {
787*4882a593Smuzhiyun 	/* Retrieve the critical trip point to enable the overheat interrupt */
788*4882a593Smuzhiyun 	const struct thermal_trip *trips = of_thermal_get_trip_points(tz);
789*4882a593Smuzhiyun 	int ret;
790*4882a593Smuzhiyun 	int i;
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	if (!trips)
793*4882a593Smuzhiyun 		return -EINVAL;
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 	for (i = 0; i < of_thermal_get_ntrips(tz); i++)
796*4882a593Smuzhiyun 		if (trips[i].type == THERMAL_TRIP_CRITICAL)
797*4882a593Smuzhiyun 			break;
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 	if (i == of_thermal_get_ntrips(tz))
800*4882a593Smuzhiyun 		return -EINVAL;
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	ret = armada_select_channel(priv, sensor_id);
803*4882a593Smuzhiyun 	if (ret)
804*4882a593Smuzhiyun 		return ret;
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun 	armada_set_overheat_thresholds(priv,
807*4882a593Smuzhiyun 				       trips[i].temperature,
808*4882a593Smuzhiyun 				       trips[i].hysteresis);
809*4882a593Smuzhiyun 	priv->overheat_sensor = tz;
810*4882a593Smuzhiyun 	priv->interrupt_source = sensor_id;
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	armada_enable_overheat_interrupt(priv);
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	return 0;
815*4882a593Smuzhiyun }
816*4882a593Smuzhiyun 
armada_thermal_probe(struct platform_device * pdev)817*4882a593Smuzhiyun static int armada_thermal_probe(struct platform_device *pdev)
818*4882a593Smuzhiyun {
819*4882a593Smuzhiyun 	struct thermal_zone_device *tz;
820*4882a593Smuzhiyun 	struct armada_thermal_sensor *sensor;
821*4882a593Smuzhiyun 	struct armada_drvdata *drvdata;
822*4882a593Smuzhiyun 	const struct of_device_id *match;
823*4882a593Smuzhiyun 	struct armada_thermal_priv *priv;
824*4882a593Smuzhiyun 	int sensor_id, irq;
825*4882a593Smuzhiyun 	int ret;
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 	match = of_match_device(armada_thermal_id_table, &pdev->dev);
828*4882a593Smuzhiyun 	if (!match)
829*4882a593Smuzhiyun 		return -ENODEV;
830*4882a593Smuzhiyun 
831*4882a593Smuzhiyun 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
832*4882a593Smuzhiyun 	if (!priv)
833*4882a593Smuzhiyun 		return -ENOMEM;
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
836*4882a593Smuzhiyun 	if (!drvdata)
837*4882a593Smuzhiyun 		return -ENOMEM;
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 	priv->dev = &pdev->dev;
840*4882a593Smuzhiyun 	priv->data = (struct armada_thermal_data *)match->data;
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	mutex_init(&priv->update_lock);
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	/*
845*4882a593Smuzhiyun 	 * Legacy DT bindings only described "control1" register (also referred
846*4882a593Smuzhiyun 	 * as "control MSB" on old documentation). Then, bindings moved to cover
847*4882a593Smuzhiyun 	 * "control0/control LSB" and "control1/control MSB" registers within
848*4882a593Smuzhiyun 	 * the same resource, which was then of size 8 instead of 4.
849*4882a593Smuzhiyun 	 *
850*4882a593Smuzhiyun 	 * The logic of defining sporadic registers is broken. For instance, it
851*4882a593Smuzhiyun 	 * blocked the addition of the overheat interrupt feature that needed
852*4882a593Smuzhiyun 	 * another resource somewhere else in the same memory area. One solution
853*4882a593Smuzhiyun 	 * is to define an overall system controller and put the thermal node
854*4882a593Smuzhiyun 	 * into it, which requires the use of regmaps across all the driver.
855*4882a593Smuzhiyun 	 */
856*4882a593Smuzhiyun 	if (IS_ERR(syscon_node_to_regmap(pdev->dev.parent->of_node))) {
857*4882a593Smuzhiyun 		/* Ensure device name is correct for the thermal core */
858*4882a593Smuzhiyun 		armada_set_sane_name(pdev, priv);
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun 		ret = armada_thermal_probe_legacy(pdev, priv);
861*4882a593Smuzhiyun 		if (ret)
862*4882a593Smuzhiyun 			return ret;
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 		priv->data->init(pdev, priv);
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 		/* Wait the sensors to be valid */
867*4882a593Smuzhiyun 		armada_wait_sensor_validity(priv);
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 		tz = thermal_zone_device_register(priv->zone_name, 0, 0, priv,
870*4882a593Smuzhiyun 						  &legacy_ops, NULL, 0, 0);
871*4882a593Smuzhiyun 		if (IS_ERR(tz)) {
872*4882a593Smuzhiyun 			dev_err(&pdev->dev,
873*4882a593Smuzhiyun 				"Failed to register thermal zone device\n");
874*4882a593Smuzhiyun 			return PTR_ERR(tz);
875*4882a593Smuzhiyun 		}
876*4882a593Smuzhiyun 
877*4882a593Smuzhiyun 		ret = thermal_zone_device_enable(tz);
878*4882a593Smuzhiyun 		if (ret) {
879*4882a593Smuzhiyun 			thermal_zone_device_unregister(tz);
880*4882a593Smuzhiyun 			return ret;
881*4882a593Smuzhiyun 		}
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 		drvdata->type = LEGACY;
884*4882a593Smuzhiyun 		drvdata->data.tz = tz;
885*4882a593Smuzhiyun 		platform_set_drvdata(pdev, drvdata);
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 		return 0;
888*4882a593Smuzhiyun 	}
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 	ret = armada_thermal_probe_syscon(pdev, priv);
891*4882a593Smuzhiyun 	if (ret)
892*4882a593Smuzhiyun 		return ret;
893*4882a593Smuzhiyun 
894*4882a593Smuzhiyun 	priv->current_channel = -1;
895*4882a593Smuzhiyun 	priv->data->init(pdev, priv);
896*4882a593Smuzhiyun 	drvdata->type = SYSCON;
897*4882a593Smuzhiyun 	drvdata->data.priv = priv;
898*4882a593Smuzhiyun 	platform_set_drvdata(pdev, drvdata);
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun 	irq = platform_get_irq(pdev, 0);
901*4882a593Smuzhiyun 	if (irq == -EPROBE_DEFER)
902*4882a593Smuzhiyun 		return irq;
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun 	/* The overheat interrupt feature is not mandatory */
905*4882a593Smuzhiyun 	if (irq > 0) {
906*4882a593Smuzhiyun 		ret = devm_request_threaded_irq(&pdev->dev, irq,
907*4882a593Smuzhiyun 						armada_overheat_isr,
908*4882a593Smuzhiyun 						armada_overheat_isr_thread,
909*4882a593Smuzhiyun 						0, NULL, priv);
910*4882a593Smuzhiyun 		if (ret) {
911*4882a593Smuzhiyun 			dev_err(&pdev->dev, "Cannot request threaded IRQ %d\n",
912*4882a593Smuzhiyun 				irq);
913*4882a593Smuzhiyun 			return ret;
914*4882a593Smuzhiyun 		}
915*4882a593Smuzhiyun 	}
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	/*
918*4882a593Smuzhiyun 	 * There is one channel for the IC and one per CPU (if any), each
919*4882a593Smuzhiyun 	 * channel has one sensor.
920*4882a593Smuzhiyun 	 */
921*4882a593Smuzhiyun 	for (sensor_id = 0; sensor_id <= priv->data->cpu_nr; sensor_id++) {
922*4882a593Smuzhiyun 		sensor = devm_kzalloc(&pdev->dev,
923*4882a593Smuzhiyun 				      sizeof(struct armada_thermal_sensor),
924*4882a593Smuzhiyun 				      GFP_KERNEL);
925*4882a593Smuzhiyun 		if (!sensor)
926*4882a593Smuzhiyun 			return -ENOMEM;
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 		/* Register the sensor */
929*4882a593Smuzhiyun 		sensor->priv = priv;
930*4882a593Smuzhiyun 		sensor->id = sensor_id;
931*4882a593Smuzhiyun 		tz = devm_thermal_zone_of_sensor_register(&pdev->dev,
932*4882a593Smuzhiyun 							  sensor->id, sensor,
933*4882a593Smuzhiyun 							  &of_ops);
934*4882a593Smuzhiyun 		if (IS_ERR(tz)) {
935*4882a593Smuzhiyun 			dev_info(&pdev->dev, "Thermal sensor %d unavailable\n",
936*4882a593Smuzhiyun 				 sensor_id);
937*4882a593Smuzhiyun 			devm_kfree(&pdev->dev, sensor);
938*4882a593Smuzhiyun 			continue;
939*4882a593Smuzhiyun 		}
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 		/*
942*4882a593Smuzhiyun 		 * The first channel that has a critical trip point registered
943*4882a593Smuzhiyun 		 * in the DT will serve as interrupt source. Others possible
944*4882a593Smuzhiyun 		 * critical trip points will simply be ignored by the driver.
945*4882a593Smuzhiyun 		 */
946*4882a593Smuzhiyun 		if (irq > 0 && !priv->overheat_sensor)
947*4882a593Smuzhiyun 			armada_configure_overheat_int(priv, tz, sensor->id);
948*4882a593Smuzhiyun 	}
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun 	/* Just complain if no overheat interrupt was set up */
951*4882a593Smuzhiyun 	if (!priv->overheat_sensor)
952*4882a593Smuzhiyun 		dev_warn(&pdev->dev, "Overheat interrupt not available\n");
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun 	return 0;
955*4882a593Smuzhiyun }
956*4882a593Smuzhiyun 
armada_thermal_exit(struct platform_device * pdev)957*4882a593Smuzhiyun static int armada_thermal_exit(struct platform_device *pdev)
958*4882a593Smuzhiyun {
959*4882a593Smuzhiyun 	struct armada_drvdata *drvdata = platform_get_drvdata(pdev);
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	if (drvdata->type == LEGACY)
962*4882a593Smuzhiyun 		thermal_zone_device_unregister(drvdata->data.tz);
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 	return 0;
965*4882a593Smuzhiyun }
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun static struct platform_driver armada_thermal_driver = {
968*4882a593Smuzhiyun 	.probe = armada_thermal_probe,
969*4882a593Smuzhiyun 	.remove = armada_thermal_exit,
970*4882a593Smuzhiyun 	.driver = {
971*4882a593Smuzhiyun 		.name = "armada_thermal",
972*4882a593Smuzhiyun 		.of_match_table = armada_thermal_id_table,
973*4882a593Smuzhiyun 	},
974*4882a593Smuzhiyun };
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun module_platform_driver(armada_thermal_driver);
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun MODULE_AUTHOR("Ezequiel Garcia <ezequiel.garcia@free-electrons.com>");
979*4882a593Smuzhiyun MODULE_DESCRIPTION("Marvell EBU Armada SoCs thermal driver");
980*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
981