1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
4  *
5  * Copyright (C) 2010 - 2012 Samsung Electronics Co., Ltd.
6  * Sylwester Nawrocki <s.nawrocki@samsung.com>
7  */
8 
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/types.h>
12 #include <linux/errno.h>
13 #include <linux/bug.h>
14 #include <linux/interrupt.h>
15 #include <linux/device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/list.h>
18 #include <linux/slab.h>
19 
20 #include <linux/videodev2.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/v4l2-mem2mem.h>
24 #include <media/v4l2-rect.h>
25 #include <media/videobuf2-v4l2.h>
26 #include <media/videobuf2-dma-contig.h>
27 
28 #include "common.h"
29 #include "fimc-core.h"
30 #include "fimc-reg.h"
31 #include "media-dev.h"
32 
fimc_capture_hw_init(struct fimc_dev * fimc)33 static int fimc_capture_hw_init(struct fimc_dev *fimc)
34 {
35 	struct fimc_source_info *si = &fimc->vid_cap.source_config;
36 	struct fimc_ctx *ctx = fimc->vid_cap.ctx;
37 	int ret;
38 	unsigned long flags;
39 
40 	if (ctx == NULL || ctx->s_frame.fmt == NULL)
41 		return -EINVAL;
42 
43 	if (si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK) {
44 		ret = fimc_hw_camblk_cfg_writeback(fimc);
45 		if (ret < 0)
46 			return ret;
47 	}
48 
49 	spin_lock_irqsave(&fimc->slock, flags);
50 	fimc_prepare_dma_offset(ctx, &ctx->d_frame);
51 	fimc_set_yuv_order(ctx);
52 
53 	fimc_hw_set_camera_polarity(fimc, si);
54 	fimc_hw_set_camera_type(fimc, si);
55 	fimc_hw_set_camera_source(fimc, si);
56 	fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
57 
58 	ret = fimc_set_scaler_info(ctx);
59 	if (!ret) {
60 		fimc_hw_set_input_path(ctx);
61 		fimc_hw_set_prescaler(ctx);
62 		fimc_hw_set_mainscaler(ctx);
63 		fimc_hw_set_target_format(ctx);
64 		fimc_hw_set_rotation(ctx);
65 		fimc_hw_set_effect(ctx);
66 		fimc_hw_set_output_path(ctx);
67 		fimc_hw_set_out_dma(ctx);
68 		if (fimc->drv_data->alpha_color)
69 			fimc_hw_set_rgb_alpha(ctx);
70 		clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
71 	}
72 	spin_unlock_irqrestore(&fimc->slock, flags);
73 	return ret;
74 }
75 
76 /*
77  * Reinitialize the driver so it is ready to start the streaming again.
78  * Set fimc->state to indicate stream off and the hardware shut down state.
79  * If not suspending (@suspend is false), return any buffers to videobuf2.
80  * Otherwise put any owned buffers onto the pending buffers queue, so they
81  * can be re-spun when the device is being resumed. Also perform FIMC
82  * software reset and disable streaming on the whole pipeline if required.
83  */
fimc_capture_state_cleanup(struct fimc_dev * fimc,bool suspend)84 static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
85 {
86 	struct fimc_vid_cap *cap = &fimc->vid_cap;
87 	struct fimc_vid_buffer *buf;
88 	unsigned long flags;
89 	bool streaming;
90 
91 	spin_lock_irqsave(&fimc->slock, flags);
92 	streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
93 
94 	fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
95 			 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
96 	if (suspend)
97 		fimc->state |= (1 << ST_CAPT_SUSPENDED);
98 	else
99 		fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
100 
101 	/* Release unused buffers */
102 	while (!suspend && !list_empty(&cap->pending_buf_q)) {
103 		buf = fimc_pending_queue_pop(cap);
104 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
105 	}
106 	/* If suspending put unused buffers onto pending queue */
107 	while (!list_empty(&cap->active_buf_q)) {
108 		buf = fimc_active_queue_pop(cap);
109 		if (suspend)
110 			fimc_pending_queue_add(cap, buf);
111 		else
112 			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
113 	}
114 
115 	fimc_hw_reset(fimc);
116 	cap->buf_index = 0;
117 
118 	spin_unlock_irqrestore(&fimc->slock, flags);
119 
120 	if (streaming)
121 		return fimc_pipeline_call(&cap->ve, set_stream, 0);
122 	else
123 		return 0;
124 }
125 
fimc_stop_capture(struct fimc_dev * fimc,bool suspend)126 static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
127 {
128 	unsigned long flags;
129 
130 	if (!fimc_capture_active(fimc))
131 		return 0;
132 
133 	spin_lock_irqsave(&fimc->slock, flags);
134 	set_bit(ST_CAPT_SHUT, &fimc->state);
135 	fimc_deactivate_capture(fimc);
136 	spin_unlock_irqrestore(&fimc->slock, flags);
137 
138 	wait_event_timeout(fimc->irq_queue,
139 			   !test_bit(ST_CAPT_SHUT, &fimc->state),
140 			   (2*HZ/10)); /* 200 ms */
141 
142 	return fimc_capture_state_cleanup(fimc, suspend);
143 }
144 
145 /**
146  * fimc_capture_config_update - apply the camera interface configuration
147  * @ctx: FIMC capture context
148  *
149  * To be called from within the interrupt handler with fimc.slock
150  * spinlock held. It updates the camera pixel crop, rotation and
151  * image flip in H/W.
152  */
fimc_capture_config_update(struct fimc_ctx * ctx)153 static int fimc_capture_config_update(struct fimc_ctx *ctx)
154 {
155 	struct fimc_dev *fimc = ctx->fimc_dev;
156 	int ret;
157 
158 	fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
159 
160 	ret = fimc_set_scaler_info(ctx);
161 	if (ret)
162 		return ret;
163 
164 	fimc_hw_set_prescaler(ctx);
165 	fimc_hw_set_mainscaler(ctx);
166 	fimc_hw_set_target_format(ctx);
167 	fimc_hw_set_rotation(ctx);
168 	fimc_hw_set_effect(ctx);
169 	fimc_prepare_dma_offset(ctx, &ctx->d_frame);
170 	fimc_hw_set_out_dma(ctx);
171 	if (fimc->drv_data->alpha_color)
172 		fimc_hw_set_rgb_alpha(ctx);
173 
174 	clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
175 	return ret;
176 }
177 
fimc_capture_irq_handler(struct fimc_dev * fimc,int deq_buf)178 void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
179 {
180 	struct fimc_vid_cap *cap = &fimc->vid_cap;
181 	struct fimc_pipeline *p = to_fimc_pipeline(cap->ve.pipe);
182 	struct v4l2_subdev *csis = p->subdevs[IDX_CSIS];
183 	struct fimc_frame *f = &cap->ctx->d_frame;
184 	struct fimc_vid_buffer *v_buf;
185 
186 	if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
187 		wake_up(&fimc->irq_queue);
188 		goto done;
189 	}
190 
191 	if (!list_empty(&cap->active_buf_q) &&
192 	    test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
193 		v_buf = fimc_active_queue_pop(cap);
194 
195 		v_buf->vb.vb2_buf.timestamp = ktime_get_ns();
196 		v_buf->vb.sequence = cap->frame_count++;
197 
198 		vb2_buffer_done(&v_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
199 	}
200 
201 	if (!list_empty(&cap->pending_buf_q)) {
202 
203 		v_buf = fimc_pending_queue_pop(cap);
204 		fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
205 		v_buf->index = cap->buf_index;
206 
207 		/* Move the buffer to the capture active queue */
208 		fimc_active_queue_add(cap, v_buf);
209 
210 		dbg("next frame: %d, done frame: %d",
211 		    fimc_hw_get_frame_index(fimc), v_buf->index);
212 
213 		if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
214 			cap->buf_index = 0;
215 	}
216 	/*
217 	 * Set up a buffer at MIPI-CSIS if current image format
218 	 * requires the frame embedded data capture.
219 	 */
220 	if (f->fmt->mdataplanes && !list_empty(&cap->active_buf_q)) {
221 		unsigned int plane = ffs(f->fmt->mdataplanes) - 1;
222 		unsigned int size = f->payload[plane];
223 		s32 index = fimc_hw_get_frame_index(fimc);
224 		void *vaddr;
225 
226 		list_for_each_entry(v_buf, &cap->active_buf_q, list) {
227 			if (v_buf->index != index)
228 				continue;
229 			vaddr = vb2_plane_vaddr(&v_buf->vb.vb2_buf, plane);
230 			v4l2_subdev_call(csis, video, s_rx_buffer,
231 					 vaddr, &size);
232 			break;
233 		}
234 	}
235 
236 	if (cap->active_buf_cnt == 0) {
237 		if (deq_buf)
238 			clear_bit(ST_CAPT_RUN, &fimc->state);
239 
240 		if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
241 			cap->buf_index = 0;
242 	} else {
243 		set_bit(ST_CAPT_RUN, &fimc->state);
244 	}
245 
246 	if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
247 		fimc_capture_config_update(cap->ctx);
248 done:
249 	if (cap->active_buf_cnt == 1) {
250 		fimc_deactivate_capture(fimc);
251 		clear_bit(ST_CAPT_STREAM, &fimc->state);
252 	}
253 
254 	dbg("frame: %d, active_buf_cnt: %d",
255 	    fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
256 }
257 
258 
start_streaming(struct vb2_queue * q,unsigned int count)259 static int start_streaming(struct vb2_queue *q, unsigned int count)
260 {
261 	struct fimc_ctx *ctx = q->drv_priv;
262 	struct fimc_dev *fimc = ctx->fimc_dev;
263 	struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
264 	int min_bufs;
265 	int ret;
266 
267 	vid_cap->frame_count = 0;
268 
269 	ret = fimc_capture_hw_init(fimc);
270 	if (ret) {
271 		fimc_capture_state_cleanup(fimc, false);
272 		return ret;
273 	}
274 
275 	set_bit(ST_CAPT_PEND, &fimc->state);
276 
277 	min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
278 
279 	if (vid_cap->active_buf_cnt >= min_bufs &&
280 	    !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
281 		fimc_activate_capture(ctx);
282 
283 		if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
284 			return fimc_pipeline_call(&vid_cap->ve, set_stream, 1);
285 	}
286 
287 	return 0;
288 }
289 
stop_streaming(struct vb2_queue * q)290 static void stop_streaming(struct vb2_queue *q)
291 {
292 	struct fimc_ctx *ctx = q->drv_priv;
293 	struct fimc_dev *fimc = ctx->fimc_dev;
294 
295 	if (!fimc_capture_active(fimc))
296 		return;
297 
298 	fimc_stop_capture(fimc, false);
299 }
300 
fimc_capture_suspend(struct fimc_dev * fimc)301 int fimc_capture_suspend(struct fimc_dev *fimc)
302 {
303 	bool suspend = fimc_capture_busy(fimc);
304 
305 	int ret = fimc_stop_capture(fimc, suspend);
306 	if (ret)
307 		return ret;
308 	return fimc_pipeline_call(&fimc->vid_cap.ve, close);
309 }
310 
311 static void buffer_queue(struct vb2_buffer *vb);
312 
fimc_capture_resume(struct fimc_dev * fimc)313 int fimc_capture_resume(struct fimc_dev *fimc)
314 {
315 	struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
316 	struct exynos_video_entity *ve = &vid_cap->ve;
317 	struct fimc_vid_buffer *buf;
318 	int i;
319 
320 	if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
321 		return 0;
322 
323 	INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
324 	vid_cap->buf_index = 0;
325 	fimc_pipeline_call(ve, open, &ve->vdev.entity, false);
326 	fimc_capture_hw_init(fimc);
327 
328 	clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
329 
330 	for (i = 0; i < vid_cap->reqbufs_count; i++) {
331 		if (list_empty(&vid_cap->pending_buf_q))
332 			break;
333 		buf = fimc_pending_queue_pop(vid_cap);
334 		buffer_queue(&buf->vb.vb2_buf);
335 	}
336 	return 0;
337 
338 }
339 
queue_setup(struct vb2_queue * vq,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])340 static int queue_setup(struct vb2_queue *vq,
341 		       unsigned int *num_buffers, unsigned int *num_planes,
342 		       unsigned int sizes[], struct device *alloc_devs[])
343 {
344 	struct fimc_ctx *ctx = vq->drv_priv;
345 	struct fimc_frame *frame = &ctx->d_frame;
346 	struct fimc_fmt *fmt = frame->fmt;
347 	unsigned long wh = frame->f_width * frame->f_height;
348 	int i;
349 
350 	if (fmt == NULL)
351 		return -EINVAL;
352 
353 	if (*num_planes) {
354 		if (*num_planes != fmt->memplanes)
355 			return -EINVAL;
356 		for (i = 0; i < *num_planes; i++)
357 			if (sizes[i] < (wh * fmt->depth[i]) / 8)
358 				return -EINVAL;
359 		return 0;
360 	}
361 
362 	*num_planes = fmt->memplanes;
363 
364 	for (i = 0; i < fmt->memplanes; i++) {
365 		unsigned int size = (wh * fmt->depth[i]) / 8;
366 
367 		if (fimc_fmt_is_user_defined(fmt->color))
368 			sizes[i] = frame->payload[i];
369 		else
370 			sizes[i] = max_t(u32, size, frame->payload[i]);
371 	}
372 
373 	return 0;
374 }
375 
buffer_prepare(struct vb2_buffer * vb)376 static int buffer_prepare(struct vb2_buffer *vb)
377 {
378 	struct vb2_queue *vq = vb->vb2_queue;
379 	struct fimc_ctx *ctx = vq->drv_priv;
380 	int i;
381 
382 	if (ctx->d_frame.fmt == NULL)
383 		return -EINVAL;
384 
385 	for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
386 		unsigned long size = ctx->d_frame.payload[i];
387 
388 		if (vb2_plane_size(vb, i) < size) {
389 			v4l2_err(&ctx->fimc_dev->vid_cap.ve.vdev,
390 				 "User buffer too small (%ld < %ld)\n",
391 				 vb2_plane_size(vb, i), size);
392 			return -EINVAL;
393 		}
394 		vb2_set_plane_payload(vb, i, size);
395 	}
396 
397 	return 0;
398 }
399 
buffer_queue(struct vb2_buffer * vb)400 static void buffer_queue(struct vb2_buffer *vb)
401 {
402 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
403 	struct fimc_vid_buffer *buf
404 		= container_of(vbuf, struct fimc_vid_buffer, vb);
405 	struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
406 	struct fimc_dev *fimc = ctx->fimc_dev;
407 	struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
408 	struct exynos_video_entity *ve = &vid_cap->ve;
409 	unsigned long flags;
410 	int min_bufs;
411 
412 	spin_lock_irqsave(&fimc->slock, flags);
413 	fimc_prepare_addr(ctx, &buf->vb.vb2_buf, &ctx->d_frame, &buf->paddr);
414 
415 	if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
416 	    !test_bit(ST_CAPT_STREAM, &fimc->state) &&
417 	    vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
418 		/* Setup the buffer directly for processing. */
419 		int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
420 				vid_cap->buf_index;
421 
422 		fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
423 		buf->index = vid_cap->buf_index;
424 		fimc_active_queue_add(vid_cap, buf);
425 
426 		if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
427 			vid_cap->buf_index = 0;
428 	} else {
429 		fimc_pending_queue_add(vid_cap, buf);
430 	}
431 
432 	min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
433 
434 
435 	if (vb2_is_streaming(&vid_cap->vbq) &&
436 	    vid_cap->active_buf_cnt >= min_bufs &&
437 	    !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
438 		int ret;
439 
440 		fimc_activate_capture(ctx);
441 		spin_unlock_irqrestore(&fimc->slock, flags);
442 
443 		if (test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
444 			return;
445 
446 		ret = fimc_pipeline_call(ve, set_stream, 1);
447 		if (ret < 0)
448 			v4l2_err(&ve->vdev, "stream on failed: %d\n", ret);
449 		return;
450 	}
451 	spin_unlock_irqrestore(&fimc->slock, flags);
452 }
453 
454 static const struct vb2_ops fimc_capture_qops = {
455 	.queue_setup		= queue_setup,
456 	.buf_prepare		= buffer_prepare,
457 	.buf_queue		= buffer_queue,
458 	.wait_prepare		= vb2_ops_wait_prepare,
459 	.wait_finish		= vb2_ops_wait_finish,
460 	.start_streaming	= start_streaming,
461 	.stop_streaming		= stop_streaming,
462 };
463 
464 static int fimc_capture_set_default_format(struct fimc_dev *fimc);
465 
fimc_capture_open(struct file * file)466 static int fimc_capture_open(struct file *file)
467 {
468 	struct fimc_dev *fimc = video_drvdata(file);
469 	struct fimc_vid_cap *vc = &fimc->vid_cap;
470 	struct exynos_video_entity *ve = &vc->ve;
471 	int ret = -EBUSY;
472 
473 	dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
474 
475 	mutex_lock(&fimc->lock);
476 
477 	if (fimc_m2m_active(fimc))
478 		goto unlock;
479 
480 	set_bit(ST_CAPT_BUSY, &fimc->state);
481 	ret = pm_runtime_get_sync(&fimc->pdev->dev);
482 	if (ret < 0) {
483 		pm_runtime_put_sync(&fimc->pdev->dev);
484 		goto unlock;
485 	}
486 
487 	ret = v4l2_fh_open(file);
488 	if (ret) {
489 		pm_runtime_put_sync(&fimc->pdev->dev);
490 		goto unlock;
491 	}
492 
493 	if (v4l2_fh_is_singular_file(file)) {
494 		fimc_md_graph_lock(ve);
495 
496 		ret = fimc_pipeline_call(ve, open, &ve->vdev.entity, true);
497 
498 		if (ret == 0)
499 			ve->vdev.entity.use_count++;
500 
501 		fimc_md_graph_unlock(ve);
502 
503 		if (ret == 0)
504 			ret = fimc_capture_set_default_format(fimc);
505 
506 		if (ret < 0) {
507 			clear_bit(ST_CAPT_BUSY, &fimc->state);
508 			pm_runtime_put_sync(&fimc->pdev->dev);
509 			v4l2_fh_release(file);
510 		}
511 	}
512 unlock:
513 	mutex_unlock(&fimc->lock);
514 	return ret;
515 }
516 
fimc_capture_release(struct file * file)517 static int fimc_capture_release(struct file *file)
518 {
519 	struct fimc_dev *fimc = video_drvdata(file);
520 	struct fimc_vid_cap *vc = &fimc->vid_cap;
521 	bool close = v4l2_fh_is_singular_file(file);
522 	int ret;
523 
524 	dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
525 
526 	mutex_lock(&fimc->lock);
527 
528 	if (close && vc->streaming) {
529 		media_pipeline_stop(&vc->ve.vdev.entity);
530 		vc->streaming = false;
531 	}
532 
533 	ret = _vb2_fop_release(file, NULL);
534 
535 	if (close) {
536 		clear_bit(ST_CAPT_BUSY, &fimc->state);
537 		fimc_pipeline_call(&vc->ve, close);
538 		clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
539 
540 		fimc_md_graph_lock(&vc->ve);
541 		vc->ve.vdev.entity.use_count--;
542 		fimc_md_graph_unlock(&vc->ve);
543 	}
544 
545 	pm_runtime_put_sync(&fimc->pdev->dev);
546 	mutex_unlock(&fimc->lock);
547 
548 	return ret;
549 }
550 
551 static const struct v4l2_file_operations fimc_capture_fops = {
552 	.owner		= THIS_MODULE,
553 	.open		= fimc_capture_open,
554 	.release	= fimc_capture_release,
555 	.poll		= vb2_fop_poll,
556 	.unlocked_ioctl	= video_ioctl2,
557 	.mmap		= vb2_fop_mmap,
558 };
559 
560 /*
561  * Format and crop negotiation helpers
562  */
563 
fimc_capture_try_format(struct fimc_ctx * ctx,u32 * width,u32 * height,u32 * code,u32 * fourcc,int pad)564 static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
565 						u32 *width, u32 *height,
566 						u32 *code, u32 *fourcc, int pad)
567 {
568 	bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
569 	struct fimc_dev *fimc = ctx->fimc_dev;
570 	const struct fimc_variant *var = fimc->variant;
571 	const struct fimc_pix_limit *pl = var->pix_limit;
572 	struct fimc_frame *dst = &ctx->d_frame;
573 	u32 depth, min_w, max_w, min_h, align_h = 3;
574 	u32 mask = FMT_FLAGS_CAM;
575 	struct fimc_fmt *ffmt;
576 
577 	/* Conversion from/to JPEG or User Defined format is not supported */
578 	if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
579 	    fimc_fmt_is_user_defined(ctx->s_frame.fmt->color))
580 		*code = ctx->s_frame.fmt->mbus_code;
581 
582 	if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad == FIMC_SD_PAD_SOURCE)
583 		mask |= FMT_FLAGS_M2M;
584 
585 	if (pad == FIMC_SD_PAD_SINK_FIFO)
586 		mask = FMT_FLAGS_WRITEBACK;
587 
588 	ffmt = fimc_find_format(fourcc, code, mask, 0);
589 	if (WARN_ON(!ffmt))
590 		return NULL;
591 
592 	if (code)
593 		*code = ffmt->mbus_code;
594 	if (fourcc)
595 		*fourcc = ffmt->fourcc;
596 
597 	if (pad != FIMC_SD_PAD_SOURCE) {
598 		max_w = fimc_fmt_is_user_defined(ffmt->color) ?
599 			pl->scaler_dis_w : pl->scaler_en_w;
600 		/* Apply the camera input interface pixel constraints */
601 		v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
602 				      height, max_t(u32, *height, 32),
603 				      FIMC_CAMIF_MAX_HEIGHT,
604 				      fimc_fmt_is_user_defined(ffmt->color) ?
605 				      3 : 1,
606 				      0);
607 		return ffmt;
608 	}
609 	/* Can't scale or crop in transparent (JPEG) transfer mode */
610 	if (fimc_fmt_is_user_defined(ffmt->color)) {
611 		*width  = ctx->s_frame.f_width;
612 		*height = ctx->s_frame.f_height;
613 		return ffmt;
614 	}
615 	/* Apply the scaler and the output DMA constraints */
616 	max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
617 	if (ctx->state & FIMC_COMPOSE) {
618 		min_w = dst->offs_h + dst->width;
619 		min_h = dst->offs_v + dst->height;
620 	} else {
621 		min_w = var->min_out_pixsize;
622 		min_h = var->min_out_pixsize;
623 	}
624 	if (var->min_vsize_align == 1 && !rotation)
625 		align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
626 
627 	depth = fimc_get_format_depth(ffmt);
628 	v4l_bound_align_image(width, min_w, max_w,
629 			      ffs(var->min_out_pixsize) - 1,
630 			      height, min_h, FIMC_CAMIF_MAX_HEIGHT,
631 			      align_h,
632 			      64/(ALIGN(depth, 8)));
633 
634 	dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
635 	    pad, code ? *code : 0, *width, *height,
636 	    dst->f_width, dst->f_height);
637 
638 	return ffmt;
639 }
640 
fimc_capture_try_selection(struct fimc_ctx * ctx,struct v4l2_rect * r,int target)641 static void fimc_capture_try_selection(struct fimc_ctx *ctx,
642 				       struct v4l2_rect *r,
643 				       int target)
644 {
645 	bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
646 	struct fimc_dev *fimc = ctx->fimc_dev;
647 	const struct fimc_variant *var = fimc->variant;
648 	const struct fimc_pix_limit *pl = var->pix_limit;
649 	struct fimc_frame *sink = &ctx->s_frame;
650 	u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
651 	u32 align_sz = 0, align_h = 4;
652 	u32 max_sc_h, max_sc_v;
653 
654 	/* In JPEG transparent transfer mode cropping is not supported */
655 	if (fimc_fmt_is_user_defined(ctx->d_frame.fmt->color)) {
656 		r->width  = sink->f_width;
657 		r->height = sink->f_height;
658 		r->left   = r->top = 0;
659 		return;
660 	}
661 	if (target == V4L2_SEL_TGT_COMPOSE) {
662 		u32 tmp_min_h = ffs(sink->width) - 3;
663 		u32 tmp_min_v = ffs(sink->height) - 1;
664 
665 		if (ctx->rotation != 90 && ctx->rotation != 270)
666 			align_h = 1;
667 		max_sc_h = min(SCALER_MAX_HRATIO, 1 << tmp_min_h);
668 		max_sc_v = min(SCALER_MAX_VRATIO, 1 << tmp_min_v);
669 		min_sz = var->min_out_pixsize;
670 	} else {
671 		u32 depth = fimc_get_format_depth(sink->fmt);
672 		align_sz = 64/ALIGN(depth, 8);
673 		min_sz = var->min_inp_pixsize;
674 		min_w = min_h = min_sz;
675 		max_sc_h = max_sc_v = 1;
676 	}
677 	/*
678 	 * For the compose rectangle the following constraints must be met:
679 	 * - it must fit in the sink pad format rectangle (f_width/f_height);
680 	 * - maximum downscaling ratio is 64;
681 	 * - maximum crop size depends if the rotator is used or not;
682 	 * - the sink pad format width/height must be 4 multiple of the
683 	 *   prescaler ratios determined by sink pad size and source pad crop,
684 	 *   the prescaler ratio is returned by fimc_get_scaler_factor().
685 	 */
686 	max_w = min_t(u32,
687 		      rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
688 		      rotate ? sink->f_height : sink->f_width);
689 	max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
690 
691 	if (target == V4L2_SEL_TGT_COMPOSE) {
692 		min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
693 		min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
694 		if (rotate) {
695 			swap(max_sc_h, max_sc_v);
696 			swap(min_w, min_h);
697 		}
698 	}
699 	v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
700 			      &r->height, min_h, max_h, align_h,
701 			      align_sz);
702 	/* Adjust left/top if crop/compose rectangle is out of bounds */
703 	r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
704 	r->top  = clamp_t(u32, r->top, 0, sink->f_height - r->height);
705 	r->left = round_down(r->left, var->hor_offs_align);
706 
707 	dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
708 	    target, r->left, r->top, r->width, r->height,
709 	    sink->f_width, sink->f_height);
710 }
711 
712 /*
713  * The video node ioctl operations
714  */
fimc_cap_querycap(struct file * file,void * priv,struct v4l2_capability * cap)715 static int fimc_cap_querycap(struct file *file, void *priv,
716 					struct v4l2_capability *cap)
717 {
718 	struct fimc_dev *fimc = video_drvdata(file);
719 
720 	__fimc_vidioc_querycap(&fimc->pdev->dev, cap);
721 	return 0;
722 }
723 
fimc_cap_enum_fmt(struct file * file,void * priv,struct v4l2_fmtdesc * f)724 static int fimc_cap_enum_fmt(struct file *file, void *priv,
725 			     struct v4l2_fmtdesc *f)
726 {
727 	struct fimc_fmt *fmt;
728 
729 	fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
730 			       f->index);
731 	if (!fmt)
732 		return -EINVAL;
733 	f->pixelformat = fmt->fourcc;
734 	return 0;
735 }
736 
fimc_pipeline_get_head(struct media_entity * me)737 static struct media_entity *fimc_pipeline_get_head(struct media_entity *me)
738 {
739 	struct media_pad *pad = &me->pads[0];
740 
741 	while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
742 		pad = media_entity_remote_pad(pad);
743 		if (!pad)
744 			break;
745 		me = pad->entity;
746 		pad = &me->pads[0];
747 	}
748 
749 	return me;
750 }
751 
752 /**
753  * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
754  *                            elements
755  * @ctx: FIMC capture context
756  * @tfmt: media bus format to try/set on subdevs
757  * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
758  * @set: true to set format on subdevs, false to try only
759  */
fimc_pipeline_try_format(struct fimc_ctx * ctx,struct v4l2_mbus_framefmt * tfmt,struct fimc_fmt ** fmt_id,bool set)760 static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
761 				    struct v4l2_mbus_framefmt *tfmt,
762 				    struct fimc_fmt **fmt_id,
763 				    bool set)
764 {
765 	struct fimc_dev *fimc = ctx->fimc_dev;
766 	struct fimc_pipeline *p = to_fimc_pipeline(fimc->vid_cap.ve.pipe);
767 	struct v4l2_subdev *sd = p->subdevs[IDX_SENSOR];
768 	struct v4l2_subdev_format sfmt;
769 	struct v4l2_mbus_framefmt *mf = &sfmt.format;
770 	struct media_entity *me;
771 	struct fimc_fmt *ffmt;
772 	struct media_pad *pad;
773 	int ret, i = 1;
774 	u32 fcc;
775 
776 	if (WARN_ON(!sd || !tfmt))
777 		return -EINVAL;
778 
779 	memset(&sfmt, 0, sizeof(sfmt));
780 	sfmt.format = *tfmt;
781 	sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
782 
783 	me = fimc_pipeline_get_head(&sd->entity);
784 
785 	while (1) {
786 		ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
787 					FMT_FLAGS_CAM, i++);
788 		if (ffmt == NULL) {
789 			/*
790 			 * Notify user-space if common pixel code for
791 			 * host and sensor does not exist.
792 			 */
793 			return -EINVAL;
794 		}
795 		mf->code = tfmt->code = ffmt->mbus_code;
796 
797 		/* set format on all pipeline subdevs */
798 		while (me != &fimc->vid_cap.subdev.entity) {
799 			sd = media_entity_to_v4l2_subdev(me);
800 
801 			sfmt.pad = 0;
802 			ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
803 			if (ret)
804 				return ret;
805 
806 			if (me->pads[0].flags & MEDIA_PAD_FL_SINK) {
807 				sfmt.pad = me->num_pads - 1;
808 				mf->code = tfmt->code;
809 				ret = v4l2_subdev_call(sd, pad, set_fmt, NULL,
810 									&sfmt);
811 				if (ret)
812 					return ret;
813 			}
814 
815 			pad = media_entity_remote_pad(&me->pads[sfmt.pad]);
816 			if (!pad)
817 				return -EINVAL;
818 			me = pad->entity;
819 		}
820 
821 		if (mf->code != tfmt->code)
822 			continue;
823 
824 		fcc = ffmt->fourcc;
825 		tfmt->width  = mf->width;
826 		tfmt->height = mf->height;
827 		ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
828 					NULL, &fcc, FIMC_SD_PAD_SINK_CAM);
829 		ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
830 					NULL, &fcc, FIMC_SD_PAD_SOURCE);
831 		if (ffmt && ffmt->mbus_code)
832 			mf->code = ffmt->mbus_code;
833 		if (mf->width != tfmt->width || mf->height != tfmt->height)
834 			continue;
835 		tfmt->code = mf->code;
836 		break;
837 	}
838 
839 	if (fmt_id && ffmt)
840 		*fmt_id = ffmt;
841 	*tfmt = *mf;
842 
843 	return 0;
844 }
845 
846 /**
847  * fimc_get_sensor_frame_desc - query the sensor for media bus frame parameters
848  * @sensor: pointer to the sensor subdev
849  * @plane_fmt: provides plane sizes corresponding to the frame layout entries
850  * @num_planes: number of planes
851  * @try: true to set the frame parameters, false to query only
852  *
853  * This function is used by this driver only for compressed/blob data formats.
854  */
fimc_get_sensor_frame_desc(struct v4l2_subdev * sensor,struct v4l2_plane_pix_format * plane_fmt,unsigned int num_planes,bool try)855 static int fimc_get_sensor_frame_desc(struct v4l2_subdev *sensor,
856 				      struct v4l2_plane_pix_format *plane_fmt,
857 				      unsigned int num_planes, bool try)
858 {
859 	struct v4l2_mbus_frame_desc fd;
860 	int i, ret;
861 	int pad;
862 
863 	for (i = 0; i < num_planes; i++)
864 		fd.entry[i].length = plane_fmt[i].sizeimage;
865 
866 	pad = sensor->entity.num_pads - 1;
867 	if (try)
868 		ret = v4l2_subdev_call(sensor, pad, set_frame_desc, pad, &fd);
869 	else
870 		ret = v4l2_subdev_call(sensor, pad, get_frame_desc, pad, &fd);
871 
872 	if (ret < 0)
873 		return ret;
874 
875 	if (num_planes != fd.num_entries)
876 		return -EINVAL;
877 
878 	for (i = 0; i < num_planes; i++)
879 		plane_fmt[i].sizeimage = fd.entry[i].length;
880 
881 	if (fd.entry[0].length > FIMC_MAX_JPEG_BUF_SIZE) {
882 		v4l2_err(sensor->v4l2_dev,  "Unsupported buffer size: %u\n",
883 			 fd.entry[0].length);
884 
885 		return -EINVAL;
886 	}
887 
888 	return 0;
889 }
890 
fimc_cap_g_fmt_mplane(struct file * file,void * fh,struct v4l2_format * f)891 static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
892 				 struct v4l2_format *f)
893 {
894 	struct fimc_dev *fimc = video_drvdata(file);
895 
896 	__fimc_get_format(&fimc->vid_cap.ctx->d_frame, f);
897 	return 0;
898 }
899 
900 /*
901  * Try or set format on the fimc.X.capture video node and additionally
902  * on the whole pipeline if @try is false.
903  * Locking: the caller must _not_ hold the graph mutex.
904  */
__video_try_or_set_format(struct fimc_dev * fimc,struct v4l2_format * f,bool try,struct fimc_fmt ** inp_fmt,struct fimc_fmt ** out_fmt)905 static int __video_try_or_set_format(struct fimc_dev *fimc,
906 				     struct v4l2_format *f, bool try,
907 				     struct fimc_fmt **inp_fmt,
908 				     struct fimc_fmt **out_fmt)
909 {
910 	struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
911 	struct fimc_vid_cap *vc = &fimc->vid_cap;
912 	struct exynos_video_entity *ve = &vc->ve;
913 	struct fimc_ctx *ctx = vc->ctx;
914 	unsigned int width = 0, height = 0;
915 	int ret = 0;
916 
917 	/* Pre-configure format at the camera input interface, for JPEG only */
918 	if (fimc_jpeg_fourcc(pix->pixelformat)) {
919 		fimc_capture_try_format(ctx, &pix->width, &pix->height,
920 					NULL, &pix->pixelformat,
921 					FIMC_SD_PAD_SINK_CAM);
922 		if (try) {
923 			width = pix->width;
924 			height = pix->height;
925 		} else {
926 			ctx->s_frame.f_width = pix->width;
927 			ctx->s_frame.f_height = pix->height;
928 		}
929 	}
930 
931 	/* Try the format at the scaler and the DMA output */
932 	*out_fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
933 					  NULL, &pix->pixelformat,
934 					  FIMC_SD_PAD_SOURCE);
935 	if (*out_fmt == NULL)
936 		return -EINVAL;
937 
938 	/* Restore image width/height for JPEG (no resizing supported). */
939 	if (try && fimc_jpeg_fourcc(pix->pixelformat)) {
940 		pix->width = width;
941 		pix->height = height;
942 	}
943 
944 	/* Try to match format at the host and the sensor */
945 	if (!vc->user_subdev_api) {
946 		struct v4l2_mbus_framefmt mbus_fmt;
947 		struct v4l2_mbus_framefmt *mf;
948 
949 		mf = try ? &mbus_fmt : &fimc->vid_cap.ci_fmt;
950 
951 		mf->code = (*out_fmt)->mbus_code;
952 		mf->width = pix->width;
953 		mf->height = pix->height;
954 
955 		fimc_md_graph_lock(ve);
956 		ret = fimc_pipeline_try_format(ctx, mf, inp_fmt, try);
957 		fimc_md_graph_unlock(ve);
958 
959 		if (ret < 0)
960 			return ret;
961 
962 		pix->width = mf->width;
963 		pix->height = mf->height;
964 	}
965 
966 	fimc_adjust_mplane_format(*out_fmt, pix->width, pix->height, pix);
967 
968 	if ((*out_fmt)->flags & FMT_FLAGS_COMPRESSED) {
969 		struct v4l2_subdev *sensor;
970 
971 		fimc_md_graph_lock(ve);
972 
973 		sensor = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR);
974 		if (sensor)
975 			fimc_get_sensor_frame_desc(sensor, pix->plane_fmt,
976 						   (*out_fmt)->memplanes, try);
977 		else
978 			ret = -EPIPE;
979 
980 		fimc_md_graph_unlock(ve);
981 	}
982 
983 	return ret;
984 }
985 
fimc_cap_try_fmt_mplane(struct file * file,void * fh,struct v4l2_format * f)986 static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
987 				   struct v4l2_format *f)
988 {
989 	struct fimc_dev *fimc = video_drvdata(file);
990 	struct fimc_fmt *out_fmt = NULL, *inp_fmt = NULL;
991 
992 	return __video_try_or_set_format(fimc, f, true, &inp_fmt, &out_fmt);
993 }
994 
fimc_capture_mark_jpeg_xfer(struct fimc_ctx * ctx,enum fimc_color_fmt color)995 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx,
996 					enum fimc_color_fmt color)
997 {
998 	bool jpeg = fimc_fmt_is_user_defined(color);
999 
1000 	ctx->scaler.enabled = !jpeg;
1001 	fimc_ctrls_activate(ctx, !jpeg);
1002 
1003 	if (jpeg)
1004 		set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
1005 	else
1006 		clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
1007 }
1008 
__fimc_capture_set_format(struct fimc_dev * fimc,struct v4l2_format * f)1009 static int __fimc_capture_set_format(struct fimc_dev *fimc,
1010 				     struct v4l2_format *f)
1011 {
1012 	struct fimc_vid_cap *vc = &fimc->vid_cap;
1013 	struct fimc_ctx *ctx = vc->ctx;
1014 	struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
1015 	struct fimc_frame *ff = &ctx->d_frame;
1016 	struct fimc_fmt *inp_fmt = NULL;
1017 	int ret, i;
1018 
1019 	if (vb2_is_busy(&fimc->vid_cap.vbq))
1020 		return -EBUSY;
1021 
1022 	ret = __video_try_or_set_format(fimc, f, false, &inp_fmt, &ff->fmt);
1023 	if (ret < 0)
1024 		return ret;
1025 
1026 	/* Update RGB Alpha control state and value range */
1027 	fimc_alpha_ctrl_update(ctx);
1028 
1029 	for (i = 0; i < ff->fmt->memplanes; i++) {
1030 		ff->bytesperline[i] = pix->plane_fmt[i].bytesperline;
1031 		ff->payload[i] = pix->plane_fmt[i].sizeimage;
1032 	}
1033 
1034 	set_frame_bounds(ff, pix->width, pix->height);
1035 	/* Reset the composition rectangle if not yet configured */
1036 	if (!(ctx->state & FIMC_COMPOSE))
1037 		set_frame_crop(ff, 0, 0, pix->width, pix->height);
1038 
1039 	fimc_capture_mark_jpeg_xfer(ctx, ff->fmt->color);
1040 
1041 	/* Reset cropping and set format at the camera interface input */
1042 	if (!vc->user_subdev_api) {
1043 		ctx->s_frame.fmt = inp_fmt;
1044 		set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
1045 		set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
1046 	}
1047 
1048 	return ret;
1049 }
1050 
fimc_cap_s_fmt_mplane(struct file * file,void * priv,struct v4l2_format * f)1051 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
1052 				 struct v4l2_format *f)
1053 {
1054 	struct fimc_dev *fimc = video_drvdata(file);
1055 
1056 	return __fimc_capture_set_format(fimc, f);
1057 }
1058 
fimc_cap_enum_input(struct file * file,void * priv,struct v4l2_input * i)1059 static int fimc_cap_enum_input(struct file *file, void *priv,
1060 			       struct v4l2_input *i)
1061 {
1062 	struct fimc_dev *fimc = video_drvdata(file);
1063 	struct exynos_video_entity *ve = &fimc->vid_cap.ve;
1064 	struct v4l2_subdev *sd;
1065 
1066 	if (i->index != 0)
1067 		return -EINVAL;
1068 
1069 	i->type = V4L2_INPUT_TYPE_CAMERA;
1070 	fimc_md_graph_lock(ve);
1071 	sd = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR);
1072 	fimc_md_graph_unlock(ve);
1073 
1074 	if (sd)
1075 		strscpy(i->name, sd->name, sizeof(i->name));
1076 
1077 	return 0;
1078 }
1079 
fimc_cap_s_input(struct file * file,void * priv,unsigned int i)1080 static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
1081 {
1082 	return i == 0 ? i : -EINVAL;
1083 }
1084 
fimc_cap_g_input(struct file * file,void * priv,unsigned int * i)1085 static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
1086 {
1087 	*i = 0;
1088 	return 0;
1089 }
1090 
1091 /**
1092  * fimc_pipeline_validate - check for formats inconsistencies
1093  *                          between source and sink pad of each link
1094  * @fimc:	the FIMC device this context applies to
1095  *
1096  * Return 0 if all formats match or -EPIPE otherwise.
1097  */
fimc_pipeline_validate(struct fimc_dev * fimc)1098 static int fimc_pipeline_validate(struct fimc_dev *fimc)
1099 {
1100 	struct v4l2_subdev_format sink_fmt, src_fmt;
1101 	struct fimc_vid_cap *vc = &fimc->vid_cap;
1102 	struct v4l2_subdev *sd = &vc->subdev;
1103 	struct fimc_pipeline *p = to_fimc_pipeline(vc->ve.pipe);
1104 	struct media_pad *sink_pad, *src_pad;
1105 	int i, ret;
1106 
1107 	while (1) {
1108 		/*
1109 		 * Find current entity sink pad and any remote sink pad linked
1110 		 * to it. We stop if there is no sink pad in current entity or
1111 		 * it is not linked to any other remote entity.
1112 		 */
1113 		src_pad = NULL;
1114 
1115 		for (i = 0; i < sd->entity.num_pads; i++) {
1116 			struct media_pad *p = &sd->entity.pads[i];
1117 
1118 			if (p->flags & MEDIA_PAD_FL_SINK) {
1119 				sink_pad = p;
1120 				src_pad = media_entity_remote_pad(sink_pad);
1121 				if (src_pad)
1122 					break;
1123 			}
1124 		}
1125 
1126 		if (!src_pad || !is_media_entity_v4l2_subdev(src_pad->entity))
1127 			break;
1128 
1129 		/* Don't call FIMC subdev operation to avoid nested locking */
1130 		if (sd == &vc->subdev) {
1131 			struct fimc_frame *ff = &vc->ctx->s_frame;
1132 			sink_fmt.format.width = ff->f_width;
1133 			sink_fmt.format.height = ff->f_height;
1134 			sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
1135 		} else {
1136 			sink_fmt.pad = sink_pad->index;
1137 			sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1138 			ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
1139 			if (ret < 0 && ret != -ENOIOCTLCMD)
1140 				return -EPIPE;
1141 		}
1142 
1143 		/* Retrieve format at the source pad */
1144 		sd = media_entity_to_v4l2_subdev(src_pad->entity);
1145 		src_fmt.pad = src_pad->index;
1146 		src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1147 		ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
1148 		if (ret < 0 && ret != -ENOIOCTLCMD)
1149 			return -EPIPE;
1150 
1151 		if (src_fmt.format.width != sink_fmt.format.width ||
1152 		    src_fmt.format.height != sink_fmt.format.height ||
1153 		    src_fmt.format.code != sink_fmt.format.code)
1154 			return -EPIPE;
1155 
1156 		if (sd == p->subdevs[IDX_SENSOR] &&
1157 		    fimc_user_defined_mbus_fmt(src_fmt.format.code)) {
1158 			struct v4l2_plane_pix_format plane_fmt[FIMC_MAX_PLANES];
1159 			struct fimc_frame *frame = &vc->ctx->d_frame;
1160 			unsigned int i;
1161 
1162 			ret = fimc_get_sensor_frame_desc(sd, plane_fmt,
1163 							 frame->fmt->memplanes,
1164 							 false);
1165 			if (ret < 0)
1166 				return -EPIPE;
1167 
1168 			for (i = 0; i < frame->fmt->memplanes; i++)
1169 				if (frame->payload[i] < plane_fmt[i].sizeimage)
1170 					return -EPIPE;
1171 		}
1172 	}
1173 	return 0;
1174 }
1175 
fimc_cap_streamon(struct file * file,void * priv,enum v4l2_buf_type type)1176 static int fimc_cap_streamon(struct file *file, void *priv,
1177 			     enum v4l2_buf_type type)
1178 {
1179 	struct fimc_dev *fimc = video_drvdata(file);
1180 	struct fimc_vid_cap *vc = &fimc->vid_cap;
1181 	struct media_entity *entity = &vc->ve.vdev.entity;
1182 	struct fimc_source_info *si = NULL;
1183 	struct v4l2_subdev *sd;
1184 	int ret;
1185 
1186 	if (fimc_capture_active(fimc))
1187 		return -EBUSY;
1188 
1189 	ret = media_pipeline_start(entity, &vc->ve.pipe->mp);
1190 	if (ret < 0)
1191 		return ret;
1192 
1193 	sd = __fimc_md_get_subdev(vc->ve.pipe, IDX_SENSOR);
1194 	if (sd)
1195 		si = v4l2_get_subdev_hostdata(sd);
1196 
1197 	if (si == NULL) {
1198 		ret = -EPIPE;
1199 		goto err_p_stop;
1200 	}
1201 	/*
1202 	 * Save configuration data related to currently attached image
1203 	 * sensor or other data source, e.g. FIMC-IS.
1204 	 */
1205 	vc->source_config = *si;
1206 
1207 	if (vc->input == GRP_ID_FIMC_IS)
1208 		vc->source_config.fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
1209 
1210 	if (vc->user_subdev_api) {
1211 		ret = fimc_pipeline_validate(fimc);
1212 		if (ret < 0)
1213 			goto err_p_stop;
1214 	}
1215 
1216 	ret = vb2_ioctl_streamon(file, priv, type);
1217 	if (!ret) {
1218 		vc->streaming = true;
1219 		return ret;
1220 	}
1221 
1222 err_p_stop:
1223 	media_pipeline_stop(entity);
1224 	return ret;
1225 }
1226 
fimc_cap_streamoff(struct file * file,void * priv,enum v4l2_buf_type type)1227 static int fimc_cap_streamoff(struct file *file, void *priv,
1228 			    enum v4l2_buf_type type)
1229 {
1230 	struct fimc_dev *fimc = video_drvdata(file);
1231 	struct fimc_vid_cap *vc = &fimc->vid_cap;
1232 	int ret;
1233 
1234 	ret = vb2_ioctl_streamoff(file, priv, type);
1235 	if (ret < 0)
1236 		return ret;
1237 
1238 	if (vc->streaming) {
1239 		media_pipeline_stop(&vc->ve.vdev.entity);
1240 		vc->streaming = false;
1241 	}
1242 
1243 	return 0;
1244 }
1245 
fimc_cap_reqbufs(struct file * file,void * priv,struct v4l2_requestbuffers * reqbufs)1246 static int fimc_cap_reqbufs(struct file *file, void *priv,
1247 			    struct v4l2_requestbuffers *reqbufs)
1248 {
1249 	struct fimc_dev *fimc = video_drvdata(file);
1250 	int ret;
1251 
1252 	ret = vb2_ioctl_reqbufs(file, priv, reqbufs);
1253 
1254 	if (!ret)
1255 		fimc->vid_cap.reqbufs_count = reqbufs->count;
1256 
1257 	return ret;
1258 }
1259 
fimc_cap_g_selection(struct file * file,void * fh,struct v4l2_selection * s)1260 static int fimc_cap_g_selection(struct file *file, void *fh,
1261 				struct v4l2_selection *s)
1262 {
1263 	struct fimc_dev *fimc = video_drvdata(file);
1264 	struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1265 	struct fimc_frame *f = &ctx->s_frame;
1266 
1267 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1268 		return -EINVAL;
1269 
1270 	switch (s->target) {
1271 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1272 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1273 		f = &ctx->d_frame;
1274 		fallthrough;
1275 	case V4L2_SEL_TGT_CROP_BOUNDS:
1276 	case V4L2_SEL_TGT_CROP_DEFAULT:
1277 		s->r.left = 0;
1278 		s->r.top = 0;
1279 		s->r.width = f->o_width;
1280 		s->r.height = f->o_height;
1281 		return 0;
1282 
1283 	case V4L2_SEL_TGT_COMPOSE:
1284 		f = &ctx->d_frame;
1285 		fallthrough;
1286 	case V4L2_SEL_TGT_CROP:
1287 		s->r.left = f->offs_h;
1288 		s->r.top = f->offs_v;
1289 		s->r.width = f->width;
1290 		s->r.height = f->height;
1291 		return 0;
1292 	}
1293 
1294 	return -EINVAL;
1295 }
1296 
fimc_cap_s_selection(struct file * file,void * fh,struct v4l2_selection * s)1297 static int fimc_cap_s_selection(struct file *file, void *fh,
1298 				struct v4l2_selection *s)
1299 {
1300 	struct fimc_dev *fimc = video_drvdata(file);
1301 	struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1302 	struct v4l2_rect rect = s->r;
1303 	struct fimc_frame *f;
1304 	unsigned long flags;
1305 
1306 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1307 		return -EINVAL;
1308 
1309 	if (s->target == V4L2_SEL_TGT_COMPOSE)
1310 		f = &ctx->d_frame;
1311 	else if (s->target == V4L2_SEL_TGT_CROP)
1312 		f = &ctx->s_frame;
1313 	else
1314 		return -EINVAL;
1315 
1316 	fimc_capture_try_selection(ctx, &rect, s->target);
1317 
1318 	if (s->flags & V4L2_SEL_FLAG_LE &&
1319 	    !v4l2_rect_enclosed(&rect, &s->r))
1320 		return -ERANGE;
1321 
1322 	if (s->flags & V4L2_SEL_FLAG_GE &&
1323 	    !v4l2_rect_enclosed(&s->r, &rect))
1324 		return -ERANGE;
1325 
1326 	s->r = rect;
1327 	spin_lock_irqsave(&fimc->slock, flags);
1328 	set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1329 		       s->r.height);
1330 	spin_unlock_irqrestore(&fimc->slock, flags);
1331 
1332 	set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1333 	return 0;
1334 }
1335 
1336 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1337 	.vidioc_querycap		= fimc_cap_querycap,
1338 
1339 	.vidioc_enum_fmt_vid_cap	= fimc_cap_enum_fmt,
1340 	.vidioc_try_fmt_vid_cap_mplane	= fimc_cap_try_fmt_mplane,
1341 	.vidioc_s_fmt_vid_cap_mplane	= fimc_cap_s_fmt_mplane,
1342 	.vidioc_g_fmt_vid_cap_mplane	= fimc_cap_g_fmt_mplane,
1343 
1344 	.vidioc_reqbufs			= fimc_cap_reqbufs,
1345 	.vidioc_querybuf		= vb2_ioctl_querybuf,
1346 	.vidioc_qbuf			= vb2_ioctl_qbuf,
1347 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
1348 	.vidioc_expbuf			= vb2_ioctl_expbuf,
1349 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
1350 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
1351 
1352 	.vidioc_streamon		= fimc_cap_streamon,
1353 	.vidioc_streamoff		= fimc_cap_streamoff,
1354 
1355 	.vidioc_g_selection		= fimc_cap_g_selection,
1356 	.vidioc_s_selection		= fimc_cap_s_selection,
1357 
1358 	.vidioc_enum_input		= fimc_cap_enum_input,
1359 	.vidioc_s_input			= fimc_cap_s_input,
1360 	.vidioc_g_input			= fimc_cap_g_input,
1361 };
1362 
1363 /* Capture subdev media entity operations */
fimc_link_setup(struct media_entity * entity,const struct media_pad * local,const struct media_pad * remote,u32 flags)1364 static int fimc_link_setup(struct media_entity *entity,
1365 			   const struct media_pad *local,
1366 			   const struct media_pad *remote, u32 flags)
1367 {
1368 	struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1369 	struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1370 	struct fimc_vid_cap *vc = &fimc->vid_cap;
1371 	struct v4l2_subdev *sensor;
1372 
1373 	if (!is_media_entity_v4l2_subdev(remote->entity))
1374 		return -EINVAL;
1375 
1376 	if (WARN_ON(fimc == NULL))
1377 		return 0;
1378 
1379 	dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1380 	    local->entity->name, remote->entity->name, flags,
1381 	    fimc->vid_cap.input);
1382 
1383 	if (!(flags & MEDIA_LNK_FL_ENABLED)) {
1384 		fimc->vid_cap.input = 0;
1385 		return 0;
1386 	}
1387 
1388 	if (vc->input != 0)
1389 		return -EBUSY;
1390 
1391 	vc->input = sd->grp_id;
1392 
1393 	if (vc->user_subdev_api)
1394 		return 0;
1395 
1396 	/* Inherit V4L2 controls from the image sensor subdev. */
1397 	sensor = fimc_find_remote_sensor(&vc->subdev.entity);
1398 	if (sensor == NULL)
1399 		return 0;
1400 
1401 	return v4l2_ctrl_add_handler(&vc->ctx->ctrls.handler,
1402 				     sensor->ctrl_handler, NULL, true);
1403 }
1404 
1405 static const struct media_entity_operations fimc_sd_media_ops = {
1406 	.link_setup = fimc_link_setup,
1407 };
1408 
1409 /**
1410  * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1411  * @sd: pointer to a subdev generating the notification
1412  * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1413  * @arg: pointer to an u32 type integer that stores the frame payload value
1414  *
1415  * The End Of Frame notification sent by sensor subdev in its still capture
1416  * mode. If there is only a single VSYNC generated by the sensor at the
1417  * beginning of a frame transmission, FIMC does not issue the LastIrq
1418  * (end of frame) interrupt. And this notification is used to complete the
1419  * frame capture and returning a buffer to user-space. Subdev drivers should
1420  * call this notification from their last 'End of frame capture' interrupt.
1421  */
fimc_sensor_notify(struct v4l2_subdev * sd,unsigned int notification,void * arg)1422 void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1423 			void *arg)
1424 {
1425 	struct fimc_source_info	*si;
1426 	struct fimc_vid_buffer *buf;
1427 	struct fimc_md *fmd;
1428 	struct fimc_dev *fimc;
1429 	unsigned long flags;
1430 
1431 	if (sd == NULL)
1432 		return;
1433 
1434 	si = v4l2_get_subdev_hostdata(sd);
1435 	fmd = entity_to_fimc_mdev(&sd->entity);
1436 
1437 	spin_lock_irqsave(&fmd->slock, flags);
1438 
1439 	fimc = si ? source_to_sensor_info(si)->host : NULL;
1440 
1441 	if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1442 	    test_bit(ST_CAPT_PEND, &fimc->state)) {
1443 		unsigned long irq_flags;
1444 		spin_lock_irqsave(&fimc->slock, irq_flags);
1445 		if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1446 			buf = list_entry(fimc->vid_cap.active_buf_q.next,
1447 					 struct fimc_vid_buffer, list);
1448 			vb2_set_plane_payload(&buf->vb.vb2_buf, 0,
1449 					      *((u32 *)arg));
1450 		}
1451 		fimc_capture_irq_handler(fimc, 1);
1452 		fimc_deactivate_capture(fimc);
1453 		spin_unlock_irqrestore(&fimc->slock, irq_flags);
1454 	}
1455 	spin_unlock_irqrestore(&fmd->slock, flags);
1456 }
1457 
fimc_subdev_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)1458 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1459 				      struct v4l2_subdev_pad_config *cfg,
1460 				      struct v4l2_subdev_mbus_code_enum *code)
1461 {
1462 	struct fimc_fmt *fmt;
1463 
1464 	fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1465 	if (!fmt)
1466 		return -EINVAL;
1467 	code->code = fmt->mbus_code;
1468 	return 0;
1469 }
1470 
fimc_subdev_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)1471 static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1472 			       struct v4l2_subdev_pad_config *cfg,
1473 			       struct v4l2_subdev_format *fmt)
1474 {
1475 	struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1476 	struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1477 	struct fimc_frame *ff = &ctx->s_frame;
1478 	struct v4l2_mbus_framefmt *mf;
1479 
1480 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1481 		mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1482 		fmt->format = *mf;
1483 		return 0;
1484 	}
1485 
1486 	mf = &fmt->format;
1487 	mutex_lock(&fimc->lock);
1488 
1489 	switch (fmt->pad) {
1490 	case FIMC_SD_PAD_SOURCE:
1491 		if (!WARN_ON(ff->fmt == NULL))
1492 			mf->code = ff->fmt->mbus_code;
1493 		/* Sink pads crop rectangle size */
1494 		mf->width = ff->width;
1495 		mf->height = ff->height;
1496 		break;
1497 	case FIMC_SD_PAD_SINK_FIFO:
1498 		*mf = fimc->vid_cap.wb_fmt;
1499 		break;
1500 	case FIMC_SD_PAD_SINK_CAM:
1501 	default:
1502 		*mf = fimc->vid_cap.ci_fmt;
1503 		break;
1504 	}
1505 
1506 	mutex_unlock(&fimc->lock);
1507 	mf->colorspace = V4L2_COLORSPACE_JPEG;
1508 
1509 	return 0;
1510 }
1511 
fimc_subdev_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)1512 static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1513 			       struct v4l2_subdev_pad_config *cfg,
1514 			       struct v4l2_subdev_format *fmt)
1515 {
1516 	struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1517 	struct v4l2_mbus_framefmt *mf = &fmt->format;
1518 	struct fimc_vid_cap *vc = &fimc->vid_cap;
1519 	struct fimc_ctx *ctx = vc->ctx;
1520 	struct fimc_frame *ff;
1521 	struct fimc_fmt *ffmt;
1522 
1523 	dbg("pad%d: code: 0x%x, %dx%d",
1524 	    fmt->pad, mf->code, mf->width, mf->height);
1525 
1526 	if (fmt->pad == FIMC_SD_PAD_SOURCE && vb2_is_busy(&vc->vbq))
1527 		return -EBUSY;
1528 
1529 	mutex_lock(&fimc->lock);
1530 	ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1531 				       &mf->code, NULL, fmt->pad);
1532 	mutex_unlock(&fimc->lock);
1533 	mf->colorspace = V4L2_COLORSPACE_JPEG;
1534 
1535 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1536 		mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1537 		*mf = fmt->format;
1538 		return 0;
1539 	}
1540 	/* There must be a bug in the driver if this happens */
1541 	if (WARN_ON(ffmt == NULL))
1542 		return -EINVAL;
1543 
1544 	/* Update RGB Alpha control state and value range */
1545 	fimc_alpha_ctrl_update(ctx);
1546 
1547 	fimc_capture_mark_jpeg_xfer(ctx, ffmt->color);
1548 	if (fmt->pad == FIMC_SD_PAD_SOURCE) {
1549 		ff = &ctx->d_frame;
1550 		/* Sink pads crop rectangle size */
1551 		mf->width = ctx->s_frame.width;
1552 		mf->height = ctx->s_frame.height;
1553 	} else {
1554 		ff = &ctx->s_frame;
1555 	}
1556 
1557 	mutex_lock(&fimc->lock);
1558 	set_frame_bounds(ff, mf->width, mf->height);
1559 
1560 	if (fmt->pad == FIMC_SD_PAD_SINK_FIFO)
1561 		vc->wb_fmt = *mf;
1562 	else if (fmt->pad == FIMC_SD_PAD_SINK_CAM)
1563 		vc->ci_fmt = *mf;
1564 
1565 	ff->fmt = ffmt;
1566 
1567 	/* Reset the crop rectangle if required. */
1568 	if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_COMPOSE)))
1569 		set_frame_crop(ff, 0, 0, mf->width, mf->height);
1570 
1571 	if (fmt->pad != FIMC_SD_PAD_SOURCE)
1572 		ctx->state &= ~FIMC_COMPOSE;
1573 
1574 	mutex_unlock(&fimc->lock);
1575 	return 0;
1576 }
1577 
fimc_subdev_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)1578 static int fimc_subdev_get_selection(struct v4l2_subdev *sd,
1579 				     struct v4l2_subdev_pad_config *cfg,
1580 				     struct v4l2_subdev_selection *sel)
1581 {
1582 	struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1583 	struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1584 	struct fimc_frame *f = &ctx->s_frame;
1585 	struct v4l2_rect *r = &sel->r;
1586 	struct v4l2_rect *try_sel;
1587 
1588 	if (sel->pad == FIMC_SD_PAD_SOURCE)
1589 		return -EINVAL;
1590 
1591 	mutex_lock(&fimc->lock);
1592 
1593 	switch (sel->target) {
1594 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1595 		f = &ctx->d_frame;
1596 		fallthrough;
1597 	case V4L2_SEL_TGT_CROP_BOUNDS:
1598 		r->width = f->o_width;
1599 		r->height = f->o_height;
1600 		r->left = 0;
1601 		r->top = 0;
1602 		mutex_unlock(&fimc->lock);
1603 		return 0;
1604 
1605 	case V4L2_SEL_TGT_CROP:
1606 		try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
1607 		break;
1608 	case V4L2_SEL_TGT_COMPOSE:
1609 		try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
1610 		f = &ctx->d_frame;
1611 		break;
1612 	default:
1613 		mutex_unlock(&fimc->lock);
1614 		return -EINVAL;
1615 	}
1616 
1617 	if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1618 		sel->r = *try_sel;
1619 	} else {
1620 		r->left = f->offs_h;
1621 		r->top = f->offs_v;
1622 		r->width = f->width;
1623 		r->height = f->height;
1624 	}
1625 
1626 	dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1627 	    sel->pad, r->left, r->top, r->width, r->height,
1628 	    f->f_width, f->f_height);
1629 
1630 	mutex_unlock(&fimc->lock);
1631 	return 0;
1632 }
1633 
fimc_subdev_set_selection(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)1634 static int fimc_subdev_set_selection(struct v4l2_subdev *sd,
1635 				     struct v4l2_subdev_pad_config *cfg,
1636 				     struct v4l2_subdev_selection *sel)
1637 {
1638 	struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1639 	struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1640 	struct fimc_frame *f = &ctx->s_frame;
1641 	struct v4l2_rect *r = &sel->r;
1642 	struct v4l2_rect *try_sel;
1643 	unsigned long flags;
1644 
1645 	if (sel->pad == FIMC_SD_PAD_SOURCE)
1646 		return -EINVAL;
1647 
1648 	mutex_lock(&fimc->lock);
1649 	fimc_capture_try_selection(ctx, r, V4L2_SEL_TGT_CROP);
1650 
1651 	switch (sel->target) {
1652 	case V4L2_SEL_TGT_CROP:
1653 		try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
1654 		break;
1655 	case V4L2_SEL_TGT_COMPOSE:
1656 		try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
1657 		f = &ctx->d_frame;
1658 		break;
1659 	default:
1660 		mutex_unlock(&fimc->lock);
1661 		return -EINVAL;
1662 	}
1663 
1664 	if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1665 		*try_sel = sel->r;
1666 	} else {
1667 		spin_lock_irqsave(&fimc->slock, flags);
1668 		set_frame_crop(f, r->left, r->top, r->width, r->height);
1669 		set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1670 		if (sel->target == V4L2_SEL_TGT_COMPOSE)
1671 			ctx->state |= FIMC_COMPOSE;
1672 		spin_unlock_irqrestore(&fimc->slock, flags);
1673 	}
1674 
1675 	dbg("target %#x: (%d,%d)/%dx%d", sel->target, r->left, r->top,
1676 	    r->width, r->height);
1677 
1678 	mutex_unlock(&fimc->lock);
1679 	return 0;
1680 }
1681 
1682 static const struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1683 	.enum_mbus_code = fimc_subdev_enum_mbus_code,
1684 	.get_selection = fimc_subdev_get_selection,
1685 	.set_selection = fimc_subdev_set_selection,
1686 	.get_fmt = fimc_subdev_get_fmt,
1687 	.set_fmt = fimc_subdev_set_fmt,
1688 };
1689 
1690 static const struct v4l2_subdev_ops fimc_subdev_ops = {
1691 	.pad = &fimc_subdev_pad_ops,
1692 };
1693 
1694 /* Set default format at the sensor and host interface */
fimc_capture_set_default_format(struct fimc_dev * fimc)1695 static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1696 {
1697 	struct v4l2_format fmt = {
1698 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1699 		.fmt.pix_mp = {
1700 			.width		= FIMC_DEFAULT_WIDTH,
1701 			.height		= FIMC_DEFAULT_HEIGHT,
1702 			.pixelformat	= V4L2_PIX_FMT_YUYV,
1703 			.field		= V4L2_FIELD_NONE,
1704 			.colorspace	= V4L2_COLORSPACE_JPEG,
1705 		},
1706 	};
1707 
1708 	return __fimc_capture_set_format(fimc, &fmt);
1709 }
1710 
1711 /* fimc->lock must be already initialized */
fimc_register_capture_device(struct fimc_dev * fimc,struct v4l2_device * v4l2_dev)1712 static int fimc_register_capture_device(struct fimc_dev *fimc,
1713 				 struct v4l2_device *v4l2_dev)
1714 {
1715 	struct video_device *vfd = &fimc->vid_cap.ve.vdev;
1716 	struct vb2_queue *q = &fimc->vid_cap.vbq;
1717 	struct fimc_ctx *ctx;
1718 	struct fimc_vid_cap *vid_cap;
1719 	struct fimc_fmt *fmt;
1720 	int ret = -ENOMEM;
1721 
1722 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1723 	if (!ctx)
1724 		return -ENOMEM;
1725 
1726 	ctx->fimc_dev	 = fimc;
1727 	ctx->in_path	 = FIMC_IO_CAMERA;
1728 	ctx->out_path	 = FIMC_IO_DMA;
1729 	ctx->state	 = FIMC_CTX_CAP;
1730 	ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1731 	ctx->d_frame.fmt = ctx->s_frame.fmt;
1732 
1733 	memset(vfd, 0, sizeof(*vfd));
1734 	snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
1735 
1736 	vfd->fops	= &fimc_capture_fops;
1737 	vfd->ioctl_ops	= &fimc_capture_ioctl_ops;
1738 	vfd->v4l2_dev	= v4l2_dev;
1739 	vfd->minor	= -1;
1740 	vfd->release	= video_device_release_empty;
1741 	vfd->queue	= q;
1742 	vfd->lock	= &fimc->lock;
1743 	vfd->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
1744 
1745 	video_set_drvdata(vfd, fimc);
1746 	vid_cap = &fimc->vid_cap;
1747 	vid_cap->active_buf_cnt = 0;
1748 	vid_cap->reqbufs_count = 0;
1749 	vid_cap->ctx = ctx;
1750 
1751 	INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1752 	INIT_LIST_HEAD(&vid_cap->active_buf_q);
1753 
1754 	memset(q, 0, sizeof(*q));
1755 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1756 	q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1757 	q->drv_priv = ctx;
1758 	q->ops = &fimc_capture_qops;
1759 	q->mem_ops = &vb2_dma_contig_memops;
1760 	q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1761 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1762 	q->lock = &fimc->lock;
1763 	q->dev = &fimc->pdev->dev;
1764 
1765 	ret = vb2_queue_init(q);
1766 	if (ret)
1767 		goto err_free_ctx;
1768 
1769 	/* Default format configuration */
1770 	fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1771 	vid_cap->ci_fmt.width = FIMC_DEFAULT_WIDTH;
1772 	vid_cap->ci_fmt.height = FIMC_DEFAULT_HEIGHT;
1773 	vid_cap->ci_fmt.code = fmt->mbus_code;
1774 
1775 	ctx->s_frame.width = FIMC_DEFAULT_WIDTH;
1776 	ctx->s_frame.height = FIMC_DEFAULT_HEIGHT;
1777 	ctx->s_frame.fmt = fmt;
1778 
1779 	fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_WRITEBACK, 0);
1780 	vid_cap->wb_fmt = vid_cap->ci_fmt;
1781 	vid_cap->wb_fmt.code = fmt->mbus_code;
1782 
1783 	vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
1784 	vfd->entity.function = MEDIA_ENT_F_PROC_VIDEO_SCALER;
1785 	ret = media_entity_pads_init(&vfd->entity, 1, &vid_cap->vd_pad);
1786 	if (ret)
1787 		goto err_free_ctx;
1788 
1789 	ret = fimc_ctrls_create(ctx);
1790 	if (ret)
1791 		goto err_me_cleanup;
1792 
1793 	ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1);
1794 	if (ret)
1795 		goto err_ctrl_free;
1796 
1797 	v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1798 		  vfd->name, video_device_node_name(vfd));
1799 
1800 	vfd->ctrl_handler = &ctx->ctrls.handler;
1801 	return 0;
1802 
1803 err_ctrl_free:
1804 	fimc_ctrls_delete(ctx);
1805 err_me_cleanup:
1806 	media_entity_cleanup(&vfd->entity);
1807 err_free_ctx:
1808 	kfree(ctx);
1809 	return ret;
1810 }
1811 
fimc_capture_subdev_registered(struct v4l2_subdev * sd)1812 static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
1813 {
1814 	struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1815 	int ret;
1816 
1817 	if (fimc == NULL)
1818 		return -ENXIO;
1819 
1820 	ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1821 	if (ret)
1822 		return ret;
1823 
1824 	fimc->vid_cap.ve.pipe = v4l2_get_subdev_hostdata(sd);
1825 
1826 	ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
1827 	if (ret) {
1828 		fimc_unregister_m2m_device(fimc);
1829 		fimc->vid_cap.ve.pipe = NULL;
1830 	}
1831 
1832 	return ret;
1833 }
1834 
fimc_capture_subdev_unregistered(struct v4l2_subdev * sd)1835 static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1836 {
1837 	struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1838 	struct video_device *vdev;
1839 
1840 	if (fimc == NULL)
1841 		return;
1842 
1843 	mutex_lock(&fimc->lock);
1844 
1845 	fimc_unregister_m2m_device(fimc);
1846 	vdev = &fimc->vid_cap.ve.vdev;
1847 
1848 	if (video_is_registered(vdev)) {
1849 		video_unregister_device(vdev);
1850 		media_entity_cleanup(&vdev->entity);
1851 		fimc_ctrls_delete(fimc->vid_cap.ctx);
1852 		fimc->vid_cap.ve.pipe = NULL;
1853 	}
1854 	kfree(fimc->vid_cap.ctx);
1855 	fimc->vid_cap.ctx = NULL;
1856 
1857 	mutex_unlock(&fimc->lock);
1858 }
1859 
1860 static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1861 	.registered = fimc_capture_subdev_registered,
1862 	.unregistered = fimc_capture_subdev_unregistered,
1863 };
1864 
fimc_initialize_capture_subdev(struct fimc_dev * fimc)1865 int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1866 {
1867 	struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1868 	int ret;
1869 
1870 	v4l2_subdev_init(sd, &fimc_subdev_ops);
1871 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1872 	snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->id);
1873 
1874 	fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_CAM].flags = MEDIA_PAD_FL_SINK;
1875 	fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_FIFO].flags = MEDIA_PAD_FL_SINK;
1876 	fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1877 	ret = media_entity_pads_init(&sd->entity, FIMC_SD_PADS_NUM,
1878 				fimc->vid_cap.sd_pads);
1879 	if (ret)
1880 		return ret;
1881 
1882 	sd->entity.ops = &fimc_sd_media_ops;
1883 	sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_SCALER;
1884 	sd->internal_ops = &fimc_capture_sd_internal_ops;
1885 	v4l2_set_subdevdata(sd, fimc);
1886 	return 0;
1887 }
1888 
fimc_unregister_capture_subdev(struct fimc_dev * fimc)1889 void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1890 {
1891 	struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1892 
1893 	v4l2_device_unregister_subdev(sd);
1894 	media_entity_cleanup(&sd->entity);
1895 	v4l2_set_subdevdata(sd, NULL);
1896 }
1897