2cc19da2f1e1694db2f16da8f066bf3c6608f1db
[platform/kernel/linux-rpi.git] / drivers / staging / media / imx / imx-media.h
1 /*
2  * V4L2 Media Controller Driver for Freescale i.MX5/6 SOC
3  *
4  * Copyright (c) 2016 Mentor Graphics Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11 #ifndef _IMX_MEDIA_H
12 #define _IMX_MEDIA_H
13
14 #include <media/v4l2-ctrls.h>
15 #include <media/v4l2-device.h>
16 #include <media/v4l2-fwnode.h>
17 #include <media/v4l2-subdev.h>
18 #include <media/videobuf2-dma-contig.h>
19 #include <video/imx-ipu-v3.h>
20
21 /*
22  * This is somewhat arbitrary, but we need at least:
23  * - 4 video devices per IPU
24  * - 3 IC subdevs per IPU
25  * - 1 VDIC subdev per IPU
26  * - 2 CSI subdevs per IPU
27  * - 1 mipi-csi2 receiver subdev
28  * - 2 video-mux subdevs
29  * - 2 camera sensor subdevs per IPU (1 parallel, 1 mipi-csi2)
30  *
31  */
32 /* max video devices */
33 #define IMX_MEDIA_MAX_VDEVS          8
34 /* max subdevices */
35 #define IMX_MEDIA_MAX_SUBDEVS       32
36 /* max pads per subdev */
37 #define IMX_MEDIA_MAX_PADS          16
38 /* max links per pad */
39 #define IMX_MEDIA_MAX_LINKS          8
40
41 /*
42  * Pad definitions for the subdevs with multiple source or
43  * sink pads
44  */
45
46 /* ipu_csi */
47 enum {
48         CSI_SINK_PAD = 0,
49         CSI_SRC_PAD_DIRECT,
50         CSI_SRC_PAD_IDMAC,
51         CSI_NUM_PADS,
52 };
53
54 #define CSI_NUM_SINK_PADS 1
55 #define CSI_NUM_SRC_PADS  2
56
57 /* ipu_vdic */
58 enum {
59         VDIC_SINK_PAD_DIRECT = 0,
60         VDIC_SINK_PAD_IDMAC,
61         VDIC_SRC_PAD_DIRECT,
62         VDIC_NUM_PADS,
63 };
64
65 #define VDIC_NUM_SINK_PADS 2
66 #define VDIC_NUM_SRC_PADS  1
67
68 /* ipu_ic_prp */
69 enum {
70         PRP_SINK_PAD = 0,
71         PRP_SRC_PAD_PRPENC,
72         PRP_SRC_PAD_PRPVF,
73         PRP_NUM_PADS,
74 };
75
76 #define PRP_NUM_SINK_PADS 1
77 #define PRP_NUM_SRC_PADS  2
78
79 /* ipu_ic_prpencvf */
80 enum {
81         PRPENCVF_SINK_PAD = 0,
82         PRPENCVF_SRC_PAD,
83         PRPENCVF_NUM_PADS,
84 };
85
86 #define PRPENCVF_NUM_SINK_PADS 1
87 #define PRPENCVF_NUM_SRC_PADS  1
88
89 /* How long to wait for EOF interrupts in the buffer-capture subdevs */
90 #define IMX_MEDIA_EOF_TIMEOUT       1000
91
92 struct imx_media_pixfmt {
93         u32     fourcc;
94         u32     codes[4];
95         int     bpp;     /* total bpp */
96         enum ipu_color_space cs;
97         bool    planar;  /* is a planar format */
98         bool    bayer;   /* is a raw bayer format */
99         bool    ipufmt;  /* is one of the IPU internal formats */
100 };
101
102 struct imx_media_buffer {
103         struct vb2_v4l2_buffer vbuf; /* v4l buffer must be first */
104         struct list_head  list;
105 };
106
107 struct imx_media_video_dev {
108         struct video_device *vfd;
109
110         /* the user format */
111         struct v4l2_format fmt;
112         const struct imx_media_pixfmt *cc;
113 };
114
115 static inline struct imx_media_buffer *to_imx_media_vb(struct vb2_buffer *vb)
116 {
117         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
118
119         return container_of(vbuf, struct imx_media_buffer, vbuf);
120 }
121
122 struct imx_media_link {
123         struct device_node *remote_sd_node;
124         char               remote_devname[32];
125         int                local_pad;
126         int                remote_pad;
127 };
128
129 struct imx_media_pad {
130         struct media_pad  pad;
131         struct imx_media_link link[IMX_MEDIA_MAX_LINKS];
132         bool devnode; /* does this pad link to a device node */
133         int num_links;
134
135         /*
136          * list of video devices that can be reached from this pad,
137          * list is only valid for source pads.
138          */
139         struct imx_media_video_dev *vdev[IMX_MEDIA_MAX_VDEVS];
140         int num_vdevs;
141 };
142
143 struct imx_media_internal_sd_platformdata {
144         char sd_name[V4L2_SUBDEV_NAME_SIZE];
145         u32 grp_id;
146         int ipu_id;
147 };
148
149 struct imx_media_subdev {
150         struct v4l2_async_subdev asd;
151         struct v4l2_subdev       *sd; /* set when bound */
152
153         struct imx_media_pad     pad[IMX_MEDIA_MAX_PADS];
154         int num_sink_pads;
155         int num_src_pads;
156
157         /* the platform device if this is an internal subdev */
158         struct platform_device *pdev;
159         /* the devname is needed for async devname match */
160         char devname[32];
161 };
162
163 struct imx_media_dev {
164         struct media_device md;
165         struct v4l2_device  v4l2_dev;
166
167         /* the pipeline object */
168         struct media_pipeline pipe;
169
170         struct mutex mutex; /* protect elements below */
171
172         /* master subdevice list */
173         struct imx_media_subdev subdev[IMX_MEDIA_MAX_SUBDEVS];
174         int num_subdevs;
175
176         /* master video device list */
177         struct imx_media_video_dev *vdev[IMX_MEDIA_MAX_VDEVS];
178         int num_vdevs;
179
180         /* IPUs this media driver control, valid after subdevs bound */
181         struct ipu_soc *ipu[2];
182
183         /* for async subdev registration */
184         struct v4l2_async_subdev *async_ptrs[IMX_MEDIA_MAX_SUBDEVS];
185         struct v4l2_async_notifier subdev_notifier;
186 };
187
188 enum codespace_sel {
189         CS_SEL_YUV = 0,
190         CS_SEL_RGB,
191         CS_SEL_ANY,
192 };
193
194 const struct imx_media_pixfmt *
195 imx_media_find_format(u32 fourcc, enum codespace_sel cs_sel, bool allow_bayer);
196 int imx_media_enum_format(u32 *fourcc, u32 index, enum codespace_sel cs_sel);
197 const struct imx_media_pixfmt *
198 imx_media_find_mbus_format(u32 code, enum codespace_sel cs_sel,
199                            bool allow_bayer);
200 int imx_media_enum_mbus_format(u32 *code, u32 index, enum codespace_sel cs_sel,
201                                bool allow_bayer);
202 const struct imx_media_pixfmt *
203 imx_media_find_ipu_format(u32 code, enum codespace_sel cs_sel);
204 int imx_media_enum_ipu_format(u32 *code, u32 index, enum codespace_sel cs_sel);
205
206 int imx_media_init_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
207                             u32 width, u32 height, u32 code, u32 field,
208                             const struct imx_media_pixfmt **cc);
209 void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
210                                         struct v4l2_mbus_framefmt *fmt,
211                                         bool ic_route);
212 int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
213                                   struct v4l2_mbus_framefmt *mbus,
214                                   const struct imx_media_pixfmt *cc);
215 int imx_media_mbus_fmt_to_ipu_image(struct ipu_image *image,
216                                     struct v4l2_mbus_framefmt *mbus);
217 int imx_media_ipu_image_to_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
218                                     struct ipu_image *image);
219
220 struct imx_media_subdev *
221 imx_media_find_async_subdev(struct imx_media_dev *imxmd,
222                             struct device_node *np,
223                             const char *devname);
224 struct imx_media_subdev *
225 imx_media_add_async_subdev(struct imx_media_dev *imxmd,
226                            struct device_node *np,
227                            struct platform_device *pdev);
228 int imx_media_add_pad_link(struct imx_media_dev *imxmd,
229                            struct imx_media_pad *pad,
230                            struct device_node *remote_node,
231                            const char *remote_devname,
232                            int local_pad, int remote_pad);
233
234 void imx_media_grp_id_to_sd_name(char *sd_name, int sz,
235                                  u32 grp_id, int ipu_id);
236
237 int imx_media_add_internal_subdevs(struct imx_media_dev *imxmd,
238                                    struct imx_media_subdev *csi[4]);
239 void imx_media_remove_internal_subdevs(struct imx_media_dev *imxmd);
240
241 struct imx_media_subdev *
242 imx_media_find_subdev_by_sd(struct imx_media_dev *imxmd,
243                             struct v4l2_subdev *sd);
244 struct imx_media_subdev *
245 imx_media_find_subdev_by_id(struct imx_media_dev *imxmd,
246                             u32 grp_id);
247 int imx_media_add_video_device(struct imx_media_dev *imxmd,
248                                struct imx_media_video_dev *vdev);
249 int imx_media_find_mipi_csi2_channel(struct imx_media_dev *imxmd,
250                                      struct media_entity *start_entity);
251 struct media_pad *
252 imx_media_find_upstream_pad(struct imx_media_dev *imxmd,
253                             struct media_entity *start_entity,
254                             u32 grp_id);
255 struct imx_media_subdev *
256 imx_media_find_upstream_subdev(struct imx_media_dev *imxmd,
257                                struct media_entity *start_entity,
258                                u32 grp_id);
259
260 struct imx_media_dma_buf {
261         void          *virt;
262         dma_addr_t     phys;
263         unsigned long  len;
264 };
265
266 void imx_media_free_dma_buf(struct imx_media_dev *imxmd,
267                             struct imx_media_dma_buf *buf);
268 int imx_media_alloc_dma_buf(struct imx_media_dev *imxmd,
269                             struct imx_media_dma_buf *buf,
270                             int size);
271
272 int imx_media_pipeline_set_stream(struct imx_media_dev *imxmd,
273                                   struct media_entity *entity,
274                                   bool on);
275
276 /* imx-media-fim.c */
277 struct imx_media_fim;
278 void imx_media_fim_eof_monitor(struct imx_media_fim *fim, ktime_t timestamp);
279 int imx_media_fim_set_stream(struct imx_media_fim *fim,
280                              const struct v4l2_fract *frame_interval,
281                              bool on);
282 int imx_media_fim_add_controls(struct imx_media_fim *fim);
283 struct imx_media_fim *imx_media_fim_init(struct v4l2_subdev *sd);
284 void imx_media_fim_free(struct imx_media_fim *fim);
285
286 /* imx-media-of.c */
287 struct imx_media_subdev *
288 imx_media_of_find_subdev(struct imx_media_dev *imxmd,
289                          struct device_node *np,
290                          const char *name);
291 int imx_media_of_parse(struct imx_media_dev *dev,
292                        struct imx_media_subdev *(*csi)[4],
293                        struct device_node *np);
294
295 /* imx-media-capture.c */
296 struct imx_media_video_dev *
297 imx_media_capture_device_init(struct v4l2_subdev *src_sd, int pad);
298 void imx_media_capture_device_remove(struct imx_media_video_dev *vdev);
299 int imx_media_capture_device_register(struct imx_media_video_dev *vdev);
300 void imx_media_capture_device_unregister(struct imx_media_video_dev *vdev);
301 struct imx_media_buffer *
302 imx_media_capture_device_next_buf(struct imx_media_video_dev *vdev);
303 void imx_media_capture_device_set_format(struct imx_media_video_dev *vdev,
304                                          struct v4l2_pix_format *pix);
305 void imx_media_capture_device_error(struct imx_media_video_dev *vdev);
306
307 /* subdev group ids */
308 #define IMX_MEDIA_GRP_ID_CSI2      BIT(8)
309 #define IMX_MEDIA_GRP_ID_CSI_BIT   9
310 #define IMX_MEDIA_GRP_ID_CSI       (0x3 << IMX_MEDIA_GRP_ID_CSI_BIT)
311 #define IMX_MEDIA_GRP_ID_CSI0      BIT(IMX_MEDIA_GRP_ID_CSI_BIT)
312 #define IMX_MEDIA_GRP_ID_CSI1      (2 << IMX_MEDIA_GRP_ID_CSI_BIT)
313 #define IMX_MEDIA_GRP_ID_VDIC      BIT(11)
314 #define IMX_MEDIA_GRP_ID_IC_PRP    BIT(12)
315 #define IMX_MEDIA_GRP_ID_IC_PRPENC BIT(13)
316 #define IMX_MEDIA_GRP_ID_IC_PRPVF  BIT(14)
317
318 #endif