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 <linux/platform_device.h>
15 #include <media/v4l2-ctrls.h>
16 #include <media/v4l2-device.h>
17 #include <media/v4l2-fwnode.h>
18 #include <media/v4l2-subdev.h>
19 #include <media/videobuf2-dma-contig.h>
20 #include <video/imx-ipu-v3.h>
21 
22 /*
23  * Pad definitions for the subdevs with multiple source or
24  * sink pads
25  */
26 
27 /* ipu_csi */
28 enum {
29 	CSI_SINK_PAD = 0,
30 	CSI_SRC_PAD_DIRECT,
31 	CSI_SRC_PAD_IDMAC,
32 	CSI_NUM_PADS,
33 };
34 
35 /* ipu_vdic */
36 enum {
37 	VDIC_SINK_PAD_DIRECT = 0,
38 	VDIC_SINK_PAD_IDMAC,
39 	VDIC_SRC_PAD_DIRECT,
40 	VDIC_NUM_PADS,
41 };
42 
43 /* ipu_ic_prp */
44 enum {
45 	PRP_SINK_PAD = 0,
46 	PRP_SRC_PAD_PRPENC,
47 	PRP_SRC_PAD_PRPVF,
48 	PRP_NUM_PADS,
49 };
50 
51 /* ipu_ic_prpencvf */
52 enum {
53 	PRPENCVF_SINK_PAD = 0,
54 	PRPENCVF_SRC_PAD,
55 	PRPENCVF_NUM_PADS,
56 };
57 
58 /* How long to wait for EOF interrupts in the buffer-capture subdevs */
59 #define IMX_MEDIA_EOF_TIMEOUT       1000
60 
61 struct imx_media_pixfmt {
62 	u32     fourcc;
63 	u32     codes[4];
64 	int     bpp;     /* total bpp */
65 	enum ipu_color_space cs;
66 	bool    planar;  /* is a planar format */
67 	bool    bayer;   /* is a raw bayer format */
68 	bool    ipufmt;  /* is one of the IPU internal formats */
69 };
70 
71 struct imx_media_buffer {
72 	struct vb2_v4l2_buffer vbuf; /* v4l buffer must be first */
73 	struct list_head  list;
74 };
75 
76 struct imx_media_video_dev {
77 	struct video_device *vfd;
78 
79 	/* the user format */
80 	struct v4l2_format fmt;
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_internal_sd_platformdata {
115 	char sd_name[V4L2_SUBDEV_NAME_SIZE];
116 	u32 grp_id;
117 	int ipu_id;
118 };
119 
120 
121 struct imx_media_async_subdev {
122 	struct v4l2_async_subdev asd;
123 	/* the platform device of IPU-internal subdevs */
124 	struct platform_device *pdev;
125 	struct list_head list;
126 };
127 
128 static inline struct imx_media_async_subdev *
129 to_imx_media_asd(struct v4l2_async_subdev *asd)
130 {
131 	return container_of(asd, struct imx_media_async_subdev, asd);
132 }
133 
134 struct imx_media_dev {
135 	struct media_device md;
136 	struct v4l2_device  v4l2_dev;
137 
138 	/* the pipeline object */
139 	struct media_pipeline pipe;
140 
141 	struct mutex mutex; /* protect elements below */
142 
143 	/* master video device list */
144 	struct list_head vdev_list;
145 
146 	/* IPUs this media driver control, valid after subdevs bound */
147 	struct ipu_soc *ipu[2];
148 
149 	/* for async subdev registration */
150 	struct list_head asd_list;
151 	struct v4l2_async_notifier subdev_notifier;
152 };
153 
154 enum codespace_sel {
155 	CS_SEL_YUV = 0,
156 	CS_SEL_RGB,
157 	CS_SEL_ANY,
158 };
159 
160 /* imx-media-utils.c */
161 const struct imx_media_pixfmt *
162 imx_media_find_format(u32 fourcc, enum codespace_sel cs_sel, bool allow_bayer);
163 int imx_media_enum_format(u32 *fourcc, u32 index, enum codespace_sel cs_sel);
164 const struct imx_media_pixfmt *
165 imx_media_find_mbus_format(u32 code, enum codespace_sel cs_sel,
166 			   bool allow_bayer);
167 int imx_media_enum_mbus_format(u32 *code, u32 index, enum codespace_sel cs_sel,
168 			       bool allow_bayer);
169 const struct imx_media_pixfmt *
170 imx_media_find_ipu_format(u32 code, enum codespace_sel cs_sel);
171 int imx_media_enum_ipu_format(u32 *code, u32 index, enum codespace_sel cs_sel);
172 int imx_media_init_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
173 			    u32 width, u32 height, u32 code, u32 field,
174 			    const struct imx_media_pixfmt **cc);
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_mbus_framefmt *mbus,
180 				  const struct imx_media_pixfmt *cc);
181 int imx_media_mbus_fmt_to_ipu_image(struct ipu_image *image,
182 				    struct v4l2_mbus_framefmt *mbus);
183 int imx_media_ipu_image_to_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
184 				    struct ipu_image *image);
185 void imx_media_grp_id_to_sd_name(char *sd_name, int sz,
186 				 u32 grp_id, int ipu_id);
187 struct v4l2_subdev *
188 imx_media_find_subdev_by_fwnode(struct imx_media_dev *imxmd,
189 				struct fwnode_handle *fwnode);
190 struct v4l2_subdev *
191 imx_media_find_subdev_by_devname(struct imx_media_dev *imxmd,
192 				 const char *devname);
193 int imx_media_add_video_device(struct imx_media_dev *imxmd,
194 			       struct imx_media_video_dev *vdev);
195 int imx_media_find_mipi_csi2_channel(struct imx_media_dev *imxmd,
196 				     struct media_entity *start_entity);
197 struct media_pad *
198 imx_media_find_upstream_pad(struct imx_media_dev *imxmd,
199 			    struct media_entity *start_entity,
200 			    u32 grp_id);
201 struct v4l2_subdev *
202 imx_media_find_upstream_subdev(struct imx_media_dev *imxmd,
203 			       struct media_entity *start_entity,
204 			       u32 grp_id);
205 
206 struct imx_media_dma_buf {
207 	void          *virt;
208 	dma_addr_t     phys;
209 	unsigned long  len;
210 };
211 
212 void imx_media_free_dma_buf(struct imx_media_dev *imxmd,
213 			    struct imx_media_dma_buf *buf);
214 int imx_media_alloc_dma_buf(struct imx_media_dev *imxmd,
215 			    struct imx_media_dma_buf *buf,
216 			    int size);
217 
218 int imx_media_pipeline_set_stream(struct imx_media_dev *imxmd,
219 				  struct media_entity *entity,
220 				  bool on);
221 
222 /* imx-media-dev.c */
223 int imx_media_add_async_subdev(struct imx_media_dev *imxmd,
224 			       struct fwnode_handle *fwnode,
225 			       struct platform_device *pdev);
226 
227 /* imx-media-fim.c */
228 struct imx_media_fim;
229 void imx_media_fim_eof_monitor(struct imx_media_fim *fim, ktime_t timestamp);
230 int imx_media_fim_set_stream(struct imx_media_fim *fim,
231 			     const struct v4l2_fract *frame_interval,
232 			     bool on);
233 int imx_media_fim_add_controls(struct imx_media_fim *fim);
234 struct imx_media_fim *imx_media_fim_init(struct v4l2_subdev *sd);
235 void imx_media_fim_free(struct imx_media_fim *fim);
236 
237 /* imx-media-internal-sd.c */
238 int imx_media_add_internal_subdevs(struct imx_media_dev *imxmd);
239 int imx_media_create_internal_links(struct imx_media_dev *imxmd,
240 				    struct v4l2_subdev *sd);
241 void imx_media_remove_internal_subdevs(struct imx_media_dev *imxmd);
242 
243 /* imx-media-of.c */
244 int imx_media_add_of_subdevs(struct imx_media_dev *dev,
245 			     struct device_node *np);
246 int imx_media_create_of_links(struct imx_media_dev *imxmd,
247 			      struct v4l2_subdev *sd);
248 int imx_media_create_csi_of_links(struct imx_media_dev *imxmd,
249 				  struct v4l2_subdev *csi);
250 
251 /* imx-media-capture.c */
252 struct imx_media_video_dev *
253 imx_media_capture_device_init(struct v4l2_subdev *src_sd, int pad);
254 void imx_media_capture_device_remove(struct imx_media_video_dev *vdev);
255 int imx_media_capture_device_register(struct imx_media_video_dev *vdev);
256 void imx_media_capture_device_unregister(struct imx_media_video_dev *vdev);
257 struct imx_media_buffer *
258 imx_media_capture_device_next_buf(struct imx_media_video_dev *vdev);
259 void imx_media_capture_device_set_format(struct imx_media_video_dev *vdev,
260 					 struct v4l2_pix_format *pix);
261 void imx_media_capture_device_error(struct imx_media_video_dev *vdev);
262 
263 /* subdev group ids */
264 #define IMX_MEDIA_GRP_ID_CSI2      BIT(8)
265 #define IMX_MEDIA_GRP_ID_CSI_BIT   9
266 #define IMX_MEDIA_GRP_ID_CSI       (0x3 << IMX_MEDIA_GRP_ID_CSI_BIT)
267 #define IMX_MEDIA_GRP_ID_CSI0      BIT(IMX_MEDIA_GRP_ID_CSI_BIT)
268 #define IMX_MEDIA_GRP_ID_CSI1      (2 << IMX_MEDIA_GRP_ID_CSI_BIT)
269 #define IMX_MEDIA_GRP_ID_VDIC      BIT(11)
270 #define IMX_MEDIA_GRP_ID_IC_PRP    BIT(12)
271 #define IMX_MEDIA_GRP_ID_IC_PRPENC BIT(13)
272 #define IMX_MEDIA_GRP_ID_IC_PRPVF  BIT(14)
273 
274 #endif
275