From nobody Sun Nov 24 18:38:36 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 4E4BA18E02E for ; Mon, 4 Nov 2024 06:44:07 +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=1730702647; cv=none; b=B+RTwHNIG+NhNAXFJPGS9GPcw+WgT2gfICnvLI4a2BA1fBE1nI9gEpyOJ0yop50fEGNNqraTIdLIv9BtlKZxmUElRaQ58O/urZSw8t2zggl5CGE8tQEIyqZqXknXvexTkaFYjFP+ungRlfpO1EIYN+MkBr4tdzjuZdqqkGVNrK4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730702647; c=relaxed/simple; bh=NJj0K2gdXXAJLf7drNqE5H8SvCozOqHpTpJbbKTSkmI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Lf4pEwJm3QaHwxn1VN0dTe+FUSV864MaqFp+Y0wrlLOa4TX4+V/wFtPZXmmWOXQAGL0P60Q6OV4bfabMpOJUYyswWU6LhHcfTwM9Y4fco/0hqV267bNxGggmZK8Tu6UjnE9tM/cuOtAIKLMqzlQ/hwLoOdyNQvQZv8VSw3FiVjA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fra3msms; 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="fra3msms" Received: by smtp.kernel.org (Postfix) with ESMTPS id 24523C4CEDA; Mon, 4 Nov 2024 06:44:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730702647; bh=NJj0K2gdXXAJLf7drNqE5H8SvCozOqHpTpJbbKTSkmI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=fra3msms5YdBzc2coae7HhaOu3fDjRwCIzlBJoxtmGfYt5WDe16E7QAicWS7Njm65 WT5BF7NwKwsSypWlLvBZs8XUkCatqxFTZJ8X6H2ExGuefMXJ3O/GtCoTEeqdEwgzZc Bl0fwtIqrJqXkLGbszTqZXMlb44dzj22ZLwfug+NAcMmyj9FQlVfVLM8gNYdHOQWIa ahHTlslmNHPHRyiBKg3keTd4vfhMueZ5GvMXeDUPgiD42zXuZ7dgXGa016S+wBMxpU qtOpak3NZddM3M1VGh+vRaAs7Z2zbzSxPq4obP7/vc+tL4xSEcUqv/X2/mu6Htjhtq jzcXqZ4JAeHUg== 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 197F0D111B3; Mon, 4 Nov 2024 06:44:07 +0000 (UTC) From: Hermes Wu via B4 Relay Date: Mon, 04 Nov 2024 14:43:40 +0800 Subject: [PATCH v7 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: <20241104-v7-upstream-v7-10-8b71fd0f1d2d@ite.com.tw> References: <20241104-v7-upstream-v7-0-8b71fd0f1d2d@ite.com.tw> In-Reply-To: <20241104-v7-upstream-v7-0-8b71fd0f1d2d@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 , Hermes Wu Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Pin-yen Lin , Kenneth Hung , Pet Weng , Hermes Wu , Dmitry Baryshkov X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1730702658; l=6930; i=Hermes.wu@ite.com.tw; s=20241101; h=from:subject:message-id; bh=J4rvU2IuQU84l+7pDQWlWwKvnpve8QmHuGwdiA2w++k=; b=AvxiQg6SF9UmzrBagdxvaSHUrGPFNZNhgSe/rzzrwOSkZmbuqwfgxHxzqsYsAEJwGO8Uv/NbS UQZQjPaYXp/CEOC20KyTlUV1H7l/WxszbWNuMqd4YcRJV2CHFE2q5Yt X-Developer-Key: i=Hermes.wu@ite.com.tw; a=ed25519; pk=FOYYbsP2Nlw6mjB3rLFYSLmAiENzj4AWQly5XTcDuMM= X-Endpoint-Received: by B4 Relay for Hermes.wu@ite.com.tw/20241101 with auth_id=268 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(). Reviewed-by: Dmitry Baryshkov Signed-off-by: Hermes Wu --- 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 83e2df618c97f8fe3e59789ffb810ce8639f96bb..c2a846f5d8d9b7251913c82d21f= 7957401e35e5e 100644 --- a/drivers/gpu/drm/bridge/ite-it6505.c +++ b/drivers/gpu/drm/bridge/ite-it6505.c @@ -268,6 +268,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 @@ -303,6 +315,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 @@ -325,7 +339,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, @@ -1107,6 +1126,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) { @@ -1116,9 +1290,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