c6cea2746424f6dcdae84a0ac41b4fe2ddf832ff
[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
39 /*
40  * Pad definitions for the subdevs with multiple source or
41  * sink pads
42  */
43
44 /* ipu_csi */
45 enum {
46         CSI_SINK_PAD = 0,
47         CSI_SRC_PAD_DIRECT,
48         CSI_SRC_PAD_IDMAC,
49         CSI_NUM_PADS,
50 };
51
52 /* ipu_vdic */
53 enum {
54         VDIC_SINK_PAD_DIRECT = 0,
55         VDIC_SINK_PAD_IDMAC,
56         VDIC_SRC_PAD_DIRECT,
57         VDIC_NUM_PADS,
58 };
59
60 /* ipu_ic_prp */
61 enum {
62         PRP_SINK_PAD = 0,
63         PRP_SRC_PAD_PRPENC,
64         PRP_SRC_PAD_PRPVF,
65         PRP_NUM_PADS,
66 };
67
68 /* ipu_ic_prpencvf */
69 enum {
70         PRPENCVF_SINK_PAD = 0,
71         PRPENCVF_SRC_PAD,
72         PRPENCVF_NUM_PADS,
73 };
74
75 /* How long to wait for EOF interrupts in the buffer-capture subdevs */
76 #define IMX_MEDIA_EOF_TIMEOUT       1000
77
78 struct imx_media_pixfmt {
79         u32     fourcc;
80         u32     codes[4];
81         int     bpp;     /* total bpp */
82         enum ipu_color_space cs;
83         bool    planar;  /* is a planar format */
84         bool    bayer;   /* is a raw bayer format */
85         bool    ipufmt;  /* is one of the IPU internal formats */
86 };
87
88 struct imx_media_buffer {
89         struct vb2_v4l2_buffer vbuf; /* v4l buffer must be first */
90         struct list_head  list;
91 };
92
93 struct imx_media_video_dev {
94         struct video_device *vfd;
95
96         /* the user format */
97         struct v4l2_format fmt;
98         const struct imx_media_pixfmt *cc;
99 };
100
101 static inline struct imx_media_buffer *to_imx_media_vb(struct vb2_buffer *vb)
102 {
103         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
104
105         return container_of(vbuf, struct imx_media_buffer, vbuf);
106 }
107
108 struct imx_media_pad {
109         /*
110          * list of video devices that can be reached from this pad,
111          * list is only valid for source pads.
112          */
113         struct imx_media_video_dev *vdev[IMX_MEDIA_MAX_VDEVS];
114         int num_vdevs;
115 };
116
117 struct imx_media_internal_sd_platformdata {
118         char sd_name[V4L2_SUBDEV_NAME_SIZE];
119         u32 grp_id;
120         int ipu_id;
121 };
122
123 struct imx_media_subdev {
124         struct v4l2_async_subdev asd;
125         struct v4l2_subdev       *sd; /* set when bound */
126
127         struct imx_media_pad     pad[IMX_MEDIA_MAX_PADS];
128
129         /* the platform device if this is an IPU-internal subdev */
130         struct platform_device *pdev;
131 };
132
133 struct imx_media_dev {
134         struct media_device md;
135         struct v4l2_device  v4l2_dev;
136
137         /* the pipeline object */
138         struct media_pipeline pipe;
139
140         struct mutex mutex; /* protect elements below */
141
142         /* master subdevice list */
143         struct imx_media_subdev subdev[IMX_MEDIA_MAX_SUBDEVS];
144         int num_subdevs;
145
146         /* master video device list */
147         struct imx_media_video_dev *vdev[IMX_MEDIA_MAX_VDEVS];
148         int num_vdevs;
149
150         /* IPUs this media driver control, valid after subdevs bound */
151         struct ipu_soc *ipu[2];
152
153         /* for async subdev registration */
154         struct v4l2_async_subdev *async_ptrs[IMX_MEDIA_MAX_SUBDEVS];
155         struct v4l2_async_notifier subdev_notifier;
156 };
157
158 enum codespace_sel {
159         CS_SEL_YUV = 0,
160         CS_SEL_RGB,
161         CS_SEL_ANY,
162 };
163
164 const struct imx_media_pixfmt *
165 imx_media_find_format(u32 fourcc, enum codespace_sel cs_sel, bool allow_bayer);
166 int imx_media_enum_format(u32 *fourcc, u32 index, enum codespace_sel cs_sel);
167 const struct imx_media_pixfmt *
168 imx_media_find_mbus_format(u32 code, enum codespace_sel cs_sel,
169                            bool allow_bayer);
170 int imx_media_enum_mbus_format(u32 *code, u32 index, enum codespace_sel cs_sel,
171                                bool allow_bayer);
172 const struct imx_media_pixfmt *
173 imx_media_find_ipu_format(u32 code, enum codespace_sel cs_sel);
174 int imx_media_enum_ipu_format(u32 *code, u32 index, enum codespace_sel cs_sel);
175
176 int imx_media_init_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
177                             u32 width, u32 height, u32 code, u32 field,
178                             const struct imx_media_pixfmt **cc);
179 void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
180                                         struct v4l2_mbus_framefmt *fmt,
181                                         bool ic_route);
182 int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
183                                   struct v4l2_mbus_framefmt *mbus,
184                                   const struct imx_media_pixfmt *cc);
185 int imx_media_mbus_fmt_to_ipu_image(struct ipu_image *image,
186                                     struct v4l2_mbus_framefmt *mbus);
187 int imx_media_ipu_image_to_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
188                                     struct ipu_image *image);
189
190 struct imx_media_subdev *
191 imx_media_find_async_subdev(struct imx_media_dev *imxmd,
192                             struct device_node *np,
193                             const char *devname);
194 struct imx_media_subdev *
195 imx_media_add_async_subdev(struct imx_media_dev *imxmd,
196                            struct device_node *np,
197                            struct platform_device *pdev);
198
199 void imx_media_grp_id_to_sd_name(char *sd_name, int sz,
200                                  u32 grp_id, int ipu_id);
201
202 int imx_media_add_internal_subdevs(struct imx_media_dev *imxmd);
203 int imx_media_create_internal_links(struct imx_media_dev *imxmd,
204                                     struct imx_media_subdev *imxsd);
205 void imx_media_remove_internal_subdevs(struct imx_media_dev *imxmd);
206
207 struct imx_media_subdev *
208 imx_media_find_subdev_by_sd(struct imx_media_dev *imxmd,
209                             struct v4l2_subdev *sd);
210 struct imx_media_subdev *
211 imx_media_find_subdev_by_id(struct imx_media_dev *imxmd,
212                             u32 grp_id);
213 int imx_media_add_video_device(struct imx_media_dev *imxmd,
214                                struct imx_media_video_dev *vdev);
215 int imx_media_find_mipi_csi2_channel(struct imx_media_dev *imxmd,
216                                      struct media_entity *start_entity);
217 struct media_pad *
218 imx_media_find_upstream_pad(struct imx_media_dev *imxmd,
219                             struct media_entity *start_entity,
220                             u32 grp_id);
221 struct imx_media_subdev *
222 imx_media_find_upstream_subdev(struct imx_media_dev *imxmd,
223                                struct media_entity *start_entity,
224                                u32 grp_id);
225
226 struct imx_media_dma_buf {
227         void          *virt;
228         dma_addr_t     phys;
229         unsigned long  len;
230 };
231
232 void imx_media_free_dma_buf(struct imx_media_dev *imxmd,
233                             struct imx_media_dma_buf *buf);
234 int imx_media_alloc_dma_buf(struct imx_media_dev *imxmd,
235                             struct imx_media_dma_buf *buf,
236                             int size);
237
238 int imx_media_pipeline_set_stream(struct imx_media_dev *imxmd,
239                                   struct media_entity *entity,
240                                   bool on);
241
242 /* imx-media-fim.c */
243 struct imx_media_fim;
244 void imx_media_fim_eof_monitor(struct imx_media_fim *fim, ktime_t timestamp);
245 int imx_media_fim_set_stream(struct imx_media_fim *fim,
246                              const struct v4l2_fract *frame_interval,
247                              bool on);
248 int imx_media_fim_add_controls(struct imx_media_fim *fim);
249 struct imx_media_fim *imx_media_fim_init(struct v4l2_subdev *sd);
250 void imx_media_fim_free(struct imx_media_fim *fim);
251
252 /* imx-media-of.c */
253 int imx_media_add_of_subdevs(struct imx_media_dev *dev,
254                              struct device_node *np);
255 int imx_media_create_of_links(struct imx_media_dev *imxmd,
256                               struct imx_media_subdev *imxsd);
257 int imx_media_create_csi_of_links(struct imx_media_dev *imxmd,
258                                   struct imx_media_subdev *csi);
259
260 /* imx-media-capture.c */
261 struct imx_media_video_dev *
262 imx_media_capture_device_init(struct v4l2_subdev *src_sd, int pad);
263 void imx_media_capture_device_remove(struct imx_media_video_dev *vdev);
264 int imx_media_capture_device_register(struct imx_media_video_dev *vdev);
265 void imx_media_capture_device_unregister(struct imx_media_video_dev *vdev);
266 struct imx_media_buffer *
267 imx_media_capture_device_next_buf(struct imx_media_video_dev *vdev);
268 void imx_media_capture_device_set_format(struct imx_media_video_dev *vdev,
269                                          struct v4l2_pix_format *pix);
270 void imx_media_capture_device_error(struct imx_media_video_dev *vdev);
271
272 /* subdev group ids */
273 #define IMX_MEDIA_GRP_ID_CSI2      BIT(8)
274 #define IMX_MEDIA_GRP_ID_CSI_BIT   9
275 #define IMX_MEDIA_GRP_ID_CSI       (0x3 << IMX_MEDIA_GRP_ID_CSI_BIT)
276 #define IMX_MEDIA_GRP_ID_CSI0      BIT(IMX_MEDIA_GRP_ID_CSI_BIT)
277 #define IMX_MEDIA_GRP_ID_CSI1      (2 << IMX_MEDIA_GRP_ID_CSI_BIT)
278 #define IMX_MEDIA_GRP_ID_VDIC      BIT(11)
279 #define IMX_MEDIA_GRP_ID_IC_PRP    BIT(12)
280 #define IMX_MEDIA_GRP_ID_IC_PRPENC BIT(13)
281 #define IMX_MEDIA_GRP_ID_IC_PRPVF  BIT(14)
282
283 #endif