c85d882c12ba5b4061da5d50b7a9d106ff02280c
[openwrt/staging/neocturne.git] /
1 From 663bb3e36ffec5228f3126b021b0d78efb002ffe Mon Sep 17 00:00:00 2001
2 From: Jonathan Bell <jonathan@raspberrypi.org>
3 Date: Thu, 9 May 2019 14:30:37 +0100
4 Subject: [PATCH] drivers: char: add chardev for mmap'ing the RPiVid
5 control registers
6
7 Based on the gpiomem driver, allow mapping of the decoder register
8 spaces such that userspace can access control/status registers.
9 This driver is intended for use with a custom ffmpeg backend accelerator
10 prior to a v4l2 driver being written.
11
12 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.org>
13
14 driver: char: rpivid: Destroy the legacy device on remove
15
16 The legacy name support created a new device that was never destroyed.
17 If the driver was unloaded and reloaded, it failed due to the
18 device already existing.
19
20 Fixes: "75f1d14 driver: char: rpivid - also support legacy name"
21 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
22
23 driver: char: rpivid: Clean up error handling use of ERR_PTR/IS_ERR
24
25 The driver used an unnecessary intermediate void* variable so it
26 only called ERR_PTR once to convert to the error value.
27
28 Switch to converting as the error arises to remove these intermediate
29 variables.
30
31 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
32
33 driver: char: rpivid: Add error handling to the legacy device load
34
35 The return value from device_create for the legacy device was never
36 checked or handled. Add the required error handling.
37
38 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
39
40 driver: char: rpivid: Fix coding style whitespace issues.
41
42 Makes checkpatch happier.
43
44 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
45
46 driver: char: rpimem: Add SPDX licence header.
47
48 Stops checkpatch complaining.
49
50 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
51
52 driver: char: rpivid: Fix access to freed memory
53
54 The error path during probe frees the private memory block, and
55 then promptly dereferences it to log an error message.
56
57 Use the base device instead of the pointer to it in the private
58 structure.
59
60 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
61 ---
62 drivers/char/broadcom/Kconfig | 8 +
63 drivers/char/broadcom/Makefile | 1 +
64 drivers/char/broadcom/rpivid-mem.c | 292 +++++++++++++++++++++++++++++
65 drivers/mfd/bcm2835-pm.c | 12 +-
66 drivers/soc/bcm/bcm2835-power.c | 6 +-
67 include/linux/mfd/bcm2835-pm.h | 2 +-
68 6 files changed, 311 insertions(+), 10 deletions(-)
69 create mode 100644 drivers/char/broadcom/rpivid-mem.c
70
71 --- a/drivers/char/broadcom/Kconfig
72 +++ b/drivers/char/broadcom/Kconfig
73 @@ -39,3 +39,11 @@ config BCM2835_SMI_DEV
74 This driver provides a character device interface (ioctl + read/write) to
75 Broadcom's Secondary Memory interface. The low-level functionality is provided
76 by the SMI driver itself.
77 +
78 +config RPIVID_MEM
79 + tristate "Character device driver for the Raspberry Pi RPIVid video decoder hardware"
80 + default n
81 + help
82 + This driver provides a character device interface for memory-map operations
83 + so userspace tools can access the control and status registers of the
84 + Raspberry Pi RPiVid video decoder hardware.
85 --- a/drivers/char/broadcom/Makefile
86 +++ b/drivers/char/broadcom/Makefile
87 @@ -2,3 +2,4 @@ obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
88 obj-$(CONFIG_BCM_VCIO) += vcio.o
89 obj-$(CONFIG_BCM2835_DEVGPIOMEM)+= bcm2835-gpiomem.o
90 obj-$(CONFIG_BCM2835_SMI_DEV) += bcm2835_smi_dev.o
91 +obj-$(CONFIG_RPIVID_MEM) += rpivid-mem.o
92 --- /dev/null
93 +++ b/drivers/char/broadcom/rpivid-mem.c
94 @@ -0,0 +1,292 @@
95 +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
96 +/**
97 + * rpivid-mem.c - character device access to the RPiVid decoder registers
98 + *
99 + * Based on bcm2835-gpiomem.c. Provides IO memory access to the decoder
100 + * register blocks such that ffmpeg plugins can access the hardware.
101 + *
102 + * Jonathan Bell <jonathan@raspberrypi.org>
103 + * Copyright (c) 2019, Raspberry Pi (Trading) Ltd.
104 + *
105 + * Redistribution and use in source and binary forms, with or without
106 + * modification, are permitted provided that the following conditions
107 + * are met:
108 + * 1. Redistributions of source code must retain the above copyright
109 + * notice, this list of conditions, and the following disclaimer,
110 + * without modification.
111 + * 2. Redistributions in binary form must reproduce the above copyright
112 + * notice, this list of conditions and the following disclaimer in the
113 + * documentation and/or other materials provided with the distribution.
114 + * 3. The names of the above-listed copyright holders may not be used
115 + * to endorse or promote products derived from this software without
116 + * specific prior written permission.
117 + *
118 + * ALTERNATIVELY, this software may be distributed under the terms of the
119 + * GNU General Public License ("GPL") version 2, as published by the Free
120 + * Software Foundation.
121 + *
122 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
123 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
124 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
125 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
126 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
127 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
128 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
129 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
130 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
131 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
132 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
133 + */
134 +
135 +#include <linux/kernel.h>
136 +#include <linux/module.h>
137 +#include <linux/of.h>
138 +#include <linux/of_device.h>
139 +#include <linux/platform_device.h>
140 +#include <linux/mm.h>
141 +#include <linux/slab.h>
142 +#include <linux/cdev.h>
143 +#include <linux/pagemap.h>
144 +#include <linux/io.h>
145 +
146 +#define DRIVER_NAME "rpivid-mem"
147 +#define DEVICE_MINOR 0
148 +
149 +struct rpivid_mem_priv {
150 + dev_t devid;
151 + struct class *class;
152 + struct cdev rpivid_mem_cdev;
153 + unsigned long regs_phys;
154 + unsigned long mem_window_len;
155 + struct device *dev;
156 + const char *name;
157 +};
158 +
159 +static int rpivid_mem_open(struct inode *inode, struct file *file)
160 +{
161 + int dev = iminor(inode);
162 + int ret = 0;
163 + struct rpivid_mem_priv *priv;
164 +
165 + if (dev != DEVICE_MINOR && dev != DEVICE_MINOR + 1)
166 + ret = -ENXIO;
167 +
168 + priv = container_of(inode->i_cdev, struct rpivid_mem_priv,
169 + rpivid_mem_cdev);
170 + if (!priv)
171 + return -EINVAL;
172 + file->private_data = priv;
173 + return ret;
174 +}
175 +
176 +static int rpivid_mem_release(struct inode *inode, struct file *file)
177 +{
178 + int dev = iminor(inode);
179 + int ret = 0;
180 +
181 + if (dev != DEVICE_MINOR && dev != DEVICE_MINOR + 1)
182 + ret = -ENXIO;
183 +
184 + return ret;
185 +}
186 +
187 +static const struct vm_operations_struct rpivid_mem_vm_ops = {
188 +#ifdef CONFIG_HAVE_IOREMAP_PROT
189 + .access = generic_access_phys
190 +#endif
191 +};
192 +
193 +static int rpivid_mem_mmap(struct file *file, struct vm_area_struct *vma)
194 +{
195 + struct rpivid_mem_priv *priv;
196 + unsigned long pages;
197 +
198 + priv = file->private_data;
199 + pages = priv->regs_phys >> PAGE_SHIFT;
200 + /*
201 + * The address decode is far larger than the actual number of registers.
202 + * Just map the whole lot in.
203 + */
204 + vma->vm_page_prot = phys_mem_access_prot(file, pages,
205 + priv->mem_window_len,
206 + vma->vm_page_prot);
207 + vma->vm_ops = &rpivid_mem_vm_ops;
208 + if (remap_pfn_range(vma, vma->vm_start,
209 + pages,
210 + priv->mem_window_len,
211 + vma->vm_page_prot)) {
212 + return -EAGAIN;
213 + }
214 + return 0;
215 +}
216 +
217 +static const struct file_operations
218 +rpivid_mem_fops = {
219 + .owner = THIS_MODULE,
220 + .open = rpivid_mem_open,
221 + .release = rpivid_mem_release,
222 + .mmap = rpivid_mem_mmap,
223 +};
224 +
225 +static const struct of_device_id rpivid_mem_of_match[];
226 +static int rpivid_mem_probe(struct platform_device *pdev)
227 +{
228 + int err;
229 + const struct of_device_id *id;
230 + struct device *dev = &pdev->dev;
231 + struct resource *ioresource;
232 + struct rpivid_mem_priv *priv;
233 +
234 + /* Allocate buffers and instance data */
235 +
236 + priv = kzalloc(sizeof(struct rpivid_mem_priv), GFP_KERNEL);
237 +
238 + if (!priv) {
239 + err = -ENOMEM;
240 + goto failed_inst_alloc;
241 + }
242 + platform_set_drvdata(pdev, priv);
243 +
244 + priv->dev = dev;
245 + id = of_match_device(rpivid_mem_of_match, dev);
246 + if (!id)
247 + return -EINVAL;
248 + priv->name = id->data;
249 +
250 + ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
251 + if (ioresource) {
252 + priv->regs_phys = ioresource->start;
253 + priv->mem_window_len = ioresource->end - ioresource->start;
254 + } else {
255 + dev_err(priv->dev, "failed to get IO resource");
256 + err = -ENOENT;
257 + goto failed_get_resource;
258 + }
259 +
260 + /* Create character device entries */
261 +
262 + err = alloc_chrdev_region(&priv->devid,
263 + DEVICE_MINOR, 2, priv->name);
264 + if (err != 0) {
265 + dev_err(priv->dev, "unable to allocate device number");
266 + goto failed_alloc_chrdev;
267 + }
268 + cdev_init(&priv->rpivid_mem_cdev, &rpivid_mem_fops);
269 + priv->rpivid_mem_cdev.owner = THIS_MODULE;
270 + err = cdev_add(&priv->rpivid_mem_cdev, priv->devid, 2);
271 + if (err != 0) {
272 + dev_err(priv->dev, "unable to register device");
273 + goto failed_cdev_add;
274 + }
275 +
276 + /* Create sysfs entries */
277 +
278 + priv->class = class_create(THIS_MODULE, priv->name);
279 + if (IS_ERR(priv->class)) {
280 + err = PTR_ERR(priv->class);
281 + goto failed_class_create;
282 + }
283 +
284 + dev = device_create(priv->class, NULL, priv->devid, NULL, priv->name);
285 + if (IS_ERR(dev)) {
286 + err = PTR_ERR(dev);
287 + goto failed_device_create;
288 + }
289 +
290 + /* Legacy alias */
291 + {
292 + char *oldname = kstrdup(priv->name, GFP_KERNEL);
293 +
294 + oldname[1] = 'a';
295 + oldname[2] = 'r';
296 + oldname[3] = 'g';
297 + oldname[4] = 'o';
298 + oldname[5] = 'n';
299 + dev = device_create(priv->class, NULL, priv->devid + 1, NULL,
300 + oldname + 1);
301 + kfree(oldname);
302 +
303 + if (IS_ERR(dev)) {
304 + err = PTR_ERR(dev);
305 + goto failed_legacy_device_create;
306 + }
307 + }
308 +
309 + dev_info(priv->dev, "%s initialised: Registers at 0x%08lx length 0x%08lx",
310 + priv->name, priv->regs_phys, priv->mem_window_len);
311 +
312 + return 0;
313 +
314 +failed_legacy_device_create:
315 + device_destroy(priv->class, priv->devid);
316 +failed_device_create:
317 + class_destroy(priv->class);
318 +failed_class_create:
319 + cdev_del(&priv->rpivid_mem_cdev);
320 +failed_cdev_add:
321 + unregister_chrdev_region(priv->devid, 1);
322 +failed_alloc_chrdev:
323 +failed_get_resource:
324 + kfree(priv);
325 +failed_inst_alloc:
326 + dev_err(&pdev->dev, "could not load rpivid_mem");
327 + return err;
328 +}
329 +
330 +static int rpivid_mem_remove(struct platform_device *pdev)
331 +{
332 + struct device *dev = &pdev->dev;
333 + struct rpivid_mem_priv *priv = platform_get_drvdata(pdev);
334 +
335 + device_destroy(priv->class, priv->devid + 1);
336 + device_destroy(priv->class, priv->devid);
337 + class_destroy(priv->class);
338 + cdev_del(&priv->rpivid_mem_cdev);
339 + unregister_chrdev_region(priv->devid, 1);
340 + kfree(priv);
341 +
342 + dev_info(dev, "%s driver removed - OK", priv->name);
343 + return 0;
344 +}
345 +
346 +static const struct of_device_id rpivid_mem_of_match[] = {
347 + {
348 + .compatible = "raspberrypi,rpivid-hevc-decoder",
349 + .data = "rpivid-hevcmem",
350 + },
351 + {
352 + .compatible = "raspberrypi,rpivid-h264-decoder",
353 + .data = "rpivid-h264mem",
354 + },
355 + {
356 + .compatible = "raspberrypi,rpivid-vp9-decoder",
357 + .data = "rpivid-vp9mem",
358 + },
359 + /* The "intc" is included as this block of hardware contains the
360 + * "frame done" status flags.
361 + */
362 + {
363 + .compatible = "raspberrypi,rpivid-local-intc",
364 + .data = "rpivid-intcmem",
365 + },
366 + { /* sentinel */ },
367 +};
368 +
369 +MODULE_DEVICE_TABLE(of, rpivid_mem_of_match);
370 +
371 +static struct platform_driver rpivid_mem_driver = {
372 + .probe = rpivid_mem_probe,
373 + .remove = rpivid_mem_remove,
374 + .driver = {
375 + .name = DRIVER_NAME,
376 + .owner = THIS_MODULE,
377 + .of_match_table = rpivid_mem_of_match,
378 + },
379 +};
380 +
381 +module_platform_driver(rpivid_mem_driver);
382 +
383 +MODULE_ALIAS("platform:rpivid-mem");
384 +MODULE_LICENSE("GPL");
385 +MODULE_DESCRIPTION("Driver for accessing RPiVid decoder registers from userspace");
386 +MODULE_AUTHOR("Jonathan Bell <jonathan@raspberrypi.org>");
387 --- a/drivers/mfd/bcm2835-pm.c
388 +++ b/drivers/mfd/bcm2835-pm.c
389 @@ -50,14 +50,14 @@ static int bcm2835_pm_probe(struct platf
390 if (ret)
391 return ret;
392
393 - /* Map the ARGON ASB regs if present. */
394 + /* Map the RPiVid ASB regs if present. */
395 res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
396 if (res) {
397 - pm->arg_asb = devm_ioremap_resource(dev, res);
398 - if (IS_ERR(pm->arg_asb)) {
399 - dev_err(dev, "Failed to map ARGON ASB: %ld\n",
400 - PTR_ERR(pm->arg_asb));
401 - return PTR_ERR(pm->arg_asb);
402 + pm->rpivid_asb = devm_ioremap_resource(dev, res);
403 + if (IS_ERR(pm->rpivid_asb)) {
404 + dev_err(dev, "Failed to map RPiVid ASB: %ld\n",
405 + PTR_ERR(pm->rpivid_asb));
406 + return PTR_ERR(pm->rpivid_asb);
407 }
408 }
409
410 --- a/drivers/soc/bcm/bcm2835-power.c
411 +++ b/drivers/soc/bcm/bcm2835-power.c
412 @@ -637,15 +637,15 @@ static int bcm2835_power_probe(struct pl
413 power->base = pm->base;
414 power->asb = pm->asb;
415
416 - /* 2711 hack: the new ARGON ASB took over V3D, which is our
417 + /* 2711 hack: the new RPiVid ASB took over V3D, which is our
418 * only consumer of this driver so far. The old ASB seems to
419 * still be present with ISP and H264 bits but no V3D, but I
420 * don't know if that's real or not. The V3D is in the same
421 * place in the new ASB as the old one, so just poke the new
422 * one for now.
423 */
424 - if (pm->arg_asb) {
425 - power->asb = pm->arg_asb;
426 + if (pm->rpivid_asb) {
427 + power->asb = pm->rpivid_asb;
428 power->is_2711 = true;
429 }
430
431 --- a/include/linux/mfd/bcm2835-pm.h
432 +++ b/include/linux/mfd/bcm2835-pm.h
433 @@ -9,7 +9,7 @@ struct bcm2835_pm {
434 struct device *dev;
435 void __iomem *base;
436 void __iomem *asb;
437 - void __iomem *arg_asb;
438 + void __iomem *rpivid_asb;
439 };
440
441 #endif /* BCM2835_MFD_PM_H */