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
|
/*
* TI LP8860 4-Channel LED Driver
*
* Copyright (C) 2014 Texas Instruments
*
* Author: Dan Murphy <dmurphy@ti.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*/
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/leds.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio/consumer.h>
#include <linux/slab.h>
#define LP8860_DISP_CL1_BRT_MSB 0x00
#define LP8860_DISP_CL1_BRT_LSB 0x01
#define LP8860_DISP_CL1_CURR_MSB 0x02
#define LP8860_DISP_CL1_CURR_LSB 0x03
#define LP8860_CL2_BRT_MSB 0x04
#define LP8860_CL2_BRT_LSB 0x05
#define LP8860_CL2_CURRENT 0x06
#define LP8860_CL3_BRT_MSB 0x07
#define LP8860_CL3_BRT_LSB 0x08
#define LP8860_CL3_CURRENT 0x09
#define LP8860_CL4_BRT_MSB 0x0a
#define LP8860_CL4_BRT_LSB 0x0b
#define LP8860_CL4_CURRENT 0x0c
#define LP8860_CONFIG 0x0d
#define LP8860_STATUS 0x0e
#define LP8860_FAULT 0x0f
#define LP8860_LED_FAULT 0x10
#define LP8860_FAULT_CLEAR 0x11
#define LP8860_ID 0x12
#define LP8860_TEMP_MSB 0x13
#define LP8860_TEMP_LSB 0x14
#define LP8860_DISP_LED_CURR_MSB 0x15
#define LP8860_DISP_LED_CURR_LSB 0x16
#define LP8860_DISP_LED_PWM_MSB 0x17
#define LP8860_DISP_LED_PWM_LSB 0x18
#define LP8860_EEPROM_CNTRL 0x19
#define LP8860_EEPROM_UNLOCK 0x1a
#define LP8860_EEPROM_REG_0 0x60
#define LP8860_EEPROM_REG_1 0x61
#define LP8860_EEPROM_REG_2 0x62
#define LP8860_EEPROM_REG_3 0x63
#define LP8860_EEPROM_REG_4 0x64
#define LP8860_EEPROM_REG_5 0x65
#define LP8860_EEPROM_REG_6 0x66
#define LP8860_EEPROM_REG_7 0x67
#define LP8860_EEPROM_REG_8 0x68
#define LP8860_EEPROM_REG_9 0x69
#define LP8860_EEPROM_REG_10 0x6a
#define LP8860_EEPROM_REG_11 0x6b
#define LP8860_EEPROM_REG_12 0x6c
#define LP8860_EEPROM_REG_13 0x6d
#define LP8860_EEPROM_REG_14 0x6e
#define LP8860_EEPROM_REG_15 0x6f
#define LP8860_EEPROM_REG_16 0x70
#define LP8860_EEPROM_REG_17 0x71
#define LP8860_EEPROM_REG_18 0x72
#define LP8860_EEPROM_REG_19 0x73
#define LP8860_EEPROM_REG_20 0x74
#define LP8860_EEPROM_REG_21 0x75
#define LP8860_EEPROM_REG_22 0x76
#define LP8860_EEPROM_REG_23 0x77
#define LP8860_EEPROM_REG_24 0x78
#define LP8860_LOCK_EEPROM 0x00
#define LP8860_UNLOCK_EEPROM 0x01
#define LP8860_PROGRAM_EEPROM 0x02
#define LP8860_EEPROM_CODE_1 0x08
#define LP8860_EEPROM_CODE_2 0xba
#define LP8860_EEPROM_CODE_3 0xef
#define LP8860_CLEAR_FAULTS 0x01
#define LP8860_DISP_LED_NAME "display_cluster"
/**
* struct lp8860_led -
* @lock - Lock for reading/writing the device
* @work - Work item used to off load the brightness register writes
* @client - Pointer to the I2C client
* @led_dev - led class device pointer
* @regmap - Devices register map
* @eeprom_regmap - EEPROM register map
* @enable_gpio - VDDIO/EN gpio to enable communication interface
* @regulator - LED supply regulator pointer
* @brightness - Current brightness value requested
* @label - LED label
**/
struct lp8860_led {
struct mutex lock;
struct work_struct work;
struct i2c_client *client;
struct led_classdev led_dev;
struct regmap *regmap;
struct regmap *eeprom_regmap;
struct gpio_desc *enable_gpio;
struct regulator *regulator;
enum led_brightness brightness;
const char *label;
};
struct lp8860_eeprom_reg {
uint8_t reg;
uint8_t value;
};
static struct lp8860_eeprom_reg lp8860_eeprom_disp_regs[] = {
{ LP8860_EEPROM_REG_0, 0xed },
{ LP8860_EEPROM_REG_1, 0xdf },
{ LP8860_EEPROM_REG_2, 0xdc },
{ LP8860_EEPROM_REG_3, 0xf0 },
{ LP8860_EEPROM_REG_4, 0xdf },
{ LP8860_EEPROM_REG_5, 0xe5 },
{ LP8860_EEPROM_REG_6, 0xf2 },
{ LP8860_EEPROM_REG_7, 0x77 },
{ LP8860_EEPROM_REG_8, 0x77 },
{ LP8860_EEPROM_REG_9, 0x71 },
{ LP8860_EEPROM_REG_10, 0x3f },
{ LP8860_EEPROM_REG_11, 0xb7 },
{ LP8860_EEPROM_REG_12, 0x17 },
{ LP8860_EEPROM_REG_13, 0xef },
{ LP8860_EEPROM_REG_14, 0xb0 },
{ LP8860_EEPROM_REG_15, 0x87 },
{ LP8860_EEPROM_REG_16, 0xce },
{ LP8860_EEPROM_REG_17, 0x72 },
{ LP8860_EEPROM_REG_18, 0xe5 },
{ LP8860_EEPROM_REG_19, 0xdf },
{ LP8860_EEPROM_REG_20, 0x35 },
{ LP8860_EEPROM_REG_21, 0x06 },
{ LP8860_EEPROM_REG_22, 0xdc },
{ LP8860_EEPROM_REG_23, 0x88 },
{ LP8860_EEPROM_REG_24, 0x3E },
};
static int lp8860_unlock_eeprom(struct lp8860_led *led, int lock)
{
int ret;
mutex_lock(&led->lock);
if (lock == LP8860_UNLOCK_EEPROM) {
ret = regmap_write(led->regmap,
LP8860_EEPROM_UNLOCK,
LP8860_EEPROM_CODE_1);
if (ret) {
dev_err(&led->client->dev, "EEPROM Unlock failed\n");
goto out;
}
ret = regmap_write(led->regmap,
LP8860_EEPROM_UNLOCK,
LP8860_EEPROM_CODE_2);
if (ret) {
dev_err(&led->client->dev, "EEPROM Unlock failed\n");
goto out;
}
ret = regmap_write(led->regmap,
LP8860_EEPROM_UNLOCK,
LP8860_EEPROM_CODE_3);
if (ret) {
dev_err(&led->client->dev, "EEPROM Unlock failed\n");
goto out;
}
} else {
ret = regmap_write(led->regmap,
LP8860_EEPROM_UNLOCK,
LP8860_LOCK_EEPROM);
}
out:
mutex_unlock(&led->lock);
return ret;
}
static int lp8860_fault_check(struct lp8860_led *led)
{
int ret, fault;
unsigned int read_buf;
ret = regmap_read(led->regmap, LP8860_LED_FAULT, &read_buf);
if (ret)
goto out;
fault = read_buf;
ret = regmap_read(led->regmap, LP8860_FAULT, &read_buf);
if (ret)
goto out;
fault |= read_buf;
/* Attempt to clear any faults */
if (fault)
ret = regmap_write(led->regmap, LP8860_FAULT_CLEAR,
LP8860_CLEAR_FAULTS);
out:
return ret;
}
static void lp8860_led_brightness_work(struct work_struct *work)
{
struct lp8860_led *led = container_of(work, struct lp8860_led, work);
int ret;
int disp_brightness = led->brightness * 255;
mutex_lock(&led->lock);
ret = lp8860_fault_check(led);
if (ret) {
dev_err(&led->client->dev, "Cannot read/clear faults\n");
goto out;
}
ret = regmap_write(led->regmap, LP8860_DISP_CL1_BRT_MSB,
(disp_brightness & 0xff00) >> 8);
if (ret) {
dev_err(&led->client->dev, "Cannot write CL1 MSB\n");
goto out;
}
ret = regmap_write(led->regmap, LP8860_DISP_CL1_BRT_LSB,
disp_brightness & 0xff);
if (ret) {
dev_err(&led->client->dev, "Cannot write CL1 LSB\n");
goto out;
}
out:
mutex_unlock(&led->lock);
}
static void lp8860_brightness_set(struct led_classdev *led_cdev,
enum led_brightness brt_val)
{
struct lp8860_led *led =
container_of(led_cdev, struct lp8860_led, led_dev);
led->brightness = brt_val;
schedule_work(&led->work);
}
static int lp8860_init(struct lp8860_led *led)
{
unsigned int read_buf;
int ret, i, reg_count;
if (led->enable_gpio)
gpiod_direction_output(led->enable_gpio, 1);
ret = lp8860_fault_check(led);
if (ret)
goto out;
ret = regmap_read(led->regmap, LP8860_STATUS, &read_buf);
if (ret)
goto out;
ret = lp8860_unlock_eeprom(led, LP8860_UNLOCK_EEPROM);
if (ret) {
dev_err(&led->client->dev, "Failed unlocking EEPROM\n");
goto out;
}
reg_count = ARRAY_SIZE(lp8860_eeprom_disp_regs) / sizeof(lp8860_eeprom_disp_regs[0]);
for (i = 0; i < reg_count; i++) {
ret = regmap_write(led->eeprom_regmap,
lp8860_eeprom_disp_regs[i].reg,
lp8860_eeprom_disp_regs[i].value);
if (ret) {
dev_err(&led->client->dev, "Failed writing EEPROM\n");
goto out;
}
}
ret = lp8860_unlock_eeprom(led, LP8860_LOCK_EEPROM);
if (ret)
goto out;
ret = regmap_write(led->regmap,
LP8860_EEPROM_CNTRL,
LP8860_PROGRAM_EEPROM);
if (ret)
dev_err(&led->client->dev, "Failed programming EEPROM
|