Commit | Line | Data |
---|---|---|
4a34ec8e SL |
1 | /* |
2 | * V4L2 Capture CSI Subdev for Freescale i.MX5/6 SOC | |
3 | * | |
4 | * Copyright (c) 2014-2017 Mentor Graphics Inc. | |
fb30ee79 | 5 | * Copyright (C) 2017 Pengutronix, Philipp Zabel <kernel@pengutronix.de> |
4a34ec8e SL |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | */ | |
12 | #include <linux/delay.h> | |
fb30ee79 | 13 | #include <linux/gcd.h> |
4a34ec8e SL |
14 | #include <linux/interrupt.h> |
15 | #include <linux/module.h> | |
16 | #include <linux/pinctrl/consumer.h> | |
17 | #include <linux/platform_device.h> | |
18 | #include <media/v4l2-ctrls.h> | |
19 | #include <media/v4l2-device.h> | |
20 | #include <media/v4l2-event.h> | |
21 | #include <media/v4l2-fwnode.h> | |
22 | #include <media/v4l2-mc.h> | |
23 | #include <media/v4l2-subdev.h> | |
24 | #include <media/videobuf2-dma-contig.h> | |
25 | #include <video/imx-ipu-v3.h> | |
26 | #include <media/imx.h> | |
27 | #include "imx-media.h" | |
28 | ||
29 | /* | |
30 | * Min/Max supported width and heights. | |
31 | * | |
32 | * We allow planar output, so we have to align width by 16 pixels | |
33 | * to meet IDMAC alignment requirements. | |
34 | * | |
35 | * TODO: move this into pad format negotiation, if capture device | |
36 | * has not requested planar formats, we should allow 8 pixel | |
37 | * alignment. | |
38 | */ | |
39 | #define MIN_W 176 | |
40 | #define MIN_H 144 | |
41 | #define MAX_W 4096 | |
42 | #define MAX_H 4096 | |
43 | #define W_ALIGN 4 /* multiple of 16 pixels */ | |
44 | #define H_ALIGN 1 /* multiple of 2 lines */ | |
45 | #define S_ALIGN 1 /* multiple of 2 */ | |
46 | ||
fb30ee79 PZ |
47 | /* |
48 | * struct csi_skip_desc - CSI frame skipping descriptor | |
49 | * @keep - number of frames kept per max_ratio frames | |
50 | * @max_ratio - width of skip_smfc, written to MAX_RATIO bitfield | |
51 | * @skip_smfc - skip pattern written to the SKIP_SMFC bitfield | |
52 | */ | |
53 | struct csi_skip_desc { | |
54 | u8 keep; | |
55 | u8 max_ratio; | |
56 | u8 skip_smfc; | |
57 | }; | |
58 | ||
4a34ec8e SL |
59 | struct csi_priv { |
60 | struct device *dev; | |
61 | struct ipu_soc *ipu; | |
62 | struct imx_media_dev *md; | |
63 | struct v4l2_subdev sd; | |
64 | struct media_pad pad[CSI_NUM_PADS]; | |
65 | /* the video device at IDMAC output pad */ | |
66 | struct imx_media_video_dev *vdev; | |
67 | struct imx_media_fim *fim; | |
68 | int csi_id; | |
69 | int smfc_id; | |
70 | ||
71 | /* lock to protect all members below */ | |
72 | struct mutex lock; | |
73 | ||
74 | int active_output_pad; | |
75 | ||
76 | struct ipuv3_channel *idmac_ch; | |
77 | struct ipu_smfc *smfc; | |
78 | struct ipu_csi *csi; | |
79 | ||
80 | struct v4l2_mbus_framefmt format_mbus[CSI_NUM_PADS]; | |
81 | const struct imx_media_pixfmt *cc[CSI_NUM_PADS]; | |
fb30ee79 | 82 | struct v4l2_fract frame_interval[CSI_NUM_PADS]; |
4a34ec8e | 83 | struct v4l2_rect crop; |
69e78611 | 84 | struct v4l2_rect compose; |
fb30ee79 | 85 | const struct csi_skip_desc *skip; |
4a34ec8e SL |
86 | |
87 | /* active vb2 buffers to send to video dev sink */ | |
88 | struct imx_media_buffer *active_vb2_buf[2]; | |
89 | struct imx_media_dma_buf underrun_buf; | |
90 | ||
91 | int ipu_buf_num; /* ipu double buffer index: 0-1 */ | |
92 | ||
93 | /* the sink for the captured frames */ | |
94 | struct media_entity *sink; | |
95 | enum ipu_csi_dest dest; | |
96 | /* the source subdev */ | |
97 | struct v4l2_subdev *src_sd; | |
98 | ||
99 | /* the mipi virtual channel number at link validate */ | |
100 | int vc_num; | |
101 | ||
102 | /* the attached sensor at stream on */ | |
103 | struct imx_media_subdev *sensor; | |
104 | ||
105 | spinlock_t irqlock; /* protect eof_irq handler */ | |
106 | struct timer_list eof_timeout_timer; | |
107 | int eof_irq; | |
108 | int nfb4eof_irq; | |
109 | ||
110 | struct v4l2_ctrl_handler ctrl_hdlr; | |
111 | ||
112 | int stream_count; /* streaming counter */ | |
113 | bool last_eof; /* waiting for last EOF at stream off */ | |
114 | bool nfb4eof; /* NFB4EOF encountered during streaming */ | |
115 | struct completion last_eof_comp; | |
116 | }; | |
117 | ||
118 | static inline struct csi_priv *sd_to_dev(struct v4l2_subdev *sdev) | |
119 | { | |
120 | return container_of(sdev, struct csi_priv, sd); | |
121 | } | |
122 | ||
123 | static void csi_idmac_put_ipu_resources(struct csi_priv *priv) | |
124 | { | |
0b2e9e79 | 125 | if (priv->idmac_ch) |
4a34ec8e SL |
126 | ipu_idmac_put(priv->idmac_ch); |
127 | priv->idmac_ch = NULL; | |
128 | ||
0b2e9e79 | 129 | if (priv->smfc) |
4a34ec8e SL |
130 | ipu_smfc_put(priv->smfc); |
131 | priv->smfc = NULL; | |
132 | } | |
133 | ||
134 | static int csi_idmac_get_ipu_resources(struct csi_priv *priv) | |
135 | { | |
136 | int ch_num, ret; | |
0b2e9e79 AB |
137 | struct ipu_smfc *smfc; |
138 | struct ipuv3_channel *idmac_ch; | |
4a34ec8e SL |
139 | |
140 | ch_num = IPUV3_CHANNEL_CSI0 + priv->smfc_id; | |
141 | ||
0b2e9e79 AB |
142 | smfc = ipu_smfc_get(priv->ipu, ch_num); |
143 | if (IS_ERR(smfc)) { | |
4a34ec8e | 144 | v4l2_err(&priv->sd, "failed to get SMFC\n"); |
0b2e9e79 | 145 | ret = PTR_ERR(smfc); |
4a34ec8e SL |
146 | goto out; |
147 | } | |
0b2e9e79 | 148 | priv->smfc = smfc; |
4a34ec8e | 149 | |
0b2e9e79 AB |
150 | idmac_ch = ipu_idmac_get(priv->ipu, ch_num); |
151 | if (IS_ERR(idmac_ch)) { | |
4a34ec8e SL |
152 | v4l2_err(&priv->sd, "could not get IDMAC channel %u\n", |
153 | ch_num); | |
0b2e9e79 | 154 | ret = PTR_ERR(idmac_ch); |
4a34ec8e SL |
155 | goto out; |
156 | } | |
0b2e9e79 | 157 | priv->idmac_ch = idmac_ch; |
4a34ec8e SL |
158 | |
159 | return 0; | |
160 | out: | |
161 | csi_idmac_put_ipu_resources(priv); | |
162 | return ret; | |
163 | } | |
164 | ||
165 | static void csi_vb2_buf_done(struct csi_priv *priv) | |
166 | { | |
167 | struct imx_media_video_dev *vdev = priv->vdev; | |
168 | struct imx_media_buffer *done, *next; | |
169 | struct vb2_buffer *vb; | |
170 | dma_addr_t phys; | |
171 | ||
172 | done = priv->active_vb2_buf[priv->ipu_buf_num]; | |
173 | if (done) { | |
174 | vb = &done->vbuf.vb2_buf; | |
175 | vb->timestamp = ktime_get_ns(); | |
176 | vb2_buffer_done(vb, priv->nfb4eof ? | |
177 | VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); | |
178 | } | |
179 | ||
180 | priv->nfb4eof = false; | |
181 | ||
182 | /* get next queued buffer */ | |
183 | next = imx_media_capture_device_next_buf(vdev); | |
184 | if (next) { | |
185 | phys = vb2_dma_contig_plane_dma_addr(&next->vbuf.vb2_buf, 0); | |
186 | priv->active_vb2_buf[priv->ipu_buf_num] = next; | |
187 | } else { | |
188 | phys = priv->underrun_buf.phys; | |
189 | priv->active_vb2_buf[priv->ipu_buf_num] = NULL; | |
190 | } | |
191 | ||
192 | if (ipu_idmac_buffer_is_ready(priv->idmac_ch, priv->ipu_buf_num)) | |
193 | ipu_idmac_clear_buffer(priv->idmac_ch, priv->ipu_buf_num); | |
194 | ||
195 | ipu_cpmem_set_buffer(priv->idmac_ch, priv->ipu_buf_num, phys); | |
196 | } | |
197 | ||
198 | static irqreturn_t csi_idmac_eof_interrupt(int irq, void *dev_id) | |
199 | { | |
200 | struct csi_priv *priv = dev_id; | |
201 | ||
202 | spin_lock(&priv->irqlock); | |
203 | ||
204 | if (priv->last_eof) { | |
205 | complete(&priv->last_eof_comp); | |
206 | priv->last_eof = false; | |
207 | goto unlock; | |
208 | } | |
209 | ||
1d88f4bc | 210 | if (priv->fim) |
4a34ec8e | 211 | /* call frame interval monitor */ |
1d88f4bc | 212 | imx_media_fim_eof_monitor(priv->fim, ktime_get()); |
4a34ec8e SL |
213 | |
214 | csi_vb2_buf_done(priv); | |
215 | ||
216 | /* select new IPU buf */ | |
217 | ipu_idmac_select_buffer(priv->idmac_ch, priv->ipu_buf_num); | |
218 | /* toggle IPU double-buffer index */ | |
219 | priv->ipu_buf_num ^= 1; | |
220 | ||
221 | /* bump the EOF timeout timer */ | |
222 | mod_timer(&priv->eof_timeout_timer, | |
223 | jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT)); | |
224 | ||
225 | unlock: | |
226 | spin_unlock(&priv->irqlock); | |
227 | return IRQ_HANDLED; | |
228 | } | |
229 | ||
230 | static irqreturn_t csi_idmac_nfb4eof_interrupt(int irq, void *dev_id) | |
231 | { | |
232 | struct csi_priv *priv = dev_id; | |
233 | ||
234 | spin_lock(&priv->irqlock); | |
235 | ||
236 | /* | |
237 | * this is not an unrecoverable error, just mark | |
238 | * the next captured frame with vb2 error flag. | |
239 | */ | |
240 | priv->nfb4eof = true; | |
241 | ||
242 | v4l2_err(&priv->sd, "NFB4EOF\n"); | |
243 | ||
244 | spin_unlock(&priv->irqlock); | |
245 | ||
246 | return IRQ_HANDLED; | |
247 | } | |
248 | ||
249 | /* | |
250 | * EOF timeout timer function. This is an unrecoverable condition | |
251 | * without a stream restart. | |
252 | */ | |
e99e88a9 | 253 | static void csi_idmac_eof_timeout(struct timer_list *t) |
4a34ec8e | 254 | { |
e99e88a9 | 255 | struct csi_priv *priv = from_timer(priv, t, eof_timeout_timer); |
4a34ec8e SL |
256 | struct imx_media_video_dev *vdev = priv->vdev; |
257 | ||
258 | v4l2_err(&priv->sd, "EOF timeout\n"); | |
259 | ||
260 | /* signal a fatal error to capture device */ | |
261 | imx_media_capture_device_error(vdev); | |
262 | } | |
263 | ||
264 | static void csi_idmac_setup_vb2_buf(struct csi_priv *priv, dma_addr_t *phys) | |
265 | { | |
266 | struct imx_media_video_dev *vdev = priv->vdev; | |
267 | struct imx_media_buffer *buf; | |
268 | int i; | |
269 | ||
270 | for (i = 0; i < 2; i++) { | |
271 | buf = imx_media_capture_device_next_buf(vdev); | |
272 | if (buf) { | |
273 | priv->active_vb2_buf[i] = buf; | |
274 | phys[i] = vb2_dma_contig_plane_dma_addr( | |
275 | &buf->vbuf.vb2_buf, 0); | |
276 | } else { | |
277 | priv->active_vb2_buf[i] = NULL; | |
278 | phys[i] = priv->underrun_buf.phys; | |
279 | } | |
280 | } | |
281 | } | |
282 | ||
283 | static void csi_idmac_unsetup_vb2_buf(struct csi_priv *priv, | |
284 | enum vb2_buffer_state return_status) | |
285 | { | |
286 | struct imx_media_buffer *buf; | |
287 | int i; | |
288 | ||
289 | /* return any remaining active frames with return_status */ | |
290 | for (i = 0; i < 2; i++) { | |
291 | buf = priv->active_vb2_buf[i]; | |
292 | if (buf) { | |
293 | struct vb2_buffer *vb = &buf->vbuf.vb2_buf; | |
294 | ||
295 | vb->timestamp = ktime_get_ns(); | |
296 | vb2_buffer_done(vb, return_status); | |
297 | } | |
298 | } | |
299 | } | |
300 | ||
301 | /* init the SMFC IDMAC channel */ | |
302 | static int csi_idmac_setup_channel(struct csi_priv *priv) | |
303 | { | |
304 | struct imx_media_video_dev *vdev = priv->vdev; | |
305 | struct v4l2_fwnode_endpoint *sensor_ep; | |
306 | struct v4l2_mbus_framefmt *infmt; | |
4a34ec8e | 307 | struct ipu_image image; |
c8da8c03 | 308 | u32 passthrough_bits; |
4a34ec8e SL |
309 | dma_addr_t phys[2]; |
310 | bool passthrough; | |
c8da8c03 | 311 | u32 burst_size; |
4a34ec8e SL |
312 | int ret; |
313 | ||
314 | infmt = &priv->format_mbus[CSI_SINK_PAD]; | |
315 | sensor_ep = &priv->sensor->sensor_ep; | |
316 | ||
317 | ipu_cpmem_zero(priv->idmac_ch); | |
318 | ||
319 | memset(&image, 0, sizeof(image)); | |
320 | image.pix = vdev->fmt.fmt.pix; | |
321 | image.rect.width = image.pix.width; | |
322 | image.rect.height = image.pix.height; | |
323 | ||
324 | csi_idmac_setup_vb2_buf(priv, phys); | |
325 | ||
326 | image.phys0 = phys[0]; | |
327 | image.phys1 = phys[1]; | |
328 | ||
4a34ec8e | 329 | /* |
c8da8c03 RK |
330 | * Check for conditions that require the IPU to handle the |
331 | * data internally as generic data, aka passthrough mode: | |
332 | * - raw bayer formats | |
333 | * - the sensor bus is 16-bit parallel | |
4a34ec8e | 334 | */ |
c8da8c03 RK |
335 | switch (image.pix.pixelformat) { |
336 | case V4L2_PIX_FMT_SBGGR8: | |
337 | case V4L2_PIX_FMT_SGBRG8: | |
338 | case V4L2_PIX_FMT_SGRBG8: | |
339 | case V4L2_PIX_FMT_SRGGB8: | |
37ea9830 | 340 | burst_size = 16; |
c8da8c03 RK |
341 | passthrough = true; |
342 | passthrough_bits = 8; | |
343 | break; | |
344 | case V4L2_PIX_FMT_SBGGR16: | |
345 | case V4L2_PIX_FMT_SGBRG16: | |
346 | case V4L2_PIX_FMT_SGRBG16: | |
347 | case V4L2_PIX_FMT_SRGGB16: | |
348 | burst_size = 4; | |
349 | passthrough = true; | |
350 | passthrough_bits = 16; | |
351 | break; | |
6f303592 PZ |
352 | case V4L2_PIX_FMT_YUV420: |
353 | case V4L2_PIX_FMT_NV12: | |
354 | burst_size = (image.pix.width & 0x3f) ? | |
355 | ((image.pix.width & 0x1f) ? | |
356 | ((image.pix.width & 0xf) ? 8 : 16) : 32) : 64; | |
357 | passthrough = (sensor_ep->bus_type != V4L2_MBUS_CSI2 && | |
358 | sensor_ep->bus.parallel.bus_width >= 16); | |
359 | passthrough_bits = 16; | |
14330d7f PZ |
360 | /* Skip writing U and V components to odd rows */ |
361 | ipu_cpmem_skip_odd_chroma_rows(priv->idmac_ch); | |
6f303592 PZ |
362 | break; |
363 | case V4L2_PIX_FMT_YUYV: | |
364 | case V4L2_PIX_FMT_UYVY: | |
365 | burst_size = (image.pix.width & 0x1f) ? | |
366 | ((image.pix.width & 0xf) ? 8 : 16) : 32; | |
367 | passthrough = (sensor_ep->bus_type != V4L2_MBUS_CSI2 && | |
368 | sensor_ep->bus.parallel.bus_width >= 16); | |
369 | passthrough_bits = 16; | |
370 | break; | |
c8da8c03 RK |
371 | default: |
372 | burst_size = (image.pix.width & 0xf) ? 8 : 16; | |
373 | passthrough = (sensor_ep->bus_type != V4L2_MBUS_CSI2 && | |
374 | sensor_ep->bus.parallel.bus_width >= 16); | |
375 | passthrough_bits = 16; | |
376 | break; | |
377 | } | |
4a34ec8e | 378 | |
c8da8c03 RK |
379 | if (passthrough) { |
380 | ipu_cpmem_set_resolution(priv->idmac_ch, image.rect.width, | |
381 | image.rect.height); | |
382 | ipu_cpmem_set_stride(priv->idmac_ch, image.pix.bytesperline); | |
383 | ipu_cpmem_set_buffer(priv->idmac_ch, 0, image.phys0); | |
384 | ipu_cpmem_set_buffer(priv->idmac_ch, 1, image.phys1); | |
385 | ipu_cpmem_set_format_passthrough(priv->idmac_ch, | |
386 | passthrough_bits); | |
387 | } else { | |
388 | ret = ipu_cpmem_set_image(priv->idmac_ch, &image); | |
389 | if (ret) | |
390 | goto unsetup_vb2; | |
391 | } | |
392 | ||
393 | ipu_cpmem_set_burstsize(priv->idmac_ch, burst_size); | |
4a34ec8e SL |
394 | |
395 | /* | |
396 | * Set the channel for the direct CSI-->memory via SMFC | |
397 | * use-case to very high priority, by enabling the watermark | |
398 | * signal in the SMFC, enabling WM in the channel, and setting | |
399 | * the channel priority to high. | |
400 | * | |
401 | * Refer to the i.mx6 rev. D TRM Table 36-8: Calculated priority | |
402 | * value. | |
403 | * | |
404 | * The WM's are set very low by intention here to ensure that | |
405 | * the SMFC FIFOs do not overflow. | |
406 | */ | |
407 | ipu_smfc_set_watermark(priv->smfc, 0x02, 0x01); | |
408 | ipu_cpmem_set_high_priority(priv->idmac_ch); | |
409 | ipu_idmac_enable_watermark(priv->idmac_ch, true); | |
410 | ipu_cpmem_set_axi_id(priv->idmac_ch, 0); | |
411 | ||
412 | burst_size = passthrough ? | |
413 | (burst_size >> 3) - 1 : (burst_size >> 2) - 1; | |
414 | ||
415 | ipu_smfc_set_burstsize(priv->smfc, burst_size); | |
416 | ||
417 | if (image.pix.field == V4L2_FIELD_NONE && | |
418 | V4L2_FIELD_HAS_BOTH(infmt->field)) | |
419 | ipu_cpmem_interlaced_scan(priv->idmac_ch, | |
420 | image.pix.bytesperline); | |
421 | ||
422 | ipu_idmac_set_double_buffer(priv->idmac_ch, true); | |
423 | ||
424 | return 0; | |
425 | ||
426 | unsetup_vb2: | |
427 | csi_idmac_unsetup_vb2_buf(priv, VB2_BUF_STATE_QUEUED); | |
428 | return ret; | |
429 | } | |
430 | ||
431 | static void csi_idmac_unsetup(struct csi_priv *priv, | |
432 | enum vb2_buffer_state state) | |
433 | { | |
434 | ipu_idmac_disable_channel(priv->idmac_ch); | |
435 | ipu_smfc_disable(priv->smfc); | |
436 | ||
437 | csi_idmac_unsetup_vb2_buf(priv, state); | |
438 | } | |
439 | ||
440 | static int csi_idmac_setup(struct csi_priv *priv) | |
441 | { | |
442 | int ret; | |
443 | ||
444 | ret = csi_idmac_setup_channel(priv); | |
445 | if (ret) | |
446 | return ret; | |
447 | ||
448 | ipu_cpmem_dump(priv->idmac_ch); | |
449 | ipu_dump(priv->ipu); | |
450 | ||
451 | ipu_smfc_enable(priv->smfc); | |
452 | ||
453 | /* set buffers ready */ | |
454 | ipu_idmac_select_buffer(priv->idmac_ch, 0); | |
455 | ipu_idmac_select_buffer(priv->idmac_ch, 1); | |
456 | ||
457 | /* enable the channels */ | |
458 | ipu_idmac_enable_channel(priv->idmac_ch); | |
459 | ||
460 | return 0; | |
461 | } | |
462 | ||
463 | static int csi_idmac_start(struct csi_priv *priv) | |
464 | { | |
465 | struct imx_media_video_dev *vdev = priv->vdev; | |
466 | struct v4l2_pix_format *outfmt; | |
467 | int ret; | |
468 | ||
469 | ret = csi_idmac_get_ipu_resources(priv); | |
470 | if (ret) | |
471 | return ret; | |
472 | ||
473 | ipu_smfc_map_channel(priv->smfc, priv->csi_id, priv->vc_num); | |
474 | ||
475 | outfmt = &vdev->fmt.fmt.pix; | |
476 | ||
477 | ret = imx_media_alloc_dma_buf(priv->md, &priv->underrun_buf, | |
478 | outfmt->sizeimage); | |
479 | if (ret) | |
480 | goto out_put_ipu; | |
481 | ||
482 | priv->ipu_buf_num = 0; | |
483 | ||
484 | /* init EOF completion waitq */ | |
485 | init_completion(&priv->last_eof_comp); | |
486 | priv->last_eof = false; | |
487 | priv->nfb4eof = false; | |
488 | ||
489 | ret = csi_idmac_setup(priv); | |
490 | if (ret) { | |
491 | v4l2_err(&priv->sd, "csi_idmac_setup failed: %d\n", ret); | |
492 | goto out_free_dma_buf; | |
493 | } | |
494 | ||
495 | priv->nfb4eof_irq = ipu_idmac_channel_irq(priv->ipu, | |
496 | priv->idmac_ch, | |
497 | IPU_IRQ_NFB4EOF); | |
498 | ret = devm_request_irq(priv->dev, priv->nfb4eof_irq, | |
499 | csi_idmac_nfb4eof_interrupt, 0, | |
500 | "imx-smfc-nfb4eof", priv); | |
501 | if (ret) { | |
502 | v4l2_err(&priv->sd, | |
503 | "Error registering NFB4EOF irq: %d\n", ret); | |
504 | goto out_unsetup; | |
505 | } | |
506 | ||
507 | priv->eof_irq = ipu_idmac_channel_irq(priv->ipu, priv->idmac_ch, | |
508 | IPU_IRQ_EOF); | |
509 | ||
510 | ret = devm_request_irq(priv->dev, priv->eof_irq, | |
511 | csi_idmac_eof_interrupt, 0, | |
512 | "imx-smfc-eof", priv); | |
513 | if (ret) { | |
514 | v4l2_err(&priv->sd, | |
515 | "Error registering eof irq: %d\n", ret); | |
516 | goto out_free_nfb4eof_irq; | |
517 | } | |
518 | ||
519 | /* start the EOF timeout timer */ | |
520 | mod_timer(&priv->eof_timeout_timer, | |
521 | jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT)); | |
522 | ||
523 | return 0; | |
524 | ||
525 | out_free_nfb4eof_irq: | |
526 | devm_free_irq(priv->dev, priv->nfb4eof_irq, priv); | |
527 | out_unsetup: | |
528 | csi_idmac_unsetup(priv, VB2_BUF_STATE_QUEUED); | |
529 | out_free_dma_buf: | |
530 | imx_media_free_dma_buf(priv->md, &priv->underrun_buf); | |
531 | out_put_ipu: | |
532 | csi_idmac_put_ipu_resources(priv); | |
533 | return ret; | |
534 | } | |
535 | ||
536 | static void csi_idmac_stop(struct csi_priv *priv) | |
537 | { | |
538 | unsigned long flags; | |
539 | int ret; | |
540 | ||
541 | /* mark next EOF interrupt as the last before stream off */ | |
542 | spin_lock_irqsave(&priv->irqlock, flags); | |
543 | priv->last_eof = true; | |
544 | spin_unlock_irqrestore(&priv->irqlock, flags); | |
545 | ||
546 | /* | |
547 | * and then wait for interrupt handler to mark completion. | |
548 | */ | |
549 | ret = wait_for_completion_timeout( | |
550 | &priv->last_eof_comp, msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT)); | |
551 | if (ret == 0) | |
552 | v4l2_warn(&priv->sd, "wait last EOF timeout\n"); | |
553 | ||
554 | devm_free_irq(priv->dev, priv->eof_irq, priv); | |
555 | devm_free_irq(priv->dev, priv->nfb4eof_irq, priv); | |
556 | ||
557 | csi_idmac_unsetup(priv, VB2_BUF_STATE_ERROR); | |
558 | ||
559 | imx_media_free_dma_buf(priv->md, &priv->underrun_buf); | |
560 | ||
561 | /* cancel the EOF timeout timer */ | |
562 | del_timer_sync(&priv->eof_timeout_timer); | |
563 | ||
564 | csi_idmac_put_ipu_resources(priv); | |
565 | } | |
566 | ||
567 | /* Update the CSI whole sensor and active windows */ | |
568 | static int csi_setup(struct csi_priv *priv) | |
569 | { | |
570 | struct v4l2_mbus_framefmt *infmt, *outfmt; | |
571 | struct v4l2_mbus_config sensor_mbus_cfg; | |
572 | struct v4l2_fwnode_endpoint *sensor_ep; | |
573 | struct v4l2_mbus_framefmt if_fmt; | |
574 | ||
575 | infmt = &priv->format_mbus[CSI_SINK_PAD]; | |
576 | outfmt = &priv->format_mbus[priv->active_output_pad]; | |
577 | sensor_ep = &priv->sensor->sensor_ep; | |
578 | ||
579 | /* compose mbus_config from sensor endpoint */ | |
580 | sensor_mbus_cfg.type = sensor_ep->bus_type; | |
581 | sensor_mbus_cfg.flags = (sensor_ep->bus_type == V4L2_MBUS_CSI2) ? | |
582 | sensor_ep->bus.mipi_csi2.flags : | |
583 | sensor_ep->bus.parallel.flags; | |
584 | ||
585 | /* | |
586 | * we need to pass input sensor frame to CSI interface, but | |
587 | * with translated field type from output format | |
588 | */ | |
589 | if_fmt = *infmt; | |
590 | if_fmt.field = outfmt->field; | |
591 | ||
592 | ipu_csi_set_window(priv->csi, &priv->crop); | |
593 | ||
594 | ipu_csi_set_downsize(priv->csi, | |
69e78611 PZ |
595 | priv->crop.width == 2 * priv->compose.width, |
596 | priv->crop.height == 2 * priv->compose.height); | |
4a34ec8e SL |
597 | |
598 | ipu_csi_init_interface(priv->csi, &sensor_mbus_cfg, &if_fmt); | |
599 | ||
600 | ipu_csi_set_dest(priv->csi, priv->dest); | |
601 | ||
fb30ee79 PZ |
602 | if (priv->dest == IPU_CSI_DEST_IDMAC) |
603 | ipu_csi_set_skip_smfc(priv->csi, priv->skip->skip_smfc, | |
604 | priv->skip->max_ratio - 1, 0); | |
605 | ||
4a34ec8e SL |
606 | ipu_csi_dump(priv->csi); |
607 | ||
608 | return 0; | |
609 | } | |
610 | ||
611 | static int csi_start(struct csi_priv *priv) | |
612 | { | |
fb30ee79 | 613 | struct v4l2_fract *output_fi, *input_fi; |
4a34ec8e SL |
614 | u32 bad_frames = 0; |
615 | int ret; | |
616 | ||
617 | if (!priv->sensor) { | |
618 | v4l2_err(&priv->sd, "no sensor attached\n"); | |
619 | return -EINVAL; | |
620 | } | |
621 | ||
fb30ee79 PZ |
622 | output_fi = &priv->frame_interval[priv->active_output_pad]; |
623 | input_fi = &priv->frame_interval[CSI_SINK_PAD]; | |
624 | ||
4a34ec8e SL |
625 | ret = v4l2_subdev_call(priv->sensor->sd, sensor, |
626 | g_skip_frames, &bad_frames); | |
627 | if (!ret && bad_frames) { | |
4a34ec8e SL |
628 | u32 delay_usec; |
629 | ||
630 | /* | |
631 | * This sensor has bad frames when it is turned on, | |
632 | * add a delay to avoid them before enabling the CSI | |
633 | * hardware. Especially for sensors with a bt.656 interface, | |
634 | * any shifts in the SAV/EAV sync codes will cause the CSI | |
635 | * to lose vert/horiz sync. | |
636 | */ | |
637 | delay_usec = DIV_ROUND_UP_ULL( | |
fb30ee79 PZ |
638 | (u64)USEC_PER_SEC * input_fi->numerator * bad_frames, |
639 | input_fi->denominator); | |
4a34ec8e SL |
640 | usleep_range(delay_usec, delay_usec + 1000); |
641 | } | |
642 | ||
643 | if (priv->dest == IPU_CSI_DEST_IDMAC) { | |
644 | ret = csi_idmac_start(priv); | |
645 | if (ret) | |
646 | return ret; | |
647 | } | |
648 | ||
649 | ret = csi_setup(priv); | |
650 | if (ret) | |
651 | goto idmac_stop; | |
652 | ||
653 | /* start the frame interval monitor */ | |
654 | if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC) { | |
fb30ee79 | 655 | ret = imx_media_fim_set_stream(priv->fim, output_fi, true); |
4a34ec8e SL |
656 | if (ret) |
657 | goto idmac_stop; | |
658 | } | |
659 | ||
660 | ret = ipu_csi_enable(priv->csi); | |
661 | if (ret) { | |
662 | v4l2_err(&priv->sd, "CSI enable error: %d\n", ret); | |
663 | goto fim_off; | |
664 | } | |
665 | ||
666 | return 0; | |
667 | ||
668 | fim_off: | |
669 | if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC) | |
fb30ee79 | 670 | imx_media_fim_set_stream(priv->fim, NULL, false); |
4a34ec8e SL |
671 | idmac_stop: |
672 | if (priv->dest == IPU_CSI_DEST_IDMAC) | |
673 | csi_idmac_stop(priv); | |
674 | return ret; | |
675 | } | |
676 | ||
677 | static void csi_stop(struct csi_priv *priv) | |
678 | { | |
679 | if (priv->dest == IPU_CSI_DEST_IDMAC) { | |
680 | csi_idmac_stop(priv); | |
681 | ||
682 | /* stop the frame interval monitor */ | |
683 | if (priv->fim) | |
fb30ee79 | 684 | imx_media_fim_set_stream(priv->fim, NULL, false); |
4a34ec8e SL |
685 | } |
686 | ||
687 | ipu_csi_disable(priv->csi); | |
688 | } | |
689 | ||
fb30ee79 PZ |
690 | static const struct csi_skip_desc csi_skip[12] = { |
691 | { 1, 1, 0x00 }, /* Keep all frames */ | |
692 | { 5, 6, 0x10 }, /* Skip every sixth frame */ | |
693 | { 4, 5, 0x08 }, /* Skip every fifth frame */ | |
694 | { 3, 4, 0x04 }, /* Skip every fourth frame */ | |
695 | { 2, 3, 0x02 }, /* Skip every third frame */ | |
696 | { 3, 5, 0x0a }, /* Skip frames 1 and 3 of every 5 */ | |
697 | { 1, 2, 0x01 }, /* Skip every second frame */ | |
698 | { 2, 5, 0x0b }, /* Keep frames 1 and 4 of every 5 */ | |
699 | { 1, 3, 0x03 }, /* Keep one in three frames */ | |
700 | { 1, 4, 0x07 }, /* Keep one in four frames */ | |
701 | { 1, 5, 0x0f }, /* Keep one in five frames */ | |
702 | { 1, 6, 0x1f }, /* Keep one in six frames */ | |
703 | }; | |
704 | ||
705 | static void csi_apply_skip_interval(const struct csi_skip_desc *skip, | |
706 | struct v4l2_fract *interval) | |
707 | { | |
708 | unsigned int div; | |
709 | ||
710 | interval->numerator *= skip->max_ratio; | |
711 | interval->denominator *= skip->keep; | |
712 | ||
713 | /* Reduce fraction to lowest terms */ | |
714 | div = gcd(interval->numerator, interval->denominator); | |
715 | if (div > 1) { | |
716 | interval->numerator /= div; | |
717 | interval->denominator /= div; | |
718 | } | |
719 | } | |
720 | ||
721 | /* | |
722 | * Find the skip pattern to produce the output frame interval closest to the | |
723 | * requested one, for the given input frame interval. Updates the output frame | |
724 | * interval to the exact value. | |
725 | */ | |
726 | static const struct csi_skip_desc *csi_find_best_skip(struct v4l2_fract *in, | |
727 | struct v4l2_fract *out) | |
728 | { | |
729 | const struct csi_skip_desc *skip = &csi_skip[0], *best_skip = skip; | |
730 | u32 min_err = UINT_MAX; | |
731 | u64 want_us; | |
732 | int i; | |
733 | ||
734 | /* Default to 1:1 ratio */ | |
735 | if (out->numerator == 0 || out->denominator == 0 || | |
736 | in->numerator == 0 || in->denominator == 0) { | |
737 | *out = *in; | |
738 | return best_skip; | |
739 | } | |
740 | ||
741 | want_us = div_u64((u64)USEC_PER_SEC * out->numerator, out->denominator); | |
742 | ||
743 | /* Find the reduction closest to the requested time per frame */ | |
744 | for (i = 0; i < ARRAY_SIZE(csi_skip); i++, skip++) { | |
745 | u64 tmp, err; | |
746 | ||
747 | tmp = div_u64((u64)USEC_PER_SEC * in->numerator * | |
748 | skip->max_ratio, in->denominator * skip->keep); | |
749 | ||
750 | err = abs((s64)tmp - want_us); | |
751 | if (err < min_err) { | |
752 | min_err = err; | |
753 | best_skip = skip; | |
754 | } | |
755 | } | |
756 | ||
757 | *out = *in; | |
758 | csi_apply_skip_interval(best_skip, out); | |
759 | ||
760 | return best_skip; | |
761 | } | |
762 | ||
4a34ec8e SL |
763 | /* |
764 | * V4L2 subdev operations. | |
765 | */ | |
766 | ||
767 | static int csi_g_frame_interval(struct v4l2_subdev *sd, | |
768 | struct v4l2_subdev_frame_interval *fi) | |
769 | { | |
770 | struct csi_priv *priv = v4l2_get_subdevdata(sd); | |
771 | ||
fb30ee79 PZ |
772 | if (fi->pad >= CSI_NUM_PADS) |
773 | return -EINVAL; | |
774 | ||
4a34ec8e | 775 | mutex_lock(&priv->lock); |
fb30ee79 PZ |
776 | |
777 | fi->interval = priv->frame_interval[fi->pad]; | |
778 | ||
4a34ec8e SL |
779 | mutex_unlock(&priv->lock); |
780 | ||
781 | return 0; | |
782 | } | |
783 | ||
784 | static int csi_s_frame_interval(struct v4l2_subdev *sd, | |
785 | struct v4l2_subdev_frame_interval *fi) | |
786 | { | |
787 | struct csi_priv *priv = v4l2_get_subdevdata(sd); | |
fb30ee79 PZ |
788 | struct v4l2_fract *input_fi; |
789 | int ret = 0; | |
4a34ec8e SL |
790 | |
791 | mutex_lock(&priv->lock); | |
792 | ||
fb30ee79 | 793 | input_fi = &priv->frame_interval[CSI_SINK_PAD]; |
4a34ec8e | 794 | |
fb30ee79 PZ |
795 | switch (fi->pad) { |
796 | case CSI_SINK_PAD: | |
797 | /* No limits on input frame interval */ | |
798 | /* Reset output intervals and frame skipping ratio to 1:1 */ | |
799 | priv->frame_interval[CSI_SRC_PAD_IDMAC] = fi->interval; | |
800 | priv->frame_interval[CSI_SRC_PAD_DIRECT] = fi->interval; | |
801 | priv->skip = &csi_skip[0]; | |
802 | break; | |
803 | case CSI_SRC_PAD_IDMAC: | |
804 | /* | |
805 | * frame interval at IDMAC output pad depends on input | |
806 | * interval, modified by frame skipping. | |
807 | */ | |
808 | priv->skip = csi_find_best_skip(input_fi, &fi->interval); | |
809 | break; | |
810 | case CSI_SRC_PAD_DIRECT: | |
811 | /* | |
812 | * frame interval at DIRECT output pad is same as input | |
813 | * interval. | |
814 | */ | |
815 | fi->interval = *input_fi; | |
816 | break; | |
817 | default: | |
818 | ret = -EINVAL; | |
819 | goto out; | |
820 | } | |
4a34ec8e | 821 | |
fb30ee79 PZ |
822 | priv->frame_interval[fi->pad] = fi->interval; |
823 | out: | |
4a34ec8e | 824 | mutex_unlock(&priv->lock); |
fb30ee79 | 825 | return ret; |
4a34ec8e SL |
826 | } |
827 | ||
828 | static int csi_s_stream(struct v4l2_subdev *sd, int enable) | |
829 | { | |
830 | struct csi_priv *priv = v4l2_get_subdevdata(sd); | |
831 | int ret = 0; | |
832 | ||
833 | mutex_lock(&priv->lock); | |
834 | ||
835 | if (!priv->src_sd || !priv->sink) { | |
836 | ret = -EPIPE; | |
837 | goto out; | |
838 | } | |
839 | ||
840 | /* | |
841 | * enable/disable streaming only if stream_count is | |
842 | * going from 0 to 1 / 1 to 0. | |
843 | */ | |
844 | if (priv->stream_count != !enable) | |
845 | goto update_count; | |
846 | ||
847 | if (enable) { | |
848 | /* upstream must be started first, before starting CSI */ | |
849 | ret = v4l2_subdev_call(priv->src_sd, video, s_stream, 1); | |
850 | ret = (ret && ret != -ENOIOCTLCMD) ? ret : 0; | |
851 | if (ret) | |
852 | goto out; | |
853 | ||
854 | dev_dbg(priv->dev, "stream ON\n"); | |
855 | ret = csi_start(priv); | |
856 | if (ret) { | |
857 | v4l2_subdev_call(priv->src_sd, video, s_stream, 0); | |
858 | goto out; | |
859 | } | |
860 | } else { | |
861 | dev_dbg(priv->dev, "stream OFF\n"); | |
862 | /* CSI must be stopped first, then stop upstream */ | |
863 | csi_stop(priv); | |
864 | v4l2_subdev_call(priv->src_sd, video, s_stream, 0); | |
865 | } | |
866 | ||
867 | update_count: | |
868 | priv->stream_count += enable ? 1 : -1; | |
de2e0456 MV |
869 | if (priv->stream_count < 0) |
870 | priv->stream_count = 0; | |
4a34ec8e SL |
871 | out: |
872 | mutex_unlock(&priv->lock); | |
873 | return ret; | |
874 | } | |
875 | ||
876 | static int csi_link_setup(struct media_entity *entity, | |
877 | const struct media_pad *local, | |
878 | const struct media_pad *remote, u32 flags) | |
879 | { | |
880 | struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity); | |
881 | struct csi_priv *priv = v4l2_get_subdevdata(sd); | |
882 | struct v4l2_subdev *remote_sd; | |
883 | int ret = 0; | |
884 | ||
885 | dev_dbg(priv->dev, "link setup %s -> %s\n", remote->entity->name, | |
886 | local->entity->name); | |
887 | ||
888 | mutex_lock(&priv->lock); | |
889 | ||
890 | if (local->flags & MEDIA_PAD_FL_SINK) { | |
891 | if (!is_media_entity_v4l2_subdev(remote->entity)) { | |
892 | ret = -EINVAL; | |
893 | goto out; | |
894 | } | |
895 | ||
896 | remote_sd = media_entity_to_v4l2_subdev(remote->entity); | |
897 | ||
898 | if (flags & MEDIA_LNK_FL_ENABLED) { | |
899 | if (priv->src_sd) { | |
900 | ret = -EBUSY; | |
901 | goto out; | |
902 | } | |
903 | priv->src_sd = remote_sd; | |
904 | } else { | |
905 | priv->src_sd = NULL; | |
906 | } | |
907 | ||
908 | goto out; | |
909 | } | |
910 | ||
911 | /* this is a source pad */ | |
912 | ||
913 | if (flags & MEDIA_LNK_FL_ENABLED) { | |
914 | if (priv->sink) { | |
915 | ret = -EBUSY; | |
916 | goto out; | |
917 | } | |
918 | } else { | |
919 | v4l2_ctrl_handler_free(&priv->ctrl_hdlr); | |
920 | v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0); | |
921 | priv->sink = NULL; | |
922 | goto out; | |
923 | } | |
924 | ||
925 | /* record which output pad is now active */ | |
926 | priv->active_output_pad = local->index; | |
927 | ||
928 | /* set CSI destination */ | |
929 | if (local->index == CSI_SRC_PAD_IDMAC) { | |
930 | if (!is_media_entity_v4l2_video_device(remote->entity)) { | |
931 | ret = -EINVAL; | |
932 | goto out; | |
933 | } | |
934 | ||
935 | if (priv->fim) { | |
936 | ret = imx_media_fim_add_controls(priv->fim); | |
937 | if (ret) | |
938 | goto out; | |
939 | } | |
940 | ||
941 | priv->dest = IPU_CSI_DEST_IDMAC; | |
942 | } else { | |
943 | if (!is_media_entity_v4l2_subdev(remote->entity)) { | |
944 | ret = -EINVAL; | |
945 | goto out; | |
946 | } | |
947 | ||
948 | remote_sd = media_entity_to_v4l2_subdev(remote->entity); | |
949 | switch (remote_sd->grp_id) { | |
950 | case IMX_MEDIA_GRP_ID_VDIC: | |
951 | priv->dest = IPU_CSI_DEST_VDIC; | |
952 | break; | |
953 | case IMX_MEDIA_GRP_ID_IC_PRP: | |
954 | priv->dest = IPU_CSI_DEST_IC; | |
955 | break; | |
956 | default: | |
957 | ret = -EINVAL; | |
958 | goto out; | |
959 | } | |
960 | } | |
961 | ||
962 | priv->sink = remote->entity; | |
963 | out: | |
964 | mutex_unlock(&priv->lock); | |
965 | return ret; | |
966 | } | |
967 | ||
968 | static int csi_link_validate(struct v4l2_subdev *sd, | |
969 | struct media_link *link, | |
970 | struct v4l2_subdev_format *source_fmt, | |
971 | struct v4l2_subdev_format *sink_fmt) | |
972 | { | |
973 | struct csi_priv *priv = v4l2_get_subdevdata(sd); | |
974 | struct v4l2_fwnode_endpoint *sensor_ep; | |
c8da8c03 | 975 | const struct imx_media_pixfmt *incc; |
4a34ec8e SL |
976 | struct imx_media_subdev *sensor; |
977 | bool is_csi2; | |
978 | int ret; | |
979 | ||
980 | ret = v4l2_subdev_link_validate_default(sd, link, | |
981 | source_fmt, sink_fmt); | |
982 | if (ret) | |
983 | return ret; | |
984 | ||
985 | sensor = __imx_media_find_sensor(priv->md, &priv->sd.entity); | |
986 | if (IS_ERR(sensor)) { | |
987 | v4l2_err(&priv->sd, "no sensor attached\n"); | |
13109fbc | 988 | return PTR_ERR(sensor); |
4a34ec8e SL |
989 | } |
990 | ||
991 | mutex_lock(&priv->lock); | |
992 | ||
993 | priv->sensor = sensor; | |
994 | sensor_ep = &priv->sensor->sensor_ep; | |
995 | is_csi2 = (sensor_ep->bus_type == V4L2_MBUS_CSI2); | |
c8da8c03 RK |
996 | incc = priv->cc[CSI_SINK_PAD]; |
997 | ||
998 | if (priv->dest != IPU_CSI_DEST_IDMAC && | |
999 | (incc->bayer || (!is_csi2 && | |
1000 | sensor_ep->bus.parallel.bus_width >= 16))) { | |
1001 | v4l2_err(&priv->sd, | |
1002 | "bayer/16-bit parallel buses must go to IDMAC pad\n"); | |
1003 | ret = -EINVAL; | |
1004 | goto out; | |
1005 | } | |
4a34ec8e SL |
1006 | |
1007 | if (is_csi2) { | |
1008 | int vc_num = 0; | |
1009 | /* | |
1010 | * NOTE! It seems the virtual channels from the mipi csi-2 | |
1011 | * receiver are used only for routing by the video mux's, | |
1012 | * or for hard-wired routing to the CSI's. Once the stream | |
1013 | * enters the CSI's however, they are treated internally | |
1014 | * in the IPU as virtual channel 0. | |
1015 | */ | |
1016 | #if 0 | |
1017 | mutex_unlock(&priv->lock); | |
1018 | vc_num = imx_media_find_mipi_csi2_channel(priv->md, | |
1019 | &priv->sd.entity); | |
1020 | if (vc_num < 0) | |
1021 | return vc_num; | |
1022 | mutex_lock(&priv->lock); | |
1023 | #endif | |
1024 | ipu_csi_set_mipi_datatype(priv->csi, vc_num, | |
1025 | &priv->format_mbus[CSI_SINK_PAD]); | |
1026 | } | |
1027 | ||
1028 | /* select either parallel or MIPI-CSI2 as input to CSI */ | |
1029 | ipu_set_csi_src_mux(priv->ipu, priv->csi_id, is_csi2); | |
c8da8c03 | 1030 | out: |
4a34ec8e SL |
1031 | mutex_unlock(&priv->lock); |
1032 | return ret; | |
1033 | } | |
1034 | ||
1035 | static struct v4l2_mbus_framefmt * | |
1036 | __csi_get_fmt(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg, | |
1037 | unsigned int pad, enum v4l2_subdev_format_whence which) | |
1038 | { | |
1039 | if (which == V4L2_SUBDEV_FORMAT_TRY) | |
1040 | return v4l2_subdev_get_try_format(&priv->sd, cfg, pad); | |
1041 | else | |
1042 | return &priv->format_mbus[pad]; | |
1043 | } | |
1044 | ||
1045 | static struct v4l2_rect * | |
1046 | __csi_get_crop(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg, | |
1047 | enum v4l2_subdev_format_whence which) | |
1048 | { | |
1049 | if (which == V4L2_SUBDEV_FORMAT_TRY) | |
1050 | return v4l2_subdev_get_try_crop(&priv->sd, cfg, CSI_SINK_PAD); | |
1051 | else | |
1052 | return &priv->crop; | |
1053 | } | |
1054 | ||
69e78611 PZ |
1055 | static struct v4l2_rect * |
1056 | __csi_get_compose(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg, | |
1057 | enum v4l2_subdev_format_whence which) | |
1058 | { | |
1059 | if (which == V4L2_SUBDEV_FORMAT_TRY) | |
1060 | return v4l2_subdev_get_try_compose(&priv->sd, cfg, | |
1061 | CSI_SINK_PAD); | |
1062 | else | |
1063 | return &priv->compose; | |
1064 | } | |
1065 | ||
4a34ec8e SL |
1066 | static void csi_try_crop(struct csi_priv *priv, |
1067 | struct v4l2_rect *crop, | |
1068 | struct v4l2_subdev_pad_config *cfg, | |
1069 | struct v4l2_mbus_framefmt *infmt, | |
1070 | struct imx_media_subdev *sensor) | |
1071 | { | |
1072 | struct v4l2_fwnode_endpoint *sensor_ep; | |
1073 | ||
1074 | sensor_ep = &sensor->sensor_ep; | |
1075 | ||
1076 | crop->width = min_t(__u32, infmt->width, crop->width); | |
1077 | if (crop->left + crop->width > infmt->width) | |
1078 | crop->left = infmt->width - crop->width; | |
1079 | /* adjust crop left/width to h/w alignment restrictions */ | |
1080 | crop->left &= ~0x3; | |
1081 | crop->width &= ~0x7; | |
1082 | ||
1083 | /* | |
1084 | * FIXME: not sure why yet, but on interlaced bt.656, | |
1085 | * changing the vertical cropping causes loss of vertical | |
1086 | * sync, so fix it to NTSC/PAL active lines. NTSC contains | |
1087 | * 2 extra lines of active video that need to be cropped. | |
1088 | */ | |
1089 | if (sensor_ep->bus_type == V4L2_MBUS_BT656 && | |
1090 | (V4L2_FIELD_HAS_BOTH(infmt->field) || | |
1091 | infmt->field == V4L2_FIELD_ALTERNATE)) { | |
1092 | crop->height = infmt->height; | |
1093 | crop->top = (infmt->height == 480) ? 2 : 0; | |
1094 | } else { | |
1095 | crop->height = min_t(__u32, infmt->height, crop->height); | |
1096 | if (crop->top + crop->height > infmt->height) | |
1097 | crop->top = infmt->height - crop->height; | |
1098 | } | |
1099 | } | |
1100 | ||
1101 | static int csi_enum_mbus_code(struct v4l2_subdev *sd, | |
1102 | struct v4l2_subdev_pad_config *cfg, | |
1103 | struct v4l2_subdev_mbus_code_enum *code) | |
1104 | { | |
1105 | struct csi_priv *priv = v4l2_get_subdevdata(sd); | |
1106 | const struct imx_media_pixfmt *incc; | |
1107 | struct v4l2_mbus_framefmt *infmt; | |
1108 | int ret = 0; | |
1109 | ||
1110 | mutex_lock(&priv->lock); | |
1111 | ||
1112 | infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, code->which); | |
1113 | incc = imx_media_find_mbus_format(infmt->code, CS_SEL_ANY, true); | |
1114 | ||
1115 | switch (code->pad) { | |
1116 | case CSI_SINK_PAD: | |
1117 | ret = imx_media_enum_mbus_format(&code->code, code->index, | |
1118 | CS_SEL_ANY, true); | |
1119 | break; | |
1120 | case CSI_SRC_PAD_DIRECT: | |
1121 | case CSI_SRC_PAD_IDMAC: | |
1122 | if (incc->bayer) { | |
1123 | if (code->index != 0) { | |
1124 | ret = -EINVAL; | |
1125 | goto out; | |
1126 | } | |
1127 | code->code = infmt->code; | |
1128 | } else { | |
1129 | u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ? | |
1130 | CS_SEL_YUV : CS_SEL_RGB; | |
1131 | ret = imx_media_enum_ipu_format(&code->code, | |
1132 | code->index, | |
1133 | cs_sel); | |
1134 | } | |
1135 | break; | |
1136 | default: | |
1137 | ret = -EINVAL; | |
1138 | } | |
1139 | ||
1140 | out: | |
1141 | mutex_unlock(&priv->lock); | |
1142 | return ret; | |
1143 | } | |
1144 | ||
949ffdbf RK |
1145 | static int csi_enum_frame_size(struct v4l2_subdev *sd, |
1146 | struct v4l2_subdev_pad_config *cfg, | |
1147 | struct v4l2_subdev_frame_size_enum *fse) | |
1148 | { | |
1149 | struct csi_priv *priv = v4l2_get_subdevdata(sd); | |
1150 | struct v4l2_rect *crop; | |
1151 | int ret = 0; | |
1152 | ||
1153 | if (fse->pad >= CSI_NUM_PADS || | |
1154 | fse->index > (fse->pad == CSI_SINK_PAD ? 0 : 3)) | |
1155 | return -EINVAL; | |
1156 | ||
1157 | mutex_lock(&priv->lock); | |
1158 | ||
1159 | if (fse->pad == CSI_SINK_PAD) { | |
1160 | fse->min_width = MIN_W; | |
1161 | fse->max_width = MAX_W; | |
1162 | fse->min_height = MIN_H; | |
1163 | fse->max_height = MAX_H; | |
1164 | } else { | |
1165 | crop = __csi_get_crop(priv, cfg, fse->which); | |
1166 | ||
1167 | fse->min_width = fse->max_width = fse->index & 1 ? | |
1168 | crop->width / 2 : crop->width; | |
1169 | fse->min_height = fse->max_height = fse->index & 2 ? | |
1170 | crop->height / 2 : crop->height; | |
1171 | } | |
1172 | ||
1173 | mutex_unlock(&priv->lock); | |
1174 | return ret; | |
1175 | } | |
1176 | ||
1177 | static int csi_enum_frame_interval(struct v4l2_subdev *sd, | |
1178 | struct v4l2_subdev_pad_config *cfg, | |
1179 | struct v4l2_subdev_frame_interval_enum *fie) | |
1180 | { | |
1181 | struct csi_priv *priv = v4l2_get_subdevdata(sd); | |
1182 | struct v4l2_fract *input_fi; | |
1183 | struct v4l2_rect *crop; | |
1184 | int ret = 0; | |
1185 | ||
1186 | if (fie->pad >= CSI_NUM_PADS || | |
1187 | fie->index >= (fie->pad != CSI_SRC_PAD_IDMAC ? | |
1188 | 1 : ARRAY_SIZE(csi_skip))) | |
1189 | return -EINVAL; | |
1190 | ||
1191 | mutex_lock(&priv->lock); | |
1192 | ||
1193 | input_fi = &priv->frame_interval[CSI_SINK_PAD]; | |
1194 | crop = __csi_get_crop(priv, cfg, fie->which); | |
1195 | ||
1196 | if ((fie->width != crop->width && fie->width != crop->width / 2) || | |
1197 | (fie->height != crop->height && fie->height != crop->height / 2)) { | |
1198 | ret = -EINVAL; | |
1199 | goto out; | |
1200 | } | |
1201 | ||
1202 | fie->interval = *input_fi; | |
1203 | ||
1204 | if (fie->pad == CSI_SRC_PAD_IDMAC) | |
1205 | csi_apply_skip_interval(&csi_skip[fie->index], | |
1206 | &fie->interval); | |
1207 | ||
1208 | out: | |
1209 | mutex_unlock(&priv->lock); | |
1210 | return ret; | |
1211 | } | |
1212 | ||
4a34ec8e SL |
1213 | static int csi_get_fmt(struct v4l2_subdev *sd, |
1214 | struct v4l2_subdev_pad_config *cfg, | |
1215 | struct v4l2_subdev_format *sdformat) | |
1216 | { | |
1217 | struct csi_priv *priv = v4l2_get_subdevdata(sd); | |
1218 | struct v4l2_mbus_framefmt *fmt; | |
1219 | int ret = 0; | |
1220 | ||
1221 | if (sdformat->pad >= CSI_NUM_PADS) | |
1222 | return -EINVAL; | |
1223 | ||
1224 | mutex_lock(&priv->lock); | |
1225 | ||
1226 | fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which); | |
1227 | if (!fmt) { | |
1228 | ret = -EINVAL; | |
1229 | goto out; | |
1230 | } | |
1231 | ||
1232 | sdformat->format = *fmt; | |
1233 | out: | |
1234 | mutex_unlock(&priv->lock); | |
1235 | return ret; | |
1236 | } | |
1237 | ||
1238 | static void csi_try_fmt(struct csi_priv *priv, | |
1239 | struct imx_media_subdev *sensor, | |
1240 | struct v4l2_subdev_pad_config *cfg, | |
1241 | struct v4l2_subdev_format *sdformat, | |
1242 | struct v4l2_rect *crop, | |
69e78611 | 1243 | struct v4l2_rect *compose, |
4a34ec8e SL |
1244 | const struct imx_media_pixfmt **cc) |
1245 | { | |
1246 | const struct imx_media_pixfmt *incc; | |
1247 | struct v4l2_mbus_framefmt *infmt; | |
1248 | u32 code; | |
1249 | ||
21e54111 SL |
1250 | infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sdformat->which); |
1251 | ||
4a34ec8e SL |
1252 | switch (sdformat->pad) { |
1253 | case CSI_SRC_PAD_DIRECT: | |
1254 | case CSI_SRC_PAD_IDMAC: | |
4a34ec8e SL |
1255 | incc = imx_media_find_mbus_format(infmt->code, |
1256 | CS_SEL_ANY, true); | |
1257 | ||
69e78611 PZ |
1258 | sdformat->format.width = compose->width; |
1259 | sdformat->format.height = compose->height; | |
4a34ec8e SL |
1260 | |
1261 | if (incc->bayer) { | |
1262 | sdformat->format.code = infmt->code; | |
1263 | *cc = incc; | |
1264 | } else { | |
1265 | u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ? | |
1266 | CS_SEL_YUV : CS_SEL_RGB; | |
1267 | ||
1268 | *cc = imx_media_find_ipu_format(sdformat->format.code, | |
1269 | cs_sel); | |
1270 | if (!*cc) { | |
1271 | imx_media_enum_ipu_format(&code, 0, cs_sel); | |
1272 | *cc = imx_media_find_ipu_format(code, cs_sel); | |
1273 | sdformat->format.code = (*cc)->codes[0]; | |
1274 | } | |
1275 | } | |
1276 | ||
1277 | if (sdformat->pad == CSI_SRC_PAD_DIRECT || | |
1278 | sdformat->format.field != V4L2_FIELD_NONE) | |
1279 | sdformat->format.field = infmt->field; | |
1280 | ||
1281 | /* | |
1282 | * translate V4L2_FIELD_ALTERNATE to SEQ_TB or SEQ_BT | |
1283 | * depending on input height (assume NTSC top-bottom | |
1284 | * order if 480 lines, otherwise PAL bottom-top order). | |
1285 | */ | |
1286 | if (sdformat->format.field == V4L2_FIELD_ALTERNATE) { | |
1287 | sdformat->format.field = (infmt->height == 480) ? | |
1288 | V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT; | |
1289 | } | |
21e54111 SL |
1290 | |
1291 | /* propagate colorimetry from sink */ | |
1292 | sdformat->format.colorspace = infmt->colorspace; | |
1293 | sdformat->format.xfer_func = infmt->xfer_func; | |
1294 | sdformat->format.quantization = infmt->quantization; | |
1295 | sdformat->format.ycbcr_enc = infmt->ycbcr_enc; | |
4a34ec8e SL |
1296 | break; |
1297 | case CSI_SINK_PAD: | |
1298 | v4l_bound_align_image(&sdformat->format.width, MIN_W, MAX_W, | |
1299 | W_ALIGN, &sdformat->format.height, | |
1300 | MIN_H, MAX_H, H_ALIGN, S_ALIGN); | |
69e78611 PZ |
1301 | |
1302 | /* Reset crop and compose rectangles */ | |
4a34ec8e SL |
1303 | crop->left = 0; |
1304 | crop->top = 0; | |
1305 | crop->width = sdformat->format.width; | |
1306 | crop->height = sdformat->format.height; | |
1307 | csi_try_crop(priv, crop, cfg, &sdformat->format, sensor); | |
69e78611 PZ |
1308 | compose->left = 0; |
1309 | compose->top = 0; | |
1310 | compose->width = crop->width; | |
1311 | compose->height = crop->height; | |
4a34ec8e SL |
1312 | |
1313 | *cc = imx_media_find_mbus_format(sdformat->format.code, | |
1314 | CS_SEL_ANY, true); | |
1315 | if (!*cc) { | |
1316 | imx_media_enum_mbus_format(&code, 0, | |
1317 | CS_SEL_ANY, false); | |
1318 | *cc = imx_media_find_mbus_format(code, | |
1319 | CS_SEL_ANY, false); | |
1320 | sdformat->format.code = (*cc)->codes[0]; | |
1321 | } | |
21e54111 SL |
1322 | |
1323 | imx_media_fill_default_mbus_fields( | |
1324 | &sdformat->format, infmt, | |
1325 | priv->active_output_pad == CSI_SRC_PAD_DIRECT); | |
4a34ec8e SL |
1326 | break; |
1327 | } | |
1328 | } | |
1329 | ||
1330 | static int csi_set_fmt(struct v4l2_subdev *sd, | |
1331 | struct v4l2_subdev_pad_config *cfg, | |
1332 | struct v4l2_subdev_format *sdformat) | |
1333 | { | |
1334 | struct csi_priv *priv = v4l2_get_subdevdata(sd); | |
1335 | struct imx_media_video_dev *vdev = priv->vdev; | |
1336 | const struct imx_media_pixfmt *cc; | |
1337 | struct imx_media_subdev *sensor; | |
1338 | struct v4l2_pix_format vdev_fmt; | |
1339 | struct v4l2_mbus_framefmt *fmt; | |
69e78611 | 1340 | struct v4l2_rect *crop, *compose; |
4a34ec8e SL |
1341 | int ret = 0; |
1342 | ||
1343 | if (sdformat->pad >= CSI_NUM_PADS) | |
1344 | return -EINVAL; | |
1345 | ||
1346 | sensor = imx_media_find_sensor(priv->md, &priv->sd.entity); | |
1347 | if (IS_ERR(sensor)) { | |
1348 | v4l2_err(&priv->sd, "no sensor attached\n"); | |
1349 | return PTR_ERR(sensor); | |
1350 | } | |
1351 | ||
1352 | mutex_lock(&priv->lock); | |
1353 | ||
1354 | if (priv->stream_count > 0) { | |
1355 | ret = -EBUSY; | |
1356 | goto out; | |
1357 | } | |
1358 | ||
1359 | crop = __csi_get_crop(priv, cfg, sdformat->which); | |
69e78611 | 1360 | compose = __csi_get_compose(priv, cfg, sdformat->which); |
4a34ec8e | 1361 | |
69e78611 | 1362 | csi_try_fmt(priv, sensor, cfg, sdformat, crop, compose, &cc); |
4a34ec8e SL |
1363 | |
1364 | fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which); | |
1365 | *fmt = sdformat->format; | |
1366 | ||
1367 | if (sdformat->pad == CSI_SINK_PAD) { | |
1368 | int pad; | |
1369 | ||
1370 | /* propagate format to source pads */ | |
1371 | for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) { | |
1372 | const struct imx_media_pixfmt *outcc; | |
1373 | struct v4l2_mbus_framefmt *outfmt; | |
1374 | struct v4l2_subdev_format format; | |
1375 | ||
1376 | format.pad = pad; | |
1377 | format.which = sdformat->which; | |
1378 | format.format = sdformat->format; | |
69e78611 PZ |
1379 | csi_try_fmt(priv, sensor, cfg, &format, NULL, compose, |
1380 | &outcc); | |
4a34ec8e SL |
1381 | |
1382 | outfmt = __csi_get_fmt(priv, cfg, pad, sdformat->which); | |
1383 | *outfmt = format.format; | |
1384 | ||
1385 | if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE) | |
1386 | priv->cc[pad] = outcc; | |
1387 | } | |
1388 | } | |
1389 | ||
1390 | if (sdformat->which == V4L2_SUBDEV_FORMAT_TRY) | |
1391 | goto out; | |
1392 | ||
1393 | priv->cc[sdformat->pad] = cc; | |
1394 | ||
1395 | /* propagate IDMAC output pad format to capture device */ | |
1396 | imx_media_mbus_fmt_to_pix_fmt(&vdev_fmt, | |
1397 | &priv->format_mbus[CSI_SRC_PAD_IDMAC], | |
1398 | priv->cc[CSI_SRC_PAD_IDMAC]); | |
1399 | mutex_unlock(&priv->lock); | |
1400 | imx_media_capture_device_set_format(vdev, &vdev_fmt); | |
1401 | ||
1402 | return 0; | |
1403 | out: | |
1404 | mutex_unlock(&priv->lock); | |
1405 | return ret; | |
1406 | } | |
1407 | ||
1408 | static int csi_get_selection(struct v4l2_subdev *sd, | |
1409 | struct v4l2_subdev_pad_config *cfg, | |
1410 | struct v4l2_subdev_selection *sel) | |
1411 | { | |
1412 | struct csi_priv *priv = v4l2_get_subdevdata(sd); | |
1413 | struct v4l2_mbus_framefmt *infmt; | |
69e78611 | 1414 | struct v4l2_rect *crop, *compose; |
4a34ec8e SL |
1415 | int ret = 0; |
1416 | ||
69e78611 | 1417 | if (sel->pad != CSI_SINK_PAD) |
4a34ec8e SL |
1418 | return -EINVAL; |
1419 | ||
1420 | mutex_lock(&priv->lock); | |
1421 | ||
1422 | infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which); | |
1423 | crop = __csi_get_crop(priv, cfg, sel->which); | |
69e78611 | 1424 | compose = __csi_get_compose(priv, cfg, sel->which); |
4a34ec8e SL |
1425 | |
1426 | switch (sel->target) { | |
1427 | case V4L2_SEL_TGT_CROP_BOUNDS: | |
1428 | sel->r.left = 0; | |
1429 | sel->r.top = 0; | |
1430 | sel->r.width = infmt->width; | |
1431 | sel->r.height = infmt->height; | |
1432 | break; | |
1433 | case V4L2_SEL_TGT_CROP: | |
1434 | sel->r = *crop; | |
1435 | break; | |
69e78611 PZ |
1436 | case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
1437 | sel->r.left = 0; | |
1438 | sel->r.top = 0; | |
1439 | sel->r.width = crop->width; | |
1440 | sel->r.height = crop->height; | |
1441 | break; | |
1442 | case V4L2_SEL_TGT_COMPOSE: | |
1443 | sel->r = *compose; | |
1444 | break; | |
4a34ec8e SL |
1445 | default: |
1446 | ret = -EINVAL; | |
1447 | } | |
1448 | ||
1449 | mutex_unlock(&priv->lock); | |
1450 | return ret; | |
1451 | } | |
1452 | ||
69e78611 PZ |
1453 | static int csi_set_scale(u32 *compose, u32 crop, u32 flags) |
1454 | { | |
1455 | if ((flags & (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE)) == | |
1456 | (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE) && | |
1457 | *compose != crop && *compose != crop / 2) | |
1458 | return -ERANGE; | |
1459 | ||
1460 | if (*compose <= crop / 2 || | |
1461 | (*compose < crop * 3 / 4 && !(flags & V4L2_SEL_FLAG_GE)) || | |
1462 | (*compose < crop && (flags & V4L2_SEL_FLAG_LE))) | |
1463 | *compose = crop / 2; | |
1464 | else | |
1465 | *compose = crop; | |
1466 | ||
1467 | return 0; | |
1468 | } | |
1469 | ||
4a34ec8e SL |
1470 | static int csi_set_selection(struct v4l2_subdev *sd, |
1471 | struct v4l2_subdev_pad_config *cfg, | |
1472 | struct v4l2_subdev_selection *sel) | |
1473 | { | |
1474 | struct csi_priv *priv = v4l2_get_subdevdata(sd); | |
1475 | struct v4l2_mbus_framefmt *infmt; | |
69e78611 | 1476 | struct v4l2_rect *crop, *compose; |
4a34ec8e | 1477 | struct imx_media_subdev *sensor; |
4a34ec8e SL |
1478 | int pad, ret = 0; |
1479 | ||
69e78611 | 1480 | if (sel->pad != CSI_SINK_PAD) |
4a34ec8e SL |
1481 | return -EINVAL; |
1482 | ||
1483 | sensor = imx_media_find_sensor(priv->md, &priv->sd.entity); | |
1484 | if (IS_ERR(sensor)) { | |
1485 | v4l2_err(&priv->sd, "no sensor attached\n"); | |
1486 | return PTR_ERR(sensor); | |
1487 | } | |
1488 | ||
1489 | mutex_lock(&priv->lock); | |
1490 | ||
1491 | if (priv->stream_count > 0) { | |
1492 | ret = -EBUSY; | |
1493 | goto out; | |
1494 | } | |
1495 | ||
1496 | infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which); | |
1497 | crop = __csi_get_crop(priv, cfg, sel->which); | |
69e78611 | 1498 | compose = __csi_get_compose(priv, cfg, sel->which); |
4a34ec8e | 1499 | |
69e78611 PZ |
1500 | switch (sel->target) { |
1501 | case V4L2_SEL_TGT_CROP: | |
1502 | /* | |
1503 | * Modifying the crop rectangle always changes the format on | |
1504 | * the source pads. If the KEEP_CONFIG flag is set, just return | |
1505 | * the current crop rectangle. | |
1506 | */ | |
1507 | if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) { | |
1508 | sel->r = priv->crop; | |
1509 | if (sel->which == V4L2_SUBDEV_FORMAT_TRY) | |
1510 | *crop = sel->r; | |
1511 | goto out; | |
1512 | } | |
1513 | ||
1514 | csi_try_crop(priv, &sel->r, cfg, infmt, sensor); | |
1515 | ||
1516 | *crop = sel->r; | |
1517 | ||
1518 | /* Reset scaling to 1:1 */ | |
1519 | compose->width = crop->width; | |
1520 | compose->height = crop->height; | |
1521 | break; | |
1522 | case V4L2_SEL_TGT_COMPOSE: | |
1523 | /* | |
1524 | * Modifying the compose rectangle always changes the format on | |
1525 | * the source pads. If the KEEP_CONFIG flag is set, just return | |
1526 | * the current compose rectangle. | |
1527 | */ | |
1528 | if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) { | |
1529 | sel->r = priv->compose; | |
1530 | if (sel->which == V4L2_SUBDEV_FORMAT_TRY) | |
1531 | *compose = sel->r; | |
1532 | goto out; | |
1533 | } | |
4a34ec8e | 1534 | |
69e78611 PZ |
1535 | sel->r.left = 0; |
1536 | sel->r.top = 0; | |
1537 | ret = csi_set_scale(&sel->r.width, crop->width, sel->flags); | |
1538 | if (ret) | |
1539 | goto out; | |
1540 | ret = csi_set_scale(&sel->r.height, crop->height, sel->flags); | |
1541 | if (ret) | |
1542 | goto out; | |
4a34ec8e | 1543 | |
69e78611 PZ |
1544 | *compose = sel->r; |
1545 | break; | |
1546 | default: | |
1547 | ret = -EINVAL; | |
1548 | goto out; | |
1549 | } | |
4a34ec8e | 1550 | |
69e78611 | 1551 | /* Reset source pads to sink compose rectangle */ |
4a34ec8e SL |
1552 | for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) { |
1553 | struct v4l2_mbus_framefmt *outfmt; | |
1554 | ||
1555 | outfmt = __csi_get_fmt(priv, cfg, pad, sel->which); | |
69e78611 PZ |
1556 | outfmt->width = compose->width; |
1557 | outfmt->height = compose->height; | |
4a34ec8e SL |
1558 | } |
1559 | ||
1560 | out: | |
1561 | mutex_unlock(&priv->lock); | |
1562 | return ret; | |
1563 | } | |
1564 | ||
1565 | static int csi_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh, | |
1566 | struct v4l2_event_subscription *sub) | |
1567 | { | |
1568 | if (sub->type != V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR) | |
1569 | return -EINVAL; | |
1570 | if (sub->id != 0) | |
1571 | return -EINVAL; | |
1572 | ||
1573 | return v4l2_event_subscribe(fh, sub, 0, NULL); | |
1574 | } | |
1575 | ||
1576 | static int csi_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh, | |
1577 | struct v4l2_event_subscription *sub) | |
1578 | { | |
1579 | return v4l2_event_unsubscribe(fh, sub); | |
1580 | } | |
1581 | ||
1582 | /* | |
1583 | * retrieve our pads parsed from the OF graph by the media device | |
1584 | */ | |
1585 | static int csi_registered(struct v4l2_subdev *sd) | |
1586 | { | |
1587 | struct csi_priv *priv = v4l2_get_subdevdata(sd); | |
0b2e9e79 | 1588 | struct ipu_csi *csi; |
4a34ec8e SL |
1589 | int i, ret; |
1590 | u32 code; | |
1591 | ||
1592 | /* get media device */ | |
1593 | priv->md = dev_get_drvdata(sd->v4l2_dev->dev); | |
1594 | ||
1595 | /* get handle to IPU CSI */ | |
0b2e9e79 AB |
1596 | csi = ipu_csi_get(priv->ipu, priv->csi_id); |
1597 | if (IS_ERR(csi)) { | |
4a34ec8e | 1598 | v4l2_err(&priv->sd, "failed to get CSI%d\n", priv->csi_id); |
0b2e9e79 | 1599 | return PTR_ERR(csi); |
4a34ec8e | 1600 | } |
0b2e9e79 | 1601 | priv->csi = csi; |
4a34ec8e SL |
1602 | |
1603 | for (i = 0; i < CSI_NUM_PADS; i++) { | |
1604 | priv->pad[i].flags = (i == CSI_SINK_PAD) ? | |
1605 | MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE; | |
1606 | ||
1607 | code = 0; | |
1608 | if (i != CSI_SINK_PAD) | |
1609 | imx_media_enum_ipu_format(&code, 0, CS_SEL_YUV); | |
1610 | ||
1611 | /* set a default mbus format */ | |
1612 | ret = imx_media_init_mbus_fmt(&priv->format_mbus[i], | |
1613 | 640, 480, code, V4L2_FIELD_NONE, | |
1614 | &priv->cc[i]); | |
1615 | if (ret) | |
1616 | goto put_csi; | |
fb30ee79 PZ |
1617 | |
1618 | /* init default frame interval */ | |
1619 | priv->frame_interval[i].numerator = 1; | |
1620 | priv->frame_interval[i].denominator = 30; | |
4a34ec8e SL |
1621 | } |
1622 | ||
fb30ee79 PZ |
1623 | /* disable frame skipping */ |
1624 | priv->skip = &csi_skip[0]; | |
4a34ec8e | 1625 | |
69e78611 PZ |
1626 | /* init default crop and compose rectangle sizes */ |
1627 | priv->crop.width = 640; | |
1628 | priv->crop.height = 480; | |
1629 | priv->compose.width = 640; | |
1630 | priv->compose.height = 480; | |
1631 | ||
4a34ec8e SL |
1632 | priv->fim = imx_media_fim_init(&priv->sd); |
1633 | if (IS_ERR(priv->fim)) { | |
1634 | ret = PTR_ERR(priv->fim); | |
1635 | goto put_csi; | |
1636 | } | |
1637 | ||
1638 | ret = media_entity_pads_init(&sd->entity, CSI_NUM_PADS, priv->pad); | |
1639 | if (ret) | |
1640 | goto free_fim; | |
1641 | ||
1642 | ret = imx_media_capture_device_register(priv->vdev); | |
1643 | if (ret) | |
1644 | goto free_fim; | |
1645 | ||
1646 | ret = imx_media_add_video_device(priv->md, priv->vdev); | |
1647 | if (ret) | |
1648 | goto unreg; | |
1649 | ||
1650 | return 0; | |
1651 | unreg: | |
1652 | imx_media_capture_device_unregister(priv->vdev); | |
1653 | free_fim: | |
1654 | if (priv->fim) | |
1655 | imx_media_fim_free(priv->fim); | |
1656 | put_csi: | |
1657 | ipu_csi_put(priv->csi); | |
1658 | return ret; | |
1659 | } | |
1660 | ||
1661 | static void csi_unregistered(struct v4l2_subdev *sd) | |
1662 | { | |
1663 | struct csi_priv *priv = v4l2_get_subdevdata(sd); | |
1664 | ||
1665 | imx_media_capture_device_unregister(priv->vdev); | |
1666 | ||
1667 | if (priv->fim) | |
1668 | imx_media_fim_free(priv->fim); | |
1669 | ||
0b2e9e79 | 1670 | if (priv->csi) |
4a34ec8e SL |
1671 | ipu_csi_put(priv->csi); |
1672 | } | |
1673 | ||
1674 | static const struct media_entity_operations csi_entity_ops = { | |
1675 | .link_setup = csi_link_setup, | |
1676 | .link_validate = v4l2_subdev_link_validate, | |
1677 | }; | |
1678 | ||
1679 | static const struct v4l2_subdev_core_ops csi_core_ops = { | |
1680 | .subscribe_event = csi_subscribe_event, | |
1681 | .unsubscribe_event = csi_unsubscribe_event, | |
1682 | }; | |
1683 | ||
1684 | static const struct v4l2_subdev_video_ops csi_video_ops = { | |
1685 | .g_frame_interval = csi_g_frame_interval, | |
1686 | .s_frame_interval = csi_s_frame_interval, | |
1687 | .s_stream = csi_s_stream, | |
1688 | }; | |
1689 | ||
1690 | static const struct v4l2_subdev_pad_ops csi_pad_ops = { | |
1691 | .enum_mbus_code = csi_enum_mbus_code, | |
949ffdbf RK |
1692 | .enum_frame_size = csi_enum_frame_size, |
1693 | .enum_frame_interval = csi_enum_frame_interval, | |
4a34ec8e SL |
1694 | .get_fmt = csi_get_fmt, |
1695 | .set_fmt = csi_set_fmt, | |
1696 | .get_selection = csi_get_selection, | |
1697 | .set_selection = csi_set_selection, | |
1698 | .link_validate = csi_link_validate, | |
1699 | }; | |
1700 | ||
1701 | static const struct v4l2_subdev_ops csi_subdev_ops = { | |
1702 | .core = &csi_core_ops, | |
1703 | .video = &csi_video_ops, | |
1704 | .pad = &csi_pad_ops, | |
1705 | }; | |
1706 | ||
1707 | static const struct v4l2_subdev_internal_ops csi_internal_ops = { | |
1708 | .registered = csi_registered, | |
1709 | .unregistered = csi_unregistered, | |
1710 | }; | |
1711 | ||
1712 | static int imx_csi_probe(struct platform_device *pdev) | |
1713 | { | |
1714 | struct ipu_client_platformdata *pdata; | |
1715 | struct pinctrl *pinctrl; | |
1716 | struct csi_priv *priv; | |
1717 | int ret; | |
1718 | ||
1719 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); | |
1720 | if (!priv) | |
1721 | return -ENOMEM; | |
1722 | ||
1723 | platform_set_drvdata(pdev, &priv->sd); | |
1724 | priv->dev = &pdev->dev; | |
1725 | ||
1726 | ret = dma_set_coherent_mask(priv->dev, DMA_BIT_MASK(32)); | |
1727 | if (ret) | |
1728 | return ret; | |
1729 | ||
1730 | /* get parent IPU */ | |
1731 | priv->ipu = dev_get_drvdata(priv->dev->parent); | |
1732 | ||
1733 | /* get our CSI id */ | |
1734 | pdata = priv->dev->platform_data; | |
1735 | priv->csi_id = pdata->csi; | |
1736 | priv->smfc_id = (priv->csi_id == 0) ? 0 : 2; | |
1737 | ||
e99e88a9 | 1738 | timer_setup(&priv->eof_timeout_timer, csi_idmac_eof_timeout, 0); |
4a34ec8e SL |
1739 | spin_lock_init(&priv->irqlock); |
1740 | ||
1741 | v4l2_subdev_init(&priv->sd, &csi_subdev_ops); | |
1742 | v4l2_set_subdevdata(&priv->sd, priv); | |
1743 | priv->sd.internal_ops = &csi_internal_ops; | |
1744 | priv->sd.entity.ops = &csi_entity_ops; | |
1745 | priv->sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER; | |
1746 | priv->sd.dev = &pdev->dev; | |
1747 | priv->sd.fwnode = of_fwnode_handle(pdata->of_node); | |
1748 | priv->sd.owner = THIS_MODULE; | |
1749 | priv->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; | |
1750 | priv->sd.grp_id = priv->csi_id ? | |
1751 | IMX_MEDIA_GRP_ID_CSI1 : IMX_MEDIA_GRP_ID_CSI0; | |
1752 | imx_media_grp_id_to_sd_name(priv->sd.name, sizeof(priv->sd.name), | |
1753 | priv->sd.grp_id, ipu_get_num(priv->ipu)); | |
1754 | ||
1755 | priv->vdev = imx_media_capture_device_init(&priv->sd, | |
1756 | CSI_SRC_PAD_IDMAC); | |
1757 | if (IS_ERR(priv->vdev)) | |
1758 | return PTR_ERR(priv->vdev); | |
1759 | ||
1760 | mutex_init(&priv->lock); | |
1761 | ||
1762 | v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0); | |
1763 | priv->sd.ctrl_handler = &priv->ctrl_hdlr; | |
1764 | ||
1765 | /* | |
1766 | * The IPUv3 driver did not assign an of_node to this | |
1767 | * device. As a result, pinctrl does not automatically | |
1768 | * configure our pin groups, so we need to do that manually | |
1769 | * here, after setting this device's of_node. | |
1770 | */ | |
1771 | priv->dev->of_node = pdata->of_node; | |
1772 | pinctrl = devm_pinctrl_get_select_default(priv->dev); | |
1773 | ||
1774 | ret = v4l2_async_register_subdev(&priv->sd); | |
1775 | if (ret) | |
1776 | goto free; | |
1777 | ||
1778 | return 0; | |
1779 | free: | |
1780 | v4l2_ctrl_handler_free(&priv->ctrl_hdlr); | |
1781 | mutex_destroy(&priv->lock); | |
1782 | imx_media_capture_device_remove(priv->vdev); | |
1783 | return ret; | |
1784 | } | |
1785 | ||
1786 | static int imx_csi_remove(struct platform_device *pdev) | |
1787 | { | |
1788 | struct v4l2_subdev *sd = platform_get_drvdata(pdev); | |
1789 | struct csi_priv *priv = sd_to_dev(sd); | |
1790 | ||
1791 | v4l2_ctrl_handler_free(&priv->ctrl_hdlr); | |
1792 | mutex_destroy(&priv->lock); | |
1793 | imx_media_capture_device_remove(priv->vdev); | |
1794 | v4l2_async_unregister_subdev(sd); | |
1795 | media_entity_cleanup(&sd->entity); | |
1796 | ||
1797 | return 0; | |
1798 | } | |
1799 | ||
1800 | static const struct platform_device_id imx_csi_ids[] = { | |
1801 | { .name = "imx-ipuv3-csi" }, | |
1802 | { }, | |
1803 | }; | |
1804 | MODULE_DEVICE_TABLE(platform, imx_csi_ids); | |
1805 | ||
1806 | static struct platform_driver imx_csi_driver = { | |
1807 | .probe = imx_csi_probe, | |
1808 | .remove = imx_csi_remove, | |
1809 | .id_table = imx_csi_ids, | |
1810 | .driver = { | |
1811 | .name = "imx-ipuv3-csi", | |
1812 | }, | |
1813 | }; | |
1814 | module_platform_driver(imx_csi_driver); | |
1815 | ||
1816 | MODULE_DESCRIPTION("i.MX CSI subdev driver"); | |
1817 | MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>"); | |
1818 | MODULE_LICENSE("GPL"); | |
1819 | MODULE_ALIAS("platform:imx-ipuv3-csi"); |