All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5 1/3] leds: pwm: check result of led_pwm_set() in led_pwm_add()
  2020-04-21 13:09 [PATCH v5 0/3] leds: pwm: add support for default-state device Denis Osterland-Heim
@ 2020-04-21 13:09 ` Denis Osterland-Heim
  2020-04-27  9:36   ` Pavel Machek
  2020-04-21 13:09 ` [PATCH v5 2/3] leds: pwm: add support for default-state device property Denis Osterland-Heim
  2020-04-21 13:09 ` [PATCH v5 3/3] leds: pwm: add reference to common leds for default-state Denis Osterland-Heim
  2 siblings, 1 reply; 5+ messages in thread
From: Denis Osterland-Heim @ 2020-04-21 13:09 UTC (permalink / raw)
  To: dmurphy, pavel, jacek.anaszewski
  Cc: linux-kernel, Denis Osterland-Heim, linux-leds, robh+dt, devicetree

led_pwm_set() now returns an error when setting the PWM fails.

Signed-off-by: Denis Osterland-Heim <Denis.Osterland@diehl.com>
---
 drivers/leds/leds-pwm.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/drivers/leds/leds-pwm.c b/drivers/leds/leds-pwm.c
index 6c8a724aac51..ef7b91bd2064 100644
--- a/drivers/leds/leds-pwm.c
+++ b/drivers/leds/leds-pwm.c
@@ -91,15 +91,21 @@ static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
 	pwm_init_state(led_data->pwm, &led_data->pwmstate);
 
 	ret = devm_led_classdev_register(dev, &led_data->cdev);
-	if (ret == 0) {
-		priv->num_leds++;
-		led_pwm_set(&led_data->cdev, led_data->cdev.brightness);
-	} else {
+	if (ret) {
 		dev_err(dev, "failed to register PWM led for %s: %d\n",
 			led->name, ret);
+		return ret;
 	}
 
-	return ret;
+	ret = led_pwm_set(&led_data->cdev, led_data->cdev.brightness);
+	if (ret) {
+		dev_err(dev, "failed to set led PWM value for %s: %d",
+			led->name, ret);
+		return ret;
+	}
+
+	priv->num_leds++;
+	return 0;
 }
 
 static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv)
-- 
2.26.2



Diehl Connectivity Solutions GmbH
Geschäftsführung: Horst Leonberger
Sitz der Gesellschaft: Nürnberg - Registergericht: Amtsgericht
Nürnberg: HRB 32315
___________________________________________________________________________________________________

Der Inhalt der vorstehenden E-Mail ist nicht rechtlich bindend. Diese E-Mail enthaelt vertrauliche und/oder rechtlich geschuetzte Informationen.
Informieren Sie uns bitte, wenn Sie diese E-Mail faelschlicherweise erhalten haben. Bitte loeschen Sie in diesem Fall die Nachricht.
Jede unerlaubte Form der Reproduktion, Bekanntgabe, Aenderung, Verteilung und/oder Publikation dieser E-Mail ist strengstens untersagt.
- Informationen zum Datenschutz, insbesondere zu Ihren Rechten, erhalten Sie unter https://www.diehl.com/group/de/transparenz-und-informationspflichten/

The contents of the above mentioned e-mail is not legally binding. This e-mail contains confidential and/or legally protected information. Please inform us if you have received this e-mail by
mistake and delete it in such a case. Each unauthorized reproduction, disclosure, alteration, distribution and/or publication of this e-mail is strictly prohibited. 
- For general information on data protection and your respective rights please visit https://www.diehl.com/group/en/transparency-and-information-obligations/

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

* [PATCH v5 2/3] leds: pwm: add support for default-state device property
  2020-04-21 13:09 [PATCH v5 0/3] leds: pwm: add support for default-state device Denis Osterland-Heim
  2020-04-21 13:09 ` [PATCH v5 1/3] leds: pwm: check result of led_pwm_set() in led_pwm_add() Denis Osterland-Heim
@ 2020-04-21 13:09 ` Denis Osterland-Heim
  2020-04-21 13:09 ` [PATCH v5 3/3] leds: pwm: add reference to common leds for default-state Denis Osterland-Heim
  2 siblings, 0 replies; 5+ messages in thread
From: Denis Osterland-Heim @ 2020-04-21 13:09 UTC (permalink / raw)
  To: dmurphy, pavel, jacek.anaszewski
  Cc: linux-kernel, Denis Osterland-Heim, linux-leds, robh+dt, devicetree

This patch adds support for "default-state" devicetree property, which
allows to defer pwm init to first use of led.

This allows to configure the PWM early in bootloader to let the LED
blink until an application in Linux userspace sets something different.

Signed-off-by: Denis Osterland-Heim <Denis.Osterland@diehl.com>
---
 drivers/leds/leds-pwm.c | 54 ++++++++++++++++++++++++++++++++++++-----
 1 file changed, 48 insertions(+), 6 deletions(-)

diff --git a/drivers/leds/leds-pwm.c b/drivers/leds/leds-pwm.c
index ef7b91bd2064..7b199c151768 100644
--- a/drivers/leds/leds-pwm.c
+++ b/drivers/leds/leds-pwm.c
@@ -18,10 +18,15 @@
 #include <linux/pwm.h>
 #include <linux/slab.h>
 
+#define LEDS_PWM_DEFSTATE_OFF	0
+#define LEDS_PWM_DEFSTATE_ON	1
+#define LEDS_PWM_DEFSTATE_KEEP	2
+
 struct led_pwm {
 	const char	*name;
 	const char	*default_trigger;
 	u8		active_low;
+	u8		default_state;
 	unsigned int	max_brightness;
 };
 
@@ -88,7 +93,30 @@ static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
 
 	led_data->cdev.brightness_set_blocking = led_pwm_set;
 
-	pwm_init_state(led_data->pwm, &led_data->pwmstate);
+	/* init PWM state */
+	if (led->default_state == LEDS_PWM_DEFSTATE_KEEP) {
+		pwm_get_state(led_data->pwm, &led_data->pwmstate);
+		if (!led_data->pwmstate.period) {
+			led->default_state = LEDS_PWM_DEFSTATE_OFF;
+			dev_warn(dev,
+				"failed to read period for %s, default to off",
+				led->name);
+		}
+	}
+	if (led->default_state != LEDS_PWM_DEFSTATE_KEEP)
+		pwm_init_state(led_data->pwm, &led_data->pwmstate);
+
+	/* set brightness */
+	if (led->default_state == LEDS_PWM_DEFSTATE_ON)
+		led_data->cdev.brightness = led->max_brightness;
+	else if (led->default_state == LEDS_PWM_DEFSTATE_KEEP) {
+		uint64_t brightness;
+
+		brightness = led->max_brightness;
+		brightness *= led_data->pwmstate.duty_cycle;
+		do_div(brightness, led_data->pwmstate.period);
+		led_data->cdev.brightness = brightness;
+	}
 
 	ret = devm_led_classdev_register(dev, &led_data->cdev);
 	if (ret) {
@@ -97,11 +125,13 @@ static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
 		return ret;
 	}
 
-	ret = led_pwm_set(&led_data->cdev, led_data->cdev.brightness);
-	if (ret) {
-		dev_err(dev, "failed to set led PWM value for %s: %d",
-			led->name, ret);
-		return ret;
+	if (led->default_state != LEDS_PWM_DEFSTATE_KEEP) {
+		ret = led_pwm_set(&led_data->cdev, led_data->cdev.brightness);
+		if (ret) {
+			dev_err(dev, "failed to set led PWM value for %s: %d",
+				led->name, ret);
+			return ret;
+		}
 	}
 
 	priv->num_leds++;
@@ -117,6 +147,8 @@ static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv)
 	memset(&led, 0, sizeof(led));
 
 	device_for_each_child_node(dev, fwnode) {
+		const char *state = NULL;
+
 		ret = fwnode_property_read_string(fwnode, "label", &led.name);
 		if (ret && is_of_node(fwnode))
 			led.name = to_of_node(fwnode)->name;
@@ -134,6 +166,16 @@ static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv)
 		fwnode_property_read_u32(fwnode, "max-brightness",
 					 &led.max_brightness);
 
+		if (!fwnode_property_read_string(fwnode, "default-state",
+						 &state)) {
+			if (!strcmp(state, "keep"))
+				led.default_state = LEDS_PWM_DEFSTATE_KEEP;
+			else if (!strcmp(state, "on"))
+				led.default_state = LEDS_PWM_DEFSTATE_ON;
+			else
+				led.default_state = LEDS_PWM_DEFSTATE_OFF;
+		}
+
 		ret = led_pwm_add(dev, priv, &led, fwnode);
 		if (ret) {
 			fwnode_handle_put(fwnode);
-- 
2.26.2



Diehl Connectivity Solutions GmbH
Geschäftsführung: Horst Leonberger
Sitz der Gesellschaft: Nürnberg - Registergericht: Amtsgericht
Nürnberg: HRB 32315
___________________________________________________________________________________________________

Der Inhalt der vorstehenden E-Mail ist nicht rechtlich bindend. Diese E-Mail enthaelt vertrauliche und/oder rechtlich geschuetzte Informationen.
Informieren Sie uns bitte, wenn Sie diese E-Mail faelschlicherweise erhalten haben. Bitte loeschen Sie in diesem Fall die Nachricht.
Jede unerlaubte Form der Reproduktion, Bekanntgabe, Aenderung, Verteilung und/oder Publikation dieser E-Mail ist strengstens untersagt.
- Informationen zum Datenschutz, insbesondere zu Ihren Rechten, erhalten Sie unter https://www.diehl.com/group/de/transparenz-und-informationspflichten/

The contents of the above mentioned e-mail is not legally binding. This e-mail contains confidential and/or legally protected information. Please inform us if you have received this e-mail by
mistake and delete it in such a case. Each unauthorized reproduction, disclosure, alteration, distribution and/or publication of this e-mail is strictly prohibited. 
- For general information on data protection and your respective rights please visit https://www.diehl.com/group/en/transparency-and-information-obligations/

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

* [PATCH v5 0/3] leds: pwm: add support for default-state device
@ 2020-04-21 13:09 Denis Osterland-Heim
  2020-04-21 13:09 ` [PATCH v5 1/3] leds: pwm: check result of led_pwm_set() in led_pwm_add() Denis Osterland-Heim
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Denis Osterland-Heim @ 2020-04-21 13:09 UTC (permalink / raw)
  To: dmurphy, pavel, jacek.anaszewski
  Cc: linux-kernel, linux-leds, robh+dt, devicetree

v4 -> v5: tested the rebase to v5.7-rc1 based for-next

 .../devicetree/bindings/leds/leds-pwm.txt          |  2 +
 drivers/leds/leds-pwm.c                            | 60 +++++++++++++++++++---
 2 files changed, 56 insertions(+), 6 deletions(-)

Message-ID: <20200321081321.15614-1-Denis.Osterland@diehl.com>




Diehl Connectivity Solutions GmbH
Geschäftsführung: Horst Leonberger
Sitz der Gesellschaft: Nürnberg - Registergericht: Amtsgericht
Nürnberg: HRB 32315
___________________________________________________________________________________________________

Der Inhalt der vorstehenden E-Mail ist nicht rechtlich bindend. Diese E-Mail enthaelt vertrauliche und/oder rechtlich geschuetzte Informationen.
Informieren Sie uns bitte, wenn Sie diese E-Mail faelschlicherweise erhalten haben. Bitte loeschen Sie in diesem Fall die Nachricht.
Jede unerlaubte Form der Reproduktion, Bekanntgabe, Aenderung, Verteilung und/oder Publikation dieser E-Mail ist strengstens untersagt.
- Informationen zum Datenschutz, insbesondere zu Ihren Rechten, erhalten Sie unter https://www.diehl.com/group/de/transparenz-und-informationspflichten/

The contents of the above mentioned e-mail is not legally binding. This e-mail contains confidential and/or legally protected information. Please inform us if you have received this e-mail by
mistake and delete it in such a case. Each unauthorized reproduction, disclosure, alteration, distribution and/or publication of this e-mail is strictly prohibited. 
- For general information on data protection and your respective rights please visit https://www.diehl.com/group/en/transparency-and-information-obligations/

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

* [PATCH v5 3/3] leds: pwm: add reference to common leds for default-state
  2020-04-21 13:09 [PATCH v5 0/3] leds: pwm: add support for default-state device Denis Osterland-Heim
  2020-04-21 13:09 ` [PATCH v5 1/3] leds: pwm: check result of led_pwm_set() in led_pwm_add() Denis Osterland-Heim
  2020-04-21 13:09 ` [PATCH v5 2/3] leds: pwm: add support for default-state device property Denis Osterland-Heim
@ 2020-04-21 13:09 ` Denis Osterland-Heim
  2 siblings, 0 replies; 5+ messages in thread
From: Denis Osterland-Heim @ 2020-04-21 13:09 UTC (permalink / raw)
  To: dmurphy, pavel, jacek.anaszewski
  Cc: robh, linux-kernel, Denis Osterland-Heim, linux-leds, robh+dt,
	devicetree

The default-state is now supported for PWM leds.

Signed-off-by: Denis Osterland-Heim <Denis.Osterland@diehl.com>
Acked-by: Jacek Anaszewski <jacek.anaszewski@gmail.com>
Acked-by: Pavel Machek <pavel@ucw.cz>
Acked-by: Rob Herring <robh@kernel.org>
---
 Documentation/devicetree/bindings/leds/leds-pwm.txt | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/Documentation/devicetree/bindings/leds/leds-pwm.txt b/Documentation/devicetree/bindings/leds/leds-pwm.txt
index 6c6583c35f2f..d0f489680594 100644
--- a/Documentation/devicetree/bindings/leds/leds-pwm.txt
+++ b/Documentation/devicetree/bindings/leds/leds-pwm.txt
@@ -19,6 +19,8 @@ LED sub-node properties:
   see Documentation/devicetree/bindings/leds/common.txt
 - linux,default-trigger :  (optional)
   see Documentation/devicetree/bindings/leds/common.txt
+- default-state : (optional)
+  see Documentation/devicetree/bindings/leds/common.yaml
 
 Example:
 
-- 
2.26.2



Diehl Connectivity Solutions GmbH
Geschäftsführung: Horst Leonberger
Sitz der Gesellschaft: Nürnberg - Registergericht: Amtsgericht
Nürnberg: HRB 32315
___________________________________________________________________________________________________

Der Inhalt der vorstehenden E-Mail ist nicht rechtlich bindend. Diese E-Mail enthaelt vertrauliche und/oder rechtlich geschuetzte Informationen.
Informieren Sie uns bitte, wenn Sie diese E-Mail faelschlicherweise erhalten haben. Bitte loeschen Sie in diesem Fall die Nachricht.
Jede unerlaubte Form der Reproduktion, Bekanntgabe, Aenderung, Verteilung und/oder Publikation dieser E-Mail ist strengstens untersagt.
- Informationen zum Datenschutz, insbesondere zu Ihren Rechten, erhalten Sie unter https://www.diehl.com/group/de/transparenz-und-informationspflichten/

The contents of the above mentioned e-mail is not legally binding. This e-mail contains confidential and/or legally protected information. Please inform us if you have received this e-mail by
mistake and delete it in such a case. Each unauthorized reproduction, disclosure, alteration, distribution and/or publication of this e-mail is strictly prohibited. 
- For general information on data protection and your respective rights please visit https://www.diehl.com/group/en/transparency-and-information-obligations/

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

* Re: [PATCH v5 1/3] leds: pwm: check result of led_pwm_set() in led_pwm_add()
  2020-04-21 13:09 ` [PATCH v5 1/3] leds: pwm: check result of led_pwm_set() in led_pwm_add() Denis Osterland-Heim
@ 2020-04-27  9:36   ` Pavel Machek
  0 siblings, 0 replies; 5+ messages in thread
From: Pavel Machek @ 2020-04-27  9:36 UTC (permalink / raw)
  To: Denis Osterland-Heim
  Cc: dmurphy, jacek.anaszewski, linux-kernel, linux-leds, robh+dt, devicetree

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

On Tue 2020-04-21 13:09:14, Denis Osterland-Heim wrote:
> led_pwm_set() now returns an error when setting the PWM fails.
> 
> Signed-off-by: Denis Osterland-Heim <Denis.Osterland@diehl.com>

Thanks, applied. (Just this patch for now).
                                                                Pavel
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

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

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

end of thread, other threads:[~2020-04-27  9:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-21 13:09 [PATCH v5 0/3] leds: pwm: add support for default-state device Denis Osterland-Heim
2020-04-21 13:09 ` [PATCH v5 1/3] leds: pwm: check result of led_pwm_set() in led_pwm_add() Denis Osterland-Heim
2020-04-27  9:36   ` Pavel Machek
2020-04-21 13:09 ` [PATCH v5 2/3] leds: pwm: add support for default-state device property Denis Osterland-Heim
2020-04-21 13:09 ` [PATCH v5 3/3] leds: pwm: add reference to common leds for default-state Denis Osterland-Heim

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.