/
bq24192_charger.c
2277 lines (1954 loc) · 58.9 KB
/
bq24192_charger.c
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* bq24192_charger.c - Charger driver for TI BQ24192,BQ24191 and BQ24190
*
* Copyright (C) 2011 Intel Corporation
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Author: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
* Author: Raj Pandey <raj.pandey@intel.com>
*/
#include <linux/module.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/workqueue.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/i2c.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/power_supply.h>
#include <linux/power/bq24192_charger.h>
#include <linux/sfi.h>
#include <linux/pm_runtime.h>
#include <linux/io.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/wakelock.h>
#include <linux/version.h>
#include <linux/usb/otg.h>
#include <linux/platform_data/intel_mid_remoteproc.h>
#include <linux/rpmsg.h>
#include <linux/acpi.h>
#include <linux/acpi_gpio.h>
#include <asm/intel_mid_gpadc.h>
#include <asm/intel_scu_ipc.h>
#include <asm/intel_scu_pmic.h>
#include <asm/intel_mid_rpmsg.h>
#define DRV_NAME "bq24192_charger"
#define DEV_NAME "bq24192"
/*
* D0, D1, D2 can be used to set current limits
* and D3, D4, D5, D6 can be used to voltage limits
*/
#define BQ24192_INPUT_SRC_CNTL_REG 0x0
#define INPUT_SRC_CNTL_EN_HIZ (1 << 7)
#define BATTERY_NEAR_FULL(a) ((a * 98)/100)
/*
* set input voltage lim to 4.68V. This will help in charger
* instability issue when duty cycle reaches 100%.
*/
#define INPUT_SRC_VOLT_LMT_DEF (3 << 4)
#define INPUT_SRC_VOLT_LMT_444 (7 << 3)
#define INPUT_SRC_VOLT_LMT_468 (5 << 4)
#define INPUT_SRC_VOLT_LMT_476 (0xB << 3)
#define INPUT_SRC_VINDPM_MASK (0xF << 3)
#define INPUT_SRC_LOW_VBAT_LIMIT 3600
#define INPUT_SRC_MIDL_VBAT_LIMIT 4000
#define INPUT_SRC_MIDH_VBAT_LIMIT 4200
#define INPUT_SRC_HIGH_VBAT_LIMIT 4350
/* D0, D1, D2 represent the input current limit */
#define INPUT_SRC_CUR_LMT0 0x0 /* 100mA */
#define INPUT_SRC_CUR_LMT1 0x1 /* 150mA */
#define INPUT_SRC_CUR_LMT2 0x2 /* 500mA */
#define INPUT_SRC_CUR_LMT3 0x3 /* 900mA */
#define INPUT_SRC_CUR_LMT4 0x4 /* 1200mA */
#define INPUT_SRC_CUR_LMT5 0x5 /* 1500mA */
#define INPUT_SRC_CUR_LMT6 0x6 /* 2000mA */
#define INPUT_SRC_CUR_LMT7 0x7 /* 3000mA */
/*
* D1, D2, D3 can be used to set min sys voltage limit
* and D4, D5 can be used to control the charger
*/
#define BQ24192_POWER_ON_CFG_REG 0x1
#define POWER_ON_CFG_RESET (1 << 7)
#define POWER_ON_CFG_I2C_WDTTMR_RESET (1 << 6)
/* BQ2419X series charger and OTG enable bits */
#define CHR_CFG_BIT_POS 4
#define CHR_CFG_BIT_LEN 2
#define CHR_CFG_CHRG_MASK 3
#define POWER_ON_CFG_CHRG_CFG_DIS (0 << 4)
#define POWER_ON_CFG_CHRG_CFG_EN (1 << 4)
#define POWER_ON_CFG_CHRG_CFG_OTG (3 << 4)
/* BQ2429X series charger and OTG enable bits */
#define POWER_ON_CFG_BQ29X_OTG_EN (1 << 5)
#define POWER_ON_CFG_BQ29X_CHRG_EN (1 << 4)
#define POWER_ON_CFG_BOOST_LIM (1 << 0)
/*
* Charge Current control register
* with range from 500 - 4532mA
*/
#define BQ24192_CHRG_CUR_CNTL_REG 0x2
#define BQ24192_CHRG_CUR_OFFSET 500 /* 500 mA */
#define BQ24192_CHRG_CUR_LSB_TO_CUR 64 /* 64 mA */
#define BQ24192_GET_CHRG_CUR(reg) ((reg>>2)*BQ24192_CHRG_CUR_LSB_TO_CUR\
+ BQ24192_CHRG_CUR_OFFSET) /* in mA */
#define BQ24192_CHRG_ITERM_OFFSET 128
#define BQ24192_CHRG_CUR_LSB_TO_ITERM 128
/* Pre charge and termination current limit reg */
#define BQ24192_PRECHRG_TERM_CUR_CNTL_REG 0x3
#define BQ24192_TERM_CURR_LIMIT_128 0 /* 128mA */
#define BQ24192_PRE_CHRG_CURR_256 (1 << 4) /* 256mA */
/* Charge voltage control reg */
#define BQ24192_CHRG_VOLT_CNTL_REG 0x4
#define BQ24192_CHRG_VOLT_OFFSET 3504 /* 3504 mV */
#define BQ24192_CHRG_VOLT_LSB_TO_VOLT 16 /* 16 mV */
/* Low voltage setting 0 - 2.8V and 1 - 3.0V */
#define CHRG_VOLT_CNTL_BATTLOWV (1 << 1)
/* Battery Recharge threshold 0 - 100mV and 1 - 300mV */
#define CHRG_VOLT_CNTL_VRECHRG (1 << 0)
#define BQ24192_GET_CHRG_VOLT(reg) ((reg>>2)*BQ24192_CHRG_VOLT_LSB_TO_VOLT\
+ BQ24192_CHRG_VOLT_OFFSET) /* in mV */
/* Charge termination and Timer control reg */
#define BQ24192_CHRG_TIMER_EXP_CNTL_REG 0x5
#define CHRG_TIMER_EXP_CNTL_EN_TERM (1 << 7)
#define CHRG_TIMER_EXP_CNTL_TERM_STAT (1 << 6)
/* WDT Timer uses 2 bits */
#define WDT_TIMER_BIT_POS 4
#define WDT_TIMER_BIT_LEN 2
#define CHRG_TIMER_EXP_CNTL_WDTDISABLE (0 << 4)
#define CHRG_TIMER_EXP_CNTL_WDT40SEC (1 << 4)
#define CHRG_TIMER_EXP_CNTL_WDT80SEC (2 << 4)
#define CHRG_TIMER_EXP_CNTL_WDT160SEC (3 << 4)
#define WDTIMER_RESET_MASK 0x40
/* Safety Timer Enable bit */
#define CHRG_TIMER_EXP_CNTL_EN_TIMER (1 << 3)
/* Charge Timer uses 2bits(20 hrs) */
#define SFT_TIMER_BIT_POS 1
#define SFT_TIMER_BIT_LEN 2
#define CHRG_TIMER_EXP_CNTL_SFT_TIMER (3 << 1)
#define BQ24192_CHRG_THRM_REGL_REG 0x6
#define BQ24192_MISC_OP_CNTL_REG 0x7
#define MISC_OP_CNTL_DPDM_EN (1 << 7)
#define MISC_OP_CNTL_TMR2X_EN (1 << 6)
#define MISC_OP_CNTL_BATFET_DIS (1 << 5)
#define MISC_OP_CNTL_BATGOOD_EN (1 << 4)
/* To mask INT's write 0 to the bit */
#define MISC_OP_CNTL_MINT_CHRG (1 << 1)
#define MISC_OP_CNTL_MINT_BATT (1 << 0)
#define BQ24192_SYSTEM_STAT_REG 0x8
/* D6, D7 show VBUS status */
#define SYSTEM_STAT_VBUS_UNKNOWN 0
#define SYSTEM_STAT_VBUS_HOST (1 << 6)
#define SYSTEM_STAT_VBUS_ADP (2 << 6)
#define SYSTEM_STAT_VBUS_OTG (3 << 6)
/* D4, D5 show charger status */
#define SYSTEM_STAT_NOT_CHRG (0 << 4)
#define SYSTEM_STAT_PRE_CHRG (1 << 4)
#define SYSTEM_STAT_FAST_CHRG (2 << 4)
#define SYSTEM_STAT_CHRG_DONE (3 << 4)
#define SYSTEM_STAT_DPM (1 << 3)
#define SYSTEM_STAT_PWR_GOOD (1 << 2)
#define SYSTEM_STAT_THERM_REG (1 << 1)
#define SYSTEM_STAT_VSYS_LOW (1 << 0)
#define SYSTEM_STAT_CHRG_MASK (3 << 4)
#define BQ24192_FAULT_STAT_REG 0x9
#define FAULT_STAT_WDT_TMR_EXP (1 << 7)
#define FAULT_STAT_OTG_FLT (1 << 6)
/* D4, D5 show charger fault status */
#define FAULT_STAT_CHRG_NORMAL (0 << 4)
#define FAULT_STAT_CHRG_IN_FLT (1 << 4)
#define FAULT_STAT_CHRG_THRM_FLT (2 << 4)
#define FAULT_STAT_CHRG_TMR_FLT (3 << 4)
#define FAULT_STAT_BATT_FLT (1 << 3)
#define BQ24192_VENDER_REV_REG 0xA
/* D3, D4, D5 indicates the chip model number */
#define BQ24190_IC_VERSION 0x0
#define BQ24191_IC_VERSION 0x1
#define BQ24192_IC_VERSION 0x2
#define BQ24192I_IC_VERSION 0x3
#define BQ24296_IC_VERSION 0x4
#define BQ24297_IC_VERSION 0xC
#define BQ24192_MAX_MEM 12
#define NR_RETRY_CNT 3
#define CHARGER_PS_NAME "bq24192_charger"
#define CHARGER_TASK_JIFFIES (HZ * 150)/* 150sec */
#define CHARGER_HOST_JIFFIES (HZ * 60) /* 60sec */
#define FULL_THREAD_JIFFIES (HZ * 30) /* 30sec */
#define BATT_TEMP_MAX_DEF 60 /* 60 degrees */
#define BATT_TEMP_MIN_DEF 0
/* Max no. of tries to clear the charger from Hi-Z mode */
#define MAX_TRY 3
/* Max no. of tries to reset the bq24192i WDT */
#define MAX_RESET_WDT_RETRY 8
static struct power_supply *fg_psy;
struct bq24192_otg_event {
struct list_head node;
bool is_enable;
};
enum bq24192_chrgr_stat {
BQ24192_CHRGR_STAT_UNKNOWN,
BQ24192_CHRGR_STAT_CHARGING,
BQ24192_CHRGR_STAT_BAT_FULL,
BQ24192_CHRGR_STAT_FAULT,
BQ24192_CHRGR_STAT_LOW_SUPPLY_FAULT
};
enum bq24192_chip_type {
BQ24190, BQ24191, BQ24192,
BQ24192I, BQ24296, BQ24297
};
struct bq24192_chip {
struct i2c_client *client;
struct bq24192_platform_data *pdata;
enum bq24192_chip_type chip_type;
struct power_supply usb;
struct delayed_work chrg_task_wrkr;
struct delayed_work chrg_full_wrkr;
struct work_struct otg_evt_work;
struct notifier_block otg_nb;
struct list_head otg_queue;
struct mutex event_lock;
struct power_supply_cable_props cap;
struct power_supply_cable_props cached_cap;
struct usb_phy *transceiver;
/* Wake lock to prevent platform from going to S3 when charging */
struct wake_lock wakelock;
spinlock_t otg_queue_lock;
enum bq24192_chrgr_stat chgr_stat;
enum power_supply_charger_cable_type cable_type;
int cc;
int cv;
int inlmt;
int max_cc;
int max_cv;
int max_temp;
int min_temp;
int iterm;
int batt_status;
int bat_health;
int cntl_state;
int irq;
bool is_charger_enabled;
bool is_charging_enabled;
bool votg;
bool is_pwr_good;
bool boost_mode;
bool online;
bool present;
bool sfttmr_expired;
};
#ifdef CONFIG_DEBUG_FS
static struct dentry *bq24192_dbgfs_root;
static char bq24192_dbg_regs[BQ24192_MAX_MEM][4];
#endif
static struct i2c_client *bq24192_client;
static enum power_supply_property bq24192_usb_props[] = {
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_ONLINE,
POWER_SUPPLY_PROP_TYPE,
POWER_SUPPLY_PROP_HEALTH,
POWER_SUPPLY_PROP_MAX_CHARGE_CURRENT,
POWER_SUPPLY_PROP_MAX_CHARGE_VOLTAGE,
POWER_SUPPLY_PROP_CHARGE_CURRENT,
POWER_SUPPLY_PROP_CHARGE_VOLTAGE,
POWER_SUPPLY_PROP_INLMT,
POWER_SUPPLY_PROP_ENABLE_CHARGING,
POWER_SUPPLY_PROP_ENABLE_CHARGER,
POWER_SUPPLY_PROP_CHARGE_TERM_CUR,
POWER_SUPPLY_PROP_CABLE_TYPE,
POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
POWER_SUPPLY_PROP_MAX_TEMP,
POWER_SUPPLY_PROP_MIN_TEMP
};
static enum power_supply_type get_power_supply_type(
enum power_supply_charger_cable_type cable)
{
switch (cable) {
case POWER_SUPPLY_CHARGER_TYPE_USB_DCP:
return POWER_SUPPLY_TYPE_USB_DCP;
case POWER_SUPPLY_CHARGER_TYPE_USB_CDP:
return POWER_SUPPLY_TYPE_USB_CDP;
case POWER_SUPPLY_CHARGER_TYPE_USB_ACA:
return POWER_SUPPLY_TYPE_USB_ACA;
case POWER_SUPPLY_CHARGER_TYPE_AC:
return POWER_SUPPLY_TYPE_MAINS;
case POWER_SUPPLY_CHARGER_TYPE_NONE:
case POWER_SUPPLY_CHARGER_TYPE_USB_SDP:
default:
return POWER_SUPPLY_TYPE_USB;
}
return POWER_SUPPLY_TYPE_USB;
}
/*-------------------------------------------------------------------------*/
/*
* Genenric register read/write interfaces to access registers in charger ic
*/
static int bq24192_write_reg(struct i2c_client *client, u8 reg, u8 value)
{
int ret, i;
for (i = 0; i < NR_RETRY_CNT; i++) {
ret = i2c_smbus_write_byte_data(client, reg, value);
if (ret == -EAGAIN || ret == -ETIMEDOUT)
continue;
else
break;
}
if (ret < 0)
dev_err(&client->dev, "I2C SMbus Write error:%d\n", ret);
return ret;
}
static int bq24192_read_reg(struct i2c_client *client, u8 reg)
{
int ret, i;
for (i = 0; i < NR_RETRY_CNT; i++) {
ret = i2c_smbus_read_byte_data(client, reg);
if (ret == -EAGAIN || ret == -ETIMEDOUT)
continue;
else
break;
}
if (ret < 0)
dev_err(&client->dev, "I2C SMbus Read error:%d\n", ret);
return ret;
}
#ifdef DEBUG
/*
* This function dumps the bq24192 registers
*/
static void bq24192_dump_registers(struct bq24192_chip *chip)
{
int ret;
dev_info(&chip->client->dev, "%s\n", __func__);
/* Input Src Ctrl register */
ret = bq24192_read_reg(chip->client, BQ24192_INPUT_SRC_CNTL_REG);
if (ret < 0)
dev_warn(&chip->client->dev, "Input Src Ctrl reg read fail\n");
dev_info(&chip->client->dev, "REG00 %x\n", ret);
/* Pwr On Cfg register */
ret = bq24192_read_reg(chip->client, BQ24192_POWER_ON_CFG_REG);
if (ret < 0)
dev_warn(&chip->client->dev, "Pwr On Cfg reg read fail\n");
dev_info(&chip->client->dev, "REG01 %x\n", ret);
/* Chrg Curr Ctrl register */
ret = bq24192_read_reg(chip->client, BQ24192_CHRG_CUR_CNTL_REG);
if (ret < 0)
dev_warn(&chip->client->dev, "Chrg Curr Ctrl reg read fail\n");
dev_info(&chip->client->dev, "REG02 %x\n", ret);
/* Pre-Chrg Term register */
ret = bq24192_read_reg(chip->client,
BQ24192_PRECHRG_TERM_CUR_CNTL_REG);
if (ret < 0)
dev_warn(&chip->client->dev, "Pre-Chrg Term reg read fail\n");
dev_info(&chip->client->dev, "REG03 %x\n", ret);
/* Chrg Volt Ctrl register */
ret = bq24192_read_reg(chip->client, BQ24192_CHRG_VOLT_CNTL_REG);
if (ret < 0)
dev_warn(&chip->client->dev, "Chrg Volt Ctrl reg read fail\n");
dev_info(&chip->client->dev, "REG04 %x\n", ret);
/* Chrg Term and Timer Ctrl register */
ret = bq24192_read_reg(chip->client, BQ24192_CHRG_TIMER_EXP_CNTL_REG);
if (ret < 0) {
dev_warn(&chip->client->dev,
"Chrg Term and Timer Ctrl reg read fail\n");
}
dev_info(&chip->client->dev, "REG05 %x\n", ret);
/* Thermal Regulation register */
ret = bq24192_read_reg(chip->client, BQ24192_CHRG_THRM_REGL_REG);
if (ret < 0) {
dev_warn(&chip->client->dev,
"Thermal Regulation reg read fail\n");
}
dev_info(&chip->client->dev, "REG06 %x\n", ret);
/* Misc Operations Ctrl register */
ret = bq24192_read_reg(chip->client, BQ24192_MISC_OP_CNTL_REG);
if (ret < 0)
dev_warn(&chip->client->dev, "Misc Op Ctrl reg read fail\n");
dev_info(&chip->client->dev, "REG07 %x\n", ret);
/* System Status register */
ret = bq24192_read_reg(chip->client, BQ24192_SYSTEM_STAT_REG);
if (ret < 0)
dev_warn(&chip->client->dev, "System Status reg read fail\n");
dev_info(&chip->client->dev, "REG08 %x\n", ret);
/* Fault Status register */
ret = bq24192_read_reg(chip->client, BQ24192_FAULT_STAT_REG);
if (ret < 0)
dev_warn(&chip->client->dev, "Fault Status reg read fail\n");
dev_info(&chip->client->dev, "REG09 %x\n", ret);
/* Vendor Revision register */
ret = bq24192_read_reg(chip->client, BQ24192_VENDER_REV_REG);
if (ret < 0)
dev_warn(&chip->client->dev, "Vendor Rev reg read fail\n");
dev_info(&chip->client->dev, "REG0A %x\n", ret);
}
#endif
/*
* If the bit_set is TRUE then val 1s will be SET in the reg else val 1s will
* be CLEARED
*/
static int bq24192_reg_read_modify(struct i2c_client *client, u8 reg,
u8 val, bool bit_set)
{
int ret;
ret = bq24192_read_reg(client, reg);
if (bit_set)
ret |= val;
else
ret &= (~val);
ret = bq24192_write_reg(client, reg, ret);
return ret;
}
static int bq24192_reg_multi_bitset(struct i2c_client *client, u8 reg,
u8 val, u8 pos, u8 len)
{
int ret;
u8 data;
ret = bq24192_read_reg(client, reg);
if (ret < 0) {
dev_warn(&client->dev, "I2C SMbus Read error:%d\n", ret);
return ret;
}
data = (1 << len) - 1;
ret = (ret & ~(data << pos)) | val;
ret = bq24192_write_reg(client, reg, ret);
return ret;
}
/*
* This function verifies if the bq24192i charger chip is in Hi-Z
* If yes, then clear the Hi-Z to resume the charger operations
*/
static int bq24192_clear_hiz(struct bq24192_chip *chip)
{
int ret, count;
dev_info(&chip->client->dev, "%s\n", __func__);
for (count = 0; count < MAX_TRY; count++) {
/*
* Read the bq24192i REG00 register for charger Hi-Z mode.
* If it is in Hi-Z, then clear the Hi-Z to resume the charging
* operations.
*/
ret = bq24192_read_reg(chip->client,
BQ24192_INPUT_SRC_CNTL_REG);
if (ret < 0) {
dev_warn(&chip->client->dev,
"Input src cntl read failed\n");
goto i2c_error;
}
if (ret & INPUT_SRC_CNTL_EN_HIZ) {
dev_warn(&chip->client->dev,
"Charger IC in Hi-Z mode\n");
#ifdef DEBUG
bq24192_dump_registers(chip);
#endif
/* Clear the Charger from Hi-Z mode */
ret = (chip->inlmt & ~INPUT_SRC_CNTL_EN_HIZ);
/* Write the values back */
ret = bq24192_write_reg(chip->client,
BQ24192_INPUT_SRC_CNTL_REG, ret);
if (ret < 0) {
dev_warn(&chip->client->dev,
"Input src cntl write failed\n");
goto i2c_error;
}
msleep(150);
} else {
dev_info(&chip->client->dev,
"Charger is not in Hi-Z\n");
break;
}
}
return ret;
i2c_error:
dev_err(&chip->client->dev, "%s\n", __func__);
return ret;
}
/* check_batt_psy -check for whether power supply type is battery
* @dev : Power Supply dev structure
* @data : Power Supply Driver Data
* Context: can sleep
*
* Return true if power supply type is battery
*
*/
static int check_batt_psy(struct device *dev, void *data)
{
struct power_supply *psy = dev_get_drvdata(dev);
/* check for whether power supply type is battery */
if (psy->type == POWER_SUPPLY_TYPE_BATTERY) {
fg_psy = psy;
return 1;
}
return 0;
}
/**
* get_fg_chip_psy - identify the Fuel Gauge Power Supply device
* Context: can sleep
*
* Return Fuel Gauge power supply structure
*/
static struct power_supply *get_fg_chip_psy(void)
{
if (fg_psy)
return fg_psy;
/* loop through power supply class */
class_for_each_device(power_supply_class, NULL, NULL,
check_batt_psy);
return fg_psy;
}
/**
* fg_chip_get_property - read a power supply property from Fuel Gauge driver
* @psp : Power Supply property
*
* Return power supply property value
*
*/
static int fg_chip_get_property(enum power_supply_property psp)
{
union power_supply_propval val;
int ret = -ENODEV;
if (!fg_psy)
fg_psy = get_fg_chip_psy();
if (fg_psy) {
ret = fg_psy->get_property(fg_psy, psp, &val);
if (!ret)
return val.intval;
}
return ret;
}
/**
* bq24192_get_charger_health - to get the charger health status
*
* Returns charger health status
*/
int bq24192_get_charger_health(void)
{
int ret_status, ret_fault;
struct bq24192_chip *chip =
i2c_get_clientdata(bq24192_client);
dev_dbg(&chip->client->dev, "%s\n", __func__);
/* If we do not have any cable connected, return health as UNKNOWN */
if (chip->cable_type == POWER_SUPPLY_CHARGER_TYPE_NONE)
return POWER_SUPPLY_HEALTH_UNKNOWN;
ret_fault = bq24192_read_reg(chip->client, BQ24192_FAULT_STAT_REG);
if (ret_fault < 0) {
dev_warn(&chip->client->dev,
"read reg failed %s\n", __func__);
return POWER_SUPPLY_HEALTH_UNKNOWN;
}
if (ret_fault & FAULT_STAT_OTG_FLT)
return POWER_SUPPLY_HEALTH_OVERVOLTAGE;
/* Check if the WeakVIN condition occured */
ret_status = bq24192_read_reg(chip->client, BQ24192_SYSTEM_STAT_REG);
if (ret_status < 0) {
dev_warn(&chip->client->dev,
"read reg failed %s\n", __func__);
return POWER_SUPPLY_HEALTH_UNKNOWN;
}
if (!(ret_status & SYSTEM_STAT_PWR_GOOD) ||
((ret_fault & FAULT_STAT_CHRG_IN_FLT) == FAULT_STAT_CHRG_IN_FLT))
return POWER_SUPPLY_HEALTH_DEAD;
return POWER_SUPPLY_HEALTH_GOOD;
}
/**
* bq24192_get_battery_health - to get the battery health status
*
* Returns battery health status
*/
int bq24192_get_battery_health(void)
{
int temp,vnow;
struct bq24192_chip *chip;
if (!bq24192_client)
return POWER_SUPPLY_HEALTH_UNKNOWN;
chip = i2c_get_clientdata(bq24192_client);
dev_info(&chip->client->dev, "+%s\n", __func__);
/* If power supply is emulating as battery, return health as good */
if (!chip->pdata->sfi_tabl_present)
return POWER_SUPPLY_HEALTH_GOOD;
/* Report the battery health w.r.t battery temperature from FG */
temp = fg_chip_get_property(POWER_SUPPLY_PROP_TEMP);
if (temp == -ENODEV || temp == -EINVAL) {
dev_err(&chip->client->dev,
"Failed to read batt profile\n");
return POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
}
temp /= 10;
if ((temp <= chip->min_temp) ||
(temp > chip->max_temp))
return POWER_SUPPLY_HEALTH_OVERHEAT;
/* read the battery voltage */
vnow = fg_chip_get_property(POWER_SUPPLY_PROP_VOLTAGE_NOW);
if (vnow == -ENODEV || vnow == -EINVAL) {
dev_err(&chip->client->dev, "Can't read voltage from FG\n");
return POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
}
/* convert voltage into millivolts */
vnow /= 1000;
dev_warn(&chip->client->dev, "vnow = %d\n", vnow);
if (vnow > chip->max_cv)
return POWER_SUPPLY_HEALTH_OVERVOLTAGE;
dev_dbg(&chip->client->dev, "-%s\n", __func__);
return POWER_SUPPLY_HEALTH_GOOD;
}
EXPORT_SYMBOL(bq24192_get_battery_health);
/***********************************************************************/
/* convert the input current limit value
* into equivalent register setting.
* Note: ilim must be in mA.
*/
static u8 chrg_ilim_to_reg(int ilim)
{
u8 reg;
/* set voltage to 5V */
reg = INPUT_SRC_VOLT_LMT_DEF;
/* Set the input source current limit
* between 100 to 1500mA */
if (ilim <= 100)
reg |= INPUT_SRC_CUR_LMT0;
else if (ilim <= 150)
reg |= INPUT_SRC_CUR_LMT1;
else if (ilim <= 500)
reg |= INPUT_SRC_CUR_LMT2;
else if (ilim <= 900)
reg |= INPUT_SRC_CUR_LMT3;
else if (ilim <= 1200)
reg |= INPUT_SRC_CUR_LMT4;
else
reg |= INPUT_SRC_CUR_LMT5;
return reg;
}
static u8 chrg_iterm_to_reg(int iterm)
{
u8 reg;
if (iterm <= BQ24192_CHRG_ITERM_OFFSET)
reg = 0;
else
reg = ((iterm - BQ24192_CHRG_ITERM_OFFSET) /
BQ24192_CHRG_CUR_LSB_TO_ITERM);
return reg;
}
/* convert the charge current value
* into equivalent register setting
*/
static u8 chrg_cur_to_reg(int cur)
{
u8 reg;
if (cur <= BQ24192_CHRG_CUR_OFFSET)
reg = 0x0;
else
reg = ((cur - BQ24192_CHRG_CUR_OFFSET) /
BQ24192_CHRG_CUR_LSB_TO_CUR) + 1;
/* D0, D1 bits of Charge Current
* register are not used */
reg = reg << 2;
return reg;
}
/* convert the charge voltage value
* into equivalent register setting
*/
static u8 chrg_volt_to_reg(int volt)
{
u8 reg;
if (volt <= BQ24192_CHRG_VOLT_OFFSET)
reg = 0x0;
else
reg = (volt - BQ24192_CHRG_VOLT_OFFSET) /
BQ24192_CHRG_VOLT_LSB_TO_VOLT;
reg = (reg << 2) | CHRG_VOLT_CNTL_BATTLOWV;
return reg;
}
static int bq24192_enable_hw_term(struct bq24192_chip *chip, bool hw_term_en)
{
int ret = 0;
dev_info(&chip->client->dev, "%s\n", __func__);
/* Disable and enable charging to restart the charging */
ret = bq24192_reg_multi_bitset(chip->client,
BQ24192_POWER_ON_CFG_REG,
POWER_ON_CFG_CHRG_CFG_DIS,
CHR_CFG_BIT_POS,
CHR_CFG_BIT_LEN);
if (ret < 0) {
dev_warn(&chip->client->dev,
"i2c reg write failed: reg: %d, ret: %d\n",
BQ24192_POWER_ON_CFG_REG, ret);
return ret;
}
/* Read the timer control register */
ret = bq24192_read_reg(chip->client, BQ24192_CHRG_TIMER_EXP_CNTL_REG);
if (ret < 0) {
dev_warn(&chip->client->dev, "TIMER CTRL reg read failed\n");
return ret;
}
/*
* Enable the HW termination. When disabled the HW termination, battery
* was taking too long to go from charging to full state. HW based
* termination could cause the battery capacity to drop but it would
* result in good battery life.
*/
if (hw_term_en)
ret |= CHRG_TIMER_EXP_CNTL_EN_TERM;
else
ret &= ~CHRG_TIMER_EXP_CNTL_EN_TERM;
/* Program the TIMER CTRL register */
ret = bq24192_write_reg(chip->client,
BQ24192_CHRG_TIMER_EXP_CNTL_REG,
ret);
if (ret < 0)
dev_warn(&chip->client->dev, "TIMER CTRL I2C write failed\n");
return ret;
}
/*
* chip->event_lock need to be acquired before calling this function
* to avoid the race condition
*/
static int program_timers(struct bq24192_chip *chip, int wdt_duration,
bool sfttmr_enable)
{
int ret;
/* Read the timer control register */
ret = bq24192_read_reg(chip->client, BQ24192_CHRG_TIMER_EXP_CNTL_REG);
if (ret < 0) {
dev_warn(&chip->client->dev, "TIMER CTRL reg read failed\n");
return ret;
}
/* Program the time with duration passed */
ret |= wdt_duration;
/* Enable/Disable the safety timer */
if (sfttmr_enable)
ret |= CHRG_TIMER_EXP_CNTL_EN_TIMER;
else
ret &= ~CHRG_TIMER_EXP_CNTL_EN_TIMER;
/* Program the TIMER CTRL register */
ret = bq24192_write_reg(chip->client,
BQ24192_CHRG_TIMER_EXP_CNTL_REG,
ret);
if (ret < 0)
dev_warn(&chip->client->dev, "TIMER CTRL I2C write failed\n");
return ret;
}
/* This function should be called with the mutex held */
static int reset_wdt_timer(struct bq24192_chip *chip)
{
int ret = 0, i;
/* reset WDT timer */
for (i = 0; i < MAX_RESET_WDT_RETRY; i++) {
ret = bq24192_reg_read_modify(chip->client,
BQ24192_POWER_ON_CFG_REG,
WDTIMER_RESET_MASK, true);
if (ret < 0)
dev_warn(&chip->client->dev, "I2C write failed:%s\n",
__func__);
}
return ret;
}
/*
*This function will modify the VINDPM as per the battery voltage
*/
static int bq24192_modify_vindpm(u8 vindpm)
{
int ret;
u8 vindpm_prev;
struct bq24192_chip *chip = i2c_get_clientdata(bq24192_client);
dev_info(&chip->client->dev, "%s\n", __func__);
/* Get the input src ctrl values programmed */
ret = bq24192_read_reg(chip->client,
BQ24192_INPUT_SRC_CNTL_REG);
if (ret < 0) {
dev_warn(&chip->client->dev, "INPUT CTRL reg read failed\n");
return ret;
}
/* Assign the return value of REG00 to vindpm_prev */
vindpm_prev = ret & INPUT_SRC_VINDPM_MASK;
ret &= ~INPUT_SRC_VINDPM_MASK;
/*
* If both the previous and current values are same do not program
* the register.
*/
if (vindpm_prev != vindpm) {
vindpm |= ret;
ret = bq24192_write_reg(chip->client,
BQ24192_INPUT_SRC_CNTL_REG, vindpm);
if (ret < 0) {
dev_info(&chip->client->dev, "VINDPM failed\n");
return ret;
}
}
return ret;
}
/* This function should be called with the mutex held */
static int bq24192_turn_otg_vbus(struct bq24192_chip *chip, bool votg_on)
{
int ret = 0;
dev_info(&chip->client->dev, "%s %d\n", __func__, votg_on);
if (votg_on) {
/* Program the timers */
ret = program_timers(chip,
CHRG_TIMER_EXP_CNTL_WDT80SEC,
false);
if (ret < 0) {
dev_warn(&chip->client->dev,
"TIMER enable failed %s\n", __func__);
goto i2c_write_fail;
}
/* Configure the charger in OTG mode */
if ((chip->chip_type == BQ24296) ||
(chip->chip_type == BQ24297))
ret = bq24192_reg_read_modify(chip->client,
BQ24192_POWER_ON_CFG_REG,
POWER_ON_CFG_BQ29X_OTG_EN, true);
else
ret = bq24192_reg_read_modify(chip->client,
BQ24192_POWER_ON_CFG_REG,
POWER_ON_CFG_CHRG_CFG_OTG, true);
if (ret < 0) {
dev_warn(&chip->client->dev,
"read reg modify failed\n");
goto i2c_write_fail;
}
/* Put the charger IC in reverse boost mode. Since
* SDP charger can supply max 500mA charging current
* Setting the boost current to 500mA
*/
ret = bq24192_reg_read_modify(chip->client,
BQ24192_POWER_ON_CFG_REG,
POWER_ON_CFG_BOOST_LIM, false);
if (ret < 0) {
dev_warn(&chip->client->dev,
"read reg modify failed\n");
goto i2c_write_fail;
}
chip->boost_mode = true;
/* Schedule the charger task worker now */
schedule_delayed_work(&chip->chrg_task_wrkr,
0);
} else {
/* Clear the charger from the OTG mode */
if ((chip->chip_type == BQ24296) ||
(chip->chip_type == BQ24297))
ret = bq24192_reg_read_modify(chip->client,
BQ24192_POWER_ON_CFG_REG,
POWER_ON_CFG_BQ29X_OTG_EN, false);
else
ret = bq24192_reg_read_modify(chip->client,
BQ24192_POWER_ON_CFG_REG,
POWER_ON_CFG_CHRG_CFG_OTG, false);
if (ret < 0) {
dev_warn(&chip->client->dev,
"read reg modify failed\n");
goto i2c_write_fail;
}
/* Put the charger IC out of reverse boost mode 500mA */
ret = bq24192_reg_read_modify(chip->client,
BQ24192_POWER_ON_CFG_REG,
POWER_ON_CFG_BOOST_LIM, false);
if (ret < 0) {
dev_warn(&chip->client->dev,
"read reg modify failed\n");