From nobody Tue Nov 26 22:40:00 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 30B741D1E65 for ; Wed, 16 Oct 2024 07:54:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729065253; cv=none; b=Ws9ImslWm+tikDDtls7NVY43NpTeuPL1aF9lz4cLz7uDgZPsmfr+Pehtvi3cHbLVITj5ywaIazCBC/vGVuFkRFY9ta6OyIYm+SCKMGvAZnPmXOyvJ0KXDQCVm1mIZm45zJeqiBkAGhQm29HXIW2PlZEnLm9fWD7ONHEr8AIhtgU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729065253; c=relaxed/simple; bh=6GLNfhT3dwFCU0ldrvAxlBEYAVhF9inTvSCRJuALhUM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=OnkhYIH3yvwotxV3IjUBpFG5yDM5MR4SbjwHYuDHv19Cdf2bMIOyxICAXdGp9/j7Bmx87S3CsGiJMpw4ZYXTBWb4IQ9L3j8hFfAab/0MF4S0Uevxqvn7zCK0h/MAnSj/rVg1SsFXtiyQxIDCOFZWYMChKZDQvRHNPbFuror89Ao= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RcubLegV; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="RcubLegV" Received: by smtp.kernel.org (Postfix) with ESMTPS id 0ED2EC4CEE1; Wed, 16 Oct 2024 07:54:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1729065253; bh=6GLNfhT3dwFCU0ldrvAxlBEYAVhF9inTvSCRJuALhUM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=RcubLegV69nSYpNMFnLbBsCv8k9HLfxrkW9RxkM5SGKPouHMPfq6VY6FlriMWQ2f+ WIao5bt1lDyzAOlGXv4wAw6OgJ2+Nidv3dLQ+ZGXcUZ595d8C32TxejRDslvtsqlcA wTp3yxXYw3UwIok4HUz8ycO2uZrYgv44VchaTx6fzveYjLEtfcTF8ZJxp/wRz2b89w UlLFy4c8iHlUmVGVvLI7B7AW7j7ljqkDckJNiP3avA//llOFkOPfiz76aE6tepgQjT vro+McEqbRPs30ytrXJpLW79OTWLY8IPlEokqULOKX27aIkgMTb5SGBIJKptcon8ry 475zkvpmd2jmQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0774BD20684; Wed, 16 Oct 2024 07:54:13 +0000 (UTC) From: Hermes Wu via B4 Relay Date: Wed, 16 Oct 2024 15:54:22 +0800 Subject: [PATCH v6 10/10] drm/bridge: it6505: add I2C functionality on AUX Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241016-upstream-v6-v6-10-4d93a0c46de1@ite.com.tw> References: <20241016-upstream-v6-v6-0-4d93a0c46de1@ite.com.tw> In-Reply-To: <20241016-upstream-v6-v6-0-4d93a0c46de1@ite.com.tw> To: Andrzej Hajda , Neil Armstrong , Robert Foss , Laurent Pinchart , Jonas Karlman , Jernej Skrabec , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , AngeloGioacchino Del Regno , Pin-yen Lin Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Kenneth Hung , Pet Weng , Hermes Wu X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1729065263; l=6869; i=Hermes.wu@ite.com.tw; s=20241016; h=from:subject:message-id; bh=gC24eYRJHnPLzseZNt75kdykjMNImvebHR8wNIWmjqI=; b=/kQxXDYIYkHFaB4DOWo4CSEL55oW2OyDK3DQrzHNVSJCp0K1DYMGJGZJj/9YfmCW5kfLW1v9c MPetwk5YNYxA6Pidpt1t3vCqVBqFiEV+yg5TJ2I5ixWB8Yr7e7zjvmQ X-Developer-Key: i=Hermes.wu@ite.com.tw; a=ed25519; pk=kV18uQEXox+1WccdQceCbMlJKN+BBNUk3N8VsoULcQ0= X-Endpoint-Received: by B4 Relay for Hermes.wu@ite.com.tw/20241016 with auth_id=250 X-Original-From: Hermes Wu Reply-To: Hermes.wu@ite.com.tw From: Hermes Wu 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(). Signed-off-by: Hermes Wu Reviewed-by: Dmitry Baryshkov --- drivers/gpu/drm/bridge/ite-it6505.c | 177 ++++++++++++++++++++++++++++++++= +++- 1 file changed, 175 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/bridge/ite-it6505.c b/drivers/gpu/drm/bridge/i= te-it6505.c index 4fc2147e9788501f0161180fc865072ab965ecbf..04e17737f0d7228e5178984aefe= 124f88bce0825 100644 --- a/drivers/gpu/drm/bridge/ite-it6505.c +++ b/drivers/gpu/drm/bridge/ite-it6505.c @@ -267,6 +267,18 @@ #define REG_SSC_CTRL1 0x189 #define REG_SSC_CTRL2 0x18A =20 +#define REG_AUX_USER_CTRL 0x190 +#define EN_USER_AUX BIT(0) +#define USER_AUX_DONE BIT(1) +#define AUX_EVENT BIT(4) + +#define REG_AUX_USER_DATA_REC 0x191 +#define M_AUX_IN_REC 0xF0 +#define M_AUX_OUT_REC 0x0F + +#define REG_AUX_USER_REPLY 0x19A +#define REG_AUX_USER_RXB(n) (n + 0x19B) + #define RBR DP_LINK_BW_1_62 #define HBR DP_LINK_BW_2_7 #define HBR2 DP_LINK_BW_5_4 @@ -302,6 +314,8 @@ #define MAX_EQ_LEVEL 0x03 #define AUX_WAIT_TIMEOUT_MS 15 #define AUX_FIFO_MAX_SIZE 16 +#define AUX_I2C_MAX_SIZE 4 +#define AUX_I2C_DEFER_RETRY 4 #define PIXEL_CLK_DELAY 1 #define PIXEL_CLK_INVERSE 0 #define ADJUST_PHASE_THRESHOLD 80000 @@ -324,7 +338,12 @@ enum aux_cmd_type { CMD_AUX_NATIVE_READ =3D 0x0, CMD_AUX_NATIVE_WRITE =3D 0x5, + CMD_AUX_GI2C_ADR =3D 0x08, + CMD_AUX_GI2C_READ =3D 0x09, + CMD_AUX_GI2C_WRITE =3D 0x0A, CMD_AUX_I2C_EDID_READ =3D 0xB, + CMD_AUX_I2C_READ =3D 0x0D, + CMD_AUX_I2C_WRITE =3D 0x0C, =20 /* KSV read with AUX FIFO extend from CMD_AUX_NATIVE_READ*/ CMD_AUX_GET_KSV_LIST =3D 0x10, @@ -1106,6 +1125,161 @@ static ssize_t it6505_aux_do_transfer(struct it6505= *it6505, return ret; } =20 +static bool it6505_aux_i2c_reply_defer(u8 reply) +{ + if (reply =3D=3D DP_AUX_NATIVE_REPLY_DEFER || reply =3D=3D DP_AUX_I2C_REP= LY_DEFER) + return true; + return false; +} + +static bool it6505_aux_i2c_reply_nack(u8 reply) +{ + if (reply =3D=3D DP_AUX_NATIVE_REPLY_NACK || reply =3D=3D DP_AUX_I2C_REPL= Y_NACK) + return true; + return false; +} + +static int it6505_aux_i2c_wait(struct it6505 *it6505, u8 *reply) +{ + int err =3D 0; + unsigned long timeout; + struct device *dev =3D it6505->dev; + + timeout =3D jiffies + msecs_to_jiffies(AUX_WAIT_TIMEOUT_MS) + 1; + + do { + if (it6505_read(it6505, REG_AUX_USER_CTRL) & AUX_EVENT) + break; + if (time_after(jiffies, timeout)) { + dev_err(dev, "Timed out waiting AUX I2C, BUSY =3D %X\n", + it6505_aux_op_finished(it6505)); + err =3D -ETIMEDOUT; + goto end_aux_i2c_wait; + } + usleep_range(300, 800); + } while (!it6505_aux_op_finished(it6505)); + + *reply =3D it6505_read(it6505, REG_AUX_USER_REPLY) >> 4; + + if (*reply =3D=3D 0) + goto end_aux_i2c_wait; + + if (it6505_aux_i2c_reply_defer(*reply)) + err =3D -EBUSY; + else if (it6505_aux_i2c_reply_nack(*reply)) + err =3D -ENXIO; + +end_aux_i2c_wait: + it6505_set_bits(it6505, REG_AUX_USER_CTRL, USER_AUX_DONE, USER_AUX_DONE); + return err; +} + +static int it6505_aux_i2c_readb(struct it6505 *it6505, u8 *buf, size_t siz= e, u8 *reply) +{ + int ret, i; + int retry; + + for (retry =3D 0; retry < AUX_I2C_DEFER_RETRY; retry++) { + it6505_write(it6505, REG_AUX_CMD_REQ, CMD_AUX_GI2C_READ); + + ret =3D it6505_aux_i2c_wait(it6505, reply); + if (it6505_aux_i2c_reply_defer(*reply)) + continue; + if (ret >=3D 0) + break; + } + + for (i =3D 0; i < size; i++) + buf[i] =3D it6505_read(it6505, REG_AUX_USER_RXB(0 + i)); + + return size; +} + +static int it6505_aux_i2c_writeb(struct it6505 *it6505, u8 *buf, size_t si= ze, u8 *reply) +{ + int i, ret; + int retry; + + for (i =3D 0; i < size; i++) + it6505_write(it6505, REG_AUX_OUT_DATA0 + i, buf[i]); + + for (retry =3D 0; retry < AUX_I2C_DEFER_RETRY; retry++) { + it6505_write(it6505, REG_AUX_CMD_REQ, CMD_AUX_GI2C_WRITE); + + ret =3D it6505_aux_i2c_wait(it6505, reply); + if (it6505_aux_i2c_reply_defer(*reply)) + continue; + if (ret >=3D 0) + break; + } + return size; +} + +static ssize_t it6505_aux_i2c_operation(struct it6505 *it6505, + struct drm_dp_aux_msg *msg) +{ + int ret; + ssize_t request_size, data_cnt =3D 0; + u8 *buffer =3D msg->buffer; + + /* set AUX user mode */ + it6505_set_bits(it6505, REG_AUX_CTRL, + AUX_USER_MODE | AUX_NO_SEGMENT_WR, AUX_USER_MODE); + it6505_set_bits(it6505, REG_AUX_USER_CTRL, EN_USER_AUX, EN_USER_AUX); + /* clear AUX FIFO */ + it6505_set_bits(it6505, REG_AUX_CTRL, + AUX_EN_FIFO_READ | CLR_EDID_FIFO, + AUX_EN_FIFO_READ | CLR_EDID_FIFO); + + it6505_set_bits(it6505, REG_AUX_CTRL, + AUX_EN_FIFO_READ | CLR_EDID_FIFO, 0x00); + + it6505_write(it6505, REG_AUX_ADR_0_7, 0x00); + it6505_write(it6505, REG_AUX_ADR_8_15, msg->address << 1); + + if (msg->size =3D=3D 0) { + /* IIC Start/STOP dummy write */ + it6505_write(it6505, REG_AUX_ADR_16_19, msg->request); + it6505_write(it6505, REG_AUX_CMD_REQ, CMD_AUX_GI2C_ADR); + ret =3D it6505_aux_i2c_wait(it6505, &msg->reply); + goto end_aux_i2c_transfer; + } + + /* IIC data transfer */ + data_cnt =3D 0; + do { + request_size =3D min_t(ssize_t, msg->size - data_cnt, AUX_I2C_MAX_SIZE); + it6505_write(it6505, REG_AUX_ADR_16_19, + msg->request | ((request_size - 1) << 4)); + if ((msg->request & DP_AUX_I2C_READ) =3D=3D DP_AUX_I2C_READ) + ret =3D it6505_aux_i2c_readb(it6505, &buffer[data_cnt], + request_size, &msg->reply); + else + ret =3D it6505_aux_i2c_writeb(it6505, &buffer[data_cnt], + request_size, &msg->reply); + + if (ret < 0) + goto end_aux_i2c_transfer; + + data_cnt +=3D request_size; + } while (data_cnt < msg->size); + ret =3D data_cnt; +end_aux_i2c_transfer: + + it6505_set_bits(it6505, REG_AUX_USER_CTRL, EN_USER_AUX, 0); + it6505_set_bits(it6505, REG_AUX_CTRL, AUX_USER_MODE, 0); + return ret; +} + +static ssize_t it6505_aux_i2c_transfer(struct drm_dp_aux *aux, + struct drm_dp_aux_msg *msg) +{ + struct it6505 *it6505 =3D container_of(aux, struct it6505, aux); + + guard(mutex)(&it6505->aux_lock); + return it6505_aux_i2c_operation(it6505, msg); +} + static ssize_t it6505_aux_transfer(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg) { @@ -1115,9 +1289,8 @@ static ssize_t it6505_aux_transfer(struct drm_dp_aux = *aux, int ret; enum aux_cmd_reply reply; =20 - /* IT6505 doesn't support arbitrary I2C read / write. */ if (is_i2c) - return -EINVAL; + return it6505_aux_i2c_transfer(aux, msg); =20 switch (msg->request) { case DP_AUX_NATIVE_READ: --=20 2.34.1