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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
|
/*
* smb347_charger.c
* Samsung SMB347 Charger Driver
*
* Copyright (C) 2012 Samsung Electronics
*
*
* 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.
*/
#define DEBUG
#include <linux/battery/sec_charger.h>
static int smb347_i2c_write(struct i2c_client *client,
int reg, u8 *buf)
{
int ret;
ret = i2c_smbus_write_i2c_block_data(client, reg, 1, buf);
if (ret < 0)
dev_err(&client->dev, "%s: Error(%d)\n", __func__, ret);
return ret;
}
static int smb347_i2c_read(struct i2c_client *client,
int reg, u8 *buf)
{
int ret;
ret = i2c_smbus_read_i2c_block_data(client, reg, 1, buf);
if (ret < 0)
dev_err(&client->dev, "%s: Error(%d)\n", __func__, ret);
return ret;
}
static void smb347_i2c_write_array(struct i2c_client *client,
u8 *buf, int size)
{
int i;
for (i = 0; i < size; i += 3)
smb347_i2c_write(client, (u8) (*(buf + i)), (buf + i) + 1);
}
static void smb347_set_command(struct i2c_client *client,
int reg, int datum)
{
int val;
u8 data = 0;
val = smb347_i2c_read(client, reg, &data);
if (val >= 0) {
dev_dbg(&client->dev, "%s : reg(0x%02x): 0x%02x",
__func__, reg, data);
if (data != datum) {
data = datum;
if (smb347_i2c_write(client, reg, &data) < 0)
dev_err(&client->dev,
"%s : error!\n", __func__);
val = smb347_i2c_read(client, reg, &data);
if (val >= 0)
dev_dbg(&client->dev, " => 0x%02x\n", data);
}
}
}
static void smb347_test_read(struct i2c_client *client)
{
u8 data = 0;
u32 addr = 0;
for (addr = 0; addr <= 0x0f; addr++) {
smb347_i2c_read(client, addr, &data);
dev_dbg(&client->dev,
"smb347 addr : 0x%02x data : 0x%02x\n", addr, data);
}
for (addr = 0x30; addr <= 0x3f; addr++) {
smb347_i2c_read(client, addr, &data);
dev_dbg(&client->dev,
"smb347 addr : 0x%02x data : 0x%02x\n", addr, data);
}
}
static void smb347_read_regs(struct i2c_client *client, char *str)
{
u8 data = 0;
u32 addr = 0;
for (addr = 0; addr <= 0x0f; addr++) {
smb347_i2c_read(client, addr, &data);
sprintf(str+strlen(str), "0x%x, ", data);
}
/* "#" considered as new line in application */
sprintf(str+strlen(str), "#");
for (addr = 0x30; addr <= 0x3f; addr++) {
smb347_i2c_read(client, addr, &data);
sprintf(str+strlen(str), "0x%x, ", data);
}
}
static int smb347_read_reg(struct i2c_client *client, int reg)
{
int ret;
ret = i2c_smbus_read_byte_data(client, reg);
if (ret < 0) {
pr_err("%s: err %d, try again!\n", __func__, ret);
ret = i2c_smbus_read_byte_data(client, reg);
if (ret < 0)
pr_err("%s: err %d\n", __func__, ret);
}
return ret;
}
static int smb347_get_charging_status(struct i2c_client *client)
{
int status = POWER_SUPPLY_STATUS_UNKNOWN;
u8 data_a = 0;
u8 data_b = 0;
u8 data_c = 0;
u8 data_d = 0;
u8 data_e = 0;
smb347_i2c_read(client, SMB347_STATUS_A, &data_a);
dev_info(&client->dev,
"%s : charger status A(0x%02x)\n", __func__, data_a);
smb347_i2c_read(client, SMB347_STATUS_B, &data_b);
dev_info(&client->dev,
"%s : charger status B(0x%02x)\n", __func__, data_b);
smb347_i2c_read(client, SMB347_STATUS_C, &data_c);
dev_info(&client->dev,
"%s : charger status C(0x%02x)\n", __func__, data_c);
smb347_i2c_read(client, SMB347_STATUS_D, &data_d);
dev_info(&client->dev,
"%s : charger status D(0x%02x)\n", __func__, data_d);
smb347_i2c_read(client, SMB347_STATUS_E, &data_e);
dev_info(&client->dev,
"%s : charger status E(0x%02x)\n", __func__, data_e);
/* At least one charge cycle terminated,
* Charge current < Termination Current
*/
if ((data_c & 0x20) == 0x20) {
/* top-off by full charging */
status = POWER_SUPPLY_STATUS_FULL;
goto charging_status_end;
}
/* Is enabled ? */
if (data_c & 0x01) {
/* check for 0x06 : no charging (0b00) */
/* not charging */
if (!(data_c & 0x06)) {
status = POWER_SUPPLY_STATUS_NOT_CHARGING;
goto charging_status_end;
} else {
status = POWER_SUPPLY_STATUS_CHARGING;
goto charging_status_end;
}
} else
status = POWER_SUPPLY_STATUS_DISCHARGING;
charging_status_end:
return (int)status;
}
static int smb347_get_charging_health(struct i2c_client *client)
{
int health = POWER_SUPPLY_HEALTH_GOOD;
u8 data_a = 0;
u8 data_b = 0;
u8 data_c = 0;
u8 data_d = 0;
u8 data_e = 0;
smb347_i2c_read(client, SMB347_STATUS_A, &data_a);
dev_info(&client->dev,
"%s : charger status A(0x%02x)\n", __func__, data_a);
smb347_i2c_read(client, SMB347_STATUS_B, &data_b);
dev_info(&client->dev,
"%s : charger status B(0x%02x)\n", __func__, data_b);
smb347_i2c_read(client, SMB347_STATUS_C, &data_c);
dev_info(&client->dev,
"%s : charger status C(0x%02x)\n", __func__, data_c);
smb347_i2c_read(client, SMB347_STATUS_D, &data_d);
dev_info(&client->dev,
"%s : charger status D(0x%02x)\n", __func__, data_d);
smb347_i2c_read(client, SMB347_STATUS_E, &data_e);
dev_info(&client->dev,
"%s : charger status E(0x%02x)\n", __func__, data_e);
/* Is enabled ? */
if (data_c & 0x01) {
if (!(data_a & 0x02)) /* Input current is NOT OK */
health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
}
return (int)health;
}
static void smb347_allow_volatile_writes(struct i2c_client *client)
{
int val, reg;
u8 data;
reg = SMB347_COMMAND_A;
val = smb347_i2c_read(client, reg, &data);
if ((val >= 0) && !(data & 0x80)) {
dev_dbg(&client->dev,
"%s : reg(0x%02x): 0x%02x", __func__, reg, data);
data |= (0x1 << 7);
if (smb347_i2c_write(client, reg, &data) < 0)
dev_err(&client->dev, "%s : error!\n", __func__);
val = smb347_i2c_read(client, reg, &data);
if (val >= 0) {
data = (u8) data;
dev_dbg(&client->dev, " => 0x%02x\n", data);
}
}
}
static u8 smb347_get_float_voltage_data(
int float_voltage)
{
u8 data;
if (float_voltage < 3500)
float_voltage = 3500;
data = (float_voltage - 3500) / 20;
return data;
}
static u8 smb347_get_input_current_limit_data(
struct sec_charger_info *charger, int input_current)
{
u8 data;
if (input_current <= 300)
data = 0x0;
else if (input_current <= 500)
data = 0x1;
else if (input_current <= 700)
data = 0x2;
else if (input_current <= 900)
data = 0x3;
else if (input_current <= 1200)
data = 0x4;
else if (input_current <= 1500)
data = 0x5;
else if (input_current <= 1800)
data = 0x6;
else if (input_current <= 2000)
data = 0x7;
else if (input_current <= 2200)
data = 0x8;
else if (input_current <= 2500)
data = 0x9;
else
data = 0;
return data;
}
static u8 smb347_get_termination_current_limit_data(
int termination_current)
{
u8 data;
if (termination_current <= 37)
data = 0x0;
else if (termination_current <= 50)
data = 0x1;
else if (termination_current <= 100)
data = 0x2;
else if (termination_current <= 150)
data = 0x3;
else if (termination_current <= 200)
data = 0x4;
else if (termination_current <= 250)
data = 0x5;
else if (termination_current <= 500)
data = 0x6;
else if (termination_current <= 600)
data = 0x7;
else
data = 0;
return data;
}
static u8 smb347_get_fast_charging_current_data(
int fast_charging_current)
{
u8 data;
if (fast_charging_current <= 700)
data = 0x0;
else if (fast_charging_current <= 900)
data = 0x1;
else if (fast_charging_current <= 1200)
data = 0x2;
else if (fast_charging_current <= 1500)
data = 0x3;
else if (fast_charging_current <= 1800)
data = 0x4;
else if (fast_charging_current <= 2000)
data = 0x5;
else if (fast_charging_current <= 2200)
data = 0x6;
else if (fast_charging_current <= 2500)
data = 0x7;
else
data = 0;
return data << 5;
}
static void smb347_charger_function_conrol(
struct i2c_client *client)
{
struct sec_charger_info *charger = i2c_get_clientdata(client);
u8 data;
if (charger->charging_current < 0) {
dev_dbg(&client->dev,
"%s : OTG is activated. Ignore command!\n", __func__);
return;
}
smb347_allow_volatile_writes(client);
if (charger->cable_type ==
POWER_SUPPLY_TYPE_BATTERY) {
/* turn off charger */
smb347_set_command(client,
SMB347_COMMAND_A, 0x80);
/* high current mode for system current */
smb347_set_command(client,
SMB347_COMMAND_B, 0x01);
} else {
/* Pre-charge curr 250mA */
dev_dbg(&client->dev,
"%s : fast charging current (%dmA)\n",
__func__, charger->charging_current);
dev_dbg(&client->dev,
"%s : termination current (%dmA)\n",
__func__, charger->pdata->charging_current[
charger->cable_type].full_check_current_1st);
data = 0x1c;
data |= smb347_get_fast_charging_current_data(
charger->charging_current);
data |= smb347_get_termination_current_limit_data(
charger->pdata->charging_current[
charger->cable_type].full_check_current_1st);
smb347_set_command(client,
SMB347_CHARGE_CURRENT, data);
/* Pin enable control */
/* DCIN Input Pre-bias Enable */
data = 0x01;
if (charger->pdata->chg_gpio_en)
data |= 0x40;
if (charger->pdata->chg_polarity_en)
data |= 0x20;
smb347_set_command(client,
SMB347_PIN_ENABLE_CONTROL, data);
/* Input current limit */
dev_dbg(&client->dev, "%s : input current (%dmA)\n",
__func__, charger->pdata->charging_current
[charger->cable_type].input_current_limit);
data = 0;
data = smb347_get_input_current_limit_data(
charger,
charger->pdata->charging_current
[charger->cable_type].input_current_limit);
smb347_set_command(client,
SMB347_INPUT_CURRENTLIMIT, data);
/*
* Input to System FET by Register
* Enable AICL, VCHG
* Max System voltage =Vflt + 0.1v
* Input Source Priority : USBIN
*/
if (charger->pdata->chg_functions_setting &
SEC_CHARGER_NO_GRADUAL_CHARGING_CURRENT)
/* disable AICL */
smb347_set_command(client,
SMB347_VARIOUS_FUNCTIONS, 0x85);
else
/* enable AICL */
smb347_set_command(client,
SMB347_VARIOUS_FUNCTIONS, 0x95);
/* Float voltage, Vprechg : 2.4V */
dev_dbg(&client->dev, "%s : float voltage (%dmV)\n",
__func__, charger->pdata->chg_float_voltage);
data = 0;
data |= smb347_get_float_voltage_data(
charger->pdata->chg_float_voltage);
smb347_set_command(client,
SMB347_FLOAT_VOLTAGE, data);
/* Charge control
* Automatic Recharge disable,
* Current Termination disable,
* BMD disable, Recharge Threshold =50mV,
* APSD disable */
data = 0xC0;
switch (charger->pdata->full_check_type) {
case SEC_BATTERY_FULLCHARGED_CHGGPIO:
case SEC_BATTERY_FULLCHARGED_CHGINT:
case SEC_BATTERY_FULLCHARGED_CHGPSY:
/* Enable Current Termination */
data &= 0xBF;
break;
default:
break;
}
smb347_set_command(client,
SMB347_CHARGE_CONTROL, data);
/* STAT, Timer control : STAT active low,
* Complete time out 1527min.
*/
smb347_set_command(client,
SMB347_STAT_TIMERS_CONTROL, 0x1A);
/* Pin/Enable
* USB 5/1/HC Dual state
* DCIN pre-bias Enable
*/
smb347_set_command(client,
SMB347_PIN_ENABLE_CONTROL, 0x09);
/* Therm control :
* Therm monitor disable,
* Minimum System Voltage 3.60V
*/
smb347_set_command(client,
SMB347_THERM_CONTROL_A, 0x7F);
/* USB selection : USB2.0(100mA/500mA),
* INOK polarity Active low
*/
smb347_set_command(client,
SMB347_SYSOK_USB30_SELECTION, 0x08);
/* Other control
* Low batt detection disable
* Minimum System Voltage 3.60V
*/
smb347_set_command(client,
SMB347_OTHER_CONTROL_A, 0x00);
/* OTG tlim therm control */
smb347_set_command(client,
SMB347_OTG_TLIM_THERM_CONTROL, 0x3F);
/* Limit cell temperature */
smb347_set_command(client,
SMB347_LIMIT_CELL_TEMPERATURE_MONITOR, 0x01);
/* Fault interrupt : Clear */
smb347_set_command(client,
SMB347_FAULT_INTERRUPT, 0x00);
/* STATUS ingerrupt : Clear */
smb347_set_command(client,
SMB347_STATUS_INTERRUPT, 0x00);
/* turn on charger */
smb347_set_command(client,
SMB347_COMMAND_A, 0xC2);
/* HC or USB5 mode */
switch (charger->cable_type) {
case POWER_SUPPLY_TYPE_MAINS:
case POWER_SUPPLY_TYPE_MISC:
/* High-current mode */
data = 0x01;
break;
case POWER_SUPPLY_TYPE_USB:
case POWER_SUPPLY_TYPE_USB_DCP:
case POWER_SUPPLY_TYPE_USB_CDP:
case POWER_SUPPLY_TYPE_USB_ACA:
/* USB5 */
data = 0x02;
break;
default:
/* USB1 */
data = 0x00;
break;
}
smb347_set_command(client,
SMB347_COMMAND_B, data);
}
}
static void smb347_charger_otg_conrol(
struct i2c_client *client)
{
struct sec_charger_info *charger = i2c_get_clientdata(client);
smb347_allow_volatile_writes(client);
if (charger->cable_type ==
POWER_SUPPLY_TYPE_BATTERY) {
/* turn off charger */
smb347_set_command(client,
SMB347_COMMAND_A, 0x80);
} else {
/* turn on OTG */
smb347_set_command(client,
SMB347_COMMAND_A, (0x1 << 4));
}
}
static int smb347_check_charging_status(struct i2c_client *client)
{
int val, reg;
u8 data = 0;
int ret = -1;
reg = SMB347_STATUS_C; /* SMB328A_BATTERY_CHARGING_STATUS_C */
val = smb347_read_reg(client, reg);
if (val >= 0) {
data = (u8) val;
pr_debug("%s : reg (0x%x) = 0x%x\n", __func__, reg, data);
ret = (data & (0x3 << 1)) >> 1;
pr_debug("%s : status = 0x%x\n", __func__, data);
}
return ret;
}
bool sec_hal_chg_init(struct i2c_client *client)
{
smb347_test_read(client);
return true;
}
bool sec_hal_chg_suspend(struct i2c_client *client)
{
return true;
}
bool sec_hal_chg_resume(struct i2c_client *client)
{
return true;
}
bool sec_hal_chg_get_property(struct i2c_client *client,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct sec_charger_info *charger = i2c_get_clientdata(client);
u8 data;
switch (psp) {
case POWER_SUPPLY_PROP_STATUS:
val->intval = smb347_get_charging_status(client);
break;
case POWER_SUPPLY_PROP_CHARGE_TYPE:
switch (smb347_check_charging_status(client)) {
case 0:
val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
break;
case 1:
val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
break;
case 2:
val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
break;
case 3:
val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
break;
default:
pr_err("%s : get charge type error!\n", __func__);
return -EINVAL;
}
break;
case POWER_SUPPLY_PROP_HEALTH:
val->intval = smb347_get_charging_health(client);
break;
case POWER_SUPPLY_PROP_CURRENT_NOW:
if (charger->charging_current) {
smb347_i2c_read(client, SMB347_STATUS_B, &data);
if (data & 0x20)
switch (data & 0x18) {
case 0:
val->intval = 100;
break;
case 1:
val->intval = 150;
break;
case 2:
val->intval = 200;
break;
case 3:
val->intval = 250;
break;
}
else
switch (data & 0x07) {
case 0:
val->intval = 700;
break;
case 1:
val->intval = 900;
break;
case 2:
val->intval = 1200;
break;
case 3:
val->intval = 1500;
break;
case 4:
val->intval = 1800;
break;
case 5:
val->intval = 2000;
break;
case 6:
val->intval = 2200;
break;
case 7:
val->intval = 2500;
break;
}
} else
val->intval = 0;
dev_dbg(&client->dev,
"%s : set-current(%dmA), current now(%dmA)\n",
__func__, charger->charging_current, val->intval);
break;
default:
return false;
}
return true;
}
bool sec_hal_chg_set_property(struct i2c_client *client,
enum power_supply_property psp,
const union power_supply_propval *val)
{
struct sec_charger_info *charger = i2c_get_clientdata(client);
switch (psp) {
/* val->intval : type */
case POWER_SUPPLY_PROP_ONLINE:
/* val->intval : charging current */
case POWER_SUPPLY_PROP_CURRENT_NOW:
if (charger->charging_current < 0)
smb347_charger_otg_conrol(client);
else if (charger->charging_current > 0)
smb347_charger_function_conrol(client);
else {
smb347_charger_function_conrol(client);
smb347_charger_otg_conrol(client);
}
smb347_test_read(client);
break;
default:
return false;
}
return true;
}
ssize_t sec_hal_chg_show_attrs(struct device *dev,
const ptrdiff_t offset, char *buf)
{
struct power_supply *psy = dev_get_drvdata(dev);
struct sec_charger_info *chg =
container_of(psy, struct sec_charger_info, psy_chg);
int i = 0;
char *str = NULL;
switch (offset) {
case CHG_DATA:
i += scnprintf(buf + i, PAGE_SIZE - i, "%x\n",
chg->reg_data);
break;
case CHG_REGS:
str = kzalloc(sizeof(char)*1024, GFP_KERNEL);
if (!str)
return -ENOMEM;
smb347_read_regs(chg->client, str);
i += scnprintf(buf + i, PAGE_SIZE - i, "%s\n",
str);
kfree(str);
break;
default:
i = -EINVAL;
break;
}
return i;
}
ssize_t sec_hal_chg_store_attrs(struct device *dev,
const ptrdiff_t offset,
const char *buf, size_t count)
{
struct power_supply *psy = dev_get_drvdata(dev);
struct sec_charger_info *chg =
container_of(psy, struct sec_charger_info, psy_chg);
int ret = 0;
int x = 0;
u8 data = 0;
switch (offset) {
case CHG_REG:
if (sscanf(buf, "%x\n", &x) == 1) {
chg->reg_addr = x;
smb347_i2c_read(chg->client,
chg->reg_addr, &data);
chg->reg_data = data;
dev_dbg(dev, "%s: (read) addr = 0x%x, data = 0x%x\n",
__func__, chg->reg_addr, chg->reg_data);
ret = count;
}
break;
case CHG_DATA:
if (sscanf(buf, "%x\n", &x) == 1) {
data = (u8)x;
dev_dbg(dev, "%s: (write) addr = 0x%x, data = 0x%x\n",
__func__, chg->reg_addr, data);
smb347_i2c_write(chg->client,
chg->reg_addr, &data);
ret = count;
}
break;
default:
ret = -EINVAL;
break;
}
return ret;
}
|