1 // SPDX-License-Identifier: GPL-2.0
2 // TI LP50XX LED chip family driver
3 // Copyright (C) 2018-20 Texas Instruments Incorporated - https://www.ti.com/
5 #include <linux/gpio/consumer.h>
7 #include <linux/init.h>
8 #include <linux/leds.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/regmap.h>
13 #include <linux/regulator/consumer.h>
14 #include <linux/slab.h>
15 #include <uapi/linux/uleds.h>
17 #include <linux/led-class-multicolor.h>
21 #define LP50XX_DEV_CFG0 0x00
22 #define LP50XX_DEV_CFG1 0x01
23 #define LP50XX_LED_CFG0 0x02
25 /* LP5009 and LP5012 registers */
26 #define LP5012_BNK_BRT 0x03
27 #define LP5012_BNKA_CLR 0x04
28 #define LP5012_BNKB_CLR 0x05
29 #define LP5012_BNKC_CLR 0x06
30 #define LP5012_LED0_BRT 0x07
31 #define LP5012_OUT0_CLR 0x0b
32 #define LP5012_RESET 0x17
34 /* LP5018 and LP5024 registers */
35 #define LP5024_BNK_BRT 0x03
36 #define LP5024_BNKA_CLR 0x04
37 #define LP5024_BNKB_CLR 0x05
38 #define LP5024_BNKC_CLR 0x06
39 #define LP5024_LED0_BRT 0x07
40 #define LP5024_OUT0_CLR 0x0f
41 #define LP5024_RESET 0x27
43 /* LP5030 and LP5036 registers */
44 #define LP5036_LED_CFG1 0x03
45 #define LP5036_BNK_BRT 0x04
46 #define LP5036_BNKA_CLR 0x05
47 #define LP5036_BNKB_CLR 0x06
48 #define LP5036_BNKC_CLR 0x07
49 #define LP5036_LED0_BRT 0x08
50 #define LP5036_OUT0_CLR 0x14
51 #define LP5036_RESET 0x38
53 #define LP50XX_SW_RESET 0xff
54 #define LP50XX_CHIP_EN BIT(6)
56 /* There are 3 LED outputs per bank */
57 #define LP50XX_LEDS_PER_MODULE 3
59 #define LP5009_MAX_LED_MODULES 2
60 #define LP5012_MAX_LED_MODULES 4
61 #define LP5018_MAX_LED_MODULES 6
62 #define LP5024_MAX_LED_MODULES 8
63 #define LP5030_MAX_LED_MODULES 10
64 #define LP5036_MAX_LED_MODULES 12
66 static const struct reg_default lp5012_reg_defs[] = {
67 {LP50XX_DEV_CFG0, 0x0},
68 {LP50XX_DEV_CFG1, 0x3c},
69 {LP50XX_LED_CFG0, 0x0},
70 {LP5012_BNK_BRT, 0xff},
71 {LP5012_BNKA_CLR, 0x0f},
72 {LP5012_BNKB_CLR, 0x0f},
73 {LP5012_BNKC_CLR, 0x0f},
74 {LP5012_LED0_BRT, 0x0f},
75 /* LEDX_BRT registers are all 0xff for defaults */
76 {0x08, 0xff}, {0x09, 0xff}, {0x0a, 0xff},
77 {LP5012_OUT0_CLR, 0x0f},
78 /* OUTX_CLR registers are all 0x0 for defaults */
79 {0x0c, 0x00}, {0x0d, 0x00}, {0x0e, 0x00}, {0x0f, 0x00}, {0x10, 0x00},
80 {0x11, 0x00}, {0x12, 0x00}, {0x13, 0x00}, {0x14, 0x00}, {0x15, 0x00},
85 static const struct reg_default lp5024_reg_defs[] = {
86 {LP50XX_DEV_CFG0, 0x0},
87 {LP50XX_DEV_CFG1, 0x3c},
88 {LP50XX_LED_CFG0, 0x0},
89 {LP5024_BNK_BRT, 0xff},
90 {LP5024_BNKA_CLR, 0x0f},
91 {LP5024_BNKB_CLR, 0x0f},
92 {LP5024_BNKC_CLR, 0x0f},
93 {LP5024_LED0_BRT, 0x0f},
94 /* LEDX_BRT registers are all 0xff for defaults */
95 {0x08, 0xff}, {0x09, 0xff}, {0x0a, 0xff}, {0x0b, 0xff}, {0x0c, 0xff},
96 {0x0d, 0xff}, {0x0e, 0xff},
97 {LP5024_OUT0_CLR, 0x0f},
98 /* OUTX_CLR registers are all 0x0 for defaults */
99 {0x10, 0x00}, {0x11, 0x00}, {0x12, 0x00}, {0x13, 0x00}, {0x14, 0x00},
100 {0x15, 0x00}, {0x16, 0x00}, {0x17, 0x00}, {0x18, 0x00}, {0x19, 0x00},
101 {0x1a, 0x00}, {0x1b, 0x00}, {0x1c, 0x00}, {0x1d, 0x00}, {0x1e, 0x00},
102 {0x1f, 0x00}, {0x20, 0x00}, {0x21, 0x00}, {0x22, 0x00}, {0x23, 0x00},
103 {0x24, 0x00}, {0x25, 0x00}, {0x26, 0x00},
107 static const struct reg_default lp5036_reg_defs[] = {
108 {LP50XX_DEV_CFG0, 0x0},
109 {LP50XX_DEV_CFG1, 0x3c},
110 {LP50XX_LED_CFG0, 0x0},
111 {LP5036_LED_CFG1, 0x0},
112 {LP5036_BNK_BRT, 0xff},
113 {LP5036_BNKA_CLR, 0x0f},
114 {LP5036_BNKB_CLR, 0x0f},
115 {LP5036_BNKC_CLR, 0x0f},
116 {LP5036_LED0_BRT, 0x0f},
117 /* LEDX_BRT registers are all 0xff for defaults */
118 {0x08, 0xff}, {0x09, 0xff}, {0x0a, 0xff}, {0x0b, 0xff}, {0x0c, 0xff},
119 {0x0d, 0xff}, {0x0e, 0xff}, {0x0f, 0xff}, {0x10, 0xff}, {0x11, 0xff},
120 {0x12, 0xff}, {0x13, 0xff},
121 {LP5036_OUT0_CLR, 0x0f},
122 /* OUTX_CLR registers are all 0x0 for defaults */
123 {0x15, 0x00}, {0x16, 0x00}, {0x17, 0x00}, {0x18, 0x00}, {0x19, 0x00},
124 {0x1a, 0x00}, {0x1b, 0x00}, {0x1c, 0x00}, {0x1d, 0x00}, {0x1e, 0x00},
125 {0x1f, 0x00}, {0x20, 0x00}, {0x21, 0x00}, {0x22, 0x00}, {0x23, 0x00},
126 {0x24, 0x00}, {0x25, 0x00}, {0x26, 0x00}, {0x27, 0x00}, {0x28, 0x00},
127 {0x29, 0x00}, {0x2a, 0x00}, {0x2b, 0x00}, {0x2c, 0x00}, {0x2d, 0x00},
128 {0x2e, 0x00}, {0x2f, 0x00}, {0x30, 0x00}, {0x31, 0x00}, {0x32, 0x00},
129 {0x33, 0x00}, {0x34, 0x00}, {0x35, 0x00}, {0x36, 0x00}, {0x37, 0x00},
133 static const struct regmap_config lp5012_regmap_config = {
137 .max_register = LP5012_RESET,
138 .reg_defaults = lp5012_reg_defs,
139 .num_reg_defaults = ARRAY_SIZE(lp5012_reg_defs),
140 .cache_type = REGCACHE_FLAT,
143 static const struct regmap_config lp5024_regmap_config = {
147 .max_register = LP5024_RESET,
148 .reg_defaults = lp5024_reg_defs,
149 .num_reg_defaults = ARRAY_SIZE(lp5024_reg_defs),
150 .cache_type = REGCACHE_FLAT,
153 static const struct regmap_config lp5036_regmap_config = {
157 .max_register = LP5036_RESET,
158 .reg_defaults = lp5036_reg_defs,
159 .num_reg_defaults = ARRAY_SIZE(lp5036_reg_defs),
160 .cache_type = REGCACHE_FLAT,
173 * struct lp50xx_chip_info -
174 * @lp50xx_regmap_config: regmap register configuration
175 * @model_id: LED device model
176 * @max_modules: total number of supported LED modules
177 * @num_leds: number of LED outputs available on the device
178 * @led_brightness0_reg: first brightness register of the device
179 * @mix_out0_reg: first color mix register of the device
180 * @bank_brt_reg: bank brightness register
181 * @bank_mix_reg: color mix register
182 * @reset_reg: device reset register
184 struct lp50xx_chip_info {
185 const struct regmap_config *lp50xx_regmap_config;
189 u8 led_brightness0_reg;
196 static const struct lp50xx_chip_info lp50xx_chip_info_tbl[] = {
199 .max_modules = LP5009_MAX_LED_MODULES,
200 .num_leds = LP5009_MAX_LED_MODULES * LP50XX_LEDS_PER_MODULE,
201 .led_brightness0_reg = LP5012_LED0_BRT,
202 .mix_out0_reg = LP5012_OUT0_CLR,
203 .bank_brt_reg = LP5012_BNK_BRT,
204 .bank_mix_reg = LP5012_BNKA_CLR,
205 .reset_reg = LP5012_RESET,
206 .lp50xx_regmap_config = &lp5012_regmap_config,
210 .max_modules = LP5012_MAX_LED_MODULES,
211 .num_leds = LP5012_MAX_LED_MODULES * LP50XX_LEDS_PER_MODULE,
212 .led_brightness0_reg = LP5012_LED0_BRT,
213 .mix_out0_reg = LP5012_OUT0_CLR,
214 .bank_brt_reg = LP5012_BNK_BRT,
215 .bank_mix_reg = LP5012_BNKA_CLR,
216 .reset_reg = LP5012_RESET,
217 .lp50xx_regmap_config = &lp5012_regmap_config,
221 .max_modules = LP5018_MAX_LED_MODULES,
222 .num_leds = LP5018_MAX_LED_MODULES * LP50XX_LEDS_PER_MODULE,
223 .led_brightness0_reg = LP5024_LED0_BRT,
224 .mix_out0_reg = LP5024_OUT0_CLR,
225 .bank_brt_reg = LP5024_BNK_BRT,
226 .bank_mix_reg = LP5024_BNKA_CLR,
227 .reset_reg = LP5024_RESET,
228 .lp50xx_regmap_config = &lp5024_regmap_config,
232 .max_modules = LP5024_MAX_LED_MODULES,
233 .num_leds = LP5024_MAX_LED_MODULES * LP50XX_LEDS_PER_MODULE,
234 .led_brightness0_reg = LP5024_LED0_BRT,
235 .mix_out0_reg = LP5024_OUT0_CLR,
236 .bank_brt_reg = LP5024_BNK_BRT,
237 .bank_mix_reg = LP5024_BNKA_CLR,
238 .reset_reg = LP5024_RESET,
239 .lp50xx_regmap_config = &lp5024_regmap_config,
243 .max_modules = LP5030_MAX_LED_MODULES,
244 .num_leds = LP5030_MAX_LED_MODULES * LP50XX_LEDS_PER_MODULE,
245 .led_brightness0_reg = LP5036_LED0_BRT,
246 .mix_out0_reg = LP5036_OUT0_CLR,
247 .bank_brt_reg = LP5036_BNK_BRT,
248 .bank_mix_reg = LP5036_BNKA_CLR,
249 .reset_reg = LP5036_RESET,
250 .lp50xx_regmap_config = &lp5036_regmap_config,
254 .max_modules = LP5036_MAX_LED_MODULES,
255 .num_leds = LP5036_MAX_LED_MODULES * LP50XX_LEDS_PER_MODULE,
256 .led_brightness0_reg = LP5036_LED0_BRT,
257 .mix_out0_reg = LP5036_OUT0_CLR,
258 .bank_brt_reg = LP5036_BNK_BRT,
259 .bank_mix_reg = LP5036_BNKA_CLR,
260 .reset_reg = LP5036_RESET,
261 .lp50xx_regmap_config = &lp5036_regmap_config,
266 struct led_classdev_mc mc_cdev;
268 unsigned long bank_modules;
269 int led_intensity[LP50XX_LEDS_PER_MODULE];
270 u8 ctrl_bank_enabled;
276 * @enable_gpio: hardware enable gpio
277 * @regulator: LED supply regulator pointer
278 * @client: pointer to the I2C client
279 * @regmap: device register map
280 * @dev: pointer to the devices device struct
281 * @lock: lock for reading/writing the device
282 * @chip_info: chip specific information (ie num_leds)
283 * @num_of_banked_leds: holds the number of banked LEDs
284 * @leds: array of LED strings
287 struct gpio_desc *enable_gpio;
288 struct regulator *regulator;
289 struct i2c_client *client;
290 struct regmap *regmap;
293 const struct lp50xx_chip_info *chip_info;
294 int num_of_banked_leds;
296 /* This needs to be at the end of the struct */
297 struct lp50xx_led leds[];
300 static struct lp50xx_led *mcled_cdev_to_led(struct led_classdev_mc *mc_cdev)
302 return container_of(mc_cdev, struct lp50xx_led, mc_cdev);
305 static int lp50xx_brightness_set(struct led_classdev *cdev,
306 enum led_brightness brightness)
308 struct led_classdev_mc *mc_dev = lcdev_to_mccdev(cdev);
309 struct lp50xx_led *led = mcled_cdev_to_led(mc_dev);
310 const struct lp50xx_chip_info *led_chip = led->priv->chip_info;
311 u8 led_offset, reg_val;
315 mutex_lock(&led->priv->lock);
316 if (led->ctrl_bank_enabled)
317 reg_val = led_chip->bank_brt_reg;
319 reg_val = led_chip->led_brightness0_reg +
322 ret = regmap_write(led->priv->regmap, reg_val, brightness);
324 dev_err(led->priv->dev,
325 "Cannot write brightness value %d\n", ret);
329 for (i = 0; i < led->mc_cdev.num_colors; i++) {
330 if (led->ctrl_bank_enabled) {
331 reg_val = led_chip->bank_mix_reg + i;
333 led_offset = (led->led_number * 3) + i;
334 reg_val = led_chip->mix_out0_reg + led_offset;
337 ret = regmap_write(led->priv->regmap, reg_val,
338 mc_dev->subled_info[i].intensity);
340 dev_err(led->priv->dev,
341 "Cannot write intensity value %d\n", ret);
346 mutex_unlock(&led->priv->lock);
350 static int lp50xx_set_banks(struct lp50xx *priv, u32 led_banks[])
352 u8 led_config_lo, led_config_hi;
353 u32 bank_enable_mask = 0;
357 for (i = 0; i < priv->chip_info->max_modules; i++) {
359 bank_enable_mask |= (1 << led_banks[i]);
362 led_config_lo = bank_enable_mask;
363 led_config_hi = bank_enable_mask >> 8;
365 ret = regmap_write(priv->regmap, LP50XX_LED_CFG0, led_config_lo);
369 if (priv->chip_info->model_id >= LP5030)
370 ret = regmap_write(priv->regmap, LP5036_LED_CFG1, led_config_hi);
375 static int lp50xx_reset(struct lp50xx *priv)
377 return regmap_write(priv->regmap, priv->chip_info->reset_reg, LP50XX_SW_RESET);
380 static int lp50xx_enable_disable(struct lp50xx *priv, int enable_disable)
384 ret = gpiod_direction_output(priv->enable_gpio, enable_disable);
389 return regmap_write(priv->regmap, LP50XX_DEV_CFG0, LP50XX_CHIP_EN);
391 return regmap_write(priv->regmap, LP50XX_DEV_CFG0, 0);
395 static int lp50xx_probe_leds(struct fwnode_handle *child, struct lp50xx *priv,
396 struct lp50xx_led *led, int num_leds)
398 u32 led_banks[LP5036_MAX_LED_MODULES] = {0};
403 if (num_leds > priv->chip_info->max_modules) {
404 dev_err(priv->dev, "reg property is invalid\n");
408 priv->num_of_banked_leds = num_leds;
410 ret = fwnode_property_read_u32_array(child, "reg", led_banks, num_leds);
412 dev_err(priv->dev, "reg property is missing\n");
416 ret = lp50xx_set_banks(priv, led_banks);
418 dev_err(priv->dev, "Cannot setup banked LEDs\n");
422 led->ctrl_bank_enabled = 1;
424 ret = fwnode_property_read_u32(child, "reg", &led_number);
426 dev_err(priv->dev, "led reg property missing\n");
430 if (led_number > priv->chip_info->num_leds) {
431 dev_err(priv->dev, "led-sources property is invalid\n");
435 led->led_number = led_number;
441 static int lp50xx_probe_dt(struct lp50xx *priv)
443 struct fwnode_handle *child = NULL;
444 struct fwnode_handle *led_node = NULL;
445 struct led_init_data init_data = {};
446 struct led_classdev *led_cdev;
447 struct mc_subled *mc_led_info;
448 struct lp50xx_led *led;
454 priv->enable_gpio = devm_gpiod_get_optional(priv->dev, "enable", GPIOD_OUT_LOW);
455 if (IS_ERR(priv->enable_gpio))
456 return dev_err_probe(priv->dev, PTR_ERR(priv->enable_gpio),
457 "Failed to get enable GPIO\n");
459 priv->regulator = devm_regulator_get(priv->dev, "vled");
460 if (IS_ERR(priv->regulator))
461 priv->regulator = NULL;
463 device_for_each_child_node(priv->dev, child) {
464 led = &priv->leds[i];
465 ret = fwnode_property_count_u32(child, "reg");
467 dev_err(priv->dev, "reg property is invalid\n");
471 ret = lp50xx_probe_leds(child, priv, led, ret);
475 init_data.fwnode = child;
479 * There are only 3 LEDs per module otherwise they should be
480 * banked which also is presented as 3 LEDs.
482 mc_led_info = devm_kcalloc(priv->dev, LP50XX_LEDS_PER_MODULE,
483 sizeof(*mc_led_info), GFP_KERNEL);
489 fwnode_for_each_child_node(child, led_node) {
490 ret = fwnode_property_read_u32(led_node, "color",
493 fwnode_handle_put(led_node);
494 dev_err(priv->dev, "Cannot read color\n");
498 mc_led_info[num_colors].color_index = color_id;
503 led->mc_cdev.num_colors = num_colors;
504 led->mc_cdev.subled_info = mc_led_info;
505 led_cdev = &led->mc_cdev.led_cdev;
506 led_cdev->brightness_set_blocking = lp50xx_brightness_set;
508 ret = devm_led_classdev_multicolor_register_ext(priv->dev,
512 dev_err(priv->dev, "led register err: %d\n", ret);
521 fwnode_handle_put(child);
525 static int lp50xx_probe(struct i2c_client *client)
531 count = device_get_child_node_count(&client->dev);
533 dev_err(&client->dev, "LEDs are not defined in device tree!");
537 led = devm_kzalloc(&client->dev, struct_size(led, leds, count),
542 mutex_init(&led->lock);
543 led->client = client;
544 led->dev = &client->dev;
545 led->chip_info = device_get_match_data(&client->dev);
546 i2c_set_clientdata(client, led);
547 led->regmap = devm_regmap_init_i2c(client,
548 led->chip_info->lp50xx_regmap_config);
549 if (IS_ERR(led->regmap)) {
550 ret = PTR_ERR(led->regmap);
551 dev_err(&client->dev, "Failed to allocate register map: %d\n",
556 ret = lp50xx_reset(led);
560 ret = lp50xx_enable_disable(led, 1);
564 return lp50xx_probe_dt(led);
567 static int lp50xx_remove(struct i2c_client *client)
569 struct lp50xx *led = i2c_get_clientdata(client);
572 ret = lp50xx_enable_disable(led, 0);
574 dev_err(led->dev, "Failed to disable chip\n");
578 if (led->regulator) {
579 ret = regulator_disable(led->regulator);
581 dev_err(led->dev, "Failed to disable regulator\n");
584 mutex_destroy(&led->lock);
589 static const struct i2c_device_id lp50xx_id[] = {
590 { "lp5009", (kernel_ulong_t)&lp50xx_chip_info_tbl[LP5009] },
591 { "lp5012", (kernel_ulong_t)&lp50xx_chip_info_tbl[LP5012] },
592 { "lp5018", (kernel_ulong_t)&lp50xx_chip_info_tbl[LP5018] },
593 { "lp5024", (kernel_ulong_t)&lp50xx_chip_info_tbl[LP5024] },
594 { "lp5030", (kernel_ulong_t)&lp50xx_chip_info_tbl[LP5030] },
595 { "lp5036", (kernel_ulong_t)&lp50xx_chip_info_tbl[LP5036] },
598 MODULE_DEVICE_TABLE(i2c, lp50xx_id);
600 static const struct of_device_id of_lp50xx_leds_match[] = {
601 { .compatible = "ti,lp5009", .data = &lp50xx_chip_info_tbl[LP5009] },
602 { .compatible = "ti,lp5012", .data = &lp50xx_chip_info_tbl[LP5012] },
603 { .compatible = "ti,lp5018", .data = &lp50xx_chip_info_tbl[LP5018] },
604 { .compatible = "ti,lp5024", .data = &lp50xx_chip_info_tbl[LP5024] },
605 { .compatible = "ti,lp5030", .data = &lp50xx_chip_info_tbl[LP5030] },
606 { .compatible = "ti,lp5036", .data = &lp50xx_chip_info_tbl[LP5036] },
609 MODULE_DEVICE_TABLE(of, of_lp50xx_leds_match);
611 static struct i2c_driver lp50xx_driver = {
614 .of_match_table = of_lp50xx_leds_match,
616 .probe_new = lp50xx_probe,
617 .remove = lp50xx_remove,
618 .id_table = lp50xx_id,
620 module_i2c_driver(lp50xx_driver);
622 MODULE_DESCRIPTION("Texas Instruments LP50XX LED driver");
623 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
624 MODULE_LICENSE("GPL v2");