Linux-LEDs Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH v2 0/2] Add Turris Omnia LEDs driver
@ 2020-04-23  6:50 Marek Behún
  2020-04-23  6:50 ` [PATCH v2 1/2] dt-bindings: leds: add cznic,turris-omnia-leds binding Marek Behún
  2020-04-23  6:51 ` [PATCH v2 2/2] leds: initial support for Turris Omnia LEDs Marek Behún
  0 siblings, 2 replies; 9+ messages in thread
From: Marek Behún @ 2020-04-23  6:50 UTC (permalink / raw)
  To: linux-leds; +Cc: Pavel Machek, Jacek Anaszewski, Marek Behún

The only changes from previous version is that MAINTAINERS file is not
updated. This will be done separately through another tree.

Marek

Marek Behún (2):
  dt-bindings: leds: add cznic,turris-omnia-leds binding
  leds: initial support for Turris Omnia LEDs

 .../leds/cznic,turris-omnia-leds.yaml         | 113 +++++++
 drivers/leds/Kconfig                          |  11 +
 drivers/leds/Makefile                         |   1 +
 drivers/leds/leds-turris-omnia.c              | 285 ++++++++++++++++++
 4 files changed, 410 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/cznic,turris-omnia-leds.yaml
 create mode 100644 drivers/leds/leds-turris-omnia.c

-- 
2.24.1


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

* [PATCH v2 1/2] dt-bindings: leds: add cznic,turris-omnia-leds binding
  2020-04-23  6:50 [PATCH v2 0/2] Add Turris Omnia LEDs driver Marek Behún
@ 2020-04-23  6:50 ` Marek Behún
  2020-05-11 19:43   ` Rob Herring
  2020-04-23  6:51 ` [PATCH v2 2/2] leds: initial support for Turris Omnia LEDs Marek Behún
  1 sibling, 1 reply; 9+ messages in thread
From: Marek Behún @ 2020-04-23  6:50 UTC (permalink / raw)
  To: linux-leds
  Cc: Pavel Machek, Jacek Anaszewski, Marek Behún, Rob Herring,
	devicetree

Add device-tree bindings documentation for Turris Omnia RGB LEDs.

Signed-off-by: Marek Behún <marek.behun@nic.cz>
Cc: Rob Herring <robh+dt@kernel.org>
Cc: devicetree@vger.kernel.org
---
 .../leds/cznic,turris-omnia-leds.yaml         | 113 ++++++++++++++++++
 1 file changed, 113 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/cznic,turris-omnia-leds.yaml

diff --git a/Documentation/devicetree/bindings/leds/cznic,turris-omnia-leds.yaml b/Documentation/devicetree/bindings/leds/cznic,turris-omnia-leds.yaml
new file mode 100644
index 000000000000..f3d13fa65df2
--- /dev/null
+++ b/Documentation/devicetree/bindings/leds/cznic,turris-omnia-leds.yaml
@@ -0,0 +1,113 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/leds/cznic,turris-omnia-leds.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: CZ.NIC's Turris Omnia LEDs driver
+
+maintainers:
+  - Marek Behún <marek.behun@nic.cz>
+
+description:
+  This module adds support for the RGB LEDs found on the fron panel of the
+  Turris Omnia router. There are 12 RGB LEDs, they are controlled by device's
+  microcontroller with which the system communicates via I2C. Each LED is
+  described as a subnode of this I2C device.
+
+properties:
+  compatible:
+    const: cznic,turris-omnia-leds
+
+  reg:
+    description: I2C slave address of the microcontroller.
+    maxItems: 1
+
+patternProperties:
+  "^led[0-9][0-9]?$":
+    type: object
+    allOf:
+      - $ref: /schemas/leds/common.yaml#
+    description:
+      This node can either represent one channel of a RGB LED or a whole RGB
+      LED.
+
+    properties:
+      led-sources:
+        description:
+          List of device current outputs the LED connects to. If one number is
+          given, the device described by this node will control one channel of a
+          RGB LED. If three numbers are given (one for each channel of a RGB
+          LED), the device described by this node will control one RGB LED. In
+          this case the numbers must increase by one and the first must be a
+          multiple of 3.
+        allOf:
+          - $ref: /schemas/leds/common.yaml#/properties/led-sources
+          - oneOf:
+              - items:
+                  minimum: 0
+                  maximum: 35
+                minItems: 1
+                maxItems: 1
+              - items:
+                  enum: [ [ 0, 1, 2 ], [ 3, 4, 5 ], [ 6, 7, 8 ], [ 9, 10, 11 ],
+                          [ 12, 13, 14 ], [ 15, 16, 17 ], [ 18, 19, 20 ],
+                          [ 21, 22, 23 ], [ 24, 25, 26 ], [ 27, 28, 29 ],
+                          [ 30, 31, 32 ], [ 33, 34, 35] ]
+
+      color:
+        description:
+          Should be one of LED_COLOR_ID_*. Allowed values are red, green or blue
+          if there is one item in led-sources, otherwise white.
+        allOf:
+          - $ref: /schemas/leds/common.yaml#/properties/color
+          - enum: [ 0, 1, 2, 3 ]
+
+    required:
+      - led-sources
+      - color
+
+examples:
+  - |
+
+    #include <dt-bindings/leds/common.h>
+
+    i2c0 {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        led-controller@2b {
+            compatible = "cznic,turris-omnia-leds";
+            reg = <0x2b>;
+
+            led0 {
+                led-sources = <33 34 35>;
+                color = <LED_COLOR_ID_WHITE>;
+                function = LED_FUNCTION_POWER;
+                linux,default-trigger = "heartbeat";
+            };
+
+            led1 {
+                led-sources = <3>;
+                color = <LED_COLOR_ID_RED>;
+                function = LED_FUNCTION_INDICATOR;
+                function-enumerator = <1>;
+            };
+
+            led2 {
+                led-sources = <4>;
+                color = <LED_COLOR_ID_GREEN>;
+                function = LED_FUNCTION_INDICATOR;
+                function-enumerator = <1>;
+            };
+
+            led3 {
+                led-sources = <5>;
+                color = <LED_COLOR_ID_BLUE>;
+                function = LED_FUNCTION_INDICATOR;
+                function-enumerator = <1>;
+            };
+        };
+    };
+
+...
-- 
2.24.1


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

* [PATCH v2 2/2] leds: initial support for Turris Omnia LEDs
  2020-04-23  6:50 [PATCH v2 0/2] Add Turris Omnia LEDs driver Marek Behún
  2020-04-23  6:50 ` [PATCH v2 1/2] dt-bindings: leds: add cznic,turris-omnia-leds binding Marek Behún
@ 2020-04-23  6:51 ` Marek Behún
  2020-05-11 19:54   ` Dan Murphy
  1 sibling, 1 reply; 9+ messages in thread
From: Marek Behún @ 2020-04-23  6:51 UTC (permalink / raw)
  To: linux-leds; +Cc: Pavel Machek, Jacek Anaszewski, Marek Behún

This adds basic support for LEDs on the front side of CZ.NIC's Turris
Omnia router.

There are 12 RGB LEDs. The controller supports HW triggering mode for
the LEDs, but this driver does not support it yet, and sets all the LEDs
into SW mode upon probe.

The user can either group all three channels of one RGB LED into one LED
classdev, or expose each channel as an individual LED classdev. This is
done by utilizing the 'led-sources' and 'color' DT properties.

Signed-off-by: Marek Behún <marek.behun@nic.cz>
---
 drivers/leds/Kconfig             |  11 ++
 drivers/leds/Makefile            |   1 +
 drivers/leds/leds-turris-omnia.c | 285 +++++++++++++++++++++++++++++++
 3 files changed, 297 insertions(+)
 create mode 100644 drivers/leds/leds-turris-omnia.c

diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
index c664d84e1667..7663a5cd9fb5 100644
--- a/drivers/leds/Kconfig
+++ b/drivers/leds/Kconfig
@@ -145,6 +145,17 @@ config LEDS_EL15203000
 	  To compile this driver as a module, choose M here: the module
 	  will be called leds-el15203000.
 
+config LEDS_TURRIS_OMNIA
+	tristate "LED support for CZ.NIC's Turris Omnia"
+	depends on LEDS_CLASS
+	depends on I2C
+	depends on MACH_ARMADA_38X || COMPILE_TEST
+	depends on OF
+	help
+	  This option enables basic support for the LEDs found on the front
+	  side of CZ.NIC's Turris Omnia router. There are 12 RGB LEDs on the
+	  front panel.
+
 config LEDS_LM3530
 	tristate "LCD Backlight driver for LM3530"
 	depends on LEDS_CLASS
diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile
index 45235d5fb218..fd61421f7d40 100644
--- a/drivers/leds/Makefile
+++ b/drivers/leds/Makefile
@@ -83,6 +83,7 @@ obj-$(CONFIG_LEDS_TCA6507)		+= leds-tca6507.o
 obj-$(CONFIG_LEDS_TI_LMU_COMMON)	+= leds-ti-lmu-common.o
 obj-$(CONFIG_LEDS_TLC591XX)		+= leds-tlc591xx.o
 obj-$(CONFIG_LEDS_TPS6105X)		+= leds-tps6105x.o
+obj-$(CONFIG_LEDS_TURRIS_OMNIA)		+= leds-turris-omnia.o
 obj-$(CONFIG_LEDS_WM831X_STATUS)	+= leds-wm831x-status.o
 obj-$(CONFIG_LEDS_WM8350)		+= leds-wm8350.o
 obj-$(CONFIG_LEDS_WRAP)			+= leds-wrap.o
diff --git a/drivers/leds/leds-turris-omnia.c b/drivers/leds/leds-turris-omnia.c
new file mode 100644
index 000000000000..aafb4be9b225
--- /dev/null
+++ b/drivers/leds/leds-turris-omnia.c
@@ -0,0 +1,285 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * CZ.NIC's Turris Omnia LEDs driver
+ *
+ * 2020 by Marek Behun <marek.behun@nic.cz>
+ */
+
+#include <linux/i2c.h>
+#include <linux/leds.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+#include <uapi/linux/uleds.h>
+#include "leds.h"
+
+#define OMNIA_BOARD_LEDS		12
+
+#define CMD_LED_MODE			3
+#define CMD_LED_MODE_LED(l)		((l) & 0x0f)
+#define CMD_LED_MODE_USER		0x10
+
+#define CMD_LED_STATE			4
+#define CMD_LED_STATE_LED(l)		((l) & 0x0f)
+#define CMD_LED_STATE_ON		0x10
+
+#define CMD_LED_COLOR			5
+#define CMD_LED_SET_BRIGHTNESS		7
+#define CMD_LED_GET_BRIGHTNESS		8
+
+#define OMNIA_CMD			0
+
+#define OMNIA_CMD_LED_COLOR_LED		1
+#define OMNIA_CMD_LED_COLOR_R		2
+#define OMNIA_CMD_LED_COLOR_G		3
+#define OMNIA_CMD_LED_COLOR_B		4
+#define OMNIA_CMD_LED_COLOR_LEN		5
+
+struct omnia_led {
+	struct led_classdev cdev;
+	int reg, color;
+};
+
+#define to_omnia_led(l)	container_of(l, struct omnia_led, cdev)
+
+struct omnia_leds {
+	struct i2c_client *client;
+	struct mutex lock;
+	u8 cache[OMNIA_BOARD_LEDS][3];
+	int nleds;
+	struct omnia_led leds[0];
+};
+
+static int omnia_led_brightness_set_blocking(struct led_classdev *cdev,
+					     enum led_brightness brightness)
+{
+	static const u8 color2cmd[] = {
+		[LED_COLOR_ID_RED] = OMNIA_CMD_LED_COLOR_R,
+		[LED_COLOR_ID_GREEN] = OMNIA_CMD_LED_COLOR_G,
+		[LED_COLOR_ID_BLUE] = OMNIA_CMD_LED_COLOR_B,
+	};
+	struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent);
+	struct omnia_led *led = to_omnia_led(cdev);
+	u8 buf[OMNIA_CMD_LED_COLOR_LEN], state;
+	int ret;
+
+	mutex_lock(&leds->lock);
+
+	buf[OMNIA_CMD] = CMD_LED_COLOR;
+	buf[OMNIA_CMD_LED_COLOR_LED] = led->reg;
+
+	if (led->color == LED_COLOR_ID_WHITE) {
+		buf[OMNIA_CMD_LED_COLOR_R] = brightness;
+		buf[OMNIA_CMD_LED_COLOR_G] = brightness;
+		buf[OMNIA_CMD_LED_COLOR_B] = brightness;
+	} else {
+		buf[OMNIA_CMD_LED_COLOR_R] = leds->cache[led->reg][0];
+		buf[OMNIA_CMD_LED_COLOR_G] = leds->cache[led->reg][1];
+		buf[OMNIA_CMD_LED_COLOR_B] = leds->cache[led->reg][2];
+		buf[color2cmd[led->color]] = brightness;
+	}
+
+	state = CMD_LED_STATE_LED(led->reg);
+	if (buf[OMNIA_CMD_LED_COLOR_R] || buf[OMNIA_CMD_LED_COLOR_G] ||
+	    buf[OMNIA_CMD_LED_COLOR_B])
+		state |= CMD_LED_STATE_ON;
+
+	ret = i2c_smbus_write_byte_data(leds->client, CMD_LED_STATE, state);
+	if (ret >= 0 && (state & CMD_LED_STATE_ON))
+		ret = i2c_master_send(leds->client, buf, 5);
+
+	leds->cache[led->reg][0] = buf[OMNIA_CMD_LED_COLOR_R];
+	leds->cache[led->reg][1] = buf[OMNIA_CMD_LED_COLOR_G];
+	leds->cache[led->reg][2] = buf[OMNIA_CMD_LED_COLOR_B];
+
+	mutex_unlock(&leds->lock);
+
+	return ret;
+}
+
+static int omnia_led_register(struct omnia_leds *leds,
+			      struct fwnode_handle *node)
+{
+	struct i2c_client *client = leds->client;
+	struct led_init_data init_data = {};
+	struct device *dev = &client->dev;
+	struct omnia_led *led;
+	int ret, nsources, color;
+	u32 led_sources[3];
+
+	led = &leds->leds[leds->nleds];
+
+	nsources = fwnode_property_count_u32(node, "led-sources");
+	if (nsources != 1 && nsources != 3) {
+		dev_warn(dev,
+			 "Node %pfw: 'led-sources' must contain either 1 or 3 items!\n",
+			 node);
+		return 0;
+	}
+
+	ret = fwnode_property_read_u32_array(node, "led-sources", led_sources,
+					     nsources);
+	if (ret) {
+		dev_err(dev, "Node %pfw: 'led-sources' read failed: %i\n",
+			node, ret);
+		return ret;
+	}
+
+	ret = fwnode_property_read_u32(node, "color", &led->color);
+	if (ret) {
+		dev_warn(dev, "Node %pfw: 'color' read failed!\n",
+			 node);
+		return 0;
+	}
+
+	if (nsources == 3) {
+		if ((led_sources[0] % 3) != 0 ||
+		    led_sources[1] != led_sources[0] + 1 ||
+		    led_sources[2] != led_sources[0] + 2 ||
+		    led_sources[2] >= OMNIA_BOARD_LEDS * 3) {
+			dev_warn(dev, "Node %pfw has invalid 'led-sources'!\n",
+				 node);
+			return 0;
+		}
+
+		color = LED_COLOR_ID_WHITE;
+	} else {
+		const int led_source_to_color[3] = {
+			LED_COLOR_ID_RED,
+			LED_COLOR_ID_GREEN,
+			LED_COLOR_ID_BLUE
+		};
+		color = led_source_to_color[led_sources[0] % 3];
+
+		if (led_sources[0] >= OMNIA_BOARD_LEDS * 3) {
+			dev_warn(dev, "Node %pfw has invalid 'led-sources'!\n",
+				 node);
+			return 0;
+		}
+	}
+
+	if (led->color != color) {
+		dev_warn(dev, "Node %pfw: 'color' should be %s!\n", node,
+			 led_colors[color]);
+		return 0;
+	}
+
+	init_data.fwnode = node;
+
+	led->reg = led_sources[0] / 3;
+	led->cdev.max_brightness = 255;
+	led->cdev.brightness_set_blocking = omnia_led_brightness_set_blocking;
+
+	fwnode_property_read_string(node, "linux,default-trigger",
+				    &led->cdev.default_trigger);
+
+	/* put the LED into software mode */
+	ret = i2c_smbus_write_byte_data(client, CMD_LED_MODE,
+					CMD_LED_MODE_LED(led->reg) |
+					CMD_LED_MODE_USER);
+	if (ret < 0) {
+		dev_err(dev, "Cannot set LED %pfw to software mode: %i\n", node,
+			ret);
+		return ret;
+	}
+
+	/* disable the LED */
+	ret = i2c_smbus_write_byte_data(client, CMD_LED_STATE,
+						CMD_LED_STATE_LED(led->reg));
+	if (ret < 0) {
+		dev_err(dev, "Cannot set LED %pfw brightness: %i\n", node, ret);
+		return ret;
+	}
+
+	ret = devm_led_classdev_register_ext(dev, &led->cdev, &init_data);
+	if (ret < 0) {
+		dev_err(dev, "Cannot register LED %pfw: %i\n", node, ret);
+		return ret;
+	}
+
+	++leds->nleds;
+
+	return 0;
+}
+
+static int omnia_leds_probe(struct i2c_client *client,
+			    const struct i2c_device_id *id)
+{
+	struct device *dev = &client->dev;
+	struct device_node *np = dev->of_node, *child;
+	struct omnia_leds *leds;
+	int ret, count;
+
+	count = of_get_available_child_count(np);
+	if (!count) {
+		dev_err(dev, "LEDs are not defined in device tree!\n");
+		return -ENODEV;
+	} else if (count > 3 * OMNIA_BOARD_LEDS) {
+		dev_err(dev, "Too many LEDs defined in device tree!\n");
+		return -EINVAL;
+	}
+
+	leds = devm_kzalloc(dev, sizeof(*leds) + count * sizeof(leds->leds[0]),
+			    GFP_KERNEL);
+	if (!leds)
+		return -ENOMEM;
+
+	leds->client = client;
+	i2c_set_clientdata(client, leds);
+
+	mutex_init(&leds->lock);
+
+	for_each_available_child_of_node(np, child) {
+		ret = omnia_led_register(leds, &child->fwnode);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+
+static int omnia_leds_remove(struct i2c_client *client)
+{
+	u8 buf[OMNIA_CMD_LED_COLOR_LEN];
+
+	/* put all LEDs into default (HW triggered) mode */
+	i2c_smbus_write_byte_data(client, CMD_LED_MODE,
+				  CMD_LED_MODE_LED(OMNIA_BOARD_LEDS));
+
+	/* set all LEDs color to [255, 255, 255] */
+	buf[OMNIA_CMD] = CMD_LED_COLOR;
+	buf[OMNIA_CMD_LED_COLOR_LED] = OMNIA_BOARD_LEDS;
+	buf[OMNIA_CMD_LED_COLOR_R] = 255;
+	buf[OMNIA_CMD_LED_COLOR_G] = 255;
+	buf[OMNIA_CMD_LED_COLOR_B] = 255;
+
+	i2c_master_send(client, buf, 5);
+
+	return 0;
+}
+
+static const struct of_device_id of_omnia_leds_match[] = {
+	{ .compatible = "cznic,turris-omnia-leds", },
+	{},
+};
+
+static const struct i2c_device_id omnia_id[] = {
+	{ "omnia", 0 },
+	{ }
+};
+
+static struct i2c_driver omnia_leds_driver = {
+	.probe		= omnia_leds_probe,
+	.remove		= omnia_leds_remove,
+	.id_table	= omnia_id,
+	.driver		= {
+		.name	= "leds-turris-omnia",
+		.of_match_table = of_omnia_leds_match,
+	},
+};
+
+module_i2c_driver(omnia_leds_driver);
+
+MODULE_AUTHOR("Marek Behun <marek.behun@nic.cz>");
+MODULE_DESCRIPTION("CZ.NIC's Turris Omnia LEDs");
+MODULE_LICENSE("GPL v2");
-- 
2.24.1


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

* Re: [PATCH v2 1/2] dt-bindings: leds: add cznic,turris-omnia-leds binding
  2020-04-23  6:50 ` [PATCH v2 1/2] dt-bindings: leds: add cznic,turris-omnia-leds binding Marek Behún
@ 2020-05-11 19:43   ` Rob Herring
  2020-05-11 20:01     ` Marek Behun
  0 siblings, 1 reply; 9+ messages in thread
From: Rob Herring @ 2020-05-11 19:43 UTC (permalink / raw)
  To: Marek Behún; +Cc: linux-leds, Pavel Machek, Jacek Anaszewski, devicetree

On Thu, Apr 23, 2020 at 08:50:59AM +0200, Marek Behún wrote:
> Add device-tree bindings documentation for Turris Omnia RGB LEDs.
> 
> Signed-off-by: Marek Behún <marek.behun@nic.cz>
> Cc: Rob Herring <robh+dt@kernel.org>
> Cc: devicetree@vger.kernel.org
> ---
>  .../leds/cznic,turris-omnia-leds.yaml         | 113 ++++++++++++++++++
>  1 file changed, 113 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/leds/cznic,turris-omnia-leds.yaml
> 
> diff --git a/Documentation/devicetree/bindings/leds/cznic,turris-omnia-leds.yaml b/Documentation/devicetree/bindings/leds/cznic,turris-omnia-leds.yaml
> new file mode 100644
> index 000000000000..f3d13fa65df2
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/leds/cznic,turris-omnia-leds.yaml
> @@ -0,0 +1,113 @@
> +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/leds/cznic,turris-omnia-leds.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: CZ.NIC's Turris Omnia LEDs driver
> +
> +maintainers:
> +  - Marek Behún <marek.behun@nic.cz>
> +
> +description:
> +  This module adds support for the RGB LEDs found on the fron panel of the
> +  Turris Omnia router. There are 12 RGB LEDs, they are controlled by device's
> +  microcontroller with which the system communicates via I2C. Each LED is
> +  described as a subnode of this I2C device.
> +
> +properties:
> +  compatible:
> +    const: cznic,turris-omnia-leds
> +
> +  reg:
> +    description: I2C slave address of the microcontroller.
> +    maxItems: 1
> +
> +patternProperties:
> +  "^led[0-9][0-9]?$":

'led-' is preferred. And numbering is usually hex.

What's the numbering here based on? Might be better to use 'reg' (and a 
unit-address) rather than led-sources. led-sources was for cases where 
the control interface (defined by reg) could have differing regulators 
connected to the LEDs.

> +    type: object
> +    allOf:
> +      - $ref: /schemas/leds/common.yaml#
> +    description:
> +      This node can either represent one channel of a RGB LED or a whole RGB
> +      LED.
> +
> +    properties:
> +      led-sources:
> +        description:
> +          List of device current outputs the LED connects to. If one number is
> +          given, the device described by this node will control one channel of a
> +          RGB LED. If three numbers are given (one for each channel of a RGB
> +          LED), the device described by this node will control one RGB LED. In
> +          this case the numbers must increase by one and the first must be a
> +          multiple of 3.
> +        allOf:
> +          - $ref: /schemas/leds/common.yaml#/properties/led-sources

No need for this. It is already done with the previous $ref.

> +          - oneOf:
> +              - items:
> +                  minimum: 0
> +                  maximum: 35
> +                minItems: 1
> +                maxItems: 1
> +              - items:
> +                  enum: [ [ 0, 1, 2 ], [ 3, 4, 5 ], [ 6, 7, 8 ], [ 9, 10, 11 ],
> +                          [ 12, 13, 14 ], [ 15, 16, 17 ], [ 18, 19, 20 ],
> +                          [ 21, 22, 23 ], [ 24, 25, 26 ], [ 27, 28, 29 ],
> +                          [ 30, 31, 32 ], [ 33, 34, 35] ]
> +
> +      color:
> +        description:
> +          Should be one of LED_COLOR_ID_*. Allowed values are red, green or blue
> +          if there is one item in led-sources, otherwise white.
> +        allOf:
> +          - $ref: /schemas/leds/common.yaml#/properties/color

Can be dropped.

> +          - enum: [ 0, 1, 2, 3 ]
> +
> +    required:
> +      - led-sources
> +      - color
> +
> +examples:
> +  - |
> +
> +    #include <dt-bindings/leds/common.h>
> +
> +    i2c0 {
> +        #address-cells = <1>;
> +        #size-cells = <0>;
> +
> +        led-controller@2b {
> +            compatible = "cznic,turris-omnia-leds";
> +            reg = <0x2b>;
> +
> +            led0 {
> +                led-sources = <33 34 35>;
> +                color = <LED_COLOR_ID_WHITE>;
> +                function = LED_FUNCTION_POWER;
> +                linux,default-trigger = "heartbeat";
> +            };
> +
> +            led1 {
> +                led-sources = <3>;
> +                color = <LED_COLOR_ID_RED>;
> +                function = LED_FUNCTION_INDICATOR;
> +                function-enumerator = <1>;
> +            };
> +
> +            led2 {
> +                led-sources = <4>;
> +                color = <LED_COLOR_ID_GREEN>;
> +                function = LED_FUNCTION_INDICATOR;
> +                function-enumerator = <1>;
> +            };
> +
> +            led3 {
> +                led-sources = <5>;
> +                color = <LED_COLOR_ID_BLUE>;
> +                function = LED_FUNCTION_INDICATOR;
> +                function-enumerator = <1>;
> +            };
> +        };
> +    };
> +
> +...
> -- 
> 2.24.1
> 

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

* Re: [PATCH v2 2/2] leds: initial support for Turris Omnia LEDs
  2020-04-23  6:51 ` [PATCH v2 2/2] leds: initial support for Turris Omnia LEDs Marek Behún
@ 2020-05-11 19:54   ` Dan Murphy
  2020-05-11 20:17     ` Marek Behun
  0 siblings, 1 reply; 9+ messages in thread
From: Dan Murphy @ 2020-05-11 19:54 UTC (permalink / raw)
  To: Marek Behún, linux-leds; +Cc: Pavel Machek, Jacek Anaszewski

Marek

On 4/23/20 1:51 AM, Marek Behún wrote:
> This adds basic support for LEDs on the front side of CZ.NIC's Turris
> Omnia router.
>
> There are 12 RGB LEDs. The controller supports HW triggering mode for
> the LEDs, but this driver does not support it yet, and sets all the LEDs
> into SW mode upon probe.
>
> The user can either group all three channels of one RGB LED into one LED
> classdev, or expose each channel as an individual LED classdev. This is
> done by utilizing the 'led-sources' and 'color' DT properties.

I think this would be a good candidate for the multicolor framework.  It 
would make the registration, brightness caching and color mapping easier

We are waiting on maintainer feedback for this.

https://lore.kernel.org/patchwork/project/lkml/list/?series=441958

But until then comments below

>
> Signed-off-by: Marek Behún <marek.behun@nic.cz>
> ---
>   drivers/leds/Kconfig             |  11 ++
>   drivers/leds/Makefile            |   1 +
>   drivers/leds/leds-turris-omnia.c | 285 +++++++++++++++++++++++++++++++
>   3 files changed, 297 insertions(+)
>   create mode 100644 drivers/leds/leds-turris-omnia.c
>
> diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
> index c664d84e1667..7663a5cd9fb5 100644
> --- a/drivers/leds/Kconfig
> +++ b/drivers/leds/Kconfig
> @@ -145,6 +145,17 @@ config LEDS_EL15203000
>   	  To compile this driver as a module, choose M here: the module
>   	  will be called leds-el15203000.
>   
> +config LEDS_TURRIS_OMNIA
> +	tristate "LED support for CZ.NIC's Turris Omnia"
> +	depends on LEDS_CLASS
> +	depends on I2C
REGMAP?
> +	depends on MACH_ARMADA_38X || COMPILE_TEST
Why is tied to a Marvel processor?
> +	depends on OF
> +	help
> +	  This option enables basic support for the LEDs found on the front
> +	  side of CZ.NIC's Turris Omnia router. There are 12 RGB LEDs on the
> +	  front panel.
> +
>   config LEDS_LM3530
>   	tristate "LCD Backlight driver for LM3530"
>   	depends on LEDS_CLASS
> diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile
> index 45235d5fb218..fd61421f7d40 100644
> --- a/drivers/leds/Makefile
> +++ b/drivers/leds/Makefile
> @@ -83,6 +83,7 @@ obj-$(CONFIG_LEDS_TCA6507)		+= leds-tca6507.o
>   obj-$(CONFIG_LEDS_TI_LMU_COMMON)	+= leds-ti-lmu-common.o
>   obj-$(CONFIG_LEDS_TLC591XX)		+= leds-tlc591xx.o
>   obj-$(CONFIG_LEDS_TPS6105X)		+= leds-tps6105x.o
> +obj-$(CONFIG_LEDS_TURRIS_OMNIA)		+= leds-turris-omnia.o
>   obj-$(CONFIG_LEDS_WM831X_STATUS)	+= leds-wm831x-status.o
>   obj-$(CONFIG_LEDS_WM8350)		+= leds-wm8350.o
>   obj-$(CONFIG_LEDS_WRAP)			+= leds-wrap.o
> diff --git a/drivers/leds/leds-turris-omnia.c b/drivers/leds/leds-turris-omnia.c
> new file mode 100644
> index 000000000000..aafb4be9b225
> --- /dev/null
> +++ b/drivers/leds/leds-turris-omnia.c
> @@ -0,0 +1,285 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * CZ.NIC's Turris Omnia LEDs driver
> + *
> + * 2020 by Marek Behun <marek.behun@nic.cz>
> + */
> +
> +#include <linux/i2c.h>
> +#include <linux/leds.h>
> +#include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/of.h>
> +#include <uapi/linux/uleds.h>
> +#include "leds.h"
> +
> +#define OMNIA_BOARD_LEDS		12
> +
> +#define CMD_LED_MODE			3
> +#define CMD_LED_MODE_LED(l)		((l) & 0x0f)
> +#define CMD_LED_MODE_USER		0x10
> +
> +#define CMD_LED_STATE			4
> +#define CMD_LED_STATE_LED(l)		((l) & 0x0f)
> +#define CMD_LED_STATE_ON		0x10
> +
> +#define CMD_LED_COLOR			5
> +#define CMD_LED_SET_BRIGHTNESS		7
> +#define CMD_LED_GET_BRIGHTNESS		8
> +
> +#define OMNIA_CMD			0
> +
> +#define OMNIA_CMD_LED_COLOR_LED		1
> +#define OMNIA_CMD_LED_COLOR_R		2
> +#define OMNIA_CMD_LED_COLOR_G		3
> +#define OMNIA_CMD_LED_COLOR_B		4
> +#define OMNIA_CMD_LED_COLOR_LEN		5
> +
> +struct omnia_led {
> +	struct led_classdev cdev;
> +	int reg, color;
> +};
> +
> +#define to_omnia_led(l)	container_of(l, struct omnia_led, cdev)
> +
> +struct omnia_leds {
> +	struct i2c_client *client;
> +	struct mutex lock;
> +	u8 cache[OMNIA_BOARD_LEDS][3];
> +	int nleds;
> +	struct omnia_led leds[0];
No need for the [0] in the flexible array
> +};
> +
> +static int omnia_led_brightness_set_blocking(struct led_classdev *cdev,
> +					     enum led_brightness brightness)
> +{
> +	static const u8 color2cmd[] = {
> +		[LED_COLOR_ID_RED] = OMNIA_CMD_LED_COLOR_R,
> +		[LED_COLOR_ID_GREEN] = OMNIA_CMD_LED_COLOR_G,
> +		[LED_COLOR_ID_BLUE] = OMNIA_CMD_LED_COLOR_B,
> +	};
> +	struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent);
> +	struct omnia_led *led = to_omnia_led(cdev);
> +	u8 buf[OMNIA_CMD_LED_COLOR_LEN], state;
> +	int ret;
> +
> +	mutex_lock(&leds->lock);
> +
> +	buf[OMNIA_CMD] = CMD_LED_COLOR;
> +	buf[OMNIA_CMD_LED_COLOR_LED] = led->reg;
> +
> +	if (led->color == LED_COLOR_ID_WHITE) {
> +		buf[OMNIA_CMD_LED_COLOR_R] = brightness;
> +		buf[OMNIA_CMD_LED_COLOR_G] = brightness;
> +		buf[OMNIA_CMD_LED_COLOR_B] = brightness;
> +	} else {
> +		buf[OMNIA_CMD_LED_COLOR_R] = leds->cache[led->reg][0];
> +		buf[OMNIA_CMD_LED_COLOR_G] = leds->cache[led->reg][1];
> +		buf[OMNIA_CMD_LED_COLOR_B] = leds->cache[led->reg][2];
> +		buf[color2cmd[led->color]] = brightness;
> +	}
> +
> +	state = CMD_LED_STATE_LED(led->reg);
> +	if (buf[OMNIA_CMD_LED_COLOR_R] || buf[OMNIA_CMD_LED_COLOR_G] ||
> +	    buf[OMNIA_CMD_LED_COLOR_B])
> +		state |= CMD_LED_STATE_ON;
> +
> +	ret = i2c_smbus_write_byte_data(leds->client, CMD_LED_STATE, state);
> +	if (ret >= 0 && (state & CMD_LED_STATE_ON))
> +		ret = i2c_master_send(leds->client, buf, 5);
No check on ret?
> +
> +	leds->cache[led->reg][0] = buf[OMNIA_CMD_LED_COLOR_R];
> +	leds->cache[led->reg][1] = buf[OMNIA_CMD_LED_COLOR_G];
> +	leds->cache[led->reg][2] = buf[OMNIA_CMD_LED_COLOR_B];
> +
> +	mutex_unlock(&leds->lock);
> +
> +	return ret;
> +}
> +
> +static int omnia_led_register(struct omnia_leds *leds,
> +			      struct fwnode_handle *node)
> +{
> +	struct i2c_client *client = leds->client;
> +	struct led_init_data init_data = {};
> +	struct device *dev = &client->dev;
> +	struct omnia_led *led;
> +	int ret, nsources, color;
> +	u32 led_sources[3];
> +
> +	led = &leds->leds[leds->nleds];
> +
> +	nsources = fwnode_property_count_u32(node, "led-sources");
> +	if (nsources != 1 && nsources != 3) {
> +		dev_warn(dev,
> +			 "Node %pfw: 'led-sources' must contain either 1 or 3 items!\n",
> +			 node);
> +		return 0;
> +	}
> +
> +	ret = fwnode_property_read_u32_array(node, "led-sources", led_sources,
> +					     nsources);
> +	if (ret) {
> +		dev_err(dev, "Node %pfw: 'led-sources' read failed: %i\n",
> +			node, ret);
> +		return ret;
> +	}
> +
> +	ret = fwnode_property_read_u32(node, "color", &led->color);
> +	if (ret) {
> +		dev_warn(dev, "Node %pfw: 'color' read failed!\n",
> +			 node);
> +		return 0;
> +	}
> +
> +	if (nsources == 3) {
> +		if ((led_sources[0] % 3) != 0 ||
> +		    led_sources[1] != led_sources[0] + 1 ||
> +		    led_sources[2] != led_sources[0] + 2 ||
> +		    led_sources[2] >= OMNIA_BOARD_LEDS * 3) {
> +			dev_warn(dev, "Node %pfw has invalid 'led-sources'!\n",
> +				 node);
> +			return 0;
> +		}
> +
> +		color = LED_COLOR_ID_WHITE;
> +	} else {
> +		const int led_source_to_color[3] = {
> +			LED_COLOR_ID_RED,
> +			LED_COLOR_ID_GREEN,
> +			LED_COLOR_ID_BLUE
> +		};
> +		color = led_source_to_color[led_sources[0] % 3];
> +
> +		if (led_sources[0] >= OMNIA_BOARD_LEDS * 3) {
> +			dev_warn(dev, "Node %pfw has invalid 'led-sources'!\n",
> +				 node);
> +			return 0;
> +		}
> +	}
> +
> +	if (led->color != color) {
> +		dev_warn(dev, "Node %pfw: 'color' should be %s!\n", node,
> +			 led_colors[color]);
> +		return 0;
> +	}
> +
> +	init_data.fwnode = node;
> +
> +	led->reg = led_sources[0] / 3;
> +	led->cdev.max_brightness = 255;
> +	led->cdev.brightness_set_blocking = omnia_led_brightness_set_blocking;
> +
> +	fwnode_property_read_string(node, "linux,default-trigger",
> +				    &led->cdev.default_trigger);
> +
> +	/* put the LED into software mode */
> +	ret = i2c_smbus_write_byte_data(client, CMD_LED_MODE,
> +					CMD_LED_MODE_LED(led->reg) |
> +					CMD_LED_MODE_USER);
> +	if (ret < 0) {
> +		dev_err(dev, "Cannot set LED %pfw to software mode: %i\n", node,
> +			ret);
> +		return ret;
> +	}
> +
> +	/* disable the LED */
> +	ret = i2c_smbus_write_byte_data(client, CMD_LED_STATE,
> +						CMD_LED_STATE_LED(led->reg));
> +	if (ret < 0) {
> +		dev_err(dev, "Cannot set LED %pfw brightness: %i\n", node, ret);
> +		return ret;
> +	}
> +
> +	ret = devm_led_classdev_register_ext(dev, &led->cdev, &init_data);
> +	if (ret < 0) {
> +		dev_err(dev, "Cannot register LED %pfw: %i\n", node, ret);
> +		return ret;
> +	}
> +
> +	++leds->nleds;
> +
> +	return 0;
> +}
> +
> +static int omnia_leds_probe(struct i2c_client *client,
> +			    const struct i2c_device_id *id)
> +{
> +	struct device *dev = &client->dev;
> +	struct device_node *np = dev->of_node, *child;
> +	struct omnia_leds *leds;
> +	int ret, count;
> +
> +	count = of_get_available_child_count(np);
device_get_child_node_count(&client->dev);
> +	if (!count) {
> +		dev_err(dev, "LEDs are not defined in device tree!\n");
> +		return -ENODEV;
> +	} else if (count > 3 * OMNIA_BOARD_LEDS) {
> +		dev_err(dev, "Too many LEDs defined in device tree!\n");
> +		return -EINVAL;
> +	}
> +
> +	leds = devm_kzalloc(dev, sizeof(*leds) + count * sizeof(leds->leds[0]),
> +			    GFP_KERNEL);
You can use this macro for flexible arrays struct_size(led, leds, count),
> +	if (!leds)
> +		return -ENOMEM;
> +
> +	leds->client = client;
> +	i2c_set_clientdata(client, leds);
> +
> +	mutex_init(&leds->lock);
> +
> +	for_each_available_child_of_node(np, child) {
> +		ret = omnia_led_register(leds, &child->fwnode);
> +		if (ret < 0)
if (ret) I don't see ret returning > 0
> +			return ret;
> +	}
> +
> +	return 0;
> +}
> +
> +static int omnia_leds_remove(struct i2c_client *client)
> +{
> +	u8 buf[OMNIA_CMD_LED_COLOR_LEN];
> +
> +	/* put all LEDs into default (HW triggered) mode */
> +	i2c_smbus_write_byte_data(client, CMD_LED_MODE,
> +				  CMD_LED_MODE_LED(OMNIA_BOARD_LEDS));
> +
> +	/* set all LEDs color to [255, 255, 255] */
> +	buf[OMNIA_CMD] = CMD_LED_COLOR;
> +	buf[OMNIA_CMD_LED_COLOR_LED] = OMNIA_BOARD_LEDS;
> +	buf[OMNIA_CMD_LED_COLOR_R] = 255;
> +	buf[OMNIA_CMD_LED_COLOR_G] = 255;
> +	buf[OMNIA_CMD_LED_COLOR_B] = 255;
> +
> +	i2c_master_send(client, buf, 5);
> +
> +	return 0;
> +}
> +
> +static const struct of_device_id of_omnia_leds_match[] = {
> +	{ .compatible = "cznic,turris-omnia-leds", },
> +	{},
> +};
> +
> +static const struct i2c_device_id omnia_id[] = {
> +	{ "omnia", 0 },
> +	{ }
> +};
> +
> +static struct i2c_driver omnia_leds_driver = {
> +	.probe		= omnia_leds_probe,
> +	.remove		= omnia_leds_remove,
> +	.id_table	= omnia_id,
> +	.driver		= {
> +		.name	= "leds-turris-omnia",
> +		.of_match_table = of_omnia_leds_match,
> +	},
> +};
> +
> +module_i2c_driver(omnia_leds_driver);
> +
> +MODULE_AUTHOR("Marek Behun <marek.behun@nic.cz>");
> +MODULE_DESCRIPTION("CZ.NIC's Turris Omnia LEDs");
> +MODULE_LICENSE("GPL v2");

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

* Re: [PATCH v2 1/2] dt-bindings: leds: add cznic,turris-omnia-leds binding
  2020-05-11 19:43   ` Rob Herring
@ 2020-05-11 20:01     ` Marek Behun
  2020-07-12  8:00       ` Pavel Machek
  0 siblings, 1 reply; 9+ messages in thread
From: Marek Behun @ 2020-05-11 20:01 UTC (permalink / raw)
  To: linux-leds; +Cc: Rob Herring, Pavel Machek, Jacek Anaszewski, devicetree

On Mon, 11 May 2020 14:43:54 -0500
Rob Herring <robh@kernel.org> wrote:

> On Thu, Apr 23, 2020 at 08:50:59AM +0200, Marek Behún wrote:
> > Add device-tree bindings documentation for Turris Omnia RGB LEDs.
> > 
> > ...
> >
> > +properties:
> > +  compatible:
> > +    const: cznic,turris-omnia-leds
> > +
> > +  reg:
> > +    description: I2C slave address of the microcontroller.
> > +    maxItems: 1
> > +
> > +patternProperties:
> > +  "^led[0-9][0-9]?$":  
> 
> 'led-' is preferred. And numbering is usually hex.
>
> What's the numbering here based on? Might be better to use 'reg' (and a 
> unit-address) rather than led-sources. led-sources was for cases where 
> the control interface (defined by reg) could have differing regulators 
> connected to the LEDs.

Jacek, Pavel, it seems that it would be better if I just rewrote the
driver into Dan's multicolor API. It would solve Rob's complaint and
could be merged once multicolor framework is...

Marek

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

* Re: [PATCH v2 2/2] leds: initial support for Turris Omnia LEDs
  2020-05-11 19:54   ` Dan Murphy
@ 2020-05-11 20:17     ` Marek Behun
  2020-05-11 20:21       ` Dan Murphy
  0 siblings, 1 reply; 9+ messages in thread
From: Marek Behun @ 2020-05-11 20:17 UTC (permalink / raw)
  To: Dan Murphy; +Cc: linux-leds, Pavel Machek, Jacek Anaszewski

On Mon, 11 May 2020 14:54:02 -0500
Dan Murphy <dmurphy@ti.com> wrote:

> Marek
> 
> On 4/23/20 1:51 AM, Marek Behún wrote:
> > This adds basic support for LEDs on the front side of CZ.NIC's Turris
> > Omnia router.
> >
> > There are 12 RGB LEDs. The controller supports HW triggering mode for
> > the LEDs, but this driver does not support it yet, and sets all the LEDs
> > into SW mode upon probe.
> >
> > The user can either group all three channels of one RGB LED into one LED
> > classdev, or expose each channel as an individual LED classdev. This is
> > done by utilizing the 'led-sources' and 'color' DT properties.  
> 
> I think this would be a good candidate for the multicolor framework.  It 
> would make the registration, brightness caching and color mapping easier
> 
> We are waiting on maintainer feedback for this.
> 
> https://lore.kernel.org/patchwork/project/lkml/list/?series=441958

Hi Dan, thanks for the review.

I just sent an email as response to Rob's review for dt-bindings, that
it seems it would be better to add this driver directly in multicolor
API. (I wanted to do it first without multicolor API support, because
multicolor API is taking a long time to be merged.)

> 
> But until then comments below
> 
> >
> > Signed-off-by: Marek Behún <marek.behun@nic.cz>
> > ---
> >   drivers/leds/Kconfig             |  11 ++
> >   drivers/leds/Makefile            |   1 +
> >   drivers/leds/leds-turris-omnia.c | 285 +++++++++++++++++++++++++++++++
> >   3 files changed, 297 insertions(+)
> >   create mode 100644 drivers/leds/leds-turris-omnia.c
> >
> > diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
> > index c664d84e1667..7663a5cd9fb5 100644
> > --- a/drivers/leds/Kconfig
> > +++ b/drivers/leds/Kconfig
> > @@ -145,6 +145,17 @@ config LEDS_EL15203000
> >   	  To compile this driver as a module, choose M here: the module
> >   	  will be called leds-el15203000.
> >   
> > +config LEDS_TURRIS_OMNIA
> > +	tristate "LED support for CZ.NIC's Turris Omnia"
> > +	depends on LEDS_CLASS
> > +	depends on I2C  
> REGMAP?

Isn't possible. Discussed with Jacek, see explanation at
https://www.spinics.net/lists/linux-leds/msg11583.html

> > +	depends on MACH_ARMADA_38X || COMPILE_TEST  
> Why is tied to a Marvel processor?

Because Turris Omnia is the only device containing microcontroller with
firmware that understands this protocol, since it was invented by my
predecessors in CZ.NIC, and we don't want to bloat various
distributions package systems with kernel modules that won't ever be
used on devices they target. If a distribution's kernel is compiled to
also run on Armada 38x, then the driver will be present. For other ARM
processors or other architectures this driver is useless.

> > +	depends on OF
> > +	help
> > +	  This option enables basic support for the LEDs found on the front
> > +	  side of CZ.NIC's Turris Omnia router. There are 12 RGB LEDs on the
> > +	  front panel.
> > +
> >   config LEDS_LM3530
> >   	tristate "LCD Backlight driver for LM3530"
> >   	depends on LEDS_CLASS
> > diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile
> > index 45235d5fb218..fd61421f7d40 100644
> > --- a/drivers/leds/Makefile
> > +++ b/drivers/leds/Makefile
> > @@ -83,6 +83,7 @@ obj-$(CONFIG_LEDS_TCA6507)		+= leds-tca6507.o
> >   obj-$(CONFIG_LEDS_TI_LMU_COMMON)	+= leds-ti-lmu-common.o
> >   obj-$(CONFIG_LEDS_TLC591XX)		+= leds-tlc591xx.o
> >   obj-$(CONFIG_LEDS_TPS6105X)		+= leds-tps6105x.o
> > +obj-$(CONFIG_LEDS_TURRIS_OMNIA)		+= leds-turris-omnia.o
> >   obj-$(CONFIG_LEDS_WM831X_STATUS)	+= leds-wm831x-status.o
> >   obj-$(CONFIG_LEDS_WM8350)		+= leds-wm8350.o
> >   obj-$(CONFIG_LEDS_WRAP)			+= leds-wrap.o
> > diff --git a/drivers/leds/leds-turris-omnia.c b/drivers/leds/leds-turris-omnia.c
> > new file mode 100644
> > index 000000000000..aafb4be9b225
> > --- /dev/null
> > +++ b/drivers/leds/leds-turris-omnia.c
> > @@ -0,0 +1,285 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/*
> > + * CZ.NIC's Turris Omnia LEDs driver
> > + *
> > + * 2020 by Marek Behun <marek.behun@nic.cz>
> > + */
> > +
> > +#include <linux/i2c.h>
> > +#include <linux/leds.h>
> > +#include <linux/module.h>
> > +#include <linux/mutex.h>
> > +#include <linux/of.h>
> > +#include <uapi/linux/uleds.h>
> > +#include "leds.h"
> > +
> > +#define OMNIA_BOARD_LEDS		12
> > +
> > +#define CMD_LED_MODE			3
> > +#define CMD_LED_MODE_LED(l)		((l) & 0x0f)
> > +#define CMD_LED_MODE_USER		0x10
> > +
> > +#define CMD_LED_STATE			4
> > +#define CMD_LED_STATE_LED(l)		((l) & 0x0f)
> > +#define CMD_LED_STATE_ON		0x10
> > +
> > +#define CMD_LED_COLOR			5
> > +#define CMD_LED_SET_BRIGHTNESS		7
> > +#define CMD_LED_GET_BRIGHTNESS		8
> > +
> > +#define OMNIA_CMD			0
> > +
> > +#define OMNIA_CMD_LED_COLOR_LED		1
> > +#define OMNIA_CMD_LED_COLOR_R		2
> > +#define OMNIA_CMD_LED_COLOR_G		3
> > +#define OMNIA_CMD_LED_COLOR_B		4
> > +#define OMNIA_CMD_LED_COLOR_LEN		5
> > +
> > +struct omnia_led {
> > +	struct led_classdev cdev;
> > +	int reg, color;
> > +};
> > +
> > +#define to_omnia_led(l)	container_of(l, struct omnia_led, cdev)
> > +
> > +struct omnia_leds {
> > +	struct i2c_client *client;
> > +	struct mutex lock;
> > +	u8 cache[OMNIA_BOARD_LEDS][3];
> > +	int nleds;
> > +	struct omnia_led leds[0];  
> No need for the [0] in the flexible array

Ack.

> > +};
> > +
> > +static int omnia_led_brightness_set_blocking(struct led_classdev *cdev,
> > +					     enum led_brightness brightness)
> > +{
> > +	static const u8 color2cmd[] = {
> > +		[LED_COLOR_ID_RED] = OMNIA_CMD_LED_COLOR_R,
> > +		[LED_COLOR_ID_GREEN] = OMNIA_CMD_LED_COLOR_G,
> > +		[LED_COLOR_ID_BLUE] = OMNIA_CMD_LED_COLOR_B,
> > +	};
> > +	struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent);
> > +	struct omnia_led *led = to_omnia_led(cdev);
> > +	u8 buf[OMNIA_CMD_LED_COLOR_LEN], state;
> > +	int ret;
> > +
> > +	mutex_lock(&leds->lock);
> > +
> > +	buf[OMNIA_CMD] = CMD_LED_COLOR;
> > +	buf[OMNIA_CMD_LED_COLOR_LED] = led->reg;
> > +
> > +	if (led->color == LED_COLOR_ID_WHITE) {
> > +		buf[OMNIA_CMD_LED_COLOR_R] = brightness;
> > +		buf[OMNIA_CMD_LED_COLOR_G] = brightness;
> > +		buf[OMNIA_CMD_LED_COLOR_B] = brightness;
> > +	} else {
> > +		buf[OMNIA_CMD_LED_COLOR_R] = leds->cache[led->reg][0];
> > +		buf[OMNIA_CMD_LED_COLOR_G] = leds->cache[led->reg][1];
> > +		buf[OMNIA_CMD_LED_COLOR_B] = leds->cache[led->reg][2];
> > +		buf[color2cmd[led->color]] = brightness;
> > +	}
> > +
> > +	state = CMD_LED_STATE_LED(led->reg);
> > +	if (buf[OMNIA_CMD_LED_COLOR_R] || buf[OMNIA_CMD_LED_COLOR_G] ||
> > +	    buf[OMNIA_CMD_LED_COLOR_B])
> > +		state |= CMD_LED_STATE_ON;
> > +
> > +	ret = i2c_smbus_write_byte_data(leds->client, CMD_LED_STATE, state);
> > +	if (ret >= 0 && (state & CMD_LED_STATE_ON))
> > +		ret = i2c_master_send(leds->client, buf, 5);  
> No check on ret?

The function returns ret.

> > +
> > +	leds->cache[led->reg][0] = buf[OMNIA_CMD_LED_COLOR_R];
> > +	leds->cache[led->reg][1] = buf[OMNIA_CMD_LED_COLOR_G];
> > +	leds->cache[led->reg][2] = buf[OMNIA_CMD_LED_COLOR_B];
> > +
> > +	mutex_unlock(&leds->lock);
> > +
> > +	return ret;
> > +}
> > +
> > +static int omnia_led_register(struct omnia_leds *leds,
> > +			      struct fwnode_handle *node)
> > +{
> > +	struct i2c_client *client = leds->client;
> > +	struct led_init_data init_data = {};
> > +	struct device *dev = &client->dev;
> > +	struct omnia_led *led;
> > +	int ret, nsources, color;
> > +	u32 led_sources[3];
> > +
> > +	led = &leds->leds[leds->nleds];
> > +
> > +	nsources = fwnode_property_count_u32(node, "led-sources");
> > +	if (nsources != 1 && nsources != 3) {
> > +		dev_warn(dev,
> > +			 "Node %pfw: 'led-sources' must contain either 1 or 3 items!\n",
> > +			 node);
> > +		return 0;
> > +	}
> > +
> > +	ret = fwnode_property_read_u32_array(node, "led-sources", led_sources,
> > +					     nsources);
> > +	if (ret) {
> > +		dev_err(dev, "Node %pfw: 'led-sources' read failed: %i\n",
> > +			node, ret);
> > +		return ret;
> > +	}
> > +
> > +	ret = fwnode_property_read_u32(node, "color", &led->color);
> > +	if (ret) {
> > +		dev_warn(dev, "Node %pfw: 'color' read failed!\n",
> > +			 node);
> > +		return 0;
> > +	}
> > +
> > +	if (nsources == 3) {
> > +		if ((led_sources[0] % 3) != 0 ||
> > +		    led_sources[1] != led_sources[0] + 1 ||
> > +		    led_sources[2] != led_sources[0] + 2 ||
> > +		    led_sources[2] >= OMNIA_BOARD_LEDS * 3) {
> > +			dev_warn(dev, "Node %pfw has invalid 'led-sources'!\n",
> > +				 node);
> > +			return 0;
> > +		}
> > +
> > +		color = LED_COLOR_ID_WHITE;
> > +	} else {
> > +		const int led_source_to_color[3] = {
> > +			LED_COLOR_ID_RED,
> > +			LED_COLOR_ID_GREEN,
> > +			LED_COLOR_ID_BLUE
> > +		};
> > +		color = led_source_to_color[led_sources[0] % 3];
> > +
> > +		if (led_sources[0] >= OMNIA_BOARD_LEDS * 3) {
> > +			dev_warn(dev, "Node %pfw has invalid 'led-sources'!\n",
> > +				 node);
> > +			return 0;
> > +		}
> > +	}
> > +
> > +	if (led->color != color) {
> > +		dev_warn(dev, "Node %pfw: 'color' should be %s!\n", node,
> > +			 led_colors[color]);
> > +		return 0;
> > +	}
> > +
> > +	init_data.fwnode = node;
> > +
> > +	led->reg = led_sources[0] / 3;
> > +	led->cdev.max_brightness = 255;
> > +	led->cdev.brightness_set_blocking = omnia_led_brightness_set_blocking;
> > +
> > +	fwnode_property_read_string(node, "linux,default-trigger",
> > +				    &led->cdev.default_trigger);
> > +
> > +	/* put the LED into software mode */
> > +	ret = i2c_smbus_write_byte_data(client, CMD_LED_MODE,
> > +					CMD_LED_MODE_LED(led->reg) |
> > +					CMD_LED_MODE_USER);
> > +	if (ret < 0) {
> > +		dev_err(dev, "Cannot set LED %pfw to software mode: %i\n", node,
> > +			ret);
> > +		return ret;
> > +	}
> > +
> > +	/* disable the LED */
> > +	ret = i2c_smbus_write_byte_data(client, CMD_LED_STATE,
> > +						CMD_LED_STATE_LED(led->reg));
> > +	if (ret < 0) {
> > +		dev_err(dev, "Cannot set LED %pfw brightness: %i\n", node, ret);
> > +		return ret;
> > +	}
> > +
> > +	ret = devm_led_classdev_register_ext(dev, &led->cdev, &init_data);
> > +	if (ret < 0) {
> > +		dev_err(dev, "Cannot register LED %pfw: %i\n", node, ret);
> > +		return ret;
> > +	}
> > +
> > +	++leds->nleds;
> > +
> > +	return 0;
> > +}
> > +
> > +static int omnia_leds_probe(struct i2c_client *client,
> > +			    const struct i2c_device_id *id)
> > +{
> > +	struct device *dev = &client->dev;
> > +	struct device_node *np = dev->of_node, *child;
> > +	struct omnia_leds *leds;
> > +	int ret, count;
> > +
> > +	count = of_get_available_child_count(np);  
> device_get_child_node_count(&client->dev);

No problem with that, but why? This driver cannot be used without OF
anyway.

> > +	if (!count) {
> > +		dev_err(dev, "LEDs are not defined in device tree!\n");
> > +		return -ENODEV;
> > +	} else if (count > 3 * OMNIA_BOARD_LEDS) {
> > +		dev_err(dev, "Too many LEDs defined in device tree!\n");
> > +		return -EINVAL;
> > +	}
> > +
> > +	leds = devm_kzalloc(dev, sizeof(*leds) + count * sizeof(leds->leds[0]),
> > +			    GFP_KERNEL);  
> You can use this macro for flexible arrays struct_size(led, leds, count),

Ack, thx.

> > +	if (!leds)
> > +		return -ENOMEM;
> > +
> > +	leds->client = client;
> > +	i2c_set_clientdata(client, leds);
> > +
> > +	mutex_init(&leds->lock);
> > +
> > +	for_each_available_child_of_node(np, child) {
> > +		ret = omnia_led_register(leds, &child->fwnode);
> > +		if (ret < 0)  
> if (ret) I don't see ret returning > 0

Ack.

> > +			return ret;
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int omnia_leds_remove(struct i2c_client *client)
> > +{
> > +	u8 buf[OMNIA_CMD_LED_COLOR_LEN];
> > +
> > +	/* put all LEDs into default (HW triggered) mode */
> > +	i2c_smbus_write_byte_data(client, CMD_LED_MODE,
> > +				  CMD_LED_MODE_LED(OMNIA_BOARD_LEDS));
> > +
> > +	/* set all LEDs color to [255, 255, 255] */
> > +	buf[OMNIA_CMD] = CMD_LED_COLOR;
> > +	buf[OMNIA_CMD_LED_COLOR_LED] = OMNIA_BOARD_LEDS;
> > +	buf[OMNIA_CMD_LED_COLOR_R] = 255;
> > +	buf[OMNIA_CMD_LED_COLOR_G] = 255;
> > +	buf[OMNIA_CMD_LED_COLOR_B] = 255;
> > +
> > +	i2c_master_send(client, buf, 5);
> > +
> > +	return 0;
> > +}
> > +
> > +static const struct of_device_id of_omnia_leds_match[] = {
> > +	{ .compatible = "cznic,turris-omnia-leds", },
> > +	{},
> > +};
> > +
> > +static const struct i2c_device_id omnia_id[] = {
> > +	{ "omnia", 0 },
> > +	{ }
> > +};
> > +
> > +static struct i2c_driver omnia_leds_driver = {
> > +	.probe		= omnia_leds_probe,
> > +	.remove		= omnia_leds_remove,
> > +	.id_table	= omnia_id,
> > +	.driver		= {
> > +		.name	= "leds-turris-omnia",
> > +		.of_match_table = of_omnia_leds_match,
> > +	},
> > +};
> > +
> > +module_i2c_driver(omnia_leds_driver);
> > +
> > +MODULE_AUTHOR("Marek Behun <marek.behun@nic.cz>");
> > +MODULE_DESCRIPTION("CZ.NIC's Turris Omnia LEDs");
> > +MODULE_LICENSE("GPL v2");  


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

* Re: [PATCH v2 2/2] leds: initial support for Turris Omnia LEDs
  2020-05-11 20:17     ` Marek Behun
@ 2020-05-11 20:21       ` Dan Murphy
  0 siblings, 0 replies; 9+ messages in thread
From: Dan Murphy @ 2020-05-11 20:21 UTC (permalink / raw)
  To: Marek Behun; +Cc: linux-leds, Pavel Machek, Jacek Anaszewski

Marek

On 5/11/20 3:17 PM, Marek Behun wrote:
> On Mon, 11 May 2020 14:54:02 -0500
> Dan Murphy <dmurphy@ti.com> wrote:
>
>> Marek
>>
>> On 4/23/20 1:51 AM, Marek Behún wrote:
>>> This adds basic support for LEDs on the front side of CZ.NIC's Turris
>>> Omnia router.
>>>
>>> There are 12 RGB LEDs. The controller supports HW triggering mode for
>>> the LEDs, but this driver does not support it yet, and sets all the LEDs
>>> into SW mode upon probe.
>>>
>>> The user can either group all three channels of one RGB LED into one LED
>>> classdev, or expose each channel as an individual LED classdev. This is
>>> done by utilizing the 'led-sources' and 'color' DT properties.
>> I think this would be a good candidate for the multicolor framework.  It
>> would make the registration, brightness caching and color mapping easier
>>
>> We are waiting on maintainer feedback for this.
>>
>> https://lore.kernel.org/patchwork/project/lkml/list/?series=441958
> Hi Dan, thanks for the review.
>
> I just sent an email as response to Rob's review for dt-bindings, that
> it seems it would be better to add this driver directly in multicolor
> API. (I wanted to do it first without multicolor API support, because
> multicolor API is taking a long time to be merged.)

Ack to that.

Also some additional coding comments below


>
>> But until then comments below
>>
>>> Signed-off-by: Marek Behún <marek.behun@nic.cz>
>>> ---
>>>    drivers/leds/Kconfig             |  11 ++
>>>    drivers/leds/Makefile            |   1 +
>>>    drivers/leds/leds-turris-omnia.c | 285 +++++++++++++++++++++++++++++++
>>>    3 files changed, 297 insertions(+)
>>>    create mode 100644 drivers/leds/leds-turris-omnia.c
>>>
>>> diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
>>> index c664d84e1667..7663a5cd9fb5 100644
>>> --- a/drivers/leds/Kconfig
>>> +++ b/drivers/leds/Kconfig
>>> @@ -145,6 +145,17 @@ config LEDS_EL15203000
>>>    	  To compile this driver as a module, choose M here: the module
>>>    	  will be called leds-el15203000.
>>>    
>>> +config LEDS_TURRIS_OMNIA
>>> +	tristate "LED support for CZ.NIC's Turris Omnia"
>>> +	depends on LEDS_CLASS
>>> +	depends on I2C
>> REGMAP?
> Isn't possible. Discussed with Jacek, see explanation at
> https://www.spinics.net/lists/linux-leds/msg11583.html
Ack
>
>>> +	depends on MACH_ARMADA_38X || COMPILE_TEST
>> Why is tied to a Marvel processor?
> Because Turris Omnia is the only device containing microcontroller with
> firmware that understands this protocol, since it was invented by my
> predecessors in CZ.NIC, and we don't want to bloat various
> distributions package systems with kernel modules that won't ever be
> used on devices they target. If a distribution's kernel is compiled to
> also run on Armada 38x, then the driver will be present. For other ARM
> processors or other architectures this driver is useless.


OK

>
>>> +	depends on OF
>>> +	help
>>> +	  This option enables basic support for the LEDs found on the front
>>> +	  side of CZ.NIC's Turris Omnia router. There are 12 RGB LEDs on the
>>> +	  front panel.
>>> +
>>>    config LEDS_LM3530
>>>    	tristate "LCD Backlight driver for LM3530"
>>>    	depends on LEDS_CLASS
>>> diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile
>>> index 45235d5fb218..fd61421f7d40 100644
>>> --- a/drivers/leds/Makefile
>>> +++ b/drivers/leds/Makefile
>>> @@ -83,6 +83,7 @@ obj-$(CONFIG_LEDS_TCA6507)		+= leds-tca6507.o
>>>    obj-$(CONFIG_LEDS_TI_LMU_COMMON)	+= leds-ti-lmu-common.o
>>>    obj-$(CONFIG_LEDS_TLC591XX)		+= leds-tlc591xx.o
>>>    obj-$(CONFIG_LEDS_TPS6105X)		+= leds-tps6105x.o
>>> +obj-$(CONFIG_LEDS_TURRIS_OMNIA)		+= leds-turris-omnia.o
>>>    obj-$(CONFIG_LEDS_WM831X_STATUS)	+= leds-wm831x-status.o
>>>    obj-$(CONFIG_LEDS_WM8350)		+= leds-wm8350.o
>>>    obj-$(CONFIG_LEDS_WRAP)			+= leds-wrap.o
>>> diff --git a/drivers/leds/leds-turris-omnia.c b/drivers/leds/leds-turris-omnia.c
>>> new file mode 100644
>>> index 000000000000..aafb4be9b225
>>> --- /dev/null
>>> +++ b/drivers/leds/leds-turris-omnia.c
>>> @@ -0,0 +1,285 @@
>>> +// SPDX-License-Identifier: GPL-2.0
>>> +/*
>>> + * CZ.NIC's Turris Omnia LEDs driver
>>> + *
>>> + * 2020 by Marek Behun <marek.behun@nic.cz>
>>> + */
>>> +
>>> +#include <linux/i2c.h>
>>> +#include <linux/leds.h>
>>> +#include <linux/module.h>
>>> +#include <linux/mutex.h>
>>> +#include <linux/of.h>
>>> +#include <uapi/linux/uleds.h>


Whats the need for this header?


>>> +#include "leds.h"
>>> +
>>> +#define OMNIA_BOARD_LEDS		12
>>> +
>>> +#define CMD_LED_MODE			3
>>> +#define CMD_LED_MODE_LED(l)		((l) & 0x0f)
>>> +#define CMD_LED_MODE_USER		0x10
>>> +
>>> +#define CMD_LED_STATE			4
>>> +#define CMD_LED_STATE_LED(l)		((l) & 0x0f)
>>> +#define CMD_LED_STATE_ON		0x10
>>> +
>>> +#define CMD_LED_COLOR			5
>>> +#define CMD_LED_SET_BRIGHTNESS		7
>>> +#define CMD_LED_GET_BRIGHTNESS		8
>>> +
>>> +#define OMNIA_CMD			0
>>> +
>>> +#define OMNIA_CMD_LED_COLOR_LED		1
>>> +#define OMNIA_CMD_LED_COLOR_R		2
>>> +#define OMNIA_CMD_LED_COLOR_G		3
>>> +#define OMNIA_CMD_LED_COLOR_B		4
>>> +#define OMNIA_CMD_LED_COLOR_LEN		5
>>> +
>>> +struct omnia_led {
>>> +	struct led_classdev cdev;
>>> +	int reg, color;
>>> +};
>>> +
>>> +#define to_omnia_led(l)	container_of(l, struct omnia_led, cdev)
>>> +
>>> +struct omnia_leds {
>>> +	struct i2c_client *client;
>>> +	struct mutex lock;
>>> +	u8 cache[OMNIA_BOARD_LEDS][3];
>>> +	int nleds;
>>> +	struct omnia_led leds[0];
>> No need for the [0] in the flexible array
> Ack.
>
>>> +};
>>> +
>>> +static int omnia_led_brightness_set_blocking(struct led_classdev *cdev,
>>> +					     enum led_brightness brightness)
>>> +{
>>> +	static const u8 color2cmd[] = {
>>> +		[LED_COLOR_ID_RED] = OMNIA_CMD_LED_COLOR_R,
>>> +		[LED_COLOR_ID_GREEN] = OMNIA_CMD_LED_COLOR_G,
>>> +		[LED_COLOR_ID_BLUE] = OMNIA_CMD_LED_COLOR_B,
>>> +	};
>>> +	struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent);
>>> +	struct omnia_led *led = to_omnia_led(cdev);
>>> +	u8 buf[OMNIA_CMD_LED_COLOR_LEN], state;
>>> +	int ret;
>>> +
>>> +	mutex_lock(&leds->lock);
>>> +
>>> +	buf[OMNIA_CMD] = CMD_LED_COLOR;
>>> +	buf[OMNIA_CMD_LED_COLOR_LED] = led->reg;
>>> +
>>> +	if (led->color == LED_COLOR_ID_WHITE) {
>>> +		buf[OMNIA_CMD_LED_COLOR_R] = brightness;
>>> +		buf[OMNIA_CMD_LED_COLOR_G] = brightness;
>>> +		buf[OMNIA_CMD_LED_COLOR_B] = brightness;
>>> +	} else {
>>> +		buf[OMNIA_CMD_LED_COLOR_R] = leds->cache[led->reg][0];
>>> +		buf[OMNIA_CMD_LED_COLOR_G] = leds->cache[led->reg][1];
>>> +		buf[OMNIA_CMD_LED_COLOR_B] = leds->cache[led->reg][2];
>>> +		buf[color2cmd[led->color]] = brightness;
>>> +	}
>>> +
>>> +	state = CMD_LED_STATE_LED(led->reg);
>>> +	if (buf[OMNIA_CMD_LED_COLOR_R] || buf[OMNIA_CMD_LED_COLOR_G] ||
>>> +	    buf[OMNIA_CMD_LED_COLOR_B])
>>> +		state |= CMD_LED_STATE_ON;
>>> +
>>> +	ret = i2c_smbus_write_byte_data(leds->client, CMD_LED_STATE, state);
>>> +	if (ret >= 0 && (state & CMD_LED_STATE_ON))
>>> +		ret = i2c_master_send(leds->client, buf, 5);
>> No check on ret?
> The function returns ret.

But you are setting the cache values whether it passes or fails.

If it fails then the cache values and device values will not match.


>
>>> +
>>> +	leds->cache[led->reg][0] = buf[OMNIA_CMD_LED_COLOR_R];
>>> +	leds->cache[led->reg][1] = buf[OMNIA_CMD_LED_COLOR_G];
>>> +	leds->cache[led->reg][2] = buf[OMNIA_CMD_LED_COLOR_B];
>>> +
>>> +	mutex_unlock(&leds->lock);
>>> +
>>> +	return ret;
>>> +}
>>> +
>>> +static int omnia_led_register(struct omnia_leds *leds,
>>> +			      struct fwnode_handle *node)
>>> +{
>>> +	struct i2c_client *client = leds->client;
>>> +	struct led_init_data init_data = {};
>>> +	struct device *dev = &client->dev;
>>> +	struct omnia_led *led;
>>> +	int ret, nsources, color;
>>> +	u32 led_sources[3];
>>> +
>>> +	led = &leds->leds[leds->nleds];
>>> +
>>> +	nsources = fwnode_property_count_u32(node, "led-sources");
>>> +	if (nsources != 1 && nsources != 3) {
>>> +		dev_warn(dev,
>>> +			 "Node %pfw: 'led-sources' must contain either 1 or 3 items!\n",
>>> +			 node);
>>> +		return 0;
>>> +	}
>>> +
>>> +	ret = fwnode_property_read_u32_array(node, "led-sources", led_sources,
>>> +					     nsources);
>>> +	if (ret) {
>>> +		dev_err(dev, "Node %pfw: 'led-sources' read failed: %i\n",
>>> +			node, ret);
>>> +		return ret;
>>> +	}
>>> +
>>> +	ret = fwnode_property_read_u32(node, "color", &led->color);
>>> +	if (ret) {
>>> +		dev_warn(dev, "Node %pfw: 'color' read failed!\n",
>>> +			 node);
>>> +		return 0;
>>> +	}
>>> +
>>> +	if (nsources == 3) {
>>> +		if ((led_sources[0] % 3) != 0 ||
>>> +		    led_sources[1] != led_sources[0] + 1 ||
>>> +		    led_sources[2] != led_sources[0] + 2 ||
>>> +		    led_sources[2] >= OMNIA_BOARD_LEDS * 3) {
>>> +			dev_warn(dev, "Node %pfw has invalid 'led-sources'!\n",
>>> +				 node);
>>> +			return 0;
>>> +		}
>>> +
>>> +		color = LED_COLOR_ID_WHITE;
>>> +	} else {
>>> +		const int led_source_to_color[3] = {
>>> +			LED_COLOR_ID_RED,
>>> +			LED_COLOR_ID_GREEN,
>>> +			LED_COLOR_ID_BLUE
>>> +		};
>>> +		color = led_source_to_color[led_sources[0] % 3];
>>> +
>>> +		if (led_sources[0] >= OMNIA_BOARD_LEDS * 3) {
>>> +			dev_warn(dev, "Node %pfw has invalid 'led-sources'!\n",
>>> +				 node);
>>> +			return 0;
>>> +		}
>>> +	}
>>> +
>>> +	if (led->color != color) {
>>> +		dev_warn(dev, "Node %pfw: 'color' should be %s!\n", node,
>>> +			 led_colors[color]);
>>> +		return 0;
>>> +	}
>>> +
>>> +	init_data.fwnode = node;
>>> +
>>> +	led->reg = led_sources[0] / 3;
>>> +	led->cdev.max_brightness = 255;
>>> +	led->cdev.brightness_set_blocking = omnia_led_brightness_set_blocking;
>>> +
>>> +	fwnode_property_read_string(node, "linux,default-trigger",
>>> +				    &led->cdev.default_trigger);
>>> +
>>> +	/* put the LED into software mode */
>>> +	ret = i2c_smbus_write_byte_data(client, CMD_LED_MODE,
>>> +					CMD_LED_MODE_LED(led->reg) |
>>> +					CMD_LED_MODE_USER);
>>> +	if (ret < 0) {
>>> +		dev_err(dev, "Cannot set LED %pfw to software mode: %i\n", node,
>>> +			ret);
>>> +		return ret;
>>> +	}
>>> +
>>> +	/* disable the LED */
>>> +	ret = i2c_smbus_write_byte_data(client, CMD_LED_STATE,
>>> +						CMD_LED_STATE_LED(led->reg));
>>> +	if (ret < 0) {
>>> +		dev_err(dev, "Cannot set LED %pfw brightness: %i\n", node, ret);
>>> +		return ret;
>>> +	}
>>> +
>>> +	ret = devm_led_classdev_register_ext(dev, &led->cdev, &init_data);
>>> +	if (ret < 0) {
>>> +		dev_err(dev, "Cannot register LED %pfw: %i\n", node, ret);
>>> +		return ret;
>>> +	}
>>> +
>>> +	++leds->nleds;
>>> +
>>> +	return 0;
>>> +}
>>> +
>>> +static int omnia_leds_probe(struct i2c_client *client,
>>> +			    const struct i2c_device_id *id)
>>> +{
>>> +	struct device *dev = &client->dev;
>>> +	struct device_node *np = dev->of_node, *child;
>>> +	struct omnia_leds *leds;
>>> +	int ret, count;
>>> +
>>> +	count = of_get_available_child_count(np);
>> device_get_child_node_count(&client->dev);
> No problem with that, but why? This driver cannot be used without OF
> anyway.
>
Consistency.  You use fwnode calls everywhere else why not here?



>>> +	if (!count) {
>>> +		dev_err(dev, "LEDs are not defined in device tree!\n");
>>> +		return -ENODEV;
>>> +	} else if (count > 3 * OMNIA_BOARD_LEDS) {
>>> +		dev_err(dev, "Too many LEDs defined in device tree!\n");
>>> +		return -EINVAL;
>>> +	}
>>> +
>>> +	leds = devm_kzalloc(dev, sizeof(*leds) + count * sizeof(leds->leds[0]),
>>> +			    GFP_KERNEL);
>> You can use this macro for flexible arrays struct_size(led, leds, count),
> Ack, thx.
>
>>> +	if (!leds)
>>> +		return -ENOMEM;
>>> +
>>> +	leds->client = client;
>>> +	i2c_set_clientdata(client, leds);
>>> +
>>> +	mutex_init(&leds->lock);
>>> +
>>> +	for_each_available_child_of_node(np, child) {
>>> +		ret = omnia_led_register(leds, &child->fwnode);
>>> +		if (ret < 0)
>> if (ret) I don't see ret returning > 0
> Ack.
>
>>> +			return ret;
>>> +	}
>>> +
>>> +	return 0;
>>> +}
>>> +
>>> +static int omnia_leds_remove(struct i2c_client *client)
>>> +{
>>> +	u8 buf[OMNIA_CMD_LED_COLOR_LEN];
>>> +
>>> +	/* put all LEDs into default (HW triggered) mode */
>>> +	i2c_smbus_write_byte_data(client, CMD_LED_MODE,
>>> +				  CMD_LED_MODE_LED(OMNIA_BOARD_LEDS));
>>> +
>>> +	/* set all LEDs color to [255, 255, 255] */
>>> +	buf[OMNIA_CMD] = CMD_LED_COLOR;
>>> +	buf[OMNIA_CMD_LED_COLOR_LED] = OMNIA_BOARD_LEDS;
>>> +	buf[OMNIA_CMD_LED_COLOR_R] = 255;
>>> +	buf[OMNIA_CMD_LED_COLOR_G] = 255;
>>> +	buf[OMNIA_CMD_LED_COLOR_B] = 255;
>>> +
>>> +	i2c_master_send(client, buf, 5);

Another comment why not just return the value of the I2C send?


>>> +
>>> +	return 0;
>>> +}
>>> +
>>> +static const struct of_device_id of_omnia_leds_match[] = {
>>> +	{ .compatible = "cznic,turris-omnia-leds", },
>>> +	{},
>>> +};
>>> +
>>> +static const struct i2c_device_id omnia_id[] = {
>>> +	{ "omnia", 0 },
>>> +	{ }
>>> +};
>>> +
>>> +static struct i2c_driver omnia_leds_driver = {
>>> +	.probe		= omnia_leds_probe,
>>> +	.remove		= omnia_leds_remove,
>>> +	.id_table	= omnia_id,
>>> +	.driver		= {
>>> +		.name	= "leds-turris-omnia",
>>> +		.of_match_table = of_omnia_leds_match,
>>> +	},
>>> +};
>>> +
>>> +module_i2c_driver(omnia_leds_driver);
>>> +
>>> +MODULE_AUTHOR("Marek Behun <marek.behun@nic.cz>");
>>> +MODULE_DESCRIPTION("CZ.NIC's Turris Omnia LEDs");
>>> +MODULE_LICENSE("GPL v2");

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

* Re: [PATCH v2 1/2] dt-bindings: leds: add cznic,turris-omnia-leds binding
  2020-05-11 20:01     ` Marek Behun
@ 2020-07-12  8:00       ` Pavel Machek
  0 siblings, 0 replies; 9+ messages in thread
From: Pavel Machek @ 2020-07-12  8:00 UTC (permalink / raw)
  To: Marek Behun; +Cc: linux-leds, Rob Herring, Jacek Anaszewski, devicetree


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

Hi!

> > What's the numbering here based on? Might be better to use 'reg' (and a 
> > unit-address) rather than led-sources. led-sources was for cases where 
> > the control interface (defined by reg) could have differing regulators 
> > connected to the LEDs.
> 
> Jacek, Pavel, it seems that it would be better if I just rewrote the
> driver into Dan's multicolor API. It would solve Rob's complaint and
> could be merged once multicolor framework is...

It would be great to try that now. Multicolor is now very close to
being merged, and having independend verification that it works would
be great.

Thank you,
									Pavel
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 181 bytes --]

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

end of thread, back to index

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-23  6:50 [PATCH v2 0/2] Add Turris Omnia LEDs driver Marek Behún
2020-04-23  6:50 ` [PATCH v2 1/2] dt-bindings: leds: add cznic,turris-omnia-leds binding Marek Behún
2020-05-11 19:43   ` Rob Herring
2020-05-11 20:01     ` Marek Behun
2020-07-12  8:00       ` Pavel Machek
2020-04-23  6:51 ` [PATCH v2 2/2] leds: initial support for Turris Omnia LEDs Marek Behún
2020-05-11 19:54   ` Dan Murphy
2020-05-11 20:17     ` Marek Behun
2020-05-11 20:21       ` Dan Murphy

Linux-LEDs Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-leds/0 linux-leds/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-leds linux-leds/ https://lore.kernel.org/linux-leds \
		linux-leds@vger.kernel.org
	public-inbox-index linux-leds

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-leds


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git