xref: /linux/drivers/media/platform/renesas/rcar-isp/risp-core.h (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (C) 2026 Renesas Electronics Corp.
4  * Copyright (C) 2026 Ideas on Board Oy
5  * Copyright (C) 2026 Ragnatech AB
6  */
7 
8 #ifndef __RCAR_ISP__
9 #define __RCAR_ISP__
10 
11 #include <linux/clk.h>
12 #include <linux/mutex.h>
13 #include <linux/platform_device.h>
14 #include <linux/reset.h>
15 #include <linux/spinlock.h>
16 #include <linux/videodev2.h>
17 
18 #include <media/v4l2-device.h>
19 #include <media/v4l2-subdev.h>
20 #include <media/videobuf2-core.h>
21 #include <media/videobuf2-dma-contig.h>
22 
23 #include <media/rppx1.h>
24 #include <media/vsp1.h>
25 
26 /* Max 2048 address + value pairs in one VSPX buffer, increase if needed. */
27 #define RISP_IO_PARAMS_BUF_SIZE	16384
28 
29 struct rcar_isp_core;
30 
31 enum risp_core_pads {
32 	RISP_CORE_INPUT1,
33 	RISP_CORE_PARAMS,
34 	RISP_CORE_STATS,
35 	RISP_CORE_OUTPUT1,
36 	RISP_CORE_NUM_PADS,
37 };
38 
39 /**
40  * struct risp_buffer - Describe an IO buffer
41  * @vb:		The VB2 buffer
42  * @list:	List of buffers queued to the IO queue
43  * @vsp_buffer:	Buffer mapped from VSP-X, only used for params IO
44  */
45 struct risp_buffer {
46 	struct vb2_v4l2_buffer vb;
47 	struct list_head list;
48 	struct vsp1_isp_buffer_desc vsp_buffer;
49 };
50 
51 /**
52  * struct rcar_isp_core_io - Information for a IO video devices
53  * @core:	Backlink to the common ISP core structure
54  *
55  * @lock:	Protects @vdev, @pad and @queue + open/close fops
56  * @vdev:	V4L2 video device associated with this IO port
57  * @pad:	Media pad for @vdev
58  * @queue:	VB2 buffers queue for $@vdev
59  *
60  * @streaming:	Flag to indicate if device is streaming, or not
61  * @buffers:	List of buffers queued to the device
62  *
63  * @format:	The active V4L2 format
64  */
65 struct rcar_isp_core_io {
66 	struct rcar_isp_core *core;
67 
68 	struct mutex lock; /* See KDoc block. */
69 	struct video_device vdev;
70 	struct media_pad pad;
71 	struct vb2_queue queue;
72 
73 	bool streaming;
74 	struct list_head buffers;
75 
76 	struct v4l2_format format;
77 };
78 
79 /**
80  * struct rcar_isp_job - R-Car ISP job description
81  *
82  * Both done_isp and done_vspx shall be set before the job can be considered
83  * completely done.
84  *
85  * @buffers: IO buffers that form a job
86  * @vspx_job: VSPX job description
87  * @job_queue: list handle
88  * @done_isp: Flag to indicate the ISP is done with the job
89  * @done_vspx: Flag to indicate the VSPX is done with the job
90  */
91 struct rcar_isp_job {
92 	struct risp_buffer *buffers[RISP_CORE_NUM_PADS];
93 	struct vsp1_isp_job_desc vspx_job;
94 	struct list_head job_queue;
95 	bool done_isp;
96 	bool done_vspx;
97 };
98 
99 /**
100  * struct rcar_isp_vspx - R-Car ISP job description
101  *
102  * @dev: Device reference to VSPX
103  * @job: Job currently being processed by VSPX
104  */
105 struct rcar_isp_vspx {
106 	struct device *dev;
107 	struct rcar_isp_job *job;
108 };
109 
110 /**
111  * struct rcar_isp_core - ISP Core
112  * @dev:	(OF) device
113  * @rppaddr:	Hardware address of the RPP ISP (from OF)
114  * @clk:	The clock for the ISP CORE
115  * @rstc:	The reset for the ISP Core
116  * @csrstc:	The reset for the ISP Channel Selector
117  *
118  * @base:	MMIO base of the ISP CORE
119  * @csbase:	MMIO base of the ISP CS
120  *
121  * @subdev:	V4L2 subdevice to represent the ISP CORE
122  * @pads:	Media pad for @subdev
123  *
124  * @v4l2_dev:	V4L2 device
125  * @rpp:	Handle to the RPP ISP connected to the ISP CORE
126  *
127  * @io_lock:	Protect io[*].streaming and io[*].buffers
128  * @io:		Array of IO ports to the ISP CORE
129  *
130  * @lock:	Protects @vspx, @risp_jobs, @sequence and @streaming
131  * @vspx:	Handle to the resources used by VSPX connected to the ISP CORE
132  * @risp_jobs:	Queue of VSPX transfer jobs
133  * @sequence:	V4L2 buffers sequence number
134  * @streaming:	Tracks if the device is streaming
135  */
136 struct rcar_isp_core {
137 	struct device *dev;
138 
139 	u32 rppaddr;
140 	struct clk *clk;
141 
142 	struct reset_control *rstc;
143 	struct reset_control *csrstc;
144 
145 	void __iomem *base;
146 	void __iomem *csbase;
147 
148 	struct v4l2_subdev subdev;
149 	struct media_pad pads[RISP_CORE_NUM_PADS];
150 
151 	struct v4l2_device v4l2_dev;
152 	struct rppx1 *rpp;
153 
154 	struct mutex io_lock; /* See KDoc block. */
155 	struct rcar_isp_core_io io[RISP_CORE_NUM_PADS];
156 
157 	spinlock_t lock;  /* See KDoc block. */
158 	struct rcar_isp_vspx vspx;
159 	struct list_head risp_jobs;
160 	unsigned int sequence;
161 	bool streaming;
162 };
163 
164 int risp_core_probe(struct rcar_isp_core *core, struct platform_device *pdev,
165 		    void __iomem *csbase, struct reset_control *csrstc);
166 void risp_core_remove(struct rcar_isp_core *core);
167 int risp_core_registered(struct rcar_isp_core *core, struct v4l2_subdev *sd);
168 
169 int risp_core_job_prepare(struct rcar_isp_core *core);
170 
171 int risp_core_start_streaming(struct rcar_isp_core *core);
172 void risp_core_stop_streaming(struct rcar_isp_core *core);
173 
174 int risp_core_io_create(struct device *dev, struct rcar_isp_core *core,
175 			struct rcar_isp_core_io *io, unsigned int pad);
176 void risp_core_io_destroy(struct rcar_isp_core_io *io);
177 
178 #endif
179