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 int imx_media_init_cfg(struct v4l2_subdev *sd,
176 		       struct v4l2_subdev_pad_config *cfg);
177 void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
178 					struct v4l2_mbus_framefmt *fmt,
179 					bool ic_route);
180 int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
181 				  struct v4l2_mbus_framefmt *mbus,
182 				  const struct imx_media_pixfmt *cc);
183 int imx_media_mbus_fmt_to_ipu_image(struct ipu_image *image,
184 				    struct v4l2_mbus_framefmt *mbus);
185 int imx_media_ipu_image_to_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
186 				    struct ipu_image *image);
187 void imx_media_grp_id_to_sd_name(char *sd_name, int sz,
188 				 u32 grp_id, int ipu_id);
189 struct v4l2_subdev *
190 imx_media_find_subdev_by_fwnode(struct imx_media_dev *imxmd,
191 				struct fwnode_handle *fwnode);
192 struct v4l2_subdev *
193 imx_media_find_subdev_by_devname(struct imx_media_dev *imxmd,
194 				 const char *devname);
195 int imx_media_add_video_device(struct imx_media_dev *imxmd,
196 			       struct imx_media_video_dev *vdev);
197 int imx_media_find_mipi_csi2_channel(struct imx_media_dev *imxmd,
198 				     struct media_entity *start_entity);
199 struct media_pad *
200 imx_media_find_upstream_pad(struct imx_media_dev *imxmd,
201 			    struct media_entity *start_entity,
202 			    u32 grp_id);
203 struct v4l2_subdev *
204 imx_media_find_upstream_subdev(struct imx_media_dev *imxmd,
205 			       struct media_entity *start_entity,
206 			       u32 grp_id);
207 
208 struct imx_media_dma_buf {
209 	void          *virt;
210 	dma_addr_t     phys;
211 	unsigned long  len;
212 };
213 
214 void imx_media_free_dma_buf(struct imx_media_dev *imxmd,
215 			    struct imx_media_dma_buf *buf);
216 int imx_media_alloc_dma_buf(struct imx_media_dev *imxmd,
217 			    struct imx_media_dma_buf *buf,
218 			    int size);
219 
220 int imx_media_pipeline_set_stream(struct imx_media_dev *imxmd,
221 				  struct media_entity *entity,
222 				  bool on);
223 
224 /* imx-media-dev.c */
225 int imx_media_add_async_subdev(struct imx_media_dev *imxmd,
226 			       struct fwnode_handle *fwnode,
227 			       struct platform_device *pdev);
228 
229 /* imx-media-fim.c */
230 struct imx_media_fim;
231 void imx_media_fim_eof_monitor(struct imx_media_fim *fim, ktime_t timestamp);
232 int imx_media_fim_set_stream(struct imx_media_fim *fim,
233 			     const struct v4l2_fract *frame_interval,
234 			     bool on);
235 int imx_media_fim_add_controls(struct imx_media_fim *fim);
236 struct imx_media_fim *imx_media_fim_init(struct v4l2_subdev *sd);
237 void imx_media_fim_free(struct imx_media_fim *fim);
238 
239 /* imx-media-internal-sd.c */
240 int imx_media_add_internal_subdevs(struct imx_media_dev *imxmd);
241 int imx_media_create_internal_links(struct imx_media_dev *imxmd,
242 				    struct v4l2_subdev *sd);
243 void imx_media_remove_internal_subdevs(struct imx_media_dev *imxmd);
244 
245 /* imx-media-of.c */
246 int imx_media_add_of_subdevs(struct imx_media_dev *dev,
247 			     struct device_node *np);
248 int imx_media_create_of_links(struct imx_media_dev *imxmd,
249 			      struct v4l2_subdev *sd);
250 int imx_media_create_csi_of_links(struct imx_media_dev *imxmd,
251 				  struct v4l2_subdev *csi);
252 
253 /* imx-media-capture.c */
254 struct imx_media_video_dev *
255 imx_media_capture_device_init(struct v4l2_subdev *src_sd, int pad);
256 void imx_media_capture_device_remove(struct imx_media_video_dev *vdev);
257 int imx_media_capture_device_register(struct imx_media_video_dev *vdev);
258 void imx_media_capture_device_unregister(struct imx_media_video_dev *vdev);
259 struct imx_media_buffer *
260 imx_media_capture_device_next_buf(struct imx_media_video_dev *vdev);
261 void imx_media_capture_device_set_format(struct imx_media_video_dev *vdev,
262 					 struct v4l2_pix_format *pix);
263 void imx_media_capture_device_error(struct imx_media_video_dev *vdev);
264 
265 /* subdev group ids */
266 #define IMX_MEDIA_GRP_ID_CSI2      BIT(8)
267 #define IMX_MEDIA_GRP_ID_CSI_BIT   9
268 #define IMX_MEDIA_GRP_ID_CSI       (0x3 << IMX_MEDIA_GRP_ID_CSI_BIT)
269 #define IMX_MEDIA_GRP_ID_CSI0      BIT(IMX_MEDIA_GRP_ID_CSI_BIT)
270 #define IMX_MEDIA_GRP_ID_CSI1      (2 << IMX_MEDIA_GRP_ID_CSI_BIT)
271 #define IMX_MEDIA_GRP_ID_VDIC      BIT(11)
272 #define IMX_MEDIA_GRP_ID_IC_PRP    BIT(12)
273 #define IMX_MEDIA_GRP_ID_IC_PRPENC BIT(13)
274 #define IMX_MEDIA_GRP_ID_IC_PRPVF  BIT(14)
275 
276 #endif
277