-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathboard.c
1365 lines (1159 loc) · 34.8 KB
/
board.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 2017 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.
*/
/*
* Microchip Evaluation Board(EVB) with
* MEC1701H 144-pin processor card.
* EVB connected to Intel SKL RVP3 configured
* for eSPI with Kabylake silicon.
*/
#include "adc.h"
#include "adc_chip.h"
#include "als.h"
#include "bd99992gw.h"
#include "battery.h"
#include "button.h"
#include "charge_state.h"
#include "charger.h"
#include "chipset.h"
#include "console.h"
#include "cypress5525.h"
#include "diagnostics.h"
#include "driver/als_cm32183.h"
#include "driver/accel_kionix.h"
#include "driver/accel_kx022.h"
#include "driver/accelgyro_bmi_common.h"
#include "driver/charger/isl9241.h"
#include "driver/tcpm/tcpci.h"
#include "extpower.h"
#include "fan.h"
#include "gpio_chip.h"
#include "gpio.h"
#include "hooks.h"
#include "host_command.h"
#include "i2c.h"
#include "i2c_slave.h"
#include "espi.h"
#include "lpc_chip.h"
#include "lpc.h"
#include "keyboard_scan.h"
#include "lid_switch.h"
#include "link_defs.h"
#include "math_util.h"
#include "motion_sense.h"
#include "motion_lid.h"
#include "pi3usb9281.h"
#include "peci.h"
#include "peci_customization.h"
#include "power.h"
#include "power_button.h"
#include "pwm.h"
#include "pwm_chip.h"
#include "spi.h"
#include "spi_chip.h"
#include "spi_flash.h"
#include "switch.h"
#include "system.h"
#include "task.h"
#include "temp_sensor.h"
#include "driver/temp_sensor/f75303.h"
#include "driver/temp_sensor/f75397.h"
#include "timer.h"
#include "uart.h"
#include "usb_charge.h"
#include "usb_mux.h"
#include "usb_pd.h"
#include "usb_pd_tcpm.h"
#include "util.h"
#include "espi.h"
#include "battery_smart.h"
#include "keyboard_scan.h"
#include "keyboard_8042.h"
#include "keyboard_8042_sharedlib.h"
#include "host_command_customization.h"
#include "flash_storage.h"
/* Console output macros */
#define CPUTS(outstr) cputs(CC_LPC, outstr)
#define CPRINTS(format, args...) cprints(CC_USBCHARGE, format, ## args)
#define CPRINTF(format, args...) cprintf(CC_USBCHARGE, format, ## args)
#ifdef CONFIG_BOARD_PRE_INIT
/*
* Used to enable JTAG debug during development.
* NOTE: If ARM Serial Wire Viewer not used then SWV pin can be
* be disabled and used for another purpose. Change mode to
* MCHP_JTAG_MODE_SWD.
* For low power idle testing enable GPIO060 as function 2(48MHZ_OUT)
* to check PLL is turning off in heavy sleep. Note, do not put GPIO060
* in gpio.inc
* GPIO060 is port 1 bit[16].
*/
void board_config_pre_init(void)
{
#ifdef CONFIG_CHIPSET_DEBUG
MCHP_EC_JTAG_EN = MCHP_JTAG_ENABLE + MCHP_JTAG_MODE_SWD_SWV;
#endif
#if defined(CONFIG_LOW_POWER_IDLE) && defined(CONFIG_MCHP_48MHZ_OUT)
gpio_set_alternate_function(1, 0x10000, 2);
#endif
/* Disable BGPO function */
MCHP_WEEK_TIMER_BGPO_POWER &= ~(BIT(0) | BIT(1) | BIT(2));
/* Make sure BPGO reset is RESET_SYS */
MCHP_WEEK_TIMER_BGPO_RESET &= ~(BIT(0) | BIT(1) | BIT(2));
}
#endif /* #ifdef CONFIG_BOARD_PRE_INIT */
/* Power signals list. Must match order of enum power_signal. */
const struct power_signal_info power_signal_list[] = {
[X86_SLP_S3_DEASSERTED] = {
SLP_S3_SIGNAL_L,
POWER_SIGNAL_ACTIVE_HIGH,
"SLP_S3_DEASSERTED",
},
[X86_SLP_S4_DEASSERTED] = {
SLP_S4_SIGNAL_L,
POWER_SIGNAL_ACTIVE_HIGH,
"SLP_S4_DEASSERTED",
},
[X86_SLP_S5_DEASSERTED] = {
SLP_S5_SIGNAL_L,
POWER_SIGNAL_ACTIVE_HIGH,
"SLP_S5_DEASSERTED",
},
[X86_SLP_SUS_DEASSERTED] = {
GPIO_PCH_SLP_SUS_L,
POWER_SIGNAL_ACTIVE_HIGH,
"SLP_SUS_DEASSERTED",
},
[X86_PWR_3V5V_PG] = {
GPIO_PWR_3V5V_PG,
POWER_SIGNAL_ACTIVE_HIGH,
"PWR_3V5V_PG",
},
[X86_VCCIN_AUX_VR_PG] = {
GPIO_VCCIN_AUX_VR_PG,
POWER_SIGNAL_ACTIVE_HIGH,
"VCCIN_AUX_VR_PG",
},
[X86_VR_PWRGD] = {
GPIO_VR_PWRGD,
POWER_SIGNAL_ACTIVE_HIGH,
"VR_PWRGD",
}
};
BUILD_ASSERT(ARRAY_SIZE(power_signal_list) == POWER_SIGNAL_COUNT);
const struct pwm_t pwm_channels[] = {
[PWM_CH_FAN] = {
.channel = 0,
.flags = PWM_CONFIG_OPEN_DRAIN,
},
[PWM_CH_KBL] = {
.channel = 4,
.flags = PWM_CONFIG_DSLEEP | PWM_CONFIG_ALT_CLOCK,
},
[PWM_CH_DB0_LED_RED] = {
.channel = 5,
.flags = PWM_CONFIG_DSLEEP,
},
[PWM_CH_DB0_LED_GREEN] = {
.channel = 6,
.flags = PWM_CONFIG_DSLEEP,
},
[PWM_CH_DB0_LED_BLUE] = {
.channel = 7,
.flags = PWM_CONFIG_DSLEEP,
},
[PWM_CH_DB1_LED_RED] = {
.channel = 1,
.flags = PWM_CONFIG_DSLEEP,
},
[PWM_CH_DB1_LED_GREEN] = {
.channel = 3,
.flags = PWM_CONFIG_DSLEEP,
},
[PWM_CH_DB1_LED_BLUE] = {
.channel = 8,
.flags = PWM_CONFIG_DSLEEP,
},
[PWM_CH_FPR_LED_RED] = {
.channel = (MCHP_PWM_ID_MAX+0),
.flags = PWM_CONFIG_DSLEEP,
},
[PWM_CH_FPR_LED_GREEN] = {
.channel = (MCHP_PWM_ID_MAX+1),
.flags = PWM_CONFIG_DSLEEP,
},
[PWM_CH_FPR_LED_BLUE] = {
.channel = (MCHP_PWM_ID_MAX+2),
.flags = PWM_CONFIG_DSLEEP,
},
};
BUILD_ASSERT(ARRAY_SIZE(pwm_channels) == PWM_CH_COUNT);
void reconfigure_kbbl_pwm_frquency(void)
{
int active_low = pwm_channels[PWM_CH_KBL].flags & PWM_CONFIG_ACTIVE_LOW;
int clock_low = pwm_channels[PWM_CH_KBL].flags & PWM_CONFIG_ALT_CLOCK;
pwm_slp_en(pwm_channels[PWM_CH_KBL].channel, 0);
MCHP_PWM_CFG(pwm_channels[PWM_CH_KBL].channel) = (3 << 3) | /* Pre-divider = 4 */
(active_low ? BIT(2) : 0) |
(clock_low ? BIT(1) : 0);
pwm_set_duty(PWM_CH_KBL, 0);
CPRINTS("reconfigure kbbl complete.");
}
#ifdef HAS_TASK_PDCMD
/* Exchange status with PD MCU. */
static void pd_mcu_interrupt(enum gpio_signal signal)
{
/* Exchange status with PD MCU to determine interrupt cause */
host_command_pd_send_status(0);
}
#endif
#include "gpio_list.h"
/* ADC channels
* name, factor multiplier, factor divider, shift, channel
*/
const struct adc_t adc_channels[] = {
[ADC_I_ADP] = {"I_ADP", 3300, 4096, 0, 0},
[ADC_I_SYS] = {"I_SYS", 3300, 4096, 0, 1},
[ADC_VCIN1_BATT_TEMP] = {"BATT_PRESENT", 3300, 4096, 0, 2},
[ADC_TP_BOARD_ID] = {"TP_BID", 3300, 4096, 0, 3},
[ADC_AD_BID] = {"AD_BID", 3300, 4096, 0, 4},
[ADC_AUDIO_BOARD_ID] = {"AUDIO_BID", 3300, 4096, 0, 5},
};
BUILD_ASSERT(ARRAY_SIZE(adc_channels) == ADC_CH_COUNT);
/*
* MCHP EVB connected to KBL RVP3
*/
const struct i2c_port_t i2c_ports[] = {
{"pch", MCHP_I2C_PORT0, 400, GPIO_I2C_0_SDA, GPIO_I2C_0_SCL},
{"batt", MCHP_I2C_PORT1, 100, GPIO_I2C_1_SDA, GPIO_I2C_1_SCL},
{"touchpd", MCHP_I2C_PORT2, 100, GPIO_I2C_2_SDA, GPIO_I2C_2_SCL},
{"sensors", MCHP_I2C_PORT3, 100, GPIO_I2C_3_SDA, GPIO_I2C_3_SCL},
{"als", MCHP_I2C_PORT4, 100, GPIO_I2C_4_SDA, GPIO_I2C_4_SCL},
{"pd1", MCHP_I2C_PORT6, 400, GPIO_I2C_6_SDA, GPIO_I2C_6_SCL},
{"pd2", MCHP_I2C_PORT7, 400, GPIO_I2C_7_SDA, GPIO_I2C_7_SCL},
};
const unsigned int i2c_ports_used = ARRAY_SIZE(i2c_ports);
struct als_t als[] = {
{"CAPELLA", cm32183_init, cm32183_read_lux, 32},
};
BUILD_ASSERT(ARRAY_SIZE(als) == ALS_COUNT);
/*
* Map ports to controller.
* Ports may map to the same controller.
*/
const uint16_t i2c_port_to_ctrl[I2C_PORT_COUNT] = {
(MCHP_I2C_CTRL0 << 8) + MCHP_I2C_PORT6,
(MCHP_I2C_CTRL2 << 8) + MCHP_I2C_PORT7,
(MCHP_I2C_CTRL1 << 8) + MCHP_I2C_PORT1,
(MCHP_I2C_CTRL1 << 8) + MCHP_I2C_PORT3,
(MCHP_I2C_CTRL1 << 8) + MCHP_I2C_PORT4,
(MCHP_I2C_CTRL4 << 8) + MCHP_I2C_PORT2,
(MCHP_I2C_CTRL3 << 8) + MCHP_I2C_PORT0,
};
/*
* default to I2C0 because callers may not check
* return value if we returned an error code.
*/
int board_i2c_p2c(int port)
{
int i;
for (i = 0; i < I2C_PORT_COUNT; i++)
if ((i2c_port_to_ctrl[i] & 0xFF) == port)
return (int)(i2c_port_to_ctrl[i] >> 8);
return -1;
}
const struct i2c_slv_port_t i2c_slv_ports[] = {
/* Actually, we are checking the controller instead of port */
{"pch", MCHP_I2C_CTRL3, 0x50}
};
const unsigned int i2c_slvs_used = ARRAY_SIZE(i2c_slv_ports);
/* SPI devices */
const struct spi_device_t spi_devices[] = {
{ QMSPI0_PORT, 4, GPIO_QMSPI_CS0},
#if defined(CONFIG_SPI_ACCEL_PORT)
{ GPSPI0_PORT, 2, GPIO_SPI0_CS0 },
#endif
};
const unsigned int spi_devices_used = ARRAY_SIZE(spi_devices);
const enum gpio_signal hibernate_wake_pins[] = {
GPIO_LID_OPEN,
GPIO_POWER_BUTTON_L,
GPIO_AC_PRESENT,
GPIO_ON_OFF_BTN_L,
GPIO_CAM_SW,
GPIO_MIC_SW,
};
const int hibernate_wake_pins_used = ARRAY_SIZE(hibernate_wake_pins);
/*
* Deep sleep support, called by chip level.
*/
#if defined(CONFIG_LOW_POWER_IDLE) && defined(CONFIG_BOARD_DEEP_SLEEP)
/*
* Perform any board level prepare for sleep actions.
* For example, disabling pin/pads to further reduce
* current during sleep.
*/
void board_prepare_for_deep_sleep(void)
{
#if defined(CONFIG_GPIO_POWER_DOWN) && \
defined(CONFIG_MCHP_DEEP_SLP_GPIO_PWR_DOWN)
gpio_power_down_module(MODULE_SPI_FLASH);
gpio_power_down_module(MODULE_SPI_MASTER);
gpio_power_down_module(MODULE_I2C);
/* powering down keyscan is causing an issue with keyscan task
* probably due to spurious interrupts on keyscan pins.
* gpio_config_module(MODULE_KEYBOARD_SCAN, 0);
*/
#ifndef CONFIG_POWER_S0IX
gpio_power_down_module(MODULE_LPC);
#endif
#endif
}
/*
* Perform any board level resume from sleep actions.
* For example, re-enabling pins powered off in
* board_prepare_for_deep_sleep().
*/
void board_resume_from_deep_sleep(void)
{
#if defined(CONFIG_GPIO_POWER_DOWN) && \
defined(CONFIG_MCHP_DEEP_SLP_GPIO_PWR_DOWN)
#ifndef CONFIG_POWER_S0IX
gpio_config_module(MODULE_LPC, 1);
#endif
/* gpio_config_module(MODULE_KEYBOARD_SCAN, 1); */
gpio_config_module(MODULE_SPI_FLASH, 1);
gpio_config_module(MODULE_SPI_MASTER, 1);
gpio_config_module(MODULE_I2C, 1);
#endif
}
#endif
/**
* Reset PD MCU
*/
void board_reset_pd_mcu(void)
{
}
void spi_mux_control(int enable)
{
if (enable) {
/* Disable LED drv */
gpio_set_level(GPIO_TYPEC_G_DRV2_EN, 0);
/* Set GPIO56 as SPI for access SPI ROM */
gpio_set_alternate_function(1, 0x4000, 2);
} else {
/* Enable LED drv */
gpio_set_level(GPIO_TYPEC_G_DRV2_EN, 1);
/* Set GPIO56 as SPI for access SPI ROM */
gpio_set_alternate_function(1, 0x4000, 1);
}
}
/**
* Check the plug-in AC then power on system setting.
*/
bool ac_poweron_check(void)
{
return flash_storage_get(FLASH_FLAGS_ACPOWERON);
}
int poweron_reason_acin(void)
{
return extpower_is_present() && ac_poweron_check();
}
static int power_button_pressed_on_boot;
int poweron_reason_powerbtn(void)
{
return power_button_pressed_on_boot;
}
static void vci_init(void)
{
if (MCHP_VCI_NEGEDGE_DETECT & (BIT(0) | BIT(1))) {
MCHP_VCI_NEGEDGE_DETECT = BIT(0) | BIT(1);
MCHP_VCI_POSEDGE_DETECT = BIT(0) | BIT(1);
power_button_pressed_on_boot = 1;
}
/**
* Switch VCI control from VCI_OUT to GPIO Pin Control
* These have to be done in sequence to prevent glitching
* the output pin
*/
MCHP_VCI_REGISTER |= MCHP_VCI_REGISTER_FW_CNTRL;
MCHP_VCI_REGISTER |= MCHP_VCI_REGISTER_FW_EXT;
/**
* only enable input for fp, powerbutton for now
* enable BIT 2 for chassis open
*/
MCHP_VCI_INPUT_ENABLE = BIT(0) | BIT(1);
MCHP_VCI_BUFFER_EN = BIT(0) | BIT(1) | BIT(2);
}
DECLARE_HOOK(HOOK_INIT, vci_init, HOOK_PRIO_FIRST);
/**
* We should really really use mchp/system.c hibernate function.
* however for now the EE design does not allow us to keep the EC on
* without also keeping on the 5v3v ALW supplies, so we just wack
* power to ourselves.
*/
static void board_power_off_deferred(void)
{
int i;
/* Turn off BGATE and NGATE for power saving */
charger_psys_enable(0);
charge_gate_onoff(0);
MCHP_VBAT_RAM(MCHP_IMAGETYPE_IDX) |= 0x80;
/* Disable interrupts */
interrupt_disable();
for (i = 0; i < MCHP_IRQ_MAX; ++i) {
task_disable_irq(i);
task_clear_pending_irq(i);
}
MCHP_VCI_NEGEDGE_DETECT = BIT(0) | BIT(1);
MCHP_VCI_POSEDGE_DETECT = BIT(0) | BIT(1);
gpio_set_level(GPIO_VS_ON, 0);
MCHP_VCI_REGISTER &= ~(MCHP_VCI_REGISTER_FW_CNTRL + MCHP_VCI_REGISTER_FW_EXT);
/* Wait for power rails to die */
while (1)
;
}
DECLARE_DEFERRED(board_power_off_deferred);
void board_power_off(void)
{
CPRINTS("Shutting down system in 30 seconds!");
hook_call_deferred(&board_power_off_deferred_data, 30000 * MSEC);
}
void cancel_board_power_off(void)
{
CPRINTS("Cancel shutdown");
hook_call_deferred(&board_power_off_deferred_data, -1);
}
static int cmd_ecoff(int argc, char **argv)
{
board_power_off_deferred();
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(ecoff, cmd_ecoff,
"ecoff",
"hard power off system now");
/**
* Notify PCH of the AC presence.
*/
static void board_extpower(void)
{
gpio_set_level(GPIO_AC_PRESENT_OUT, extpower_is_present());
if (chipset_in_state(CHIPSET_STATE_HARD_OFF)) {
/* if AC disconnected, need to power_off EC_ON */
if (!extpower_is_present())
board_power_off();
else
cancel_board_power_off();
}
if (chipset_in_state(CHIPSET_STATE_ANY_OFF) &&
extpower_is_present() && ac_boot_status()) {
CPRINTS("Power on from boot on AC present");
power_button_simulate_press();
}
}
DECLARE_HOOK(HOOK_AC_CHANGE, board_extpower, HOOK_PRIO_DEFAULT);
int ac_boot_status(void)
{
return (*host_get_customer_memmap(0x48) & BIT(0)) ? true : false;
}
/* counter for chassis open while ec no power, only rtc power */
static uint8_t chassis_vtr_open_count;
/* counter for chassis open while ec has power */
static uint8_t chassis_open_count;
/* counter for chassis press while ec has power, clear when enter S0 */
static uint8_t chassis_press_counter;
static void check_chassis_open(int init)
{
if (MCHP_VCI_NEGEDGE_DETECT & BIT(2)) {
MCHP_VCI_POSEDGE_DETECT = BIT(2);
MCHP_VCI_NEGEDGE_DETECT = BIT(2);
system_set_bbram(STSTEM_BBRAM_IDX_CHASSIS_WAS_OPEN, 1);
if (init) {
system_get_bbram(STSTEM_BBRAM_IDX_CHASSIS_VTR_OPEN,
&chassis_vtr_open_count);
if (chassis_vtr_open_count < 0xFF)
system_set_bbram(STSTEM_BBRAM_IDX_CHASSIS_VTR_OPEN,
++chassis_vtr_open_count);
} else {
system_get_bbram(SYSTEM_BBRAM_IDX_CHASSIS_TOTAL,
&chassis_open_count);
if (chassis_open_count < 0xFF)
system_set_bbram(SYSTEM_BBRAM_IDX_CHASSIS_TOTAL,
++chassis_open_count);
}
/* counter for chasis pin */
if (chipset_in_state(CHIPSET_STATE_ANY_OFF))
if (chassis_press_counter < 0xFF)
chassis_press_counter++;
CPRINTF("Chassis was open");
}
}
void charge_gate_onoff(uint8_t enable)
{
int control0 = 0x0000;
int control1 = 0x0000;
if (i2c_read16(I2C_PORT_CHARGER, ISL9241_ADDR_FLAGS,
ISL9241_REG_CONTROL0, &control0)) {
CPRINTS("read gate control1 fail");
}
if (i2c_read16(I2C_PORT_CHARGER, ISL9241_ADDR_FLAGS,
ISL9241_REG_CONTROL1, &control1)) {
CPRINTS("read gate control1 fail");
}
if (enable) {
control0 &= ~ISL9241_CONTROL0_NGATE;
control1 &= ~ISL9241_CONTROL1_BGATE;
CPRINTS("B&N Gate off");
} else {
control0 |= ISL9241_CONTROL0_NGATE;
control1 |= ISL9241_CONTROL1_BGATE;
CPRINTS("B&N Gate on");
}
if (i2c_write16(I2C_PORT_CHARGER, ISL9241_ADDR_FLAGS,
ISL9241_REG_CONTROL0, control0)) {
CPRINTS("Update gate control0 fail");
}
if (i2c_write16(I2C_PORT_CHARGER, ISL9241_ADDR_FLAGS,
ISL9241_REG_CONTROL1, control1)) {
CPRINTS("Update gate control1 fail");
}
}
void charger_psys_enable(uint8_t enable)
{
int control1 = 0x0000;
int control4 = 0x0000;
int data = 0x0000;
if (i2c_read16(I2C_PORT_CHARGER, ISL9241_ADDR_FLAGS,
ISL9241_REG_CONTROL1, &control1)) {
CPRINTS("read psys control1 fail");
}
if (enable) {
control1 &= ~ISL9241_CONTROL1_IMON;
control1 |= ISL9241_CONTROL1_PSYS;
control4 &= ~ISL9241_CONTROL4_GP_COMPARATOR;
data = 0x0B00; /* Set ACOK reference to 4.544V */
CPRINTS("Power saving disable");
} else {
control1 |= ISL9241_CONTROL1_IMON;
control1 &= ~ISL9241_CONTROL1_PSYS;
control4 |= ISL9241_CONTROL4_GP_COMPARATOR;
data = 0x0000; /* Set ACOK reference to 0V */
CPRINTS("Power saving enable");
}
if (i2c_write16(I2C_PORT_CHARGER, ISL9241_ADDR_FLAGS,
ISL9241_REG_ACOK_REFERENCE, data)) {
CPRINTS("Update ACOK reference fail");
}
if (i2c_write16(I2C_PORT_CHARGER, ISL9241_ADDR_FLAGS,
ISL9241_REG_CONTROL1, control1)) {
CPRINTS("Update psys control1 fail");
}
if (i2c_write16(I2C_PORT_CHARGER, ISL9241_ADDR_FLAGS,
ISL9241_REG_CONTROL4, control4)) {
CPRINTS("Update psys control4 fail");
}
}
/* Initialize board. */
static void board_init(void)
{
if (flash_storage_get(FLASH_FLAGS_ACPOWERON) && !ac_boot_status())
*host_get_customer_memmap(0x48) = flash_storage_get(FLASH_FLAGS_ACPOWERON);
if (flash_storage_get(FLASH_FLAGS_STANDALONE))
set_standalone_mode(1);
check_chassis_open(1);
gpio_enable_interrupt(GPIO_SOC_ENBKL);
gpio_enable_interrupt(GPIO_ON_OFF_BTN_L);
reconfigure_kbbl_pwm_frquency();
/**
* If we plug-in DC and chassis open, EC will assert EC_ON.
* We should de-assert the VCI_OUT if we do not power on system.
*/
if (!extpower_is_present())
board_power_off();
/* GPIO062 for DVT2 or the older boards need to set the input. */
if (board_get_version() <= BOARD_VERSION_8)
gpio_set_flags(GPIO_PM_SLP_S0_L, GPIO_INPUT);
}
DECLARE_HOOK(HOOK_INIT, board_init, HOOK_PRIO_DEFAULT + 1);
/* Called on AP S5 -> S3 transition */
static void board_chipset_startup(void)
{
CPRINTS("HOOK_CHIPSET_STARTUP - called board_chipset_startup");
charger_psys_enable(1);
}
DECLARE_HOOK(HOOK_CHIPSET_STARTUP,
board_chipset_startup,
HOOK_PRIO_DEFAULT);
/* Called on AP S3 -> S5 transition */
static void board_chipset_shutdown(void)
{
int batt_status;
battery_status(&batt_status);
CPRINTS(" HOOK_CHIPSET_SHUTDOWN board_chipset_shutdown");
#ifdef CONFIG_EMI_REGION1
lpc_set_host_event_mask(LPC_HOST_EVENT_SCI, 0);
#endif
charger_psys_enable(0);
}
DECLARE_HOOK(HOOK_CHIPSET_SHUTDOWN,
board_chipset_shutdown,
HOOK_PRIO_DEFAULT);
static int force_gpio6_rework;
void disable_bb_retimer_power_deferred(void)
{
if (board_get_version() > BOARD_VERSION_10 || force_gpio6_rework) {
CPRINTS("Disable BBR power");
gpio_set_level(GPIO_PM_SLP_S0_L, 0);
}
}
DECLARE_DEFERRED(disable_bb_retimer_power_deferred);
/* Called on AP S3 -> S0 transition */
static void board_chipset_resume(void)
{
CPRINTS("HOOK_CHIPSET_RESUME");
/*gpio_set_level(GPIO_ENABLE_BACKLIGHT, 1);*/
gpio_set_level(GPIO_EC_MUTE_L, 1);
gpio_set_level(GPIO_CAM_EN, 1);
charger_psys_enable(1);
/* Enable BB retimer power, for MP boards. */
if (board_get_version() > BOARD_VERSION_10 || force_gpio6_rework) {
CPRINTS("Enable BBR power");
gpio_set_level(GPIO_PM_SLP_S0_L, 1);
}
hook_call_deferred(&disable_bb_retimer_power_deferred_data, -1);
}
DECLARE_HOOK(HOOK_CHIPSET_RESUME, board_chipset_resume,
MOTION_SENSE_HOOK_PRIO-1);
/* Called on AP S0 -> S3 transition */
static void board_chipset_suspend(void)
{
CPRINTS("HOOK_CHIPSET_SUSPEND");
/*gpio_set_level(GPIO_ENABLE_BACKLIGHT, 0);*/
if (power_get_state() == POWER_S0S3) {
gpio_set_level(GPIO_EC_MUTE_L, 0);
gpio_set_level(GPIO_CAM_EN, 0);
}
charger_psys_enable(0);
/* Disable BB retimer power, for MP boards. */
hook_call_deferred(&disable_bb_retimer_power_deferred_data, 3 * SECOND);
}
DECLARE_HOOK(HOOK_CHIPSET_SUSPEND,
board_chipset_suspend,
HOOK_PRIO_DEFAULT);
static int cmd_forcegpio6(int argc, char **argv)
{
if (argc == 2 && !strcasecmp(argv[1], "enable")) {
force_gpio6_rework = 1;
gpio_set_flags(GPIO_PM_SLP_S0_L, GPIO_OUTPUT);
} else if (argc == 2 && !strcasecmp(argv[1], "disable")) {
force_gpio6_rework = 0;
if (board_get_version() <= BOARD_VERSION_8)
gpio_set_flags(GPIO_PM_SLP_S0_L, GPIO_INPUT);
} else {
return EC_ERROR_PARAM1;
}
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(forcegpio6, cmd_forcegpio6,
"[enable/disable]",
"Force retimer GPIO6 control on early boards with rework");
void board_hibernate(void)
{
board_power_off_deferred();
}
void board_hibernate_late(void)
{
/* put host chipset into reset */
gpio_set_level(GPIO_SYS_RESET_L, 0);
}
/* according to Panel team suggest, delay 60ms to meet spec */
static void bkoff_on_deferred(void)
{
gpio_set_level(GPIO_EC_BKOFF_L, 1);
}
DECLARE_DEFERRED(bkoff_on_deferred);
void soc_signal_interrupt(enum gpio_signal signal)
{
/* TODO: EC BKOFF signal is related soc enable panel siganl */
if (gpio_get_level(GPIO_SOC_ENBKL))
hook_call_deferred(&bkoff_on_deferred_data, 60 * MSEC);
else
gpio_set_level(GPIO_EC_BKOFF_L, 0);
}
void chassis_control_interrupt(enum gpio_signal signal)
{
/* TODO: implement c cover open/close behavior
* When c cover close, drop the EC_ON to tune off EC power
*/
}
struct {
enum hx20_board_version version;
int thresh_mv;
} const hx20_board_versions[] = {
/* Vin = 3.3V, Ideal voltage */
{ BOARD_VERSION_0, 203 }, /* 100 mV, 0 Kohm - Unused */
{ BOARD_VERSION_1, 409 }, /* 310 mV, Kohm - Unused */
{ BOARD_VERSION_2, 615 }, /* 520 mV, Kohm - Unused */
{ BOARD_VERSION_3, 821 }, /* 720 mV, Kohm - Unused */
{ BOARD_VERSION_4, 1028}, /* 930 mV, Kohm - EVT1 */
{ BOARD_VERSION_5, 1234 }, /* 1130 mV, Kohm - DVT1 Vpro */
{ BOARD_VERSION_6, 1440 }, /* 1340 mV, Kohm - DVT1 Non Vpro */
{ BOARD_VERSION_7, 1646 }, /* 1550 mV, Kohm - DVT2 Vpro */
{ BOARD_VERSION_8, 1853 }, /* 1750 mV, Kohm - DVT2 Non Vpro */
{ BOARD_VERSION_9, 2059 }, /* 1960 mV, Kohm - PVT Vpro */
{ BOARD_VERSION_10, 2265 }, /* 2170 mV, Kohm - PVT Non Vpro */
{ BOARD_VERSION_11, 2471 }, /* 2370 mV, Kohm - MP Vpro */
{ BOARD_VERSION_12, 2678 }, /* 2580 mV, Kohm - MP Non Vpro */
{ BOARD_VERSION_13, 2884 }, /* 2780 mV, Kohm */
{ BOARD_VERSION_14, 3090 }, /* 2990 mV, Kohm */
{ BOARD_VERSION_15, 3300 }, /* 3300 mV, Kohm */
};
BUILD_ASSERT(ARRAY_SIZE(hx20_board_versions) == BOARD_VERSION_COUNT);
int get_hardware_id(enum adc_channel channel)
{
int version = BOARD_VERSION_UNKNOWN;
int mv;
int i;
mv = adc_read_channel(channel);
if (mv == ADC_READ_ERROR)
return BOARD_VERSION_UNKNOWN;
for (i = 0; i < BOARD_VERSION_COUNT; i++)
if (mv < hx20_board_versions[i].thresh_mv) {
version = hx20_board_versions[i].version;
return version;
}
return version;
}
int board_get_version(void)
{
static int version = BOARD_VERSION_UNKNOWN;
if (version != BOARD_VERSION_UNKNOWN)
return version;
version = get_hardware_id(ADC_AD_BID);
return version;
}
/* Keyboard scan setting */
struct keyboard_scan_config keyscan_config = {
/*
* F3 key scan cycle completed but scan input is not
* charging to logic high when EC start scan next
* column for "T" key, so we set .output_settle_us
* to 80us from 50us.
*/
.output_settle_us = 80,
.debounce_down_us = 20 * MSEC,
.debounce_up_us = 30 * MSEC,
.scan_period_us = 3 * MSEC,
.min_post_scan_delay_us = 1000,
.poll_timeout_us = 100 * MSEC,
.actual_key_mask = {
0xff, 0xff, 0xff, 0x03, 0xff, 0xff, 0xff,
0xff, 0xff, 0x03, 0xff, 0xff, 0x03, 0xff, 0xff, 0xef /* full set */
},
};
/* Charger chips */
const struct charger_config_t chg_chips[] = {
{
.i2c_port = I2C_PORT_CHARGER,
.i2c_addr_flags = ISL9241_ADDR_FLAGS,
.drv = &isl9241_drv,
},
};
#ifdef CONFIG_CHARGER_CUSTOMER_SETTING
static void charger_chips_init(void);
void charger_chips_init_retry(void)
{
charger_chips_init();
}
DECLARE_DEFERRED(charger_chips_init_retry);
static void charger_chips_init(void)
{
/* Battery present need ADC function ready, so change the initail priority
* after ADC
*/
int chip;
uint16_t val = 0x0000; /*default ac setting */
uint32_t data = 0;
/*In our case the EC can boot before the charger has power so
* check if the charger is responsive before we try to init it */
if (i2c_read16(I2C_PORT_CHARGER, ISL9241_ADDR_FLAGS,
ISL9241_REG_ACOK_REFERENCE, &data) != EC_SUCCESS) {
CPRINTS("Retry Charger init");
hook_call_deferred(&charger_chips_init_retry_data, 100*MSEC);
return;
}
for (chip = 0; chip < board_get_charger_chip_count(); chip++) {
if (chg_chips[chip].drv->init)
chg_chips[chip].drv->init(chip);
}
if (i2c_write16(I2C_PORT_CHARGER, ISL9241_ADDR_FLAGS,
ISL9241_REG_CONTROL2, ISL9241_CONTROL2_TRICKLE_CHG_CURR_128 |
ISL9241_CONTROL2_GENERAL_PURPOSE_COMPARATOR |
ISL9241_CONTROL2_PROCHOT_DEBOUNCE_1000))
goto init_fail;
if (i2c_write16(I2C_PORT_CHARGER, ISL9241_ADDR_FLAGS,
ISL9241_REG_CONTROL3, ISL9241_CONTROL3_PSYS_GAIN |
ISL9241_CONTROL3_ACLIM_RELOAD))
goto init_fail;
if (i2c_write16(I2C_PORT_CHARGER, ISL9241_ADDR_FLAGS,
ISL9241_REG_CONTROL0, 0x0000))
goto init_fail;
val = ISL9241_CONTROL1_PROCHOT_REF_6800 | ISL9241_CONTROL1_SWITCH_FREQ;
/* make sure battery FET is enabled on EC on */
val &= ~ISL9241_CONTROL1_BGATE;
if (i2c_write16(I2C_PORT_CHARGER, ISL9241_ADDR_FLAGS,
ISL9241_REG_CONTROL1, val))
goto init_fail;
/* according to Power team suggest, Set ACOK reference to 4.544V */
if (i2c_write16(I2C_PORT_CHARGER, ISL9241_ADDR_FLAGS,
ISL9241_REG_ACOK_REFERENCE, 0x0B00))
goto init_fail;
cypd_charger_init_complete();
return;
init_fail:
CPRINTF("ISL9241 customer init failed!");
}
DECLARE_HOOK(HOOK_INIT, charger_chips_init, HOOK_PRIO_INIT_ADC + 1);
void charger_update(void)
{
static int pre_ac_state;
static int pre_dc_state;
int val = 0x0000;
if (pre_ac_state != extpower_is_present() ||
pre_dc_state != battery_is_present())
{
CPRINTS("update charger!!");
if (i2c_read16(I2C_PORT_CHARGER, ISL9241_ADDR_FLAGS,
ISL9241_REG_CONTROL1, &val)) {
CPRINTS("read charger control1 fail");
}
val |= (ISL9241_CONTROL1_PROCHOT_REF_6800 |
ISL9241_CONTROL1_SWITCH_FREQ);
if (i2c_write16(I2C_PORT_CHARGER, ISL9241_ADDR_FLAGS,
ISL9241_REG_CONTROL1, val)) {
CPRINTS("Update charger control1 fail");
}
/* Set DC prochot to 6.912A */
if (i2c_write16(I2C_PORT_CHARGER, ISL9241_ADDR_FLAGS,
ISL9241_REG_DC_PROCHOT, 0x1B00))
CPRINTS("Update DC prochot fail");
pre_ac_state = extpower_is_present();
pre_dc_state = battery_is_present();
}
}
DECLARE_HOOK(HOOK_AC_CHANGE, charger_update, HOOK_PRIO_DEFAULT);
DECLARE_HOOK(HOOK_BATTERY_SOC_CHANGE, charger_update, HOOK_PRIO_DEFAULT);