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 	/* cycles per pixel for generic (bayer) formats for the parallel bus */
66 	int	cycles;
67 	enum ipu_color_space cs;
68 	bool    planar;  /* is a planar format */
69 	bool    bayer;   /* is a raw bayer format */
70 	bool    ipufmt;  /* is one of the IPU internal formats */
71 };
72 
73 struct imx_media_buffer {
74 	struct vb2_v4l2_buffer vbuf; /* v4l buffer must be first */
75 	struct list_head  list;
76 };
77 
78 struct imx_media_video_dev {
79 	struct video_device *vfd;
80 
81 	/* the user format */
82 	struct v4l2_format fmt;
83 	const struct imx_media_pixfmt *cc;
84 
85 	/* links this vdev to master list */
86 	struct list_head list;
87 };
88 
to_imx_media_vb(struct vb2_buffer * vb)89 static inline struct imx_media_buffer *to_imx_media_vb(struct vb2_buffer *vb)
90 {
91 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
92 
93 	return container_of(vbuf, struct imx_media_buffer, vbuf);
94 }
95 
96 /*
97  * to support control inheritance to video devices, this
98  * retrieves a pad's list_head of video devices that can
99  * be reached from the pad. Note that only the lists in
100  * source pads get populated, sink pads have empty lists.
101  */
102 static inline struct list_head *
to_pad_vdev_list(struct v4l2_subdev * sd,int pad_index)103 to_pad_vdev_list(struct v4l2_subdev *sd, int pad_index)
104 {
105 	struct list_head *vdev_list = sd->host_priv;
106 
107 	return vdev_list ? &vdev_list[pad_index] : NULL;
108 }
109 
110 /* an entry in a pad's video device list */
111 struct imx_media_pad_vdev {
112 	struct imx_media_video_dev *vdev;
113 	struct list_head list;
114 };
115 
116 struct imx_media_internal_sd_platformdata {
117 	char sd_name[V4L2_SUBDEV_NAME_SIZE];
118 	u32 grp_id;
119 	int ipu_id;
120 };
121 
122 
123 struct imx_media_async_subdev {
124 	struct v4l2_async_subdev asd;
125 	/* the platform device of IPU-internal subdevs */
126 	struct platform_device *pdev;
127 	struct list_head list;
128 };
129 
130 static inline struct imx_media_async_subdev *
to_imx_media_asd(struct v4l2_async_subdev * asd)131 to_imx_media_asd(struct v4l2_async_subdev *asd)
132 {
133 	return container_of(asd, struct imx_media_async_subdev, asd);
134 }
135 
136 struct imx_media_dev {
137 	struct media_device md;
138 	struct v4l2_device  v4l2_dev;
139 
140 	/* the pipeline object */
141 	struct media_pipeline pipe;
142 
143 	struct mutex mutex; /* protect elements below */
144 
145 	/* master video device list */
146 	struct list_head vdev_list;
147 
148 	/* IPUs this media driver control, valid after subdevs bound */
149 	struct ipu_soc *ipu[2];
150 
151 	/* for async subdev registration */
152 	struct list_head asd_list;
153 	struct v4l2_async_notifier subdev_notifier;
154 };
155 
156 enum codespace_sel {
157 	CS_SEL_YUV = 0,
158 	CS_SEL_RGB,
159 	CS_SEL_ANY,
160 };
161 
162 /* imx-media-utils.c */
163 const struct imx_media_pixfmt *
164 imx_media_find_format(u32 fourcc, enum codespace_sel cs_sel, bool allow_bayer);
165 int imx_media_enum_format(u32 *fourcc, u32 index, enum codespace_sel cs_sel);
166 const struct imx_media_pixfmt *
167 imx_media_find_mbus_format(u32 code, enum codespace_sel cs_sel,
168 			   bool allow_bayer);
169 int imx_media_enum_mbus_format(u32 *code, u32 index, enum codespace_sel cs_sel,
170 			       bool allow_bayer);
171 const struct imx_media_pixfmt *
172 imx_media_find_ipu_format(u32 code, enum codespace_sel cs_sel);
173 int imx_media_enum_ipu_format(u32 *code, u32 index, enum codespace_sel cs_sel);
174 int imx_media_init_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
175 			    u32 width, u32 height, u32 code, u32 field,
176 			    const struct imx_media_pixfmt **cc);
177 int imx_media_init_cfg(struct v4l2_subdev *sd,
178 		       struct v4l2_subdev_pad_config *cfg);
179 void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
180 					struct v4l2_mbus_framefmt *fmt,
181 					bool ic_route);
182 int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
183 				  struct v4l2_mbus_framefmt *mbus,
184 				  const struct imx_media_pixfmt *cc);
185 int imx_media_mbus_fmt_to_ipu_image(struct ipu_image *image,
186 				    struct v4l2_mbus_framefmt *mbus);
187 int imx_media_ipu_image_to_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
188 				    struct ipu_image *image);
189 void imx_media_grp_id_to_sd_name(char *sd_name, int sz,
190 				 u32 grp_id, int ipu_id);
191 struct v4l2_subdev *
192 imx_media_find_subdev_by_fwnode(struct imx_media_dev *imxmd,
193 				struct fwnode_handle *fwnode);
194 struct v4l2_subdev *
195 imx_media_find_subdev_by_devname(struct imx_media_dev *imxmd,
196 				 const char *devname);
197 int imx_media_add_video_device(struct imx_media_dev *imxmd,
198 			       struct imx_media_video_dev *vdev);
199 int imx_media_find_mipi_csi2_channel(struct imx_media_dev *imxmd,
200 				     struct media_entity *start_entity);
201 struct media_pad *
202 imx_media_find_upstream_pad(struct imx_media_dev *imxmd,
203 			    struct media_entity *start_entity,
204 			    u32 grp_id);
205 struct v4l2_subdev *
206 imx_media_find_upstream_subdev(struct imx_media_dev *imxmd,
207 			       struct media_entity *start_entity,
208 			       u32 grp_id);
209 
210 struct imx_media_dma_buf {
211 	void          *virt;
212 	dma_addr_t     phys;
213 	unsigned long  len;
214 };
215 
216 void imx_media_free_dma_buf(struct imx_media_dev *imxmd,
217 			    struct imx_media_dma_buf *buf);
218 int imx_media_alloc_dma_buf(struct imx_media_dev *imxmd,
219 			    struct imx_media_dma_buf *buf,
220 			    int size);
221 
222 int imx_media_pipeline_set_stream(struct imx_media_dev *imxmd,
223 				  struct media_entity *entity,
224 				  bool on);
225 
226 /* imx-media-dev.c */
227 int imx_media_add_async_subdev(struct imx_media_dev *imxmd,
228 			       struct fwnode_handle *fwnode,
229 			       struct platform_device *pdev);
230 
231 /* imx-media-fim.c */
232 struct imx_media_fim;
233 void imx_media_fim_eof_monitor(struct imx_media_fim *fim, ktime_t timestamp);
234 int imx_media_fim_set_stream(struct imx_media_fim *fim,
235 			     const struct v4l2_fract *frame_interval,
236 			     bool on);
237 int imx_media_fim_add_controls(struct imx_media_fim *fim);
238 struct imx_media_fim *imx_media_fim_init(struct v4l2_subdev *sd);
239 void imx_media_fim_free(struct imx_media_fim *fim);
240 
241 /* imx-media-internal-sd.c */
242 int imx_media_add_internal_subdevs(struct imx_media_dev *imxmd);
243 int imx_media_create_internal_links(struct imx_media_dev *imxmd,
244 				    struct v4l2_subdev *sd);
245 void imx_media_remove_internal_subdevs(struct imx_media_dev *imxmd);
246 
247 /* imx-media-of.c */
248 int imx_media_add_of_subdevs(struct imx_media_dev *dev,
249 			     struct device_node *np);
250 int imx_media_create_of_links(struct imx_media_dev *imxmd,
251 			      struct v4l2_subdev *sd);
252 int imx_media_create_csi_of_links(struct imx_media_dev *imxmd,
253 				  struct v4l2_subdev *csi);
254 
255 /* imx-media-capture.c */
256 struct imx_media_video_dev *
257 imx_media_capture_device_init(struct v4l2_subdev *src_sd, int pad);
258 void imx_media_capture_device_remove(struct imx_media_video_dev *vdev);
259 int imx_media_capture_device_register(struct imx_media_video_dev *vdev);
260 void imx_media_capture_device_unregister(struct imx_media_video_dev *vdev);
261 struct imx_media_buffer *
262 imx_media_capture_device_next_buf(struct imx_media_video_dev *vdev);
263 void imx_media_capture_device_set_format(struct imx_media_video_dev *vdev,
264 					 struct v4l2_pix_format *pix);
265 void imx_media_capture_device_error(struct imx_media_video_dev *vdev);
266 
267 /* subdev group ids */
268 #define IMX_MEDIA_GRP_ID_CSI2      BIT(8)
269 #define IMX_MEDIA_GRP_ID_CSI_BIT   9
270 #define IMX_MEDIA_GRP_ID_CSI       (0x3 << IMX_MEDIA_GRP_ID_CSI_BIT)
271 #define IMX_MEDIA_GRP_ID_CSI0      BIT(IMX_MEDIA_GRP_ID_CSI_BIT)
272 #define IMX_MEDIA_GRP_ID_CSI1      (2 << IMX_MEDIA_GRP_ID_CSI_BIT)
273 #define IMX_MEDIA_GRP_ID_VDIC      BIT(11)
274 #define IMX_MEDIA_GRP_ID_IC_PRP    BIT(12)
275 #define IMX_MEDIA_GRP_ID_IC_PRPENC BIT(13)
276 #define IMX_MEDIA_GRP_ID_IC_PRPVF  BIT(14)
277 
278 #endif
279