2 * V4L2 Media Controller Driver for Freescale i.MX5/6 SOC
4 * Copyright (c) 2016 Mentor Graphics Inc.
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.
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>
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)
32 /* max video devices */
33 #define IMX_MEDIA_MAX_VDEVS 8
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
42 * Pad definitions for the subdevs with multiple source or
54 #define CSI_NUM_SINK_PADS 1
55 #define CSI_NUM_SRC_PADS 2
59 VDIC_SINK_PAD_DIRECT = 0,
65 #define VDIC_NUM_SINK_PADS 2
66 #define VDIC_NUM_SRC_PADS 1
76 #define PRP_NUM_SINK_PADS 1
77 #define PRP_NUM_SRC_PADS 2
81 PRPENCVF_SINK_PAD = 0,
86 #define PRPENCVF_NUM_SINK_PADS 1
87 #define PRPENCVF_NUM_SRC_PADS 1
89 /* How long to wait for EOF interrupts in the buffer-capture subdevs */
90 #define IMX_MEDIA_EOF_TIMEOUT 1000
92 struct imx_media_pixfmt {
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 */
102 struct imx_media_buffer {
103 struct vb2_v4l2_buffer vbuf; /* v4l buffer must be first */
104 struct list_head list;
107 struct imx_media_video_dev {
108 struct video_device *vfd;
110 /* the user format */
111 struct v4l2_format fmt;
112 const struct imx_media_pixfmt *cc;
115 static inline struct imx_media_buffer *to_imx_media_vb(struct vb2_buffer *vb)
117 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
119 return container_of(vbuf, struct imx_media_buffer, vbuf);
122 struct imx_media_link {
123 struct device_node *remote_sd_node;
124 char remote_devname[32];
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 */
136 * list of video devices that can be reached from this pad,
137 * list is only valid for source pads.
139 struct imx_media_video_dev *vdev[IMX_MEDIA_MAX_VDEVS];
143 struct imx_media_internal_sd_platformdata {
144 char sd_name[V4L2_SUBDEV_NAME_SIZE];
149 struct imx_media_subdev {
150 struct v4l2_async_subdev asd;
151 struct v4l2_subdev *sd; /* set when bound */
153 struct imx_media_pad pad[IMX_MEDIA_MAX_PADS];
157 /* the platform device if this is an internal subdev */
158 struct platform_device *pdev;
159 /* the devname is needed for async devname match */
163 struct imx_media_dev {
164 struct media_device md;
165 struct v4l2_device v4l2_dev;
167 /* the pipeline object */
168 struct media_pipeline pipe;
170 struct mutex mutex; /* protect elements below */
172 /* master subdevice list */
173 struct imx_media_subdev subdev[IMX_MEDIA_MAX_SUBDEVS];
176 /* master video device list */
177 struct imx_media_video_dev *vdev[IMX_MEDIA_MAX_VDEVS];
180 /* IPUs this media driver control, valid after subdevs bound */
181 struct ipu_soc *ipu[2];
183 /* for async subdev registration */
184 struct v4l2_async_subdev *async_ptrs[IMX_MEDIA_MAX_SUBDEVS];
185 struct v4l2_async_notifier subdev_notifier;
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,
200 int imx_media_enum_mbus_format(u32 *code, u32 index, enum codespace_sel cs_sel,
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);
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,
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);
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);
234 void imx_media_grp_id_to_sd_name(char *sd_name, int sz,
235 u32 grp_id, int ipu_id);
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);
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,
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);
252 imx_media_find_upstream_pad(struct imx_media_dev *imxmd,
253 struct media_entity *start_entity,
255 struct imx_media_subdev *
256 imx_media_find_upstream_subdev(struct imx_media_dev *imxmd,
257 struct media_entity *start_entity,
260 struct imx_media_dma_buf {
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,
272 int imx_media_pipeline_set_stream(struct imx_media_dev *imxmd,
273 struct media_entity *entity,
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,
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);
287 struct imx_media_subdev *
288 imx_media_of_find_subdev(struct imx_media_dev *imxmd,
289 struct device_node *np,
291 int imx_media_of_parse(struct imx_media_dev *dev,
292 struct imx_media_subdev *(*csi)[4],
293 struct device_node *np);
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);
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)