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