1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Driver for Renesas R-Car VIN
4  *
5  * Copyright (C) 2016 Renesas Electronics Corp.
6  * Copyright (C) 2011-2013 Renesas Solutions Corp.
7  * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com>
8  * Copyright (C) 2008 Magnus Damm
9  *
10  * Based on the soc-camera rcar_vin driver
11  */
12 
13 #ifndef __RCAR_VIN__
14 #define __RCAR_VIN__
15 
16 #include <linux/kref.h>
17 
18 #include <media/v4l2-async.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-dev.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-fwnode.h>
23 #include <media/videobuf2-v4l2.h>
24 
25 /* Number of HW buffers */
26 #define HW_BUFFER_NUM 3
27 
28 /* Address alignment mask for HW buffers */
29 #define HW_BUFFER_MASK 0x7f
30 
31 /* Max number on VIN instances that can be in a system */
32 #define RCAR_VIN_NUM 32
33 
34 struct rvin_group;
35 
36 enum model_id {
37 	RCAR_H1,
38 	RCAR_M1,
39 	RCAR_GEN2,
40 	RCAR_GEN3,
41 };
42 
43 enum rvin_csi_id {
44 	RVIN_CSI20,
45 	RVIN_CSI21,
46 	RVIN_CSI40,
47 	RVIN_CSI41,
48 	RVIN_CSI_MAX,
49 };
50 
51 enum rvin_isp_id {
52 	RVIN_ISP0,
53 	RVIN_ISP1,
54 	RVIN_ISP2,
55 	RVIN_ISP4,
56 	RVIN_ISP_MAX,
57 };
58 
59 #define RVIN_REMOTES_MAX \
60 	(((unsigned int)RVIN_CSI_MAX) > ((unsigned int)RVIN_ISP_MAX) ? \
61 	 RVIN_CSI_MAX : RVIN_ISP_MAX)
62 
63 /**
64  * enum rvin_dma_state - DMA states
65  * @STOPPED:   No operation in progress
66  * @STARTING:  Capture starting up
67  * @RUNNING:   Operation in progress have buffers
68  * @STOPPING:  Stopping operation
69  * @SUSPENDED: Capture is suspended
70  */
71 enum rvin_dma_state {
72 	STOPPED = 0,
73 	STARTING,
74 	RUNNING,
75 	STOPPING,
76 	SUSPENDED,
77 };
78 
79 /**
80  * enum rvin_buffer_type
81  *
82  * Describes how a buffer is given to the hardware. To be able
83  * to capture SEQ_TB/BT it's needed to capture to the same vb2
84  * buffer twice so the type of buffer needs to be kept.
85  *
86  * @FULL: One capture fills the whole vb2 buffer
87  * @HALF_TOP: One capture fills the top half of the vb2 buffer
88  * @HALF_BOTTOM: One capture fills the bottom half of the vb2 buffer
89  */
90 enum rvin_buffer_type {
91 	FULL,
92 	HALF_TOP,
93 	HALF_BOTTOM,
94 };
95 
96 /**
97  * struct rvin_video_format - Data format stored in memory
98  * @fourcc:	Pixelformat
99  * @bpp:	Bytes per pixel
100  */
101 struct rvin_video_format {
102 	u32 fourcc;
103 	u8 bpp;
104 };
105 
106 /**
107  * struct rvin_parallel_entity - Parallel video input endpoint descriptor
108  * @asd:	sub-device descriptor for async framework
109  * @subdev:	subdevice matched using async framework
110  * @mbus_type:	media bus type
111  * @bus:	media bus parallel configuration
112  * @source_pad:	source pad of remote subdevice
113  * @sink_pad:	sink pad of remote subdevice
114  *
115  */
116 struct rvin_parallel_entity {
117 	struct v4l2_async_subdev *asd;
118 	struct v4l2_subdev *subdev;
119 
120 	enum v4l2_mbus_type mbus_type;
121 	struct v4l2_mbus_config_parallel bus;
122 
123 	unsigned int source_pad;
124 	unsigned int sink_pad;
125 };
126 
127 /**
128  * struct rvin_group_route - describes a route from a channel of a
129  *	CSI-2 receiver to a VIN
130  *
131  * @master:	VIN group master ID.
132  * @csi:	CSI-2 receiver ID.
133  * @chsel:	CHSEL register values that connects VIN group to CSI-2.
134  *
135  * .. note::
136  *	Each R-Car CSI-2 receiver has four output channels facing the VIN
137  *	devices, each channel can carry one CSI-2 Virtual Channel (VC).
138  *	There is no correlation between channel number and CSI-2 VC. It's
139  *	up to the CSI-2 receiver driver to configure which VC is output
140  *	on which channel, the VIN devices only care about output channels.
141  */
142 struct rvin_group_route {
143 	unsigned int master;
144 	enum rvin_csi_id csi;
145 	unsigned int chsel;
146 };
147 
148 /**
149  * struct rvin_info - Information about the particular VIN implementation
150  * @model:		VIN model
151  * @use_mc:		use media controller instead of controlling subdevice
152  * @use_isp:		the VIN is connected to the ISP and not to the CSI-2
153  * @nv12:		support outputing NV12 pixel format
154  * @max_width:		max input width the VIN supports
155  * @max_height:		max input height the VIN supports
156  * @routes:		list of possible routes from the CSI-2 recivers to
157  *			all VINs. The list mush be NULL terminated.
158  */
159 struct rvin_info {
160 	enum model_id model;
161 	bool use_mc;
162 	bool use_isp;
163 	bool nv12;
164 
165 	unsigned int max_width;
166 	unsigned int max_height;
167 	const struct rvin_group_route *routes;
168 };
169 
170 /**
171  * struct rvin_dev - Renesas VIN device structure
172  * @dev:		(OF) device
173  * @base:		device I/O register space remapped to virtual memory
174  * @info:		info about VIN instance
175  *
176  * @vdev:		V4L2 video device associated with VIN
177  * @v4l2_dev:		V4L2 device
178  * @ctrl_handler:	V4L2 control handler
179  * @notifier:		V4L2 asynchronous subdevs notifier
180  *
181  * @parallel:		parallel input subdevice descriptor
182  *
183  * @group:		Gen3 CSI group
184  * @id:			Gen3 group id for this VIN
185  * @pad:		media pad for the video device entity
186  *
187  * @lock:		protects @queue
188  * @queue:		vb2 buffers queue
189  * @scratch:		cpu address for scratch buffer
190  * @scratch_phys:	physical address of the scratch buffer
191  *
192  * @qlock:		protects @buf_hw, @buf_list, @sequence and @state
193  * @buf_hw:		Keeps track of buffers given to HW slot
194  * @buf_list:		list of queued buffers
195  * @sequence:		V4L2 buffers sequence number
196  * @state:		keeps track of operation state
197  *
198  * @is_csi:		flag to mark the VIN as using a CSI-2 subdevice
199  * @chsel:		Cached value of the current CSI-2 channel selection
200  *
201  * @mbus_code:		media bus format code
202  * @format:		active V4L2 pixel format
203  *
204  * @crop:		active cropping
205  * @compose:		active composing
206  * @src_rect:		active size of the video source
207  * @std:		active video standard of the video source
208  *
209  * @alpha:		Alpha component to fill in for supported pixel formats
210  */
211 struct rvin_dev {
212 	struct device *dev;
213 	void __iomem *base;
214 	const struct rvin_info *info;
215 
216 	struct video_device vdev;
217 	struct v4l2_device v4l2_dev;
218 	struct v4l2_ctrl_handler ctrl_handler;
219 	struct v4l2_async_notifier notifier;
220 
221 	struct rvin_parallel_entity parallel;
222 
223 	struct rvin_group *group;
224 	unsigned int id;
225 	struct media_pad pad;
226 
227 	struct mutex lock;
228 	struct vb2_queue queue;
229 	void *scratch;
230 	dma_addr_t scratch_phys;
231 
232 	spinlock_t qlock;
233 	struct {
234 		struct vb2_v4l2_buffer *buffer;
235 		enum rvin_buffer_type type;
236 		dma_addr_t phys;
237 	} buf_hw[HW_BUFFER_NUM];
238 	struct list_head buf_list;
239 	unsigned int sequence;
240 	enum rvin_dma_state state;
241 
242 	bool is_csi;
243 	unsigned int chsel;
244 
245 	u32 mbus_code;
246 	struct v4l2_pix_format format;
247 
248 	struct v4l2_rect crop;
249 	struct v4l2_rect compose;
250 	struct v4l2_rect src_rect;
251 	v4l2_std_id std;
252 
253 	unsigned int alpha;
254 };
255 
256 #define vin_to_source(vin)		((vin)->parallel.subdev)
257 
258 /* Debug */
259 #define vin_dbg(d, fmt, arg...)		dev_dbg(d->dev, fmt, ##arg)
260 #define vin_info(d, fmt, arg...)	dev_info(d->dev, fmt, ##arg)
261 #define vin_warn(d, fmt, arg...)	dev_warn(d->dev, fmt, ##arg)
262 #define vin_err(d, fmt, arg...)		dev_err(d->dev, fmt, ##arg)
263 
264 /**
265  * struct rvin_group - VIN CSI2 group information
266  * @refcount:		number of VIN instances using the group
267  *
268  * @mdev:		media device which represents the group
269  *
270  * @lock:		protects the count, notifier, vin and csi members
271  * @count:		number of enabled VIN instances found in DT
272  * @notifier:		group notifier for CSI-2 async subdevices
273  * @vin:		VIN instances which are part of the group
274  * @link_setup:		Callback to create all links for the media graph
275  * @remotes:		array of pairs of fwnode and subdev pointers
276  *			to all remote subdevices.
277  */
278 struct rvin_group {
279 	struct kref refcount;
280 
281 	struct media_device mdev;
282 
283 	struct mutex lock;
284 	unsigned int count;
285 	struct v4l2_async_notifier notifier;
286 	struct rvin_dev *vin[RCAR_VIN_NUM];
287 
288 	int (*link_setup)(struct rvin_dev *vin);
289 
290 	struct {
291 		struct v4l2_async_subdev *asd;
292 		struct v4l2_subdev *subdev;
293 	} remotes[RVIN_REMOTES_MAX];
294 };
295 
296 int rvin_dma_register(struct rvin_dev *vin, int irq);
297 void rvin_dma_unregister(struct rvin_dev *vin);
298 
299 int rvin_v4l2_register(struct rvin_dev *vin);
300 void rvin_v4l2_unregister(struct rvin_dev *vin);
301 
302 const struct rvin_video_format *rvin_format_from_pixel(struct rvin_dev *vin,
303 						       u32 pixelformat);
304 
305 
306 /* Cropping, composing and scaling */
307 void rvin_crop_scale_comp(struct rvin_dev *vin);
308 
309 int rvin_set_channel_routing(struct rvin_dev *vin, u8 chsel);
310 void rvin_set_alpha(struct rvin_dev *vin, unsigned int alpha);
311 
312 int rvin_start_streaming(struct rvin_dev *vin);
313 void rvin_stop_streaming(struct rvin_dev *vin);
314 
315 #endif
316