Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp4874914ybb; Tue, 24 Mar 2020 06:57:58 -0700 (PDT) X-Google-Smtp-Source: ADFU+vsX7C6+46582vG2iHW5xVB0YMe9oTls8l3PzqCaOQPYPkzodz7xdn+ICFEqss9imwXIxWhL X-Received: by 2002:a9d:7c94:: with SMTP id q20mr10328797otn.150.1585058278263; Tue, 24 Mar 2020 06:57:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585058278; cv=none; d=google.com; s=arc-20160816; b=ChD852HoiRDubxxIr/DiI0R71u7W2g+xiWZNmOeVSR5yfNQCG6mRMfoSe9RLDW3Z7V ZSBCMfB0YWF1C5f7FLTNG6dUIvq1n4ygQKQGpdsmZC4EgF4fMrnlV4CBxBoPGg+X7H2h ncS+ksTv0JueOs/LjV/XAwLbMCBwJPiq5wQz2N/F1OHtwsInt968jKmFhQb3LIrIdLof UIHTbmHdI2sWAVtBA1xT7vb3yux4369K/BHNOroX5GDle80GtfJMxw4YJ0hGxC5hx7Ye 7iC5Xl89tyLlwYJ5aQTJO6/8vPzVENCRQlYdIiB1Szbiu1mmUtanlFjCoQkl3QmGSCq8 EgcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=FoBW/SEb4jS0r/RLuOVU/AtkbasvsWEAI8QoU2XZa6s=; b=bND/uR1irTTGbAiFieXTvxiBxo9YNflVue4YTPsb4xYGiYzqzRDyelX0UGBSzcManW im/2Kplp7iPbCOwPXTYRrQIL0FUDYVI/G2FPn/sWHqKFsymO6GfhWgJvO/fNI3l1KWRH 9tpcVdq8cQDWOcWrzWRjaJMM1cuzdbyUaIFJ+H7FRzLb64O0sAnJIHXyJpX86DTjoW/U ze03/I2ZPdpQgyumMbwr+Gy36LPaeDGFpckztYReBThin9G3IpUdxzGjij40nyG2WGoQ km2PT1u/tq7RZNhjVwa6BBZ4EK+EihJjF1F8SpIufZ8mAdTrzDHsjr3LIbdVu0BWjO3W jjRg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j84si9257039oia.126.2020.03.24.06.57.45; Tue, 24 Mar 2020 06:57:58 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728194AbgCXN5W (ORCPT + 99 others); Tue, 24 Mar 2020 09:57:22 -0400 Received: from laurent.telenet-ops.be ([195.130.137.89]:36976 "EHLO laurent.telenet-ops.be" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727607AbgCXN5J (ORCPT ); Tue, 24 Mar 2020 09:57:09 -0400 Received: from ramsan ([84.195.182.253]) by laurent.telenet-ops.be with bizsmtp id JDwu2200H5USYZQ01Dwulu; Tue, 24 Mar 2020 14:57:05 +0100 Received: from rox.of.borg ([192.168.97.57]) by ramsan with esmtp (Exim 4.90_1) (envelope-from ) id 1jGk34-0006Ol-KV; Tue, 24 Mar 2020 14:56:54 +0100 Received: from geert by rox.of.borg with local (Exim 4.90_1) (envelope-from ) id 1jGk34-0001kf-Ih; Tue, 24 Mar 2020 14:56:54 +0100 From: Geert Uytterhoeven To: Linus Walleij , Bartosz Golaszewski , Jonathan Corbet , Harish Jenny K N , Eugeniu Rosca Cc: Alexander Graf , Peter Maydell , Paolo Bonzini , Phil Reid , Marc Zyngier , Christoffer Dall , Magnus Damm , Rob Herring , Mark Rutland , linux-gpio@vger.kernel.org, linux-doc@vger.kernel.org, linux-renesas-soc@vger.kernel.org, linux-kernel@vger.kernel.org, qemu-devel@nongnu.org, Geert Uytterhoeven Subject: [PATCH v6 4/8] gpiolib: Add support for GPIO lookup by line name Date: Tue, 24 Mar 2020 14:56:49 +0100 Message-Id: <20200324135653.6676-4-geert+renesas@glider.be> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200324135653.6676-1-geert+renesas@glider.be> References: <20200324135328.5796-1-geert+renesas@glider.be> <20200324135653.6676-1-geert+renesas@glider.be> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently a GPIO lookup table can only refer to a specific GPIO by a tuple, consisting of a GPIO controller label and a GPIO offset inside the controller. However, a GPIO may also carry a line name, defined by DT or ACPI. If present, the line name is the most use-centric way to refer to a GPIO. Hence add support for looking up GPIOs by line name. Implement this by reusing the existing gpiod_lookup infrastructure. Rename gpiod_lookup.chip_label to gpiod_lookup.key, to make it clear that this field can have two meanings, and update the kerneldoc and GPIO_LOOKUP*() macros. Signed-off-by: Geert Uytterhoeven Reviewed-by: Ulrich Hecht Reviewed-by: Eugeniu Rosca Tested-by: Eugeniu Rosca --- v6: - Update Documentation/driver-api/gpio/board.rst, - Reword rationale, v5: - Add Reviewed-by, Tested-by, v4: - Add Reviewed-by, - Rename gpiod_lookup.chip_label. - Use U16_MAX instead of (u16)-1, v3: - New. --- Documentation/driver-api/gpio/board.rst | 10 ++++++---- drivers/gpio/gpiolib.c | 22 +++++++++++++++++----- include/linux/gpio/machine.h | 15 ++++++++------- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/Documentation/driver-api/gpio/board.rst b/Documentation/driver-api/gpio/board.rst index ce91518bf9f48ded..0ad1f8cacf5e5d26 100644 --- a/Documentation/driver-api/gpio/board.rst +++ b/Documentation/driver-api/gpio/board.rst @@ -113,13 +113,15 @@ files that desire to do so need to include the following header:: GPIOs are mapped by the means of tables of lookups, containing instances of the gpiod_lookup structure. Two macros are defined to help declaring such mappings:: - GPIO_LOOKUP(chip_label, chip_hwnum, con_id, flags) - GPIO_LOOKUP_IDX(chip_label, chip_hwnum, con_id, idx, flags) + GPIO_LOOKUP(key, chip_hwnum, con_id, flags) + GPIO_LOOKUP_IDX(key, chip_hwnum, con_id, idx, flags) where - - chip_label is the label of the gpiod_chip instance providing the GPIO - - chip_hwnum is the hardware number of the GPIO within the chip + - key is either the label of the gpiod_chip instance providing the GPIO, or + the GPIO line name + - chip_hwnum is the hardware number of the GPIO within the chip, or U16_MAX + to indicate that key is a GPIO line name - con_id is the name of the GPIO function from the device point of view. It can be NULL, in which case it will match any function. - idx is the index of the GPIO within the function. diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 8d7366f4451fe695..c756602e249c052e 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -4643,7 +4643,7 @@ static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, if (!table) return desc; - for (p = &table->table[0]; p->chip_label; p++) { + for (p = &table->table[0]; p->key; p++) { struct gpio_chip *chip; /* idx must always match exactly */ @@ -4654,18 +4654,30 @@ static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) continue; - chip = find_chip_by_name(p->chip_label); + if (p->chip_hwnum == U16_MAX) { + desc = gpio_name_to_desc(p->key); + if (desc) { + *flags = p->flags; + return desc; + } + + dev_warn(dev, "cannot find GPIO line %s, deferring\n", + p->key); + return ERR_PTR(-EPROBE_DEFER); + } + + chip = find_chip_by_name(p->key); if (!chip) { /* * As the lookup table indicates a chip with - * p->chip_label should exist, assume it may + * p->key should exist, assume it may * still appear later and let the interested * consumer be probed again or let the Deferred * Probe infrastructure handle the error. */ dev_warn(dev, "cannot find GPIO chip %s, deferring\n", - p->chip_label); + p->key); return ERR_PTR(-EPROBE_DEFER); } @@ -4696,7 +4708,7 @@ static int platform_gpio_count(struct device *dev, const char *con_id) if (!table) return -ENOENT; - for (p = &table->table[0]; p->chip_label; p++) { + for (p = &table->table[0]; p->key; p++) { if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || (!con_id && !p->con_id)) count++; diff --git a/include/linux/gpio/machine.h b/include/linux/gpio/machine.h index 1ebe5be05d5f81fa..84c66fbf54fd5811 100644 --- a/include/linux/gpio/machine.h +++ b/include/linux/gpio/machine.h @@ -20,8 +20,9 @@ enum gpio_lookup_flags { /** * struct gpiod_lookup - lookup table - * @chip_label: name of the chip the GPIO belongs to - * @chip_hwnum: hardware number (i.e. relative to the chip) of the GPIO + * @key: either the name of the chip the GPIO belongs to, or the GPIO line name + * @chip_hwnum: hardware number (i.e. relative to the chip) of the GPIO, or + * U16_MAX to indicate that @key is a GPIO line name * @con_id: name of the GPIO from the device's point of view * @idx: index of the GPIO in case several GPIOs share the same name * @flags: bitmask of gpio_lookup_flags GPIO_* values @@ -30,7 +31,7 @@ enum gpio_lookup_flags { * functions using platform data. */ struct gpiod_lookup { - const char *chip_label; + const char *key; u16 chip_hwnum; const char *con_id; unsigned int idx; @@ -63,17 +64,17 @@ struct gpiod_hog { /* * Simple definition of a single GPIO under a con_id */ -#define GPIO_LOOKUP(_chip_label, _chip_hwnum, _con_id, _flags) \ - GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _con_id, 0, _flags) +#define GPIO_LOOKUP(_key, _chip_hwnum, _con_id, _flags) \ + GPIO_LOOKUP_IDX(_key, _chip_hwnum, _con_id, 0, _flags) /* * Use this macro if you need to have several GPIOs under the same con_id. * Each GPIO needs to use a different index and can be accessed using * gpiod_get_index() */ -#define GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _con_id, _idx, _flags) \ +#define GPIO_LOOKUP_IDX(_key, _chip_hwnum, _con_id, _idx, _flags) \ { \ - .chip_label = _chip_label, \ + .key = _key, \ .chip_hwnum = _chip_hwnum, \ .con_id = _con_id, \ .idx = _idx, \ -- 2.17.1