From patchwork Fri Sep 1 18:57:34 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thierry Reding X-Patchwork-Id: 808956 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; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="rrSzEnuy"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3xkT7R3bQsz9sPm for ; Sat, 2 Sep 2017 04:59:31 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752664AbdIAS7R (ORCPT ); Fri, 1 Sep 2017 14:59:17 -0400 Received: from mail-wm0-f67.google.com ([74.125.82.67]:33769 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752549AbdIAS6F (ORCPT ); Fri, 1 Sep 2017 14:58:05 -0400 Received: by mail-wm0-f67.google.com with SMTP id 187so742196wmn.0; Fri, 01 Sep 2017 11:58:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=tSuOz+0X/Nt4eZ9r+H9vcDH+vQ1IsOrt+xO/5ascoCw=; b=rrSzEnuyHlZjkPmzvX8pCIWuSev0l6/Px4eFP/gz1eKPj/z1YKnpVfr+j/cr+gbGn/ GdQm8ygWNT0rySOoH4NuQoylAM8o77NOLa/O9xLklpP9a2hwhLxJvRsIEU4eVW/HHo+B Hp/PA2eAT/XnO4Oo1HnHrVivYHDrolo2+2v/Rvqv1YbOj1YQZPauz0Bm14BLGy4XffO0 Jrhsf/OlXIMqAfd+vy/MaWVzkMOQoK1aN2lNO/4rnxGe6q4buolRek6Ydlt1OCB4rd36 LoEhQKM4LMnm9WyOlaR5B/EtuzZD2AUwQEzpH+akmZcyZiKyLJwG0pXCn5ukHoJI7g8J on2g== 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=tSuOz+0X/Nt4eZ9r+H9vcDH+vQ1IsOrt+xO/5ascoCw=; b=GoigUByGxVgPfotQtHdpmOff2N6aM/J5YaWQAW5KG17O+Xq1rZ/nD/YJcTVNXM9usJ 9kBBJRJ4yvG5x6bEdDG79vJHSudEXsIabY36V311s/wFSTYTiNH9j1cZdKhwdjRWlxUs WKtIz4+JHLh3uWicfSo0AlDTAI/WQ1XM5sB/gtNk+++bd5YEXJwtQKnWCq1mwgPVYXjQ wVGJ1DhfDhWySPn2qk17V+LYtyPNFv97k8362tpNwKteJtnUEhIuNf95cCoshCnFipqs o3Jsh8CbwXNpz9H+X2BKlz5/rlLgF+r4tfA/KAD/RUTqtY43SnM2l6/Wu3KInMNnE7Zo RxFA== X-Gm-Message-State: AHPjjUjPCtPrg8lOg3A6EaQ77KmSFTztrZH7oNUdR91Wl+VVPRgJ9Lkw Ov6Ly0rUi58du739 X-Google-Smtp-Source: ADKCNb5G2u5Mj/PNvFEcxwvpybuskWTmXxaLXMo/ISFIR7X+3516YbKK4N6Q+OVtKvmXobYTJSWF7g== X-Received: by 10.28.31.214 with SMTP id f205mr1144194wmf.139.1504292283892; Fri, 01 Sep 2017 11:58:03 -0700 (PDT) Received: from localhost (p200300E41BD6D60076D02BFFFE273F51.dip0.t-ipconnect.de. [2003:e4:1bd6:d600:76d0:2bff:fe27:3f51]) by smtp.gmail.com with ESMTPSA id k18sm708398wmd.19.2017.09.01.11.58.02 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 01 Sep 2017 11:58:02 -0700 (PDT) From: Thierry Reding To: Linus Walleij Cc: Jonathan Hunter , linux-gpio@vger.kernel.org, linux-tegra@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 14/16] gpio: Add support for banked GPIO controllers Date: Fri, 1 Sep 2017 20:57:34 +0200 Message-Id: <20170901185736.28051-15-thierry.reding@gmail.com> X-Mailer: git-send-email 2.13.3 In-Reply-To: <20170901185736.28051-1-thierry.reding@gmail.com> References: <20170901185736.28051-1-thierry.reding@gmail.com> Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Thierry Reding Some GPIO controllers are subdivided into multiple logical blocks called banks (or ports). This is often caused by the design assigning separate resources, such as register regions or interrupts, to each bank, or some set of banks. This commit adds support for describing controllers that have such a banked design and provides common code for dealing with them. Signed-off-by: Thierry Reding --- drivers/gpio/gpiolib-of.c | 101 +++++++++++++++++++++++++++++++++++++++++ drivers/gpio/gpiolib.c | 98 ++++++++++++++++++++++++++++++++++++++++ include/linux/gpio/driver.h | 108 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/of_gpio.h | 10 ++++ 4 files changed, 317 insertions(+) diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c index bfcd20699ec8..8b89f03076c2 100644 --- a/drivers/gpio/gpiolib-of.c +++ b/drivers/gpio/gpiolib-of.c @@ -310,6 +310,107 @@ int of_gpio_simple_xlate(struct gpio_chip *gc, EXPORT_SYMBOL(of_gpio_simple_xlate); /** + * gpio_banked_irq_domain_xlate - decode an IRQ specifier for banked chips + * @domain: IRQ domain + * @np: device tree node + * @spec: IRQ specifier + * @size: number of cells in IRQ specifier + * @hwirq: return location for the hardware IRQ number + * @type: return location for the IRQ type + * + * Translates the IRQ specifier found in device tree into a hardware IRQ + * number and an interrupt type. + * + * Returns: + * 0 on success or a negative error code on failure. + */ +int gpio_banked_irq_domain_xlate(struct irq_domain *domain, + struct device_node *np, + const u32 *spec, unsigned int size, + unsigned long *hwirq, + unsigned int *type) +{ + struct gpio_chip *gc = domain->host_data; + unsigned int bank, pin, i, offset = 0; + + if (size < 2) + return -EINVAL; + + bank = (spec[0] >> gc->of_gpio_bank_mask) & gc->of_gpio_bank_shift; + pin = (spec[0] >> gc->of_gpio_pin_mask) & gc->of_gpio_pin_shift; + + if (bank >= gc->num_banks) { + dev_err(gc->parent, "invalid bank number: %u\n", bank); + return -EINVAL; + } + + if (pin >= gc->banks[bank]->num_pins) { + dev_err(gc->parent, "invalid pin number: %u\n", pin); + return -EINVAL; + } + + for (i = 0; i < bank; i++) + offset += gc->banks[i]->num_pins; + + *type = spec[1] & IRQ_TYPE_SENSE_MASK; + *hwirq = offset + pin; + + return 0; +} +EXPORT_SYMBOL_GPL(gpio_banked_irq_domain_xlate); + +/** + * of_gpio_banked_xlate - translate GPIO specifier to a GPIO number and flags + * @gc: GPIO chip + * @gpiospec: GPIO specifier + * @flags: return location for flags parsed from the GPIO specifier + * + * This translation function takes into account multiple banks that can make + * up a single controller. Each bank can contain one or more pins. A single + * cell in the specifier is used to represent a (bank, pin) pair, with each + * encoded in different fields. The &gpio_chip.of_gpio_bank_shift and + * &gpio_chip.of_gpio_bank_mask fields, and &gpio_chip.of_gpio_pin_shift and + * &gpio_chip.of_gpio_pin_mask are used to specify the encoding. + * + * Returns: + * The chip-relative index of the pin given by the GPIO specifier. + */ +int of_gpio_banked_xlate(struct gpio_chip *gc, + const struct of_phandle_args *gpiospec, u32 *flags) +{ + unsigned int offset = 0, bank, pin, i; + const u32 *spec = gpiospec->args; + + if (WARN_ON(gc->of_gpio_n_cells < 2)) + return -EINVAL; + + if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) + return -EINVAL; + + bank = (spec[0] >> gc->of_gpio_bank_shift) & gc->of_gpio_bank_mask; + pin = (spec[0] >> gc->of_gpio_pin_shift) & gc->of_gpio_pin_mask; + + if (bank >= gc->num_banks) { + dev_err(gc->parent, "invalid bank number: %u\n", bank); + return -EINVAL; + } + + if (pin >= gc->banks[bank]->num_pins) { + dev_err(gc->parent, "invalid pin number: %u\n", pin); + return -EINVAL; + } + + for (i = 0; i < bank; i++) + offset += gc->banks[i]->num_pins; + + if (flags) + *flags = spec[1]; + + return offset + pin; +} +EXPORT_SYMBOL(of_gpio_banked_xlate); + +/** * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank) * @np: device node of the GPIO chip * @mm_gc: pointer to the of_mm_gpio_chip allocated structure diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 7a16dd37bd3d..306c3a13e814 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -1776,6 +1776,57 @@ static int gpiochip_add_irqchip(struct gpio_chip *gpiochip) gpiochip->to_irq = gpiochip_to_irq; gpiochip->irq.default_type = type; + if (gpiochip->num_banks > 0 && !gpiochip->irq.map) { + struct gpio_irq_chip *irq = &gpiochip->irq; + unsigned int i, j, offset = 0; + + if (!irq->parents) { + chip_err(gpiochip, "no parent interrupts defined\n"); + return -EINVAL; + } + + irq->map = devm_kcalloc(gpiochip->parent, gpiochip->ngpio, + sizeof(*irq->map), GFP_KERNEL); + if (!irq->map) + return -ENOMEM; + + for (i = 0; i < gpiochip->num_banks; i++) { + struct gpio_bank *bank = gpiochip->banks[i]; + unsigned int parent = bank->parent_irq; + + for (j = 0; j < bank->num_pins; j++) { + if (parent >= irq->num_parents) { + chip_err(gpiochip, + "invalid parent interrupt: %u\n", + parent); + return -EINVAL; + } + + irq->map[offset + j] = irq->parents[parent]; + } + + offset += bank->num_pins; + } + } + + if (gpiochip->num_banks > 0) { + unsigned int i; + + for (i = 0; i < gpiochip->num_banks; i++) { + struct gpio_bank *bank = gpiochip->banks[i]; + unsigned int num_pins = bank->num_pins; + + bank->pending = devm_kcalloc(gpiochip->parent, + BITS_TO_LONGS(num_pins), + sizeof(unsigned long), + GFP_KERNEL); + if (!bank->pending) + return -ENOMEM; + + bank->chip = gpiochip; + } + } + if (gpiochip->irq.domain_ops) ops = gpiochip->irq.domain_ops; else @@ -1984,6 +2035,53 @@ int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip, } EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key); +/** + * gpio_irq_chip_banked_handler - interrupt handler for banked IRQ chips + * @desc: IRQ descriptor + * + * Drivers can use this interrupt handler for banked GPIO controllers. This + * implementation iterates over all banks and handles pending interrupts of + * the pins associated with the bank. + * + * This function uses driver specific parts, split out into the + * &gpio_chip.update_bank() callback, to retrieves the interrupt pending + * state for each of the GPIOs exposed by the given bank. + */ +void gpio_irq_chip_banked_handler(struct irq_desc *desc) +{ + struct gpio_chip *gpio = irq_desc_get_handler_data(desc); + struct irq_chip *irq = irq_desc_get_chip(desc); + unsigned int parent = irq_desc_get_irq(desc); + struct gpio_irq_chip *chip = &gpio->irq; + unsigned int i, offset = 0; + + chained_irq_enter(irq, desc); + + for (i = 0; i < gpio->num_banks; i++) { + struct gpio_bank *bank = gpio->banks[i]; + unsigned int pin, virq; + + if (parent != chip->parents[bank->parent_irq]) + goto skip; + + chip->update_bank(bank); + + for_each_set_bit(pin, bank->pending, bank->num_pins) { + virq = irq_find_mapping(chip->domain, offset + pin); + if (WARN_ON(virq == 0)) + continue; + + generic_handle_irq(virq); + } + +skip: + offset += bank->num_pins; + } + + chained_irq_exit(irq, desc); +} +EXPORT_SYMBOL_GPL(gpio_irq_chip_banked_handler); + #else /* CONFIG_GPIOLIB_IRQCHIP */ static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip) diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h index c453e0716228..ecbeb50f8801 100644 --- a/include/linux/gpio/driver.h +++ b/include/linux/gpio/driver.h @@ -19,6 +19,47 @@ struct module; #ifdef CONFIG_GPIOLIB +/** + * struct gpio_bank - GPIO bank + * + * A GPIO bank, sometimes also referred to as port, represents a subset of the + * pins of a GPIO controller. The separation into banks is often caused by the + * sharing of one or more resource (register region, interrupt, ...) for each + * of the pins in the bank. + * + * In many cases the banking is transparent, but when it is not, GPIO drivers + * can use this code, along with some supporting fields in &struct gpio_chip. + */ +struct gpio_bank { + /** + * @chip: + * + * A pointer to the &struct gpio_chip that this bank belongs to. + */ + struct gpio_chip *chip; + + /** + * @parent_irq: + * + * The interrupt parent for this bank. + */ + unsigned int parent_irq; + + /** + * @num_pins: + * + * The number of pins provided by this bank. + */ + unsigned int num_pins; + + /** + * @pending: + * + * Current interrupt state of each pin in the bank. + */ + unsigned long *pending; +}; + #ifdef CONFIG_GPIOLIB_IRQCHIP /** * struct gpio_irq_chip - GPIO interrupt controller @@ -136,6 +177,15 @@ struct gpio_irq_chip { * in IRQ domain of the chip. */ unsigned long *valid_mask; + + /** + * @update_bank: + * + * Callback used by banked interrupt controllers. The driver updates + * the &gpio_bank.pending field of the given @bank with the current + * status for each of the GPIOs that it provides. + */ + void (*update_bank)(struct gpio_bank *bank); }; static inline struct gpio_irq_chip *to_gpio_irq_chip(struct irq_chip *chip) @@ -281,6 +331,24 @@ struct gpio_chip { struct gpio_irq_chip irq; #endif + /** + * @banks: + * + * If a GPIO controller is subdivided into multiple banks, the driver + * can use this field to store information about these banks. + * + * Note that the driver owns this field and the core will not modify + * it, only reference it. + */ + struct gpio_bank **banks; + + /** + * @num_banks: + * + * The number of banks described in @banks. + */ + unsigned int num_banks; + #if defined(CONFIG_OF_GPIO) /* * If CONFIG_OF is enabled, then all GPIO controllers described in the @@ -302,6 +370,38 @@ struct gpio_chip { unsigned int of_gpio_n_cells; /** + * @of_gpio_bank_shift: + * + * The offset of the field in the cell denoting the bank number of a + * specified GPIO. + */ + unsigned int of_gpio_bank_shift; + + /** + * @of_gpio_bank_mask: + * + * The mask of the field in the cell denoting the bank number of a + * specified GPIO. + */ + unsigned int of_gpio_bank_mask; + + /** + * @of_gpio_pin_shift: + * + * The offset of the field in the cell denoting the pin number of a + * specified GPIO within its bank. + */ + unsigned int of_gpio_pin_shift; + + /** + * @of_gpio_pin_mask: + * + * The mask of the field in the cell denoting the pin number of a + * specified GPIO within its bank. + */ + unsigned int of_gpio_pin_mask; + + /** * @of_xlate: * * Callback to translate a device tree GPIO specifier into a chip- @@ -374,6 +474,12 @@ int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hwirq); void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq); +int gpio_banked_irq_domain_xlate(struct irq_domain *domain, + struct device_node *np, + const u32 *spec, unsigned int size, + unsigned long *hwirq, + unsigned int *type); + void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip, struct irq_chip *irqchip, unsigned int parent_irq, @@ -391,6 +497,8 @@ int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip, bool nested, struct lock_class_key *lock_key); +void gpio_irq_chip_banked_handler(struct irq_desc *desc); + #ifdef CONFIG_LOCKDEP /* diff --git a/include/linux/of_gpio.h b/include/linux/of_gpio.h index ca10f43564de..f85414dc31f4 100644 --- a/include/linux/of_gpio.h +++ b/include/linux/of_gpio.h @@ -66,6 +66,9 @@ extern void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc); extern int of_gpio_simple_xlate(struct gpio_chip *gc, const struct of_phandle_args *gpiospec, u32 *flags); +extern int of_gpio_banked_xlate(struct gpio_chip *gc, + const struct of_phandle_args *gpiospec, + u32 *flags); #else /* CONFIG_OF_GPIO */ @@ -86,6 +89,13 @@ static inline int of_gpio_simple_xlate(struct gpio_chip *gc, return -ENOSYS; } +static inline int of_gpio_banked_xlate(struct gpio_chip *gc, + const struct of_phandle_args *gpiospec, + u32 *flags) +{ + return -ENOSYS; +} + #endif /* CONFIG_OF_GPIO */ /**