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