| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include <assert.h> |
| 10 |
#include <boardid.h> |
| 11 |
#include <console/console.h> |
| 12 |
#include <delay.h> |
| 13 |
#include <device/i2c_simple.h> |
| 14 |
#include <drivers/camera/cros_camera.h> |
| 15 |
#include <ec/google/chromeec/ec.h> |
| 16 |
#include <soc/auxadc_common.h> |
| 17 |
#include <soc/i2c.h> |
| 18 |
#include <soc/pmic_wrap_common.h> |
| 19 |
|
| 20 |
|
| 21 |
#define FLAPJACK_UNDEF_SKU_ID 0 |
| 22 |
|
| 23 |
#define ADC_LEVELS 12 |
| 24 |
|
| 25 |
enum { |
| 26 |
LCM_ID_CHANNEL = 2, |
| 27 |
RAM_ID_CHANNEL = 3, |
| 28 |
SKU_ID_CHANNEL = 4, |
| 29 |
}; |
| 30 |
|
| 31 |
static const int ram_voltages[ADC_LEVELS] = { |
| 32 |
|
| 33 |
74000, |
| 34 |
212000, |
| 35 |
319000, |
| 36 |
429000, |
| 37 |
542000, |
| 38 |
666000, |
| 39 |
781000, |
| 40 |
900000, |
| 41 |
1023000, |
| 42 |
1137000, |
| 43 |
1240000, |
| 44 |
1343000, |
| 45 |
}; |
| 46 |
|
| 47 |
static const int lcm_voltages[ADC_LEVELS] = { |
| 48 |
|
| 49 |
0, |
| 50 |
283000, |
| 51 |
394000, |
| 52 |
503000, |
| 53 |
608000, |
| 54 |
712000, |
| 55 |
823000, |
| 56 |
937000, |
| 57 |
1046000, |
| 58 |
1155000, |
| 59 |
1277000, |
| 60 |
1434000, |
| 61 |
}; |
| 62 |
|
| 63 |
static const int *adc_voltages[] = { |
| 64 |
[LCM_ID_CHANNEL] = lcm_voltages, |
| 65 |
[RAM_ID_CHANNEL] = ram_voltages, |
| 66 |
[SKU_ID_CHANNEL] = ram_voltages, |
| 67 |
}; |
| 68 |
|
| 69 |
static uint32_t get_adc_index(unsigned int channel) |
| 70 |
{ |
| 71 |
int value = auxadc_get_voltage_uv(channel); |
| 72 |
|
| 73 |
assert(channel < ARRAY_SIZE(adc_voltages)); |
| 74 |
const int *voltages = adc_voltages[channel]; |
| 75 |
assert(voltages); |
| 76 |
|
| 77 |
|
| 78 |
uint32_t id; |
| 79 |
for (id = 0; id < ADC_LEVELS - 1; id++) |
| 80 |
if (value < (voltages[id] + voltages[id + 1]) / 2) |
| 81 |
break; |
| 82 |
printk(BIOS_DEBUG, "ADC[%d]: Raw value=%d ID=%d\n", channel, value, id); |
| 83 |
return id; |
| 84 |
} |
| 85 |
|
| 86 |
static uint8_t eeprom_random_read(uint8_t bus, uint8_t slave, uint16_t offset, |
| 87 |
uint8_t *data, uint16_t len) |
| 88 |
{ |
| 89 |
struct i2c_msg seg[2]; |
| 90 |
uint8_t address[2]; |
| 91 |
|
| 92 |
address[0] = offset >> 8; |
| 93 |
address[1] = offset & 0xff; |
| 94 |
|
| 95 |
seg[0].flags = 0; |
| 96 |
seg[0].slave = slave; |
| 97 |
seg[0].buf = address; |
| 98 |
seg[0].len = sizeof(address); |
| 99 |
seg[1].flags = I2C_M_RD; |
| 100 |
seg[1].slave = slave; |
| 101 |
seg[1].buf = data; |
| 102 |
seg[1].len = len; |
| 103 |
|
| 104 |
return i2c_transfer(bus, seg, ARRAY_SIZE(seg)); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
#define PMIC_LDO_VCAMIO_CON0 0x1cb0 |
| 109 |
|
| 110 |
#define CROS_CAMERA_INFO_OFFSET 0x1f80 |
| 111 |
#define MT8183_FORMAT 0x8183 |
| 112 |
#define KODAMA_PID 0x00c7 |
| 113 |
|
| 114 |
|
| 115 |
static uint8_t wfc_id(void) |
| 116 |
{ |
| 117 |
if (!CONFIG(BOARD_GOOGLE_KODAMA)) |
| 118 |
return 0; |
| 119 |
|
| 120 |
int i, ret; |
| 121 |
uint8_t bus = 2; |
| 122 |
uint8_t dev_addr = 0x50; |
| 123 |
|
| 124 |
struct cros_camera_info data = {0}; |
| 125 |
|
| 126 |
const uint16_t sensor_pids[] = { |
| 127 |
[0] = 0x5965, |
| 128 |
[1] = 0x5035, |
| 129 |
}; |
| 130 |
|
| 131 |
mtk_i2c_bus_init(bus, I2C_SPEED_FAST); |
| 132 |
|
| 133 |
|
| 134 |
pwrap_write(PMIC_LDO_VCAMIO_CON0, 0x1); |
| 135 |
udelay(270); |
| 136 |
|
| 137 |
ret = eeprom_random_read(bus, dev_addr, CROS_CAMERA_INFO_OFFSET, |
| 138 |
(uint8_t *)&data, sizeof(data)); |
| 139 |
pwrap_write(PMIC_LDO_VCAMIO_CON0, 0x0); |
| 140 |
|
| 141 |
if (ret) { |
| 142 |
printk(BIOS_ERR, |
| 143 |
"Failed to read from EEPROM; using default WFC id 0\n"); |
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
if (check_cros_camera_info(&data)) { |
| 148 |
printk(BIOS_ERR, |
| 149 |
"Failed to check camera info; using default WFC id 0\n"); |
| 150 |
return 0; |
| 151 |
} |
| 152 |
|
| 153 |
if (data.data_format != MT8183_FORMAT) { |
| 154 |
printk(BIOS_ERR, "Incompatible camera format: %#04x\n", |
| 155 |
data.data_format); |
| 156 |
return 0; |
| 157 |
} |
| 158 |
if (data.module_pid != KODAMA_PID) { |
| 159 |
printk(BIOS_ERR, "Incompatible module pid: %#04x\n", |
| 160 |
data.module_pid); |
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
printk(BIOS_DEBUG, "Camera sensor pid: %#04x\n", data.sensor_pid); |
| 165 |
|
| 166 |
for (i = 0; i < ARRAY_SIZE(sensor_pids); i++) { |
| 167 |
if (data.sensor_pid == sensor_pids[i]) { |
| 168 |
printk(BIOS_INFO, "Detected WFC id: %d\n", i); |
| 169 |
return i; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
printk(BIOS_WARNING, "Unknown WFC id; using default id 0\n"); |
| 174 |
return 0; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
static uint8_t lcm_id(void) |
| 179 |
{ |
| 180 |
|
| 181 |
if (CONFIG(BOARD_GOOGLE_JACUZZI_COMMON)) |
| 182 |
return CONFIG_BOARD_OVERRIDE_LCM_ID; |
| 183 |
|
| 184 |
return get_adc_index(LCM_ID_CHANNEL); |
| 185 |
} |
| 186 |
|
| 187 |
uint32_t sku_id(void) |
| 188 |
{ |
| 189 |
static uint32_t cached_sku_id = BOARD_ID_INIT; |
| 190 |
|
| 191 |
if (cached_sku_id != BOARD_ID_INIT) |
| 192 |
return cached_sku_id; |
| 193 |
|
| 194 |
|
| 195 |
if (CONFIG(BOARD_GOOGLE_FLAPJACK)) { |
| 196 |
if (google_chromeec_cbi_get_sku_id(&cached_sku_id)) |
| 197 |
cached_sku_id = FLAPJACK_UNDEF_SKU_ID; |
| 198 |
return cached_sku_id; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
if (CONFIG(BOARD_GOOGLE_KUKUI)) { |
| 203 |
if (board_id() == 1) { |
| 204 |
cached_sku_id = 0; |
| 205 |
return cached_sku_id; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
cached_sku_id = (wfc_id() << 8 | |
| 216 |
lcm_id() << 4 | |
| 217 |
get_adc_index(SKU_ID_CHANNEL)); |
| 218 |
|
| 219 |
return cached_sku_id; |
| 220 |
} |
| 221 |
|
| 222 |
uint32_t ram_code(void) |
| 223 |
{ |
| 224 |
static uint32_t cached_ram_code = BOARD_ID_INIT; |
| 225 |
|
| 226 |
if (cached_ram_code == BOARD_ID_INIT) { |
| 227 |
cached_ram_code = get_adc_index(RAM_ID_CHANNEL); |
| 228 |
|
| 229 |
cached_ram_code += CONFIG_BOARD_SDRAM_TABLE_OFFSET; |
| 230 |
} |
| 231 |
return cached_ram_code; |
| 232 |
} |