From patchwork Sat Nov 11 21:51:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Luiz Angelo Daros de Luca X-Patchwork-Id: 13453190 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 DDE141DA50; Sat, 11 Nov 2023 21:57:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PFL1HlsK" Received: from mail-ot1-x334.google.com (mail-ot1-x334.google.com [IPv6:2607:f8b0:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DB3B62737; Sat, 11 Nov 2023 13:57:12 -0800 (PST) Received: by mail-ot1-x334.google.com with SMTP id 46e09a7af769-6ce2ee17cb5so1873205a34.2; Sat, 11 Nov 2023 13:57:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699739831; x=1700344631; 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=NXqAk6xN281UpSgUatM/i7XchjeZPtdGe/hPZ9eSb6o=; b=PFL1HlsKlCNdeyufI/ja0COxZeRn8ENLPEkImQm92yLnkXA5k4Fy2+fiYpLTl16Pso yl+ZmsR91rCs0NxnnjO4qqkaawLXWWiq3EVDVgegbK5g5PwujdcJSboANjiAHHC0N1eO l94TpEZvVn7NX2mQg+iMzOLLBVi3DjeZcIZNHZzhBCk5z6FjCD3P6L/8R92lDjYQ0t5p 3YSCzo2Of+SGMJc8xrtRoJmSl/FzluN6aB3k2l/HYEMPVZ4LniV80WfoWjpBU+yBA7ZW IWXrKEjw4kLDhwYv8214kuHPkv9TvTYd/QcZr/OUMRQAgvCX+2H+pypRAC53yDSzsc6j i6CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699739831; x=1700344631; 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=NXqAk6xN281UpSgUatM/i7XchjeZPtdGe/hPZ9eSb6o=; b=a6nZwXpYuyiPYGyULw48VhFSdcCuVzE21faDQhENn5q7/0UA7JyizGpmu+tByBK5gK 5bsBSPNZzE6V2DKw4WyM477jJvp8PRMqVh7cuXvJK5ADIY+K0/Ub4MfSvxUN8BEPKy1k OtQC9dn96nIW5LHdnPPyXT8YA8n3TSZyawVa75jZXvSzaQFyf/km6QUbpYipNkz5rGuP eWPDNm1BHu/XVknUjxUvMvxc5ZtBo5DksSQo8510bm1palVZkwBeSICG6lKa7ldUDEg8 ZKAe3kyvc2NbYh3//ZSGiZvnkJJiOlJo/azpkhs/2KUQhslGpfD9+NKLaMOqHzUFNlQ9 utlA== X-Gm-Message-State: AOJu0Yw1oZwR5U1xHbX5hSPkChmSo8q3/CMlLtCrpXRYgOV3gXMC7NLO kyje9WfSKx6KYdW56YsT+NRuVPcTR0UfIg== X-Google-Smtp-Source: AGHT+IGHH3mShd6Tpzm45Xac6betq7nRzlhXyxCv5mxIKsLYRZ12Vtbov4OWTH7z4Y+UAdkrvsXnKQ== X-Received: by 2002:a05:6870:2b04:b0:1e9:94c1:9179 with SMTP id ld4-20020a0568702b0400b001e994c19179mr4496278oab.21.1699739831265; Sat, 11 Nov 2023 13:57:11 -0800 (PST) Received: from tresc054937.tre-sc.gov.br (177-131-126-82.acessoline.net.br. [177.131.126.82]) by smtp.gmail.com with ESMTPSA id 25-20020a17090a199900b002801184885dsm1867210pji.4.2023.11.11.13.57.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Nov 2023 13:57:10 -0800 (PST) From: Luiz Angelo Daros de Luca To: netdev@vger.kernel.org Cc: linus.walleij@linaro.org, alsi@bang-olufsen.dk, andrew@lunn.ch, vivien.didelot@gmail.com, f.fainelli@gmail.com, olteanv@gmail.com, davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com, robh+dt@kernel.org, krzk+dt@kernel.org, arinc.unal@arinc9.com, Luiz Angelo Daros de Luca , devicetree@vger.kernel.org, Rob Herring Subject: [RFC net-next 1/5] dt-bindings: net: dsa: realtek: reset-gpios is not required Date: Sat, 11 Nov 2023 18:51:04 -0300 Message-ID: <20231111215647.4966-2-luizluca@gmail.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231111215647.4966-1-luizluca@gmail.com> References: <20231111215647.4966-1-luizluca@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC The 'reset-gpios' should not be mandatory. although they might be required for some devices if the switch reset was left asserted by a previous driver, such as the bootloader. Signed-off-by: Luiz Angelo Daros de Luca Cc: devicetree@vger.kernel.org Acked-by: Arınç ÜNAL Acked-by: Rob Herring Reviewed-by: Linus Walleij Reviewed-by: Alvin Šipraga --- Documentation/devicetree/bindings/net/dsa/realtek.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/Documentation/devicetree/bindings/net/dsa/realtek.yaml b/Documentation/devicetree/bindings/net/dsa/realtek.yaml index cce692f57b08..46e113df77c8 100644 --- a/Documentation/devicetree/bindings/net/dsa/realtek.yaml +++ b/Documentation/devicetree/bindings/net/dsa/realtek.yaml @@ -127,7 +127,6 @@ else: - mdc-gpios - mdio-gpios - mdio - - reset-gpios required: - compatible From patchwork Sat Nov 11 21:51:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Luiz Angelo Daros de Luca X-Patchwork-Id: 13453191 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 B86361DFE4; Sat, 11 Nov 2023 21:57:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XblSY0XV" Received: from mail-io1-xd31.google.com (mail-io1-xd31.google.com [IPv6:2607:f8b0:4864:20::d31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0309B2737; Sat, 11 Nov 2023 13:57:17 -0800 (PST) Received: by mail-io1-xd31.google.com with SMTP id ca18e2360f4ac-7a69a71cc1dso122293739f.0; Sat, 11 Nov 2023 13:57:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699739835; x=1700344635; 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=s/5uDgxkKk+a96wf8mqYcPgtbw0PYyOeWz5yErsLgRI=; b=XblSY0XVHfDSowtYdIsTj7bAcm+kWdLL1iqV+R4vSNn0QO2uN9Mf0n24EhiUXbMuHk BaotuSdpnb7QfPH1CY6QE0BjE5EhI6p2PGd9GvhmcJsSI4g0m6tBenQRuqRPF2K/Ucc/ 9TGCyJxXCoZuIY2xcAHoIIHXQx2tgZhSzF5cxPTgHZJeS1A2XE3o4YOolCYxlgUt5OA0 a3M84/noLxh3/kw7CCLwQouLNbgtqXKeH2bTV79wKekJdlRy+7I/ILAvTQ9qiguG4Fg2 gWckUYA6QkIzzi0VDzhhswEodKcwMxmWWOZBhqIco1sebz3o+H24Zqs4+Ykyk+qGLRaI aaew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699739835; x=1700344635; 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=s/5uDgxkKk+a96wf8mqYcPgtbw0PYyOeWz5yErsLgRI=; b=SgMb7UAMTZEKlVf5nbBm9gKdVo6gf/i9kZBNEzR20gq+FYO4V1DUHBck4X4KxUvGbX veZD6n2konwL7eH/w6OR9SCRCpzXdRylPK2YKjvzC5lX66MUN70knjnLHZcLZjt+H4/5 yWXDmWhal5faEQax54dL+lLKJQVbDOrsuaA+EmzN8f8soQMuNVWe/z7FMTjH49yeo4PA Xh/EqxDAut3H+lTKRKpsoZvfeS99BoF3PEBBdH2yEklbzvUgHsgxWMf7l0uRQUN3yekX gKwIr5TP4YQcH7GyRRwnGfaahOR6rnA271IzIjrpzbRmYn1tGPN2V+uQCDFN/zeGUTNs rWNQ== X-Gm-Message-State: AOJu0YyMYeUIxW/yvih0n8UXhr/WUkBXCDtgyWZZHbRLXj4jl+VTQx++ QbF/J0jIzmq/QeshZGWVdUj2vWyvHZIUUg== X-Google-Smtp-Source: AGHT+IHQ/jyTmvo2HXsC4gEuqg9za3k/5SvHaVttATYDSDe3/aCAD+hkt+Dpna0M4yCz8kPRP7x8Xg== X-Received: by 2002:a05:6e02:1a8b:b0:34f:70ec:d4cf with SMTP id k11-20020a056e021a8b00b0034f70ecd4cfmr4819948ilv.8.1699739835397; Sat, 11 Nov 2023 13:57:15 -0800 (PST) Received: from tresc054937.tre-sc.gov.br (177-131-126-82.acessoline.net.br. [177.131.126.82]) by smtp.gmail.com with ESMTPSA id 25-20020a17090a199900b002801184885dsm1867210pji.4.2023.11.11.13.57.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Nov 2023 13:57:15 -0800 (PST) From: Luiz Angelo Daros de Luca To: netdev@vger.kernel.org Cc: linus.walleij@linaro.org, alsi@bang-olufsen.dk, andrew@lunn.ch, vivien.didelot@gmail.com, f.fainelli@gmail.com, olteanv@gmail.com, davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com, robh+dt@kernel.org, krzk+dt@kernel.org, arinc.unal@arinc9.com, Luiz Angelo Daros de Luca , devicetree@vger.kernel.org Subject: [RFC net-next 2/5] dt-bindings: net: dsa: realtek: add reset controller Date: Sat, 11 Nov 2023 18:51:05 -0300 Message-ID: <20231111215647.4966-3-luizluca@gmail.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231111215647.4966-1-luizluca@gmail.com> References: <20231111215647.4966-1-luizluca@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC Realtek switches can use a reset controller instead of reset-gpios. Signed-off-by: Luiz Angelo Daros de Luca Cc: devicetree@vger.kernel.org Acked-by: Arınç ÜNAL Reviewed-by: Linus Walleij Reviewed-by: Alvin Šipraga Acked-by: Rob Herring --- Documentation/devicetree/bindings/net/dsa/realtek.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Documentation/devicetree/bindings/net/dsa/realtek.yaml b/Documentation/devicetree/bindings/net/dsa/realtek.yaml index 46e113df77c8..70b6bda3cf98 100644 --- a/Documentation/devicetree/bindings/net/dsa/realtek.yaml +++ b/Documentation/devicetree/bindings/net/dsa/realtek.yaml @@ -59,6 +59,9 @@ properties: description: GPIO to be used to reset the whole device maxItems: 1 + resets: + maxItems: 1 + realtek,disable-leds: type: boolean description: | From patchwork Sat Nov 11 21:51:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Angelo Daros de Luca X-Patchwork-Id: 13453192 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 05A26200AC for ; Sat, 11 Nov 2023 21:57:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NZSojA2+" Received: from mail-oo1-xc2a.google.com (mail-oo1-xc2a.google.com [IPv6:2607:f8b0:4864:20::c2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8502A2737 for ; Sat, 11 Nov 2023 13:57:21 -0800 (PST) Received: by mail-oo1-xc2a.google.com with SMTP id 006d021491bc7-58a0d0cdcc1so1038727eaf.1 for ; Sat, 11 Nov 2023 13:57:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699739840; x=1700344640; 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=aYpmLsQs+dQQCyYUPYba+b1wzeVO8kEdwnR5cr6h/Cw=; b=NZSojA2+SqIe6fIe97Lu9Da5qvzx68ZBoO3encMZKmkFxad7V5PsN1F+QF//8BesyY O+4BJheW2+petIJbd38xgFt0/SwaVwiyO/phywDjaSDL9czwneK9MutcJ5zYpimN6qrk IGfFPsqk6I52HK7M3HksoVKzzBLgqTZSo4sV/9Ixxt2THlJ2tE8+22g1Gv1YDxNLXCns wC+Ujk7dY06tZEP34DVs80t/Y57ufR0Ev1+l4mjMrmNNzO5Im9vcvkid+NmMKGZtTHuu /TS854ACygqcVuxFaATF5L9/ERsL64GOziXg3spD9jB3vBu8OGDSTC63NpOKfPAKN4wF y4Tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699739840; x=1700344640; 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=aYpmLsQs+dQQCyYUPYba+b1wzeVO8kEdwnR5cr6h/Cw=; b=qBf3uj9TJllakHQd1Y942va5t85Q3D8gVnZnT6A6dd1ASpdwX2/a0Sl3PVqsguCAV0 SAd9ltAIVd8dWREgsvO37TAnTEzKAI6MjrBcoe97hd7bRjncwlwHIYc8fYsy9L0iZ5ec yxU3CLVTGhhKVnl63ggkwAi8xl+5vKF8pUcm4+BvF5gdul1iFPkzBa3fMj4EYH/Flpte JI4xNyM8XEuYebPxcq4ptPXOBbt+DdPGAIxBAXZ5prvxxgMRvuvqmzQDUuaz38eiWrmc EVCtgg4g6W8bSqnl/3woDypQ3UHGGGykrCVxAsRiIrcOod1AP92dcOrLtFJIaHA1ZruA C09g== X-Gm-Message-State: AOJu0YxX1pdv+ulEADOdJoiJEa2t3pq8320ms7akmZUcn7h3go026xMm gF2wkCudWxlhD8hh+fCTDjDKsnWrgnakOQ== X-Google-Smtp-Source: AGHT+IFYxf22m4HPbyr3x9Rbd/u7RGQEPQR8UEqV7Qlqj4B/lJQcfmAYrRgti+0ao3P1R26W6gEOKA== X-Received: by 2002:a05:6870:3b11:b0:1f2:dfbf:607b with SMTP id gh17-20020a0568703b1100b001f2dfbf607bmr4214213oab.25.1699739839476; Sat, 11 Nov 2023 13:57:19 -0800 (PST) Received: from tresc054937.tre-sc.gov.br (177-131-126-82.acessoline.net.br. [177.131.126.82]) by smtp.gmail.com with ESMTPSA id 25-20020a17090a199900b002801184885dsm1867210pji.4.2023.11.11.13.57.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Nov 2023 13:57:19 -0800 (PST) From: Luiz Angelo Daros de Luca To: netdev@vger.kernel.org Cc: linus.walleij@linaro.org, alsi@bang-olufsen.dk, andrew@lunn.ch, vivien.didelot@gmail.com, f.fainelli@gmail.com, olteanv@gmail.com, davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com, robh+dt@kernel.org, krzk+dt@kernel.org, arinc.unal@arinc9.com, Luiz Angelo Daros de Luca Subject: [RFC net-next 3/5] net: dsa: realtek: create realtek-common Date: Sat, 11 Nov 2023 18:51:06 -0300 Message-ID: <20231111215647.4966-4-luizluca@gmail.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231111215647.4966-1-luizluca@gmail.com> References: <20231111215647.4966-1-luizluca@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC Some code can be shared between both interface modules (MDIO and SMI) and amongst variants. For now, these interface functions are shared: - realtek_common_lock - realtek_common_unlock - realtek_common_probe - realtek_common_remove The reset during probe was moved to the last moment before a variant detects the switch. This way, we avoid a reset if anything else fails. The symbols from variants used in of_match_table are now in a single match table in realtek-common, used by both interfaces. Signed-off-by: Luiz Angelo Daros de Luca Reviewed-by: Linus Walleij --- drivers/net/dsa/realtek/Makefile | 1 + drivers/net/dsa/realtek/realtek-common.c | 139 +++++++++++++++++++++++ drivers/net/dsa/realtek/realtek-common.h | 16 +++ drivers/net/dsa/realtek/realtek-mdio.c | 116 +++---------------- drivers/net/dsa/realtek/realtek-smi.c | 127 +++------------------ drivers/net/dsa/realtek/realtek.h | 2 + 6 files changed, 184 insertions(+), 217 deletions(-) create mode 100644 drivers/net/dsa/realtek/realtek-common.c create mode 100644 drivers/net/dsa/realtek/realtek-common.h diff --git a/drivers/net/dsa/realtek/Makefile b/drivers/net/dsa/realtek/Makefile index 0aab57252a7c..5e0c1ef200a3 100644 --- a/drivers/net/dsa/realtek/Makefile +++ b/drivers/net/dsa/realtek/Makefile @@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_NET_DSA_REALTEK) += realtek-common.o obj-$(CONFIG_NET_DSA_REALTEK_MDIO) += realtek-mdio.o obj-$(CONFIG_NET_DSA_REALTEK_SMI) += realtek-smi.o obj-$(CONFIG_NET_DSA_REALTEK_RTL8366RB) += rtl8366.o diff --git a/drivers/net/dsa/realtek/realtek-common.c b/drivers/net/dsa/realtek/realtek-common.c new file mode 100644 index 000000000000..36f8b60771be --- /dev/null +++ b/drivers/net/dsa/realtek/realtek-common.c @@ -0,0 +1,139 @@ +// SPDX-License-Identifier: GPL-2.0+ + +#include + +#include "realtek.h" +#include "realtek-common.h" + +void realtek_common_lock(void *ctx) +{ + struct realtek_priv *priv = ctx; + + mutex_lock(&priv->map_lock); +} +EXPORT_SYMBOL_GPL(realtek_common_lock); + +void realtek_common_unlock(void *ctx) +{ + struct realtek_priv *priv = ctx; + + mutex_unlock(&priv->map_lock); +} +EXPORT_SYMBOL_GPL(realtek_common_unlock); + +struct realtek_priv *realtek_common_probe(struct device *dev, + struct regmap_config rc, struct regmap_config rc_nolock) +{ + const struct realtek_variant *var; + struct realtek_priv *priv; + struct device_node *np; + int ret; + + var = of_device_get_match_data(dev); + if (!var) + return ERR_PTR(-EINVAL); + + priv = devm_kzalloc(dev, size_add(sizeof(*priv), var->chip_data_sz), + GFP_KERNEL); + if (!priv) + return ERR_PTR(-ENOMEM); + + mutex_init(&priv->map_lock); + + rc.lock_arg = priv; + priv->map = devm_regmap_init(dev, NULL, priv, &rc); + if (IS_ERR(priv->map)) { + ret = PTR_ERR(priv->map); + dev_err(dev, "regmap init failed: %d\n", ret); + return ERR_PTR(ret); + } + + priv->map_nolock = devm_regmap_init(dev, NULL, priv, &rc_nolock); + if (IS_ERR(priv->map_nolock)) { + ret = PTR_ERR(priv->map_nolock); + dev_err(dev, "regmap init failed: %d\n", ret); + return ERR_PTR(ret); + } + + /* Link forward and backward */ + priv->dev = dev; + priv->clk_delay = var->clk_delay; + priv->cmd_read = var->cmd_read; + priv->cmd_write = var->cmd_write; + priv->variant = var; + priv->ops = var->ops; + priv->chip_data = (void *)priv + sizeof(*priv); + + dev_set_drvdata(dev, priv); + spin_lock_init(&priv->lock); + + /* Fetch MDIO pins */ + priv->mdc = devm_gpiod_get_optional(dev, "mdc", GPIOD_OUT_LOW); + if (IS_ERR(priv->mdc)) + return ERR_CAST(priv->mdc); + + priv->mdio = devm_gpiod_get_optional(dev, "mdio", GPIOD_OUT_LOW); + if (IS_ERR(priv->mdio)) + return ERR_CAST(priv->mdio); + + np = dev->of_node; + + priv->leds_disabled = of_property_read_bool(np, "realtek,disable-leds"); + + /* TODO: if power is software controlled, set up any regulators here */ + + priv->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); + if (IS_ERR(priv->reset)) { + dev_err(dev, "failed to get RESET GPIO\n"); + return ERR_CAST(priv->reset); + } + if (priv->reset) { + gpiod_set_value(priv->reset, 1); + dev_dbg(dev, "asserted RESET\n"); + msleep(REALTEK_HW_STOP_DELAY); + gpiod_set_value(priv->reset, 0); + msleep(REALTEK_HW_START_DELAY); + dev_dbg(dev, "deasserted RESET\n"); + } + + priv->ds = devm_kzalloc(dev, sizeof(*priv->ds), GFP_KERNEL); + if (!priv->ds) + return ERR_PTR(-ENOMEM); + + priv->ds->dev = dev; + priv->ds->priv = priv; + + return priv; +} +EXPORT_SYMBOL(realtek_common_probe); + +void realtek_common_remove(struct realtek_priv *priv) +{ + if (!priv) + return; + + dsa_unregister_switch(priv->ds); + if (priv->user_mii_bus) + of_node_put(priv->user_mii_bus->dev.of_node); + + /* leave the device reset asserted */ + if (priv->reset) + gpiod_set_value(priv->reset, 1); +} +EXPORT_SYMBOL(realtek_common_remove); + +const struct of_device_id realtek_common_of_match[] = { +#if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8366RB) + { .compatible = "realtek,rtl8366rb", .data = &rtl8366rb_variant, }, +#endif +#if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8365MB) + { .compatible = "realtek,rtl8365mb", .data = &rtl8366rb_variant, }, +#endif + { /* sentinel */ }, +}; +MODULE_DEVICE_TABLE(of, realtek_common_of_match); +EXPORT_SYMBOL_GPL(realtek_common_of_match); + +MODULE_AUTHOR("Luiz Angelo Daros de Luca "); +MODULE_DESCRIPTION("Realtek DSA switches common module"); +MODULE_LICENSE("GPL"); diff --git a/drivers/net/dsa/realtek/realtek-common.h b/drivers/net/dsa/realtek/realtek-common.h new file mode 100644 index 000000000000..90a949386518 --- /dev/null +++ b/drivers/net/dsa/realtek/realtek-common.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ + +#ifndef _REALTEK_INTERFACE_H +#define _REALTEK_INTERFACE_H + +#include + +extern const struct of_device_id realtek_common_of_match[]; + +void realtek_common_lock(void *ctx); +void realtek_common_unlock(void *ctx); +struct realtek_priv *realtek_common_probe(struct device *dev, + struct regmap_config rc, struct regmap_config rc_nolock); +void realtek_common_remove(struct realtek_priv *priv); + +#endif diff --git a/drivers/net/dsa/realtek/realtek-mdio.c b/drivers/net/dsa/realtek/realtek-mdio.c index 292e6d087e8b..6f610386c977 100644 --- a/drivers/net/dsa/realtek/realtek-mdio.c +++ b/drivers/net/dsa/realtek/realtek-mdio.c @@ -25,6 +25,7 @@ #include #include "realtek.h" +#include "realtek-common.h" /* Read/write via mdiobus */ #define REALTEK_MDIO_CTRL0_REG 31 @@ -99,20 +100,6 @@ static int realtek_mdio_read(void *ctx, u32 reg, u32 *val) return ret; } -static void realtek_mdio_lock(void *ctx) -{ - struct realtek_priv *priv = ctx; - - mutex_lock(&priv->map_lock); -} - -static void realtek_mdio_unlock(void *ctx) -{ - struct realtek_priv *priv = ctx; - - mutex_unlock(&priv->map_lock); -} - static const struct regmap_config realtek_mdio_regmap_config = { .reg_bits = 10, /* A4..A0 R4..R0 */ .val_bits = 16, @@ -123,8 +110,8 @@ static const struct regmap_config realtek_mdio_regmap_config = { .reg_read = realtek_mdio_read, .reg_write = realtek_mdio_write, .cache_type = REGCACHE_NONE, - .lock = realtek_mdio_lock, - .unlock = realtek_mdio_unlock, + .lock = realtek_common_lock, + .unlock = realtek_common_unlock, }; static const struct regmap_config realtek_mdio_nolock_regmap_config = { @@ -142,75 +129,19 @@ static const struct regmap_config realtek_mdio_nolock_regmap_config = { static int realtek_mdio_probe(struct mdio_device *mdiodev) { - struct realtek_priv *priv; struct device *dev = &mdiodev->dev; - const struct realtek_variant *var; - struct regmap_config rc; - struct device_node *np; + struct realtek_priv *priv; int ret; - var = of_device_get_match_data(dev); - if (!var) - return -EINVAL; - - priv = devm_kzalloc(&mdiodev->dev, - size_add(sizeof(*priv), var->chip_data_sz), - GFP_KERNEL); - if (!priv) - return -ENOMEM; - - mutex_init(&priv->map_lock); - - rc = realtek_mdio_regmap_config; - rc.lock_arg = priv; - priv->map = devm_regmap_init(dev, NULL, priv, &rc); - if (IS_ERR(priv->map)) { - ret = PTR_ERR(priv->map); - dev_err(dev, "regmap init failed: %d\n", ret); - return ret; - } - - rc = realtek_mdio_nolock_regmap_config; - priv->map_nolock = devm_regmap_init(dev, NULL, priv, &rc); - if (IS_ERR(priv->map_nolock)) { - ret = PTR_ERR(priv->map_nolock); - dev_err(dev, "regmap init failed: %d\n", ret); - return ret; - } + priv = realtek_common_probe(dev, realtek_mdio_regmap_config, + realtek_mdio_nolock_regmap_config); + if (IS_ERR(priv)) + return PTR_ERR(priv); priv->mdio_addr = mdiodev->addr; priv->bus = mdiodev->bus; - priv->dev = &mdiodev->dev; - priv->chip_data = (void *)priv + sizeof(*priv); - - priv->clk_delay = var->clk_delay; - priv->cmd_read = var->cmd_read; - priv->cmd_write = var->cmd_write; - priv->ops = var->ops; - priv->write_reg_noack = realtek_mdio_write; - - np = dev->of_node; - - dev_set_drvdata(dev, priv); - - /* TODO: if power is software controlled, set up any regulators here */ - priv->leds_disabled = of_property_read_bool(np, "realtek,disable-leds"); - - priv->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); - if (IS_ERR(priv->reset)) { - dev_err(dev, "failed to get RESET GPIO\n"); - return PTR_ERR(priv->reset); - } - - if (priv->reset) { - gpiod_set_value(priv->reset, 1); - dev_dbg(dev, "asserted RESET\n"); - msleep(REALTEK_HW_STOP_DELAY); - gpiod_set_value(priv->reset, 0); - msleep(REALTEK_HW_START_DELAY); - dev_dbg(dev, "deasserted RESET\n"); - } + priv->ds->ops = priv->variant->ds_ops_mdio; ret = priv->ops->detect(priv); if (ret) { @@ -218,18 +149,12 @@ static int realtek_mdio_probe(struct mdio_device *mdiodev) return ret; } - priv->ds = devm_kzalloc(dev, sizeof(*priv->ds), GFP_KERNEL); - if (!priv->ds) - return -ENOMEM; - - priv->ds->dev = dev; priv->ds->num_ports = priv->num_ports; - priv->ds->priv = priv; - priv->ds->ops = var->ds_ops_mdio; ret = dsa_register_switch(priv->ds); if (ret) { - dev_err(priv->dev, "unable to register switch ret = %d\n", ret); + dev_err_probe(dev, ret, "unable to register switch ret = %pe\n", + ERR_PTR(ret)); return ret; } @@ -243,11 +168,7 @@ static void realtek_mdio_remove(struct mdio_device *mdiodev) if (!priv) return; - dsa_unregister_switch(priv->ds); - - /* leave the device reset asserted */ - if (priv->reset) - gpiod_set_value(priv->reset, 1); + realtek_common_remove(priv); } static void realtek_mdio_shutdown(struct mdio_device *mdiodev) @@ -262,21 +183,10 @@ static void realtek_mdio_shutdown(struct mdio_device *mdiodev) dev_set_drvdata(&mdiodev->dev, NULL); } -static const struct of_device_id realtek_mdio_of_match[] = { -#if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8366RB) - { .compatible = "realtek,rtl8366rb", .data = &rtl8366rb_variant, }, -#endif -#if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8365MB) - { .compatible = "realtek,rtl8365mb", .data = &rtl8365mb_variant, }, -#endif - { /* sentinel */ }, -}; -MODULE_DEVICE_TABLE(of, realtek_mdio_of_match); - static struct mdio_driver realtek_mdio_driver = { .mdiodrv.driver = { .name = "realtek-mdio", - .of_match_table = realtek_mdio_of_match, + .of_match_table = realtek_common_of_match, }, .probe = realtek_mdio_probe, .remove = realtek_mdio_remove, diff --git a/drivers/net/dsa/realtek/realtek-smi.c b/drivers/net/dsa/realtek/realtek-smi.c index 755546ed8db6..0cf89f9db99e 100644 --- a/drivers/net/dsa/realtek/realtek-smi.c +++ b/drivers/net/dsa/realtek/realtek-smi.c @@ -40,6 +40,7 @@ #include #include "realtek.h" +#include "realtek-common.h" #define REALTEK_SMI_ACK_RETRY_COUNT 5 @@ -310,20 +311,6 @@ static int realtek_smi_read(void *ctx, u32 reg, u32 *val) return realtek_smi_read_reg(priv, reg, val); } -static void realtek_smi_lock(void *ctx) -{ - struct realtek_priv *priv = ctx; - - mutex_lock(&priv->map_lock); -} - -static void realtek_smi_unlock(void *ctx) -{ - struct realtek_priv *priv = ctx; - - mutex_unlock(&priv->map_lock); -} - static const struct regmap_config realtek_smi_regmap_config = { .reg_bits = 10, /* A4..A0 R4..R0 */ .val_bits = 16, @@ -334,8 +321,8 @@ static const struct regmap_config realtek_smi_regmap_config = { .reg_read = realtek_smi_read, .reg_write = realtek_smi_write, .cache_type = REGCACHE_NONE, - .lock = realtek_smi_lock, - .unlock = realtek_smi_unlock, + .lock = realtek_common_lock, + .unlock = realtek_common_unlock, }; static const struct regmap_config realtek_smi_nolock_regmap_config = { @@ -410,78 +397,18 @@ static int realtek_smi_setup_mdio(struct dsa_switch *ds) static int realtek_smi_probe(struct platform_device *pdev) { - const struct realtek_variant *var; struct device *dev = &pdev->dev; struct realtek_priv *priv; - struct regmap_config rc; - struct device_node *np; int ret; - var = of_device_get_match_data(dev); - np = dev->of_node; - - priv = devm_kzalloc(dev, sizeof(*priv) + var->chip_data_sz, GFP_KERNEL); - if (!priv) - return -ENOMEM; - priv->chip_data = (void *)priv + sizeof(*priv); - - mutex_init(&priv->map_lock); - - rc = realtek_smi_regmap_config; - rc.lock_arg = priv; - priv->map = devm_regmap_init(dev, NULL, priv, &rc); - if (IS_ERR(priv->map)) { - ret = PTR_ERR(priv->map); - dev_err(dev, "regmap init failed: %d\n", ret); - return ret; - } - - rc = realtek_smi_nolock_regmap_config; - priv->map_nolock = devm_regmap_init(dev, NULL, priv, &rc); - if (IS_ERR(priv->map_nolock)) { - ret = PTR_ERR(priv->map_nolock); - dev_err(dev, "regmap init failed: %d\n", ret); - return ret; - } - - /* Link forward and backward */ - priv->dev = dev; - priv->clk_delay = var->clk_delay; - priv->cmd_read = var->cmd_read; - priv->cmd_write = var->cmd_write; - priv->ops = var->ops; + priv = realtek_common_probe(dev, realtek_smi_regmap_config, + realtek_smi_nolock_regmap_config); + if (IS_ERR(priv)) + return PTR_ERR(priv); priv->setup_interface = realtek_smi_setup_mdio; priv->write_reg_noack = realtek_smi_write_reg_noack; - - dev_set_drvdata(dev, priv); - spin_lock_init(&priv->lock); - - /* TODO: if power is software controlled, set up any regulators here */ - - priv->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); - if (IS_ERR(priv->reset)) { - dev_err(dev, "failed to get RESET GPIO\n"); - return PTR_ERR(priv->reset); - } - if (priv->reset) { - gpiod_set_value(priv->reset, 1); - dev_dbg(dev, "asserted RESET\n"); - msleep(REALTEK_HW_STOP_DELAY); - gpiod_set_value(priv->reset, 0); - msleep(REALTEK_HW_START_DELAY); - dev_dbg(dev, "deasserted RESET\n"); - } - - /* Fetch MDIO pins */ - priv->mdc = devm_gpiod_get_optional(dev, "mdc", GPIOD_OUT_LOW); - if (IS_ERR(priv->mdc)) - return PTR_ERR(priv->mdc); - priv->mdio = devm_gpiod_get_optional(dev, "mdio", GPIOD_OUT_LOW); - if (IS_ERR(priv->mdio)) - return PTR_ERR(priv->mdio); - - priv->leds_disabled = of_property_read_bool(np, "realtek,disable-leds"); + priv->ds->ops = priv->variant->ds_ops_smi; ret = priv->ops->detect(priv); if (ret) { @@ -489,20 +416,15 @@ static int realtek_smi_probe(struct platform_device *pdev) return ret; } - priv->ds = devm_kzalloc(dev, sizeof(*priv->ds), GFP_KERNEL); - if (!priv->ds) - return -ENOMEM; - - priv->ds->dev = dev; priv->ds->num_ports = priv->num_ports; - priv->ds->priv = priv; - priv->ds->ops = var->ds_ops_smi; ret = dsa_register_switch(priv->ds); if (ret) { - dev_err_probe(dev, ret, "unable to register switch\n"); + dev_err_probe(dev, ret, "unable to register switch ret = %pe\n", + ERR_PTR(ret)); return ret; } + return 0; } @@ -513,13 +435,7 @@ static void realtek_smi_remove(struct platform_device *pdev) if (!priv) return; - dsa_unregister_switch(priv->ds); - if (priv->user_mii_bus) - of_node_put(priv->user_mii_bus->dev.of_node); - - /* leave the device reset asserted */ - if (priv->reset) - gpiod_set_value(priv->reset, 1); + realtek_common_remove(priv); } static void realtek_smi_shutdown(struct platform_device *pdev) @@ -534,27 +450,10 @@ static void realtek_smi_shutdown(struct platform_device *pdev) platform_set_drvdata(pdev, NULL); } -static const struct of_device_id realtek_smi_of_match[] = { -#if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8366RB) - { - .compatible = "realtek,rtl8366rb", - .data = &rtl8366rb_variant, - }, -#endif -#if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8365MB) - { - .compatible = "realtek,rtl8365mb", - .data = &rtl8365mb_variant, - }, -#endif - { /* sentinel */ }, -}; -MODULE_DEVICE_TABLE(of, realtek_smi_of_match); - static struct platform_driver realtek_smi_driver = { .driver = { .name = "realtek-smi", - .of_match_table = realtek_smi_of_match, + .of_match_table = realtek_common_of_match, }, .probe = realtek_smi_probe, .remove_new = realtek_smi_remove, diff --git a/drivers/net/dsa/realtek/realtek.h b/drivers/net/dsa/realtek/realtek.h index 790488e9c667..8d9d546bf5f5 100644 --- a/drivers/net/dsa/realtek/realtek.h +++ b/drivers/net/dsa/realtek/realtek.h @@ -79,6 +79,8 @@ struct realtek_priv { int vlan_enabled; int vlan4k_enabled; + const struct realtek_variant *variant; + char buf[4096]; void *chip_data; /* Per-chip extra variant data */ }; From patchwork Sat Nov 11 21:51:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Luiz Angelo Daros de Luca X-Patchwork-Id: 13453193 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 65A781DFCC for ; Sat, 11 Nov 2023 21:57:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fWG5p6em" Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24BC43253 for ; Sat, 11 Nov 2023 13:57:25 -0800 (PST) Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-2802e5ae23bso3005942a91.2 for ; Sat, 11 Nov 2023 13:57:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699739844; x=1700344644; 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=c1H5txFFqn5OFTfh/LqNKrPLRjDeTeFCxnPBeLUeS/M=; b=fWG5p6emGm2YF2P7fywcqsnD/VAULGLjH94xF2JqD2I64itL+frMwRv86nm5etrnFB uDaeas0bu2+wTBdQjMrdEG/WsA/tow9wUIAyMl+2IFGQq/3sDYjLU+ZkaKL4NGy/AI7X YuN4tg8WLYAeRCrsCIgVQpLFRD9rbmXF3DH7Mr+jIskRSZgMGhDxfdWbBDgtSDxyjc1X ygMPtaz3PHKNAnyd+BJn+srSgiDZIx/QKrBO46HtID07rXHDlrA7g/xTuLwricQfW1HL lUl3+fqlZraTG2mU2Ggr0eLWCGZjULNkl3Pu13wkbNrwgwI1CIQJpwVMdgomIEO+Xa4A NlTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699739844; x=1700344644; 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=c1H5txFFqn5OFTfh/LqNKrPLRjDeTeFCxnPBeLUeS/M=; b=UQ/809ZFhP3P3HpeWSiULdc4i3uc88AQ6YUKyHiBfMQqyW2cwWkQMfql206Zao9KOT Iok5mH96Ajaz2qjnpb/FBpiari32PmZEMah86SpSvK3DYCbm4idN1efOxlHyA7DwGlSG dB9AnO0fupB9DbQ7EKzNpyjjUUiITVyS7BgEJvy5AQKKk7xNVeu1KK9WIuji9Yw9ACzp AFceWKofJ3mmFin2khbHlNiWmlaqaE+rJO6JeLahV3Ux0iKzjVVq3kXoXNDHXK8WIup/ HZfA7leWjkHJgVhaSGNqlHJ7qomgLnWoAT7LNq+AYjQuDODYiTD8yA93FHaqAT9j+UVu HXfA== X-Gm-Message-State: AOJu0YwsIfFi8YpHvApiG3468wAKwBWGsJhb1LGDYWEQciTxe3RyHfpf Q0VTlN2ACTxYVfdGXa9yDr56j/By0Ggs2A== X-Google-Smtp-Source: AGHT+IFynvinJ6IDWeQL0agl1QMv/t9hZy1eRPkhWcisftfCrRT6fldRcrYh5qJNoQfnMgbhpUvnIw== X-Received: by 2002:a17:90b:164d:b0:281:858:a086 with SMTP id il13-20020a17090b164d00b002810858a086mr3292025pjb.6.1699739843533; Sat, 11 Nov 2023 13:57:23 -0800 (PST) Received: from tresc054937.tre-sc.gov.br (177-131-126-82.acessoline.net.br. [177.131.126.82]) by smtp.gmail.com with ESMTPSA id 25-20020a17090a199900b002801184885dsm1867210pji.4.2023.11.11.13.57.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Nov 2023 13:57:23 -0800 (PST) From: Luiz Angelo Daros de Luca To: netdev@vger.kernel.org Cc: linus.walleij@linaro.org, alsi@bang-olufsen.dk, andrew@lunn.ch, vivien.didelot@gmail.com, f.fainelli@gmail.com, olteanv@gmail.com, davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com, robh+dt@kernel.org, krzk+dt@kernel.org, arinc.unal@arinc9.com, Luiz Angelo Daros de Luca Subject: [RFC net-next 4/5] net: dsa: realtek: load switch variants on demand Date: Sat, 11 Nov 2023 18:51:07 -0300 Message-ID: <20231111215647.4966-5-luizluca@gmail.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231111215647.4966-1-luizluca@gmail.com> References: <20231111215647.4966-1-luizluca@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC realtek-common had a hard dependency on both switch variants. That way, it was not possible to selectively load only one model at runtime. Now variants are registered at the realtek-common module and interface modules look for a variant using the compatible string. Signed-off-by: Luiz Angelo Daros de Luca --- drivers/net/dsa/realtek/realtek-common.c | 125 ++++++++++++++++++++--- drivers/net/dsa/realtek/realtek-common.h | 3 + drivers/net/dsa/realtek/realtek-mdio.c | 9 +- drivers/net/dsa/realtek/realtek-smi.c | 9 +- drivers/net/dsa/realtek/realtek.h | 36 ++++++- drivers/net/dsa/realtek/rtl8365mb.c | 4 +- drivers/net/dsa/realtek/rtl8366rb.c | 4 +- 7 files changed, 162 insertions(+), 28 deletions(-) diff --git a/drivers/net/dsa/realtek/realtek-common.c b/drivers/net/dsa/realtek/realtek-common.c index 36f8b60771be..e383db21c776 100644 --- a/drivers/net/dsa/realtek/realtek-common.c +++ b/drivers/net/dsa/realtek/realtek-common.c @@ -1,10 +1,76 @@ // SPDX-License-Identifier: GPL-2.0+ #include +#include #include "realtek.h" #include "realtek-common.h" +static LIST_HEAD(realtek_variants_list); +static DEFINE_MUTEX(realtek_variants_lock); + +void realtek_variant_register(struct realtek_variant_entry *var_ent) +{ + mutex_lock(&realtek_variants_lock); + list_add_tail(&var_ent->list, &realtek_variants_list); + mutex_unlock(&realtek_variants_lock); +} +EXPORT_SYMBOL_GPL(realtek_variant_register); + +void realtek_variant_unregister(struct realtek_variant_entry *var_ent) +{ + mutex_lock(&realtek_variants_lock); + list_del(&var_ent->list); + mutex_unlock(&realtek_variants_lock); +} +EXPORT_SYMBOL_GPL(realtek_variant_unregister); + +const struct realtek_variant *realtek_variant_get( + const struct of_device_id *match) +{ + const struct realtek_variant *var = ERR_PTR(-ENOENT); + struct realtek_variant_entry *var_ent; + const char *modname = match->data; + + request_module(modname); + + mutex_lock(&realtek_variants_lock); + list_for_each_entry(var_ent, &realtek_variants_list, list) { + const struct realtek_variant *tmp = var_ent->variant; + + if (strcmp(match->compatible, var_ent->compatible)) + continue; + + if (!try_module_get(var_ent->owner)) + break; + + var = tmp; + break; + } + mutex_unlock(&realtek_variants_lock); + + return var; +} +EXPORT_SYMBOL_GPL(realtek_variant_get); + +void realtek_variant_put(const struct realtek_variant *var) +{ + struct realtek_variant_entry *var_ent; + + mutex_lock(&realtek_variants_lock); + list_for_each_entry(var_ent, &realtek_variants_list, list) { + if (var_ent->variant != var) + continue; + + if (var_ent->owner) + module_put(var_ent->owner); + + break; + } + mutex_unlock(&realtek_variants_lock); +} +EXPORT_SYMBOL_GPL(realtek_variant_put); + void realtek_common_lock(void *ctx) { struct realtek_priv *priv = ctx; @@ -25,18 +91,30 @@ struct realtek_priv *realtek_common_probe(struct device *dev, struct regmap_config rc, struct regmap_config rc_nolock) { const struct realtek_variant *var; + const struct of_device_id *match; struct realtek_priv *priv; struct device_node *np; int ret; - var = of_device_get_match_data(dev); - if (!var) + match = of_match_device(dev->driver->of_match_table, dev); + if (!match || !match->data) return ERR_PTR(-EINVAL); + var = realtek_variant_get(match); + if (IS_ERR(var)) { + ret = PTR_ERR(var); + dev_err_probe(dev, ret, + "failed to get module for '%s'. Is '%s' loaded?", + match->compatible, match->data); + goto err_variant_put; + } + priv = devm_kzalloc(dev, size_add(sizeof(*priv), var->chip_data_sz), GFP_KERNEL); - if (!priv) - return ERR_PTR(-ENOMEM); + if (!priv) { + ret = -ENOMEM; + goto err_variant_put; + } mutex_init(&priv->map_lock); @@ -45,14 +123,14 @@ struct realtek_priv *realtek_common_probe(struct device *dev, if (IS_ERR(priv->map)) { ret = PTR_ERR(priv->map); dev_err(dev, "regmap init failed: %d\n", ret); - return ERR_PTR(ret); + goto err_variant_put; } priv->map_nolock = devm_regmap_init(dev, NULL, priv, &rc_nolock); if (IS_ERR(priv->map_nolock)) { ret = PTR_ERR(priv->map_nolock); dev_err(dev, "regmap init failed: %d\n", ret); - return ERR_PTR(ret); + goto err_variant_put; } /* Link forward and backward */ @@ -69,23 +147,27 @@ struct realtek_priv *realtek_common_probe(struct device *dev, /* Fetch MDIO pins */ priv->mdc = devm_gpiod_get_optional(dev, "mdc", GPIOD_OUT_LOW); - if (IS_ERR(priv->mdc)) - return ERR_CAST(priv->mdc); + + if (IS_ERR(priv->mdc)) { + ret = PTR_ERR(priv->mdc); + goto err_variant_put; + } priv->mdio = devm_gpiod_get_optional(dev, "mdio", GPIOD_OUT_LOW); - if (IS_ERR(priv->mdio)) - return ERR_CAST(priv->mdio); + if (IS_ERR(priv->mdio)) { + ret = PTR_ERR(priv->mdio); + goto err_variant_put; + } np = dev->of_node; - priv->leds_disabled = of_property_read_bool(np, "realtek,disable-leds"); /* TODO: if power is software controlled, set up any regulators here */ - priv->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); if (IS_ERR(priv->reset)) { + ret = PTR_ERR(priv->reset); dev_err(dev, "failed to get RESET GPIO\n"); - return ERR_CAST(priv->reset); + goto err_variant_put; } if (priv->reset) { gpiod_set_value(priv->reset, 1); @@ -97,13 +179,20 @@ struct realtek_priv *realtek_common_probe(struct device *dev, } priv->ds = devm_kzalloc(dev, sizeof(*priv->ds), GFP_KERNEL); - if (!priv->ds) - return ERR_PTR(-ENOMEM); + if (!priv->ds) { + ret = -ENOMEM; + goto err_variant_put; + } priv->ds->dev = dev; priv->ds->priv = priv; return priv; + +err_variant_put: + realtek_variant_put(var); + + return ERR_PTR(ret); } EXPORT_SYMBOL(realtek_common_probe); @@ -116,6 +205,8 @@ void realtek_common_remove(struct realtek_priv *priv) if (priv->user_mii_bus) of_node_put(priv->user_mii_bus->dev.of_node); + realtek_variant_put(priv->variant); + /* leave the device reset asserted */ if (priv->reset) gpiod_set_value(priv->reset, 1); @@ -124,10 +215,10 @@ EXPORT_SYMBOL(realtek_common_remove); const struct of_device_id realtek_common_of_match[] = { #if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8366RB) - { .compatible = "realtek,rtl8366rb", .data = &rtl8366rb_variant, }, + { .compatible = "realtek,rtl8366rb", .data = REALTEK_RTL8366RB_MODNAME, }, #endif #if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8365MB) - { .compatible = "realtek,rtl8365mb", .data = &rtl8366rb_variant, }, + { .compatible = "realtek,rtl8365mb", .data = REALTEK_RTL8365MB_MODNAME, }, #endif { /* sentinel */ }, }; diff --git a/drivers/net/dsa/realtek/realtek-common.h b/drivers/net/dsa/realtek/realtek-common.h index 90a949386518..089fda2d4fa9 100644 --- a/drivers/net/dsa/realtek/realtek-common.h +++ b/drivers/net/dsa/realtek/realtek-common.h @@ -12,5 +12,8 @@ void realtek_common_unlock(void *ctx); struct realtek_priv *realtek_common_probe(struct device *dev, struct regmap_config rc, struct regmap_config rc_nolock); void realtek_common_remove(struct realtek_priv *priv); +const struct realtek_variant *realtek_variant_get( + const struct of_device_id *match); +void realtek_variant_put(const struct realtek_variant *var); #endif diff --git a/drivers/net/dsa/realtek/realtek-mdio.c b/drivers/net/dsa/realtek/realtek-mdio.c index 6f610386c977..6d81cd88dbe6 100644 --- a/drivers/net/dsa/realtek/realtek-mdio.c +++ b/drivers/net/dsa/realtek/realtek-mdio.c @@ -146,7 +146,7 @@ static int realtek_mdio_probe(struct mdio_device *mdiodev) ret = priv->ops->detect(priv); if (ret) { dev_err(dev, "unable to detect switch\n"); - return ret; + goto err_variant_put; } priv->ds->num_ports = priv->num_ports; @@ -155,10 +155,15 @@ static int realtek_mdio_probe(struct mdio_device *mdiodev) if (ret) { dev_err_probe(dev, ret, "unable to register switch ret = %pe\n", ERR_PTR(ret)); - return ret; + goto err_variant_put; } return 0; + +err_variant_put: + realtek_variant_put(priv->variant); + + return ret; } static void realtek_mdio_remove(struct mdio_device *mdiodev) diff --git a/drivers/net/dsa/realtek/realtek-smi.c b/drivers/net/dsa/realtek/realtek-smi.c index 0cf89f9db99e..a772bb7dbe35 100644 --- a/drivers/net/dsa/realtek/realtek-smi.c +++ b/drivers/net/dsa/realtek/realtek-smi.c @@ -413,7 +413,7 @@ static int realtek_smi_probe(struct platform_device *pdev) ret = priv->ops->detect(priv); if (ret) { dev_err(dev, "unable to detect switch\n"); - return ret; + goto err_variant_put; } priv->ds->num_ports = priv->num_ports; @@ -422,10 +422,15 @@ static int realtek_smi_probe(struct platform_device *pdev) if (ret) { dev_err_probe(dev, ret, "unable to register switch ret = %pe\n", ERR_PTR(ret)); - return ret; + goto err_variant_put; } return 0; + +err_variant_put: + realtek_variant_put(priv->variant); + + return ret; } static void realtek_smi_remove(struct platform_device *pdev) diff --git a/drivers/net/dsa/realtek/realtek.h b/drivers/net/dsa/realtek/realtek.h index 8d9d546bf5f5..f9bd6678e3bd 100644 --- a/drivers/net/dsa/realtek/realtek.h +++ b/drivers/net/dsa/realtek/realtek.h @@ -16,6 +16,38 @@ #define REALTEK_HW_STOP_DELAY 25 /* msecs */ #define REALTEK_HW_START_DELAY 100 /* msecs */ +#define REALTEK_RTL8365MB_MODNAME "rtl8365mb" +#define REALTEK_RTL8366RB_MODNAME "rtl8366" + +struct realtek_variant_entry { + const struct realtek_variant *variant; + const char *compatible; + struct module *owner; + struct list_head list; +}; + +#define module_realtek_variant(__variant, __compatible) \ +static struct realtek_variant_entry __variant ## _entry = { \ + .compatible = __compatible, \ + .variant = &(__variant), \ + .owner = THIS_MODULE, \ +}; \ +static int __init realtek_variant_module_init(void) \ +{ \ + realtek_variant_register(&__variant ## _entry); \ + return 0; \ +} \ +module_init(realtek_variant_module_init) \ + \ +static void __exit realtek_variant_module_exit(void) \ +{ \ + realtek_variant_unregister(&__variant ## _entry); \ +} \ +module_exit(realtek_variant_module_exit) + +void realtek_variant_register(struct realtek_variant_entry *var_ent); +void realtek_variant_unregister(struct realtek_variant_entry *var_ent); + struct realtek_ops; struct dentry; struct inode; @@ -120,6 +152,7 @@ struct realtek_ops { struct realtek_variant { const struct dsa_switch_ops *ds_ops_smi; const struct dsa_switch_ops *ds_ops_mdio; + const struct realtek_variant_info *info; const struct realtek_ops *ops; unsigned int clk_delay; u8 cmd_read; @@ -146,7 +179,4 @@ void rtl8366_get_strings(struct dsa_switch *ds, int port, u32 stringset, int rtl8366_get_sset_count(struct dsa_switch *ds, int port, int sset); void rtl8366_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data); -extern const struct realtek_variant rtl8366rb_variant; -extern const struct realtek_variant rtl8365mb_variant; - #endif /* _REALTEK_H */ diff --git a/drivers/net/dsa/realtek/rtl8365mb.c b/drivers/net/dsa/realtek/rtl8365mb.c index 0875e4fc9f57..b5b22a4d01eb 100644 --- a/drivers/net/dsa/realtek/rtl8365mb.c +++ b/drivers/net/dsa/realtek/rtl8365mb.c @@ -2163,7 +2163,7 @@ static const struct realtek_ops rtl8365mb_ops = { .phy_write = rtl8365mb_phy_write, }; -const struct realtek_variant rtl8365mb_variant = { +static const struct realtek_variant rtl8365mb_variant = { .ds_ops_smi = &rtl8365mb_switch_ops_smi, .ds_ops_mdio = &rtl8365mb_switch_ops_mdio, .ops = &rtl8365mb_ops, @@ -2172,7 +2172,7 @@ const struct realtek_variant rtl8365mb_variant = { .cmd_write = 0xb8, .chip_data_sz = sizeof(struct rtl8365mb), }; -EXPORT_SYMBOL_GPL(rtl8365mb_variant); +module_realtek_variant(rtl8365mb_variant, "realtek,rtl8365mb"); MODULE_AUTHOR("Alvin Šipraga "); MODULE_DESCRIPTION("Driver for RTL8365MB-VC ethernet switch"); diff --git a/drivers/net/dsa/realtek/rtl8366rb.c b/drivers/net/dsa/realtek/rtl8366rb.c index b39b719a5b8f..208a8f17a089 100644 --- a/drivers/net/dsa/realtek/rtl8366rb.c +++ b/drivers/net/dsa/realtek/rtl8366rb.c @@ -1911,7 +1911,7 @@ static const struct realtek_ops rtl8366rb_ops = { .phy_write = rtl8366rb_phy_write, }; -const struct realtek_variant rtl8366rb_variant = { +static const struct realtek_variant rtl8366rb_variant = { .ds_ops_smi = &rtl8366rb_switch_ops_smi, .ds_ops_mdio = &rtl8366rb_switch_ops_mdio, .ops = &rtl8366rb_ops, @@ -1920,7 +1920,7 @@ const struct realtek_variant rtl8366rb_variant = { .cmd_write = 0xa8, .chip_data_sz = sizeof(struct rtl8366rb), }; -EXPORT_SYMBOL_GPL(rtl8366rb_variant); +module_realtek_variant(rtl8366rb_variant, "realtek,rtl8366rb"); MODULE_AUTHOR("Linus Walleij "); MODULE_DESCRIPTION("Driver for RTL8366RB ethernet switch"); From patchwork Sat Nov 11 21:51:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Angelo Daros de Luca X-Patchwork-Id: 13453194 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 2F6E91DFCC for ; Sat, 11 Nov 2023 21:57:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="A0qXo6Av" Received: from mail-oa1-x29.google.com (mail-oa1-x29.google.com [IPv6:2001:4860:4864:20::29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1905D2737 for ; Sat, 11 Nov 2023 13:57:30 -0800 (PST) Received: by mail-oa1-x29.google.com with SMTP id 586e51a60fabf-1f084cb8b54so1958243fac.1 for ; Sat, 11 Nov 2023 13:57:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699739847; x=1700344647; 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=FRjCguQAxTRUszaUSoLu66y6AGiagR4ChKN9bUhcBHA=; b=A0qXo6AvjNdZFyOdF0decwPmiAtsL9lFPS2fpOpwXh3jXxobnItNoXItmdbmnKg6v8 0psUmioi4X8ivnVcvKyaEJ/t2z2POfivKLzmWMspCzbAUxSgd3RATHiIjphAkLYfvr8f k3C/2eGlVs1ZHchBFpN2MyqSogZ9HZKdVeU5Heu4jmqmP546ngCAzcunBd9o3xruG6jK LAJxyh60/JlucCAnun1T3O4X9lIyD4D5FJoRPiJ0Y6Grx+Fi3p0K1aELyHD1imLt76Zt mXPrpdVnu/ntqstt30toGQw1Q+ZVhZ7Cje5sYtFvObGZDEsKILS4P57mOF29VBwQhDji SQhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699739847; x=1700344647; 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=FRjCguQAxTRUszaUSoLu66y6AGiagR4ChKN9bUhcBHA=; b=bnWPQZx7l77D+ckuIrt8fuY7Q3CAlGN+NyrRHy0ObhfwnPskcOdJNrNAhGvTfjAJci 4rI7kV5XuxQklRSJKGeEAKgffvS/LhH1sk3lrih7GItQTiJ1o49unliXTB5tJN1X1BEL e66x0xLwvaWq0KAuzxca6cUbRnAnuIYC9A6xYyDkgZRyWicFShAmU4Vyi7+my/1bJgbv oShCh1s2m24ka7/uurp60F1lN3FEONc6bhohOukmBHpnBJm0A9HisrU+QuobgKuWjcI7 5gtOu+iPxuXZB4i3x4PUG9wJV7semLCa5Foe37y6F8XEDA79mmiCpDbnpoQUSBE1Su4X BHBg== X-Gm-Message-State: AOJu0YzejVHyX3575ua/HnDP+1b8EXhE375rRc2/igoSqFxxgyqvdbla P4Hlg0kzb3C5+hONMipez1RqZs1yPCdd+w== X-Google-Smtp-Source: AGHT+IGoe11qaQ8DN/FV0G43cfoq1hxrrxCWElchiV8BSrMUwjkYz1kvqI1sTUT3duIGh0cn1CHiaw== X-Received: by 2002:a05:6871:4087:b0:1ea:ce71:dd12 with SMTP id kz7-20020a056871408700b001eace71dd12mr4359904oab.3.1699739847594; Sat, 11 Nov 2023 13:57:27 -0800 (PST) Received: from tresc054937.tre-sc.gov.br (177-131-126-82.acessoline.net.br. [177.131.126.82]) by smtp.gmail.com with ESMTPSA id 25-20020a17090a199900b002801184885dsm1867210pji.4.2023.11.11.13.57.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Nov 2023 13:57:27 -0800 (PST) From: Luiz Angelo Daros de Luca To: netdev@vger.kernel.org Cc: linus.walleij@linaro.org, alsi@bang-olufsen.dk, andrew@lunn.ch, vivien.didelot@gmail.com, f.fainelli@gmail.com, olteanv@gmail.com, davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com, robh+dt@kernel.org, krzk+dt@kernel.org, arinc.unal@arinc9.com, Luiz Angelo Daros de Luca Subject: [RFC net-next 5/5] net: dsa: realtek: support reset controller Date: Sat, 11 Nov 2023 18:51:08 -0300 Message-ID: <20231111215647.4966-6-luizluca@gmail.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231111215647.4966-1-luizluca@gmail.com> References: <20231111215647.4966-1-luizluca@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC The 'reset-gpios' will not work when the switch reset is controlled by a reset controller. Although the reset is optional and the driver performs a soft reset during setup, if the initial reset state was asserted, the driver will not detect it. The reset controller will take precedence over the reset GPIO. Signed-off-by: Luiz Angelo Daros de Luca --- drivers/net/dsa/realtek/realtek-common.c | 53 +++++++++++++++++++++--- drivers/net/dsa/realtek/realtek-common.h | 3 ++ drivers/net/dsa/realtek/realtek.h | 2 + 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/drivers/net/dsa/realtek/realtek-common.c b/drivers/net/dsa/realtek/realtek-common.c index e383db21c776..1450e5c206c3 100644 --- a/drivers/net/dsa/realtek/realtek-common.c +++ b/drivers/net/dsa/realtek/realtek-common.c @@ -163,17 +163,25 @@ struct realtek_priv *realtek_common_probe(struct device *dev, priv->leds_disabled = of_property_read_bool(np, "realtek,disable-leds"); /* TODO: if power is software controlled, set up any regulators here */ + priv->reset_ctl = devm_reset_control_get_optional(dev, NULL); + if (IS_ERR(priv->reset_ctl)) { + ret = PTR_ERR(priv->reset_ctl); + dev_err_probe(dev, ret, "failed to get reset control\n"); + goto err_variant_put; + } + priv->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); if (IS_ERR(priv->reset)) { ret = PTR_ERR(priv->reset); dev_err(dev, "failed to get RESET GPIO\n"); goto err_variant_put; } - if (priv->reset) { - gpiod_set_value(priv->reset, 1); + + if (priv->reset_ctl || priv->reset) { + realtek_reset_assert(priv); dev_dbg(dev, "asserted RESET\n"); msleep(REALTEK_HW_STOP_DELAY); - gpiod_set_value(priv->reset, 0); + realtek_reset_deassert(priv); msleep(REALTEK_HW_START_DELAY); dev_dbg(dev, "deasserted RESET\n"); } @@ -208,11 +216,46 @@ void realtek_common_remove(struct realtek_priv *priv) realtek_variant_put(priv->variant); /* leave the device reset asserted */ - if (priv->reset) - gpiod_set_value(priv->reset, 1); + realtek_reset_assert(priv); } EXPORT_SYMBOL(realtek_common_remove); +void realtek_reset_assert(struct realtek_priv *priv) +{ + int ret; + + if (priv->reset_ctl) { + ret = reset_control_assert(priv->reset_ctl); + if (!ret) + return; + + dev_warn(priv->dev, + "Failed to assert the switch reset control: %pe\n", + ERR_PTR(ret)); + } + + if (priv->reset) + gpiod_set_value(priv->reset, true); +} + +void realtek_reset_deassert(struct realtek_priv *priv) +{ + int ret; + + if (priv->reset_ctl) { + ret = reset_control_deassert(priv->reset_ctl); + if (!ret) + return; + + dev_warn(priv->dev, + "Failed to deassert the switch reset control: %pe\n", + ERR_PTR(ret)); + } + + if (priv->reset) + gpiod_set_value(priv->reset, false); +} + const struct of_device_id realtek_common_of_match[] = { #if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8366RB) { .compatible = "realtek,rtl8366rb", .data = REALTEK_RTL8366RB_MODNAME, }, diff --git a/drivers/net/dsa/realtek/realtek-common.h b/drivers/net/dsa/realtek/realtek-common.h index 089fda2d4fa9..603b4f9891d3 100644 --- a/drivers/net/dsa/realtek/realtek-common.h +++ b/drivers/net/dsa/realtek/realtek-common.h @@ -16,4 +16,7 @@ const struct realtek_variant *realtek_variant_get( const struct of_device_id *match); void realtek_variant_put(const struct realtek_variant *var); +void realtek_reset_assert(struct realtek_priv *priv); +void realtek_reset_deassert(struct realtek_priv *priv); + #endif diff --git a/drivers/net/dsa/realtek/realtek.h b/drivers/net/dsa/realtek/realtek.h index f9bd6678e3bd..86f33327155b 100644 --- a/drivers/net/dsa/realtek/realtek.h +++ b/drivers/net/dsa/realtek/realtek.h @@ -12,6 +12,7 @@ #include #include #include +#include #define REALTEK_HW_STOP_DELAY 25 /* msecs */ #define REALTEK_HW_START_DELAY 100 /* msecs */ @@ -80,6 +81,7 @@ struct rtl8366_vlan_4k { struct realtek_priv { struct device *dev; + struct reset_control *reset_ctl; struct gpio_desc *reset; struct gpio_desc *mdc; struct gpio_desc *mdio;