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