GNU Linux-libre 4.14.266-gnu1
[releases.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         /* if this is a sensor */
163         struct v4l2_fwnode_endpoint sensor_ep;
164 };
165
166 struct imx_media_dev {
167         struct media_device md;
168         struct v4l2_device  v4l2_dev;
169
170         /* the pipeline object */
171         struct media_pipeline pipe;
172
173         struct mutex mutex; /* protect elements below */
174
175         /* master subdevice list */
176         struct imx_media_subdev subdev[IMX_MEDIA_MAX_SUBDEVS];
177         int num_subdevs;
178
179         /* master video device list */
180         struct imx_media_video_dev *vdev[IMX_MEDIA_MAX_VDEVS];
181         int num_vdevs;
182
183         /* IPUs this media driver control, valid after subdevs bound */
184         struct ipu_soc *ipu[2];
185
186         /* for async subdev registration */
187         struct v4l2_async_subdev *async_ptrs[IMX_MEDIA_MAX_SUBDEVS];
188         struct v4l2_async_notifier subdev_notifier;
189 };
190
191 enum codespace_sel {
192         CS_SEL_YUV = 0,
193         CS_SEL_RGB,
194         CS_SEL_ANY,
195 };
196
197 const struct imx_media_pixfmt *
198 imx_media_find_format(u32 fourcc, enum codespace_sel cs_sel, bool allow_bayer);
199 int imx_media_enum_format(u32 *fourcc, u32 index, enum codespace_sel cs_sel);
200 const struct imx_media_pixfmt *
201 imx_media_find_mbus_format(u32 code, enum codespace_sel cs_sel,
202                            bool allow_bayer);
203 int imx_media_enum_mbus_format(u32 *code, u32 index, enum codespace_sel cs_sel,
204                                bool allow_bayer);
205 const struct imx_media_pixfmt *
206 imx_media_find_ipu_format(u32 code, enum codespace_sel cs_sel);
207 int imx_media_enum_ipu_format(u32 *code, u32 index, enum codespace_sel cs_sel);
208
209 int imx_media_init_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
210                             u32 width, u32 height, u32 code, u32 field,
211                             const struct imx_media_pixfmt **cc);
212 void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
213                                         struct v4l2_mbus_framefmt *fmt,
214                                         bool ic_route);
215 int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
216                                   struct v4l2_mbus_framefmt *mbus,
217                                   const struct imx_media_pixfmt *cc);
218 int imx_media_mbus_fmt_to_ipu_image(struct ipu_image *image,
219                                     struct v4l2_mbus_framefmt *mbus);
220 int imx_media_ipu_image_to_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
221                                     struct ipu_image *image);
222
223 struct imx_media_subdev *
224 imx_media_find_async_subdev(struct imx_media_dev *imxmd,
225                             struct device_node *np,
226                             const char *devname);
227 struct imx_media_subdev *
228 imx_media_add_async_subdev(struct imx_media_dev *imxmd,
229                            struct device_node *np,
230                            struct platform_device *pdev);
231 int imx_media_add_pad_link(struct imx_media_dev *imxmd,
232                            struct imx_media_pad *pad,
233                            struct device_node *remote_node,
234                            const char *remote_devname,
235                            int local_pad, int remote_pad);
236
237 void imx_media_grp_id_to_sd_name(char *sd_name, int sz,
238                                  u32 grp_id, int ipu_id);
239
240 int imx_media_add_internal_subdevs(struct imx_media_dev *imxmd,
241                                    struct imx_media_subdev *csi[4]);
242 void imx_media_remove_internal_subdevs(struct imx_media_dev *imxmd);
243
244 struct imx_media_subdev *
245 imx_media_find_subdev_by_sd(struct imx_media_dev *imxmd,
246                             struct v4l2_subdev *sd);
247 struct imx_media_subdev *
248 imx_media_find_subdev_by_id(struct imx_media_dev *imxmd,
249                             u32 grp_id);
250 int imx_media_add_video_device(struct imx_media_dev *imxmd,
251                                struct imx_media_video_dev *vdev);
252 int imx_media_find_mipi_csi2_channel(struct imx_media_dev *imxmd,
253                                      struct media_entity *start_entity);
254 struct imx_media_subdev *
255 imx_media_find_upstream_subdev(struct imx_media_dev *imxmd,
256                                struct media_entity *start_entity,
257                                u32 grp_id);
258 struct imx_media_subdev *
259 __imx_media_find_sensor(struct imx_media_dev *imxmd,
260                         struct media_entity *start_entity);
261 struct imx_media_subdev *
262 imx_media_find_sensor(struct imx_media_dev *imxmd,
263                       struct media_entity *start_entity);
264
265 struct imx_media_dma_buf {
266         void          *virt;
267         dma_addr_t     phys;
268         unsigned long  len;
269 };
270
271 void imx_media_free_dma_buf(struct imx_media_dev *imxmd,
272                             struct imx_media_dma_buf *buf);
273 int imx_media_alloc_dma_buf(struct imx_media_dev *imxmd,
274                             struct imx_media_dma_buf *buf,
275                             int size);
276
277 int imx_media_pipeline_set_stream(struct imx_media_dev *imxmd,
278                                   struct media_entity *entity,
279                                   bool on);
280
281 /* imx-media-fim.c */
282 struct imx_media_fim;
283 void imx_media_fim_eof_monitor(struct imx_media_fim *fim, struct timespec *ts);
284 int imx_media_fim_set_stream(struct imx_media_fim *fim,
285                              const struct v4l2_fract *frame_interval,
286                              bool on);
287 int imx_media_fim_add_controls(struct imx_media_fim *fim);
288 struct imx_media_fim *imx_media_fim_init(struct v4l2_subdev *sd);
289 void imx_media_fim_free(struct imx_media_fim *fim);
290
291 /* imx-media-of.c */
292 struct imx_media_subdev *
293 imx_media_of_find_subdev(struct imx_media_dev *imxmd,
294                          struct device_node *np,
295                          const char *name);
296 int imx_media_of_parse(struct imx_media_dev *dev,
297                        struct imx_media_subdev *(*csi)[4],
298                        struct device_node *np);
299
300 /* imx-media-capture.c */
301 struct imx_media_video_dev *
302 imx_media_capture_device_init(struct v4l2_subdev *src_sd, int pad);
303 void imx_media_capture_device_remove(struct imx_media_video_dev *vdev);
304 int imx_media_capture_device_register(struct imx_media_video_dev *vdev);
305 void imx_media_capture_device_unregister(struct imx_media_video_dev *vdev);
306 struct imx_media_buffer *
307 imx_media_capture_device_next_buf(struct imx_media_video_dev *vdev);
308 void imx_media_capture_device_set_format(struct imx_media_video_dev *vdev,
309                                          struct v4l2_pix_format *pix);
310 void imx_media_capture_device_error(struct imx_media_video_dev *vdev);
311
312 /* subdev group ids */
313 #define IMX_MEDIA_GRP_ID_SENSOR    (1 << 8)
314 #define IMX_MEDIA_GRP_ID_VIDMUX    (1 << 9)
315 #define IMX_MEDIA_GRP_ID_CSI2      (1 << 10)
316 #define IMX_MEDIA_GRP_ID_CSI_BIT   11
317 #define IMX_MEDIA_GRP_ID_CSI       (0x3 << IMX_MEDIA_GRP_ID_CSI_BIT)
318 #define IMX_MEDIA_GRP_ID_CSI0      (1 << IMX_MEDIA_GRP_ID_CSI_BIT)
319 #define IMX_MEDIA_GRP_ID_CSI1      (2 << IMX_MEDIA_GRP_ID_CSI_BIT)
320 #define IMX_MEDIA_GRP_ID_VDIC      (1 << 13)
321 #define IMX_MEDIA_GRP_ID_IC_PRP    (1 << 14)
322 #define IMX_MEDIA_GRP_ID_IC_PRPENC (1 << 15)
323 #define IMX_MEDIA_GRP_ID_IC_PRPVF  (1 << 16)
324
325 #endif