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