1 From e264c7d05860aa12664fa3db8bcf8c4cbc20a62e 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
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.
12 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.org>
14 driver: char: rpivid: Destroy the legacy device on remove
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.
20 Fixes: "75f1d14 driver: char: rpivid - also support legacy name"
21 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
23 driver: char: rpivid: Clean up error handling use of ERR_PTR/IS_ERR
25 The driver used an unnecessary intermediate void* variable so it
26 only called ERR_PTR once to convert to the error value.
28 Switch to converting as the error arises to remove these intermediate
31 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
33 driver: char: rpivid: Add error handling to the legacy device load
35 The return value from device_create for the legacy device was never
36 checked or handled. Add the required error handling.
38 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
40 driver: char: rpivid: Fix coding style whitespace issues.
42 Makes checkpatch happier.
44 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
46 driver: char: rpimem: Add SPDX licence header.
48 Stops checkpatch complaining.
50 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
52 driver: char: rpivid: Fix access to freed memory
54 The error path during probe frees the private memory block, and
55 then promptly dereferences it to log an error message.
57 Use the base device instead of the pointer to it in the private
60 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
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
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.
79 + tristate "Character device driver for the Raspberry Pi RPIVid video decoder hardware"
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
93 +++ b/drivers/char/broadcom/rpivid-mem.c
95 +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
97 + * rpivid-mem.c - character device access to the RPiVid decoder registers
99 + * Based on bcm2835-gpiomem.c. Provides IO memory access to the decoder
100 + * register blocks such that ffmpeg plugins can access the hardware.
102 + * Jonathan Bell <jonathan@raspberrypi.org>
103 + * Copyright (c) 2019, Raspberry Pi (Trading) Ltd.
105 + * Redistribution and use in source and binary forms, with or without
106 + * modification, are permitted provided that the following conditions
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.
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.
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.
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>
146 +#define DRIVER_NAME "rpivid-mem"
147 +#define DEVICE_MINOR 0
149 +struct rpivid_mem_priv {
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;
159 +static int rpivid_mem_open(struct inode *inode, struct file *file)
161 + int dev = iminor(inode);
163 + struct rpivid_mem_priv *priv;
165 + if (dev != DEVICE_MINOR && dev != DEVICE_MINOR + 1)
168 + priv = container_of(inode->i_cdev, struct rpivid_mem_priv,
172 + file->private_data = priv;
176 +static int rpivid_mem_release(struct inode *inode, struct file *file)
178 + int dev = iminor(inode);
181 + if (dev != DEVICE_MINOR && dev != DEVICE_MINOR + 1)
187 +static const struct vm_operations_struct rpivid_mem_vm_ops = {
188 +#ifdef CONFIG_HAVE_IOREMAP_PROT
189 + .access = generic_access_phys
193 +static int rpivid_mem_mmap(struct file *file, struct vm_area_struct *vma)
195 + struct rpivid_mem_priv *priv;
196 + unsigned long pages;
198 + priv = file->private_data;
199 + pages = priv->regs_phys >> PAGE_SHIFT;
201 + * The address decode is far larger than the actual number of registers.
202 + * Just map the whole lot in.
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,
210 + priv->mem_window_len,
211 + vma->vm_page_prot)) {
217 +static const struct file_operations
219 + .owner = THIS_MODULE,
220 + .open = rpivid_mem_open,
221 + .release = rpivid_mem_release,
222 + .mmap = rpivid_mem_mmap,
225 +static const struct of_device_id rpivid_mem_of_match[];
226 +static int rpivid_mem_probe(struct platform_device *pdev)
229 + const struct of_device_id *id;
230 + struct device *dev = &pdev->dev;
231 + struct resource *ioresource;
232 + struct rpivid_mem_priv *priv;
234 + /* Allocate buffers and instance data */
236 + priv = kzalloc(sizeof(struct rpivid_mem_priv), GFP_KERNEL);
240 + goto failed_inst_alloc;
242 + platform_set_drvdata(pdev, priv);
245 + id = of_match_device(rpivid_mem_of_match, dev);
248 + priv->name = id->data;
250 + ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
252 + priv->regs_phys = ioresource->start;
253 + priv->mem_window_len = ioresource->end - ioresource->start;
255 + dev_err(priv->dev, "failed to get IO resource");
257 + goto failed_get_resource;
260 + /* Create character device entries */
262 + err = alloc_chrdev_region(&priv->devid,
263 + DEVICE_MINOR, 2, priv->name);
265 + dev_err(priv->dev, "unable to allocate device number");
266 + goto failed_alloc_chrdev;
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);
272 + dev_err(priv->dev, "unable to register device");
273 + goto failed_cdev_add;
276 + /* Create sysfs entries */
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;
284 + dev = device_create(priv->class, NULL, priv->devid, NULL, priv->name);
286 + err = PTR_ERR(dev);
287 + goto failed_device_create;
292 + char *oldname = kstrdup(priv->name, GFP_KERNEL);
299 + dev = device_create(priv->class, NULL, priv->devid + 1, NULL,
304 + err = PTR_ERR(dev);
305 + goto failed_legacy_device_create;
309 + dev_info(priv->dev, "%s initialised: Registers at 0x%08lx length 0x%08lx",
310 + priv->name, priv->regs_phys, priv->mem_window_len);
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);
321 + unregister_chrdev_region(priv->devid, 1);
322 +failed_alloc_chrdev:
323 +failed_get_resource:
326 + dev_err(&pdev->dev, "could not load rpivid_mem");
330 +static int rpivid_mem_remove(struct platform_device *pdev)
332 + struct device *dev = &pdev->dev;
333 + struct rpivid_mem_priv *priv = platform_get_drvdata(pdev);
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);
342 + dev_info(dev, "%s driver removed - OK", priv->name);
346 +static const struct of_device_id rpivid_mem_of_match[] = {
348 + .compatible = "raspberrypi,rpivid-hevc-decoder",
349 + .data = "rpivid-hevcmem",
352 + .compatible = "raspberrypi,rpivid-h264-decoder",
353 + .data = "rpivid-h264mem",
356 + .compatible = "raspberrypi,rpivid-vp9-decoder",
357 + .data = "rpivid-vp9mem",
359 + /* The "intc" is included as this block of hardware contains the
360 + * "frame done" status flags.
363 + .compatible = "raspberrypi,rpivid-local-intc",
364 + .data = "rpivid-intcmem",
366 + { /* sentinel */ },
369 +MODULE_DEVICE_TABLE(of, rpivid_mem_of_match);
371 +static struct platform_driver rpivid_mem_driver = {
372 + .probe = rpivid_mem_probe,
373 + .remove = rpivid_mem_remove,
375 + .name = DRIVER_NAME,
376 + .owner = THIS_MODULE,
377 + .of_match_table = rpivid_mem_of_match,
381 +module_platform_driver(rpivid_mem_driver);
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
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);
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);
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;
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
425 - power->asb = pm->arg_asb;
426 + if (pm->rpivid_asb) {
427 + power->asb = pm->rpivid_asb;
428 power->is_2711 = true;
431 --- a/include/linux/mfd/bcm2835-pm.h
432 +++ b/include/linux/mfd/bcm2835-pm.h
433 @@ -9,7 +9,7 @@ struct bcm2835_pm {
437 - void __iomem *arg_asb;
438 + void __iomem *rpivid_asb;
441 #endif /* BCM2835_MFD_PM_H */