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/module.h>
10 #include <linux/mutex.h>
12 #include <linux/of_gpio.h>
13 #include <linux/regmap.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/slab.h>
16 #include <uapi/linux/uleds.h>
18 #include <linux/led-class-multicolor.h>
22 #define LP50XX_DEV_CFG0 0x00
23 #define LP50XX_DEV_CFG1 0x01
24 #define LP50XX_LED_CFG0 0x02
26 /* LP5009 and LP5012 registers */
27 #define LP5012_BNK_BRT 0x03
28 #define LP5012_BNKA_CLR 0x04
29 #define LP5012_BNKB_CLR 0x05
30 #define LP5012_BNKC_CLR 0x06
31 #define LP5012_LED0_BRT 0x07
32 #define LP5012_OUT0_CLR 0x0b
33 #define LP5012_RESET 0x17
35 /* LP5018 and LP5024 registers */
36 #define LP5024_BNK_BRT 0x03
37 #define LP5024_BNKA_CLR 0x04
38 #define LP5024_BNKB_CLR 0x05
39 #define LP5024_BNKC_CLR 0x06
40 #define LP5024_LED0_BRT 0x07
41 #define LP5024_OUT0_CLR 0x0f
42 #define LP5024_RESET 0x27
44 /* LP5030 and LP5036 registers */
45 #define LP5036_LED_CFG1 0x03
46 #define LP5036_BNK_BRT 0x04
47 #define LP5036_BNKA_CLR 0x05
48 #define LP5036_BNKB_CLR 0x06
49 #define LP5036_BNKC_CLR 0x07
50 #define LP5036_LED0_BRT 0x08
51 #define LP5036_OUT0_CLR 0x14
52 #define LP5036_RESET 0x38
54 #define LP50XX_SW_RESET 0xff
55 #define LP50XX_CHIP_EN BIT(6)
57 /* There are 3 LED outputs per bank */
58 #define LP50XX_LEDS_PER_MODULE 3
60 #define LP5009_MAX_LED_MODULES 2
61 #define LP5012_MAX_LED_MODULES 4
62 #define LP5018_MAX_LED_MODULES 6
63 #define LP5024_MAX_LED_MODULES 8
64 #define LP5030_MAX_LED_MODULES 10
65 #define LP5036_MAX_LED_MODULES 12
67 static const struct reg_default lp5012_reg_defs
[] = {
68 {LP50XX_DEV_CFG0
, 0x0},
69 {LP50XX_DEV_CFG1
, 0x3c},
70 {LP50XX_LED_CFG0
, 0x0},
71 {LP5012_BNK_BRT
, 0xff},
72 {LP5012_BNKA_CLR
, 0x0f},
73 {LP5012_BNKB_CLR
, 0x0f},
74 {LP5012_BNKC_CLR
, 0x0f},
75 {LP5012_LED0_BRT
, 0x0f},
76 /* LEDX_BRT registers are all 0xff for defaults */
77 {0x08, 0xff}, {0x09, 0xff}, {0x0a, 0xff},
78 {LP5012_OUT0_CLR
, 0x0f},
79 /* OUTX_CLR registers are all 0x0 for defaults */
80 {0x0c, 0x00}, {0x0d, 0x00}, {0x0e, 0x00}, {0x0f, 0x00}, {0x10, 0x00},
81 {0x11, 0x00}, {0x12, 0x00}, {0x13, 0x00}, {0x14, 0x00}, {0x15, 0x00},
86 static const struct reg_default lp5024_reg_defs
[] = {
87 {LP50XX_DEV_CFG0
, 0x0},
88 {LP50XX_DEV_CFG1
, 0x3c},
89 {LP50XX_LED_CFG0
, 0x0},
90 {LP5024_BNK_BRT
, 0xff},
91 {LP5024_BNKA_CLR
, 0x0f},
92 {LP5024_BNKB_CLR
, 0x0f},
93 {LP5024_BNKC_CLR
, 0x0f},
94 {LP5024_LED0_BRT
, 0x0f},
95 /* LEDX_BRT registers are all 0xff for defaults */
96 {0x08, 0xff}, {0x09, 0xff}, {0x0a, 0xff}, {0x0b, 0xff}, {0x0c, 0xff},
97 {0x0d, 0xff}, {0x0e, 0xff},
98 {LP5024_OUT0_CLR
, 0x0f},
99 /* OUTX_CLR registers are all 0x0 for defaults */
100 {0x10, 0x00}, {0x11, 0x00}, {0x12, 0x00}, {0x13, 0x00}, {0x14, 0x00},
101 {0x15, 0x00}, {0x16, 0x00}, {0x17, 0x00}, {0x18, 0x00}, {0x19, 0x00},
102 {0x1a, 0x00}, {0x1b, 0x00}, {0x1c, 0x00}, {0x1d, 0x00}, {0x1e, 0x00},
103 {0x1f, 0x00}, {0x20, 0x00}, {0x21, 0x00}, {0x22, 0x00}, {0x23, 0x00},
104 {0x24, 0x00}, {0x25, 0x00}, {0x26, 0x00},
108 static const struct reg_default lp5036_reg_defs
[] = {
109 {LP50XX_DEV_CFG0
, 0x0},
110 {LP50XX_DEV_CFG1
, 0x3c},
111 {LP50XX_LED_CFG0
, 0x0},
112 {LP5036_LED_CFG1
, 0x0},
113 {LP5036_BNK_BRT
, 0xff},
114 {LP5036_BNKA_CLR
, 0x0f},
115 {LP5036_BNKB_CLR
, 0x0f},
116 {LP5036_BNKC_CLR
, 0x0f},
117 {LP5036_LED0_BRT
, 0x0f},
118 /* LEDX_BRT registers are all 0xff for defaults */
119 {0x08, 0xff}, {0x09, 0xff}, {0x0a, 0xff}, {0x0b, 0xff}, {0x0c, 0xff},
120 {0x0d, 0xff}, {0x0e, 0xff}, {0x0f, 0xff}, {0x10, 0xff}, {0x11, 0xff},
121 {0x12, 0xff}, {0x13, 0xff},
122 {LP5036_OUT0_CLR
, 0x0f},
123 /* OUTX_CLR registers are all 0x0 for defaults */
124 {0x15, 0x00}, {0x16, 0x00}, {0x17, 0x00}, {0x18, 0x00}, {0x19, 0x00},
125 {0x1a, 0x00}, {0x1b, 0x00}, {0x1c, 0x00}, {0x1d, 0x00}, {0x1e, 0x00},
126 {0x1f, 0x00}, {0x20, 0x00}, {0x21, 0x00}, {0x22, 0x00}, {0x23, 0x00},
127 {0x24, 0x00}, {0x25, 0x00}, {0x26, 0x00}, {0x27, 0x00}, {0x28, 0x00},
128 {0x29, 0x00}, {0x2a, 0x00}, {0x2b, 0x00}, {0x2c, 0x00}, {0x2d, 0x00},
129 {0x2e, 0x00}, {0x2f, 0x00}, {0x30, 0x00}, {0x31, 0x00}, {0x32, 0x00},
130 {0x33, 0x00}, {0x34, 0x00}, {0x35, 0x00}, {0x36, 0x00}, {0x37, 0x00},
134 static const struct regmap_config lp5012_regmap_config
= {
138 .max_register
= LP5012_RESET
,
139 .reg_defaults
= lp5012_reg_defs
,
140 .num_reg_defaults
= ARRAY_SIZE(lp5012_reg_defs
),
141 .cache_type
= REGCACHE_FLAT
,
144 static const struct regmap_config lp5024_regmap_config
= {
148 .max_register
= LP5024_RESET
,
149 .reg_defaults
= lp5024_reg_defs
,
150 .num_reg_defaults
= ARRAY_SIZE(lp5024_reg_defs
),
151 .cache_type
= REGCACHE_FLAT
,
154 static const struct regmap_config lp5036_regmap_config
= {
158 .max_register
= LP5036_RESET
,
159 .reg_defaults
= lp5036_reg_defs
,
160 .num_reg_defaults
= ARRAY_SIZE(lp5036_reg_defs
),
161 .cache_type
= REGCACHE_FLAT
,
174 * struct lp50xx_chip_info -
175 * @lp50xx_regmap_config: regmap register configuration
176 * @model_id: LED device model
177 * @max_modules: total number of supported LED modules
178 * @num_leds: number of LED outputs available on the device
179 * @led_brightness0_reg: first brightness register of the device
180 * @mix_out0_reg: first color mix register of the device
181 * @bank_brt_reg: bank brightness register
182 * @bank_mix_reg: color mix register
183 * @reset_reg: device reset register
185 struct lp50xx_chip_info
{
186 const struct regmap_config
*lp50xx_regmap_config
;
190 u8 led_brightness0_reg
;
197 static const struct lp50xx_chip_info lp50xx_chip_info_tbl
[] = {
200 .max_modules
= LP5009_MAX_LED_MODULES
,
201 .num_leds
= LP5009_MAX_LED_MODULES
* LP50XX_LEDS_PER_MODULE
,
202 .led_brightness0_reg
= LP5012_LED0_BRT
,
203 .mix_out0_reg
= LP5012_OUT0_CLR
,
204 .bank_brt_reg
= LP5012_BNK_BRT
,
205 .bank_mix_reg
= LP5012_BNKA_CLR
,
206 .reset_reg
= LP5012_RESET
,
207 .lp50xx_regmap_config
= &lp5012_regmap_config
,
211 .max_modules
= LP5012_MAX_LED_MODULES
,
212 .num_leds
= LP5012_MAX_LED_MODULES
* LP50XX_LEDS_PER_MODULE
,
213 .led_brightness0_reg
= LP5012_LED0_BRT
,
214 .mix_out0_reg
= LP5012_OUT0_CLR
,
215 .bank_brt_reg
= LP5012_BNK_BRT
,
216 .bank_mix_reg
= LP5012_BNKA_CLR
,
217 .reset_reg
= LP5012_RESET
,
218 .lp50xx_regmap_config
= &lp5012_regmap_config
,
222 .max_modules
= LP5018_MAX_LED_MODULES
,
223 .num_leds
= LP5018_MAX_LED_MODULES
* LP50XX_LEDS_PER_MODULE
,
224 .led_brightness0_reg
= LP5024_LED0_BRT
,
225 .mix_out0_reg
= LP5024_OUT0_CLR
,
226 .bank_brt_reg
= LP5024_BNK_BRT
,
227 .bank_mix_reg
= LP5024_BNKA_CLR
,
228 .reset_reg
= LP5024_RESET
,
229 .lp50xx_regmap_config
= &lp5024_regmap_config
,
233 .max_modules
= LP5024_MAX_LED_MODULES
,
234 .num_leds
= LP5024_MAX_LED_MODULES
* LP50XX_LEDS_PER_MODULE
,
235 .led_brightness0_reg
= LP5024_LED0_BRT
,
236 .mix_out0_reg
= LP5024_OUT0_CLR
,
237 .bank_brt_reg
= LP5024_BNK_BRT
,
238 .bank_mix_reg
= LP5024_BNKA_CLR
,
239 .reset_reg
= LP5024_RESET
,
240 .lp50xx_regmap_config
= &lp5024_regmap_config
,
244 .max_modules
= LP5030_MAX_LED_MODULES
,
245 .num_leds
= LP5030_MAX_LED_MODULES
* LP50XX_LEDS_PER_MODULE
,
246 .led_brightness0_reg
= LP5036_LED0_BRT
,
247 .mix_out0_reg
= LP5036_OUT0_CLR
,
248 .bank_brt_reg
= LP5036_BNK_BRT
,
249 .bank_mix_reg
= LP5036_BNKA_CLR
,
250 .reset_reg
= LP5036_RESET
,
251 .lp50xx_regmap_config
= &lp5036_regmap_config
,
255 .max_modules
= LP5036_MAX_LED_MODULES
,
256 .num_leds
= LP5036_MAX_LED_MODULES
* LP50XX_LEDS_PER_MODULE
,
257 .led_brightness0_reg
= LP5036_LED0_BRT
,
258 .mix_out0_reg
= LP5036_OUT0_CLR
,
259 .bank_brt_reg
= LP5036_BNK_BRT
,
260 .bank_mix_reg
= LP5036_BNKA_CLR
,
261 .reset_reg
= LP5036_RESET
,
262 .lp50xx_regmap_config
= &lp5036_regmap_config
,
267 struct led_classdev_mc mc_cdev
;
269 unsigned long bank_modules
;
270 int led_intensity
[LP50XX_LEDS_PER_MODULE
];
271 u8 ctrl_bank_enabled
;
277 * @enable_gpio: hardware enable gpio
278 * @regulator: LED supply regulator pointer
279 * @client: pointer to the I2C client
280 * @regmap: device register map
281 * @dev: pointer to the devices device struct
282 * @lock: lock for reading/writing the device
283 * @chip_info: chip specific information (ie num_leds)
284 * @num_of_banked_leds: holds the number of banked LEDs
285 * @leds: array of LED strings
288 struct gpio_desc
*enable_gpio
;
289 struct regulator
*regulator
;
290 struct i2c_client
*client
;
291 struct regmap
*regmap
;
294 const struct lp50xx_chip_info
*chip_info
;
295 int num_of_banked_leds
;
297 /* This needs to be at the end of the struct */
298 struct lp50xx_led leds
[];
301 static struct lp50xx_led
*mcled_cdev_to_led(struct led_classdev_mc
*mc_cdev
)
303 return container_of(mc_cdev
, struct lp50xx_led
, mc_cdev
);
306 static int lp50xx_brightness_set(struct led_classdev
*cdev
,
307 enum led_brightness brightness
)
309 struct led_classdev_mc
*mc_dev
= lcdev_to_mccdev(cdev
);
310 struct lp50xx_led
*led
= mcled_cdev_to_led(mc_dev
);
311 const struct lp50xx_chip_info
*led_chip
= led
->priv
->chip_info
;
312 u8 led_offset
, reg_val
;
316 mutex_lock(&led
->priv
->lock
);
317 if (led
->ctrl_bank_enabled
)
318 reg_val
= led_chip
->bank_brt_reg
;
320 reg_val
= led_chip
->led_brightness0_reg
+
323 ret
= regmap_write(led
->priv
->regmap
, reg_val
, brightness
);
325 dev_err(&led
->priv
->client
->dev
,
326 "Cannot write brightness value %d\n", ret
);
330 for (i
= 0; i
< led
->mc_cdev
.num_colors
; i
++) {
331 if (led
->ctrl_bank_enabled
) {
332 reg_val
= led_chip
->bank_mix_reg
+ i
;
334 led_offset
= (led
->led_number
* 3) + i
;
335 reg_val
= led_chip
->mix_out0_reg
+ led_offset
;
338 ret
= regmap_write(led
->priv
->regmap
, reg_val
,
339 mc_dev
->subled_info
[i
].intensity
);
341 dev_err(&led
->priv
->client
->dev
,
342 "Cannot write intensity value %d\n", ret
);
347 mutex_unlock(&led
->priv
->lock
);
351 static int lp50xx_set_banks(struct lp50xx
*priv
, u32 led_banks
[])
353 u8 led_config_lo
, led_config_hi
;
354 u32 bank_enable_mask
= 0;
358 for (i
= 0; i
< priv
->chip_info
->max_modules
; i
++) {
360 bank_enable_mask
|= (1 << led_banks
[i
]);
363 led_config_lo
= (u8
)(bank_enable_mask
& 0xff);
364 led_config_hi
= (u8
)(bank_enable_mask
>> 8) & 0xff;
366 ret
= regmap_write(priv
->regmap
, LP50XX_LED_CFG0
, led_config_lo
);
370 if (priv
->chip_info
->model_id
>= LP5030
)
371 ret
= regmap_write(priv
->regmap
, LP5036_LED_CFG1
, led_config_hi
);
376 static int lp50xx_reset(struct lp50xx
*priv
)
378 return regmap_write(priv
->regmap
, priv
->chip_info
->reset_reg
, LP50XX_SW_RESET
);
381 static int lp50xx_enable_disable(struct lp50xx
*priv
, int enable_disable
)
385 if (priv
->enable_gpio
) {
386 ret
= gpiod_direction_output(priv
->enable_gpio
, enable_disable
);
392 return regmap_write(priv
->regmap
, LP50XX_DEV_CFG0
, LP50XX_CHIP_EN
);
394 return regmap_write(priv
->regmap
, LP50XX_DEV_CFG0
, 0);
398 static int lp50xx_probe_leds(struct fwnode_handle
*child
, struct lp50xx
*priv
,
399 struct lp50xx_led
*led
, int num_leds
)
401 u32 led_banks
[LP5036_MAX_LED_MODULES
] = {0};
406 if (num_leds
> priv
->chip_info
->max_modules
) {
407 dev_err(&priv
->client
->dev
, "reg property is invalid\n");
411 priv
->num_of_banked_leds
= num_leds
;
413 ret
= fwnode_property_read_u32_array(child
, "reg", led_banks
, num_leds
);
415 dev_err(&priv
->client
->dev
, "reg property is missing\n");
419 ret
= lp50xx_set_banks(priv
, led_banks
);
421 dev_err(&priv
->client
->dev
, "Cannot setup banked LEDs\n");
425 led
->ctrl_bank_enabled
= 1;
427 ret
= fwnode_property_read_u32(child
, "reg", &led_number
);
429 dev_err(&priv
->client
->dev
, "led reg property missing\n");
433 if (led_number
> priv
->chip_info
->num_leds
) {
434 dev_err(&priv
->client
->dev
, "led-sources property is invalid\n");
438 led
->led_number
= led_number
;
444 static int lp50xx_probe_dt(struct lp50xx
*priv
)
446 struct fwnode_handle
*child
= NULL
;
447 struct fwnode_handle
*led_node
= NULL
;
448 struct led_init_data init_data
= {};
449 struct led_classdev
*led_cdev
;
450 struct mc_subled
*mc_led_info
;
451 struct lp50xx_led
*led
;
457 priv
->enable_gpio
= devm_gpiod_get_optional(priv
->dev
, "enable", GPIOD_OUT_LOW
);
458 if (IS_ERR(priv
->enable_gpio
)) {
459 ret
= PTR_ERR(priv
->enable_gpio
);
460 dev_err(&priv
->client
->dev
, "Failed to get enable gpio: %d\n",
465 priv
->regulator
= devm_regulator_get(priv
->dev
, "vled");
466 if (IS_ERR(priv
->regulator
))
467 priv
->regulator
= NULL
;
469 device_for_each_child_node(priv
->dev
, child
) {
470 led
= &priv
->leds
[i
];
471 ret
= fwnode_property_count_u32(child
, "reg");
473 dev_err(&priv
->client
->dev
, "reg property is invalid\n");
477 ret
= lp50xx_probe_leds(child
, priv
, led
, ret
);
481 init_data
.fwnode
= child
;
485 * There are only 3 LEDs per module otherwise they should be
486 * banked which also is presented as 3 LEDs.
488 mc_led_info
= devm_kcalloc(priv
->dev
, LP50XX_LEDS_PER_MODULE
,
489 sizeof(*mc_led_info
), GFP_KERNEL
);
495 fwnode_for_each_child_node(child
, led_node
) {
496 ret
= fwnode_property_read_u32(led_node
, "color",
499 dev_err(priv
->dev
, "Cannot read color\n");
503 mc_led_info
[num_colors
].color_index
= color_id
;
508 led
->mc_cdev
.num_colors
= num_colors
;
509 led
->mc_cdev
.subled_info
= mc_led_info
;
510 led_cdev
= &led
->mc_cdev
.led_cdev
;
511 led_cdev
->brightness_set_blocking
= lp50xx_brightness_set
;
513 ret
= devm_led_classdev_multicolor_register_ext(&priv
->client
->dev
,
517 dev_err(&priv
->client
->dev
, "led register err: %d\n",
522 fwnode_handle_put(child
);
528 fwnode_handle_put(child
);
532 static int lp50xx_probe(struct i2c_client
*client
,
533 const struct i2c_device_id
*id
)
539 count
= device_get_child_node_count(&client
->dev
);
541 dev_err(&client
->dev
, "LEDs are not defined in device tree!");
545 led
= devm_kzalloc(&client
->dev
, struct_size(led
, leds
, count
),
550 mutex_init(&led
->lock
);
551 led
->client
= client
;
552 led
->dev
= &client
->dev
;
553 led
->chip_info
= &lp50xx_chip_info_tbl
[id
->driver_data
];
554 i2c_set_clientdata(client
, led
);
555 led
->regmap
= devm_regmap_init_i2c(client
,
556 led
->chip_info
->lp50xx_regmap_config
);
557 if (IS_ERR(led
->regmap
)) {
558 ret
= PTR_ERR(led
->regmap
);
559 dev_err(&client
->dev
, "Failed to allocate register map: %d\n",
564 ret
= lp50xx_reset(led
);
568 ret
= lp50xx_enable_disable(led
, 1);
572 return lp50xx_probe_dt(led
);
575 static int lp50xx_remove(struct i2c_client
*client
)
577 struct lp50xx
*led
= i2c_get_clientdata(client
);
580 ret
= lp50xx_enable_disable(led
, 0);
582 dev_err(&led
->client
->dev
, "Failed to disable chip\n");
586 if (led
->regulator
) {
587 ret
= regulator_disable(led
->regulator
);
589 dev_err(&led
->client
->dev
,
590 "Failed to disable regulator\n");
593 mutex_destroy(&led
->lock
);
598 static const struct i2c_device_id lp50xx_id
[] = {
599 { "lp5009", LP5009
},
600 { "lp5012", LP5012
},
601 { "lp5018", LP5018
},
602 { "lp5024", LP5024
},
603 { "lp5030", LP5030
},
604 { "lp5036", LP5036
},
607 MODULE_DEVICE_TABLE(i2c
, lp50xx_id
);
609 static const struct of_device_id of_lp50xx_leds_match
[] = {
610 { .compatible
= "ti,lp5009", .data
= (void *)LP5009
},
611 { .compatible
= "ti,lp5012", .data
= (void *)LP5012
},
612 { .compatible
= "ti,lp5018", .data
= (void *)LP5018
},
613 { .compatible
= "ti,lp5024", .data
= (void *)LP5024
},
614 { .compatible
= "ti,lp5030", .data
= (void *)LP5030
},
615 { .compatible
= "ti,lp5036", .data
= (void *)LP5036
},
618 MODULE_DEVICE_TABLE(of
, of_lp50xx_leds_match
);
620 static struct i2c_driver lp50xx_driver
= {
623 .of_match_table
= of_lp50xx_leds_match
,
625 .probe
= lp50xx_probe
,
626 .remove
= lp50xx_remove
,
627 .id_table
= lp50xx_id
,
629 module_i2c_driver(lp50xx_driver
);
631 MODULE_DESCRIPTION("Texas Instruments LP50XX LED driver");
632 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
633 MODULE_LICENSE("GPL v2");