Merge tag 'media/v5.2-1' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux-2.6-block.git] / drivers / staging / media / imx / imx-media.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * V4L2 Media Controller Driver for Freescale i.MX5/6 SOC
4  *
5  * Copyright (c) 2016 Mentor Graphics Inc.
6  */
7 #ifndef _IMX_MEDIA_H
8 #define _IMX_MEDIA_H
9
10 #include <linux/platform_device.h>
11 #include <media/v4l2-ctrls.h>
12 #include <media/v4l2-device.h>
13 #include <media/v4l2-fwnode.h>
14 #include <media/v4l2-subdev.h>
15 #include <media/videobuf2-dma-contig.h>
16 #include <video/imx-ipu-v3.h>
17
18 /*
19  * Pad definitions for the subdevs with multiple source or
20  * sink pads
21  */
22
23 /* ipu_csi */
24 enum {
25         CSI_SINK_PAD = 0,
26         CSI_SRC_PAD_DIRECT,
27         CSI_SRC_PAD_IDMAC,
28         CSI_NUM_PADS,
29 };
30
31 /* ipu_vdic */
32 enum {
33         VDIC_SINK_PAD_DIRECT = 0,
34         VDIC_SINK_PAD_IDMAC,
35         VDIC_SRC_PAD_DIRECT,
36         VDIC_NUM_PADS,
37 };
38
39 /* ipu_ic_prp */
40 enum {
41         PRP_SINK_PAD = 0,
42         PRP_SRC_PAD_PRPENC,
43         PRP_SRC_PAD_PRPVF,
44         PRP_NUM_PADS,
45 };
46
47 /* ipu_ic_prpencvf */
48 enum {
49         PRPENCVF_SINK_PAD = 0,
50         PRPENCVF_SRC_PAD,
51         PRPENCVF_NUM_PADS,
52 };
53
54 /* How long to wait for EOF interrupts in the buffer-capture subdevs */
55 #define IMX_MEDIA_EOF_TIMEOUT       1000
56
57 struct imx_media_pixfmt {
58         u32     fourcc;
59         u32     codes[4];
60         int     bpp;     /* total bpp */
61         /* cycles per pixel for generic (bayer) formats for the parallel bus */
62         int     cycles;
63         enum ipu_color_space cs;
64         bool    planar;  /* is a planar format */
65         bool    bayer;   /* is a raw bayer format */
66         bool    ipufmt;  /* is one of the IPU internal formats */
67 };
68
69 struct imx_media_buffer {
70         struct vb2_v4l2_buffer vbuf; /* v4l buffer must be first */
71         struct list_head  list;
72 };
73
74 struct imx_media_video_dev {
75         struct video_device *vfd;
76
77         /* the user format */
78         struct v4l2_format fmt;
79         /* the compose rectangle */
80         struct v4l2_rect compose;
81         const struct imx_media_pixfmt *cc;
82
83         /* links this vdev to master list */
84         struct list_head list;
85 };
86
87 static inline struct imx_media_buffer *to_imx_media_vb(struct vb2_buffer *vb)
88 {
89         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
90
91         return container_of(vbuf, struct imx_media_buffer, vbuf);
92 }
93
94 /*
95  * to support control inheritance to video devices, this
96  * retrieves a pad's list_head of video devices that can
97  * be reached from the pad. Note that only the lists in
98  * source pads get populated, sink pads have empty lists.
99  */
100 static inline struct list_head *
101 to_pad_vdev_list(struct v4l2_subdev *sd, int pad_index)
102 {
103         struct list_head *vdev_list = sd->host_priv;
104
105         return vdev_list ? &vdev_list[pad_index] : NULL;
106 }
107
108 /* an entry in a pad's video device list */
109 struct imx_media_pad_vdev {
110         struct imx_media_video_dev *vdev;
111         struct list_head list;
112 };
113
114 struct imx_media_ipu_internal_sd_pdata {
115         char sd_name[V4L2_SUBDEV_NAME_SIZE];
116         u32 grp_id;
117         int ipu_id;
118 };
119
120 struct imx_media_async_subdev {
121         /* the base asd - must be first in this struct */
122         struct v4l2_async_subdev asd;
123         /* the platform device of IPU-internal subdevs */
124         struct platform_device *pdev;
125 };
126
127 static inline struct imx_media_async_subdev *
128 to_imx_media_asd(struct v4l2_async_subdev *asd)
129 {
130         return container_of(asd, struct imx_media_async_subdev, asd);
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 video device list */
143         struct list_head vdev_list;
144
145         /* IPUs this media driver control, valid after subdevs bound */
146         struct ipu_soc *ipu[2];
147
148         /* for async subdev registration */
149         struct v4l2_async_notifier notifier;
150 };
151
152 enum codespace_sel {
153         CS_SEL_YUV = 0,
154         CS_SEL_RGB,
155         CS_SEL_ANY,
156 };
157
158 /* imx-media-utils.c */
159 const struct imx_media_pixfmt *
160 imx_media_find_format(u32 fourcc, enum codespace_sel cs_sel, bool allow_bayer);
161 int imx_media_enum_format(u32 *fourcc, u32 index, enum codespace_sel cs_sel);
162 const struct imx_media_pixfmt *
163 imx_media_find_mbus_format(u32 code, enum codespace_sel cs_sel,
164                            bool allow_bayer);
165 int imx_media_enum_mbus_format(u32 *code, u32 index, enum codespace_sel cs_sel,
166                                bool allow_bayer);
167 const struct imx_media_pixfmt *
168 imx_media_find_ipu_format(u32 code, enum codespace_sel cs_sel);
169 int imx_media_enum_ipu_format(u32 *code, u32 index, enum codespace_sel cs_sel);
170 int imx_media_init_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
171                             u32 width, u32 height, u32 code, u32 field,
172                             const struct imx_media_pixfmt **cc);
173 int imx_media_init_cfg(struct v4l2_subdev *sd,
174                        struct v4l2_subdev_pad_config *cfg);
175 void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
176                                         struct v4l2_mbus_framefmt *fmt,
177                                         bool ic_route);
178 int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
179                                   struct v4l2_rect *compose,
180                                   const struct v4l2_mbus_framefmt *mbus,
181                                   const struct imx_media_pixfmt *cc);
182 int imx_media_mbus_fmt_to_ipu_image(struct ipu_image *image,
183                                     struct v4l2_mbus_framefmt *mbus);
184 int imx_media_ipu_image_to_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
185                                     struct ipu_image *image);
186 void imx_media_grp_id_to_sd_name(char *sd_name, int sz,
187                                  u32 grp_id, int ipu_id);
188 struct v4l2_subdev *
189 imx_media_find_subdev_by_fwnode(struct imx_media_dev *imxmd,
190                                 struct fwnode_handle *fwnode);
191 struct v4l2_subdev *
192 imx_media_find_subdev_by_devname(struct imx_media_dev *imxmd,
193                                  const char *devname);
194 int imx_media_add_video_device(struct imx_media_dev *imxmd,
195                                struct imx_media_video_dev *vdev);
196 int imx_media_find_mipi_csi2_channel(struct imx_media_dev *imxmd,
197                                      struct media_entity *start_entity);
198 struct media_pad *
199 imx_media_find_upstream_pad(struct imx_media_dev *imxmd,
200                             struct media_entity *start_entity,
201                             u32 grp_id);
202 struct v4l2_subdev *
203 imx_media_find_upstream_subdev(struct imx_media_dev *imxmd,
204                                struct media_entity *start_entity,
205                                u32 grp_id);
206
207 struct imx_media_dma_buf {
208         void          *virt;
209         dma_addr_t     phys;
210         unsigned long  len;
211 };
212
213 void imx_media_free_dma_buf(struct imx_media_dev *imxmd,
214                             struct imx_media_dma_buf *buf);
215 int imx_media_alloc_dma_buf(struct imx_media_dev *imxmd,
216                             struct imx_media_dma_buf *buf,
217                             int size);
218
219 int imx_media_pipeline_set_stream(struct imx_media_dev *imxmd,
220                                   struct media_entity *entity,
221                                   bool on);
222
223 /* imx-media-dev.c */
224 int imx_media_add_async_subdev(struct imx_media_dev *imxmd,
225                                struct fwnode_handle *fwnode,
226                                struct platform_device *pdev);
227
228 int imx_media_subdev_bound(struct v4l2_async_notifier *notifier,
229                            struct v4l2_subdev *sd,
230                            struct v4l2_async_subdev *asd);
231 int imx_media_link_notify(struct media_link *link, u32 flags,
232                           unsigned int notification);
233 void imx_media_notify(struct v4l2_subdev *sd, unsigned int notification,
234                       void *arg);
235 int imx_media_probe_complete(struct v4l2_async_notifier *notifier);
236
237 struct imx_media_dev *imx_media_dev_init(struct device *dev);
238 int imx_media_dev_notifier_register(struct imx_media_dev *imxmd);
239
240 /* imx-media-fim.c */
241 struct imx_media_fim;
242 void imx_media_fim_eof_monitor(struct imx_media_fim *fim, ktime_t timestamp);
243 int imx_media_fim_set_stream(struct imx_media_fim *fim,
244                              const struct v4l2_fract *frame_interval,
245                              bool on);
246 int imx_media_fim_add_controls(struct imx_media_fim *fim);
247 struct imx_media_fim *imx_media_fim_init(struct v4l2_subdev *sd);
248 void imx_media_fim_free(struct imx_media_fim *fim);
249
250 /* imx-media-internal-sd.c */
251 int imx_media_add_ipu_internal_subdevs(struct imx_media_dev *imxmd,
252                                        int ipu_id);
253 int imx_media_create_ipu_internal_links(struct imx_media_dev *imxmd,
254                                         struct v4l2_subdev *sd);
255 void imx_media_remove_ipu_internal_subdevs(struct imx_media_dev *imxmd);
256
257 /* imx-media-of.c */
258 int imx_media_add_of_subdevs(struct imx_media_dev *dev,
259                              struct device_node *np);
260 int imx_media_create_of_links(struct imx_media_dev *imxmd,
261                               struct v4l2_subdev *sd);
262 int imx_media_create_csi_of_links(struct imx_media_dev *imxmd,
263                                   struct v4l2_subdev *csi);
264 int imx_media_of_add_csi(struct imx_media_dev *imxmd,
265                          struct device_node *csi_np);
266
267 /* imx-media-capture.c */
268 struct imx_media_video_dev *
269 imx_media_capture_device_init(struct v4l2_subdev *src_sd, int pad);
270 void imx_media_capture_device_remove(struct imx_media_video_dev *vdev);
271 int imx_media_capture_device_register(struct imx_media_dev *md,
272                                       struct imx_media_video_dev *vdev);
273 void imx_media_capture_device_unregister(struct imx_media_video_dev *vdev);
274 struct imx_media_buffer *
275 imx_media_capture_device_next_buf(struct imx_media_video_dev *vdev);
276 void imx_media_capture_device_set_format(struct imx_media_video_dev *vdev,
277                                          const struct v4l2_pix_format *pix,
278                                          const struct v4l2_rect *compose);
279 void imx_media_capture_device_error(struct imx_media_video_dev *vdev);
280
281 /* subdev group ids */
282 #define IMX_MEDIA_GRP_ID_CSI2          BIT(8)
283 #define IMX_MEDIA_GRP_ID_CSI           BIT(9)
284 #define IMX_MEDIA_GRP_ID_IPU_CSI_BIT   10
285 #define IMX_MEDIA_GRP_ID_IPU_CSI       (0x3 << IMX_MEDIA_GRP_ID_IPU_CSI_BIT)
286 #define IMX_MEDIA_GRP_ID_IPU_CSI0      BIT(IMX_MEDIA_GRP_ID_IPU_CSI_BIT)
287 #define IMX_MEDIA_GRP_ID_IPU_CSI1      (2 << IMX_MEDIA_GRP_ID_IPU_CSI_BIT)
288 #define IMX_MEDIA_GRP_ID_IPU_VDIC      BIT(12)
289 #define IMX_MEDIA_GRP_ID_IPU_IC_PRP    BIT(13)
290 #define IMX_MEDIA_GRP_ID_IPU_IC_PRPENC BIT(14)
291 #define IMX_MEDIA_GRP_ID_IPU_IC_PRPVF  BIT(15)
292
293 #endif