diff mbox series

[v1,4/4] pwm: sysfs: Utilize an array for polarity strings

Message ID 20220806212331.40086-4-andriy.shevchenko@linux.intel.com
State Superseded
Headers show
Series [v1,1/4] pwm: sysfs: Switch to DEFINE_SIMPLE_DEV_PM_OPS() and pm_sleep_ptr() | expand

Commit Message

Andy Shevchenko Aug. 6, 2022, 9:23 p.m. UTC
Code is smaller and looks nicer if we combine polarity strings into an array.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/pwm/sysfs.c | 32 ++++++++++++--------------------
 1 file changed, 12 insertions(+), 20 deletions(-)

Comments

Uwe Kleine-K├Ânig Aug. 7, 2022, 11:23 a.m. UTC | #1
Hello Andy,

On Sun, Aug 07, 2022 at 12:23:31AM +0300, Andy Shevchenko wrote:
> Code is smaller and looks nicer if we combine polarity strings into an array.
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>  drivers/pwm/sysfs.c | 32 ++++++++++++--------------------
>  1 file changed, 12 insertions(+), 20 deletions(-)
> 
> diff --git a/drivers/pwm/sysfs.c b/drivers/pwm/sysfs.c
> index 767c4b19afb1..1bbc5286b7c6 100644
> --- a/drivers/pwm/sysfs.c
> +++ b/drivers/pwm/sysfs.c
> @@ -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.)

> +	[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
diff mbox series

Patch

diff --git a/drivers/pwm/sysfs.c b/drivers/pwm/sysfs.c
index 767c4b19afb1..1bbc5286b7c6 100644
--- a/drivers/pwm/sysfs.c
+++ b/drivers/pwm/sysfs.c
@@ -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);