1 /* SPDX-License-Identifier: GPL-2.0-only */
5 #include <console/console.h>
6 #include <ec/google/chromeec/ec.h>
7 #include <soc/auxadc.h>
11 /* board_id is provided by ec/google/chromeec/ec_boardid.c */
17 RAM_ID_LOW_CHANNEL
= 2,
18 RAM_ID_HIGH_CHANNEL
= 3,
20 SKU_ID_LOW_CHANNEL
= 4,
21 SKU_ID_HIGH_CHANNEL
= 5,
24 static const unsigned int lcm_voltages
[ADC_LEVELS
] = {
25 /* ID : Voltage (unit: uV) */
40 static const unsigned int ram_voltages
[ADC_LEVELS
] = {
41 /* ID : Voltage (unit: uV) */
56 static const unsigned int *adc_voltages
[] = {
57 [RAM_ID_LOW_CHANNEL
] = ram_voltages
,
58 [RAM_ID_HIGH_CHANNEL
] = ram_voltages
,
59 [SKU_ID_LOW_CHANNEL
] = ram_voltages
,
60 [SKU_ID_HIGH_CHANNEL
] = ram_voltages
,
63 static const unsigned int *adc_voltages_detachable
[] = {
64 [RAM_ID_LOW_CHANNEL
] = ram_voltages
,
65 [RAM_ID_HIGH_CHANNEL
] = ram_voltages
,
66 [SKU_ID_LOW_CHANNEL
] = ram_voltages
,
67 [SKU_ID_HIGH_CHANNEL
] = lcm_voltages
,
70 static uint32_t get_adc_index(unsigned int channel
)
72 unsigned int value
= auxadc_get_voltage_uv(channel
);
73 const unsigned int *voltages
;
75 if (CONFIG(BOARD_GOOGLE_STARYU_COMMON
)) {
76 assert(channel
< ARRAY_SIZE(adc_voltages_detachable
));
77 voltages
= adc_voltages_detachable
[channel
];
79 assert(channel
< ARRAY_SIZE(adc_voltages
));
80 voltages
= adc_voltages
[channel
];
85 /* Find the closest voltage */
87 for (id
= 0; id
< ADC_LEVELS
- 1; id
++)
88 if (value
< (voltages
[id
] + voltages
[id
+ 1]) / 2)
91 printk(BIOS_DEBUG
, "ADC[%u]: Raw value=%u ID=%u\n", channel
, value
, id
);
95 /* Detachables use ADC channel 5 for panel ID */
96 uint32_t panel_id(void)
98 static uint32_t cached_panel_id
= BOARD_ID_INIT
;
100 if (cached_panel_id
== BOARD_ID_INIT
)
101 cached_panel_id
= get_adc_index(SKU_ID_HIGH_CHANNEL
);
103 return cached_panel_id
;
106 uint32_t sku_id(void)
108 static uint32_t cached_sku_code
= BOARD_ID_INIT
;
110 if (cached_sku_code
!= BOARD_ID_INIT
)
111 return cached_sku_code
;
113 cached_sku_code
= google_chromeec_get_board_sku();
115 if (CONFIG(BOARD_GOOGLE_STARYU_COMMON
)) {
116 if (cached_sku_code
== CROS_SKU_UNPROVISIONED
||
117 cached_sku_code
== CROS_SKU_UNKNOWN
) {
118 printk(BIOS_WARNING
, "SKU code from EC: %s\n",
119 (cached_sku_code
== CROS_SKU_UNKNOWN
) ?
120 "CROS_SKU_UNKNOWN" : "CROS_SKU_UNPROVISIONED");
121 /* Reserve last 4 bits to report PANEL_ID */
122 cached_sku_code
= 0x7FFFFFF0UL
| panel_id();
124 } else if (cached_sku_code
== CROS_SKU_UNKNOWN
) {
125 printk(BIOS_WARNING
, "Failed to get SKU code from EC\n");
126 cached_sku_code
= (get_adc_index(SKU_ID_HIGH_CHANNEL
) << 4 |
127 get_adc_index(SKU_ID_LOW_CHANNEL
));
130 printk(BIOS_DEBUG
, "SKU Code: %#02x\n", cached_sku_code
);
131 return cached_sku_code
;
134 uint32_t ram_code(void)
136 static uint32_t cached_ram_code
= BOARD_ID_INIT
;
138 if (cached_ram_code
== BOARD_ID_INIT
) {
139 cached_ram_code
= (get_adc_index(RAM_ID_HIGH_CHANNEL
) << 4 |
140 get_adc_index(RAM_ID_LOW_CHANNEL
));
141 printk(BIOS_DEBUG
, "RAM Code: %#02x\n", cached_ram_code
);
144 return cached_ram_code
;