All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] led: Implement software led blinking
@ 2024-06-27 11:31 Mikhail Kshevetskiy
  2024-06-27 11:31 ` [PATCH 2/2] led: Add dts property to specify blinking of the led Mikhail Kshevetskiy
                   ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: Mikhail Kshevetskiy @ 2024-06-27 11:31 UTC (permalink / raw)
  To: Tom Rini, Rasmus Villemoes, Doug Zobel, Marek Vasut,
	Christian Gmeiner, u-boot
  Cc: Michael Polyntsov

From: Michael Polyntsov <michael.polyntsov@iopsys.eu>

If hardware (or driver) doesn't support leds blinking, it's
now possible to use software implementation of blinking instead.
This relies on cyclic functions.

Signed-off-by: Michael Polyntsov <michael.polyntsov@iopsys.eu>
Signed-off-by: Mikhail Kshevetskiy <mikhail.kshevetskiy@iopsys.eu>
---
 drivers/led/Kconfig      |   9 ++
 drivers/led/led-uclass.c | 190 ++++++++++++++++++++++++++++++++++++++-
 2 files changed, 195 insertions(+), 4 deletions(-)

diff --git a/drivers/led/Kconfig b/drivers/led/Kconfig
index 9837960198d..4330f014239 100644
--- a/drivers/led/Kconfig
+++ b/drivers/led/Kconfig
@@ -73,6 +73,15 @@ config LED_BLINK
 	  This option enables support for this which adds slightly to the
 	  code size.
 
+config LED_SW_BLINK
+	bool "Support software LED blinking"
+	depends on LED_BLINK
+	select CYCLIC
+	help
+	  Turns on led blinking implemented in the software, useful when
+	  the hardware doesn't support led blinking. Does nothing if
+	  driver supports blinking.
+
 config SPL_LED
 	bool "Enable LED support in SPL"
 	depends on SPL_DM
diff --git a/drivers/led/led-uclass.c b/drivers/led/led-uclass.c
index a4be56fc258..b35964f2e99 100644
--- a/drivers/led/led-uclass.c
+++ b/drivers/led/led-uclass.c
@@ -15,6 +15,10 @@
 #include <dm/root.h>
 #include <dm/uclass-internal.h>
 
+#ifdef CONFIG_LED_SW_BLINK
+#include <malloc.h>
+#endif
+
 int led_bind_generic(struct udevice *parent, const char *driver_name)
 {
 	struct udevice *dev;
@@ -41,6 +45,7 @@ int led_get_by_label(const char *label, struct udevice **devp)
 	ret = uclass_get(UCLASS_LED, &uc);
 	if (ret)
 		return ret;
+
 	uclass_foreach_dev(dev, uc) {
 		struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
 
@@ -52,14 +57,180 @@ int led_get_by_label(const char *label, struct udevice **devp)
 	return -ENODEV;
 }
 
-int led_set_state(struct udevice *dev, enum led_state_t state)
+#ifdef CONFIG_LED_SW_BLINK
+
+enum led_sw_blink_state_t {
+	LED_SW_BLINK_ST_OFF = 0,
+	LED_SW_BLINK_ST_ON = 1,
+	LED_SW_BLINK_ST_NONE = 2,
+};
+
+struct sw_blink_state {
+	struct udevice *dev;
+	enum led_sw_blink_state_t cur_blink_state;
+};
+
+static bool led_driver_supports_hw_blinking(const struct udevice *dev)
+{
+	struct led_ops *ops = led_get_ops(dev);
+
+	/*
+	 * We assume that if driver supports set_period, then it correctly
+	 * handles all other requests, for example, that
+	 * led_set_state(LEDST_BLINK) works correctly.
+	 */
+	return ops->set_period != NULL;
+}
+
+static const char *led_sw_label_to_cyclic_func_name(const char *label)
+{
+#define MAX_NAME_LEN 50
+	static char cyclic_func_name[MAX_NAME_LEN] = {0};
+
+	snprintf(cyclic_func_name, MAX_NAME_LEN, "sw_blink_%s", label);
+	return cyclic_func_name;
+#undef MAX_NAME_LEN
+}
+
+static struct cyclic_info *led_sw_find_blinking_led(const char *label)
+{
+	struct cyclic_info *cyclic;
+	const char *cyclic_name;
+
+	cyclic_name = led_sw_label_to_cyclic_func_name(label);
+
+	hlist_for_each_entry(cyclic, cyclic_get_list(), list) {
+		if (strcmp(cyclic->name, cyclic_name) == 0)
+			return cyclic;
+	}
+
+	return NULL;
+}
+
+static bool led_sw_is_blinking(struct udevice *dev)
+{
+	struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
+	struct cyclic_info *cyclic = led_sw_find_blinking_led(uc_plat->label);
+
+	if (cyclic != NULL) {
+		struct sw_blink_state *state;
+
+		state = (struct sw_blink_state *)cyclic->ctx;
+		return state->cur_blink_state != LED_SW_BLINK_ST_NONE;
+	}
+
+	return false;
+}
+
+static void led_sw_blink(void *void_state)
+{
+	struct sw_blink_state *state = (struct sw_blink_state *)void_state;
+	struct udevice *dev = state->dev;
+	struct led_ops *ops = led_get_ops(dev);
+
+	switch (state->cur_blink_state) {
+	case LED_SW_BLINK_ST_OFF:
+		state->cur_blink_state = LED_SW_BLINK_ST_ON;
+		ops->set_state(dev, LEDST_ON);
+		break;
+	case LED_SW_BLINK_ST_ON:
+		state->cur_blink_state = LED_SW_BLINK_ST_OFF;
+		ops->set_state(dev, LEDST_OFF);
+		break;
+	case LED_SW_BLINK_ST_NONE:
+		/*
+		 * led_set_period has been called, but
+		 * led_set_state(LDST_BLINK) has not yet,
+		 * so doing nothing
+		 */
+		break;
+	}
+}
+
+static void led_sw_free_cyclic(struct cyclic_info *cyclic)
+{
+	free(cyclic->ctx);
+	cyclic_unregister(cyclic);
+}
+
+static int led_sw_set_period(struct udevice *dev, int period_ms)
+{
+	struct cyclic_info *cyclic;
+	struct sw_blink_state *state;
+	struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
+	const char *cyclic_func_name;
+
+	state = malloc(sizeof(struct sw_blink_state));
+	if (state == NULL) {
+		printf("Allocating memory for sw_blink_state for %s failed\n",
+		       uc_plat->label);
+		return -ENOMEM;
+	}
+
+	state->cur_blink_state = LED_SW_BLINK_ST_NONE;
+	state->dev = dev;
+
+	/*
+	 * Make sure that there is no cyclic function already
+	 * registered for this label
+	 */
+	cyclic = led_sw_find_blinking_led(uc_plat->label);
+	if (cyclic != NULL)
+		led_sw_free_cyclic(cyclic);
+
+	cyclic_func_name = led_sw_label_to_cyclic_func_name(uc_plat->label);
+
+	cyclic = cyclic_register(led_sw_blink, period_ms * 1000,
+				 cyclic_func_name, (void *)state);
+	if (cyclic == NULL) {
+		printf("Registering of blinking function for %s failed\n",
+		       uc_plat->label);
+		free(state);
+		return -ENOMEM;
+	}
+
+	return 0;
+}
+
+#endif
+
+int led_set_state(struct udevice *dev, enum led_state_t new_state)
 {
 	struct led_ops *ops = led_get_ops(dev);
 
 	if (!ops->set_state)
 		return -ENOSYS;
 
-	return ops->set_state(dev, state);
+#ifdef CONFIG_LED_SW_BLINK
+	if (!led_driver_supports_hw_blinking(dev)) {
+		struct cyclic_info *cyclic;
+		struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
+
+		cyclic = led_sw_find_blinking_led(uc_plat->label);
+
+		if (cyclic != NULL) {
+			if (new_state == LEDST_BLINK) {
+				struct sw_blink_state *cur_st;
+
+				cur_st = (struct sw_blink_state *)cyclic->ctx;
+
+				/*
+				 * Next call to led_sw_blink will start blinking
+				 */
+				cur_st->cur_blink_state = LED_SW_BLINK_ST_OFF;
+				return 0;
+			}
+
+			/*
+			 * Changing current blinking state to
+			 * something else
+			 */
+			led_sw_free_cyclic(cyclic);
+		}
+	}
+#endif
+
+	return ops->set_state(dev, new_state);
 }
 
 enum led_state_t led_get_state(struct udevice *dev)
@@ -69,19 +240,31 @@ enum led_state_t led_get_state(struct udevice *dev)
 	if (!ops->get_state)
 		return -ENOSYS;
 
+#ifdef CONFIG_LED_SW_BLINK
+	if (!led_driver_supports_hw_blinking(dev) && led_sw_is_blinking(dev))
+		return LEDST_BLINK;
+#endif
+
 	return ops->get_state(dev);
 }
 
 #ifdef CONFIG_LED_BLINK
+
 int led_set_period(struct udevice *dev, int period_ms)
 {
 	struct led_ops *ops = led_get_ops(dev);
 
-	if (!ops->set_period)
+	if (!ops->set_period) {
+#ifdef CONFIG_LED_SW_BLINK
+		return led_sw_set_period(dev, period_ms);
+#else
 		return -ENOSYS;
+#endif
+	}
 
 	return ops->set_period(dev, period_ms);
 }
+
 #endif
 
 static int led_post_bind(struct udevice *dev)
@@ -113,7 +296,6 @@ static int led_post_bind(struct udevice *dev)
 	 * probe() to configure its default state during startup.
 	 */
 	dev_or_flags(dev, DM_FLAG_PROBE_AFTER_BIND);
-
 	return 0;
 }
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 17+ messages in thread
* [PATCH 1/2] led: Implement software led blinking
@ 2024-07-05  2:26 Mikhail Kshevetskiy
  2024-07-05  8:29 ` Mark Kettenis
  0 siblings, 1 reply; 17+ messages in thread
From: Mikhail Kshevetskiy @ 2024-07-05  2:26 UTC (permalink / raw)
  To: Tom Rini, Rasmus Villemoes, Doug Zobel, Marek Vasut,
	Christian Gmeiner, u-boot, Christian Marangi
  Cc: Michael Polyntsov, Mikhail Kshevetskiy

From: Michael Polyntsov <michael.polyntsov@iopsys.eu>

If hardware (or driver) doesn't support leds blinking, it's
now possible to use software implementation of blinking instead.
This relies on cyclic functions.

v2 changes:
 * Drop sw_blink_state structure, move its necessary fields to
   led_uc_plat structure.
 * Add cyclic_info pointer to led_uc_plat structure. This
   simplify code a lot.
 * Remove cyclic function search logic. Not needed anymore.
 * Fix blinking period. It was twice large.
 * Other cleanups.

v3 changes:
 * Adapt code to recent cyclic function changes
 * Move software blinking functions to separate file
 * Other small changes

Signed-off-by: Michael Polyntsov <michael.polyntsov@iopsys.eu>
Signed-off-by: Mikhail Kshevetskiy <mikhail.kshevetskiy@iopsys.eu>
---
 drivers/led/Kconfig        |  14 +++++
 drivers/led/Makefile       |   1 +
 drivers/led/led-uclass.c   |  16 +++++-
 drivers/led/led_sw_blink.c | 106 +++++++++++++++++++++++++++++++++++++
 include/led.h              |  17 ++++++
 5 files changed, 152 insertions(+), 2 deletions(-)
 create mode 100644 drivers/led/led_sw_blink.c

diff --git a/drivers/led/Kconfig b/drivers/led/Kconfig
index 9837960198d..dc9d4c8a757 100644
--- a/drivers/led/Kconfig
+++ b/drivers/led/Kconfig
@@ -73,6 +73,20 @@ config LED_BLINK
 	  This option enables support for this which adds slightly to the
 	  code size.
 
+config LED_SW_BLINK
+	bool "Support software LED blinking"
+	depends on LED_BLINK
+	select CYCLIC
+	help
+	  Turns on led blinking implemented in the software, useful when
+	  the hardware doesn't support led blinking. Half of the period
+	  led will be ON and the rest time it will be OFF. Standard
+	  led commands can be used to configure blinking. Does nothing
+	  if driver supports blinking.
+	  WARNING: Blinking may be inaccurate during execution of time
+	  consuming commands (ex. flash reading). Also it completely
+	  stops during OS booting.
+
 config SPL_LED
 	bool "Enable LED support in SPL"
 	depends on SPL_DM
diff --git a/drivers/led/Makefile b/drivers/led/Makefile
index 2bcb8589087..e27aa488482 100644
--- a/drivers/led/Makefile
+++ b/drivers/led/Makefile
@@ -4,6 +4,7 @@
 # Written by Simon Glass <sjg@chromium.org>
 
 obj-y += led-uclass.o
+obj-$(CONFIG_LED_SW_BLINK) += led_sw_blink.o
 obj-$(CONFIG_LED_BCM6328) += led_bcm6328.o
 obj-$(CONFIG_LED_BCM6358) += led_bcm6358.o
 obj-$(CONFIG_LED_BCM6753) += led_bcm6753.o
diff --git a/drivers/led/led-uclass.c b/drivers/led/led-uclass.c
index f37bf6a1550..37dc99cecdc 100644
--- a/drivers/led/led-uclass.c
+++ b/drivers/led/led-uclass.c
@@ -58,6 +58,10 @@ int led_set_state(struct udevice *dev, enum led_state_t state)
 	if (!ops->set_state)
 		return -ENOSYS;
 
+	if (IS_ENABLED(CONFIG_LED_SW_BLINK) &&
+	    led_sw_on_state_change(dev, state))
+		return 0;
+
 	return ops->set_state(dev, state);
 }
 
@@ -68,6 +72,10 @@ enum led_state_t led_get_state(struct udevice *dev)
 	if (!ops->get_state)
 		return -ENOSYS;
 
+	if (IS_ENABLED(CONFIG_LED_SW_BLINK) &&
+	    led_sw_is_blinking(dev))
+		return LEDST_BLINK;
+
 	return ops->get_state(dev);
 }
 
@@ -76,8 +84,12 @@ int led_set_period(struct udevice *dev, int period_ms)
 {
 	struct led_ops *ops = led_get_ops(dev);
 
-	if (!ops->set_period)
-		return -ENOSYS;
+	if (!ops->set_period) {
+		if (IS_ENABLED(CONFIG_LED_SW_BLINK))
+			return led_sw_set_period(dev, period_ms);
+		else
+			return -ENOSYS;
+	}
 
 	return ops->set_period(dev, period_ms);
 }
diff --git a/drivers/led/led_sw_blink.c b/drivers/led/led_sw_blink.c
new file mode 100644
index 00000000000..ab56111a60b
--- /dev/null
+++ b/drivers/led/led_sw_blink.c
@@ -0,0 +1,106 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Software blinking helpers
+ * Copyright (C) 2024 IOPSYS Software Solutions AB
+ * Author: Mikhail Kshevetskiy <mikhail.kshevetskiy@iopsys.eu>
+ */
+
+#include <dm.h>
+#include <led.h>
+#include <time.h>
+#include <stdlib.h>
+
+static void led_sw_blink(struct cyclic_info *c)
+{
+	struct led_uc_plat *uc_plat;
+	struct udevice *dev;
+	struct led_ops *ops;
+
+	uc_plat = container_of(c, struct led_uc_plat, cyclic);
+	dev = uc_plat->dev;
+	ops = led_get_ops(dev);
+
+	switch (uc_plat->sw_blink_state) {
+	case LED_SW_BLINK_ST_OFF:
+		uc_plat->sw_blink_state = LED_SW_BLINK_ST_ON;
+		ops->set_state(dev, LEDST_ON);
+		break;
+	case LED_SW_BLINK_ST_ON:
+		uc_plat->sw_blink_state = LED_SW_BLINK_ST_OFF;
+		ops->set_state(dev, LEDST_OFF);
+		break;
+	case LED_SW_BLINK_ST_NOT_READY:
+		/*
+		 * led_set_period has been called, but
+		 * led_set_state(LDST_BLINK) has not yet,
+		 * so doing nothing
+		 */
+		break;
+	default:
+		break;
+	}
+}
+
+int led_sw_set_period(struct udevice *dev, int period_ms)
+{
+	struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
+	struct cyclic_info *cyclic = &uc_plat->cyclic;
+	struct led_ops *ops = led_get_ops(dev);
+	int half_period_us;
+	char *name;
+	int len;
+
+	half_period_us = period_ms * 1000 / 2;
+
+	name = (char *)cyclic->name;
+	if (name == NULL) {
+		len = snprintf(NULL, 0, "led_sw_blink_%s", uc_plat->label);
+		if (len <= 0)
+			return -ENOMEM;
+
+		name = malloc(len + 1);
+		if (!name)
+			return -ENOMEM;
+
+		snprintf(name, len + 1, "led_sw_blink_%s", uc_plat->label);
+	}
+
+	if (uc_plat->sw_blink_state == LED_SW_BLINK_ST_DISABLED) {
+		uc_plat->dev = dev;
+		cyclic_register(cyclic, led_sw_blink, half_period_us, name);
+	} else {
+		cyclic->delay_us = half_period_us;
+		cyclic->start_time_us = timer_get_us();
+	}
+
+	uc_plat->sw_blink_state = LED_SW_BLINK_ST_NOT_READY;
+	ops->set_state(dev, LEDST_OFF);
+
+	return 0;
+}
+
+bool led_sw_is_blinking(struct udevice *dev)
+{
+	struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
+
+	return (uc_plat->sw_blink_state > LED_SW_BLINK_ST_NOT_READY);
+}
+
+bool led_sw_on_state_change(struct udevice *dev, enum led_state_t state)
+{
+	struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
+
+	if (uc_plat->sw_blink_state != LED_SW_BLINK_ST_DISABLED) {
+		if (state == LEDST_BLINK) {
+			/* start blinking on next led_sw_blink() call */
+			uc_plat->sw_blink_state = LED_SW_BLINK_ST_OFF;
+			return true;
+		}
+
+		/* stop blinking */
+		cyclic_unregister(&uc_plat->cyclic);
+		uc_plat->sw_blink_state = LED_SW_BLINK_ST_DISABLED;
+	}
+
+	return false;
+}
diff --git a/include/led.h b/include/led.h
index a6353166289..26955269d3e 100644
--- a/include/led.h
+++ b/include/led.h
@@ -20,6 +20,13 @@ enum led_state_t {
 	LEDST_COUNT,
 };
 
+enum led_sw_blink_state_t {
+	LED_SW_BLINK_ST_DISABLED,
+	LED_SW_BLINK_ST_NOT_READY,
+	LED_SW_BLINK_ST_OFF,
+	LED_SW_BLINK_ST_ON,
+};
+
 /**
  * struct led_uc_plat - Platform data the uclass stores about each device
  *
@@ -29,6 +36,11 @@ enum led_state_t {
 struct led_uc_plat {
 	const char *label;
 	enum led_state_t default_state;
+#ifdef CONFIG_LED_SW_BLINK
+	struct udevice *dev;
+	struct cyclic_info cyclic;
+	enum led_sw_blink_state_t sw_blink_state;
+#endif
 };
 
 /**
@@ -118,4 +130,9 @@ int led_set_period(struct udevice *dev, int period_ms);
  */
 int led_bind_generic(struct udevice *parent, const char *driver_name);
 
+/* Internal functions for software blinking. Do not use them in your code */
+int led_sw_set_period(struct udevice *dev, int period_ms);
+bool led_sw_is_blinking(struct udevice *dev);
+bool led_sw_on_state_change(struct udevice *dev, enum led_state_t state);
+
 #endif
-- 
2.39.2


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

end of thread, other threads:[~2024-07-05 12:46 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-27 11:31 [PATCH 1/2] led: Implement software led blinking Mikhail Kshevetskiy
2024-06-27 11:31 ` [PATCH 2/2] led: Add dts property to specify blinking of the led Mikhail Kshevetskiy
2024-06-27 19:05 ` [PATCH 1/2] led: Implement software led blinking Simon Glass
2024-07-02 11:54   ` Mikhail Kshevetskiy
2024-07-02 15:51     ` Simon Glass
2024-07-03  1:01       ` led blinking patches Mikhail Kshevetskiy
2024-07-03  1:01         ` [PATCH 1/2] led: Implement software led blinking Mikhail Kshevetskiy
2024-07-03  8:08           ` Simon Glass
2024-07-03 11:27           ` Rasmus Villemoes
2024-07-03  1:01         ` [PATCH 2/2] led: Add dts property to specify blinking of the led Mikhail Kshevetskiy
2024-07-03  8:08           ` Simon Glass
2024-07-05  2:20             ` Mikhail Kshevetskiy
2024-06-27 19:36 ` [PATCH 1/2] led: Implement software led blinking Tom Rini
2024-06-27 20:29   ` Christian Marangi
  -- strict thread matches above, loose matches on Subject: below --
2024-07-05  2:26 Mikhail Kshevetskiy
2024-07-05  8:29 ` Mark Kettenis
2024-07-05  9:24   ` Mikhail Kshevetskiy

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.