Skip to content

Commit c7dd343

Browse files
Lukas Timmermannlag-linaro
authored andcommitted
leds: as3668: Driver for the ams Osram 4-channel i2c LED driver
Since there were no existing drivers for the AS3668 or related devices, a new driver was introduced in a separate file. Similar devices were reviewed, but none shared enough characteristics to justify code reuse. As a result, this driver is written specifically for the AS3668. Signed-off-by: Lukas Timmermann <linux@timmermann.space> Link: https://patch.msgid.link/20260118165010.902086-3-linux@timmermann.space Signed-off-by: Lee Jones <lee@kernel.org>
1 parent 9339608 commit c7dd343

4 files changed

Lines changed: 217 additions & 0 deletions

File tree

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3791,6 +3791,7 @@ M: Lukas Timmermann <linux@timmermann.space>
37913791
L: linux-leds@vger.kernel.org
37923792
S: Maintained
37933793
F: Documentation/devicetree/bindings/leds/ams,as3668.yaml
3794+
F: drivers/leds/leds-as3668.c
37943795

37953796
ASAHI KASEI AK7375 LENS VOICE COIL DRIVER
37963797
M: Tianshu Qiu <tian.shu.qiu@intel.com>

drivers/leds/Kconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ config LEDS_ARIEL
107107

108108
Say Y to if your machine is a Dell Wyse 3020 thin client.
109109

110+
config LEDS_OSRAM_AMS_AS3668
111+
tristate "LED support for Osram AMS AS3668"
112+
depends on LEDS_CLASS
113+
depends on I2C
114+
help
115+
This option enables support for the Osram AMS AS3668 LED controller.
116+
The AS3668 provides up to four LED channels and is controlled via
117+
the I2C bus. This driver offers basic brightness control for each
118+
channel, without support for blinking or other advanced features.
119+
120+
To compile this driver as a module, choose M here: the module
121+
will be called leds-as3668.
122+
110123
config LEDS_AW200XX
111124
tristate "LED support for Awinic AW20036/AW20054/AW20072/AW20108"
112125
depends on LEDS_CLASS

drivers/leds/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ obj-$(CONFIG_LEDS_ADP5520) += leds-adp5520.o
1515
obj-$(CONFIG_LEDS_AN30259A) += leds-an30259a.o
1616
obj-$(CONFIG_LEDS_APU) += leds-apu.o
1717
obj-$(CONFIG_LEDS_ARIEL) += leds-ariel.o
18+
obj-$(CONFIG_LEDS_AS3668) += leds-as3668.o
1819
obj-$(CONFIG_LEDS_AW200XX) += leds-aw200xx.o
1920
obj-$(CONFIG_LEDS_AW2013) += leds-aw2013.o
2021
obj-$(CONFIG_LEDS_BCM6328) += leds-bcm6328.o

drivers/leds/leds-as3668.c

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Osram AMS AS3668 LED Driver IC
4+
*
5+
* Copyright (C) 2025 Lukas Timmermann <linux@timmermann.space>
6+
*/
7+
8+
#include <linux/bitfield.h>
9+
#include <linux/i2c.h>
10+
#include <linux/leds.h>
11+
#include <linux/module.h>
12+
#include <linux/uleds.h>
13+
14+
#define AS3668_MAX_LEDS 4
15+
16+
/* Chip Ident */
17+
18+
#define AS3668_CHIP_ID1_REG 0x3e
19+
#define AS3668_CHIP_ID 0xa5
20+
21+
/* Current Control */
22+
23+
#define AS3668_CURR_MODE_REG 0x01
24+
#define AS3668_CURR_MODE_OFF 0x0
25+
#define AS3668_CURR_MODE_ON 0x1
26+
#define AS3668_CURR1_MODE_MASK GENMASK(1, 0)
27+
#define AS3668_CURR2_MODE_MASK GENMASK(3, 2)
28+
#define AS3668_CURR3_MODE_MASK GENMASK(5, 4)
29+
#define AS3668_CURR4_MODE_MASK GENMASK(7, 6)
30+
#define AS3668_CURR1_REG 0x02
31+
#define AS3668_CURR2_REG 0x03
32+
#define AS3668_CURR3_REG 0x04
33+
#define AS3668_CURR4_REG 0x05
34+
35+
#define AS3668_CURR_MODE_PACK(mode) (((mode) << 0) | \
36+
((mode) << 2) | \
37+
((mode) << 4) | \
38+
((mode) << 6))
39+
40+
struct as3668_led {
41+
struct led_classdev cdev;
42+
struct as3668 *chip;
43+
struct fwnode_handle *fwnode;
44+
u8 mode_mask;
45+
u8 current_reg;
46+
};
47+
48+
struct as3668 {
49+
struct i2c_client *client;
50+
struct as3668_led leds[AS3668_MAX_LEDS];
51+
};
52+
53+
static int as3668_channel_mode_set(struct as3668_led *led, u8 mode)
54+
{
55+
int ret;
56+
u8 channel_modes;
57+
58+
ret = i2c_smbus_read_byte_data(led->chip->client, AS3668_CURR_MODE_REG);
59+
if (ret < 0) {
60+
dev_err(led->cdev.dev, "failed to read channel modes\n");
61+
return ret;
62+
}
63+
channel_modes = (u8)ret;
64+
65+
channel_modes &= ~led->mode_mask;
66+
channel_modes |= led->mode_mask & (AS3668_CURR_MODE_PACK(mode));
67+
68+
return i2c_smbus_write_byte_data(led->chip->client, AS3668_CURR_MODE_REG, channel_modes);
69+
}
70+
71+
static enum led_brightness as3668_brightness_get(struct led_classdev *cdev)
72+
{
73+
struct as3668_led *led = container_of(cdev, struct as3668_led, cdev);
74+
75+
return i2c_smbus_read_byte_data(led->chip->client, led->current_reg);
76+
}
77+
78+
static void as3668_brightness_set(struct led_classdev *cdev, enum led_brightness brightness)
79+
{
80+
struct as3668_led *led = container_of(cdev, struct as3668_led, cdev);
81+
int err;
82+
83+
err = as3668_channel_mode_set(led, !!brightness);
84+
if (err)
85+
dev_err(cdev->dev, "failed to set channel mode: %d\n", err);
86+
87+
err = i2c_smbus_write_byte_data(led->chip->client, led->current_reg, brightness);
88+
if (err)
89+
dev_err(cdev->dev, "failed to set brightness: %d\n", err);
90+
}
91+
92+
static int as3668_dt_init(struct as3668 *as3668)
93+
{
94+
struct device *dev = &as3668->client->dev;
95+
struct as3668_led *led;
96+
struct led_init_data init_data = {};
97+
int err;
98+
u32 reg;
99+
100+
for_each_available_child_of_node_scoped(dev_of_node(dev), child) {
101+
err = of_property_read_u32(child, "reg", &reg);
102+
if (err)
103+
return dev_err_probe(dev, err, "failed to read 'reg' property");
104+
105+
if (reg < 0 || reg >= AS3668_MAX_LEDS)
106+
return dev_err_probe(dev, -EINVAL,
107+
"unsupported LED: %d\n", reg);
108+
109+
led = &as3668->leds[reg];
110+
led->fwnode = of_fwnode_handle(child);
111+
112+
led->current_reg = reg + AS3668_CURR1_REG;
113+
led->mode_mask = AS3668_CURR1_MODE_MASK << (reg * 2);
114+
led->chip = as3668;
115+
116+
led->cdev.max_brightness = U8_MAX;
117+
led->cdev.brightness_get = as3668_brightness_get;
118+
led->cdev.brightness_set = as3668_brightness_set;
119+
120+
init_data.fwnode = led->fwnode;
121+
init_data.default_label = ":";
122+
123+
err = devm_led_classdev_register_ext(dev, &led->cdev, &init_data);
124+
if (err)
125+
return dev_err_probe(dev, err, "failed to register LED %d\n", reg);
126+
}
127+
128+
return 0;
129+
}
130+
131+
static int as3668_probe(struct i2c_client *client)
132+
{
133+
struct as3668 *as3668;
134+
int err;
135+
u8 chip_id;
136+
137+
chip_id = i2c_smbus_read_byte_data(client, AS3668_CHIP_ID1_REG);
138+
if (chip_id != AS3668_CHIP_ID)
139+
return dev_err_probe(&client->dev, -ENODEV,
140+
"expected chip ID 0x%02x, got 0x%02x\n",
141+
AS3668_CHIP_ID, chip_id);
142+
143+
as3668 = devm_kzalloc(&client->dev, sizeof(*as3668), GFP_KERNEL);
144+
if (!as3668)
145+
return -ENOMEM;
146+
147+
as3668->client = client;
148+
149+
err = as3668_dt_init(as3668);
150+
if (err)
151+
return err;
152+
153+
/* Set all four channel modes to 'off' */
154+
err = i2c_smbus_write_byte_data(client, AS3668_CURR_MODE_REG,
155+
FIELD_PREP(AS3668_CURR1_MODE_MASK, AS3668_CURR_MODE_OFF) |
156+
FIELD_PREP(AS3668_CURR2_MODE_MASK, AS3668_CURR_MODE_OFF) |
157+
FIELD_PREP(AS3668_CURR3_MODE_MASK, AS3668_CURR_MODE_OFF) |
158+
FIELD_PREP(AS3668_CURR4_MODE_MASK, AS3668_CURR_MODE_OFF));
159+
160+
/* Set initial currents to 0mA */
161+
err |= i2c_smbus_write_byte_data(client, AS3668_CURR1_REG, 0);
162+
err |= i2c_smbus_write_byte_data(client, AS3668_CURR2_REG, 0);
163+
err |= i2c_smbus_write_byte_data(client, AS3668_CURR3_REG, 0);
164+
err |= i2c_smbus_write_byte_data(client, AS3668_CURR4_REG, 0);
165+
166+
if (err)
167+
return dev_err_probe(&client->dev, -EIO, "failed to set zero initial current levels\n");
168+
169+
return 0;
170+
}
171+
172+
static void as3668_remove(struct i2c_client *client)
173+
{
174+
i2c_smbus_write_byte_data(client, AS3668_CURR_MODE_REG, 0);
175+
}
176+
177+
static const struct i2c_device_id as3668_idtable[] = {
178+
{ "as3668" },
179+
{ }
180+
};
181+
MODULE_DEVICE_TABLE(i2c, as3668_idtable);
182+
183+
static const struct of_device_id as3668_match_table[] = {
184+
{ .compatible = "ams,as3668" },
185+
{ }
186+
};
187+
MODULE_DEVICE_TABLE(of, as3668_match_table);
188+
189+
static struct i2c_driver as3668_driver = {
190+
.driver = {
191+
.name = "leds_as3668",
192+
.of_match_table = as3668_match_table,
193+
},
194+
.probe = as3668_probe,
195+
.remove = as3668_remove,
196+
.id_table = as3668_idtable,
197+
};
198+
module_i2c_driver(as3668_driver);
199+
200+
MODULE_AUTHOR("Lukas Timmermann <linux@timmermann.space>");
201+
MODULE_DESCRIPTION("AS3668 LED driver");
202+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)