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 */
162 /* if this is a sensor */
163 struct v4l2_fwnode_endpoint sensor_ep;
166 struct imx_media_dev {
167 struct media_device md;
168 struct v4l2_device v4l2_dev;
170 /* the pipeline object */
171 struct media_pipeline pipe;
173 struct mutex mutex; /* protect elements below */
175 /* master subdevice list */
176 struct imx_media_subdev subdev[IMX_MEDIA_MAX_SUBDEVS];
179 /* master video device list */
180 struct imx_media_video_dev *vdev[IMX_MEDIA_MAX_VDEVS];
183 /* IPUs this media driver control, valid after subdevs bound */
184 struct ipu_soc *ipu[2];
186 /* for async subdev registration */
187 struct v4l2_async_subdev *async_ptrs[IMX_MEDIA_MAX_SUBDEVS];
188 struct v4l2_async_notifier subdev_notifier;
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,
203 int imx_media_enum_mbus_format(u32 *code, u32 index, enum codespace_sel cs_sel,
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);
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,
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);
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);
237 void imx_media_grp_id_to_sd_name(char *sd_name, int sz,
238 u32 grp_id, int ipu_id);
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);
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,
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,
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);
265 struct imx_media_dma_buf {
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,
277 int imx_media_pipeline_set_stream(struct imx_media_dev *imxmd,
278 struct media_entity *entity,
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,
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);
292 struct imx_media_subdev *
293 imx_media_of_find_subdev(struct imx_media_dev *imxmd,
294 struct device_node *np,
296 int imx_media_of_parse(struct imx_media_dev *dev,
297 struct imx_media_subdev *(*csi)[4],
298 struct device_node *np);
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);
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)