From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.7 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E4E3C4321A for ; Fri, 28 Jun 2019 05:21:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4CA1E214AF for ; Fri, 28 Jun 2019 05:21:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726572AbfF1FVU (ORCPT ); Fri, 28 Jun 2019 01:21:20 -0400 Received: from relay1.mentorg.com ([192.94.38.131]:42326 "EHLO relay1.mentorg.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726880AbfF1FVT (ORCPT ); Fri, 28 Jun 2019 01:21:19 -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 1hgjJc-0003ZX-Ou from Harish_Kandiga@mentor.com ; Thu, 27 Jun 2019 22:20:52 -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; Fri, 28 Jun 2019 06:20:48 +0100 From: Harish Jenny K N To: Linus Walleij , Bartosz Golaszewski CC: , Harish Jenny K N , Balasubramani Vivekanandan Subject: [PATCH V4 1/2] gpio: inverter: Add Inverter controller for gpio configuration Date: Fri, 28 Jun 2019 10:50:35 +0530 Message-ID: <1561699236-18620-2-git-send-email-harish_kandiga@mentor.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1561699236-18620-1-git-send-email-harish_kandiga@mentor.com> References: <1561699236-18620-1-git-send-email-harish_kandiga@mentor.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: svr-ies-mbx-06.mgc.mentorg.com (139.181.222.6) 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 inverter gpio controller to configure the polarity of the gpio pins. This driver enables the consumers to directly use the gpio pin without worrying about the hardware level polarity configuration. Polarity configuration will be done by the inverter 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 | 128 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 drivers/gpio/gpio-inverter.c diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index acd40eb..8978047 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 "Inverter GPIO controller for handling hardware inverters" + depends on OF_GPIO + help + Enabling this configuration provides an inverter gpio controller to + configure the polarity of the gpio pins. + This enables the consumers 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..b951b73 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -61,6 +61,7 @@ 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_IOP) += gpio-iop.o +obj-$(CONFIG_GPIO_INVERTER) += gpio-inverter.o obj-$(CONFIG_GPIO_IXP4XX) += gpio-ixp4xx.o obj-$(CONFIG_GPIO_IT87) += gpio-it87.o obj-$(CONFIG_GPIO_JANZ_TTL) += gpio-janz-ttl.o diff --git a/drivers/gpio/gpio-inverter.c b/drivers/gpio/gpio-inverter.c new file mode 100644 index 0000000..4883b33 --- /dev/null +++ b/drivers/gpio/gpio-inverter.c @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Inverter 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 + +struct gpio_inverter { + struct gpio_chip gpiochip; + int count; + struct gpio_desc *gpios[]; +}; + +static int gpio_inverter_direction_input(struct gpio_chip *chip, + unsigned int offset) +{ + struct gpio_inverter *inv = gpiochip_get_data(chip); + + return gpiod_direction_input(inv->gpios[offset]); +} + +static int gpio_inverter_direction_output(struct gpio_chip *chip, + unsigned int offset, int value) +{ + struct gpio_inverter *inv = gpiochip_get_data(chip); + + return gpiod_direction_output(inv->gpios[offset], value); +} + +static int gpio_inverter_get(struct gpio_chip *chip, + unsigned int offset) +{ + struct gpio_inverter *inv = gpiochip_get_data(chip); + + return !gpiod_get_value(inv->gpios[offset]); +} + +static void gpio_inverter_set(struct gpio_chip *chip, + unsigned int offset, int value) +{ + struct gpio_inverter *inv = gpiochip_get_data(chip); + + return gpiod_set_value(inv->gpios[offset], !value); +} + +static int gpio_inverter_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct gpio_inverter *inv; + struct gpio_chip *gpio_chip; + struct gpio_desc *gpio; + int index = 0; + int count; + int ret; + + count = gpiod_count(dev, "inverted"); + if (count <= 0) + return count ? count : -ENOENT; + + inv = devm_kzalloc(dev, struct_size(inv, gpios, count), GFP_KERNEL); + if (!inv) + return -ENOMEM; + + inv->count = count; + gpio_chip = &inv->gpiochip; + + platform_set_drvdata(pdev, inv); + + while (index < count) { + gpio = devm_gpiod_get_index(dev, "inverted", index, GPIOD_ASIS); + + if (gpio == ERR_PTR(-ENOENT)) + return -EPROBE_DEFER; + + if (IS_ERR(gpio)) + return PTR_ERR(gpio); + + inv->gpios[index++] = gpio; + + if (!gpio_chip->can_sleep && gpiod_cansleep(gpio)) + gpio_chip->can_sleep = true; + } + + 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 = devm_gpiochip_add_data(dev, gpio_chip, inv); + if (ret) { + dev_err(dev, "failed to add gpio controller\n"); + return ret; + } + + 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, + .driver = { + .name = "gpio-inverter", + .of_match_table = of_match_ptr(gpio_inverter_match), + } +}; + +module_platform_driver(gpio_inverter_driver); + +MODULE_AUTHOR("Harish Jenny K N "); +MODULE_AUTHOR("Balasubramani Vivekanandan "); +MODULE_DESCRIPTION("Inverter GPIO controller for configuring the gpio polarity"); +MODULE_LICENSE("GPL v2"); -- 2.7.4