xref: /openbmc/linux/drivers/hwmon/axi-fan-control.c (revision 8412b410fa5e1e494a0fec84c3c462d49870d3f5)
1*8412b410SNuno Sá // SPDX-License-Identifier: GPL-2.0
2*8412b410SNuno Sá /*
3*8412b410SNuno Sá  * Fan Control HDL CORE driver
4*8412b410SNuno Sá  *
5*8412b410SNuno Sá  * Copyright 2019 Analog Devices Inc.
6*8412b410SNuno Sá  */
7*8412b410SNuno Sá #include <linux/bits.h>
8*8412b410SNuno Sá #include <linux/clk.h>
9*8412b410SNuno Sá #include <linux/fpga/adi-axi-common.h>
10*8412b410SNuno Sá #include <linux/hwmon.h>
11*8412b410SNuno Sá #include <linux/interrupt.h>
12*8412b410SNuno Sá #include <linux/io.h>
13*8412b410SNuno Sá #include <linux/kernel.h>
14*8412b410SNuno Sá #include <linux/module.h>
15*8412b410SNuno Sá #include <linux/of.h>
16*8412b410SNuno Sá #include <linux/platform_device.h>
17*8412b410SNuno Sá 
18*8412b410SNuno Sá #define ADI_AXI_PCORE_VER_MAJOR(version)	(((version) >> 16) & 0xff)
19*8412b410SNuno Sá #define ADI_AXI_PCORE_VER_MINOR(version)	(((version) >> 8) & 0xff)
20*8412b410SNuno Sá #define ADI_AXI_PCORE_VER_PATCH(version)	((version) & 0xff)
21*8412b410SNuno Sá 
22*8412b410SNuno Sá /* register map */
23*8412b410SNuno Sá #define ADI_REG_RSTN		0x0080
24*8412b410SNuno Sá #define ADI_REG_PWM_WIDTH	0x0084
25*8412b410SNuno Sá #define ADI_REG_TACH_PERIOD	0x0088
26*8412b410SNuno Sá #define ADI_REG_TACH_TOLERANCE	0x008c
27*8412b410SNuno Sá #define ADI_REG_PWM_PERIOD	0x00c0
28*8412b410SNuno Sá #define ADI_REG_TACH_MEASUR	0x00c4
29*8412b410SNuno Sá #define ADI_REG_TEMPERATURE	0x00c8
30*8412b410SNuno Sá 
31*8412b410SNuno Sá #define ADI_REG_IRQ_MASK	0x0040
32*8412b410SNuno Sá #define ADI_REG_IRQ_PENDING	0x0044
33*8412b410SNuno Sá #define ADI_REG_IRQ_SRC		0x0048
34*8412b410SNuno Sá 
35*8412b410SNuno Sá /* IRQ sources */
36*8412b410SNuno Sá #define ADI_IRQ_SRC_PWM_CHANGED		BIT(0)
37*8412b410SNuno Sá #define ADI_IRQ_SRC_TACH_ERR		BIT(1)
38*8412b410SNuno Sá #define ADI_IRQ_SRC_TEMP_INCREASE	BIT(2)
39*8412b410SNuno Sá #define ADI_IRQ_SRC_NEW_MEASUR		BIT(3)
40*8412b410SNuno Sá #define ADI_IRQ_SRC_MASK		GENMASK(3, 0)
41*8412b410SNuno Sá #define ADI_IRQ_MASK_OUT_ALL		0xFFFFFFFFU
42*8412b410SNuno Sá 
43*8412b410SNuno Sá #define SYSFS_PWM_MAX			255
44*8412b410SNuno Sá 
45*8412b410SNuno Sá struct axi_fan_control_data {
46*8412b410SNuno Sá 	void __iomem *base;
47*8412b410SNuno Sá 	struct device *hdev;
48*8412b410SNuno Sá 	unsigned long clk_rate;
49*8412b410SNuno Sá 	int irq;
50*8412b410SNuno Sá 	/* pulses per revolution */
51*8412b410SNuno Sá 	u32 ppr;
52*8412b410SNuno Sá 	bool hw_pwm_req;
53*8412b410SNuno Sá 	bool update_tacho_params;
54*8412b410SNuno Sá 	u8 fan_fault;
55*8412b410SNuno Sá };
56*8412b410SNuno Sá 
57*8412b410SNuno Sá static inline void axi_iowrite(const u32 val, const u32 reg,
58*8412b410SNuno Sá 			       const struct axi_fan_control_data *ctl)
59*8412b410SNuno Sá {
60*8412b410SNuno Sá 	iowrite32(val, ctl->base + reg);
61*8412b410SNuno Sá }
62*8412b410SNuno Sá 
63*8412b410SNuno Sá static inline u32 axi_ioread(const u32 reg,
64*8412b410SNuno Sá 			     const struct axi_fan_control_data *ctl)
65*8412b410SNuno Sá {
66*8412b410SNuno Sá 	return ioread32(ctl->base + reg);
67*8412b410SNuno Sá }
68*8412b410SNuno Sá 
69*8412b410SNuno Sá static long axi_fan_control_get_pwm_duty(const struct axi_fan_control_data *ctl)
70*8412b410SNuno Sá {
71*8412b410SNuno Sá 	u32 pwm_width = axi_ioread(ADI_REG_PWM_WIDTH, ctl);
72*8412b410SNuno Sá 	u32 pwm_period = axi_ioread(ADI_REG_PWM_PERIOD, ctl);
73*8412b410SNuno Sá 	/*
74*8412b410SNuno Sá 	 * PWM_PERIOD is a RO register set by the core. It should never be 0.
75*8412b410SNuno Sá 	 * For now we are trusting the HW...
76*8412b410SNuno Sá 	 */
77*8412b410SNuno Sá 	return DIV_ROUND_CLOSEST(pwm_width * SYSFS_PWM_MAX, pwm_period);
78*8412b410SNuno Sá }
79*8412b410SNuno Sá 
80*8412b410SNuno Sá static int axi_fan_control_set_pwm_duty(const long val,
81*8412b410SNuno Sá 					struct axi_fan_control_data *ctl)
82*8412b410SNuno Sá {
83*8412b410SNuno Sá 	u32 pwm_period = axi_ioread(ADI_REG_PWM_PERIOD, ctl);
84*8412b410SNuno Sá 	u32 new_width;
85*8412b410SNuno Sá 	long __val = clamp_val(val, 0, SYSFS_PWM_MAX);
86*8412b410SNuno Sá 
87*8412b410SNuno Sá 	new_width = DIV_ROUND_CLOSEST(__val * pwm_period, SYSFS_PWM_MAX);
88*8412b410SNuno Sá 
89*8412b410SNuno Sá 	axi_iowrite(new_width, ADI_REG_PWM_WIDTH, ctl);
90*8412b410SNuno Sá 
91*8412b410SNuno Sá 	return 0;
92*8412b410SNuno Sá }
93*8412b410SNuno Sá 
94*8412b410SNuno Sá static long axi_fan_control_get_fan_rpm(const struct axi_fan_control_data *ctl)
95*8412b410SNuno Sá {
96*8412b410SNuno Sá 	const u32 tach = axi_ioread(ADI_REG_TACH_MEASUR, ctl);
97*8412b410SNuno Sá 
98*8412b410SNuno Sá 	if (tach == 0)
99*8412b410SNuno Sá 		/* should we return error, EAGAIN maybe? */
100*8412b410SNuno Sá 		return 0;
101*8412b410SNuno Sá 	/*
102*8412b410SNuno Sá 	 * The tacho period should be:
103*8412b410SNuno Sá 	 *      TACH = 60/(ppr * rpm), where rpm is revolutions per second
104*8412b410SNuno Sá 	 *      and ppr is pulses per revolution.
105*8412b410SNuno Sá 	 * Given the tacho period, we can multiply it by the input clock
106*8412b410SNuno Sá 	 * so that we know how many clocks we need to have this period.
107*8412b410SNuno Sá 	 * From this, we can derive the RPM value.
108*8412b410SNuno Sá 	 */
109*8412b410SNuno Sá 	return DIV_ROUND_CLOSEST(60 * ctl->clk_rate, ctl->ppr * tach);
110*8412b410SNuno Sá }
111*8412b410SNuno Sá 
112*8412b410SNuno Sá static int axi_fan_control_read_temp(struct device *dev, u32 attr, long *val)
113*8412b410SNuno Sá {
114*8412b410SNuno Sá 	struct axi_fan_control_data *ctl = dev_get_drvdata(dev);
115*8412b410SNuno Sá 	long raw_temp;
116*8412b410SNuno Sá 
117*8412b410SNuno Sá 	switch (attr) {
118*8412b410SNuno Sá 	case hwmon_temp_input:
119*8412b410SNuno Sá 		raw_temp = axi_ioread(ADI_REG_TEMPERATURE, ctl);
120*8412b410SNuno Sá 		/*
121*8412b410SNuno Sá 		 * The formula for the temperature is:
122*8412b410SNuno Sá 		 *      T = (ADC * 501.3743 / 2^bits) - 273.6777
123*8412b410SNuno Sá 		 * It's multiplied by 1000 to have millidegrees as
124*8412b410SNuno Sá 		 * specified by the hwmon sysfs interface.
125*8412b410SNuno Sá 		 */
126*8412b410SNuno Sá 		*val = ((raw_temp * 501374) >> 16) - 273677;
127*8412b410SNuno Sá 		return 0;
128*8412b410SNuno Sá 	default:
129*8412b410SNuno Sá 		return -ENOTSUPP;
130*8412b410SNuno Sá 	}
131*8412b410SNuno Sá }
132*8412b410SNuno Sá 
133*8412b410SNuno Sá static int axi_fan_control_read_fan(struct device *dev, u32 attr, long *val)
134*8412b410SNuno Sá {
135*8412b410SNuno Sá 	struct axi_fan_control_data *ctl = dev_get_drvdata(dev);
136*8412b410SNuno Sá 
137*8412b410SNuno Sá 	switch (attr) {
138*8412b410SNuno Sá 	case hwmon_fan_fault:
139*8412b410SNuno Sá 		*val = ctl->fan_fault;
140*8412b410SNuno Sá 		/* clear it now */
141*8412b410SNuno Sá 		ctl->fan_fault = 0;
142*8412b410SNuno Sá 		return 0;
143*8412b410SNuno Sá 	case hwmon_fan_input:
144*8412b410SNuno Sá 		*val = axi_fan_control_get_fan_rpm(ctl);
145*8412b410SNuno Sá 		return 0;
146*8412b410SNuno Sá 	default:
147*8412b410SNuno Sá 		return -ENOTSUPP;
148*8412b410SNuno Sá 	}
149*8412b410SNuno Sá }
150*8412b410SNuno Sá 
151*8412b410SNuno Sá static int axi_fan_control_read_pwm(struct device *dev, u32 attr, long *val)
152*8412b410SNuno Sá {
153*8412b410SNuno Sá 	struct axi_fan_control_data *ctl = dev_get_drvdata(dev);
154*8412b410SNuno Sá 
155*8412b410SNuno Sá 	switch (attr) {
156*8412b410SNuno Sá 	case hwmon_pwm_input:
157*8412b410SNuno Sá 		*val = axi_fan_control_get_pwm_duty(ctl);
158*8412b410SNuno Sá 		return 0;
159*8412b410SNuno Sá 	default:
160*8412b410SNuno Sá 		return -ENOTSUPP;
161*8412b410SNuno Sá 	}
162*8412b410SNuno Sá }
163*8412b410SNuno Sá 
164*8412b410SNuno Sá static int axi_fan_control_write_pwm(struct device *dev, u32 attr, long val)
165*8412b410SNuno Sá {
166*8412b410SNuno Sá 	struct axi_fan_control_data *ctl = dev_get_drvdata(dev);
167*8412b410SNuno Sá 
168*8412b410SNuno Sá 	switch (attr) {
169*8412b410SNuno Sá 	case hwmon_pwm_input:
170*8412b410SNuno Sá 		return axi_fan_control_set_pwm_duty(val, ctl);
171*8412b410SNuno Sá 	default:
172*8412b410SNuno Sá 		return -ENOTSUPP;
173*8412b410SNuno Sá 	}
174*8412b410SNuno Sá }
175*8412b410SNuno Sá 
176*8412b410SNuno Sá static int axi_fan_control_read_labels(struct device *dev,
177*8412b410SNuno Sá 				       enum hwmon_sensor_types type,
178*8412b410SNuno Sá 				       u32 attr, int channel, const char **str)
179*8412b410SNuno Sá {
180*8412b410SNuno Sá 	switch (type) {
181*8412b410SNuno Sá 	case hwmon_fan:
182*8412b410SNuno Sá 		*str = "FAN";
183*8412b410SNuno Sá 		return 0;
184*8412b410SNuno Sá 	case hwmon_temp:
185*8412b410SNuno Sá 		*str = "SYSMON4";
186*8412b410SNuno Sá 		return 0;
187*8412b410SNuno Sá 	default:
188*8412b410SNuno Sá 		return -ENOTSUPP;
189*8412b410SNuno Sá 	}
190*8412b410SNuno Sá }
191*8412b410SNuno Sá 
192*8412b410SNuno Sá static int axi_fan_control_read(struct device *dev,
193*8412b410SNuno Sá 				enum hwmon_sensor_types type,
194*8412b410SNuno Sá 				u32 attr, int channel, long *val)
195*8412b410SNuno Sá {
196*8412b410SNuno Sá 	switch (type) {
197*8412b410SNuno Sá 	case hwmon_fan:
198*8412b410SNuno Sá 		return axi_fan_control_read_fan(dev, attr, val);
199*8412b410SNuno Sá 	case hwmon_pwm:
200*8412b410SNuno Sá 		return axi_fan_control_read_pwm(dev, attr, val);
201*8412b410SNuno Sá 	case hwmon_temp:
202*8412b410SNuno Sá 		return axi_fan_control_read_temp(dev, attr, val);
203*8412b410SNuno Sá 	default:
204*8412b410SNuno Sá 		return -ENOTSUPP;
205*8412b410SNuno Sá 	}
206*8412b410SNuno Sá }
207*8412b410SNuno Sá 
208*8412b410SNuno Sá static int axi_fan_control_write(struct device *dev,
209*8412b410SNuno Sá 				 enum hwmon_sensor_types type,
210*8412b410SNuno Sá 				 u32 attr, int channel, long val)
211*8412b410SNuno Sá {
212*8412b410SNuno Sá 	switch (type) {
213*8412b410SNuno Sá 	case hwmon_pwm:
214*8412b410SNuno Sá 		return axi_fan_control_write_pwm(dev, attr, val);
215*8412b410SNuno Sá 	default:
216*8412b410SNuno Sá 		return -ENOTSUPP;
217*8412b410SNuno Sá 	}
218*8412b410SNuno Sá }
219*8412b410SNuno Sá 
220*8412b410SNuno Sá static umode_t axi_fan_control_fan_is_visible(const u32 attr)
221*8412b410SNuno Sá {
222*8412b410SNuno Sá 	switch (attr) {
223*8412b410SNuno Sá 	case hwmon_fan_input:
224*8412b410SNuno Sá 	case hwmon_fan_fault:
225*8412b410SNuno Sá 	case hwmon_fan_label:
226*8412b410SNuno Sá 		return 0444;
227*8412b410SNuno Sá 	default:
228*8412b410SNuno Sá 		return 0;
229*8412b410SNuno Sá 	}
230*8412b410SNuno Sá }
231*8412b410SNuno Sá 
232*8412b410SNuno Sá static umode_t axi_fan_control_pwm_is_visible(const u32 attr)
233*8412b410SNuno Sá {
234*8412b410SNuno Sá 	switch (attr) {
235*8412b410SNuno Sá 	case hwmon_pwm_input:
236*8412b410SNuno Sá 		return 0644;
237*8412b410SNuno Sá 	default:
238*8412b410SNuno Sá 		return 0;
239*8412b410SNuno Sá 	}
240*8412b410SNuno Sá }
241*8412b410SNuno Sá 
242*8412b410SNuno Sá static umode_t axi_fan_control_temp_is_visible(const u32 attr)
243*8412b410SNuno Sá {
244*8412b410SNuno Sá 	switch (attr) {
245*8412b410SNuno Sá 	case hwmon_temp_input:
246*8412b410SNuno Sá 	case hwmon_temp_label:
247*8412b410SNuno Sá 		return 0444;
248*8412b410SNuno Sá 	default:
249*8412b410SNuno Sá 		return 0;
250*8412b410SNuno Sá 	}
251*8412b410SNuno Sá }
252*8412b410SNuno Sá 
253*8412b410SNuno Sá static umode_t axi_fan_control_is_visible(const void *data,
254*8412b410SNuno Sá 					  enum hwmon_sensor_types type,
255*8412b410SNuno Sá 					  u32 attr, int channel)
256*8412b410SNuno Sá {
257*8412b410SNuno Sá 	switch (type) {
258*8412b410SNuno Sá 	case hwmon_fan:
259*8412b410SNuno Sá 		return axi_fan_control_fan_is_visible(attr);
260*8412b410SNuno Sá 	case hwmon_pwm:
261*8412b410SNuno Sá 		return axi_fan_control_pwm_is_visible(attr);
262*8412b410SNuno Sá 	case hwmon_temp:
263*8412b410SNuno Sá 		return axi_fan_control_temp_is_visible(attr);
264*8412b410SNuno Sá 	default:
265*8412b410SNuno Sá 		return 0;
266*8412b410SNuno Sá 	}
267*8412b410SNuno Sá }
268*8412b410SNuno Sá 
269*8412b410SNuno Sá /*
270*8412b410SNuno Sá  * This core has two main ways of changing the PWM duty cycle. It is done,
271*8412b410SNuno Sá  * either by a request from userspace (writing on pwm1_input) or by the
272*8412b410SNuno Sá  * core itself. When the change is done by the core, it will use predefined
273*8412b410SNuno Sá  * parameters to evaluate the tach signal and, on that case we cannot set them.
274*8412b410SNuno Sá  * On the other hand, when the request is done by the user, with some arbitrary
275*8412b410SNuno Sá  * value that the core does not now about, we have to provide the tach
276*8412b410SNuno Sá  * parameters so that, the core can evaluate the signal. On the IRQ handler we
277*8412b410SNuno Sá  * distinguish this by using the ADI_IRQ_SRC_TEMP_INCREASE interrupt. This tell
278*8412b410SNuno Sá  * us that the CORE requested a new duty cycle. After this, there is 5s delay
279*8412b410SNuno Sá  * on which the core waits for the fan rotation speed to stabilize. After this
280*8412b410SNuno Sá  * we get ADI_IRQ_SRC_PWM_CHANGED irq where we will decide if we need to set
281*8412b410SNuno Sá  * the tach parameters or not on the next tach measurement cycle (corresponding
282*8412b410SNuno Sá  * already to the ney duty cycle) based on the %ctl->hw_pwm_req flag.
283*8412b410SNuno Sá  */
284*8412b410SNuno Sá static irqreturn_t axi_fan_control_irq_handler(int irq, void *data)
285*8412b410SNuno Sá {
286*8412b410SNuno Sá 	struct axi_fan_control_data *ctl = (struct axi_fan_control_data *)data;
287*8412b410SNuno Sá 	u32 irq_pending = axi_ioread(ADI_REG_IRQ_PENDING, ctl);
288*8412b410SNuno Sá 	u32 clear_mask;
289*8412b410SNuno Sá 
290*8412b410SNuno Sá 	if (irq_pending & ADI_IRQ_SRC_NEW_MEASUR) {
291*8412b410SNuno Sá 		if (ctl->update_tacho_params) {
292*8412b410SNuno Sá 			u32 new_tach = axi_ioread(ADI_REG_TACH_MEASUR, ctl);
293*8412b410SNuno Sá 
294*8412b410SNuno Sá 			/* get 25% tolerance */
295*8412b410SNuno Sá 			u32 tach_tol = DIV_ROUND_CLOSEST(new_tach * 25, 100);
296*8412b410SNuno Sá 			/* set new tacho parameters */
297*8412b410SNuno Sá 			axi_iowrite(new_tach, ADI_REG_TACH_PERIOD, ctl);
298*8412b410SNuno Sá 			axi_iowrite(tach_tol, ADI_REG_TACH_TOLERANCE, ctl);
299*8412b410SNuno Sá 			ctl->update_tacho_params = false;
300*8412b410SNuno Sá 		}
301*8412b410SNuno Sá 	}
302*8412b410SNuno Sá 
303*8412b410SNuno Sá 	if (irq_pending & ADI_IRQ_SRC_PWM_CHANGED) {
304*8412b410SNuno Sá 		/*
305*8412b410SNuno Sá 		 * if the pwm changes on behalf of software,
306*8412b410SNuno Sá 		 * we need to provide new tacho parameters to the core.
307*8412b410SNuno Sá 		 * Wait for the next measurement for that...
308*8412b410SNuno Sá 		 */
309*8412b410SNuno Sá 		if (!ctl->hw_pwm_req) {
310*8412b410SNuno Sá 			ctl->update_tacho_params = true;
311*8412b410SNuno Sá 		} else {
312*8412b410SNuno Sá 			ctl->hw_pwm_req = false;
313*8412b410SNuno Sá 			sysfs_notify(&ctl->hdev->kobj, NULL, "pwm1");
314*8412b410SNuno Sá 		}
315*8412b410SNuno Sá 	}
316*8412b410SNuno Sá 
317*8412b410SNuno Sá 	if (irq_pending & ADI_IRQ_SRC_TEMP_INCREASE)
318*8412b410SNuno Sá 		/* hardware requested a new pwm */
319*8412b410SNuno Sá 		ctl->hw_pwm_req = true;
320*8412b410SNuno Sá 
321*8412b410SNuno Sá 	if (irq_pending & ADI_IRQ_SRC_TACH_ERR)
322*8412b410SNuno Sá 		ctl->fan_fault = 1;
323*8412b410SNuno Sá 
324*8412b410SNuno Sá 	/* clear all interrupts */
325*8412b410SNuno Sá 	clear_mask = irq_pending & ADI_IRQ_SRC_MASK;
326*8412b410SNuno Sá 	axi_iowrite(clear_mask, ADI_REG_IRQ_PENDING, ctl);
327*8412b410SNuno Sá 
328*8412b410SNuno Sá 	return IRQ_HANDLED;
329*8412b410SNuno Sá }
330*8412b410SNuno Sá 
331*8412b410SNuno Sá static int axi_fan_control_init(struct axi_fan_control_data *ctl,
332*8412b410SNuno Sá 				const struct device_node *np)
333*8412b410SNuno Sá {
334*8412b410SNuno Sá 	int ret;
335*8412b410SNuno Sá 
336*8412b410SNuno Sá 	/* get fan pulses per revolution */
337*8412b410SNuno Sá 	ret = of_property_read_u32(np, "pulses-per-revolution", &ctl->ppr);
338*8412b410SNuno Sá 	if (ret)
339*8412b410SNuno Sá 		return ret;
340*8412b410SNuno Sá 
341*8412b410SNuno Sá 	/* 1, 2 and 4 are the typical and accepted values */
342*8412b410SNuno Sá 	if (ctl->ppr != 1 && ctl->ppr != 2 && ctl->ppr != 4)
343*8412b410SNuno Sá 		return -EINVAL;
344*8412b410SNuno Sá 	/*
345*8412b410SNuno Sá 	 * Enable all IRQs
346*8412b410SNuno Sá 	 */
347*8412b410SNuno Sá 	axi_iowrite(ADI_IRQ_MASK_OUT_ALL &
348*8412b410SNuno Sá 		    ~(ADI_IRQ_SRC_NEW_MEASUR | ADI_IRQ_SRC_TACH_ERR |
349*8412b410SNuno Sá 		      ADI_IRQ_SRC_PWM_CHANGED | ADI_IRQ_SRC_TEMP_INCREASE),
350*8412b410SNuno Sá 		    ADI_REG_IRQ_MASK, ctl);
351*8412b410SNuno Sá 
352*8412b410SNuno Sá 	/* bring the device out of reset */
353*8412b410SNuno Sá 	axi_iowrite(0x01, ADI_REG_RSTN, ctl);
354*8412b410SNuno Sá 
355*8412b410SNuno Sá 	return ret;
356*8412b410SNuno Sá }
357*8412b410SNuno Sá 
358*8412b410SNuno Sá static const struct hwmon_channel_info *axi_fan_control_info[] = {
359*8412b410SNuno Sá 	HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT),
360*8412b410SNuno Sá 	HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_FAULT | HWMON_F_LABEL),
361*8412b410SNuno Sá 	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL),
362*8412b410SNuno Sá 	NULL
363*8412b410SNuno Sá };
364*8412b410SNuno Sá 
365*8412b410SNuno Sá static const struct hwmon_ops axi_fan_control_hwmon_ops = {
366*8412b410SNuno Sá 	.is_visible = axi_fan_control_is_visible,
367*8412b410SNuno Sá 	.read = axi_fan_control_read,
368*8412b410SNuno Sá 	.write = axi_fan_control_write,
369*8412b410SNuno Sá 	.read_string = axi_fan_control_read_labels,
370*8412b410SNuno Sá };
371*8412b410SNuno Sá 
372*8412b410SNuno Sá static const struct hwmon_chip_info axi_chip_info = {
373*8412b410SNuno Sá 	.ops = &axi_fan_control_hwmon_ops,
374*8412b410SNuno Sá 	.info = axi_fan_control_info,
375*8412b410SNuno Sá };
376*8412b410SNuno Sá 
377*8412b410SNuno Sá static const u32 version_1_0_0 = ADI_AXI_PCORE_VER(1, 0, 'a');
378*8412b410SNuno Sá 
379*8412b410SNuno Sá static const struct of_device_id axi_fan_control_of_match[] = {
380*8412b410SNuno Sá 	{ .compatible = "adi,axi-fan-control-1.00.a",
381*8412b410SNuno Sá 		.data = (void *)&version_1_0_0},
382*8412b410SNuno Sá 	{},
383*8412b410SNuno Sá };
384*8412b410SNuno Sá MODULE_DEVICE_TABLE(of, axi_fan_control_of_match);
385*8412b410SNuno Sá 
386*8412b410SNuno Sá static int axi_fan_control_probe(struct platform_device *pdev)
387*8412b410SNuno Sá {
388*8412b410SNuno Sá 	struct axi_fan_control_data *ctl;
389*8412b410SNuno Sá 	struct clk *clk;
390*8412b410SNuno Sá 	const struct of_device_id *id;
391*8412b410SNuno Sá 	const char *name = "axi_fan_control";
392*8412b410SNuno Sá 	u32 version;
393*8412b410SNuno Sá 	int ret;
394*8412b410SNuno Sá 
395*8412b410SNuno Sá 	id = of_match_node(axi_fan_control_of_match, pdev->dev.of_node);
396*8412b410SNuno Sá 	if (!id)
397*8412b410SNuno Sá 		return -EINVAL;
398*8412b410SNuno Sá 
399*8412b410SNuno Sá 	ctl = devm_kzalloc(&pdev->dev, sizeof(*ctl), GFP_KERNEL);
400*8412b410SNuno Sá 	if (!ctl)
401*8412b410SNuno Sá 		return -ENOMEM;
402*8412b410SNuno Sá 
403*8412b410SNuno Sá 	ctl->base = devm_platform_ioremap_resource(pdev, 0);
404*8412b410SNuno Sá 	if (IS_ERR(ctl->base))
405*8412b410SNuno Sá 		return PTR_ERR(ctl->base);
406*8412b410SNuno Sá 
407*8412b410SNuno Sá 	clk = devm_clk_get(&pdev->dev, NULL);
408*8412b410SNuno Sá 	if (IS_ERR(clk)) {
409*8412b410SNuno Sá 		dev_err(&pdev->dev, "clk_get failed with %ld\n", PTR_ERR(clk));
410*8412b410SNuno Sá 		return PTR_ERR(clk);
411*8412b410SNuno Sá 	}
412*8412b410SNuno Sá 
413*8412b410SNuno Sá 	ctl->clk_rate = clk_get_rate(clk);
414*8412b410SNuno Sá 	if (!ctl->clk_rate)
415*8412b410SNuno Sá 		return -EINVAL;
416*8412b410SNuno Sá 
417*8412b410SNuno Sá 	version = axi_ioread(ADI_AXI_REG_VERSION, ctl);
418*8412b410SNuno Sá 	if (ADI_AXI_PCORE_VER_MAJOR(version) !=
419*8412b410SNuno Sá 	    ADI_AXI_PCORE_VER_MAJOR((*(u32 *)id->data))) {
420*8412b410SNuno Sá 		dev_err(&pdev->dev, "Major version mismatch. Expected %d.%.2d.%c, Reported %d.%.2d.%c\n",
421*8412b410SNuno Sá 			ADI_AXI_PCORE_VER_MAJOR((*(u32 *)id->data)),
422*8412b410SNuno Sá 			ADI_AXI_PCORE_VER_MINOR((*(u32 *)id->data)),
423*8412b410SNuno Sá 			ADI_AXI_PCORE_VER_PATCH((*(u32 *)id->data)),
424*8412b410SNuno Sá 			ADI_AXI_PCORE_VER_MAJOR(version),
425*8412b410SNuno Sá 			ADI_AXI_PCORE_VER_MINOR(version),
426*8412b410SNuno Sá 			ADI_AXI_PCORE_VER_PATCH(version));
427*8412b410SNuno Sá 		return -ENODEV;
428*8412b410SNuno Sá 	}
429*8412b410SNuno Sá 
430*8412b410SNuno Sá 	ctl->irq = platform_get_irq(pdev, 0);
431*8412b410SNuno Sá 	if (ctl->irq < 0)
432*8412b410SNuno Sá 		return ctl->irq;
433*8412b410SNuno Sá 
434*8412b410SNuno Sá 	ret = devm_request_threaded_irq(&pdev->dev, ctl->irq, NULL,
435*8412b410SNuno Sá 					axi_fan_control_irq_handler,
436*8412b410SNuno Sá 					IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
437*8412b410SNuno Sá 					pdev->driver_override, ctl);
438*8412b410SNuno Sá 	if (ret) {
439*8412b410SNuno Sá 		dev_err(&pdev->dev, "failed to request an irq, %d", ret);
440*8412b410SNuno Sá 		return ret;
441*8412b410SNuno Sá 	}
442*8412b410SNuno Sá 
443*8412b410SNuno Sá 	ret = axi_fan_control_init(ctl, pdev->dev.of_node);
444*8412b410SNuno Sá 	if (ret) {
445*8412b410SNuno Sá 		dev_err(&pdev->dev, "Failed to initialize device\n");
446*8412b410SNuno Sá 		return ret;
447*8412b410SNuno Sá 	}
448*8412b410SNuno Sá 
449*8412b410SNuno Sá 	ctl->hdev = devm_hwmon_device_register_with_info(&pdev->dev,
450*8412b410SNuno Sá 							 name,
451*8412b410SNuno Sá 							 ctl,
452*8412b410SNuno Sá 							 &axi_chip_info,
453*8412b410SNuno Sá 							 NULL);
454*8412b410SNuno Sá 
455*8412b410SNuno Sá 	return PTR_ERR_OR_ZERO(ctl->hdev);
456*8412b410SNuno Sá }
457*8412b410SNuno Sá 
458*8412b410SNuno Sá static struct platform_driver axi_fan_control_driver = {
459*8412b410SNuno Sá 	.driver = {
460*8412b410SNuno Sá 		.name = "axi_fan_control_driver",
461*8412b410SNuno Sá 		.of_match_table = axi_fan_control_of_match,
462*8412b410SNuno Sá 	},
463*8412b410SNuno Sá 	.probe = axi_fan_control_probe,
464*8412b410SNuno Sá };
465*8412b410SNuno Sá module_platform_driver(axi_fan_control_driver);
466*8412b410SNuno Sá 
467*8412b410SNuno Sá MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
468*8412b410SNuno Sá MODULE_DESCRIPTION("Analog Devices Fan Control HDL CORE driver");
469*8412b410SNuno Sá MODULE_LICENSE("GPL");
470