Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 16835C001DC for ; Thu, 27 Jul 2023 15:50:48 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 456D7867F7; Thu, 27 Jul 2023 17:50:45 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1690473046; bh=JM3SV62es0sHhxY8fVmqYy7NzOhUjEcdA5eePfLCqQM=; h=From:To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From; b=ideZYDMOUeUyux/d0tMyLWrmZYU7UG45P9fj7nldxb3vtm0lXLnPB6vImwhJzMET0 a7AuyNLsil7WvMhjLd7bAR8JrIVeLVHO6oOBIXQJifmTudpU1pqq9YI7en++6hzsbW 9XBZY+Yt3N7EX0hLKjDlaJU5jZL5ZcKO+DovfAKEVEjvwc/WVFhf7m220Gswp/p0YI RvdRwKkO8BoAla9jl573ZS+V9ck56w0q1QUILE646sPRpNeuq58fGLJUA4D7fDDJmC hsqEnRAbGjwgV/vhX0Q8xe4lQSY2ZoCXm0afAl5PmZpf4cUpj3/Mzyf7vg/UZZBPJI Uj5OAnhSsHwbA== Received: from tr.lan (ip-86-49-120-218.bb.vodafone.cz [86.49.120.218]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: marex@denx.de) by phobos.denx.de (Postfix) with ESMTPSA id CD34E867F7; Thu, 27 Jul 2023 17:50:37 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1690473038; bh=JM3SV62es0sHhxY8fVmqYy7NzOhUjEcdA5eePfLCqQM=; h=From:To:Cc:Subject:Date:From; b=NgGMgChdEh+55Q5vlVy79QA9TDhRerw1KTwVhUCjmHZtmz3E65FGxgcgUspM48x+v VYCwvUFyDF+X+fdzOGETiJAA3H6Y8tkHrBfUEmU+dzMa5CrSwbM90JMP10iSuzR7Kh nc4d5y48WEzBy17vzNRWxD/i6S166SObRd8exJGe/SvAHlYo4o1mUju/oNBscm5u3B JMUxwTdHjlFvH0Qwzy6KlNKl3m8J+K4l8OFeE3zkGtbF0Ilg7xi+BWcT6OTb/Ve+oX xjMfbuVSwqxSYtleNm3602UTfxyyEy+hGWBUpZ3bZJanC1RjqjP1mmdKS73V3ysTsI KM8wlExW86Ezw== From: Marek Vasut To: u-boot@lists.denx.de Cc: Marek Vasut , Michal Suchanek , Patrice Chotard , Patrick Delaunay , Rasmus Villemoes , Samuel Holland , Simon Glass Subject: [PATCH] gpio: Use separate bitfield array to indicate GPIO is claimed Date: Thu, 27 Jul 2023 17:50:25 +0200 Message-Id: <20230727155025.82680-1-marex@denx.de> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean The current gpio-uclass design uses name field in struct gpio_dev_priv as an indicator that GPIO is claimed by consumer. This overloads the function of name field and does not work well for named pins not configured as GPIO pins. Introduce separate bitfield array as the claim indicator. This unbreaks dual-purpose AF and GPIO operation on STM32MP since commit 2c38f7c31806 ("pinctrl: pinctrl_stm32: Populate uc_priv->name[] with pinmux node's name") where any pin which has already been configured as AF could no longer be claimed as dual-purpose GPIO. This is important for pins like STM32 MMCI st,cmd-gpios . Signed-off-by: Marek Vasut --- Cc: Michal Suchanek Cc: Patrice Chotard Cc: Patrick Delaunay Cc: Rasmus Villemoes Cc: Samuel Holland Cc: Simon Glass --- drivers/gpio/gpio-uclass.c | 35 ++++++++++++++++++++++++++++++----- include/asm-generic/gpio.h | 2 ++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/drivers/gpio/gpio-uclass.c b/drivers/gpio/gpio-uclass.c index 712119c3415..25873f07bd4 100644 --- a/drivers/gpio/gpio-uclass.c +++ b/drivers/gpio/gpio-uclass.c @@ -75,6 +75,20 @@ static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc) return -ENOENT; } +/** + * gpio_is_claimed() - Test whether GPIO is claimed by consumer + * + * Test whether GPIO is claimed by consumer already. + * + * @uc_priv: gpio_dev_priv pointer. + * @offset: gpio offset within the device + * @return: true if claimed, false if not claimed + */ +static bool gpio_is_claimed(struct gpio_dev_priv *uc_priv, unsigned int offset) +{ + return !!(uc_priv->claimed[offset / 32] & BIT(offset % 32)); +} + #if CONFIG_IS_ENABLED(DM_GPIO_LOOKUP_LABEL) /** * dm_gpio_lookup_label() - look for name in gpio device @@ -94,7 +108,7 @@ static int dm_gpio_lookup_label(const char *name, *offset = -1; for (i = 0; i < uc_priv->gpio_count; i++) { - if (!uc_priv->name[i]) + if (!gpio_is_claimed(uc_priv, i)) continue; if (!strcmp(name, uc_priv->name[i])) { *offset = i; @@ -350,7 +364,7 @@ int dm_gpio_request(struct gpio_desc *desc, const char *label) int ret; uc_priv = dev_get_uclass_priv(dev); - if (uc_priv->name[desc->offset]) + if (gpio_is_claimed(uc_priv, desc->offset)) return -EBUSY; str = strdup(label); if (!str) @@ -362,6 +376,8 @@ int dm_gpio_request(struct gpio_desc *desc, const char *label) return ret; } } + + uc_priv->claimed[desc->offset / 32] |= BIT(desc->offset % 32); uc_priv->name[desc->offset] = str; return 0; @@ -438,7 +454,7 @@ int _dm_gpio_free(struct udevice *dev, uint offset) int ret; uc_priv = dev_get_uclass_priv(dev); - if (!uc_priv->name[offset]) + if (!gpio_is_claimed(uc_priv, offset)) return -ENXIO; if (ops->rfree) { ret = ops->rfree(dev, offset); @@ -446,6 +462,7 @@ int _dm_gpio_free(struct udevice *dev, uint offset) return ret; } + uc_priv->claimed[offset / 32] &= ~BIT(offset % 32); free(uc_priv->name[offset]); uc_priv->name[offset] = NULL; @@ -480,7 +497,7 @@ static int check_reserved(const struct gpio_desc *desc, const char *func) return -ENOENT; uc_priv = dev_get_uclass_priv(desc->dev); - if (!uc_priv->name[desc->offset]) { + if (!gpio_is_claimed(uc_priv, desc->offset)) { printf("%s: %s: error: gpio %s%d not reserved\n", desc->dev->name, func, uc_priv->bank_name ? uc_priv->bank_name : "", @@ -826,7 +843,7 @@ static int get_function(struct udevice *dev, int offset, bool skip_unused, return -EINVAL; if (namep) *namep = uc_priv->name[offset]; - if (skip_unused && !uc_priv->name[offset]) + if (skip_unused && !gpio_is_claimed(uc_priv, offset)) return GPIOF_UNUSED; if (ops->get_function) { int ret; @@ -1341,6 +1358,13 @@ static int gpio_post_probe(struct udevice *dev) if (!uc_priv->name) return -ENOMEM; + uc_priv->claimed = calloc(DIV_ROUND_UP(uc_priv->gpio_count, 32), + sizeof(*uc_priv->claimed)); + if (!uc_priv->claimed) { + free(uc_priv->name); + return -ENOMEM; + } + return gpio_renumber(NULL); } @@ -1353,6 +1377,7 @@ static int gpio_pre_remove(struct udevice *dev) if (uc_priv->name[i]) free(uc_priv->name[i]); } + free(uc_priv->claimed); free(uc_priv->name); return gpio_renumber(dev); diff --git a/include/asm-generic/gpio.h b/include/asm-generic/gpio.h index c4a7fd28439..a21c606f2b8 100644 --- a/include/asm-generic/gpio.h +++ b/include/asm-generic/gpio.h @@ -414,6 +414,7 @@ struct dm_gpio_ops { * @gpio_base: Base GPIO number for this device. For the first active device * this will be 0; the numbering for others will follow sequentially so that * @gpio_base for device 1 will equal the number of GPIOs in device 0. + * @claimed: Array of bits indicating which GPIOs in the bank are claimed. * @name: Array of pointers to the name for each GPIO in this bank. The * value of the pointer will be NULL if the GPIO has not been claimed. */ @@ -421,6 +422,7 @@ struct gpio_dev_priv { const char *bank_name; unsigned gpio_count; unsigned gpio_base; + u32 *claimed; char **name; }; -- 2.40.1