Skip to content

Commit

Permalink
Add usbc_mux_info command and implement for oryp10
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed Dec 8, 2023
1 parent cbad8e0 commit a707422
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/board/system76/common/include/board/usbpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ void usbpd_init(void);
void usbpd_event(void);
void usbpd_disable_charging(void);
void usbpd_enable_charging(void);
bool usbc_mux_info(uint8_t port, uint16_t * info);

#endif // _BOARD_USBPD_H
17 changes: 17 additions & 0 deletions src/board/system76/common/smfi.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <board/scratch.h>
#include <board/kbled.h>
#include <board/kbscan.h>
#include <board/usbpd.h>

#if CONFIG_SECURITY
#include <board/security.h>
Expand Down Expand Up @@ -262,6 +263,18 @@ static enum Result cmd_security_set(void) {
}
#endif // CONFIG_SECURITY

static enum Result cmd_usbc_mux_info(void) {
uint8_t port = smfi_cmd[SMFI_CMD_DATA];
uint16_t info = 0;
if (usbc_mux_info(port, &info)) {
smfi_cmd[SMFI_CMD_DATA + 1] = (uint8_t)info;
smfi_cmd[SMFI_CMD_DATA + 2] = (uint8_t)(info >> 8);
return RES_OK;
} else {
return RES_ERR;
}
}

#endif // !defined(__SCRATCH__)

#if defined(__SCRATCH__)
Expand Down Expand Up @@ -419,6 +432,10 @@ void smfi_event(void) {
break;
#endif // CONFIG_SECURITY

case CMD_USBC_MUX_INFO:
smfi_cmd[SMFI_CMD_RES] = cmd_usbc_mux_info();
break;

#endif // !defined(__SCRATCH__)
case CMD_SPI:
smfi_cmd[SMFI_CMD_RES] = cmd_spi();
Expand Down
4 changes: 4 additions & 0 deletions src/board/system76/common/usbpd/none.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ void usbpd_event(void) {}
void usbpd_disable_charging(void) {}

void usbpd_enable_charging(void) {}

bool usbc_mux_info(uint8_t port, uint16_t * info) {
return false;
}
135 changes: 130 additions & 5 deletions src/board/system76/common/usbpd/tps65987.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,39 @@
#include <board/gpio.h>
#include <board/power.h>
#include <board/usbpd.h>
#include <common/command.h>
#include <common/debug.h>
#include <ec/i2c.h>

#define USBPD_ADDRESS 0x20

#define REG_CMD_1 0x08
#define REG_TX_SINK_CAPABILITIES 0x33
#define REG_ACTIVE_CONTRACT_PDO 0x34
#define REG_DATA_STATUS 0x5F
#define REG_DATA_STATUS_DATA_ORIENTATION (1 << 1)
#define REG_DATA_STATUS_ACTIVE_CABLE (1 << 2)
#define REG_DATA_STATUS_USB3_CONNECTION (1 << 5)
#define REG_DATA_STATUS_USB_DATA_ROLE (1 << 7)
#define REG_DATA_STATUS_DP_CONNECTION (1 << 8)
#define REG_DATA_STATUS_DP_PIN_ASSIGNMENT_MASK (0b11 << 10)
#define REG_DATA_STATUS_DP_PIN_ASSIGNMENT_AB (0b10 << 10)
#define REG_DATA_STATUS_DP_PIN_ASSIGNMENT_CD (0b01 << 10)
#define REG_DATA_STATUS_DP_PIN_ASSIGNMENT_EF (0b00 << 10)
#define REG_DATA_STATUS_DEBUG_ACCESSORY_MODE (1 << 12)
#define REG_DATA_STATUS_HPD_IRQ (1 << 14)
#define REG_DATA_STATUS_HPD_LEVEL (1 << 15)

#ifndef HAVE_USBPD_CHARGING
#define HAVE_USBPD_CHARGING 1
#endif // HAVE_USBPD_CHARGING

void usbpd_init(void) {
i2c_reset(&I2C_USBPD, true);
}

#if HAVE_USBPD_CHARGING

static int16_t usbpd_current_limit(void) {
uint8_t value[7] = { 0 };
int16_t res = i2c_get(&I2C_USBPD, USBPD_ADDRESS, REG_ACTIVE_CONTRACT_PDO, value, sizeof(value));
Expand Down Expand Up @@ -168,7 +190,7 @@ static int16_t usbpd_aneg(void) {
int16_t res;

uint8_t cmd[5] = { 4, 'A', 'N', 'e', 'g' };
res = i2c_set(&I2C_USBPD, USBPD_ADDRESS, 0x08, cmd, sizeof(cmd));
res = i2c_set(&I2C_USBPD, USBPD_ADDRESS, REG_CMD_1, cmd, sizeof(cmd));
if (res < 0) {
return res;
}
Expand All @@ -185,7 +207,7 @@ void usbpd_disable_charging(void) {

// Read current value
uint8_t value[2] = { 0 };
res = i2c_get(&I2C_USBPD, USBPD_ADDRESS, 0x33, value, sizeof(value));
res = i2c_get(&I2C_USBPD, USBPD_ADDRESS, REG_TX_SINK_CAPABILITIES, value, sizeof(value));
if (res < 0) {
DEBUG("ERR %04X\n", -res);
return;
Expand All @@ -200,7 +222,7 @@ void usbpd_disable_charging(void) {
// Enable only the first TX sink PDO (5V)
value[0] = 1;
value[1] = 1;
res = i2c_set(&I2C_USBPD, USBPD_ADDRESS, 0x33, value, sizeof(value));
res = i2c_set(&I2C_USBPD, USBPD_ADDRESS, REG_TX_SINK_CAPABILITIES, value, sizeof(value));
if (res < 0) {
DEBUG("ERR %04X\n", -res);
return;
Expand All @@ -223,7 +245,7 @@ void usbpd_enable_charging(void) {

// Read current value
uint8_t value[2] = { 0 };
res = i2c_get(&I2C_USBPD, USBPD_ADDRESS, 0x33, value, sizeof(value));
res = i2c_get(&I2C_USBPD, USBPD_ADDRESS, REG_TX_SINK_CAPABILITIES, value, sizeof(value));
if (res < 0) {
DEBUG("ERR %04X\n", -res);
return;
Expand All @@ -238,7 +260,7 @@ void usbpd_enable_charging(void) {
// Enable the first two TX sink PDO (5V and 20V)
value[0] = 1;
value[1] = 2;
res = i2c_set(&I2C_USBPD, USBPD_ADDRESS, 0x33, value, sizeof(value));
res = i2c_set(&I2C_USBPD, USBPD_ADDRESS, REG_TX_SINK_CAPABILITIES, value, sizeof(value));
if (res < 0) {
DEBUG("ERR %04X\n", -res);
return;
Expand All @@ -253,3 +275,106 @@ void usbpd_enable_charging(void) {

DEBUG("OK\n");
}

#else // HAVE_USBPD_CHARGING

void usbpd_event(void) {
bool update = false;

static bool last_ac_in = false;
bool ac_in = !gpio_get(&ACIN_N);
if (ac_in != last_ac_in) {
last_ac_in = ac_in;
update = true;

DEBUG("AC_IN %d\n", ac_in);
}

if (update) {
int16_t res;

DEBUG("USBPD DATA STATUS ");

// Read current value
uint8_t value[6] = { 0 };
res = i2c_get(&I2C_USBPD, USBPD_ADDRESS, REG_DATA_STATUS, value, sizeof(value));
if (res < 0) {
DEBUG("ERR %04X\n", -res);
} else {
DEBUG("OK %02x = %02X, %02X%02X%02X%02X\n", value[0], value[5], value[4], value[3], value[2], value[1]);
}
}
}

void usbpd_disable_charging(void) {}

#endif // HAVE_USBPD_CHARGING

bool usbc_mux_info(uint8_t port, uint16_t * info) {
if (port != 0) {
// Only port 0 is supported right now
WARN("usbc_mux_info does not support port %d\n", port);
return false;
}

uint8_t value[6] = { 0 };
int16_t res = i2c_get(&I2C_USBPD, USBPD_ADDRESS, REG_DATA_STATUS, value, sizeof(value));
if (res < 0) {
DEBUG("ERR %04X\n", -res);
return false;
} else {
DEBUG("OK %02X, %02x = %02X, %02X%02X%02X%02X\n", res, value[0], value[5], value[4], value[3], value[2], value[1]);
uint32_t data_status = ((uint32_t)value[1]) | (((uint32_t)value[2]) << 8) |
(((uint32_t)value[3]) << 16) | (((uint32_t)value[4]) << 24);
*info = 0;
if (data_status & REG_DATA_STATUS_DP_CONNECTION) {
*info |= CMD_USBC_MUX_INFO_DP;
}
if (data_status & REG_DATA_STATUS_USB3_CONNECTION) {
*info |= CMD_USBC_MUX_INFO_USB;
}
if (data_status & REG_DATA_STATUS_ACTIVE_CABLE) {
*info |= CMD_USBC_MUX_INFO_CABLE;
}
if (data_status & REG_DATA_STATUS_DATA_ORIENTATION) {
*info |= CMD_USBC_MUX_INFO_POLARITY;
}
if (data_status & REG_DATA_STATUS_HPD_LEVEL) {
*info |= CMD_USBC_MUX_INFO_HPD_LVL;
}
if (data_status & REG_DATA_STATUS_HPD_IRQ) {
*info |= CMD_USBC_MUX_INFO_HPD_IRQ;
}
if (data_status & REG_DATA_STATUS_USB_DATA_ROLE) {
*info |= CMD_USBC_MUX_INFO_UFP;
}
if (data_status & REG_DATA_STATUS_DEBUG_ACCESSORY_MODE) {
*info |= CMD_USBC_MUX_INFO_DBG_ACC;
}
switch (data_status & REG_DATA_STATUS_DP_PIN_ASSIGNMENT_MASK) {
case REG_DATA_STATUS_DP_PIN_ASSIGNMENT_AB:
if (data_status & REG_DATA_STATUS_USB3_CONNECTION) {
*info |= CMD_USBC_MUX_INFO_DP_MODE_B;
} else {
*info |= CMD_USBC_MUX_INFO_DP_MODE_A;
}
break;
case REG_DATA_STATUS_DP_PIN_ASSIGNMENT_CD:
if (data_status & REG_DATA_STATUS_USB3_CONNECTION) {
*info |= CMD_USBC_MUX_INFO_DP_MODE_D;
} else {
*info |= CMD_USBC_MUX_INFO_DP_MODE_C;
}
break;
case REG_DATA_STATUS_DP_PIN_ASSIGNMENT_EF:
if (data_status & REG_DATA_STATUS_USB3_CONNECTION) {
*info |= CMD_USBC_MUX_INFO_DP_MODE_F;
} else {
*info |= CMD_USBC_MUX_INFO_DP_MODE_E;
}
break;
}
DEBUG("USBC_MUX_INFO: %04X\n", *info);
return true;
}
}
9 changes: 8 additions & 1 deletion src/board/system76/oryp10/board.mk
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ CFLAGS+=-DI2C_SMBUS=I2C_4
# Set touchpad PS2 bus
CFLAGS+=-DPS2_TOUCHPAD=PS2_3

# Set USB-PD I2C bus
CFLAGS+=-DI2C_USBPD=I2C_1

# Set smart charger parameters
# TODO: actually bq24800
# FIXME: Verify parts and values.
Expand All @@ -40,9 +43,13 @@ CFLAGS+=\
-DCHARGER_CHARGE_VOLTAGE=13050 \
-DCHARGER_INPUT_CURRENT=11500

# Set USB-PD parameters
USBPD=tps65987
CFLAGS+=-DHAVE_USBPD_CHARGING=0

# Set CPU power limits in watts
CFLAGS+=\
-DPOWER_LIMIT_AC=180 \
-DPOWER_LIMIT_AC=230 \
-DPOWER_LIMIT_DC=45

# Disable syncing fan speeds
Expand Down
35 changes: 35 additions & 0 deletions src/common/include/common/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ enum Command {
CMD_SECURITY_GET = 20,
// Set security state
CMD_SECURITY_SET = 21,
// Get USB-C mux info (for coreboot)
CMD_USBC_MUX_INFO = 23,
//TODO
};

Expand Down Expand Up @@ -85,4 +87,37 @@ enum SecurityState {
SECURITY_STATE_PREPARE_UNLOCK = 3,
};

enum UsbcMuxInfoFlags {
// DisplayPort connected if set
CMD_USBC_MUX_INFO_DP = (1 << 0),
// USB connected if set
CMD_USBC_MUX_INFO_USB = (1 << 1),
// Active cable if set, passive if not set
CMD_USBC_MUX_INFO_CABLE = (1 << 2),
// Polarity of device, flipped if set, normal if not set
CMD_USBC_MUX_INFO_POLARITY = (1 << 3),
// HPD level assert
CMD_USBC_MUX_INFO_HPD_LVL = (1 << 4),
// HPD IRQ assert
CMD_USBC_MUX_INFO_HPD_IRQ = (1 << 5),
// UFP if set, DFP if not set
CMD_USBC_MUX_INFO_UFP = (1 << 6),
// Debug accessory if set
CMD_USBC_MUX_INFO_DBG_ACC = (1 << 7),
// Mask for DP pin mode
CMD_USBC_MUX_INFO_DP_MODE_MASK = (0xF << 8),
// DP pin mode A
CMD_USBC_MUX_INFO_DP_MODE_A = (1 << 8),
// DP pin mode B
CMD_USBC_MUX_INFO_DP_MODE_B = (2 << 8),
// DP pin mode C
CMD_USBC_MUX_INFO_DP_MODE_C = (3 << 8),
// DP pin mode D
CMD_USBC_MUX_INFO_DP_MODE_D = (4 << 8),
// DP pin mode E
CMD_USBC_MUX_INFO_DP_MODE_E = (5 << 8),
// DP pin mode F
CMD_USBC_MUX_INFO_DP_MODE_F = (6 << 8),
};

#endif // _COMMON_COMMAND_H

0 comments on commit a707422

Please sign in to comment.