From 7d95201ddd2cbcd71e29abb200f12157486456dd Mon Sep 17 00:00:00 2001 From: "wanwei.jiang" Date: Mon, 20 Dec 2021 20:06:52 +0800 Subject: [PATCH] gpio: porting gpiolib function [1/1] PD#SWPL-65870 Problem: add function gpiod_set_pull Solution: add function gpiod_set_pull Verify: s4d Signed-off-by: wanwei.jiang Change-Id: Id027b5b1abfb675445ff01fc8faaa207147a8954 --- arch/arm64/configs/amlogic_gki.fragment | 3 + drivers/Kconfig | 2 + drivers/Makefile | 1 + drivers/gpio/Kconfig | 10 +++ drivers/gpio/Makefile | 7 ++ drivers/gpio/gpiolib.c | 98 +++++++++++++++++++++++++ include/linux/amlogic/gpiolib.h | 31 ++++++++ 7 files changed, 152 insertions(+) create mode 100644 drivers/gpio/Kconfig create mode 100644 drivers/gpio/Makefile create mode 100644 drivers/gpio/gpiolib.c create mode 100644 include/linux/amlogic/gpiolib.h diff --git a/arch/arm64/configs/amlogic_gki.fragment b/arch/arm64/configs/amlogic_gki.fragment index 662f80760..3f6656592 100644 --- a/arch/arm64/configs/amlogic_gki.fragment +++ b/arch/arm64/configs/amlogic_gki.fragment @@ -47,3 +47,6 @@ CONFIG_AMLOGIC_CPU_INFO=m # amlogic-pm.ko CONFIG_AMLOGIC_GX_SUSPEND=m CONFIG_AMLOGIC_LEGACY_EARLY_SUSPEND=y + +# amlogic-gpiolib.ko +CONFIG_AMLOGIC_GPIOLIB=m diff --git a/drivers/Kconfig b/drivers/Kconfig index 2ce490091..08acd983b 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -10,4 +10,6 @@ source "../common_drivers/drivers/secmon/Kconfig" source "../common_drivers/drivers/power/Kconfig" source "../common_drivers/drivers/cpu_info/Kconfig" source "../common_drivers/drivers/pm/Kconfig" +source "../common_drivers/drivers/gpio/Kconfig" + endmenu diff --git a/drivers/Makefile b/drivers/Makefile index ffcb9a006..57d166777 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -8,6 +8,7 @@ obj-y += secmon/ obj-y += power/ obj-y += cpu_info/ obj-y += pm/ +obj-y += gpio/ KBUILD_CFLAGS_MODULE += $(GKI_EXT_MODULE_PREDEFINE) diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig new file mode 100644 index 000000000..f3fc2d0d9 --- /dev/null +++ b/drivers/gpio/Kconfig @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: (GPL-2.0+ OR MIT) + +config AMLOGIC_GPIOLIB + tristate "Amlogic gpiolib expand" + depends on GPIOLIB + default n + help + Say Y here if you want to use the + amlogic gpiolib expand. + diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile new file mode 100644 index 000000000..4c0fde80b --- /dev/null +++ b/drivers/gpio/Makefile @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: (GPL-2.0+ OR MIT) + +ccflags-y += -I$(srctree)/drivers/gpio + +MODULE_NAME = amlogic-gpiolib +obj-$(CONFIG_AMLOGIC_GPIOLIB) = $(MODULE_NAME).o +$(MODULE_NAME)-y += gpiolib.o diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c new file mode 100644 index 000000000..fb2e912fa --- /dev/null +++ b/drivers/gpio/gpiolib.c @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: (GPL-2.0+ OR MIT) +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * This descriptor validation needs to be inserted verbatim into each + * function taking a descriptor, so we need to use a preprocessor + * macro to avoid endless duplication. If the desc is NULL it is an + * optional GPIO and calls should just bail out. + */ +static int validate_desc(const struct gpio_desc *desc, const char *func) +{ + if (!desc) + return 0; + if (IS_ERR(desc)) { + pr_warn("%s: invalid GPIO (errorpointer)\n", func); + return PTR_ERR(desc); + } + if (!desc->gdev) { + pr_warn("%s: invalid GPIO (no device)\n", func); + return -EINVAL; + } + if (!desc->gdev->chip) { + dev_warn(&desc->gdev->dev, + "%s: backing chip is gone\n", func); + return 0; + } + return 1; +} + +#define VALIDATE_DESC(desc) do { \ + int __valid = validate_desc(desc, __func__); \ + if (__valid <= 0) \ + return __valid; \ + } while (0) + +/** + * gpiod_set_pull - enable pull-down/up for the gpio, or disable. + * @desc: descriptor of the GPIO for which to set pull + * @value: value to set + * + * Returns: + * 0 on success, %-ENOTSUPP if the controller doesn't support setting the + * pull. + */ +int gpiod_set_pull(struct gpio_desc *desc, unsigned int value) +{ + struct gpio_chip *chip; + unsigned long config; + enum pin_config_param mode; + + VALIDATE_DESC(desc); + chip = desc->gdev->chip; + if (!chip || !chip->set_config) { + gpiod_dbg(desc, + "%s: missing set() or set_config() operations\n", + __func__); + return -ENOTSUPP; + } + + switch (value) { + case GPIOD_PULL_DIS: + mode = PIN_CONFIG_BIAS_DISABLE; + break; + case GPIOD_PULL_DOWN: + mode = PIN_CONFIG_BIAS_PULL_DOWN; + break; + case GPIOD_PULL_UP: + mode = PIN_CONFIG_BIAS_PULL_UP; + break; + default: + return -ENOTSUPP; + } + config = PIN_CONF_PACKED(mode, 0); + + return chip->set_config(chip, gpio_chip_hwgpio(desc), config); +} +EXPORT_SYMBOL_GPL(gpiod_set_pull); + +static int __init pinctrl_module_init(void) +{ + return 0; +} + +static void __exit pinctrl_module_exit(void) +{ +} + +module_init(pinctrl_module_init); +module_exit(pinctrl_module_exit); + +MODULE_LICENSE("GPL v2"); diff --git a/include/linux/amlogic/gpiolib.h b/include/linux/amlogic/gpiolib.h new file mode 100644 index 000000000..c71f16367 --- /dev/null +++ b/include/linux/amlogic/gpiolib.h @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: (GPL-2.0+ OR MIT) + +/* + * Copyright (c) 2019 Amlogic, Inc. All rights reserved. + */ + +#ifndef __AML_GPIO_H__ +#define __AML_GPIO_H__ + +#include +#include +#include + +enum gpiod_pull_type { + GPIOD_PULL_DIS = 0, + GPIOD_PULL_DOWN = 1, + GPIOD_PULL_UP = 2, +}; + +#ifdef CONFIG_GPIOLIB +int gpiod_set_pull(struct gpio_desc *desc, unsigned int value); +#else /* CONFIG_GPIOLIB */ +static inline int gpiod_set_pull(struct gpio_desc *desc, unsigned int value) +{ + /* GPIO can never have been requested */ + WARN_ON(1); + return -EINVAL; +} +#endif /* CONFIG_GPIOLIB */ + +#endif //__AML_GPIO_H__