bcm27xx: update 6.1 patches to latest version
[openwrt/staging/dangole.git] / target / linux / bcm27xx / patches-6.1 / 950-0907-drivers-char-add-generic-gpiomem-driver.patch
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
5
6 Based on bcm2835-gpiomem.
7
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
10 not required).
11
12 This driver allows access to either rp1-gpiomem or gpiomem, depending on
13 which nodes are populated in devicetree.
14
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.
19
20 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.com>
21 ---
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
27
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".
33
34 +config RASPBERRYPI_GPIOMEM
35 + tristate "Rootless GPIO access via mmap() on Raspberry Pi boards"
36 + default n
37 + help
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.
41 +
42 endmenu
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
50 --- /dev/null
51 +++ b/drivers/char/raspberrypi-gpiomem.c
52 @@ -0,0 +1,276 @@
53 +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
54 +/**
55 + * raspberrypi-gpiomem.c
56 + *
57 + * Provides MMIO access to discontiguous section of Device memory as a linear
58 + * user mapping. Successor to bcm2835-gpiomem.c.
59 + *
60 + * Copyright (c) 2023, Raspberry Pi Ltd.
61 + */
62 +
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>
73 +
74 +#define DRIVER_NAME "rpi-gpiomem"
75 +#define DEVICE_MINOR 0
76 +
77 +/*
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.
81 + */
82 +#define MAX_RANGES 4
83 +
84 +struct io_windows {
85 + unsigned long phys_base;
86 + unsigned long len;
87 +};
88 +
89 +struct rpi_gpiomem_priv {
90 + dev_t devid;
91 + struct class *class;
92 + struct cdev rpi_gpiomem_cdev;
93 + struct device *dev;
94 + const char *name;
95 + unsigned int nr_wins;
96 + struct io_windows iowins[4];
97 +};
98 +
99 +static int rpi_gpiomem_open(struct inode *inode, struct file *file)
100 +{
101 + int dev = iminor(inode);
102 + int ret = 0;
103 + struct rpi_gpiomem_priv *priv;
104 +
105 + if (dev != DEVICE_MINOR)
106 + ret = -ENXIO;
107 +
108 + priv = container_of(inode->i_cdev, struct rpi_gpiomem_priv,
109 + rpi_gpiomem_cdev);
110 + if (!priv)
111 + return -EINVAL;
112 + file->private_data = priv;
113 + return ret;
114 +}
115 +
116 +static int rpi_gpiomem_release(struct inode *inode, struct file *file)
117 +{
118 + int dev = iminor(inode);
119 + int ret = 0;
120 +
121 + if (dev != DEVICE_MINOR)
122 + ret = -ENXIO;
123 +
124 + return ret;
125 +}
126 +
127 +static const struct vm_operations_struct rpi_gpiomem_vm_ops = {
128 +#ifdef CONFIG_HAVE_IOREMAP_PROT
129 + .access = generic_access_phys
130 +#endif
131 +};
132 +
133 +static int rpi_gpiomem_mmap(struct file *file, struct vm_area_struct *vma)
134 +{
135 + int i;
136 + struct rpi_gpiomem_priv *priv;
137 + unsigned long base;
138 + unsigned long len = 0;
139 + unsigned long offset;
140 +
141 + priv = file->private_data;
142 + /*
143 + * Userspace must provide a virtual address space at least
144 + * the size of the concatenated ranges.
145 + */
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)
149 + return -EINVAL;
150 +
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,
159 + base, len,
160 + vma->vm_page_prot))
161 + break;
162 + offset += len;
163 + }
164 +
165 + if (i < priv->nr_wins)
166 + return -EAGAIN;
167 +
168 + return 0;
169 +}
170 +
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,
176 +};
177 +
178 +static const struct of_device_id rpi_gpiomem_of_match[];
179 +
180 +static int rpi_gpiomem_probe(struct platform_device *pdev)
181 +{
182 + int err, i;
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;
188 +
189 + /* Allocate buffers and instance data */
190 +
191 + priv = kzalloc(sizeof(struct rpi_gpiomem_priv), GFP_KERNEL);
192 +
193 + if (!priv) {
194 + err = -ENOMEM;
195 + goto failed_inst_alloc;
196 + }
197 + platform_set_drvdata(pdev, priv);
198 +
199 + priv->dev = dev;
200 + id = of_match_device(rpi_gpiomem_of_match, dev);
201 + if (!id)
202 + return -EINVAL;
203 +
204 + /*
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.
208 + */
209 + if (id == &rpi_gpiomem_of_match[0]) {
210 + priv->name = "gpiomem";
211 + } else {
212 + err = of_property_read_string(node, "chardev-name", &priv->name);
213 + if (err)
214 + return -EINVAL;
215 + }
216 +
217 + /*
218 + * Go find the register ranges associated with this instance
219 + */
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");
224 + err = -ENOENT;
225 + goto failed_get_resource;
226 + }
227 + if (!ioresource)
228 + break;
229 +
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);
234 + priv->nr_wins++;
235 + }
236 +
237 + /* Create character device entries */
238 +
239 + err = alloc_chrdev_region(&priv->devid,
240 + DEVICE_MINOR, 1, priv->name);
241 + if (err != 0) {
242 + dev_err(priv->dev, "unable to allocate device number");
243 + goto failed_alloc_chrdev;
244 + }
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);
248 + if (err != 0) {
249 + dev_err(priv->dev, "unable to register device");
250 + goto failed_cdev_add;
251 + }
252 +
253 + /* Create sysfs entries */
254 +
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;
259 + }
260 +
261 + dev = device_create(priv->class, NULL, priv->devid, NULL, priv->name);
262 + if (IS_ERR(dev)) {
263 + err = PTR_ERR(dev);
264 + goto failed_device_create;
265 + }
266 +
267 + dev_info(priv->dev, "initialised %u regions as /dev/%s\n",
268 + priv->nr_wins, priv->name);
269 +
270 + return 0;
271 +
272 +failed_device_create:
273 + class_destroy(priv->class);
274 +failed_class_create:
275 + cdev_del(&priv->rpi_gpiomem_cdev);
276 +failed_cdev_add:
277 + unregister_chrdev_region(priv->devid, 1);
278 +failed_alloc_chrdev:
279 +failed_get_resource:
280 + kfree(priv);
281 +failed_inst_alloc:
282 + dev_err(&pdev->dev, "could not load rpi_gpiomem");
283 + return err;
284 +}
285 +
286 +static int rpi_gpiomem_remove(struct platform_device *pdev)
287 +{
288 + struct device *dev = &pdev->dev;
289 + struct rpi_gpiomem_priv *priv = platform_get_drvdata(pdev);
290 +
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);
295 + kfree(priv);
296 +
297 + dev_info(dev, "%s driver removed - OK", priv->name);
298 + return 0;
299 +}
300 +
301 +static const struct of_device_id rpi_gpiomem_of_match[] = {
302 + {
303 + .compatible = "brcm,bcm2835-gpiomem",
304 + },
305 + {
306 + .compatible = "raspberrypi,gpiomem",
307 + },
308 + { /* sentinel */ },
309 +};
310 +
311 +MODULE_DEVICE_TABLE(of, rpi_gpiomem_of_match);
312 +
313 +static struct platform_driver rpi_gpiomem_driver = {
314 + .probe = rpi_gpiomem_probe,
315 + .remove = rpi_gpiomem_remove,
316 + .driver = {
317 + .name = DRIVER_NAME,
318 + .owner = THIS_MODULE,
319 + .of_match_table = rpi_gpiomem_of_match,
320 + },
321 +};
322 +
323 +module_platform_driver(rpi_gpiomem_driver);
324 +
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>");