summaryrefslogtreecommitdiff
path: root/board/spring/usb_charging.c
blob: 5556886cd64819aa0568a40e6127ddcd38412a08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
/* Copyright (c) 2012 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.
 */

/* USB charging control for spring board */

#include "adc.h"
#include "board.h"
#include "chipset.h"
#include "clock.h"
#include "console.h"
#include "gpio.h"
#include "hooks.h"
#include "host_command.h"
#include "keyboard_scan.h"
#include "lp5562.h"
#include "pmu_tpschrome.h"
#include "registers.h"
#include "smart_battery.h"
#include "stm32_adc.h"
#include "system.h"
#include "task.h"
#include "timer.h"
#include "tsu6721.h"
#include "util.h"

#define PWM_FREQUENCY 32000 /* Hz */

/* Console output macros */
#define CPUTS(outstr) cputs(CC_USBCHARGE, outstr)
#define CPRINTF(format, args...) cprintf(CC_USBCHARGE, format, ## args)

/* Devices that need VBUS power */
#define POWERED_5000_DEVICE_TYPE (TSU6721_TYPE_OTG)
#define POWERED_3300_DEVICE_TYPE (TSU6721_TYPE_JIG_UART_ON)

/* Toad cable */
#define TOAD_DEVICE_TYPE (TSU6721_TYPE_UART | TSU6721_TYPE_AUDIO3)

/* Voltage threshold of D+ for video */
#define VIDEO_ID_THRESHOLD	1300

/*
 * Mapping from PWM duty to current:
 *   Current = A + B * PWM_Duty
 */
#define PWM_MAPPING_A 2958
#define PWM_MAPPING_B (-29)

/* Map current in milli-amps to PWM duty cycle percentage */
#define MA_TO_PWM(curr) (((curr) - PWM_MAPPING_A) / PWM_MAPPING_B)

/* PWM controlled current limit */
#define I_LIMIT_100MA   MA_TO_PWM(100)
#define I_LIMIT_500MA   MA_TO_PWM(500)
#define I_LIMIT_1000MA  MA_TO_PWM(1000)
#define I_LIMIT_1500MA  MA_TO_PWM(1500)
#define I_LIMIT_2000MA  MA_TO_PWM(2000)
#define I_LIMIT_2400MA  MA_TO_PWM(2400)
#define I_LIMIT_3000MA  0

/* PWM control loop parameters */
#define PWM_CTRL_MAX_DUTY	I_LIMIT_100MA /* Minimum current */
#define PWM_CTRL_BEGIN_OFFSET	90
#define PWM_CTRL_OC_MARGIN	15
#define PWM_CTRL_OC_DETECT_TIME	(1200 * MSEC)
#define PWM_CTRL_OC_BACK_OFF	3
#define PWM_CTRL_OC_RETRY	2
#define PWM_CTRL_STEP_DOWN	3
#define PWM_CTRL_STEP_UP	5
#define PWM_CTRL_VBUS_HARD_LOW	4400
#define PWM_CTRL_VBUS_LOW	4500
#define PWM_CTRL_VBUS_HIGH	4700 /* Must be higher than 4.5V */
#define PWM_CTRL_VBUS_HIGH_500MA 4550

/* Delay before notifying kernel of device type change */
#define BATTERY_KEY_DELAY (PWM_CTRL_OC_DETECT_TIME + 400 * MSEC)

/* Delay for signals to settle */
#define DELAY_POWER_MS		20
#define DELAY_USB_DP_DN_MS	20
#define DELAY_ID_MUX_MS		30
#define CABLE_DET_POLL_MS	100
#define CABLE_DET_POLL_COUNT	6

/* Battery level thresholds for S5 boost control */
#define S5_BOOST_CTRL_LOWER_BOUND 94
#define S5_BOOST_CTRL_UPPER_BOUND 98

static int current_dev_type = TSU6721_TYPE_NONE;
static int nominal_pwm_duty;
static int current_pwm_duty;
static int user_pwm_duty = -1;

static int pending_tsu6721_reset;
static int pending_adc_watchdog_disable;
static int pending_dev_type_update;
static int pending_video_power_off;
static int restore_id_mux;

static int board_rev = 1; /* Assume new boards unless told otherwise */

static int s5_boost_ctrl;

static enum {
	LIMIT_NORMAL,
	LIMIT_AGGRESSIVE,
} current_limit_mode = LIMIT_AGGRESSIVE;

static enum {
	ADC_WATCH_NONE,
	ADC_WATCH_TOAD,
	ADC_WATCH_USB,
} current_watchdog = ADC_WATCH_NONE;

struct {
	int type;
	const char *name;
} const known_dev_types[] = {
	{TSU6721_TYPE_OTG, "OTG"},
	{TSU6721_TYPE_USB_HOST, "USB"},
	{TSU6721_TYPE_CHG12, "Type-1/2-Chg"},
	{TSU6721_TYPE_NON_STD_CHG, "Non-Std-Chg"},
	{TSU6721_TYPE_DCP, "DCP"},
	{TSU6721_TYPE_CDP, "CDP"},
	{TSU6721_TYPE_U200_CHG, "U200-Chg"},
	{TSU6721_TYPE_APPLE_CHG, "Apple-Chg"},
	{TSU6721_TYPE_JIG_UART_ON, "Video"},
	{TSU6721_TYPE_AUDIO3, "Audio-3"},
	{TSU6721_TYPE_UART, "UART"},
	{TSU6721_TYPE_VBUS_DEBOUNCED, "Power"} };

/*
 * Last time we see a power source removed. Also records the power source
 * type and PWM duty cycle at that moment.
 * Index: 0 = Unknown power source.
 *        1 = Recognized power source.
 */
static timestamp_t power_removed_time[2];
static uint32_t power_removed_type[2];
static int power_removed_pwm_duty[2];
static int oc_detect_retry[2] = {PWM_CTRL_OC_RETRY, PWM_CTRL_OC_RETRY};

/* PWM duty cycle limit based on over current event */
static int over_current_pwm_duty;

static enum ilim_config current_ilim_config = ILIM_CONFIG_MANUAL_OFF;

static const int apple_charger_type[4] = {I_LIMIT_500MA,
					  I_LIMIT_1000MA,
					  I_LIMIT_2000MA,
					  I_LIMIT_2400MA};

static int video_power_enabled;

#define NON_STD_CHARGER_REDETECT_DELAY (4 * SECOND)
static enum {
	NO_REDETECT,
	REDETECT_SCHEDULED,
	REDETECTED,
} charger_need_redetect = NO_REDETECT;
static timestamp_t charger_redetection_time;

static int get_video_power(void)
{
	return video_power_enabled;
}

static void set_video_power(int enabled)
{
	int power_good;

	pmu_enable_fet(FET_VIDEO, enabled, enabled ? &power_good : NULL);
	if (enabled && !power_good)
		pmu_enable_fet(FET_VIDEO, 0, NULL);
	video_power_enabled = enabled;
}

static void board_ilim_use_gpio(void)
{
	/* Disable counter */
	STM32_TIM_CR1(3) &= ~0x1;

	/* Disable TIM3 clock */
	STM32_RCC_APB1ENR &= ~0x2;

	/* Switch to GPIO */
	gpio_set_flags(GPIO_ILIM, GPIO_OUTPUT);
}

static void board_ilim_use_pwm(void)
{
	uint32_t val;

	/* Config alt. function (TIM3/PWM) */
	val = STM32_GPIO_CRL_OFF(GPIO_B) & ~0x000f0000;
	val |= 0x00090000;
	STM32_GPIO_CRL_OFF(GPIO_B) = val;

	/* Enable TIM3 clock */
	STM32_RCC_APB1ENR |= 0x2;

	/* Disable counter during setup */
	STM32_TIM_CR1(3) = 0x0000;

	/*
	 * CPU_CLOCK / (PSC + 1) determines how fast the counter operates.
	 * ARR determines the wave period, CCRn determines duty cycle.
	 * Thus, frequency = CPU_CLOCK / (PSC + 1) / ARR.
	 *
	 * Assuming 16MHz clock and ARR=100, PSC needed to achieve PWM_FREQUENCY
	 * is: PSC = CPU_CLOCK / PWM_FREQUENCY / ARR - 1
	 */
	STM32_TIM_PSC(3) = CPU_CLOCK / PWM_FREQUENCY / 100 - 1; /* pre-scaler */
	STM32_TIM_ARR(3) = 100;			/* auto-reload value */
	STM32_TIM_CCR1(3) = 100;		/* duty cycle */

	/* CC1 configured as output, PWM mode 1, preload enable */
	STM32_TIM_CCMR1(3) = (6 << 4) | (1 << 3);

	/* CC1 output enable, active high */
	STM32_TIM_CCER(3) = (1 << 0);

	/* Generate update event to force loading of shadow registers */
	STM32_TIM_EGR(3) |= 1;

	/* Enable auto-reload preload, start counting */
	STM32_TIM_CR1(3) |= (1 << 7) | (1 << 0);
}

void board_ilim_config(enum ilim_config config)
{
	if (config == current_ilim_config)
		return;
	current_ilim_config = config;

	switch (config) {
	case ILIM_CONFIG_MANUAL_OFF:
	case ILIM_CONFIG_MANUAL_ON:
		board_ilim_use_gpio();
		gpio_set_level(GPIO_ILIM,
			       config == ILIM_CONFIG_MANUAL_ON ? 1 : 0);
		break;
	case ILIM_CONFIG_PWM:
		board_ilim_use_pwm();
		break;
	default:
		break;
	}
}

/* Returns Apple charger current limit */
static int board_apple_charger_current(void)
{
	int vp, vn;
	int type = 0;
	int data[ADC_CH_COUNT];

	/* TODO(victoryang): Handle potential race condition. */
	tsu6721_disable_interrupts();
	tsu6721_mux(TSU6721_MUX_USB);
	/* Wait 20ms for signal to stablize */
	msleep(DELAY_USB_DP_DN_MS);
	adc_read_all_channels(data);
	vp = data[ADC_CH_USB_DP_SNS];
	vn = data[ADC_CH_USB_DN_SNS];
	tsu6721_mux(TSU6721_MUX_AUTO);
	tsu6721_enable_interrupts();
	if (vp > 1215)
		type |= 0x2;
	if (vn > 1215)
		type |= 0x1;

	return apple_charger_type[type];
}

static int hard_current_limit(int limit)
{
	/*
	 * In aggressive mode, the PWM duty cycle goes lower than the nominal
	 * cycle for PWM_CTRL_OC_MARGIN. Therefore, increase duty cycle by
	 * PWM_CTRL_OC_MARGIN avoids going over the hard limit.
	 * (Note that lower PWM cycle translates to higher current)
	 */
	if (current_limit_mode == LIMIT_AGGRESSIVE)
		return MIN(limit + PWM_CTRL_OC_MARGIN, 100);
	else
		return limit;
}

static int video_dev_type(int device_type)
{
	return (device_type & ~TSU6721_TYPE_USB_HOST) |
	       TSU6721_TYPE_JIG_UART_ON;
}

static int board_video_id_present(void)
{
	return adc_read_channel(ADC_CH_USB_DP_SNS) > VIDEO_ID_THRESHOLD;
}

static int board_poll_video_id(void)
{
	int i;
	for (i = 0; i < CABLE_DET_POLL_COUNT; ++i) {
		msleep(CABLE_DET_POLL_MS);
		if (board_video_id_present())
			return 1;
	}
	return 0;
}

static int board_probe_video(int device_type)
{
	tsu6721_disable_interrupts();
	gpio_set_level(GPIO_ID_MUX, 1);
	msleep(DELAY_ID_MUX_MS);

	if (board_poll_video_id()) {
		/* Not USB host but video */
		device_type = video_dev_type(device_type);
		return device_type;
	} else {
		if (adc_read_channel(ADC_CH_USB_VBUS_SNS) > 3500) {
			/*
			 * Either USB host or video dongle.
			 * Leave ID_MUX high so we see the change on
			 * DP_SNS if any.
			 *
			 * ADC watchdog is responsible for sensing a
			 * detach event and switch back ID_MUX.
			 */
			return device_type;
		} else {
			/* Unhandled unpowered video dongle. Ignore it. */
			gpio_set_level(GPIO_ID_MUX, 0);
			msleep(DELAY_ID_MUX_MS);
			tsu6721_enable_interrupts();
			return TSU6721_TYPE_NONE;
		}
	}
}

int board_has_high_power_ac(void)
{
	return board_get_usb_dev_type() & TSU6721_TYPE_CHG12;
}

void board_pwm_duty_cycle(int percent)
{
	if (current_ilim_config != ILIM_CONFIG_PWM)
		board_ilim_config(ILIM_CONFIG_PWM);
	if (percent < 0)
		percent = 0;
	if (percent > 100)
		percent = 100;
	STM32_TIM_CCR1(3) = (percent * STM32_TIM_ARR(3)) / 100;
	current_pwm_duty = percent;
}

void board_pwm_init_limit(void)
{
	board_pwm_duty_cycle(I_LIMIT_500MA);
}

/**
 * Returns next lower PWM duty cycle, or -1 for unchanged duty cycle.
 */
static int board_pwm_get_next_lower(void)
{
	if (current_limit_mode == LIMIT_AGGRESSIVE) {
		if (current_pwm_duty > nominal_pwm_duty -
				       PWM_CTRL_OC_MARGIN &&
		    current_pwm_duty > over_current_pwm_duty &&
		    current_pwm_duty > 0)
			return MAX(current_pwm_duty - PWM_CTRL_STEP_DOWN, 0);
		return -1;
	} else {
		if (current_pwm_duty > nominal_pwm_duty && current_pwm_duty > 0)
			return MAX(current_pwm_duty - PWM_CTRL_STEP_DOWN, 0);
		else
			return -1;
	}
}

static int board_pwm_check_vbus_high(int vbus)
{
	if (vbus > PWM_CTRL_VBUS_HIGH)
		return 1;
	if (vbus > PWM_CTRL_VBUS_HIGH_500MA && current_pwm_duty > I_LIMIT_500MA)
		return 1;
	return 0;
}

static int board_pwm_check_vbus_low(int vbus, int battery_current)
{
	if (battery_current >= 0)
		return vbus < PWM_CTRL_VBUS_LOW && current_pwm_duty < 100;
	else
		return vbus < PWM_CTRL_VBUS_HARD_LOW && current_pwm_duty < 100;
}

static void board_pwm_tweak(void)
{
	int vbus, current;
	int next;

	if (current_ilim_config != ILIM_CONFIG_PWM)
		return;

	vbus = adc_read_channel(ADC_CH_USB_VBUS_SNS);
	if (battery_current(&current))
		current = 0;

	if (user_pwm_duty >= 0) {
		if (current_pwm_duty != user_pwm_duty)
			board_pwm_duty_cycle(user_pwm_duty);
		return;
	}

	/*
	 * If VBUS voltage is too low:
	 *   - If battery is discharging, throttling more is going to draw
	 *     more current from the battery, so do nothing unless VBUS is
	 *     about to be lower than AC good threshold.
	 *   - Otherwise, throttle input current to raise VBUS voltage.
	 * If VBUS voltage is high enough, allow more current until we hit
	 * current limit target.
	 */
	if (board_pwm_check_vbus_low(vbus, current)) {
		board_pwm_duty_cycle(current_pwm_duty + PWM_CTRL_STEP_UP);
		CPRINTF("[%T PWM duty up %d%%]\n", current_pwm_duty);
	} else if (board_pwm_check_vbus_high(vbus)) {
		next = board_pwm_get_next_lower();
		if (next >= 0) {
			board_pwm_duty_cycle(next);
			CPRINTF("[%T PWM duty down %d%%]\n", current_pwm_duty);
		}
	}
}
DECLARE_HOOK(HOOK_SECOND, board_pwm_tweak, HOOK_PRIO_DEFAULT);

void board_pwm_nominal_duty_cycle(int percent)
{
	int new_percent = percent;

	new_percent += PWM_CTRL_BEGIN_OFFSET;
	new_percent = MIN(new_percent, PWM_CTRL_MAX_DUTY);

	board_pwm_duty_cycle(new_percent);
	nominal_pwm_duty = percent;
}

void usb_charge_interrupt(enum gpio_signal signal)
{
	task_wake(TASK_ID_PMU_TPS65090_CHARGER);
}

static void board_adc_watch_vbus(int high, int low)
{
	adc_enable_watchdog(STM32_AIN(5), high, low);
	task_clear_pending_irq(STM32_IRQ_ADC_1);
	task_enable_irq(STM32_IRQ_ADC_1);
}

static void board_adc_watch_toad(void)
{
	/* Watch VBUS and interrupt if voltage goes under 3V. */
	board_adc_watch_vbus(4095, 1800);
	current_watchdog = ADC_WATCH_TOAD;
}

static void board_adc_watch_usb(void)
{
	/* Watch VBUS and interrupt if voltage goes under 3V. */
	board_adc_watch_vbus(4095, 1800);
	current_watchdog = ADC_WATCH_USB;
}

static void board_adc_watchdog_interrupt(void)
{
	switch (current_watchdog) {
	case ADC_WATCH_USB:
		restore_id_mux = 1;
		/* Fall through */
	case ADC_WATCH_TOAD:
		pending_tsu6721_reset = 1;
		pending_adc_watchdog_disable = 1;
		task_disable_irq(STM32_IRQ_ADC_1);
		task_wake(TASK_ID_PMU_TPS65090_CHARGER);
		break;
	default:
		break;
	}
}
DECLARE_IRQ(STM32_IRQ_ADC_1, board_adc_watchdog_interrupt, 2);

static int usb_maybe_power_input(int dev_type)
{
	if (dev_type & TSU6721_TYPE_JIG_UART_ON)
		return 1;
	return (dev_type & TSU6721_TYPE_VBUS_DEBOUNCED) &&
	       !(dev_type & POWERED_5000_DEVICE_TYPE);
}

static int usb_has_power_input(int dev_type)
{
	return !!(usb_maybe_power_input(dev_type) &&
		 (dev_type & TSU6721_TYPE_VBUS_DEBOUNCED));
}

static int usb_need_boost(int dev_type)
{
	if (dev_type & POWERED_5000_DEVICE_TYPE)
		return 0;
	if (chipset_in_state(CHIPSET_STATE_ON | CHIPSET_STATE_SUSPEND))
		return 1;
	return (dev_type != TSU6721_TYPE_NONE);
}

static void usb_s5_manage_boost(void)
{
	int chg, cap;
	int boost = gpio_get_level(GPIO_BOOST_EN);

	if (!usb_maybe_power_input(current_dev_type)) {
		if (boost)
			gpio_set_level(GPIO_BOOST_EN, 0);
		return;
	}

	if (battery_remaining_capacity(&chg) ||
	    battery_full_charge_capacity(&cap))
		return;

	if (boost == 0 && chg * 100 <= S5_BOOST_CTRL_LOWER_BOUND * cap) {
		gpio_set_level(GPIO_BOOST_EN, 1);
		gpio_set_level(GPIO_CHARGER_EN, 1);
	} else if (boost == 1 && chg * 100 >= S5_BOOST_CTRL_UPPER_BOUND * cap) {
		gpio_set_level(GPIO_CHARGER_EN, 0);
		gpio_set_level(GPIO_BOOST_EN, 0);
	}
}

static void usb_boost_power_hook(int power_on)
{
	s5_boost_ctrl = !power_on;
	if (power_on && usb_need_boost(current_dev_type))
		gpio_set_level(GPIO_BOOST_EN, 1);
	else if (current_dev_type & TSU6721_TYPE_JIG_UART_ON)
		set_video_power(power_on);
}

static void usb_boost_pwr_on_hook(void) { usb_boost_power_hook(1); }
static void usb_boost_pwr_off_hook(void) { usb_boost_power_hook(0); }
DECLARE_HOOK(HOOK_CHIPSET_PRE_INIT, usb_boost_pwr_on_hook, HOOK_PRIO_DEFAULT);
DECLARE_HOOK(HOOK_CHIPSET_SHUTDOWN, usb_boost_pwr_off_hook, HOOK_PRIO_DEFAULT);

static int usb_charger_removed(int dev_type)
{
	if (!(current_dev_type & TSU6721_TYPE_VBUS_DEBOUNCED))
		return 0;

	/* Charger is removed */
	if (dev_type == TSU6721_TYPE_NONE)
		return 1;

	/*
	 * Device type changed from known type to unknown type. Assuming
	 * it went away and came back.
	 */
	if ((current_dev_type != TSU6721_TYPE_VBUS_DEBOUNCED) &&
	    (dev_type == TSU6721_TYPE_VBUS_DEBOUNCED))
		return 1;

	return 0;
}

/*
 * When a power source is removed, record time, power source type,
 * and PWM duty cycle. Then when we see a power source, compare type
 * and calculate time difference to determine if we have just
 * encountered an over current event.
 */
static void usb_detect_overcurrent(int dev_type)
{
	if (usb_charger_removed(dev_type)) {
		int idx = !(current_dev_type == TSU6721_TYPE_VBUS_DEBOUNCED);
		power_removed_time[idx] = get_time();
		power_removed_type[idx] = current_dev_type;
		/*
		 * TODO(victoryang): Record the maximum current seen during
		 * retry?
		 */
		power_removed_pwm_duty[idx] = current_pwm_duty;
	} else if (dev_type & TSU6721_TYPE_VBUS_DEBOUNCED) {
		int idx = !(dev_type == TSU6721_TYPE_VBUS_DEBOUNCED);
		timestamp_t now = get_time();
		now.val -= power_removed_time[idx].val;
		if (now.val >= PWM_CTRL_OC_DETECT_TIME) {
			oc_detect_retry[idx] = PWM_CTRL_OC_RETRY;
			return;
		}
		if (power_removed_type[idx] == dev_type) {
			if (oc_detect_retry[idx] > 0) {
				CPRINTF("[%T USB overcurrent: Retry (%d)]\n",
					oc_detect_retry[idx]);
				oc_detect_retry[idx]--;
				return;
			}
			over_current_pwm_duty = power_removed_pwm_duty[idx] +
						PWM_CTRL_OC_BACK_OFF;
			CPRINTF("[%T USB overcurrent: Limited to %d%%]\n",
				over_current_pwm_duty);
		}
	}
}

/*
 * Supply 5V VBUS if needed. If we toggle power output, wait for a
 * moment, and then update device type. To avoid race condition, check
 * if power requirement changes during this time.
 */
static int usb_manage_boost(int dev_type)
{
	int need_boost;
	int retry_limit = 3;

	do {
		if (retry_limit-- <= 0)
			break;

		need_boost = usb_need_boost(dev_type);
		if (need_boost != gpio_get_level(GPIO_BOOST_EN)) {
			gpio_set_level(GPIO_BOOST_EN, need_boost);
			msleep(DELAY_POWER_MS);
			dev_type = tsu6721_get_device_type();
			if (gpio_get_level(GPIO_ID_MUX))
				dev_type = video_dev_type(dev_type);
		}
	} while (need_boost == !usb_need_boost(dev_type));

	return dev_type;
}

/* Updates ILIM current limit according to device type. */
static void usb_update_ilim(int dev_type)
{
	if (usb_maybe_power_input(dev_type)) {
		/* Limit USB port current. 500mA for not listed types. */
		int current_limit = I_LIMIT_500MA;
		if (dev_type & TSU6721_TYPE_CHG12)
			current_limit = I_LIMIT_3000MA;
		else if (dev_type & TSU6721_TYPE_APPLE_CHG)
			current_limit = board_apple_charger_current();
		else if (dev_type & TSU6721_TYPE_CDP)
			current_limit = I_LIMIT_1500MA;
		else if (dev_type & TSU6721_TYPE_DCP)
			current_limit = hard_current_limit(I_LIMIT_1500MA);
		else if (dev_type & TSU6721_TYPE_JIG_UART_ON)
			current_limit = hard_current_limit(I_LIMIT_2000MA);
		else if (dev_type & TOAD_DEVICE_TYPE)
			current_limit = hard_current_limit(I_LIMIT_500MA);
		else if (dev_type == TSU6721_TYPE_VBUS_DEBOUNCED)
			current_limit = hard_current_limit(I_LIMIT_100MA);

		board_pwm_nominal_duty_cycle(current_limit);
	} else {
		board_ilim_config(ILIM_CONFIG_MANUAL_ON);
	}
}

static void usb_log_dev_type(int dev_type)
{
	int i = sizeof(known_dev_types) / sizeof(known_dev_types[0]);

	CPRINTF("[%T USB: 0x%06x", dev_type);
	for (--i; i >= 0; --i)
		if (dev_type & known_dev_types[i].type)
			CPRINTF(" %s", known_dev_types[i].name);
	CPRINTF("]\n");
}

static void send_battery_key_deferred(void)
{
	keyboard_send_battery_key();
}
DECLARE_DEFERRED(send_battery_key_deferred);

static void usb_release_vac(void)
{
	gpio_set_level(GPIO_PMIC_RESET, 0);
	CPRINTF("[%T Stop pulling VAC]\n");
}
DECLARE_DEFERRED(usb_release_vac);

static void usb_pull_vac(void)
{
	gpio_set_level(GPIO_PMIC_RESET, 1);
	hook_call_deferred(usb_release_vac, 550 * MSEC);
	CPRINTF("[%T Pulling VAC low]\n");
}
DECLARE_DEFERRED(usb_pull_vac);

static void notify_dev_type_change(int dev_type)
{
	int org_type = current_dev_type;

	current_dev_type = dev_type;
	usb_log_dev_type(dev_type);
	if (usb_has_power_input(org_type) !=
	    usb_has_power_input(dev_type))
		hook_notify(HOOK_AC_CHANGE);
	hook_call_deferred(send_battery_key_deferred, BATTERY_KEY_DELAY);

	/*
	 * If the charger is surely removed (not coming back within
	 * BATTERY_KEY_DELAY), pull down VAC.
	 */
	if (board_rev) {
		if (!(dev_type & TSU6721_TYPE_VBUS_DEBOUNCED))
			hook_call_deferred(usb_pull_vac, BATTERY_KEY_DELAY);
		else
			hook_call_deferred(usb_pull_vac, -1);
	}
}

static int usb_want_redetect(int dev_type)
{
	if (chipset_in_state(CHIPSET_STATE_ANY_OFF) &&
	    dev_type & TSU6721_TYPE_USB_HOST)
		return 1;
	return (dev_type & TSU6721_TYPE_NON_STD_CHG) ||
	       (dev_type == TSU6721_TYPE_VBUS_DEBOUNCED);
}

static void usb_device_change(int dev_type)
{

	if (current_dev_type == dev_type)
		return;

	over_current_pwm_duty = 0;

	/*
	 * Video output is recognized incorrectly as USB host. When we see
	 * USB host, probe for video output.
	 */
	if (dev_type & TSU6721_TYPE_USB_HOST)
		dev_type = board_probe_video(dev_type);

	usb_detect_overcurrent(dev_type);

	dev_type = usb_manage_boost(dev_type);

	/* Supply 3.3V VBUS if needed. */
	if (dev_type & POWERED_3300_DEVICE_TYPE)
		set_video_power(1);

	usb_update_ilim(dev_type);

	if ((dev_type & TOAD_DEVICE_TYPE) &&
	    (dev_type & TSU6721_TYPE_VBUS_DEBOUNCED))
		board_adc_watch_toad();
	else if (dev_type & TSU6721_TYPE_USB_HOST)
		board_adc_watch_usb();

	if (dev_type != current_dev_type) {
		if (usb_want_redetect(dev_type) &&
		    charger_need_redetect == NO_REDETECT) {
			/* Schedule redetection */
			charger_need_redetect = REDETECT_SCHEDULED;
			charger_redetection_time = get_time();
			charger_redetection_time.val +=
				NON_STD_CHARGER_REDETECT_DELAY;
		} else if (!usb_want_redetect(dev_type)) {
			/* Disarm redetection timer. */
			charger_need_redetect = NO_REDETECT;
		}
		notify_dev_type_change(dev_type);
	}

	if (dev_type)
		disable_sleep(SLEEP_MASK_USB_PWR);
	else
		enable_sleep(SLEEP_MASK_USB_PWR);
}

static void board_usb_detach_video(void)
{
	if (!(current_dev_type & TSU6721_TYPE_JIG_UART_ON))
		return;
	pending_video_power_off = 1;
	restore_id_mux = 1;
	pending_tsu6721_reset = 1;
	task_wake(TASK_ID_PMU_TPS65090_CHARGER);
}
DECLARE_HOOK(HOOK_CHIPSET_SUSPEND, board_usb_detach_video, HOOK_PRIO_DEFAULT);
DECLARE_HOOK(HOOK_CHIPSET_SHUTDOWN, board_usb_detach_video, HOOK_PRIO_DEFAULT);

static void board_usb_monitor_detach(void)
{
	int vbus;

	if (!(current_dev_type & TSU6721_TYPE_JIG_UART_ON))
		return;

	if (!board_video_id_present()) {
		board_usb_detach_video();
		return;
	}

	/* Check if there is external power */
	vbus = adc_read_channel(ADC_CH_USB_VBUS_SNS);
	if (get_video_power() && vbus > 4000) {
		set_video_power(0);
		notify_dev_type_change(current_dev_type |
				       TSU6721_TYPE_VBUS_DEBOUNCED);
	} else if  (!get_video_power() && vbus <= 4000) {
		board_pwm_duty_cycle(100);
		set_video_power(1);
		notify_dev_type_change(current_dev_type &
				       ~TSU6721_TYPE_VBUS_DEBOUNCED);
	}
}
DECLARE_HOOK(HOOK_SECOND, board_usb_monitor_detach, HOOK_PRIO_DEFAULT);

static void board_usb_monitor_cable_det(void)
{
	if (!(current_dev_type & TSU6721_TYPE_USB_HOST))
		return;

	if (board_video_id_present())
		board_adc_watchdog_interrupt();
}
DECLARE_HOOK(HOOK_SECOND, board_usb_monitor_cable_det, HOOK_PRIO_DEFAULT);

static void board_usb_charger_redetect(void)
{
	if (charger_need_redetect != REDETECT_SCHEDULED)
		return;

	if (timestamp_expired(charger_redetection_time, NULL)) {
		CPRINTF("[%T USB Redetecting]\n");
		/*
		 * TSU6721 doesn't update device type if power or ID pin
		 * is present. Therefore, if the device type is the same,
		 * we need to reset TSU6721 to force a redetection.
		 */
		if (tsu6721_get_device_type() == current_dev_type)
			pending_tsu6721_reset = 1;
		else
			pending_dev_type_update = 1;
		if (gpio_get_level(GPIO_ID_MUX))
			restore_id_mux = 1;
		charger_need_redetect = REDETECTED;
		task_wake(TASK_ID_PMU_TPS65090_CHARGER);
	}
}
DECLARE_HOOK(HOOK_SECOND, board_usb_charger_redetect, HOOK_PRIO_DEFAULT);

void board_usb_charge_update(int force_update)
{
	int int_val = 0;

	if (restore_id_mux) {
		gpio_set_level(GPIO_ID_MUX, 0);
		msleep(DELAY_ID_MUX_MS);
		restore_id_mux = 0;
	}

	if (pending_adc_watchdog_disable) {
		current_watchdog = ADC_WATCH_NONE;
		adc_disable_watchdog();
		pending_adc_watchdog_disable = 0;
	}

	if (pending_video_power_off) {
		set_video_power(0);
		pending_video_power_off = 0;
	}

	if (pending_tsu6721_reset) {
		tsu6721_reset();
		force_update = 1;
		pending_tsu6721_reset = 0;
	}

	if (pending_dev_type_update) {
		force_update = 1;
		pending_dev_type_update = 0;
	}

	if (s5_boost_ctrl)
		usb_s5_manage_boost();

	/*
	 * Check device type except when:
	 *   1. Current device type is non-standard charger or undetermined
	 *      charger type. This is handled by charger re-detection.
	 *   2. ID_MUX=1. This is handled by ADC watchdog.
	 */
	if (current_dev_type != TSU6721_TYPE_VBUS_DEBOUNCED &&
	    !(current_dev_type & TSU6721_TYPE_NON_STD_CHG) &&
	    gpio_get_level(GPIO_ID_MUX) == 0)
		force_update |= (tsu6721_get_device_type() != current_dev_type);

	if (!force_update)
		int_val = tsu6721_get_interrupts();

	if (int_val & TSU6721_INT_DETACH)
		usb_device_change(TSU6721_TYPE_NONE);
	else if (int_val || force_update)
		usb_device_change(tsu6721_get_device_type());
}

int board_get_usb_dev_type(void)
{
	return current_dev_type;
}

int board_get_usb_current_limit(void)
{
	/* Approximate value by PWM duty cycle */
	return PWM_MAPPING_A + PWM_MAPPING_B * current_pwm_duty;
}

int board_get_ac(void)
{
	static int last_vbus;
	int vbus, vbus_good;

	if (!usb_maybe_power_input(current_dev_type))
		return 0;

	/*
	 * UVLO is 4.1V. We consider AC bad when its voltage drops below 4.2V
	 * for two consecutive samples. This is to give PWM a chance to bring
	 * voltage up.
	 */
	vbus = adc_read_channel(ADC_CH_USB_VBUS_SNS);
	vbus_good = (vbus >= 4200 || last_vbus >= 4200);
	last_vbus = vbus;

	return vbus_good;
}

/*
 * Console commands for debugging.
 * TODO(victoryang): Remove after charging control is done.
 */
static int command_ilim(int argc, char **argv)
{
	char *e;
	int percent;

	if (argc >= 2) {
		if (strcasecmp(argv[1], "on") == 0)
			board_ilim_config(ILIM_CONFIG_MANUAL_ON);
		else if (strcasecmp(argv[1], "off") == 0)
			board_ilim_config(ILIM_CONFIG_MANUAL_OFF);
		else {
			percent = strtoi(argv[1], &e, 0);
			if (*e)
				return EC_ERROR_PARAM1;
			board_pwm_duty_cycle(percent);
		}
	}

	if (current_ilim_config == ILIM_CONFIG_MANUAL_ON)
		ccprintf("ILIM is GPIO high\n");
	else if (current_ilim_config == ILIM_CONFIG_MANUAL_OFF)
		ccprintf("ILIM is GPIO low\n");
	else
		ccprintf("ILIM is PWM duty cycle %d%%\n", STM32_TIM_CCR1(3));

	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(ilim, command_ilim,
		"[percent | on | off]",
		"Set or show ILIM duty cycle/GPIO value",
		NULL);

static int command_batdebug(int argc, char **argv)
{
	int val;
	ccprintf("VBUS = %d mV\n", adc_read_channel(ADC_CH_USB_VBUS_SNS));
	ccprintf("VAC = %d mV\n", pmu_adc_read(ADC_VAC, ADC_FLAG_KEEP_ON)
				  * 17000 / 1024);
	ccprintf("IAC = %d mA\n", pmu_adc_read(ADC_IAC, ADC_FLAG_KEEP_ON)
				  * (1000 / R_INPUT_MOHM) * 33 / 1024);
	ccprintf("VBAT = %d mV\n", pmu_adc_read(ADC_VBAT, ADC_FLAG_KEEP_ON)
				  * 17000 / 1024);
	ccprintf("IBAT = %d mA\n", pmu_adc_read(ADC_IBAT, 0)
				  * (1000 / R_BATTERY_MOHM) * 40 / 1024);
	ccprintf("PWM = %d%%\n", STM32_TIM_CCR1(3));
	battery_current(&val);
	ccprintf("Battery Current = %d mA\n", val);
	battery_voltage(&val);
	ccprintf("Battery Voltage= %d mV\n", val);
	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(batdebug, command_batdebug,
			NULL, NULL, NULL);

static int command_current_limit_mode(int argc, char **argv)
{
	if (1 == argc) {
		if (current_limit_mode == LIMIT_NORMAL)
			ccprintf("Normal mode\n");
		else
			ccprintf("Aggressive mode\n");
		return EC_SUCCESS;
	} else if (2 == argc) {
		if (!strcasecmp(argv[1], "normal"))
			current_limit_mode = LIMIT_NORMAL;
		else if (!strcasecmp(argv[1], "aggressive"))
			current_limit_mode = LIMIT_AGGRESSIVE;
		else
			return EC_ERROR_INVAL;
		return EC_SUCCESS;
	}
	return EC_ERROR_INVAL;
}
DECLARE_CONSOLE_COMMAND(limitmode, command_current_limit_mode,
			"[normal | aggressive]",
			"Set current limit mode",
			NULL);

/*****************************************************************************/
/* Host commands */

static int ext_power_command_current_limit(struct host_cmd_handler_args *args)
{
	const struct ec_params_ext_power_current_limit *p = args->params;

	if (system_is_locked())
		return EC_RES_ACCESS_DENIED;

	user_pwm_duty = ((int)(p->limit) - PWM_MAPPING_A) / PWM_MAPPING_B;

	return EC_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_EXT_POWER_CURRENT_LIMIT,
		     ext_power_command_current_limit,
		     EC_VER_MASK(0));

static int ext_power_command_hack_board_rev(struct host_cmd_handler_args *args)
{
	const struct ec_params_hib_delay *p = args->params;

	if (p->delay_secs)
		board_rev = 1;
	else
		board_rev = 0;

	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_SET_HIB_DELAY,
		     ext_power_command_hack_board_rev,
		     EC_VER_MASK(0));