From patchwork Tue Jan 2 13:19:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baruch Siach X-Patchwork-Id: 10140521 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0A047601A1 for ; Tue, 2 Jan 2018 13:27:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F2F49203B9 for ; Tue, 2 Jan 2018 13:27:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E7DFE285B7; Tue, 2 Jan 2018 13:27:18 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 317A5203B9 for ; Tue, 2 Jan 2018 13:27:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=1YB+snNNfcZtTbkR2wMdY8aPnGe4d5xSqpHBhG0sEX4=; b=dkAcS/bY6u6L2Or+w42r94vYEs S9cqzU1bTsRSQ15n3yUmoWTFJXRikC8f4JjN1dGhiDNWIRPuI0e1kWXuHY8wdccJ5XMH7cHICkDvS ndvmw9Nx8gg3OdKey4beoqtYneAh105PkAgvcsU8ovS90+YgDhsl8Ae6ct5UR4QZrZ9xuRPMNlHxe oGFaM+ZMa94lNHkVRX0Tl8TH0afNqbxTye4SAsgxO71nUIOo3oIIX7U9N+gZRk0vnkkqvgNjRrdvj 5/nY0+q7ReE19uON6VMB1xX+FQvaRRuToNQIMGpMQe1xfo7oH3MoWWhwXmC9e7KucaksXYtUNjjiF gLZDbmqg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.89 #1 (Red Hat Linux)) id 1eWMb3-0000mG-9Q; Tue, 02 Jan 2018 13:27:13 +0000 Received: from guitar.tcltek.co.il ([192.115.133.116] helo=mx.tkos.co.il) by bombadil.infradead.org with esmtps (Exim 4.89 #1 (Red Hat Linux)) id 1eWMWB-0005wn-IZ; Tue, 02 Jan 2018 13:22:17 +0000 Received: from tarshish.tkos.co.il (unknown [10.0.8.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx.tkos.co.il (Postfix) with ESMTPSA id A31A64409ED; Tue, 2 Jan 2018 15:21:54 +0200 (IST) From: Baruch Siach To: Dave Stevenson , Eric Anholt , Stefan Wahren , Linus Walleij Subject: [PATCH 3/4] bcm2835-gpio-exp: Driver for GPIO expander via mailbox service Date: Tue, 2 Jan 2018 15:19:20 +0200 Message-Id: X-Mailer: git-send-email 2.15.1 In-Reply-To: References: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180102_052212_176645_3C603B1F X-CRM114-Status: GOOD ( 19.09 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-gpio@vger.kernel.org, Baruch Siach , linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, devicetree@vger.kernel.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Dave Stevenson Pi3 and Compute Module 3 have a GPIO expander that the VPU communicates with. There is a mailbox service that now allows control of this expander, so add a kernel driver that can make use of it. Signed-off-by: Dave Stevenson Signed-off-by: Baruch Siach --- drivers/gpio/Kconfig | 7 ++ drivers/gpio/Makefile | 1 + drivers/gpio/gpio-bcm-exp.c | 254 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 262 insertions(+) create mode 100644 drivers/gpio/gpio-bcm-exp.c diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index d6a8e851ad13..e2aab64ea772 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -128,6 +128,13 @@ config GPIO_AXP209 help Say yes to enable GPIO support for the AXP209 PMIC +config GPIO_BCM_EXP + bool "Broadcom Exp GPIO" + depends on OF_GPIO && RASPBERRYPI_FIRMWARE && (ARCH_BCM2835 || COMPILE_TEST) + help + Turn on GPIO support for Broadcom chips using the firmware mailbox + to communicate with VideoCore on BCM283x chips. + config GPIO_BCM_KONA bool "Broadcom Kona GPIO" depends on OF_GPIO && (ARCH_BCM_MOBILE || COMPILE_TEST) diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile index 4bc24febb889..c5f481b1d53c 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -33,6 +33,7 @@ obj-$(CONFIG_GPIO_ARIZONA) += gpio-arizona.o obj-$(CONFIG_GPIO_ATH79) += gpio-ath79.o obj-$(CONFIG_GPIO_ASPEED) += gpio-aspeed.o obj-$(CONFIG_GPIO_AXP209) += gpio-axp209.o +obj-$(CONFIG_GPIO_BCM_EXP) += gpio-bcm-exp.o obj-$(CONFIG_GPIO_BCM_KONA) += gpio-bcm-kona.o obj-$(CONFIG_GPIO_BD9571MWV) += gpio-bd9571mwv.o obj-$(CONFIG_GPIO_BRCMSTB) += gpio-brcmstb.o diff --git a/drivers/gpio/gpio-bcm-exp.c b/drivers/gpio/gpio-bcm-exp.c new file mode 100644 index 000000000000..d68adafaee4a --- /dev/null +++ b/drivers/gpio/gpio-bcm-exp.c @@ -0,0 +1,254 @@ +/* + * Broadcom expander GPIO driver + * + * Uses the firmware mailbox service to communicate with the + * GPIO expander on the VPU. + * + * Copyright (C) 2017 Raspberry Pi Trading Ltd. + * + * Author: Dave Stevenson + * Based on gpio-bcm-virt.c by Dom Cobley + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#include +#include +#include +#include +#include +#include + +#define MODULE_NAME "brcmexp-gpio" +#define NUM_GPIO 8 + +struct brcmexp_gpio { + struct gpio_chip gc; + struct device *dev; + struct rpi_firmware *fw; +}; + +struct gpio_set_config { + u32 gpio, direction, polarity, term_en, term_pull_up, state; +}; + +struct gpio_get_config { + u32 gpio, direction, polarity, term_en, term_pull_up; +}; + +struct gpio_get_set_state { + u32 gpio, state; +}; + +static int brcmexp_gpio_get_polarity(struct gpio_chip *gc, unsigned int off) +{ + struct brcmexp_gpio *gpio; + struct gpio_get_config get; + int ret; + + gpio = container_of(gc, struct brcmexp_gpio, gc); + + get.gpio = off + gpio->gc.base; /* GPIO to update */ + + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_GET_GPIO_CONFIG, + &get, sizeof(get)); + if (ret) { + dev_err(gpio->dev, + "Failed to get GPIO %u config (%d)\n", off, ret); + return ret; + } + return get.polarity; +} + +static int brcmexp_gpio_dir_in(struct gpio_chip *gc, unsigned int off) +{ + struct brcmexp_gpio *gpio; + struct gpio_set_config set_in; + int ret; + + gpio = container_of(gc, struct brcmexp_gpio, gc); + + set_in.gpio = off + gpio->gc.base; /* GPIO to update */ + set_in.direction = 0; /* Input */ + set_in.polarity = brcmexp_gpio_get_polarity(gc, off); + /* Retain existing setting */ + set_in.term_en = 0; /* termination disabled */ + set_in.term_pull_up = 0; /* n/a as termination disabled */ + set_in.state = 0; /* n/a as configured as an input */ + + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_SET_GPIO_CONFIG, + &set_in, sizeof(set_in)); + if (ret) { + dev_err(gpio->dev, + "Failed to set GPIO %u to input (%d)\n", + off, ret); + return ret; + } + return 0; +} + +static int brcmexp_gpio_dir_out(struct gpio_chip *gc, unsigned int off, int val) +{ + struct brcmexp_gpio *gpio; + struct gpio_set_config set_out; + int ret; + + gpio = container_of(gc, struct brcmexp_gpio, gc); + + set_out.gpio = off + gpio->gc.base; /* GPIO to update */ + set_out.direction = 1; /* Output */ + set_out.polarity = brcmexp_gpio_get_polarity(gc, off); + /* Retain existing setting */ + set_out.term_en = 0; /* n/a as an output */ + set_out.term_pull_up = 0; /* n/a as termination disabled */ + set_out.state = val; /* Output state */ + + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_SET_GPIO_CONFIG, + &set_out, sizeof(set_out)); + if (ret) { + dev_err(gpio->dev, + "Failed to set GPIO %u to output (%d)\n", off, ret); + return ret; + } + return 0; +} + +static int brcmexp_gpio_get_direction(struct gpio_chip *gc, unsigned int off) +{ + struct brcmexp_gpio *gpio; + struct gpio_get_config get; + int ret; + + gpio = container_of(gc, struct brcmexp_gpio, gc); + + get.gpio = off + gpio->gc.base; /* GPIO to update */ + + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_GET_GPIO_CONFIG, + &get, sizeof(get)); + if (ret) { + dev_err(gpio->dev, + "Failed to get GPIO %u config (%d)\n", off, ret); + return ret; + } + return get.direction ? GPIOF_DIR_OUT : GPIOF_DIR_IN; +} + +static int brcmexp_gpio_get(struct gpio_chip *gc, unsigned int off) +{ + struct brcmexp_gpio *gpio; + struct gpio_get_set_state get; + int ret; + + gpio = container_of(gc, struct brcmexp_gpio, gc); + + get.gpio = off + gpio->gc.base; /* GPIO to update */ + get.state = 0; /* storage for returned value */ + + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_GET_GPIO_STATE, + &get, sizeof(get)); + if (ret) { + dev_err(gpio->dev, + "Failed to get GPIO %u state (%d)\n", off, ret); + return ret; + } + return !!get.state; +} + +static void brcmexp_gpio_set(struct gpio_chip *gc, unsigned int off, int val) +{ + struct brcmexp_gpio *gpio; + struct gpio_get_set_state set; + int ret; + + gpio = container_of(gc, struct brcmexp_gpio, gc); + + set.gpio = off + gpio->gc.base; /* GPIO to update */ + set.state = val; /* Output state */ + + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_SET_GPIO_STATE, + &set, sizeof(set)); + if (ret) + dev_err(gpio->dev, + "Failed to set GPIO %u state (%d)\n", off, ret); +} + +static int brcmexp_gpio_probe(struct platform_device *pdev) +{ + int err = 0; + struct device *dev = &pdev->dev; + struct device_node *np = dev->of_node; + struct device_node *fw_node; + struct rpi_firmware *fw; + struct brcmexp_gpio *ucb; + + fw_node = of_parse_phandle(np, "firmware", 0); + if (!fw_node) { + dev_err(dev, "Missing firmware node\n"); + return -ENOENT; + } + + fw = rpi_firmware_get(fw_node); + if (!fw) + return -EPROBE_DEFER; + + ucb = devm_kzalloc(dev, sizeof(*ucb), GFP_KERNEL); + if (!ucb) + return -EINVAL; + + ucb->fw = fw; + ucb->dev = dev; + ucb->gc.label = MODULE_NAME; + ucb->gc.owner = THIS_MODULE; + ucb->gc.of_node = np; + ucb->gc.base = 128; + ucb->gc.ngpio = NUM_GPIO; + + ucb->gc.direction_input = brcmexp_gpio_dir_in; + ucb->gc.direction_output = brcmexp_gpio_dir_out; + ucb->gc.get_direction = brcmexp_gpio_get_direction; + ucb->gc.get = brcmexp_gpio_get; + ucb->gc.set = brcmexp_gpio_set; + ucb->gc.can_sleep = true; + + err = gpiochip_add(&ucb->gc); + if (err) + return err; + + platform_set_drvdata(pdev, ucb); + + return 0; +} + +static int brcmexp_gpio_remove(struct platform_device *pdev) +{ + struct brcmexp_gpio *ucb = platform_get_drvdata(pdev); + + gpiochip_remove(&ucb->gc); + + return 0; +} + +static const struct of_device_id __maybe_unused brcmexp_gpio_ids[] = { + { .compatible = "brcm,bcm2835-expgpio" }, + { } +}; +MODULE_DEVICE_TABLE(of, brcmexp_gpio_ids); + +static struct platform_driver brcmexp_gpio_driver = { + .driver = { + .name = MODULE_NAME, + .owner = THIS_MODULE, + .of_match_table = of_match_ptr(brcmexp_gpio_ids), + }, + .probe = brcmexp_gpio_probe, + .remove = brcmexp_gpio_remove, +}; +module_platform_driver(brcmexp_gpio_driver); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Dave Stevenson "); +MODULE_DESCRIPTION("brcm-exp GPIO driver"); +MODULE_ALIAS("platform:brcmexp-gpio");