From nobody Thu Oct 2 16:30:55 2025 Received: from mail-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ADE74207A38 for ; Mon, 15 Sep 2025 06:02:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757916124; cv=none; b=PB+G/c+fJ3jRe5fnBj3yfWJ0tGZ88OoSn5io0idj1kE0eHt7WWYxr0XjmlWm0Fd0xn7bi1FzWRfy5FZDrFcC7YJRZuWMIT1nFCicDe4eplYZgzAQ8yR55lSjIPgi2yDzP3WO5rnITObVCbdddRRm9E3WQRnZhX20Un6zr+6NaNc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757916124; c=relaxed/simple; bh=H86OdhgTHF+sXtUDW7aUQJv0MWH5MWu/K9PkO+/x6d8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=j2RTrEThwrSD0OFT5MtIcaO6rdKCYp/Cv1DlNCmUHiSPS/MHZdgOcFxH38KwQzh00qRB83Gv2KcLgmn1F7iWOEWlyCltqb0wSYRTQozGYeq/ZraDWpK+x5zi4/XPLfGKcuBQFb+nWMJAK/6eXPqUtiJ4yjXEiGllsHmxjEvgsAM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=H4Ieyqrl; arc=none smtp.client-ip=209.85.167.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="H4Ieyqrl" Received: by mail-lf1-f45.google.com with SMTP id 2adb3069b0e04-55f72452a8eso4402512e87.3 for ; Sun, 14 Sep 2025 23:02:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1757916121; x=1758520921; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3GAzaDQKIDLrSVL1wcySzyvMJnY8KNZJxvSnSoEULDg=; b=H4IeyqrlEP1w9hYzZpidqEX0G58A4F45FJWvMaaKXeTHIoAurn6qEprL0dh4Qh1wb3 Tdco/8EvWBKMJfPGm+b09OCeSyN+D7sDvLlB7bU38XqkkWATlM5rRCM3xHKPc5ClBa5E SXiavKNbHMAXcScYqhJdpdLZMztSnttor9ut+8VFUJxOVDYx+8BFE1crZsJ0lxG00CEm zJ7oZfYwiCHLg6EvZIV4WOjFGm3TFrB6jTlfAFOgwkc8mGa8vhO4rsp8+SAve7BC2bbE K3oHQy0QLMrD6tN/q6rpzqHyIBclI6sq50VX4orayK3Nvli9clMI1SMV5fRhUsvubVfd 2LkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757916121; x=1758520921; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3GAzaDQKIDLrSVL1wcySzyvMJnY8KNZJxvSnSoEULDg=; b=VM+SYkkdnMqJQAOgOoNAEDNHV+C3pLR0CCUWE8JB+1h46itEUWmJJpoy8sIKZmcM/r FqMyPqWxcheFWVzVUskuwlxK8ND7BvZ0zeDDmoiK2aNaqD0iaWxsP9f/bBewpaEFyp/T CsO8w69R+HxXXkA23bMj13K1RXMwgFYH6AlACBItrtCt2IcZgM9kxj+jm+WzX0g07Y4R QnlYXtw94wy09wxH63r9C3+CnrORIrL4qytM9UTsCAROyJ8n+tu8YiDU5VKMTjl7V8AM 9f9CJDax4FWApCzD/Ws3E5Hmvgcp8qDdylFnHOK+zkqJqGVPP0LasrskvtqPlEhfx2yt OjNw== X-Forwarded-Encrypted: i=1; AJvYcCVwFOmKmtMeoVFy+pihstR4sXBucAaryTsTJ5I2Jxs7R0rApGRUYhEy6zKHAuXqoqybVsFnMHxJwjtvGgw=@vger.kernel.org X-Gm-Message-State: AOJu0Yznp62anz05GpRAkCCiuVRpSvahx1KTV6hb9LKSEihCpnS2g2b1 n+VfO/ue1IVg19mfYoVpAriTD5i5Nf4bPLNlRR+8rlD0wut0dM2j8QCT X-Gm-Gg: ASbGncspash8CCB5iqkwjXlfq3RC/C4e7KQqNlIBZdO9+gz6hP4jB9ysPQydyzsX/bK hchgmnRHqa5WftJBGPXCirUeJooDMs2ixiW8qlOBOFC/YA9FuW9NkkudUTQlqqlL840YpcQxs/o fUg7pPIADko381vpTWe2maOnBNQ2SUskJufz5jBRy2dKWJVEaedWf3NP4SoABRE0e3I2ONI8S06 WOkwQiyy4Xc/F0wDuaJb+3FND+FuxlNbS2OEPEm7TRaDT1XbO3es/AQEZt5nwtTmU2Dx0s4EuHi yAjXV1taVdhfVSeQicS5M/TBIxyeYCMP4MkiI/Fgj4Koovp3PmhC5dJ42cmWuRjajUYe+KfSHR0 4xsAAg0k2b08XXQ== X-Google-Smtp-Source: AGHT+IH/1syhnxH2MgON3fBQYZRWQzr7rl4FW7/6FJ9ZvlyxiPfGztFWjjW5t/Xc1eUHZDZ7Yq0H2Q== X-Received: by 2002:a05:6512:32c6:b0:55f:5245:d695 with SMTP id 2adb3069b0e04-57048bea410mr3293682e87.10.1757916120363; Sun, 14 Sep 2025 23:02:00 -0700 (PDT) Received: from xeon.. ([188.163.112.70]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-571d16e79d0sm2062214e87.86.2025.09.14.23.01.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Sep 2025 23:02:00 -0700 (PDT) From: Svyatoslav Ryhel To: Andi Shyti , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Peter Rosin , =?UTF-8?q?Micha=C5=82=20Miros=C5=82aw?= , Svyatoslav Ryhel , =?UTF-8?q?Jonas=20Schw=C3=B6bel?= Cc: linux-i2c@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v1 1/2] dt-bindings: i2c: Document GPIO detected hot-plugged I2C bus Date: Mon, 15 Sep 2025 09:01:37 +0300 Message-ID: <20250915060141.12540-2-clamor95@gmail.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250915060141.12540-1-clamor95@gmail.com> References: <20250915060141.12540-1-clamor95@gmail.com> 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 Schema describes hardware configuration that uses a GPIO signal to determine the bus's presence and state, allowing the system to dynamically configure I2C devices as they are plugged in or removed. Signed-off-by: Svyatoslav Ryhel --- .../bindings/i2c/i2c-hotplug-gpio.yaml | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Documentation/devicetree/bindings/i2c/i2c-hotplug-gpio.= yaml diff --git a/Documentation/devicetree/bindings/i2c/i2c-hotplug-gpio.yaml b/= Documentation/devicetree/bindings/i2c/i2c-hotplug-gpio.yaml new file mode 100644 index 000000000000..d1d5d830c91b --- /dev/null +++ b/Documentation/devicetree/bindings/i2c/i2c-hotplug-gpio.yaml @@ -0,0 +1,65 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/i2c/i2c-hotplug-gpio.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: GPIO detected hot-plugged I2C bus + +maintainers: + - Micha=C5=82 Miros=C5=82aw + - Svyatoslav Ryhel + +description: An I2C bus, where some devices on the bus are hot-pluggable + and their presence is indicated by GPIO line. + +properties: + compatible: + const: i2c-hotplug-gpio + + '#address-cells': + const: 1 + + '#size-cells': + const: 0 + + interrupts: + maxItems: 1 + + detect-gpios: + description: usually the same GPIO used as an interrupt. In the active + state should indicate that detachable devices are plugged in. + maxItems: 1 + + i2c-parent: + maxItems: 1 + +required: + - compatible + - '#address-cells' + - '#size-cells' + - interrupts + - detect-gpios + - i2c-parent + +additionalProperties: false + +examples: + - | + #include + #include + + i2c-dock { + compatible =3D "i2c-hotplug-gpio"; + + interrupt-parent =3D <&gpio>; + interrupts =3D <164 IRQ_TYPE_EDGE_BOTH>; + + detect-gpios =3D <&gpio 164 GPIO_ACTIVE_LOW>; + + i2c-parent =3D <&gen2_i2c>; + + #address-cells =3D <1>; + #size-cells =3D <0>; + }; +... --=20 2.48.1 From nobody Thu Oct 2 16:30:55 2025 Received: from mail-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF9902773E5 for ; Mon, 15 Sep 2025 06:02:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757916125; cv=none; b=Eo9RKCKd7rXW2hkwcsd7iXS5kr19oypJG8B9W0Pm5utY/2lLxpBt/aQNN73oODxRLjL1mqlSCh68dP7+OtOfkXxfuyxxTiMjl4R6Wu8Ez23NgaOlzphsdzIbYbMAnKTggVw94FO3y3j6Kw3BvE+ZNC/LgtAZK2q7eesmpfpACNg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757916125; c=relaxed/simple; bh=n19cUIUifzb9e7i9WDr+ddZdeWjbqWFWgaplDIhF8iw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=l8rrFoOQu1RGCLyuShBnnVnr1RZKwTH9xGWrYWouA35HHJDrgiXJHW6pgXc1jOr1/nqCLAaCxYnpBIS0lDCRMGXmWMLugHCS7QP3Sl1CS8dEMAlMgixCwT66dI7KRzqgNsViWp/jrRtuB5vcTmnQFMGTx1HUieBESA/WmE3XwL8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=OaXXGixJ; arc=none smtp.client-ip=209.85.167.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OaXXGixJ" Received: by mail-lf1-f45.google.com with SMTP id 2adb3069b0e04-55f7039aa1eso4012498e87.1 for ; Sun, 14 Sep 2025 23:02:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1757916122; x=1758520922; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5oi6MNQDMTUKU8TfoLzmAqqYXa8BYp1KgoMucS99LGk=; b=OaXXGixJ5rqMYJhorqZ3jP5GikQ+VSH+RHZEyDgvUfP+0y0x1ATIdiK6C3+LF1aZc8 dx99bHvwgCxN4aTozaLdOvuVZEAKEwyUBx6KSwWyViRaqDSrGCnOASqsWH2HUIRFe0hP BjTrU8blWCbWc2rjDvDnf2brYWAt9fZV3N9d30IfK0mUO/aguL6AH3vPXO3t+qf/kN+c ovXKZjsra7UU9Wek6O33aUZsJItiRGUubfjpMmjsDcJqMK8WGLhlkr2QY9zdmdhvUIZ+ TO/srMZ1T+60zDL0dgBIbGu4U8Lj4TdgluZaPaDz9Cb7cqwcPMqrNM2knag/ngG5EbD5 uk5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757916122; x=1758520922; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5oi6MNQDMTUKU8TfoLzmAqqYXa8BYp1KgoMucS99LGk=; b=CE4FcdAndGTGRjT0DdtCGV0yV+WysgpxzwH9adsVth1ulIShBj9UDgHUiGeBzqnlqo nlrbN8P4rwFl6oh+JygZ47XfGFPXZKTu1wcCPAtuXDXDR9U77IpVS1kaaB7HbXPbt1oP P5BCUlmDhN+3DN4Yxsj/eZQ/Fmpudot7MacJlKNtdaRC77qe4MiI0ZLkEBF2AK2z39Ex Fkj/lAaR1Gsv6XGMIFHLgBHofgaB4QB2fUEf4UzczQHD4qQPwlftYfDgb3GOKMXOSBV1 /nUlLovQeCxO5YEmCdyiV0S62ArLWlMrkVk/DtJTKgK3KRXNh91Csi0R1AFNdCWzzxDj e8mw== X-Forwarded-Encrypted: i=1; AJvYcCUgFWle3xyvAoO6w7cUd0dIi+8qhQphUqN+O9ZPdPL86WMW6x2+sw5/L/gIZiUQS2j1ABz49kyxEmVM7D0=@vger.kernel.org X-Gm-Message-State: AOJu0Yx6HeytpgqGZp6IpkklT6/4oU1UlkKF9QuOWT+Dvfen5aNPKrJt vmYtAJ49LKB+6Tntto06P461OA+n2iuHMuLuLwXfiwqQNOUk9K3u/Fwh X-Gm-Gg: ASbGncsccy9GBYa2AtXLJkvBlQWW2SbVPFPD+O9iV/LG079aZm91BU0/jGTvJYRXHPw T1u9Y0MQ8NFRi4BPgUIk+nr5tJk6EcmMsefsH+fCLsmhM6fdBnxluw1h+Kjq93UGY/fQKqbZmja e7JWqDKMr6TkJ0JFJx6hkZfJ0SPifurhF7p0oxrUMw0V0Y2A/GCtK2f44C6GojZWjGkM8Sjbzee 3Lb9uu0vtuS65apRIXhrgUMiBpZ4n9n9ZbzH3I2cJdlJDDGcUu91ddZ4Dt5EqxdCfBAClz1GsNp ztq8vmBWGdI47CTuGSxmEuG1+zcWMAQ9D203tP5D20IFRe0D7IsCnPr0Yuf63IULi+VXkDQgqQW c8k34aCc1eyKN8tt2PQeC7jDkgFJJxMctgTo= X-Google-Smtp-Source: AGHT+IHERTqKv+7XCBnORjKJPEkgWWVgiR2NEJtrtHTmkUGBElqfkNzQoFW/ItWo+dD7ngvgV+MVJQ== X-Received: by 2002:a05:6512:36da:b0:55f:4633:7b2 with SMTP id 2adb3069b0e04-5704e723ef0mr2414746e87.46.1757916121637; Sun, 14 Sep 2025 23:02:01 -0700 (PDT) Received: from xeon.. ([188.163.112.70]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-571d16e79d0sm2062214e87.86.2025.09.14.23.02.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Sep 2025 23:02:01 -0700 (PDT) From: Svyatoslav Ryhel To: Andi Shyti , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Peter Rosin , =?UTF-8?q?Micha=C5=82=20Miros=C5=82aw?= , Svyatoslav Ryhel , =?UTF-8?q?Jonas=20Schw=C3=B6bel?= Cc: linux-i2c@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v1 2/2] i2c: muxes: Add GPIO-detected hotplug I2C Date: Mon, 15 Sep 2025 09:01:38 +0300 Message-ID: <20250915060141.12540-3-clamor95@gmail.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250915060141.12540-1-clamor95@gmail.com> References: <20250915060141.12540-1-clamor95@gmail.com> 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 From: Micha=C5=82 Miros=C5=82aw Add support for hot-plugged I2C busses, where some devices on the bus are hot-pluggable and their presence is indicated by GPIO line. This feature is used by the ASUS Transformers, Microsoft Surface RT/2 and maybe more. Co-developed-by: Ion Agorria Signed-off-by: Ion Agorria Signed-off-by: Micha=C5=82 Miros=C5=82aw Signed-off-by: Svyatoslav Ryhel --- drivers/i2c/muxes/Kconfig | 11 ++ drivers/i2c/muxes/Makefile | 1 + drivers/i2c/muxes/i2c-hotplug-gpio.c | 263 +++++++++++++++++++++++++++ 3 files changed, 275 insertions(+) create mode 100644 drivers/i2c/muxes/i2c-hotplug-gpio.c diff --git a/drivers/i2c/muxes/Kconfig b/drivers/i2c/muxes/Kconfig index 6d2f66810cdc..0e410be5ce55 100644 --- a/drivers/i2c/muxes/Kconfig +++ b/drivers/i2c/muxes/Kconfig @@ -19,6 +19,17 @@ config I2C_ARB_GPIO_CHALLENGE This driver can also be built as a module. If so, the module will be called i2c-arb-gpio-challenge. =20 +config I2C_HOTPLUG_GPIO + tristate "Hot-plugged I2C bus detected by GPIO" + depends on GPIOLIB + depends on OF + help + Say Y here if you want support for hot-plugging I2C devices + with presence detected by GPIO pin value. + + This driver can also be built as a module. If so, the module + will be called i2c-hotplug-gpio. + config I2C_MUX_GPIO tristate "GPIO-based I2C multiplexer" depends on GPIOLIB diff --git a/drivers/i2c/muxes/Makefile b/drivers/i2c/muxes/Makefile index 4b24f49515a7..36df41c8cf05 100644 --- a/drivers/i2c/muxes/Makefile +++ b/drivers/i2c/muxes/Makefile @@ -6,6 +6,7 @@ obj-$(CONFIG_I2C_ARB_GPIO_CHALLENGE) +=3D i2c-arb-gpio-chal= lenge.o =20 obj-$(CONFIG_I2C_DEMUX_PINCTRL) +=3D i2c-demux-pinctrl.o =20 +obj-$(CONFIG_I2C_HOTPLUG_GPIO) +=3D i2c-hotplug-gpio.o obj-$(CONFIG_I2C_MUX_GPIO) +=3D i2c-mux-gpio.o obj-$(CONFIG_I2C_MUX_GPMUX) +=3D i2c-mux-gpmux.o obj-$(CONFIG_I2C_MUX_LTC4306) +=3D i2c-mux-ltc4306.o diff --git a/drivers/i2c/muxes/i2c-hotplug-gpio.c b/drivers/i2c/muxes/i2c-h= otplug-gpio.c new file mode 100644 index 000000000000..7f56964e285e --- /dev/null +++ b/drivers/i2c/muxes/i2c-hotplug-gpio.c @@ -0,0 +1,263 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include +#include +#include +#include +#include + +struct i2c_hotplug_priv { + struct i2c_adapter adap; + struct i2c_adapter *parent; + struct device *dev; + struct gpio_desc *gpio; + int irq; +}; + +static inline struct i2c_adapter *i2c_hotplug_parent(struct i2c_adapter *a= dap) +{ + struct i2c_hotplug_priv *priv =3D container_of(adap, struct i2c_hotplug_p= riv, adap); + + return priv->parent; +} + +static int i2c_hotplug_master_xfer(struct i2c_adapter *adap, + struct i2c_msg msgs[], int num) +{ + struct i2c_adapter *parent =3D i2c_hotplug_parent(adap); + + return parent->algo->master_xfer(parent, msgs, num); +} + +static int i2c_hotplug_smbus_xfer(struct i2c_adapter *adap, u16 addr, + unsigned short flags, char read_write, + u8 command, int protocol, + union i2c_smbus_data *data) +{ + struct i2c_adapter *parent =3D i2c_hotplug_parent(adap); + + return parent->algo->smbus_xfer(parent, addr, flags, read_write, + command, protocol, data); +} + +static u32 i2c_hotplug_functionality(struct i2c_adapter *adap) +{ + u32 parent_func =3D i2c_get_functionality(i2c_hotplug_parent(adap)); + + return parent_func & ~I2C_FUNC_SLAVE; +} + +static const struct i2c_algorithm i2c_hotplug_algo_i2c =3D { + .master_xfer =3D i2c_hotplug_master_xfer, + .functionality =3D i2c_hotplug_functionality, +}; + +static const struct i2c_algorithm i2c_hotplug_algo_smbus =3D { + .smbus_xfer =3D i2c_hotplug_smbus_xfer, + .functionality =3D i2c_hotplug_functionality, +}; + +static const struct i2c_algorithm i2c_hotplug_algo_both =3D { + .master_xfer =3D i2c_hotplug_master_xfer, + .smbus_xfer =3D i2c_hotplug_smbus_xfer, + .functionality =3D i2c_hotplug_functionality, +}; + +static const struct i2c_algorithm *const i2c_hotplug_algo[2][2] =3D { + /* non-I2C */ + { NULL, &i2c_hotplug_algo_smbus }, + /* I2C */ + { &i2c_hotplug_algo_i2c, &i2c_hotplug_algo_both } +}; + +static void i2c_hotplug_lock_bus(struct i2c_adapter *adap, unsigned int fl= ags) +{ + i2c_lock_bus(i2c_hotplug_parent(adap), flags); +} + +static int i2c_hotplug_trylock_bus(struct i2c_adapter *adap, + unsigned int flags) +{ + return i2c_trylock_bus(i2c_hotplug_parent(adap), flags); +} + +static void i2c_hotplug_unlock_bus(struct i2c_adapter *adap, + unsigned int flags) +{ + i2c_unlock_bus(i2c_hotplug_parent(adap), flags); +} + +static const struct i2c_lock_operations i2c_hotplug_lock_ops =3D { + .lock_bus =3D i2c_hotplug_lock_bus, + .trylock_bus =3D i2c_hotplug_trylock_bus, + .unlock_bus =3D i2c_hotplug_unlock_bus, +}; + +static int i2c_hotplug_recover_bus(struct i2c_adapter *adap) +{ + return i2c_recover_bus(i2c_hotplug_parent(adap)); +} + +static struct i2c_bus_recovery_info i2c_hotplug_recovery_info =3D { + .recover_bus =3D i2c_hotplug_recover_bus, +}; + +static int i2c_hotplug_activate(struct i2c_hotplug_priv *priv) +{ + int ret; + + if (priv->adap.algo_data) + return 0; + + /* + * Store the dev data in adapter dev, since + * previous i2c_del_adapter might have wiped it. + */ + priv->adap.dev.parent =3D priv->dev; + priv->adap.dev.of_node =3D priv->dev->of_node; + + dev_dbg(priv->adap.dev.parent, "connection detected"); + + ret =3D i2c_add_adapter(&priv->adap); + if (!ret) + priv->adap.algo_data =3D (void *)1; + + return ret; +} + +static void i2c_hotplug_deactivate(struct i2c_hotplug_priv *priv) +{ + if (!priv->adap.algo_data) + return; + + dev_dbg(priv->adap.dev.parent, "disconnection detected"); + + i2c_del_adapter(&priv->adap); + priv->adap.algo_data =3D NULL; +} + +static irqreturn_t i2c_hotplug_interrupt(int irq, void *dev_id) +{ + struct i2c_hotplug_priv *priv =3D dev_id; + + /* debounce */ + msleep(20); + + if (gpiod_get_value_cansleep(priv->gpio)) + i2c_hotplug_activate(priv); + else + i2c_hotplug_deactivate(priv); + + return IRQ_HANDLED; +} + +static void wrap_i2c_put_adapter(void *adapter) +{ + i2c_put_adapter(adapter); +} + +static void wrap_i2c_hotplug_deactivate(void *priv) +{ + i2c_hotplug_deactivate(priv); +} + +static int i2c_hotplug_gpio_probe(struct platform_device *pdev) +{ + struct device_node *parent_np; + struct i2c_adapter *parent; + struct i2c_hotplug_priv *priv; + bool is_i2c, is_smbus; + int err; + + priv =3D devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + platform_set_drvdata(pdev, priv); + priv->dev =3D &pdev->dev; + + parent_np =3D of_parse_phandle(pdev->dev.of_node, "i2c-parent", 0); + if (IS_ERR(parent_np)) + return dev_err_probe(&pdev->dev, PTR_ERR(parent_np), + "cannot parse i2c-parent\n"); + + parent =3D of_find_i2c_adapter_by_node(parent_np); + of_node_put(parent_np); + if (IS_ERR(parent)) + return dev_err_probe(&pdev->dev, PTR_ERR(parent), + "failed to get parent I2C adapter\n"); + priv->parent =3D parent; + + err =3D devm_add_action_or_reset(&pdev->dev, wrap_i2c_put_adapter, + parent); + if (err) + return err; + + priv->gpio =3D devm_gpiod_get(&pdev->dev, "detect", GPIOD_IN); + if (IS_ERR(priv->gpio)) + return dev_err_probe(&pdev->dev, PTR_ERR(priv->gpio), + "failed to get detect GPIO\n"); + + is_i2c =3D parent->algo->master_xfer; + is_smbus =3D parent->algo->smbus_xfer; + + snprintf(priv->adap.name, sizeof(priv->adap.name), + "i2c-hotplug (master i2c-%d)", i2c_adapter_id(parent)); + priv->adap.owner =3D THIS_MODULE; + priv->adap.algo =3D i2c_hotplug_algo[is_i2c][is_smbus]; + priv->adap.algo_data =3D NULL; + priv->adap.lock_ops =3D &i2c_hotplug_lock_ops; + priv->adap.class =3D parent->class; + priv->adap.retries =3D parent->retries; + priv->adap.timeout =3D parent->timeout; + priv->adap.quirks =3D parent->quirks; + if (parent->bus_recovery_info) + /* .bus_recovery_info is not const, but won't be modified */ + priv->adap.bus_recovery_info =3D (void *)&i2c_hotplug_recovery_info; + + if (!priv->adap.algo) + return -EINVAL; + + err =3D devm_add_action_or_reset(&pdev->dev, wrap_i2c_hotplug_deactivate,= priv); + if (err) + return err; + + priv->irq =3D platform_get_irq(pdev, 0); + if (priv->irq < 0) + return dev_err_probe(&pdev->dev, priv->irq, + "failed to get IRQ %d\n", priv->irq); + + err =3D devm_request_threaded_irq(&pdev->dev, priv->irq, NULL, + i2c_hotplug_interrupt, + IRQF_ONESHOT | IRQF_SHARED, + "i2c-hotplug", priv); + if (err) + return dev_err_probe(&pdev->dev, err, + "failed to register IRQ %d\n", priv->irq); + + irq_wake_thread(priv->irq, priv); + + return 0; +} + +static const struct of_device_id i2c_hotplug_gpio_of_match[] =3D { + { .compatible =3D "i2c-hotplug-gpio" }, + { }, +}; +MODULE_DEVICE_TABLE(of, i2c_hotplug_gpio_of_match); + +static struct platform_driver i2c_hotplug_gpio_driver =3D { + .driver =3D { + .name =3D "i2c-hotplug-gpio", + .of_match_table =3D i2c_hotplug_gpio_of_match, + }, + .probe =3D i2c_hotplug_gpio_probe, +}; + +module_platform_driver(i2c_hotplug_gpio_driver); + +MODULE_DESCRIPTION("Hot-plugged I2C bus detected by GPIO"); +MODULE_AUTHOR("Micha=C5=82 Miros=C5=82aw "); +MODULE_LICENSE("GPL"); --=20 2.48.1