linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 1/4] pwm: sysfs: Switch to DEFINE_SIMPLE_DEV_PM_OPS() and pm_sleep_ptr()
@ 2022-08-06 21:23 Andy Shevchenko
  2022-08-06 21:23 ` [PATCH v1 2/4] pwm: sysfs: Return directly from the for-loop in PM callbacks Andy Shevchenko
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Andy Shevchenko @ 2022-08-06 21:23 UTC (permalink / raw)
  To: Andy Shevchenko, linux-pwm, linux-kernel
  Cc: Thierry Reding, Uwe Kleine-König

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.

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

diff --git a/drivers/pwm/sysfs.c b/drivers/pwm/sysfs.c
index 9903c3a7eced..c21b6046067b 100644
--- a/drivers/pwm/sysfs.c
+++ b/drivers/pwm/sysfs.c
@@ -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)
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v1 2/4] pwm: sysfs: Return directly from the for-loop in PM callbacks
  2022-08-06 21:23 [PATCH v1 1/4] pwm: sysfs: Switch to DEFINE_SIMPLE_DEV_PM_OPS() and pm_sleep_ptr() Andy Shevchenko
@ 2022-08-06 21:23 ` Andy Shevchenko
  2022-08-07  8:57   ` Uwe Kleine-König
  2022-08-06 21:23 ` [PATCH v1 3/4] pwm: sysfs: Replace sprintf() with sysfs_emit() Andy Shevchenko
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 8+ messages in thread
From: Andy Shevchenko @ 2022-08-06 21:23 UTC (permalink / raw)
  To: Andy Shevchenko, linux-pwm, linux-kernel
  Cc: Thierry Reding, Uwe Kleine-König

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 <andriy.shevchenko@linux.intel.com>
---
 drivers/pwm/sysfs.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/pwm/sysfs.c b/drivers/pwm/sysfs.c
index c21b6046067b..1543fe07765b 100644
--- a/drivers/pwm/sysfs.c
+++ b/drivers/pwm/sysfs.c
@@ -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)
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v1 3/4] pwm: sysfs: Replace sprintf() with sysfs_emit()
  2022-08-06 21:23 [PATCH v1 1/4] pwm: sysfs: Switch to DEFINE_SIMPLE_DEV_PM_OPS() and pm_sleep_ptr() Andy Shevchenko
  2022-08-06 21:23 ` [PATCH v1 2/4] pwm: sysfs: Return directly from the for-loop in PM callbacks Andy Shevchenko
@ 2022-08-06 21:23 ` Andy Shevchenko
  2022-08-07  9:03   ` Uwe Kleine-König
  2022-08-06 21:23 ` [PATCH v1 4/4] pwm: sysfs: Utilize an array for polarity strings Andy Shevchenko
  2022-08-07  8:56 ` [PATCH v1 1/4] pwm: sysfs: Switch to DEFINE_SIMPLE_DEV_PM_OPS() and pm_sleep_ptr() Uwe Kleine-König
  3 siblings, 1 reply; 8+ messages in thread
From: Andy Shevchenko @ 2022-08-06 21:23 UTC (permalink / raw)
  To: Andy Shevchenko, linux-pwm, linux-kernel
  Cc: Thierry Reding, Uwe Kleine-König

For 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.

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

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


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v1 4/4] pwm: sysfs: Utilize an array for polarity strings
  2022-08-06 21:23 [PATCH v1 1/4] pwm: sysfs: Switch to DEFINE_SIMPLE_DEV_PM_OPS() and pm_sleep_ptr() Andy Shevchenko
  2022-08-06 21:23 ` [PATCH v1 2/4] pwm: sysfs: Return directly from the for-loop in PM callbacks Andy Shevchenko
  2022-08-06 21:23 ` [PATCH v1 3/4] pwm: sysfs: Replace sprintf() with sysfs_emit() Andy Shevchenko
@ 2022-08-06 21:23 ` Andy Shevchenko
  2022-08-07 11:23   ` Uwe Kleine-König
  2022-08-07  8:56 ` [PATCH v1 1/4] pwm: sysfs: Switch to DEFINE_SIMPLE_DEV_PM_OPS() and pm_sleep_ptr() Uwe Kleine-König
  3 siblings, 1 reply; 8+ messages in thread
From: Andy Shevchenko @ 2022-08-06 21:23 UTC (permalink / raw)
  To: Andy Shevchenko, linux-pwm, linux-kernel
  Cc: Thierry Reding, Uwe Kleine-König

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[] = {
+	[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);
 
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v1 1/4] pwm: sysfs: Switch to DEFINE_SIMPLE_DEV_PM_OPS() and pm_sleep_ptr()
  2022-08-06 21:23 [PATCH v1 1/4] pwm: sysfs: Switch to DEFINE_SIMPLE_DEV_PM_OPS() and pm_sleep_ptr() Andy Shevchenko
                   ` (2 preceding siblings ...)
  2022-08-06 21:23 ` [PATCH v1 4/4] pwm: sysfs: Utilize an array for polarity strings Andy Shevchenko
@ 2022-08-07  8:56 ` Uwe Kleine-König
  3 siblings, 0 replies; 8+ messages in thread
From: Uwe Kleine-König @ 2022-08-07  8:56 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: linux-pwm, linux-kernel, Thierry Reding

[-- Attachment #1: Type: text/plain, Size: 989 bytes --]

Hello,

On Sun, Aug 07, 2022 at 12:23:28AM +0300, Andy Shevchenko wrote:
> 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.

Reviewed-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v1 2/4] pwm: sysfs: Return directly from the for-loop in PM callbacks
  2022-08-06 21:23 ` [PATCH v1 2/4] pwm: sysfs: Return directly from the for-loop in PM callbacks Andy Shevchenko
@ 2022-08-07  8:57   ` Uwe Kleine-König
  0 siblings, 0 replies; 8+ messages in thread
From: Uwe Kleine-König @ 2022-08-07  8:57 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: linux-pwm, linux-kernel, Thierry Reding

[-- Attachment #1: Type: text/plain, Size: 552 bytes --]

On Sun, Aug 07, 2022 at 12:23:29AM +0300, Andy Shevchenko wrote:
> 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 <andriy.shevchenko@linux.intel.com>

Acked-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

Thanks!
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v1 3/4] pwm: sysfs: Replace sprintf() with sysfs_emit()
  2022-08-06 21:23 ` [PATCH v1 3/4] pwm: sysfs: Replace sprintf() with sysfs_emit() Andy Shevchenko
@ 2022-08-07  9:03   ` Uwe Kleine-König
  0 siblings, 0 replies; 8+ messages in thread
From: Uwe Kleine-König @ 2022-08-07  9:03 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: linux-pwm, linux-kernel, Thierry Reding

[-- Attachment #1: Type: text/plain, Size: 675 bytes --]

Hello,

On Sun, Aug 07, 2022 at 12:23:30AM +0300, Andy Shevchenko wrote:
> For 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.

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.

Reviewed-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v1 4/4] pwm: sysfs: Utilize an array for polarity strings
  2022-08-06 21:23 ` [PATCH v1 4/4] pwm: sysfs: Utilize an array for polarity strings Andy Shevchenko
@ 2022-08-07 11:23   ` Uwe Kleine-König
  0 siblings, 0 replies; 8+ messages in thread
From: Uwe Kleine-König @ 2022-08-07 11:23 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: linux-pwm, linux-kernel, Thierry Reding

[-- Attachment #1: Type: text/plain, Size: 2076 bytes --]

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

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-08-07 11:23 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-06 21:23 [PATCH v1 1/4] pwm: sysfs: Switch to DEFINE_SIMPLE_DEV_PM_OPS() and pm_sleep_ptr() Andy Shevchenko
2022-08-06 21:23 ` [PATCH v1 2/4] pwm: sysfs: Return directly from the for-loop in PM callbacks Andy Shevchenko
2022-08-07  8:57   ` Uwe Kleine-König
2022-08-06 21:23 ` [PATCH v1 3/4] pwm: sysfs: Replace sprintf() with sysfs_emit() Andy Shevchenko
2022-08-07  9:03   ` Uwe Kleine-König
2022-08-06 21:23 ` [PATCH v1 4/4] pwm: sysfs: Utilize an array for polarity strings Andy Shevchenko
2022-08-07 11:23   ` Uwe Kleine-König
2022-08-07  8:56 ` [PATCH v1 1/4] pwm: sysfs: Switch to DEFINE_SIMPLE_DEV_PM_OPS() and pm_sleep_ptr() Uwe Kleine-König

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).