aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/battery/bq24190_charger.c
blob: a68572cd7ef8cbff41b1ce0b8d0a7815380e6221 (plain)
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
/*
 *  bq24190_charger.c
 *  Samsung BQ24190 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 bq24190_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 bq24190_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;
}

#if 0
static void bq24190_i2c_write_array(struct i2c_client *client, u8 *buf,
				    int size)
{
	int i;
	for (i = 0; i < size; i += 3)
		bq24190_i2c_write(client, (u8) (*(buf + i)), (buf + i) + 1);
}
#endif

static int bq24190_read_regs(struct i2c_client *client, char *str)
{
	u8 data = 0;
	u32 addr = 0;

	if (!client)
		return -EINVAL;

	for (addr = 0; addr <= 0x0A; addr++) {
		bq24190_i2c_read(client, addr, &data);
		sprintf(str+strlen(str), "0x%02x", data);
		if (addr != 0x0A)
			sprintf(str+strlen(str), ", ");
	}

	return 0;
}

static int bq24190_test_read(struct i2c_client *client)
{
	struct sec_charger_info *charger;
	u8 data = 0;
	u32 addr = 0;

	if (!client)
		return -EINVAL;

	for (addr = 0; addr <= 0x0A; addr++) {
		bq24190_i2c_read(client, addr, &data);
		dev_info(&client->dev,
			"bq24190 addr : 0x%02x data : 0x%02x\n",
			addr, data);
	}

	charger = i2c_get_clientdata(client);

	dev_info(&client->dev,
		"bq24190 EN(%d), nCHG(%d), nCON(%d)\n",
		gpio_get_value(charger->pdata->chg_gpio_en),
		gpio_get_value(charger->pdata->chg_gpio_status),
		gpio_get_value(charger->pdata->bat_gpio_ta_nconnected));

	return 0;
}

static int bq24190_enable_charging(struct i2c_client *client)
{
	struct sec_charger_info *charger;
	u8 data = 0;

	if (!client)
		return -EINVAL;

	charger = i2c_get_clientdata(client);

	/* Set CE pin to LOW */
	if (charger->pdata->chg_gpio_en)
		gpio_set_value(charger->pdata->chg_gpio_en,
			charger->pdata->chg_polarity_en ? 1 : 0);

	/* Set register */
	bq24190_i2c_read(client, BQ24190_REG_PWRON_CFG, &data);
	data = data & ~(0x3 << 4) | (0x1 << 4);
	bq24190_i2c_write(client, BQ24190_REG_PWRON_CFG, &data);

	return 0;
}

static int bq24190_disable_charging(struct i2c_client *client)
{
	struct sec_charger_info *charger;
	u8 data = 0;

	if (!client)
		return -EINVAL;

	charger = i2c_get_clientdata(client);

	/* Set CE pin to HIGH */
	if (charger->pdata->chg_gpio_en)
		gpio_set_value(charger->pdata->chg_gpio_en,
			charger->pdata->chg_polarity_en ? 0 : 1);

	/* Set register */
	bq24190_i2c_read(client, BQ24190_REG_PWRON_CFG, &data);
	data = data & ~(0x3 << 4);
	bq24190_i2c_write(client, BQ24190_REG_PWRON_CFG, &data);

	return 0;
}

static int bq24190_get_status(struct i2c_client *client, u8 *status)
{
	u8 data = 0;

	if (!client)
		return -EINVAL;

	bq24190_i2c_read(client, BQ24190_REG_STATUS, &data);

	*status = data;

	/* [7:6] VBUS_STAT */
	switch (data >> 6) {
	case 0:
		dev_dbg(&client->dev,
			"%s: 0x%02x VBUS_STAT: Unknown\n",
			__func__, data);
		break;
	case 1:
		dev_dbg(&client->dev,
			"%s: 0x%02x VBUS_STAT: USB host\n",
			__func__, data);
		break;
	case 2:
		dev_dbg(&client->dev,
			"%s: 0x%02x VBUS_STAT: Adapter port\n",
			__func__, data);
		break;
	case 3:
		dev_dbg(&client->dev,
			"%s: 0x%02x VBUS_STAT: OTG\n",
			__func__, data);
		break;
	}

	/* [5:4] CHRG_STAT */
	switch ((data & (0x3 << 4)) >> 4) {
	case 0:
		dev_dbg(&client->dev,
			"%s: 0x%02x CHRG_STAT: Not Charging\n",
			__func__, data);
		break;
	case 1:
		dev_dbg(&client->dev,
			"%s: 0x%02x CHRG_STAT: Pre-charge\n",
			__func__, data);
		break;
	case 2:
		dev_dbg(&client->dev,
			"%s: 0x%02x CHRG_STAT: Fast Charging\n",
			__func__, data);
		break;
	case 3:
		dev_dbg(&client->dev,
			"%s: 0x%02x CHRG_STAT: Charge Done\n",
			__func__, data);
		break;
	}

	/* [3] DPM_STAT */
	switch ((data & (0x1 << 3)) >> 3) {
	case 0:
		dev_dbg(&client->dev,
			"%s: 0x%02x DPM_STAT: Not DPM\n",
			__func__, data);
		break;
	case 1:
		dev_dbg(&client->dev,
			"%s: 0x%02x DPM_STAT: VINDPM or IINDPM\n",
			__func__, data);
		break;
	}

	/* [2] PG_STAT */
	switch ((data & (0x1 << 2)) >> 2) {
	case 0:
		dev_dbg(&client->dev,
			"%s: 0x%02x PG_STAT: Not Power Good\n",
			__func__, data);
		break;
	case 1:
		dev_dbg(&client->dev,
			"%s: 0x%02x PG_STAT: Power Good\n",
			__func__, data);
		break;
	}

	/* [1] THERM_STAT */
	switch ((data & (0x1 << 1)) >> 1) {
	case 0:
		dev_dbg(&client->dev,
			"%s: 0x%02x THERM_STAT: Normal\n",
			__func__, data);
		break;
	case 1:
		dev_dbg(&client->dev,
			"%s: 0x%02x THERM_STAT: TREG\n",
			__func__, data);
		break;
	}

	/* [0] VSYS_STAT */
	switch (data & 0x1) {
	case 0:
		dev_dbg(&client->dev,
			"%s: 0x%02x VSYS_STAT: BAT > VSYSMIN\n",
			__func__, data);
		break;
	case 1:
		dev_dbg(&client->dev,
			"%s: 0x%02x VSYS_STAT: Battery is too low\n",
			__func__, data);
		break;
	}

	return 0;
}

static int bq24190_get_fault(struct i2c_client *client, u8 *fault)
{
	u8 data = 0;

	if (!client)
		return -EINVAL;

	bq24190_i2c_read(client, BQ24190_REG_FAULT, &data);

	*fault = data;

	if (data == 0x00)
		dev_dbg(&client->dev, "%s: 0x%02x No fault\n",
			__func__, data);
	else {
		if (data & (0x1 << 7))
			dev_info(&client->dev,
				"%s: 0x%02x watchdog timer expiration\n",
				__func__, data);
		if (data & (0x1 << 6))
			dev_info(&client->dev,
				"%s: 0x%02x OTG fault\n", __func__, data);
		if (data & (0x1 << 4))
			dev_info(&client->dev,
				"%s: 0x%02x Input fault (OVP or bad source)\n",
				__func__, data);
		if (data & (0x2 << 4))
			dev_info(&client->dev,
				"%s: 0x%02x Thermal shutdown\n",
				__func__, data);
		if (data & (0x3 << 4))
			dev_info(&client->dev,
				"%s: 0x%02x Charge timer expiration\n",
				__func__, data);
		if (data & (0x1 << 3))
			dev_info(&client->dev,
				"%s: 0x%02x System OVP\n", __func__, data);
		if (data & (0x1 << 0))
			dev_info(&client->dev,
				"%s: 0x%02x TS1 Cold\n", __func__, data);
		if (data & (0x2 << 0))
			dev_info(&client->dev,
				"%s: 0x%02x TS1 Hot\n", __func__, data);
		if (data & (0x3 << 0))
			dev_info(&client->dev,
				"%s: 0x%02x TS2 Cold\n", __func__, data);
		if (data & (0x4 << 0))
			dev_info(&client->dev,
				"%s: 0x%02x TS2 Hot\n", __func__, data);
		if (data & (0x5 << 0))
			dev_info(&client->dev,
				"%s: 0x%02x Both Cold\n", __func__, data);
		if (data & (0x6 << 0))
			dev_info(&client->dev,
				"%s: 0x%02x Both Hot\n", __func__, data);
		if (data & (0x7 << 0))
			dev_info(&client->dev,
				"%s: 0x%02x one Hot one Cold\n",
				__func__, data);
	}

	return 0;
}

static int bq24190_get_charging_status(struct i2c_client *client)
{
	struct sec_charger_info *charger;
	int status = POWER_SUPPLY_STATUS_UNKNOWN;
	u8 data_status = 0;
	u8 data_fault = 0;

	if (!client)
		return -EINVAL;

	charger = i2c_get_clientdata(client);

	if (bq24190_get_status(client, &data_status) < 0)
		dev_err(&client->dev, "%s: fail to get status\n", __func__);

	if (bq24190_get_fault(client, &data_fault) < 0)
		dev_err(&client->dev, "%s: fail to get fault\n", __func__);

#if 0	/* CHECK ME */
	/* At least one charge cycle terminated,
	 *Charge current < Termination Current
	 */
	if ((data_status & BQ24190_CHARGING_DONE) == 0x30)
		status = POWER_SUPPLY_STATUS_FULL;
	goto charging_status_end;
	if (data_status & BQ24190_CHARGING_ENABLE)
		status = POWER_SUPPLY_STATUS_CHARGING;
	goto charging_status_end;
	if (data_fault)

		/* if error bit check, ignore the status of charger-ic */
		status = POWER_SUPPLY_STATUS_DISCHARGING;
charging_status_end:
#endif

	return (int)status;
}

static int bq24190_get_charging_health(struct i2c_client *client)
{
	int health = POWER_SUPPLY_HEALTH_GOOD;
	u8 data_fault = 0;

	if (!client)
		return -EINVAL;

	if (bq24190_get_fault(client, &data_fault) < 0)
		dev_err(&client->dev, "%s: fail to get fault\n", __func__);

#if 0	/* CHECK ME */
	if (data_fault)
		pr_info("%s : Fault (0x%02x)\n", __func__, data_fault);
	if ((data_fault & BQ24190_CHARGING_DONE) == 0x20)
		health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
	goto charging_health_end;
	if (data_fault)

		/* if error bit check, ignore the status of charger-ic */
		health = POWER_SUPPLY_HEALTH_GOOD;
charging_health_end:
#endif

	return health;
}

static int bq24190_get_charging_current(struct i2c_client *client)
{
	u8 data = 0;
	int ichg = 500;

	if (!client)
		return -EINVAL;

	bq24190_i2c_read(client, BQ24190_REG_CHRG_C, &data);

	if (data & (0x1 << 7))
		ichg += 2048;
	if (data & (0x1 << 6))
		ichg += 1024;
	if (data & (0x1 << 5))
		ichg += 512;
	if (data & (0x1 << 4))
		ichg += 256;
	if (data & (0x1 << 3))
		ichg += 128;
	if (data & (0x1 << 2))
		ichg += 64;

	return ichg;
}

static int bq24190_set_charging_current(
				struct i2c_client *client, int set_current)
{
	u8 data = 0;

	if (!client)
		return -EINVAL;

	if (set_current > 500) {

		/* High-current mode */
		data = 0x48;
		bq24190_i2c_write(client, BQ24190_REG_CHRG_C, &data);
		udelay(10);
	} else {

		/* USB5 */
		data = 0x00;
		bq24190_i2c_write(client, BQ24190_REG_CHRG_C, &data);
		udelay(10);
	}

	dev_dbg(&client->dev, "%s: Set charging current as %dmA.\n",
		__func__, set_current);

	return 0;
}

static int bq24190_set_input_current_limit(struct i2c_client *client,
					     int charger_type)
{
	u8 data = 0;

	if (!client)
		return -EINVAL;

	bq24190_i2c_read(client, BQ24190_REG_INSRC, &data);
	data = data & ~(0x7);	/* clear IINLIM bits */

	switch (charger_type) {
	case POWER_SUPPLY_TYPE_MAINS:
		/* 2A (110) */
		data = data | (0x6);
		break;
	case POWER_SUPPLY_TYPE_USB:
	default:
		/* 500mA (010) */
		data = data | (0x2);
		break;
	}

	bq24190_i2c_write(client, BQ24190_REG_INSRC, &data);

	return 0;
}

static int bq24190_set_fast_charge_current(struct i2c_client *client,
					     int charger_type)
{
	u8 data = 0;

	if (!client)
		return -EINVAL;

	bq24190_i2c_read(client, BQ24190_REG_CHRG_C, &data);
	data = data & ~(0x3F << 2);	/* clear ICHG bits (111111) */

	switch (charger_type) {
	case POWER_SUPPLY_TYPE_MAINS:
		/* 2036mA (011000) */
		data = data | (0x18 << 2);
		break;
	case POWER_SUPPLY_TYPE_USB:
	default:
		/* 500mA (000000) */
		data = data | (0x0 << 2);
		break;
	}

	bq24190_i2c_write(client, BQ24190_REG_CHRG_C, &data);

	return 0;
}

static int bq24190_set_termination_current(struct i2c_client *client,
					     int charger_type)
{
	u8 data = 0;

	if (!client)
		return -EINVAL;

	/* Set termination current */
	bq24190_i2c_read(client, BQ24190_REG_PCHRG_TRM_C, &data);
	data = data & ~(0xF);	/* clear ITERM bits */
	data = data | 0x1;	/* 256mA (0001) */
	bq24190_i2c_write(client, BQ24190_REG_PCHRG_TRM_C, &data);

	/* Enable charge termination */
	bq24190_i2c_read(client, BQ24190_REG_CHRG_TRM_TMR, &data);
	data = data & ~(0x1 << 7);	/* clear EN_TERM bit */
	data = data | (0x1 << 7);	/* Enable termination */
	bq24190_i2c_write(client, BQ24190_REG_CHRG_TRM_TMR, &data);

	return 0;
}

static int bq24190_charger_function_control(struct i2c_client *client,
					     int charger_type)
{
	struct sec_charger_info *charger;

	if (!client)
		return -EINVAL;

	charger = i2c_get_clientdata(client);

	if (charger_type == POWER_SUPPLY_TYPE_BATTERY) {
		/* No charger */
		bq24190_disable_charging(client);
		charger->is_charging = false;
	} else {
		/* Set charging current by charger type */
		bq24190_set_input_current_limit(client, charger_type);
		bq24190_set_fast_charge_current(client, charger_type);
		bq24190_set_termination_current(client, charger_type);
		bq24190_enable_charging(client);
	}

	return 0;
}

static int bq24190_disable_timer(struct i2c_client *client)
{
	u8 data = 0;

	if (!client)
		return -EINVAL;

	dev_dbg(&client->dev, "%s\n", __func__);

	/* Disable watchdog timer and safety timer */
	bq24190_i2c_read(client, BQ24190_REG_CHRG_TRM_TMR, &data);
	data = data & ~(0x30);
	data = data & ~(0x08);
	bq24190_i2c_write(client, BQ24190_REG_CHRG_TRM_TMR, &data);

	return 0;
}

static int bq24190_set_minimum_system_voltage(struct i2c_client *client)
{
	u8 data = 0;

	if (!client)
		return -EINVAL;

	dev_dbg(&client->dev, "%s\n", __func__);

	/* 3.0V */
	bq24190_i2c_read(client, BQ24190_REG_PWRON_CFG, &data);
	data = data & ~(0x7 << 1);	/* Clear SYS_MIN bits */
	bq24190_i2c_write(client, BQ24190_REG_PWRON_CFG, &data);

	return 0;
}

bool sec_hal_chg_init(struct i2c_client *client)
{
	bq24190_disable_timer(client);
	bq24190_set_minimum_system_voltage(client);
	bq24190_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;

	if (!client)
		return false;

	charger = i2c_get_clientdata(client);

	switch (psp) {
	case POWER_SUPPLY_PROP_STATUS:
		val->intval = bq24190_get_charging_status(client);
		dev_dbg(&client->dev, "%s: STATUS, %d\n",
			__func__, val->intval);
		break;
	case POWER_SUPPLY_PROP_HEALTH:
		val->intval = bq24190_get_charging_health(client);
		dev_dbg(&client->dev, "%s: HEALTH, %d\n",
			__func__, val->intval);
		break;
	case POWER_SUPPLY_PROP_CURRENT_NOW:
		if (charger->charging_current)
			val->intval = bq24190_get_charging_current(client);

		else
			val->intval = 0;
		dev_dbg(&client->dev, "%s: CURRENT_NOW, %d\n",
			__func__, 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;

	if (!client)
		return false;

	charger = i2c_get_clientdata(client);

	switch (psp) {
	/* val->intval : type */
	case POWER_SUPPLY_PROP_ONLINE:
		dev_dbg(&client->dev, "%s: ONLINE, %d\n",
			__func__, val->intval);
		bq24190_charger_function_control(client, val->intval);
		bq24190_test_read(client);
		break;
	/* val->intval : charging current */
	case POWER_SUPPLY_PROP_CURRENT_NOW:
		dev_dbg(&client->dev, "%s: CURRENT_NOW, %d\n",
			__func__, val->intval);
		bq24190_set_charging_current(client, val->intval);
		bq24190_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_REG: */
/*		break; */
	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;

		bq24190_read_regs(chg->client, str);
		dev_dbg(&chg->client->dev, "%s: %s\n", __func__, str);
		i += scnprintf(buf + i, PAGE_SIZE - i, "%s\n",
			str);

		kfree(str);
		break;
	default:
		i = -EINVAL;
	}

	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;
			bq24190_i2c_read(chg->client, chg->reg_addr, &data);
			chg->reg_data = data;
			dev_dbg(&chg->client->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(&chg->client->dev,
				"%s: (write) addr = 0x%x, data = 0x%x\n",
				__func__, chg->reg_addr, data);
			bq24190_i2c_write(chg->client,
				chg->reg_addr, &data);
			ret = count;
		}
		break;
	default:
		ret = -EINVAL;
	}

	return ret;
}