1 // SPDX-License-Identifier: GPL-2.0+
3 // Driver for Panasonic AN30259A 3-channel LED driver
5 // Copyright (c) 2018 Simon Shields <simon@lineageos.org>
8 // https://www.alliedelec.com/m/d/a9d2b3ee87c2d1a535a41dd747b1c247.pdf
10 #include <linux/i2c.h>
11 #include <linux/leds.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
15 #include <linux/regmap.h>
17 #define AN30259A_MAX_LEDS 3
19 #define AN30259A_REG_SRESET 0x00
20 #define AN30259A_LED_SRESET BIT(0)
22 /* LED power registers */
23 #define AN30259A_REG_LED_ON 0x01
24 #define AN30259A_LED_EN(x) BIT((x) - 1)
25 #define AN30259A_LED_SLOPE(x) BIT(((x) - 1) + 4)
27 #define AN30259A_REG_LEDCC(x) (0x03 + ((x) - 1))
29 /* slope control registers */
30 #define AN30259A_REG_SLOPE(x) (0x06 + ((x) - 1))
31 #define AN30259A_LED_SLOPETIME1(x) (x)
32 #define AN30259A_LED_SLOPETIME2(x) ((x) << 4)
34 #define AN30259A_REG_LEDCNT1(x) (0x09 + (4 * ((x) - 1)))
35 #define AN30259A_LED_DUTYMAX(x) ((x) << 4)
36 #define AN30259A_LED_DUTYMID(x) (x)
38 #define AN30259A_REG_LEDCNT2(x) (0x0A + (4 * ((x) - 1)))
39 #define AN30259A_LED_DELAY(x) ((x) << 4)
40 #define AN30259A_LED_DUTYMIN(x) (x)
42 /* detention time control (length of each slope step) */
43 #define AN30259A_REG_LEDCNT3(x) (0x0B + (4 * ((x) - 1)))
44 #define AN30259A_LED_DT1(x) (x)
45 #define AN30259A_LED_DT2(x) ((x) << 4)
47 #define AN30259A_REG_LEDCNT4(x) (0x0C + (4 * ((x) - 1)))
48 #define AN30259A_LED_DT3(x) (x)
49 #define AN30259A_LED_DT4(x) ((x) << 4)
51 #define AN30259A_REG_MAX 0x14
53 #define AN30259A_BLINK_MAX_TIME 7500 /* ms */
54 #define AN30259A_SLOPE_RESOLUTION 500 /* ms */
56 #define AN30259A_NAME "an30259a"
61 struct an30259a *chip;
62 struct fwnode_handle *fwnode;
63 struct led_classdev cdev;
65 enum led_default_state default_state;
70 struct mutex mutex; /* held when writing to registers */
71 struct i2c_client *client;
72 struct an30259a_led leds[AN30259A_MAX_LEDS];
73 struct regmap *regmap;
77 static int an30259a_brightness_set(struct led_classdev *cdev,
78 enum led_brightness brightness)
80 struct an30259a_led *led;
84 led = container_of(cdev, struct an30259a_led, cdev);
85 mutex_lock(&led->chip->mutex);
87 ret = regmap_read(led->chip->regmap, AN30259A_REG_LED_ON, &led_on);
93 led_on &= ~AN30259A_LED_EN(led->num);
94 led_on &= ~AN30259A_LED_SLOPE(led->num);
98 led_on |= AN30259A_LED_EN(led->num);
100 led_on |= AN30259A_LED_SLOPE(led->num);
101 ret = regmap_write(led->chip->regmap,
102 AN30259A_REG_LEDCNT1(led->num),
103 AN30259A_LED_DUTYMAX(0xf) |
104 AN30259A_LED_DUTYMID(0xf));
110 ret = regmap_write(led->chip->regmap, AN30259A_REG_LED_ON, led_on);
114 ret = regmap_write(led->chip->regmap, AN30259A_REG_LEDCC(led->num),
118 mutex_unlock(&led->chip->mutex);
123 static int an30259a_blink_set(struct led_classdev *cdev,
124 unsigned long *delay_off, unsigned long *delay_on)
126 struct an30259a_led *led;
129 unsigned long off = *delay_off, on = *delay_on;
131 led = container_of(cdev, struct an30259a_led, cdev);
133 mutex_lock(&led->chip->mutex);
136 /* slope time can only be a multiple of 500ms. */
137 if (off % AN30259A_SLOPE_RESOLUTION || on % AN30259A_SLOPE_RESOLUTION) {
142 /* up to a maximum of 7500ms. */
143 if (off > AN30259A_BLINK_MAX_TIME || on > AN30259A_BLINK_MAX_TIME) {
148 /* if no blink specified, default to 1 Hz. */
150 *delay_off = off = 500;
151 *delay_on = on = 500;
154 /* convert into values the HW will understand. */
155 off /= AN30259A_SLOPE_RESOLUTION;
156 on /= AN30259A_SLOPE_RESOLUTION;
158 /* duty min should be zero (=off), delay should be zero. */
159 ret = regmap_write(led->chip->regmap, AN30259A_REG_LEDCNT2(num),
160 AN30259A_LED_DELAY(0) | AN30259A_LED_DUTYMIN(0));
164 /* reset detention time (no "breathing" effect). */
165 ret = regmap_write(led->chip->regmap, AN30259A_REG_LEDCNT3(num),
166 AN30259A_LED_DT1(0) | AN30259A_LED_DT2(0));
169 ret = regmap_write(led->chip->regmap, AN30259A_REG_LEDCNT4(num),
170 AN30259A_LED_DT3(0) | AN30259A_LED_DT4(0));
174 /* slope time controls on/off cycle length. */
175 ret = regmap_write(led->chip->regmap, AN30259A_REG_SLOPE(num),
176 AN30259A_LED_SLOPETIME1(off) |
177 AN30259A_LED_SLOPETIME2(on));
181 /* Finally, enable slope mode. */
182 ret = regmap_read(led->chip->regmap, AN30259A_REG_LED_ON, &led_on);
186 led_on |= AN30259A_LED_SLOPE(num) | AN30259A_LED_EN(led->num);
188 ret = regmap_write(led->chip->regmap, AN30259A_REG_LED_ON, led_on);
193 mutex_unlock(&led->chip->mutex);
198 static int an30259a_dt_init(struct i2c_client *client,
199 struct an30259a *chip)
201 struct device_node *np = dev_of_node(&client->dev), *child;
204 struct an30259a_led *led;
206 count = of_get_available_child_count(np);
207 if (!count || count > AN30259A_MAX_LEDS)
210 for_each_available_child_of_node(np, child) {
213 ret = of_property_read_u32(child, "reg", &source);
214 if (ret != 0 || !source || source > AN30259A_MAX_LEDS) {
215 dev_err(&client->dev, "Couldn't read LED address: %d\n",
221 led = &chip->leds[i];
225 led->fwnode = of_fwnode_handle(child);
226 led->default_state = led_init_default_state_get(led->fwnode);
239 static const struct regmap_config an30259a_regmap_config = {
242 .max_register = AN30259A_REG_MAX,
245 static void an30259a_init_default_state(struct an30259a_led *led)
247 struct an30259a *chip = led->chip;
250 switch (led->default_state) {
251 case LEDS_DEFSTATE_ON:
252 led->cdev.brightness = LED_FULL;
254 case LEDS_DEFSTATE_KEEP:
255 err = regmap_read(chip->regmap, AN30259A_REG_LED_ON, &led_on);
259 if (!(led_on & AN30259A_LED_EN(led->num))) {
260 led->cdev.brightness = LED_OFF;
263 regmap_read(chip->regmap, AN30259A_REG_LEDCC(led->num),
264 &led->cdev.brightness);
267 led->cdev.brightness = LED_OFF;
270 an30259a_brightness_set(&led->cdev, led->cdev.brightness);
273 static int an30259a_probe(struct i2c_client *client)
275 struct an30259a *chip;
278 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
282 err = an30259a_dt_init(client, chip);
286 mutex_init(&chip->mutex);
287 chip->client = client;
288 i2c_set_clientdata(client, chip);
290 chip->regmap = devm_regmap_init_i2c(client, &an30259a_regmap_config);
292 if (IS_ERR(chip->regmap)) {
293 err = PTR_ERR(chip->regmap);
294 dev_err(&client->dev, "Failed to allocate register map: %d\n",
299 for (i = 0; i < chip->num_leds; i++) {
300 struct led_init_data init_data = {};
302 an30259a_init_default_state(&chip->leds[i]);
303 chip->leds[i].cdev.brightness_set_blocking =
304 an30259a_brightness_set;
305 chip->leds[i].cdev.blink_set = an30259a_blink_set;
307 init_data.fwnode = chip->leds[i].fwnode;
308 init_data.devicename = AN30259A_NAME;
309 init_data.default_label = ":";
311 err = devm_led_classdev_register_ext(&client->dev,
320 mutex_destroy(&chip->mutex);
324 static void an30259a_remove(struct i2c_client *client)
326 struct an30259a *chip = i2c_get_clientdata(client);
328 mutex_destroy(&chip->mutex);
331 static const struct of_device_id an30259a_match_table[] = {
332 { .compatible = "panasonic,an30259a", },
336 MODULE_DEVICE_TABLE(of, an30259a_match_table);
338 static const struct i2c_device_id an30259a_id[] = {
342 MODULE_DEVICE_TABLE(i2c, an30259a_id);
344 static struct i2c_driver an30259a_driver = {
346 .name = "leds-an30259a",
347 .of_match_table = of_match_ptr(an30259a_match_table),
349 .probe = an30259a_probe,
350 .remove = an30259a_remove,
351 .id_table = an30259a_id,
354 module_i2c_driver(an30259a_driver);
356 MODULE_AUTHOR("Simon Shields <simon@lineageos.org>");
357 MODULE_DESCRIPTION("AN30259A LED driver");
358 MODULE_LICENSE("GPL v2");