pwm: sysfs: Switch to DEFINE_SIMPLE_DEV_PM_OPS() and pm_sleep_ptr()
PATCH 1/4 pwm: sysfs: Switch to DEFINE_SIMPLE_DEV_PM_OPS() and pm_sleep_ptr()
Using these newer macros allows the compiler to remove the unused structure and functions when !CONFIG_PM_SLEEP + removes the need to mark pm functions __maybe_unused.
--- drivers/pwm/sysfs.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/pwm/sysfs.c +4 -4
@@ -433,7 +433,7 @@ static int pwm_class_resume_npwm(struct device *parent, unsigned int npwm)return ret; } -static int __maybe_unused pwm_class_suspend (struct device *parent) +static int pwm_class_suspend (struct device *parent) {struct pwm_chip *chip =dev_get_drvdata (parent);unsigned int i;
@@ -464,20 +464,20 @@ static int __maybe_unused pwm_class_suspend(struct device *parent)return ret; } -static int __maybe_unused pwm_class_resume (struct device *parent) +static int pwm_class_resume (struct device *parent) {struct pwm_chip *chip =dev_get_drvdata (parent);return pwm_class_resume_npwm (parent, chip->npwm); } -static SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume); +static DEFINE_SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume);static struct class pwm_class = { .name ="pwm" , .owner =THIS_MODULE , .dev_groups = pwm_chip_groups, - .pm = &pwm_class_pm_ops, + .pm =pm_sleep_ptr (&pwm_class_pm_ops), };static int pwmchip_sysfs_match (struct device *parent,const void *data)
Signature · 1 line
2.35.1
PATCH 2/4 pwm: sysfs: Return directly from the for-loop in PM callbacks
There is no need to assign ret to 0 and then break the loop just for returning the error to the caller. Instead, return directly from the for-loop, and 0 otherwise.
--- drivers/pwm/sysfs.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/pwm/sysfs.c +6 -6
@@ -413,7 +413,7 @@ static int pwm_class_resume_npwm(struct device *parent, unsigned int npwm) {struct pwm_chip *chip =dev_get_drvdata (parent);unsigned int i; -int ret =0 ; +int ret;for (i =0 ; i < npwm; i++) {struct pwm_device *pwm = &chip->pwms[i];
@@ -427,17 +427,17 @@ static int pwm_class_resume_npwm(struct device *parent, unsigned int npwm) state.enabled = export->suspend.enabled; ret =pwm_class_apply_state (export, pwm, &state);if (ret <0 ) -break ; +return ret; } -return ret; +return 0 ; }static int pwm_class_suspend (struct device *parent) {struct pwm_chip *chip =dev_get_drvdata (parent);unsigned int i; -int ret =0 ; +int ret;for (i =0 ; i < chip->npwm; i++) {struct pwm_device *pwm = &chip->pwms[i];
@@ -457,11 +457,11 @@ static int pwm_class_suspend(struct device *parent)* this suspend function. */ pwm_class_resume_npwm (parent, i); -break ; +return ret; } } -return ret; +return 0 ; }static int pwm_class_resume (struct device *parent)
Signature · 1 line
2.35.1
Re: [PATCH v1 2/4] pwm: sysfs: Return directly from the for-loop in PM callbacks
Quoted 5 lines · Andy Shevchenko
There is no need to assign ret to 0 and then break the loop just for returning the error to the caller. Instead, return directly from the for-loop, and 0 otherwise.
Signed-off-by Andy Shevchenko <[email protected]>
Thanks!
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
PATCH 3/4 pwm: sysfs: Replace sprintf() with sysfs_emit()
In reply to Andy ShevchenkoFor sysfs outputs, it's safer to use a new helper, sysfs_emit(), instead of the raw sprintf() & co. This patch replaces such a sprintf() call straightforwardly with the new helper.
--- drivers/pwm/sysfs.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/pwm/sysfs.c +6 -6
@@ -42,7 +42,7 @@ static ssize_t period_show(struct device *child,pwm_get_state (pwm, &state); -return sprintf (buf,"%llu\n" , state.period); +return sysfs_emit (buf,"%llu\n" , state.period); } static ssize_t period_store(struct device *child,
@@ -77,7 +77,7 @@ static ssize_t duty_cycle_show(struct device *child,pwm_get_state (pwm, &state); -return sprintf (buf,"%llu\n" , state.duty_cycle); +return sysfs_emit (buf,"%llu\n" , state.duty_cycle); } static ssize_t duty_cycle_store(struct device *child,
@@ -112,7 +112,7 @@ static ssize_t enable_show(struct device *child,pwm_get_state (pwm, &state); -return sprintf (buf,"%d\n" , state.enabled); +return sysfs_emit (buf,"%d\n" , state.enabled); } static ssize_t enable_store(struct device *child,
@@ -171,7 +171,7 @@ static ssize_t polarity_show(struct device *child,break ; } -return sprintf (buf,"%s\n" , polarity); +return sysfs_emit (buf,"%s\n" , polarity); } static ssize_t polarity_store(struct device *child,
@@ -212,7 +212,7 @@ static ssize_t capture_show(struct device *child,if (ret)return ret; -return sprintf (buf,"%u %u\n" , result.period, result.duty_cycle); +return sysfs_emit (buf,"%u %u\n" , result.period, result.duty_cycle); }static DEVICE_ATTR_RW (period);
@@ -361,7 +361,7 @@ static ssize_t npwm_show(struct device *parent, struct device_attribute *attr, {const struct pwm_chip *chip =dev_get_drvdata (parent); -return sprintf (buf,"%u\n" , chip->npwm); +return sysfs_emit (buf,"%u\n" , chip->npwm); }static DEVICE_ATTR_RO (npwm);
Signature · 1 line
2.35.1
Re: [PATCH v1 3/4] pwm: sysfs: Replace sprintf() with sysfs_emit()
Hello,
Quoted 3 lines · Andy Shevchenko
For sysfs outputs, it's safer to use a new helper,
sysfs_emit(), instead of the rawsprintf()& co. This patch replaces such asprintf()call straightforwardly with the new helper.
TIL!
There is no actual harm in using sprintf, but for consistency and to provide a good reference for copying, conversion to sysfs_emit() seems sensible.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
PATCH 4/4 pwm: sysfs: Utilize an array for polarity strings
In reply to Andy ShevchenkoCode is smaller and looks nicer if we combine polarity strings into an array.
--- drivers/pwm/sysfs.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-)
drivers/pwm/sysfs.c +12 -20
@@ -151,27 +151,23 @@ static ssize_t enable_store(struct device *child,return ret ? : size; } +static const char *const polarity_strings[] = { + [PWM_POLARITY_NORMAL ] ="normal" , + [PWM_POLARITY_INVERSED ] ="inversed" , +}; +static ssize_t polarity_show (struct device *child,struct device_attribute *attr,char *buf) {const struct pwm_device *pwm =child_to_pwm_device (child); -const char *polarity ="unknown" ;struct pwm_state state;pwm_get_state (pwm, &state); +if (state.polarity <0 || state.polarity >=ARRAY_SIZE (polarity_strings)) +return sysfs_emit (buf,"unknown\n" ); -switch (state.polarity) { -case PWM_POLARITY_NORMAL : - polarity ="normal" ; -break ; - -case PWM_POLARITY_INVERSED : - polarity ="inversed" ; -break ; - } - -return sysfs_emit (buf,"%s\n" , polarity); +return sysfs_emit (buf,"%s\n" , polarity_strings[state.polarity]); } static ssize_t polarity_store(struct device *child,
@@ -180,20 +176,16 @@ static ssize_t polarity_store(struct device *child, {struct pwm_export *export =child_to_pwm_export (child);struct pwm_device *pwm = export->pwm; -enum pwm_polarity polarity;struct pwm_state state;int ret; -if (sysfs_streq (buf,"normal" )) - polarity =PWM_POLARITY_NORMAL ; -else if (sysfs_streq (buf,"inversed" )) - polarity =PWM_POLARITY_INVERSED ; -else -return -EINVAL ; + ret =sysfs_match_string (polarity_strings, buf); +if (ret <0 ) +return ret;mutex_lock (&export->lock);pwm_get_state (pwm, &state); - state.polarity = polarity; + state.polarity = ret; ret =pwm_apply_state (pwm, &state);mutex_unlock (&export->lock);
Signature · 1 line
2.35.1
Re: [PATCH v1 4/4] pwm: sysfs: Utilize an array for polarity strings
Hello Andy,
Quoted 12 lines · Andy Shevchenko
Code is smaller and looks nicer if we combine polarity strings into an array.
Signed-off-by Andy Shevchenko <[email protected]>--- drivers/pwm/sysfs.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-)drivers/pwm/sysfs.c +0
-0
drivers/pwm/sysfs.c static ssize_t enable_store(struct device *child, · line 154
Quoted 4 lines
drivers/pwm/sysfs.c +1
-0@@ -151,27 +151,23 @@ static ssize_t enable_store(struct device *child,return ret ? : size; } +static const char *const polarity_strings[] = {
I like having function and variable prefixes, so I'd prefer this to be called pwm_polarity_strings[]. (Side note: The device show and store callbacks obviously don't have a prefix either because DEVICE_ATTR_RW et al force the functions to be called "${name}_show" and "${name}_store". I considered already a few times to introduce something like
#define __ATTR_NS_RW_MODE(_name, _ns,_mode) { \
.attr = { .name = __stringify(_name), \
.mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
.show = _ns ## _ ## _name ## _show, \
.store = _ns ## _ ## _name ## _store, \
}
#define DEVICE_ATTR_NS_RW(_name, _ns) \
struct device_attribute _ns ## _dev_attr_ ## _name = __ATTR_NS_RW_MODE(_name, _ns, 0600)To allow the functions to have a name space. Never came around to do that though.)
drivers/pwm/sysfs.c static ssize_t enable_store(struct device *child, · line 156
Quoted 2 lines
drivers/pwm/sysfs.c +2
-0+ [PWM_POLARITY_NORMAL] = "normal", + [PWM_POLARITY_INVERSED] = "inversed",
I slightly prefer to not align the = in such definitions. Using a single plain space before = is already used in the definiton of pwm_class in the same file.
Otherwise the patch looks fine.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Hello,
Quoted 3 lines · Andy Shevchenko
Using these newer macros allows the compiler to remove the unused structure and functions when !CONFIG_PM_SLEEP + removes the need to mark pm functions
__maybe_unused.
Tested on ARCH=arm allmodconfig: No change in the binary (as expected I think). When disabling PM_SLEEP the resulting change of this patch looks as follows:
add/remove: 0/1 grow/shrink: 0/0 up/down: 0/-92 (-92) Function old new delta pwm_class_pm_ops 92 - -92 Total: Before=5213, After=5121, chg -1.76%
Looks like a quest to convert all SIMPLE_DEV_PM_OPS users to DEFINE_SIMPLE_DEV_PM_OPS.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |