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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
|
/*
* LED driver for Samsung Bolt ET6312B
*
* Copyright (C) 2018 Samsung Electronics
* Author: Hyuk Kang, HyungJae Im
*
*/
#define pr_fmt(fmt) "sled_et6312b: " fmt
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include <linux/regulator/consumer.h>
#include <linux/hrtimer.h>
#include "led-sep.h"
#define LED_R_SHIFT 0
#define LED_G_SHIFT 2
#define LED_B_SHIFT 5
#define LEDXMD_R_MASK 0x03
#define LEDXMD_G_MASK 0x1C
#define LEDXMD_B_MASK 0xE0
#define ALWAYS_OFF 0
#define ALWAYS_ON 1
#define BLINK 2
#define ENABLE 0x90
#define DISABLE 0x00
#define LED_MAX 4
#define LED_NORMAL_CURRENT 255
#define RED COLOR(0x1D, 0, 0)
#define GREEN COLOR(0, 0xF, 0)
#define WHITE COLOR(0x7, 0x7, 0x7)
#define ET6312B_CHIPCTR 0x00
#define ET6312B_PERIOD_RGB0 0x01
#define ET6312B_PERIOD_RGB3 0x10
#define ET6312B_TON1_RGB0 0x02
#define ET6312B_TON1_RGB3 0x11
#define ET6312B_DELAY_TIME_RGB0 0x05
#define ET6312B_DELAY_TIME_RGB1 0x0A
#define ET6312B_DELAY_TIME_RGB2 0x0F
#define ET6312B_DELAY_TIME_RGB3 0x14
#define ET6312B_LEDXMD_RGB0 0x16
#define ET6312B_LEDXMD_RGB1 0x17
#define ET6312B_LEDXMD_RGB2 0x18
#define ET6312B_LEDXMD_RGB3 0x19
#define ET6312B_LED1_CURT 0x1A
#define ET6312B_LED4_CURT 0x1D
#define ET6312B_LED7_CURT 0x20
#define ET6312B_LED10_CURT 0x23
#define ET6312B_LED12_CURT 0x25
#define TIMER_SET(total_time, count) ktime_set(total_time * count / 1000, \
(total_time * count % 1000) * 1000000)
static struct hrtimer et6312b_timer[4];
static struct work_struct et6312b_work[4];
extern unsigned int lpcharge;
enum led_regs {
CURR,
XMD,
PERIOD,
TON1,
DELAY,
};
/* CURR, XMD, PERIOD, TON1, DELAY */
static int rgb0[] = { 0x1a, 0x16, 0x01, 0x02, 0x05 };
static int rgb1[] = { 0x1d, 0x17, 0x06, 0x07, 0x0a };
static int rgb2[] = { 0x20, 0x18, 0x0b, 0x0c, 0x0f };
static int rgb3[] = { 0x23, 0x19, 0x10, 0x11, 0x14 };
struct et6312b_led {
struct i2c_client *i2c;
int pattern_count;
struct regulator *vdd;
const char *regulator_name;
int *rgb[4];
};
enum led_index {
FIVEG = 1,
LTE = 2,
WIFI = 4,
BATTERY = 8,
};
struct workqueue_struct *et6312b_workqueue;
static inline u8 get_address(struct et6312b_led *led, int led_num, int reg)
{
if (led && (led_num < 4) && (reg < 5) && led->rgb[led_num])
return led->rgb[led_num][reg];
else {
pr_err("%s failed. led%d, reg %d\n", __func__, led_num, reg);
return 0;
}
}
static int et6312b_i2c_write(struct i2c_client *i2c, u8 reg, u8 value)
{
int ret;
ret = i2c_smbus_write_byte_data(i2c, reg, value);
if (ret < 0)
pr_info("%s 0x%x, ret(%d)\n", __func__, reg, ret);
return ret;
}
static int et6312b_i2c_read(struct i2c_client *i2c, u8 reg)
{
int ret;
ret = i2c_smbus_read_byte_data(i2c, reg);
if (ret < 0)
pr_info("%s 0x%x: 0x%x\n", __func__, reg, ret);
return ret;
}
static int et6312b_write(struct et6312b_led *led, u8 reg, u8 value)
{
if (led && led->i2c)
return et6312b_i2c_write(led->i2c, reg, value);
return -ENODEV;
}
static int et6312b_read(struct et6312b_led *led, u8 reg)
{
if (led && led->i2c)
return et6312b_i2c_read(led->i2c, reg);
return -ENODEV;
}
static int dump_reg(struct et6312b_led *led)
{
int ret = 0;
u8 i = 0;
pr_info("DUMP START ============================================\n");
for (i = 0; i < 0x27; i++) {
ret = et6312b_read(led, i);
pr_info("et6312b 0x%x: 0x%x\n", i, ret);
}
pr_info("DUMP END ============================================\n");
return ret;
}
static inline void et6312b_clear(struct et6312b_led *led)
{
u8 i = 0;
for (i = 0; i < 0x27; i++)
et6312b_write(led, i, 0);
}
static int et6312b_test(struct led_sep_ops *ops, int reg, int value)
{
int ret = 0;
struct et6312b_led *led = led_sep_get_opsdata(ops);
pr_info("%s 0x%x, 0x%x\n", __func__, reg, value);
ret = et6312b_write(led, reg, value);
if (ret < 0)
pr_info("%s 0x%x, ret(%d)\n", __func__, reg, ret);
dump_reg(led);
return ret;
}
static int et6312b_reset(struct led_sep_ops *ops)
{
struct et6312b_led *led = led_sep_get_opsdata(ops);
pr_info("%s\n", __func__);
et6312b_clear(led);
return 0;
}
static int et6312b_power(struct et6312b_led *led, bool on)
{
int ret = 0;
pr_info("%s: %s\n", __func__, on ? "on" : "off");
if (!led->vdd) {
pr_info("%s: no regulator, return without control\n", __func__);
return -ENODEV;
}
if (on) {
ret = regulator_enable(led->vdd);
if (ret)
pr_err("%s: Failed to enable LDO: %d\n", __func__, ret);
} else
regulator_disable(led->vdd);
return ret;
}
static inline int update_xmd(u8 xmd, u8 mask, u8 shift, int value)
{
u8 reg = xmd & ~mask;
reg |= value << shift;
return reg;
}
static int et6312b_workmode(struct et6312b_led *led,
int led_num, int color, int mode)
{
int ret = 0;
u8 reg = get_address(led, led_num, XMD);
u8 backup = et6312b_read(led, reg);
u8 xmd = 0;
pr_info("%s: led%d color 0x%x mode 0x%x\n", __func__,
led_num, color, mode);
if (mode == ALWAYS_OFF) {
if (!LED_R(color))
xmd = update_xmd(xmd, LEDXMD_R_MASK, LED_R_SHIFT, mode);
if (!LED_G(color))
xmd = update_xmd(xmd, LEDXMD_G_MASK, LED_G_SHIFT, mode);
if (!LED_B(color))
xmd = update_xmd(xmd, LEDXMD_B_MASK, LED_B_SHIFT, mode);
} else {
if (LED_R(color))
xmd = update_xmd(xmd, LEDXMD_R_MASK, LED_R_SHIFT, mode);
if (LED_G(color))
xmd = update_xmd(xmd, LEDXMD_G_MASK, LED_G_SHIFT, mode);
if (LED_B(color))
xmd = update_xmd(xmd, LEDXMD_B_MASK, LED_B_SHIFT, mode);
}
if (backup != xmd) {
pr_info("%s: led%d xmd 0x%x -> 0x%x\n", __func__,
led_num, backup, xmd);
et6312b_write(led, reg, xmd);
}
return ret;
}
static int et6312b_color(struct et6312b_led *led,
int led_num, int color)
{
int ret = 0;
u8 reg = 0;
reg = get_address(led, led_num, CURR);
pr_info("%s: led%d (0x%x) color 0x%x\n", __func__,
led_num, reg, color);
ret = et6312b_write(led, reg, LED_R(color));
if (ret < 0)
return ret;
ret = et6312b_write(led, reg + 1, LED_G(color));
if (ret < 0)
return ret;
ret = et6312b_write(led, reg + 2, LED_B(color));
if (ret < 0)
return ret;
return ret;
}
static inline int calc_period(int total)
{
return (total < 128) ? 0 :
(total < 16380) ? (total / 128) : 127;
}
static inline int calc_ton1(int on, int off)
{
return (on * 100) / (on + off) * 256 / 100;
}
static int et6312b_set_blink(struct et6312b_led *led,
int led_num, int color, int on, int off)
{
u8 period, ton1, ton1_time;
int total_time = on + off;
period = get_address(led, led_num, PERIOD);
ton1 = get_address(led, led_num, TON1);
total_time = calc_period(total_time);
ton1_time = calc_ton1(on, off);
pr_info("blink period %d ms -> %d value, ton1 %d\n",
on + off, total_time, ton1_time);
et6312b_color(led, led_num, color);
et6312b_workmode(led, led_num, color, BLINK);
et6312b_write(led, period, total_time);
et6312b_write(led, ton1, ton1_time);
return 0;
}
static inline void et6312b_clear_delay(struct et6312b_led *led,
int led_num)
{
u8 delay = 0;
delay = get_address(led, led_num, DELAY);
et6312b_write(led, delay, 0);
}
static int et6312b_init(struct et6312b_led *led)
{
int i = 0;
for (i = 0; i < 4; i++) {
/*delete delay set from BL */
et6312b_clear_delay(led, i);
}
return 0;
}
static void et6312b_cancel_timer(void)
{
int led_index = 0;
for (led_index = 0 ; led_index < LED_MAX; led_index++) {
hrtimer_cancel(&et6312b_timer[led_index]);
}
}
static void et6312b_pattern_shutdown(struct led_sep_ops *ops)
{
struct et6312b_led *led = led_sep_get_opsdata(ops);
u8 reg_delay = 0;
int i = 0;
int j = 3;
pr_info("%s\n", __func__);
led_sep_clear_cap(ops, SEP_NODE_CONTROL);
et6312b_cancel_timer();
et6312b_write(led, ET6312B_CHIPCTR, DISABLE);
for (i = 0; i < 4; i++, j--) {
et6312b_set_blink(led, i, RED, 500, 1500);
reg_delay = get_address(led, i, DELAY);
et6312b_write(led, reg_delay, j * 4);
}
et6312b_write(led, ET6312B_CHIPCTR, ENABLE);
dump_reg(led);
}
static void et6312b_pattern_fota(struct led_sep_ops *ops)
{
struct et6312b_led *led = led_sep_get_opsdata(ops);
u8 reg_delay = 0;
int i = 0;
int j = 0;
pr_info("%s\n", __func__);
et6312b_write(led, ET6312B_CHIPCTR, DISABLE);
for (i = 0; i < 4; i++, j++) {
et6312b_set_blink(led, i, WHITE, 250, 750);
reg_delay = get_address(led, i, DELAY);
et6312b_write(led, reg_delay, j * 2);
}
et6312b_write(led, ET6312B_CHIPCTR, ENABLE);
dump_reg(led);
}
static int et6312b_light(struct led_sep_ops *ops,
int id, int on, int color)
{
struct et6312b_led *led = led_sep_get_opsdata(ops);
int i, ret = 0;
pr_info("%s: %d %d color 0x%x\n", __func__,
id, on, color);
et6312b_cancel_timer();
et6312b_write(led, ET6312B_CHIPCTR, DISABLE);
for (i = 0; i < 4; i++) {
if (id & (1 << i)) {
et6312b_color(led, i, color);
et6312b_workmode(led, i, color,
on ? ALWAYS_ON : ALWAYS_OFF);
}
}
et6312b_write(led, ET6312B_CHIPCTR, ENABLE);
return ret;
}
static int et6312b_blink(struct led_sep_ops *ops,
int id, int color, int on_time, int off_time, int count)
{
struct et6312b_led *led = led_sep_get_opsdata(ops);
int total_time = 0;
int i, ret = 0;
pr_info("%s: %d %x %d %d %d\n", __func__,
id, color, on_time, off_time, count);
et6312b_cancel_timer();
total_time = on_time + off_time;
et6312b_write(led, ET6312B_CHIPCTR, DISABLE);
for (i = 0; i < 4; i++) {
if (id & (1 << i)) {
et6312b_set_blink(led, i, color, on_time, off_time);
et6312b_clear_delay(led, i);
if (count)
hrtimer_start(&et6312b_timer[i],
TIMER_SET(total_time, count),
HRTIMER_MODE_REL);
}
}
et6312b_write(led, ET6312B_CHIPCTR, ENABLE);
return ret;
}
static int et6312b_pattern(struct led_sep_ops *ops, int mode)
{
struct et6312b_led *led = led_sep_get_opsdata(ops);
int ret = 1;
pr_info("%s: %s\n", __func__, pattern_string(mode));
switch (mode) {
case PATTERN_OFF:
if (lpcharge)
et6312b_clear(led);
break;
case CHARGING:
et6312b_light(ops, BATTERY, ALWAYS_ON, RED);
break;
case CHARGING_ERR:
et6312b_blink(ops, BATTERY, RED, 500, 500, 0);
break;
case FULLY_CHARGED:
et6312b_light(ops, BATTERY, ALWAYS_ON, GREEN);
break;
case POWERING:
case POWERING_OFF:
/*
* 19s: boot ani -> powering (6)
* -> we will discard this event.
*/
if (led->pattern_count)
et6312b_pattern_shutdown(ops);
break;
case FOTA:
et6312b_pattern_fota(ops);
break;
default:
ret = 0;
break;
}
pr_info("%s: %s Done, count %d\n", __func__,
pattern_string(mode),
led->pattern_count);
led->pattern_count++;
return ret;
}
static struct led_sep_ops et6312b_ops = {
.name = "ET6312B",
.current_max = 255,
.current_lowpower = 255,
.light = et6312b_light,
.test = et6312b_test,
.blink = et6312b_blink,
.reset = et6312b_reset,
.pattern = et6312b_pattern,
.capability = (SEP_NODE_PATTERN |
SEP_NODE_CONTROL |
SEP_NODE_TEST |
SEP_NODE_RESET),
};
static void et6312b_5G_finish_count(struct work_struct *work)
{
et6312b_light(&et6312b_ops, FIVEG, 0, ALWAYS_OFF);
}
static void et6312b_LTE_finish_count(struct work_struct *work)
{
et6312b_light(&et6312b_ops, LTE, 0, ALWAYS_OFF);
}
static void et6312b_WIFI_finish_count(struct work_struct *work)
{
et6312b_light(&et6312b_ops, WIFI, 0, ALWAYS_OFF);
}
static void et6312b_BATTERY_finish_count(struct work_struct *work)
{
et6312b_light(&et6312b_ops, BATTERY, 0, ALWAYS_OFF);
}
static inline enum hrtimer_restart et6312b_5G_timer_func(struct hrtimer *timer)
{
queue_work(et6312b_workqueue, &et6312b_work[0]);
return HRTIMER_NORESTART;
}
static enum hrtimer_restart et6312b_LTE_timer_func(struct hrtimer *timer)
{
queue_work(et6312b_workqueue, &et6312b_work[1]);
return HRTIMER_NORESTART;
}
static enum hrtimer_restart et6312b_WIFI_timer_func(struct hrtimer *timer)
{
queue_work(et6312b_workqueue, &et6312b_work[2]);
return HRTIMER_NORESTART;
}
static enum hrtimer_restart et6312b_BATTERY_timer_func(struct hrtimer *timer)
{
queue_work(et6312b_workqueue, &et6312b_work[3]);
return HRTIMER_NORESTART;
}
static int parse_rgb(struct et6312b_led *led,
char *rgb_str, int led_num)
{
struct device_node *np = led->i2c->dev.of_node;
int index = 0;
int ret = 0;
ret = of_property_read_u32(np, rgb_str, &index);
if (ret) {
pr_info("error for reading %s, %d\n", rgb_str, ret);
return -EINVAL;
}
if (index >= LED_MAX || index < 0) {
pr_info("out of range for %s. led is changed %d -> %d\n",
rgb_str, index, led_num);
index = led_num;
ret = -EINVAL;
}
switch (led_num) {
case 0: led->rgb[index] = rgb0; break;
case 1: led->rgb[index] = rgb1; break;
case 2: led->rgb[index] = rgb2; break;
case 3: led->rgb[index] = rgb3; break;
default:
pr_info("out of range for led%d\n", led_num);
ret = -EINVAL;
}
return ret;
}
static int et6312b_parse_dt(struct et6312b_led *led)
{
struct device_node *np = led->i2c->dev.of_node;
const char *power;
int ret = 0;
pr_info("%s\n", __func__);
ret = of_property_read_string(np, "et6312b,power", &power);
if (ret) {
pr_info("error for reading et6312b,power\n");
ret = -EINVAL;
} else {
pr_info("power : %s\n", power);
led->vdd = regulator_get(NULL, power);
if (IS_ERR_OR_NULL(led->vdd)) {
pr_err("%s: Failed to get %s regulator.\n",
__func__, power);
led->vdd = NULL;
ret = PTR_ERR(led->vdd);
}
}
ret = parse_rgb(led, "et6312b,rgb0", 0);
ret = parse_rgb(led, "et6312b,rgb1", 1);
ret = parse_rgb(led, "et6312b,rgb2", 2);
ret = parse_rgb(led, "et6312b,rgb3", 3);
return ret;
}
static int et6312b_led_probe(struct i2c_client *i2c,
const struct i2c_device_id *i2c_id)
{
struct et6312b_led *led;
int led_num, ret;
dev_info(&i2c->dev, "%s\n", __func__);
led = devm_kzalloc(&i2c->dev,
sizeof(struct et6312b_led),
GFP_KERNEL);
if (!led) {
pr_err("%s Not enough memory.\n", __func__);
return -ENOMEM;
}
led->i2c = i2c;
i2c_set_clientdata(i2c, led);
ret = et6312b_parse_dt(led);
if (ret) {
pr_info("%s: error while parsing values from dt\n", __func__);
return ret;
}
et6312b_power(led, true);
et6312b_init(led);
for (led_num = 0; led_num < LED_MAX; led_num++) {
hrtimer_init(&et6312b_timer[led_num],
CLOCK_MONOTONIC, HRTIMER_MODE_REL);
#if 0
et6312b_timer[led_num].function = et6312b_timer_func;
INIT_WORK(&et6312b_work[i], et6312b_finish_count);
#endif
}
et6312b_timer[0].function = et6312b_5G_timer_func;
INIT_WORK(&et6312b_work[0], et6312b_5G_finish_count);
et6312b_timer[1].function = et6312b_LTE_timer_func;
INIT_WORK(&et6312b_work[1], et6312b_LTE_finish_count);
et6312b_timer[2].function = et6312b_WIFI_timer_func;
INIT_WORK(&et6312b_work[2], et6312b_WIFI_finish_count);
et6312b_timer[3].function = et6312b_BATTERY_timer_func;
INIT_WORK(&et6312b_work[3], et6312b_BATTERY_finish_count);
et6312b_workqueue = create_singlethread_workqueue("et6312b");
led_sep_set_opsdata(&et6312b_ops, led);
led_sep_register_device(&et6312b_ops);
dev_info(&i2c->dev, "%s power on -> Done\n", __func__);
return 0;
}
static int et6312b_led_remove(struct i2c_client *i2c)
{
struct et6312b_led *led = i2c_get_clientdata(i2c);
dev_info(&i2c->dev, "%s\n", __func__);
destroy_workqueue(et6312b_workqueue);
et6312b_clear(led);
regulator_put(led->vdd);
dev_info(&i2c->dev, "%s Done\n", __func__);
return 0;
}
static void et6312b_led_shutdown(struct i2c_client *i2c)
{
dev_info(&i2c->dev, "%s\n", __func__);
et6312b_led_remove(i2c);
dev_info(&i2c->dev, "%s Done\n", __func__);
}
static const struct i2c_device_id et6312b_i2c_id[] = {
{ "ET6312B", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c, et6312b_i2c_id);
static const struct of_device_id et6312b_led_of_match[] = {
{ .compatible = "samsung,et6312b", },
{ }
};
static struct i2c_driver et6312b_led_driver = {
.driver = {
.owner = THIS_MODULE,
.name = "sled_et6312b",
.of_match_table = of_match_ptr(et6312b_led_of_match),
},
.probe = et6312b_led_probe,
.remove = et6312b_led_remove,
.shutdown = et6312b_led_shutdown,
.id_table = et6312b_i2c_id,
};
static int __init et6312b_i2c_init(void)
{
pr_info("%s\n", __func__);
return i2c_add_driver(&et6312b_led_driver);
}
module_init(et6312b_i2c_init);
static void __exit et6312b_i2c_exit(void)
{
i2c_del_driver(&et6312b_led_driver);
}
module_exit(et6312b_i2c_exit);
MODULE_AUTHOR("Samsung EIF Team");
|