1 From fdf9cab5eaa849e90b12e17718bc47130a91433c Mon Sep 17 00:00:00 2001
2 From: Jonathan Bell <jonathan@raspberrypi.com>
3 Date: Tue, 25 Apr 2023 15:52:13 +0100
4 Subject: [PATCH] drivers: char: add generic gpiomem driver
6 Based on bcm2835-gpiomem.
8 We allow export of the "GPIO registers" to userspace via a chardev as
9 this allows for finer access control (e.g. users must be group gpio, root
12 This driver allows access to either rp1-gpiomem or gpiomem, depending on
13 which nodes are populated in devicetree.
15 RP1 has a different look-and-feel to BCM283x SoCs as it has split ranges
16 for IO controls and the parallel registered OE/IN/OUT access. To handle
17 this, the driver concatenates the ranges for an IO bank and the
18 corresponding RIO instance into a contiguous buffer.
20 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.com>
22 drivers/char/Kconfig | 8 +
23 drivers/char/Makefile | 1 +
24 drivers/char/raspberrypi-gpiomem.c | 276 +++++++++++++++++++++++++++++
25 3 files changed, 285 insertions(+)
26 create mode 100644 drivers/char/raspberrypi-gpiomem.c
28 --- a/drivers/char/Kconfig
29 +++ b/drivers/char/Kconfig
30 @@ -461,4 +461,12 @@ config RANDOM_TRUST_BOOTLOADER
31 believe its RNG facilities may be faulty. This may also be configured
32 at boot time with "random.trust_bootloader=on/off".
34 +config RASPBERRYPI_GPIOMEM
35 + tristate "Rootless GPIO access via mmap() on Raspberry Pi boards"
38 + Provides users with root-free access to the GPIO registers
39 + on the board. Calling mmap(/dev/gpiomem) will map the GPIO
40 + register page to the user's pointer.
43 --- a/drivers/char/Makefile
44 +++ b/drivers/char/Makefile
45 @@ -46,3 +46,4 @@ obj-$(CONFIG_XILLYBUS_CLASS) += xillybus
46 obj-$(CONFIG_POWERNV_OP_PANEL) += powernv-op-panel.o
47 obj-$(CONFIG_ADI) += adi.o
48 obj-$(CONFIG_BRCM_CHAR_DRIVERS) += broadcom/
49 +obj-$(CONFIG_RASPBERRYPI_GPIOMEM) += raspberrypi-gpiomem.o
51 +++ b/drivers/char/raspberrypi-gpiomem.c
53 +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
55 + * raspberrypi-gpiomem.c
57 + * Provides MMIO access to discontiguous section of Device memory as a linear
58 + * user mapping. Successor to bcm2835-gpiomem.c.
60 + * Copyright (c) 2023, Raspberry Pi Ltd.
63 +#include <linux/kernel.h>
64 +#include <linux/module.h>
65 +#include <linux/of.h>
66 +#include <linux/of_device.h>
67 +#include <linux/platform_device.h>
68 +#include <linux/mm.h>
69 +#include <linux/slab.h>
70 +#include <linux/cdev.h>
71 +#include <linux/pagemap.h>
72 +#include <linux/io.h>
74 +#define DRIVER_NAME "rpi-gpiomem"
75 +#define DEVICE_MINOR 0
78 + * Sensible max for a hypothetical "gpio" controller that splits pads,
79 + * IO controls, GPIO in/out/enable, and function selection into different
80 + * ranges. Most use only one or two.
85 + unsigned long phys_base;
89 +struct rpi_gpiomem_priv {
91 + struct class *class;
92 + struct cdev rpi_gpiomem_cdev;
95 + unsigned int nr_wins;
96 + struct io_windows iowins[4];
99 +static int rpi_gpiomem_open(struct inode *inode, struct file *file)
101 + int dev = iminor(inode);
103 + struct rpi_gpiomem_priv *priv;
105 + if (dev != DEVICE_MINOR)
108 + priv = container_of(inode->i_cdev, struct rpi_gpiomem_priv,
112 + file->private_data = priv;
116 +static int rpi_gpiomem_release(struct inode *inode, struct file *file)
118 + int dev = iminor(inode);
121 + if (dev != DEVICE_MINOR)
127 +static const struct vm_operations_struct rpi_gpiomem_vm_ops = {
128 +#ifdef CONFIG_HAVE_IOREMAP_PROT
129 + .access = generic_access_phys
133 +static int rpi_gpiomem_mmap(struct file *file, struct vm_area_struct *vma)
136 + struct rpi_gpiomem_priv *priv;
137 + unsigned long base;
138 + unsigned long len = 0;
139 + unsigned long offset;
141 + priv = file->private_data;
143 + * Userspace must provide a virtual address space at least
144 + * the size of the concatenated ranges.
146 + for (i = 0; i < priv->nr_wins; i++)
147 + len += priv->iowins[i].len;
148 + if (len > vma->vm_end - vma->vm_start + 1)
151 + vma->vm_ops = &rpi_gpiomem_vm_ops;
152 + offset = vma->vm_start;
153 + for (i = 0; i < priv->nr_wins; i++) {
154 + base = priv->iowins[i].phys_base >> PAGE_SHIFT;
155 + len = priv->iowins[i].len;
156 + vma->vm_page_prot = phys_mem_access_prot(file, base, len,
157 + vma->vm_page_prot);
158 + if (remap_pfn_range(vma, offset,
160 + vma->vm_page_prot))
165 + if (i < priv->nr_wins)
171 +static const struct file_operations rpi_gpiomem_fops = {
172 + .owner = THIS_MODULE,
173 + .open = rpi_gpiomem_open,
174 + .release = rpi_gpiomem_release,
175 + .mmap = rpi_gpiomem_mmap,
178 +static const struct of_device_id rpi_gpiomem_of_match[];
180 +static int rpi_gpiomem_probe(struct platform_device *pdev)
183 + const struct of_device_id *id;
184 + struct device *dev = &pdev->dev;
185 + struct device_node *node = dev->of_node;
186 + struct resource *ioresource;
187 + struct rpi_gpiomem_priv *priv;
189 + /* Allocate buffers and instance data */
191 + priv = kzalloc(sizeof(struct rpi_gpiomem_priv), GFP_KERNEL);
195 + goto failed_inst_alloc;
197 + platform_set_drvdata(pdev, priv);
200 + id = of_match_device(rpi_gpiomem_of_match, dev);
205 + * Device node naming - for legacy (bcm2835) DT bindings, the driver
206 + * created the node based on a hardcoded name - for new bindings,
207 + * take the node name from DT.
209 + if (id == &rpi_gpiomem_of_match[0]) {
210 + priv->name = "gpiomem";
212 + err = of_property_read_string(node, "chardev-name", &priv->name);
218 + * Go find the register ranges associated with this instance
220 + for (i = 0; i < MAX_RANGES; i++) {
221 + ioresource = platform_get_resource(pdev, IORESOURCE_MEM, i);
222 + if (!ioresource && i == 0) {
223 + dev_err(priv->dev, "failed to get IO resource - no ranges available\n");
225 + goto failed_get_resource;
230 + priv->iowins[i].phys_base = ioresource->start;
231 + priv->iowins[i].len = (ioresource->end + 1) - ioresource->start;
232 + dev_info(&pdev->dev, "window base 0x%08lx size 0x%08lx\n",
233 + priv->iowins[i].phys_base, priv->iowins[i].len);
237 + /* Create character device entries */
239 + err = alloc_chrdev_region(&priv->devid,
240 + DEVICE_MINOR, 1, priv->name);
242 + dev_err(priv->dev, "unable to allocate device number");
243 + goto failed_alloc_chrdev;
245 + cdev_init(&priv->rpi_gpiomem_cdev, &rpi_gpiomem_fops);
246 + priv->rpi_gpiomem_cdev.owner = THIS_MODULE;
247 + err = cdev_add(&priv->rpi_gpiomem_cdev, priv->devid, 1);
249 + dev_err(priv->dev, "unable to register device");
250 + goto failed_cdev_add;
253 + /* Create sysfs entries */
255 + priv->class = class_create(THIS_MODULE, priv->name);
256 + if (IS_ERR(priv->class)) {
257 + err = PTR_ERR(priv->class);
258 + goto failed_class_create;
261 + dev = device_create(priv->class, NULL, priv->devid, NULL, priv->name);
263 + err = PTR_ERR(dev);
264 + goto failed_device_create;
267 + dev_info(priv->dev, "initialised %u regions as /dev/%s\n",
268 + priv->nr_wins, priv->name);
272 +failed_device_create:
273 + class_destroy(priv->class);
274 +failed_class_create:
275 + cdev_del(&priv->rpi_gpiomem_cdev);
277 + unregister_chrdev_region(priv->devid, 1);
278 +failed_alloc_chrdev:
279 +failed_get_resource:
282 + dev_err(&pdev->dev, "could not load rpi_gpiomem");
286 +static int rpi_gpiomem_remove(struct platform_device *pdev)
288 + struct device *dev = &pdev->dev;
289 + struct rpi_gpiomem_priv *priv = platform_get_drvdata(pdev);
291 + device_destroy(priv->class, priv->devid);
292 + class_destroy(priv->class);
293 + cdev_del(&priv->rpi_gpiomem_cdev);
294 + unregister_chrdev_region(priv->devid, 1);
297 + dev_info(dev, "%s driver removed - OK", priv->name);
301 +static const struct of_device_id rpi_gpiomem_of_match[] = {
303 + .compatible = "brcm,bcm2835-gpiomem",
306 + .compatible = "raspberrypi,gpiomem",
308 + { /* sentinel */ },
311 +MODULE_DEVICE_TABLE(of, rpi_gpiomem_of_match);
313 +static struct platform_driver rpi_gpiomem_driver = {
314 + .probe = rpi_gpiomem_probe,
315 + .remove = rpi_gpiomem_remove,
317 + .name = DRIVER_NAME,
318 + .owner = THIS_MODULE,
319 + .of_match_table = rpi_gpiomem_of_match,
323 +module_platform_driver(rpi_gpiomem_driver);
325 +MODULE_ALIAS("platform:rpi-gpiomem");
326 +MODULE_LICENSE("Dual BSD/GPL");
327 +MODULE_DESCRIPTION("Driver for accessing GPIOs from userspace");
328 +MODULE_AUTHOR("Jonathan Bell <jonathan@raspberrypi.com>");