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