1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * LP5562 LED driver
4 *
5 * Copyright (C) 2013 Texas Instruments
6 *
7 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
8 */
9
10 #include <linux/cleanup.h>
11 #include <linux/delay.h>
12 #include <linux/firmware.h>
13 #include <linux/i2c.h>
14 #include <linux/leds.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/of.h>
18 #include <linux/platform_data/leds-lp55xx.h>
19 #include <linux/slab.h>
20
21 #include "leds-lp55xx-common.h"
22
23 #define LP5562_MAX_LEDS 4
24
25 /* ENABLE Register 00h */
26 #define LP5562_REG_ENABLE 0x00
27 #define LP5562_EXEC_ENG1_M 0x30
28 #define LP5562_EXEC_ENG2_M 0x0C
29 #define LP5562_EXEC_ENG3_M 0x03
30 #define LP5562_EXEC_M 0x3F
31 #define LP5562_MASTER_ENABLE 0x40 /* Chip master enable */
32 #define LP5562_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */
33 #define LP5562_EXEC_RUN 0x2A
34 #define LP5562_ENABLE_DEFAULT \
35 (LP5562_MASTER_ENABLE | LP5562_LOGARITHMIC_PWM)
36 #define LP5562_ENABLE_RUN_PROGRAM \
37 (LP5562_ENABLE_DEFAULT | LP5562_EXEC_RUN)
38
39 /* OPMODE Register 01h */
40 #define LP5562_REG_OP_MODE 0x01
41
42 /* BRIGHTNESS Registers */
43 #define LP5562_REG_R_PWM 0x04
44 #define LP5562_REG_G_PWM 0x03
45 #define LP5562_REG_B_PWM 0x02
46 #define LP5562_REG_W_PWM 0x0E
47
48 /* CURRENT Registers */
49 #define LP5562_REG_R_CURRENT 0x07
50 #define LP5562_REG_G_CURRENT 0x06
51 #define LP5562_REG_B_CURRENT 0x05
52 #define LP5562_REG_W_CURRENT 0x0F
53
54 /* CONFIG Register 08h */
55 #define LP5562_REG_CONFIG 0x08
56 #define LP5562_PWM_HF 0x40
57 #define LP5562_PWRSAVE_EN 0x20
58 #define LP5562_CLK_INT 0x01 /* Internal clock */
59 #define LP5562_DEFAULT_CFG (LP5562_PWM_HF | LP5562_PWRSAVE_EN)
60
61 /* RESET Register 0Dh */
62 #define LP5562_REG_RESET 0x0D
63 #define LP5562_RESET 0xFF
64
65 /* PROGRAM ENGINE Registers */
66 #define LP5562_REG_PROG_MEM_ENG1 0x10
67 #define LP5562_REG_PROG_MEM_ENG2 0x30
68 #define LP5562_REG_PROG_MEM_ENG3 0x50
69
70 /* LEDMAP Register 70h */
71 #define LP5562_REG_ENG_SEL 0x70
72 #define LP5562_ENG_SEL_PWM 0
73 #define LP5562_ENG_FOR_RGB_M 0x3F
74 #define LP5562_ENG_SEL_RGB 0x1B /* R:ENG1, G:ENG2, B:ENG3 */
75 #define LP5562_ENG_FOR_W_M 0xC0
76 #define LP5562_ENG1_FOR_W 0x40 /* W:ENG1 */
77 #define LP5562_ENG2_FOR_W 0x80 /* W:ENG2 */
78 #define LP5562_ENG3_FOR_W 0xC0 /* W:ENG3 */
79
80 /* Program Commands */
81 #define LP5562_CMD_DISABLE 0x00
82 #define LP5562_CMD_LOAD 0x15
83 #define LP5562_CMD_RUN 0x2A
84 #define LP5562_CMD_DIRECT 0x3F
85 #define LP5562_PATTERN_OFF 0
86
lp5562_wait_opmode_done(void)87 static inline void lp5562_wait_opmode_done(void)
88 {
89 /* operation mode change needs to be longer than 153 us */
90 usleep_range(200, 300);
91 }
92
lp5562_wait_enable_done(void)93 static inline void lp5562_wait_enable_done(void)
94 {
95 /* it takes more 488 us to update ENABLE register */
96 usleep_range(500, 600);
97 }
98
lp5562_set_led_current(struct lp55xx_led * led,u8 led_current)99 static void lp5562_set_led_current(struct lp55xx_led *led, u8 led_current)
100 {
101 static const u8 addr[] = {
102 LP5562_REG_R_CURRENT,
103 LP5562_REG_G_CURRENT,
104 LP5562_REG_B_CURRENT,
105 LP5562_REG_W_CURRENT,
106 };
107
108 led->led_current = led_current;
109 lp55xx_write(led->chip, addr[led->chan_nr], led_current);
110 }
111
lp5562_run_engine(struct lp55xx_chip * chip,bool start)112 static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
113 {
114 int ret;
115
116 /* stop engine */
117 if (!start) {
118 lp55xx_write(chip, LP5562_REG_ENABLE, LP5562_ENABLE_DEFAULT);
119 lp5562_wait_enable_done();
120 lp55xx_stop_all_engine(chip);
121 lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
122 lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
123 lp5562_wait_opmode_done();
124 return;
125 }
126
127 ret = lp55xx_run_engine_common(chip);
128 if (!ret)
129 lp5562_wait_enable_done();
130 }
131
lp5562_post_init_device(struct lp55xx_chip * chip)132 static int lp5562_post_init_device(struct lp55xx_chip *chip)
133 {
134 int ret;
135 u8 cfg = LP5562_DEFAULT_CFG;
136
137 /* Set all PWMs to direct control mode */
138 ret = lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
139 if (ret)
140 return ret;
141
142 lp5562_wait_opmode_done();
143
144 /* Update configuration for the clock setting */
145 if (!lp55xx_is_extclk_used(chip))
146 cfg |= LP5562_CLK_INT;
147
148 ret = lp55xx_write(chip, LP5562_REG_CONFIG, cfg);
149 if (ret)
150 return ret;
151
152 /* Initialize all channels PWM to zero -> leds off */
153 lp55xx_write(chip, LP5562_REG_R_PWM, 0);
154 lp55xx_write(chip, LP5562_REG_G_PWM, 0);
155 lp55xx_write(chip, LP5562_REG_B_PWM, 0);
156 lp55xx_write(chip, LP5562_REG_W_PWM, 0);
157
158 /* Set LED map as register PWM by default */
159 lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
160
161 return 0;
162 }
163
lp5562_multicolor_brightness(struct lp55xx_led * led)164 static int lp5562_multicolor_brightness(struct lp55xx_led *led)
165 {
166 struct lp55xx_chip *chip = led->chip;
167 static const u8 addr[] = {
168 LP5562_REG_R_PWM,
169 LP5562_REG_G_PWM,
170 LP5562_REG_B_PWM,
171 LP5562_REG_W_PWM,
172 };
173 int ret;
174 int i;
175
176 guard(mutex)(&chip->lock);
177 for (i = 0; i < led->mc_cdev.num_colors; i++) {
178 ret = lp55xx_write(chip,
179 addr[led->mc_cdev.subled_info[i].channel],
180 led->mc_cdev.subled_info[i].brightness);
181 if (ret)
182 break;
183 }
184
185 return ret;
186 }
187
lp5562_led_brightness(struct lp55xx_led * led)188 static int lp5562_led_brightness(struct lp55xx_led *led)
189 {
190 struct lp55xx_chip *chip = led->chip;
191 static const u8 addr[] = {
192 LP5562_REG_R_PWM,
193 LP5562_REG_G_PWM,
194 LP5562_REG_B_PWM,
195 LP5562_REG_W_PWM,
196 };
197 int ret;
198
199 guard(mutex)(&chip->lock);
200
201 ret = lp55xx_write(chip, addr[led->chan_nr], led->brightness);
202
203 return ret;
204 }
205
lp5562_write_program_memory(struct lp55xx_chip * chip,u8 base,const u8 * rgb,int size)206 static void lp5562_write_program_memory(struct lp55xx_chip *chip,
207 u8 base, const u8 *rgb, int size)
208 {
209 int i;
210
211 if (!rgb || size <= 0)
212 return;
213
214 for (i = 0; i < size; i++)
215 lp55xx_write(chip, base + i, *(rgb + i));
216
217 lp55xx_write(chip, base + i, 0);
218 lp55xx_write(chip, base + i + 1, 0);
219 }
220
221 /* check the size of program count */
_is_pc_overflow(struct lp55xx_predef_pattern * ptn)222 static inline bool _is_pc_overflow(struct lp55xx_predef_pattern *ptn)
223 {
224 return ptn->size_r >= LP55xx_BYTES_PER_PAGE ||
225 ptn->size_g >= LP55xx_BYTES_PER_PAGE ||
226 ptn->size_b >= LP55xx_BYTES_PER_PAGE;
227 }
228
lp5562_run_predef_led_pattern(struct lp55xx_chip * chip,int mode)229 static int lp5562_run_predef_led_pattern(struct lp55xx_chip *chip, int mode)
230 {
231 struct lp55xx_predef_pattern *ptn;
232 int i;
233
234 if (mode == LP5562_PATTERN_OFF) {
235 lp5562_run_engine(chip, false);
236 return 0;
237 }
238
239 ptn = chip->pdata->patterns + (mode - 1);
240 if (!ptn || _is_pc_overflow(ptn)) {
241 dev_err(&chip->cl->dev, "invalid pattern data\n");
242 return -EINVAL;
243 }
244
245 lp55xx_stop_all_engine(chip);
246
247 /* Set LED map as RGB */
248 lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_RGB);
249
250 /* Load engines */
251 for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
252 chip->engine_idx = i;
253 lp55xx_load_engine(chip);
254 }
255
256 /* Clear program registers */
257 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1, 0);
258 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1 + 1, 0);
259 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2, 0);
260 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2 + 1, 0);
261 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3, 0);
262 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3 + 1, 0);
263
264 /* Program engines */
265 lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG1,
266 ptn->r, ptn->size_r);
267 lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG2,
268 ptn->g, ptn->size_g);
269 lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG3,
270 ptn->b, ptn->size_b);
271
272 /* Run engines */
273 lp5562_run_engine(chip, true);
274
275 return 0;
276 }
277
lp5562_store_pattern(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)278 static ssize_t lp5562_store_pattern(struct device *dev,
279 struct device_attribute *attr,
280 const char *buf, size_t len)
281 {
282 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
283 struct lp55xx_chip *chip = led->chip;
284 struct lp55xx_predef_pattern *ptn = chip->pdata->patterns;
285 int num_patterns = chip->pdata->num_patterns;
286 unsigned long mode;
287 int ret;
288
289 ret = kstrtoul(buf, 0, &mode);
290 if (ret)
291 return ret;
292
293 if (mode > num_patterns || !ptn)
294 return -EINVAL;
295
296 guard(mutex)(&chip->lock);
297
298 ret = lp5562_run_predef_led_pattern(chip, mode);
299
300 if (ret)
301 return ret;
302
303 return len;
304 }
305
lp5562_store_engine_mux(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)306 static ssize_t lp5562_store_engine_mux(struct device *dev,
307 struct device_attribute *attr,
308 const char *buf, size_t len)
309 {
310 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
311 struct lp55xx_chip *chip = led->chip;
312 u8 mask;
313 u8 val;
314
315 /* LED map
316 * R ... Engine 1 (fixed)
317 * G ... Engine 2 (fixed)
318 * B ... Engine 3 (fixed)
319 * W ... Engine 1 or 2 or 3
320 */
321
322 if (sysfs_streq(buf, "RGB")) {
323 mask = LP5562_ENG_FOR_RGB_M;
324 val = LP5562_ENG_SEL_RGB;
325 } else if (sysfs_streq(buf, "W")) {
326 enum lp55xx_engine_index idx = chip->engine_idx;
327
328 mask = LP5562_ENG_FOR_W_M;
329 switch (idx) {
330 case LP55XX_ENGINE_1:
331 val = LP5562_ENG1_FOR_W;
332 break;
333 case LP55XX_ENGINE_2:
334 val = LP5562_ENG2_FOR_W;
335 break;
336 case LP55XX_ENGINE_3:
337 val = LP5562_ENG3_FOR_W;
338 break;
339 default:
340 return -EINVAL;
341 }
342
343 } else {
344 dev_err(dev, "choose RGB or W\n");
345 return -EINVAL;
346 }
347
348 guard(mutex)(&chip->lock);
349
350 lp55xx_update_bits(chip, LP5562_REG_ENG_SEL, mask, val);
351
352 return len;
353 }
354
355 static LP55XX_DEV_ATTR_WO(led_pattern, lp5562_store_pattern);
356 static LP55XX_DEV_ATTR_WO(engine_mux, lp5562_store_engine_mux);
357
358 static struct attribute *lp5562_attributes[] = {
359 &dev_attr_led_pattern.attr,
360 &dev_attr_engine_mux.attr,
361 NULL,
362 };
363
364 static const struct attribute_group lp5562_group = {
365 .attrs = lp5562_attributes,
366 };
367
368 /* Chip specific configurations */
369 static struct lp55xx_device_config lp5562_cfg = {
370 .max_channel = LP5562_MAX_LEDS,
371 .reg_op_mode = {
372 .addr = LP5562_REG_OP_MODE,
373 },
374 .reg_exec = {
375 .addr = LP5562_REG_ENABLE,
376 },
377 .reset = {
378 .addr = LP5562_REG_RESET,
379 .val = LP5562_RESET,
380 },
381 .enable = {
382 .addr = LP5562_REG_ENABLE,
383 .val = LP5562_ENABLE_DEFAULT,
384 },
385 .prog_mem_base = {
386 .addr = LP5562_REG_PROG_MEM_ENG1,
387 },
388 .post_init_device = lp5562_post_init_device,
389 .set_led_current = lp5562_set_led_current,
390 .brightness_fn = lp5562_led_brightness,
391 .multicolor_brightness_fn = lp5562_multicolor_brightness,
392 .run_engine = lp5562_run_engine,
393 .firmware_cb = lp55xx_firmware_loaded_cb,
394 .dev_attr_group = &lp5562_group,
395 };
396
397 static const struct i2c_device_id lp5562_id[] = {
398 { "lp5562", .driver_data = (kernel_ulong_t)&lp5562_cfg, },
399 { }
400 };
401 MODULE_DEVICE_TABLE(i2c, lp5562_id);
402
403 static const struct of_device_id of_lp5562_leds_match[] = {
404 { .compatible = "ti,lp5562", .data = &lp5562_cfg, },
405 {},
406 };
407
408 MODULE_DEVICE_TABLE(of, of_lp5562_leds_match);
409
410 static struct i2c_driver lp5562_driver = {
411 .driver = {
412 .name = "lp5562",
413 .of_match_table = of_lp5562_leds_match,
414 },
415 .probe = lp55xx_probe,
416 .remove = lp55xx_remove,
417 .id_table = lp5562_id,
418 };
419
420 module_i2c_driver(lp5562_driver);
421
422 MODULE_DESCRIPTION("Texas Instruments LP5562 LED Driver");
423 MODULE_AUTHOR("Milo Kim");
424 MODULE_LICENSE("GPL");
425