Skip to content

Commit 041d61a

Browse files
Hermes Wulumag
authored andcommitted
drm/bridge: it6505: add I2C functionality on AUX
DisplayPort AUX protocol supports I2C transport which is capable of reading EDID or supports MCCS. In drm_dp_helper, drm_dp_i2c_xfer() packs I2C requests into a sequence of AUX requests. it6505_aux_i2c_operation() is implemented to match drm_dp_i2c_xfer() operactions. it6505_aux_i2c_transfer() adds I2C functionality for it6505_aux_transfer(). Reviewed-by: Dmitry Baryshkov <[email protected]> Signed-off-by: Hermes Wu <[email protected]> Reviewed-by: AngeloGioacchino Del Regno <[email protected]> Signed-off-by: Dmitry Baryshkov <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/20241230-v7-upstream-v7-10-e0fdd4844703@ite.corp-partner.google.com
1 parent 9f9eef9 commit 041d61a

File tree

1 file changed

+175
-2
lines changed

1 file changed

+175
-2
lines changed

drivers/gpu/drm/bridge/ite-it6505.c

Lines changed: 175 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,18 @@
268268
#define REG_SSC_CTRL1 0x189
269269
#define REG_SSC_CTRL2 0x18A
270270

271+
#define REG_AUX_USER_CTRL 0x190
272+
#define EN_USER_AUX BIT(0)
273+
#define USER_AUX_DONE BIT(1)
274+
#define AUX_EVENT BIT(4)
275+
276+
#define REG_AUX_USER_DATA_REC 0x191
277+
#define M_AUX_IN_REC 0xF0
278+
#define M_AUX_OUT_REC 0x0F
279+
280+
#define REG_AUX_USER_REPLY 0x19A
281+
#define REG_AUX_USER_RXB(n) (n + 0x19B)
282+
271283
#define RBR DP_LINK_BW_1_62
272284
#define HBR DP_LINK_BW_2_7
273285
#define HBR2 DP_LINK_BW_5_4
@@ -303,6 +315,8 @@
303315
#define MAX_EQ_LEVEL 0x03
304316
#define AUX_WAIT_TIMEOUT_MS 15
305317
#define AUX_FIFO_MAX_SIZE 16
318+
#define AUX_I2C_MAX_SIZE 4
319+
#define AUX_I2C_DEFER_RETRY 4
306320
#define PIXEL_CLK_DELAY 1
307321
#define PIXEL_CLK_INVERSE 0
308322
#define ADJUST_PHASE_THRESHOLD 80000
@@ -325,7 +339,12 @@
325339
enum aux_cmd_type {
326340
CMD_AUX_NATIVE_READ = 0x0,
327341
CMD_AUX_NATIVE_WRITE = 0x5,
342+
CMD_AUX_GI2C_ADR = 0x08,
343+
CMD_AUX_GI2C_READ = 0x09,
344+
CMD_AUX_GI2C_WRITE = 0x0A,
328345
CMD_AUX_I2C_EDID_READ = 0xB,
346+
CMD_AUX_I2C_READ = 0x0D,
347+
CMD_AUX_I2C_WRITE = 0x0C,
329348

330349
/* KSV read with AUX FIFO extend from CMD_AUX_NATIVE_READ*/
331350
CMD_AUX_GET_KSV_LIST = 0x10,
@@ -1107,6 +1126,161 @@ static ssize_t it6505_aux_do_transfer(struct it6505 *it6505,
11071126
return ret;
11081127
}
11091128

1129+
static bool it6505_aux_i2c_reply_defer(u8 reply)
1130+
{
1131+
if (reply == DP_AUX_NATIVE_REPLY_DEFER || reply == DP_AUX_I2C_REPLY_DEFER)
1132+
return true;
1133+
return false;
1134+
}
1135+
1136+
static bool it6505_aux_i2c_reply_nack(u8 reply)
1137+
{
1138+
if (reply == DP_AUX_NATIVE_REPLY_NACK || reply == DP_AUX_I2C_REPLY_NACK)
1139+
return true;
1140+
return false;
1141+
}
1142+
1143+
static int it6505_aux_i2c_wait(struct it6505 *it6505, u8 *reply)
1144+
{
1145+
int err = 0;
1146+
unsigned long timeout;
1147+
struct device *dev = it6505->dev;
1148+
1149+
timeout = jiffies + msecs_to_jiffies(AUX_WAIT_TIMEOUT_MS) + 1;
1150+
1151+
do {
1152+
if (it6505_read(it6505, REG_AUX_USER_CTRL) & AUX_EVENT)
1153+
break;
1154+
if (time_after(jiffies, timeout)) {
1155+
dev_err(dev, "Timed out waiting AUX I2C, BUSY = %X\n",
1156+
it6505_aux_op_finished(it6505));
1157+
err = -ETIMEDOUT;
1158+
goto end_aux_i2c_wait;
1159+
}
1160+
usleep_range(300, 800);
1161+
} while (!it6505_aux_op_finished(it6505));
1162+
1163+
*reply = it6505_read(it6505, REG_AUX_USER_REPLY) >> 4;
1164+
1165+
if (*reply == 0)
1166+
goto end_aux_i2c_wait;
1167+
1168+
if (it6505_aux_i2c_reply_defer(*reply))
1169+
err = -EBUSY;
1170+
else if (it6505_aux_i2c_reply_nack(*reply))
1171+
err = -ENXIO;
1172+
1173+
end_aux_i2c_wait:
1174+
it6505_set_bits(it6505, REG_AUX_USER_CTRL, USER_AUX_DONE, USER_AUX_DONE);
1175+
return err;
1176+
}
1177+
1178+
static int it6505_aux_i2c_readb(struct it6505 *it6505, u8 *buf, size_t size, u8 *reply)
1179+
{
1180+
int ret, i;
1181+
int retry;
1182+
1183+
for (retry = 0; retry < AUX_I2C_DEFER_RETRY; retry++) {
1184+
it6505_write(it6505, REG_AUX_CMD_REQ, CMD_AUX_GI2C_READ);
1185+
1186+
ret = it6505_aux_i2c_wait(it6505, reply);
1187+
if (it6505_aux_i2c_reply_defer(*reply))
1188+
continue;
1189+
if (ret >= 0)
1190+
break;
1191+
}
1192+
1193+
for (i = 0; i < size; i++)
1194+
buf[i] = it6505_read(it6505, REG_AUX_USER_RXB(0 + i));
1195+
1196+
return size;
1197+
}
1198+
1199+
static int it6505_aux_i2c_writeb(struct it6505 *it6505, u8 *buf, size_t size, u8 *reply)
1200+
{
1201+
int i, ret;
1202+
int retry;
1203+
1204+
for (i = 0; i < size; i++)
1205+
it6505_write(it6505, REG_AUX_OUT_DATA0 + i, buf[i]);
1206+
1207+
for (retry = 0; retry < AUX_I2C_DEFER_RETRY; retry++) {
1208+
it6505_write(it6505, REG_AUX_CMD_REQ, CMD_AUX_GI2C_WRITE);
1209+
1210+
ret = it6505_aux_i2c_wait(it6505, reply);
1211+
if (it6505_aux_i2c_reply_defer(*reply))
1212+
continue;
1213+
if (ret >= 0)
1214+
break;
1215+
}
1216+
return size;
1217+
}
1218+
1219+
static ssize_t it6505_aux_i2c_operation(struct it6505 *it6505,
1220+
struct drm_dp_aux_msg *msg)
1221+
{
1222+
int ret;
1223+
ssize_t request_size, data_cnt = 0;
1224+
u8 *buffer = msg->buffer;
1225+
1226+
/* set AUX user mode */
1227+
it6505_set_bits(it6505, REG_AUX_CTRL,
1228+
AUX_USER_MODE | AUX_NO_SEGMENT_WR, AUX_USER_MODE);
1229+
it6505_set_bits(it6505, REG_AUX_USER_CTRL, EN_USER_AUX, EN_USER_AUX);
1230+
/* clear AUX FIFO */
1231+
it6505_set_bits(it6505, REG_AUX_CTRL,
1232+
AUX_EN_FIFO_READ | CLR_EDID_FIFO,
1233+
AUX_EN_FIFO_READ | CLR_EDID_FIFO);
1234+
1235+
it6505_set_bits(it6505, REG_AUX_CTRL,
1236+
AUX_EN_FIFO_READ | CLR_EDID_FIFO, 0x00);
1237+
1238+
it6505_write(it6505, REG_AUX_ADR_0_7, 0x00);
1239+
it6505_write(it6505, REG_AUX_ADR_8_15, msg->address << 1);
1240+
1241+
if (msg->size == 0) {
1242+
/* IIC Start/STOP dummy write */
1243+
it6505_write(it6505, REG_AUX_ADR_16_19, msg->request);
1244+
it6505_write(it6505, REG_AUX_CMD_REQ, CMD_AUX_GI2C_ADR);
1245+
ret = it6505_aux_i2c_wait(it6505, &msg->reply);
1246+
goto end_aux_i2c_transfer;
1247+
}
1248+
1249+
/* IIC data transfer */
1250+
data_cnt = 0;
1251+
do {
1252+
request_size = min_t(ssize_t, msg->size - data_cnt, AUX_I2C_MAX_SIZE);
1253+
it6505_write(it6505, REG_AUX_ADR_16_19,
1254+
msg->request | ((request_size - 1) << 4));
1255+
if ((msg->request & DP_AUX_I2C_READ) == DP_AUX_I2C_READ)
1256+
ret = it6505_aux_i2c_readb(it6505, &buffer[data_cnt],
1257+
request_size, &msg->reply);
1258+
else
1259+
ret = it6505_aux_i2c_writeb(it6505, &buffer[data_cnt],
1260+
request_size, &msg->reply);
1261+
1262+
if (ret < 0)
1263+
goto end_aux_i2c_transfer;
1264+
1265+
data_cnt += request_size;
1266+
} while (data_cnt < msg->size);
1267+
ret = data_cnt;
1268+
end_aux_i2c_transfer:
1269+
1270+
it6505_set_bits(it6505, REG_AUX_USER_CTRL, EN_USER_AUX, 0);
1271+
it6505_set_bits(it6505, REG_AUX_CTRL, AUX_USER_MODE, 0);
1272+
return ret;
1273+
}
1274+
1275+
static ssize_t it6505_aux_i2c_transfer(struct drm_dp_aux *aux,
1276+
struct drm_dp_aux_msg *msg)
1277+
{
1278+
struct it6505 *it6505 = container_of(aux, struct it6505, aux);
1279+
1280+
guard(mutex)(&it6505->aux_lock);
1281+
return it6505_aux_i2c_operation(it6505, msg);
1282+
}
1283+
11101284
static ssize_t it6505_aux_transfer(struct drm_dp_aux *aux,
11111285
struct drm_dp_aux_msg *msg)
11121286
{
@@ -1116,9 +1290,8 @@ static ssize_t it6505_aux_transfer(struct drm_dp_aux *aux,
11161290
int ret;
11171291
enum aux_cmd_reply reply;
11181292

1119-
/* IT6505 doesn't support arbitrary I2C read / write. */
11201293
if (is_i2c)
1121-
return -EINVAL;
1294+
return it6505_aux_i2c_transfer(aux, msg);
11221295

11231296
switch (msg->request) {
11241297
case DP_AUX_NATIVE_READ:

0 commit comments

Comments
 (0)