forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathab8500_charger.c
3750 lines (3311 loc) · 102 KB
/
ab8500_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
/*
* Copyright (C) ST-Ericsson SA 2012
*
* Charger driver for AB8500
*
* License Terms: GNU General Public License v2
* Author:
* Johan Palsson <[email protected]>
* Karl Komierowski <[email protected]>
* Arun R Murthy <[email protected]>
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/notifier.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/completion.h>
#include <linux/regulator/consumer.h>
#include <linux/err.h>
#include <linux/workqueue.h>
#include <linux/kobject.h>
#include <linux/of.h>
#include <linux/mfd/core.h>
#include <linux/mfd/abx500/ab8500.h>
#include <linux/mfd/abx500.h>
#include <linux/mfd/abx500/ab8500-bm.h>
#include <linux/mfd/abx500/ab8500-gpadc.h>
#include <linux/mfd/abx500/ux500_chargalg.h>
#include <linux/usb/otg.h>
#include <linux/mutex.h>
/* Charger constants */
#define NO_PW_CONN 0
#define AC_PW_CONN 1
#define USB_PW_CONN 2
#define MAIN_WDOG_ENA 0x01
#define MAIN_WDOG_KICK 0x02
#define MAIN_WDOG_DIS 0x00
#define CHARG_WD_KICK 0x01
#define MAIN_CH_ENA 0x01
#define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
#define USB_CH_ENA 0x01
#define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
#define MAIN_CH_DET 0x01
#define MAIN_CH_CV_ON 0x04
#define USB_CH_CV_ON 0x08
#define VBUS_DET_DBNC100 0x02
#define VBUS_DET_DBNC1 0x01
#define OTP_ENABLE_WD 0x01
#define DROP_COUNT_RESET 0x01
#define USB_CH_DET 0x01
#define MAIN_CH_INPUT_CURR_SHIFT 4
#define VBUS_IN_CURR_LIM_SHIFT 4
#define AB8540_VBUS_IN_CURR_LIM_SHIFT 2
#define AUTO_VBUS_IN_CURR_LIM_SHIFT 4
#define AB8540_AUTO_VBUS_IN_CURR_MASK 0x3F
#define VBUS_IN_CURR_LIM_RETRY_SET_TIME 30 /* seconds */
#define LED_INDICATOR_PWM_ENA 0x01
#define LED_INDICATOR_PWM_DIS 0x00
#define LED_IND_CUR_5MA 0x04
#define LED_INDICATOR_PWM_DUTY_252_256 0xBF
/* HW failure constants */
#define MAIN_CH_TH_PROT 0x02
#define VBUS_CH_NOK 0x08
#define USB_CH_TH_PROT 0x02
#define VBUS_OVV_TH 0x01
#define MAIN_CH_NOK 0x01
#define VBUS_DET 0x80
#define MAIN_CH_STATUS2_MAINCHGDROP 0x80
#define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
#define USB_CH_VBUSDROP 0x40
#define USB_CH_VBUSDETDBNC 0x01
/* UsbLineStatus register bit masks */
#define AB8500_USB_LINK_STATUS 0x78
#define AB8505_USB_LINK_STATUS 0xF8
#define AB8500_STD_HOST_SUSP 0x18
#define USB_LINK_STATUS_SHIFT 3
/* Watchdog timeout constant */
#define WD_TIMER 0x30 /* 4min */
#define WD_KICK_INTERVAL (60 * HZ)
/* Lowest charger voltage is 3.39V -> 0x4E */
#define LOW_VOLT_REG 0x4E
/* Step up/down delay in us */
#define STEP_UDELAY 1000
#define CHARGER_STATUS_POLL 10 /* in ms */
#define CHG_WD_INTERVAL (60 * HZ)
#define AB8500_SW_CONTROL_FALLBACK 0x03
/* Wait for enumeration before charing in us */
#define WAIT_ACA_RID_ENUMERATION (5 * 1000)
/*External charger control*/
#define AB8500_SYS_CHARGER_CONTROL_REG 0x52
#define EXTERNAL_CHARGER_DISABLE_REG_VAL 0x03
#define EXTERNAL_CHARGER_ENABLE_REG_VAL 0x07
/* UsbLineStatus register - usb types */
enum ab8500_charger_link_status {
USB_STAT_NOT_CONFIGURED,
USB_STAT_STD_HOST_NC,
USB_STAT_STD_HOST_C_NS,
USB_STAT_STD_HOST_C_S,
USB_STAT_HOST_CHG_NM,
USB_STAT_HOST_CHG_HS,
USB_STAT_HOST_CHG_HS_CHIRP,
USB_STAT_DEDICATED_CHG,
USB_STAT_ACA_RID_A,
USB_STAT_ACA_RID_B,
USB_STAT_ACA_RID_C_NM,
USB_STAT_ACA_RID_C_HS,
USB_STAT_ACA_RID_C_HS_CHIRP,
USB_STAT_HM_IDGND,
USB_STAT_RESERVED,
USB_STAT_NOT_VALID_LINK,
USB_STAT_PHY_EN,
USB_STAT_SUP_NO_IDGND_VBUS,
USB_STAT_SUP_IDGND_VBUS,
USB_STAT_CHARGER_LINE_1,
USB_STAT_CARKIT_1,
USB_STAT_CARKIT_2,
USB_STAT_ACA_DOCK_CHARGER,
};
enum ab8500_usb_state {
AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
AB8500_BM_USB_STATE_CONFIGURED,
AB8500_BM_USB_STATE_SUSPEND,
AB8500_BM_USB_STATE_RESUME,
AB8500_BM_USB_STATE_MAX,
};
/* VBUS input current limits supported in AB8500 in mA */
#define USB_CH_IP_CUR_LVL_0P05 50
#define USB_CH_IP_CUR_LVL_0P09 98
#define USB_CH_IP_CUR_LVL_0P19 193
#define USB_CH_IP_CUR_LVL_0P29 290
#define USB_CH_IP_CUR_LVL_0P38 380
#define USB_CH_IP_CUR_LVL_0P45 450
#define USB_CH_IP_CUR_LVL_0P5 500
#define USB_CH_IP_CUR_LVL_0P6 600
#define USB_CH_IP_CUR_LVL_0P7 700
#define USB_CH_IP_CUR_LVL_0P8 800
#define USB_CH_IP_CUR_LVL_0P9 900
#define USB_CH_IP_CUR_LVL_1P0 1000
#define USB_CH_IP_CUR_LVL_1P1 1100
#define USB_CH_IP_CUR_LVL_1P3 1300
#define USB_CH_IP_CUR_LVL_1P4 1400
#define USB_CH_IP_CUR_LVL_1P5 1500
#define VBAT_TRESH_IP_CUR_RED 3800
#define to_ab8500_charger_usb_device_info(x) container_of((x), \
struct ab8500_charger, usb_chg)
#define to_ab8500_charger_ac_device_info(x) container_of((x), \
struct ab8500_charger, ac_chg)
/**
* struct ab8500_charger_interrupts - ab8500 interupts
* @name: name of the interrupt
* @isr function pointer to the isr
*/
struct ab8500_charger_interrupts {
char *name;
irqreturn_t (*isr)(int irq, void *data);
};
struct ab8500_charger_info {
int charger_connected;
int charger_online;
int charger_voltage;
int cv_active;
bool wd_expired;
int charger_current;
};
struct ab8500_charger_event_flags {
bool mainextchnotok;
bool main_thermal_prot;
bool usb_thermal_prot;
bool vbus_ovv;
bool usbchargernotok;
bool chgwdexp;
bool vbus_collapse;
bool vbus_drop_end;
};
struct ab8500_charger_usb_state {
int usb_current;
int usb_current_tmp;
enum ab8500_usb_state state;
enum ab8500_usb_state state_tmp;
spinlock_t usb_lock;
};
struct ab8500_charger_max_usb_in_curr {
int usb_type_max;
int set_max;
int calculated_max;
};
/**
* struct ab8500_charger - ab8500 Charger device information
* @dev: Pointer to the structure device
* @vbus_detected: VBUS detected
* @vbus_detected_start:
* VBUS detected during startup
* @ac_conn: This will be true when the AC charger has been plugged
* @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
* charger is enabled
* @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
* charger is enabled
* @vbat Battery voltage
* @old_vbat Previously measured battery voltage
* @usb_device_is_unrecognised USB device is unrecognised by the hardware
* @autopower Indicate if we should have automatic pwron after pwrloss
* @autopower_cfg platform specific power config support for "pwron after pwrloss"
* @invalid_charger_detect_state State when forcing AB to use invalid charger
* @is_aca_rid: Incicate if accessory is ACA type
* @current_stepping_sessions:
* Counter for current stepping sessions
* @parent: Pointer to the struct ab8500
* @gpadc: Pointer to the struct gpadc
* @bm: Platform specific battery management information
* @flags: Structure for information about events triggered
* @usb_state: Structure for usb stack information
* @max_usb_in_curr: Max USB charger input current
* @ac_chg: AC charger power supply
* @usb_chg: USB charger power supply
* @ac: Structure that holds the AC charger properties
* @usb: Structure that holds the USB charger properties
* @regu: Pointer to the struct regulator
* @charger_wq: Work queue for the IRQs and checking HW state
* @usb_ipt_crnt_lock: Lock to protect VBUS input current setting from mutuals
* @pm_lock: Lock to prevent system to suspend
* @check_vbat_work Work for checking vbat threshold to adjust vbus current
* @check_hw_failure_work: Work for checking HW state
* @check_usbchgnotok_work: Work for checking USB charger not ok status
* @kick_wd_work: Work for kicking the charger watchdog in case
* of ABB rev 1.* due to the watchog logic bug
* @ac_charger_attached_work: Work for checking if AC charger is still
* connected
* @usb_charger_attached_work: Work for checking if USB charger is still
* connected
* @ac_work: Work for checking AC charger connection
* @detect_usb_type_work: Work for detecting the USB type connected
* @usb_link_status_work: Work for checking the new USB link status
* @usb_state_changed_work: Work for checking USB state
* @attach_work: Work for detecting USB type
* @vbus_drop_end_work: Work for detecting VBUS drop end
* @check_main_thermal_prot_work:
* Work for checking Main thermal status
* @check_usb_thermal_prot_work:
* Work for checking USB thermal status
* @charger_attached_mutex: For controlling the wakelock
*/
struct ab8500_charger {
struct device *dev;
bool vbus_detected;
bool vbus_detected_start;
bool ac_conn;
bool vddadc_en_ac;
bool vddadc_en_usb;
int vbat;
int old_vbat;
bool usb_device_is_unrecognised;
bool autopower;
bool autopower_cfg;
int invalid_charger_detect_state;
int is_aca_rid;
atomic_t current_stepping_sessions;
struct ab8500 *parent;
struct ab8500_gpadc *gpadc;
struct abx500_bm_data *bm;
struct ab8500_charger_event_flags flags;
struct ab8500_charger_usb_state usb_state;
struct ab8500_charger_max_usb_in_curr max_usb_in_curr;
struct ux500_charger ac_chg;
struct ux500_charger usb_chg;
struct ab8500_charger_info ac;
struct ab8500_charger_info usb;
struct regulator *regu;
struct workqueue_struct *charger_wq;
struct mutex usb_ipt_crnt_lock;
struct delayed_work check_vbat_work;
struct delayed_work check_hw_failure_work;
struct delayed_work check_usbchgnotok_work;
struct delayed_work kick_wd_work;
struct delayed_work usb_state_changed_work;
struct delayed_work attach_work;
struct delayed_work ac_charger_attached_work;
struct delayed_work usb_charger_attached_work;
struct delayed_work vbus_drop_end_work;
struct work_struct ac_work;
struct work_struct detect_usb_type_work;
struct work_struct usb_link_status_work;
struct work_struct check_main_thermal_prot_work;
struct work_struct check_usb_thermal_prot_work;
struct usb_phy *usb_phy;
struct notifier_block nb;
struct mutex charger_attached_mutex;
};
/* AC properties */
static enum power_supply_property ab8500_charger_ac_props[] = {
POWER_SUPPLY_PROP_HEALTH,
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_ONLINE,
POWER_SUPPLY_PROP_VOLTAGE_NOW,
POWER_SUPPLY_PROP_VOLTAGE_AVG,
POWER_SUPPLY_PROP_CURRENT_NOW,
};
/* USB properties */
static enum power_supply_property ab8500_charger_usb_props[] = {
POWER_SUPPLY_PROP_HEALTH,
POWER_SUPPLY_PROP_CURRENT_AVG,
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_ONLINE,
POWER_SUPPLY_PROP_VOLTAGE_NOW,
POWER_SUPPLY_PROP_VOLTAGE_AVG,
POWER_SUPPLY_PROP_CURRENT_NOW,
};
/*
* Function for enabling and disabling sw fallback mode
* should always be disabled when no charger is connected.
*/
static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
bool fallback)
{
u8 val;
u8 reg;
u8 bank;
u8 bit;
int ret;
dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
if (is_ab8500(di->parent)) {
bank = 0x15;
reg = 0x0;
bit = 3;
} else {
bank = AB8500_SYS_CTRL1_BLOCK;
reg = AB8500_SW_CONTROL_FALLBACK;
bit = 0;
}
/* read the register containing fallback bit */
ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
if (ret < 0) {
dev_err(di->dev, "%d read failed\n", __LINE__);
return;
}
if (is_ab8500(di->parent)) {
/* enable the OPT emulation registers */
ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
if (ret) {
dev_err(di->dev, "%d write failed\n", __LINE__);
goto disable_otp;
}
}
if (fallback)
val |= (1 << bit);
else
val &= ~(1 << bit);
/* write back the changed fallback bit value to register */
ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
if (ret) {
dev_err(di->dev, "%d write failed\n", __LINE__);
}
disable_otp:
if (is_ab8500(di->parent)) {
/* disable the set OTP registers again */
ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
if (ret) {
dev_err(di->dev, "%d write failed\n", __LINE__);
}
}
}
/**
* ab8500_power_supply_changed - a wrapper with local extentions for
* power_supply_changed
* @di: pointer to the ab8500_charger structure
* @psy: pointer to power_supply_that have changed.
*
*/
static void ab8500_power_supply_changed(struct ab8500_charger *di,
struct power_supply *psy)
{
if (di->autopower_cfg) {
if (!di->usb.charger_connected &&
!di->ac.charger_connected &&
di->autopower) {
di->autopower = false;
ab8500_enable_disable_sw_fallback(di, false);
} else if (!di->autopower &&
(di->ac.charger_connected ||
di->usb.charger_connected)) {
di->autopower = true;
ab8500_enable_disable_sw_fallback(di, true);
}
}
power_supply_changed(psy);
}
static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
bool connected)
{
if (connected != di->usb.charger_connected) {
dev_dbg(di->dev, "USB connected:%i\n", connected);
di->usb.charger_connected = connected;
if (!connected)
di->flags.vbus_drop_end = false;
sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
if (connected) {
mutex_lock(&di->charger_attached_mutex);
mutex_unlock(&di->charger_attached_mutex);
if (is_ab8500(di->parent))
queue_delayed_work(di->charger_wq,
&di->usb_charger_attached_work,
HZ);
} else {
cancel_delayed_work_sync(&di->usb_charger_attached_work);
mutex_lock(&di->charger_attached_mutex);
mutex_unlock(&di->charger_attached_mutex);
}
}
}
/**
* ab8500_charger_get_ac_voltage() - get ac charger voltage
* @di: pointer to the ab8500_charger structure
*
* Returns ac charger voltage (on success)
*/
static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
{
int vch;
/* Only measure voltage if the charger is connected */
if (di->ac.charger_connected) {
vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
if (vch < 0)
dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
} else {
vch = 0;
}
return vch;
}
/**
* ab8500_charger_ac_cv() - check if the main charger is in CV mode
* @di: pointer to the ab8500_charger structure
*
* Returns ac charger CV mode (on success) else error code
*/
static int ab8500_charger_ac_cv(struct ab8500_charger *di)
{
u8 val;
int ret = 0;
/* Only check CV mode if the charger is online */
if (di->ac.charger_online) {
ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
AB8500_CH_STATUS1_REG, &val);
if (ret < 0) {
dev_err(di->dev, "%s ab8500 read failed\n", __func__);
return 0;
}
if (val & MAIN_CH_CV_ON)
ret = 1;
else
ret = 0;
}
return ret;
}
/**
* ab8500_charger_get_vbus_voltage() - get vbus voltage
* @di: pointer to the ab8500_charger structure
*
* This function returns the vbus voltage.
* Returns vbus voltage (on success)
*/
static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
{
int vch;
/* Only measure voltage if the charger is connected */
if (di->usb.charger_connected) {
vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
if (vch < 0)
dev_err(di->dev, "%s gpadc conv failed\n", __func__);
} else {
vch = 0;
}
return vch;
}
/**
* ab8500_charger_get_usb_current() - get usb charger current
* @di: pointer to the ab8500_charger structure
*
* This function returns the usb charger current.
* Returns usb current (on success) and error code on failure
*/
static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
{
int ich;
/* Only measure current if the charger is online */
if (di->usb.charger_online) {
ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
if (ich < 0)
dev_err(di->dev, "%s gpadc conv failed\n", __func__);
} else {
ich = 0;
}
return ich;
}
/**
* ab8500_charger_get_ac_current() - get ac charger current
* @di: pointer to the ab8500_charger structure
*
* This function returns the ac charger current.
* Returns ac current (on success) and error code on failure.
*/
static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
{
int ich;
/* Only measure current if the charger is online */
if (di->ac.charger_online) {
ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
if (ich < 0)
dev_err(di->dev, "%s gpadc conv failed\n", __func__);
} else {
ich = 0;
}
return ich;
}
/**
* ab8500_charger_usb_cv() - check if the usb charger is in CV mode
* @di: pointer to the ab8500_charger structure
*
* Returns ac charger CV mode (on success) else error code
*/
static int ab8500_charger_usb_cv(struct ab8500_charger *di)
{
int ret;
u8 val;
/* Only check CV mode if the charger is online */
if (di->usb.charger_online) {
ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
AB8500_CH_USBCH_STAT1_REG, &val);
if (ret < 0) {
dev_err(di->dev, "%s ab8500 read failed\n", __func__);
return 0;
}
if (val & USB_CH_CV_ON)
ret = 1;
else
ret = 0;
} else {
ret = 0;
}
return ret;
}
/**
* ab8500_charger_detect_chargers() - Detect the connected chargers
* @di: pointer to the ab8500_charger structure
* @probe: if probe, don't delay and wait for HW
*
* Returns the type of charger connected.
* For USB it will not mean we can actually charge from it
* but that there is a USB cable connected that we have to
* identify. This is used during startup when we don't get
* interrupts of the charger detection
*
* Returns an integer value, that means,
* NO_PW_CONN no power supply is connected
* AC_PW_CONN if the AC power supply is connected
* USB_PW_CONN if the USB power supply is connected
* AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
*/
static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
{
int result = NO_PW_CONN;
int ret;
u8 val;
/* Check for AC charger */
ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
AB8500_CH_STATUS1_REG, &val);
if (ret < 0) {
dev_err(di->dev, "%s ab8500 read failed\n", __func__);
return ret;
}
if (val & MAIN_CH_DET)
result = AC_PW_CONN;
/* Check for USB charger */
if (!probe) {
/*
* AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
* when disconnecting ACA even though no
* charger was connected. Try waiting a little
* longer than the 100 ms of VBUS_DET_DBNC100...
*/
msleep(110);
}
ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
AB8500_CH_USBCH_STAT1_REG, &val);
if (ret < 0) {
dev_err(di->dev, "%s ab8500 read failed\n", __func__);
return ret;
}
dev_dbg(di->dev,
"%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
val);
if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
result |= USB_PW_CONN;
return result;
}
/**
* ab8500_charger_max_usb_curr() - get the max curr for the USB type
* @di: pointer to the ab8500_charger structure
* @link_status: the identified USB type
*
* Get the maximum current that is allowed to be drawn from the host
* based on the USB type.
* Returns error code in case of failure else 0 on success
*/
static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
enum ab8500_charger_link_status link_status)
{
int ret = 0;
di->usb_device_is_unrecognised = false;
/*
* Platform only supports USB 2.0.
* This means that charging current from USB source
* is maximum 500 mA. Every occurence of USB_STAT_*_HOST_*
* should set USB_CH_IP_CUR_LVL_0P5.
*/
switch (link_status) {
case USB_STAT_STD_HOST_NC:
case USB_STAT_STD_HOST_C_NS:
case USB_STAT_STD_HOST_C_S:
dev_dbg(di->dev, "USB Type - Standard host is "
"detected through USB driver\n");
di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
di->is_aca_rid = 0;
break;
case USB_STAT_HOST_CHG_HS_CHIRP:
di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
di->is_aca_rid = 0;
break;
case USB_STAT_HOST_CHG_HS:
di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
di->is_aca_rid = 0;
break;
case USB_STAT_ACA_RID_C_HS:
di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P9;
di->is_aca_rid = 0;
break;
case USB_STAT_ACA_RID_A:
/*
* Dedicated charger level minus maximum current accessory
* can consume (900mA). Closest level is 500mA
*/
dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
di->is_aca_rid = 1;
break;
case USB_STAT_ACA_RID_B:
/*
* Dedicated charger level minus 120mA (20mA for ACA and
* 100mA for potential accessory). Closest level is 1300mA
*/
di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P3;
dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
di->max_usb_in_curr.usb_type_max);
di->is_aca_rid = 1;
break;
case USB_STAT_HOST_CHG_NM:
di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
di->is_aca_rid = 0;
break;
case USB_STAT_DEDICATED_CHG:
di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
di->is_aca_rid = 0;
break;
case USB_STAT_ACA_RID_C_HS_CHIRP:
case USB_STAT_ACA_RID_C_NM:
di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
di->is_aca_rid = 1;
break;
case USB_STAT_NOT_CONFIGURED:
if (di->vbus_detected) {
di->usb_device_is_unrecognised = true;
dev_dbg(di->dev, "USB Type - Legacy charger.\n");
di->max_usb_in_curr.usb_type_max =
USB_CH_IP_CUR_LVL_1P5;
break;
}
case USB_STAT_HM_IDGND:
dev_err(di->dev, "USB Type - Charging not allowed\n");
di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
ret = -ENXIO;
break;
case USB_STAT_RESERVED:
if (is_ab8500(di->parent)) {
di->flags.vbus_collapse = true;
dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
"VBUS has collapsed\n");
ret = -ENXIO;
break;
} else {
dev_dbg(di->dev, "USB Type - Charging not allowed\n");
di->max_usb_in_curr.usb_type_max =
USB_CH_IP_CUR_LVL_0P05;
dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
link_status,
di->max_usb_in_curr.usb_type_max);
ret = -ENXIO;
break;
}
break;
case USB_STAT_CARKIT_1:
case USB_STAT_CARKIT_2:
case USB_STAT_ACA_DOCK_CHARGER:
case USB_STAT_CHARGER_LINE_1:
di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
di->max_usb_in_curr.usb_type_max);
break;
case USB_STAT_NOT_VALID_LINK:
dev_err(di->dev, "USB Type invalid - try charging anyway\n");
di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
break;
default:
dev_err(di->dev, "USB Type - Unknown\n");
di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
ret = -ENXIO;
break;
};
di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
link_status, di->max_usb_in_curr.set_max);
return ret;
}
/**
* ab8500_charger_read_usb_type() - read the type of usb connected
* @di: pointer to the ab8500_charger structure
*
* Detect the type of the plugged USB
* Returns error code in case of failure else 0 on success
*/
static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
{
int ret;
u8 val;
ret = abx500_get_register_interruptible(di->dev,
AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
if (ret < 0) {
dev_err(di->dev, "%s ab8500 read failed\n", __func__);
return ret;
}
if (is_ab8500(di->parent))
ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
AB8500_USB_LINE_STAT_REG, &val);
else
ret = abx500_get_register_interruptible(di->dev,
AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
if (ret < 0) {
dev_err(di->dev, "%s ab8500 read failed\n", __func__);
return ret;
}
/* get the USB type */
if (is_ab8500(di->parent))
val = (val & AB8500_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
else
val = (val & AB8505_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
ret = ab8500_charger_max_usb_curr(di,
(enum ab8500_charger_link_status) val);
return ret;
}
/**
* ab8500_charger_detect_usb_type() - get the type of usb connected
* @di: pointer to the ab8500_charger structure
*
* Detect the type of the plugged USB
* Returns error code in case of failure else 0 on success
*/
static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
{
int i, ret;
u8 val;
/*
* On getting the VBUS rising edge detect interrupt there
* is a 250ms delay after which the register UsbLineStatus
* is filled with valid data.
*/
for (i = 0; i < 10; i++) {
msleep(250);
ret = abx500_get_register_interruptible(di->dev,
AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
&val);
dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
__func__, val);
if (ret < 0) {
dev_err(di->dev, "%s ab8500 read failed\n", __func__);
return ret;
}
if (is_ab8500(di->parent))
ret = abx500_get_register_interruptible(di->dev,
AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
else
ret = abx500_get_register_interruptible(di->dev,
AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
if (ret < 0) {
dev_err(di->dev, "%s ab8500 read failed\n", __func__);
return ret;
}
dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
val);
/*
* Until the IT source register is read the UsbLineStatus
* register is not updated, hence doing the same
* Revisit this:
*/
/* get the USB type */
if (is_ab8500(di->parent))
val = (val & AB8500_USB_LINK_STATUS) >>
USB_LINK_STATUS_SHIFT;
else
val = (val & AB8505_USB_LINK_STATUS) >>
USB_LINK_STATUS_SHIFT;
if (val)
break;
}
ret = ab8500_charger_max_usb_curr(di,
(enum ab8500_charger_link_status) val);
return ret;
}
/*
* This array maps the raw hex value to charger voltage used by the AB8500
* Values taken from the UM0836
*/
static int ab8500_charger_voltage_map[] = {
3500 ,
3525 ,
3550 ,
3575 ,
3600 ,
3625 ,
3650 ,
3675 ,
3700 ,
3725 ,
3750 ,
3775 ,
3800 ,
3825 ,
3850 ,
3875 ,
3900 ,
3925 ,
3950 ,
3975 ,
4000 ,
4025 ,
4050 ,
4060 ,
4070 ,
4080 ,
4090 ,
4100 ,
4110 ,
4120 ,
4130 ,
4140 ,
4150 ,
4160 ,
4170 ,
4180 ,
4190 ,
4200 ,
4210 ,
4220 ,
4230 ,
4240 ,
4250 ,
4260 ,
4270 ,
4280 ,
4290 ,
4300 ,
4310 ,
4320 ,
4330 ,
4340 ,
4350 ,
4360 ,
4370 ,
4380 ,
4390 ,
4400 ,
4410 ,
4420 ,
4430 ,
4440 ,
4450 ,
4460 ,
4470 ,
4480 ,
4490 ,
4500 ,
4510 ,
4520 ,
4530 ,
4540 ,
4550 ,
4560 ,
4570 ,
4580 ,
4590 ,
4600 ,
};
static int ab8500_voltage_to_regval(int voltage)
{
int i;
/* Special case for voltage below 3.5V */
if (voltage < ab8500_charger_voltage_map[0])
return LOW_VOLT_REG;
for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
if (voltage < ab8500_charger_voltage_map[i])
return i - 1;
}
/* If not last element, return error */
i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
if (voltage == ab8500_charger_voltage_map[i])