From patchwork Wed Jun 12 04:50:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harish Jenny K N X-Patchwork-Id: 1114284 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=mentor.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 45Nvbh6gjrz9s9y for ; Wed, 12 Jun 2019 14:51:44 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730249AbfFLEvo (ORCPT ); Wed, 12 Jun 2019 00:51:44 -0400 Received: from relay1.mentorg.com ([192.94.38.131]:51257 "EHLO relay1.mentorg.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730017AbfFLEvo (ORCPT ); Wed, 12 Jun 2019 00:51:44 -0400 Received: from nat-ies.mentorg.com ([192.94.31.2] helo=svr-ies-mbx-01.mgc.mentorg.com) by relay1.mentorg.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-SHA384:256) id 1havDq-0000fO-Te from Harish_Kandiga@mentor.com ; Tue, 11 Jun 2019 21:50:55 -0700 Received: from hkandiga-VirtualBox.ina-wifi.mentorg.com (137.202.0.90) by svr-ies-mbx-01.mgc.mentorg.com (139.181.222.1) with Microsoft SMTP Server (TLS) id 15.0.1320.4; Wed, 12 Jun 2019 05:50:50 +0100 From: Harish Jenny K N To: Linus Walleij , Bartosz Golaszewski CC: , Harish Jenny K N , Balasubramani Vivekanandan Subject: [PATCH V1 1/2] gpio: inverter: Add virtual controller for gpio configuration Date: Wed, 12 Jun 2019 10:20:33 +0530 Message-ID: <1560315034-29712-2-git-send-email-harish_kandiga@mentor.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1560315034-29712-1-git-send-email-harish_kandiga@mentor.com> References: <1560315034-29712-1-git-send-email-harish_kandiga@mentor.com> MIME-Version: 1.0 X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: SVR-IES-MBX-03.mgc.mentorg.com (139.181.222.3) To svr-ies-mbx-01.mgc.mentorg.com (139.181.222.1) Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Provides a new virtual gpio controller to configure the polarity of the gpio pins used by the userspace. When there is no kernel driver using the gpio pin, it becomes necessary for the userspace to configure the polarity of the gpio pin. This driver enables the userspace to directly use the gpio pin without worrying about the hardware level polarity configuration. Polarity configuration will be done by the virtual gpio controller based on device tree information Signed-off-by: Balasubramani Vivekanandan Signed-off-by: Harish Jenny K N --- drivers/gpio/Kconfig | 9 +++ drivers/gpio/Makefile | 1 + drivers/gpio/gpio-inverter.c | 144 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 drivers/gpio/gpio-inverter.c -- 2.7.4 diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index acd40eb..15893dd 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -77,6 +77,15 @@ config GPIO_GENERIC depends on HAS_IOMEM # Only for IOMEM drivers tristate +config GPIO_INVERTER + tristate "Virtual GPIO controller for configuring the gpio polarity" + depends on OF_GPIO + help + Enabling this configuration provides a virtual gpio controller to + configure the polarity of the gpio pins used by the userspace. + This enables the userspace to directly use the gpio pin without + worrying about the hardware level polarity configuration + # put drivers in the right section, in alphabetical order # This symbol is selected by both I2C and SPI expanders diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile index 6700eee..63d1bcc8 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -60,6 +60,7 @@ obj-$(CONFIG_GPIO_GW_PLD) += gpio-gw-pld.o obj-$(CONFIG_GPIO_HLWD) += gpio-hlwd.o obj-$(CONFIG_HTC_EGPIO) += gpio-htc-egpio.o obj-$(CONFIG_GPIO_ICH) += gpio-ich.o +obj-$(CONFIG_GPIO_INVERTER) += gpio-inverter.o obj-$(CONFIG_GPIO_IOP) += gpio-iop.o obj-$(CONFIG_GPIO_IXP4XX) += gpio-ixp4xx.o obj-$(CONFIG_GPIO_IT87) += gpio-it87.o diff --git a/drivers/gpio/gpio-inverter.c b/drivers/gpio/gpio-inverter.c new file mode 100644 index 0000000..2408791 --- /dev/null +++ b/drivers/gpio/gpio-inverter.c @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Virtual GPIO controller for configuring the gpio polarity + * + * Copyright (c) 2019 Mentor Graphics Inc. + * Developed using gpiolib and gpio documentation as reference + * + */ + +#include +#include +#include +#include +#include +#include + +struct gpio_inverter { + struct gpio_chip gpiochip; + int count; + struct gpio_desc *gpios[0]; +}; + +static int gpio_inverter_direction_input(struct gpio_chip *chip, + unsigned int offset) +{ + struct gpio_inverter *virt = gpiochip_get_data(chip); + + return gpiod_direction_input(virt->gpios[offset]); +} + +static int gpio_inverter_direction_output(struct gpio_chip *chip, + unsigned int offset, int value) +{ + struct gpio_inverter *virt = gpiochip_get_data(chip); + + return gpiod_direction_output(virt->gpios[offset], value); +} + +static int gpio_inverter_get(struct gpio_chip *chip, + unsigned int offset) +{ + struct gpio_inverter *virt = gpiochip_get_data(chip); + + return gpiod_get_value(virt->gpios[offset]); +} + +static void gpio_inverter_set(struct gpio_chip *chip, + unsigned int offset, int value) +{ + struct gpio_inverter *virt = gpiochip_get_data(chip); + + return gpiod_set_value(virt->gpios[offset], value); +} + +static int gpio_inverter_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct gpio_inverter *virt; + struct gpio_chip *gpio_chip; + struct gpio_desc *gpio; + int index = 0; + int count; + int ret; + int size; + + count = of_gpio_named_count(dev->of_node, "mapped-gpios"); + if (count <= 0) + return count ? count : -ENOENT; + + size = sizeof(*virt) + count * sizeof(struct gpio_desc *); + virt = devm_kzalloc(dev, size, GFP_KERNEL); + if (!virt) + return -ENOMEM; + + virt->count = count; + + platform_set_drvdata(pdev, virt); + + while (index < count) { + gpio = devm_gpiod_get_index(dev, "mapped", index, GPIOD_ASIS); + if (IS_ERR(gpio)) + return PTR_ERR(gpio); + + virt->gpios[index++] = gpio; + } + + gpio_chip = &virt->gpiochip; + gpio_chip->direction_input = gpio_inverter_direction_input; + gpio_chip->direction_output = gpio_inverter_direction_output; + gpio_chip->get = gpio_inverter_get; + gpio_chip->set = gpio_inverter_set; + gpio_chip->label = dev_name(dev); + gpio_chip->parent = dev; + gpio_chip->owner = THIS_MODULE; + gpio_chip->base = -1; + gpio_chip->ngpio = count; + + ret = gpiochip_add_data(gpio_chip, virt); + if (ret) { + dev_err(dev, "failed to add gpio controller\n"); + return ret; + } + + return 0; +} + +static int gpio_inverter_remove(struct platform_device *pdev) +{ + struct gpio_inverter *virt = platform_get_drvdata(pdev); + + gpiochip_remove(&virt->gpiochip); + + return 0; +} + +static const struct of_device_id gpio_inverter_match[] = { + { .compatible = "gpio-inverter", }, { }, +}; + +static struct platform_driver gpio_inverter_driver = { + .probe = gpio_inverter_probe, + .remove = gpio_inverter_remove, + .driver = { + .name = "gpio-inverter", + .of_match_table = of_match_ptr(gpio_inverter_match), + } +}; + +static int __init gpio_inverter_init(void) +{ + return platform_driver_register(&gpio_inverter_driver); +} +late_initcall(gpio_inverter_init); + +static void __exit gpio_inverter_exit(void) +{ + platform_driver_unregister(&gpio_inverter_driver); +} +module_exit(gpio_inverter_exit); + +MODULE_AUTHOR("Harish Jenny K N "); +MODULE_AUTHOR("Balasubramani Vivekanandan "); +MODULE_DESCRIPTION("Virtual GPIO controller for configuring the gpio polarity"); +MODULE_LICENSE("GPL v2"); From patchwork Wed Jun 12 04:50:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harish Jenny K N X-Patchwork-Id: 1114283 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=mentor.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 45NvZr3g5nz9s3l for ; Wed, 12 Jun 2019 14:51:00 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730248AbfFLEvA (ORCPT ); Wed, 12 Jun 2019 00:51:00 -0400 Received: from relay1.mentorg.com ([192.94.38.131]:51264 "EHLO relay1.mentorg.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730250AbfFLEvA (ORCPT ); Wed, 12 Jun 2019 00:51:00 -0400 Received: from nat-ies.mentorg.com ([192.94.31.2] helo=svr-ies-mbx-01.mgc.mentorg.com) by relay1.mentorg.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-SHA384:256) id 1havDu-0000ff-0c from Harish_Kandiga@mentor.com ; Tue, 11 Jun 2019 21:50:58 -0700 Received: from hkandiga-VirtualBox.ina-wifi.mentorg.com (137.202.0.90) by svr-ies-mbx-01.mgc.mentorg.com (139.181.222.1) with Microsoft SMTP Server (TLS) id 15.0.1320.4; Wed, 12 Jun 2019 05:50:53 +0100 From: Harish Jenny K N To: Linus Walleij , Bartosz Golaszewski CC: , Harish Jenny K N , Balasubramani Vivekanandan Subject: [PATCH V1 2/2] gpio: inverter: document the inverter bindings Date: Wed, 12 Jun 2019 10:20:34 +0530 Message-ID: <1560315034-29712-3-git-send-email-harish_kandiga@mentor.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1560315034-29712-1-git-send-email-harish_kandiga@mentor.com> References: <1560315034-29712-1-git-send-email-harish_kandiga@mentor.com> MIME-Version: 1.0 X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: SVR-IES-MBX-03.mgc.mentorg.com (139.181.222.3) To svr-ies-mbx-01.mgc.mentorg.com (139.181.222.1) Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Document the device tree binding for the virtual gpio controller to configure the polarity of the gpio pins used by the userspace. Signed-off-by: Harish Jenny K N --- .../devicetree/bindings/gpio/gpio-inverter.txt | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Documentation/devicetree/bindings/gpio/gpio-inverter.txt -- 2.7.4 diff --git a/Documentation/devicetree/bindings/gpio/gpio-inverter.txt b/Documentation/devicetree/bindings/gpio/gpio-inverter.txt new file mode 100644 index 0000000..4411f83 --- /dev/null +++ b/Documentation/devicetree/bindings/gpio/gpio-inverter.txt @@ -0,0 +1,30 @@ +GPIO-INVERTER +====== +This binding defines the gpio-inverter. The gpio-inverter is a driver that +allows to properly describe the gpio polarities on the hardware for those gpio +pins which are used only from userspace + +Please refer to gpio.txt for generic information regarding GPIO bindings. + +Required properties: +- compatible : "gpio-inverter". +- gpio-controller: Marks the port as GPIO controller. +- #gpio-cells: Two. The first cell is the pin number and + the second cell is used to specify the gpio polarity as defined in + defined in : + 0 = GPIO_ACTIVE_HIGH + 1 = GPIO_ACTIVE_LOW +- mapped-gpios: Array of GPIO pins required from userspace, whose polarity has + to be configured in the driver. +- gpio-line-names: This is an array of strings defining the names for the + mapped-gpios correspondingly. Name should be defined for each gpio pin. + +Example: + +gpio_inv: gpio-inv { + compatible = "gpio-inverter"; + gpio-controller; + #gpio-cell = <2>; + mapped-gpios = <&gpio5 24 1>, <&gpio7 0 1>, <&gpio7 1 1>; + gpio-line-names = "JTAG_DNL_EN", "lvds-pwrdwn", "lcd-on"; +};