x86: remove old references to kmod-acpi-button
[openwrt.git] / target / linux / brcm2708 / patches-4.4 / 0166-bcm2835-virtgpio-Virtual-GPIO-driver.patch
1 From 5e3c3e845f998f86c2f22017576cb19e5d7fe9bb 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 166/170] bcm2835-virtgpio: Virtual GPIO driver
5
6 Add a virtual GPIO driver that uses the firmware mailbox interface to
7 request that the VPU toggles LEDs.
8 ---
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
16
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
22  CONFIG_GPIO_SYSFS=y
23 +CONFIG_GPIO_BCM_VIRT=y
24  CONFIG_GPIO_ARIZONA=m
25  CONFIG_GPIO_STMPE=y
26  CONFIG_W1=m
27 --- a/drivers/gpio/Kconfig
28 +++ b/drivers/gpio/Kconfig
29 @@ -132,6 +132,12 @@ config GPIO_BCM_KONA
30         help
31           Turn on GPIO support for Broadcom "Kona" chips.
32  
33 +config GPIO_BCM_VIRT
34 +       bool "Broadcom Virt GPIO"
35 +       depends on OF_GPIO && RASPBERRYPI_FIRMWARE && (ARCH_BCM2835 || ARCH_BCM2708 || ARCH_BCM2709 || COMPILE_TEST)
36 +       help
37 +         Turn on virtual GPIO support for Broadcom BCM283X chips.
38 +
39  config GPIO_BRCMSTB
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
52 --- /dev/null
53 +++ b/drivers/gpio/gpio-bcm-virt.c
54 @@ -0,0 +1,180 @@
55 +/*
56 + *  brcmvirt GPIO driver
57 + *
58 + *  Copyright (C) 2012,2013 Dom Cobley <popcornmix@gmail.com>
59 + *  Based on gpio-clps711x.c by Alexander Shiyan <shc_work@mail.ru>
60 + *
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.
65 + */
66 +
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>
73 +
74 +#define MODULE_NAME "brcmvirt-gpio"
75 +#define NUM_GPIO 2
76 +
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];
83 +};
84 +
85 +static int brcmvirt_gpio_dir_in(struct gpio_chip *gc, unsigned off)
86 +{
87 +       struct brcmvirt_gpio *gpio;
88 +       gpio = container_of(gc, struct brcmvirt_gpio, gc);
89 +       return -EINVAL;
90 +}
91 +
92 +static int brcmvirt_gpio_dir_out(struct gpio_chip *gc, unsigned off, int val)
93 +{
94 +       struct brcmvirt_gpio *gpio;
95 +       gpio = container_of(gc, struct brcmvirt_gpio, gc);
96 +       return 0;
97 +}
98 +
99 +static int brcmvirt_gpio_get(struct gpio_chip *gc, unsigned off)
100 +{
101 +       struct brcmvirt_gpio *gpio;
102 +       unsigned v;
103 +       gpio = container_of(gc, struct brcmvirt_gpio, gc);
104 +       v = readl(gpio->ts_base + off);
105 +       return (v >> off) & 1;
106 +}
107 +
108 +static void brcmvirt_gpio_set(struct gpio_chip *gc, unsigned off, int val)
109 +{
110 +       struct brcmvirt_gpio *gpio;
111 +       u16 enables, disables;
112 +       s16 diff;
113 +       bool lit;
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);
118 +       lit = diff > 0;
119 +       if ((val && lit) || (!val && !lit))
120 +               return;
121 +       if (val)
122 +               enables++;
123 +       else
124 +               disables++;
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);
129 +}
130 +
131 +static int brcmvirt_gpio_probe(struct platform_device *pdev)
132 +{
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;
138 +       u32 gpiovirtbuf;
139 +       int err = 0;
140 +
141 +       fw_node = of_parse_phandle(np, "firmware", 0);
142 +       if (!fw_node) {
143 +               dev_err(dev, "Missing firmware node\n");
144 +               return -ENOENT;
145 +       }
146 +
147 +       fw = rpi_firmware_get(fw_node);
148 +       if (!fw)
149 +               return -EPROBE_DEFER;
150 +
151 +       err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_GET_GPIOVIRTBUF,
152 +                                   &gpiovirtbuf, sizeof(gpiovirtbuf));
153 +
154 +       if (err) {
155 +               dev_err(dev, "Failed to get gpiovirtbuf\n");
156 +               goto err;
157 +       }
158 +
159 +       if (!gpiovirtbuf) {
160 +               dev_err(dev, "No virtgpio buffer\n");
161 +               err = -ENOENT;
162 +               goto err;
163 +       }
164 +
165 +       ucb = devm_kzalloc(dev, sizeof *ucb, GFP_KERNEL);
166 +       if (!ucb) {
167 +               err = -EINVAL;
168 +               goto err;
169 +       }
170 +
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");
176 +               err = -ENOENT;
177 +               goto err;
178 +       }
179 +
180 +       ucb->gc.label = MODULE_NAME;
181 +       ucb->gc.owner = THIS_MODULE;
182 +       ucb->gc.dev = dev;
183 +       ucb->gc.of_node = np;
184 +       ucb->gc.base = 100;
185 +       ucb->gc.ngpio = NUM_GPIO;
186 +
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;
192 +
193 +       err = gpiochip_add(&ucb->gc);
194 +       if (err)
195 +               goto err;
196 +
197 +       platform_set_drvdata(pdev, ucb);
198 +
199 +err:
200 +       return err;
201 +
202 +}
203 +
204 +static int brcmvirt_gpio_remove(struct platform_device *pdev)
205 +{
206 +       int err = 0;
207 +       struct brcmvirt_gpio *ucb = platform_get_drvdata(pdev);
208 +
209 +       gpiochip_remove(&ucb->gc);
210 +       iounmap(ucb->ts_base);
211 +       return err;
212 +}
213 +
214 +static const struct of_device_id __maybe_unused brcmvirt_gpio_ids[] = {
215 +       { .compatible = "brcm,bcm2835-virtgpio" },
216 +       { }
217 +};
218 +MODULE_DEVICE_TABLE(of, brcmvirt_gpio_ids);
219 +
220 +static struct platform_driver brcmvirt_gpio_driver = {
221 +       .driver = {
222 +               .name           = MODULE_NAME,
223 +               .owner          = THIS_MODULE,
224 +               .of_match_table = of_match_ptr(brcmvirt_gpio_ids),
225 +       },
226 +       .probe  = brcmvirt_gpio_probe,
227 +       .remove = brcmvirt_gpio_remove,
228 +};
229 +module_platform_driver(brcmvirt_gpio_driver);
230 +
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,