Skip to content

Commit e1024a4

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 edd0a6e commit e1024a4

File tree

5 files changed

+189
-0
lines changed

5 files changed

+189
-0
lines changed

arch/arm/configs/bcm2709_defconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ CONFIG_PPS=m
607607
CONFIG_PPS_CLIENT_LDISC=m
608608
CONFIG_PPS_CLIENT_GPIO=m
609609
CONFIG_GPIO_SYSFS=y
610+
CONFIG_GPIO_BCM_VIRT=y
610611
CONFIG_GPIO_ARIZONA=m
611612
CONFIG_GPIO_STMPE=y
612613
CONFIG_W1=m

drivers/gpio/Kconfig

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

135+
config GPIO_BCM_VIRT
136+
bool "Broadcom Virt GPIO"
137+
depends on OF_GPIO && RASPBERRYPI_FIRMWARE && (ARCH_BCM2835 || ARCH_BCM2708 || ARCH_BCM2709 || COMPILE_TEST)
138+
help
139+
Turn on virtual GPIO support for Broadcom BCM283X chips.
140+
135141
config GPIO_BRCMSTB
136142
tristate "BRCMSTB GPIO support"
137143
default y if ARCH_BRCMSTB

drivers/gpio/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ obj-$(CONFIG_GPIO_AMDPT) += gpio-amdpt.o
2424
obj-$(CONFIG_GPIO_ARIZONA) += gpio-arizona.o
2525
obj-$(CONFIG_ATH79) += gpio-ath79.o
2626
obj-$(CONFIG_GPIO_BCM_KONA) += gpio-bcm-kona.o
27+
obj-$(CONFIG_GPIO_BCM_VIRT) += gpio-bcm-virt.o
2728
obj-$(CONFIG_GPIO_BRCMSTB) += gpio-brcmstb.o
2829
obj-$(CONFIG_GPIO_BT8XX) += gpio-bt8xx.o
2930
obj-$(CONFIG_GPIO_CLPS711X) += gpio-clps711x.o

drivers/gpio/gpio-bcm-virt.c

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
* brcmvirt GPIO driver
3+
*
4+
* Copyright (C) 2012,2013 Dom Cobley <[email protected]>
5+
* Based on gpio-clps711x.c by Alexander Shiyan <[email protected]>
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/basic_mmio_gpio.h>
17+
#include <linux/platform_device.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+
};
30+
31+
static int brcmvirt_gpio_dir_in(struct gpio_chip *gc, unsigned off)
32+
{
33+
struct brcmvirt_gpio *gpio;
34+
gpio = container_of(gc, struct brcmvirt_gpio, gc);
35+
return -EINVAL;
36+
}
37+
38+
static int brcmvirt_gpio_dir_out(struct gpio_chip *gc, unsigned off, int val)
39+
{
40+
struct brcmvirt_gpio *gpio;
41+
gpio = container_of(gc, struct brcmvirt_gpio, gc);
42+
return 0;
43+
}
44+
45+
static int brcmvirt_gpio_get(struct gpio_chip *gc, unsigned off)
46+
{
47+
struct brcmvirt_gpio *gpio;
48+
unsigned v;
49+
gpio = container_of(gc, struct brcmvirt_gpio, gc);
50+
v = readl(gpio->ts_base + off);
51+
return (v >> off) & 1;
52+
}
53+
54+
static void brcmvirt_gpio_set(struct gpio_chip *gc, unsigned off, int val)
55+
{
56+
struct brcmvirt_gpio *gpio;
57+
u16 enables, disables;
58+
s16 diff;
59+
bool lit;
60+
gpio = container_of(gc, struct brcmvirt_gpio, gc);
61+
enables = gpio->enables_disables[off] >> 16;
62+
disables = gpio->enables_disables[off] >> 0;
63+
diff = (s16)(enables - disables);
64+
lit = diff > 0;
65+
if ((val && lit) || (!val && !lit))
66+
return;
67+
if (val)
68+
enables++;
69+
else
70+
disables++;
71+
diff = (s16)(enables - disables);
72+
BUG_ON(diff != 0 && diff != 1);
73+
gpio->enables_disables[off] = (enables << 16) | (disables << 0);
74+
writel(gpio->enables_disables[off], gpio->ts_base + off);
75+
}
76+
77+
static int brcmvirt_gpio_probe(struct platform_device *pdev)
78+
{
79+
struct device *dev = &pdev->dev;
80+
struct device_node *np = dev->of_node;
81+
struct device_node *fw_node;
82+
struct rpi_firmware *fw;
83+
struct brcmvirt_gpio *ucb;
84+
u32 gpiovirtbuf;
85+
int err = 0;
86+
87+
fw_node = of_parse_phandle(np, "firmware", 0);
88+
if (!fw_node) {
89+
dev_err(dev, "Missing firmware node\n");
90+
return -ENOENT;
91+
}
92+
93+
fw = rpi_firmware_get(fw_node);
94+
if (!fw)
95+
return -EPROBE_DEFER;
96+
97+
err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_GET_GPIOVIRTBUF,
98+
&gpiovirtbuf, sizeof(gpiovirtbuf));
99+
100+
if (err) {
101+
dev_err(dev, "Failed to get gpiovirtbuf\n");
102+
goto err;
103+
}
104+
105+
if (!gpiovirtbuf) {
106+
dev_err(dev, "No virtgpio buffer\n");
107+
err = -ENOENT;
108+
goto err;
109+
}
110+
111+
ucb = devm_kzalloc(dev, sizeof *ucb, GFP_KERNEL);
112+
if (!ucb) {
113+
err = -EINVAL;
114+
goto err;
115+
}
116+
117+
// mmap the physical memory
118+
gpiovirtbuf &= ~0xc0000000;
119+
ucb->ts_base = ioremap(gpiovirtbuf, 4096);
120+
if (ucb->ts_base == NULL) {
121+
dev_err(dev, "Failed to map physical address\n");
122+
err = -ENOENT;
123+
goto err;
124+
}
125+
126+
ucb->gc.label = MODULE_NAME;
127+
ucb->gc.owner = THIS_MODULE;
128+
ucb->gc.dev = dev;
129+
ucb->gc.of_node = np;
130+
ucb->gc.base = 100;
131+
ucb->gc.ngpio = NUM_GPIO;
132+
133+
ucb->gc.direction_input = brcmvirt_gpio_dir_in;
134+
ucb->gc.direction_output = brcmvirt_gpio_dir_out;
135+
ucb->gc.get = brcmvirt_gpio_get;
136+
ucb->gc.set = brcmvirt_gpio_set;
137+
ucb->gc.can_sleep = true;
138+
139+
err = gpiochip_add(&ucb->gc);
140+
if (err)
141+
goto err;
142+
143+
platform_set_drvdata(pdev, ucb);
144+
145+
err:
146+
return err;
147+
148+
}
149+
150+
static int brcmvirt_gpio_remove(struct platform_device *pdev)
151+
{
152+
int err = 0;
153+
struct brcmvirt_gpio *ucb = platform_get_drvdata(pdev);
154+
155+
gpiochip_remove(&ucb->gc);
156+
iounmap(ucb->ts_base);
157+
return err;
158+
}
159+
160+
static const struct of_device_id __maybe_unused brcmvirt_gpio_ids[] = {
161+
{ .compatible = "brcm,bcm2835-virtgpio" },
162+
{ }
163+
};
164+
MODULE_DEVICE_TABLE(of, brcmvirt_gpio_ids);
165+
166+
static struct platform_driver brcmvirt_gpio_driver = {
167+
.driver = {
168+
.name = MODULE_NAME,
169+
.owner = THIS_MODULE,
170+
.of_match_table = of_match_ptr(brcmvirt_gpio_ids),
171+
},
172+
.probe = brcmvirt_gpio_probe,
173+
.remove = brcmvirt_gpio_remove,
174+
};
175+
module_platform_driver(brcmvirt_gpio_driver);
176+
177+
MODULE_LICENSE("GPL");
178+
MODULE_AUTHOR("Dom Cobley <[email protected]>");
179+
MODULE_DESCRIPTION("brcmvirt GPIO driver");
180+
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
@@ -93,6 +93,7 @@ enum rpi_firmware_property_tag {
9393
RPI_FIRMWARE_FRAMEBUFFER_GET_OVERSCAN = 0x0004000a,
9494
RPI_FIRMWARE_FRAMEBUFFER_GET_PALETTE = 0x0004000b,
9595
RPI_FIRMWARE_FRAMEBUFFER_GET_TOUCHBUF = 0x0004000f,
96+
RPI_FIRMWARE_FRAMEBUFFER_GET_GPIOVIRTBUF = 0x00040010,
9697
RPI_FIRMWARE_FRAMEBUFFER_RELEASE = 0x00048001,
9798
RPI_FIRMWARE_FRAMEBUFFER_TEST_PHYSICAL_WIDTH_HEIGHT = 0x00044003,
9899
RPI_FIRMWARE_FRAMEBUFFER_TEST_VIRTUAL_WIDTH_HEIGHT = 0x00044004,

0 commit comments

Comments
 (0)