Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp186136rwi; Wed, 12 Oct 2022 18:19:54 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6fM6eNvg2uhOxk4PqDh7icdUi1u0/2lUB6mLceydKk/mDNQDnN6ed/BJQk9xkVD0ajNC4j X-Received: by 2002:a17:907:d89:b0:78d:b143:7aeb with SMTP id go9-20020a1709070d8900b0078db1437aebmr15577044ejc.459.1665623994405; Wed, 12 Oct 2022 18:19:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665623994; cv=none; d=google.com; s=arc-20160816; b=c3puXde9l3eO9qm93vlr3RYMCCVzV9d7R+hsnSDUFAIRVUYo2IYfWAhCM+6Y7mRldB mIO+OhoLSdL3o+sfGVWbmdla2B0F+JZh7I5DiFSJsv1nNfCSwdSxdTGKwt2uvd/ET7o3 KMgDX/faVdl0PI6BHtKuXq8FNuf169ld9CHafZ7W1iDY6ky7FEAwBrwAoxcBNvWSoaUQ lrKY6uR9yQzUKPWYKa2zHGz+qvorgCdhR+wn1vBjS+I6n/JAl9qeiA5kaNQZ9xGJWRzT AIoLcni++i4gJMJOtu80Qb6HE11VyCfucVspjoaG9sPi5Lx2b5yrLPjuEq8S6N9LUCpX 1UIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=OHf8+2BFdiKFlOQyqLOkxZuYfOyvt9b5kSYkCKqd0+w=; b=YPGqTNPx5+4xiGmKxiCwqJyJ83EcBqX1B4J+hTjG6caeQvkQRtyCqdRVhML3AP9UZZ Uw+9nIIkbaRE6r4b1WLvmTrIUZ9i7dT05DbB2+JxjeeXNkMTcw+KTcsYEyerzif/QdNL rxt4J4BqQ3ZNKW3UuG/v/jvXp0DN0qhEJkZYybxTQDZYW4S90FF6YK2J6qfSqZxtlR3S 6iFiXeTlOgWdymsVWS+Y/PzhHIztIaifRfXk61Bqgb3nCDatgzor/Bg9SuncX/6ZMuHa OO7KElOZ4KnMeDy24OZCJZhA9H2b7gIoB/Bfj/j2AR4g8Cgs7x6KIpAmO/jVz8VUYKio l88g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KZ1Yx1++; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id du19-20020a17090772d300b0078d4e67ca81si20083055ejc.605.2022.10.12.18.19.28; Wed, 12 Oct 2022 18:19:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KZ1Yx1++; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230483AbiJMBJZ (ORCPT + 99 others); Wed, 12 Oct 2022 21:09:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232513AbiJMBIv (ORCPT ); Wed, 12 Oct 2022 21:08:51 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54547EE88A; Wed, 12 Oct 2022 18:06:59 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 37CE8B81CE1; Thu, 13 Oct 2022 00:22:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 22C46C433C1; Thu, 13 Oct 2022 00:22:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1665620539; bh=JGLTKbsD/sdAt4qMN1vPrUfBqXQvtOwEO0dsNfJFF40=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KZ1Yx1++3iVsZyrfow5NMvWL2nNYEcqwIWQvQvBen1P03QT+y6pd4mibcR34yTQ0M 1f4FDeQBUzRr29CM/M5OodF2jUpjw0eDM9+esPDYWOu8iVH/M03vmfj4puIEsvxWhN b1ZnBGVaHoJkv4Np2TsZxTnPZrEhFITSHbCCgSTgR0bKv9nQroQpqRSaljDmwJJrpR fmFFVD1MzT9LdJyX+DYWKCq+uDt1YvoYLghko833wNWiiOz37va5NoREdlbhyEU2cA JNuIfB1r3E6AGetXYY9jn3FokrriSENFFmy0/QhqdyhctfFz1PB9hJpCzcA2QgXg1d iaZpJawOhF1og== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Dmitry Torokhov , Linus Walleij , Bartosz Golaszewski , Sasha Levin , linux-gpio@vger.kernel.org Subject: [PATCH AUTOSEL 5.15 18/47] gpiolib: rework quirk handling in of_find_gpio() Date: Wed, 12 Oct 2022 20:20:53 -0400 Message-Id: <20221013002124.1894077-18-sashal@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221013002124.1894077-1-sashal@kernel.org> References: <20221013002124.1894077-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dmitry Torokhov [ Upstream commit a2b5e207cade33b4d2dfd920f783f13b1f173e78 ] Instead of having a string of "if" statements let's put all quirks into an array and iterate over them. Signed-off-by: Dmitry Torokhov Reviewed-by: Linus Walleij Signed-off-by: Bartosz Golaszewski Signed-off-by: Sasha Levin --- drivers/gpio/gpiolib-of.c | 62 ++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c index fe34061d918b..77fa14776078 100644 --- a/drivers/gpio/gpiolib-of.c +++ b/drivers/gpio/gpiolib-of.c @@ -369,14 +369,12 @@ EXPORT_SYMBOL_GPL(gpiod_get_from_of_node); * properties should be named "foo-gpios" so we have this special kludge for * them. */ -static struct gpio_desc *of_find_spi_gpio(struct device *dev, +static struct gpio_desc *of_find_spi_gpio(struct device_node *np, const char *con_id, unsigned int idx, enum of_gpio_flags *of_flags) { char prop_name[32]; /* 32 is max size of property name */ - const struct device_node *np = dev->of_node; - struct gpio_desc *desc; /* * Hopefully the compiler stubs the rest of the function if this @@ -392,8 +390,7 @@ static struct gpio_desc *of_find_spi_gpio(struct device *dev, /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */ snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id); - desc = of_get_named_gpiod_flags(np, prop_name, idx, of_flags); - return desc; + return of_get_named_gpiod_flags(np, prop_name, idx, of_flags); } /* @@ -401,13 +398,11 @@ static struct gpio_desc *of_find_spi_gpio(struct device *dev, * lines rather than "cs-gpios" like all other SPI hardware. Account for this * with a special quirk. */ -static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev, +static struct gpio_desc *of_find_spi_cs_gpio(struct device_node *np, const char *con_id, unsigned int idx, enum of_gpio_flags *of_flags) { - const struct device_node *np = dev->of_node; - if (!IS_ENABLED(CONFIG_SPI_MASTER)) return ERR_PTR(-ENOENT); @@ -425,7 +420,7 @@ static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev, * uses just "gpios" so translate to that when "cs-gpios" is * requested. */ - return of_get_named_gpiod_flags(dev->of_node, "gpios", idx, of_flags); + return of_get_named_gpiod_flags(np, "gpios", idx, of_flags); } /* @@ -433,7 +428,7 @@ static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev, * properties should be named "foo-gpios" so we have this special kludge for * them. */ -static struct gpio_desc *of_find_regulator_gpio(struct device *dev, +static struct gpio_desc *of_find_regulator_gpio(struct device_node *np, const char *con_id, unsigned int idx, enum of_gpio_flags *of_flags) @@ -444,8 +439,6 @@ static struct gpio_desc *of_find_regulator_gpio(struct device *dev, "wlf,ldo1ena", /* WM8994 */ "wlf,ldo2ena", /* WM8994 */ }; - const struct device_node *np = dev->of_node; - struct gpio_desc *desc; int i; if (!IS_ENABLED(CONFIG_REGULATOR)) @@ -458,11 +451,10 @@ static struct gpio_desc *of_find_regulator_gpio(struct device *dev, if (i < 0) return ERR_PTR(-ENOENT); - desc = of_get_named_gpiod_flags(np, con_id, idx, of_flags); - return desc; + return of_get_named_gpiod_flags(np, con_id, idx, of_flags); } -static struct gpio_desc *of_find_arizona_gpio(struct device *dev, +static struct gpio_desc *of_find_arizona_gpio(struct device_node *np, const char *con_id, unsigned int idx, enum of_gpio_flags *of_flags) @@ -473,10 +465,10 @@ static struct gpio_desc *of_find_arizona_gpio(struct device *dev, if (!con_id || strcmp(con_id, "wlf,reset")) return ERR_PTR(-ENOENT); - return of_get_named_gpiod_flags(dev->of_node, con_id, idx, of_flags); + return of_get_named_gpiod_flags(np, con_id, idx, of_flags); } -static struct gpio_desc *of_find_usb_gpio(struct device *dev, +static struct gpio_desc *of_find_usb_gpio(struct device_node *np, const char *con_id, unsigned int idx, enum of_gpio_flags *of_flags) @@ -492,14 +484,27 @@ static struct gpio_desc *of_find_usb_gpio(struct device *dev, if (!con_id || strcmp(con_id, "fcs,int_n")) return ERR_PTR(-ENOENT); - return of_get_named_gpiod_flags(dev->of_node, con_id, idx, of_flags); + return of_get_named_gpiod_flags(np, con_id, idx, of_flags); } +typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np, + const char *con_id, + unsigned int idx, + enum of_gpio_flags *of_flags); +static const of_find_gpio_quirk of_find_gpio_quirks[] = { + of_find_spi_gpio, + of_find_spi_cs_gpio, + of_find_regulator_gpio, + of_find_arizona_gpio, + of_find_usb_gpio, +}; + struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, unsigned int idx, unsigned long *flags) { char prop_name[32]; /* 32 is max size of property name */ enum of_gpio_flags of_flags; + const of_find_gpio_quirk *q; struct gpio_desc *desc; unsigned int i; @@ -519,24 +524,9 @@ struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, break; } - if (gpiod_not_found(desc)) { - /* Special handling for SPI GPIOs if used */ - desc = of_find_spi_gpio(dev, con_id, idx, &of_flags); - } - - if (gpiod_not_found(desc)) - desc = of_find_spi_cs_gpio(dev, con_id, idx, &of_flags); - - if (gpiod_not_found(desc)) { - /* Special handling for regulator GPIOs if used */ - desc = of_find_regulator_gpio(dev, con_id, idx, &of_flags); - } - - if (gpiod_not_found(desc)) - desc = of_find_arizona_gpio(dev, con_id, idx, &of_flags); - - if (gpiod_not_found(desc)) - desc = of_find_usb_gpio(dev, con_id, idx, &of_flags); + /* Properly named GPIO was not found, try workarounds */ + for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++) + desc = (*q)(dev->of_node, con_id, idx, &of_flags); if (IS_ERR(desc)) return desc; -- 2.35.1