Document multiple fan tach support in pwm-fan driver
Subsystems (2)
cover Document multiple fan tach support in pwm-fan driver
This patch updates the device tree bindings to match the changes I'm about to send for the pwm-fan driver. Those changes will extend the pwm-fan driver to support multiple fan tachometer inputs.
These changes can also be pulled from:
https://gitlab.com/pbarker.dev/staging/linux.git
tag: for-dt-next/pwm-fan_2020-09-20
Paul Barker (1):
dt-bindings: hwmon: pwm-fan: Support multiple fan tachometer inputs
.../devicetree/bindings/hwmon/pwm-fan.txt | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-)
base-commit: 73f76a41c4ed7def5dc2ec7c33c7e9f94e601a20
Signature · 1 line
2.28.0
PATCH 1/1 dt-bindings: hwmon: pwm-fan: Support multiple fan tachometer inputs
Document and give an example of how to define multiple fan tachometer inputs for the pwm-fan driver.
--- .../devicetree/bindings/hwmon/pwm-fan.txt | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-)
Documentation/devicetree/bindings/hwmon/pwm-fan.txt +19 -9
@@ -8,15 +8,16 @@ Required properties: Optional properties: - fan-supply : phandle to the regulator that provides power to the fan -- interrupts : This contains a single interrupt specifier which - describes the tachometer output of the fan as an - interrupt source. The output signal must generate a - defined number of interrupts per fan revolution, which - require that it must be self resetting edge interrupts. - See interrupt-controller/interrupts.txt for the format. -- pulses-per-revolution : define the tachometer pulses per fan revolution as - an integer (default is 2 interrupts per revolution). - The value must be greater than zero. +- interrupts : This contains an interrupt specifier for each fan + tachometer output connected to an interrupt source. + The output signal must generate a defined number of + interrupts per fan revolution, which require that + it must be self resetting edge interrupts. See + interrupt-controller/interrupts.txt for the format. +- pulses-per-revolution : define the number of pulses per fan revolution for + each tachometer input as an integer (default is 2 + interrupts per revolution). The value must be + greater than zero. Example: fan0: pwm-fan {
@@ -55,3 +56,12 @@ Example 2: interrupts = <1 IRQ_TYPE_EDGE_FALLING>; pulses-per-revolution = <2>; }; + +Example 3: + fan0: pwm-fan { + compatible = "pwm-fan"; + pwms = <&pwm1 0 25000 0>; + interrupts-extended = <&gpio1 1 IRQ_TYPE_EDGE_FALLING>, + <&gpio2 5 IRQ_TYPE_EDGE_FALLING>; + pulses-per-revolution = <2>, <1>; + };
Signature · 1 line
2.28.0
Re: [PATCH 1/1] dt-bindings: hwmon: pwm-fan: Support multiple fan tachometer inputs
Quoted 8 lines · Paul Barker
Document and give an example of how to define multiple fan tachometer inputs for the pwm-fan driver.
Signed-off-by Paul Barker <[email protected]>--- .../devicetree/bindings/hwmon/pwm-fan.txt | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-)
Re: [PATCH 1/1] dt-bindings: hwmon: pwm-fan: Support multiple fan tachometer inputs
In reply to Paul BarkerQuoted 5 lines · Paul Barker
Document and give an example of how to define multiple fan tachometer inputs for the pwm-fan driver.
Signed-off-by Paul Barker <[email protected]>Reviewed-by Rob Herring <[email protected]>
Applied.
Thanks,
Guenter
Quoted 47 lines
--- .../devicetree/bindings/hwmon/pwm-fan.txt | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-)Documentation/devicetree/bindings/hwmon/pwm-fan.txt +19
-9@@ -8,15 +8,16 @@ Required properties: Optional properties: - fan-supply : phandle to the regulator that provides power to the fan -- interrupts : This contains a single interrupt specifier which - describes the tachometer output of the fan as an - interrupt source. The output signal must generate a - defined number of interrupts per fan revolution, which - require that it must be self resetting edge interrupts. - See interrupt-controller/interrupts.txt for the format. -- pulses-per-revolution : define the tachometer pulses per fan revolution as - an integer (default is 2 interrupts per revolution). - The value must be greater than zero. +- interrupts : This contains an interrupt specifier for each fan + tachometer output connected to an interrupt source. + The output signal must generate a defined number of + interrupts per fan revolution, which require that + it must be self resetting edge interrupts. See + interrupt-controller/interrupts.txt for the format. +- pulses-per-revolution : define the number of pulses per fan revolution for + each tachometer input as an integer (default is 2 + interrupts per revolution). The value must be + greater than zero. Example: fan0: pwm-fan {@@ -55,3 +56,12 @@ Example 2: interrupts = <1 IRQ_TYPE_EDGE_FALLING>; pulses-per-revolution = <2>; }; + +Example 3: + fan0: pwm-fan { + compatible = "pwm-fan"; + pwms = <&pwm1 0 25000 0>; + interrupts-extended = <&gpio1 1 IRQ_TYPE_EDGE_FALLING>, + <&gpio2 5 IRQ_TYPE_EDGE_FALLING>; + pulses-per-revolution = <2>, <1>; + };
cover pwm-fan: Support multiple tach inputs & fix RPM calc
In reply to Paul BarkerThese changes were made to support a custom board where one PWM output is routed to two fans, each of which has a tachometer signal routed to a GPIO input on the SoC. While debugging on this board it was found that the RPM readings were being overestimated due to a bug in the calculation code so I've included a fix for that.
As the custom board doesn't currently support the latest mainline kernel I've tested these changes on a SanCloud BeagleBone Enhanced using an oscilloscope to check the PWM output and a signal generator to simulate the fan tachometer signals. I've tested variants of the device tree with 0, 1 and 2 fan tachometer inputs configured to ensure the logic in the probe function is correct.
The device tree bindings changes have been submitted in a separate patch.
These changes can also be pulled from:
https://gitlab.com/pbarker.dev/staging/linux.git
tag: for-hwmon/pwm-fan_2020-09-20
Paul Barker (2):
hwmon: pwm-fan: Support multiple tachometer inputs
hwmon: pwm-fan: Fix RPM calculation
drivers/hwmon/pwm-fan.c | 164 ++++++++++++++++++++++++---------------- 1 file changed, 100 insertions(+), 64 deletions(-)
base-commit: 2835d860d3fcc3e4829e96987544e811d35dee48
Signature · 1 line
2.28.0
Re: [PATCH 0/2] pwm-fan: Support multiple tach inputs & fix RPM calc
Quoted 26 lines · Paul Barker
These changes were made to support a custom board where one PWM output is routed to two fans, each of which has a tachometer signal routed to a GPIO input on the SoC. While debugging on this board it was found that the RPM readings were being overestimated due to a bug in the calculation code so I've included a fix for that.
As the custom board doesn't currently support the latest mainline kernel I've tested these changes on a SanCloud BeagleBone Enhanced using an oscilloscope to check the PWM output and a signal generator to simulate the fan tachometer signals. I've tested variants of the device tree with 0, 1 and 2 fan tachometer inputs configured to ensure the logic in the probe function is correct.
The device tree bindings changes have been submitted in a separate patch.
These changes can also be pulled from:
https://gitlab.com/pbarker.dev/staging/linux.git
tag: for-hwmon/pwm-fan_2020-09-20Paul Barker (2):
hwmon: pwm-fan: Support multiple tachometer inputs
hwmon: pwm-fan: Fix RPM calculation
Quoted 5 lines
drivers/hwmon/pwm-fan.c | 164 ++++++++++++++++++++++++---------------- 1 file changed, 100 insertions(+), 64 deletions(-)base-commit:
2835d860d3fcc3e4829e96987544e811d35dee48
Ping on these, I've not had any feedback for 2 weeks.
Thanks,
Signature · 2 lines
Paul Barker Konsulko Group
Guenter Roeck
maintainerRe: [PATCH 0/2] pwm-fan: Support multiple tach inputs & fix RPM calc
Quoted 27 lines · Paul Barker
Quoted 26 lines · Paul Barker
These changes were made to support a custom board where one PWM output is routed to two fans, each of which has a tachometer signal routed to a GPIO input on the SoC. While debugging on this board it was found that the RPM readings were being overestimated due to a bug in the calculation code so I've included a fix for that.
As the custom board doesn't currently support the latest mainline kernel I've tested these changes on a SanCloud BeagleBone Enhanced using an oscilloscope to check the PWM output and a signal generator to simulate the fan tachometer signals. I've tested variants of the device tree with 0, 1 and 2 fan tachometer inputs configured to ensure the logic in the probe function is correct.
The device tree bindings changes have been submitted in a separate patch.
These changes can also be pulled from:
https://gitlab.com/pbarker.dev/staging/linux.git
tag: for-hwmon/pwm-fan_2020-09-20Paul Barker (2):
hwmon: pwm-fan: Support multiple tachometer inputs
hwmon: pwm-fan: Fix RPM calculation
Quoted 8 lines
Quoted 5 lines
drivers/hwmon/pwm-fan.c | 164 ++++++++++++++++++++++++---------------- 1 file changed, 100 insertions(+), 64 deletions(-)base-commit:
2835d860d3fcc3e4829e96987544e811d35dee48Ping on these, I've not had any feedback for 2 weeks.
Unfortunately the patches are a bit difficult to understand, and I did not have the time to fully analyze them. One thing I can say is that the order should be reversed - the bug fix should come first. Also, patch 2 adds a potential divide by 0 problem, if 'delta' happens to be 0.
There are little details, such as replacing 'int pulses' with 'unsigned int pulses', which make things more difficult to review. atomic_t is int. Assigning it to an unsigned int _should_ not matter here, but again such little changes make it more difficult to review a patch because they require extra scrutiny.
Guenter
Re: [PATCH 0/2] pwm-fan: Support multiple tach inputs & fix RPM calc
Quoted 44 lines · Guenter Roeck
Quoted 27 lines · Paul Barker
Quoted 26 lines · Paul Barker
These changes were made to support a custom board where one PWM output is routed to two fans, each of which has a tachometer signal routed to a GPIO input on the SoC. While debugging on this board it was found that the RPM readings were being overestimated due to a bug in the calculation code so I've included a fix for that.
As the custom board doesn't currently support the latest mainline kernel I've tested these changes on a SanCloud BeagleBone Enhanced using an oscilloscope to check the PWM output and a signal generator to simulate the fan tachometer signals. I've tested variants of the device tree with 0, 1 and 2 fan tachometer inputs configured to ensure the logic in the probe function is correct.
The device tree bindings changes have been submitted in a separate patch.
These changes can also be pulled from:
https://gitlab.com/pbarker.dev/staging/linux.git
tag: for-hwmon/pwm-fan_2020-09-20Paul Barker (2):
hwmon: pwm-fan: Support multiple tachometer inputs
hwmon: pwm-fan: Fix RPM calculationQuoted 8 lines
Quoted 5 lines
drivers/hwmon/pwm-fan.c | 164 ++++++++++++++++++++++++---------------- 1 file changed, 100 insertions(+), 64 deletions(-)base-commit:
2835d860d3fcc3e4829e96987544e811d35dee48Ping on these, I've not had any feedback for 2 weeks.
Unfortunately the patches are a bit difficult to understand, and I did not have the time to fully analyze them. One thing I can say is that the order should be reversed - the bug fix should come first. Also, patch 2 adds a potential divide by 0 problem, if 'delta' happens to be 0.
Quoted 5 lines
There are little details, such as replacing 'int pulses' with 'unsigned int pulses', which make things more difficult to review.
atomic_tis int. Assigning it to an unsigned int _should_ not matter here, but again such little changes make it more difficult to review a patch because they require extra scrutiny.
Thanks for the feedback, I'll do some rework and re-submit. Probably just the calculation fix first and then the support for multiple fan tachometer inputs as a follow-up.
Signature · 2 lines
Paul Barker Konsulko Group
PATCH 1/2 hwmon: pwm-fan: Support multiple tachometer inputs
In reply to Paul BarkerThe pwm-fan driver is extended to support multiple fan tachometer signals connected to GPIO inputs. This is intended to support the case where a single PWM output signal is routed to multiple fans, each of which have a tachometer output connected back to a GPIO pin.
The number of fan tachometer inputs is determined by the number of interrupt sources configured for the pwm-fan device. The number of pulses-per-revolution entries should match the number of interrupt sources so that each input has a value assigned.
The fan tachometer measurements are exposed as sysfs files fan1_input, fan2_input, etc up to the number of configured inputs. If no inputs are configured then no corresponding sysfs files will be created. This avoids the need for an is_visible function to hide unused fanN_input sysfs files.
--- drivers/hwmon/pwm-fan.c | 165 ++++++++++++++++++++++++---------------- 1 file changed, 101 insertions(+), 64 deletions(-)
drivers/hwmon/pwm-fan.c +101 -64
@@ -22,15 +22,22 @@#define MAX_PWM 255 +struct pwm_fan_tach { +int irq; +atomic_t pulses; +unsigned int rpm; +u8 pulses_per_revolution; + +struct sensor_device_attribute sensor_attr; +}; +struct pwm_fan_ctx {struct mutex lock;struct pwm_device *pwm;struct regulator *reg_en; -int irq; -atomic_t pulses; -unsigned int rpm; -u8 pulses_per_revolution; +int tach_count; +struct pwm_fan_tach *tachs;ktime_t sample_start;struct timer_list rpm_timer;
@@ -39,14 +46,17 @@ struct pwm_fan_ctx {unsigned int pwm_fan_max_state;unsigned int *pwm_fan_cooling_levels;struct thermal_cooling_device *cdev; + +struct attribute_group fan_group; +struct attribute_group *fan_groups[2 ]; };/* This handler assumes self resetting edge triggered interrupt. */ static irqreturn_t pulse_handler (int irq,void *dev_id) { -struct pwm_fan_ctx *ctx = dev_id; +struct pwm_fan_tach *tach = dev_id; -atomic_inc (&ctx->pulses); +atomic_inc (&tach->pulses);return IRQ_HANDLED ; }
@@ -54,16 +64,23 @@ static irqreturn_t pulse_handler(int irq, void *dev_id)static void sample_timer (struct timer_list *t) {struct pwm_fan_ctx *ctx =from_timer (ctx, t, rpm_timer); -int pulses; -u64 tmp; +ktime_t now =ktime_get (); +int i; - pulses =atomic_read (&ctx->pulses); -atomic_sub (pulses, &ctx->pulses); - tmp = (u64 )pulses *ktime_ms_delta (ktime_get (), ctx->sample_start) *60 ; -do_div (tmp, ctx->pulses_per_revolution *1000 ); - ctx->rpm = tmp; +for (i =0 ; i < ctx->tach_count; i++) { +struct pwm_fan_tach *tach; +int pulses; +u64 tmp; + + tach = &ctx->tachs[i]; + pulses =atomic_read (&tach->pulses); +atomic_sub (pulses, &tach->pulses); + tmp = (u64 )pulses *ktime_ms_delta (now, ctx->sample_start) *60 ; +do_div (tmp, tach->pulses_per_revolution *1000 ); + tach->rpm = tmp; + } - ctx->sample_start =ktime_get (); + ctx->sample_start = now;mod_timer (&ctx->rpm_timer, jiffies +HZ ); }
@@ -131,41 +148,13 @@ static ssize_t rpm_show(struct device *dev, struct device_attribute *attr, char *buf) { struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); +struct sensor_device_attribute *sensor_attr =to_sensor_dev_attr (attr); +struct pwm_fan_tach *tach = &ctx->tachs[sensor_attr->index]; -return sprintf (buf,"%u\n" , ctx->rpm); +return sprintf (buf,"%u\n" , tach->rpm); } static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0); -static SENSOR_DEVICE_ATTR_RO(fan1_input, rpm, 0); - -static struct attribute *pwm_fan_attrs[] = { - &sensor_dev_attr_pwm1.dev_attr.attr, - &sensor_dev_attr_fan1_input.dev_attr.attr, -NULL , -}; - -static umode_t pwm_fan_attrs_visible (struct kobject *kobj,struct attribute *a, -int n) -{ - struct device *dev = container_of(kobj, struct device, kobj); -struct pwm_fan_ctx *ctx =dev_get_drvdata (dev); - -/* Hide fan_input in case no interrupt is available */ -if (n ==1 && ctx->irq <=0 ) -return 0 ; - -return a->mode; -} - -static const struct attribute_group pwm_fan_group = { - .attrs = pwm_fan_attrs, - .is_visible = pwm_fan_attrs_visible, -}; - -static const struct attribute_group *pwm_fan_groups[] = { - &pwm_fan_group, -NULL , -};/* thermal cooling device callbacks */ static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
@@ -284,7 +273,8 @@ static int pwm_fan_probe(struct platform_device *pdev)struct device *hwmon;int ret;struct pwm_state state = { }; -u32 ppr =2 ; +size_t sz; +unsigned int i; ctx =devm_kzalloc (dev,sizeof (*ctx),GFP_KERNEL );if (!ctx)
@@ -298,10 +288,6 @@ static int pwm_fan_probe(struct platform_device *pdev)platform_set_drvdata (pdev, ctx); - ctx->irq =platform_get_irq_optional (pdev,0 ); -if (ctx->irq == -EPROBE_DEFER ) -return ctx->irq; - ctx->reg_en =devm_regulator_get_optional (dev,"fan" );if (IS_ERR (ctx->reg_en)) {if (PTR_ERR (ctx->reg_en) != -ENODEV )
@@ -337,26 +323,77 @@ static int pwm_fan_probe(struct platform_device *pdev)if (ret)return ret; -of_property_read_u32 (dev->of_node,"pulses-per-revolution" , &ppr); - ctx->pulses_per_revolution = ppr; -if (!ctx->pulses_per_revolution) { -dev_err (dev,"pulses-per-revolution can't be zero.\n" ); -return -EINVAL ; - } + ctx->tach_count =platform_irq_count (pdev); +if (ctx->tach_count <0 ) +return dev_err_probe (dev, ctx->tach_count, +"Could not get number of fan tachometer inputs\n" ); +dev_dbg (dev,"%d fan tachometer inputs\n" , ctx->tach_count); -if (ctx->irq >0 ) { - ret =devm_request_irq (dev, ctx->irq, pulse_handler,0 , - pdev->name, ctx); -if (ret) { -dev_err (dev,"Failed to request interrupt: %d\n" , ret); -return ret; + sz = (2 + ctx->tach_count) *sizeof (struct attribute *); + ctx->fan_group.attrs =devm_kzalloc (dev, sz,GFP_KERNEL ); +if (!ctx->fan_group.attrs) +return -ENOMEM ; + + sz = ctx->tach_count *sizeof (struct pwm_fan_tach ); + ctx->tachs =devm_kzalloc (dev, sz,GFP_KERNEL ); +if (!ctx->tachs) +return -ENOMEM ; + + ctx->fan_group.attrs[0 ] = &sensor_dev_attr_pwm1.dev_attr.attr; +for (i =0 ; i < ctx->tach_count; i++) { +struct pwm_fan_tach *tach = &ctx->tachs[i]; +u32 ppr =2 ; +char *name; + + tach->irq =platform_get_irq (pdev, i); +if (tach->irq == -EPROBE_DEFER ) +return tach->irq; + +of_property_read_u32_index (dev->of_node, +"pulses-per-revolution" , + i, + &ppr); + tach->pulses_per_revolution = ppr; +if (!tach->pulses_per_revolution) { +dev_err (dev,"pulses-per-revolution can't be zero.\n" ); +return -EINVAL ; } + +if (tach->irq >0 ) { + ret =devm_request_irq (dev, tach->irq, pulse_handler,0 , + pdev->name, tach); +if (ret) { +dev_err (dev, +"Failed to request interrupt: %d\n" , + ret); +return ret; + } + } + +sysfs_attr_init (&tach->sensor_attr.dev_attr.attr); + + name =devm_kzalloc (dev,16 ,GFP_KERNEL ); +snprintf (name,16 ,"fan%d_input" , i +1 ); + tach->sensor_attr.dev_attr.attr.name = name; + tach->sensor_attr.dev_attr.attr.mode =0444 ; + tach->sensor_attr.dev_attr.show = rpm_show; + tach->sensor_attr.index = i; + ctx->fan_group.attrs[i +1 ] = &tach->sensor_attr.dev_attr.attr; + +dev_dbg (dev,"%s: irq=%d, pulses_per_revolution=%d" , + name, tach->irq, tach->pulses_per_revolution); + } + +if (ctx->tach_count >0 ) { ctx->sample_start =ktime_get ();mod_timer (&ctx->rpm_timer, jiffies +HZ ); } - hwmon =devm_hwmon_device_register_with_groups (dev,"pwmfan" , - ctx, pwm_fan_groups); + ctx->fan_groups[0 ] = &ctx->fan_group; + + hwmon =devm_hwmon_device_register_with_groups (dev, +"pwmfan" , ctx, + (const struct attribute_group **)ctx->fan_groups);if (IS_ERR (hwmon)) {dev_err (dev,"Failed to register hwmon device\n" );return PTR_ERR (hwmon);
Signature · 1 line
2.28.0
PATCH 2/2 hwmon: pwm-fan: Fix RPM calculation
In reply to Paul BarkerTo convert the number of pulses counted into an RPM estimation, we need to divide by the width of our measurement interval instead of multiplying by it.
We also don't need to do 64-bit division, with 32-bits we can handle a fan running at over 4 million RPM.
--- drivers/hwmon/pwm-fan.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/hwmon/pwm-fan.c +4 -5
@@ -65,19 +65,18 @@ static void sample_timer(struct timer_list *t) {struct pwm_fan_ctx *ctx =from_timer (ctx, t, rpm_timer);ktime_t now =ktime_get (); +unsigned int delta =ktime_ms_delta (now, ctx->sample_start);int i;for (i =0 ; i < ctx->tach_count; i++) {struct pwm_fan_tach *tach; -int pulses; -u64 tmp; +unsigned int pulses; tach = &ctx->tachs[i]; pulses =atomic_read (&tach->pulses);atomic_sub (pulses, &tach->pulses); - tmp = (u64 )pulses *ktime_ms_delta (now, ctx->sample_start) *60 ; -do_div (tmp, tach->pulses_per_revolution *1000 ); - tach->rpm = tmp; + tach->rpm = (pulses *1000 *60 ) / + (tach->pulses_per_revolution * delta); } ctx->sample_start = now;
Signature · 1 line
2.28.0