1 From 1b6bee75a64ca43404e6bc8ff2fdbe8b1d0a0423 Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Tue, 23 Feb 2016 19:56:04 +0000
4 Subject: [PATCH] bcm2835-virtgpio: Virtual GPIO driver
6 Add a virtual GPIO driver that uses the firmware mailbox interface to
7 request that the VPU toggles LEDs.
9 drivers/gpio/Kconfig | 6 +
10 drivers/gpio/Makefile | 1 +
11 drivers/gpio/gpio-bcm-virt.c | 214 +++++++++++++++++++++++++++++
12 include/soc/bcm2835/raspberrypi-firmware.h | 1 +
13 4 files changed, 222 insertions(+)
14 create mode 100644 drivers/gpio/gpio-bcm-virt.c
16 --- a/drivers/gpio/Kconfig
17 +++ b/drivers/gpio/Kconfig
18 @@ -134,6 +134,12 @@ config GPIO_BCM_KONA
20 Turn on GPIO support for Broadcom "Kona" chips.
23 + bool "Broadcom Virt GPIO"
24 + depends on OF_GPIO && RASPBERRYPI_FIRMWARE && (ARCH_BCM2835 || COMPILE_TEST)
26 + Turn on virtual GPIO support for Broadcom BCM283X chips.
29 tristate "BRCMSTB GPIO support"
30 default y if (ARCH_BRCMSTB || BMIPS_GENERIC)
31 --- a/drivers/gpio/Makefile
32 +++ b/drivers/gpio/Makefile
33 @@ -31,6 +31,7 @@ obj-$(CONFIG_GPIO_ATH79) += gpio-ath79.o
34 obj-$(CONFIG_GPIO_ASPEED) += gpio-aspeed.o
35 obj-$(CONFIG_GPIO_AXP209) += gpio-axp209.o
36 obj-$(CONFIG_GPIO_BCM_KONA) += gpio-bcm-kona.o
37 +obj-$(CONFIG_GPIO_BCM_VIRT) += gpio-bcm-virt.o
38 obj-$(CONFIG_GPIO_BRCMSTB) += gpio-brcmstb.o
39 obj-$(CONFIG_GPIO_BT8XX) += gpio-bt8xx.o
40 obj-$(CONFIG_GPIO_CLPS711X) += gpio-clps711x.o
42 +++ b/drivers/gpio/gpio-bcm-virt.c
45 + * brcmvirt GPIO driver
47 + * Copyright (C) 2012,2013 Dom Cobley <popcornmix@gmail.com>
48 + * Based on gpio-clps711x.c by Alexander Shiyan <shc_work@mail.ru>
50 + * This program is free software; you can redistribute it and/or modify
51 + * it under the terms of the GNU General Public License as published by
52 + * the Free Software Foundation; either version 2 of the License, or
53 + * (at your option) any later version.
56 +#include <linux/err.h>
57 +#include <linux/gpio.h>
58 +#include <linux/module.h>
59 +#include <linux/platform_device.h>
60 +#include <linux/dma-mapping.h>
61 +#include <soc/bcm2835/raspberrypi-firmware.h>
63 +#define MODULE_NAME "brcmvirt-gpio"
66 +struct brcmvirt_gpio {
67 + struct gpio_chip gc;
68 + u32 __iomem *ts_base;
69 + /* two packed 16-bit counts of enabled and disables
70 + Allows host to detect a brief enable that was missed */
71 + u32 enables_disables[NUM_GPIO];
72 + dma_addr_t bus_addr;
75 +static int brcmvirt_gpio_dir_in(struct gpio_chip *gc, unsigned off)
77 + struct brcmvirt_gpio *gpio;
78 + gpio = container_of(gc, struct brcmvirt_gpio, gc);
82 +static int brcmvirt_gpio_dir_out(struct gpio_chip *gc, unsigned off, int val)
84 + struct brcmvirt_gpio *gpio;
85 + gpio = container_of(gc, struct brcmvirt_gpio, gc);
89 +static int brcmvirt_gpio_get(struct gpio_chip *gc, unsigned off)
91 + struct brcmvirt_gpio *gpio;
93 + gpio = container_of(gc, struct brcmvirt_gpio, gc);
94 + v = readl(gpio->ts_base + off);
95 + return (v >> off) & 1;
98 +static void brcmvirt_gpio_set(struct gpio_chip *gc, unsigned off, int val)
100 + struct brcmvirt_gpio *gpio;
101 + u16 enables, disables;
104 + gpio = container_of(gc, struct brcmvirt_gpio, gc);
105 + enables = gpio->enables_disables[off] >> 16;
106 + disables = gpio->enables_disables[off] >> 0;
107 + diff = (s16)(enables - disables);
109 + if ((val && lit) || (!val && !lit))
115 + diff = (s16)(enables - disables);
116 + BUG_ON(diff != 0 && diff != 1);
117 + gpio->enables_disables[off] = (enables << 16) | (disables << 0);
118 + writel(gpio->enables_disables[off], gpio->ts_base + off);
121 +static int brcmvirt_gpio_probe(struct platform_device *pdev)
124 + struct device *dev = &pdev->dev;
125 + struct device_node *np = dev->of_node;
126 + struct device_node *fw_node;
127 + struct rpi_firmware *fw;
128 + struct brcmvirt_gpio *ucb;
131 + fw_node = of_parse_phandle(np, "firmware", 0);
133 + dev_err(dev, "Missing firmware node\n");
137 + fw = rpi_firmware_get(fw_node);
139 + return -EPROBE_DEFER;
141 + ucb = devm_kzalloc(dev, sizeof *ucb, GFP_KERNEL);
147 + ucb->ts_base = dma_zalloc_coherent(dev, PAGE_SIZE, &ucb->bus_addr, GFP_KERNEL);
148 + if (!ucb->ts_base) {
149 + pr_err("[%s]: failed to dma_alloc_coherent(%ld)\n",
150 + __func__, PAGE_SIZE);
155 + gpiovirtbuf = (u32)ucb->bus_addr;
156 + err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_SET_GPIOVIRTBUF,
157 + &gpiovirtbuf, sizeof(gpiovirtbuf));
159 + if (err || gpiovirtbuf != 0) {
160 + dev_warn(dev, "Failed to set gpiovirtbuf, trying to get err:%x\n", err);
161 + dma_free_coherent(dev, PAGE_SIZE, ucb->ts_base, ucb->bus_addr);
166 + if (!ucb->ts_base) {
167 + err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_GET_GPIOVIRTBUF,
168 + &gpiovirtbuf, sizeof(gpiovirtbuf));
171 + dev_err(dev, "Failed to get gpiovirtbuf\n");
175 + if (!gpiovirtbuf) {
176 + dev_err(dev, "No virtgpio buffer\n");
181 + // mmap the physical memory
182 + gpiovirtbuf &= ~0xc0000000;
183 + ucb->ts_base = ioremap(gpiovirtbuf, 4096);
184 + if (ucb->ts_base == NULL) {
185 + dev_err(dev, "Failed to map physical address\n");
191 + ucb->gc.label = MODULE_NAME;
192 + ucb->gc.owner = THIS_MODULE;
193 + //ucb->gc.dev = dev;
194 + ucb->gc.of_node = np;
195 + ucb->gc.base = 100;
196 + ucb->gc.ngpio = NUM_GPIO;
198 + ucb->gc.direction_input = brcmvirt_gpio_dir_in;
199 + ucb->gc.direction_output = brcmvirt_gpio_dir_out;
200 + ucb->gc.get = brcmvirt_gpio_get;
201 + ucb->gc.set = brcmvirt_gpio_set;
202 + ucb->gc.can_sleep = true;
204 + err = gpiochip_add(&ucb->gc);
208 + platform_set_drvdata(pdev, ucb);
212 + if (ucb->bus_addr) {
213 + dma_free_coherent(dev, PAGE_SIZE, ucb->ts_base, ucb->bus_addr);
215 + ucb->ts_base = NULL;
216 + } else if (ucb->ts_base) {
217 + iounmap(ucb->ts_base);
218 + ucb->ts_base = NULL;
223 +static int brcmvirt_gpio_remove(struct platform_device *pdev)
225 + struct device *dev = &pdev->dev;
227 + struct brcmvirt_gpio *ucb = platform_get_drvdata(pdev);
229 + gpiochip_remove(&ucb->gc);
231 + dma_free_coherent(dev, PAGE_SIZE, ucb->ts_base, ucb->bus_addr);
232 + else if (ucb->ts_base)
233 + iounmap(ucb->ts_base);
237 +static const struct of_device_id __maybe_unused brcmvirt_gpio_ids[] = {
238 + { .compatible = "brcm,bcm2835-virtgpio" },
241 +MODULE_DEVICE_TABLE(of, brcmvirt_gpio_ids);
243 +static struct platform_driver brcmvirt_gpio_driver = {
245 + .name = MODULE_NAME,
246 + .owner = THIS_MODULE,
247 + .of_match_table = of_match_ptr(brcmvirt_gpio_ids),
249 + .probe = brcmvirt_gpio_probe,
250 + .remove = brcmvirt_gpio_remove,
252 +module_platform_driver(brcmvirt_gpio_driver);
254 +MODULE_LICENSE("GPL");
255 +MODULE_AUTHOR("Dom Cobley <popcornmix@gmail.com>");
256 +MODULE_DESCRIPTION("brcmvirt GPIO driver");
257 +MODULE_ALIAS("platform:brcmvirt-gpio");
258 --- a/include/soc/bcm2835/raspberrypi-firmware.h
259 +++ b/include/soc/bcm2835/raspberrypi-firmware.h
260 @@ -116,6 +116,7 @@ enum rpi_firmware_property_tag {
261 RPI_FIRMWARE_FRAMEBUFFER_SET_OVERSCAN = 0x0004800a,
262 RPI_FIRMWARE_FRAMEBUFFER_SET_PALETTE = 0x0004800b,
263 RPI_FIRMWARE_FRAMEBUFFER_SET_TOUCHBUF = 0x0004801f,
264 + RPI_FIRMWARE_FRAMEBUFFER_SET_GPIOVIRTBUF = 0x00048020,
265 RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC = 0x0004800e,
266 RPI_FIRMWARE_FRAMEBUFFER_SET_BACKLIGHT = 0x0004800f,