diff options
Diffstat (limited to 'driver/tcpm/rt1718s.c')
-rw-r--r-- | driver/tcpm/rt1718s.c | 100 |
1 files changed, 94 insertions, 6 deletions
diff --git a/driver/tcpm/rt1718s.c b/driver/tcpm/rt1718s.c index 631d98a522..96a08785c2 100644 --- a/driver/tcpm/rt1718s.c +++ b/driver/tcpm/rt1718s.c @@ -25,26 +25,42 @@ #define RT1718S_SW_RESET_DELAY_MS 2 /* i2c_write function which won't wake TCPC from low power mode. */ -int rt1718s_write8(int port, int reg, int val) +static int rt1718s_write(int port, int reg, int val, int len) { if (reg > 0xFF) { return i2c_write_offset16( tcpc_config[port].i2c_info.port, tcpc_config[port].i2c_info.addr_flags, - reg, val, 1); + reg, val, len); + } else if (len == 1) { + return tcpc_write(port, reg, val); + } else { + return tcpc_write16(port, reg, val); } - return tcpc_write(port, reg, val); } -int rt1718s_read8(int port, int reg, int *val) +static int rt1718s_read(int port, int reg, int *val, int len) { if (reg > 0xFF) { return i2c_read_offset16( tcpc_config[port].i2c_info.port, tcpc_config[port].i2c_info.addr_flags, - reg, val, 1); + reg, val, len); + } else if (len == 1) { + return tcpc_read(port, reg, val); + } else { + return tcpc_read16(port, reg, val); } - return tcpc_read(port, reg, val); +} + +int rt1718s_write8(int port, int reg, int val) +{ + return rt1718s_write(port, reg, val, 1); +} + +int rt1718s_read8(int port, int reg, int *val) +{ + return rt1718s_read(port, reg, val, 1); } int rt1718s_update_bits8(int port, int reg, int mask, int val) @@ -61,6 +77,17 @@ int rt1718s_update_bits8(int port, int reg, int mask, int val) return rt1718s_write8(port, reg, reg_val); } +int rt1718s_write16(int port, int reg, int val) +{ + return rt1718s_write(port, reg, val, 2); +} + +int rt1718s_read16(int port, int reg, int *val) +{ + return rt1718s_read(port, reg, val, 2); +} + + static int rt1718s_sw_reset(int port) { int rv; @@ -385,6 +412,67 @@ static int rt1718s_enter_low_power_mode(int port) } #endif +int rt1718s_get_adc(int port, enum rt1718s_adc_channel channel, int *adc_val) +{ + static struct mutex adc_lock; + int rv; + const int max_wait_times = 30; + + if (in_interrupt_context()) { + CPRINTS("Err: use ADC in IRQ"); + return EC_ERROR_INVAL; + } + + mutex_lock(&adc_lock); + + /* Start ADC conversation */ + rv = rt1718s_write16(port, RT1718S_ADC_CTRL_01, BIT(channel)); + if (rv) + goto out; + + /* + * The expected conversion time is 85.3us * number of enabled channels. + * Polling for 3ms should be long enough. + */ + for (int i = 0; i < max_wait_times; i++) { + int adc_done; + + usleep(100); + rv = rt1718s_read8(port, RT1718S_RT_INT6, &adc_done); + if (rv) + goto out; + if (adc_done & RT1718S_RT_INT6_INT_ADC_DONE) + break; + if (i == max_wait_times - 1) { + CPRINTS("conversion fail channel=%d", channel); + rv = EC_ERROR_TIMEOUT; + goto out; + } + } + + /* Read ADC data */ + rv = rt1718s_read16(port, RT1718S_ADC_CHX_VOL_L(channel), adc_val); + if (rv) + goto out; + + /* + * The resolution of VBUS1 ADC is 12.5mV, + * other channels are 4mV. + */ + if (channel == RT1718S_ADC_VBUS1) + *adc_val = *adc_val * 125 / 10; + else + *adc_val *= 4; + +out: + /* Cleanup: disable adc and clear interrupt. Error ignored. */ + rt1718s_write16(port, RT1718S_ADC_CTRL_01, 0); + rt1718s_write8(port, RT1718S_RT_INT6, RT1718S_RT_INT6_INT_ADC_DONE); + + mutex_unlock(&adc_lock); + return rv; +} + /* RT1718S is a TCPCI compatible port controller */ const struct tcpm_drv rt1718s_tcpm_drv = { .init = &rt1718s_init, |