From f8e84013a5263f3d1ec4643cce88382e58abbfcf Mon Sep 17 00:00:00 2001 From: Jianqun Xu Date: Tue, 15 Mar 2022 12:54:30 +0800 Subject: [PATCH] tools: testing: selftests: add iomux instance Add a new instance for rockchip pinctrl to do selftest, and the userland can set or get the iomux to one pin directly. Build the iomux by gcc: xx-gcc tools/testing/selftests/rkpinctrl/iomux.c -o iomux Tested on rk3588-evb1: [root@RK3588:/]# iomux 1 15 1 [root@RK3588:/]# iomux 1 15 mux get (GPIO1-15) = 1 [root@RK3588:/]# iomux 1 15 2 [root@RK3588:/]# iomux 1 15 mux get (GPIO1-15) = 2 Signed-off-by: Jianqun Xu Change-Id: Ic5271b25e55e1023caa5580346ffb82fe7a4cb65 --- tools/testing/selftests/rkpinctrl/Makefile | 6 ++ tools/testing/selftests/rkpinctrl/iomux.c | 107 +++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 tools/testing/selftests/rkpinctrl/Makefile create mode 100644 tools/testing/selftests/rkpinctrl/iomux.c diff --git a/tools/testing/selftests/rkpinctrl/Makefile b/tools/testing/selftests/rkpinctrl/Makefile new file mode 100644 index 000000000000..780f910f7679 --- /dev/null +++ b/tools/testing/selftests/rkpinctrl/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 +CFLAGS += -static -O3 -Wl,-no-as-needed -Wall + +TEST_GEN_PROGS = iomux + +include ../lib.mk diff --git a/tools/testing/selftests/rkpinctrl/iomux.c b/tools/testing/selftests/rkpinctrl/iomux.c new file mode 100644 index 000000000000..6d829c36bc30 --- /dev/null +++ b/tools/testing/selftests/rkpinctrl/iomux.c @@ -0,0 +1,107 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2022 Rockchip Electronics Co. Ltd. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../../../include/uapi/linux/rk-iomux.h" + +static int rk_iomux_ioctl_set(int fd, int bank, int pin, int mux) +{ + struct iomux_ioctl_data data = { + .bank = bank, + .pin = pin, + .mux = mux, + }; + int ret; + + if (!fd) + return -EINVAL; + + ret = ioctl(fd, IOMUX_IOC_MUX_SET, &data); + if (ret < 0) { + perror("fail to ioctl"); + return ret; + } + + return 0; +} + +static int rk_iomux_ioctl_get(int fd, int bank, int pin, int *mux) +{ + struct iomux_ioctl_data data = { + .bank = bank, + .pin = pin, + }; + int ret; + + if (!fd) + return -EINVAL; + + ret = ioctl(fd, IOMUX_IOC_MUX_GET, &data); + if (ret < 0) { + perror("fail to ioctl"); + return ret; + } + *mux = data.mux; + + return 0; +} + +static void usage(void) +{ + printf("%s:\n" + "set iomux:\n" + "iomux [bank index] [pin index] [mux value]\n" + "get iomux:\n" + "iomux [bank index] [pin index]\n", + __func__); +} + +int main(int argc, char *argv[]) +{ + const char *name = "/dev/iomux"; + int fd; + int bank, pin, mux; + int ret; + + if ((argc != 3) && (argc != 4)) { + usage(); + return -1; + } + + bank = atoi(argv[1]); + pin = atoi(argv[2]); + + fd = open(name, O_RDWR); + if (fd < 0) { + printf("open %s failed!\n", name); + return fd; + } + + if (argc == 4) { + mux = atoi(argv[3]); + ret = rk_iomux_ioctl_set(fd, bank, pin, mux); + if (ret) + goto err; + } else if (argc == 3) { + ret = rk_iomux_ioctl_get(fd, bank, pin, &mux); + if (ret) + goto err; + printf("mux get (GPIO%d-%d) = %d\n", bank, pin, mux); + } + +err: + close(fd); + return 0; +}