[PATCH] led: led_pwm: Add a driver for LEDs connected to PWM

2022-03-12 Thread Ivan Vozvakhov
Add a driver which allows to use of LEDs connected
to PWM (Linux compatible).
MAINTAINERS: add i.vozvakhov as a maintainer of leds-pwm
C(required during new functionality adding).

Signed-off-by: Ivan Vozvakhov 
---

 MAINTAINERS|   6 +
 doc/device-tree-bindings/leds/leds-pwm.txt |  47 +
 drivers/led/Kconfig|   6 +
 drivers/led/Makefile   |   1 +
 drivers/led/led_pwm.c  | 192 +
 5 files changed, 252 insertions(+)
 create mode 100644 doc/device-tree-bindings/leds/leds-pwm.txt
 create mode 100644 drivers/led/led_pwm.c

diff --git a/MAINTAINERS b/MAINTAINERS
index fb171e0c68..2e8f8cdada 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -869,6 +869,12 @@ F: doc/README.kwbimage
 F: doc/kwboot.1
 F: tools/kwb*
 
+LED
+M: Ivan Vozvakhov 
+S: Supported
+F: doc/device-tree-bindings/leds/leds-pwm.txt
+F: drivers/led/led_pwm.c
+
 LOGGING
 M: Simon Glass 
 S: Maintained
diff --git a/doc/device-tree-bindings/leds/leds-pwm.txt 
b/doc/device-tree-bindings/leds/leds-pwm.txt
new file mode 100644
index 00..186e8a848f
--- /dev/null
+++ b/doc/device-tree-bindings/leds/leds-pwm.txt
@@ -0,0 +1,47 @@
+LEDs connected to PWM (Linux compatible)
+
+Required properties:
+- compatible : should be "pwm-leds".
+
+Each LED is represented as a sub-node of the pwm-leds device.  Each
+node's name represents the name of the corresponding LED.
+
+LED sub-node properties:
+- pwms :  (required) LED pwm channel, see "pwms property" in
+  doc/device-tree-bindings/pwm/pwm.txt
+- label :  (optional) LED label, see "label property" in
+  doc/device-tree-bindings/led/common.txt
+- max-brightness :  (optional, unsigned, default 255) Maximum brightness 
possible
+  for the LED
+- active-low :  (optional, boolean, default false) For PWMs where the LED is
+  wired to supply rather than ground
+- u-boot,default-brightness :  (optional, unsigned, default 0) Initial state
+  of pwm-leds
+
+Example:
+
+leds {
+compatible = "pwm-leds";
+status = "okay";
+
+blue {
+label = "led-blue";
+pwms = < 0 10 0>;
+max-brightness = <255>;
+u-boot,default-brightness = <127>;
+};
+
+green {
+label = "led-green";
+pwms = < 0 10 0>;
+max-brightness = <255>;
+u-boot,default-brightness = <127>;
+};
+
+red {
+label = "led-red";
+pwms = < 0 10 0>;
+max-brightness = <255>;
+u-boot,default-brightness = <127>;
+};
+}
diff --git a/drivers/led/Kconfig b/drivers/led/Kconfig
index cc87fbf395..48616e2f55 100644
--- a/drivers/led/Kconfig
+++ b/drivers/led/Kconfig
@@ -42,6 +42,12 @@ config LED_CORTINA
  This option enables support for LEDs connected to the Cortina
  Access CA SOCs.
 
+config LED_PWM
+   bool "LED PWM"
+   depends on LED && DM_PWM
+   help
+ Enable support for LEDs connected to PWM.
+ Linux compatible ofdata.
 
 config LED_BLINK
bool "Support LED blinking"
diff --git a/drivers/led/Makefile b/drivers/led/Makefile
index 8e3ae7f146..c31a59e1aa 100644
--- a/drivers/led/Makefile
+++ b/drivers/led/Makefile
@@ -7,5 +7,6 @@ obj-y += led-uclass.o
 obj-$(CONFIG_LED_BCM6328) += led_bcm6328.o
 obj-$(CONFIG_LED_BCM6358) += led_bcm6358.o
 obj-$(CONFIG_LED_BCM6858) += led_bcm6858.o
+obj-$(CONFIG_LED_PWM) += led_pwm.o
 obj-$(CONFIG_$(SPL_)LED_GPIO) += led_gpio.o
 obj-$(CONFIG_LED_CORTINA) += led_cortina.o
diff --git a/drivers/led/led_pwm.c b/drivers/led/led_pwm.c
new file mode 100644
index 00..4e50272258
--- /dev/null
+++ b/drivers/led/led_pwm.c
@@ -0,0 +1,192 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2022 VK
+ * Author: Ivan Vozvakhov 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define LEDS_PWM_DRIVER_NAME   "led_pwm"
+
+struct led_pwm_priv {
+   struct udevice *pwm;
+   uint period;/* period in ns */
+   uint duty;  /* duty cycle in ns */
+   uint channel;   /* pwm channel number */
+   bool active_low;/* pwm polarity */
+   bool enabled;
+};
+
+static int led_pwm_enable(struct udevice *dev)
+{
+   struct led_pwm_priv *priv = dev_get_priv(dev);
+   int ret;
+
+   ret = pwm_set_invert(priv->pwm, priv->channel, priv->active_low);
+   if (ret)
+   return ret;
+
+   ret = pwm_set_config(priv->pwm, priv->channel, priv->period, 
priv->duty);
+   if (ret)
+   return ret;
+
+   ret = pwm_set_enable(priv->pwm, priv->channel, true);
+   if (ret)
+   return ret;
+
+   priv->enabled = true;
+
+   return 0;
+}
+
+static int led_pwm_disable(struct udevice *dev)
+{
+  

[PATCH] led: led_pwm: Add a driver for LEDs connected to PWM

2022-02-27 Thread Ivan Vozvakhov
From: Ivan Vozvakhov 

Add a driver which allows to use of LEDs connected
to PWM (Linux compatible).
MAINTAINERS: add i.vozvakhov as a maintainer of leds-pwm
(required during new functionality adding).

Signed-off-by: Ivan Vozvakhov 
Signed-off-by: Ivan Vozvakhov 
---

 MAINTAINERS|   6 +
 doc/device-tree-bindings/leds/leds-pwm.txt |  47 +
 drivers/led/Kconfig|   6 +
 drivers/led/Makefile   |   1 +
 drivers/led/led_pwm.c  | 189 +
 5 files changed, 249 insertions(+)
 create mode 100644 doc/device-tree-bindings/leds/leds-pwm.txt
 create mode 100644 drivers/led/led_pwm.c

diff --git a/MAINTAINERS b/MAINTAINERS
index fb171e0c68..2e8f8cdada 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -869,6 +869,12 @@ F: doc/README.kwbimage
 F: doc/kwboot.1
 F: tools/kwb*
 
+LED
+M: Ivan Vozvakhov 
+S: Supported
+F: doc/device-tree-bindings/leds/leds-pwm.txt
+F: drivers/led/led_pwm.c
+
 LOGGING
 M: Simon Glass 
 S: Maintained
diff --git a/doc/device-tree-bindings/leds/leds-pwm.txt 
b/doc/device-tree-bindings/leds/leds-pwm.txt
new file mode 100644
index 00..186e8a848f
--- /dev/null
+++ b/doc/device-tree-bindings/leds/leds-pwm.txt
@@ -0,0 +1,47 @@
+LEDs connected to PWM (Linux compatible)
+
+Required properties:
+- compatible : should be "pwm-leds".
+
+Each LED is represented as a sub-node of the pwm-leds device.  Each
+node's name represents the name of the corresponding LED.
+
+LED sub-node properties:
+- pwms :  (required) LED pwm channel, see "pwms property" in
+  doc/device-tree-bindings/pwm/pwm.txt
+- label :  (optional) LED label, see "label property" in
+  doc/device-tree-bindings/led/common.txt
+- max-brightness :  (optional, unsigned, default 255) Maximum brightness 
possible
+  for the LED
+- active-low :  (optional, boolean, default false) For PWMs where the LED is
+  wired to supply rather than ground
+- u-boot,default-brightness :  (optional, unsigned, default 0) Initial state
+  of pwm-leds
+
+Example:
+
+leds {
+compatible = "pwm-leds";
+status = "okay";
+
+blue {
+label = "led-blue";
+pwms = < 0 10 0>;
+max-brightness = <255>;
+u-boot,default-brightness = <127>;
+};
+
+green {
+label = "led-green";
+pwms = < 0 10 0>;
+max-brightness = <255>;
+u-boot,default-brightness = <127>;
+};
+
+red {
+label = "led-red";
+pwms = < 0 10 0>;
+max-brightness = <255>;
+u-boot,default-brightness = <127>;
+};
+}
diff --git a/drivers/led/Kconfig b/drivers/led/Kconfig
index cc87fbf395..48616e2f55 100644
--- a/drivers/led/Kconfig
+++ b/drivers/led/Kconfig
@@ -42,6 +42,12 @@ config LED_CORTINA
  This option enables support for LEDs connected to the Cortina
  Access CA SOCs.
 
+config LED_PWM
+   bool "LED PWM"
+   depends on LED && DM_PWM
+   help
+ Enable support for LEDs connected to PWM.
+ Linux compatible ofdata.
 
 config LED_BLINK
bool "Support LED blinking"
diff --git a/drivers/led/Makefile b/drivers/led/Makefile
index 8e3ae7f146..c31a59e1aa 100644
--- a/drivers/led/Makefile
+++ b/drivers/led/Makefile
@@ -7,5 +7,6 @@ obj-y += led-uclass.o
 obj-$(CONFIG_LED_BCM6328) += led_bcm6328.o
 obj-$(CONFIG_LED_BCM6358) += led_bcm6358.o
 obj-$(CONFIG_LED_BCM6858) += led_bcm6858.o
+obj-$(CONFIG_LED_PWM) += led_pwm.o
 obj-$(CONFIG_$(SPL_)LED_GPIO) += led_gpio.o
 obj-$(CONFIG_LED_CORTINA) += led_cortina.o
diff --git a/drivers/led/led_pwm.c b/drivers/led/led_pwm.c
new file mode 100644
index 00..d36ce4c0df
--- /dev/null
+++ b/drivers/led/led_pwm.c
@@ -0,0 +1,189 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2022 VK
+ * Author: Ivan Vozvakhov 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define LEDS_PWM_DRIVER_NAME   "led_pwm"
+
+struct led_pwm_priv {
+   struct udevice *pwm;
+   uint active_low;/* polarity */
+   uint period;/* period_ns */
+   uint duty;
+   uint channel;
+   bool enabled;
+   bool polarity;
+};
+
+static int led_pwm_enable(struct udevice *dev)
+{
+   struct led_pwm_priv *priv = dev_get_priv(dev);
+   int ret;
+
+   ret = pwm_set_invert(priv->pwm, priv->channel, priv->polarity);
+   if (ret)
+   return ret;
+
+   ret = pwm_set_config(priv->pwm, priv->channel, priv->period, 
priv->duty);
+   if (ret)
+   return ret;
+
+   ret = pwm_set_enable(priv->pwm, priv->channel, true);
+   if (ret)
+   return ret;
+
+   priv->enabled = true;
+
+   return 0;
+}
+
+static int led_pwm_disable(struct udevi