-
Notifications
You must be signed in to change notification settings - Fork 55
/
charge_state_v2.c
2951 lines (2602 loc) · 84.3 KB
/
charge_state_v2.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 2014 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
* Battery charging task and state machine.
*/
#include "battery.h"
#include "battery_smart.h"
#include "charge_manager.h"
#include "charger_profile_override.h"
#include "charge_state.h"
#include "charger.h"
#include "chipset.h"
#include "common.h"
#include "console.h"
#include "ec_ec_comm_master.h"
#include "ec_ec_comm_slave.h"
#include "extpower.h"
#include "gpio.h"
#include "hooks.h"
#include "host_command.h"
#include "i2c.h"
#include "math_util.h"
#include "printf.h"
#include "system.h"
#include "task.h"
#include "throttle_ap.h"
#include "timer.h"
#include "usb_common.h"
#include "usb_pd.h"
#include "util.h"
/* Console output macros */
#define CPUTS(outstr) cputs(CC_CHARGER, outstr)
#define CPRINTS(format, args...) cprints(CC_CHARGER, format, ## args)
#define CPRINTF(format, args...) cprintf(CC_CHARGER, format, ## args)
/* Extra debugging prints when allocating power between lid and base. */
#undef CHARGE_ALLOCATE_EXTRA_DEBUG
#define CRITICAL_BATTERY_SHUTDOWN_TIMEOUT_US \
(CONFIG_BATTERY_CRITICAL_SHUTDOWN_TIMEOUT * SECOND)
#define PRECHARGE_TIMEOUT_US (PRECHARGE_TIMEOUT * SECOND)
#define LFCC_EVENT_THRESH 5 /* Full-capacity change reqd for host event */
#ifdef CONFIG_THROTTLE_AP_ON_BAT_DISCHG_CURRENT
#ifndef CONFIG_HOSTCMD_EVENTS
#error "CONFIG_THROTTLE_AP_ON_BAT_DISCHG_CURRENT needs CONFIG_HOSTCMD_EVENTS"
#endif /* CONFIG_HOSTCMD_EVENTS */
#define BAT_OCP_TIMEOUT_US (60 * SECOND)
/* BAT_OCP_HYSTERESIS_PCT can be optionally overridden in board.h. */
#ifndef BAT_OCP_HYSTERESIS_PCT
#define BAT_OCP_HYSTERESIS_PCT 10
#endif /* BAT_OCP_HYSTERESIS_PCT */
#define BAT_OCP_HYSTERESIS \
(BAT_MAX_DISCHG_CURRENT * BAT_OCP_HYSTERESIS_PCT / 100) /* mA */
#endif /* CONFIG_THROTTLE_AP_ON_BAT_DISCHG_CURRENT */
#ifdef CONFIG_THROTTLE_AP_ON_BAT_VOLTAGE
#ifndef CONFIG_HOSTCMD_EVENTS
#error "CONFIG_THROTTLE_AP_ON_BAT_VOLTAGE needs CONFIG_HOSTCMD_EVENTS"
#endif /* CONFIG_HOSTCMD_EVENTS */
#define BAT_UVP_TIMEOUT_US (60 * SECOND)
/* BAT_UVP_HYSTERESIS_PCT can be optionally overridden in board.h. */
#ifndef BAT_UVP_HYSTERESIS_PCT
#define BAT_UVP_HYSTERESIS_PCT 3
#endif /* BAT_UVP_HYSTERESIS_PCT */
#define BAT_UVP_HYSTERESIS \
(BAT_LOW_VOLTAGE_THRESH * BAT_UVP_HYSTERESIS_PCT / 100) /* mV */
static timestamp_t uvp_throttle_start_time;
#endif /* CONFIG_THROTTLE_AP_ON_BAT_OLTAGE */
static int charge_request(int voltage, int current);
static uint8_t battery_level_shutdown;
/*
* State for charger_task(). Here so we can reset it on a HOOK_INIT, and
* because stack space is more limited than .bss
*/
static const struct battery_info *batt_info;
static struct charge_state_data curr;
static enum charge_state_v2 prev_state;
static int prev_ac, prev_charge, prev_full, prev_disp_charge;
static enum battery_present prev_bp;
static int is_full; /* battery not accepting current */
static enum ec_charge_control_mode chg_ctl_mode;
static int manual_voltage; /* Manual voltage override (-1 = no override) */
static int manual_current; /* Manual current override (-1 = no override) */
static unsigned int user_current_limit = -1U;
test_export_static timestamp_t shutdown_target_time;
static timestamp_t precharge_start_time;
/*
* The timestamp when the battery charging current becomes stable.
* When a new charging status happens, charger needs several seconds to
* stabilize the battery charging current.
* stable_current should be evaluated when stable_ts expired.
* stable_ts should be reset if the charger input voltage/current changes,
* or a new battery charging voltage/request happened.
* By evaluating stable_current, we can evaluate the battery's desired charging
* power desired_mw. This allow us to have a better charging efficiency by
* negotiating the most fit PDO, i.e. the PDO provides the power just enough for
* the system and battery, or the PDO with preferred voltage.
*/
STATIC_IF(CONFIG_USB_PD_PREFER_MV) timestamp_t stable_ts;
/* battery charging current evaluated after stable_ts expired */
STATIC_IF(CONFIG_USB_PD_PREFER_MV) int stable_current;
/* battery desired power in mW. This is used to negotiate the suitable PDO */
STATIC_IF(CONFIG_USB_PD_PREFER_MV) int desired_mw;
STATIC_IF_NOT(CONFIG_USB_PD_PREFER_MV) struct pd_pref_config_t pd_pref_config;
#ifdef CONFIG_EC_EC_COMM_BATTERY_MASTER
static int base_connected;
/* Base has responded to one of our commands already. */
static int base_responsive;
static int charge_base;
static int prev_charge_base;
static int prev_current_base;
static int prev_allow_charge_base;
static int prev_current_lid;
/*
* In debugging mode, with AC, input current to allocate to base. Negative
* value disables manual mode.
*/
static int manual_ac_current_base = -1;
/*
* In debugging mode, when discharging, current to transfer from lid to base
* (negative to transfer from base to lid). Only valid when enabled is true.
*/
static int manual_noac_enabled;
static int manual_noac_current_base;
#else
static const int base_connected;
#endif
/* Is battery connected but unresponsive after precharge? */
static int battery_seems_to_be_dead;
static int battery_seems_to_be_disconnected;
/*
* Was battery removed? Set when we see BP_NO, cleared after the battery is
* reattached and becomes responsive. Used to indicate an error state after
* removal and trigger re-reading the battery static info when battery is
* reattached and responsive.
*/
static int battery_was_removed;
static int problems_exist;
static int debugging;
/* Track problems in communicating with the battery or charger */
enum problem_type {
PR_STATIC_UPDATE,
PR_SET_VOLTAGE,
PR_SET_CURRENT,
PR_SET_MODE,
PR_SET_INPUT_CURR,
PR_POST_INIT,
PR_CHG_FLAGS,
PR_BATT_FLAGS,
PR_CUSTOM,
PR_CFG_SEC_CHG,
NUM_PROBLEM_TYPES
};
static const char * const prob_text[] = {
"static update",
"set voltage",
"set current",
"set mode",
"set input current",
"post init",
"chg params",
"batt params",
"custom profile",
"cfg secondary chg"
};
BUILD_ASSERT(ARRAY_SIZE(prob_text) == NUM_PROBLEM_TYPES);
/*
* TODO(crosbug.com/p/27639): When do we decide a problem is real and not
* just intermittent? And what do we do about it?
*/
static void problem(enum problem_type p, int v)
{
static int __bss_slow last_prob_val[NUM_PROBLEM_TYPES];
static timestamp_t __bss_slow last_prob_time[NUM_PROBLEM_TYPES];
timestamp_t t_now, t_diff;
if (last_prob_val[p] != v) {
t_now = get_time();
t_diff.val = t_now.val - last_prob_time[p].val;
CPRINTS("charge problem: %s, 0x%x -> 0x%x after %.6" PRId64 "s",
prob_text[p], last_prob_val[p], v, t_diff.val);
last_prob_val[p] = v;
last_prob_time[p] = t_now;
}
problems_exist = 1;
}
#ifdef CONFIG_EC_EC_COMM_BATTERY_MASTER
/*
* Parameters for dual-battery policy.
* TODO(b:71881017): This should be made configurable by AP in the future.
*/
struct dual_battery_policy {
/*** Policies when AC is not connected. ***/
/* Voltage to use when using OTG mode between lid and base (mV) */
uint16_t otg_voltage;
/* Maximum current to apply from base to lid (mA) */
uint16_t max_base_to_lid_current;
/*
* Margin to apply between provided OTG output current and input current
* limit, to make sure that input charger does not overcurrent output
* charger. input_current = (1-margin) * output_current. (/128)
*/
uint8_t margin_otg_current;
/* Only do base to lid OTG when base battery above this value (%) */
uint8_t min_charge_base_otg;
/*
* When base/lid battery percentage is below this value, do
* battery-to-battery charging. (%)
*/
uint8_t max_charge_base_batt_to_batt;
uint8_t max_charge_lid_batt_to_batt;
/*** Policies when AC is connected. ***/
/* Minimum power to allocate to base (mW), includes some margin to allow
* base to charge when critically low.
*/
uint16_t min_base_system_power;
/* Smoothing factor for lid power (/128) */
uint8_t lid_system_power_smooth;
/*
* Smoothing factor for base/lid battery power, when the battery power
* is decreasing only: we try to estimate the maximum power that the
* battery is willing to take and always reset it when it draws more
* than the estimate. (/128)
*/
uint8_t battery_power_smooth;
/*
* Margin to add to requested base/lid battery power, to figure out how
* much current to allocate. allocation = (1+margin) * request. (/128)
*/
uint8_t margin_base_battery_power;
uint8_t margin_lid_battery_power;
/* Maximum current to apply from lid to base (mA) */
uint16_t max_lid_to_base_current;
};
static const struct dual_battery_policy db_policy = {
.otg_voltage = 12000, /* mV */
.max_base_to_lid_current = 1800, /* mA, about 2000mA with margin. */
.margin_otg_current = 13, /* /128 = 10.1% */
.min_charge_base_otg = 5, /* % */
.max_charge_base_batt_to_batt = 4, /* % */
.max_charge_lid_batt_to_batt = 10, /* % */
.min_base_system_power = 1300, /* mW */
.lid_system_power_smooth = 32, /* 32/128 = 0.25 */
.battery_power_smooth = 1, /* 1/128 = 0.008 */
.margin_base_battery_power = 32, /* 32/128 = 0.25 */
.margin_lid_battery_power = 32, /* 32/128 = 0.25 */
.max_lid_to_base_current = 2000, /* mA */
};
/* Add at most "value" to power_var, subtracting from total_power budget. */
#define CHG_ALLOCATE(power_var, total_power, value) do { \
int val_capped = MIN(value, total_power); \
(power_var) += val_capped; \
(total_power) -= val_capped; \
} while (0)
/* Update base battery information */
static void update_base_battery_info(void)
{
struct ec_response_battery_dynamic_info *const bd =
&battery_dynamic[BATT_IDX_BASE];
base_connected = board_is_base_connected();
if (!base_connected) {
const int invalid_flags = EC_BATT_FLAG_INVALID_DATA;
/* Invalidate static/dynamic information */
if (bd->flags != invalid_flags) {
bd->flags = invalid_flags;
host_set_single_event(EC_HOST_EVENT_BATTERY);
host_set_single_event(EC_HOST_EVENT_BATTERY_STATUS);
}
charge_base = -1;
base_responsive = 0;
prev_current_base = 0;
prev_allow_charge_base = 0;
} else if (base_responsive) {
int old_flags = bd->flags;
int flags_changed;
int old_full_capacity = bd->full_capacity;
ec_ec_master_base_get_dynamic_info();
flags_changed = (old_flags != bd->flags);
/* Fetch static information when flags change. */
if (flags_changed)
ec_ec_master_base_get_static_info();
battery_memmap_refresh(BATT_IDX_BASE);
/* Newly connected battery, or change in capacity. */
if (old_flags & EC_BATT_FLAG_INVALID_DATA ||
((old_flags & EC_BATT_FLAG_BATT_PRESENT) !=
(bd->flags & EC_BATT_FLAG_BATT_PRESENT)) ||
old_full_capacity != bd->full_capacity)
host_set_single_event(EC_HOST_EVENT_BATTERY);
if (flags_changed)
host_set_single_event(EC_HOST_EVENT_BATTERY_STATUS);
/* Update charge_base */
if (bd->flags & (BATT_FLAG_BAD_FULL_CAPACITY |
BATT_FLAG_BAD_REMAINING_CAPACITY))
charge_base = -1;
else if (bd->full_capacity > 0)
charge_base = 100 * bd->remaining_capacity
/ bd->full_capacity;
else
charge_base = 0;
}
}
/**
* Setup current settings for base, and record previous values, if the base
* is responsive.
*
* @param current_base Current to be drawn by base (negative to provide power)
* @param allow_charge_base Whether base battery should be charged (only makes
* sense with positive current)
*/
static int set_base_current(int current_base, int allow_charge_base)
{
/* "OTG" voltage from base to lid. */
const int otg_voltage = db_policy.otg_voltage;
int ret;
ret = ec_ec_master_base_charge_control(current_base,
otg_voltage, allow_charge_base);
if (ret) {
/* Ignore errors until the base is responsive. */
if (base_responsive)
return ret;
} else {
base_responsive = 1;
prev_current_base = current_base;
prev_allow_charge_base = allow_charge_base;
}
return EC_RES_SUCCESS;
}
/**
* Setup current settings for lid and base, in a safe way.
*
* @param current_base Current to be drawn by base (negative to provide power)
* @param allow_charge_base Whether base battery should be charged (only makes
* sense with positive current)
* @param current_lid Current to be drawn by lid (negative to provide power)
* @param allow_charge_lid Whether lid battery should be charged
*/
static void set_base_lid_current(int current_base, int allow_charge_base,
int current_lid, int allow_charge_lid)
{
/* "OTG" voltage from lid to base. */
const int otg_voltage = db_policy.otg_voltage;
int lid_first;
int ret;
int chgnum = 0;
/* TODO(b:71881017): This is still quite verbose during charging. */
if (prev_current_base != current_base ||
prev_allow_charge_base != allow_charge_base ||
prev_current_lid != current_lid) {
CPRINTS("Base/Lid: %d%s/%d%s mA",
current_base, allow_charge_base ? "+" : "",
current_lid, allow_charge_lid ? "+" : "");
}
/*
* To decide whether to first control the lid or the base, we first
* control the side that _reduces_ current that would be drawn, then
* setup one that would start providing power, then increase current.
*/
if (current_lid >= 0 && current_lid < prev_current_lid)
lid_first = 1; /* Lid decreases current */
else if (current_base >= 0 && current_base < prev_current_base)
lid_first = 0; /* Base decreases current */
else if (current_lid < 0)
lid_first = 1; /* Lid provide power */
else
lid_first = 0; /* All other cases: control the base first */
if (!lid_first && base_connected) {
ret = set_base_current(current_base, allow_charge_base);
if (ret)
return;
}
if (current_lid >= 0) {
ret = charge_set_output_current_limit(CHARGER_SOLO, 0, 0);
if (ret)
return;
ret = charger_set_input_current(chgnum, current_lid);
if (ret)
return;
if (allow_charge_lid)
ret = charge_request(curr.requested_voltage,
curr.requested_current);
else
ret = charge_request(0, 0);
} else {
ret = charge_set_output_current_limit(CHARGER_SOLO,
-current_lid, otg_voltage);
}
if (ret)
return;
prev_current_lid = current_lid;
if (lid_first && base_connected) {
ret = set_base_current(current_base, allow_charge_base);
if (ret)
return;
}
/*
* Make sure cross-power is enabled (it might not be enabled right after
* plugging the base, or when an adapter just got connected).
*/
if (base_connected && current_base != 0)
board_enable_base_power(1);
}
/**
* Smooth power value, covering some edge cases.
* Compute s*curr+(1-s)*prev, where s is in 1/128 unit.
*/
static int smooth_value(int prev, int curr, int s)
{
if (curr < 0)
curr = 0;
if (prev < 0)
return curr;
return prev + s * (curr - prev) / 128;
}
/**
* Add margin m to value. Compute (1+m)*value, where m is in 1/128 unit.
*/
static int add_margin(int value, int m)
{
return value + m * value / 128;
}
static void charge_allocate_input_current_limit(void)
{
/*
* All the power numbers are in mW.
*
* Since we work with current and voltage in mA and mV, multiplying them
* gives numbers in uW, which are dangerously close to overflowing when
* doing intermediate computations (60W * 100 overflows a 32-bit int,
* for example). We therefore divide the product by 1000 and re-multiply
* the power numbers by 1000 when converting them back to current.
*/
int total_power = 0;
static int prev_base_battery_power = -1;
int base_battery_power = 0;
int base_battery_power_max = 0;
static int prev_lid_system_power = -1;
int lid_system_power;
static int prev_lid_battery_power = -1;
int lid_battery_power = 0;
int lid_battery_power_max = 0;
int power_base = 0;
int power_lid = 0;
int current_base = 0;
int current_lid = 0;
int charge_lid = charge_get_percent();
const struct ec_response_battery_dynamic_info *const base_bd =
&battery_dynamic[BATT_IDX_BASE];
if (!base_connected) {
set_base_lid_current(0, 0, curr.desired_input_current, 1);
prev_base_battery_power = -1;
return;
}
/* Charging */
if (curr.desired_input_current > 0 && curr.input_voltage > 0)
total_power =
curr.desired_input_current * curr.input_voltage / 1000;
/*
* TODO(b:71723024): We should be able to replace this test by curr.ac,
* but the value is currently wrong, especially during transitions.
*/
if (total_power <= 0) {
int base_critical = charge_base >= 0 &&
charge_base < db_policy.max_charge_base_batt_to_batt;
/* Discharging */
prev_base_battery_power = -1;
prev_lid_system_power = -1;
prev_lid_battery_power = -1;
/* Manual control */
if (manual_noac_enabled) {
int lid_current, base_current;
if (manual_noac_current_base > 0) {
base_current = -manual_noac_current_base;
lid_current =
add_margin(manual_noac_current_base,
db_policy.margin_otg_current);
} else {
lid_current = manual_noac_current_base;
base_current =
add_margin(-manual_noac_current_base,
db_policy.margin_otg_current);
}
set_base_lid_current(base_current, 0, lid_current, 0);
return;
}
/*
* System is off, cut power to the base. We'll reset the base
* when system restarts, or when AC is plugged.
*/
if (chipset_in_state(CHIPSET_STATE_ANY_OFF)) {
set_base_lid_current(0, 0, 0, 0);
if (base_responsive) {
/* Base still responsive, put it to sleep. */
CPRINTF("Hibernating base\n");
ec_ec_master_hibernate();
base_responsive = 0;
board_enable_base_power(0);
}
return;
}
/*
* System is suspended, let the lid and base run on their
* own power. However, if the base battery is critically low, we
* still want to provide power to the base, to make sure it
* stays alive to be able to wake the system on keyboard or
* touchpad events.
*/
if (chipset_in_state(CHIPSET_STATE_ANY_SUSPEND) &&
!base_critical) {
set_base_lid_current(0, 0, 0, 0);
return;
}
if (charge_base > db_policy.min_charge_base_otg) {
int lid_current = db_policy.max_base_to_lid_current;
int base_current = add_margin(lid_current,
db_policy.margin_otg_current);
/* Draw current from base to lid */
set_base_lid_current(-base_current, 0, lid_current,
charge_lid < db_policy.max_charge_lid_batt_to_batt);
} else {
/*
* Base battery is too low, apply power to it, and allow
* it to charge if it is critically low.
*
* TODO(b:71881017): When suspended, this will make the
* battery charge oscillate between 3 and 4 percent,
* which might not be great for battery life. We need
* some hysteresis.
*/
/*
* TODO(b:71881017): Precompute (ideally, at build time)
* the base_current, so we do not need to do a division
* here.
*/
int base_current =
(db_policy.min_base_system_power * 1000) /
db_policy.otg_voltage;
int lid_current = add_margin(base_current,
db_policy.margin_otg_current);
set_base_lid_current(base_current, base_critical,
-lid_current, 0);
}
return;
}
/* Manual control */
if (manual_ac_current_base >= 0) {
int current_base = manual_ac_current_base;
int current_lid =
curr.desired_input_current - manual_ac_current_base;
if (current_lid < 0) {
current_base = curr.desired_input_current;
current_lid = 0;
}
set_base_lid_current(current_base, 1, current_lid, 1);
return;
}
/* Estimate system power. */
lid_system_power = charger_get_system_power() / 1000;
/* Smooth system power, as it is very spiky */
lid_system_power = smooth_value(prev_lid_system_power,
lid_system_power, db_policy.lid_system_power_smooth);
prev_lid_system_power = lid_system_power;
/*
* TODO(b:71881017): Smoothing the battery power isn't necessarily a
* good idea: if the system takes up too much power, we may reduce the
* estimate power too quickly, leading to oscillations when the system
* power goes down. Instead, we should probably estimate the current
* based on remaining capacity.
*/
/* Estimate lid battery power. */
if (!(curr.batt.flags &
(BATT_FLAG_BAD_VOLTAGE | BATT_FLAG_BAD_CURRENT)))
lid_battery_power = curr.batt.current *
curr.batt.voltage / 1000;
if (lid_battery_power < prev_lid_battery_power)
lid_battery_power = smooth_value(prev_lid_battery_power,
lid_battery_power, db_policy.battery_power_smooth);
if (!(curr.batt.flags &
(BATT_FLAG_BAD_DESIRED_VOLTAGE |
BATT_FLAG_BAD_DESIRED_CURRENT)))
lid_battery_power_max = curr.batt.desired_current *
curr.batt.desired_voltage / 1000;
lid_battery_power = MIN(lid_battery_power, lid_battery_power_max);
/* Estimate base battery power. */
if (!(base_bd->flags & EC_BATT_FLAG_INVALID_DATA)) {
base_battery_power = base_bd->actual_current *
base_bd->actual_voltage / 1000;
base_battery_power_max = base_bd->desired_current *
base_bd->desired_voltage / 1000;
}
if (base_battery_power < prev_base_battery_power)
base_battery_power = smooth_value(prev_base_battery_power,
base_battery_power, db_policy.battery_power_smooth);
base_battery_power = MIN(base_battery_power, base_battery_power_max);
if (debugging) {
CPRINTF("%s:\n", __func__);
CPRINTF("total power: %d\n", total_power);
CPRINTF("base battery power: %d (%d)\n",
base_battery_power, base_battery_power_max);
CPRINTF("lid system power: %d\n", lid_system_power);
CPRINTF("lid battery power: %d\n", lid_battery_power);
CPRINTF("percent base/lid: %d%% %d%%\n",
charge_base, charge_lid);
}
prev_lid_battery_power = lid_battery_power;
prev_base_battery_power = base_battery_power;
if (total_power > 0) { /* Charging */
/* Allocate system power */
CHG_ALLOCATE(power_base, total_power,
db_policy.min_base_system_power);
CHG_ALLOCATE(power_lid, total_power, lid_system_power);
/* Allocate lid, then base battery power */
lid_battery_power = add_margin(lid_battery_power,
db_policy.margin_lid_battery_power);
CHG_ALLOCATE(power_lid, total_power, lid_battery_power);
base_battery_power = add_margin(base_battery_power,
db_policy.margin_base_battery_power);
CHG_ALLOCATE(power_base, total_power, base_battery_power);
/* Give everything else to the lid. */
CHG_ALLOCATE(power_lid, total_power, total_power);
if (debugging)
CPRINTF("power: base %d mW / lid %d mW\n",
power_base, power_lid);
current_base = 1000 * power_base / curr.input_voltage;
current_lid = 1000 * power_lid / curr.input_voltage;
if (current_base > db_policy.max_lid_to_base_current) {
current_lid += (current_base
- db_policy.max_lid_to_base_current);
current_base = db_policy.max_lid_to_base_current;
}
if (debugging)
CPRINTF("current: base %d mA / lid %d mA\n",
current_base, current_lid);
set_base_lid_current(current_base, 1, current_lid, 1);
} else { /* Discharging */
}
if (debugging)
CPRINTF("====\n");
}
#endif /* CONFIG_EC_EC_COMM_BATTERY_MASTER */
#ifndef CONFIG_BATTERY_V2
/* Returns zero if every item was updated. */
static int update_static_battery_info(void)
{
char *batt_str;
int batt_serial;
uint8_t batt_flags = 0;
/*
* The return values have type enum ec_error_list, but EC_SUCCESS is
* zero. We'll just look for any failures so we can try them all again.
*/
int rv;
/* Smart battery serial number is 16 bits */
batt_str = (char *)host_get_memmap(EC_MEMMAP_BATT_SERIAL);
memset(batt_str, 0, EC_MEMMAP_TEXT_MAX);
rv = battery_serial_number(&batt_serial);
if (!rv)
snprintf(batt_str, EC_MEMMAP_TEXT_MAX, "%04X", batt_serial);
/* Design Capacity of Full */
rv |= battery_design_capacity(
(int *)host_get_memmap(EC_MEMMAP_BATT_DCAP));
/* Design Voltage */
rv |= battery_design_voltage(
(int *)host_get_memmap(EC_MEMMAP_BATT_DVLT));
/* Last Full Charge Capacity (this is only mostly static) */
rv |= battery_full_charge_capacity(
(int *)host_get_memmap(EC_MEMMAP_BATT_LFCC));
/* Cycle Count */
rv |= battery_cycle_count((int *)host_get_memmap(EC_MEMMAP_BATT_CCNT));
/* Battery Manufacturer string */
batt_str = (char *)host_get_memmap(EC_MEMMAP_BATT_MFGR);
memset(batt_str, 0, EC_MEMMAP_TEXT_MAX);
rv |= battery_manufacturer_name(batt_str, EC_MEMMAP_TEXT_MAX);
/* Battery Model string */
batt_str = (char *)host_get_memmap(EC_MEMMAP_BATT_MODEL);
memset(batt_str, 0, EC_MEMMAP_TEXT_MAX);
rv |= battery_device_name(batt_str, EC_MEMMAP_TEXT_MAX);
/* Battery Type string */
batt_str = (char *)host_get_memmap(EC_MEMMAP_BATT_TYPE);
rv |= battery_device_chemistry(batt_str, EC_MEMMAP_TEXT_MAX);
/* Zero the dynamic entries. They'll come next. */
*(int *)host_get_memmap(EC_MEMMAP_BATT_VOLT) = 0;
*(int *)host_get_memmap(EC_MEMMAP_BATT_RATE) = 0;
*(int *)host_get_memmap(EC_MEMMAP_BATT_CAP) = 0;
*(int *)host_get_memmap(EC_MEMMAP_BATT_LFCC) = 0;
if (extpower_is_present())
batt_flags |= EC_BATT_FLAG_AC_PRESENT;
*host_get_memmap(EC_MEMMAP_BATT_FLAG) = batt_flags;
if (rv)
problem(PR_STATIC_UPDATE, rv);
else
/* No errors seen. Battery data is now present */
*host_get_memmap(EC_MEMMAP_BATTERY_VERSION) = 1;
return rv;
}
static void update_dynamic_battery_info(void)
{
/* The memmap address is constant. We should fix these calls somehow. */
int *memmap_volt = (int *)host_get_memmap(EC_MEMMAP_BATT_VOLT);
int *memmap_rate = (int *)host_get_memmap(EC_MEMMAP_BATT_RATE);
int *memmap_cap = (int *)host_get_memmap(EC_MEMMAP_BATT_CAP);
int *memmap_lfcc = (int *)host_get_memmap(EC_MEMMAP_BATT_LFCC);
uint8_t *memmap_flags = host_get_memmap(EC_MEMMAP_BATT_FLAG);
uint8_t tmp;
int send_batt_status_event = 0;
int send_batt_info_event = 0;
static int __bss_slow batt_present;
static int batt_os_percentage;
tmp = 0;
#ifdef CONFIG_EXTPOWER_GPIO
/* sync AC present flag to avoid OS ac flag flicker */
if (extpower_is_present())
tmp |= EC_BATT_FLAG_AC_PRESENT;
#else
if (curr.ac)
tmp |= EC_BATT_FLAG_AC_PRESENT;
#endif
if (curr.batt.is_present == BP_YES) {
tmp |= EC_BATT_FLAG_BATT_PRESENT;
batt_present = 1;
/* Tell the AP to read battery info if it is newly present. */
if (!(*memmap_flags & EC_BATT_FLAG_BATT_PRESENT))
send_batt_info_event++;
} else {
/*
* Require two consecutive updates with BP_NOT_SURE
* before reporting it gone to the host.
*/
if (batt_present)
tmp |= EC_BATT_FLAG_BATT_PRESENT;
else if (*memmap_flags & EC_BATT_FLAG_BATT_PRESENT)
send_batt_info_event++;
batt_present = 0;
}
if (curr.batt.flags & EC_BATT_FLAG_INVALID_DATA)
tmp |= EC_BATT_FLAG_INVALID_DATA;
if (!(curr.batt.flags & BATT_FLAG_BAD_VOLTAGE))
*memmap_volt = curr.batt.voltage;
#ifdef CONFIG_EMI_REGION1
/* let the OS battery remaining time both empty and full time more smooth */
if (!(curr.batt.flags & BATT_FLAG_BAD_CURRENT))
*memmap_rate = ABS(battery_get_avg_current());
#else
if (!(curr.batt.flags & BATT_FLAG_BAD_CURRENT))
*memmap_rate = ABS(curr.batt.current);
#endif
if (!(curr.batt.flags & BATT_FLAG_BAD_REMAINING_CAPACITY)
#ifdef CONFIG_EMI_REGION1
&& !(curr.batt.flags & BATT_FLAG_BAD_FULL_CAPACITY)
#endif
) {
/*
* If we're running off the battery, it must have some charge.
* Don't report zero charge, as that has special meaning
* to Chrome OS powerd.
*/
if (curr.batt.remaining_capacity == 0 && !curr.batt_is_charging)
*memmap_cap = 1;
#ifdef CONFIG_EMI_REGION1
/* Avoid to show the percentage when battery fully charge */
else if (curr.ac && (curr.batt.status & STATUS_FULLY_CHARGED))
*memmap_cap = curr.batt.full_capacity;
#endif
else
*memmap_cap = curr.batt.remaining_capacity;
}
if (!(curr.batt.flags & BATT_FLAG_BAD_FULL_CAPACITY) &&
(curr.batt.full_capacity <= (*memmap_lfcc - LFCC_EVENT_THRESH) ||
curr.batt.full_capacity >= (*memmap_lfcc + LFCC_EVENT_THRESH))) {
*memmap_lfcc = curr.batt.full_capacity;
/* Poke the AP if the full_capacity changes. */
send_batt_info_event++;
}
if (curr.batt.is_present == BP_YES &&
!(curr.batt.flags & BATT_FLAG_BAD_STATE_OF_CHARGE) &&
curr.batt.state_of_charge <= BATTERY_LEVEL_CRITICAL)
tmp |= EC_BATT_FLAG_LEVEL_CRITICAL;
#ifdef CONFIG_EMI_REGION1
batt_os_percentage = (*memmap_cap * 1000) / (curr.batt.full_capacity + 1);
/*
* sync with OS battery percentage to avoid battery show charging icon at 100%
* os battery display formula: rounding (remainig / full capacity)*100
*/
if (curr.ac && batt_os_percentage > 994) {
tmp |= EC_BATT_FLAG_DISCHARGING;
} else {
tmp |= curr.batt_is_charging ? EC_BATT_FLAG_CHARGING :
EC_BATT_FLAG_DISCHARGING;
}
#else
tmp |= curr.batt_is_charging ? EC_BATT_FLAG_CHARGING :
EC_BATT_FLAG_DISCHARGING;
#endif
/* Tell the AP to re-read battery status if charge state changes */
if (*memmap_flags != tmp)
send_batt_status_event++;
/* Update flags before sending host events. */
*memmap_flags = tmp;
battery_charger_notify(tmp);
if (send_batt_info_event)
host_set_single_event(EC_HOST_EVENT_BATTERY);
if (send_batt_status_event)
host_set_single_event(EC_HOST_EVENT_BATTERY_STATUS);
}
#else /* CONFIG_BATTERY_V2 */
static int update_static_battery_info(void)
{
int batt_serial;
int val;
/*
* The return values have type enum ec_error_list, but EC_SUCCESS is
* zero. We'll just look for any failures so we can try them all again.
*/
int rv, ret;
struct ec_response_battery_static_info *const bs =
&battery_static[BATT_IDX_MAIN];
/* Clear all static information. */
memset(bs, 0, sizeof(*bs));
/* Smart battery serial number is 16 bits */
rv = battery_serial_number(&batt_serial);
if (!rv)
snprintf(bs->serial, sizeof(bs->serial), "%04X", batt_serial);
/* Design Capacity of Full */
ret = battery_design_capacity(&val);
if (!ret)
bs->design_capacity = val;
rv |= ret;
/* Design Voltage */
ret = battery_design_voltage(&val);
if (!ret)
bs->design_voltage = val;
rv |= ret;
/* Cycle Count */
ret = battery_cycle_count(&val);
if (!ret)
bs->cycle_count = val;
rv |= ret;
/* Battery Manufacturer string */
rv |= battery_manufacturer_name(bs->manufacturer,
sizeof(bs->manufacturer));
/* Battery Model string */
rv |= battery_device_name(bs->model, sizeof(bs->model));
/* Battery Type string */
rv |= battery_device_chemistry(bs->type, sizeof(bs->type));
/* Zero the dynamic entries. They'll come next. */
memset(&battery_dynamic[BATT_IDX_MAIN], 0,
sizeof(battery_dynamic[BATT_IDX_MAIN]));
if (rv)
problem(PR_STATIC_UPDATE, rv);
#ifdef HAS_TASK_HOSTCMD
battery_memmap_refresh(BATT_IDX_MAIN);
#endif
return rv;
}
static void update_dynamic_battery_info(void)
{
static int __bss_slow batt_present;
uint8_t tmp;
int send_batt_status_event = 0;
int send_batt_info_event = 0;
struct ec_response_battery_dynamic_info *const bd =
&battery_dynamic[BATT_IDX_MAIN];
tmp = 0;
if (curr.ac)
tmp |= EC_BATT_FLAG_AC_PRESENT;
if (curr.batt.is_present == BP_YES) {
tmp |= EC_BATT_FLAG_BATT_PRESENT;