1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Driver for Renesas R-Car VIN
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
10 * Based on the soc-camera rcar_vin driver
16 #include <linux/kref.h>
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>
25 /* Number of HW buffers */
26 #define HW_BUFFER_NUM 3
28 /* Address alignment mask for HW buffers */
29 #define HW_BUFFER_MASK 0x7f
31 /* Max number on VIN instances that can be in a system */
32 #define RCAR_VIN_NUM 32
60 #define RVIN_REMOTES_MAX \
61 (((unsigned int)RVIN_CSI_MAX) > ((unsigned int)RVIN_ISP_MAX) ? \
62 RVIN_CSI_MAX : RVIN_ISP_MAX)
65 * enum rvin_dma_state - DMA states
66 * @STOPPED: No operation in progress
67 * @STARTING: Capture starting up
68 * @RUNNING: Operation in progress have buffers
69 * @STOPPING: Stopping operation
70 * @SUSPENDED: Capture is suspended
81 * enum rvin_buffer_type
83 * Describes how a buffer is given to the hardware. To be able
84 * to capture SEQ_TB/BT it's needed to capture to the same vb2
85 * buffer twice so the type of buffer needs to be kept.
87 * @FULL: One capture fills the whole vb2 buffer
88 * @HALF_TOP: One capture fills the top half of the vb2 buffer
89 * @HALF_BOTTOM: One capture fills the bottom half of the vb2 buffer
91 enum rvin_buffer_type {
98 * struct rvin_video_format - Data format stored in memory
99 * @fourcc: Pixelformat
100 * @bpp: Bytes per pixel
102 struct rvin_video_format {
108 * struct rvin_parallel_entity - Parallel video input endpoint descriptor
109 * @asc: async connection descriptor for async framework
110 * @subdev: subdevice matched using async framework
111 * @mbus_type: media bus type
112 * @bus: media bus parallel configuration
113 * @source_pad: source pad of remote subdevice
114 * @sink_pad: sink pad of remote subdevice
117 struct rvin_parallel_entity {
118 struct v4l2_async_connection *asc;
119 struct v4l2_subdev *subdev;
121 enum v4l2_mbus_type mbus_type;
122 struct v4l2_mbus_config_parallel bus;
124 unsigned int source_pad;
125 unsigned int sink_pad;
129 * struct rvin_group_route - describes a route from a channel of a
130 * CSI-2 receiver to a VIN
132 * @master: VIN group master ID.
133 * @csi: CSI-2 receiver ID.
134 * @chsel: CHSEL register values that connects VIN group to CSI-2.
137 * Each R-Car CSI-2 receiver has four output channels facing the VIN
138 * devices, each channel can carry one CSI-2 Virtual Channel (VC).
139 * There is no correlation between channel number and CSI-2 VC. It's
140 * up to the CSI-2 receiver driver to configure which VC is output
141 * on which channel, the VIN devices only care about output channels.
143 struct rvin_group_route {
145 enum rvin_csi_id csi;
150 * struct rvin_info - Information about the particular VIN implementation
152 * @use_mc: use media controller instead of controlling subdevice
153 * @use_isp: the VIN is connected to the ISP and not to the CSI-2
154 * @nv12: support outputing NV12 pixel format
155 * @max_width: max input width the VIN supports
156 * @max_height: max input height the VIN supports
157 * @routes: list of possible routes from the CSI-2 recivers to
158 * all VINs. The list mush be NULL terminated.
159 * @scaler: Optional scaler
167 unsigned int max_width;
168 unsigned int max_height;
169 const struct rvin_group_route *routes;
170 void (*scaler)(struct rvin_dev *vin);
174 * struct rvin_dev - Renesas VIN device structure
176 * @base: device I/O register space remapped to virtual memory
177 * @info: info about VIN instance
179 * @vdev: V4L2 video device associated with VIN
180 * @v4l2_dev: V4L2 device
181 * @ctrl_handler: V4L2 control handler
182 * @notifier: V4L2 asynchronous subdevs notifier
184 * @parallel: parallel input subdevice descriptor
186 * @group: Gen3 CSI group
187 * @id: Gen3 group id for this VIN
188 * @pad: media pad for the video device entity
190 * @lock: protects @queue
191 * @queue: vb2 buffers queue
192 * @scratch: cpu address for scratch buffer
193 * @scratch_phys: physical address of the scratch buffer
195 * @qlock: protects @buf_hw, @buf_list, @sequence and @state
196 * @buf_hw: Keeps track of buffers given to HW slot
197 * @buf_list: list of queued buffers
198 * @sequence: V4L2 buffers sequence number
199 * @state: keeps track of operation state
201 * @is_csi: flag to mark the VIN as using a CSI-2 subdevice
202 * @chsel: Cached value of the current CSI-2 channel selection
204 * @mbus_code: media bus format code
205 * @format: active V4L2 pixel format
207 * @crop: active cropping
208 * @compose: active composing
209 * @scaler: Optional scaler
210 * @std: active video standard of the video source
212 * @alpha: Alpha component to fill in for supported pixel formats
217 const struct rvin_info *info;
219 struct video_device vdev;
220 struct v4l2_device v4l2_dev;
221 struct v4l2_ctrl_handler ctrl_handler;
222 struct v4l2_async_notifier notifier;
224 struct rvin_parallel_entity parallel;
226 struct rvin_group *group;
228 struct media_pad pad;
231 struct vb2_queue queue;
233 dma_addr_t scratch_phys;
237 struct vb2_v4l2_buffer *buffer;
238 enum rvin_buffer_type type;
240 } buf_hw[HW_BUFFER_NUM];
241 struct list_head buf_list;
242 unsigned int sequence;
243 enum rvin_dma_state state;
249 struct v4l2_pix_format format;
251 struct v4l2_rect crop;
252 struct v4l2_rect compose;
253 void (*scaler)(struct rvin_dev *vin);
259 #define vin_to_source(vin) ((vin)->parallel.subdev)
262 #define vin_dbg(d, fmt, arg...) dev_dbg(d->dev, fmt, ##arg)
263 #define vin_info(d, fmt, arg...) dev_info(d->dev, fmt, ##arg)
264 #define vin_warn(d, fmt, arg...) dev_warn(d->dev, fmt, ##arg)
265 #define vin_err(d, fmt, arg...) dev_err(d->dev, fmt, ##arg)
268 * struct rvin_group - VIN CSI2 group information
269 * @refcount: number of VIN instances using the group
271 * @mdev: media device which represents the group
273 * @lock: protects the count, notifier, vin and csi members
274 * @count: number of enabled VIN instances found in DT
275 * @notifier: group notifier for CSI-2 async connections
276 * @vin: VIN instances which are part of the group
277 * @link_setup: Callback to create all links for the media graph
278 * @remotes: array of pairs of async connection and subdev pointers
279 * to all remote subdevices.
282 struct kref refcount;
284 struct media_device mdev;
288 struct v4l2_async_notifier notifier;
289 struct rvin_dev *vin[RCAR_VIN_NUM];
291 int (*link_setup)(struct rvin_dev *vin);
294 struct v4l2_async_connection *asc;
295 struct v4l2_subdev *subdev;
296 } remotes[RVIN_REMOTES_MAX];
299 int rvin_dma_register(struct rvin_dev *vin, int irq);
300 void rvin_dma_unregister(struct rvin_dev *vin);
302 int rvin_v4l2_register(struct rvin_dev *vin);
303 void rvin_v4l2_unregister(struct rvin_dev *vin);
305 const struct rvin_video_format *rvin_format_from_pixel(struct rvin_dev *vin,
309 /* Cropping, composing and scaling */
310 void rvin_scaler_gen2(struct rvin_dev *vin);
311 void rvin_scaler_gen3(struct rvin_dev *vin);
312 void rvin_crop_scale_comp(struct rvin_dev *vin);
314 int rvin_set_channel_routing(struct rvin_dev *vin, u8 chsel);
315 void rvin_set_alpha(struct rvin_dev *vin, unsigned int alpha);
317 int rvin_start_streaming(struct rvin_dev *vin);
318 void rvin_stop_streaming(struct rvin_dev *vin);