mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] leds: flash: Add driver for the TI LM3554
@ 2026-11-16 18:08 Volodymyr Chapman
  2026-11-17 14:14 ` Uwe Kleine-König
  0 siblings, 1 reply; 2+ messages in thread
From: Volodymyr Chapman @ 2026-11-16 18:08 UTC (permalink / raw)
  To: Lee Jones, Pavel Machek
  Cc: Griffin Kroah-Hartman, Hans de Goede, linux-leds, linux-kernel,
	Volodymyr Chapman

The Texas Instruments LM3554 is a flash LED driver with two current
sources for a flash LED, used together, and an indicator LED output,
controlled over I2C. It is used for the rear camera flash of the Lenovo
ThinkPad 10 (20C1/20C3), where the firmware describes it as ACPI device
INTCF1C, at I2C address 0x53, with the STROBE, TORCH and HWEN GPIOs
listed in that order and without names.

An atomisp specific driver for the LM3554 was removed in commit
f1aa35ab5943 ("media: atomisp: Drop the atomisp custom lm3554 flash
driver"), which suggested re-implementing it with the LED flash class
API if needed. This is such a driver, written from the datasheet and
using the removed driver as a reference for the ACPI GPIO order.

The driver controls the LED over I2C only and keeps the STROBE and TORCH
inputs low. It registers a flash LED class device with 8 torch levels
(34 to 295 mA), a flash current of 71 to 1037 mA and a flash timeout of
32 to 512 ms. The chip allows up to 1191 mA and 1024 ms, but the
firmware does not describe the LED's ratings, so the flash current and
timeout are limited to the chip's power-on defaults. Faults are read
from the flags register. The indicator output is not supported, it is
not connected on the ThinkPad 10.

Tested on a Lenovo ThinkPad 10 (20C3, Atom Z3795), with the driver and
led-class-flash built out of tree against a 7.2.15 kernel: torch at
levels 1 and 8, flash at 71 mA / 256 ms (the flash ends by itself and
the timeout fault is reported) and at 365 mA / 96 ms, s2idle suspend and
resume with the torch on (the LED is off while suspended and on again
after resume) and off, and unloading. This was also build-tested with
W=1 on top of for-leds-next and media.git next.

The driver was written with the help of an AI coding assistant (Claude);
the testing was done on the hardware by the submitter.

Signed-off-by: Volodymyr Chapman <volodymyrchapman@gmail.com>
---
 MAINTAINERS                      |   6 +
 drivers/leds/flash/Kconfig       |  12 ++
 drivers/leds/flash/Makefile      |   1 +
 drivers/leds/flash/leds-lm3554.c | 337 +++++++++++++++++++++++++++++++
 4 files changed, 356 insertions(+)
 create mode 100644 drivers/leds/flash/leds-lm3554.c

diff --git a/MAINTAINERS b/MAINTAINERS
index af302622c..6382ea861 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -27685,6 +27685,12 @@ F:	include/linux/dma/k3-udma-glue.h
 F:	include/linux/dma/ti-cppi5.h
 X:	drivers/dma/ti/cppi41.c
 
+TEXAS INSTRUMENTS LM3554 FLASH LED DRIVER
+M:	Volodymyr Chapman <volodymyrchapman@gmail.com>
+L:	linux-leds@vger.kernel.org
+S:	Maintained
+F:	drivers/leds/flash/leds-lm3554.c
+
 TEXAS INSTRUMENTS TPS25990 HARDWARE MONITOR DRIVER
 M:	Jerome Brunet <jbrunet@baylibre.com>
 L:	linux-hwmon@vger.kernel.org
diff --git a/drivers/leds/flash/Kconfig b/drivers/leds/flash/Kconfig
index 9b6dd3f1f..bfc1f8888 100644
--- a/drivers/leds/flash/Kconfig
+++ b/drivers/leds/flash/Kconfig
@@ -31,6 +31,18 @@ config LEDS_KTD2692
 
 	  Say Y to enable this driver.
 
+config LEDS_LM3554
+	tristate "LED support for the TI LM3554 flash LED driver"
+	depends on ACPI && I2C && GPIOLIB
+	select REGMAP_I2C
+	help
+	  This option enables support for the Texas Instruments LM3554
+	  flash LED driver, as used for the rear camera flash of the
+	  Lenovo ThinkPad 10 (20C1/20C3).
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called leds-lm3554.
+
 config LEDS_LM3601X
 	tristate "LED support for LM3601x Chips"
 	depends on LEDS_CLASS && I2C
diff --git a/drivers/leds/flash/Makefile b/drivers/leds/flash/Makefile
index 44e6c1b4b..90afe6583 100644
--- a/drivers/leds/flash/Makefile
+++ b/drivers/leds/flash/Makefile
@@ -5,6 +5,7 @@ obj-$(CONFIG_LEDS_MT6370_FLASH)	+= leds-mt6370-flash.o
 obj-$(CONFIG_LEDS_AAT1290)	+= leds-aat1290.o
 obj-$(CONFIG_LEDS_AS3645A)	+= leds-as3645a.o
 obj-$(CONFIG_LEDS_KTD2692)	+= leds-ktd2692.o
+obj-$(CONFIG_LEDS_LM3554)	+= leds-lm3554.o
 obj-$(CONFIG_LEDS_LM3601X)	+= leds-lm3601x.o
 obj-$(CONFIG_LEDS_MAX77693)	+= leds-max77693.o
 obj-$(CONFIG_LEDS_QCOM_FLASH)	+= leds-qcom-flash.o
diff --git a/drivers/leds/flash/leds-lm3554.c b/drivers/leds/flash/leds-lm3554.c
new file mode 100644
index 000000000..70c526df3
--- /dev/null
+++ b/drivers/leds/flash/leds-lm3554.c
@@ -0,0 +1,337 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Texas Instruments LM3554 flash LED driver
+ *
+ * The LM3554 drives a flash LED through two current sources (LED1, LED2),
+ * which are used together, and an indicator LED (LEDI). It is used e.g. for
+ * the rear camera flash of the Lenovo ThinkPad 10 (20C1/20C3), where the
+ * firmware describes it as ACPI device INTCF1C.
+ *
+ * Copyright (C) 2026 Volodymyr Chapman <volodymyrchapman@gmail.com>
+ */
+
+#include <linux/acpi.h>
+#include <linux/bits.h>
+#include <linux/bitfield.h>
+#include <linux/delay.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/led-class-flash.h>
+#include <linux/leds.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/regmap.h>
+
+#define LM3554_REG_TORCH		0xa0
+#define   LM3554_TORCH_CURRENT		GENMASK(5, 3)
+#define LM3554_REG_FLASH		0xb0
+#define   LM3554_FLASH_CURRENT		GENMASK(6, 3)
+/* Bits [2:0] of the torch and flash registers are shared */
+#define   LM3554_MODE			GENMASK(2, 0)
+#define   LM3554_MODE_SHUTDOWN		0
+#define   LM3554_MODE_TORCH		2
+#define   LM3554_MODE_FLASH		3
+#define LM3554_REG_DURATION		0xc0
+#define   LM3554_TIMEOUT		GENMASK(4, 0)
+#define LM3554_REG_FLAGS		0xd0
+#define   LM3554_FLAG_TIMEOUT		BIT(0)
+#define   LM3554_FLAG_THERMAL_SHUTDOWN	BIT(1)
+#define   LM3554_FLAG_LED_FAULT		BIT(2)
+#define   LM3554_FLAG_LED_THERMAL	BIT(5)
+#define   LM3554_FLAG_VIN_MONITOR	BIT(7)
+#define LM3554_REG_CONFIG1		0xe0
+#define LM3554_REG_CONFIG2		0xf0
+
+#define LM3554_TIMEOUT_STEP_US		32000
+#define LM3554_TIMEOUT_MIN_US		32000
+/* Power-on default, the chip allows up to 1024 ms */
+#define LM3554_TIMEOUT_MAX_US		512000
+
+/*
+ * 8 torch current settings, LED1 + LED2 together: 34, 71, 108, 146, 180, 218,
+ * 256 and 295 mA.
+ */
+#define LM3554_TORCH_LEVELS		8
+
+/* Total of LED1 + LED2, for the 16 flash current settings */
+static const u32 lm3554_flash_ua[] = {
+	71000, 146000, 218000, 295000, 365000, 441000, 518000, 596000,
+	652000, 729000, 805000, 881000, 960000, 1037000, 1113000, 1191000,
+};
+
+/* Power-on default (13), the chip allows up to 1191 mA */
+#define LM3554_FLASH_MAX_UA		1037000
+
+struct lm3554 {
+	struct led_classdev_flash fled_cdev;
+	struct regmap *regmap;
+	struct gpio_desc *enable_gpio;
+	/* Serialises register access between the LED and flash callbacks */
+	struct mutex lock;
+};
+
+static struct lm3554 *fled_cdev_to_lm3554(struct led_classdev_flash *fled_cdev)
+{
+	return container_of(fled_cdev, struct lm3554, fled_cdev);
+}
+
+static bool lm3554_volatile_reg(struct device *dev, unsigned int reg)
+{
+	/* The mode bits clear themselves when a flash times out */
+	return reg == LM3554_REG_TORCH || reg == LM3554_REG_FLASH ||
+	       reg == LM3554_REG_FLAGS;
+}
+
+static const struct regmap_config lm3554_regmap_config = {
+	.reg_bits = 8,
+	.val_bits = 8,
+	.max_register = LM3554_REG_CONFIG2,
+	.volatile_reg = lm3554_volatile_reg,
+};
+
+static int lm3554_set_mode(struct lm3554 *lm3554, unsigned int mode)
+{
+	return regmap_update_bits(lm3554->regmap, LM3554_REG_TORCH,
+				  LM3554_MODE, mode);
+}
+
+static int lm3554_brightness_set(struct led_classdev *led_cdev,
+				 enum led_brightness brightness)
+{
+	struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
+	struct lm3554 *lm3554 = fled_cdev_to_lm3554(fled_cdev);
+	int ret;
+
+	guard(mutex)(&lm3554->lock);
+
+	if (brightness == LED_OFF)
+		return lm3554_set_mode(lm3554, LM3554_MODE_SHUTDOWN);
+
+	ret = regmap_update_bits(lm3554->regmap, LM3554_REG_TORCH,
+				 LM3554_TORCH_CURRENT,
+				 FIELD_PREP(LM3554_TORCH_CURRENT, brightness - 1));
+	if (ret)
+		return ret;
+
+	return lm3554_set_mode(lm3554, LM3554_MODE_TORCH);
+}
+
+static int lm3554_flash_brightness_set(struct led_classdev_flash *fled_cdev,
+				       u32 brightness)
+{
+	struct lm3554 *lm3554 = fled_cdev_to_lm3554(fled_cdev);
+	unsigned int i;
+
+	/* Use the highest setting which does not exceed the requested current */
+	for (i = ARRAY_SIZE(lm3554_flash_ua) - 1; i > 0; i--)
+		if (lm3554_flash_ua[i] <= brightness)
+			break;
+
+	guard(mutex)(&lm3554->lock);
+
+	return regmap_update_bits(lm3554->regmap, LM3554_REG_FLASH,
+				  LM3554_FLASH_CURRENT,
+				  FIELD_PREP(LM3554_FLASH_CURRENT, i));
+}
+
+static int lm3554_timeout_set(struct led_classdev_flash *fled_cdev, u32 timeout)
+{
+	struct lm3554 *lm3554 = fled_cdev_to_lm3554(fled_cdev);
+
+	guard(mutex)(&lm3554->lock);
+
+	return regmap_update_bits(lm3554->regmap, LM3554_REG_DURATION,
+				  LM3554_TIMEOUT,
+				  FIELD_PREP(LM3554_TIMEOUT,
+					     timeout / LM3554_TIMEOUT_STEP_US - 1));
+}
+
+static int lm3554_strobe_set(struct led_classdev_flash *fled_cdev, bool state)
+{
+	struct lm3554 *lm3554 = fled_cdev_to_lm3554(fled_cdev);
+	unsigned int flags;
+	int ret;
+
+	guard(mutex)(&lm3554->lock);
+
+	if (!state)
+		return lm3554_set_mode(lm3554, LM3554_MODE_SHUTDOWN);
+
+	/* Clear old flags, the timeout flag would otherwise stay set */
+	ret = regmap_read(lm3554->regmap, LM3554_REG_FLAGS, &flags);
+	if (ret)
+		return ret;
+
+	return lm3554_set_mode(lm3554, LM3554_MODE_FLASH);
+}
+
+static int lm3554_strobe_get(struct led_classdev_flash *fled_cdev, bool *state)
+{
+	struct lm3554 *lm3554 = fled_cdev_to_lm3554(fled_cdev);
+	unsigned int val;
+	int ret;
+
+	guard(mutex)(&lm3554->lock);
+
+	ret = regmap_read(lm3554->regmap, LM3554_REG_TORCH, &val);
+	if (ret)
+		return ret;
+
+	*state = FIELD_GET(LM3554_MODE, val) == LM3554_MODE_FLASH;
+
+	return 0;
+}
+
+static int lm3554_fault_get(struct led_classdev_flash *fled_cdev, u32 *fault)
+{
+	struct lm3554 *lm3554 = fled_cdev_to_lm3554(fled_cdev);
+	unsigned int flags;
+	int ret;
+
+	guard(mutex)(&lm3554->lock);
+
+	/* Reading the flags register clears it */
+	ret = regmap_read(lm3554->regmap, LM3554_REG_FLAGS, &flags);
+	if (ret)
+		return ret;
+
+	*fault = 0;
+	if (flags & LM3554_FLAG_TIMEOUT)
+		*fault |= LED_FAULT_TIMEOUT;
+	if (flags & LM3554_FLAG_THERMAL_SHUTDOWN)
+		*fault |= LED_FAULT_OVER_TEMPERATURE;
+	if (flags & LM3554_FLAG_LED_FAULT)
+		*fault |= LED_FAULT_SHORT_CIRCUIT;
+	if (flags & LM3554_FLAG_LED_THERMAL)
+		*fault |= LED_FAULT_LED_OVER_TEMPERATURE;
+	if (flags & LM3554_FLAG_VIN_MONITOR)
+		*fault |= LED_FAULT_INPUT_VOLTAGE;
+
+	return 0;
+}
+
+static const struct led_flash_ops lm3554_flash_ops = {
+	.flash_brightness_set	= lm3554_flash_brightness_set,
+	.strobe_set		= lm3554_strobe_set,
+	.strobe_get		= lm3554_strobe_get,
+	.timeout_set		= lm3554_timeout_set,
+	.fault_get		= lm3554_fault_get,
+};
+
+/*
+ * The ACPI resources list the STROBE, TORCH and HWEN (enable) GPIOs in this
+ * order, without names.
+ */
+static const struct acpi_gpio_params lm3554_strobe_gpio = { 0, 0, false };
+static const struct acpi_gpio_params lm3554_torch_gpio = { 1, 0, false };
+static const struct acpi_gpio_params lm3554_enable_gpio = { 2, 0, false };
+
+static const struct acpi_gpio_mapping lm3554_acpi_gpios[] = {
+	{ "strobe-gpios", &lm3554_strobe_gpio, 1 },
+	{ "torch-gpios", &lm3554_torch_gpio, 1 },
+	{ "enable-gpios", &lm3554_enable_gpio, 1 },
+	{ }
+};
+
+static void lm3554_power_off(void *data)
+{
+	struct lm3554 *lm3554 = data;
+
+	lm3554_set_mode(lm3554, LM3554_MODE_SHUTDOWN);
+	gpiod_set_value_cansleep(lm3554->enable_gpio, 0);
+}
+
+static int lm3554_probe(struct i2c_client *client)
+{
+	struct device *dev = &client->dev;
+	struct led_flash_setting *setting;
+	struct led_classdev *led_cdev;
+	struct gpio_desc *gpio;
+	struct lm3554 *lm3554;
+	unsigned int val;
+	int ret;
+
+	lm3554 = devm_kzalloc(dev, sizeof(*lm3554), GFP_KERNEL);
+	if (!lm3554)
+		return -ENOMEM;
+
+	ret = devm_mutex_init(dev, &lm3554->lock);
+	if (ret)
+		return ret;
+
+	ret = devm_acpi_dev_add_driver_gpios(dev, lm3554_acpi_gpios);
+	if (ret)
+		return ret;
+
+	/*
+	 * This driver only controls the LED over I2C. Keep the STROBE and
+	 * TORCH inputs low so that they do not turn on the LED.
+	 */
+	gpio = devm_gpiod_get_optional(dev, "strobe", GPIOD_OUT_LOW);
+	if (IS_ERR(gpio))
+		return dev_err_probe(dev, PTR_ERR(gpio), "getting strobe GPIO\n");
+
+	gpio = devm_gpiod_get_optional(dev, "torch", GPIOD_OUT_LOW);
+	if (IS_ERR(gpio))
+		return dev_err_probe(dev, PTR_ERR(gpio), "getting torch GPIO\n");
+
+	lm3554->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH);
+	if (IS_ERR(lm3554->enable_gpio))
+		return dev_err_probe(dev, PTR_ERR(lm3554->enable_gpio),
+				     "getting enable GPIO\n");
+
+	lm3554->regmap = devm_regmap_init_i2c(client, &lm3554_regmap_config);
+	if (IS_ERR(lm3554->regmap))
+		return PTR_ERR(lm3554->regmap);
+
+	ret = devm_add_action_or_reset(dev, lm3554_power_off, lm3554);
+	if (ret)
+		return ret;
+
+	/* Check that the chip is there, this also clears its flags */
+	ret = regmap_read(lm3554->regmap, LM3554_REG_FLAGS, &val);
+	if (ret)
+		return dev_err_probe(dev, ret, "reading flags register\n");
+
+	lm3554->fled_cdev.ops = &lm3554_flash_ops;
+
+	setting = &lm3554->fled_cdev.timeout;
+	setting->min = LM3554_TIMEOUT_MIN_US;
+	setting->max = LM3554_TIMEOUT_MAX_US;
+	setting->step = LM3554_TIMEOUT_STEP_US;
+	setting->val = LM3554_TIMEOUT_MAX_US;
+
+	setting = &lm3554->fled_cdev.brightness;
+	setting->min = lm3554_flash_ua[0];
+	setting->max = LM3554_FLASH_MAX_UA;
+	setting->step = 1;
+	setting->val = LM3554_FLASH_MAX_UA;
+
+	led_cdev = &lm3554->fled_cdev.led_cdev;
+	led_cdev->name = "white:" LED_FUNCTION_FLASH;
+	led_cdev->max_brightness = LM3554_TORCH_LEVELS;
+	led_cdev->brightness_set_blocking = lm3554_brightness_set;
+	led_cdev->flags |= LED_DEV_CAP_FLASH | LED_CORE_SUSPENDRESUME;
+
+	return devm_led_classdev_flash_register(dev, &lm3554->fled_cdev);
+}
+
+static const struct acpi_device_id lm3554_acpi_match[] = {
+	{ "INTCF1C" },
+	{ }
+};
+MODULE_DEVICE_TABLE(acpi, lm3554_acpi_match);
+
+static struct i2c_driver lm3554_driver = {
+	.driver = {
+		.name = "leds-lm3554",
+		.acpi_match_table = lm3554_acpi_match,
+	},
+	.probe = lm3554_probe,
+};
+module_i2c_driver(lm3554_driver);
+
+MODULE_DESCRIPTION("Texas Instruments LM3554 flash LED driver");
+MODULE_AUTHOR("Volodymyr Chapman <volodymyrchapman@gmail.com>");
+MODULE_LICENSE("GPL");

base-commit: 3fc9303dba01676c2090768126e5327e341b7761
-- 
2.43.0


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

* Re: [PATCH] leds: flash: Add driver for the TI LM3554
  2026-11-16 18:08 [PATCH] leds: flash: Add driver for the TI LM3554 Volodymyr Chapman
@ 2026-11-17 14:14 ` Uwe Kleine-König
  0 siblings, 0 replies; 2+ messages in thread
From: Uwe Kleine-König @ 2026-11-17 14:14 UTC (permalink / raw)
  To: Volodymyr Chapman
  Cc: Lee Jones, Pavel Machek, Griffin Kroah-Hartman, Hans de Goede,
	linux-leds, linux-kernel

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

On Mon, Nov 16, 2026 at 06:08:09PM +0000, Volodymyr Chapman wrote:
> +#include <linux/i2c.h>
> [..]
> +#include <linux/mod_devicetable.h>

Don't include <linux/mod_devicetable.h>. <linux/i2c.h> already provides
acpi_device_id, so you can just drop <linux/mod_devicetable.h>.

> [...]
> +static const struct acpi_device_id lm3554_acpi_match[] = {
> +	{ "INTCF1C" },

Please make this:

	{ .id = "INTCF1C" },

> +	{ }
> +};
> +MODULE_DEVICE_TABLE(acpi, lm3554_acpi_match);

Best regards
Uwe

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

end of thread, other threads:[~2026-11-17 14:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 18:08 [PATCH] leds: flash: Add driver for the TI LM3554 Volodymyr Chapman
2026-11-17 14:14 ` Uwe Kleine-König

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®