Skip to content

Commit e39bcab

Browse files
committed
bcm2835-virtgpio: Virtual GPIO driver
Add a virtual GPIO driver that uses the firmware mailbox interface to request that the VPU toggles LEDs.
1 parent 4eb1cd6 commit e39bcab

4 files changed

Lines changed: 222 additions & 0 deletions

File tree

‎drivers/gpio/Kconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ config GPIO_BCM_KONA
134134
help
135135
Turn on GPIO support for Broadcom "Kona" chips.
136136

137+
config GPIO_BCM_VIRT
138+
bool "Broadcom Virt GPIO"
139+
depends on OF_GPIO && RASPBERRYPI_FIRMWARE && (ARCH_BCM2835 || COMPILE_TEST)
140+
help
141+
Turn on virtual GPIO support for Broadcom BCM283X chips.
142+
137143
config GPIO_BRCMSTB
138144
tristate "BRCMSTB GPIO support"
139145
default y if (ARCH_BRCMSTB || BMIPS_GENERIC)

‎drivers/gpio/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ obj-$(CONFIG_GPIO_ATH79) += gpio-ath79.o
3131
obj-$(CONFIG_GPIO_ASPEED) += gpio-aspeed.o
3232
obj-$(CONFIG_GPIO_AXP209) += gpio-axp209.o
3333
obj-$(CONFIG_GPIO_BCM_KONA) += gpio-bcm-kona.o
34+
obj-$(CONFIG_GPIO_BCM_VIRT) += gpio-bcm-virt.o
3435
obj-$(CONFIG_GPIO_BRCMSTB) += gpio-brcmstb.o
3536
obj-$(CONFIG_GPIO_BT8XX) += gpio-bt8xx.o
3637
obj-$(CONFIG_GPIO_CLPS711X) += gpio-clps711x.o

‎drivers/gpio/gpio-bcm-virt.c‎

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
/*
2+
* brcmvirt GPIO driver
3+
*
4+
* Copyright (C) 2012,2013 Dom Cobley <popcornmix@gmail.com>
5+
* Based on gpio-clps711x.c by Alexander Shiyan <shc_work@mail.ru>
6+
*
7+
* This program is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*/
12+
13+
#include <linux/err.h>
14+
#include <linux/gpio.h>
15+
#include <linux/module.h>
16+
#include <linux/platform_device.h>
17+
#include <linux/dma-mapping.h>
18+
#include <soc/bcm2835/raspberrypi-firmware.h>
19+
20+
#define MODULE_NAME "brcmvirt-gpio"
21+
#define NUM_GPIO 2
22+
23+
struct brcmvirt_gpio {
24+
struct gpio_chip gc;
25+
u32 __iomem *ts_base;
26+
/* two packed 16-bit counts of enabled and disables
27+
Allows host to detect a brief enable that was missed */
28+
u32 enables_disables[NUM_GPIO];
29+
dma_addr_t bus_addr;
30+
};
31+
32+
static int brcmvirt_gpio_dir_in(struct gpio_chip *gc, unsigned off)
33+
{
34+
struct brcmvirt_gpio *gpio;
35+
gpio = container_of(gc, struct brcmvirt_gpio, gc);
36+
return -EINVAL;
37+
}
38+
39+
static int brcmvirt_gpio_dir_out(struct gpio_chip *gc, unsigned off, int val)
40+
{
41+
struct brcmvirt_gpio *gpio;
42+
gpio = container_of(gc, struct brcmvirt_gpio, gc);
43+
return 0;
44+
}
45+
46+
static int brcmvirt_gpio_get(struct gpio_chip *gc, unsigned off)
47+
{
48+
struct brcmvirt_gpio *gpio;
49+
unsigned v;
50+
gpio = container_of(gc, struct brcmvirt_gpio, gc);
51+
v = readl(gpio->ts_base + off);
52+
return (v >> off) & 1;
53+
}
54+
55+
static void brcmvirt_gpio_set(struct gpio_chip *gc, unsigned off, int val)
56+
{
57+
struct brcmvirt_gpio *gpio;
58+
u16 enables, disables;
59+
s16 diff;
60+
bool lit;
61+
gpio = container_of(gc, struct brcmvirt_gpio, gc);
62+
enables = gpio->enables_disables[off] >> 16;
63+
disables = gpio->enables_disables[off] >> 0;
64+
diff = (s16)(enables - disables);
65+
lit = diff > 0;
66+
if ((val && lit) || (!val && !lit))
67+
return;
68+
if (val)
69+
enables++;
70+
else
71+
disables++;
72+
diff = (s16)(enables - disables);
73+
BUG_ON(diff != 0 && diff != 1);
74+
gpio->enables_disables[off] = (enables << 16) | (disables << 0);
75+
writel(gpio->enables_disables[off], gpio->ts_base + off);
76+
}
77+
78+
static int brcmvirt_gpio_probe(struct platform_device *pdev)
79+
{
80+
int err = 0;
81+
struct device *dev = &pdev->dev;
82+
struct device_node *np = dev->of_node;
83+
struct device_node *fw_node;
84+
struct rpi_firmware *fw;
85+
struct brcmvirt_gpio *ucb;
86+
u32 gpiovirtbuf;
87+
88+
fw_node = of_parse_phandle(np, "firmware", 0);
89+
if (!fw_node) {
90+
dev_err(dev, "Missing firmware node\n");
91+
return -ENOENT;
92+
}
93+
94+
fw = rpi_firmware_get(fw_node);
95+
if (!fw)
96+
return -EPROBE_DEFER;
97+
98+
ucb = devm_kzalloc(dev, sizeof *ucb, GFP_KERNEL);
99+
if (!ucb) {
100+
err = -EINVAL;
101+
goto out;
102+
}
103+
104+
ucb->ts_base = dma_zalloc_coherent(dev, PAGE_SIZE, &ucb->bus_addr, GFP_KERNEL);
105+
if (!ucb->ts_base) {
106+
pr_err("[%s]: failed to dma_alloc_coherent(%ld)\n",
107+
__func__, PAGE_SIZE);
108+
err = -ENOMEM;
109+
goto out;
110+
}
111+
112+
gpiovirtbuf = (u32)ucb->bus_addr;
113+
err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_SET_GPIOVIRTBUF,
114+
&gpiovirtbuf, sizeof(gpiovirtbuf));
115+
116+
if (err || gpiovirtbuf != 0) {
117+
dev_warn(dev, "Failed to set gpiovirtbuf, trying to get err:%x\n", err);
118+
dma_free_coherent(dev, PAGE_SIZE, ucb->ts_base, ucb->bus_addr);
119+
ucb->ts_base = 0;
120+
ucb->bus_addr = 0;
121+
}
122+
123+
if (!ucb->ts_base) {
124+
err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_GET_GPIOVIRTBUF,
125+
&gpiovirtbuf, sizeof(gpiovirtbuf));
126+
127+
if (err) {
128+
dev_err(dev, "Failed to get gpiovirtbuf\n");
129+
goto out;
130+
}
131+
132+
if (!gpiovirtbuf) {
133+
dev_err(dev, "No virtgpio buffer\n");
134+
err = -ENOENT;
135+
goto out;
136+
}
137+
138+
// mmap the physical memory
139+
gpiovirtbuf &= ~0xc0000000;
140+
ucb->ts_base = ioremap(gpiovirtbuf, 4096);
141+
if (ucb->ts_base == NULL) {
142+
dev_err(dev, "Failed to map physical address\n");
143+
err = -ENOENT;
144+
goto out;
145+
}
146+
ucb->bus_addr = 0;
147+
}
148+
ucb->gc.label = MODULE_NAME;
149+
ucb->gc.owner = THIS_MODULE;
150+
//ucb->gc.dev = dev;
151+
ucb->gc.of_node = np;
152+
ucb->gc.base = 100;
153+
ucb->gc.ngpio = NUM_GPIO;
154+
155+
ucb->gc.direction_input = brcmvirt_gpio_dir_in;
156+
ucb->gc.direction_output = brcmvirt_gpio_dir_out;
157+
ucb->gc.get = brcmvirt_gpio_get;
158+
ucb->gc.set = brcmvirt_gpio_set;
159+
ucb->gc.can_sleep = true;
160+
161+
err = gpiochip_add(&ucb->gc);
162+
if (err)
163+
goto out;
164+
165+
platform_set_drvdata(pdev, ucb);
166+
167+
return 0;
168+
out:
169+
if (ucb->bus_addr) {
170+
dma_free_coherent(dev, PAGE_SIZE, ucb->ts_base, ucb->bus_addr);
171+
ucb->bus_addr = 0;
172+
ucb->ts_base = NULL;
173+
} else if (ucb->ts_base) {
174+
iounmap(ucb->ts_base);
175+
ucb->ts_base = NULL;
176+
}
177+
return err;
178+
}
179+
180+
static int brcmvirt_gpio_remove(struct platform_device *pdev)
181+
{
182+
struct device *dev = &pdev->dev;
183+
int err = 0;
184+
struct brcmvirt_gpio *ucb = platform_get_drvdata(pdev);
185+
186+
gpiochip_remove(&ucb->gc);
187+
if (ucb->bus_addr)
188+
dma_free_coherent(dev, PAGE_SIZE, ucb->ts_base, ucb->bus_addr);
189+
else if (ucb->ts_base)
190+
iounmap(ucb->ts_base);
191+
return err;
192+
}
193+
194+
static const struct of_device_id __maybe_unused brcmvirt_gpio_ids[] = {
195+
{ .compatible = "brcm,bcm2835-virtgpio" },
196+
{ }
197+
};
198+
MODULE_DEVICE_TABLE(of, brcmvirt_gpio_ids);
199+
200+
static struct platform_driver brcmvirt_gpio_driver = {
201+
.driver = {
202+
.name = MODULE_NAME,
203+
.owner = THIS_MODULE,
204+
.of_match_table = of_match_ptr(brcmvirt_gpio_ids),
205+
},
206+
.probe = brcmvirt_gpio_probe,
207+
.remove = brcmvirt_gpio_remove,
208+
};
209+
module_platform_driver(brcmvirt_gpio_driver);
210+
211+
MODULE_LICENSE("GPL");
212+
MODULE_AUTHOR("Dom Cobley <popcornmix@gmail.com>");
213+
MODULE_DESCRIPTION("brcmvirt GPIO driver");
214+
MODULE_ALIAS("platform:brcmvirt-gpio");

‎include/soc/bcm2835/raspberrypi-firmware.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ enum rpi_firmware_property_tag {
116116
RPI_FIRMWARE_FRAMEBUFFER_SET_OVERSCAN = 0x0004800a,
117117
RPI_FIRMWARE_FRAMEBUFFER_SET_PALETTE = 0x0004800b,
118118
RPI_FIRMWARE_FRAMEBUFFER_SET_TOUCHBUF = 0x0004801f,
119+
RPI_FIRMWARE_FRAMEBUFFER_SET_GPIOVIRTBUF = 0x00048020,
119120
RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC = 0x0004800e,
120121
RPI_FIRMWARE_FRAMEBUFFER_SET_BACKLIGHT = 0x0004800f,
121122

0 commit comments

Comments
 (0)