1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2024 Christian Marangi <ansuelsmth@gmail.com>
*/
#include <linux/bitfield.h>
#include <linux/cleanup.h>
#include <linux/delay.h>
#include <linux/firmware.h>
#include <linux/i2c.h>
#include <linux/iopoll.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/platform_data/leds-lp55xx.h>
#include <linux/slab.h>
#include <dt-bindings/leds/leds-lp55xx.h>
#include "leds-lp55xx-common.h"
#define LP5569_MAX_LEDS 9
/* Memory is used like this:
* 0x00 engine 1 program (4 pages)
* 0x40 engine 2 program (4 pages)
* 0x80 engine 3 program (4 pages)
* 0xc0 engine 1 muxing info (1 page)
* 0xd0 engine 2 muxing info (1 page)
* 0xe0 engine 3 muxing info (1 page)
*/
#define LP5569_PAGES_PER_ENGINE 4
#define LP5569_REG_ENABLE 0x00
#define LP5569_ENABLE BIT(6)
#define LP5569_REG_EXEC_CTRL 0x01
#define LP5569_MODE_ENG_SHIFT 2
#define LP5569_REG_OP_MODE 0x02
#define LP5569_EXEC_ENG_SHIFT 2
#define LP5569_REG_ENABLE_LEDS_MSB 0x04
#define LP5569_REG_ENABLE_LEDS_LSB 0x05
#define LP5569_REG_LED_CTRL_BASE 0x07
#define LP5569_FADER_MAPPING_MASK GENMASK(7, 5)
#define LP5569_REG_LED_PWM_BASE 0x16
#define LP5569_REG_LED_CURRENT_BASE 0x22
#define LP5569_REG_MISC 0x2F
#define LP5569_AUTO_INC BIT(6)
#define LP5569_PWR_SAVE BIT(5)
#define LP5569_CP_MODE_MASK GENMASK(4, 3)
#define LP5569_PWM_PWR_SAVE BIT(2)
#define LP5569_INTERNAL_CLK BIT(0)
#define LP5569_REG_MISC2 0x33
#define LP5569_LED_SHORT_TEST BIT(4)
#define LP5569_LED_OPEN_TEST BIT(3)
#define LP5569_REG_STATUS 0x3C
#define LP5569_MASK_BUSY BIT(7)
#define LP5569_STARTUP_BUSY BIT(6)
#define LP5569_ENGINE_BUSY BIT(5)
#define LP5569_ENGINE1_INT BIT(2)
#define LP5569_ENGINE2_INT BIT(1)
#define LP5569_ENGINE3_INT BIT(0)
#define LP5569_ENG_STATUS_MASK (LP5569_ENGINE1_INT | LP5569_ENGINE2_INT | \
LP5569_ENGINE3_INT)
#define LP5569_REG_IO_CONTROL 0x3D
#define LP5569_CLK_OUTPUT BIT(3)
#define LP5569_REG_RESET 0x3F
#define LP5569_RESET 0xFF
#define LP5569_REG_MASTER_FADER_BASE 0x46
#define LP5569_REG_CH1_PROG_START 0x4B
#define LP5569_REG_CH2_PROG_START 0x4C
#define LP5569_REG_CH3_PROG_START 0x4D
#define LP5569_REG_PROG_PAGE_SEL 0x4F
#define LP5569_REG_PROG_MEM 0x50
#define LP5569_REG_LED_FAULT1 0x81
#define LP5569_LED_FAULT8 BIT(0)
#define LP5569_REG_LED_FAULT2 0x82
#define LP5569_LED_FAULT7 BIT(7)
#define LP5569_LED_FAULT6 BIT(6)
#define LP5569_LED_FAULT5 BIT(5)
#define LP5569_LED_FAULT4 BIT(4)
#define LP5569_LED_FAULT3 BIT(3)
#define LP5569_LED_FAULT2 BIT(2)
#define LP5569_LED_FAULT1 BIT(1)
#define LP5569_LED_FAULT0 BIT(0)
#define LP5569_ENG1_PROG_ADDR 0x0
#define LP5569_ENG2_PROG_ADDR 0x40
#define LP5569_ENG3_PROG_ADDR 0x80
#define LP5569_ENG1_MUX_ADDR 0xc0
#define LP5569_ENG2_MUX_ADDR 0xd0
#define LP5569_ENG3_MUX_ADDR 0xe0
#define LP5569_STARTUP_SLEEP 500
#define LEDn_STATUS_FAULT(n, status) ((status) >> (n) & BIT(0))
#define LP5569_DEFAULT_CONFIG \
(LP5569_AUTO_INC | LP5569_PWR_SAVE | LP5569_PWM_PWR_SAVE)
static void lp5569_run_engine(struct lp55xx_chip *chip, bool start)
{
if (!start) {
lp55xx_stop_engine(chip);
lp55xx_turn_off_channels(chip);
return;
}
lp55xx_run_engine_common(chip);
}
static int lp5569_init_program_engine(struct lp55xx_chip *chip)
{
int i;
int j;
int ret;
u8 status;
/* Precompiled pattern per ENGINE setting LED MUX start and stop addresses */
static const u8 pattern[][LP55xx_BYTES_PER_PAGE] = {
{ 0x9c, LP5569_ENG1_MUX_ADDR, 0x9c, 0xb0, 0x9d, 0x80, 0xd8, 0x00, 0},
{ 0x9c, LP5569_ENG2_MUX_ADDR, 0x9c, 0xc0, 0x9d, 0x80, 0xd8, 0x00, 0},
{ 0x9c, LP5569_ENG3_MUX_ADDR, 0x9c, 0xd0, 0x9d, 0x80, 0xd8, 0x00, 0},
};
/* Setup each ENGINE program start address */
ret = lp55xx_write(chip, LP5569_REG_CH1_PROG_START, LP5569_ENG1_PROG_ADDR);
if (ret)
return ret;
ret = lp55xx_write(chip, LP5569_REG_CH2_PROG_START, LP5569_ENG2_PROG_ADDR);
if (ret)
return ret;
ret = lp55xx_write(chip, LP5569_REG_CH3_PROG_START, LP5569_ENG3_PROG_ADDR);
if (ret)
return ret;
/* Write precompiled pattern for LED MUX address space for each ENGINE */
for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
chip->engine_idx = i;
lp55xx_load_engine(chip);
for (j = 0; j < LP55xx_BYTES_PER_PAGE; j++) {
ret = lp55xx_write(chip, LP5569_REG_PROG_MEM + j,
pattern[i - 1][j]);
if (ret)
goto out;
}
}
lp5569_run_engine(chip, true);
/* Let the programs run for couple of ms and check the engine status */
usleep_range(3000, 6000);
lp55xx_read(chip, LP5569_REG_STATUS, &status);
status = FIELD_GET(LP5569_ENG_STATUS_MASK, status);
if (status != LP5569_ENG_STATUS_MASK) {
dev_err(&chip->cl->dev,
"could not configure LED engine, status = 0x%.2x\n",
status);
ret = -EINVAL;
}
out:
lp55xx_stop_all_engine(chip);
return ret;
}
static int lp5569_post_init_device(struct lp55xx_chip *chip)
{
int ret;
u8 val;
val = LP5569_DEFAULT_CONFIG;
val |= FIELD_PREP(LP5569_CP_MODE_MASK, chip->pdata->charge_pump_mode);
ret = lp55xx_write(chip, LP5569_REG_MISC, val);
if (ret)
return ret;
if (chip->pdata->clock_mode == LP55XX_CLOCK_INT) {
/* Internal clock MUST be configured before CLK output */
ret = lp55xx_update_bits(chip, LP5569_REG_MISC,
LP5569_INTERNAL_CLK,
LP5569_INTERNAL_CLK);
if (ret)
return ret;
ret = lp55xx_update_bits(chip, LP5569_REG_IO_CONTROL,
LP5569_CLK_OUTPUT,
LP5569_CLK_OUTPUT);
if (ret)
return ret;
}
ret = lp55xx_write(chip, LP5569_REG_ENABLE, LP5569_ENABLE);
if (ret)
return ret;
read_poll_timeout(lp55xx_read, ret, !(val & LP5569_STARTUP_BUSY),
LP5569_STARTUP_SLEEP, LP5569_STARTUP_SLEEP * 10, false,
chip, LP5569_REG_STATUS, &val);
return lp5569_init_program_engine(chip);
}
static ssize_t lp5569_led_open_test(struct lp55xx_led *led, char *buf)
{
struct lp55xx_chip *chip = led->chip;
struct lp55xx_platform_data *pdata = chip->pdata;
bool leds_fault[LP5569_MAX_LEDS];
struct lp55xx_led *led_tmp = led;
int i, ret, pos = 0;
u8 status;
/* Set in STANDBY state */
ret = lp55xx_write(chip, LP5569_REG_ENABLE, 0);
if (ret)
goto exit;
/* Wait 1ms for device to enter STANDBY state */
usleep_range(1000, 2000);
/* Set Charge Pump to 1.5x */
ret = lp55xx_update_bits(chip, LP5569_REG_MISC,
FIELD_PREP(LP5569_CP_MODE_MASK, LP55XX_CP_BOOST),
LP5569_CP_MODE_MASK);
if (ret)
goto exit;
/* Enable LED Open Test */
ret = lp55xx_update_bits(chip, LP5569_REG_MISC2, LP5569_LED_OPEN_TEST,
LP5569_LED_OPEN_TEST);
if (ret)
goto exit;
/* Put Device in NORMAL state */
ret = lp55xx_write(chip, LP5569_REG_ENABLE, LP5569_ENABLE);
if (ret)
goto exit;
/* Wait 500 us for device to enter NORMAL state */
usleep_range(500, 750);
/* Enable LED and set to 100% brightness */
for (i = 0; i < pdata->num_channels; i++) {
ret = lp55xx_write(chip, LP5569_REG_LED_PWM_BASE + led_tmp->chan_nr,
LED_FULL);
if (ret)
goto exit;
led_tmp++;
}
/* Wait 500 us for device to fill status regs */
usleep_range(500, 750);
/* Parse status led fault 1 regs */
ret = lp55xx_read(chip, LP5569_REG_LED_FAULT1, &status);
if (ret < 0)
goto exit;
for (i = 0; i < 8; i++)
leds_fault[i] = !!((status >> i) & 0x1);
/* Parse status led fault 2 regs */
ret = lp55xx_read(chip, LP5569_REG_LED_FAULT2, &status);
if (ret < 0)
goto exit;
for (i = 0; i < 1; i++)
leds_fault[i + 8] = !!((status >> i) & 0x1);
/* Report LED fault */
led_tmp = led;
for (i = 0; i < pdata->num_channels; i++) {
if (leds_fault[led_tmp->chan_nr])
pos += sysfs_emit_at(buf, pos, "LED %d OPEN FAIL\n",
led_tmp->chan_nr);
led_tmp++;
}
ret = pos;
exit:
/* Disable LED Open Test */
lp55xx_update_bits(chip, LP5569_REG_MISC2, LP5569_LED_OPEN_TEST, 0);
led_tmp = led;
for (i = 0; i < pdata->num_channels; i++) {
lp55xx_write(chip, LP5569_REG_LED_PWM_BASE + led_tmp->chan_nr, 0);
led_tmp++;
}
return ret;
}
static ssize_t lp5569_led_short_test(struct lp55xx_led *led, char *buf)
{
struct lp55xx_chip *chip = led->chip;
struct lp55xx_platform_data *pdata = chip->pdata;
bool leds_fault[LP5569_MAX_LEDS];
struct lp55xx_led *led_tmp = led;
int i, ret, pos = 0;
u8 status;
/* Set in STANDBY state */
ret = lp55xx_write(chip, LP5569_REG_ENABLE, 0);
if (ret)
goto exit;
/* Wait 1ms for device to enter STANDBY state */
usleep_range(1000, 2000);
/* Set Charge Pump to 1x */
ret = lp55xx_update_bits(chip, LP5569_REG_MISC,
FIELD_PREP(LP5569_CP_MODE_MASK, LP55XX_CP_BYPASS),
LP5569_CP_MODE_MASK);
if (ret)
goto exit;
/* Enable LED and set to 100% brightness and current to 100% (25.5mA) */
for (i = 0; i < pdata->num_channels; i++) {
ret = lp55xx_write(chip, LP5569_REG_LED_PWM_BASE + led_tmp->chan_nr,
LED_FULL);
if (ret)
goto exit;
ret = lp55xx_write(chip, LP5569_REG_LED_CURRENT_BASE + led_tmp->chan_nr,
LED_FULL);
if (ret)
goto exit;
led_tmp++;
}
/* Put Device in NORMAL state */
ret = lp55xx_write(chip, LP5569_REG_ENABLE, LP5569_ENABLE);
if (ret)
goto exit;
/* Wait 500 us for device to enter NORMAL state */
usleep_range(500, 750);
/* Enable LED Shorted Test */
ret = lp55xx_update_bits(chip, LP5569_REG_MISC2, LP5569_LED_OPEN_TEST,
LP5569_LED_SHORT_TEST);
if (ret)
goto exit;
/* Wait 500 us for device to fill status regs */
usleep_range(500, 750);
/* Parse status led fault 1 regs */
ret = lp55xx_read(chip, LP5569_REG_LED_FAULT1, &status);
if (ret < 0)
goto exit;
for (i = 0; i < 8; i++)
leds_fault[i] = !!LEDn_STATUS_FAULT(i, status);
/* Parse status led fault 2 regs */
ret = lp55xx_read(chip, LP5569_REG_LED_FAULT2, &status);
if (ret < 0)
goto exit;
for (i = 0; i < 1; i++)
leds_fault[i + 8] = !!LEDn_STATUS_FAULT(i, status);
/* Report LED fault */
led_tmp = led;
for (i = 0; i < pdata->num_channels; i++) {
if (leds_fault[led_tmp->chan_nr])
pos += sysfs_emit_at(buf, pos, "LED %d SHORTED FAIL\n",
led_tmp->chan_nr);
led_tmp++;
}
ret = pos;
exit:
/* Disable LED Shorted Test */
lp55xx_update_bits(chip, LP5569_REG_MISC2, LP5569_LED_SHORT_TEST, 0);
led_tmp = led;
for (i = 0; i < pdata->num_channels; i++) {
lp55xx_write(chip, LP5569_REG_LED_PWM_BASE + led_tmp->chan_nr, 0);
led_tmp++;
}
return ret;
}
static ssize_t lp5569_selftest(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
struct lp55xx_chip *chip = led->chip;
int i, pos = 0;
guard(mutex)(&chip->lock);
/* Test LED Open */
pos = lp5569_led_open_test(led, buf);
if (pos < 0)
return sprintf(buf, "FAIL\n");
/* Test LED Shorted */
pos += lp5569_led_short_test(led, buf);
if (pos < 0)
return sprintf(buf, "FAIL\n");
for (i = 0; i < chip->pdata->num_channels; i++) {
/* Restore current */
lp55xx_write(chip, LP5569_REG_LED_CURRENT_BASE + led->chan_nr,
led->led_current);
/* Restore brightness */
lp55xx_write(chip, LP5569_REG_LED_PWM_BASE + led->chan_nr,
led->brightness);
led++;
}
return pos == 0 ? sysfs_emit(buf, "OK\n") : pos;
}
LP55XX_DEV_ATTR_ENGINE_MODE(1);
LP55XX_DEV_ATTR_ENGINE_MODE(2);
LP55XX_DEV_ATTR_ENGINE_MODE(3);
LP55XX_DEV_ATTR_ENGINE_LEDS(1);
LP55XX_DEV_ATTR_ENGINE_LEDS(2);
LP55XX_DEV_ATTR_ENGINE_LEDS(3);
LP55XX_DEV_ATTR_ENGINE_LOAD(1);
LP55XX_DEV_ATTR_ENGINE_LOAD(2);
LP55XX_DEV_ATTR_ENGINE_LOAD(3);
static LP55XX_DEV_ATTR_RO(selftest, lp5569_selftest);
LP55XX_DEV_ATTR_MASTER_FADER(1);
LP55XX_DEV_ATTR_MASTER_FADER(2);
LP55XX_DEV_ATTR_MASTER_FADER(3);
static LP55XX_DEV_ATTR_RW(master_fader_leds, lp55xx_show_master_fader_leds,
lp55xx_store_master_fader_leds);
static struct attribute *lp5569_attributes[] = {
&dev_attr_engine1_mode.attr,
&dev_attr_engine2_mode.attr,
&dev_attr_engine3_mode.attr,
&dev_attr_engine1_load.attr,
&dev_attr_engine2_load.attr,
&dev_attr_engine3_load.attr,
&dev_attr_engine1_leds.attr,
&dev_attr_engine2_leds.attr,
&dev_attr_engine3_leds.attr,
&dev_attr_selftest.attr,
&dev_attr_master_fader1.attr,
&dev_attr_master_fader2.attr,
&dev_attr_master_fader3.attr,
&dev_attr_master_fader_leds.attr,
NULL,
};
static const struct attribute_group lp5569_group = {
.attrs = lp5569_attributes,
};
/* Chip specific configurations */
static struct lp55xx_device_config lp5569_cfg = {
.reg_op_mode = {
.addr = LP5569_REG_OP_MODE,
.shift = LP5569_MODE_ENG_SHIFT,
},
.reg_exec = {
.addr = LP5569_REG_EXEC_CTRL,
.shift = LP5569_EXEC_ENG_SHIFT,
},
.reset = {
.addr = LP5569_REG_RESET,
.val = LP5569_RESET,
},
.enable = {
.addr = LP5569_REG_ENABLE,
.val = LP5569_ENABLE,
},
.prog_mem_base = {
.addr = LP5569_REG_PROG_MEM,
},
.reg_led_pwm_base = {
.addr = LP5569_REG_LED_PWM_BASE,
},
.reg_led_current_base = {
.addr = LP5569_REG_LED_CURRENT_BASE,
},
.reg_master_fader_base = {
.addr = LP5569_REG_MASTER_FADER_BASE,
},
.reg_led_ctrl_base = {
.addr = LP5569_REG_LED_CTRL_BASE,
},
.pages_per_engine = LP5569_PAGES_PER_ENGINE,
.max_channel = LP5569_MAX_LEDS,
.post_init_device = lp5569_post_init_device,
.brightness_fn = lp55xx_led_brightness,
.multicolor_brightness_fn = lp55xx_multicolor_brightness,
.set_led_current = lp55xx_set_led_current,
.firmware_cb = lp55xx_firmware_loaded_cb,
.run_engine = lp5569_run_engine,
.dev_attr_group = &lp5569_group,
};
static const struct i2c_device_id lp5569_id[] = {
{ "lp5569", .driver_data = (kernel_ulong_t)&lp5569_cfg, },
{ }
};
MODULE_DEVICE_TABLE(i2c, lp5569_id);
static const struct of_device_id of_lp5569_leds_match[] = {
{ .compatible = "ti,lp5569", .data = &lp5569_cfg, },
{},
};
MODULE_DEVICE_TABLE(of, of_lp5569_leds_match);
static struct i2c_driver lp5569_driver = {
.driver = {
.name = "lp5569",
.of_match_table = of_lp5569_leds_match,
},
.probe = lp55xx_probe,
.remove = lp55xx_remove,
.id_table = lp5569_id,
};
module_i2c_driver(lp5569_driver);
MODULE_AUTHOR("Christian Marangi <ansuelsmth@gmail.com>");
MODULE_DESCRIPTION("LP5569 LED engine");
MODULE_LICENSE("GPL");
|