1 // SPDX-License-Identifier: GPL-2.0
3 * Fan Control HDL CORE driver
5 * Copyright 2019 Analog Devices Inc.
7 #include <linux/bits.h>
9 #include <linux/fpga/adi-axi-common.h>
10 #include <linux/hwmon.h>
11 #include <linux/hwmon-sysfs.h>
12 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/platform_device.h>
18 #include <linux/property.h>
21 #define ADI_REG_RSTN 0x0080
22 #define ADI_REG_PWM_WIDTH 0x0084
23 #define ADI_REG_TACH_PERIOD 0x0088
24 #define ADI_REG_TACH_TOLERANCE 0x008c
25 #define ADI_REG_PWM_PERIOD 0x00c0
26 #define ADI_REG_TACH_MEASUR 0x00c4
27 #define ADI_REG_TEMPERATURE 0x00c8
28 #define ADI_REG_TEMP_00_H 0x0100
29 #define ADI_REG_TEMP_25_L 0x0104
30 #define ADI_REG_TEMP_25_H 0x0108
31 #define ADI_REG_TEMP_50_L 0x010c
32 #define ADI_REG_TEMP_50_H 0x0110
33 #define ADI_REG_TEMP_75_L 0x0114
34 #define ADI_REG_TEMP_75_H 0x0118
35 #define ADI_REG_TEMP_100_L 0x011c
37 #define ADI_REG_IRQ_MASK 0x0040
38 #define ADI_REG_IRQ_PENDING 0x0044
39 #define ADI_REG_IRQ_SRC 0x0048
42 #define ADI_IRQ_SRC_PWM_CHANGED BIT(0)
43 #define ADI_IRQ_SRC_TACH_ERR BIT(1)
44 #define ADI_IRQ_SRC_TEMP_INCREASE BIT(2)
45 #define ADI_IRQ_SRC_NEW_MEASUR BIT(3)
46 #define ADI_IRQ_SRC_MASK GENMASK(3, 0)
47 #define ADI_IRQ_MASK_OUT_ALL 0xFFFFFFFFU
49 #define SYSFS_PWM_MAX 255
51 struct axi_fan_control_data
{
54 unsigned long clk_rate
;
56 /* pulses per revolution */
59 bool update_tacho_params
;
63 static inline void axi_iowrite(const u32 val
, const u32 reg
,
64 const struct axi_fan_control_data
*ctl
)
66 iowrite32(val
, ctl
->base
+ reg
);
69 static inline u32
axi_ioread(const u32 reg
,
70 const struct axi_fan_control_data
*ctl
)
72 return ioread32(ctl
->base
+ reg
);
76 * The core calculates the temperature as:
77 * T = /raw * 509.3140064 / 65535) - 280.2308787
79 static ssize_t
axi_fan_control_show(struct device
*dev
, struct device_attribute
*da
, char *buf
)
81 struct axi_fan_control_data
*ctl
= dev_get_drvdata(dev
);
82 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
83 u32 temp
= axi_ioread(attr
->index
, ctl
);
85 temp
= DIV_ROUND_CLOSEST_ULL(temp
* 509314ULL, 65535) - 280230;
87 return sysfs_emit(buf
, "%u\n", temp
);
90 static ssize_t
axi_fan_control_store(struct device
*dev
, struct device_attribute
*da
,
91 const char *buf
, size_t count
)
93 struct axi_fan_control_data
*ctl
= dev_get_drvdata(dev
);
94 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
98 ret
= kstrtou32(buf
, 10, &temp
);
102 temp
= DIV_ROUND_CLOSEST_ULL((temp
+ 280230) * 65535ULL, 509314);
103 axi_iowrite(temp
, attr
->index
, ctl
);
108 static long axi_fan_control_get_pwm_duty(const struct axi_fan_control_data
*ctl
)
110 u32 pwm_width
= axi_ioread(ADI_REG_PWM_WIDTH
, ctl
);
111 u32 pwm_period
= axi_ioread(ADI_REG_PWM_PERIOD
, ctl
);
113 * PWM_PERIOD is a RO register set by the core. It should never be 0.
114 * For now we are trusting the HW...
116 return DIV_ROUND_CLOSEST(pwm_width
* SYSFS_PWM_MAX
, pwm_period
);
119 static int axi_fan_control_set_pwm_duty(const long val
,
120 struct axi_fan_control_data
*ctl
)
122 u32 pwm_period
= axi_ioread(ADI_REG_PWM_PERIOD
, ctl
);
124 long __val
= clamp_val(val
, 0, SYSFS_PWM_MAX
);
126 new_width
= DIV_ROUND_CLOSEST(__val
* pwm_period
, SYSFS_PWM_MAX
);
128 axi_iowrite(new_width
, ADI_REG_PWM_WIDTH
, ctl
);
133 static long axi_fan_control_get_fan_rpm(const struct axi_fan_control_data
*ctl
)
135 const u32 tach
= axi_ioread(ADI_REG_TACH_MEASUR
, ctl
);
138 /* should we return error, EAGAIN maybe? */
141 * The tacho period should be:
142 * TACH = 60/(ppr * rpm), where rpm is revolutions per second
143 * and ppr is pulses per revolution.
144 * Given the tacho period, we can multiply it by the input clock
145 * so that we know how many clocks we need to have this period.
146 * From this, we can derive the RPM value.
148 return DIV_ROUND_CLOSEST(60 * ctl
->clk_rate
, ctl
->ppr
* tach
);
151 static int axi_fan_control_read_temp(struct device
*dev
, u32 attr
, long *val
)
153 struct axi_fan_control_data
*ctl
= dev_get_drvdata(dev
);
157 case hwmon_temp_input
:
158 raw_temp
= axi_ioread(ADI_REG_TEMPERATURE
, ctl
);
160 * The formula for the temperature is:
161 * T = (ADC * 501.3743 / 2^bits) - 273.6777
162 * It's multiplied by 1000 to have millidegrees as
163 * specified by the hwmon sysfs interface.
165 *val
= ((raw_temp
* 501374) >> 16) - 273677;
172 static int axi_fan_control_read_fan(struct device
*dev
, u32 attr
, long *val
)
174 struct axi_fan_control_data
*ctl
= dev_get_drvdata(dev
);
177 case hwmon_fan_fault
:
178 *val
= ctl
->fan_fault
;
182 case hwmon_fan_input
:
183 *val
= axi_fan_control_get_fan_rpm(ctl
);
190 static int axi_fan_control_read_pwm(struct device
*dev
, u32 attr
, long *val
)
192 struct axi_fan_control_data
*ctl
= dev_get_drvdata(dev
);
195 case hwmon_pwm_input
:
196 *val
= axi_fan_control_get_pwm_duty(ctl
);
203 static int axi_fan_control_write_pwm(struct device
*dev
, u32 attr
, long val
)
205 struct axi_fan_control_data
*ctl
= dev_get_drvdata(dev
);
208 case hwmon_pwm_input
:
209 return axi_fan_control_set_pwm_duty(val
, ctl
);
215 static int axi_fan_control_read_labels(struct device
*dev
,
216 enum hwmon_sensor_types type
,
217 u32 attr
, int channel
, const char **str
)
231 static int axi_fan_control_read(struct device
*dev
,
232 enum hwmon_sensor_types type
,
233 u32 attr
, int channel
, long *val
)
237 return axi_fan_control_read_fan(dev
, attr
, val
);
239 return axi_fan_control_read_pwm(dev
, attr
, val
);
241 return axi_fan_control_read_temp(dev
, attr
, val
);
247 static int axi_fan_control_write(struct device
*dev
,
248 enum hwmon_sensor_types type
,
249 u32 attr
, int channel
, long val
)
253 return axi_fan_control_write_pwm(dev
, attr
, val
);
259 static umode_t
axi_fan_control_fan_is_visible(const u32 attr
)
262 case hwmon_fan_input
:
263 case hwmon_fan_fault
:
264 case hwmon_fan_label
:
271 static umode_t
axi_fan_control_pwm_is_visible(const u32 attr
)
274 case hwmon_pwm_input
:
281 static umode_t
axi_fan_control_temp_is_visible(const u32 attr
)
284 case hwmon_temp_input
:
285 case hwmon_temp_label
:
292 static umode_t
axi_fan_control_is_visible(const void *data
,
293 enum hwmon_sensor_types type
,
294 u32 attr
, int channel
)
298 return axi_fan_control_fan_is_visible(attr
);
300 return axi_fan_control_pwm_is_visible(attr
);
302 return axi_fan_control_temp_is_visible(attr
);
309 * This core has two main ways of changing the PWM duty cycle. It is done,
310 * either by a request from userspace (writing on pwm1_input) or by the
311 * core itself. When the change is done by the core, it will use predefined
312 * parameters to evaluate the tach signal and, on that case we cannot set them.
313 * On the other hand, when the request is done by the user, with some arbitrary
314 * value that the core does not now about, we have to provide the tach
315 * parameters so that, the core can evaluate the signal. On the IRQ handler we
316 * distinguish this by using the ADI_IRQ_SRC_TEMP_INCREASE interrupt. This tell
317 * us that the CORE requested a new duty cycle. After this, there is 5s delay
318 * on which the core waits for the fan rotation speed to stabilize. After this
319 * we get ADI_IRQ_SRC_PWM_CHANGED irq where we will decide if we need to set
320 * the tach parameters or not on the next tach measurement cycle (corresponding
321 * already to the ney duty cycle) based on the %ctl->hw_pwm_req flag.
323 static irqreturn_t
axi_fan_control_irq_handler(int irq
, void *data
)
325 struct axi_fan_control_data
*ctl
= (struct axi_fan_control_data
*)data
;
326 u32 irq_pending
= axi_ioread(ADI_REG_IRQ_PENDING
, ctl
);
329 if (irq_pending
& ADI_IRQ_SRC_TEMP_INCREASE
)
330 /* hardware requested a new pwm */
331 ctl
->hw_pwm_req
= true;
333 if (irq_pending
& ADI_IRQ_SRC_PWM_CHANGED
) {
335 * if the pwm changes on behalf of software,
336 * we need to provide new tacho parameters to the core.
337 * Wait for the next measurement for that...
339 if (!ctl
->hw_pwm_req
) {
340 ctl
->update_tacho_params
= true;
342 ctl
->hw_pwm_req
= false;
343 hwmon_notify_event(ctl
->hdev
, hwmon_pwm
,
348 if (irq_pending
& ADI_IRQ_SRC_NEW_MEASUR
) {
349 if (ctl
->update_tacho_params
) {
350 u32 new_tach
= axi_ioread(ADI_REG_TACH_MEASUR
, ctl
);
351 /* get 25% tolerance */
352 u32 tach_tol
= DIV_ROUND_CLOSEST(new_tach
* 25, 100);
354 /* set new tacho parameters */
355 axi_iowrite(new_tach
, ADI_REG_TACH_PERIOD
, ctl
);
356 axi_iowrite(tach_tol
, ADI_REG_TACH_TOLERANCE
, ctl
);
357 ctl
->update_tacho_params
= false;
361 if (irq_pending
& ADI_IRQ_SRC_TACH_ERR
)
364 /* clear all interrupts */
365 clear_mask
= irq_pending
& ADI_IRQ_SRC_MASK
;
366 axi_iowrite(clear_mask
, ADI_REG_IRQ_PENDING
, ctl
);
371 static int axi_fan_control_init(struct axi_fan_control_data
*ctl
,
372 const struct device
*dev
)
376 /* get fan pulses per revolution */
377 ret
= device_property_read_u32(dev
, "pulses-per-revolution", &ctl
->ppr
);
381 /* 1, 2 and 4 are the typical and accepted values */
382 if (ctl
->ppr
!= 1 && ctl
->ppr
!= 2 && ctl
->ppr
!= 4)
387 axi_iowrite(ADI_IRQ_MASK_OUT_ALL
&
388 ~(ADI_IRQ_SRC_NEW_MEASUR
| ADI_IRQ_SRC_TACH_ERR
|
389 ADI_IRQ_SRC_PWM_CHANGED
| ADI_IRQ_SRC_TEMP_INCREASE
),
390 ADI_REG_IRQ_MASK
, ctl
);
392 /* bring the device out of reset */
393 axi_iowrite(0x01, ADI_REG_RSTN
, ctl
);
398 static const struct hwmon_channel_info
* const axi_fan_control_info
[] = {
399 HWMON_CHANNEL_INFO(pwm
, HWMON_PWM_INPUT
),
400 HWMON_CHANNEL_INFO(fan
, HWMON_F_INPUT
| HWMON_F_FAULT
| HWMON_F_LABEL
),
401 HWMON_CHANNEL_INFO(temp
, HWMON_T_INPUT
| HWMON_T_LABEL
),
405 static const struct hwmon_ops axi_fan_control_hwmon_ops
= {
406 .is_visible
= axi_fan_control_is_visible
,
407 .read
= axi_fan_control_read
,
408 .write
= axi_fan_control_write
,
409 .read_string
= axi_fan_control_read_labels
,
412 static const struct hwmon_chip_info axi_chip_info
= {
413 .ops
= &axi_fan_control_hwmon_ops
,
414 .info
= axi_fan_control_info
,
417 /* temperature threshold below which PWM should be 0% */
418 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp_hyst
, axi_fan_control
, ADI_REG_TEMP_00_H
);
419 /* temperature threshold above which PWM should be 25% */
420 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp
, axi_fan_control
, ADI_REG_TEMP_25_L
);
421 /* temperature threshold below which PWM should be 25% */
422 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_temp_hyst
, axi_fan_control
, ADI_REG_TEMP_25_H
);
423 /* temperature threshold above which PWM should be 50% */
424 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_temp
, axi_fan_control
, ADI_REG_TEMP_50_L
);
425 /* temperature threshold below which PWM should be 50% */
426 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_temp_hyst
, axi_fan_control
, ADI_REG_TEMP_50_H
);
427 /* temperature threshold above which PWM should be 75% */
428 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_temp
, axi_fan_control
, ADI_REG_TEMP_75_L
);
429 /* temperature threshold below which PWM should be 75% */
430 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_temp_hyst
, axi_fan_control
, ADI_REG_TEMP_75_H
);
431 /* temperature threshold above which PWM should be 100% */
432 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_temp
, axi_fan_control
, ADI_REG_TEMP_100_L
);
434 static struct attribute
*axi_fan_control_attrs
[] = {
435 &sensor_dev_attr_pwm1_auto_point1_temp_hyst
.dev_attr
.attr
,
436 &sensor_dev_attr_pwm1_auto_point1_temp
.dev_attr
.attr
,
437 &sensor_dev_attr_pwm1_auto_point2_temp_hyst
.dev_attr
.attr
,
438 &sensor_dev_attr_pwm1_auto_point2_temp
.dev_attr
.attr
,
439 &sensor_dev_attr_pwm1_auto_point3_temp_hyst
.dev_attr
.attr
,
440 &sensor_dev_attr_pwm1_auto_point3_temp
.dev_attr
.attr
,
441 &sensor_dev_attr_pwm1_auto_point4_temp_hyst
.dev_attr
.attr
,
442 &sensor_dev_attr_pwm1_auto_point4_temp
.dev_attr
.attr
,
445 ATTRIBUTE_GROUPS(axi_fan_control
);
447 static int axi_fan_control_probe(struct platform_device
*pdev
)
449 struct axi_fan_control_data
*ctl
;
451 const unsigned int *id
;
452 const char *name
= "axi_fan_control";
456 id
= device_get_match_data(&pdev
->dev
);
460 ctl
= devm_kzalloc(&pdev
->dev
, sizeof(*ctl
), GFP_KERNEL
);
464 ctl
->base
= devm_platform_ioremap_resource(pdev
, 0);
465 if (IS_ERR(ctl
->base
))
466 return PTR_ERR(ctl
->base
);
468 clk
= devm_clk_get_enabled(&pdev
->dev
, NULL
);
470 return dev_err_probe(&pdev
->dev
, PTR_ERR(clk
),
473 ctl
->clk_rate
= clk_get_rate(clk
);
477 version
= axi_ioread(ADI_AXI_REG_VERSION
, ctl
);
478 if (ADI_AXI_PCORE_VER_MAJOR(version
) !=
479 ADI_AXI_PCORE_VER_MAJOR((*id
)))
480 return dev_err_probe(&pdev
->dev
, -ENODEV
,
481 "Major version mismatch. Expected %d.%.2d.%c, Reported %d.%.2d.%c\n",
482 ADI_AXI_PCORE_VER_MAJOR(*id
),
483 ADI_AXI_PCORE_VER_MINOR(*id
),
484 ADI_AXI_PCORE_VER_PATCH(*id
),
485 ADI_AXI_PCORE_VER_MAJOR(version
),
486 ADI_AXI_PCORE_VER_MINOR(version
),
487 ADI_AXI_PCORE_VER_PATCH(version
));
489 ret
= axi_fan_control_init(ctl
, &pdev
->dev
);
491 return dev_err_probe(&pdev
->dev
, ret
,
492 "Failed to initialize device\n");
494 ctl
->hdev
= devm_hwmon_device_register_with_info(&pdev
->dev
,
498 axi_fan_control_groups
);
500 if (IS_ERR(ctl
->hdev
))
501 return PTR_ERR(ctl
->hdev
);
503 ctl
->irq
= platform_get_irq(pdev
, 0);
507 ret
= devm_request_threaded_irq(&pdev
->dev
, ctl
->irq
, NULL
,
508 axi_fan_control_irq_handler
,
509 IRQF_ONESHOT
| IRQF_TRIGGER_HIGH
,
510 pdev
->driver_override
, ctl
);
512 return dev_err_probe(&pdev
->dev
, ret
,
513 "failed to request an irq\n");
518 static const u32 version_1_0_0
= ADI_AXI_PCORE_VER(1, 0, 'a');
520 static const struct of_device_id axi_fan_control_of_match
[] = {
521 { .compatible
= "adi,axi-fan-control-1.00.a",
522 .data
= (void *)&version_1_0_0
},
525 MODULE_DEVICE_TABLE(of
, axi_fan_control_of_match
);
527 static struct platform_driver axi_fan_control_driver
= {
529 .name
= "axi_fan_control_driver",
530 .of_match_table
= axi_fan_control_of_match
,
532 .probe
= axi_fan_control_probe
,
534 module_platform_driver(axi_fan_control_driver
);
536 MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
537 MODULE_DESCRIPTION("Analog Devices Fan Control HDL CORE driver");
538 MODULE_LICENSE("GPL");