[PATCH v2 leds-next 1/3] leds: Add basic support for Turris Omnia LEDs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

Also setting colors is not supported, only on/off state.

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

diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
index a72f97fca57b..adcd53c02b3c 100644
--- a/drivers/leds/Kconfig
+++ b/drivers/leds/Kconfig
@@ -128,6 +128,18 @@ config LEDS_CR0014114
 	  To compile this driver as a module, choose M here: the module
 	  will be called leds-cr0014114.
 
+config LEDS_TURRIS_OMNIA
+	tristate "LED support for CZ.NIC's Turris Omnia"
+	depends on LEDS_CLASS
+	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.
+	  This driver does not currently support setting LED colors, only
+	  on/off state. Also HW triggering is disabled when the controller
+	  is probed by this driver.
+
 config LEDS_LM3530
 	tristate "LCD Backlight driver for LM3530"
 	depends on LEDS_CLASS
diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile
index 4c1b0054f379..0e55579dc4f9 100644
--- a/drivers/leds/Makefile
+++ b/drivers/leds/Makefile
@@ -79,6 +79,7 @@ obj-$(CONFIG_LEDS_MT6323)		+= leds-mt6323.o
 obj-$(CONFIG_LEDS_LM3692X)		+= leds-lm3692x.o
 obj-$(CONFIG_LEDS_SC27XX_BLTC)		+= leds-sc27xx-bltc.o
 obj-$(CONFIG_LEDS_LM3601X)		+= leds-lm3601x.o
+obj-$(CONFIG_LEDS_TURRIS_OMNIA)		+= leds-turris-omnia.o
 
 # LED SPI Drivers
 obj-$(CONFIG_LEDS_CR0014114)		+= leds-cr0014114.o
diff --git a/drivers/leds/leds-turris-omnia.c b/drivers/leds/leds-turris-omnia.c
new file mode 100644
index 000000000000..9b96a2da4e38
--- /dev/null
+++ b/drivers/leds/leds-turris-omnia.c
@@ -0,0 +1,178 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+// CZ.NIC's Turris Omnia LEDs driver
+//
+// 2019 by Marek Behun <marek.behun@xxxxxx>
+
+#include <linux/i2c.h>
+#include <linux/leds.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of.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
+
+struct omnia_leds {
+	struct i2c_client *client;
+	struct mutex lock;
+	struct led_classdev leds[OMNIA_BOARD_LEDS];
+};
+
+static int omnia_led_idx(struct omnia_leds *leds, struct led_classdev *led)
+{
+	int idx = led - &leds->leds[0];
+
+	if (idx < 0 || idx >= OMNIA_BOARD_LEDS)
+		return -ENXIO;
+
+	return idx;
+}
+
+static int omnia_led_brightness_set_blocking(struct led_classdev *led,
+					     enum led_brightness brightness)
+{
+	struct omnia_leds *leds = dev_get_drvdata(led->dev->parent);
+	int idx = omnia_led_idx(leds, led);
+	int ret;
+	u8 state;
+
+	if (idx < 0)
+		return idx;
+
+	state = CMD_LED_STATE_LED(idx);
+	if (brightness)
+		state |= CMD_LED_STATE_ON;
+
+	mutex_lock(&leds->lock);
+	ret = i2c_smbus_write_byte_data(leds->client, CMD_LED_STATE, state);
+	mutex_unlock(&leds->lock);
+
+	return ret;
+}
+
+static int omnia_led_register(struct omnia_leds *leds, struct device_node *np)
+{
+	struct i2c_client *client = leds->client;
+	struct device *dev = &client->dev;
+	int ret;
+	u32 reg;
+
+	ret = of_property_read_u32(np, "reg", &reg);
+	if (ret) {
+		dev_err(dev, "Failed to read LED 'reg' property: %i\n", ret);
+		return ret;
+	}
+
+	if (reg >= OMNIA_BOARD_LEDS) {
+		dev_warn(dev, "Invalid Turris Omnia LED index %u\n", reg);
+		return 0;
+	}
+
+	leds->leds[reg].brightness = 0;
+	leds->leds[reg].brightness_set_blocking =
+		omnia_led_brightness_set_blocking;
+
+	leds->leds[reg].name = of_get_property(np, "label", NULL) ? : np->name;
+
+	leds->leds[reg].default_trigger = of_get_property(np,
+		"linux,default-trigger", NULL);
+
+	/* put the LED into software mode */
+	ret = i2c_smbus_write_byte_data(client, CMD_LED_MODE,
+					CMD_LED_MODE_LED(reg) |
+					CMD_LED_MODE_USER);
+	if (ret < 0) {
+		dev_err(dev, "Cannot set LED %i to software mode: %i\n", reg,
+			ret);
+		return ret;
+	}
+
+	/* disable the LED */
+	ret = i2c_smbus_write_byte_data(client, CMD_LED_STATE,
+						CMD_LED_STATE_LED(reg));
+	if (ret < 0) {
+		dev_err(dev, "Cannot set LED %i brightness: %i\n", reg, ret);
+		return ret;
+	}
+
+	ret = devm_led_classdev_register(dev, &leds->leds[reg]);
+	if (ret < 0) {
+		dev_err(dev, "Cannot register LED %i: %i\n", reg, ret);
+		return ret;
+	}
+
+	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 *child;
+	struct omnia_leds *leds;
+	int ret;
+
+	leds = devm_kzalloc(dev, sizeof(*leds), GFP_KERNEL);
+	if (!leds)
+		return -ENOMEM;
+
+	leds->client = client;
+	i2c_set_clientdata(client, leds);
+
+	mutex_init(&leds->lock);
+
+	for_each_available_child_of_node(dev->of_node, child) {
+		ret = omnia_led_register(leds, child);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+
+static int omnia_leds_remove(struct i2c_client *client)
+{
+	/* put all LEDs into default (HW triggered) mode */
+	i2c_smbus_write_byte_data(client, CMD_LED_MODE,
+				  CMD_LED_MODE_LED(OMNIA_BOARD_LEDS));
+
+	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@xxxxxx>");
+MODULE_DESCRIPTION("CZ.NIC's Turris Omnia LEDs");
+MODULE_LICENSE("GPL v2");
-- 
2.19.2




[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux