Skip to contentSkip to conversation

cap11xx: add LED support

PATCH Superseded Matt Ranostay 3 participants 7 messages Jun 9, 2015

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cap11xx: add LED support

Several cap11xx variants have LEDs that be can be controlled, this patchset implements this functionality.

---
 drivers/input/keyboard/cap11xx.c | 102 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 99 insertions(+), 3 deletions(-)
drivers/input/keyboard/cap11xx.c +99 -3
index f07461a..dc48936 100644
@@ -12,6 +12,7 @@
 #include <linux/module.h>
 #include <linux/interrupt.h>
 #include <linux/input.h>
+#include <linux/leds.h>
 #include <linux/of_irq.h>
 #include <linux/regmap.h>
 #include <linux/i2c.h>
@@ -47,6 +48,8 @@
 #define CAP11XX_REG_CONFIG2		0x44
 #define CAP11XX_REG_CONFIG2_ALT_POL	BIT(6)
 #define CAP11XX_REG_SENSOR_BASE_CNT(X)	(0x50 + (X))
+#define CAP11XX_REG_LED_POLARITY	0x73
+#define CAP11XX_REG_LED_OUTPUT_CONTROL	0x74
 #define CAP11XX_REG_SENSOR_CALIB	(0xb1 + (X))
 #define CAP11XX_REG_SENSOR_CALIB_LSB1	0xb9
 #define CAP11XX_REG_SENSOR_CALIB_LSB2	0xba
@@ -56,10 +59,25 @@
 
 #define CAP11XX_MANUFACTURER_ID	0x5d
 
+#ifdef CONFIG_LEDS_CLASS
+struct cap11xx_led {
+	struct cap11xx_priv *priv;
+	struct led_classdev cdev;
+	struct work_struct work;
+	char name[32];
+	int id;
+	enum led_brightness new_brightness;
+};
+#endif
+
 struct cap11xx_priv {
 	struct regmap *regmap;
 	struct input_dev *idev;
 
+#ifdef CONFIG_LEDS_CLASS
+	struct cap11xx_led *leds;
+	int num_leds;
+#endif
 	/* config */
 	u32 keycodes[];
 };
@@ -67,6 +85,7 @@ struct cap11xx_priv {
 struct cap11xx_hw_model {
 	u8 product_id;
 	unsigned int num_channels;
+	unsigned int num_leds;
 };
 
 enum {
@@ -76,9 +95,9 @@ enum {
 };
 
 static const struct cap11xx_hw_model cap11xx_devices[] = {
-	[CAP1106] = { .product_id = 0x55, .num_channels = 6 },
-	[CAP1126] = { .product_id = 0x53, .num_channels = 6 },
-	[CAP1188] = { .product_id = 0x50, .num_channels = 8 },
+	[CAP1106] = { .product_id = 0x55, .num_channels = 6, .num_leds = 0 },
+	[CAP1126] = { .product_id = 0x53, .num_channels = 6, .num_leds = 2 },
+	[CAP1188] = { .product_id = 0x50, .num_channels = 8, .num_leds = 8 },
 };
 
 static const struct reg_default cap11xx_reg_defaults[] = {
@@ -111,6 +130,8 @@ static const struct reg_default cap11xx_reg_defaults[] = {
 	{ CAP11XX_REG_STANDBY_SENSITIVITY,	0x02 },
 	{ CAP11XX_REG_STANDBY_THRESH,		0x40 },
 	{ CAP11XX_REG_CONFIG2,			0x40 },
+	{ CAP11XX_REG_LED_POLARITY,		0x00 },
+	{ CAP11XX_REG_LED_OUTPUT_CONTROL,	0x00 },
 	{ CAP11XX_REG_SENSOR_CALIB_LSB1,	0x00 },
 	{ CAP11XX_REG_SENSOR_CALIB_LSB2,	0x00 },
 };
@@ -196,6 +217,69 @@ static void cap11xx_input_close(struct input_dev *idev)
 	cap11xx_set_sleep(priv, true);
 }
 
+#ifdef CONFIG_LEDS_CLASS
+static void cap11xx_led_work(struct work_struct *work)
+{
+	struct cap11xx_led *led = container_of(work, struct cap11xx_led, work);
+	struct cap11xx_priv *priv = led->priv;
+	int value = led->new_brightness;
+
+	regmap_update_bits(priv->regmap, CAP11XX_REG_LED_OUTPUT_CONTROL,
+				BIT(led->id), !!value ? BIT(led->id) : 0);
+}
+
+static void cap11xx_led_set(struct led_classdev *cdev,
+			   enum led_brightness value)
+{
+	struct cap11xx_led *led = container_of(cdev, struct cap11xx_led, cdev);
+
+	led->new_brightness = value;
+	schedule_work(&led->work);
+}
+
+static int cap11xx_register_leds(struct device *dev, struct cap11xx_priv *priv)
+{
+	int ret;
+	int i;
+
+	for (i = 0; i < priv->num_leds; i++) {
+		struct cap11xx_led *led = &priv->leds[i];
+
+		snprintf(led->name, sizeof(led->name), "cap11xx:led%d", i + 1);
+		led->cdev.name = led->name;
+		led->cdev.brightness_set = cap11xx_led_set;
+		led->cdev.brightness = LED_OFF;
+		led->cdev.max_brightness = 1;
+		led->id = i;
+		led->priv = priv;
+
+		ret = led_classdev_register(dev, &led->cdev);
+		if (ret < 0)
+			return ret;
+
+		INIT_WORK(&led->work, cap11xx_led_work);
+	};
+	return 0;
+}
+
+static int cap11xx_i2c_remove(struct i2c_client *client)
+{
+	struct cap11xx_priv *priv = i2c_get_clientdata(client);
+	int i;
+
+	for (i = 0; i < priv->num_leds; i++) {
+		led_classdev_unregister(&priv->leds[i].cdev);
+		cancel_work_sync(&priv->leds[i].work);
+	}
+	regmap_update_bits(priv->regmap,
+		CAP11XX_REG_LED_OUTPUT_CONTROL, 0xff, 0);
+
+	return 0;
+}
+#else
+#define cap11xx_i2c_remove	NULL
+#endif
+
 static int cap11xx_i2c_probe(struct i2c_client *i2c_client,
 			     const struct i2c_device_id *id)
 {
@@ -316,6 +400,17 @@ static int cap11xx_i2c_probe(struct i2c_client *i2c_client,
 	priv->idev->open = cap11xx_input_open;
 	priv->idev->close = cap11xx_input_close;
 
+#ifdef CONFIG_LEDS_CLASS
+	if (cap->num_leds > 0) {
+		priv->leds = devm_kzalloc(dev,
+				cap->num_leds * sizeof(struct cap11xx_led),
+				GFP_KERNEL);
+		if (!priv->leds)
+			return -ENOMEM;
+		priv->num_leds = cap->num_leds;
+		cap11xx_register_leds(dev, priv);
+	}
+#endif
 	input_set_drvdata(priv->idev, priv);
 
 	/*
@@ -366,6 +461,7 @@ static struct i2c_driver cap11xx_i2c_driver = {
 	},
 	.id_table	= cap11xx_i2c_ids,
 	.probe		= cap11xx_i2c_probe,
+	.remove		= cap11xx_i2c_remove,
 };
 
 module_i2c_driver(cap11xx_i2c_driver);
Signature · 1 line
1.9.1
#1 lore raw

Hi Matt,

Quoted 110 lines · Matt Ranostay

Several cap11xx variants have LEDs that be can be controlled, this patchset implements this functionality.

---
 drivers/input/keyboard/cap11xx.c | 102 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 99 insertions(+), 3 deletions(-)
drivers/input/keyboard/cap11xx.c +45 -3
index f07461a..dc48936 100644
@@ -12,6 +12,7 @@
 #include <linux/module.h>
 #include <linux/interrupt.h>
 #include <linux/input.h>
+#include <linux/leds.h>
 #include <linux/of_irq.h>
 #include <linux/regmap.h>
 #include <linux/i2c.h>
@@ -47,6 +48,8 @@
 #define CAP11XX_REG_CONFIG2		0x44
 #define CAP11XX_REG_CONFIG2_ALT_POL	BIT(6)
 #define CAP11XX_REG_SENSOR_BASE_CNT(X)	(0x50 + (X))
+#define CAP11XX_REG_LED_POLARITY	0x73
+#define CAP11XX_REG_LED_OUTPUT_CONTROL	0x74
 #define CAP11XX_REG_SENSOR_CALIB	(0xb1 + (X))
 #define CAP11XX_REG_SENSOR_CALIB_LSB1	0xb9
 #define CAP11XX_REG_SENSOR_CALIB_LSB2	0xba
@@ -56,10 +59,25 @@
 
 #define CAP11XX_MANUFACTURER_ID	0x5d
 
+#ifdef CONFIG_LEDS_CLASS
+struct cap11xx_led {
+	struct cap11xx_priv *priv;
+	struct led_classdev cdev;
+	struct work_struct work;
+	char name[32];
+	int id;
+	enum led_brightness new_brightness;
+};
+#endif
+
 struct cap11xx_priv {
 	struct regmap *regmap;
 	struct input_dev *idev;
 
+#ifdef CONFIG_LEDS_CLASS
+	struct cap11xx_led *leds;
+	int num_leds;
+#endif
 	/* config */
 	u32 keycodes[];
 };
@@ -67,6 +85,7 @@ struct cap11xx_priv {
 struct cap11xx_hw_model {
 	u8 product_id;
 	unsigned int num_channels;
+	unsigned int num_leds;
 };
 
 enum {
@@ -76,9 +95,9 @@ enum {
 };
 
 static const struct cap11xx_hw_model cap11xx_devices[] = {
-	[CAP1106] = { .product_id = 0x55, .num_channels = 6 },
-	[CAP1126] = { .product_id = 0x53, .num_channels = 6 },
-	[CAP1188] = { .product_id = 0x50, .num_channels = 8 },
+	[CAP1106] = { .product_id = 0x55, .num_channels = 6, .num_leds = 0 },
+	[CAP1126] = { .product_id = 0x53, .num_channels = 6, .num_leds = 2 },
+	[CAP1188] = { .product_id = 0x50, .num_channels = 8, .num_leds = 8 },
 };
 
 static const struct reg_default cap11xx_reg_defaults[] = {
@@ -111,6 +130,8 @@ static const struct reg_default cap11xx_reg_defaults[] = {
 	{ CAP11XX_REG_STANDBY_SENSITIVITY,	0x02 },
 	{ CAP11XX_REG_STANDBY_THRESH,		0x40 },
 	{ CAP11XX_REG_CONFIG2,			0x40 },
+	{ CAP11XX_REG_LED_POLARITY,		0x00 },
+	{ CAP11XX_REG_LED_OUTPUT_CONTROL,	0x00 },
 	{ CAP11XX_REG_SENSOR_CALIB_LSB1,	0x00 },
 	{ CAP11XX_REG_SENSOR_CALIB_LSB2,	0x00 },
 };
@@ -196,6 +217,69 @@ static void cap11xx_input_close(struct input_dev *idev)
 	cap11xx_set_sleep(priv, true);
 }
 
+#ifdef CONFIG_LEDS_CLASS
+static void cap11xx_led_work(struct work_struct *work)
+{
+	struct cap11xx_led *led = container_of(work, struct cap11xx_led, work);
+	struct cap11xx_priv *priv = led->priv;
+	int value = led->new_brightness;
+
+	regmap_update_bits(priv->regmap, CAP11XX_REG_LED_OUTPUT_CONTROL,
+				BIT(led->id), !!value ? BIT(led->id) : 0);
+}
+
+static void cap11xx_led_set(struct led_classdev *cdev,
+			   enum led_brightness value)
+{
+	struct cap11xx_led *led = container_of(cdev, struct cap11xx_led, cdev);
+
+	led->new_brightness = value;
+	schedule_work(&led->work);
+}
+
+static int cap11xx_register_leds(struct device *dev, struct cap11xx_priv *priv)

drivers/input/keyboard/cap11xx.c static void cap11xx_input_close(struct input_dev *idev) · line 250

Quoted 10 lines
drivers/input/keyboard/cap11xx.c +10 -0
+{
+	int ret;
+	int i;
+
+	for (i = 0; i < priv->num_leds; i++) {
+		struct cap11xx_led *led = &priv->leds[i];
+
+		snprintf(led->name, sizeof(led->name), "cap11xx:led%d", i + 1);
+		led->cdev.name = led->name;
+		led->cdev.brightness_set = cap11xx_led_set;

Use the .brightness_set_sync callback, so you don't have to dance around with the workers at all.

drivers/input/keyboard/cap11xx.c static void cap11xx_input_close(struct input_dev *idev) · line 256

Quoted 6 lines
drivers/input/keyboard/cap11xx.c +6 -0
+		led->cdev.brightness = LED_OFF;
+		led->cdev.max_brightness = 1;
+		led->id = i;
+		led->priv = priv;
+
+		ret = led_classdev_register(dev, &led->cdev);

You can use the devm_variant of this function. That will solve the tricky part of unwinding if registration of only one LED fails.

Quoted 7 lines
drivers/input/keyboard/cap11xx.c +7 -0
+		if (ret < 0)
+			return ret;
+
+		INIT_WORK(&led->work, cap11xx_led_work);
+	};
+	return 0;
+}

drivers/input/keyboard/cap11xx.c static void cap11xx_input_close(struct input_dev *idev) · line 273

Quoted 10 lines
drivers/input/keyboard/cap11xx.c +10 -0
+
+static int cap11xx_i2c_remove(struct i2c_client *client)
+{
+	struct cap11xx_priv *priv = i2c_get_clientdata(client);
+	int i;
+
+	for (i = 0; i < priv->num_leds; i++) {
+		led_classdev_unregister(&priv->leds[i].cdev);
+		cancel_work_sync(&priv->leds[i].work);
+	}

With the two changes from above, this code can go away.

drivers/input/keyboard/cap11xx.c static void cap11xx_input_close(struct input_dev *idev) · line 275

Quoted 2 lines
drivers/input/keyboard/cap11xx.c +2 -0
+	regmap_update_bits(priv->regmap,
+		CAP11XX_REG_LED_OUTPUT_CONTROL, 0xff, 0);

Well, we don't reset the other registers either, so I don't know if it's worth it. After all, the entire function can go away if you left this register untouched on removal time.

Quoted 11 lines
drivers/input/keyboard/cap11xx.c +7 -0
+
+	return 0;
+}
+#else
+#define cap11xx_i2c_remove	NULL
+#endif
+
 static int cap11xx_i2c_probe(struct i2c_client *i2c_client,
 			     const struct i2c_device_id *id)
 {

drivers/input/keyboard/cap11xx.c static int cap11xx_i2c_probe(struct i2c_client *i2c_client, · line 407

Quoted 8 lines
drivers/input/keyboard/cap11xx.c +5 -0
@@ -316,6 +400,17 @@ static int cap11xx_i2c_probe(struct i2c_client *i2c_client,
 	priv->idev->open = cap11xx_input_open;
 	priv->idev->close = cap11xx_input_close;
 
+#ifdef CONFIG_LEDS_CLASS
+	if (cap->num_leds > 0) {
+		priv->leds = devm_kzalloc(dev,
+				cap->num_leds * sizeof(struct cap11xx_led),
+				GFP_KERNEL);

There's devm_kcalloc(), but that's nitpicking.

drivers/input/keyboard/cap11xx.c static int cap11xx_i2c_probe(struct i2c_client *i2c_client, · line 411

Quoted 4 lines
drivers/input/keyboard/cap11xx.c +4 -0
+		if (!priv->leds)
+			return -ENOMEM;
+		priv->num_leds = cap->num_leds;
+		cap11xx_register_leds(dev, priv);

You need to check the return value here and bail.

Thanks,
Daniel

maintainer
#2 lore raw
Quoted 118 lines · Daniel Mack

Hi Matt,

Quoted 115 lines · Matt Ranostay

Several cap11xx variants have LEDs that be can be controlled, this patchset implements this functionality.

---
 drivers/input/keyboard/cap11xx.c | 102 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 99 insertions(+), 3 deletions(-)
drivers/input/keyboard/cap11xx.c +50 -3
index f07461a..dc48936 100644
@@ -12,6 +12,7 @@
 #include <linux/module.h>
 #include <linux/interrupt.h>
 #include <linux/input.h>
+#include <linux/leds.h>
 #include <linux/of_irq.h>
 #include <linux/regmap.h>
 #include <linux/i2c.h>
@@ -47,6 +48,8 @@
 #define CAP11XX_REG_CONFIG2		0x44
 #define CAP11XX_REG_CONFIG2_ALT_POL	BIT(6)
 #define CAP11XX_REG_SENSOR_BASE_CNT(X)	(0x50 + (X))
+#define CAP11XX_REG_LED_POLARITY	0x73
+#define CAP11XX_REG_LED_OUTPUT_CONTROL	0x74
 #define CAP11XX_REG_SENSOR_CALIB	(0xb1 + (X))
 #define CAP11XX_REG_SENSOR_CALIB_LSB1	0xb9
 #define CAP11XX_REG_SENSOR_CALIB_LSB2	0xba
@@ -56,10 +59,25 @@
 
 #define CAP11XX_MANUFACTURER_ID	0x5d
 
+#ifdef CONFIG_LEDS_CLASS
+struct cap11xx_led {
+	struct cap11xx_priv *priv;
+	struct led_classdev cdev;
+	struct work_struct work;
+	char name[32];
+	int id;
+	enum led_brightness new_brightness;
+};
+#endif
+
 struct cap11xx_priv {
 	struct regmap *regmap;
 	struct input_dev *idev;
 
+#ifdef CONFIG_LEDS_CLASS
+	struct cap11xx_led *leds;
+	int num_leds;
+#endif
 	/* config */
 	u32 keycodes[];
 };
@@ -67,6 +85,7 @@ struct cap11xx_priv {
 struct cap11xx_hw_model {
 	u8 product_id;
 	unsigned int num_channels;
+	unsigned int num_leds;
 };
 
 enum {
@@ -76,9 +95,9 @@ enum {
 };
 
 static const struct cap11xx_hw_model cap11xx_devices[] = {
-	[CAP1106] = { .product_id = 0x55, .num_channels = 6 },
-	[CAP1126] = { .product_id = 0x53, .num_channels = 6 },
-	[CAP1188] = { .product_id = 0x50, .num_channels = 8 },
+	[CAP1106] = { .product_id = 0x55, .num_channels = 6, .num_leds = 0 },
+	[CAP1126] = { .product_id = 0x53, .num_channels = 6, .num_leds = 2 },
+	[CAP1188] = { .product_id = 0x50, .num_channels = 8, .num_leds = 8 },
 };
 
 static const struct reg_default cap11xx_reg_defaults[] = {
@@ -111,6 +130,8 @@ static const struct reg_default cap11xx_reg_defaults[] = {
 	{ CAP11XX_REG_STANDBY_SENSITIVITY,	0x02 },
 	{ CAP11XX_REG_STANDBY_THRESH,		0x40 },
 	{ CAP11XX_REG_CONFIG2,			0x40 },
+	{ CAP11XX_REG_LED_POLARITY,		0x00 },
+	{ CAP11XX_REG_LED_OUTPUT_CONTROL,	0x00 },
 	{ CAP11XX_REG_SENSOR_CALIB_LSB1,	0x00 },
 	{ CAP11XX_REG_SENSOR_CALIB_LSB2,	0x00 },
 };
@@ -196,6 +217,69 @@ static void cap11xx_input_close(struct input_dev *idev)
 	cap11xx_set_sleep(priv, true);
 }
 
+#ifdef CONFIG_LEDS_CLASS
+static void cap11xx_led_work(struct work_struct *work)
+{
+	struct cap11xx_led *led = container_of(work, struct cap11xx_led, work);
+	struct cap11xx_priv *priv = led->priv;
+	int value = led->new_brightness;
+
+	regmap_update_bits(priv->regmap, CAP11XX_REG_LED_OUTPUT_CONTROL,
+				BIT(led->id), !!value ? BIT(led->id) : 0);
+}
+
+static void cap11xx_led_set(struct led_classdev *cdev,
+			   enum led_brightness value)
+{
+	struct cap11xx_led *led = container_of(cdev, struct cap11xx_led, cdev);
+
+	led->new_brightness = value;
+	schedule_work(&led->work);
+}
+
+static int cap11xx_register_leds(struct device *dev, struct cap11xx_priv *priv)
+{
+	int ret;
+	int i;
+
+	for (i = 0; i < priv->num_leds; i++) {
Quoted 8 lines

drivers/input/keyboard/cap11xx.c static void cap11xx_input_close(struct input_dev *idev) · line 250

Quoted 5 lines
drivers/input/keyboard/cap11xx.c +5 -0
+		struct cap11xx_led *led = &priv->leds[i];
+
+		snprintf(led->name, sizeof(led->name), "cap11xx:led%d", i + 1);
+		led->cdev.name = led->name;
+		led->cdev.brightness_set = cap11xx_led_set;

Use the .brightness_set_sync callback, so you don't have to dance around with the workers at all.

Umm, how will it help? You still can't sleep in set_brightness(). brightness_set_sync() just says that the driver guarantees that the LED state will be set (or error returned) when the function returns.

Thanks.

Signature · 1 line
Dmitry
#3 lore raw
Quoted 128 lines · Dmitry Torokhov
Quoted 118 lines · Daniel Mack

Hi Matt,

Quoted 115 lines · Matt Ranostay

Several cap11xx variants have LEDs that be can be controlled, this patchset implements this functionality.

---
 drivers/input/keyboard/cap11xx.c | 102 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 99 insertions(+), 3 deletions(-)
drivers/input/keyboard/cap11xx.c +50 -3
index f07461a..dc48936 100644
@@ -12,6 +12,7 @@
 #include <linux/module.h>
 #include <linux/interrupt.h>
 #include <linux/input.h>
+#include <linux/leds.h>
 #include <linux/of_irq.h>
 #include <linux/regmap.h>
 #include <linux/i2c.h>
@@ -47,6 +48,8 @@
 #define CAP11XX_REG_CONFIG2                0x44
 #define CAP11XX_REG_CONFIG2_ALT_POL        BIT(6)
 #define CAP11XX_REG_SENSOR_BASE_CNT(X)     (0x50 + (X))
+#define CAP11XX_REG_LED_POLARITY   0x73
+#define CAP11XX_REG_LED_OUTPUT_CONTROL     0x74
 #define CAP11XX_REG_SENSOR_CALIB   (0xb1 + (X))
 #define CAP11XX_REG_SENSOR_CALIB_LSB1      0xb9
 #define CAP11XX_REG_SENSOR_CALIB_LSB2      0xba
@@ -56,10 +59,25 @@
 
 #define CAP11XX_MANUFACTURER_ID    0x5d
 
+#ifdef CONFIG_LEDS_CLASS
+struct cap11xx_led {
+   struct cap11xx_priv *priv;
+   struct led_classdev cdev;
+   struct work_struct work;
+   char name[32];
+   int id;
+   enum led_brightness new_brightness;
+};
+#endif
+
 struct cap11xx_priv {
    struct regmap *regmap;
    struct input_dev *idev;
 
+#ifdef CONFIG_LEDS_CLASS
+   struct cap11xx_led *leds;
+   int num_leds;
+#endif
    /* config */
    u32 keycodes[];
 };
@@ -67,6 +85,7 @@ struct cap11xx_priv {
 struct cap11xx_hw_model {
    u8 product_id;
    unsigned int num_channels;
+   unsigned int num_leds;
 };
 
 enum {
@@ -76,9 +95,9 @@ enum {
 };
 
 static const struct cap11xx_hw_model cap11xx_devices[] = {
-   [CAP1106] = { .product_id = 0x55, .num_channels = 6 },
-   [CAP1126] = { .product_id = 0x53, .num_channels = 6 },
-   [CAP1188] = { .product_id = 0x50, .num_channels = 8 },
+   [CAP1106] = { .product_id = 0x55, .num_channels = 6, .num_leds = 0 },
+   [CAP1126] = { .product_id = 0x53, .num_channels = 6, .num_leds = 2 },
+   [CAP1188] = { .product_id = 0x50, .num_channels = 8, .num_leds = 8 },
 };
 
 static const struct reg_default cap11xx_reg_defaults[] = {
@@ -111,6 +130,8 @@ static const struct reg_default cap11xx_reg_defaults[] = {
    { CAP11XX_REG_STANDBY_SENSITIVITY,      0x02 },
    { CAP11XX_REG_STANDBY_THRESH,           0x40 },
    { CAP11XX_REG_CONFIG2,                  0x40 },
+   { CAP11XX_REG_LED_POLARITY,             0x00 },
+   { CAP11XX_REG_LED_OUTPUT_CONTROL,       0x00 },
    { CAP11XX_REG_SENSOR_CALIB_LSB1,        0x00 },
    { CAP11XX_REG_SENSOR_CALIB_LSB2,        0x00 },
 };
@@ -196,6 +217,69 @@ static void cap11xx_input_close(struct input_dev *idev)
    cap11xx_set_sleep(priv, true);
 }
 
+#ifdef CONFIG_LEDS_CLASS
+static void cap11xx_led_work(struct work_struct *work)
+{
+   struct cap11xx_led *led = container_of(work, struct cap11xx_led, work);
+   struct cap11xx_priv *priv = led->priv;
+   int value = led->new_brightness;
+
+   regmap_update_bits(priv->regmap, CAP11XX_REG_LED_OUTPUT_CONTROL,
+                           BIT(led->id), !!value ? BIT(led->id) : 0);
+}
+
+static void cap11xx_led_set(struct led_classdev *cdev,
+                      enum led_brightness value)
+{
+   struct cap11xx_led *led = container_of(cdev, struct cap11xx_led, cdev);
+
+   led->new_brightness = value;
+   schedule_work(&led->work);
+}
+
+static int cap11xx_register_leds(struct device *dev, struct cap11xx_priv *priv)
+{
+   int ret;
+   int i;
+
+   for (i = 0; i < priv->num_leds; i++) {
Quoted 8 lines

drivers/input/keyboard/cap11xx.c static void cap11xx_input_close(struct input_dev *idev) · line 250

Quoted 5 lines
drivers/input/keyboard/cap11xx.c +5 -0
+           struct cap11xx_led *led = &priv->leds[i];
+
+           snprintf(led->name, sizeof(led->name), "cap11xx:led%d", i + 1);
+           led->cdev.name = led->name;
+           led->cdev.brightness_set = cap11xx_led_set;

Use the .brightness_set_sync callback, so you don't have to dance around with the workers at all.

Quoted 4 lines

Umm, how will it help? You still can't sleep in set_brightness(). brightness_set_sync() just says that the driver guarantees that the LED state will be set (or error returned) when the function returns.

Yeah the worker queues are for sure needed.

Quoted 4 lines

Thanks.

--
Dmitry

#4 lore raw
In reply to Dmitry Torokhov
Quoted 8 lines · Dmitry Torokhov
Quoted 2 lines · Daniel Mack

Use the .brightness_set_sync callback, so you don't have to dance around with the workers at all.

Umm, how will it help? You still can't sleep in set_brightness(). brightness_set_sync() just says that the driver guarantees that the LED state will be set (or error returned) when the function returns.

Hmm, right. I was mislead by this comment in include/linux/leds.h:

/*
* Set LED brightness level immediately - it can block the caller for
* the time required for accessing a LED device register. */

But I in fact don't see any wrapper logic that magically uses a worker when called from interrupt context.

Thanks for the heads-up!
Daniel

#5 lore raw
In reply to Matt Ranostay
Quoted 7 lines · Matt Ranostay

Several cap11xx variants have LEDs that be can be controlled, this patchset implements this functionality.

---
 drivers/input/keyboard/cap11xx.c | 102 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 99 insertions(+), 3 deletions(-)

Ah, and please include the LED maintainers in v2, they might spot other details :)

Thanks,
Daniel

#6 lore raw
Quoted 4 lines · Daniel Mack
Quoted 3 lines · Matt Ranostay

Several cap11xx variants have LEDs that be can be controlled, this patchset implements this functionality.

Quoted 8 lines
Quoted 4 lines
---
 drivers/input/keyboard/cap11xx.c | 102 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 99 insertions(+), 3 deletions(-)

Ah, and please include the LED maintainers in v2, they might spot other details :)

Will do.

Quoted 5 lines

Thanks,
Daniel