forked from microsoft/WSL2-Linux-Kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabx500_chargalg.c
1921 lines (1730 loc) · 53.5 KB
/
abx500_chargalg.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
*
* Charging algorithm driver for abx500 variants
*
* License Terms: GNU General Public License v2
* Authors:
* Johan Palsson <johan.palsson@stericsson.com>
* Karl Komierowski <karl.komierowski@stericsson.com>
* Arun R Murthy <arun.murthy@stericsson.com>
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/completion.h>
#include <linux/workqueue.h>
#include <linux/kobject.h>
#include <linux/mfd/abx500.h>
#include <linux/mfd/abx500/ux500_chargalg.h>
#include <linux/mfd/abx500/ab8500-bm.h>
/* Watchdog kick interval */
#define CHG_WD_INTERVAL (6 * HZ)
/* End-of-charge criteria counter */
#define EOC_COND_CNT 10
/* Recharge criteria counter */
#define RCH_COND_CNT 3
#define to_abx500_chargalg_device_info(x) container_of((x), \
struct abx500_chargalg, chargalg_psy);
enum abx500_chargers {
NO_CHG,
AC_CHG,
USB_CHG,
};
struct abx500_chargalg_charger_info {
enum abx500_chargers conn_chg;
enum abx500_chargers prev_conn_chg;
enum abx500_chargers online_chg;
enum abx500_chargers prev_online_chg;
enum abx500_chargers charger_type;
bool usb_chg_ok;
bool ac_chg_ok;
int usb_volt;
int usb_curr;
int ac_volt;
int ac_curr;
int usb_vset;
int usb_iset;
int ac_vset;
int ac_iset;
};
struct abx500_chargalg_suspension_status {
bool suspended_change;
bool ac_suspended;
bool usb_suspended;
};
struct abx500_chargalg_battery_data {
int temp;
int volt;
int avg_curr;
int inst_curr;
int percent;
};
enum abx500_chargalg_states {
STATE_HANDHELD_INIT,
STATE_HANDHELD,
STATE_CHG_NOT_OK_INIT,
STATE_CHG_NOT_OK,
STATE_HW_TEMP_PROTECT_INIT,
STATE_HW_TEMP_PROTECT,
STATE_NORMAL_INIT,
STATE_NORMAL,
STATE_WAIT_FOR_RECHARGE_INIT,
STATE_WAIT_FOR_RECHARGE,
STATE_MAINTENANCE_A_INIT,
STATE_MAINTENANCE_A,
STATE_MAINTENANCE_B_INIT,
STATE_MAINTENANCE_B,
STATE_TEMP_UNDEROVER_INIT,
STATE_TEMP_UNDEROVER,
STATE_TEMP_LOWHIGH_INIT,
STATE_TEMP_LOWHIGH,
STATE_SUSPENDED_INIT,
STATE_SUSPENDED,
STATE_OVV_PROTECT_INIT,
STATE_OVV_PROTECT,
STATE_SAFETY_TIMER_EXPIRED_INIT,
STATE_SAFETY_TIMER_EXPIRED,
STATE_BATT_REMOVED_INIT,
STATE_BATT_REMOVED,
STATE_WD_EXPIRED_INIT,
STATE_WD_EXPIRED,
};
static const char *states[] = {
"HANDHELD_INIT",
"HANDHELD",
"CHG_NOT_OK_INIT",
"CHG_NOT_OK",
"HW_TEMP_PROTECT_INIT",
"HW_TEMP_PROTECT",
"NORMAL_INIT",
"NORMAL",
"WAIT_FOR_RECHARGE_INIT",
"WAIT_FOR_RECHARGE",
"MAINTENANCE_A_INIT",
"MAINTENANCE_A",
"MAINTENANCE_B_INIT",
"MAINTENANCE_B",
"TEMP_UNDEROVER_INIT",
"TEMP_UNDEROVER",
"TEMP_LOWHIGH_INIT",
"TEMP_LOWHIGH",
"SUSPENDED_INIT",
"SUSPENDED",
"OVV_PROTECT_INIT",
"OVV_PROTECT",
"SAFETY_TIMER_EXPIRED_INIT",
"SAFETY_TIMER_EXPIRED",
"BATT_REMOVED_INIT",
"BATT_REMOVED",
"WD_EXPIRED_INIT",
"WD_EXPIRED",
};
struct abx500_chargalg_events {
bool batt_unknown;
bool mainextchnotok;
bool batt_ovv;
bool batt_rem;
bool btemp_underover;
bool btemp_lowhigh;
bool main_thermal_prot;
bool usb_thermal_prot;
bool main_ovv;
bool vbus_ovv;
bool usbchargernotok;
bool safety_timer_expired;
bool maintenance_timer_expired;
bool ac_wd_expired;
bool usb_wd_expired;
bool ac_cv_active;
bool usb_cv_active;
bool vbus_collapsed;
};
/**
* struct abx500_charge_curr_maximization - Charger maximization parameters
* @original_iset: the non optimized/maximised charger current
* @current_iset: the charging current used at this moment
* @test_delta_i: the delta between the current we want to charge and the
current that is really going into the battery
* @condition_cnt: number of iterations needed before a new charger current
is set
* @max_current: maximum charger current
* @wait_cnt: to avoid too fast current step down in case of charger
* voltage collapse, we insert this delay between step
* down
* @level: tells in how many steps the charging current has been
increased
*/
struct abx500_charge_curr_maximization {
int original_iset;
int current_iset;
int test_delta_i;
int condition_cnt;
int max_current;
int wait_cnt;
u8 level;
};
enum maxim_ret {
MAXIM_RET_NOACTION,
MAXIM_RET_CHANGE,
MAXIM_RET_IBAT_TOO_HIGH,
};
/**
* struct abx500_chargalg - abx500 Charging algorithm device information
* @dev: pointer to the structure device
* @charge_status: battery operating status
* @eoc_cnt: counter used to determine end-of_charge
* @rch_cnt: counter used to determine start of recharge
* @maintenance_chg: indicate if maintenance charge is active
* @t_hyst_norm temperature hysteresis when the temperature has been
* over or under normal limits
* @t_hyst_lowhigh temperature hysteresis when the temperature has been
* over or under the high or low limits
* @charge_state: current state of the charging algorithm
* @ccm charging current maximization parameters
* @chg_info: information about connected charger types
* @batt_data: data of the battery
* @susp_status: current charger suspension status
* @pdata: pointer to the abx500_chargalg platform data
* @bat: pointer to the abx500_bm platform data
* @chargalg_psy: structure that holds the battery properties exposed by
* the charging algorithm
* @events: structure for information about events triggered
* @chargalg_wq: work queue for running the charging algorithm
* @chargalg_periodic_work: work to run the charging algorithm periodically
* @chargalg_wd_work: work to kick the charger watchdog periodically
* @chargalg_work: work to run the charging algorithm instantly
* @safety_timer: charging safety timer
* @maintenance_timer: maintenance charging timer
* @chargalg_kobject: structure of type kobject
*/
struct abx500_chargalg {
struct device *dev;
int charge_status;
int eoc_cnt;
int rch_cnt;
bool maintenance_chg;
int t_hyst_norm;
int t_hyst_lowhigh;
enum abx500_chargalg_states charge_state;
struct abx500_charge_curr_maximization ccm;
struct abx500_chargalg_charger_info chg_info;
struct abx500_chargalg_battery_data batt_data;
struct abx500_chargalg_suspension_status susp_status;
struct abx500_chargalg_platform_data *pdata;
struct abx500_bm_data *bat;
struct power_supply chargalg_psy;
struct ux500_charger *ac_chg;
struct ux500_charger *usb_chg;
struct abx500_chargalg_events events;
struct workqueue_struct *chargalg_wq;
struct delayed_work chargalg_periodic_work;
struct delayed_work chargalg_wd_work;
struct work_struct chargalg_work;
struct timer_list safety_timer;
struct timer_list maintenance_timer;
struct kobject chargalg_kobject;
};
/* Main battery properties */
static enum power_supply_property abx500_chargalg_props[] = {
POWER_SUPPLY_PROP_STATUS,
POWER_SUPPLY_PROP_HEALTH,
};
/**
* abx500_chargalg_safety_timer_expired() - Expiration of the safety timer
* @data: pointer to the abx500_chargalg structure
*
* This function gets called when the safety timer for the charger
* expires
*/
static void abx500_chargalg_safety_timer_expired(unsigned long data)
{
struct abx500_chargalg *di = (struct abx500_chargalg *) data;
dev_err(di->dev, "Safety timer expired\n");
di->events.safety_timer_expired = true;
/* Trigger execution of the algorithm instantly */
queue_work(di->chargalg_wq, &di->chargalg_work);
}
/**
* abx500_chargalg_maintenance_timer_expired() - Expiration of
* the maintenance timer
* @i: pointer to the abx500_chargalg structure
*
* This function gets called when the maintenence timer
* expires
*/
static void abx500_chargalg_maintenance_timer_expired(unsigned long data)
{
struct abx500_chargalg *di = (struct abx500_chargalg *) data;
dev_dbg(di->dev, "Maintenance timer expired\n");
di->events.maintenance_timer_expired = true;
/* Trigger execution of the algorithm instantly */
queue_work(di->chargalg_wq, &di->chargalg_work);
}
/**
* abx500_chargalg_state_to() - Change charge state
* @di: pointer to the abx500_chargalg structure
*
* This function gets called when a charge state change should occur
*/
static void abx500_chargalg_state_to(struct abx500_chargalg *di,
enum abx500_chargalg_states state)
{
dev_dbg(di->dev,
"State changed: %s (From state: [%d] %s =to=> [%d] %s )\n",
di->charge_state == state ? "NO" : "YES",
di->charge_state,
states[di->charge_state],
state,
states[state]);
di->charge_state = state;
}
/**
* abx500_chargalg_check_charger_connection() - Check charger connection change
* @di: pointer to the abx500_chargalg structure
*
* This function will check if there is a change in the charger connection
* and change charge state accordingly. AC has precedence over USB.
*/
static int abx500_chargalg_check_charger_connection(struct abx500_chargalg *di)
{
if (di->chg_info.conn_chg != di->chg_info.prev_conn_chg ||
di->susp_status.suspended_change) {
/*
* Charger state changed or suspension
* has changed since last update
*/
if ((di->chg_info.conn_chg & AC_CHG) &&
!di->susp_status.ac_suspended) {
dev_dbg(di->dev, "Charging source is AC\n");
if (di->chg_info.charger_type != AC_CHG) {
di->chg_info.charger_type = AC_CHG;
abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
}
} else if ((di->chg_info.conn_chg & USB_CHG) &&
!di->susp_status.usb_suspended) {
dev_dbg(di->dev, "Charging source is USB\n");
di->chg_info.charger_type = USB_CHG;
abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
} else if (di->chg_info.conn_chg &&
(di->susp_status.ac_suspended ||
di->susp_status.usb_suspended)) {
dev_dbg(di->dev, "Charging is suspended\n");
di->chg_info.charger_type = NO_CHG;
abx500_chargalg_state_to(di, STATE_SUSPENDED_INIT);
} else {
dev_dbg(di->dev, "Charging source is OFF\n");
di->chg_info.charger_type = NO_CHG;
abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
}
di->chg_info.prev_conn_chg = di->chg_info.conn_chg;
di->susp_status.suspended_change = false;
}
return di->chg_info.conn_chg;
}
/**
* abx500_chargalg_start_safety_timer() - Start charging safety timer
* @di: pointer to the abx500_chargalg structure
*
* The safety timer is used to avoid overcharging of old or bad batteries.
* There are different timers for AC and USB
*/
static void abx500_chargalg_start_safety_timer(struct abx500_chargalg *di)
{
unsigned long timer_expiration = 0;
switch (di->chg_info.charger_type) {
case AC_CHG:
timer_expiration =
round_jiffies(jiffies +
(di->bat->main_safety_tmr_h * 3600 * HZ));
break;
case USB_CHG:
timer_expiration =
round_jiffies(jiffies +
(di->bat->usb_safety_tmr_h * 3600 * HZ));
break;
default:
dev_err(di->dev, "Unknown charger to charge from\n");
break;
}
di->events.safety_timer_expired = false;
di->safety_timer.expires = timer_expiration;
if (!timer_pending(&di->safety_timer))
add_timer(&di->safety_timer);
else
mod_timer(&di->safety_timer, timer_expiration);
}
/**
* abx500_chargalg_stop_safety_timer() - Stop charging safety timer
* @di: pointer to the abx500_chargalg structure
*
* The safety timer is stopped whenever the NORMAL state is exited
*/
static void abx500_chargalg_stop_safety_timer(struct abx500_chargalg *di)
{
di->events.safety_timer_expired = false;
del_timer(&di->safety_timer);
}
/**
* abx500_chargalg_start_maintenance_timer() - Start charging maintenance timer
* @di: pointer to the abx500_chargalg structure
* @duration: duration of ther maintenance timer in hours
*
* The maintenance timer is used to maintain the charge in the battery once
* the battery is considered full. These timers are chosen to match the
* discharge curve of the battery
*/
static void abx500_chargalg_start_maintenance_timer(struct abx500_chargalg *di,
int duration)
{
unsigned long timer_expiration;
/* Convert from hours to jiffies */
timer_expiration = round_jiffies(jiffies + (duration * 3600 * HZ));
di->events.maintenance_timer_expired = false;
di->maintenance_timer.expires = timer_expiration;
if (!timer_pending(&di->maintenance_timer))
add_timer(&di->maintenance_timer);
else
mod_timer(&di->maintenance_timer, timer_expiration);
}
/**
* abx500_chargalg_stop_maintenance_timer() - Stop maintenance timer
* @di: pointer to the abx500_chargalg structure
*
* The maintenance timer is stopped whenever maintenance ends or when another
* state is entered
*/
static void abx500_chargalg_stop_maintenance_timer(struct abx500_chargalg *di)
{
di->events.maintenance_timer_expired = false;
del_timer(&di->maintenance_timer);
}
/**
* abx500_chargalg_kick_watchdog() - Kick charger watchdog
* @di: pointer to the abx500_chargalg structure
*
* The charger watchdog have to be kicked periodically whenever the charger is
* on, else the ABB will reset the system
*/
static int abx500_chargalg_kick_watchdog(struct abx500_chargalg *di)
{
/* Check if charger exists and kick watchdog if charging */
if (di->ac_chg && di->ac_chg->ops.kick_wd &&
di->chg_info.online_chg & AC_CHG)
return di->ac_chg->ops.kick_wd(di->ac_chg);
else if (di->usb_chg && di->usb_chg->ops.kick_wd &&
di->chg_info.online_chg & USB_CHG)
return di->usb_chg->ops.kick_wd(di->usb_chg);
return -ENXIO;
}
/**
* abx500_chargalg_ac_en() - Turn on/off the AC charger
* @di: pointer to the abx500_chargalg structure
* @enable: charger on/off
* @vset: requested charger output voltage
* @iset: requested charger output current
*
* The AC charger will be turned on/off with the requested charge voltage and
* current
*/
static int abx500_chargalg_ac_en(struct abx500_chargalg *di, int enable,
int vset, int iset)
{
if (!di->ac_chg || !di->ac_chg->ops.enable)
return -ENXIO;
/* Select maximum of what both the charger and the battery supports */
if (di->ac_chg->max_out_volt)
vset = min(vset, di->ac_chg->max_out_volt);
if (di->ac_chg->max_out_curr)
iset = min(iset, di->ac_chg->max_out_curr);
di->chg_info.ac_iset = iset;
di->chg_info.ac_vset = vset;
return di->ac_chg->ops.enable(di->ac_chg, enable, vset, iset);
}
/**
* abx500_chargalg_usb_en() - Turn on/off the USB charger
* @di: pointer to the abx500_chargalg structure
* @enable: charger on/off
* @vset: requested charger output voltage
* @iset: requested charger output current
*
* The USB charger will be turned on/off with the requested charge voltage and
* current
*/
static int abx500_chargalg_usb_en(struct abx500_chargalg *di, int enable,
int vset, int iset)
{
if (!di->usb_chg || !di->usb_chg->ops.enable)
return -ENXIO;
/* Select maximum of what both the charger and the battery supports */
if (di->usb_chg->max_out_volt)
vset = min(vset, di->usb_chg->max_out_volt);
if (di->usb_chg->max_out_curr)
iset = min(iset, di->usb_chg->max_out_curr);
di->chg_info.usb_iset = iset;
di->chg_info.usb_vset = vset;
return di->usb_chg->ops.enable(di->usb_chg, enable, vset, iset);
}
/**
* abx500_chargalg_update_chg_curr() - Update charger current
* @di: pointer to the abx500_chargalg structure
* @iset: requested charger output current
*
* The charger output current will be updated for the charger
* that is currently in use
*/
static int abx500_chargalg_update_chg_curr(struct abx500_chargalg *di,
int iset)
{
/* Check if charger exists and update current if charging */
if (di->ac_chg && di->ac_chg->ops.update_curr &&
di->chg_info.charger_type & AC_CHG) {
/*
* Select maximum of what both the charger
* and the battery supports
*/
if (di->ac_chg->max_out_curr)
iset = min(iset, di->ac_chg->max_out_curr);
di->chg_info.ac_iset = iset;
return di->ac_chg->ops.update_curr(di->ac_chg, iset);
} else if (di->usb_chg && di->usb_chg->ops.update_curr &&
di->chg_info.charger_type & USB_CHG) {
/*
* Select maximum of what both the charger
* and the battery supports
*/
if (di->usb_chg->max_out_curr)
iset = min(iset, di->usb_chg->max_out_curr);
di->chg_info.usb_iset = iset;
return di->usb_chg->ops.update_curr(di->usb_chg, iset);
}
return -ENXIO;
}
/**
* abx500_chargalg_stop_charging() - Stop charging
* @di: pointer to the abx500_chargalg structure
*
* This function is called from any state where charging should be stopped.
* All charging is disabled and all status parameters and timers are changed
* accordingly
*/
static void abx500_chargalg_stop_charging(struct abx500_chargalg *di)
{
abx500_chargalg_ac_en(di, false, 0, 0);
abx500_chargalg_usb_en(di, false, 0, 0);
abx500_chargalg_stop_safety_timer(di);
abx500_chargalg_stop_maintenance_timer(di);
di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
di->maintenance_chg = false;
cancel_delayed_work(&di->chargalg_wd_work);
power_supply_changed(&di->chargalg_psy);
}
/**
* abx500_chargalg_hold_charging() - Pauses charging
* @di: pointer to the abx500_chargalg structure
*
* This function is called in the case where maintenance charging has been
* disabled and instead a battery voltage mode is entered to check when the
* battery voltage has reached a certain recharge voltage
*/
static void abx500_chargalg_hold_charging(struct abx500_chargalg *di)
{
abx500_chargalg_ac_en(di, false, 0, 0);
abx500_chargalg_usb_en(di, false, 0, 0);
abx500_chargalg_stop_safety_timer(di);
abx500_chargalg_stop_maintenance_timer(di);
di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
di->maintenance_chg = false;
cancel_delayed_work(&di->chargalg_wd_work);
power_supply_changed(&di->chargalg_psy);
}
/**
* abx500_chargalg_start_charging() - Start the charger
* @di: pointer to the abx500_chargalg structure
* @vset: requested charger output voltage
* @iset: requested charger output current
*
* A charger will be enabled depending on the requested charger type that was
* detected previously.
*/
static void abx500_chargalg_start_charging(struct abx500_chargalg *di,
int vset, int iset)
{
switch (di->chg_info.charger_type) {
case AC_CHG:
dev_dbg(di->dev,
"AC parameters: Vset %d, Ich %d\n", vset, iset);
abx500_chargalg_usb_en(di, false, 0, 0);
abx500_chargalg_ac_en(di, true, vset, iset);
break;
case USB_CHG:
dev_dbg(di->dev,
"USB parameters: Vset %d, Ich %d\n", vset, iset);
abx500_chargalg_ac_en(di, false, 0, 0);
abx500_chargalg_usb_en(di, true, vset, iset);
break;
default:
dev_err(di->dev, "Unknown charger to charge from\n");
break;
}
}
/**
* abx500_chargalg_check_temp() - Check battery temperature ranges
* @di: pointer to the abx500_chargalg structure
*
* The battery temperature is checked against the predefined limits and the
* charge state is changed accordingly
*/
static void abx500_chargalg_check_temp(struct abx500_chargalg *di)
{
if (di->batt_data.temp > (di->bat->temp_low + di->t_hyst_norm) &&
di->batt_data.temp < (di->bat->temp_high - di->t_hyst_norm)) {
/* Temp OK! */
di->events.btemp_underover = false;
di->events.btemp_lowhigh = false;
di->t_hyst_norm = 0;
di->t_hyst_lowhigh = 0;
} else {
if (((di->batt_data.temp >= di->bat->temp_high) &&
(di->batt_data.temp <
(di->bat->temp_over - di->t_hyst_lowhigh))) ||
((di->batt_data.temp >
(di->bat->temp_under + di->t_hyst_lowhigh)) &&
(di->batt_data.temp <= di->bat->temp_low))) {
/* TEMP minor!!!!! */
di->events.btemp_underover = false;
di->events.btemp_lowhigh = true;
di->t_hyst_norm = di->bat->temp_hysteresis;
di->t_hyst_lowhigh = 0;
} else if (di->batt_data.temp <= di->bat->temp_under ||
di->batt_data.temp >= di->bat->temp_over) {
/* TEMP major!!!!! */
di->events.btemp_underover = true;
di->events.btemp_lowhigh = false;
di->t_hyst_norm = 0;
di->t_hyst_lowhigh = di->bat->temp_hysteresis;
} else {
/* Within hysteresis */
dev_dbg(di->dev, "Within hysteresis limit temp: %d "
"hyst_lowhigh %d, hyst normal %d\n",
di->batt_data.temp, di->t_hyst_lowhigh,
di->t_hyst_norm);
}
}
}
/**
* abx500_chargalg_check_charger_voltage() - Check charger voltage
* @di: pointer to the abx500_chargalg structure
*
* Charger voltage is checked against maximum limit
*/
static void abx500_chargalg_check_charger_voltage(struct abx500_chargalg *di)
{
if (di->chg_info.usb_volt > di->bat->chg_params->usb_volt_max)
di->chg_info.usb_chg_ok = false;
else
di->chg_info.usb_chg_ok = true;
if (di->chg_info.ac_volt > di->bat->chg_params->ac_volt_max)
di->chg_info.ac_chg_ok = false;
else
di->chg_info.ac_chg_ok = true;
}
/**
* abx500_chargalg_end_of_charge() - Check if end-of-charge criteria is fulfilled
* @di: pointer to the abx500_chargalg structure
*
* End-of-charge criteria is fulfilled when the battery voltage is above a
* certain limit and the battery current is below a certain limit for a
* predefined number of consecutive seconds. If true, the battery is full
*/
static void abx500_chargalg_end_of_charge(struct abx500_chargalg *di)
{
if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING &&
di->charge_state == STATE_NORMAL &&
!di->maintenance_chg && (di->batt_data.volt >=
di->bat->bat_type[di->bat->batt_id].termination_vol ||
di->events.usb_cv_active || di->events.ac_cv_active) &&
di->batt_data.avg_curr <
di->bat->bat_type[di->bat->batt_id].termination_curr &&
di->batt_data.avg_curr > 0) {
if (++di->eoc_cnt >= EOC_COND_CNT) {
di->eoc_cnt = 0;
di->charge_status = POWER_SUPPLY_STATUS_FULL;
di->maintenance_chg = true;
dev_dbg(di->dev, "EOC reached!\n");
power_supply_changed(&di->chargalg_psy);
} else {
dev_dbg(di->dev,
" EOC limit reached for the %d"
" time, out of %d before EOC\n",
di->eoc_cnt,
EOC_COND_CNT);
}
} else {
di->eoc_cnt = 0;
}
}
static void init_maxim_chg_curr(struct abx500_chargalg *di)
{
di->ccm.original_iset =
di->bat->bat_type[di->bat->batt_id].normal_cur_lvl;
di->ccm.current_iset =
di->bat->bat_type[di->bat->batt_id].normal_cur_lvl;
di->ccm.test_delta_i = di->bat->maxi->charger_curr_step;
di->ccm.max_current = di->bat->maxi->chg_curr;
di->ccm.condition_cnt = di->bat->maxi->wait_cycles;
di->ccm.level = 0;
}
/**
* abx500_chargalg_chg_curr_maxim - increases the charger current to
* compensate for the system load
* @di pointer to the abx500_chargalg structure
*
* This maximization function is used to raise the charger current to get the
* battery current as close to the optimal value as possible. The battery
* current during charging is affected by the system load
*/
static enum maxim_ret abx500_chargalg_chg_curr_maxim(struct abx500_chargalg *di)
{
int delta_i;
if (!di->bat->maxi->ena_maxi)
return MAXIM_RET_NOACTION;
delta_i = di->ccm.original_iset - di->batt_data.inst_curr;
if (di->events.vbus_collapsed) {
dev_dbg(di->dev, "Charger voltage has collapsed %d\n",
di->ccm.wait_cnt);
if (di->ccm.wait_cnt == 0) {
dev_dbg(di->dev, "lowering current\n");
di->ccm.wait_cnt++;
di->ccm.condition_cnt = di->bat->maxi->wait_cycles;
di->ccm.max_current =
di->ccm.current_iset - di->ccm.test_delta_i;
di->ccm.current_iset = di->ccm.max_current;
di->ccm.level--;
return MAXIM_RET_CHANGE;
} else {
dev_dbg(di->dev, "waiting\n");
/* Let's go in here twice before lowering curr again */
di->ccm.wait_cnt = (di->ccm.wait_cnt + 1) % 3;
return MAXIM_RET_NOACTION;
}
}
di->ccm.wait_cnt = 0;
if ((di->batt_data.inst_curr > di->ccm.original_iset)) {
dev_dbg(di->dev, " Maximization Ibat (%dmA) too high"
" (limit %dmA) (current iset: %dmA)!\n",
di->batt_data.inst_curr, di->ccm.original_iset,
di->ccm.current_iset);
if (di->ccm.current_iset == di->ccm.original_iset)
return MAXIM_RET_NOACTION;
di->ccm.condition_cnt = di->bat->maxi->wait_cycles;
di->ccm.current_iset = di->ccm.original_iset;
di->ccm.level = 0;
return MAXIM_RET_IBAT_TOO_HIGH;
}
if (delta_i > di->ccm.test_delta_i &&
(di->ccm.current_iset + di->ccm.test_delta_i) <
di->ccm.max_current) {
if (di->ccm.condition_cnt-- == 0) {
/* Increse the iset with cco.test_delta_i */
di->ccm.condition_cnt = di->bat->maxi->wait_cycles;
di->ccm.current_iset += di->ccm.test_delta_i;
di->ccm.level++;
dev_dbg(di->dev, " Maximization needed, increase"
" with %d mA to %dmA (Optimal ibat: %d)"
" Level %d\n",
di->ccm.test_delta_i,
di->ccm.current_iset,
di->ccm.original_iset,
di->ccm.level);
return MAXIM_RET_CHANGE;
} else {
return MAXIM_RET_NOACTION;
}
} else {
di->ccm.condition_cnt = di->bat->maxi->wait_cycles;
return MAXIM_RET_NOACTION;
}
}
static void handle_maxim_chg_curr(struct abx500_chargalg *di)
{
enum maxim_ret ret;
int result;
ret = abx500_chargalg_chg_curr_maxim(di);
switch (ret) {
case MAXIM_RET_CHANGE:
result = abx500_chargalg_update_chg_curr(di,
di->ccm.current_iset);
if (result)
dev_err(di->dev, "failed to set chg curr\n");
break;
case MAXIM_RET_IBAT_TOO_HIGH:
result = abx500_chargalg_update_chg_curr(di,
di->bat->bat_type[di->bat->batt_id].normal_cur_lvl);
if (result)
dev_err(di->dev, "failed to set chg curr\n");
break;
case MAXIM_RET_NOACTION:
default:
/* Do nothing..*/
break;
}
}
static int abx500_chargalg_get_ext_psy_data(struct device *dev, void *data)
{
struct power_supply *psy;
struct power_supply *ext;
struct abx500_chargalg *di;
union power_supply_propval ret;
int i, j;
bool psy_found = false;
psy = (struct power_supply *)data;
ext = dev_get_drvdata(dev);
di = to_abx500_chargalg_device_info(psy);
/* For all psy where the driver name appears in any supplied_to */
for (i = 0; i < ext->num_supplicants; i++) {
if (!strcmp(ext->supplied_to[i], psy->name))
psy_found = true;
}
if (!psy_found)
return 0;
/* Go through all properties for the psy */
for (j = 0; j < ext->num_properties; j++) {
enum power_supply_property prop;
prop = ext->properties[j];
/* Initialize chargers if not already done */
if (!di->ac_chg &&
ext->type == POWER_SUPPLY_TYPE_MAINS)
di->ac_chg = psy_to_ux500_charger(ext);
else if (!di->usb_chg &&
ext->type == POWER_SUPPLY_TYPE_USB)
di->usb_chg = psy_to_ux500_charger(ext);
if (ext->get_property(ext, prop, &ret))
continue;
switch (prop) {
case POWER_SUPPLY_PROP_PRESENT:
switch (ext->type) {
case POWER_SUPPLY_TYPE_BATTERY:
/* Battery present */
if (ret.intval)
di->events.batt_rem = false;
/* Battery removed */
else
di->events.batt_rem = true;
break;
case POWER_SUPPLY_TYPE_MAINS:
/* AC disconnected */
if (!ret.intval &&
(di->chg_info.conn_chg & AC_CHG)) {
di->chg_info.prev_conn_chg =
di->chg_info.conn_chg;
di->chg_info.conn_chg &= ~AC_CHG;
}
/* AC connected */
else if (ret.intval &&
!(di->chg_info.conn_chg & AC_CHG)) {
di->chg_info.prev_conn_chg =
di->chg_info.conn_chg;
di->chg_info.conn_chg |= AC_CHG;
}
break;
case POWER_SUPPLY_TYPE_USB:
/* USB disconnected */
if (!ret.intval &&
(di->chg_info.conn_chg & USB_CHG)) {
di->chg_info.prev_conn_chg =
di->chg_info.conn_chg;
di->chg_info.conn_chg &= ~USB_CHG;
}
/* USB connected */
else if (ret.intval &&
!(di->chg_info.conn_chg & USB_CHG)) {
di->chg_info.prev_conn_chg =
di->chg_info.conn_chg;
di->chg_info.conn_chg |= USB_CHG;
}
break;
default:
break;
}
break;
case POWER_SUPPLY_PROP_ONLINE:
switch (ext->type) {
case POWER_SUPPLY_TYPE_BATTERY:
break;
case POWER_SUPPLY_TYPE_MAINS:
/* AC offline */
if (!ret.intval &&
(di->chg_info.online_chg & AC_CHG)) {
di->chg_info.prev_online_chg =
di->chg_info.online_chg;
di->chg_info.online_chg &= ~AC_CHG;
}
/* AC online */
else if (ret.intval &&
!(di->chg_info.online_chg & AC_CHG)) {
di->chg_info.prev_online_chg =
di->chg_info.online_chg;
di->chg_info.online_chg |= AC_CHG;
queue_delayed_work(di->chargalg_wq,
&di->chargalg_wd_work, 0);
}
break;
case POWER_SUPPLY_TYPE_USB:
/* USB offline */
if (!ret.intval &&
(di->chg_info.online_chg & USB_CHG)) {
di->chg_info.prev_online_chg =
di->chg_info.online_chg;
di->chg_info.online_chg &= ~USB_CHG;
}
/* USB online */
else if (ret.intval &&
!(di->chg_info.online_chg & USB_CHG)) {
di->chg_info.prev_online_chg =
di->chg_info.online_chg;
di->chg_info.online_chg |= USB_CHG;
queue_delayed_work(di->chargalg_wq,
&di->chargalg_wd_work, 0);
}
break;
default:
break;
}
break;
case POWER_SUPPLY_PROP_HEALTH:
switch (ext->type) {
case POWER_SUPPLY_TYPE_BATTERY:
break;
case POWER_SUPPLY_TYPE_MAINS:
switch (ret.intval) {
case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
di->events.mainextchnotok = true;
di->events.main_thermal_prot = false;
di->events.main_ovv = false;
di->events.ac_wd_expired = false;
break;
case POWER_SUPPLY_HEALTH_DEAD:
di->events.ac_wd_expired = true;
di->events.mainextchnotok = false;
di->events.main_ovv = false;
di->events.main_thermal_prot = false;
break;
case POWER_SUPPLY_HEALTH_COLD:
case POWER_SUPPLY_HEALTH_OVERHEAT: