• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 8
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 /**
52  * enum rvin_dma_state - DMA states
53  * @STOPPED:   No operation in progress
54  * @STARTING:  Capture starting up
55  * @RUNNING:   Operation in progress have buffers
56  * @STOPPING:  Stopping operation
57  * @SUSPENDED: Capture is suspended
58  */
59 enum rvin_dma_state {
60 	STOPPED = 0,
61 	STARTING,
62 	RUNNING,
63 	STOPPING,
64 	SUSPENDED,
65 };
66 
67 /**
68  * enum rvin_buffer_type
69  *
70  * Describes how a buffer is given to the hardware. To be able
71  * to capture SEQ_TB/BT it's needed to capture to the same vb2
72  * buffer twice so the type of buffer needs to be kept.
73  *
74  * @FULL: One capture fills the whole vb2 buffer
75  * @HALF_TOP: One capture fills the top half of the vb2 buffer
76  * @HALF_BOTTOM: One capture fills the bottom half of the vb2 buffer
77  */
78 enum rvin_buffer_type {
79 	FULL,
80 	HALF_TOP,
81 	HALF_BOTTOM,
82 };
83 
84 /**
85  * struct rvin_video_format - Data format stored in memory
86  * @fourcc:	Pixelformat
87  * @bpp:	Bytes per pixel
88  */
89 struct rvin_video_format {
90 	u32 fourcc;
91 	u8 bpp;
92 };
93 
94 /**
95  * struct rvin_parallel_entity - Parallel video input endpoint descriptor
96  * @asd:	sub-device descriptor for async framework
97  * @subdev:	subdevice matched using async framework
98  * @mbus_type:	media bus type
99  * @bus:	media bus parallel configuration
100  * @source_pad:	source pad of remote subdevice
101  * @sink_pad:	sink pad of remote subdevice
102  *
103  */
104 struct rvin_parallel_entity {
105 	struct v4l2_async_subdev *asd;
106 	struct v4l2_subdev *subdev;
107 
108 	enum v4l2_mbus_type mbus_type;
109 	struct v4l2_fwnode_bus_parallel bus;
110 
111 	unsigned int source_pad;
112 	unsigned int sink_pad;
113 };
114 
115 /**
116  * struct rvin_group_route - describes a route from a channel of a
117  *	CSI-2 receiver to a VIN
118  *
119  * @csi:	CSI-2 receiver ID.
120  * @channel:	Output channel of the CSI-2 receiver.
121  * @vin:	VIN ID.
122  * @mask:	Bitmask of the different CHSEL register values that
123  *		allow for a route from @csi + @chan to @vin.
124  *
125  * .. note::
126  *	Each R-Car CSI-2 receiver has four output channels facing the VIN
127  *	devices, each channel can carry one CSI-2 Virtual Channel (VC).
128  *	There is no correlation between channel number and CSI-2 VC. It's
129  *	up to the CSI-2 receiver driver to configure which VC is output
130  *	on which channel, the VIN devices only care about output channels.
131  *
132  *	There are in some cases multiple CHSEL register settings which would
133  *	allow for the same route from @csi + @channel to @vin. For example
134  *	on R-Car H3 both the CHSEL values 0 and 3 allow for a route from
135  *	CSI40/VC0 to VIN0. All possible CHSEL values for a route need to be
136  *	recorded as a bitmask in @mask, in this example bit 0 and 3 should
137  *	be set.
138  */
139 struct rvin_group_route {
140 	enum rvin_csi_id csi;
141 	unsigned int channel;
142 	unsigned int vin;
143 	unsigned int mask;
144 };
145 
146 /**
147  * struct rvin_info - Information about the particular VIN implementation
148  * @model:		VIN model
149  * @use_mc:		use media controller instead of controlling subdevice
150  * @nv12:		support outputing NV12 pixel format
151  * @max_width:		max input width the VIN supports
152  * @max_height:		max input height the VIN supports
153  * @routes:		list of possible routes from the CSI-2 recivers to
154  *			all VINs. The list mush be NULL terminated.
155  */
156 struct rvin_info {
157 	enum model_id model;
158 	bool use_mc;
159 	bool nv12;
160 
161 	unsigned int max_width;
162 	unsigned int max_height;
163 	const struct rvin_group_route *routes;
164 };
165 
166 /**
167  * struct rvin_dev - Renesas VIN device structure
168  * @dev:		(OF) device
169  * @base:		device I/O register space remapped to virtual memory
170  * @info:		info about VIN instance
171  *
172  * @vdev:		V4L2 video device associated with VIN
173  * @v4l2_dev:		V4L2 device
174  * @ctrl_handler:	V4L2 control handler
175  * @notifier:		V4L2 asynchronous subdevs notifier
176  *
177  * @parallel:		parallel input subdevice descriptor
178  *
179  * @group:		Gen3 CSI group
180  * @id:			Gen3 group id for this VIN
181  * @pad:		media pad for the video device entity
182  *
183  * @lock:		protects @queue
184  * @queue:		vb2 buffers queue
185  * @scratch:		cpu address for scratch buffer
186  * @scratch_phys:	physical address of the scratch buffer
187  *
188  * @qlock:		protects @buf_hw, @buf_list, @sequence and @state
189  * @buf_hw:		Keeps track of buffers given to HW slot
190  * @buf_list:		list of queued buffers
191  * @sequence:		V4L2 buffers sequence number
192  * @state:		keeps track of operation state
193  *
194  * @is_csi:		flag to mark the VIN as using a CSI-2 subdevice
195  * @chsel:		Cached value of the current CSI-2 channel selection
196  *
197  * @mbus_code:		media bus format code
198  * @format:		active V4L2 pixel format
199  *
200  * @crop:		active cropping
201  * @compose:		active composing
202  * @src_rect:		active size of the video source
203  * @std:		active video standard of the video source
204  *
205  * @alpha:		Alpha component to fill in for supported pixel formats
206  */
207 struct rvin_dev {
208 	struct device *dev;
209 	void __iomem *base;
210 	const struct rvin_info *info;
211 
212 	struct video_device vdev;
213 	struct v4l2_device v4l2_dev;
214 	struct v4l2_ctrl_handler ctrl_handler;
215 	struct v4l2_async_notifier notifier;
216 
217 	struct rvin_parallel_entity parallel;
218 
219 	struct rvin_group *group;
220 	unsigned int id;
221 	struct media_pad pad;
222 
223 	struct mutex lock;
224 	struct vb2_queue queue;
225 	void *scratch;
226 	dma_addr_t scratch_phys;
227 
228 	spinlock_t qlock;
229 	struct {
230 		struct vb2_v4l2_buffer *buffer;
231 		enum rvin_buffer_type type;
232 		dma_addr_t phys;
233 	} buf_hw[HW_BUFFER_NUM];
234 	struct list_head buf_list;
235 	unsigned int sequence;
236 	enum rvin_dma_state state;
237 
238 	bool is_csi;
239 	unsigned int chsel;
240 
241 	u32 mbus_code;
242 	struct v4l2_pix_format format;
243 
244 	struct v4l2_rect crop;
245 	struct v4l2_rect compose;
246 	struct v4l2_rect src_rect;
247 	v4l2_std_id std;
248 
249 	unsigned int alpha;
250 };
251 
252 #define vin_to_source(vin)		((vin)->parallel.subdev)
253 
254 /* Debug */
255 #define vin_dbg(d, fmt, arg...)		dev_dbg(d->dev, fmt, ##arg)
256 #define vin_info(d, fmt, arg...)	dev_info(d->dev, fmt, ##arg)
257 #define vin_warn(d, fmt, arg...)	dev_warn(d->dev, fmt, ##arg)
258 #define vin_err(d, fmt, arg...)		dev_err(d->dev, fmt, ##arg)
259 
260 /**
261  * struct rvin_group - VIN CSI2 group information
262  * @refcount:		number of VIN instances using the group
263  *
264  * @mdev:		media device which represents the group
265  *
266  * @lock:		protects the count, notifier, vin and csi members
267  * @count:		number of enabled VIN instances found in DT
268  * @notifier:		group notifier for CSI-2 async subdevices
269  * @vin:		VIN instances which are part of the group
270  * @csi:		array of pairs of fwnode and subdev pointers
271  *			to all CSI-2 subdevices.
272  */
273 struct rvin_group {
274 	struct kref refcount;
275 
276 	struct media_device mdev;
277 
278 	struct mutex lock;
279 	unsigned int count;
280 	struct v4l2_async_notifier notifier;
281 	struct rvin_dev *vin[RCAR_VIN_NUM];
282 
283 	struct {
284 		struct v4l2_async_subdev *asd;
285 		struct v4l2_subdev *subdev;
286 	} csi[RVIN_CSI_MAX];
287 };
288 
289 int rvin_dma_register(struct rvin_dev *vin, int irq);
290 void rvin_dma_unregister(struct rvin_dev *vin);
291 
292 int rvin_v4l2_register(struct rvin_dev *vin);
293 void rvin_v4l2_unregister(struct rvin_dev *vin);
294 
295 const struct rvin_video_format *rvin_format_from_pixel(struct rvin_dev *vin,
296 						       u32 pixelformat);
297 
298 
299 /* Cropping, composing and scaling */
300 void rvin_crop_scale_comp(struct rvin_dev *vin);
301 
302 int rvin_set_channel_routing(struct rvin_dev *vin, u8 chsel);
303 void rvin_set_alpha(struct rvin_dev *vin, unsigned int alpha);
304 
305 int rvin_start_streaming(struct rvin_dev *vin);
306 void rvin_stop_streaming(struct rvin_dev *vin);
307 
308 #endif
309