Skip to contentSkip to conversation

leds: add driver for LM3530 ALS

PATCH Superseded Shreshtha Kumar SAHU 2 participants 3 messages Jan 20, 2011 – Jan 21, 2011 LED

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 leds: add driver for LM3530 ALS

From: Shreshtha Kumar Sahu <[email protected]>

simple backlight driver for National Semiconductor LM3530. Presently only manual mode is supported, PWM and ALS support to be added.

---
changes in v2:
modified according to comments from Richard Purdie
- i2c write in a loop in init function
- meaningful sysfs user input values

 drivers/leds/Kconfig       |   10 ++
 drivers/leds/Makefile      |    1 +
 drivers/leds/leds-lm3530.c |  381 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/led-lm3530.h |  108 +++++++++++++
 4 files changed, 500 insertions(+), 0 deletions(-)
 create mode 100644 drivers/leds/leds-lm3530.c
 create mode 100644 include/linux/led-lm3530.h
drivers/leds/Kconfig +10 -0
index 6f190f4..9bec869 100644
@@ -34,6 +34,16 @@ config LEDS_ATMEL_PWM
 	  This option enables support for LEDs driven using outputs
 	  of the dedicated PWM controller found on newer Atmel SOCs.
 
+config LEDS_LM3530
+	tristate "LCD Backlight driver for LM3530"
+	depends on LEDS_CLASS
+	depends on I2C
+	help
+	  This option enables support for the LCD backlight using
+	  LM3530 ambient light sensor chip. This ALS chip can be
+	  controlled manually or using PWM input or using ambient
+	  light automatically.
+
 config LEDS_LOCOMO
 	tristate "LED Support for Locomo device"
 	depends on LEDS_CLASS
drivers/leds/Makefile +1 -0
index aae6989..39c80fc 100644
@@ -9,6 +9,7 @@ obj-$(CONFIG_LEDS_88PM860X)		+= leds-88pm860x.o
 obj-$(CONFIG_LEDS_ATMEL_PWM)		+= leds-atmel-pwm.o
 obj-$(CONFIG_LEDS_BD2802)		+= leds-bd2802.o
 obj-$(CONFIG_LEDS_LOCOMO)		+= leds-locomo.o
+obj-$(CONFIG_LEDS_LM3530)		+= leds-lm3530.o
 obj-$(CONFIG_LEDS_MIKROTIK_RB532)	+= leds-rb532.o
 obj-$(CONFIG_LEDS_S3C24XX)		+= leds-s3c24xx.o
 obj-$(CONFIG_LEDS_AMS_DELTA)		+= leds-ams-delta.o
drivers/leds/leds-lm3530.c new file +381 -0

381 lines not on this page · raw message

include/linux/led-lm3530.h new file +108 -0

108 lines not on this page · raw message

Signature · 1 line
1.7.2.dirty
#1 lore raw
Quoted 9 lines · On Thu, 20 Jan 2011 15:40:49 +0530 Shreshtha Kumar SAHU…

From: Shreshtha Kumar Sahu <[email protected]>

simple backlight driver for National Semiconductor LM3530. Presently only manual mode is supported, PWM and ALS support to be added.

...

drivers/leds/leds-lm3530.c line 130

Quoted 6 lines
drivers/leds/leds-lm3530.c new file +6 -0
+static int lm3530_get_mode_from_str(const char *str, int count)
+{
+	int i;
+
+	if (str[0] == '\n')
+		return -1;

Why is this here? I think the function would work OK if it was removed?

drivers/leds/leds-lm3530.c line 132

Quoted 1 line
drivers/leds/leds-lm3530.c new file +1 -0
+	for (i = 0; i < LM3530_BL_MODE_MAX; i++)

Could use ARRAY_SIZE(mode_map) here and do away with LM3530_BL_MODE_MAX.

drivers/leds/leds-lm3530.c line 133

Quoted 1 line
drivers/leds/leds-lm3530.c new file +1 -0
+		if (!strncmp(mode_map[i].mode, str, count))

Why strncmp? The code will treat input of the form "alsfoo" as "als", which is sloppy.

Quoted 8 lines
drivers/leds/leds-lm3530.c new file +5 -0
+			return mode_map[i].mode_val;
+
+	return -1;
+}
+
 
...
 

drivers/leds/leds-lm3530.c line 242

Quoted 10 lines
drivers/leds/leds-lm3530.c new file +10 -0
+static ssize_t lm3530_mode_set(struct device *dev, struct device_attribute
+				   *attr, const char *buf, size_t size)
+{
+	int err;
+	struct i2c_client *client = container_of(
+					dev->parent, struct i2c_client, dev);
+	struct lm3530_data *drvdata = i2c_get_clientdata(client);
+	int mode;
+
+	mode = lm3530_get_mode_from_str(buf, size-1);

And why the "size-1"? Assuming there's a \n? But userspace should be able to do write(fd, 3, "als")?

Perhaps all this can be tidied up with

	mode = lm3530_get_mode_from_str(strim(buf));

Alternatively, see sysfs_streq() - it was added to address these sorts of things.

Quoted 26 lines
drivers/leds/leds-lm3530.c new file +23 -0
+	if (mode < 0) {
+		dev_err(dev, "Invalid mode\n");
+		return -EINVAL;
+	}
+
+	if (mode == LM3530_BL_MODE_MANUAL)
+		drvdata->mode = LM3530_BL_MODE_MANUAL;
+	else if (mode == LM3530_BL_MODE_ALS)
+		drvdata->mode = LM3530_BL_MODE_ALS;
+	else if (mode == LM3530_BL_MODE_PWM) {
+		dev_err(dev, "PWM mode not supported\n");
+		return -EINVAL;
+	}
+
+	err = lm3530_init_registers(drvdata);
+	if (err) {
+		dev_err(dev, "Setting %s Mode failed :%d\n", buf, err);
+		return err;
+	}
+
+	return sizeof(drvdata->mode);
+}
+
 
...
#2 lore raw

PATCH v3 1/2 leds: add driver for LM3530 ALS

From: Shreshtha Kumar Sahu <[email protected]>

simple backlight driver for National Semiconductor LM3530. Presently only manual mode is supported, PWM and ALS support to be added.

---
changes in v3:
modified according to comments from Andrew Morton
- rectify sloppy string comparision; now using sysfs_streq()
- remove unnecessary LM3530_BL_MODE_MAX and use ARRAY_MA

 drivers/leds/Kconfig       |   10 ++
 drivers/leds/Makefile      |    1 +
 drivers/leds/leds-lm3530.c |  378 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/led-lm3530.h |  107 +++++++++++++
 4 files changed, 496 insertions(+), 0 deletions(-)
 create mode 100644 drivers/leds/leds-lm3530.c
 create mode 100644 include/linux/led-lm3530.h
drivers/leds/Kconfig +10 -0
index 6f190f4..9bec869 100644
@@ -34,6 +34,16 @@ config LEDS_ATMEL_PWM
 	  This option enables support for LEDs driven using outputs
 	  of the dedicated PWM controller found on newer Atmel SOCs.
 
+config LEDS_LM3530
+	tristate "LCD Backlight driver for LM3530"
+	depends on LEDS_CLASS
+	depends on I2C
+	help
+	  This option enables support for the LCD backlight using
+	  LM3530 ambient light sensor chip. This ALS chip can be
+	  controlled manually or using PWM input or using ambient
+	  light automatically.
+
 config LEDS_LOCOMO
 	tristate "LED Support for Locomo device"
 	depends on LEDS_CLASS
drivers/leds/Makefile +1 -0
index aae6989..39c80fc 100644
@@ -9,6 +9,7 @@ obj-$(CONFIG_LEDS_88PM860X)		+= leds-88pm860x.o
 obj-$(CONFIG_LEDS_ATMEL_PWM)		+= leds-atmel-pwm.o
 obj-$(CONFIG_LEDS_BD2802)		+= leds-bd2802.o
 obj-$(CONFIG_LEDS_LOCOMO)		+= leds-locomo.o
+obj-$(CONFIG_LEDS_LM3530)		+= leds-lm3530.o
 obj-$(CONFIG_LEDS_MIKROTIK_RB532)	+= leds-rb532.o
 obj-$(CONFIG_LEDS_S3C24XX)		+= leds-s3c24xx.o
 obj-$(CONFIG_LEDS_AMS_DELTA)		+= leds-ams-delta.o
drivers/leds/leds-lm3530.c new file +378 -0

378 lines not on this page · raw message

include/linux/led-lm3530.h new file +107 -0

107 lines not on this page · raw message

Signature · 1 line
1.7.2.dirty