1 From 2fcaab4e60a00ed9b63606b45ae499d7c5561c16 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 arch/arm/configs/bcm2709_defconfig | 1 +
10 drivers/gpio/Kconfig | 6 +
11 drivers/gpio/Makefile | 1 +
12 drivers/gpio/gpio-bcm-virt.c | 180 +++++++++++++++++++++++++++++
13 include/soc/bcm2835/raspberrypi-firmware.h | 1 +
14 5 files changed, 189 insertions(+)
15 create mode 100644 drivers/gpio/gpio-bcm-virt.c
17 --- a/arch/arm/configs/bcm2709_defconfig
18 +++ b/arch/arm/configs/bcm2709_defconfig
19 @@ -607,6 +607,7 @@ CONFIG_PPS=m
20 CONFIG_PPS_CLIENT_LDISC=m
21 CONFIG_PPS_CLIENT_GPIO=m
23 +CONFIG_GPIO_BCM_VIRT=y
27 --- a/drivers/gpio/Kconfig
28 +++ b/drivers/gpio/Kconfig
29 @@ -133,6 +133,12 @@ config GPIO_BCM_KONA
31 Turn on GPIO support for Broadcom "Kona" chips.
34 + bool "Broadcom Virt GPIO"
35 + depends on OF_GPIO && RASPBERRYPI_FIRMWARE && (ARCH_BCM2835 || ARCH_BCM2708 || ARCH_BCM2709 || COMPILE_TEST)
37 + Turn on virtual GPIO support for Broadcom BCM283X chips.
40 tristate "BRCMSTB GPIO support"
41 default y if ARCH_BRCMSTB
42 --- a/drivers/gpio/Makefile
43 +++ b/drivers/gpio/Makefile
44 @@ -24,6 +24,7 @@ obj-$(CONFIG_GPIO_AMDPT) += gpio-amdpt.o
45 obj-$(CONFIG_GPIO_ARIZONA) += gpio-arizona.o
46 obj-$(CONFIG_ATH79) += gpio-ath79.o
47 obj-$(CONFIG_GPIO_BCM_KONA) += gpio-bcm-kona.o
48 +obj-$(CONFIG_GPIO_BCM_VIRT) += gpio-bcm-virt.o
49 obj-$(CONFIG_GPIO_BRCMSTB) += gpio-brcmstb.o
50 obj-$(CONFIG_GPIO_BT8XX) += gpio-bt8xx.o
51 obj-$(CONFIG_GPIO_CLPS711X) += gpio-clps711x.o
53 +++ b/drivers/gpio/gpio-bcm-virt.c
56 + * brcmvirt GPIO driver
58 + * Copyright (C) 2012,2013 Dom Cobley <popcornmix@gmail.com>
59 + * Based on gpio-clps711x.c by Alexander Shiyan <shc_work@mail.ru>
61 + * This program is free software; you can redistribute it and/or modify
62 + * it under the terms of the GNU General Public License as published by
63 + * the Free Software Foundation; either version 2 of the License, or
64 + * (at your option) any later version.
67 +#include <linux/err.h>
68 +#include <linux/gpio.h>
69 +#include <linux/module.h>
70 +#include <linux/basic_mmio_gpio.h>
71 +#include <linux/platform_device.h>
72 +#include <soc/bcm2835/raspberrypi-firmware.h>
74 +#define MODULE_NAME "brcmvirt-gpio"
77 +struct brcmvirt_gpio {
78 + struct gpio_chip gc;
79 + u32 __iomem *ts_base;
80 + /* two packed 16-bit counts of enabled and disables
81 + Allows host to detect a brief enable that was missed */
82 + u32 enables_disables[NUM_GPIO];
85 +static int brcmvirt_gpio_dir_in(struct gpio_chip *gc, unsigned off)
87 + struct brcmvirt_gpio *gpio;
88 + gpio = container_of(gc, struct brcmvirt_gpio, gc);
92 +static int brcmvirt_gpio_dir_out(struct gpio_chip *gc, unsigned off, int val)
94 + struct brcmvirt_gpio *gpio;
95 + gpio = container_of(gc, struct brcmvirt_gpio, gc);
99 +static int brcmvirt_gpio_get(struct gpio_chip *gc, unsigned off)
101 + struct brcmvirt_gpio *gpio;
103 + gpio = container_of(gc, struct brcmvirt_gpio, gc);
104 + v = readl(gpio->ts_base + off);
105 + return (v >> off) & 1;
108 +static void brcmvirt_gpio_set(struct gpio_chip *gc, unsigned off, int val)
110 + struct brcmvirt_gpio *gpio;
111 + u16 enables, disables;
114 + gpio = container_of(gc, struct brcmvirt_gpio, gc);
115 + enables = gpio->enables_disables[off] >> 16;
116 + disables = gpio->enables_disables[off] >> 0;
117 + diff = (s16)(enables - disables);
119 + if ((val && lit) || (!val && !lit))
125 + diff = (s16)(enables - disables);
126 + BUG_ON(diff != 0 && diff != 1);
127 + gpio->enables_disables[off] = (enables << 16) | (disables << 0);
128 + writel(gpio->enables_disables[off], gpio->ts_base + off);
131 +static int brcmvirt_gpio_probe(struct platform_device *pdev)
133 + struct device *dev = &pdev->dev;
134 + struct device_node *np = dev->of_node;
135 + struct device_node *fw_node;
136 + struct rpi_firmware *fw;
137 + struct brcmvirt_gpio *ucb;
141 + fw_node = of_parse_phandle(np, "firmware", 0);
143 + dev_err(dev, "Missing firmware node\n");
147 + fw = rpi_firmware_get(fw_node);
149 + return -EPROBE_DEFER;
151 + err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_GET_GPIOVIRTBUF,
152 + &gpiovirtbuf, sizeof(gpiovirtbuf));
155 + dev_err(dev, "Failed to get gpiovirtbuf\n");
159 + if (!gpiovirtbuf) {
160 + dev_err(dev, "No virtgpio buffer\n");
165 + ucb = devm_kzalloc(dev, sizeof *ucb, GFP_KERNEL);
171 + // mmap the physical memory
172 + gpiovirtbuf &= ~0xc0000000;
173 + ucb->ts_base = ioremap(gpiovirtbuf, 4096);
174 + if (ucb->ts_base == NULL) {
175 + dev_err(dev, "Failed to map physical address\n");
180 + ucb->gc.label = MODULE_NAME;
181 + ucb->gc.owner = THIS_MODULE;
183 + ucb->gc.of_node = np;
184 + ucb->gc.base = 100;
185 + ucb->gc.ngpio = NUM_GPIO;
187 + ucb->gc.direction_input = brcmvirt_gpio_dir_in;
188 + ucb->gc.direction_output = brcmvirt_gpio_dir_out;
189 + ucb->gc.get = brcmvirt_gpio_get;
190 + ucb->gc.set = brcmvirt_gpio_set;
191 + ucb->gc.can_sleep = true;
193 + err = gpiochip_add(&ucb->gc);
197 + platform_set_drvdata(pdev, ucb);
204 +static int brcmvirt_gpio_remove(struct platform_device *pdev)
207 + struct brcmvirt_gpio *ucb = platform_get_drvdata(pdev);
209 + gpiochip_remove(&ucb->gc);
210 + iounmap(ucb->ts_base);
214 +static const struct of_device_id __maybe_unused brcmvirt_gpio_ids[] = {
215 + { .compatible = "brcm,bcm2835-virtgpio" },
218 +MODULE_DEVICE_TABLE(of, brcmvirt_gpio_ids);
220 +static struct platform_driver brcmvirt_gpio_driver = {
222 + .name = MODULE_NAME,
223 + .owner = THIS_MODULE,
224 + .of_match_table = of_match_ptr(brcmvirt_gpio_ids),
226 + .probe = brcmvirt_gpio_probe,
227 + .remove = brcmvirt_gpio_remove,
229 +module_platform_driver(brcmvirt_gpio_driver);
231 +MODULE_LICENSE("GPL");
232 +MODULE_AUTHOR("Dom Cobley <popcornmix@gmail.com>");
233 +MODULE_DESCRIPTION("brcmvirt GPIO driver");
234 +MODULE_ALIAS("platform:brcmvirt-gpio");
235 --- a/include/soc/bcm2835/raspberrypi-firmware.h
236 +++ b/include/soc/bcm2835/raspberrypi-firmware.h
237 @@ -93,6 +93,7 @@ enum rpi_firmware_property_tag {
238 RPI_FIRMWARE_FRAMEBUFFER_GET_OVERSCAN = 0x0004000a,
239 RPI_FIRMWARE_FRAMEBUFFER_GET_PALETTE = 0x0004000b,
240 RPI_FIRMWARE_FRAMEBUFFER_GET_TOUCHBUF = 0x0004000f,
241 + RPI_FIRMWARE_FRAMEBUFFER_GET_GPIOVIRTBUF = 0x00040010,
242 RPI_FIRMWARE_FRAMEBUFFER_RELEASE = 0x00048001,
243 RPI_FIRMWARE_FRAMEBUFFER_TEST_PHYSICAL_WIDTH_HEIGHT = 0x00044003,
244 RPI_FIRMWARE_FRAMEBUFFER_TEST_VIRTUAL_WIDTH_HEIGHT = 0x00044004,