From nobody Tue Nov 24 11:17:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795410662; b=jHiZ4bn0ckCGxXXaU2RlulRRZ8nF1zO+U3c2vKungaXkK0Xg+4IffODZunexMJNjqRdf qW3hd7MpG7tLQSK9QlTMsbUTHjPYhCaVQsIqxMGU64AcDQAaXrkVx7nSLx328wlaZNT9L 8yEZo8iK1Pt/vf5VExkDvWITyvuHlArv52sOeDOrTxiPTtX2CNI+ATGE5zf0Is4x9D7da z11DSxDEO+9EJOvInD/yRIc+oibV0B3yg/9giKsgQvwTacZp0Hoc9D6LUgNBvydGlEKVV ungnPKkwaY6szPQW23cAHwHeXOZhKfx5TBk5QCySatRncZf18IBn3O55tRT+5XMK7xQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795410662; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=iesDrvecprqj1brWwbrs25SuyK5gHClIHsr42f7gvQs=; b=pvGpEXMajxeZA3JL67avFfFeqIpNwfGOfDp+ftzdHqegJnvSk06GR/BQYijerlIsVB/Z iLF9LyKaEK0PnMDQQ2KrRQAQsyW3aKz0NQTkPR0bplBXIfhIoRffVCkitN6BeGCE0NBBW G1trKO7XS1pZXHHOUELVNHi2GfR2RuPuPeCP+iOvMpIVu5cbR4ONrlxZM9wFwt7tttEvZ uWGzccNO+ODifi1W5dKBDOweSP36OITbhXlBoBWVkfDSD1Vt996j2pUAh4LJi0Z+aHzLy t4oQjgQzgi5sqzUvArYvhb1sRJ4utlnTI+lRPXWeUA+Y0ZTTVlw4DCxOHTuqDWxPTkQ== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=HsQVSx6m; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 43C16606A338; Mon, 23 Nov 2026 05:11:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=HsQVSx6m Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795410661; b=Lzw/bJ6T+7NA+WS7RqIu6IyfzrC1H6aNHCWD/sbS9zYODui4qHeGULtQwAOAXSiAQ+gx l7yvovsD0o97mwJVJ+pDFm5m4JFRbF8g6sD9gtwW3RixU6UPu0YyxHCQ5YamheSAqI0Hx 9+r9fOvmXteWp0+hJlSKRvAGTmRZfi6UF/37XVcFhTe6P8ObDz8EXWi7/IvuaUalxFHd5 fdQ39g/VAM2oE4vNT8OmXFqedrV4AI5lenioMqpL9COcEAxtOxGOd5YiDLQViEohOoRed emTsrQ0Q2OdouX36UcxkHXoSo1m2ZrNpLadAd7tCQVeFmFoyevrei+Z7VM6aHd2YHjw== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795410661; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=iesDrvecprqj1brWwbrs25SuyK5gHClIHsr42f7gvQs=; b=ZHQ03uCNHALTfFGI1X+ePwEsZnZpa9ROUqn+KpJhKmd3W9u77ALGJgH5z3HCQELUrnVB 4P3ML+5NAVTuhTeeh6ro+Xze2D87+VmJesl+hhbVAJl323CvJ4zfUIennGW4nRTIoD61R WYNxJLoAxSG4EvELFJoKU4SasgSgZedi8qtRznHEi3YT/dyUmjAuTSeZ+9KksL8SqKDZI Z0SPqrltLi0aqoylpNkLayDLLt5CFKHaLmn/IY2IS7EECof3cQGUszysbg9j62JJJo9BX 5ep9Y1zfecIZvDe3XRtMfAK6vCtmshG3MglN7aT5Lm5U6XynWuNKNxrWH4Wdn0FPkiA== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id B7D4D1F000CA; Mon, 23 Nov 2026 05:10:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795410661; bh=iesDrvecprqj1brWwbrs25SuyK5gHClIHsr42f7gvQs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=HsQVSx6m1V3MPRgyoZyGje/pUry1BcWFAxJ9q9XIv+/STLiA2aw3AgDfMN7zuk8MP eqirJz+EBO9ncVD729HmQFQigOKKKhtQzS6QLBg+PUZTwn54UFyEstKSOyzOFZ6Bou geSqD9crO02w8dig+XweESTu0riSclmPaAdpP8wOaXAQnD1P3KZcwyeZfATVzxoLOS syc6SU/hZOwNhrbMyKi0ooNNTlDT6k9SrLatf8YRPJCZW9G3nD46qGd+EfjwtFo+YK w0VNKmabz4NiCkRfMwhyyslieaJdREVfdAwPCb+DRT9IY3+XbElEf2+4A67TdQIQqQ oehCYc/f9lORQ== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 3F0C44C35A; Mon, 23 Nov 2026 05:10:53 +0000 (UTC) From: Fil Dunsky via B4 Relay Date: Mon, 23 Nov 2026 08:10:37 +0300 Subject: [PATCH v7 1/2] dt-bindings: i2c: Add Cortina Systems CS7542 I2C controller X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261123-cs75xx-i2c-v7-1-42080ecf30be@gmail.com> References: <20261123-cs75xx-i2c-v7-0-42080ecf30be@gmail.com> In-Reply-To: <20261123-cs75xx-i2c-v7-0-42080ecf30be@gmail.com> To: Andi Shyti , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: linux-i2c@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Fil Dunsky X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795410652; l=1877; i=filipp.dunsky@gmail.com; s=20260930; h=from:subject:message-id; bh=Mzt932NWHbMP6cNTGXOVF/25Jv1slV/2vvPLoA2toCU=; b=2tl3xJcvvYkzaLDpD6OPx3Kioh7YkbIeIFOrypeoevgitlqd3X5WID8y+HaPvOT/G6eVEjdPb Q2aEwENKPmiCQr4FNp82Gm37XOQQ0HN4NTPb15txPYxPx14v+MqOaMC X-Developer-Key: i=filipp.dunsky@gmail.com; a=ed25519; pk=JAYP8e2rJ54DHOmhNtXud8rl1igAaAXoOHxtJmVXI1g= X-Endpoint-Received: by B4 Relay for filipp.dunsky@gmail.com/20260930 with auth_id=1085 X-Original-From: Fil Dunsky Reply-To: filipp.dunsky@gmail.com Content-Transfer-Encoding: quoted-printable From: Fil Dunsky Add a binding for the I2C ("Bi-Wire") controller of the Cortina Systems CS75xx SoCs. Reviewed-by: Rob Herring (Arm) Signed-off-by: Fil Dunsky --- .../bindings/i2c/cortina,cs7542-i2c.yaml | 56 ++++++++++++++++++= ++++ 1 file changed, 56 insertions(+) diff --git a/Documentation/devicetree/bindings/i2c/cortina,cs7542-i2c.yaml = b/Documentation/devicetree/bindings/i2c/cortina,cs7542-i2c.yaml new file mode 100644 index 000000000..ef31284c1 --- /dev/null +++ b/Documentation/devicetree/bindings/i2c/cortina,cs7542-i2c.yaml @@ -0,0 +1,56 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/i2c/cortina,cs7542-i2c.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Cortina Systems CS7542 I2C controller + +maintainers: + - Fil Dunsky + +description: + The I2C ("Bi-Wire") controller of the Cortina Systems CS75xx (Goldengate + G2) SoCs. It transfers one byte per command and runs from the peripheral + (APB) clock through a 16-bit prescaler. + +allOf: + - $ref: /schemas/i2c/i2c-controller.yaml# + +properties: + compatible: + const: cortina,cs7542-i2c + + reg: + maxItems: 1 + + clocks: + maxItems: 1 + + clock-frequency: + maximum: 400000 + default: 100000 + + interrupts: + maxItems: 1 + +required: + - compatible + - reg + - clocks + - interrupts + +unevaluatedProperties: false + +examples: + - | + i2c@f00700e8 { + compatible =3D "cortina,cs7542-i2c"; + reg =3D <0xf00700e8 0x28>; + clocks =3D <&apb_clk>; + clock-frequency =3D <400000>; + interrupt-parent =3D <&intc>; + interrupts =3D <11>; + #address-cells =3D <1>; + #size-cells =3D <0>; + }; --=20 2.53.0 From nobody Tue Nov 24 11:17:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795410665; b=CiJHXLn9/k/eT7TgdgppIwa1iI4SAOUmAAr9RyBJIpnJnK6APiUnRQAI1Hv7gA1lUY1F KJ9obmdV4LBwH9HzcAIE8wtgIXixZHK3FZdKT1X28z6ldNitxnXVBUJ1wwGYiaIXPg1pq 9h7RsmSdKrE5YJGNjCSH/sUXeV5LLvVyAnmJMvHQjzFPljUo+BVlMcLN/qEAaW87f/hxq KzqKOmfEdQvAKU3Sy9lKjCVc1htCHrkKcU6Kxj5PFL5GEl/OuDZq2X1FjW0tWH005Qh0z O3/U0QuPiFh7qSwRPXFD71eXiu2jWYUkDKlZRVJVF3BebNzcta+GtCgl0U6a0b6sOjg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795410665; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=3FwG7mX5zlZBqN+BGxKHGoAGEMqnQOwJV3TjX51GPvs=; b=fwWegImJoj81yXZ/6J6M6uLBGzE19EO3yZzA5YIkuqPVHrxuQOFGIRt8fIxoM6ZPAOPF S9V/YB3NMMB2q5u/yy69Rm6Gzi7BV4x3yvm1wWizWXstRlCWXX6KNz9/yxHJj5lTHoFcQ DmGOdUXm7+Hp8ssOZiMikc8yykDyT10+F3W/9xYNaTrvZ6Pd6UiUet0hEsAU9eC2QiFTh NkcJI5Qt0YtcW9O/T1GyOVS6611gMFFLNgQ3wbtvWQGr1YkGxQg1fmQMSxSJzfP0CDgu9 C1ieGvSulIglwY06inF15UUQsPP9zQ06L6oBbWOqt0BMCCQOZSpEk80r/6JNAroLwNA== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=KP63BvjH; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9DD80606A333; Mon, 23 Nov 2026 05:11:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=KP63BvjH Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795410664; b=yTIl4k27q995keLc3/fuyrTowB62OfJXNb+EfuurKLigdb122GT3rFsKU9By5EMlL4s9 v+x1kPTY3Mvd6/i74+utairqu6OobCrHod+P+XKc5gxSOQDr1u37hBlWSDfgSmEWYb3mr t8msTcXLHw0sYT6WxZ3MxQZx1+kicKKp1n4ThcotZxlB9zvyo19RPb6JqyTKEENPC+amD bCn3CiTe/fidqh1MmlEwq/vx8lWKoFSlGT2pzsWkEaOb00on3plBGE0zd+EFiZmjAQoTs wI2QIyyN597aaglujlO9ryGp5se/Ng1ekqLf0ytOKBbu+6sx7ut+V6EXBEEkI39z3yQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795410664; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=3FwG7mX5zlZBqN+BGxKHGoAGEMqnQOwJV3TjX51GPvs=; b=lExkYyQnaam2cJQDMCoI0fPYd5NZ3r5tcFt2Nv56VeMm6u+gry29j7RhfoDWuoeqM/NH nq72F13sKxlIlq8f5voZIHTeYGsEL2gccIgOAsQ5K3AivNaMysd+k+j48saplUx36S04g dqilpgyWpPJUxdVVCezmiBYT34hYx5zRLUsE2afIYa7GUrc7AVTHGLzz5ucAE5tdxs2yj lkcUcUuFS1LshqiKB8uc8fauFW9NHZ3wOxWJBsB/d+6LhPP8CntMBxCDoB/yhwVo9okFV ndD/yMosViBrPwK307x2o421ifdS1A3gRMPD4iGCswaXKOGZp67bbRliLUQ18xlTy5g== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id C72F31F000CF; Mon, 23 Nov 2026 05:10:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795410664; bh=3FwG7mX5zlZBqN+BGxKHGoAGEMqnQOwJV3TjX51GPvs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=KP63BvjHf0YBK0Fcc7lzjNWSvX/8dZiUozqECZRrSXawCeebUj6jauPNdD3nAQ0zc s0t/DYdNzH808vH5CnY+7xKNBQroA9EetnPGliHgisTssi8heZns5+xVhFZ8d8m3to cqaePADfeLev+JyqfgPBWeWeCBkM32TfgmKzpJ3EQLZU2y1ohzTWyX2Gyj6OjFNCQq SvKr3WvksnEqB40r4j1mqd8M3DFX8cwIiLOx0AIDnYfUmJSnOCKyH98XWc9tT6NDxC /JQPjEvjos8bWxeEEHm8vuniSTnqiObjADfDjpXWQWz0LZPtfOHt2cRkfbjr3Bfx1l iuoZ8zktYFmUQ== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 57F2B4C35E; Mon, 23 Nov 2026 05:10:53 +0000 (UTC) From: Fil Dunsky via B4 Relay Date: Mon, 23 Nov 2026 08:10:38 +0300 Subject: [PATCH v7 2/2] i2c: Add Cortina Systems CS75xx I2C controller driver X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261123-cs75xx-i2c-v7-2-42080ecf30be@gmail.com> References: <20261123-cs75xx-i2c-v7-0-42080ecf30be@gmail.com> In-Reply-To: <20261123-cs75xx-i2c-v7-0-42080ecf30be@gmail.com> To: Andi Shyti , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: linux-i2c@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Fil Dunsky X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795410652; l=12869; i=filipp.dunsky@gmail.com; s=20260930; h=from:subject:message-id; bh=sdKb23F6aWeqpJLbpvhACtdkiZ/sWmFtTwhFjE/scGw=; b=2cbYG26WZmVvSoqtFUmipZaF4DqMFI7hCBmauOWX3iqqBSyrx99CuoRiQiN86WENh/YQbVWPT V+HOk959X/5AM/nqyZwR38//YRLE9sr+iv1m4rfPnLfBvnf1FGAMr7T X-Developer-Key: i=filipp.dunsky@gmail.com; a=ed25519; pk=JAYP8e2rJ54DHOmhNtXud8rl1igAaAXoOHxtJmVXI1g= X-Endpoint-Received: by B4 Relay for filipp.dunsky@gmail.com/20260930 with auth_id=1085 X-Original-From: Fil Dunsky Reply-To: filipp.dunsky@gmail.com Content-Transfer-Encoding: quoted-printable From: Fil Dunsky The I2C ("Bi-Wire") controller of the Cortina Systems CS75xx SoCs transfers one byte per command, with a command register much like the OpenCores one (start, stop, read, write, acknowledge), and raises an interrupt when a command has completed. Messages are sent with a repeated start between them and a stop after the last one. Every message needs at least one data byte, so SMBus quick commands are not supported. Arbitration loss is reported as -EAGAIN and retried by the core. The driver depends on OF and HAS_IOMEM and uses generic platform and I2C APIs, so it can be enabled without the CS75xx platform series. Signed-off-by: Fil Dunsky --- drivers/i2c/busses/Kconfig | 11 ++ drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-cs75xx.c | 393 ++++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 405 insertions(+) diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig index 275aaa7f3..1bebffc7d 100644 --- a/drivers/i2c/busses/Kconfig +++ b/drivers/i2c/busses/Kconfig @@ -550,6 +550,17 @@ config I2C_CPM This driver can also be built as a module. If so, the module will be called i2c-cpm. =20 +config I2C_CS75XX + tristate "Cortina Systems CS75xx I2C controller" + depends on OF + depends on HAS_IOMEM + help + Say yes here to support the I2C ("Bi-Wire") controller of the + Cortina Systems CS75xx SoCs. + + This driver can also be built as a module. If so, the module + will be called i2c-cs75xx. + config I2C_DAVINCI tristate "DaVinci I2C driver" depends on ARCH_DAVINCI || ARCH_KEYSTONE || COMPILE_TEST diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile index e5440de0f..f19df25eb 100644 --- a/drivers/i2c/busses/Makefile +++ b/drivers/i2c/busses/Makefile @@ -49,6 +49,7 @@ obj-$(CONFIG_I2C_CADENCE) +=3D i2c-cadence.o obj-$(CONFIG_I2C_CBUS_GPIO) +=3D i2c-cbus-gpio.o obj-$(CONFIG_I2C_CGBC) +=3D i2c-cgbc.o obj-$(CONFIG_I2C_CPM) +=3D i2c-cpm.o +obj-$(CONFIG_I2C_CS75XX) +=3D i2c-cs75xx.o obj-$(CONFIG_I2C_DAVINCI) +=3D i2c-davinci.o obj-$(CONFIG_I2C_DESIGNWARE_CORE) +=3D i2c-designware-core.o i2c-designware-core-y :=3D i2c-designware-common.o diff --git a/drivers/i2c/busses/i2c-cs75xx.c b/drivers/i2c/busses/i2c-cs75x= x.c new file mode 100644 index 000000000..09a77c7be --- /dev/null +++ b/drivers/i2c/busses/i2c-cs75xx.c @@ -0,0 +1,393 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Cortina Systems CS75xx I2C ("Bi-Wire") controller + * + * The controller works one byte at a time: software writes the byte to + * transmit and a command (start, stop, read, write, acknowledge) and gets + * an interrupt when the command has completed. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define CS75XX_I2C_CFG 0x00 +#define CS75XX_I2C_CTRL 0x04 +#define CS75XX_I2C_TXR 0x08 +#define CS75XX_I2C_RXR 0x0c +#define CS75XX_I2C_ACK 0x10 +#define CS75XX_I2C_IE 0x14 +#define CS75XX_I2C_INT 0x18 + +#define CS75XX_I2C_CFG_EN BIT(0) +#define CS75XX_I2C_CFG_PRER GENMASK(31, 16) + +#define CS75XX_I2C_CTRL_NACK BIT(3) +#define CS75XX_I2C_CTRL_WRITE BIT(4) +#define CS75XX_I2C_CTRL_READ BIT(5) +#define CS75XX_I2C_CTRL_STOP BIT(6) +#define CS75XX_I2C_CTRL_START BIT(7) + +#define CS75XX_I2C_ACK_AL BIT(0) +#define CS75XX_I2C_ACK_BUSY BIT(1) +#define CS75XX_I2C_ACK_NACK BIT(2) + +#define CS75XX_I2C_INT_DONE BIT(0) + +enum cs75xx_i2c_state { + CS75XX_I2C_IDLE, + CS75XX_I2C_ADDR, + CS75XX_I2C_WRITE, + CS75XX_I2C_READ, + CS75XX_I2C_STOP, +}; + +struct cs75xx_i2c { + struct i2c_adapter adap; + struct device *dev; + void __iomem *base; + int irq; + u32 prer; + + spinlock_t lock; /* protects the transfer state */ + struct completion done; + enum cs75xx_i2c_state state; + struct i2c_msg *msg; + unsigned int pos; + bool last; + int err; +}; + +static void cs75xx_i2c_hw_init(struct cs75xx_i2c *i2c) +{ + if (readl(i2c->base + CS75XX_I2C_CFG) & CS75XX_I2C_CFG_EN) { + writel(0, i2c->base + CS75XX_I2C_CFG); + fsleep(5000); + } + + writel(FIELD_PREP(CS75XX_I2C_CFG_PRER, i2c->prer) | CS75XX_I2C_CFG_EN, + i2c->base + CS75XX_I2C_CFG); + fsleep(5000); +} + +/* + * Reset the controller and enable its interrupt again, with the handler + * kept away from the registers meanwhile + */ +static void cs75xx_i2c_reset(struct cs75xx_i2c *i2c) +{ + disable_irq(i2c->irq); + cs75xx_i2c_hw_init(i2c); + writel(CS75XX_I2C_INT_DONE, i2c->base + CS75XX_I2C_INT); + writel(CS75XX_I2C_INT_DONE, i2c->base + CS75XX_I2C_IE); + enable_irq(i2c->irq); +} + +static void cs75xx_i2c_finish(struct cs75xx_i2c *i2c, int err) +{ + i2c->err =3D err; + i2c->state =3D CS75XX_I2C_IDLE; + complete(&i2c->done); +} + +/* + * End the transfer with a stop condition and complete it only once the + * stop is done, so that its interrupt cannot be taken for the next one + */ +static void cs75xx_i2c_abort(struct cs75xx_i2c *i2c, int err) +{ + i2c->err =3D err; + i2c->state =3D CS75XX_I2C_STOP; + writel(CS75XX_I2C_CTRL_STOP, i2c->base + CS75XX_I2C_CTRL); +} + +/* Start the transfer of the next data byte of the message */ +static void cs75xx_i2c_next_byte(struct cs75xx_i2c *i2c) +{ + struct i2c_msg *msg =3D i2c->msg; + bool last_byte =3D i2c->pos =3D=3D msg->len - 1; + u32 cmd; + + if (msg->flags & I2C_M_RD) { + i2c->state =3D CS75XX_I2C_READ; + cmd =3D CS75XX_I2C_CTRL_READ; + /* Do not acknowledge the last byte of a read */ + if (last_byte) + cmd |=3D CS75XX_I2C_CTRL_NACK; + } else { + i2c->state =3D CS75XX_I2C_WRITE; + writel(msg->buf[i2c->pos++], i2c->base + CS75XX_I2C_TXR); + cmd =3D CS75XX_I2C_CTRL_WRITE; + } + + if (last_byte && i2c->last) + cmd |=3D CS75XX_I2C_CTRL_STOP; + + writel(cmd, i2c->base + CS75XX_I2C_CTRL); +} + +static void cs75xx_i2c_process(struct cs75xx_i2c *i2c, u32 ack) +{ + struct i2c_msg *msg =3D i2c->msg; + + /* Late interrupt of a transfer that timed out */ + if (i2c->state =3D=3D CS75XX_I2C_IDLE) + return; + + if (i2c->state =3D=3D CS75XX_I2C_STOP) { + cs75xx_i2c_finish(i2c, i2c->err); + return; + } + + if (ack & CS75XX_I2C_ACK_AL) { + cs75xx_i2c_abort(i2c, -EAGAIN); + return; + } + + switch (i2c->state) { + case CS75XX_I2C_ADDR: + if (ack & CS75XX_I2C_ACK_NACK) { + cs75xx_i2c_abort(i2c, -ENXIO); + return; + } + cs75xx_i2c_next_byte(i2c); + break; + + case CS75XX_I2C_WRITE: + if (ack & CS75XX_I2C_ACK_NACK) { + /* The last byte of the last message carried the stop */ + if (i2c->pos =3D=3D msg->len && i2c->last) + cs75xx_i2c_finish(i2c, -EIO); + else + cs75xx_i2c_abort(i2c, -EIO); + return; + } + if (i2c->pos =3D=3D msg->len) { + cs75xx_i2c_finish(i2c, 0); + return; + } + cs75xx_i2c_next_byte(i2c); + break; + + case CS75XX_I2C_READ: + msg->buf[i2c->pos++] =3D readl(i2c->base + CS75XX_I2C_RXR); + if (i2c->pos =3D=3D msg->len) { + cs75xx_i2c_finish(i2c, 0); + return; + } + cs75xx_i2c_next_byte(i2c); + break; + + default: + break; + } +} + +static irqreturn_t cs75xx_i2c_isr(int irq, void *dev_id) +{ + struct cs75xx_i2c *i2c =3D dev_id; + u32 ctrl, ack; + + guard(spinlock)(&i2c->lock); + + if (!(readl(i2c->base + CS75XX_I2C_INT) & CS75XX_I2C_INT_DONE)) + return IRQ_NONE; + + writel(CS75XX_I2C_INT_DONE, i2c->base + CS75XX_I2C_INT); + + /* Acknowledge DONE with a CTRL read/writeback, as in the vendor driver. = */ + ctrl =3D readl(i2c->base + CS75XX_I2C_CTRL); + writel(ctrl, i2c->base + CS75XX_I2C_CTRL); + + ack =3D readl(i2c->base + CS75XX_I2C_ACK); + cs75xx_i2c_process(i2c, ack); + + return IRQ_HANDLED; +} + +static int cs75xx_i2c_xfer_msg(struct cs75xx_i2c *i2c, struct i2c_msg *msg, + bool last) +{ + unsigned long time_left; + + spin_lock_irq(&i2c->lock); + reinit_completion(&i2c->done); + i2c->msg =3D msg; + i2c->pos =3D 0; + i2c->last =3D last; + i2c->err =3D 0; + i2c->state =3D CS75XX_I2C_ADDR; + writel(i2c_8bit_addr_from_msg(msg), i2c->base + CS75XX_I2C_TXR); + writel(CS75XX_I2C_CTRL_START | CS75XX_I2C_CTRL_WRITE, + i2c->base + CS75XX_I2C_CTRL); + spin_unlock_irq(&i2c->lock); + + time_left =3D wait_for_completion_timeout(&i2c->done, i2c->adap.timeout); + if (!time_left) { + spin_lock_irq(&i2c->lock); + i2c->state =3D CS75XX_I2C_IDLE; + spin_unlock_irq(&i2c->lock); + dev_err(i2c->dev, "transfer timed out, resetting the controller\n"); + cs75xx_i2c_reset(i2c); + return -ETIMEDOUT; + } + + return i2c->err; +} + +static int cs75xx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, + int num) +{ + struct cs75xx_i2c *i2c =3D i2c_get_adapdata(adap); + u32 val; + int i, ret; + + /* Another master may hold the bus for a whole transfer */ + if (readl_poll_timeout(i2c->base + CS75XX_I2C_ACK, val, + !(val & CS75XX_I2C_ACK_BUSY), 100, + jiffies_to_usecs(adap->timeout))) { + cs75xx_i2c_reset(i2c); + if (readl(i2c->base + CS75XX_I2C_ACK) & CS75XX_I2C_ACK_BUSY) + return -EBUSY; + } + + for (i =3D 0; i < num; i++) { + ret =3D cs75xx_i2c_xfer_msg(i2c, &msgs[i], i =3D=3D num - 1); + if (ret) + return ret; + } + + return num; +} + +static u32 cs75xx_i2c_func(struct i2c_adapter *adap) +{ + return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK); +} + +static const struct i2c_algorithm cs75xx_i2c_algo =3D { + .xfer =3D cs75xx_i2c_xfer, + .functionality =3D cs75xx_i2c_func, +}; + +/* A message always carries at least one data byte after the address */ +static const struct i2c_adapter_quirks cs75xx_i2c_quirks =3D { + .flags =3D I2C_AQ_NO_ZERO_LEN, +}; + +static void cs75xx_i2c_disable(void *data) +{ + struct cs75xx_i2c *i2c =3D data; + + writel(0, i2c->base + CS75XX_I2C_IE); + writel(0, i2c->base + CS75XX_I2C_CFG); +} + +static int cs75xx_i2c_probe(struct platform_device *pdev) +{ + struct device *dev =3D &pdev->dev; + struct cs75xx_i2c *i2c; + unsigned long ref_rate; + struct clk *clk; + u32 bus_rate; + u64 div; + int ret; + + i2c =3D devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL); + if (!i2c) + return -ENOMEM; + + i2c->dev =3D dev; + spin_lock_init(&i2c->lock); + init_completion(&i2c->done); + + i2c->base =3D devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(i2c->base)) + return PTR_ERR(i2c->base); + + i2c->irq =3D platform_get_irq(pdev, 0); + if (i2c->irq < 0) + return i2c->irq; + + clk =3D devm_clk_get_enabled(dev, NULL); + if (IS_ERR(clk)) + return dev_err_probe(dev, PTR_ERR(clk), "failed to get clock\n"); + + ref_rate =3D clk_get_rate(clk); + if (device_property_read_u32(dev, "clock-frequency", &bus_rate)) + bus_rate =3D I2C_MAX_STANDARD_MODE_FREQ; + if (!bus_rate || bus_rate > I2C_MAX_FAST_MODE_FREQ) + return dev_err_probe(dev, -EINVAL, + "unsupported bus frequency %u Hz\n", + bus_rate); + + div =3D DIV_ROUND_UP_ULL(ref_rate, 5ULL * bus_rate); + if (!div || div - 1 > FIELD_MAX(CS75XX_I2C_CFG_PRER)) + return dev_err_probe(dev, -EINVAL, + "cannot derive %u Hz from %lu Hz\n", + bus_rate, ref_rate); + i2c->prer =3D div - 1; + + cs75xx_i2c_hw_init(i2c); + + writel(0, i2c->base + CS75XX_I2C_IE); + writel(CS75XX_I2C_INT_DONE, i2c->base + CS75XX_I2C_INT); + + ret =3D devm_add_action_or_reset(dev, cs75xx_i2c_disable, i2c); + if (ret) + return ret; + + ret =3D devm_request_irq(dev, i2c->irq, cs75xx_i2c_isr, 0, dev_name(dev), + i2c); + if (ret) + return ret; + + writel(CS75XX_I2C_INT_DONE, i2c->base + CS75XX_I2C_IE); + + i2c->adap.owner =3D THIS_MODULE; + i2c->adap.algo =3D &cs75xx_i2c_algo; + i2c->adap.quirks =3D &cs75xx_i2c_quirks; + i2c->adap.retries =3D 3; + i2c->adap.dev.parent =3D dev; + device_set_node(&i2c->adap.dev, dev_fwnode(dev)); + strscpy(i2c->adap.name, dev_name(dev), sizeof(i2c->adap.name)); + i2c_set_adapdata(&i2c->adap, i2c); + + ret =3D devm_i2c_add_adapter(dev, &i2c->adap); + if (ret) + return ret; + + dev_dbg(dev, "bus %u Hz, reference %lu Hz\n", bus_rate, + ref_rate); + + return 0; +} + +static const struct of_device_id cs75xx_i2c_of_match[] =3D { + { .compatible =3D "cortina,cs7542-i2c" }, + { } +}; +MODULE_DEVICE_TABLE(of, cs75xx_i2c_of_match); + +static struct platform_driver cs75xx_i2c_driver =3D { + .probe =3D cs75xx_i2c_probe, + .driver =3D { + .name =3D "cs75xx-i2c", + .of_match_table =3D cs75xx_i2c_of_match, + }, +}; +module_platform_driver(cs75xx_i2c_driver); + +MODULE_DESCRIPTION("Cortina Systems CS75xx I2C controller driver"); +MODULE_LICENSE("GPL"); --=20 2.53.0