public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	linux-pwm@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: "Thierry Reding" <thierry.reding@gmail.com>,
	"Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
Subject: [PATCH v2 4/4] pwm: sysfs: Utilize an array for polarity strings
Date: Fri, 26 Aug 2022 20:07:16 +0300	[thread overview]
Message-ID: <20220826170716.6886-4-andriy.shevchenko@linux.intel.com> (raw)
In-Reply-To: <20220826170716.6886-1-andriy.shevchenko@linux.intel.com>

Code is smaller and looks nicer if we combine polarity strings
into an array.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
v2: added pwm_ prefix to the variable (Uwe), adjusted intendation (Uwe)
 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..502167e44a3d 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 pwm_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(pwm_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", pwm_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(pwm_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);
 
-- 
2.35.1


  parent reply	other threads:[~2022-08-26 17:07 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-26 17:07 [PATCH v2 1/4] pwm: sysfs: Switch to DEFINE_SIMPLE_DEV_PM_OPS() and pm_sleep_ptr() Andy Shevchenko
2022-08-26 17:07 ` [PATCH v2 2/4] pwm: sysfs: Return directly from the for-loop in PM callbacks Andy Shevchenko
2022-09-28 12:31   ` Thierry Reding
2022-09-28 13:43     ` Andy Shevchenko
2022-08-26 17:07 ` [PATCH v2 3/4] pwm: sysfs: Replace sprintf() with sysfs_emit() Andy Shevchenko
2022-09-28 12:28   ` Thierry Reding
2022-09-28 13:40     ` Andy Shevchenko
2022-09-28 13:58       ` Andy Shevchenko
2022-09-28 15:20         ` Thierry Reding
2022-09-28 15:21           ` Thierry Reding
2022-09-28 15:39           ` Andy Shevchenko
2022-08-26 17:07 ` Andy Shevchenko [this message]
2022-08-28  2:07   ` [PATCH v2 4/4] pwm: sysfs: Utilize an array for polarity strings Joe Perches
     [not found]     ` <CAHp75VfY5RgAju5ASvAp565oF6VmYYiuowNsPTGSm=+1iFJ98A@mail.gmail.com>
2022-08-28 13:46       ` Joe Perches
2022-08-28 17:40         ` Andy Shevchenko
2022-08-28 18:19           ` Joe Perches
2022-08-29  7:59             ` Andy Shevchenko
2022-09-28 12:15             ` Thierry Reding
2022-09-06 13:57 ` [PATCH v2 1/4] pwm: sysfs: Switch to DEFINE_SIMPLE_DEV_PM_OPS() and pm_sleep_ptr() Andy Shevchenko
2022-09-28 12:31 ` Thierry Reding
2022-09-28 14:01   ` Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220826170716.6886-4-andriy.shevchenko@linux.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=thierry.reding@gmail.com \
    --cc=u.kleine-koenig@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox