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
|
/* Copyright 2016 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.
*/
/* Poppy board-specific configuration */
#include "adc.h"
#include "adc_chip.h"
#include "als.h"
#include "bd99992gw.h"
#include "board_config.h"
#include "button.h"
#include "charge_manager.h"
#include "charge_state.h"
#include "charge_ramp.h"
#include "charger.h"
#include "chipset.h"
#include "console.h"
#include "driver/accelgyro_bmi160.h"
#include "driver/als_opt3001.h"
#include "driver/baro_bmp280.h"
#include "driver/charger/isl923x.h"
#include "driver/tcpm/anx74xx.h"
#include "driver/tcpm/ps8xxx.h"
#include "driver/tcpm/tcpci.h"
#include "driver/tcpm/tcpm.h"
#include "driver/temp_sensor/bd99992gw.h"
#include "extpower.h"
#include "gpio.h"
#include "hooks.h"
#include "host_command.h"
#include "i2c.h"
#include "lid_switch.h"
#include "math_util.h"
#include "motion_lid.h"
#include "motion_sense.h"
#include "pi3usb9281.h"
#include "power.h"
#include "power_button.h"
#include "spi.h"
#include "switch.h"
#include "system.h"
#include "tablet_mode.h"
#include "task.h"
#include "temp_sensor.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"
#define CPRINTS(format, args...) cprints(CC_USBCHARGE, format, ## args)
#define CPRINTF(format, args...) cprintf(CC_USBCHARGE, format, ## args)
#define USB_PD_PORT_ANX74XX 0
/* Minimum input current limit. */
#define ILIM_MIN_MA 472
static void tcpc_alert_event(enum gpio_signal signal)
{
if ((signal == GPIO_USB_C0_PD_INT_ODL) &&
!gpio_get_level(GPIO_USB_C0_PD_RST_L))
return;
else if ((signal == GPIO_USB_C1_PD_INT_ODL) &&
!gpio_get_level(GPIO_USB_C1_PD_RST_L))
return;
#ifdef HAS_TASK_PDCMD
/* Exchange status with TCPCs */
host_command_pd_send_status(PD_CHARGE_NO_CHANGE);
#endif
}
/* Set PD discharge whenever VBUS detection is high (i.e. below threshold). */
static void vbus_discharge_handler(void)
{
if (system_get_board_version() >= 2) {
pd_set_vbus_discharge(0,
gpio_get_level(GPIO_USB_C0_VBUS_WAKE_L));
pd_set_vbus_discharge(1,
gpio_get_level(GPIO_USB_C1_VBUS_WAKE_L));
}
}
DECLARE_DEFERRED(vbus_discharge_handler);
void vbus0_evt(enum gpio_signal signal)
{
/* VBUS present GPIO is inverted */
usb_charger_vbus_change(0, !gpio_get_level(signal));
task_wake(TASK_ID_PD_C0);
hook_call_deferred(&vbus_discharge_handler_data, 0);
}
void vbus1_evt(enum gpio_signal signal)
{
/* VBUS present GPIO is inverted */
usb_charger_vbus_change(1, !gpio_get_level(signal));
task_wake(TASK_ID_PD_C1);
hook_call_deferred(&vbus_discharge_handler_data, 0);
}
void usb0_evt(enum gpio_signal signal)
{
task_set_event(TASK_ID_USB_CHG_P0, USB_CHG_EVENT_BC12, 0);
}
void usb1_evt(enum gpio_signal signal)
{
task_set_event(TASK_ID_USB_CHG_P1, USB_CHG_EVENT_BC12, 0);
}
#ifdef CONFIG_USB_PD_TCPC_LOW_POWER
static void anx74xx_cable_det_handler(void)
{
int cable_det = gpio_get_level(GPIO_USB_C0_CABLE_DET);
int reset_n = gpio_get_level(GPIO_USB_C0_PD_RST_L);
/*
* A cable_det low->high transition was detected. If following the
* debounce time, cable_det is high, and reset_n is low, then ANX3429 is
* currently in standby mode and needs to be woken up. Set the
* TCPC_RESET event which will bring the ANX3429 out of standby
* mode. Setting this event is gated on reset_n being low because the
* ANX3429 will always set cable_det when transitioning to normal mode
* and if in normal mode, then there is no need to trigger a tcpc reset.
*/
if (cable_det && !reset_n)
task_set_event(TASK_ID_PD_C0, PD_EVENT_TCPC_RESET, 0);
}
DECLARE_DEFERRED(anx74xx_cable_det_handler);
void anx74xx_cable_det_interrupt(enum gpio_signal signal)
{
/* debounce for 2 msec */
hook_call_deferred(&anx74xx_cable_det_handler_data, (2 * MSEC));
}
#endif
#include "gpio_list.h"
/* Hibernate wake configuration */
const enum gpio_signal hibernate_wake_pins[] = {
GPIO_AC_PRESENT,
GPIO_POWER_BUTTON_L,
};
const int hibernate_wake_pins_used = ARRAY_SIZE(hibernate_wake_pins);
/* ADC channels */
const struct adc_t adc_channels[] = {
/* Base detection */
[ADC_BASE_DET] = {"BASE_DET", NPCX_ADC_CH0,
ADC_MAX_VOLT, ADC_READ_MAX+1, 0},
/* Vbus sensing (10x voltage divider). */
[ADC_VBUS] = {"VBUS", NPCX_ADC_CH2, ADC_MAX_VOLT*10, ADC_READ_MAX+1, 0},
/*
* Adapter current output or battery charging/discharging current (uV)
* 18x amplification on charger side.
*/
[ADC_AMON_BMON] = {"AMON_BMON", NPCX_ADC_CH1, ADC_MAX_VOLT*1000/18,
ADC_READ_MAX+1, 0},
#ifdef BOARD_LUX
/*
* ISL9238 PSYS output is 1.44 uA/W over 12.4K resistor, to read
* 0.8V @ 45 W, i.e. 56250 uW/mV. Using ADC_MAX_VOLT*56250 and
* ADC_READ_MAX+1 as multiplier/divider leads to overflows, so we
* only divide by 2 (enough to avoid precision issues).
*/
[ADC_PSYS] = {"PSYS", NPCX_ADC_CH3,
ADC_MAX_VOLT*56250*2/(ADC_READ_MAX+1), 2, 0},
#endif
};
BUILD_ASSERT(ARRAY_SIZE(adc_channels) == ADC_CH_COUNT);
/* I2C port map */
const struct i2c_port_t i2c_ports[] = {
{"tcpc", NPCX_I2C_PORT0_0, 400, GPIO_I2C0_0_SCL, GPIO_I2C0_0_SDA},
{"als", NPCX_I2C_PORT0_1, 400, GPIO_I2C0_1_SCL, GPIO_I2C0_1_SDA},
{"charger", NPCX_I2C_PORT1, 100, GPIO_I2C1_SCL, GPIO_I2C1_SDA},
{"pmic", NPCX_I2C_PORT2, 400, GPIO_I2C2_SCL, GPIO_I2C2_SDA},
{"accelgyro", NPCX_I2C_PORT3, 400, GPIO_I2C3_SCL, GPIO_I2C3_SDA},
};
const unsigned int i2c_ports_used = ARRAY_SIZE(i2c_ports);
/* TCPC mux configuration */
const struct tcpc_config_t tcpc_config[CONFIG_USB_PD_PORT_MAX_COUNT] = {
{
.bus_type = EC_BUS_TYPE_I2C,
.i2c_info = {
.port = NPCX_I2C_PORT0_0,
.addr_flags = ANX74XX_I2C_ADDR1_FLAGS,
},
.drv = &anx74xx_tcpm_drv,
},
{
.bus_type = EC_BUS_TYPE_I2C,
.i2c_info = {
.port = NPCX_I2C_PORT0_0,
.addr_flags = PS8751_I2C_ADDR1_FLAGS,
},
.drv = &ps8xxx_tcpm_drv,
},
};
struct usb_mux usb_muxes[CONFIG_USB_PD_PORT_MAX_COUNT] = {
{
.driver = &anx74xx_tcpm_usb_mux_driver,
.hpd_update = &anx74xx_tcpc_update_hpd_status,
},
{
.driver = &tcpci_tcpm_usb_mux_driver,
.hpd_update = &ps8xxx_tcpc_update_hpd_status,
}
};
struct pi3usb9281_config pi3usb9281_chips[] = {
{
.i2c_port = I2C_PORT_USB_CHARGER_0,
.mux_lock = NULL,
},
{
.i2c_port = I2C_PORT_USB_CHARGER_1,
.mux_lock = NULL,
},
};
BUILD_ASSERT(ARRAY_SIZE(pi3usb9281_chips) ==
CONFIG_BC12_DETECT_PI3USB9281_CHIP_COUNT);
const struct charger_config_t chg_chips[] = {
{
.i2c_port = I2C_PORT_CHARGER,
.i2c_addr_flags = ISL923X_ADDR_FLAGS,
.drv = &isl923x_drv,
},
};
const unsigned int chg_cnt = ARRAY_SIZE(chg_chips);
/**
* Power on (or off) a single TCPC.
* minimum on/off delays are included.
*
* @param port Port number of TCPC.
* @param mode 0: power off, 1: power on.
*/
void board_set_tcpc_power_mode(int port, int mode)
{
if (port != USB_PD_PORT_ANX74XX)
return;
if (mode) {
gpio_set_level(GPIO_USB_C0_TCPC_PWR, 1);
msleep(ANX74XX_PWR_H_RST_H_DELAY_MS);
gpio_set_level(GPIO_USB_C0_PD_RST_L, 1);
} else {
gpio_set_level(GPIO_USB_C0_PD_RST_L, 0);
msleep(ANX74XX_RST_L_PWR_L_DELAY_MS);
gpio_set_level(GPIO_USB_C0_TCPC_PWR, 0);
msleep(ANX74XX_PWR_L_PWR_H_DELAY_MS);
}
}
void board_reset_pd_mcu(void)
{
/* Assert reset */
gpio_set_level(GPIO_USB_C0_PD_RST_L, 0);
gpio_set_level(GPIO_USB_C1_PD_RST_L, 0);
msleep(MAX(1, ANX74XX_RST_L_PWR_L_DELAY_MS));
gpio_set_level(GPIO_USB_C1_PD_RST_L, 1);
/* Disable TCPC0 (anx3429) power */
gpio_set_level(GPIO_USB_C0_TCPC_PWR, 0);
msleep(ANX74XX_PWR_L_PWR_H_DELAY_MS);
board_set_tcpc_power_mode(USB_PD_PORT_ANX74XX, 1);
}
void board_tcpc_init(void)
{
int port, reg;
/* Only reset TCPC if not sysjump */
if (!system_jumped_to_this_image()) {
gpio_set_level(GPIO_PP3300_USB_PD, 1);
/* TODO(crosbug.com/p/61098): How long do we need to wait? */
msleep(10);
board_reset_pd_mcu();
}
/*
* TODO: Remove when Poppy is updated with PS8751 A3.
*
* Force PS8751 A2 to wake from low power mode.
* If PS8751 remains in low power mode after sysjump,
* TCPM_INIT will fail due to not able to access PS8751.
*
* NOTE: PS8751 A3 will wake on any I2C access.
*/
i2c_read8(NPCX_I2C_PORT0_1, 0x08, 0xA0, ®);
/* Enable TCPC interrupts */
gpio_enable_interrupt(GPIO_USB_C0_PD_INT_ODL);
gpio_enable_interrupt(GPIO_USB_C1_PD_INT_ODL);
#ifdef CONFIG_USB_PD_TCPC_LOW_POWER
/* Enable CABLE_DET interrupt for ANX3429 wake from standby */
gpio_enable_interrupt(GPIO_USB_C0_CABLE_DET);
#endif
/*
* Initialize HPD to low; after sysjump SOC needs to see
* HPD pulse to enable video path
*/
for (port = 0; port < CONFIG_USB_PD_PORT_MAX_COUNT; port++) {
const struct usb_mux *mux = &usb_muxes[port];
mux->hpd_update(port, 0, 0);
}
}
DECLARE_HOOK(HOOK_INIT, board_tcpc_init, HOOK_PRIO_INIT_I2C+1);
uint16_t tcpc_get_alert_status(void)
{
uint16_t status = 0;
if (!gpio_get_level(GPIO_USB_C0_PD_INT_ODL)) {
if (gpio_get_level(GPIO_USB_C0_PD_RST_L))
status |= PD_STATUS_TCPC_ALERT_0;
}
if (!gpio_get_level(GPIO_USB_C1_PD_INT_ODL)) {
if (gpio_get_level(GPIO_USB_C1_PD_RST_L))
status |= PD_STATUS_TCPC_ALERT_1;
}
return status;
}
const struct temp_sensor_t temp_sensors[] = {
{"Battery", TEMP_SENSOR_TYPE_BATTERY, charge_get_battery_temp, 0, 4},
/* These BD99992GW temp sensors are only readable in S0 */
{"Ambient", TEMP_SENSOR_TYPE_BOARD, bd99992gw_get_val,
BD99992GW_ADC_CHANNEL_SYSTHERM0, 4},
{"Charger", TEMP_SENSOR_TYPE_BOARD, bd99992gw_get_val,
BD99992GW_ADC_CHANNEL_SYSTHERM1, 4},
{"DRAM", TEMP_SENSOR_TYPE_BOARD, bd99992gw_get_val,
BD99992GW_ADC_CHANNEL_SYSTHERM2, 4},
{"eMMC", TEMP_SENSOR_TYPE_BOARD, bd99992gw_get_val,
BD99992GW_ADC_CHANNEL_SYSTHERM3, 4},
};
BUILD_ASSERT(ARRAY_SIZE(temp_sensors) == TEMP_SENSOR_COUNT);
/*
* Check if PMIC fault registers indicate VR fault. If yes, print out fault
* register info to console. Additionally, set panic reason so that the OS can
* check for fault register info by looking at offset 0x14(PWRSTAT1) and
* 0x15(PWRSTAT2) in cros ec panicinfo.
*/
static void board_report_pmic_fault(const char *str)
{
int vrfault, pwrstat1 = 0, pwrstat2 = 0;
uint32_t info;
/* RESETIRQ1 -- Bit 4: VRFAULT */
if (i2c_read8(I2C_PORT_PMIC, I2C_ADDR_BD99992_FLAGS, 0x8, &vrfault)
!= EC_SUCCESS)
return;
if (!(vrfault & BIT(4)))
return;
/* VRFAULT has occurred, print VRFAULT status bits. */
/* PWRSTAT1 */
i2c_read8(I2C_PORT_PMIC, I2C_ADDR_BD99992_FLAGS, 0x16, &pwrstat1);
/* PWRSTAT2 */
i2c_read8(I2C_PORT_PMIC, I2C_ADDR_BD99992_FLAGS, 0x17, &pwrstat2);
CPRINTS("PMIC VRFAULT: %s", str);
CPRINTS("PMIC VRFAULT: PWRSTAT1=0x%02x PWRSTAT2=0x%02x", pwrstat1,
pwrstat2);
/* Clear all faults -- Write 1 to clear. */
i2c_write8(I2C_PORT_PMIC, I2C_ADDR_BD99992_FLAGS, 0x8, BIT(4));
i2c_write8(I2C_PORT_PMIC, I2C_ADDR_BD99992_FLAGS, 0x16, pwrstat1);
i2c_write8(I2C_PORT_PMIC, I2C_ADDR_BD99992_FLAGS, 0x17, pwrstat2);
/*
* Status of the fault registers can be checked in the OS by looking at
* offset 0x14(PWRSTAT1) and 0x15(PWRSTAT2) in cros ec panicinfo.
*/
info = ((pwrstat2 & 0xFF) << 8) | (pwrstat1 & 0xFF);
panic_set_reason(PANIC_SW_PMIC_FAULT, info, 0);
}
static void board_pmic_disable_slp_s0_vr_decay(void)
{
/*
* VCCIOCNT:
* Bit 6 (0) - Disable decay of VCCIO on SLP_S0# assertion
* Bits 5:4 (00) - Nominal output voltage: 0.850V
* Bits 3:2 (10) - VR set to AUTO on SLP_S0# de-assertion
* Bits 1:0 (10) - VR set to AUTO operating mode
*/
i2c_write8(I2C_PORT_PMIC, I2C_ADDR_BD99992_FLAGS, 0x30, 0x3a);
/*
* V18ACNT:
* Bits 7:6 (00) - Disable low power mode on SLP_S0# assertion
* Bits 5:4 (10) - Nominal voltage set to 1.8V
* Bits 3:2 (10) - VR set to AUTO on SLP_S0# de-assertion
* Bits 1:0 (10) - VR set to AUTO operating mode
*/
i2c_write8(I2C_PORT_PMIC, I2C_ADDR_BD99992_FLAGS, 0x34, 0x2a);
/*
* V100ACNT:
* Bits 7:6 (00) - Disable low power mode on SLP_S0# assertion
* Bits 5:4 (01) - Nominal voltage 1.0V
* Bits 3:2 (10) - VR set to AUTO on SLP_S0# de-assertion
* Bits 1:0 (10) - VR set to AUTO operating mode
*/
i2c_write8(I2C_PORT_PMIC, I2C_ADDR_BD99992_FLAGS, 0x37, 0x1a);
/*
* V085ACNT:
* Bits 7:6 (00) - Disable low power mode on SLP_S0# assertion
* Bits 5:4 (11) - Nominal voltage 1.0V
* Bits 3:2 (10) - VR set to AUTO on SLP_S0# de-assertion
* Bits 1:0 (10) - VR set to AUTO operating mode
*/
i2c_write8(I2C_PORT_PMIC, I2C_ADDR_BD99992_FLAGS, 0x38, 0x3a);
}
static void board_pmic_enable_slp_s0_vr_decay(void)
{
/*
* VCCIOCNT:
* Bit 6 (1) - Enable decay of VCCIO on SLP_S0# assertion
* Bits 5:4 (00) - Nominal output voltage: 0.850V
* Bits 3:2 (10) - VR set to AUTO on SLP_S0# de-assertion
* Bits 1:0 (10) - VR set to AUTO operating mode
*/
i2c_write8(I2C_PORT_PMIC, I2C_ADDR_BD99992_FLAGS, 0x30, 0x7a);
/*
* V18ACNT:
* Bits 7:6 (01) - Enable low power mode on SLP_S0# assertion
* Bits 5:4 (10) - Nominal voltage set to 1.8V
* Bits 3:2 (10) - VR set to AUTO on SLP_S0# de-assertion
* Bits 1:0 (10) - VR set to AUTO operating mode
*/
i2c_write8(I2C_PORT_PMIC, I2C_ADDR_BD99992_FLAGS, 0x34, 0x6a);
/*
* V100ACNT:
* Bits 7:6 (01) - Enable low power mode on SLP_S0# assertion
* Bits 5:4 (01) - Nominal voltage 1.0V
* Bits 3:2 (10) - VR set to AUTO on SLP_S0# de-assertion
* Bits 1:0 (10) - VR set to AUTO operating mode
*/
i2c_write8(I2C_PORT_PMIC, I2C_ADDR_BD99992_FLAGS, 0x37, 0x5a);
/*
* V085ACNT:
* Bits 7:6 (01) - Enable low power mode on SLP_S0# assertion
* Bits 5:4 (11) - Nominal voltage 1.0V
* Bits 3:2 (10) - VR set to AUTO on SLP_S0# de-assertion
* Bits 1:0 (10) - VR set to AUTO operating mode
*/
i2c_write8(I2C_PORT_PMIC, I2C_ADDR_BD99992_FLAGS, 0x38, 0x7a);
}
__override void power_board_handle_host_sleep_event(
enum host_sleep_event state)
{
if (state == HOST_SLEEP_EVENT_S0IX_SUSPEND)
board_pmic_enable_slp_s0_vr_decay();
else if (state == HOST_SLEEP_EVENT_S0IX_RESUME)
board_pmic_disable_slp_s0_vr_decay();
}
static void board_pmic_init(void)
{
board_report_pmic_fault("SYSJUMP");
if (system_jumped_to_this_image())
return;
/* DISCHGCNT3 - enable 100 ohm discharge on V1.00A */
i2c_write8(I2C_PORT_PMIC, I2C_ADDR_BD99992_FLAGS, 0x3e, 0x04);
board_pmic_disable_slp_s0_vr_decay();
/* VRMODECTRL - disable low-power mode for all rails */
i2c_write8(I2C_PORT_PMIC, I2C_ADDR_BD99992_FLAGS, 0x3b, 0x1f);
/* Disable power button shutdown timer. */
i2c_write8(I2C_PORT_PMIC, I2C_ADDR_BD99992_FLAGS, 0x14, 0x00);
}
DECLARE_DEFERRED(board_pmic_init);
/* Initialize board. */
static void board_init(void)
{
/*
* This enables pull-down on F_DIO1 (SPI MISO), and F_DIO0 (SPI MOSI),
* whenever the EC is not doing SPI flash transactions. This avoids
* floating SPI buffer input (MISO), which causes power leakage (see
* b/64797021).
*/
NPCX_PUPD_EN1 |= BIT(NPCX_DEVPU1_F_SPI_PUD_EN);
/* Provide AC status to the PCH */
gpio_set_level(GPIO_PCH_ACOK, extpower_is_present());
/* Enable sensors power supply */
gpio_set_level(GPIO_PP1800_DX_SENSOR, 1);
gpio_set_level(GPIO_PP3300_DX_SENSOR, 1);
/* Enable VBUS interrupt */
if (system_get_board_version() == 0) {
/*
* crosbug.com/p/61929: rev0 does not have VBUS detection,
* force detection on both ports.
*/
gpio_set_flags(GPIO_USB_C0_VBUS_WAKE_L,
GPIO_INPUT | GPIO_PULL_DOWN);
gpio_set_flags(GPIO_USB_C1_VBUS_WAKE_L,
GPIO_INPUT | GPIO_PULL_DOWN);
vbus0_evt(GPIO_USB_C0_VBUS_WAKE_L);
vbus1_evt(GPIO_USB_C1_VBUS_WAKE_L);
} else {
gpio_enable_interrupt(GPIO_USB_C0_VBUS_WAKE_L);
gpio_enable_interrupt(GPIO_USB_C1_VBUS_WAKE_L);
}
/* Enable pericom BC1.2 interrupts */
gpio_enable_interrupt(GPIO_USB_C0_BC12_INT_L);
gpio_enable_interrupt(GPIO_USB_C1_BC12_INT_L);
/*
* Set unused GPIO_LED_YELLO_C0[_OLD] as INPUT | PULL_UP
* for better S0ix/S3 power
*/
if (system_get_board_version() >= 5)
gpio_set_flags(GPIO_LED_YELLOW_C0_OLD,
GPIO_INPUT | GPIO_PULL_UP);
else
gpio_set_flags(GPIO_LED_YELLOW_C0,
GPIO_INPUT | GPIO_PULL_UP);
#ifdef BOARD_SORAKA
/*
* TODO(b/64503543): Add proper options(#ifdef ) for Non-LTE SKU
* Set unused LTE related pins as INPUT | PULL_UP
* for better S0ix/S3 power
*/
if (system_get_board_version() >= 4) {
gpio_set_flags(GPIO_WLAN_PE_RST,
GPIO_INPUT | GPIO_PULL_UP);
gpio_set_flags(GPIO_PP3300_DX_LTE,
GPIO_INPUT | GPIO_PULL_UP);
gpio_set_flags(GPIO_LTE_GPS_OFF_L,
GPIO_INPUT | GPIO_PULL_UP);
gpio_set_flags(GPIO_LTE_BODY_SAR_L,
GPIO_INPUT | GPIO_PULL_UP);
gpio_set_flags(GPIO_LTE_WAKE_L,
GPIO_INPUT | GPIO_PULL_UP);
gpio_set_flags(GPIO_LTE_OFF_ODL,
GPIO_INPUT | GPIO_PULL_UP);
}
#endif
/* Enable Gyro interrupts */
gpio_enable_interrupt(GPIO_ACCELGYRO3_INT_L);
/* Initialize PMIC */
hook_call_deferred(&board_pmic_init_data, 0);
}
DECLARE_HOOK(HOOK_INIT, board_init, HOOK_PRIO_DEFAULT);
/**
* Buffer the AC present GPIO to the PCH.
*/
static void board_extpower(void)
{
gpio_set_level(GPIO_PCH_ACOK, extpower_is_present());
}
DECLARE_HOOK(HOOK_AC_CHANGE, board_extpower, HOOK_PRIO_DEFAULT);
/**
* Set active charge port -- only one port can be active at a time.
*
* @param charge_port Charge port to enable.
*
* Returns EC_SUCCESS if charge port is accepted and made active,
* EC_ERROR_* otherwise.
*/
int board_set_active_charge_port(int charge_port)
{
/* charge port is a physical port */
int is_real_port = (charge_port >= 0 &&
charge_port < CONFIG_USB_PD_PORT_MAX_COUNT);
/* check if we are source VBUS on the port */
int source = gpio_get_level(charge_port == 0 ? GPIO_USB_C0_5V_EN :
GPIO_USB_C1_5V_EN);
if (is_real_port && source) {
CPRINTF("Skip enable p%d", charge_port);
return EC_ERROR_INVAL;
}
CPRINTF("New chg p%d", charge_port);
if (charge_port == CHARGE_PORT_NONE) {
/* Disable both ports */
gpio_set_level(GPIO_USB_C0_CHARGE_L, 1);
gpio_set_level(GPIO_USB_C1_CHARGE_L, 1);
} else {
#ifdef BOARD_LUX
/* Disable cross-power with base, charger task will reenable. */
board_enable_base_power(0);
#endif
/* Make sure non-charging port is disabled */
gpio_set_level(charge_port ? GPIO_USB_C0_CHARGE_L :
GPIO_USB_C1_CHARGE_L, 1);
/* Enable charging port */
gpio_set_level(charge_port ? GPIO_USB_C1_CHARGE_L :
GPIO_USB_C0_CHARGE_L, 0);
}
return EC_SUCCESS;
}
/**
* Set the charge limit based upon desired maximum.
*
* @param port Port number.
* @param supplier Charge supplier type.
* @param charge_ma Desired charge limit (mA).
* @param charge_mv Negotiated charge voltage (mV).
*/
void board_set_charge_limit(int port, int supplier, int charge_ma,
int max_ma, int charge_mv)
{
/* Adjust ILIM according to measurements to eliminate overshoot. */
charge_ma = (charge_ma - 500) * 31 / 32 + 472;
/* 5V is significantly more accurate than other voltages. */
if (charge_mv > 5000)
charge_ma -= 52;
charge_set_input_current_limit(MAX(charge_ma, ILIM_MIN_MA), charge_mv);
}
void board_hibernate(void)
{
CPRINTS("Triggering PMIC shutdown.");
uart_flush_output();
/* Trigger PMIC shutdown. */
if (i2c_write8(I2C_PORT_PMIC, I2C_ADDR_BD99992_FLAGS,
0x49, 0x01)) {
/*
* If we can't tell the PMIC to shutdown, instead reset
* and don't start the AP. Hopefully we'll be able to
* communicate with the PMIC next time.
*/
CPRINTS("PMIC i2c failed.");
system_reset(SYSTEM_RESET_LEAVE_AP_OFF);
}
/* Await shutdown. */
while (1)
;
}
int board_get_version(void)
{
static int ver = -1;
uint8_t id4;
if (ver != -1)
return ver;
ver = 0;
/* First 3 strappings are binary. */
if (gpio_get_level(GPIO_BOARD_VERSION1))
ver |= 0x01;
if (gpio_get_level(GPIO_BOARD_VERSION2))
ver |= 0x02;
if (gpio_get_level(GPIO_BOARD_VERSION3))
ver |= 0x04;
/*
* 4th bit is using tristate strapping, ternary encoding:
* Hi-Z (id4=2) => 0, (id4=0) => 1, (id4=1) => 2
*/
id4 = gpio_get_ternary(GPIO_BOARD_VERSION4);
ver |= ((id4 + 1) % 3) * 0x08;
CPRINTS("Board ID = %d", ver);
return ver;
}
/* Lid Sensor mutex */
static struct mutex g_lid_mutex;
static struct bmi160_drv_data_t g_bmi160_data;
static struct opt3001_drv_data_t g_opt3001_data = {
.scale = 1,
.uscale = 0,
.offset = 0,
};
/* Matrix to rotate accelrator into standard reference frame */
const mat33_fp_t mag_standard_ref = {
{ FLOAT_TO_FP(-1), 0, 0},
{ 0, FLOAT_TO_FP(1), 0},
{ 0, 0, FLOAT_TO_FP(-1)}
};
#ifdef BOARD_SORAKA
const mat33_fp_t lid_standard_ref = {
{ 0, FLOAT_TO_FP(-1), 0},
{FLOAT_TO_FP(1), 0, 0},
{ 0, 0, FLOAT_TO_FP(1)}
};
/* For rev3 and older */
const mat33_fp_t lid_standard_ref_old = {
{FLOAT_TO_FP(-1), 0, 0},
{ 0, FLOAT_TO_FP(-1), 0},
{ 0, 0, FLOAT_TO_FP(1)}
};
#else
const mat33_fp_t lid_standard_ref = {
{FLOAT_TO_FP(-1), 0, 0},
{ 0, FLOAT_TO_FP(-1), 0},
{ 0, 0, FLOAT_TO_FP(1)}
};
#endif
struct motion_sensor_t motion_sensors[] = {
[LID_ACCEL] = {
.name = "Lid Accel",
.active_mask = SENSOR_ACTIVE_S0,
.chip = MOTIONSENSE_CHIP_BMI160,
.type = MOTIONSENSE_TYPE_ACCEL,
.location = MOTIONSENSE_LOC_LID,
.drv = &bmi160_drv,
.mutex = &g_lid_mutex,
.drv_data = &g_bmi160_data,
.port = I2C_PORT_GYRO,
.i2c_spi_addr_flags = BMI160_ADDR0_FLAGS,
.rot_standard_ref = &lid_standard_ref,
.default_range = 4, /* g, to meet CDD 7.3.1/C-1-4 reqs */
.min_frequency = BMI160_ACCEL_MIN_FREQ,
.max_frequency = BMI160_ACCEL_MAX_FREQ,
.config = {
/* EC use accel for angle detection */
[SENSOR_CONFIG_EC_S0] = {
.odr = 10000 | ROUND_UP_FLAG,
.ec_rate = 100 * MSEC,
},
},
},
[LID_GYRO] = {
.name = "Lid Gyro",
.active_mask = SENSOR_ACTIVE_S0,
.chip = MOTIONSENSE_CHIP_BMI160,
.type = MOTIONSENSE_TYPE_GYRO,
.location = MOTIONSENSE_LOC_LID,
.drv = &bmi160_drv,
.mutex = &g_lid_mutex,
.drv_data = &g_bmi160_data,
.port = I2C_PORT_GYRO,
.i2c_spi_addr_flags = BMI160_ADDR0_FLAGS,
.default_range = 1000, /* dps */
.rot_standard_ref = &lid_standard_ref,
.min_frequency = BMI160_GYRO_MIN_FREQ,
.max_frequency = BMI160_GYRO_MAX_FREQ,
},
[LID_MAG] = {
.name = "Lid Mag",
.active_mask = SENSOR_ACTIVE_S0,
.chip = MOTIONSENSE_CHIP_BMI160,
.type = MOTIONSENSE_TYPE_MAG,
.location = MOTIONSENSE_LOC_LID,
.drv = &bmi160_drv,
.mutex = &g_lid_mutex,
.drv_data = &g_bmi160_data,
.port = I2C_PORT_GYRO,
.i2c_spi_addr_flags = BMI160_ADDR0_FLAGS,
.default_range = BIT(11), /* 16LSB / uT, fixed */
.rot_standard_ref = &mag_standard_ref,
.min_frequency = BMM150_MAG_MIN_FREQ,
.max_frequency = BMM150_MAG_MAX_FREQ(SPECIAL),
},
[LID_ALS] = {
.name = "Light",
.active_mask = SENSOR_ACTIVE_S0,
.chip = MOTIONSENSE_CHIP_OPT3001,
.type = MOTIONSENSE_TYPE_LIGHT,
.location = MOTIONSENSE_LOC_LID,
.drv = &opt3001_drv,
.drv_data = &g_opt3001_data,
.port = I2C_PORT_ALS,
.i2c_spi_addr_flags = OPT3001_I2C_ADDR_FLAGS,
.rot_standard_ref = NULL,
.default_range = 0x10000, /* scale = 1; uscale = 0 */
.min_frequency = OPT3001_LIGHT_MIN_FREQ,
.max_frequency = OPT3001_LIGHT_MAX_FREQ,
.config = {
[SENSOR_CONFIG_EC_S0] = {
.odr = 1000,
},
},
},
};
const unsigned int motion_sensor_count = ARRAY_SIZE(motion_sensors);
/* ALS instances when LPC mapping is needed. Each entry directs to a sensor. */
const struct motion_sensor_t *motion_als_sensors[] = {
&motion_sensors[LID_ALS],
};
BUILD_ASSERT(ARRAY_SIZE(motion_als_sensors) == ALS_COUNT);
#ifdef BOARD_SORAKA
static void board_sensor_init(void)
{
/* Old soraka use a different reference matrix */
if (system_get_board_version() <= 3) {
motion_sensors[LID_ACCEL].rot_standard_ref =
&lid_standard_ref_old;
motion_sensors[LID_GYRO].rot_standard_ref =
&lid_standard_ref_old;
}
}
DECLARE_HOOK(HOOK_INIT, board_sensor_init, HOOK_PRIO_DEFAULT);
#endif
static void board_chipset_reset(void)
{
board_report_pmic_fault("CHIPSET RESET");
}
DECLARE_HOOK(HOOK_CHIPSET_RESET, board_chipset_reset, HOOK_PRIO_DEFAULT);
/* Called on AP S3 -> S0 transition */
static void board_chipset_resume(void)
{
gpio_set_level(GPIO_ENABLE_BACKLIGHT, 1);
}
DECLARE_HOOK(HOOK_CHIPSET_RESUME, board_chipset_resume, HOOK_PRIO_DEFAULT);
/* Called on AP S0 -> S3 transition */
static void board_chipset_suspend(void)
{
gpio_set_level(GPIO_ENABLE_BACKLIGHT, 0);
}
DECLARE_HOOK(HOOK_CHIPSET_SUSPEND, board_chipset_suspend, HOOK_PRIO_DEFAULT);
int board_has_working_reset_flags(void)
{
int version = system_get_board_version();
/* Boards Rev1 and Rev2 will lose reset flags on power cycle. */
if ((version == 1) || (version == 2))
return 0;
/* All other board versions should have working reset flags */
return 1;
}
|