brcm2708: update 4.1 patches
[openwrt.git] / target / linux / brcm2708 / patches-4.1 / 0168-Add-dev-gpiomem-device-for-rootless-user-GPIO-access.patch
1 From ba24fdcd3a1f7282dfba9e5137beaa9d0f999969 Mon Sep 17 00:00:00 2001
2 From: Luke Wren <luke@raspberrypi.org>
3 Date: Fri, 21 Aug 2015 23:14:48 +0100
4 Subject: [PATCH 168/171] Add /dev/gpiomem device for rootless user GPIO access
5
6 Signed-off-by: Luke Wren <luke@raspberrypi.org>
7 ---
8  arch/arm/boot/dts/bcm2708.dtsi          |   6 +
9  arch/arm/boot/dts/bcm2709.dtsi          |   6 +
10  drivers/char/broadcom/Kconfig           |   9 ++
11  drivers/char/broadcom/Makefile          |   3 +
12  drivers/char/broadcom/bcm2835-gpiomem.c | 265 ++++++++++++++++++++++++++++++++
13  5 files changed, 289 insertions(+)
14  create mode 100644 drivers/char/broadcom/bcm2835-gpiomem.c
15
16 --- a/arch/arm/boot/dts/bcm2708.dtsi
17 +++ b/arch/arm/boot/dts/bcm2708.dtsi
18 @@ -15,5 +15,11 @@
19                 arm-pmu {
20                         compatible = "arm,arm1176-pmu";
21                 };
22 +
23 +               gpiomem {
24 +                       compatible = "brcm,bcm2835-gpiomem";
25 +                       reg = <0x7e200000 0x1000>;
26 +                       status = "okay";
27 +               };
28         };
29  };
30 --- a/arch/arm/boot/dts/bcm2709.dtsi
31 +++ b/arch/arm/boot/dts/bcm2709.dtsi
32 @@ -16,6 +16,12 @@
33                         compatible = "arm,cortex-a7-pmu";
34                         interrupts = <3 9>;
35                 };
36 +
37 +               gpiomem {
38 +                       compatible = "brcm,bcm2835-gpiomem";
39 +                       reg = <0x7e200000 0x1000>;
40 +                       status = "okay";
41 +               };
42         };
43  
44         timer {
45 --- a/drivers/char/broadcom/Kconfig
46 +++ b/drivers/char/broadcom/Kconfig
47 @@ -38,3 +38,12 @@ config BCM_VC_SM
48         help
49         Support for the VC shared memory on the Broadcom reference
50         design. Uses the VCHIQ stack.
51 +
52 +config BCM2835_DEVGPIOMEM
53 +       tristate "/dev/gpiomem rootless GPIO access via mmap() on the BCM2835"
54 +       default m
55 +       help
56 +               Provides users with root-free access to the GPIO registers
57 +               on the 2835. Calling mmap(/dev/gpiomem) will map the GPIO
58 +               register page to the user's pointer.
59 +
60 --- a/drivers/char/broadcom/Makefile
61 +++ b/drivers/char/broadcom/Makefile
62 @@ -2,3 +2,6 @@ obj-$(CONFIG_BCM_VC_CMA)        += vc_cma/
63  obj-$(CONFIG_BCM2708_VCMEM)    += vc_mem.o
64  obj-$(CONFIG_BCM_VCIO)         += vcio.o
65  obj-$(CONFIG_BCM_VC_SM)         += vc_sm/
66 +
67 +obj-$(CONFIG_BCM2835_DEVGPIOMEM)+= bcm2835-gpiomem.o
68 +
69 --- /dev/null
70 +++ b/drivers/char/broadcom/bcm2835-gpiomem.c
71 @@ -0,0 +1,265 @@
72 +/**
73 + * GPIO memory device driver
74 + *
75 + * Creates a chardev /dev/gpiomem which will provide user access to
76 + * the BCM2835's GPIO registers when it is mmap()'d.
77 + * No longer need root for user GPIO access, but without relaxing permissions
78 + * on /dev/mem.
79 + *
80 + * Written by Luke Wren <luke@raspberrypi.org>
81 + * Copyright (c) 2015, Raspberry Pi (Trading) Ltd.
82 + *
83 + * Redistribution and use in source and binary forms, with or without
84 + * modification, are permitted provided that the following conditions
85 + * are met:
86 + * 1. Redistributions of source code must retain the above copyright
87 + *    notice, this list of conditions, and the following disclaimer,
88 + *    without modification.
89 + * 2. Redistributions in binary form must reproduce the above copyright
90 + *    notice, this list of conditions and the following disclaimer in the
91 + *    documentation and/or other materials provided with the distribution.
92 + * 3. The names of the above-listed copyright holders may not be used
93 + *    to endorse or promote products derived from this software without
94 + *    specific prior written permission.
95 + *
96 + * ALTERNATIVELY, this software may be distributed under the terms of the
97 + * GNU General Public License ("GPL") version 2, as published by the Free
98 + * Software Foundation.
99 + *
100 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
101 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
102 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
103 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
104 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
105 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
106 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
107 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
108 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
109 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
110 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
111 + */
112 +
113 +#include <linux/kernel.h>
114 +#include <linux/module.h>
115 +#include <linux/of.h>
116 +#include <linux/platform_device.h>
117 +#include <linux/mm.h>
118 +#include <linux/slab.h>
119 +#include <linux/cdev.h>
120 +#include <linux/pagemap.h>
121 +#include <linux/io.h>
122 +
123 +#define DEVICE_NAME "bcm2835-gpiomem"
124 +#define DRIVER_NAME "gpiomem-bcm2835"
125 +#define DEVICE_MINOR 0
126 +
127 +struct bcm2835_gpiomem_instance {
128 +       unsigned long gpio_regs_phys;
129 +       struct device *dev;
130 +};
131 +
132 +static struct cdev bcm2835_gpiomem_cdev;
133 +static dev_t bcm2835_gpiomem_devid;
134 +static struct class *bcm2835_gpiomem_class;
135 +static struct device *bcm2835_gpiomem_dev;
136 +static struct bcm2835_gpiomem_instance *inst;
137 +
138 +
139 +/****************************************************************************
140 +*
141 +*   GPIO mem chardev file ops
142 +*
143 +***************************************************************************/
144 +
145 +static int bcm2835_gpiomem_open(struct inode *inode, struct file *file)
146 +{
147 +       int dev = iminor(inode);
148 +       int ret = 0;
149 +
150 +       dev_info(inst->dev, "gpiomem device opened.");
151 +
152 +       if (dev != DEVICE_MINOR) {
153 +               dev_err(inst->dev, "Unknown minor device: %d", dev);
154 +               ret = -ENXIO;
155 +       }
156 +       return ret;
157 +}
158 +
159 +static int bcm2835_gpiomem_release(struct inode *inode, struct file *file)
160 +{
161 +       int dev = iminor(inode);
162 +       int ret = 0;
163 +
164 +       if (dev != DEVICE_MINOR) {
165 +               dev_err(inst->dev, "Unknown minor device %d", dev);
166 +               ret = -ENXIO;
167 +       }
168 +       return ret;
169 +}
170 +
171 +static const struct vm_operations_struct bcm2835_gpiomem_vm_ops = {
172 +#ifdef CONFIG_HAVE_IOREMAP_PROT
173 +       .access = generic_access_phys
174 +#endif
175 +};
176 +
177 +static int bcm2835_gpiomem_mmap(struct file *file, struct vm_area_struct *vma)
178 +{
179 +       /* Ignore what the user says - they're getting the GPIO regs
180 +          whether they like it or not! */
181 +       unsigned long gpio_page = inst->gpio_regs_phys >> PAGE_SHIFT;
182 +
183 +       vma->vm_page_prot = phys_mem_access_prot(file, gpio_page,
184 +                                                PAGE_SIZE,
185 +                                                vma->vm_page_prot);
186 +       vma->vm_ops = &bcm2835_gpiomem_vm_ops;
187 +       if (remap_pfn_range(vma, vma->vm_start,
188 +                       gpio_page,
189 +                       PAGE_SIZE,
190 +                       vma->vm_page_prot)) {
191 +               return -EAGAIN;
192 +       }
193 +       return 0;
194 +}
195 +
196 +static const struct file_operations
197 +bcm2835_gpiomem_fops = {
198 +       .owner = THIS_MODULE,
199 +       .open = bcm2835_gpiomem_open,
200 +       .release = bcm2835_gpiomem_release,
201 +       .mmap = bcm2835_gpiomem_mmap,
202 +};
203 +
204 +
205 + /****************************************************************************
206 +*
207 +*   Probe and remove functions
208 +*
209 +***************************************************************************/
210 +
211 +
212 +static int bcm2835_gpiomem_probe(struct platform_device *pdev)
213 +{
214 +       int err;
215 +       void *ptr_err;
216 +       struct device *dev = &pdev->dev;
217 +       struct device_node *node = dev->of_node;
218 +       struct resource *ioresource;
219 +
220 +       /* Allocate buffers and instance data */
221 +
222 +       inst = kzalloc(sizeof(struct bcm2835_gpiomem_instance), GFP_KERNEL);
223 +
224 +       if (!inst) {
225 +               err = -ENOMEM;
226 +               goto failed_inst_alloc;
227 +       }
228 +
229 +       inst->dev = dev;
230 +
231 +       /* Create character device entries */
232 +
233 +       err = alloc_chrdev_region(&bcm2835_gpiomem_devid,
234 +                                 DEVICE_MINOR, 1, DEVICE_NAME);
235 +       if (err != 0) {
236 +               dev_err(inst->dev, "unable to allocate device number");
237 +               goto failed_alloc_chrdev;
238 +       }
239 +       cdev_init(&bcm2835_gpiomem_cdev, &bcm2835_gpiomem_fops);
240 +       bcm2835_gpiomem_cdev.owner = THIS_MODULE;
241 +       err = cdev_add(&bcm2835_gpiomem_cdev, bcm2835_gpiomem_devid, 1);
242 +       if (err != 0) {
243 +               dev_err(inst->dev, "unable to register device");
244 +               goto failed_cdev_add;
245 +       }
246 +
247 +       /* Create sysfs entries */
248 +
249 +       bcm2835_gpiomem_class = class_create(THIS_MODULE, DEVICE_NAME);
250 +       ptr_err = bcm2835_gpiomem_class;
251 +       if (IS_ERR(ptr_err))
252 +               goto failed_class_create;
253 +
254 +       bcm2835_gpiomem_dev = device_create(bcm2835_gpiomem_class, NULL,
255 +                                       bcm2835_gpiomem_devid, NULL,
256 +                                       "gpiomem");
257 +       ptr_err = bcm2835_gpiomem_dev;
258 +       if (IS_ERR(ptr_err))
259 +               goto failed_device_create;
260 +
261 +       /* Get address from device tree if available (*_resource() correctly
262 +          converts the bus address in device tree to a physical address),
263 +          or use hardcoded offset + BCM2708_PERI_BASE if not.
264 +          (In spite of its name 2708 actually seems to have the correct
265 +          mach-dependent value on 2709 etc, as it is defined in
266 +          mach-bcm270x/platform.h) */
267 +
268 +       if (node) {
269 +               ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
270 +               inst->gpio_regs_phys = ioresource->start;
271 +       } else {
272 +               inst->gpio_regs_phys = GPIO_BASE;
273 +       }
274 +
275 +       dev_info(inst->dev, "Initialised: Registers at 0x%08lx",
276 +               inst->gpio_regs_phys);
277 +
278 +       return 0;
279 +
280 +failed_device_create:
281 +       class_destroy(bcm2835_gpiomem_class);
282 +failed_class_create:
283 +       cdev_del(&bcm2835_gpiomem_cdev);
284 +       err = PTR_ERR(ptr_err);
285 +failed_cdev_add:
286 +       unregister_chrdev_region(bcm2835_gpiomem_devid, 1);
287 +failed_alloc_chrdev:
288 +       kfree(inst);
289 +failed_inst_alloc:
290 +       dev_err(inst->dev, "could not load bcm2835_gpiomem");
291 +       return err;
292 +}
293 +
294 +static int bcm2835_gpiomem_remove(struct platform_device *pdev)
295 +{
296 +       struct device *dev = inst->dev;
297 +
298 +       kfree(inst);
299 +       device_destroy(bcm2835_gpiomem_class, bcm2835_gpiomem_devid);
300 +       class_destroy(bcm2835_gpiomem_class);
301 +       cdev_del(&bcm2835_gpiomem_cdev);
302 +       unregister_chrdev_region(bcm2835_gpiomem_devid, 1);
303 +
304 +       dev_info(dev, "GPIO mem driver removed - OK");
305 +       return 0;
306 +}
307 +
308 + /****************************************************************************
309 +*
310 +*   Register the driver with device tree
311 +*
312 +***************************************************************************/
313 +
314 +static const struct of_device_id bcm2835_gpiomem_of_match[] = {
315 +       {.compatible = "brcm,bcm2835-gpiomem",},
316 +       { /* sentinel */ },
317 +};
318 +
319 +MODULE_DEVICE_TABLE(of, bcm2835_gpiomem_of_match);
320 +
321 +static struct platform_driver bcm2835_gpiomem_driver = {
322 +       .probe = bcm2835_gpiomem_probe,
323 +       .remove = bcm2835_gpiomem_remove,
324 +       .driver = {
325 +                  .name = DRIVER_NAME,
326 +                  .owner = THIS_MODULE,
327 +                  .of_match_table = bcm2835_gpiomem_of_match,
328 +                  },
329 +};
330 +
331 +module_platform_driver(bcm2835_gpiomem_driver);
332 +
333 +MODULE_ALIAS("platform:gpiomem-bcm2835");
334 +MODULE_LICENSE("GPL");
335 +MODULE_DESCRIPTION("gpiomem driver for accessing GPIO from userspace");
336 +MODULE_AUTHOR("Luke Wren <luke@raspberrypi.org>");