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>
19 #define LP50XX_DEV_CFG0 0x00
20 #define LP50XX_DEV_CFG1 0x01
21 #define LP50XX_LED_CFG0 0x02
23 /* LP5009 and LP5012 registers */
24 #define LP5012_BNK_BRT 0x03
25 #define LP5012_BNKA_CLR 0x04
26 #define LP5012_BNKB_CLR 0x05
27 #define LP5012_BNKC_CLR 0x06
28 #define LP5012_LED0_BRT 0x07
29 #define LP5012_OUT0_CLR 0x0b
30 #define LP5012_RESET 0x17
32 /* LP5018 and LP5024 registers */
33 #define LP5024_BNK_BRT 0x03
34 #define LP5024_BNKA_CLR 0x04
35 #define LP5024_BNKB_CLR 0x05
36 #define LP5024_BNKC_CLR 0x06
37 #define LP5024_LED0_BRT 0x07
38 #define LP5024_OUT0_CLR 0x0f
39 #define LP5024_RESET 0x27
41 /* LP5030 and LP5036 registers */
42 #define LP5036_LED_CFG1 0x03
43 #define LP5036_BNK_BRT 0x04
44 #define LP5036_BNKA_CLR 0x05
45 #define LP5036_BNKB_CLR 0x06
46 #define LP5036_BNKC_CLR 0x07
47 #define LP5036_LED0_BRT 0x08
48 #define LP5036_OUT0_CLR 0x14
49 #define LP5036_RESET 0x38
51 #define LP50XX_SW_RESET 0xff
52 #define LP50XX_CHIP_EN BIT(6)
54 /* There are 3 LED outputs per bank */
55 #define LP50XX_LEDS_PER_MODULE 3
57 #define LP5009_MAX_LED_MODULES 2
58 #define LP5012_MAX_LED_MODULES 4
59 #define LP5018_MAX_LED_MODULES 6
60 #define LP5024_MAX_LED_MODULES 8
61 #define LP5030_MAX_LED_MODULES 10
62 #define LP5036_MAX_LED_MODULES 12
64 static const struct reg_default lp5012_reg_defs
[] = {
65 {LP50XX_DEV_CFG0
, 0x0},
66 {LP50XX_DEV_CFG1
, 0x3c},
67 {LP50XX_LED_CFG0
, 0x0},
68 {LP5012_BNK_BRT
, 0xff},
69 {LP5012_BNKA_CLR
, 0x0f},
70 {LP5012_BNKB_CLR
, 0x0f},
71 {LP5012_BNKC_CLR
, 0x0f},
72 {LP5012_LED0_BRT
, 0x0f},
73 /* LEDX_BRT registers are all 0xff for defaults */
74 {0x08, 0xff}, {0x09, 0xff}, {0x0a, 0xff},
75 {LP5012_OUT0_CLR
, 0x0f},
76 /* OUTX_CLR registers are all 0x0 for defaults */
77 {0x0c, 0x00}, {0x0d, 0x00}, {0x0e, 0x00}, {0x0f, 0x00}, {0x10, 0x00},
78 {0x11, 0x00}, {0x12, 0x00}, {0x13, 0x00}, {0x14, 0x00}, {0x15, 0x00},
83 static const struct reg_default lp5024_reg_defs
[] = {
84 {LP50XX_DEV_CFG0
, 0x0},
85 {LP50XX_DEV_CFG1
, 0x3c},
86 {LP50XX_LED_CFG0
, 0x0},
87 {LP5024_BNK_BRT
, 0xff},
88 {LP5024_BNKA_CLR
, 0x0f},
89 {LP5024_BNKB_CLR
, 0x0f},
90 {LP5024_BNKC_CLR
, 0x0f},
91 {LP5024_LED0_BRT
, 0x0f},
92 /* LEDX_BRT registers are all 0xff for defaults */
93 {0x08, 0xff}, {0x09, 0xff}, {0x0a, 0xff}, {0x0b, 0xff}, {0x0c, 0xff},
94 {0x0d, 0xff}, {0x0e, 0xff},
95 {LP5024_OUT0_CLR
, 0x0f},
96 /* OUTX_CLR registers are all 0x0 for defaults */
97 {0x10, 0x00}, {0x11, 0x00}, {0x12, 0x00}, {0x13, 0x00}, {0x14, 0x00},
98 {0x15, 0x00}, {0x16, 0x00}, {0x17, 0x00}, {0x18, 0x00}, {0x19, 0x00},
99 {0x1a, 0x00}, {0x1b, 0x00}, {0x1c, 0x00}, {0x1d, 0x00}, {0x1e, 0x00},
100 {0x1f, 0x00}, {0x20, 0x00}, {0x21, 0x00}, {0x22, 0x00}, {0x23, 0x00},
101 {0x24, 0x00}, {0x25, 0x00}, {0x26, 0x00},
105 static const struct reg_default lp5036_reg_defs
[] = {
106 {LP50XX_DEV_CFG0
, 0x0},
107 {LP50XX_DEV_CFG1
, 0x3c},
108 {LP50XX_LED_CFG0
, 0x0},
109 {LP5036_LED_CFG1
, 0x0},
110 {LP5036_BNK_BRT
, 0xff},
111 {LP5036_BNKA_CLR
, 0x0f},
112 {LP5036_BNKB_CLR
, 0x0f},
113 {LP5036_BNKC_CLR
, 0x0f},
114 {LP5036_LED0_BRT
, 0x0f},
115 /* LEDX_BRT registers are all 0xff for defaults */
116 {0x08, 0xff}, {0x09, 0xff}, {0x0a, 0xff}, {0x0b, 0xff}, {0x0c, 0xff},
117 {0x0d, 0xff}, {0x0e, 0xff}, {0x0f, 0xff}, {0x10, 0xff}, {0x11, 0xff},
118 {0x12, 0xff}, {0x13, 0xff},
119 {LP5036_OUT0_CLR
, 0x0f},
120 /* OUTX_CLR registers are all 0x0 for defaults */
121 {0x15, 0x00}, {0x16, 0x00}, {0x17, 0x00}, {0x18, 0x00}, {0x19, 0x00},
122 {0x1a, 0x00}, {0x1b, 0x00}, {0x1c, 0x00}, {0x1d, 0x00}, {0x1e, 0x00},
123 {0x1f, 0x00}, {0x20, 0x00}, {0x21, 0x00}, {0x22, 0x00}, {0x23, 0x00},
124 {0x24, 0x00}, {0x25, 0x00}, {0x26, 0x00}, {0x27, 0x00}, {0x28, 0x00},
125 {0x29, 0x00}, {0x2a, 0x00}, {0x2b, 0x00}, {0x2c, 0x00}, {0x2d, 0x00},
126 {0x2e, 0x00}, {0x2f, 0x00}, {0x30, 0x00}, {0x31, 0x00}, {0x32, 0x00},
127 {0x33, 0x00}, {0x34, 0x00}, {0x35, 0x00}, {0x36, 0x00}, {0x37, 0x00},
131 static const struct regmap_config lp5012_regmap_config
= {
135 .max_register
= LP5012_RESET
,
136 .reg_defaults
= lp5012_reg_defs
,
137 .num_reg_defaults
= ARRAY_SIZE(lp5012_reg_defs
),
138 .cache_type
= REGCACHE_FLAT
,
141 static const struct regmap_config lp5024_regmap_config
= {
145 .max_register
= LP5024_RESET
,
146 .reg_defaults
= lp5024_reg_defs
,
147 .num_reg_defaults
= ARRAY_SIZE(lp5024_reg_defs
),
148 .cache_type
= REGCACHE_FLAT
,
151 static const struct regmap_config lp5036_regmap_config
= {
155 .max_register
= LP5036_RESET
,
156 .reg_defaults
= lp5036_reg_defs
,
157 .num_reg_defaults
= ARRAY_SIZE(lp5036_reg_defs
),
158 .cache_type
= REGCACHE_FLAT
,
171 * struct lp50xx_chip_info -
172 * @lp50xx_regmap_config: regmap register configuration
173 * @model_id: LED device model
174 * @max_modules: total number of supported LED modules
175 * @num_leds: number of LED outputs available on the device
176 * @led_brightness0_reg: first brightness register of the device
177 * @mix_out0_reg: first color mix register of the device
178 * @bank_brt_reg: bank brightness register
179 * @bank_mix_reg: color mix register
180 * @reset_reg: device reset register
182 struct lp50xx_chip_info
{
183 const struct regmap_config
*lp50xx_regmap_config
;
187 u8 led_brightness0_reg
;
194 static const struct lp50xx_chip_info lp50xx_chip_info_tbl
[] = {
197 .max_modules
= LP5009_MAX_LED_MODULES
,
198 .num_leds
= LP5009_MAX_LED_MODULES
* LP50XX_LEDS_PER_MODULE
,
199 .led_brightness0_reg
= LP5012_LED0_BRT
,
200 .mix_out0_reg
= LP5012_OUT0_CLR
,
201 .bank_brt_reg
= LP5012_BNK_BRT
,
202 .bank_mix_reg
= LP5012_BNKA_CLR
,
203 .reset_reg
= LP5012_RESET
,
204 .lp50xx_regmap_config
= &lp5012_regmap_config
,
208 .max_modules
= LP5012_MAX_LED_MODULES
,
209 .num_leds
= LP5012_MAX_LED_MODULES
* LP50XX_LEDS_PER_MODULE
,
210 .led_brightness0_reg
= LP5012_LED0_BRT
,
211 .mix_out0_reg
= LP5012_OUT0_CLR
,
212 .bank_brt_reg
= LP5012_BNK_BRT
,
213 .bank_mix_reg
= LP5012_BNKA_CLR
,
214 .reset_reg
= LP5012_RESET
,
215 .lp50xx_regmap_config
= &lp5012_regmap_config
,
219 .max_modules
= LP5018_MAX_LED_MODULES
,
220 .num_leds
= LP5018_MAX_LED_MODULES
* LP50XX_LEDS_PER_MODULE
,
221 .led_brightness0_reg
= LP5024_LED0_BRT
,
222 .mix_out0_reg
= LP5024_OUT0_CLR
,
223 .bank_brt_reg
= LP5024_BNK_BRT
,
224 .bank_mix_reg
= LP5024_BNKA_CLR
,
225 .reset_reg
= LP5024_RESET
,
226 .lp50xx_regmap_config
= &lp5024_regmap_config
,
230 .max_modules
= LP5024_MAX_LED_MODULES
,
231 .num_leds
= LP5024_MAX_LED_MODULES
* LP50XX_LEDS_PER_MODULE
,
232 .led_brightness0_reg
= LP5024_LED0_BRT
,
233 .mix_out0_reg
= LP5024_OUT0_CLR
,
234 .bank_brt_reg
= LP5024_BNK_BRT
,
235 .bank_mix_reg
= LP5024_BNKA_CLR
,
236 .reset_reg
= LP5024_RESET
,
237 .lp50xx_regmap_config
= &lp5024_regmap_config
,
241 .max_modules
= LP5030_MAX_LED_MODULES
,
242 .num_leds
= LP5030_MAX_LED_MODULES
* LP50XX_LEDS_PER_MODULE
,
243 .led_brightness0_reg
= LP5036_LED0_BRT
,
244 .mix_out0_reg
= LP5036_OUT0_CLR
,
245 .bank_brt_reg
= LP5036_BNK_BRT
,
246 .bank_mix_reg
= LP5036_BNKA_CLR
,
247 .reset_reg
= LP5036_RESET
,
248 .lp50xx_regmap_config
= &lp5036_regmap_config
,
252 .max_modules
= LP5036_MAX_LED_MODULES
,
253 .num_leds
= LP5036_MAX_LED_MODULES
* LP50XX_LEDS_PER_MODULE
,
254 .led_brightness0_reg
= LP5036_LED0_BRT
,
255 .mix_out0_reg
= LP5036_OUT0_CLR
,
256 .bank_brt_reg
= LP5036_BNK_BRT
,
257 .bank_mix_reg
= LP5036_BNKA_CLR
,
258 .reset_reg
= LP5036_RESET
,
259 .lp50xx_regmap_config
= &lp5036_regmap_config
,
264 struct led_classdev_mc mc_cdev
;
266 u8 ctrl_bank_enabled
;
272 * @enable_gpio: hardware enable gpio
273 * @regulator: LED supply regulator pointer
274 * @client: pointer to the I2C client
275 * @regmap: device register map
276 * @dev: pointer to the devices device struct
277 * @lock: lock for reading/writing the device
278 * @chip_info: chip specific information (ie num_leds)
279 * @leds: array of LED strings
282 struct gpio_desc
*enable_gpio
;
283 struct regulator
*regulator
;
284 struct i2c_client
*client
;
285 struct regmap
*regmap
;
288 const struct lp50xx_chip_info
*chip_info
;
290 /* This needs to be at the end of the struct */
291 struct lp50xx_led leds
[];
294 static struct lp50xx_led
*mcled_cdev_to_led(struct led_classdev_mc
*mc_cdev
)
296 return container_of(mc_cdev
, struct lp50xx_led
, mc_cdev
);
299 static int lp50xx_brightness_set(struct led_classdev
*cdev
,
300 enum led_brightness brightness
)
302 struct led_classdev_mc
*mc_dev
= lcdev_to_mccdev(cdev
);
303 struct lp50xx_led
*led
= mcled_cdev_to_led(mc_dev
);
304 const struct lp50xx_chip_info
*led_chip
= led
->priv
->chip_info
;
305 u8 led_offset
, reg_val
;
309 mutex_lock(&led
->priv
->lock
);
310 if (led
->ctrl_bank_enabled
)
311 reg_val
= led_chip
->bank_brt_reg
;
313 reg_val
= led_chip
->led_brightness0_reg
+
316 ret
= regmap_write(led
->priv
->regmap
, reg_val
, brightness
);
318 dev_err(led
->priv
->dev
,
319 "Cannot write brightness value %d\n", ret
);
323 for (i
= 0; i
< led
->mc_cdev
.num_colors
; i
++) {
324 if (led
->ctrl_bank_enabled
) {
325 reg_val
= led_chip
->bank_mix_reg
+ i
;
327 led_offset
= (led
->led_number
* 3) + i
;
328 reg_val
= led_chip
->mix_out0_reg
+ led_offset
;
331 ret
= regmap_write(led
->priv
->regmap
, reg_val
,
332 mc_dev
->subled_info
[i
].intensity
);
334 dev_err(led
->priv
->dev
,
335 "Cannot write intensity value %d\n", ret
);
340 mutex_unlock(&led
->priv
->lock
);
344 static int lp50xx_set_banks(struct lp50xx
*priv
, u32 led_banks
[])
346 u8 led_config_lo
, led_config_hi
;
347 u32 bank_enable_mask
= 0;
351 for (i
= 0; i
< priv
->chip_info
->max_modules
; i
++) {
353 bank_enable_mask
|= (1 << led_banks
[i
]);
356 led_config_lo
= bank_enable_mask
;
357 led_config_hi
= bank_enable_mask
>> 8;
359 ret
= regmap_write(priv
->regmap
, LP50XX_LED_CFG0
, led_config_lo
);
363 if (priv
->chip_info
->model_id
>= LP5030
)
364 ret
= regmap_write(priv
->regmap
, LP5036_LED_CFG1
, led_config_hi
);
369 static int lp50xx_reset(struct lp50xx
*priv
)
371 return regmap_write(priv
->regmap
, priv
->chip_info
->reset_reg
, LP50XX_SW_RESET
);
374 static int lp50xx_enable_disable(struct lp50xx
*priv
, int enable_disable
)
378 ret
= gpiod_direction_output(priv
->enable_gpio
, enable_disable
);
383 return regmap_write(priv
->regmap
, LP50XX_DEV_CFG0
, LP50XX_CHIP_EN
);
385 return regmap_write(priv
->regmap
, LP50XX_DEV_CFG0
, 0);
389 static int lp50xx_probe_leds(struct fwnode_handle
*child
, struct lp50xx
*priv
,
390 struct lp50xx_led
*led
, int num_leds
)
392 u32 led_banks
[LP5036_MAX_LED_MODULES
] = {0};
397 if (num_leds
> priv
->chip_info
->max_modules
) {
398 dev_err(priv
->dev
, "reg property is invalid\n");
402 ret
= fwnode_property_read_u32_array(child
, "reg", led_banks
, num_leds
);
404 dev_err(priv
->dev
, "reg property is missing\n");
408 ret
= lp50xx_set_banks(priv
, led_banks
);
410 dev_err(priv
->dev
, "Cannot setup banked LEDs\n");
414 led
->ctrl_bank_enabled
= 1;
416 ret
= fwnode_property_read_u32(child
, "reg", &led_number
);
418 dev_err(priv
->dev
, "led reg property missing\n");
422 if (led_number
> priv
->chip_info
->num_leds
) {
423 dev_err(priv
->dev
, "led-sources property is invalid\n");
427 led
->led_number
= led_number
;
433 static int lp50xx_probe_dt(struct lp50xx
*priv
)
435 struct fwnode_handle
*led_node
= NULL
;
436 struct led_init_data init_data
= {};
437 struct led_classdev
*led_cdev
;
438 struct mc_subled
*mc_led_info
;
439 struct lp50xx_led
*led
;
445 priv
->enable_gpio
= devm_gpiod_get_optional(priv
->dev
, "enable", GPIOD_OUT_LOW
);
446 if (IS_ERR(priv
->enable_gpio
))
447 return dev_err_probe(priv
->dev
, PTR_ERR(priv
->enable_gpio
),
448 "Failed to get enable GPIO\n");
450 priv
->regulator
= devm_regulator_get(priv
->dev
, "vled");
451 if (IS_ERR(priv
->regulator
))
452 priv
->regulator
= NULL
;
454 device_for_each_child_node_scoped(priv
->dev
, child
) {
455 led
= &priv
->leds
[i
];
456 ret
= fwnode_property_count_u32(child
, "reg");
458 dev_err(priv
->dev
, "reg property is invalid\n");
462 ret
= lp50xx_probe_leds(child
, priv
, led
, ret
);
466 init_data
.fwnode
= child
;
470 * There are only 3 LEDs per module otherwise they should be
471 * banked which also is presented as 3 LEDs.
473 mc_led_info
= devm_kcalloc(priv
->dev
, LP50XX_LEDS_PER_MODULE
,
474 sizeof(*mc_led_info
), GFP_KERNEL
);
478 fwnode_for_each_child_node(child
, led_node
) {
479 ret
= fwnode_property_read_u32(led_node
, "color",
482 fwnode_handle_put(led_node
);
483 dev_err(priv
->dev
, "Cannot read color\n");
487 mc_led_info
[num_colors
].color_index
= color_id
;
492 led
->mc_cdev
.num_colors
= num_colors
;
493 led
->mc_cdev
.subled_info
= mc_led_info
;
494 led_cdev
= &led
->mc_cdev
.led_cdev
;
495 led_cdev
->brightness_set_blocking
= lp50xx_brightness_set
;
497 ret
= devm_led_classdev_multicolor_register_ext(priv
->dev
,
501 dev_err(priv
->dev
, "led register err: %d\n", ret
);
510 static int lp50xx_probe(struct i2c_client
*client
)
516 count
= device_get_child_node_count(&client
->dev
);
518 dev_err(&client
->dev
, "LEDs are not defined in device tree!");
522 led
= devm_kzalloc(&client
->dev
, struct_size(led
, leds
, count
),
527 mutex_init(&led
->lock
);
528 led
->client
= client
;
529 led
->dev
= &client
->dev
;
530 led
->chip_info
= device_get_match_data(&client
->dev
);
531 i2c_set_clientdata(client
, led
);
532 led
->regmap
= devm_regmap_init_i2c(client
,
533 led
->chip_info
->lp50xx_regmap_config
);
534 if (IS_ERR(led
->regmap
)) {
535 ret
= PTR_ERR(led
->regmap
);
536 dev_err(&client
->dev
, "Failed to allocate register map: %d\n",
541 ret
= lp50xx_reset(led
);
545 ret
= lp50xx_enable_disable(led
, 1);
549 return lp50xx_probe_dt(led
);
552 static void lp50xx_remove(struct i2c_client
*client
)
554 struct lp50xx
*led
= i2c_get_clientdata(client
);
557 ret
= lp50xx_enable_disable(led
, 0);
559 dev_err(led
->dev
, "Failed to disable chip\n");
561 if (led
->regulator
) {
562 ret
= regulator_disable(led
->regulator
);
564 dev_err(led
->dev
, "Failed to disable regulator\n");
567 mutex_destroy(&led
->lock
);
570 static const struct i2c_device_id lp50xx_id
[] = {
571 { "lp5009", (kernel_ulong_t
)&lp50xx_chip_info_tbl
[LP5009
] },
572 { "lp5012", (kernel_ulong_t
)&lp50xx_chip_info_tbl
[LP5012
] },
573 { "lp5018", (kernel_ulong_t
)&lp50xx_chip_info_tbl
[LP5018
] },
574 { "lp5024", (kernel_ulong_t
)&lp50xx_chip_info_tbl
[LP5024
] },
575 { "lp5030", (kernel_ulong_t
)&lp50xx_chip_info_tbl
[LP5030
] },
576 { "lp5036", (kernel_ulong_t
)&lp50xx_chip_info_tbl
[LP5036
] },
579 MODULE_DEVICE_TABLE(i2c
, lp50xx_id
);
581 static const struct of_device_id of_lp50xx_leds_match
[] = {
582 { .compatible
= "ti,lp5009", .data
= &lp50xx_chip_info_tbl
[LP5009
] },
583 { .compatible
= "ti,lp5012", .data
= &lp50xx_chip_info_tbl
[LP5012
] },
584 { .compatible
= "ti,lp5018", .data
= &lp50xx_chip_info_tbl
[LP5018
] },
585 { .compatible
= "ti,lp5024", .data
= &lp50xx_chip_info_tbl
[LP5024
] },
586 { .compatible
= "ti,lp5030", .data
= &lp50xx_chip_info_tbl
[LP5030
] },
587 { .compatible
= "ti,lp5036", .data
= &lp50xx_chip_info_tbl
[LP5036
] },
590 MODULE_DEVICE_TABLE(of
, of_lp50xx_leds_match
);
592 static struct i2c_driver lp50xx_driver
= {
595 .of_match_table
= of_lp50xx_leds_match
,
597 .probe
= lp50xx_probe
,
598 .remove
= lp50xx_remove
,
599 .id_table
= lp50xx_id
,
601 module_i2c_driver(lp50xx_driver
);
603 MODULE_DESCRIPTION("Texas Instruments LP50XX LED driver");
604 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
605 MODULE_LICENSE("GPL v2");