From patchwork Sun Oct 13 11:40:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manivannan Sadhasivam X-Patchwork-Id: 176072 Delivered-To: patch@linaro.org Received: by 2002:a92:7e96:0:0:0:0:0 with SMTP id q22csp3236935ill; Sun, 13 Oct 2019 04:41:07 -0700 (PDT) X-Google-Smtp-Source: APXvYqwjDCu3tHl1FObO72SHvshchHn8Kfr0p2y4aoqQaUDn110rfSC5ko5fNNAqI1Z+fUa84knQ X-Received: by 2002:a50:8f65:: with SMTP id 92mr23924154edy.9.1570966867317; Sun, 13 Oct 2019 04:41:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570966867; cv=none; d=google.com; s=arc-20160816; b=Ut7uMz3tISSu1z5YvieYhNVV8VmcLSiQ6bf0iTcsD1+vQR3NHpKjFy0vebh9lQiiH+ 04Hv+X7x1R7iYGLkASl9jOd1NPY+CNgfgUXC6nFI3iN5SzdnQoer2W1sED3Jus8lDthQ IRfqCbjz3zFVVp8WMJ6aop48ax2fbGGGVe3R9BWY9DLSCoZ/N5z1o0g47ABm4NEIsKON XVCQVsE+vW5DhNKKg/q6L3hpTNz0fBlgntURcJCg0nE0F5Pre5craj8d7sw8t0YXLgRH Lc+PYm8DAUKXTscTl8W2cCE6jhSe6tbSNQ91nwMhEDcYEDiMr7Y8I/CHjSpAmG8L6aH2 13NA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=lUWLCO3j+mo/BAGmxYST8nF91emB067pVH67pMgS/AQ=; b=SgOZ4hGPRZpegg7KkUVhYr+aGdO873mCvIwW8LmnK+TVIlo2TLhAZDjo6kkIFqr95M 35+zGB/vmj9z/IWtF3PMl0VIFgPZA+dIzyFHZrBp5E9JMpwpTgnIPapJXM5RFXGN87kf 1siJFtYGVDAJBODmG7+O3ic/z8huSGtmnBq65we6kH+1q9WcLoqMI4BXgWqP71fLIHIM /yk+EIjPTIzqQolVrBCmPk6iiR5FS0lkuX4CvZHad3MflKNDCHeZSHrvtAvbdtGlQzEQ xnC+qA198B2BwTMLP2cyZDfs5b1On/eckisSu5MiwjarNnHuQ9zygd+cwFOS6dqrPZJe xnKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=FQdjfChI; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id oc20si9046899ejb.260.2019.10.13.04.41.06; Sun, 13 Oct 2019 04:41:07 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=FQdjfChI; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729066AbfJMLlA (ORCPT + 22 others); Sun, 13 Oct 2019 07:41:00 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:34142 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728956AbfJMLk7 (ORCPT ); Sun, 13 Oct 2019 07:40:59 -0400 Received: by mail-pg1-f195.google.com with SMTP id k20so1074506pgi.1 for ; Sun, 13 Oct 2019 04:40:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=lUWLCO3j+mo/BAGmxYST8nF91emB067pVH67pMgS/AQ=; b=FQdjfChIOZUhJc5h+Y0k+pMCnM2bKElBSUTtfyEcS2g8af7Leg+e6lF47jVbTZ8+WH MuQLULiVY9M9U9stRAvTWEVcFb3U0sr/AQLoLehnBdxP2+sp0dZqQJ5aNWAv1SywcA+N bcOFmH9+SgPHlW2/jX8/E6oGtKw435QrFYRzGs92efEyeqEboChIuSVIvFctoFavRJ/S H08XK1blvc7r5jQfifCuyYNhFish5JZ8LPmeVENhmRqYVagP2IJUS0PddQ9Kz6vK0p+z t9zENDWs5YEOPuINs8xE8Jhc2BpN0vjnkz8RzO5QZAha7AE5NWtsxYdrs5ECs4oqTU94 fpsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=lUWLCO3j+mo/BAGmxYST8nF91emB067pVH67pMgS/AQ=; b=Fj48HZmOsAk5c4Pwqv3mBdvuKgYgzdykWkOT6CJ9P/05V1AnJJmjkhXhp1lBVJkdCV fI+6lWIet2rjM9zRnOMimQKWLeLfbUvtd54pyTgw9erO4x8zrdpiLTdrO0NDbczzA/YW zlengppO4PsPnR4TTkTe6QOqLJFPaPP0vl/VhdH6tBqDpb5gnXwXCl0uOhB6l4jJprTd ZzYY2RdTxGunMbPnGP0lFA4/UCGaaX7RX8dlNg9F8dYmpmi39zPNPJkAphfEk9d5ZtxK HZu5zOn1D1zJw4/Bjm3HhiLynZaAcUBbyQ6j0lnLeVUyMxN1nHZ9P2K90bVE3Iw62RTv 0/BQ== X-Gm-Message-State: APjAAAV2WJl44Y6AiSPNtU4Lcni02ZKBpHQqBGJo0qGLnIt/2vmcUwLC Q1hj+Tne8l6GEsBbMtKEzAwi X-Received: by 2002:a65:50c5:: with SMTP id s5mr25503082pgp.52.1570966858511; Sun, 13 Oct 2019 04:40:58 -0700 (PDT) Received: from localhost.localdomain ([2409:4072:8e:4f53:b957:652b:7622:f311]) by smtp.gmail.com with ESMTPSA id g12sm23165736pfb.97.2019.10.13.04.40.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Oct 2019 04:40:57 -0700 (PDT) From: Manivannan Sadhasivam To: linus.walleij@linaro.org, bgolaszewski@baylibre.com Cc: linux-arm-kernel@lists.infradead.org, linux-unisoc@lists.infradead.org, linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, orsonzhai@gmail.com, Manivannan Sadhasivam Subject: [PATCH 2/4] gpio: Add RDA Micro GPIO controller support Date: Sun, 13 Oct 2019 17:10:35 +0530 Message-Id: <20191013114037.9845-3-manivannan.sadhasivam@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191013114037.9845-1-manivannan.sadhasivam@linaro.org> References: <20191013114037.9845-1-manivannan.sadhasivam@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add support for GPIO controller from RDA Micro. Signed-off-by: Manivannan Sadhasivam --- drivers/gpio/Kconfig | 8 + drivers/gpio/Makefile | 1 + drivers/gpio/gpio-rda.c | 334 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 343 insertions(+) create mode 100644 drivers/gpio/gpio-rda.c -- 2.17.1 diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index 38e096e6925f..71826e61fdb3 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -435,6 +435,14 @@ config GPIO_RCAR help Say yes here to support GPIO on Renesas R-Car SoCs. +config GPIO_RDA + bool "RDA Micro GPIO controller support" + depends on ARCH_RDA || COMPILE_TEST + depends on OF_GPIO + select GPIOLIB_IRQCHIP + help + Say Y here to support RDA Micro GPIO controller. + config GPIO_REG bool help diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile index d2fd19c15bae..5c68c9a48fa3 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -115,6 +115,7 @@ obj-$(CONFIG_GPIO_PXA) += gpio-pxa.o obj-$(CONFIG_GPIO_RASPBERRYPI_EXP) += gpio-raspberrypi-exp.o obj-$(CONFIG_GPIO_RC5T583) += gpio-rc5t583.o obj-$(CONFIG_GPIO_RCAR) += gpio-rcar.o +obj-$(CONFIG_GPIO_RDA) += gpio-rda.o obj-$(CONFIG_GPIO_RDC321X) += gpio-rdc321x.o obj-$(CONFIG_GPIO_REG) += gpio-reg.o obj-$(CONFIG_ARCH_SA1100) += gpio-sa1100.o diff --git a/drivers/gpio/gpio-rda.c b/drivers/gpio/gpio-rda.c new file mode 100644 index 000000000000..5a4adeb25f72 --- /dev/null +++ b/drivers/gpio/gpio-rda.c @@ -0,0 +1,334 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * RDA Micro GPIO driver + * + * Copyright (C) 2012 RDA Micro Inc. + * Copyright (C) 2019 Manivannan Sadhasivam + */ + +#include +#include +#include +#include +#include +#include +#include + +#define RDA_GPIO_OEN_VAL 0x00 +#define RDA_GPIO_OEN_SET_OUT 0x04 +#define RDA_GPIO_OEN_SET_IN 0x08 +#define RDA_GPIO_VAL 0x0c +#define RDA_GPIO_SET 0x10 +#define RDA_GPIO_CLR 0x14 +#define RDA_GPIO_INT_CTRL_SET 0x18 +#define RDA_GPIO_INT_CTRL_CLR 0x1c +#define RDA_GPIO_INT_CLR 0x20 +#define RDA_GPIO_INT_STATUS 0x24 + +#define RDA_GPIO_IRQ_RISE_SHIFT 0 +#define RDA_GPIO_IRQ_FALL_SHIFT 8 +#define RDA_GPIO_DEBOUCE_SHIFT 16 +#define RDA_GPIO_LEVEL_SHIFT 24 + +#define RDA_GPIO_IRQ_MASK 0xff + +/* Each bank consists of 32 GPIOs */ +#define RDA_GPIO_BANK_NR 32 + +struct rda_gpio { + struct gpio_chip chip; + void __iomem *base; + spinlock_t lock; + struct irq_chip irq_chip; + int irq; +}; + +static void rda_gpio_update(struct gpio_chip *chip, unsigned int offset, + u16 reg, int val) +{ + struct rda_gpio *rda_gpio = gpiochip_get_data(chip); + void __iomem *base = rda_gpio->base; + unsigned long flags; + u32 tmp; + + spin_lock_irqsave(&rda_gpio->lock, flags); + tmp = readl_relaxed(base + reg); + + if (val) + tmp |= BIT(offset); + else + tmp &= ~BIT(offset); + + writel_relaxed(tmp, base + reg); + spin_unlock_irqrestore(&rda_gpio->lock, flags); +} + +static int rda_gpio_request(struct gpio_chip *chip, unsigned int offset) +{ + /* Not supported currently */ + return 0; +} + +static void rda_gpio_free(struct gpio_chip *chip, unsigned int offset) +{ + /* Not supported currently */ +} + +static int rda_gpio_direction_input(struct gpio_chip *chip, unsigned int offset) +{ + rda_gpio_update(chip, offset, RDA_GPIO_OEN_SET_IN, 1); + + return 0; +} + +static int rda_gpio_direction_output(struct gpio_chip *chip, + unsigned int offset, int value) +{ + rda_gpio_update(chip, offset, RDA_GPIO_OEN_SET_OUT, 1); + + return 0; +} + +static int rda_gpio_get(struct gpio_chip *chip, unsigned int offset) +{ + struct rda_gpio *rda_gpio = gpiochip_get_data(chip); + void __iomem *base = rda_gpio->base; + + if (readl_relaxed(base + RDA_GPIO_OEN_VAL) & BIT(offset)) + return !!(readl_relaxed(base + RDA_GPIO_VAL) & BIT(offset)); + else + return !!(readl_relaxed(base + RDA_GPIO_SET) & BIT(offset)); +} + +static void rda_gpio_set(struct gpio_chip *chip, unsigned int offset, int value) +{ + if (value) + rda_gpio_update(chip, offset, RDA_GPIO_SET, 1); + else + rda_gpio_update(chip, offset, RDA_GPIO_CLR, 1); +} + +static void rda_gpio_irq_mask(struct irq_data *data) +{ + struct gpio_chip *chip = irq_data_get_irq_chip_data(data); + struct rda_gpio *rda_gpio = gpiochip_get_data(chip); + void __iomem *base = rda_gpio->base; + u32 offset = irqd_to_hwirq(data); + u32 value; + + value = BIT(offset) << RDA_GPIO_IRQ_RISE_SHIFT; + value |= BIT(offset) << RDA_GPIO_IRQ_FALL_SHIFT; + + writel_relaxed(value, base + RDA_GPIO_INT_CTRL_CLR); +} + +static void rda_gpio_irq_ack(struct irq_data *data) +{ + struct gpio_chip *chip = irq_data_get_irq_chip_data(data); + u32 offset = irqd_to_hwirq(data); + + rda_gpio_update(chip, offset, RDA_GPIO_INT_CLR, 1); +} + +static int rda_gpio_set_irq(struct gpio_chip *chip, u32 offset, + unsigned int flow_type) +{ + struct rda_gpio *rda_gpio = gpiochip_get_data(chip); + void __iomem *base = rda_gpio->base; + u32 value; + + switch (flow_type) { + case IRQ_TYPE_EDGE_RISING: + /* Set rising edge trigger */ + value = BIT(offset) << RDA_GPIO_IRQ_RISE_SHIFT; + writel_relaxed(value, base + RDA_GPIO_INT_CTRL_SET); + + /* Switch to edge trigger interrupt */ + value = BIT(offset) << RDA_GPIO_LEVEL_SHIFT; + writel_relaxed(value, base + RDA_GPIO_INT_CTRL_CLR); + break; + + case IRQ_TYPE_EDGE_FALLING: + /* Set falling edge trigger */ + value = BIT(offset) << RDA_GPIO_IRQ_FALL_SHIFT; + writel_relaxed(value, base + RDA_GPIO_INT_CTRL_SET); + + /* Switch to edge trigger interrupt */ + value = BIT(offset) << RDA_GPIO_LEVEL_SHIFT; + writel_relaxed(value, base + RDA_GPIO_INT_CTRL_CLR); + break; + + case IRQ_TYPE_EDGE_BOTH: + /* Set both edge trigger */ + value = BIT(offset) << RDA_GPIO_IRQ_RISE_SHIFT; + value |= BIT(offset) << RDA_GPIO_IRQ_FALL_SHIFT; + writel_relaxed(value, base + RDA_GPIO_INT_CTRL_SET); + + /* Switch to edge trigger interrupt */ + value = BIT(offset) << RDA_GPIO_LEVEL_SHIFT; + writel_relaxed(value, base + RDA_GPIO_INT_CTRL_CLR); + break; + + case IRQ_TYPE_LEVEL_HIGH: + /* Set high level trigger */ + value = BIT(offset) << RDA_GPIO_IRQ_RISE_SHIFT; + + /* Switch to level trigger interrupt */ + value |= BIT(offset) << RDA_GPIO_LEVEL_SHIFT; + writel_relaxed(value, base + RDA_GPIO_INT_CTRL_SET); + break; + + case IRQ_TYPE_LEVEL_LOW: + /* Set low level trigger */ + value = BIT(offset) << RDA_GPIO_IRQ_FALL_SHIFT; + + /* Switch to level trigger interrupt */ + value |= BIT(offset) << RDA_GPIO_LEVEL_SHIFT; + writel_relaxed(value, base + RDA_GPIO_INT_CTRL_SET); + break; + + default: + return -EINVAL; + } + + return 0; +} + +static void rda_gpio_irq_unmask(struct irq_data *data) +{ + struct gpio_chip *chip = irq_data_get_irq_chip_data(data); + u32 offset = irqd_to_hwirq(data); + u32 trigger = irqd_get_trigger_type(data); + + rda_gpio_set_irq(chip, offset, trigger); +} + +static int rda_gpio_irq_set_type(struct irq_data *data, unsigned int flow_type) +{ + struct gpio_chip *chip = irq_data_get_irq_chip_data(data); + u32 offset = irqd_to_hwirq(data); + int ret; + + ret = rda_gpio_set_irq(chip, offset, flow_type); + if (ret) + return ret; + + if (flow_type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH)) + irq_set_handler_locked(data, handle_level_irq); + else if (flow_type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING)) + irq_set_handler_locked(data, handle_edge_irq); + + return 0; +} + +static void rda_gpio_irq_handler(struct irq_desc *desc) +{ + struct gpio_chip *chip = irq_desc_get_handler_data(desc); + struct irq_chip *ic = irq_desc_get_chip(desc); + struct rda_gpio *rda_gpio = gpiochip_get_data(chip); + unsigned long status; + u32 n, girq; + + chained_irq_enter(ic, desc); + + status = readl_relaxed(rda_gpio->base + RDA_GPIO_INT_STATUS); + /* Only lower 8 bits are capable of generating interrupts */ + status &= RDA_GPIO_IRQ_MASK; + + for_each_set_bit(n, &status, RDA_GPIO_BANK_NR) { + girq = irq_find_mapping(chip->irq.domain, n); + generic_handle_irq(girq); + } + + chained_irq_exit(ic, desc); +} + +static int rda_gpio_probe(struct platform_device *pdev) +{ + struct device_node *np = pdev->dev.of_node; + struct gpio_irq_chip *irq; + struct rda_gpio *rda_gpio; + u32 ngpios; + int ret; + + rda_gpio = devm_kzalloc(&pdev->dev, sizeof(*rda_gpio), GFP_KERNEL); + if (!rda_gpio) + return -ENOMEM; + + ret = of_property_read_u32(np, "ngpios", &ngpios); + if (ret < 0) + return ret; + + /* + * Not all ports have interrupt capability. For instance, on + * RDA8810PL, GPIOC doesn't support interrupt. So we must handle + * those also. + */ + rda_gpio->irq = platform_get_irq(pdev, 0); + + rda_gpio->base = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(rda_gpio->base)) + return PTR_ERR(rda_gpio->base); + + spin_lock_init(&rda_gpio->lock); + + rda_gpio->chip.label = dev_name(&pdev->dev); + rda_gpio->chip.ngpio = ngpios; + rda_gpio->chip.base = -1; + rda_gpio->chip.parent = &pdev->dev; + rda_gpio->chip.of_node = np; + rda_gpio->chip.request = rda_gpio_request; + rda_gpio->chip.free = rda_gpio_free; + rda_gpio->chip.get = rda_gpio_get; + rda_gpio->chip.set = rda_gpio_set; + rda_gpio->chip.direction_input = rda_gpio_direction_input; + rda_gpio->chip.direction_output = rda_gpio_direction_output; + + if (rda_gpio->irq >= 0) { + rda_gpio->irq_chip.name = "rda-gpio", + rda_gpio->irq_chip.irq_ack = rda_gpio_irq_ack, + rda_gpio->irq_chip.irq_mask = rda_gpio_irq_mask, + rda_gpio->irq_chip.irq_unmask = rda_gpio_irq_unmask, + rda_gpio->irq_chip.irq_set_type = rda_gpio_irq_set_type, + rda_gpio->irq_chip.flags = IRQCHIP_SKIP_SET_WAKE, + + irq = &rda_gpio->chip.irq; + irq->chip = &rda_gpio->irq_chip; + irq->handler = handle_bad_irq; + irq->default_type = IRQ_TYPE_NONE; + irq->parent_handler = rda_gpio_irq_handler; + irq->parent_handler_data = rda_gpio; + irq->num_parents = 1; + irq->parents = &rda_gpio->irq; + } + + ret = devm_gpiochip_add_data(&pdev->dev, &rda_gpio->chip, rda_gpio); + if (ret < 0) { + dev_err(&pdev->dev, "Could not register gpiochip %d\n", ret); + return ret; + } + + platform_set_drvdata(pdev, rda_gpio); + + return 0; +} + +static const struct of_device_id rda_gpio_of_match[] = { + { .compatible = "rda,8810pl-gpio", }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, rda_gpio_of_match); + +static struct platform_driver rda_gpio_driver = { + .probe = rda_gpio_probe, + .driver = { + .name = "rda-gpio", + .of_match_table = rda_gpio_of_match, + }, +}; + +module_platform_driver_probe(rda_gpio_driver, rda_gpio_probe); + +MODULE_DESCRIPTION("RDA Micro GPIO driver"); +MODULE_AUTHOR("Manivannan Sadhasivam "); +MODULE_LICENSE("GPL v2");