Commit | Line | Data |
---|---|---|
242b8117 DM |
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/ | |
4 | ||
5 | #include <linux/gpio/consumer.h> | |
6 | #include <linux/i2c.h> | |
7 | #include <linux/init.h> | |
8 | #include <linux/leds.h> | |
fb0f236b | 9 | #include <linux/mod_devicetable.h> |
242b8117 DM |
10 | #include <linux/module.h> |
11 | #include <linux/mutex.h> | |
242b8117 DM |
12 | #include <linux/regmap.h> |
13 | #include <linux/regulator/consumer.h> | |
14 | #include <linux/slab.h> | |
15 | #include <uapi/linux/uleds.h> | |
16 | ||
17 | #include <linux/led-class-multicolor.h> | |
18 | ||
19 | #include "leds.h" | |
20 | ||
21 | #define LP50XX_DEV_CFG0 0x00 | |
22 | #define LP50XX_DEV_CFG1 0x01 | |
23 | #define LP50XX_LED_CFG0 0x02 | |
24 | ||
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 | |
33 | ||
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 | |
42 | ||
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 | |
52 | ||
53 | #define LP50XX_SW_RESET 0xff | |
54 | #define LP50XX_CHIP_EN BIT(6) | |
55 | ||
56 | /* There are 3 LED outputs per bank */ | |
57 | #define LP50XX_LEDS_PER_MODULE 3 | |
58 | ||
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 | |
65 | ||
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}, | |
81 | {0x16, 0x00}, | |
82 | {LP5012_RESET, 0x00} | |
83 | }; | |
84 | ||
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}, | |
104 | {LP5024_RESET, 0x00} | |
105 | }; | |
106 | ||
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}, | |
130 | {LP5036_RESET, 0x00} | |
131 | }; | |
132 | ||
133 | static const struct regmap_config lp5012_regmap_config = { | |
134 | .reg_bits = 8, | |
135 | .val_bits = 8, | |
136 | ||
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, | |
141 | }; | |
142 | ||
143 | static const struct regmap_config lp5024_regmap_config = { | |
144 | .reg_bits = 8, | |
145 | .val_bits = 8, | |
146 | ||
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, | |
151 | }; | |
152 | ||
153 | static const struct regmap_config lp5036_regmap_config = { | |
154 | .reg_bits = 8, | |
155 | .val_bits = 8, | |
156 | ||
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, | |
161 | }; | |
162 | ||
163 | enum lp50xx_model { | |
164 | LP5009, | |
165 | LP5012, | |
166 | LP5018, | |
167 | LP5024, | |
168 | LP5030, | |
169 | LP5036, | |
170 | }; | |
171 | ||
172 | /** | |
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 | |
183 | */ | |
184 | struct lp50xx_chip_info { | |
185 | const struct regmap_config *lp50xx_regmap_config; | |
186 | int model_id; | |
187 | u8 max_modules; | |
188 | u8 num_leds; | |
189 | u8 led_brightness0_reg; | |
190 | u8 mix_out0_reg; | |
191 | u8 bank_brt_reg; | |
192 | u8 bank_mix_reg; | |
193 | u8 reset_reg; | |
194 | }; | |
195 | ||
196 | static const struct lp50xx_chip_info lp50xx_chip_info_tbl[] = { | |
197 | [LP5009] = { | |
198 | .model_id = LP5009, | |
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, | |
207 | }, | |
208 | [LP5012] = { | |
209 | .model_id = LP5012, | |
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, | |
218 | }, | |
219 | [LP5018] = { | |
220 | .model_id = LP5018, | |
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, | |
229 | }, | |
230 | [LP5024] = { | |
231 | .model_id = LP5024, | |
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, | |
240 | }, | |
241 | [LP5030] = { | |
242 | .model_id = LP5030, | |
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, | |
251 | }, | |
252 | [LP5036] = { | |
253 | .model_id = LP5036, | |
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, | |
262 | }, | |
263 | }; | |
264 | ||
265 | struct lp50xx_led { | |
266 | struct led_classdev_mc mc_cdev; | |
267 | struct lp50xx *priv; | |
268 | unsigned long bank_modules; | |
242b8117 DM |
269 | u8 ctrl_bank_enabled; |
270 | int led_number; | |
271 | }; | |
272 | ||
273 | /** | |
274 | * struct lp50xx - | |
275 | * @enable_gpio: hardware enable gpio | |
276 | * @regulator: LED supply regulator pointer | |
277 | * @client: pointer to the I2C client | |
278 | * @regmap: device register map | |
279 | * @dev: pointer to the devices device struct | |
280 | * @lock: lock for reading/writing the device | |
281 | * @chip_info: chip specific information (ie num_leds) | |
282 | * @num_of_banked_leds: holds the number of banked LEDs | |
283 | * @leds: array of LED strings | |
284 | */ | |
285 | struct lp50xx { | |
286 | struct gpio_desc *enable_gpio; | |
287 | struct regulator *regulator; | |
288 | struct i2c_client *client; | |
289 | struct regmap *regmap; | |
290 | struct device *dev; | |
291 | struct mutex lock; | |
292 | const struct lp50xx_chip_info *chip_info; | |
293 | int num_of_banked_leds; | |
294 | ||
295 | /* This needs to be at the end of the struct */ | |
296 | struct lp50xx_led leds[]; | |
297 | }; | |
298 | ||
299 | static struct lp50xx_led *mcled_cdev_to_led(struct led_classdev_mc *mc_cdev) | |
300 | { | |
301 | return container_of(mc_cdev, struct lp50xx_led, mc_cdev); | |
302 | } | |
303 | ||
304 | static int lp50xx_brightness_set(struct led_classdev *cdev, | |
305 | enum led_brightness brightness) | |
306 | { | |
307 | struct led_classdev_mc *mc_dev = lcdev_to_mccdev(cdev); | |
308 | struct lp50xx_led *led = mcled_cdev_to_led(mc_dev); | |
309 | const struct lp50xx_chip_info *led_chip = led->priv->chip_info; | |
310 | u8 led_offset, reg_val; | |
311 | int ret = 0; | |
312 | int i; | |
313 | ||
314 | mutex_lock(&led->priv->lock); | |
315 | if (led->ctrl_bank_enabled) | |
316 | reg_val = led_chip->bank_brt_reg; | |
317 | else | |
318 | reg_val = led_chip->led_brightness0_reg + | |
319 | led->led_number; | |
320 | ||
321 | ret = regmap_write(led->priv->regmap, reg_val, brightness); | |
322 | if (ret) { | |
556f15fe | 323 | dev_err(led->priv->dev, |
242b8117 DM |
324 | "Cannot write brightness value %d\n", ret); |
325 | goto out; | |
326 | } | |
327 | ||
328 | for (i = 0; i < led->mc_cdev.num_colors; i++) { | |
329 | if (led->ctrl_bank_enabled) { | |
330 | reg_val = led_chip->bank_mix_reg + i; | |
331 | } else { | |
332 | led_offset = (led->led_number * 3) + i; | |
333 | reg_val = led_chip->mix_out0_reg + led_offset; | |
334 | } | |
335 | ||
336 | ret = regmap_write(led->priv->regmap, reg_val, | |
337 | mc_dev->subled_info[i].intensity); | |
338 | if (ret) { | |
556f15fe | 339 | dev_err(led->priv->dev, |
242b8117 DM |
340 | "Cannot write intensity value %d\n", ret); |
341 | goto out; | |
342 | } | |
343 | } | |
344 | out: | |
345 | mutex_unlock(&led->priv->lock); | |
346 | return ret; | |
347 | } | |
348 | ||
349 | static int lp50xx_set_banks(struct lp50xx *priv, u32 led_banks[]) | |
350 | { | |
351 | u8 led_config_lo, led_config_hi; | |
352 | u32 bank_enable_mask = 0; | |
353 | int ret; | |
354 | int i; | |
355 | ||
356 | for (i = 0; i < priv->chip_info->max_modules; i++) { | |
357 | if (led_banks[i]) | |
358 | bank_enable_mask |= (1 << led_banks[i]); | |
359 | } | |
360 | ||
b0a82efa AS |
361 | led_config_lo = bank_enable_mask; |
362 | led_config_hi = bank_enable_mask >> 8; | |
242b8117 DM |
363 | |
364 | ret = regmap_write(priv->regmap, LP50XX_LED_CFG0, led_config_lo); | |
365 | if (ret) | |
366 | return ret; | |
367 | ||
368 | if (priv->chip_info->model_id >= LP5030) | |
369 | ret = regmap_write(priv->regmap, LP5036_LED_CFG1, led_config_hi); | |
370 | ||
371 | return ret; | |
372 | } | |
373 | ||
374 | static int lp50xx_reset(struct lp50xx *priv) | |
375 | { | |
376 | return regmap_write(priv->regmap, priv->chip_info->reset_reg, LP50XX_SW_RESET); | |
377 | } | |
378 | ||
379 | static int lp50xx_enable_disable(struct lp50xx *priv, int enable_disable) | |
380 | { | |
381 | int ret; | |
382 | ||
5d2bfb3f AS |
383 | ret = gpiod_direction_output(priv->enable_gpio, enable_disable); |
384 | if (ret) | |
385 | return ret; | |
242b8117 DM |
386 | |
387 | if (enable_disable) | |
388 | return regmap_write(priv->regmap, LP50XX_DEV_CFG0, LP50XX_CHIP_EN); | |
389 | else | |
390 | return regmap_write(priv->regmap, LP50XX_DEV_CFG0, 0); | |
391 | ||
392 | } | |
393 | ||
394 | static int lp50xx_probe_leds(struct fwnode_handle *child, struct lp50xx *priv, | |
395 | struct lp50xx_led *led, int num_leds) | |
396 | { | |
397 | u32 led_banks[LP5036_MAX_LED_MODULES] = {0}; | |
398 | int led_number; | |
399 | int ret; | |
400 | ||
401 | if (num_leds > 1) { | |
402 | if (num_leds > priv->chip_info->max_modules) { | |
556f15fe | 403 | dev_err(priv->dev, "reg property is invalid\n"); |
242b8117 DM |
404 | return -EINVAL; |
405 | } | |
406 | ||
407 | priv->num_of_banked_leds = num_leds; | |
408 | ||
409 | ret = fwnode_property_read_u32_array(child, "reg", led_banks, num_leds); | |
410 | if (ret) { | |
556f15fe | 411 | dev_err(priv->dev, "reg property is missing\n"); |
242b8117 DM |
412 | return ret; |
413 | } | |
414 | ||
415 | ret = lp50xx_set_banks(priv, led_banks); | |
416 | if (ret) { | |
556f15fe | 417 | dev_err(priv->dev, "Cannot setup banked LEDs\n"); |
242b8117 DM |
418 | return ret; |
419 | } | |
420 | ||
421 | led->ctrl_bank_enabled = 1; | |
422 | } else { | |
423 | ret = fwnode_property_read_u32(child, "reg", &led_number); | |
424 | if (ret) { | |
556f15fe | 425 | dev_err(priv->dev, "led reg property missing\n"); |
242b8117 DM |
426 | return ret; |
427 | } | |
428 | ||
429 | if (led_number > priv->chip_info->num_leds) { | |
556f15fe | 430 | dev_err(priv->dev, "led-sources property is invalid\n"); |
242b8117 DM |
431 | return -EINVAL; |
432 | } | |
433 | ||
434 | led->led_number = led_number; | |
435 | } | |
436 | ||
437 | return 0; | |
438 | } | |
439 | ||
440 | static int lp50xx_probe_dt(struct lp50xx *priv) | |
441 | { | |
442 | struct fwnode_handle *child = NULL; | |
443 | struct fwnode_handle *led_node = NULL; | |
444 | struct led_init_data init_data = {}; | |
445 | struct led_classdev *led_cdev; | |
446 | struct mc_subled *mc_led_info; | |
447 | struct lp50xx_led *led; | |
448 | int ret = -EINVAL; | |
449 | int num_colors; | |
450 | u32 color_id; | |
451 | int i = 0; | |
452 | ||
453 | priv->enable_gpio = devm_gpiod_get_optional(priv->dev, "enable", GPIOD_OUT_LOW); | |
9a10def9 AS |
454 | if (IS_ERR(priv->enable_gpio)) |
455 | return dev_err_probe(priv->dev, PTR_ERR(priv->enable_gpio), | |
456 | "Failed to get enable GPIO\n"); | |
242b8117 DM |
457 | |
458 | priv->regulator = devm_regulator_get(priv->dev, "vled"); | |
459 | if (IS_ERR(priv->regulator)) | |
460 | priv->regulator = NULL; | |
461 | ||
462 | device_for_each_child_node(priv->dev, child) { | |
463 | led = &priv->leds[i]; | |
464 | ret = fwnode_property_count_u32(child, "reg"); | |
465 | if (ret < 0) { | |
556f15fe | 466 | dev_err(priv->dev, "reg property is invalid\n"); |
242b8117 DM |
467 | goto child_out; |
468 | } | |
469 | ||
470 | ret = lp50xx_probe_leds(child, priv, led, ret); | |
471 | if (ret) | |
472 | goto child_out; | |
473 | ||
474 | init_data.fwnode = child; | |
475 | num_colors = 0; | |
476 | ||
477 | /* | |
478 | * There are only 3 LEDs per module otherwise they should be | |
479 | * banked which also is presented as 3 LEDs. | |
480 | */ | |
481 | mc_led_info = devm_kcalloc(priv->dev, LP50XX_LEDS_PER_MODULE, | |
482 | sizeof(*mc_led_info), GFP_KERNEL); | |
6d8d014c CJ |
483 | if (!mc_led_info) { |
484 | ret = -ENOMEM; | |
485 | goto child_out; | |
486 | } | |
242b8117 DM |
487 | |
488 | fwnode_for_each_child_node(child, led_node) { | |
489 | ret = fwnode_property_read_u32(led_node, "color", | |
490 | &color_id); | |
491 | if (ret) { | |
f1e1d532 | 492 | fwnode_handle_put(led_node); |
242b8117 DM |
493 | dev_err(priv->dev, "Cannot read color\n"); |
494 | goto child_out; | |
495 | } | |
496 | ||
497 | mc_led_info[num_colors].color_index = color_id; | |
498 | num_colors++; | |
499 | } | |
500 | ||
501 | led->priv = priv; | |
502 | led->mc_cdev.num_colors = num_colors; | |
503 | led->mc_cdev.subled_info = mc_led_info; | |
504 | led_cdev = &led->mc_cdev.led_cdev; | |
505 | led_cdev->brightness_set_blocking = lp50xx_brightness_set; | |
506 | ||
556f15fe | 507 | ret = devm_led_classdev_multicolor_register_ext(priv->dev, |
242b8117 DM |
508 | &led->mc_cdev, |
509 | &init_data); | |
510 | if (ret) { | |
556f15fe | 511 | dev_err(priv->dev, "led register err: %d\n", ret); |
242b8117 DM |
512 | goto child_out; |
513 | } | |
514 | i++; | |
242b8117 DM |
515 | } |
516 | ||
517 | return 0; | |
518 | ||
519 | child_out: | |
520 | fwnode_handle_put(child); | |
521 | return ret; | |
522 | } | |
523 | ||
ea1ff99c | 524 | static int lp50xx_probe(struct i2c_client *client) |
242b8117 DM |
525 | { |
526 | struct lp50xx *led; | |
527 | int count; | |
528 | int ret; | |
529 | ||
530 | count = device_get_child_node_count(&client->dev); | |
531 | if (!count) { | |
532 | dev_err(&client->dev, "LEDs are not defined in device tree!"); | |
533 | return -ENODEV; | |
534 | } | |
535 | ||
536 | led = devm_kzalloc(&client->dev, struct_size(led, leds, count), | |
537 | GFP_KERNEL); | |
538 | if (!led) | |
539 | return -ENOMEM; | |
540 | ||
541 | mutex_init(&led->lock); | |
542 | led->client = client; | |
543 | led->dev = &client->dev; | |
ea1ff99c | 544 | led->chip_info = device_get_match_data(&client->dev); |
242b8117 DM |
545 | i2c_set_clientdata(client, led); |
546 | led->regmap = devm_regmap_init_i2c(client, | |
547 | led->chip_info->lp50xx_regmap_config); | |
548 | if (IS_ERR(led->regmap)) { | |
549 | ret = PTR_ERR(led->regmap); | |
550 | dev_err(&client->dev, "Failed to allocate register map: %d\n", | |
551 | ret); | |
552 | return ret; | |
553 | } | |
554 | ||
555 | ret = lp50xx_reset(led); | |
556 | if (ret) | |
557 | return ret; | |
558 | ||
559 | ret = lp50xx_enable_disable(led, 1); | |
560 | if (ret) | |
561 | return ret; | |
562 | ||
563 | return lp50xx_probe_dt(led); | |
564 | } | |
565 | ||
566 | static int lp50xx_remove(struct i2c_client *client) | |
567 | { | |
568 | struct lp50xx *led = i2c_get_clientdata(client); | |
569 | int ret; | |
570 | ||
571 | ret = lp50xx_enable_disable(led, 0); | |
73bce575 | 572 | if (ret) |
556f15fe | 573 | dev_err(led->dev, "Failed to disable chip\n"); |
242b8117 DM |
574 | |
575 | if (led->regulator) { | |
576 | ret = regulator_disable(led->regulator); | |
577 | if (ret) | |
556f15fe | 578 | dev_err(led->dev, "Failed to disable regulator\n"); |
242b8117 DM |
579 | } |
580 | ||
581 | mutex_destroy(&led->lock); | |
582 | ||
583 | return 0; | |
584 | } | |
585 | ||
586 | static const struct i2c_device_id lp50xx_id[] = { | |
ea1ff99c AS |
587 | { "lp5009", (kernel_ulong_t)&lp50xx_chip_info_tbl[LP5009] }, |
588 | { "lp5012", (kernel_ulong_t)&lp50xx_chip_info_tbl[LP5012] }, | |
589 | { "lp5018", (kernel_ulong_t)&lp50xx_chip_info_tbl[LP5018] }, | |
590 | { "lp5024", (kernel_ulong_t)&lp50xx_chip_info_tbl[LP5024] }, | |
591 | { "lp5030", (kernel_ulong_t)&lp50xx_chip_info_tbl[LP5030] }, | |
592 | { "lp5036", (kernel_ulong_t)&lp50xx_chip_info_tbl[LP5036] }, | |
242b8117 DM |
593 | { } |
594 | }; | |
595 | MODULE_DEVICE_TABLE(i2c, lp50xx_id); | |
596 | ||
597 | static const struct of_device_id of_lp50xx_leds_match[] = { | |
ea1ff99c AS |
598 | { .compatible = "ti,lp5009", .data = &lp50xx_chip_info_tbl[LP5009] }, |
599 | { .compatible = "ti,lp5012", .data = &lp50xx_chip_info_tbl[LP5012] }, | |
600 | { .compatible = "ti,lp5018", .data = &lp50xx_chip_info_tbl[LP5018] }, | |
601 | { .compatible = "ti,lp5024", .data = &lp50xx_chip_info_tbl[LP5024] }, | |
602 | { .compatible = "ti,lp5030", .data = &lp50xx_chip_info_tbl[LP5030] }, | |
603 | { .compatible = "ti,lp5036", .data = &lp50xx_chip_info_tbl[LP5036] }, | |
604 | {} | |
242b8117 DM |
605 | }; |
606 | MODULE_DEVICE_TABLE(of, of_lp50xx_leds_match); | |
607 | ||
608 | static struct i2c_driver lp50xx_driver = { | |
609 | .driver = { | |
610 | .name = "lp50xx", | |
611 | .of_match_table = of_lp50xx_leds_match, | |
612 | }, | |
ea1ff99c | 613 | .probe_new = lp50xx_probe, |
242b8117 DM |
614 | .remove = lp50xx_remove, |
615 | .id_table = lp50xx_id, | |
616 | }; | |
617 | module_i2c_driver(lp50xx_driver); | |
618 | ||
619 | MODULE_DESCRIPTION("Texas Instruments LP50XX LED driver"); | |
620 | MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>"); | |
621 | MODULE_LICENSE("GPL v2"); |