pwm: Give some sysfs related variables and functions better names
authorUwe Kleine-König <u.kleine-koenig@pengutronix.de>
Sun, 17 Mar 2024 10:40:33 +0000 (11:40 +0100)
committerUwe Kleine-König <u.kleine-koenig@pengutronix.de>
Fri, 26 Apr 2024 19:29:17 +0000 (21:29 +0200)
The code handling the sysfs API uses "child" and "parent" to refer to
the devices corresponding to a struct pwm or a struct pwm_chip
respectively.

Other parts of the pwm core use "parent" to refer to the parent device of
a struct pwm_chip.

So rename "child" to "pwm_dev" and "parent" to "pwmchip_dev" which
better explains the semantic. Also two functions are changed to match
the new names:

        child_to_pwm_export() -> pwmexport_from_dev()
        child_to_pwm_device() -> pwm_from_dev()

(which have the additional advantage to start with "pwm" which gives the
right scope). Additionally introduce a wrapper for dev_get_drvdata() to
convert a pwmchip_dev to the respective pwm_chip.

Link: https://lore.kernel.org/r/9cc05aceeae2f06ecb850bccb15ba821e768c183.1710670958.git.u.kleine-koenig@pengutronix.de
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
drivers/pwm/sysfs.c

index 3f434a771fb52de3f0249f1f4f673906347c0036..1a1b1f6dd98f6804fde49d1bfc66ba7f5519fd42 100644 (file)
 #include <linux/pwm.h>
 
 struct pwm_export {
-       struct device child;
+       struct device pwm_dev;
        struct pwm_device *pwm;
        struct mutex lock;
        struct pwm_state suspend;
 };
 
-static struct pwm_export *child_to_pwm_export(struct device *child)
+static inline struct pwm_chip *pwmchip_from_dev(struct device *pwmchip_dev)
 {
-       return container_of(child, struct pwm_export, child);
+       return dev_get_drvdata(pwmchip_dev);
 }
 
-static struct pwm_device *child_to_pwm_device(struct device *child)
+static inline struct pwm_export *pwmexport_from_dev(struct device *pwm_dev)
 {
-       struct pwm_export *export = child_to_pwm_export(child);
+       return container_of(pwm_dev, struct pwm_export, pwm_dev);
+}
+
+static inline struct pwm_device *pwm_from_dev(struct device *pwm_dev)
+{
+       struct pwm_export *export = pwmexport_from_dev(pwm_dev);
 
        return export->pwm;
 }
 
-static ssize_t period_show(struct device *child,
+static ssize_t period_show(struct device *pwm_dev,
                           struct device_attribute *attr,
                           char *buf)
 {
-       const struct pwm_device *pwm = child_to_pwm_device(child);
+       const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
        struct pwm_state state;
 
        pwm_get_state(pwm, &state);
@@ -45,11 +50,11 @@ static ssize_t period_show(struct device *child,
        return sysfs_emit(buf, "%llu\n", state.period);
 }
 
-static ssize_t period_store(struct device *child,
+static ssize_t period_store(struct device *pwm_dev,
                            struct device_attribute *attr,
                            const char *buf, size_t size)
 {
-       struct pwm_export *export = child_to_pwm_export(child);
+       struct pwm_export *export = pwmexport_from_dev(pwm_dev);
        struct pwm_device *pwm = export->pwm;
        struct pwm_state state;
        u64 val;
@@ -68,11 +73,11 @@ static ssize_t period_store(struct device *child,
        return ret ? : size;
 }
 
-static ssize_t duty_cycle_show(struct device *child,
+static ssize_t duty_cycle_show(struct device *pwm_dev,
                               struct device_attribute *attr,
                               char *buf)
 {
-       const struct pwm_device *pwm = child_to_pwm_device(child);
+       const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
        struct pwm_state state;
 
        pwm_get_state(pwm, &state);
@@ -80,11 +85,11 @@ static ssize_t duty_cycle_show(struct device *child,
        return sysfs_emit(buf, "%llu\n", state.duty_cycle);
 }
 
-static ssize_t duty_cycle_store(struct device *child,
+static ssize_t duty_cycle_store(struct device *pwm_dev,
                                struct device_attribute *attr,
                                const char *buf, size_t size)
 {
-       struct pwm_export *export = child_to_pwm_export(child);
+       struct pwm_export *export = pwmexport_from_dev(pwm_dev);
        struct pwm_device *pwm = export->pwm;
        struct pwm_state state;
        u64 val;
@@ -103,11 +108,11 @@ static ssize_t duty_cycle_store(struct device *child,
        return ret ? : size;
 }
 
-static ssize_t enable_show(struct device *child,
+static ssize_t enable_show(struct device *pwm_dev,
                           struct device_attribute *attr,
                           char *buf)
 {
-       const struct pwm_device *pwm = child_to_pwm_device(child);
+       const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
        struct pwm_state state;
 
        pwm_get_state(pwm, &state);
@@ -115,11 +120,11 @@ static ssize_t enable_show(struct device *child,
        return sysfs_emit(buf, "%d\n", state.enabled);
 }
 
-static ssize_t enable_store(struct device *child,
+static ssize_t enable_store(struct device *pwm_dev,
                            struct device_attribute *attr,
                            const char *buf, size_t size)
 {
-       struct pwm_export *export = child_to_pwm_export(child);
+       struct pwm_export *export = pwmexport_from_dev(pwm_dev);
        struct pwm_device *pwm = export->pwm;
        struct pwm_state state;
        int val, ret;
@@ -151,11 +156,11 @@ unlock:
        return ret ? : size;
 }
 
-static ssize_t polarity_show(struct device *child,
+static ssize_t polarity_show(struct device *pwm_dev,
                             struct device_attribute *attr,
                             char *buf)
 {
-       const struct pwm_device *pwm = child_to_pwm_device(child);
+       const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
        const char *polarity = "unknown";
        struct pwm_state state;
 
@@ -174,11 +179,11 @@ static ssize_t polarity_show(struct device *child,
        return sysfs_emit(buf, "%s\n", polarity);
 }
 
-static ssize_t polarity_store(struct device *child,
+static ssize_t polarity_store(struct device *pwm_dev,
                              struct device_attribute *attr,
                              const char *buf, size_t size)
 {
-       struct pwm_export *export = child_to_pwm_export(child);
+       struct pwm_export *export = pwmexport_from_dev(pwm_dev);
        struct pwm_device *pwm = export->pwm;
        enum pwm_polarity polarity;
        struct pwm_state state;
@@ -200,11 +205,11 @@ static ssize_t polarity_store(struct device *child,
        return ret ? : size;
 }
 
-static ssize_t capture_show(struct device *child,
+static ssize_t capture_show(struct device *pwm_dev,
                            struct device_attribute *attr,
                            char *buf)
 {
-       struct pwm_device *pwm = child_to_pwm_device(child);
+       struct pwm_device *pwm = pwm_from_dev(pwm_dev);
        struct pwm_capture result;
        int ret;
 
@@ -231,14 +236,14 @@ static struct attribute *pwm_attrs[] = {
 };
 ATTRIBUTE_GROUPS(pwm);
 
-static void pwm_export_release(struct device *child)
+static void pwm_export_release(struct device *pwm_dev)
 {
-       struct pwm_export *export = child_to_pwm_export(child);
+       struct pwm_export *export = pwmexport_from_dev(pwm_dev);
 
        kfree(export);
 }
 
-static int pwm_export_child(struct device *parent, struct pwm_device *pwm)
+static int pwm_export_child(struct device *pwmchip_dev, struct pwm_device *pwm)
 {
        struct pwm_export *export;
        char *pwm_prop[2];
@@ -256,62 +261,62 @@ static int pwm_export_child(struct device *parent, struct pwm_device *pwm)
        export->pwm = pwm;
        mutex_init(&export->lock);
 
-       export->child.release = pwm_export_release;
-       export->child.parent = parent;
-       export->child.devt = MKDEV(0, 0);
-       export->child.groups = pwm_groups;
-       dev_set_name(&export->child, "pwm%u", pwm->hwpwm);
+       export->pwm_dev.release = pwm_export_release;
+       export->pwm_dev.parent = pwmchip_dev;
+       export->pwm_dev.devt = MKDEV(0, 0);
+       export->pwm_dev.groups = pwm_groups;
+       dev_set_name(&export->pwm_dev, "pwm%u", pwm->hwpwm);
 
-       ret = device_register(&export->child);
+       ret = device_register(&export->pwm_dev);
        if (ret) {
                clear_bit(PWMF_EXPORTED, &pwm->flags);
-               put_device(&export->child);
+               put_device(&export->pwm_dev);
                export = NULL;
                return ret;
        }
        pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm);
        pwm_prop[1] = NULL;
-       kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop);
+       kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop);
        kfree(pwm_prop[0]);
 
        return 0;
 }
 
-static int pwm_unexport_match(struct device *child, void *data)
+static int pwm_unexport_match(struct device *pwm_dev, void *data)
 {
-       return child_to_pwm_device(child) == data;
+       return pwm_from_dev(pwm_dev) == data;
 }
 
-static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm)
+static int pwm_unexport_child(struct device *pwmchip_dev, struct pwm_device *pwm)
 {
-       struct device *child;
+       struct device *pwm_dev;
        char *pwm_prop[2];
 
        if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
                return -ENODEV;
 
-       child = device_find_child(parent, pwm, pwm_unexport_match);
-       if (!child)
+       pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match);
+       if (!pwm_dev)
                return -ENODEV;
 
        pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm);
        pwm_prop[1] = NULL;
-       kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop);
+       kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop);
        kfree(pwm_prop[0]);
 
        /* for device_find_child() */
-       put_device(child);
-       device_unregister(child);
+       put_device(pwm_dev);
+       device_unregister(pwm_dev);
        pwm_put(pwm);
 
        return 0;
 }
 
-static ssize_t export_store(struct device *parent,
+static ssize_t export_store(struct device *pwmchip_dev,
                            struct device_attribute *attr,
                            const char *buf, size_t len)
 {
-       struct pwm_chip *chip = dev_get_drvdata(parent);
+       struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
        struct pwm_device *pwm;
        unsigned int hwpwm;
        int ret;
@@ -327,7 +332,7 @@ static ssize_t export_store(struct device *parent,
        if (IS_ERR(pwm))
                return PTR_ERR(pwm);
 
-       ret = pwm_export_child(parent, pwm);
+       ret = pwm_export_child(pwmchip_dev, pwm);
        if (ret < 0)
                pwm_put(pwm);
 
@@ -335,11 +340,11 @@ static ssize_t export_store(struct device *parent,
 }
 static DEVICE_ATTR_WO(export);
 
-static ssize_t unexport_store(struct device *parent,
+static ssize_t unexport_store(struct device *pwmchip_dev,
                              struct device_attribute *attr,
                              const char *buf, size_t len)
 {
-       struct pwm_chip *chip = dev_get_drvdata(parent);
+       struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
        unsigned int hwpwm;
        int ret;
 
@@ -350,16 +355,16 @@ static ssize_t unexport_store(struct device *parent,
        if (hwpwm >= chip->npwm)
                return -ENODEV;
 
-       ret = pwm_unexport_child(parent, &chip->pwms[hwpwm]);
+       ret = pwm_unexport_child(pwmchip_dev, &chip->pwms[hwpwm]);
 
        return ret ? : len;
 }
 static DEVICE_ATTR_WO(unexport);
 
-static ssize_t npwm_show(struct device *parent, struct device_attribute *attr,
+static ssize_t npwm_show(struct device *pwmchip_dev, struct device_attribute *attr,
                         char *buf)
 {
-       const struct pwm_chip *chip = dev_get_drvdata(parent);
+       const struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
 
        return sysfs_emit(buf, "%u\n", chip->npwm);
 }
@@ -374,22 +379,22 @@ static struct attribute *pwm_chip_attrs[] = {
 ATTRIBUTE_GROUPS(pwm_chip);
 
 /* takes export->lock on success */
-static struct pwm_export *pwm_class_get_state(struct device *parent,
+static struct pwm_export *pwm_class_get_state(struct device *pwmchip_dev,
                                              struct pwm_device *pwm,
                                              struct pwm_state *state)
 {
-       struct device *child;
+       struct device *pwm_dev;
        struct pwm_export *export;
 
        if (!test_bit(PWMF_EXPORTED, &pwm->flags))
                return NULL;
 
-       child = device_find_child(parent, pwm, pwm_unexport_match);
-       if (!child)
+       pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match);
+       if (!pwm_dev)
                return NULL;
 
-       export = child_to_pwm_export(child);
-       put_device(child);      /* for device_find_child() */
+       export = pwmexport_from_dev(pwm_dev);
+       put_device(pwm_dev);    /* for device_find_child() */
 
        mutex_lock(&export->lock);
        pwm_get_state(pwm, state);
@@ -409,9 +414,9 @@ static int pwm_class_apply_state(struct pwm_export *export,
        return ret;
 }
 
-static int pwm_class_resume_npwm(struct device *parent, unsigned int npwm)
+static int pwm_class_resume_npwm(struct device *pwmchip_dev, unsigned int npwm)
 {
-       struct pwm_chip *chip = dev_get_drvdata(parent);
+       struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
        unsigned int i;
        int ret = 0;
 
@@ -420,7 +425,7 @@ static int pwm_class_resume_npwm(struct device *parent, unsigned int npwm)
                struct pwm_state state;
                struct pwm_export *export;
 
-               export = pwm_class_get_state(parent, pwm, &state);
+               export = pwm_class_get_state(pwmchip_dev, pwm, &state);
                if (!export)
                        continue;
 
@@ -440,9 +445,9 @@ static int pwm_class_resume_npwm(struct device *parent, unsigned int npwm)
        return ret;
 }
 
-static int pwm_class_suspend(struct device *parent)
+static int pwm_class_suspend(struct device *pwmchip_dev)
 {
-       struct pwm_chip *chip = dev_get_drvdata(parent);
+       struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
        unsigned int i;
        int ret = 0;
 
@@ -451,7 +456,7 @@ static int pwm_class_suspend(struct device *parent)
                struct pwm_state state;
                struct pwm_export *export;
 
-               export = pwm_class_get_state(parent, pwm, &state);
+               export = pwm_class_get_state(pwmchip_dev, pwm, &state);
                if (!export)
                        continue;
 
@@ -473,7 +478,7 @@ static int pwm_class_suspend(struct device *parent)
                         * roll back the PWM devices that were disabled by
                         * this suspend function.
                         */
-                       pwm_class_resume_npwm(parent, i);
+                       pwm_class_resume_npwm(pwmchip_dev, i);
                        break;
                }
        }
@@ -481,11 +486,11 @@ static int pwm_class_suspend(struct device *parent)
        return ret;
 }
 
-static int pwm_class_resume(struct device *parent)
+static int pwm_class_resume(struct device *pwmchip_dev)
 {
-       struct pwm_chip *chip = dev_get_drvdata(parent);
+       struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
 
-       return pwm_class_resume_npwm(parent, chip->npwm);
+       return pwm_class_resume_npwm(pwmchip_dev, chip->npwm);
 }
 
 static DEFINE_SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume);
@@ -496,22 +501,22 @@ static struct class pwm_class = {
        .pm = pm_sleep_ptr(&pwm_class_pm_ops),
 };
 
-static int pwmchip_sysfs_match(struct device *parent, const void *data)
+static int pwmchip_sysfs_match(struct device *pwmchip_dev, const void *data)
 {
-       return dev_get_drvdata(parent) == data;
+       return pwmchip_from_dev(pwmchip_dev) == data;
 }
 
 void pwmchip_sysfs_export(struct pwm_chip *chip)
 {
-       struct device *parent;
+       struct device *pwmchip_dev;
 
        /*
         * If device_create() fails the pwm_chip is still usable by
         * the kernel it's just not exported.
         */
-       parent = device_create(&pwm_class, pwmchip_parent(chip), MKDEV(0, 0), chip,
+       pwmchip_dev = device_create(&pwm_class, pwmchip_parent(chip), MKDEV(0, 0), chip,
                               "pwmchip%d", chip->id);
-       if (IS_ERR(parent)) {
+       if (IS_ERR(pwmchip_dev)) {
                dev_warn(pwmchip_parent(chip),
                         "device_create failed for pwm_chip sysfs export\n");
        }
@@ -519,23 +524,23 @@ void pwmchip_sysfs_export(struct pwm_chip *chip)
 
 void pwmchip_sysfs_unexport(struct pwm_chip *chip)
 {
-       struct device *parent;
+       struct device *pwmchip_dev;
        unsigned int i;
 
-       parent = class_find_device(&pwm_class, NULL, chip,
+       pwmchip_dev = class_find_device(&pwm_class, NULL, chip,
                                   pwmchip_sysfs_match);
-       if (!parent)
+       if (!pwmchip_dev)
                return;
 
        for (i = 0; i < chip->npwm; i++) {
                struct pwm_device *pwm = &chip->pwms[i];
 
                if (test_bit(PWMF_EXPORTED, &pwm->flags))
-                       pwm_unexport_child(parent, pwm);
+                       pwm_unexport_child(pwmchip_dev, pwm);
        }
 
-       put_device(parent);
-       device_unregister(parent);
+       put_device(pwmchip_dev);
+       device_unregister(pwmchip_dev);
 }
 
 static int __init pwm_sysfs_init(void)