From nobody Wed Feb 5 22:54:05 2025 Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) (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 6E8CD207A19; Tue, 4 Feb 2025 09:29:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738661371; cv=none; b=m2m1uRY6GXCHW6ddzpvDHZQSdV62JI7kXr7yWMW5pgXeiLhR54gFZpJrbM3koVS4z8vIHFZxMKHzdjDTefYwYIbICfJKxxip2woNFcy2UHvZq4VSsh+LlqusZu1eqtlHT+pKYWKAeX+7b1gUi7+yRyVS3pT/wWf6XCQzknVr4dA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738661371; c=relaxed/simple; bh=uwleHELCDxefzsTrKEyvJkFzG4zqH/uU5XUDcbB2FYk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=YY0OgAsFNCYKgoQ0GW9EK6wtzh5ZpUIM9T+q9rfFMU5i5fTAnGJiIO6VsgDTH8AWlgjinz9N/y73LI+UAZUcAPss6/D/l/hSqPklgZvwfzJB0+3KgLDYhNEcAn4bx25wS43Shqfm7gXUQdD1WZJ4zxO3vkEg6n0bCMp6Kx7U6uQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=Acp/vsSd; arc=none smtp.client-ip=217.70.183.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="Acp/vsSd" Received: by mail.gandi.net (Postfix) with ESMTPSA id A49884435C; Tue, 4 Feb 2025 09:29:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1738661360; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FEupvcbyFP1FMX7h0RUYEpOlmfghhLcT2eYt5M/AdM8=; b=Acp/vsSdyGQ5NyG1+C9q1SEEM9EIT/+2kQW5RjZflrgSQXdweWunvKZZZ/1TjPEhTc4d/x 8VQ/ZwWt5T1kNdTNtckIPcpUueYuLorVkAZFLZsoSzPVV95JTU1/wv8ZmPD4lxzTcur8rB AjQFdRyPHe1FQVWN0TWO5rjkQLRF3oExurzhQo7eyEsd2OyOY8uHtJGvCheLve9mf5uN1D +Kfrg5MQhlZgOUoYsfL0tKWoAv7/NduSZl+TQNEd8N/6dAaCb64xoVi1z1hMqYh2IM5Uq+ bTv0P7AyPQxGY7NE4w7euLGs1GHgB9rkTNYa14bq5lqRNApww//t82lLpYTE+Q== From: Romain Gantois Date: Tue, 04 Feb 2025 10:29:12 +0100 Subject: [PATCH v7 1/9] dt-bindings: misc: Describe TI FPC202 dual port controller 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: <20250204-fpc202-v7-1-78b4b8a35cf1@bootlin.com> References: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com> In-Reply-To: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com> To: Wolfram Sang , Tomi Valkeinen , Luca Ceresoli , Andi Shyti , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Derek Kiernan , Dragan Cvetic , Arnd Bergmann , Greg Kroah-Hartman , Mauro Carvalho Chehab , Linus Walleij , Bartosz Golaszewski Cc: Thomas Petazzoni , Kory Maincent , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-media@vger.kernel.org, linux-gpio@vger.kernel.org, Romain Gantois , Conor Dooley X-Mailer: b4 0.14.2 X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdduhecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfitefpfffkpdcuggftfghnshhusghstghrihgsvgenuceurghilhhouhhtmecufedtudenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephfffufggtgfgkfhfjgfvvefosehtjeertdertdejnecuhfhrohhmpeftohhmrghinhcuifgrnhhtohhishcuoehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomheqnecuggftrfgrthhtvghrnhepieethfejleejueduleffveejleefkeeiheffffefkefgtedtiedvvddtfeetgefhnecuffhomhgrihhnpeguvghvihgtvghtrhgvvgdrohhrghenucfkphepledtrdekledrudeifedruddvjeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepihhnvghtpeeltddrkeelrdduieefrdduvdejpdhhvghloheplgduledvrdduieekrddtrddufegnpdhmrghilhhfrhhomheprhhomhgrihhnrdhgrghnthhoihhssegsohhothhlihhnrdgtohhmpdhnsggprhgtphhtthhopedvfedprhgtphhtthhopegrrhhnugesrghrnhgusgdruggvpdhrtghpthhtoheprhhomhgrihhnrdhgrghnthhoihhssegsohhothhlihhnrdgtohhmpdhrtghpthhtohepthhhohhmrghsrdhpvghtrgiiiihonhhisegsohhothhlihhnrdgtohhmpdhrtghpthhtohepuggvrhgvkhdrkhhivghrnhgrnhesrghmugdrtghom hdprhgtphhtthhopehtohhmihdrvhgrlhhkvghinhgvnhesihguvggrshhonhgsohgrrhgurdgtohhmpdhrtghpthhtohepughrrghgrghnrdgtvhgvthhitgesrghmugdrtghomhdprhgtphhtthhopeguvghvihgtvghtrhgvvgesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopegrnhguihdrshhhhihtiheskhgvrhhnvghlrdhorhhg X-GND-Sasl: romain.gantois@bootlin.com The FPC202 dual port controller serves as a low speed signal aggregator for common port types, notably SFP. It provides access to I2C and low-speed GPIO signals of a downstream device through a single upstream control interface. Up to two logical I2C addresses can be accessed on each of the FPC202's ports. The port controller acts as an I2C translator (ATR). It converts addresses of incoming and outgoing I2C transactions. One use case of this is accessing two SFP modules at logical address 0x50 from the same upstream I2C controller, using two different client aliases. Reviewed-by: Conor Dooley Signed-off-by: Romain Gantois --- .../devicetree/bindings/misc/ti,fpc202.yaml | 94 ++++++++++++++++++= ++++ MAINTAINERS | 6 ++ 2 files changed, 100 insertions(+) diff --git a/Documentation/devicetree/bindings/misc/ti,fpc202.yaml b/Docume= ntation/devicetree/bindings/misc/ti,fpc202.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a8cb10f2d0df3fa5224f95f6f87= 467a8bce253bc --- /dev/null +++ b/Documentation/devicetree/bindings/misc/ti,fpc202.yaml @@ -0,0 +1,94 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/misc/ti,fpc202.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: TI FPC202 dual port controller with expanded IOs + +maintainers: + - Romain Gantois + +allOf: + - $ref: /schemas/i2c/i2c-atr.yaml# + +properties: + compatible: + const: ti,fpc202 + + reg: + maxItems: 1 + + gpio-controller: true + + "#gpio-cells": + const: 2 + + enable-gpios: + description: + Specifier for the GPIO connected to the EN pin. + maxItems: 1 + + '#address-cells': + const: 1 + + '#size-cells': + const: 0 + +patternProperties: + "^i2c@[0-1]$": + $ref: /schemas/i2c/i2c-controller.yaml# + description: Downstream device ports 0 and 1 + + properties: + reg: + maxItems: 1 + description: + Downstream port ID + + required: + - "#address-cells" + - "#size-cells" + - reg + + unevaluatedProperties: false + +required: + - compatible + - reg + - gpio-controller + - "#gpio-cells" + - "#address-cells" + - "#size-cells" + +unevaluatedProperties: false + +examples: + - | + i2c { + #address-cells =3D <1>; + #size-cells =3D <0>; + + i2c-atr@f { + compatible =3D "ti,fpc202"; + reg =3D <0xf>; + #address-cells =3D <1>; + #size-cells =3D <0>; + + gpio-controller; + #gpio-cells =3D <2>; + + i2c@0 { + #address-cells =3D <1>; + #size-cells =3D <0>; + reg =3D <0>; + }; + + i2c@1 { + #address-cells =3D <1>; + #size-cells =3D <0>; + reg =3D <1>; + }; + }; + }; +... diff --git a/MAINTAINERS b/MAINTAINERS index 896a307fa06545e2861abe46ea7029f9b4d3628e..4bb13de2b8ebad5180e2bde607d= ac40f35c51782 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -23709,6 +23709,12 @@ F: drivers/misc/tifm* F: drivers/mmc/host/tifm_sd.c F: include/linux/tifm.h =20 +TI FPC202 DUAL PORT CONTROLLER +M: Romain Gantois +L: linux-kernel@vger.kernel.org +S: Maintained +F: Documentation/devicetree/bindings/misc/ti,fpc202.yaml + TI FPD-LINK DRIVERS M: Tomi Valkeinen L: linux-media@vger.kernel.org --=20 2.47.1 From nobody Wed Feb 5 22:54:05 2025 Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) (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 A940D207A27; Tue, 4 Feb 2025 09:29:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738661366; cv=none; b=A9hRWB3rv8PCPSdzbbgJtdDGayaaEPLN37zlEHB16g0F32Ihbo3VNDCcETH+iiLCllTfQJbLYF/MlznXX5qTYZ2Sg95AyZOSh6wE83bZvsz03N4DcgvYt0yn0w8O1+CaQ7X/9vNTxqzJHE9YxemBY6/E7BgV3bcOv4OU/1JnWJw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738661366; c=relaxed/simple; bh=PWVjDlbY+5QXMES1Jg43CGsHf063LAxvEUnul1zh5kw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=GzRDxGoxtTAKfg6vOg+Ohj+BuHH551ejochcyHO09yHpPrnReucf9rhKSEyEJst0pwPenI/U8f61m4ifC493QSpD/o1pOxRN8Hcvy/s6VqhjK6FOuJdbtMO+lUSilxXaoGAl8vzQRpb+upL0QxKOwYRZSK8aUb7dRwVzk6Uer4Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=Y85470om; arc=none smtp.client-ip=217.70.183.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="Y85470om" Received: by mail.gandi.net (Postfix) with ESMTPSA id C808843423; Tue, 4 Feb 2025 09:29:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1738661361; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vTmcIxBFhs+q9FfJ0TmuNV/3ZVPBqmxE+uUND15pWMY=; b=Y85470omv+PEK8UOJ3mUTZmtHMB2wygbE1Wc7a0qyLFuhXob012694idgZH4CY/FyIBRFB 9d02rPJD+EIssAuPkUIWBfuZfY0NURE+2XRTMj4wHjKGlLHFiwnYjv85KbzGyGYCqc/anj 0C67LnFX8wlXdf+G8Gu/8cnPMifQTDFAUOydmuvob1vqgRXiunXn1Te71e0eJH4/dF1HLn QXUv4nshOZL2wxH6kkASYN9OPPnfWN1SaZpOnbvd9kQ7NFoL2shUotEqBQgbAkVFwreoJJ 22y5em54/2zPjC8QdrNHLgheNj+DHXUvd9CHKcnLmhu9+r/NMydH3HwBNARb2A== From: Romain Gantois Date: Tue, 04 Feb 2025 10:29:13 +0100 Subject: [PATCH v7 2/9] media: i2c: ds90ub960: Replace aliased clients list with address list 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: <20250204-fpc202-v7-2-78b4b8a35cf1@bootlin.com> References: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com> In-Reply-To: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com> To: Wolfram Sang , Tomi Valkeinen , Luca Ceresoli , Andi Shyti , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Derek Kiernan , Dragan Cvetic , Arnd Bergmann , Greg Kroah-Hartman , Mauro Carvalho Chehab , Linus Walleij , Bartosz Golaszewski Cc: Thomas Petazzoni , Kory Maincent , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-media@vger.kernel.org, linux-gpio@vger.kernel.org, Romain Gantois X-Mailer: b4 0.14.2 X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdduhecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfitefpfffkpdcuggftfghnshhusghstghrihgsvgenuceurghilhhouhhtmecufedtudenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephfffufggtgfgkfhfjgfvvefosehtjeertdertdejnecuhfhrohhmpeftohhmrghinhcuifgrnhhtohhishcuoehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomheqnecuggftrfgrthhtvghrnhepkeelieefteelffeuheevtdetkefhfffhteffkefgtefhkeevudeutdeugfffheegnecukfhppeeltddrkeelrdduieefrdduvdejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledtrdekledrudeifedruddvjedphhgvlhhopegludelvddrudeikedrtddrudefngdpmhgrihhlfhhrohhmpehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomhdpnhgspghrtghpthhtohepvddvpdhrtghpthhtoheprghrnhgusegrrhhnuggsrdguvgdprhgtphhtthhopehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomhdprhgtphhtthhopehthhhomhgrshdrphgvthgriiiiohhnihessghoohhtlhhinhdrtghomhdprhgtphhtthhopeguvghrvghkrdhkihgvrhhnrghnsegrmhgurdgtohhmpdhrtghpthhtohepthhomhhirdhvrghlkhgvihhnvghns ehiuggvrghsohhnsghorghrugdrtghomhdprhgtphhtthhopegurhgrghgrnhdrtghvvghtihgtsegrmhgurdgtohhmpdhrtghpthhtohepuggvvhhitggvthhrvggvsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheprghnughirdhshhihthhisehkvghrnhgvlhdrohhrgh X-GND-Sasl: romain.gantois@bootlin.com The ds90ub960 driver currently uses a list of i2c_client structs to keep track of used I2C address translator (ATR) alias slots for each RX port. Keeping these i2c_client structs in the alias slot list isn't actually needed, the driver only needs to know the client address for each slot. Convert the aliased_clients list to a list of aliased client addresses. This will allow removing the "client" parameter from the i2c-atr callbacks in a future patch. Signed-off-by: Romain Gantois --- drivers/media/i2c/ds90ub960.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/drivers/media/i2c/ds90ub960.c b/drivers/media/i2c/ds90ub960.c index 5dde8452739b64dd5b847a7bc1dac556ea43ca6c..e1d53ef087538f7df2c992612e7= 0ce6a3e24906b 100644 --- a/drivers/media/i2c/ds90ub960.c +++ b/drivers/media/i2c/ds90ub960.c @@ -478,7 +478,7 @@ struct ub960_rxport { }; } eq; =20 - const struct i2c_client *aliased_clients[UB960_MAX_PORT_ALIASES]; + u16 aliased_addrs[UB960_MAX_PORT_ALIASES]; }; =20 struct ub960_asd { @@ -1054,17 +1054,17 @@ static int ub960_atr_attach_client(struct i2c_atr *= atr, u32 chan_id, struct device *dev =3D &priv->client->dev; unsigned int reg_idx; =20 - for (reg_idx =3D 0; reg_idx < ARRAY_SIZE(rxport->aliased_clients); reg_id= x++) { - if (!rxport->aliased_clients[reg_idx]) + for (reg_idx =3D 0; reg_idx < ARRAY_SIZE(rxport->aliased_addrs); reg_idx+= +) { + if (!rxport->aliased_addrs[reg_idx]) break; } =20 - if (reg_idx =3D=3D ARRAY_SIZE(rxport->aliased_clients)) { + if (reg_idx =3D=3D ARRAY_SIZE(rxport->aliased_addrs)) { dev_err(dev, "rx%u: alias pool exhausted\n", rxport->nport); return -EADDRNOTAVAIL; } =20 - rxport->aliased_clients[reg_idx] =3D client; + rxport->aliased_addrs[reg_idx] =3D client->addr; =20 ub960_rxport_write(priv, chan_id, UB960_RR_SLAVE_ID(reg_idx), client->addr << 1); @@ -1085,18 +1085,18 @@ static void ub960_atr_detach_client(struct i2c_atr = *atr, u32 chan_id, struct device *dev =3D &priv->client->dev; unsigned int reg_idx; =20 - for (reg_idx =3D 0; reg_idx < ARRAY_SIZE(rxport->aliased_clients); reg_id= x++) { - if (rxport->aliased_clients[reg_idx] =3D=3D client) + for (reg_idx =3D 0; reg_idx < ARRAY_SIZE(rxport->aliased_addrs); reg_idx+= +) { + if (rxport->aliased_addrs[reg_idx] =3D=3D client->addr) break; } =20 - if (reg_idx =3D=3D ARRAY_SIZE(rxport->aliased_clients)) { + if (reg_idx =3D=3D ARRAY_SIZE(rxport->aliased_addrs)) { dev_err(dev, "rx%u: client 0x%02x is not mapped!\n", rxport->nport, client->addr); return; } =20 - rxport->aliased_clients[reg_idx] =3D NULL; + rxport->aliased_addrs[reg_idx] =3D 0; =20 ub960_rxport_write(priv, chan_id, UB960_RR_SLAVE_ALIAS(reg_idx), 0); =20 --=20 2.47.1 From nobody Wed Feb 5 22:54:05 2025 Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) (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 AFC8A207DED; Tue, 4 Feb 2025 09:29:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738661367; cv=none; b=hlr5U/CxL7h1FF2Fk8pdNaT32d3dpR/dzGa1hFqwKUCyYA3vCt7FLvIpV9QjKEo9emMEyy9NCn3ogs+a5Ib0P5wyp+xEZkTHk1MvAFMyYgUm7Q4iu0jx8sqY59TcaPmPhAjyDPRsjiGjrm0izarhyBKwSSTkJv+tnvo7vBWJSpg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738661367; c=relaxed/simple; bh=+mJGIDwUzXRlTT0Fw3Bn+gb+tu4Yk1FGiSzjZr/q9Ok=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=UZBkDA7ikzBe9CQqupSJT1N9OWY/Jtxhlc6CVvNhtKrdm+Flfm7d0N48ylob79pDwjcYHCsfLss7YjivyflzDk2fwjQwve91e+2D+uE8DpO0Vr41jP3R1G+j0R8rQb7BXor/+yz6itT/gjvEJ4YheZXjUUb9pYLcxyEks9c28ZQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=hfYBUHGC; arc=none smtp.client-ip=217.70.183.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="hfYBUHGC" Received: by mail.gandi.net (Postfix) with ESMTPSA id DEE8844366; Tue, 4 Feb 2025 09:29:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1738661362; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DeqY4AwIO0d0PgYoKVzkcW+SM8cm9P6Q9OhdJ8KffYY=; b=hfYBUHGC4SBWBpsl87sMSR2wZTBnHddjlPMoifYTUmvx3dBlsx1qezimeqKvnyaXYnBD6A BWJIqywr/yjiorI3Xctviqq54SxggtSu6XIv8MQ2X2mY/2OmdhQ2V8id2sVYR5PuBmIfTX N6T0wZvJT1HXgett+SBXh6gbGfly2zCR1NgXtcO7kYkfo01piVRe5sx/Xdl5veqVtvthIb hT9EIzoYdZCr6VNBvl0S9rn+e7UH8P6s4/ZsYhgDv8iK6vLKV4M4iTNKZOV1ZvurZiAgVe TRQkjFCuxQu2yPOgmR1hlZFYdtC1JnRjk/u077UFyf8Xi6XJ3DIcsyzcBk10JA== From: Romain Gantois Date: Tue, 04 Feb 2025 10:29:14 +0100 Subject: [PATCH v7 3/9] media: i2c: ds90ub960: Protect alias_use_mask with a mutex 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: <20250204-fpc202-v7-3-78b4b8a35cf1@bootlin.com> References: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com> In-Reply-To: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com> To: Wolfram Sang , Tomi Valkeinen , Luca Ceresoli , Andi Shyti , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Derek Kiernan , Dragan Cvetic , Arnd Bergmann , Greg Kroah-Hartman , Mauro Carvalho Chehab , Linus Walleij , Bartosz Golaszewski Cc: Thomas Petazzoni , Kory Maincent , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-media@vger.kernel.org, linux-gpio@vger.kernel.org, Romain Gantois X-Mailer: b4 0.14.2 X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdduhecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfitefpfffkpdcuggftfghnshhusghstghrihgsvgenuceurghilhhouhhtmecufedtudenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephfffufggtgfgkfhfjgfvvefosehtjeertdertdejnecuhfhrohhmpeftohhmrghinhcuifgrnhhtohhishcuoehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomheqnecuggftrfgrthhtvghrnhepkeelieefteelffeuheevtdetkefhfffhteffkefgtefhkeevudeutdeugfffheegnecukfhppeeltddrkeelrdduieefrdduvdejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledtrdekledrudeifedruddvjedphhgvlhhopegludelvddrudeikedrtddrudefngdpmhgrihhlfhhrohhmpehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomhdpnhgspghrtghpthhtohepvddvpdhrtghpthhtoheprghrnhgusegrrhhnuggsrdguvgdprhgtphhtthhopehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomhdprhgtphhtthhopehthhhomhgrshdrphgvthgriiiiohhnihessghoohhtlhhinhdrtghomhdprhgtphhtthhopeguvghrvghkrdhkihgvrhhnrghnsegrmhgurdgtohhmpdhrtghpthhtohepthhomhhirdhvrghlkhgvihhnvghns ehiuggvrghsohhnsghorghrugdrtghomhdprhgtphhtthhopegurhgrghgrnhdrtghvvghtihgtsegrmhgurdgtohhmpdhrtghpthhtohepuggvvhhitggvthhrvggvsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheprghnughirdhshhihthhisehkvghrnhgvlhdrohhrgh X-GND-Sasl: romain.gantois@bootlin.com The aliased_addrs list represents the occupation of an RX port's hardware alias table. This list and the underlying hardware table are only accessed in the attach/detach_client() callbacks. These functions are only called from a bus notifier handler in i2c-atr.c, which is always called with the notifier chain's semaphore held. This indirectly prevents concurrent access to the aliased_addrs list. However, more explicit and direct locking is preferable. Moreover, with the introduction of dynamic address translation in a future patch, the attach/detach_client() callbacks will be called from outside of the notifier chain's read section. Introduce a mutex to protect access to the aliased_addrs list and its underlying hardware table. Signed-off-by: Romain Gantois --- drivers/media/i2c/ds90ub960.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/drivers/media/i2c/ds90ub960.c b/drivers/media/i2c/ds90ub960.c index e1d53ef087538f7df2c992612e70ce6a3e24906b..5969692480409a1632ac05e43c5= 8df479982bdd5 100644 --- a/drivers/media/i2c/ds90ub960.c +++ b/drivers/media/i2c/ds90ub960.c @@ -27,6 +27,7 @@ */ =20 #include +#include #include #include #include @@ -478,6 +479,8 @@ struct ub960_rxport { }; } eq; =20 + /* lock for aliased_addrs and associated registers */ + struct mutex aliased_addrs_lock; u16 aliased_addrs[UB960_MAX_PORT_ALIASES]; }; =20 @@ -1054,6 +1057,8 @@ static int ub960_atr_attach_client(struct i2c_atr *at= r, u32 chan_id, struct device *dev =3D &priv->client->dev; unsigned int reg_idx; =20 + guard(mutex)(&rxport->aliased_addrs_lock); + for (reg_idx =3D 0; reg_idx < ARRAY_SIZE(rxport->aliased_addrs); reg_idx+= +) { if (!rxport->aliased_addrs[reg_idx]) break; @@ -1085,6 +1090,8 @@ static void ub960_atr_detach_client(struct i2c_atr *a= tr, u32 chan_id, struct device *dev =3D &priv->client->dev; unsigned int reg_idx; =20 + guard(mutex)(&rxport->aliased_addrs_lock); + for (reg_idx =3D 0; reg_idx < ARRAY_SIZE(rxport->aliased_addrs); reg_idx+= +) { if (rxport->aliased_addrs[reg_idx] =3D=3D client->addr) break; @@ -3236,6 +3243,8 @@ static void ub960_rxport_free_ports(struct ub960_data= *priv) fwnode_handle_put(rxport->source.ep_fwnode); fwnode_handle_put(rxport->ser.fwnode); =20 + mutex_destroy(&rxport->aliased_addrs_lock); + kfree(rxport); priv->rxports[nport] =3D NULL; } @@ -3456,6 +3465,8 @@ static int ub960_parse_dt_rxport(struct ub960_data *p= riv, unsigned int nport, if (ret) goto err_put_remote_fwnode; =20 + mutex_init(&rxport->aliased_addrs_lock); + return 0; =20 err_put_remote_fwnode: --=20 2.47.1 From nobody Wed Feb 5 22:54:05 2025 Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) (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 92350207DFF; Tue, 4 Feb 2025 09:29:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738661368; cv=none; b=eYOyNaZfRRqVZLV/UDLl/IJp36i3Td2s9ZsVxSUyoh7kr5kD5NBL5e24pzjPtpS3E+3Gkz2jtzrSlbC5D7RIj+0r8OthhKEmI2Rzg3mggauXyK8flKYVi57pq44d61u6axdFU6H4dbIoseY4sa6a88eNTffpA1HCGVOXuziVkBY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738661368; c=relaxed/simple; bh=n0DBKAJ9pgKz/m5yLANuJaeG8xY3Quf8nfeXa7JfymI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=W+mbKG4YEFcP1LUlKFxfo0FU6UFK2iFJWJnL+dHPiQwg4Gq/46ir+gTLa4xlo3VS+Otjsy/5gGwRcckGgCG41OxJ2IHZL9aHkyIkbjLMXxo/5Ip77Fo5ER+kMw8qFMRePzJ+j68vhx2Iq9dVYWDu9LIwlz2beTHPoIJsbk/MAec= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=YkXXUwe+; arc=none smtp.client-ip=217.70.183.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="YkXXUwe+" Received: by mail.gandi.net (Postfix) with ESMTPSA id F334A44360; Tue, 4 Feb 2025 09:29:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1738661363; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=peZy17OXvlBM3hd7zz+egN6FRCg5cULHXylYXdPt8v8=; b=YkXXUwe+FPK78Dv2WZFrEyjiVv6fzHfdi+FZ9IklOXo6QzTtb1A2UchYnbPZsjOqT2rtRt XMjazOEheSCjmxhpDpA+AgPhcXJvZVyoP5nUtWUDRp/an7IJKBPAP3FFldh6v8SS1kSZsz yRXV+QM9KS7N4jCkqBJHG1PhAQoDHK6wm2G/o+V3rO7m0AghTmB/ghqxgVrURqC/8kFhxo IgrdnqR/8F1It6myCeH9qCKjeX9l7xZX9elxJvnE8I7jWwhxrGbkFSoSrhMDf4Z/kIE063 Bes0ygAMjE6vEpOQzZMvcU9aejasw5znoKlb8RrjoLesavao0mqeiDrOYTlwOQ== From: Romain Gantois Date: Tue, 04 Feb 2025 10:29:15 +0100 Subject: [PATCH v7 4/9] i2c: use client addresses directly in ATR interface 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: <20250204-fpc202-v7-4-78b4b8a35cf1@bootlin.com> References: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com> In-Reply-To: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com> To: Wolfram Sang , Tomi Valkeinen , Luca Ceresoli , Andi Shyti , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Derek Kiernan , Dragan Cvetic , Arnd Bergmann , Greg Kroah-Hartman , Mauro Carvalho Chehab , Linus Walleij , Bartosz Golaszewski Cc: Thomas Petazzoni , Kory Maincent , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-media@vger.kernel.org, linux-gpio@vger.kernel.org, Romain Gantois X-Mailer: b4 0.14.2 X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdduhecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfitefpfffkpdcuggftfghnshhusghstghrihgsvgenuceurghilhhouhhtmecufedtudenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephfffufggtgfgkfhfjgfvvefosehtjeertdertdejnecuhfhrohhmpeftohhmrghinhcuifgrnhhtohhishcuoehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomheqnecuggftrfgrthhtvghrnhepkeelieefteelffeuheevtdetkefhfffhteffkefgtefhkeevudeutdeugfffheegnecukfhppeeltddrkeelrdduieefrdduvdejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledtrdekledrudeifedruddvjedphhgvlhhopegludelvddrudeikedrtddrudefngdpmhgrihhlfhhrohhmpehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomhdpnhgspghrtghpthhtohepvddvpdhrtghpthhtoheprghrnhgusegrrhhnuggsrdguvgdprhgtphhtthhopehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomhdprhgtphhtthhopehthhhomhgrshdrphgvthgriiiiohhnihessghoohhtlhhinhdrtghomhdprhgtphhtthhopeguvghrvghkrdhkihgvrhhnrghnsegrmhgurdgtohhmpdhrtghpthhtohepthhomhhirdhvrghlkhgvihhnvghns ehiuggvrghsohhnsghorghrugdrtghomhdprhgtphhtthhopegurhgrghgrnhdrtghvvghtihgtsegrmhgurdgtohhmpdhrtghpthhtohepuggvvhhitggvthhrvggvsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheprghnughirdhshhihthhisehkvghrnhgvlhdrohhrgh X-GND-Sasl: romain.gantois@bootlin.com The I2C Address Translator (ATR) module defines mappings from i2c_client structs to aliases. However, only the physical address of each i2c_client struct is actually relevant to the workings of the ATR module. Moreover, some drivers require address translation functionality but do not allocate i2c_client structs, accessing the adapter directly instead. The SFP subsystem is an example of this. Replace the "i2c_client" field of the i2c_atr_alias_pair struct with a u16 "addr" field. Rewrite helper functions and callbacks as needed. Reviewed-by: Tomi Valkeinen Signed-off-by: Romain Gantois --- drivers/i2c/i2c-atr.c | 52 ++++++++++++++++-----------------------= ---- drivers/media/i2c/ds90ub960.c | 24 ++++++++++---------- include/linux/i2c-atr.h | 20 ++++++++--------- 3 files changed, 41 insertions(+), 55 deletions(-) diff --git a/drivers/i2c/i2c-atr.c b/drivers/i2c/i2c-atr.c index 8fe9ddff8e96f6f613f48d91a09c09b326e6cb01..118fd7d764dce51de587e274ef4= 52e031ccff52d 100644 --- a/drivers/i2c/i2c-atr.c +++ b/drivers/i2c/i2c-atr.c @@ -21,16 +21,16 @@ #define ATR_MAX_SYMLINK_LEN 11 /* Longest name is 10 chars: "channel-99" */ =20 /** - * struct i2c_atr_alias_pair - Holds the alias assigned to a client. + * struct i2c_atr_alias_pair - Holds the alias assigned to a client addres= s. * @node: List node - * @client: Pointer to the client on the child bus + * @addr: Address of the client on the child bus. * @alias: I2C alias address assigned by the driver. * This is the address that will be used to issue I2C transactions * on the parent (physical) bus. */ struct i2c_atr_alias_pair { struct list_head node; - const struct i2c_client *client; + u16 addr; u16 alias; }; =20 @@ -97,27 +97,13 @@ struct i2c_atr { struct i2c_adapter *adapter[] __counted_by(max_adapters); }; =20 -static struct i2c_atr_alias_pair * -i2c_atr_find_mapping_by_client(const struct list_head *list, - const struct i2c_client *client) -{ - struct i2c_atr_alias_pair *c2a; - - list_for_each_entry(c2a, list, node) { - if (c2a->client =3D=3D client) - return c2a; - } - - return NULL; -} - static struct i2c_atr_alias_pair * i2c_atr_find_mapping_by_addr(const struct list_head *list, u16 phys_addr) { struct i2c_atr_alias_pair *c2a; =20 list_for_each_entry(c2a, list, node) { - if (c2a->client->addr =3D=3D phys_addr) + if (c2a->addr =3D=3D phys_addr) return c2a; } =20 @@ -313,8 +299,8 @@ static void i2c_atr_release_alias(struct i2c_atr *atr, = u16 alias) dev_warn(atr->dev, "Unable to find mapped alias\n"); } =20 -static int i2c_atr_attach_client(struct i2c_adapter *adapter, - const struct i2c_client *client) +static int i2c_atr_attach_addr(struct i2c_adapter *adapter, + u16 addr) { struct i2c_atr_chan *chan =3D adapter->algo_data; struct i2c_atr *atr =3D chan->atr; @@ -334,14 +320,14 @@ static int i2c_atr_attach_client(struct i2c_adapter *= adapter, goto err_release_alias; } =20 - ret =3D atr->ops->attach_client(atr, chan->chan_id, client, alias); + ret =3D atr->ops->attach_addr(atr, chan->chan_id, addr, alias); if (ret) goto err_free; =20 - dev_dbg(atr->dev, "chan%u: client 0x%02x mapped at alias 0x%02x (%s)\n", - chan->chan_id, client->addr, alias, client->name); + dev_dbg(atr->dev, "chan%u: using alias 0x%02x for addr 0x%02x\n", + chan->chan_id, alias, addr); =20 - c2a->client =3D client; + c2a->addr =3D addr; c2a->alias =3D alias; list_add(&c2a->node, &chan->alias_list); =20 @@ -355,16 +341,16 @@ static int i2c_atr_attach_client(struct i2c_adapter *= adapter, return ret; } =20 -static void i2c_atr_detach_client(struct i2c_adapter *adapter, - const struct i2c_client *client) +static void i2c_atr_detach_addr(struct i2c_adapter *adapter, + u16 addr) { struct i2c_atr_chan *chan =3D adapter->algo_data; struct i2c_atr *atr =3D chan->atr; struct i2c_atr_alias_pair *c2a; =20 - atr->ops->detach_client(atr, chan->chan_id, client); + atr->ops->detach_addr(atr, chan->chan_id, addr); =20 - c2a =3D i2c_atr_find_mapping_by_client(&chan->alias_list, client); + c2a =3D i2c_atr_find_mapping_by_addr(&chan->alias_list, addr); if (!c2a) { /* This should never happen */ dev_warn(atr->dev, "Unable to find address mapping\n"); @@ -374,8 +360,8 @@ static void i2c_atr_detach_client(struct i2c_adapter *a= dapter, i2c_atr_release_alias(atr, c2a->alias); =20 dev_dbg(atr->dev, - "chan%u: client 0x%02x unmapped from alias 0x%02x (%s)\n", - chan->chan_id, client->addr, c2a->alias, client->name); + "chan%u: detached alias 0x%02x from addr 0x%02x\n", + chan->chan_id, c2a->alias, addr); =20 list_del(&c2a->node); kfree(c2a); @@ -405,7 +391,7 @@ static int i2c_atr_bus_notifier_call(struct notifier_bl= ock *nb, =20 switch (event) { case BUS_NOTIFY_ADD_DEVICE: - ret =3D i2c_atr_attach_client(client->adapter, client); + ret =3D i2c_atr_attach_addr(client->adapter, client->addr); if (ret) dev_err(atr->dev, "Failed to attach remote client '%s': %d\n", @@ -413,7 +399,7 @@ static int i2c_atr_bus_notifier_call(struct notifier_bl= ock *nb, break; =20 case BUS_NOTIFY_REMOVED_DEVICE: - i2c_atr_detach_client(client->adapter, client); + i2c_atr_detach_addr(client->adapter, client->addr); break; =20 default: @@ -506,7 +492,7 @@ struct i2c_atr *i2c_atr_new(struct i2c_adapter *parent,= struct device *dev, if (max_adapters > ATR_MAX_ADAPTERS) return ERR_PTR(-EINVAL); =20 - if (!ops || !ops->attach_client || !ops->detach_client) + if (!ops || !ops->attach_addr || !ops->detach_addr) return ERR_PTR(-EINVAL); =20 atr =3D kzalloc(struct_size(atr, adapter, max_adapters), GFP_KERNEL); diff --git a/drivers/media/i2c/ds90ub960.c b/drivers/media/i2c/ds90ub960.c index 5969692480409a1632ac05e43c58df479982bdd5..869e32bd07e8b08f443d141e20e= b53eeeb3de864 100644 --- a/drivers/media/i2c/ds90ub960.c +++ b/drivers/media/i2c/ds90ub960.c @@ -1049,8 +1049,8 @@ static int ub960_ind_update_bits(struct ub960_data *p= riv, u8 block, u8 reg, * I2C-ATR (address translator) */ =20 -static int ub960_atr_attach_client(struct i2c_atr *atr, u32 chan_id, - const struct i2c_client *client, u16 alias) +static int ub960_atr_attach_addr(struct i2c_atr *atr, u32 chan_id, + u16 addr, u16 alias) { struct ub960_data *priv =3D i2c_atr_get_driver_data(atr); struct ub960_rxport *rxport =3D priv->rxports[chan_id]; @@ -1069,21 +1069,21 @@ static int ub960_atr_attach_client(struct i2c_atr *= atr, u32 chan_id, return -EADDRNOTAVAIL; } =20 - rxport->aliased_addrs[reg_idx] =3D client->addr; + rxport->aliased_addrs[reg_idx] =3D addr; =20 ub960_rxport_write(priv, chan_id, UB960_RR_SLAVE_ID(reg_idx), - client->addr << 1); + addr << 1); ub960_rxport_write(priv, chan_id, UB960_RR_SLAVE_ALIAS(reg_idx), alias << 1); =20 dev_dbg(dev, "rx%u: client 0x%02x assigned alias 0x%02x at slot %u\n", - rxport->nport, client->addr, alias, reg_idx); + rxport->nport, addr, alias, reg_idx); =20 return 0; } =20 -static void ub960_atr_detach_client(struct i2c_atr *atr, u32 chan_id, - const struct i2c_client *client) +static void ub960_atr_detach_addr(struct i2c_atr *atr, u32 chan_id, + u16 addr) { struct ub960_data *priv =3D i2c_atr_get_driver_data(atr); struct ub960_rxport *rxport =3D priv->rxports[chan_id]; @@ -1093,13 +1093,13 @@ static void ub960_atr_detach_client(struct i2c_atr = *atr, u32 chan_id, guard(mutex)(&rxport->aliased_addrs_lock); =20 for (reg_idx =3D 0; reg_idx < ARRAY_SIZE(rxport->aliased_addrs); reg_idx+= +) { - if (rxport->aliased_addrs[reg_idx] =3D=3D client->addr) + if (rxport->aliased_addrs[reg_idx] =3D=3D addr) break; } =20 if (reg_idx =3D=3D ARRAY_SIZE(rxport->aliased_addrs)) { dev_err(dev, "rx%u: client 0x%02x is not mapped!\n", - rxport->nport, client->addr); + rxport->nport, addr); return; } =20 @@ -1108,12 +1108,12 @@ static void ub960_atr_detach_client(struct i2c_atr = *atr, u32 chan_id, ub960_rxport_write(priv, chan_id, UB960_RR_SLAVE_ALIAS(reg_idx), 0); =20 dev_dbg(dev, "rx%u: client 0x%02x released at slot %u\n", rxport->nport, - client->addr, reg_idx); + addr, reg_idx); } =20 static const struct i2c_atr_ops ub960_atr_ops =3D { - .attach_client =3D ub960_atr_attach_client, - .detach_client =3D ub960_atr_detach_client, + .attach_addr =3D ub960_atr_attach_addr, + .detach_addr =3D ub960_atr_detach_addr, }; =20 static int ub960_init_atr(struct ub960_data *priv) diff --git a/include/linux/i2c-atr.h b/include/linux/i2c-atr.h index 4d5da161c22516b3294e73702ace7a4546cdd588..14c1f9175c0db6a8a9c6ef5d771= ae68361132a76 100644 --- a/include/linux/i2c-atr.h +++ b/include/linux/i2c-atr.h @@ -20,20 +20,20 @@ struct i2c_atr; =20 /** * struct i2c_atr_ops - Callbacks from ATR to the device driver. - * @attach_client: Notify the driver of a new device connected on a child - * bus, with the alias assigned to it. The driver must - * configure the hardware to use the alias. - * @detach_client: Notify the driver of a device getting disconnected. The - * driver must configure the hardware to stop using the - * alias. + * @attach_addr: Notify the driver of a new device connected on a child + * bus, with the alias assigned to it. The driver must + * configure the hardware to use the alias. + * @detach_addr: Notify the driver of a device getting disconnected. The + * driver must configure the hardware to stop using the + * alias. * * All these functions return 0 on success, a negative error code otherwis= e. */ struct i2c_atr_ops { - int (*attach_client)(struct i2c_atr *atr, u32 chan_id, - const struct i2c_client *client, u16 alias); - void (*detach_client)(struct i2c_atr *atr, u32 chan_id, - const struct i2c_client *client); + int (*attach_addr)(struct i2c_atr *atr, u32 chan_id, + u16 addr, u16 alias); + void (*detach_addr)(struct i2c_atr *atr, u32 chan_id, + u16 addr); }; =20 /** --=20 2.47.1 From nobody Wed Feb 5 22:54:05 2025 Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) (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 611702080C1; Tue, 4 Feb 2025 09:29:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738661369; cv=none; b=VSDCbmcZAB0y09KJK8F5UoFOf+mYbMIuF+bsGnsqKGw5kEVJY710LrFgbEJUpaD50Sb9bB2BykgXWqkdU7JXN5KJ4mX1gyO/tTDg9q0ObJVUKs0+frkl34JMr9bgw9TZvNefOejhfIu3TSn30k4Q63nPwd6bUi2axbEq7wdtILY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738661369; c=relaxed/simple; bh=kOHgtMJQnzKTtOt9UOB8BLauX2P0LGwAyEj/Qx4o4sk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fRQH03vJb0zptHRpiDx0ARnyqoX7l3xjdHDEq0tQnWVV3UjJ4AABjpvvdypH9hRUJ7YUNjmNqnFQJ4gugilVGlQbU7Rb8IkXawzbmkMiWhEbiszoAqX8oNKQEDIBjMMXEUWhnD/SsXEkTbyjZFASC7Vya1ABpymGFT01P1DphoA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=YSpyImKx; arc=none smtp.client-ip=217.70.183.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="YSpyImKx" Received: by mail.gandi.net (Postfix) with ESMTPSA id 09EFC44369; Tue, 4 Feb 2025 09:29:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1738661365; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ug2eWia+k3UQyHA7NujRxo07jigzgpiMJPEbVVCS7B4=; b=YSpyImKxu6JERshcVkkasyeV01jsVqToCjQBHbSHfpP2RCrDec7CczudFx9TOhEE2cVBUP 2WQtA0RmLZTaj54aqWEHgxMhpICjMdPn0/RK9dWOAx3EA1AOwZN5YzrwI+SsUwPx7r6Qsi hk1eI7lQF0JdflgExq8DkNGTtIbqUAwuiy6Epd1XR1IOdhAPRKKJdJFLNnSAIocrBHKvN2 OS/uyjufdWGBZICDt/4bgTTt7fBjSF6u4fuqKPfchlEyHgSOi9YdSPnpuaHLJ6EpW6h3YS /PvWSznN/gXMeE2lBwLLUcgT3LnTGe/uVNzR0/F4RLClASQeEOgUdjakYIc/tQ== From: Romain Gantois Date: Tue, 04 Feb 2025 10:29:16 +0100 Subject: [PATCH v7 5/9] i2c: move ATR alias pool to a separate struct 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: <20250204-fpc202-v7-5-78b4b8a35cf1@bootlin.com> References: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com> In-Reply-To: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com> To: Wolfram Sang , Tomi Valkeinen , Luca Ceresoli , Andi Shyti , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Derek Kiernan , Dragan Cvetic , Arnd Bergmann , Greg Kroah-Hartman , Mauro Carvalho Chehab , Linus Walleij , Bartosz Golaszewski Cc: Thomas Petazzoni , Kory Maincent , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-media@vger.kernel.org, linux-gpio@vger.kernel.org, Romain Gantois X-Mailer: b4 0.14.2 X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdduhecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfitefpfffkpdcuggftfghnshhusghstghrihgsvgenuceurghilhhouhhtmecufedtudenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephfffufggtgfgkfhfjgfvvefosehtjeertdertdejnecuhfhrohhmpeftohhmrghinhcuifgrnhhtohhishcuoehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomheqnecuggftrfgrthhtvghrnhepkeelieefteelffeuheevtdetkefhfffhteffkefgtefhkeevudeutdeugfffheegnecukfhppeeltddrkeelrdduieefrdduvdejnecuvehluhhsthgvrhfuihiivgepfeenucfrrghrrghmpehinhgvthepledtrdekledrudeifedruddvjedphhgvlhhopegludelvddrudeikedrtddrudefngdpmhgrihhlfhhrohhmpehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomhdpnhgspghrtghpthhtohepvddvpdhrtghpthhtoheprghrnhgusegrrhhnuggsrdguvgdprhgtphhtthhopehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomhdprhgtphhtthhopehthhhomhgrshdrphgvthgriiiiohhnihessghoohhtlhhinhdrtghomhdprhgtphhtthhopeguvghrvghkrdhkihgvrhhnrghnsegrmhgurdgtohhmpdhrtghpthhtohepthhomhhirdhvrghlkhgvihhnvghns ehiuggvrghsohhnsghorghrugdrtghomhdprhgtphhtthhopegurhgrghgrnhdrtghvvghtihgtsegrmhgurdgtohhmpdhrtghpthhtohepuggvvhhitggvthhrvggvsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheprghnughirdhshhihthhisehkvghrnhgvlhdrohhrgh X-GND-Sasl: romain.gantois@bootlin.com Each I2C address translator (ATR) has a pool of client aliases which can be used as translation targets. Some ATRs have a single alias pool shared by all downstream channels, while others have a separate alias pool for each channel. Currently, this alias pool is represented by the "aliases", "num_aliases", and "use_mask" fields of struct i2c_atr. In preparation for adding per-channel alias pool support, move the "aliases", "num_aliases", "use_mask" and associated lock to a new struct called "struct alias_pool". Signed-off-by: Romain Gantois --- drivers/i2c/i2c-atr.c | 172 +++++++++++++++++++++++++++++++---------------= ---- 1 file changed, 107 insertions(+), 65 deletions(-) diff --git a/drivers/i2c/i2c-atr.c b/drivers/i2c/i2c-atr.c index 118fd7d764dce51de587e274ef452e031ccff52d..e9c6b85e3e4a4ff84fdf9ad139f= 05fdceb441120 100644 --- a/drivers/i2c/i2c-atr.c +++ b/drivers/i2c/i2c-atr.c @@ -34,6 +34,23 @@ struct i2c_atr_alias_pair { u16 alias; }; =20 +/** + * struct i2c_atr_alias_pool - Pool of client aliases available for an ATR. + * @size: Total number of aliases + * + * @lock: Lock protecting @aliases and @use_mask + * @aliases: Array of aliases, must hold exactly @size elements + * @use_mask: Mask of used aliases + */ +struct i2c_atr_alias_pool { + size_t size; + + /* Protects aliases and use_mask */ + spinlock_t lock; + u16 *aliases; + unsigned long *use_mask; +}; + /** * struct i2c_atr_chan - Data for a channel. * @adap: The &struct i2c_adapter for the channel @@ -67,10 +84,7 @@ struct i2c_atr_chan { * @algo: The &struct i2c_algorithm for adapters * @lock: Lock for the I2C bus segment (see &struct i2c_lock_operatio= ns) * @max_adapters: Maximum number of adapters this I2C ATR can have - * @num_aliases: Number of aliases in the aliases array - * @aliases: The aliases array - * @alias_mask_lock: Lock protecting alias_use_mask - * @alias_use_mask: Bitmask for used aliases in aliases array + * @alias_pool: Pool of available client aliases * @i2c_nb: Notifier for remote client add & del events * @adapter: Array of adapters */ @@ -86,17 +100,54 @@ struct i2c_atr { struct mutex lock; int max_adapters; =20 - size_t num_aliases; - const u16 *aliases; - /* Protects alias_use_mask */ - spinlock_t alias_mask_lock; - unsigned long *alias_use_mask; + struct i2c_atr_alias_pool *alias_pool; =20 struct notifier_block i2c_nb; =20 struct i2c_adapter *adapter[] __counted_by(max_adapters); }; =20 +static struct i2c_atr_alias_pool *i2c_atr_alloc_alias_pool(size_t num_alia= ses) +{ + struct i2c_atr_alias_pool *alias_pool; + int ret; + + alias_pool =3D kzalloc(sizeof(*alias_pool), GFP_KERNEL); + if (!alias_pool) + return ERR_PTR(-ENOMEM); + + alias_pool->size =3D num_aliases; + + alias_pool->aliases =3D kcalloc(num_aliases, sizeof(*alias_pool->aliases)= , GFP_KERNEL); + if (!alias_pool->aliases) { + ret =3D -ENOMEM; + goto err_free_alias_pool; + } + + alias_pool->use_mask =3D bitmap_zalloc(num_aliases, GFP_KERNEL); + if (!alias_pool->use_mask) { + ret =3D -ENOMEM; + goto err_free_aliases; + } + + spin_lock_init(&alias_pool->lock); + + return alias_pool; + +err_free_aliases: + kfree(alias_pool->aliases); +err_free_alias_pool: + kfree(alias_pool); + return ERR_PTR(ret); +} + +static void i2c_atr_free_alias_pool(struct i2c_atr_alias_pool *alias_pool) +{ + bitmap_free(alias_pool->use_mask); + kfree(alias_pool->aliases); + kfree(alias_pool); +} + static struct i2c_atr_alias_pair * i2c_atr_find_mapping_by_addr(const struct list_head *list, u16 phys_addr) { @@ -259,44 +310,42 @@ static const struct i2c_lock_operations i2c_atr_lock_= ops =3D { .unlock_bus =3D i2c_atr_unlock_bus, }; =20 -static int i2c_atr_reserve_alias(struct i2c_atr *atr) +static int i2c_atr_reserve_alias(struct i2c_atr_alias_pool *alias_pool) { unsigned long idx; + u16 alias; =20 - spin_lock(&atr->alias_mask_lock); + spin_lock(&alias_pool->lock); =20 - idx =3D find_first_zero_bit(atr->alias_use_mask, atr->num_aliases); - if (idx >=3D atr->num_aliases) { - spin_unlock(&atr->alias_mask_lock); - dev_err(atr->dev, "failed to find a free alias\n"); + idx =3D find_first_zero_bit(alias_pool->use_mask, alias_pool->size); + if (idx >=3D alias_pool->size) { + spin_unlock(&alias_pool->lock); return -EBUSY; } =20 - set_bit(idx, atr->alias_use_mask); + set_bit(idx, alias_pool->use_mask); =20 - spin_unlock(&atr->alias_mask_lock); + alias =3D alias_pool->aliases[idx]; =20 - return atr->aliases[idx]; + spin_unlock(&alias_pool->lock); + return alias; } =20 -static void i2c_atr_release_alias(struct i2c_atr *atr, u16 alias) +static void i2c_atr_release_alias(struct i2c_atr_alias_pool *alias_pool, u= 16 alias) { unsigned int idx; =20 - spin_lock(&atr->alias_mask_lock); + spin_lock(&alias_pool->lock); =20 - for (idx =3D 0; idx < atr->num_aliases; ++idx) { - if (atr->aliases[idx] =3D=3D alias) { - clear_bit(idx, atr->alias_use_mask); - spin_unlock(&atr->alias_mask_lock); + for (idx =3D 0; idx < alias_pool->size; ++idx) { + if (alias_pool->aliases[idx] =3D=3D alias) { + clear_bit(idx, alias_pool->use_mask); + spin_unlock(&alias_pool->lock); return; } } =20 - spin_unlock(&atr->alias_mask_lock); - - /* This should never happen */ - dev_warn(atr->dev, "Unable to find mapped alias\n"); + spin_unlock(&alias_pool->lock); } =20 static int i2c_atr_attach_addr(struct i2c_adapter *adapter, @@ -308,9 +357,11 @@ static int i2c_atr_attach_addr(struct i2c_adapter *ada= pter, u16 alias; int ret; =20 - ret =3D i2c_atr_reserve_alias(atr); - if (ret < 0) + ret =3D i2c_atr_reserve_alias(atr->alias_pool); + if (ret < 0) { + dev_err(atr->dev, "failed to find a free alias\n"); return ret; + } =20 alias =3D ret; =20 @@ -336,7 +387,7 @@ static int i2c_atr_attach_addr(struct i2c_adapter *adap= ter, err_free: kfree(c2a); err_release_alias: - i2c_atr_release_alias(atr, alias); + i2c_atr_release_alias(atr->alias_pool, alias); =20 return ret; } @@ -357,7 +408,7 @@ static void i2c_atr_detach_addr(struct i2c_adapter *ada= pter, return; } =20 - i2c_atr_release_alias(atr, c2a->alias); + i2c_atr_release_alias(atr->alias_pool, c2a->alias); =20 dev_dbg(atr->dev, "chan%u: detached alias 0x%02x from addr 0x%02x\n", @@ -411,12 +462,11 @@ static int i2c_atr_bus_notifier_call(struct notifier_= block *nb, =20 static int i2c_atr_parse_alias_pool(struct i2c_atr *atr) { + struct i2c_atr_alias_pool *alias_pool; struct device *dev =3D atr->dev; - unsigned long *alias_use_mask; size_t num_aliases; unsigned int i; u32 *aliases32; - u16 *aliases16; int ret; =20 ret =3D fwnode_property_count_u32(dev_fwnode(dev), "i2c-alias-pool"); @@ -428,12 +478,23 @@ static int i2c_atr_parse_alias_pool(struct i2c_atr *a= tr) =20 num_aliases =3D ret; =20 - if (!num_aliases) + alias_pool =3D i2c_atr_alloc_alias_pool(num_aliases); + if (IS_ERR(alias_pool)) { + ret =3D PTR_ERR(alias_pool); + dev_err(dev, "Failed to allocate alias pool, err %d\n", ret); + return ret; + } + + atr->alias_pool =3D alias_pool; + + if (!alias_pool->size) return 0; =20 aliases32 =3D kcalloc(num_aliases, sizeof(*aliases32), GFP_KERNEL); - if (!aliases32) - return -ENOMEM; + if (!aliases32) { + ret =3D -ENOMEM; + goto err_free_alias_pool; + } =20 ret =3D fwnode_property_read_u32_array(dev_fwnode(dev), "i2c-alias-pool", aliases32, num_aliases); @@ -443,43 +504,27 @@ static int i2c_atr_parse_alias_pool(struct i2c_atr *a= tr) goto err_free_aliases32; } =20 - aliases16 =3D kcalloc(num_aliases, sizeof(*aliases16), GFP_KERNEL); - if (!aliases16) { - ret =3D -ENOMEM; - goto err_free_aliases32; - } - for (i =3D 0; i < num_aliases; i++) { if (!(aliases32[i] & 0xffff0000)) { - aliases16[i] =3D aliases32[i]; + alias_pool->aliases[i] =3D aliases32[i]; continue; } =20 dev_err(dev, "Failed to parse 'i2c-alias-pool' property: I2C flags are n= ot supported\n"); ret =3D -EINVAL; - goto err_free_aliases16; - } - - alias_use_mask =3D bitmap_zalloc(num_aliases, GFP_KERNEL); - if (!alias_use_mask) { - ret =3D -ENOMEM; - goto err_free_aliases16; + goto err_free_aliases32; } =20 kfree(aliases32); =20 - atr->num_aliases =3D num_aliases; - atr->aliases =3D aliases16; - atr->alias_use_mask =3D alias_use_mask; - - dev_dbg(dev, "i2c-alias-pool has %zu aliases", atr->num_aliases); + dev_dbg(dev, "i2c-alias-pool has %zu aliases\n", alias_pool->size); =20 return 0; =20 -err_free_aliases16: - kfree(aliases16); err_free_aliases32: kfree(aliases32); +err_free_alias_pool: + i2c_atr_free_alias_pool(alias_pool); return ret; } =20 @@ -500,7 +545,6 @@ struct i2c_atr *i2c_atr_new(struct i2c_adapter *parent,= struct device *dev, return ERR_PTR(-ENOMEM); =20 mutex_init(&atr->lock); - spin_lock_init(&atr->alias_mask_lock); =20 atr->parent =3D parent; atr->dev =3D dev; @@ -520,13 +564,12 @@ struct i2c_atr *i2c_atr_new(struct i2c_adapter *paren= t, struct device *dev, atr->i2c_nb.notifier_call =3D i2c_atr_bus_notifier_call; ret =3D bus_register_notifier(&i2c_bus_type, &atr->i2c_nb); if (ret) - goto err_free_aliases; + goto err_free_alias_pool; =20 return atr; =20 -err_free_aliases: - bitmap_free(atr->alias_use_mask); - kfree(atr->aliases); +err_free_alias_pool: + i2c_atr_free_alias_pool(atr->alias_pool); err_destroy_mutex: mutex_destroy(&atr->lock); kfree(atr); @@ -543,8 +586,7 @@ void i2c_atr_delete(struct i2c_atr *atr) WARN_ON(atr->adapter[i]); =20 bus_unregister_notifier(&i2c_bus_type, &atr->i2c_nb); - bitmap_free(atr->alias_use_mask); - kfree(atr->aliases); + i2c_atr_free_alias_pool(atr->alias_pool); mutex_destroy(&atr->lock); kfree(atr); } --=20 2.47.1 From nobody Wed Feb 5 22:54:05 2025 Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) (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 D701E20A5CA; Tue, 4 Feb 2025 09:29:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738661370; cv=none; b=beM+JaSlCKNUpmKfACYmpGl9Varo9fWVkY5F+p+45LoyFfHpJVv9edgDm037274l7c19nm2434bQqi1OZOViz5ny54CBIBT3DX1s+9Tma8HMI40mpHPQAzinnZZSMa1NWVNG112yvAK3XpIyEfrC+wsKF07xELc3GE7cIZvCIyA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738661370; c=relaxed/simple; bh=u6TT5tLgQSB2LcT8eyCO7h0Vll4qkdcUfOlP4+tYeZA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Z87vKxO9xpSI3pWomP1sPkX3zHpGDc0uQqDQEr87G7P6KRO+7YOjjEFsKie0P6KvNMh51WPPJOHtX5o0LtBvUWlv1k4kwSX0EstsCmtLb0+UPSBDfz9kkgpabP7lIWGLnnWOrCaOpSXiKg2Ys9c6hr2Lm9zxWxtST1aj0lkIzks= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=IugucDF9; arc=none smtp.client-ip=217.70.183.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="IugucDF9" Received: by mail.gandi.net (Postfix) with ESMTPSA id 23556433EE; Tue, 4 Feb 2025 09:29:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1738661366; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=v40zWBlUzwQVJicfUqDAPQECoG5UIa9/l4Qp0jQwMv8=; b=IugucDF90pFK4Cge1l2VjzUDk5srWoaXlYNDPfimUSNQVwwAqo8UHf3UnHAMBSrltq3t5m lPbEKzqi0GuBhihrdwnUNDH0nGWLvwYYZzKnfd4wToFZAA0ql7OP4VHCkWhoRF6aTnNE5H SmSCoku0b/RXKjLW7OYOV9LcmT9r64XGPDPKC4gWo8w10zxQFK7liyzm550zmJyY1zXdMc jLwz0nfkTQ+8v7DdceICpteo9LphsN3CVjRRXUTk6eNfnvzw2ypFM7Gxy9AKIg1UV3uDVt cJr36aeKMxms4pFMOBRl9oP8nWfqqo6mH28ZSQl2hJvwf6SjZAjjK23vSIznIg== From: Romain Gantois Date: Tue, 04 Feb 2025 10:29:17 +0100 Subject: [PATCH v7 6/9] i2c: rename field 'alias_list' of struct i2c_atr_chan to 'alias_pairs' 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: <20250204-fpc202-v7-6-78b4b8a35cf1@bootlin.com> References: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com> In-Reply-To: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com> To: Wolfram Sang , Tomi Valkeinen , Luca Ceresoli , Andi Shyti , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Derek Kiernan , Dragan Cvetic , Arnd Bergmann , Greg Kroah-Hartman , Mauro Carvalho Chehab , Linus Walleij , Bartosz Golaszewski Cc: Thomas Petazzoni , Kory Maincent , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-media@vger.kernel.org, linux-gpio@vger.kernel.org, Romain Gantois X-Mailer: b4 0.14.2 X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdduhecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfitefpfffkpdcuggftfghnshhusghstghrihgsvgenuceurghilhhouhhtmecufedtudenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephfffufggtgfgkfhfjgfvvefosehtjeertdertdejnecuhfhrohhmpeftohhmrghinhcuifgrnhhtohhishcuoehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomheqnecuggftrfgrthhtvghrnhepkeelieefteelffeuheevtdetkefhfffhteffkefgtefhkeevudeutdeugfffheegnecukfhppeeltddrkeelrdduieefrdduvdejnecuvehluhhsthgvrhfuihiivgepfeenucfrrghrrghmpehinhgvthepledtrdekledrudeifedruddvjedphhgvlhhopegludelvddrudeikedrtddrudefngdpmhgrihhlfhhrohhmpehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomhdpnhgspghrtghpthhtohepvddvpdhrtghpthhtoheprghrnhgusegrrhhnuggsrdguvgdprhgtphhtthhopehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomhdprhgtphhtthhopehthhhomhgrshdrphgvthgriiiiohhnihessghoohhtlhhinhdrtghomhdprhgtphhtthhopeguvghrvghkrdhkihgvrhhnrghnsegrmhgurdgtohhmpdhrtghpthhtohepthhomhhirdhvrghlkhgvihhnvghns ehiuggvrghsohhnsghorghrugdrtghomhdprhgtphhtthhopegurhgrghgrnhdrtghvvghtihgtsegrmhgurdgtohhmpdhrtghpthhtohepuggvvhhitggvthhrvggvsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheprghnughirdhshhihthhisehkvghrnhgvlhdrohhrgh X-GND-Sasl: romain.gantois@bootlin.com The "alias_list" field of struct i2c_atr_chan describes translation table entries programmed in the ATR channel. This terminology will become more confusing when per-channel alias pool support is introduced, as struct i2c_atr_chan will gain a new field called "alias_pool", which will describe aliases which are available to the ATR channel. Rename the "alias_list" field to "alias_pairs" to clearly distinguish it from the future "alias_pool" field. No functional change is intended. Signed-off-by: Romain Gantois --- drivers/i2c/i2c-atr.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/drivers/i2c/i2c-atr.c b/drivers/i2c/i2c-atr.c index e9c6b85e3e4a4ff84fdf9ad139f05fdceb441120..01f8c6f6e68a0a6012101a201c3= fd8c7c0f25b47 100644 --- a/drivers/i2c/i2c-atr.c +++ b/drivers/i2c/i2c-atr.c @@ -56,7 +56,7 @@ struct i2c_atr_alias_pool { * @adap: The &struct i2c_adapter for the channel * @atr: The parent I2C ATR * @chan_id: The ID of this channel - * @alias_list: List of @struct i2c_atr_alias_pair containing the + * @alias_pairs: List of @struct i2c_atr_alias_pair containing the * assigned aliases * @orig_addrs_lock: Mutex protecting @orig_addrs * @orig_addrs: Buffer used to store the original addresses during tr= ansmit @@ -67,7 +67,7 @@ struct i2c_atr_chan { struct i2c_atr *atr; u32 chan_id; =20 - struct list_head alias_list; + struct list_head alias_pairs; =20 /* Lock orig_addrs during xfer */ struct mutex orig_addrs_lock; @@ -192,7 +192,7 @@ static int i2c_atr_map_msgs(struct i2c_atr_chan *chan, = struct i2c_msg *msgs, for (i =3D 0; i < num; i++) { chan->orig_addrs[i] =3D msgs[i].addr; =20 - c2a =3D i2c_atr_find_mapping_by_addr(&chan->alias_list, + c2a =3D i2c_atr_find_mapping_by_addr(&chan->alias_pairs, msgs[i].addr); if (!c2a) { dev_err(atr->dev, "client 0x%02x not mapped!\n", @@ -262,7 +262,7 @@ static int i2c_atr_smbus_xfer(struct i2c_adapter *adap,= u16 addr, struct i2c_adapter *parent =3D atr->parent; struct i2c_atr_alias_pair *c2a; =20 - c2a =3D i2c_atr_find_mapping_by_addr(&chan->alias_list, addr); + c2a =3D i2c_atr_find_mapping_by_addr(&chan->alias_pairs, addr); if (!c2a) { dev_err(atr->dev, "client 0x%02x not mapped!\n", addr); return -ENXIO; @@ -380,7 +380,7 @@ static int i2c_atr_attach_addr(struct i2c_adapter *adap= ter, =20 c2a->addr =3D addr; c2a->alias =3D alias; - list_add(&c2a->node, &chan->alias_list); + list_add(&c2a->node, &chan->alias_pairs); =20 return 0; =20 @@ -401,7 +401,7 @@ static void i2c_atr_detach_addr(struct i2c_adapter *ada= pter, =20 atr->ops->detach_addr(atr, chan->chan_id, addr); =20 - c2a =3D i2c_atr_find_mapping_by_addr(&chan->alias_list, addr); + c2a =3D i2c_atr_find_mapping_by_addr(&chan->alias_pairs, addr); if (!c2a) { /* This should never happen */ dev_warn(atr->dev, "Unable to find address mapping\n"); @@ -621,7 +621,7 @@ int i2c_atr_add_adapter(struct i2c_atr *atr, u32 chan_i= d, =20 chan->atr =3D atr; chan->chan_id =3D chan_id; - INIT_LIST_HEAD(&chan->alias_list); + INIT_LIST_HEAD(&chan->alias_pairs); mutex_init(&chan->orig_addrs_lock); =20 snprintf(chan->adap.name, sizeof(chan->adap.name), "i2c-%d-atr-%d", --=20 2.47.1 From nobody Wed Feb 5 22:54:05 2025 Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) (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 E25A820B1EB; Tue, 4 Feb 2025 09:29:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738661373; cv=none; b=beIzWI+ui2SkW8PeyQ2z7Czv/7HN1iZMZRWAwgDLHkPvLowiIc1GGwyb4YPaF0+39pu5dqTiPVsMUvVrosEfXztwaFRPqEb7afQiFvKwEc82akZAdomcoad4QL/Io997H33MtVVNVs8gNdUf4so4drdugc31QgzNbVUDtMFO/zY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738661373; c=relaxed/simple; bh=9UyTqG6Scxnw2JCbscV5mgn9TTmIFbUHcnqLy7WyfA0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DWEdUBgK/hvV7uovCMr3pB7T6Cte+NFWpfTC5R/wQXCWdeor4I/uVLPYUP6YmVaA+x/j+1+xJ+5G2K4g+EDwyNJ7DUynY/ZI3RQETl+oRhnWiHi8l3c61obiOoTAyeuxnXQRCbOMSTDS4dSFKev1tSorHOjmsup0apz00ijq5r4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=L7pEl6Ib; arc=none smtp.client-ip=217.70.183.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="L7pEl6Ib" Received: by mail.gandi.net (Postfix) with ESMTPSA id 3183743424; Tue, 4 Feb 2025 09:29:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1738661367; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JO7X/XjeAWCq1N/Kk05dh0ZWAY65eHnekvIueQlv25A=; b=L7pEl6IbZHpoEX9u9TnXDjxD/ATZhmTikEhkeAflqD828s4ZXoDOEk/Gu46PLBPUa4DTXd TWoK1+PfW6BlgZo/fPSyhkcJ65gnGDWYNz7jYgJlyIbAtumL7mum8LncSpVdT9U9hbqPn1 OJC5hshjlzLDdKkXCPmm8nZ+RW4//wEdNkOcZR3x4w8x4lwmBSwJyUXQZ05+ng6k2sTRww hubRgqF0vVhDzeyD1nByCs8vXVueDBMLwfDN84l4+mReqqa0DbQ9FihI9OHEKLufpDCa/+ /9dWQ/dAwBWEbbQ/Im8rUUfzNIHsrEYIe5M/q8XufsrhATifZfOIfeHBF8fR4w== From: Romain Gantois Date: Tue, 04 Feb 2025 10:29:18 +0100 Subject: [PATCH v7 7/9] i2c: support per-channel ATR alias pools 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: <20250204-fpc202-v7-7-78b4b8a35cf1@bootlin.com> References: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com> In-Reply-To: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com> To: Wolfram Sang , Tomi Valkeinen , Luca Ceresoli , Andi Shyti , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Derek Kiernan , Dragan Cvetic , Arnd Bergmann , Greg Kroah-Hartman , Mauro Carvalho Chehab , Linus Walleij , Bartosz Golaszewski Cc: Thomas Petazzoni , Kory Maincent , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-media@vger.kernel.org, linux-gpio@vger.kernel.org, Romain Gantois X-Mailer: b4 0.14.2 X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdduhecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfitefpfffkpdcuggftfghnshhusghstghrihgsvgenuceurghilhhouhhtmecufedtudenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephfffufggtgfgkfhfjgfvvefosehtjeertdertdejnecuhfhrohhmpeftohhmrghinhcuifgrnhhtohhishcuoehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomheqnecuggftrfgrthhtvghrnhepkeelieefteelffeuheevtdetkefhfffhteffkefgtefhkeevudeutdeugfffheegnecukfhppeeltddrkeelrdduieefrdduvdejnecuvehluhhsthgvrhfuihiivgepfeenucfrrghrrghmpehinhgvthepledtrdekledrudeifedruddvjedphhgvlhhopegludelvddrudeikedrtddrudefngdpmhgrihhlfhhrohhmpehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomhdpnhgspghrtghpthhtohepvddvpdhrtghpthhtoheprghrnhgusegrrhhnuggsrdguvgdprhgtphhtthhopehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomhdprhgtphhtthhopehthhhomhgrshdrphgvthgriiiiohhnihessghoohhtlhhinhdrtghomhdprhgtphhtthhopeguvghrvghkrdhkihgvrhhnrghnsegrmhgurdgtohhmpdhrtghpthhtohepthhomhhirdhvrghlkhgvihhnvghns ehiuggvrghsohhnsghorghrugdrtghomhdprhgtphhtthhopegurhgrghgrnhdrtghvvghtihgtsegrmhgurdgtohhmpdhrtghpthhtohepuggvvhhitggvthhrvggvsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheprghnughirdhshhihthhisehkvghrnhgvlhdrohhrgh X-GND-Sasl: romain.gantois@bootlin.com Some I2C address translators (ATRs) assign each of their remote peripheral aliases to a specific channel. To properly handle these devices, add support for having separate alias pools for each ATR channel. This is achieved by allowing callers of i2c_atr_add_adapter to pass an optional alias list. If present, this list will be used to populate the channel's alias pool. Otherwise, the common alias pool will be used. Signed-off-by: Romain Gantois --- drivers/i2c/i2c-atr.c | 74 ++++++++++++++++++++++++++++++---------= ---- drivers/media/i2c/ds90ub913.c | 9 ++++-- drivers/media/i2c/ds90ub953.c | 9 ++++-- include/linux/i2c-atr.h | 34 ++++++++++++++------ 4 files changed, 91 insertions(+), 35 deletions(-) diff --git a/drivers/i2c/i2c-atr.c b/drivers/i2c/i2c-atr.c index 01f8c6f6e68a0a6012101a201c3fd8c7c0f25b47..6b4cf979b86e0dcbba3bd9dbb29= 7a8b5e6216dd5 100644 --- a/drivers/i2c/i2c-atr.c +++ b/drivers/i2c/i2c-atr.c @@ -37,6 +37,7 @@ struct i2c_atr_alias_pair { /** * struct i2c_atr_alias_pool - Pool of client aliases available for an ATR. * @size: Total number of aliases + * @shared: Indicates if this alias pool is shared by multiple channels * * @lock: Lock protecting @aliases and @use_mask * @aliases: Array of aliases, must hold exactly @size elements @@ -44,6 +45,7 @@ struct i2c_atr_alias_pair { */ struct i2c_atr_alias_pool { size_t size; + bool shared; =20 /* Protects aliases and use_mask */ spinlock_t lock; @@ -58,6 +60,8 @@ struct i2c_atr_alias_pool { * @chan_id: The ID of this channel * @alias_pairs: List of @struct i2c_atr_alias_pair containing the * assigned aliases + * @alias_pool: Pool of available client aliases + * * @orig_addrs_lock: Mutex protecting @orig_addrs * @orig_addrs: Buffer used to store the original addresses during tr= ansmit * @orig_addrs_size: Size of @orig_addrs @@ -68,6 +72,7 @@ struct i2c_atr_chan { u32 chan_id; =20 struct list_head alias_pairs; + struct i2c_atr_alias_pool *alias_pool; =20 /* Lock orig_addrs during xfer */ struct mutex orig_addrs_lock; @@ -84,7 +89,7 @@ struct i2c_atr_chan { * @algo: The &struct i2c_algorithm for adapters * @lock: Lock for the I2C bus segment (see &struct i2c_lock_operatio= ns) * @max_adapters: Maximum number of adapters this I2C ATR can have - * @alias_pool: Pool of available client aliases + * @alias_pool: Optional common pool of available client aliases * @i2c_nb: Notifier for remote client add & del events * @adapter: Array of adapters */ @@ -107,7 +112,7 @@ struct i2c_atr { struct i2c_adapter *adapter[] __counted_by(max_adapters); }; =20 -static struct i2c_atr_alias_pool *i2c_atr_alloc_alias_pool(size_t num_alia= ses) +static struct i2c_atr_alias_pool *i2c_atr_alloc_alias_pool(size_t num_alia= ses, bool shared) { struct i2c_atr_alias_pool *alias_pool; int ret; @@ -130,6 +135,8 @@ static struct i2c_atr_alias_pool *i2c_atr_alloc_alias_p= ool(size_t num_aliases) goto err_free_aliases; } =20 + alias_pool->shared =3D shared; + spin_lock_init(&alias_pool->lock); =20 return alias_pool; @@ -357,7 +364,7 @@ static int i2c_atr_attach_addr(struct i2c_adapter *adap= ter, u16 alias; int ret; =20 - ret =3D i2c_atr_reserve_alias(atr->alias_pool); + ret =3D i2c_atr_reserve_alias(chan->alias_pool); if (ret < 0) { dev_err(atr->dev, "failed to find a free alias\n"); return ret; @@ -387,7 +394,7 @@ static int i2c_atr_attach_addr(struct i2c_adapter *adap= ter, err_free: kfree(c2a); err_release_alias: - i2c_atr_release_alias(atr->alias_pool, alias); + i2c_atr_release_alias(chan->alias_pool, alias); =20 return ret; } @@ -408,7 +415,7 @@ static void i2c_atr_detach_addr(struct i2c_adapter *ada= pter, return; } =20 - i2c_atr_release_alias(atr->alias_pool, c2a->alias); + i2c_atr_release_alias(chan->alias_pool, c2a->alias); =20 dev_dbg(atr->dev, "chan%u: detached alias 0x%02x from addr 0x%02x\n", @@ -469,16 +476,20 @@ static int i2c_atr_parse_alias_pool(struct i2c_atr *a= tr) u32 *aliases32; int ret; =20 - ret =3D fwnode_property_count_u32(dev_fwnode(dev), "i2c-alias-pool"); - if (ret < 0) { - dev_err(dev, "Failed to count 'i2c-alias-pool' property: %d\n", - ret); - return ret; - } + if (!fwnode_property_present(dev_fwnode(dev), "i2c-alias-pool")) { + num_aliases =3D 0; + } else { + ret =3D fwnode_property_count_u32(dev_fwnode(dev), "i2c-alias-pool"); + if (ret < 0) { + dev_err(dev, "Failed to count 'i2c-alias-pool' property: %d\n", + ret); + return ret; + } =20 - num_aliases =3D ret; + num_aliases =3D ret; + } =20 - alias_pool =3D i2c_atr_alloc_alias_pool(num_aliases); + alias_pool =3D i2c_atr_alloc_alias_pool(num_aliases, true); if (IS_ERR(alias_pool)) { ret =3D PTR_ERR(alias_pool); dev_err(dev, "Failed to allocate alias pool, err %d\n", ret); @@ -592,15 +603,15 @@ void i2c_atr_delete(struct i2c_atr *atr) } EXPORT_SYMBOL_NS_GPL(i2c_atr_delete, "I2C_ATR"); =20 -int i2c_atr_add_adapter(struct i2c_atr *atr, u32 chan_id, - struct device *adapter_parent, - struct fwnode_handle *bus_handle) +int i2c_atr_add_adapter(struct i2c_atr *atr, struct i2c_atr_adap_desc *des= c) { + struct fwnode_handle *bus_handle =3D desc->bus_handle; struct i2c_adapter *parent =3D atr->parent; + char symlink_name[ATR_MAX_SYMLINK_LEN]; struct device *dev =3D atr->dev; + u32 chan_id =3D desc->chan_id; struct i2c_atr_chan *chan; - char symlink_name[ATR_MAX_SYMLINK_LEN]; - int ret; + int ret, idx; =20 if (chan_id >=3D atr->max_adapters) { dev_err(dev, "No room for more i2c-atr adapters\n"); @@ -616,8 +627,8 @@ int i2c_atr_add_adapter(struct i2c_atr *atr, u32 chan_i= d, if (!chan) return -ENOMEM; =20 - if (!adapter_parent) - adapter_parent =3D dev; + if (!desc->parent) + desc->parent =3D dev; =20 chan->atr =3D atr; chan->chan_id =3D chan_id; @@ -629,7 +640,7 @@ int i2c_atr_add_adapter(struct i2c_atr *atr, u32 chan_i= d, chan->adap.owner =3D THIS_MODULE; chan->adap.algo =3D &atr->algo; chan->adap.algo_data =3D chan; - chan->adap.dev.parent =3D adapter_parent; + chan->adap.dev.parent =3D desc->parent; chan->adap.retries =3D parent->retries; chan->adap.timeout =3D parent->timeout; chan->adap.quirks =3D parent->quirks; @@ -656,13 +667,26 @@ int i2c_atr_add_adapter(struct i2c_atr *atr, u32 chan= _id, fwnode_handle_put(atr_node); } =20 + if (desc->num_aliases > 0) { + chan->alias_pool =3D i2c_atr_alloc_alias_pool(desc->num_aliases, false); + if (IS_ERR(chan->alias_pool)) { + ret =3D PTR_ERR(chan->alias_pool); + goto err_fwnode_put; + } + + for (idx =3D 0; idx < desc->num_aliases; idx++) + chan->alias_pool->aliases[idx] =3D desc->aliases[idx]; + } else { + chan->alias_pool =3D atr->alias_pool; + } + atr->adapter[chan_id] =3D &chan->adap; =20 ret =3D i2c_add_adapter(&chan->adap); if (ret) { dev_err(dev, "failed to add atr-adapter %u (error=3D%d)\n", chan_id, ret); - goto err_fwnode_put; + goto err_free_alias_pool; } =20 snprintf(symlink_name, sizeof(symlink_name), "channel-%u", @@ -679,6 +703,9 @@ int i2c_atr_add_adapter(struct i2c_atr *atr, u32 chan_i= d, =20 return 0; =20 +err_free_alias_pool: + if (!chan->alias_pool->shared) + i2c_atr_free_alias_pool(chan->alias_pool); err_fwnode_put: fwnode_handle_put(dev_fwnode(&chan->adap.dev)); mutex_destroy(&chan->orig_addrs_lock); @@ -711,6 +738,9 @@ void i2c_atr_del_adapter(struct i2c_atr *atr, u32 chan_= id) =20 i2c_del_adapter(adap); =20 + if (!chan->alias_pool->shared) + i2c_atr_free_alias_pool(chan->alias_pool); + atr->adapter[chan_id] =3D NULL; =20 fwnode_handle_put(fwnode); diff --git a/drivers/media/i2c/ds90ub913.c b/drivers/media/i2c/ds90ub913.c index fd2d2d5272bfb688f00d7bf5a109e978f6c322e6..bedfeb5f9273ac48d29b480cc7c= ee67e55e926a2 100644 --- a/drivers/media/i2c/ds90ub913.c +++ b/drivers/media/i2c/ds90ub913.c @@ -670,6 +670,7 @@ static int ub913_i2c_master_init(struct ub913_data *pri= v) static int ub913_add_i2c_adapter(struct ub913_data *priv) { struct device *dev =3D &priv->client->dev; + struct i2c_atr_adap_desc desc =3D { }; struct fwnode_handle *i2c_handle; int ret; =20 @@ -677,8 +678,12 @@ static int ub913_add_i2c_adapter(struct ub913_data *pr= iv) if (!i2c_handle) return 0; =20 - ret =3D i2c_atr_add_adapter(priv->plat_data->atr, priv->plat_data->port, - dev, i2c_handle); + desc.chan_id =3D priv->plat_data->port; + desc.parent =3D dev; + desc.bus_handle =3D i2c_handle; + desc.num_aliases =3D 0; + + ret =3D i2c_atr_add_adapter(priv->plat_data->atr, &desc); =20 fwnode_handle_put(i2c_handle); =20 diff --git a/drivers/media/i2c/ds90ub953.c b/drivers/media/i2c/ds90ub953.c index 46569381b332de3dfd89a0720ada64cdfa3297de..11d541bdc80e054c83a8c84c2f2= b8d400db74a4a 100644 --- a/drivers/media/i2c/ds90ub953.c +++ b/drivers/media/i2c/ds90ub953.c @@ -1122,6 +1122,7 @@ static int ub953_register_clkout(struct ub953_data *p= riv) static int ub953_add_i2c_adapter(struct ub953_data *priv) { struct device *dev =3D &priv->client->dev; + struct i2c_atr_adap_desc desc =3D { }; struct fwnode_handle *i2c_handle; int ret; =20 @@ -1129,8 +1130,12 @@ static int ub953_add_i2c_adapter(struct ub953_data *= priv) if (!i2c_handle) return 0; =20 - ret =3D i2c_atr_add_adapter(priv->plat_data->atr, priv->plat_data->port, - dev, i2c_handle); + desc.chan_id =3D priv->plat_data->port; + desc.parent =3D dev; + desc.bus_handle =3D i2c_handle; + desc.num_aliases =3D 0; + + ret =3D i2c_atr_add_adapter(priv->plat_data->atr, &desc); =20 fwnode_handle_put(i2c_handle); =20 diff --git a/include/linux/i2c-atr.h b/include/linux/i2c-atr.h index 14c1f9175c0db6a8a9c6ef5d771ae68361132a76..1c3a5bcd939fc56f4a6ca1b6a5c= c0ac2c17083b7 100644 --- a/include/linux/i2c-atr.h +++ b/include/linux/i2c-atr.h @@ -36,6 +36,29 @@ struct i2c_atr_ops { u16 addr); }; =20 +/** + * struct i2c_atr_adap_desc - An ATR downstream bus descriptor + * @chan_id: Index of the new adapter (0 .. max_adapters-1). This = value is + * passed to the callbacks in `struct i2c_atr_ops`. + * @parent: The device used as the parent of the new i2c adapter, = or NULL + * to use the i2c-atr device as the parent. + * @bus_handle: The fwnode handle that points to the adapter's i2c + * peripherals, or NULL. + * @num_aliases: The number of aliases in this adapter's private alias = pool. Set + * to zero if this adapter uses the ATR's global alias po= ol. + * @aliases: An optional array of private aliases used by the adapt= er + * instead of the ATR's global pool of aliases. Must cont= ain + * exactly num_aliases entries if num_aliases > 0, is ign= ored + * otherwise. + */ +struct i2c_atr_adap_desc { + u32 chan_id; + struct device *parent; + struct fwnode_handle *bus_handle; + size_t num_aliases; + u16 *aliases; +}; + /** * i2c_atr_new() - Allocate and initialize an I2C ATR helper. * @parent: The parent (upstream) adapter @@ -65,12 +88,7 @@ void i2c_atr_delete(struct i2c_atr *atr); /** * i2c_atr_add_adapter - Create a child ("downstream") I2C bus. * @atr: The I2C ATR - * @chan_id: Index of the new adapter (0 .. max_adapters-1). This valu= e is - * passed to the callbacks in `struct i2c_atr_ops`. - * @adapter_parent: The device used as the parent of the new i2c adapter, = or NULL - * to use the i2c-atr device as the parent. - * @bus_handle: The fwnode handle that points to the adapter's i2c - * peripherals, or NULL. + * @desc: An ATR adapter descriptor * * After calling this function a new i2c bus will appear. Adding and remov= ing * devices on the downstream bus will result in calls to the @@ -85,9 +103,7 @@ void i2c_atr_delete(struct i2c_atr *atr); * * Return: 0 on success, a negative error code otherwise. */ -int i2c_atr_add_adapter(struct i2c_atr *atr, u32 chan_id, - struct device *adapter_parent, - struct fwnode_handle *bus_handle); +int i2c_atr_add_adapter(struct i2c_atr *atr, struct i2c_atr_adap_desc *des= c); =20 /** * i2c_atr_del_adapter - Remove a child ("downstream") I2C bus added by --=20 2.47.1 From nobody Wed Feb 5 22:54:05 2025 Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) (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 C5B5F20C00B; Tue, 4 Feb 2025 09:29:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738661372; cv=none; b=BcoPPh4Q+2dhbKyevsf1GsZzyF/kj7WD8+94XND9kcrrIpeJZQd49i0wBdQxkeqqKhzAVjGz/p/dS50AtDSHLOIxgIJ9/L8oaLinQmPzhewSOxuRiXXs0Ii8XGPsO54tBMA85mWadOkNLMeidHW2Xx0BL3lLJ8g4HlqZ+Dnwufc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738661372; c=relaxed/simple; bh=6u6nId+12BH95CMZTPIoLH/Yo7DF/AjT6qsjLLZOr7s=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=rbsaXwvwKs8uqjwm50AmQJCKdxwt3V7BnA5nQ7598Snq4HA1YHLGqf7iRBVOjmSW+cInfoNIu/MATEIkfi8Px0pjNTAmh3+N0yj0fVDzVSY5trO4fl1xQESdWbQnVvUPcE5K66//5M3D7byiWZux7eWhCqbV8oPbenPx+jRLZuQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=ThRxFsYe; arc=none smtp.client-ip=217.70.183.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="ThRxFsYe" Received: by mail.gandi.net (Postfix) with ESMTPSA id 5C63E433ED; Tue, 4 Feb 2025 09:29:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1738661368; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CpBR5RLshQdrbdLflI2XooigLD05TtTFhbUTNcnU0nI=; b=ThRxFsYe81gU7UlbprOn9zF0uVgechDyDfUvwmjpYx0Fylm+z6cHqge2EA92EC007P2rAt pRGq9kg0RziS5T92aJsgTCJUsI68ar+0+b9RHzbIBckhILo0UxH8INHBssizoyXkbFqhsx Fhzia0spjFAp4rHZDT8+/uAxhp0qzSIjqDrLmyx8jzEnAukFzJpx6FemADw+NCR2phi0Xe L11qyHdxtDOmhAnprTqOoKWF1fAS1z7WN5Qz7MJJBh9yekkHsm9rWBoLeMxIj6RjCYe1vk U6eWIwyfVQT1x9LD66ixpgjEHKb4qfoV3ae7hB1QxsNL4EsDd6i2rZ7hJtaEUw== From: Romain Gantois Date: Tue, 04 Feb 2025 10:29:19 +0100 Subject: [PATCH v7 8/9] i2c: Support dynamic address translation 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: <20250204-fpc202-v7-8-78b4b8a35cf1@bootlin.com> References: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com> In-Reply-To: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com> To: Wolfram Sang , Tomi Valkeinen , Luca Ceresoli , Andi Shyti , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Derek Kiernan , Dragan Cvetic , Arnd Bergmann , Greg Kroah-Hartman , Mauro Carvalho Chehab , Linus Walleij , Bartosz Golaszewski Cc: Thomas Petazzoni , Kory Maincent , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-media@vger.kernel.org, linux-gpio@vger.kernel.org, Romain Gantois X-Mailer: b4 0.14.2 X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdduhecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfitefpfffkpdcuggftfghnshhusghstghrihgsvgenuceurghilhhouhhtmecufedtudenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephfffufggtgfgkfhfjgfvvefosehtjeertdertdejnecuhfhrohhmpeftohhmrghinhcuifgrnhhtohhishcuoehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomheqnecuggftrfgrthhtvghrnhepkeelieefteelffeuheevtdetkefhfffhteffkefgtefhkeevudeutdeugfffheegnecukfhppeeltddrkeelrdduieefrdduvdejnecuvehluhhsthgvrhfuihiivgepfeenucfrrghrrghmpehinhgvthepledtrdekledrudeifedruddvjedphhgvlhhopegludelvddrudeikedrtddrudefngdpmhgrihhlfhhrohhmpehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomhdpnhgspghrtghpthhtohepvddvpdhrtghpthhtoheprghrnhgusegrrhhnuggsrdguvgdprhgtphhtthhopehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomhdprhgtphhtthhopehthhhomhgrshdrphgvthgriiiiohhnihessghoohhtlhhinhdrtghomhdprhgtphhtthhopeguvghrvghkrdhkihgvrhhnrghnsegrmhgurdgtohhmpdhrtghpthhtohepthhomhhirdhvrghlkhgvihhnvghns ehiuggvrghsohhnsghorghrugdrtghomhdprhgtphhtthhopegurhgrghgrnhdrtghvvghtihgtsegrmhgurdgtohhmpdhrtghpthhtohepuggvvhhitggvthhrvggvsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheprghnughirdhshhihthhisehkvghrnhgvlhdrohhrgh X-GND-Sasl: romain.gantois@bootlin.com The i2c-atr module keeps a list of associations between I2C client aliases and I2C addresses. This represents the address translation table which is programmed into an ATR channel at any given time. This list is only updated when a new client is bound to the channel. However in some cases, an ATR channel can have more downstream clients than available aliases. One example of this is an SFP module that is bound to an FPC202 port. The FPC202 port can only access up to two logical I2C addresses. However, the SFP module may expose up to three logical I2C addresses: its EEPROM on 7-bit addresses 0x50 and 0x51, and a PHY transceiver on address 0x56. In cases like these, it is necessary to reconfigure the channel's translation table on the fly, so that all three I2C addresses can be accessed when needed. As there are currently no known ATR's which do not support dynamic address translation, this feature can be enabled by default without breaking existing use cases. Modify the i2c-atr module to provide on-the-fly address translation. This is achieved by modifying an ATR channel's translation table whenever an I2C transaction with unmapped clients is requested. Add a mutex to protect alias_list. This prevents i2c_atr_dynamic_attach/detach_addr from racing with the bus notifier handler to modify alias_list. Signed-off-by: Romain Gantois --- drivers/i2c/i2c-atr.c | 249 +++++++++++++++++++++++++++++++++++++---------= ---- 1 file changed, 188 insertions(+), 61 deletions(-) diff --git a/drivers/i2c/i2c-atr.c b/drivers/i2c/i2c-atr.c index 6b4cf979b86e0dcbba3bd9dbb297a8b5e6216dd5..1a6ff47b420020feb9bf4111d49= e2f3f1dae46e7 100644 --- a/drivers/i2c/i2c-atr.c +++ b/drivers/i2c/i2c-atr.c @@ -16,6 +16,7 @@ #include #include #include +#include =20 #define ATR_MAX_ADAPTERS 100 /* Just a sanity limit */ #define ATR_MAX_SYMLINK_LEN 11 /* Longest name is 10 chars: "channel-99" */ @@ -27,9 +28,17 @@ * @alias: I2C alias address assigned by the driver. * This is the address that will be used to issue I2C transactions * on the parent (physical) bus. + * @fixed: Alias pair cannot be replaced during dynamic address attachmen= t. + * This flag is necessary for situations where a single I2C trans= action + * contains more distinct target addresses than the ATR channel c= an handle. + * It marks addresses that have already been attached to an alias= so + * that their alias pair is not evicted by a subsequent address i= n the same + * transaction. + * */ struct i2c_atr_alias_pair { struct list_head node; + bool fixed; u16 addr; u16 alias; }; @@ -58,6 +67,7 @@ struct i2c_atr_alias_pool { * @adap: The &struct i2c_adapter for the channel * @atr: The parent I2C ATR * @chan_id: The ID of this channel + * @alias_pairs_lock: Mutex protecting @alias_pairs * @alias_pairs: List of @struct i2c_atr_alias_pair containing the * assigned aliases * @alias_pool: Pool of available client aliases @@ -71,6 +81,8 @@ struct i2c_atr_chan { struct i2c_atr *atr; u32 chan_id; =20 + /* Lock alias_pairs during attach/detach */ + struct mutex alias_pairs_lock; struct list_head alias_pairs; struct i2c_atr_alias_pool *alias_pool; =20 @@ -155,16 +167,132 @@ static void i2c_atr_free_alias_pool(struct i2c_atr_a= lias_pool *alias_pool) kfree(alias_pool); } =20 +/* Must be called with alias_pairs_lock held */ +static struct i2c_atr_alias_pair *i2c_atr_create_c2a(struct i2c_atr_chan *= chan, + u16 alias, u16 addr) +{ + struct i2c_atr_alias_pair *c2a; + + lockdep_assert_held(&chan->alias_pairs_lock); + + c2a =3D kzalloc(sizeof(*c2a), GFP_KERNEL); + if (!c2a) + return NULL; + + c2a->addr =3D addr; + c2a->alias =3D alias; + + list_add(&c2a->node, &chan->alias_pairs); + + return c2a; +} + +/* Must be called with alias_pairs_lock held */ +static void i2c_atr_destroy_c2a(struct i2c_atr_alias_pair **pc2a) +{ + list_del(&(*pc2a)->node); + kfree(*pc2a); + *pc2a =3D NULL; +} + +static int i2c_atr_reserve_alias(struct i2c_atr_alias_pool *alias_pool) +{ + unsigned long idx; + u16 alias; + + spin_lock(&alias_pool->lock); + + idx =3D find_first_zero_bit(alias_pool->use_mask, alias_pool->size); + if (idx >=3D alias_pool->size) { + spin_unlock(&alias_pool->lock); + return -EBUSY; + } + + set_bit(idx, alias_pool->use_mask); + + alias =3D alias_pool->aliases[idx]; + + spin_unlock(&alias_pool->lock); + return alias; +} + +static void i2c_atr_release_alias(struct i2c_atr_alias_pool *alias_pool, u= 16 alias) +{ + unsigned int idx; + + spin_lock(&alias_pool->lock); + + for (idx =3D 0; idx < alias_pool->size; ++idx) { + if (alias_pool->aliases[idx] =3D=3D alias) { + clear_bit(idx, alias_pool->use_mask); + spin_unlock(&alias_pool->lock); + return; + } + } + + spin_unlock(&alias_pool->lock); +} + +/* Must be called with alias_pairs_lock held */ static struct i2c_atr_alias_pair * -i2c_atr_find_mapping_by_addr(const struct list_head *list, u16 phys_addr) +i2c_atr_find_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr) { + struct i2c_atr *atr =3D chan->atr; struct i2c_atr_alias_pair *c2a; + struct list_head *alias_pairs; + u16 alias; + int ret; + + lockdep_assert_held(&chan->alias_pairs_lock); =20 - list_for_each_entry(c2a, list, node) { - if (c2a->addr =3D=3D phys_addr) + alias_pairs =3D &chan->alias_pairs; + + list_for_each_entry(c2a, alias_pairs, node) { + if (c2a->addr =3D=3D addr) return c2a; } =20 + ret =3D i2c_atr_reserve_alias(chan->alias_pool); + if (ret < 0) { + // If no free aliases are left, replace an existing one + if (unlikely(list_empty(alias_pairs))) + return NULL; + + list_for_each_entry_reverse(c2a, alias_pairs, node) + if (!c2a->fixed) + break; + + if (c2a->fixed) + return NULL; + + atr->ops->detach_addr(atr, chan->chan_id, c2a->addr); + c2a->addr =3D addr; + + // Move updated entry to beginning of list + list_move(&c2a->node, alias_pairs); + + alias =3D c2a->alias; + } else { + alias =3D ret; + + c2a =3D i2c_atr_create_c2a(chan, alias, addr); + if (!c2a) + goto err_release_alias; + } + + ret =3D atr->ops->attach_addr(atr, chan->chan_id, c2a->addr, c2a->alias); + if (ret) { + dev_err(atr->dev, "failed to attach 0x%02x on channel %d: err %d\n", + addr, chan->chan_id, ret); + goto err_del_c2a; + } + + return c2a; + +err_del_c2a: + i2c_atr_destroy_c2a(&c2a); +err_release_alias: + i2c_atr_release_alias(chan->alias_pool, alias); return NULL; } =20 @@ -180,7 +308,7 @@ static int i2c_atr_map_msgs(struct i2c_atr_chan *chan, = struct i2c_msg *msgs, { struct i2c_atr *atr =3D chan->atr; static struct i2c_atr_alias_pair *c2a; - int i; + int i, ret =3D 0; =20 /* Ensure we have enough room to save the original addresses */ if (unlikely(chan->orig_addrs_size < num)) { @@ -196,11 +324,13 @@ static int i2c_atr_map_msgs(struct i2c_atr_chan *chan= , struct i2c_msg *msgs, chan->orig_addrs_size =3D num; } =20 + mutex_lock(&chan->alias_pairs_lock); + for (i =3D 0; i < num; i++) { chan->orig_addrs[i] =3D msgs[i].addr; =20 - c2a =3D i2c_atr_find_mapping_by_addr(&chan->alias_pairs, - msgs[i].addr); + c2a =3D i2c_atr_find_mapping_by_addr(chan, msgs[i].addr); + if (!c2a) { dev_err(atr->dev, "client 0x%02x not mapped!\n", msgs[i].addr); @@ -208,13 +338,19 @@ static int i2c_atr_map_msgs(struct i2c_atr_chan *chan= , struct i2c_msg *msgs, while (i--) msgs[i].addr =3D chan->orig_addrs[i]; =20 - return -ENXIO; + ret =3D -ENXIO; + goto out_unlock; } =20 + // Prevent c2a from being overwritten by another client in this transact= ion + c2a->fixed =3D true; + msgs[i].addr =3D c2a->alias; } =20 - return 0; +out_unlock: + mutex_unlock(&chan->alias_pairs_lock); + return ret; } =20 /* @@ -227,10 +363,24 @@ static int i2c_atr_map_msgs(struct i2c_atr_chan *chan= , struct i2c_msg *msgs, static void i2c_atr_unmap_msgs(struct i2c_atr_chan *chan, struct i2c_msg *= msgs, int num) { + struct i2c_atr_alias_pair *c2a; int i; =20 for (i =3D 0; i < num; i++) msgs[i].addr =3D chan->orig_addrs[i]; + + mutex_lock(&chan->alias_pairs_lock); + + if (unlikely(list_empty(&chan->alias_pairs))) + goto out_unlock; + + // unfix c2a entries so that subsequent transfers can reuse their aliases + list_for_each_entry(c2a, &chan->alias_pairs, node) { + c2a->fixed =3D false; + } + +out_unlock: + mutex_unlock(&chan->alias_pairs_lock); } =20 static int i2c_atr_master_xfer(struct i2c_adapter *adap, struct i2c_msg *m= sgs, @@ -268,14 +418,23 @@ static int i2c_atr_smbus_xfer(struct i2c_adapter *ada= p, u16 addr, struct i2c_atr *atr =3D chan->atr; struct i2c_adapter *parent =3D atr->parent; struct i2c_atr_alias_pair *c2a; + u16 alias; + + mutex_lock(&chan->alias_pairs_lock); + + c2a =3D i2c_atr_find_mapping_by_addr(chan, addr); =20 - c2a =3D i2c_atr_find_mapping_by_addr(&chan->alias_pairs, addr); if (!c2a) { dev_err(atr->dev, "client 0x%02x not mapped!\n", addr); + mutex_unlock(&chan->alias_pairs_lock); return -ENXIO; } =20 - return i2c_smbus_xfer(parent, c2a->alias, flags, read_write, command, + alias =3D c2a->alias; + + mutex_unlock(&chan->alias_pairs_lock); + + return i2c_smbus_xfer(parent, alias, flags, read_write, command, size, data); } =20 @@ -317,44 +476,6 @@ static const struct i2c_lock_operations i2c_atr_lock_o= ps =3D { .unlock_bus =3D i2c_atr_unlock_bus, }; =20 -static int i2c_atr_reserve_alias(struct i2c_atr_alias_pool *alias_pool) -{ - unsigned long idx; - u16 alias; - - spin_lock(&alias_pool->lock); - - idx =3D find_first_zero_bit(alias_pool->use_mask, alias_pool->size); - if (idx >=3D alias_pool->size) { - spin_unlock(&alias_pool->lock); - return -EBUSY; - } - - set_bit(idx, alias_pool->use_mask); - - alias =3D alias_pool->aliases[idx]; - - spin_unlock(&alias_pool->lock); - return alias; -} - -static void i2c_atr_release_alias(struct i2c_atr_alias_pool *alias_pool, u= 16 alias) -{ - unsigned int idx; - - spin_lock(&alias_pool->lock); - - for (idx =3D 0; idx < alias_pool->size; ++idx) { - if (alias_pool->aliases[idx] =3D=3D alias) { - clear_bit(idx, alias_pool->use_mask); - spin_unlock(&alias_pool->lock); - return; - } - } - - spin_unlock(&alias_pool->lock); -} - static int i2c_atr_attach_addr(struct i2c_adapter *adapter, u16 addr) { @@ -372,7 +493,9 @@ static int i2c_atr_attach_addr(struct i2c_adapter *adap= ter, =20 alias =3D ret; =20 - c2a =3D kzalloc(sizeof(*c2a), GFP_KERNEL); + mutex_lock(&chan->alias_pairs_lock); + + c2a =3D i2c_atr_create_c2a(chan, alias, addr); if (!c2a) { ret =3D -ENOMEM; goto err_release_alias; @@ -380,22 +503,19 @@ static int i2c_atr_attach_addr(struct i2c_adapter *ad= apter, =20 ret =3D atr->ops->attach_addr(atr, chan->chan_id, addr, alias); if (ret) - goto err_free; + goto err_del_c2a; =20 dev_dbg(atr->dev, "chan%u: using alias 0x%02x for addr 0x%02x\n", chan->chan_id, alias, addr); =20 - c2a->addr =3D addr; - c2a->alias =3D alias; - list_add(&c2a->node, &chan->alias_pairs); - - return 0; + goto out_unlock; =20 -err_free: - kfree(c2a); +err_del_c2a: + i2c_atr_destroy_c2a(&c2a); err_release_alias: i2c_atr_release_alias(chan->alias_pool, alias); - +out_unlock: + mutex_unlock(&chan->alias_pairs_lock); return ret; } =20 @@ -408,21 +528,25 @@ static void i2c_atr_detach_addr(struct i2c_adapter *a= dapter, =20 atr->ops->detach_addr(atr, chan->chan_id, addr); =20 - c2a =3D i2c_atr_find_mapping_by_addr(&chan->alias_pairs, addr); + mutex_lock(&chan->alias_pairs_lock); + + c2a =3D i2c_atr_find_mapping_by_addr(chan, addr); if (!c2a) { /* This should never happen */ dev_warn(atr->dev, "Unable to find address mapping\n"); + mutex_unlock(&chan->alias_pairs_lock); return; } =20 + mutex_unlock(&chan->alias_pairs_lock); + i2c_atr_release_alias(chan->alias_pool, c2a->alias); =20 dev_dbg(atr->dev, "chan%u: detached alias 0x%02x from addr 0x%02x\n", chan->chan_id, c2a->alias, addr); =20 - list_del(&c2a->node); - kfree(c2a); + i2c_atr_destroy_c2a(&c2a); } =20 static int i2c_atr_bus_notifier_call(struct notifier_block *nb, @@ -633,6 +757,7 @@ int i2c_atr_add_adapter(struct i2c_atr *atr, struct i2c= _atr_adap_desc *desc) chan->atr =3D atr; chan->chan_id =3D chan_id; INIT_LIST_HEAD(&chan->alias_pairs); + mutex_init(&chan->alias_pairs_lock); mutex_init(&chan->orig_addrs_lock); =20 snprintf(chan->adap.name, sizeof(chan->adap.name), "i2c-%d-atr-%d", @@ -709,6 +834,7 @@ int i2c_atr_add_adapter(struct i2c_atr *atr, struct i2c= _atr_adap_desc *desc) err_fwnode_put: fwnode_handle_put(dev_fwnode(&chan->adap.dev)); mutex_destroy(&chan->orig_addrs_lock); + mutex_destroy(&chan->alias_pairs_lock); kfree(chan); return ret; } @@ -745,6 +871,7 @@ void i2c_atr_del_adapter(struct i2c_atr *atr, u32 chan_= id) =20 fwnode_handle_put(fwnode); mutex_destroy(&chan->orig_addrs_lock); + mutex_destroy(&chan->alias_pairs_lock); kfree(chan->orig_addrs); kfree(chan); } --=20 2.47.1 From nobody Wed Feb 5 22:54:05 2025 Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) (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 16F0120CCFD; Tue, 4 Feb 2025 09:29:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738661380; cv=none; b=feds/7jKajHvgtBk7hJWGZ81N5Ofb+iKtv02O6OEpagPzsYZmEb8GYZe6KmXq6HHVioKnHzve2C4PP6MMYn/D8nZD34B77VYUDO8ePXqf+VnhEyC/6uGnfJdwx66M6+sav25uTRGl5WeJD7Z86TYRSmG/H+RIaWCGJ76eN013/c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738661380; c=relaxed/simple; bh=LwaS+b4K6app9iaYHwpPqauV502DqwowxBGf28dPgg4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sRBAI4CpBcBqvSC6pX0lSfDszK0xgo9Uda2F1IUfSbuEiZ02xpoqVy0AMIIIDi8zYzyawxiBqlL5CApt44EKo/rBwtM0F6ZaWdNrcXIQCxos+CNkXMM9lcB0R5CbUO2QEcPwB3Ob9DcrBrDpAVHb4Gl4amsDDaZgxxZrckVI/dg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=pdeQimmz; arc=none smtp.client-ip=217.70.183.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="pdeQimmz" Received: by mail.gandi.net (Postfix) with ESMTPSA id 8467444360; Tue, 4 Feb 2025 09:29:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1738661369; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=H2bA0INaMx9C3B656GQashh98wscRjOmWtNYma2Epn8=; b=pdeQimmzyZG0p5wlNe7MCy1VrMP5BfzeIF80Zctuk4pmr5S+nzP1+5eGMqb+pUSbbMG5+/ NxFxcS1v/sE4l107SMuUPd935cU2D32/wK6P+FixQ0HD1sfstaJU5uubLRXgvIbZvIghKc Cy71u3qA4iJYiA9CIyGenaafRj/8SlPBEJwOEdPlRQbhR3NKVXR4b6Pehpd+N7ZVcTUlYI Cqtrhup9Qck60OyPvRLaFU2dKOjU48P9Gu3gxwkNKV19iGa+hmC8V8G7AmRGozrwjYdemz th7xEoIAGyisOV4YD8mksPNQrx4ADQLnbUivT8Bfvg8Ctd1RRSQ3tzdXksUrhw== From: Romain Gantois Date: Tue, 04 Feb 2025 10:29:20 +0100 Subject: [PATCH v7 9/9] misc: add FPC202 dual port controller driver 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: <20250204-fpc202-v7-9-78b4b8a35cf1@bootlin.com> References: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com> In-Reply-To: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com> To: Wolfram Sang , Tomi Valkeinen , Luca Ceresoli , Andi Shyti , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Derek Kiernan , Dragan Cvetic , Arnd Bergmann , Greg Kroah-Hartman , Mauro Carvalho Chehab , Linus Walleij , Bartosz Golaszewski Cc: Thomas Petazzoni , Kory Maincent , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-media@vger.kernel.org, linux-gpio@vger.kernel.org, Romain Gantois X-Mailer: b4 0.14.2 X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdduhecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfitefpfffkpdcuggftfghnshhusghstghrihgsvgenuceurghilhhouhhtmecufedtudenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephfffufggtgfgkfhfjgfvvefosehtjeertdertdejnecuhfhrohhmpeftohhmrghinhcuifgrnhhtohhishcuoehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomheqnecuggftrfgrthhtvghrnhepkeelieefteelffeuheevtdetkefhfffhteffkefgtefhkeevudeutdeugfffheegnecukfhppeeltddrkeelrdduieefrdduvdejnecuvehluhhsthgvrhfuihiivgepfeenucfrrghrrghmpehinhgvthepledtrdekledrudeifedruddvjedphhgvlhhopegludelvddrudeikedrtddrudefngdpmhgrihhlfhhrohhmpehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomhdpnhgspghrtghpthhtohepvddvpdhrtghpthhtoheprghrnhgusegrrhhnuggsrdguvgdprhgtphhtthhopehrohhmrghinhdrghgrnhhtohhishessghoohhtlhhinhdrtghomhdprhgtphhtthhopehthhhomhgrshdrphgvthgriiiiohhnihessghoohhtlhhinhdrtghomhdprhgtphhtthhopeguvghrvghkrdhkihgvrhhnrghnsegrmhgurdgtohhmpdhrtghpthhtohepthhomhhirdhvrghlkhgvihhnvghns ehiuggvrghsohhnsghorghrugdrtghomhdprhgtphhtthhopegurhgrghgrnhdrtghvvghtihgtsegrmhgurdgtohhmpdhrtghpthhtohepuggvvhhitggvthhrvggvsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheprghnughirdhshhihthhisehkvghrnhgvlhdrohhrgh X-GND-Sasl: romain.gantois@bootlin.com The TI FPC202 dual port controller serves as a low-speed signal aggregator for common port types such as SFP, QSFP, Mini-SAS HD, and others. It aggregates GPIO and I2C signals across two downstream ports, acting as both a GPIO controller and an I2C address translator for up to two logical devices per port. Reviewed-by: Linus Walleij Signed-off-by: Romain Gantois --- MAINTAINERS | 1 + drivers/misc/Kconfig | 11 ++ drivers/misc/Makefile | 1 + drivers/misc/ti_fpc202.c | 438 +++++++++++++++++++++++++++++++++++++++++++= ++++ 4 files changed, 451 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 4bb13de2b8ebad5180e2bde607dac40f35c51782..7d2b08d0b0ad5390eb8528eb7dd= c9da8b2729026 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -23714,6 +23714,7 @@ M: Romain Gantois L: linux-kernel@vger.kernel.org S: Maintained F: Documentation/devicetree/bindings/misc/ti,fpc202.yaml +F: drivers/misc/ti_fpc202.c =20 TI FPD-LINK DRIVERS M: Tomi Valkeinen diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig index 56bc72c7ce4a998f6b9b3ed90a0845b52715f405..e4a95a5e7efee908da5bcaefe7d= f8877579d3980 100644 --- a/drivers/misc/Kconfig +++ b/drivers/misc/Kconfig @@ -114,6 +114,17 @@ config RPMB =20 If unsure, select N. =20 +config TI_FPC202 + tristate "TI FPC202 Dual Port Controller" + select GPIOLIB + depends on I2C_ATR + help + If you say yes here you get support for the Texas Instruments FPC202 + Dual Port Controller. + + This driver can also be built as a module. If so, the module will be + called fpc202. + config TIFM_CORE tristate "TI Flash Media interface support" depends on PCI diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile index 545aad06d088563aaddf7d2f3b82d995e817fde0..2e27d4b7590313fab8da3dc311f= 492359ef44ee8 100644 --- a/drivers/misc/Makefile +++ b/drivers/misc/Makefile @@ -12,6 +12,7 @@ obj-$(CONFIG_ATMEL_SSC) +=3D atmel-ssc.o obj-$(CONFIG_DUMMY_IRQ) +=3D dummy-irq.o obj-$(CONFIG_ICS932S401) +=3D ics932s401.o obj-$(CONFIG_LKDTM) +=3D lkdtm/ +obj-$(CONFIG_TI_FPC202) +=3D ti_fpc202.o obj-$(CONFIG_TIFM_CORE) +=3D tifm_core.o obj-$(CONFIG_TIFM_7XX1) +=3D tifm_7xx1.o obj-$(CONFIG_PHANTOM) +=3D phantom.o diff --git a/drivers/misc/ti_fpc202.c b/drivers/misc/ti_fpc202.c new file mode 100644 index 0000000000000000000000000000000000000000..b9c9ee4bfc4edba102b3bf7ae33= a2b68c19c83ec --- /dev/null +++ b/drivers/misc/ti_fpc202.c @@ -0,0 +1,438 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * ti_fpc202.c - FPC202 Dual Port Controller driver + * + * Copyright (C) 2024 Bootlin + * + */ + +#include +#include +#include +#include +#include +#include +#include + +#define FPC202_NUM_PORTS 2 +#define FPC202_ALIASES_PER_PORT 2 + +/* + * GPIO: port mapping + * + * 0: P0_S0_IN_A + * 1: P0_S1_IN_A + * 2: P1_S0_IN_A + * 3: P1_S1_IN_A + * 4: P0_S0_IN_B + * ... + * 8: P0_S0_IN_C + * ... + * 12: P0_S0_OUT_A + * ... + * 16: P0_S0_OUT_B + * ... + * 19: P1_S1_OUT_B + * + */ + +#define FPC202_GPIO_COUNT 20 +#define FPC202_GPIO_P0_S0_IN_B 4 +#define FPC202_GPIO_P0_S0_OUT_A 12 + +#define FPC202_REG_IN_A_INT 0x6 +#define FPC202_REG_IN_C_IN_B 0x7 +#define FPC202_REG_OUT_A_OUT_B 0x8 + +#define FPC202_REG_OUT_A_OUT_B_VAL 0xa + +#define FPC202_REG_MOD_DEV(port, dev) (0xb4 + ((port) * 4) + (dev)) +#define FPC202_REG_AUX_DEV(port, dev) (0xb6 + ((port) * 4) + (dev)) + +/* + * The FPC202 doesn't support turning off address translation on a single = port. + * So just set an invalid I2C address as the translation target when no cl= ient + * address is attached. + */ +#define FPC202_REG_DEV_INVALID 0 + +/* Even aliases are assigned to device 0 and odd aliases to device 1 */ +#define fpc202_dev_num_from_alias(alias) ((alias) % 2) + +struct fpc202_priv { + struct i2c_client *client; + struct i2c_atr *atr; + struct gpio_desc *en_gpio; + struct gpio_chip gpio; + + /* Lock REG_MOD/AUX_DEV and addr_caches during attach/detach */ + struct mutex reg_dev_lock; + + /* Cached device addresses for both ports and their devices */ + u8 addr_caches[2][2]; + + /* Keep track of which ports were probed */ + DECLARE_BITMAP(probed_ports, FPC202_NUM_PORTS); +}; + +static void fpc202_fill_alias_table(struct i2c_client *client, u16 *aliase= s, int port_id) +{ + u16 first_alias; + int i; + + /* + * There is a predefined list of aliases for each FPC202 I2C + * self-address. This allows daisy-chained FPC202 units to + * automatically take on different sets of aliases. + * Each port of an FPC202 unit is assigned two aliases from this list. + */ + first_alias =3D 0x10 + 4 * port_id + 8 * ((u16)client->addr - 2); + + for (i =3D 0; i < FPC202_ALIASES_PER_PORT; i++) + aliases[i] =3D first_alias + i; +} + +static int fpc202_gpio_get_dir(int offset) +{ + return offset < FPC202_GPIO_P0_S0_OUT_A ? GPIO_LINE_DIRECTION_IN : GPIO_L= INE_DIRECTION_OUT; +} + +static int fpc202_read(struct fpc202_priv *priv, u8 reg) +{ + int val; + + val =3D i2c_smbus_read_byte_data(priv->client, reg); + return val; +} + +static int fpc202_write(struct fpc202_priv *priv, u8 reg, u8 value) +{ + return i2c_smbus_write_byte_data(priv->client, reg, value); +} + +static void fpc202_set_enable(struct fpc202_priv *priv, int enable) +{ + if (!priv->en_gpio) + return; + + gpiod_set_value(priv->en_gpio, enable); +} + +static void fpc202_gpio_set(struct gpio_chip *chip, unsigned int offset, + int value) +{ + struct fpc202_priv *priv =3D gpiochip_get_data(chip); + int ret; + u8 val; + + if (fpc202_gpio_get_dir(offset) =3D=3D GPIO_LINE_DIRECTION_IN) + return; + + ret =3D fpc202_read(priv, FPC202_REG_OUT_A_OUT_B_VAL); + if (ret < 0) { + dev_err(&priv->client->dev, "Failed to set GPIO %d value! err %d\n", off= set, ret); + return; + } + + val =3D (u8)ret; + + if (value) + val |=3D BIT(offset - FPC202_GPIO_P0_S0_OUT_A); + else + val &=3D ~BIT(offset - FPC202_GPIO_P0_S0_OUT_A); + + fpc202_write(priv, FPC202_REG_OUT_A_OUT_B_VAL, val); +} + +static int fpc202_gpio_get(struct gpio_chip *chip, unsigned int offset) +{ + struct fpc202_priv *priv =3D gpiochip_get_data(chip); + u8 reg, bit; + int ret; + + if (offset < FPC202_GPIO_P0_S0_IN_B) { + reg =3D FPC202_REG_IN_A_INT; + bit =3D BIT(4 + offset); + } else if (offset < FPC202_GPIO_P0_S0_OUT_A) { + reg =3D FPC202_REG_IN_C_IN_B; + bit =3D BIT(offset - FPC202_GPIO_P0_S0_IN_B); + } else { + reg =3D FPC202_REG_OUT_A_OUT_B_VAL; + bit =3D BIT(offset - FPC202_GPIO_P0_S0_OUT_A); + } + + ret =3D fpc202_read(priv, reg); + if (ret < 0) + return ret; + + return !!(((u8)ret) & bit); +} + +static int fpc202_gpio_direction_input(struct gpio_chip *chip, unsigned in= t offset) +{ + if (fpc202_gpio_get_dir(offset) =3D=3D GPIO_LINE_DIRECTION_OUT) + return -EINVAL; + + return 0; +} + +static int fpc202_gpio_direction_output(struct gpio_chip *chip, unsigned i= nt offset, + int value) +{ + struct fpc202_priv *priv =3D gpiochip_get_data(chip); + int ret; + u8 val; + + if (fpc202_gpio_get_dir(offset) =3D=3D GPIO_LINE_DIRECTION_IN) + return -EINVAL; + + fpc202_gpio_set(chip, offset, value); + + ret =3D fpc202_read(priv, FPC202_REG_OUT_A_OUT_B); + if (ret < 0) + return ret; + + val =3D (u8)ret | BIT(offset - FPC202_GPIO_P0_S0_OUT_A); + + return fpc202_write(priv, FPC202_REG_OUT_A_OUT_B, val); +} + +/* + * Set the translation table entry associated with a port and device numbe= r. + * + * Each downstream port of the FPC202 has two fixed aliases corresponding = to + * device numbers 0 and 1. If one of these aliases is found in an incoming= I2C + * transfer, it will be translated to the address given by the correspondi= ng + * translation table entry. + */ +static int fpc202_write_dev_addr(struct fpc202_priv *priv, u32 port_id, in= t dev_num, u16 addr) +{ + int ret, reg_mod, reg_aux; + u8 val; + + guard(mutex)(&priv->reg_dev_lock); + + reg_mod =3D FPC202_REG_MOD_DEV(port_id, dev_num); + reg_aux =3D FPC202_REG_AUX_DEV(port_id, dev_num); + val =3D addr & 0x7f; + + ret =3D fpc202_write(priv, reg_mod, val); + if (ret) + return ret; + + /* + * The FPC202 datasheet is unclear about the role of the AUX registers. + * Empirically, writing to them as well seems to be necessary for + * address translation to function properly. + */ + ret =3D fpc202_write(priv, reg_aux, val); + + priv->addr_caches[port_id][dev_num] =3D val; + + return ret; +} + +static int fpc202_attach_addr(struct i2c_atr *atr, u32 chan_id, + u16 addr, u16 alias) +{ + struct fpc202_priv *priv =3D i2c_atr_get_driver_data(atr); + + dev_dbg(&priv->client->dev, "attaching address 0x%02x to alias 0x%02x\n",= addr, alias); + + return fpc202_write_dev_addr(priv, chan_id, fpc202_dev_num_from_alias(ali= as), addr); +} + +static void fpc202_detach_addr(struct i2c_atr *atr, u32 chan_id, + u16 addr) +{ + struct fpc202_priv *priv =3D i2c_atr_get_driver_data(atr); + int dev_num, reg_mod, val; + + for (dev_num =3D 0; dev_num < 2; dev_num++) { + reg_mod =3D FPC202_REG_MOD_DEV(chan_id, dev_num); + + mutex_lock(&priv->reg_dev_lock); + + val =3D priv->addr_caches[chan_id][dev_num]; + + mutex_unlock(&priv->reg_dev_lock); + + if (val < 0) { + dev_err(&priv->client->dev, "failed to read register 0x%x while detachi= ng address 0x%02x\n", + reg_mod, addr); + return; + } + + if (val =3D=3D (addr & 0x7f)) { + fpc202_write_dev_addr(priv, chan_id, dev_num, FPC202_REG_DEV_INVALID); + return; + } + } +} + +static const struct i2c_atr_ops fpc202_atr_ops =3D { + .attach_addr =3D fpc202_attach_addr, + .detach_addr =3D fpc202_detach_addr, +}; + +static int fpc202_probe_port(struct fpc202_priv *priv, struct device_node = *i2c_handle, int port_id) +{ + u16 aliases[FPC202_ALIASES_PER_PORT] =3D { }; + struct device *dev =3D &priv->client->dev; + struct i2c_atr_adap_desc desc =3D { }; + int ret =3D 0; + + desc.chan_id =3D port_id; + desc.parent =3D dev; + desc.bus_handle =3D of_node_to_fwnode(i2c_handle); + desc.num_aliases =3D FPC202_ALIASES_PER_PORT; + + fpc202_fill_alias_table(priv->client, aliases, port_id); + desc.aliases =3D aliases; + + ret =3D i2c_atr_add_adapter(priv->atr, &desc); + if (ret) + return ret; + + set_bit(port_id, priv->probed_ports); + + ret =3D fpc202_write_dev_addr(priv, port_id, 0, FPC202_REG_DEV_INVALID); + if (ret) + return ret; + + return fpc202_write_dev_addr(priv, port_id, 1, FPC202_REG_DEV_INVALID); +} + +static void fpc202_remove_port(struct fpc202_priv *priv, int port_id) +{ + i2c_atr_del_adapter(priv->atr, port_id); + clear_bit(port_id, priv->probed_ports); +} + +static int fpc202_probe(struct i2c_client *client) +{ + struct device *dev =3D &client->dev; + struct device_node *i2c_handle; + struct fpc202_priv *priv; + int ret, port_id; + + priv =3D devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + mutex_init(&priv->reg_dev_lock); + + priv->client =3D client; + i2c_set_clientdata(client, priv); + + priv->en_gpio =3D devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_HIGH); + if (IS_ERR(priv->en_gpio)) { + ret =3D PTR_ERR(priv->en_gpio); + dev_err(dev, "failed to fetch enable GPIO! err %d\n", ret); + goto destroy_mutex; + } + + priv->gpio.label =3D "gpio-fpc202"; + priv->gpio.base =3D -1; + priv->gpio.direction_input =3D fpc202_gpio_direction_input; + priv->gpio.direction_output =3D fpc202_gpio_direction_output; + priv->gpio.set =3D fpc202_gpio_set; + priv->gpio.get =3D fpc202_gpio_get; + priv->gpio.ngpio =3D FPC202_GPIO_COUNT; + priv->gpio.parent =3D dev; + priv->gpio.owner =3D THIS_MODULE; + + ret =3D gpiochip_add_data(&priv->gpio, priv); + if (ret) { + priv->gpio.parent =3D NULL; + dev_err(dev, "failed to add gpiochip err %d\n", ret); + goto disable_gpio; + } + + priv->atr =3D i2c_atr_new(client->adapter, dev, &fpc202_atr_ops, 2); + if (IS_ERR(priv->atr)) { + ret =3D PTR_ERR(priv->atr); + dev_err(dev, "failed to create i2c atr err %d\n", ret); + goto disable_gpio; + } + + i2c_atr_set_driver_data(priv->atr, priv); + + bitmap_zero(priv->probed_ports, FPC202_NUM_PORTS); + + for_each_child_of_node(dev->of_node, i2c_handle) { + ret =3D of_property_read_u32(i2c_handle, "reg", &port_id); + if (ret) { + if (ret =3D=3D -EINVAL) + continue; + + dev_err(dev, "failed to read 'reg' property of child node, err %d\n", r= et); + goto unregister_chans; + } + + if (port_id > FPC202_NUM_PORTS) { + dev_err(dev, "port ID %d is out of range!\n", port_id); + ret =3D -EINVAL; + goto unregister_chans; + } + + ret =3D fpc202_probe_port(priv, i2c_handle, port_id); + if (ret) { + dev_err(dev, "Failed to probe port %d, err %d\n", port_id, ret); + goto unregister_chans; + } + } + + goto out; + +unregister_chans: + for_each_set_bit(port_id, priv->probed_ports, FPC202_NUM_PORTS) + fpc202_remove_port(priv, port_id); + + i2c_atr_delete(priv->atr); +disable_gpio: + fpc202_set_enable(priv, 0); + gpiochip_remove(&priv->gpio); +destroy_mutex: + mutex_destroy(&priv->reg_dev_lock); +out: + return ret; +} + +static void fpc202_remove(struct i2c_client *client) +{ + struct fpc202_priv *priv =3D i2c_get_clientdata(client); + int port_id; + + for_each_set_bit(port_id, priv->probed_ports, FPC202_NUM_PORTS) + fpc202_remove_port(priv, port_id); + + mutex_destroy(&priv->reg_dev_lock); + + i2c_atr_delete(priv->atr); + + fpc202_set_enable(priv, 0); + gpiochip_remove(&priv->gpio); +} + +static const struct of_device_id fpc202_of_match[] =3D { + { .compatible =3D "ti,fpc202" }, + {} +}; +MODULE_DEVICE_TABLE(of, fpc202_of_match); + +static struct i2c_driver fpc202_driver =3D { + .driver =3D { + .name =3D "fpc202", + .of_match_table =3D fpc202_of_match, + }, + .probe =3D fpc202_probe, + .remove =3D fpc202_remove, +}; + +module_i2c_driver(fpc202_driver); + +MODULE_AUTHOR("Romain Gantois "); +MODULE_DESCRIPTION("TI FPC202 Dual Port Controller driver"); +MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS("I2C_ATR"); --=20 2.47.1