From patchwork Tue Nov 30 15:41:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 517254 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 83ED5C433FE for ; Tue, 30 Nov 2021 15:41:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239060AbhK3Po7 (ORCPT ); Tue, 30 Nov 2021 10:44:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239072AbhK3Poy (ORCPT ); Tue, 30 Nov 2021 10:44:54 -0500 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD0D1C06175A for ; Tue, 30 Nov 2021 07:41:34 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id j3so45401128wrp.1 for ; Tue, 30 Nov 2021 07:41:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=R2S7hZsbaWxqnojZmhvTzst7bF3mpPWOC8sPSeXHhco=; b=QTTj6avdYbS8LwupOSolvw1V+rZnQJgj3t0TopIR2G1kFUI3xP7A7mxUt/6CzOaJE6 VU8P9V4iI0tsNllKZK6GG3bPj/R3iFIkrq9w6n2pc4Ug2cCEomTUirsFnHPl8QU6h8yW 3hzgokszrQTSfXALdUwd9tcXZ9w7SY5CPrJMbwwcAEmNDqC2pfSsTXWnYroNd0nG/xbY NLhPyz23m86QAWOGRRvBJnkLVblZyW7bJ0eicDqG9E1JqB/URoMJ45/t2b5cfX35bEkK XygvV3L24uK+f+F2JUVGCaT1Jq7zbSjHgZfnnT1d40H5wz/IDq38AjcTaRl41xaXtZME Txog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=R2S7hZsbaWxqnojZmhvTzst7bF3mpPWOC8sPSeXHhco=; b=oqU4i/jDx+1K8KLYrQtsnU4PnhWyNQhlRGTHCgORrJXQ1pPQAA7aVtWTq+wEzK9Tdg zNpjbTVqHBTyKHEXwKzkCHFNaF187HGI+KEAWuom2kG0XiibA8ohCK/155hKlnz+zCjW UoP3E7HHRJ7i/Y6echcxPrc5KhLIMFH7Pupgh2JYwS/AB0O2bdr1f9aycrSviMaoqrAS q/mgc1l02MJ2D40xF97jSKAPA5JVFaTtjG8Axm+IG8jAPzXVe85N2x8NWOjphz/PyS38 D2Oi/SGjE4Zf+0BzdomkTPMUQa1mPk1WLrQ6bsAVxx1mEJRcxR2RdvcVjz0toZCNVaRA 1+KA== X-Gm-Message-State: AOAM530Cgh8Kqe3l1dBbywWyeE23VDuiUSoDzmnGaSwfXN7YEVVichzp FYYgbvcPgmcwvgVK8fTOMlAgzQ== X-Google-Smtp-Source: ABdhPJzZozeWyfWUODkBPC10jiGnF31xLmcWBbejO6Sdl+B8tJJrIqIrZXAJAAPHjmpY2byw9NTVZg== X-Received: by 2002:adf:e3ca:: with SMTP id k10mr41577212wrm.592.1638286893330; Tue, 30 Nov 2021 07:41:33 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:334:ac00:7d50:ff5:f5c1:e225]) by smtp.gmail.com with ESMTPSA id l2sm2962792wmq.42.2021.11.30.07.41.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Nov 2021 07:41:33 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Shuah Khan , Geert Uytterhoeven Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v11 2/6] gpiolib: allow to specify the firmware node in struct gpio_chip Date: Tue, 30 Nov 2021 16:41:23 +0100 Message-Id: <20211130154127.12272-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20211130154127.12272-1-brgl@bgdev.pl> References: <20211130154127.12272-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Software nodes allow us to represent hierarchies for device components that don't have their struct device representation yet - for instance: banks of GPIOs under a common GPIO expander. The core gpiolib core however doesn't offer any way of passing this information from the drivers. This extends struct gpio_chip with a pointer to fwnode that can be set by the driver and used to pass device properties for child nodes. This is similar to how we handle device-tree sub-nodes with CONFIG_OF_GPIO enabled. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 15 ++++++++++++++- include/linux/gpio/driver.h | 2 ++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 22b98a590a88..2a877b4ccdee 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -593,13 +593,26 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, struct lock_class_key *lock_key, struct lock_class_key *request_key) { - struct fwnode_handle *fwnode = gc->parent ? dev_fwnode(gc->parent) : NULL; + struct fwnode_handle *fwnode = NULL; unsigned long flags; int ret = 0; unsigned i; int base = gc->base; struct gpio_device *gdev; +#if IS_ENABLED(CONFIG_OF_GPIO) + if (gc->of_node && gc->fwnode) { + pr_err("%s: tried to set both the of_node and fwnode in gpio_chip\n", + __func__); + return -EINVAL; + } +#endif /* CONFIG_OF_GPIO */ + + if (gc->fwnode) + fwnode = gc->fwnode; + else if (gc->parent) + fwnode = dev_fwnode(gc->parent); + /* * First: allocate and populate the internal stat container, and * set up the struct device. diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h index a673a359e20b..b0728c8ad90c 100644 --- a/include/linux/gpio/driver.h +++ b/include/linux/gpio/driver.h @@ -289,6 +289,7 @@ struct gpio_irq_chip { * number or the name of the SoC IP-block implementing it. * @gpiodev: the internal state holder, opaque struct * @parent: optional parent device providing the GPIOs + * @fwnode: optional fwnode providing this controller's properties * @owner: helps prevent removal of modules exporting active GPIOs * @request: optional hook for chip-specific activation, such as * enabling module power and clock; may sleep @@ -377,6 +378,7 @@ struct gpio_chip { const char *label; struct gpio_device *gpiodev; struct device *parent; + struct fwnode_handle *fwnode; struct module *owner; int (*request)(struct gpio_chip *gc, From patchwork Tue Nov 30 15:41:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 517253 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EB58AC4332F for ; Tue, 30 Nov 2021 15:42:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242790AbhK3Ppj (ORCPT ); Tue, 30 Nov 2021 10:45:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239235AbhK3Poz (ORCPT ); Tue, 30 Nov 2021 10:44:55 -0500 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29352C061756 for ; Tue, 30 Nov 2021 07:41:36 -0800 (PST) Received: by mail-wr1-x435.google.com with SMTP id u1so45322571wru.13 for ; Tue, 30 Nov 2021 07:41:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2GWUpkiuXSzN66GVjCDh6suML+iAta4LGHtIvVqjGEk=; b=PJzVzAJyxffNnedFEdvo481yX7NGXk4byfDTk9Z+gAKucSrVtPDOmma+Zq16xS4zPL /oLFiqmI1ZQ3+lfq9LpkGTH1V1WqqwvFN1V5Y320M+foXAXjllcA8CRcZlfGsnH4Llox 8BaOyYiRVwdMGUN58XJ6nWjhieOO0LmXhG0xcyXvoGP1Xhz65XUfClslBQ8mHGZmdxku jKAH5npL9jWZDUW0Jpl7Y5geb9YHprVMnDMV13mYlpW0/F87QzP23ZDDbnmOjS7AeDsE IrKFJYoXlRjd6AIFWYie7vFBZfOTsm3KpIFBFtL16epNToVpCGKz/w3m3eVw+79SxWqY F3yQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2GWUpkiuXSzN66GVjCDh6suML+iAta4LGHtIvVqjGEk=; b=ENOmUeOw15z+fqQEg7v1RRe2MYE96ySGo71hwQ3zc78w4rwYdcEsHMKrQR4nAd1Sva OV0vHQbO8GJALEHhojqoAZaSc/6fRqvv6GPRImLF29lwqPvt9RdAe4RTFfV7DHSfrm1Q r8errW7YI2vi+sLHaIry3Y1/cVlYx/ZObROx8OOupkwkiP7LAXeFor1VDHKlvaLZQUIr WfB5irYQ+L9GaJBDxCicx7agTIKVTF/LHCm1T34N9C4mK+8yt9rwsyoj7JLEXLGI/pkg uyg26eA29GB5ADwBWCUOMFuHxfWlNuNl+juKNPE0nzhcnmNzGEG7y3iCbUcRRheZUVgq t5hw== X-Gm-Message-State: AOAM530u1U5dX1tVIB7jL7TmTwjqGhS12ivBFP3VVgZhyplCmeRgrQju EDp/ixzTj3Kf8oZRIkCn2fQfXtXHPj1UTkbk X-Google-Smtp-Source: ABdhPJz0VsHHfilKNFFewqQ0dSxoK0Rdda6+4+w0X/nSuuXalMZ/KZ7xP+oj2cH5v0NA84jqI1U4Jg== X-Received: by 2002:adf:ce0e:: with SMTP id p14mr40866287wrn.423.1638286894783; Tue, 30 Nov 2021 07:41:34 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:334:ac00:7d50:ff5:f5c1:e225]) by smtp.gmail.com with ESMTPSA id l2sm2962792wmq.42.2021.11.30.07.41.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Nov 2021 07:41:34 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Shuah Khan , Geert Uytterhoeven Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v11 4/6] selftests: gpio: provide a helper for reading chip info Date: Tue, 30 Nov 2021 16:41:25 +0100 Message-Id: <20211130154127.12272-5-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20211130154127.12272-1-brgl@bgdev.pl> References: <20211130154127.12272-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Add a simple program that allows to retrieve chip properties from the GPIO character device. This will be used in gpio-sim selftests. Signed-off-by: Bartosz Golaszewski --- tools/testing/selftests/gpio/.gitignore | 1 + tools/testing/selftests/gpio/Makefile | 2 +- tools/testing/selftests/gpio/gpio-chip-info.c | 57 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/gpio/gpio-chip-info.c diff --git a/tools/testing/selftests/gpio/.gitignore b/tools/testing/selftests/gpio/.gitignore index a4969f7ee020..4ea4f58dab1a 100644 --- a/tools/testing/selftests/gpio/.gitignore +++ b/tools/testing/selftests/gpio/.gitignore @@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only gpio-mockup-cdev +gpio-chip-info diff --git a/tools/testing/selftests/gpio/Makefile b/tools/testing/selftests/gpio/Makefile index d7b312b44a62..a40b818c394e 100644 --- a/tools/testing/selftests/gpio/Makefile +++ b/tools/testing/selftests/gpio/Makefile @@ -2,7 +2,7 @@ TEST_PROGS := gpio-mockup.sh TEST_FILES := gpio-mockup-sysfs.sh -TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev +TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info CFLAGS += -O2 -g -Wall -I../../../../usr/include/ include ../lib.mk diff --git a/tools/testing/selftests/gpio/gpio-chip-info.c b/tools/testing/selftests/gpio/gpio-chip-info.c new file mode 100644 index 000000000000..8f2d2e23e9f6 --- /dev/null +++ b/tools/testing/selftests/gpio/gpio-chip-info.c @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * GPIO character device helper for reading chip information. + * + * Copyright (C) 2021 Bartosz Golaszewski + */ + +#include +#include +#include +#include +#include +#include +#include + +static void print_usage(void) +{ + printf("usage:\n"); + printf(" gpio-chip-info [name|label|num-lines]\n"); +} + +int main(int argc, char **argv) +{ + struct gpiochip_info info; + int fd, ret; + + if (argc !=3) { + print_usage(); + return EXIT_FAILURE; + } + + fd = open(argv[1], O_RDWR); + if (fd < 0) { + perror("unable to open the GPIO chip"); + return EXIT_FAILURE; + } + + memset(&info, 0, sizeof(info)); + ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &info); + if (ret) { + perror("chip info ioctl failed"); + return EXIT_FAILURE; + } + + if (strcmp(argv[2], "name") == 0) { + printf("%s\n", info.name); + } else if (strcmp(argv[2], "label") == 0) { + printf("%s\n", info.label); + } else if (strcmp(argv[2], "num-lines") == 0) { + printf("%u\n", info.lines); + } else { + fprintf(stderr, "unknown command: %s\n", argv[2]); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} From patchwork Tue Nov 30 15:41:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 517252 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ED01EC433EF for ; Tue, 30 Nov 2021 15:43:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229777AbhK3Pq7 (ORCPT ); Tue, 30 Nov 2021 10:46:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239499AbhK3Po6 (ORCPT ); Tue, 30 Nov 2021 10:44:58 -0500 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C5A4C06175D for ; Tue, 30 Nov 2021 07:41:37 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id v11so45364199wrw.10 for ; Tue, 30 Nov 2021 07:41:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sdC8L7KcajeuYgRxRzq/BDd9/Try3NuMaNKMjlQg9Bs=; b=Yf4mQKJblrXfM3raJEqcE4hPfyer0wn3iuXn68BPZ15SAnYis+DGRlWy2iLRGY0lwI WWvOtt1DyAIjAFWEt3Qi1kpn3LW7yIaoujYS9czLLYqT5G/MRinWBxcq3hauu90xwcIv q7/WHviChlHskgD7+oTEJI5W4ocuITqUk8iFjWLviFVaTOAax2Hy96gy2Z58f7DZqElp B7VZp58LY5IBXVz9OZPu9XkyGhV9cKs5chlci6nc/XO7jQhkpS95fA8hrMN09x3lX0jR DPu+ymrAn09w/brAZJ3BTpIGypvK2ZjhkthtJI48r4GJ9t98MB2TT1KHPdfY5DJPWZHR n2QQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sdC8L7KcajeuYgRxRzq/BDd9/Try3NuMaNKMjlQg9Bs=; b=mAa8/uhj6nzz6LAYilZ4fSmbgkHJcafABMn2OzWHlI80f3SMioDpQcnDgkSlGcmmya 8VnQvulCwJouT5AJRUhx81bMxYaxsZV7jXl5v9H3HfoHM6JirkLcRHNcdrvEH/w9ydhM NrGzmIe69S1MstK/J1ePw68+kXvOBzPpfz/OePkwJBkpxmxV6UHSHoY9XUWBjmFhSYjS SHA64/qXpdWQV3VQBhewKIzCyU5poG0Zbjg706YbvnDiuKaeBpNL0jmPVNfiPal77vmG Npa8HsndP4NNGCovxews/4rViv8PKS/vQHMJ2vMdIXXWMo+TjCCm7O+FMxVEOs6wKZtJ F6sg== X-Gm-Message-State: AOAM532f0gQw9oZBLPpPMhQHRTGmfEzoZCV9NkhGh84o+Hf9Xuy1wSUF zSsMFpeJZhPy5P+zd17GPEwp9Q== X-Google-Smtp-Source: ABdhPJzm8IvnQQYw9VS4Q99U891uia38OB50QT7S89pka8U/LJBr9flPXUv6TQkHNrkv/PsEpDAQng== X-Received: by 2002:a5d:5850:: with SMTP id i16mr42149708wrf.197.1638286895675; Tue, 30 Nov 2021 07:41:35 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:334:ac00:7d50:ff5:f5c1:e225]) by smtp.gmail.com with ESMTPSA id l2sm2962792wmq.42.2021.11.30.07.41.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Nov 2021 07:41:35 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Shuah Khan , Geert Uytterhoeven Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v11 5/6] selftests: gpio: add a helper for reading GPIO line names Date: Tue, 30 Nov 2021 16:41:26 +0100 Message-Id: <20211130154127.12272-6-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20211130154127.12272-1-brgl@bgdev.pl> References: <20211130154127.12272-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Add a simple program that allows to read GPIO line names from the character device. This will be used in gpio-sim selftests. Signed-off-by: Bartosz Golaszewski --- tools/testing/selftests/gpio/.gitignore | 1 + tools/testing/selftests/gpio/Makefile | 2 +- tools/testing/selftests/gpio/gpio-line-name.c | 55 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/gpio/gpio-line-name.c diff --git a/tools/testing/selftests/gpio/.gitignore b/tools/testing/selftests/gpio/.gitignore index 4ea4f58dab1a..ededb077a3a6 100644 --- a/tools/testing/selftests/gpio/.gitignore +++ b/tools/testing/selftests/gpio/.gitignore @@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only gpio-mockup-cdev gpio-chip-info +gpio-line-name diff --git a/tools/testing/selftests/gpio/Makefile b/tools/testing/selftests/gpio/Makefile index a40b818c394e..293aa9749408 100644 --- a/tools/testing/selftests/gpio/Makefile +++ b/tools/testing/selftests/gpio/Makefile @@ -2,7 +2,7 @@ TEST_PROGS := gpio-mockup.sh TEST_FILES := gpio-mockup-sysfs.sh -TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info +TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info gpio-line-name CFLAGS += -O2 -g -Wall -I../../../../usr/include/ include ../lib.mk diff --git a/tools/testing/selftests/gpio/gpio-line-name.c b/tools/testing/selftests/gpio/gpio-line-name.c new file mode 100644 index 000000000000..e635cfadbded --- /dev/null +++ b/tools/testing/selftests/gpio/gpio-line-name.c @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * GPIO character device helper for reading line names. + * + * Copyright (C) 2021 Bartosz Golaszewski + */ + +#include +#include +#include +#include +#include +#include +#include + +static void print_usage(void) +{ + printf("usage:\n"); + printf(" gpio-line-name \n"); +} + +int main(int argc, char **argv) +{ + struct gpio_v2_line_info info; + int fd, ret; + char *endp; + + if (argc != 3) { + print_usage(); + return EXIT_FAILURE; + } + + fd = open(argv[1], O_RDWR); + if (fd < 0) { + perror("unable to open the GPIO chip"); + return EXIT_FAILURE; + } + + memset(&info, 0, sizeof(info)); + info.offset = strtoul(argv[2], &endp, 10); + if (*endp != '\0') { + print_usage(); + return EXIT_FAILURE; + } + + ret = ioctl(fd, GPIO_V2_GET_LINEINFO_IOCTL, &info); + if (ret) { + perror("line info ioctl failed"); + return EXIT_FAILURE; + } + + printf("%s\n", info.name); + + return EXIT_SUCCESS; +}