Skip to content

Commit 88247ef

Browse files
Jonathan Bellpopcornmix
authored andcommitted
drivers: char: add chardev for mmap'ing the RPiVid control registers
Based on the gpiomem driver, allow mapping of the decoder register spaces such that userspace can access control/status registers. This driver is intended for use with a custom ffmpeg backend accelerator prior to a v4l2 driver being written. Signed-off-by: Jonathan Bell <jonathan@raspberrypi.org>
1 parent 8f01e2d commit 88247ef

6 files changed

Lines changed: 305 additions & 10 deletions

File tree

‎drivers/char/broadcom/Kconfig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ config BCM2835_SMI_DEV
4949
This driver provides a character device interface (ioctl + read/write) to
5050
Broadcom's Secondary Memory interface. The low-level functionality is provided
5151
by the SMI driver itself.
52+
53+
config RPIVID_MEM
54+
tristate "Character device driver for the Raspberry Pi RPIVid video decoder hardware"
55+
default n
56+
help
57+
This driver provides a character device interface for memory-map operations
58+
so userspace tools can access the control and status registers of the
59+
Raspberry Pi RPiVid video decoder hardware.

‎drivers/char/broadcom/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ obj-$(CONFIG_BCM_VC_SM) += vc_sm/
44

55
obj-$(CONFIG_BCM2835_DEVGPIOMEM)+= bcm2835-gpiomem.o
66
obj-$(CONFIG_BCM2835_SMI_DEV) += bcm2835_smi_dev.o
7+
obj-$(CONFIG_RPIVID_MEM) += rpivid-mem.o

‎drivers/char/broadcom/rpivid-mem.c‎

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
/**
2+
* rpivid-mem.c - character device access to the RPiVid decoder registers
3+
*
4+
* Based on bcm2835-gpiomem.c. Provides IO memory access to the decoder
5+
* register blocks such that ffmpeg plugins can access the hardware.
6+
*
7+
* Jonathan Bell <jonathan@raspberrypi.org>
8+
* Copyright (c) 2019, Raspberry Pi (Trading) Ltd.
9+
*
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions
12+
* are met:
13+
* 1. Redistributions of source code must retain the above copyright
14+
* notice, this list of conditions, and the following disclaimer,
15+
* without modification.
16+
* 2. Redistributions in binary form must reproduce the above copyright
17+
* notice, this list of conditions and the following disclaimer in the
18+
* documentation and/or other materials provided with the distribution.
19+
* 3. The names of the above-listed copyright holders may not be used
20+
* to endorse or promote products derived from this software without
21+
* specific prior written permission.
22+
*
23+
* ALTERNATIVELY, this software may be distributed under the terms of the
24+
* GNU General Public License ("GPL") version 2, as published by the Free
25+
* Software Foundation.
26+
*
27+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28+
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38+
*/
39+
40+
#include <linux/kernel.h>
41+
#include <linux/module.h>
42+
#include <linux/of.h>
43+
#include <linux/of_device.h>
44+
#include <linux/platform_device.h>
45+
#include <linux/mm.h>
46+
#include <linux/slab.h>
47+
#include <linux/cdev.h>
48+
#include <linux/pagemap.h>
49+
#include <linux/io.h>
50+
51+
#define DRIVER_NAME "rpivid-mem"
52+
#define DEVICE_MINOR 0
53+
54+
struct rpivid_mem_priv {
55+
dev_t devid;
56+
struct class *class;
57+
struct cdev rpivid_mem_cdev;
58+
unsigned long regs_phys;
59+
unsigned long mem_window_len;
60+
struct device *dev;
61+
const char *name;
62+
};
63+
64+
static int rpivid_mem_open(struct inode *inode, struct file *file)
65+
{
66+
int dev = iminor(inode);
67+
int ret = 0;
68+
struct rpivid_mem_priv *priv;
69+
if (dev != DEVICE_MINOR && dev != DEVICE_MINOR + 1)
70+
ret = -ENXIO;
71+
72+
priv = container_of(inode->i_cdev, struct rpivid_mem_priv,
73+
rpivid_mem_cdev);
74+
if (!priv)
75+
return -EINVAL;
76+
file->private_data = priv;
77+
return ret;
78+
}
79+
80+
static int rpivid_mem_release(struct inode *inode, struct file *file)
81+
{
82+
int dev = iminor(inode);
83+
int ret = 0;
84+
85+
if (dev != DEVICE_MINOR && dev != DEVICE_MINOR + 1)
86+
ret = -ENXIO;
87+
88+
return ret;
89+
}
90+
91+
static const struct vm_operations_struct rpivid_mem_vm_ops = {
92+
#ifdef CONFIG_HAVE_IOREMAP_PROT
93+
.access = generic_access_phys
94+
#endif
95+
};
96+
97+
static int rpivid_mem_mmap(struct file *file, struct vm_area_struct *vma)
98+
{
99+
struct rpivid_mem_priv *priv;
100+
unsigned long pages;
101+
102+
priv = file->private_data;
103+
pages = priv->regs_phys >> PAGE_SHIFT;
104+
/*
105+
* The address decode is far larger than the actual number of registers.
106+
* Just map the whole lot in.
107+
*/
108+
vma->vm_page_prot = phys_mem_access_prot(file, pages,
109+
priv->mem_window_len,
110+
vma->vm_page_prot);
111+
vma->vm_ops = &rpivid_mem_vm_ops;
112+
if (remap_pfn_range(vma, vma->vm_start,
113+
pages,
114+
priv->mem_window_len,
115+
vma->vm_page_prot)) {
116+
return -EAGAIN;
117+
}
118+
return 0;
119+
}
120+
121+
static const struct file_operations
122+
rpivid_mem_fops = {
123+
.owner = THIS_MODULE,
124+
.open = rpivid_mem_open,
125+
.release = rpivid_mem_release,
126+
.mmap = rpivid_mem_mmap,
127+
};
128+
129+
static const struct of_device_id rpivid_mem_of_match[];
130+
static int rpivid_mem_probe(struct platform_device *pdev)
131+
{
132+
int err;
133+
void *ptr_err;
134+
const struct of_device_id *id;
135+
struct device *dev = &pdev->dev;
136+
struct device *rpivid_mem_dev;
137+
struct resource *ioresource;
138+
struct rpivid_mem_priv *priv;
139+
140+
141+
/* Allocate buffers and instance data */
142+
143+
priv = kzalloc(sizeof(struct rpivid_mem_priv), GFP_KERNEL);
144+
145+
if (!priv) {
146+
err = -ENOMEM;
147+
goto failed_inst_alloc;
148+
}
149+
platform_set_drvdata(pdev, priv);
150+
151+
priv->dev = dev;
152+
id = of_match_device(rpivid_mem_of_match, dev);
153+
if (!id)
154+
return -EINVAL;
155+
priv->name = id->data;
156+
157+
ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
158+
if (ioresource) {
159+
priv->regs_phys = ioresource->start;
160+
priv->mem_window_len = ioresource->end - ioresource->start;
161+
} else {
162+
dev_err(priv->dev, "failed to get IO resource");
163+
err = -ENOENT;
164+
goto failed_get_resource;
165+
}
166+
167+
/* Create character device entries */
168+
169+
err = alloc_chrdev_region(&priv->devid,
170+
DEVICE_MINOR, 2, priv->name);
171+
if (err != 0) {
172+
dev_err(priv->dev, "unable to allocate device number");
173+
goto failed_alloc_chrdev;
174+
}
175+
cdev_init(&priv->rpivid_mem_cdev, &rpivid_mem_fops);
176+
priv->rpivid_mem_cdev.owner = THIS_MODULE;
177+
err = cdev_add(&priv->rpivid_mem_cdev, priv->devid, 2);
178+
if (err != 0) {
179+
dev_err(priv->dev, "unable to register device");
180+
goto failed_cdev_add;
181+
}
182+
183+
/* Create sysfs entries */
184+
185+
priv->class = class_create(THIS_MODULE, priv->name);
186+
ptr_err = priv->class;
187+
if (IS_ERR(ptr_err))
188+
goto failed_class_create;
189+
190+
rpivid_mem_dev = device_create(priv->class, NULL,
191+
priv->devid, NULL,
192+
priv->name);
193+
ptr_err = rpivid_mem_dev;
194+
if (IS_ERR(ptr_err))
195+
goto failed_device_create;
196+
197+
/* Legacy alias */
198+
{
199+
char *oldname = kstrdup(priv->name, GFP_KERNEL);
200+
201+
oldname[1] = 'a';
202+
oldname[2] = 'r';
203+
oldname[3] = 'g';
204+
oldname[4] = 'o';
205+
oldname[5] = 'n';
206+
(void)device_create(priv->class, NULL, priv->devid + 1, NULL,
207+
oldname + 1);
208+
kfree(oldname);
209+
}
210+
211+
dev_info(priv->dev, "%s initialised: Registers at 0x%08lx length 0x%08lx",
212+
priv->name, priv->regs_phys, priv->mem_window_len);
213+
214+
return 0;
215+
216+
failed_device_create:
217+
class_destroy(priv->class);
218+
failed_class_create:
219+
cdev_del(&priv->rpivid_mem_cdev);
220+
err = PTR_ERR(ptr_err);
221+
failed_cdev_add:
222+
unregister_chrdev_region(priv->devid, 1);
223+
failed_alloc_chrdev:
224+
failed_get_resource:
225+
kfree(priv);
226+
failed_inst_alloc:
227+
dev_err(priv->dev, "could not load rpivid_mem");
228+
return err;
229+
}
230+
231+
static int rpivid_mem_remove(struct platform_device *pdev)
232+
{
233+
struct device *dev = &pdev->dev;
234+
struct rpivid_mem_priv *priv = platform_get_drvdata(pdev);
235+
236+
device_destroy(priv->class, priv->devid);
237+
class_destroy(priv->class);
238+
cdev_del(&priv->rpivid_mem_cdev);
239+
unregister_chrdev_region(priv->devid, 1);
240+
kfree(priv);
241+
242+
dev_info(dev, "%s driver removed - OK", priv->name);
243+
return 0;
244+
}
245+
246+
static const struct of_device_id rpivid_mem_of_match[] = {
247+
{
248+
.compatible = "raspberrypi,rpivid-hevc-decoder",
249+
.data = "rpivid-hevcmem",
250+
},
251+
{
252+
.compatible = "raspberrypi,rpivid-h264-decoder",
253+
.data = "rpivid-h264mem",
254+
},
255+
{
256+
.compatible = "raspberrypi,rpivid-vp9-decoder",
257+
.data = "rpivid-vp9mem",
258+
},
259+
/* The "intc" is included as this block of hardware contains the
260+
* "frame done" status flags.
261+
*/
262+
{
263+
.compatible = "raspberrypi,rpivid-local-intc",
264+
.data = "rpivid-intcmem",
265+
},
266+
{ /* sentinel */ },
267+
};
268+
269+
MODULE_DEVICE_TABLE(of, rpivid_mem_of_match);
270+
271+
static struct platform_driver rpivid_mem_driver = {
272+
.probe = rpivid_mem_probe,
273+
.remove = rpivid_mem_remove,
274+
.driver = {
275+
.name = DRIVER_NAME,
276+
.owner = THIS_MODULE,
277+
.of_match_table = rpivid_mem_of_match,
278+
},
279+
};
280+
281+
module_platform_driver(rpivid_mem_driver);
282+
283+
MODULE_ALIAS("platform:rpivid-mem");
284+
MODULE_LICENSE("GPL");
285+
MODULE_DESCRIPTION("Driver for accessing RPiVid decoder registers from userspace");
286+
MODULE_AUTHOR("Jonathan Bell <jonathan@raspberrypi.org>");

‎drivers/mfd/bcm2835-pm.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ static int bcm2835_pm_probe(struct platform_device *pdev)
5050
if (ret)
5151
return ret;
5252

53-
/* Map the ARGON ASB regs if present. */
53+
/* Map the RPiVid ASB regs if present. */
5454
res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
5555
if (res) {
56-
pm->arg_asb = devm_ioremap_resource(dev, res);
57-
if (IS_ERR(pm->arg_asb)) {
58-
dev_err(dev, "Failed to map ARGON ASB: %ld\n",
59-
PTR_ERR(pm->arg_asb));
60-
return PTR_ERR(pm->arg_asb);
56+
pm->rpivid_asb = devm_ioremap_resource(dev, res);
57+
if (IS_ERR(pm->rpivid_asb)) {
58+
dev_err(dev, "Failed to map RPiVid ASB: %ld\n",
59+
PTR_ERR(pm->rpivid_asb));
60+
return PTR_ERR(pm->rpivid_asb);
6161
}
6262
}
6363

‎drivers/soc/bcm/bcm2835-power.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,15 +637,15 @@ static int bcm2835_power_probe(struct platform_device *pdev)
637637
power->base = pm->base;
638638
power->asb = pm->asb;
639639

640-
/* 2711 hack: the new ARGON ASB took over V3D, which is our
640+
/* 2711 hack: the new RPiVid ASB took over V3D, which is our
641641
* only consumer of this driver so far. The old ASB seems to
642642
* still be present with ISP and H264 bits but no V3D, but I
643643
* don't know if that's real or not. The V3D is in the same
644644
* place in the new ASB as the old one, so just poke the new
645645
* one for now.
646646
*/
647-
if (pm->arg_asb) {
648-
power->asb = pm->arg_asb;
647+
if (pm->rpivid_asb) {
648+
power->asb = pm->rpivid_asb;
649649
power->is_2711 = true;
650650
}
651651

‎include/linux/mfd/bcm2835-pm.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct bcm2835_pm {
99
struct device *dev;
1010
void __iomem *base;
1111
void __iomem *asb;
12-
void __iomem *arg_asb;
12+
void __iomem *rpivid_asb;
1313
};
1414

1515
#endif /* BCM2835_MFD_PM_H */

0 commit comments

Comments
 (0)