Skip to contentSkip to conversation

pwm-fan: Refactor and convert to recommended API

PATCH Superseded Paul Barker 2 participants 5 messages Nov 26, 2020 – Nov 27, 2020 Hardware monitoring

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover pwm-fan: Refactor and convert to recommended API

This series includes the first patch from my previous series adding support for multiple fan tachometers [1] which Guenter has already reviewed, plus a second patch to convert the driver to use the hwmon_device_register_with_info API as requested.

These patches have been tested 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 both with and without a fan tachometer input defined in the device tree.

There shouldn't be any functional change to the driver after these patches, it just puts us in a much better place for further development.

These changes can also be pulled from:

https://gitlab.com/pbarker.dev/staging/linux.git
tag: for-hwmon/pwm-fan-refactor_2020-11-26

Paul Barker (2):
hwmon: pwm-fan: Refactor pwm_fan_probe
hwmon: pwm-fan: Convert to hwmon_device_register_with_info API

 drivers/hwmon/pwm-fan.c | 162 ++++++++++++++++++++++++++--------------
 1 file changed, 104 insertions(+), 58 deletions(-)

base-commit: e6e2c18f63c62df778ce484945fccad088594533

Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/2 hwmon: pwm-fan: Refactor pwm_fan_probe

Use platform_irq_count to determine the number of fan tachometer inputs configured in the device tree. At this stage we support either 0 or 1 inputs.

Once we have this information we only need to read the pulses-per-revolution value if a fan tachometer is actually configured via an IRQ value.

Also add a debug print of the IRQ number and the pulses-per-revolution value to aid in investigating issues.

---
 drivers/hwmon/pwm-fan.c | 50 +++++++++++++++++++++++++++--------------
 1 file changed, 33 insertions(+), 17 deletions(-)
drivers/hwmon/pwm-fan.c +33 -17
index 1f63807c0399..efe2764f42d3 100644
@@ -286,7 +286,7 @@ static int pwm_fan_probe(struct platform_device *pdev)
 	struct device *hwmon;
 	int ret;
 	struct pwm_state state = { };
-	u32 ppr = 2;
+	int tach_count;
 
 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
 	if (!ctx)
@@ -300,10 +300,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)
@@ -339,20 +335,40 @@ 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;
-	}
+	tach_count = platform_irq_count(pdev);
+	if (tach_count < 0)
+		return dev_err_probe(dev, tach_count,
+				     "Could not get number of fan tachometer inputs\n");
+
+	if (tach_count > 0) {
+		u32 ppr = 2;
+
+		ctx->irq = platform_get_irq(pdev, 0);
+		if (ctx->irq == -EPROBE_DEFER)
+			return ctx->irq;
+		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;
+			}
+		}
 
-	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;
+		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;
 		}
+
+		dev_dbg(dev, "tach: irq=%d, pulses_per_revolution=%d\n",
+			ctx->irq, ctx->pulses_per_revolution);
+
 		ctx->sample_start = ktime_get();
 		mod_timer(&ctx->rpm_timer, jiffies + HZ);
 	}
Signature · 1 line
2.26.2
#2 lore raw

PATCH 2/2 hwmon: pwm-fan: Convert to hwmon_device_register_with_info API

In reply to Paul Barker

The pwm-fan driver is updated to use the recommended API.

---
 drivers/hwmon/pwm-fan.c | 112 +++++++++++++++++++++++++---------------
 1 file changed, 71 insertions(+), 41 deletions(-)
drivers/hwmon/pwm-fan.c +71 -41
index efe2764f42d3..c8a1d41d6f06 100644
@@ -8,7 +8,6 @@
  */
 
 #include <linux/hwmon.h>
-#include <linux/hwmon-sysfs.h>
 #include <linux/interrupt.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
@@ -39,6 +38,28 @@ struct pwm_fan_ctx {
 	unsigned int pwm_fan_max_state;
 	unsigned int *pwm_fan_cooling_levels;
 	struct thermal_cooling_device *cdev;
+
+	struct hwmon_chip_info info;
+};
+
+static const u32 pwm_fan_channel_config_pwm[] = {
+	HWMON_PWM_INPUT,
+	0
+};
+
+static const struct hwmon_channel_info pwm_fan_channel_pwm = {
+	.type = hwmon_pwm,
+	.config = pwm_fan_channel_config_pwm,
+};
+
+static const u32 pwm_fan_channel_config_fan[] = {
+	HWMON_F_INPUT,
+	0
+};
+
+static const struct hwmon_channel_info pwm_fan_channel_fan = {
+	.type = hwmon_fan,
+	.config = pwm_fan_channel_config_fan,
 };
 
 /* This handler assumes self resetting edge triggered interrupt. */
@@ -103,14 +124,14 @@ static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
 	ctx->pwm_fan_state = i;
 }
 
-static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
-			 const char *buf, size_t count)
+static int pwm_fan_write(struct device *dev, enum hwmon_sensor_types type,
+			 u32 attr, int channel, long val)
 {
 	struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
-	unsigned long pwm;
+	unsigned long pwm = val;
 	int ret;
 
-	if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
+	if (pwm > MAX_PWM)
 		return -EINVAL;
 
 	ret = __set_pwm(ctx, pwm);
@@ -118,55 +139,50 @@ static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
 		return ret;
 
 	pwm_fan_update_state(ctx, pwm);
-	return count;
+	return 0;
 }
 
-static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
-			char *buf)
+static int pwm_fan_read(struct device *dev, enum hwmon_sensor_types type,
+			u32 attr, int channel, long *val)
 {
 	struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
 
-	return sprintf(buf, "%u\n", ctx->pwm_value);
-}
+	switch (type) {
+	case hwmon_pwm:
+		*val = ctx->pwm_value;
+		return 0;
 
-static ssize_t rpm_show(struct device *dev,
-			struct device_attribute *attr, char *buf)
-{
-	struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
+	case hwmon_fan:
+		*val = ctx->rpm;
+		return 0;
 
-	return sprintf(buf, "%u\n", ctx->rpm);
+	default:
+		return -EINVAL;
+	}
 }
 
-static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
-static SENSOR_DEVICE_ATTR_RO(fan1_input, rpm, 0);
+static umode_t pwm_fan_is_visible(const void *data,
+				  enum hwmon_sensor_types type,
+				  u32 attr, int channel)
+{
+	struct pwm_fan_ctx *ctx = (struct pwm_fan_ctx *)data;
 
-static struct attribute *pwm_fan_attrs[] = {
-	&sensor_dev_attr_pwm1.dev_attr.attr,
-	&sensor_dev_attr_fan1_input.dev_attr.attr,
-	NULL,
-};
+	switch (type) {
+	case hwmon_pwm:
+		return 0644;
 
-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);
+	case hwmon_fan:
+		return 0444;
 
-	/* Hide fan_input in case no interrupt is available  */
-	if (n == 1 && ctx->irq <= 0)
+	default:
 		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,
+static const struct hwmon_ops pwm_fan_hwmon_ops = {
+	.is_visible = pwm_fan_is_visible,
+	.read = pwm_fan_read,
+	.write = pwm_fan_write,
 };
 
 /* thermal cooling device callbacks */
@@ -287,6 +303,8 @@ static int pwm_fan_probe(struct platform_device *pdev)
 	int ret;
 	struct pwm_state state = { };
 	int tach_count;
+	size_t sz;
+	const struct hwmon_channel_info **channels;
 
 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
 	if (!ctx)
@@ -340,6 +358,13 @@ static int pwm_fan_probe(struct platform_device *pdev)
 		return dev_err_probe(dev, tach_count,
 				     "Could not get number of fan tachometer inputs\n");
 
+	sz = (2 + tach_count) * sizeof(struct hwmon_channel_info *);
+	channels = devm_kzalloc(dev, sz, GFP_KERNEL);
+	if (!channels)
+		return -ENOMEM;
+
+	channels[0] = &pwm_fan_channel_pwm;
+
 	if (tach_count > 0) {
 		u32 ppr = 2;
 
@@ -371,10 +396,15 @@ static int pwm_fan_probe(struct platform_device *pdev)
 
 		ctx->sample_start = ktime_get();
 		mod_timer(&ctx->rpm_timer, jiffies + HZ);
+
+		channels[1] = &pwm_fan_channel_fan;
 	}
 
-	hwmon = devm_hwmon_device_register_with_groups(dev, "pwmfan",
-						       ctx, pwm_fan_groups);
+	ctx->info.ops = &pwm_fan_hwmon_ops;
+	ctx->info.info = channels;
+
+	hwmon = devm_hwmon_device_register_with_info(dev, "pwmfan",
+						     ctx, &ctx->info, NULL);
 	if (IS_ERR(hwmon)) {
 		dev_err(dev, "Failed to register hwmon device\n");
 		return PTR_ERR(hwmon);
Signature · 1 line
2.26.2
maintainer
#3 lore raw

Re: [PATCH 2/2] hwmon: pwm-fan: Convert to hwmon_device_register_with_info API

Quoted 36 lines · Paul Barker

The pwm-fan driver is updated to use the recommended API.

---
 drivers/hwmon/pwm-fan.c | 112 +++++++++++++++++++++++++---------------
 1 file changed, 71 insertions(+), 41 deletions(-)
drivers/hwmon/pwm-fan.c +13 -1
index efe2764f42d3..c8a1d41d6f06 100644
@@ -8,7 +8,6 @@
  */
 
 #include <linux/hwmon.h>
-#include <linux/hwmon-sysfs.h>
 #include <linux/interrupt.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
@@ -39,6 +38,28 @@ struct pwm_fan_ctx {
 	unsigned int pwm_fan_max_state;
 	unsigned int *pwm_fan_cooling_levels;
 	struct thermal_cooling_device *cdev;
+
+	struct hwmon_chip_info info;
+};
+
+static const u32 pwm_fan_channel_config_pwm[] = {
+	HWMON_PWM_INPUT,
+	0
+};
+
+static const struct hwmon_channel_info pwm_fan_channel_pwm = {
+	.type = hwmon_pwm,
+	.config = pwm_fan_channel_config_pwm,
+};

drivers/hwmon/pwm-fan.c struct pwm_fan_ctx { · line 63

Quoted 10 lines
drivers/hwmon/pwm-fan.c +9 -0
+
+static const u32 pwm_fan_channel_config_fan[] = {
+	HWMON_F_INPUT,
+	0
+};
+
+static const struct hwmon_channel_info pwm_fan_channel_fan = {
+	.type = hwmon_fan,
+	.config = pwm_fan_channel_config_fan,
 };

In general I'd suggest to use the HWMON_CHANNEL_INFO() macro instead, but I think this is to prepare for a dynamic number of channels. With that in mind, it is ok.

Quoted 9 lines
drivers/hwmon/pwm-fan.c +1 -2
 /* This handler assumes self resetting edge triggered interrupt. */
@@ -103,14 +124,14 @@ static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
 	ctx->pwm_fan_state = i;
 }
 
-static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
-			 const char *buf, size_t count)
+static int pwm_fan_write(struct device *dev, enum hwmon_sensor_types type,

drivers/hwmon/pwm-fan.c static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm) · line 135

Quoted 10 lines
drivers/hwmon/pwm-fan.c +3 -2
+			 u32 attr, int channel, long val)
 {
 	struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
-	unsigned long pwm;
+	unsigned long pwm = val;
 	int ret;
 
-	if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
+	if (pwm > MAX_PWM)
 		return -EINVAL;

This happens to catch negative values, but it is odd and somewhat difficult to review and understand. I would suggest to drop the pwm variable and use

	if (val < 0 || val > MAX_PWM)
		return -EINVAL;

instead.

Quoted 25 lines
drivers/hwmon/pwm-fan.c +7 -6
 	ret = __set_pwm(ctx, pwm);
@@ -118,55 +139,50 @@ static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
 		return ret;
 
 	pwm_fan_update_state(ctx, pwm);
-	return count;
+	return 0;
 }
 
-static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
-			char *buf)
+static int pwm_fan_read(struct device *dev, enum hwmon_sensor_types type,
+			u32 attr, int channel, long *val)
 {
 	struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
 
-	return sprintf(buf, "%u\n", ctx->pwm_value);
-}
+	switch (type) {
+	case hwmon_pwm:
+		*val = ctx->pwm_value;
+		return 0;
 
-static ssize_t rpm_show(struct device *dev,

drivers/hwmon/pwm-fan.c static ssize_t pwm_store(struct device *dev, struct device_attribute *attr, · line 160

Quoted 10 lines
drivers/hwmon/pwm-fan.c +5 -4
-			struct device_attribute *attr, char *buf)
-{
-	struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
+	case hwmon_fan:
+		*val = ctx->rpm;
+		return 0;
 
-	return sprintf(buf, "%u\n", ctx->rpm);
+	default:
+		return -EINVAL;
		return -EOPNOTSUPP;
Quoted 62 lines
drivers/hwmon/pwm-fan.c +19 -24
+	}
 }
 
-static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
-static SENSOR_DEVICE_ATTR_RO(fan1_input, rpm, 0);
+static umode_t pwm_fan_is_visible(const void *data,
+				  enum hwmon_sensor_types type,
+				  u32 attr, int channel)
+{
+	struct pwm_fan_ctx *ctx = (struct pwm_fan_ctx *)data;
 
-static struct attribute *pwm_fan_attrs[] = {
-	&sensor_dev_attr_pwm1.dev_attr.attr,
-	&sensor_dev_attr_fan1_input.dev_attr.attr,
-	NULL,
-};
+	switch (type) {
+	case hwmon_pwm:
+		return 0644;
 
-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);
+	case hwmon_fan:
+		return 0444;
 
-	/* Hide fan_input in case no interrupt is available  */
-	if (n == 1 && ctx->irq <= 0)
+	default:
 		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,
+static const struct hwmon_ops pwm_fan_hwmon_ops = {
+	.is_visible = pwm_fan_is_visible,
+	.read = pwm_fan_read,
+	.write = pwm_fan_write,
 };
 
 /* thermal cooling device callbacks */
@@ -287,6 +303,8 @@ static int pwm_fan_probe(struct platform_device *pdev)
 	int ret;
 	struct pwm_state state = { };
 	int tach_count;
+	size_t sz;
+	const struct hwmon_channel_info **channels;
 
 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
 	if (!ctx)

drivers/hwmon/pwm-fan.c static int pwm_fan_probe(struct platform_device *pdev) · line 362

Quoted 5 lines
drivers/hwmon/pwm-fan.c +2 -0
@@ -340,6 +358,13 @@ static int pwm_fan_probe(struct platform_device *pdev)
 		return dev_err_probe(dev, tach_count,
 				     "Could not get number of fan tachometer inputs\n");
 
+	sz = (2 + tach_count) * sizeof(struct hwmon_channel_info *);
+	channels = devm_kzalloc(dev, sz, GFP_KERNEL);

devm_kcalloc() is preferred.

	channels = devm_kcalloc(dev, tach_count + 2,
				sizeof(struct hwmon_channel_info *), GFP_KERNEL);
Quoted 26 lines
drivers/hwmon/pwm-fan.c +12 -2
+	if (!channels)
+		return -ENOMEM;
+
+	channels[0] = &pwm_fan_channel_pwm;
+
 	if (tach_count > 0) {
 		u32 ppr = 2;
 
@@ -371,10 +396,15 @@ static int pwm_fan_probe(struct platform_device *pdev)
 
 		ctx->sample_start = ktime_get();
 		mod_timer(&ctx->rpm_timer, jiffies + HZ);
+
+		channels[1] = &pwm_fan_channel_fan;
 	}
 
-	hwmon = devm_hwmon_device_register_with_groups(dev, "pwmfan",
-						       ctx, pwm_fan_groups);
+	ctx->info.ops = &pwm_fan_hwmon_ops;
+	ctx->info.info = channels;
+
+	hwmon = devm_hwmon_device_register_with_info(dev, "pwmfan",
+						     ctx, &ctx->info, NULL);
 	if (IS_ERR(hwmon)) {
 		dev_err(dev, "Failed to register hwmon device\n");
 		return PTR_ERR(hwmon);
#4 lore raw

Re: [PATCH 2/2] hwmon: pwm-fan: Convert to hwmon_device_register_with_info API

Quoted 205 lines · Guenter Roeck
Quoted 36 lines · Paul Barker

The pwm-fan driver is updated to use the recommended API.

---
 drivers/hwmon/pwm-fan.c | 112 +++++++++++++++++++++++++---------------
 1 file changed, 71 insertions(+), 41 deletions(-)
drivers/hwmon/pwm-fan.c +13 -1
index efe2764f42d3..c8a1d41d6f06 100644
@@ -8,7 +8,6 @@
  */
 
 #include <linux/hwmon.h>
-#include <linux/hwmon-sysfs.h>
 #include <linux/interrupt.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
@@ -39,6 +38,28 @@ struct pwm_fan_ctx {
      unsigned int pwm_fan_max_state;
      unsigned int *pwm_fan_cooling_levels;
      struct thermal_cooling_device *cdev;
+
+     struct hwmon_chip_info info;
+};
+
+static const u32 pwm_fan_channel_config_pwm[] = {
+     HWMON_PWM_INPUT,
+     0
+};
+
+static const struct hwmon_channel_info pwm_fan_channel_pwm = {
+     .type = hwmon_pwm,
+     .config = pwm_fan_channel_config_pwm,
+};

drivers/hwmon/pwm-fan.c struct pwm_fan_ctx { · line 63

Quoted 10 lines
drivers/hwmon/pwm-fan.c +9 -0
+
+static const u32 pwm_fan_channel_config_fan[] = {
+     HWMON_F_INPUT,
+     0
+};
+
+static const struct hwmon_channel_info pwm_fan_channel_fan = {
+     .type = hwmon_fan,
+     .config = pwm_fan_channel_config_fan,
 };

In general I'd suggest to use the HWMON_CHANNEL_INFO() macro instead, but I think this is to prepare for a dynamic number of channels. With that in mind, it is ok.

Quoted 9 lines
drivers/hwmon/pwm-fan.c +1 -2
 /* This handler assumes self resetting edge triggered interrupt. */
@@ -103,14 +124,14 @@ static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
      ctx->pwm_fan_state = i;
 }
 
-static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
-                      const char *buf, size_t count)
+static int pwm_fan_write(struct device *dev, enum hwmon_sensor_types type,

drivers/hwmon/pwm-fan.c static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm) · line 135

Quoted 10 lines
drivers/hwmon/pwm-fan.c +3 -2
+                      u32 attr, int channel, long val)
 {
      struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
-     unsigned long pwm;
+     unsigned long pwm = val;
      int ret;
 
-     if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
+     if (pwm > MAX_PWM)
              return -EINVAL;

This happens to catch negative values, but it is odd and somewhat difficult to review and understand. I would suggest to drop the pwm variable and use

        if (val < 0 || val > MAX_PWM)
                return -EINVAL;

instead.

Quoted 25 lines
drivers/hwmon/pwm-fan.c +7 -6
      ret = __set_pwm(ctx, pwm);
@@ -118,55 +139,50 @@ static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
              return ret;
 
      pwm_fan_update_state(ctx, pwm);
-     return count;
+     return 0;
 }
 
-static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
-                     char *buf)
+static int pwm_fan_read(struct device *dev, enum hwmon_sensor_types type,
+                     u32 attr, int channel, long *val)
 {
      struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
 
-     return sprintf(buf, "%u\n", ctx->pwm_value);
-}
+     switch (type) {
+     case hwmon_pwm:
+             *val = ctx->pwm_value;
+             return 0;
 
-static ssize_t rpm_show(struct device *dev,

drivers/hwmon/pwm-fan.c static ssize_t pwm_store(struct device *dev, struct device_attribute *attr, · line 160

Quoted 10 lines
drivers/hwmon/pwm-fan.c +5 -4
-                     struct device_attribute *attr, char *buf)
-{
-     struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
+     case hwmon_fan:
+             *val = ctx->rpm;
+             return 0;
 
-     return sprintf(buf, "%u\n", ctx->rpm);
+     default:
+             return -EINVAL;
                return -EOPNOTSUPP;
Quoted 62 lines
drivers/hwmon/pwm-fan.c +19 -24
+     }
 }
 
-static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
-static SENSOR_DEVICE_ATTR_RO(fan1_input, rpm, 0);
+static umode_t pwm_fan_is_visible(const void *data,
+                               enum hwmon_sensor_types type,
+                               u32 attr, int channel)
+{
+     struct pwm_fan_ctx *ctx = (struct pwm_fan_ctx *)data;
 
-static struct attribute *pwm_fan_attrs[] = {
-     &sensor_dev_attr_pwm1.dev_attr.attr,
-     &sensor_dev_attr_fan1_input.dev_attr.attr,
-     NULL,
-};
+     switch (type) {
+     case hwmon_pwm:
+             return 0644;
 
-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);
+     case hwmon_fan:
+             return 0444;
 
-     /* Hide fan_input in case no interrupt is available  */
-     if (n == 1 && ctx->irq <= 0)
+     default:
              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,
+static const struct hwmon_ops pwm_fan_hwmon_ops = {
+     .is_visible = pwm_fan_is_visible,
+     .read = pwm_fan_read,
+     .write = pwm_fan_write,
 };
 
 /* thermal cooling device callbacks */
@@ -287,6 +303,8 @@ static int pwm_fan_probe(struct platform_device *pdev)
      int ret;
      struct pwm_state state = { };
      int tach_count;
+     size_t sz;
+     const struct hwmon_channel_info **channels;
 
      ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
      if (!ctx)

drivers/hwmon/pwm-fan.c static int pwm_fan_probe(struct platform_device *pdev) · line 362

Quoted 5 lines
drivers/hwmon/pwm-fan.c +2 -0
@@ -340,6 +358,13 @@ static int pwm_fan_probe(struct platform_device *pdev)
              return dev_err_probe(dev, tach_count,
                                   "Could not get number of fan tachometer inputs\n");
 
+     sz = (2 + tach_count) * sizeof(struct hwmon_channel_info *);
+     channels = devm_kzalloc(dev, sz, GFP_KERNEL);

devm_kcalloc() is preferred.
channels = devm_kcalloc(dev, tach_count + 2,
sizeof(struct hwmon_channel_info *), GFP_KERNEL);

Quoted 16 lines
drivers/hwmon/pwm-fan.c +7 -0
+     if (!channels)
+             return -ENOMEM;
+
+     channels[0] = &pwm_fan_channel_pwm;
+
      if (tach_count > 0) {
              u32 ppr = 2;
 
@@ -371,10 +396,15 @@ static int pwm_fan_probe(struct platform_device *pdev)
 
              ctx->sample_start = ktime_get();
              mod_timer(&ctx->rpm_timer, jiffies + HZ);
+
+             channels[1] = &pwm_fan_channel_fan;
      }
 
Quoted 10 lines

drivers/hwmon/pwm-fan.c static int pwm_fan_probe(struct platform_device *pdev) · line 410

Quoted 10 lines
drivers/hwmon/pwm-fan.c +5 -2
-     hwmon = devm_hwmon_device_register_with_groups(dev, "pwmfan",
-                                                    ctx, pwm_fan_groups);
+     ctx->info.ops = &pwm_fan_hwmon_ops;
+     ctx->info.info = channels;
+
+     hwmon = devm_hwmon_device_register_with_info(dev, "pwmfan",
+                                                  ctx, &ctx->info, NULL);
      if (IS_ERR(hwmon)) {
              dev_err(dev, "Failed to register hwmon device\n");
              return PTR_ERR(hwmon);

Thanks for the review. Those are all straightforward changes so I'll make them and send a v2.

Signature · 2 lines
Paul Barker
Konsulko Group