• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *
150  * To be called from within the interrupt handler with fimc.slock
151  * spinlock held. It updates the camera pixel crop, rotation and
152  * image flip in H/W.
153  */
fimc_capture_config_update(struct fimc_ctx * ctx)154 static int fimc_capture_config_update(struct fimc_ctx *ctx)
155 {
156 	struct fimc_dev *fimc = ctx->fimc_dev;
157 	int ret;
158 
159 	fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
160 
161 	ret = fimc_set_scaler_info(ctx);
162 	if (ret)
163 		return ret;
164 
165 	fimc_hw_set_prescaler(ctx);
166 	fimc_hw_set_mainscaler(ctx);
167 	fimc_hw_set_target_format(ctx);
168 	fimc_hw_set_rotation(ctx);
169 	fimc_hw_set_effect(ctx);
170 	fimc_prepare_dma_offset(ctx, &ctx->d_frame);
171 	fimc_hw_set_out_dma(ctx);
172 	if (fimc->drv_data->alpha_color)
173 		fimc_hw_set_rgb_alpha(ctx);
174 
175 	clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
176 	return ret;
177 }
178 
fimc_capture_irq_handler(struct fimc_dev * fimc,int deq_buf)179 void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
180 {
181 	struct fimc_vid_cap *cap = &fimc->vid_cap;
182 	struct fimc_pipeline *p = to_fimc_pipeline(cap->ve.pipe);
183 	struct v4l2_subdev *csis = p->subdevs[IDX_CSIS];
184 	struct fimc_frame *f = &cap->ctx->d_frame;
185 	struct fimc_vid_buffer *v_buf;
186 
187 	if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
188 		wake_up(&fimc->irq_queue);
189 		goto done;
190 	}
191 
192 	if (!list_empty(&cap->active_buf_q) &&
193 	    test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
194 		v_buf = fimc_active_queue_pop(cap);
195 
196 		v4l2_get_timestamp(&v_buf->vb.timestamp);
197 		v_buf->vb.sequence = cap->frame_count++;
198 
199 		vb2_buffer_done(&v_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
200 	}
201 
202 	if (!list_empty(&cap->pending_buf_q)) {
203 
204 		v_buf = fimc_pending_queue_pop(cap);
205 		fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
206 		v_buf->index = cap->buf_index;
207 
208 		/* Move the buffer to the capture active queue */
209 		fimc_active_queue_add(cap, v_buf);
210 
211 		dbg("next frame: %d, done frame: %d",
212 		    fimc_hw_get_frame_index(fimc), v_buf->index);
213 
214 		if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
215 			cap->buf_index = 0;
216 	}
217 	/*
218 	 * Set up a buffer at MIPI-CSIS if current image format
219 	 * requires the frame embedded data capture.
220 	 */
221 	if (f->fmt->mdataplanes && !list_empty(&cap->active_buf_q)) {
222 		unsigned int plane = ffs(f->fmt->mdataplanes) - 1;
223 		unsigned int size = f->payload[plane];
224 		s32 index = fimc_hw_get_frame_index(fimc);
225 		void *vaddr;
226 
227 		list_for_each_entry(v_buf, &cap->active_buf_q, list) {
228 			if (v_buf->index != index)
229 				continue;
230 			vaddr = vb2_plane_vaddr(&v_buf->vb.vb2_buf, plane);
231 			v4l2_subdev_call(csis, video, s_rx_buffer,
232 					 vaddr, &size);
233 			break;
234 		}
235 	}
236 
237 	if (cap->active_buf_cnt == 0) {
238 		if (deq_buf)
239 			clear_bit(ST_CAPT_RUN, &fimc->state);
240 
241 		if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
242 			cap->buf_index = 0;
243 	} else {
244 		set_bit(ST_CAPT_RUN, &fimc->state);
245 	}
246 
247 	if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
248 		fimc_capture_config_update(cap->ctx);
249 done:
250 	if (cap->active_buf_cnt == 1) {
251 		fimc_deactivate_capture(fimc);
252 		clear_bit(ST_CAPT_STREAM, &fimc->state);
253 	}
254 
255 	dbg("frame: %d, active_buf_cnt: %d",
256 	    fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
257 }
258 
259 
start_streaming(struct vb2_queue * q,unsigned int count)260 static int start_streaming(struct vb2_queue *q, unsigned int count)
261 {
262 	struct fimc_ctx *ctx = q->drv_priv;
263 	struct fimc_dev *fimc = ctx->fimc_dev;
264 	struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
265 	int min_bufs;
266 	int ret;
267 
268 	vid_cap->frame_count = 0;
269 
270 	ret = fimc_capture_hw_init(fimc);
271 	if (ret) {
272 		fimc_capture_state_cleanup(fimc, false);
273 		return ret;
274 	}
275 
276 	set_bit(ST_CAPT_PEND, &fimc->state);
277 
278 	min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
279 
280 	if (vid_cap->active_buf_cnt >= min_bufs &&
281 	    !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
282 		fimc_activate_capture(ctx);
283 
284 		if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
285 			return fimc_pipeline_call(&vid_cap->ve, set_stream, 1);
286 	}
287 
288 	return 0;
289 }
290 
stop_streaming(struct vb2_queue * q)291 static void stop_streaming(struct vb2_queue *q)
292 {
293 	struct fimc_ctx *ctx = q->drv_priv;
294 	struct fimc_dev *fimc = ctx->fimc_dev;
295 
296 	if (!fimc_capture_active(fimc))
297 		return;
298 
299 	fimc_stop_capture(fimc, false);
300 }
301 
fimc_capture_suspend(struct fimc_dev * fimc)302 int fimc_capture_suspend(struct fimc_dev *fimc)
303 {
304 	bool suspend = fimc_capture_busy(fimc);
305 
306 	int ret = fimc_stop_capture(fimc, suspend);
307 	if (ret)
308 		return ret;
309 	return fimc_pipeline_call(&fimc->vid_cap.ve, close);
310 }
311 
312 static void buffer_queue(struct vb2_buffer *vb);
313 
fimc_capture_resume(struct fimc_dev * fimc)314 int fimc_capture_resume(struct fimc_dev *fimc)
315 {
316 	struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
317 	struct exynos_video_entity *ve = &vid_cap->ve;
318 	struct fimc_vid_buffer *buf;
319 	int i;
320 
321 	if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
322 		return 0;
323 
324 	INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
325 	vid_cap->buf_index = 0;
326 	fimc_pipeline_call(ve, open, &ve->vdev.entity, false);
327 	fimc_capture_hw_init(fimc);
328 
329 	clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
330 
331 	for (i = 0; i < vid_cap->reqbufs_count; i++) {
332 		if (list_empty(&vid_cap->pending_buf_q))
333 			break;
334 		buf = fimc_pending_queue_pop(vid_cap);
335 		buffer_queue(&buf->vb.vb2_buf);
336 	}
337 	return 0;
338 
339 }
340 
queue_setup(struct vb2_queue * vq,const void * parg,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],void * allocators[])341 static int queue_setup(struct vb2_queue *vq, const void *parg,
342 		       unsigned int *num_buffers, unsigned int *num_planes,
343 		       unsigned int sizes[], void *allocators[])
344 {
345 	const struct v4l2_format *pfmt = parg;
346 	const struct v4l2_pix_format_mplane *pixm = NULL;
347 	struct fimc_ctx *ctx = vq->drv_priv;
348 	struct fimc_frame *frame = &ctx->d_frame;
349 	struct fimc_fmt *fmt = frame->fmt;
350 	unsigned long wh;
351 	int i;
352 
353 	if (pfmt) {
354 		pixm = &pfmt->fmt.pix_mp;
355 		fmt = fimc_find_format(&pixm->pixelformat, NULL,
356 				       FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
357 		wh = pixm->width * pixm->height;
358 	} else {
359 		wh = frame->f_width * frame->f_height;
360 	}
361 
362 	if (fmt == NULL)
363 		return -EINVAL;
364 
365 	*num_planes = fmt->memplanes;
366 
367 	for (i = 0; i < fmt->memplanes; i++) {
368 		unsigned int size = (wh * fmt->depth[i]) / 8;
369 		if (pixm)
370 			sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
371 		else if (fimc_fmt_is_user_defined(fmt->color))
372 			sizes[i] = frame->payload[i];
373 		else
374 			sizes[i] = max_t(u32, size, frame->payload[i]);
375 
376 		allocators[i] = ctx->fimc_dev->alloc_ctx;
377 	}
378 
379 	return 0;
380 }
381 
buffer_prepare(struct vb2_buffer * vb)382 static int buffer_prepare(struct vb2_buffer *vb)
383 {
384 	struct vb2_queue *vq = vb->vb2_queue;
385 	struct fimc_ctx *ctx = vq->drv_priv;
386 	int i;
387 
388 	if (ctx->d_frame.fmt == NULL)
389 		return -EINVAL;
390 
391 	for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
392 		unsigned long size = ctx->d_frame.payload[i];
393 
394 		if (vb2_plane_size(vb, i) < size) {
395 			v4l2_err(&ctx->fimc_dev->vid_cap.ve.vdev,
396 				 "User buffer too small (%ld < %ld)\n",
397 				 vb2_plane_size(vb, i), size);
398 			return -EINVAL;
399 		}
400 		vb2_set_plane_payload(vb, i, size);
401 	}
402 
403 	return 0;
404 }
405 
buffer_queue(struct vb2_buffer * vb)406 static void buffer_queue(struct vb2_buffer *vb)
407 {
408 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
409 	struct fimc_vid_buffer *buf
410 		= container_of(vbuf, struct fimc_vid_buffer, vb);
411 	struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
412 	struct fimc_dev *fimc = ctx->fimc_dev;
413 	struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
414 	struct exynos_video_entity *ve = &vid_cap->ve;
415 	unsigned long flags;
416 	int min_bufs;
417 
418 	spin_lock_irqsave(&fimc->slock, flags);
419 	fimc_prepare_addr(ctx, &buf->vb.vb2_buf, &ctx->d_frame, &buf->paddr);
420 
421 	if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
422 	    !test_bit(ST_CAPT_STREAM, &fimc->state) &&
423 	    vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
424 		/* Setup the buffer directly for processing. */
425 		int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
426 				vid_cap->buf_index;
427 
428 		fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
429 		buf->index = vid_cap->buf_index;
430 		fimc_active_queue_add(vid_cap, buf);
431 
432 		if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
433 			vid_cap->buf_index = 0;
434 	} else {
435 		fimc_pending_queue_add(vid_cap, buf);
436 	}
437 
438 	min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
439 
440 
441 	if (vb2_is_streaming(&vid_cap->vbq) &&
442 	    vid_cap->active_buf_cnt >= min_bufs &&
443 	    !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
444 		int ret;
445 
446 		fimc_activate_capture(ctx);
447 		spin_unlock_irqrestore(&fimc->slock, flags);
448 
449 		if (test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
450 			return;
451 
452 		ret = fimc_pipeline_call(ve, set_stream, 1);
453 		if (ret < 0)
454 			v4l2_err(&ve->vdev, "stream on failed: %d\n", ret);
455 		return;
456 	}
457 	spin_unlock_irqrestore(&fimc->slock, flags);
458 }
459 
460 static struct vb2_ops fimc_capture_qops = {
461 	.queue_setup		= queue_setup,
462 	.buf_prepare		= buffer_prepare,
463 	.buf_queue		= buffer_queue,
464 	.wait_prepare		= vb2_ops_wait_prepare,
465 	.wait_finish		= vb2_ops_wait_finish,
466 	.start_streaming	= start_streaming,
467 	.stop_streaming		= stop_streaming,
468 };
469 
470 static int fimc_capture_set_default_format(struct fimc_dev *fimc);
471 
fimc_capture_open(struct file * file)472 static int fimc_capture_open(struct file *file)
473 {
474 	struct fimc_dev *fimc = video_drvdata(file);
475 	struct fimc_vid_cap *vc = &fimc->vid_cap;
476 	struct exynos_video_entity *ve = &vc->ve;
477 	int ret = -EBUSY;
478 
479 	dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
480 
481 	mutex_lock(&fimc->lock);
482 
483 	if (fimc_m2m_active(fimc))
484 		goto unlock;
485 
486 	set_bit(ST_CAPT_BUSY, &fimc->state);
487 	ret = pm_runtime_get_sync(&fimc->pdev->dev);
488 	if (ret < 0)
489 		goto unlock;
490 
491 	ret = v4l2_fh_open(file);
492 	if (ret) {
493 		pm_runtime_put_sync(&fimc->pdev->dev);
494 		goto unlock;
495 	}
496 
497 	if (v4l2_fh_is_singular_file(file)) {
498 		fimc_md_graph_lock(ve);
499 
500 		ret = fimc_pipeline_call(ve, open, &ve->vdev.entity, true);
501 
502 		if (ret == 0 && vc->user_subdev_api && vc->inh_sensor_ctrls) {
503 			/*
504 			 * Recreate controls of the the video node to drop
505 			 * any controls inherited from the sensor subdev.
506 			 */
507 			fimc_ctrls_delete(vc->ctx);
508 
509 			ret = fimc_ctrls_create(vc->ctx);
510 			if (ret == 0)
511 				vc->inh_sensor_ctrls = false;
512 		}
513 		if (ret == 0)
514 			ve->vdev.entity.use_count++;
515 
516 		fimc_md_graph_unlock(ve);
517 
518 		if (ret == 0)
519 			ret = fimc_capture_set_default_format(fimc);
520 
521 		if (ret < 0) {
522 			clear_bit(ST_CAPT_BUSY, &fimc->state);
523 			pm_runtime_put_sync(&fimc->pdev->dev);
524 			v4l2_fh_release(file);
525 		}
526 	}
527 unlock:
528 	mutex_unlock(&fimc->lock);
529 	return ret;
530 }
531 
fimc_capture_release(struct file * file)532 static int fimc_capture_release(struct file *file)
533 {
534 	struct fimc_dev *fimc = video_drvdata(file);
535 	struct fimc_vid_cap *vc = &fimc->vid_cap;
536 	bool close = v4l2_fh_is_singular_file(file);
537 	int ret;
538 
539 	dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
540 
541 	mutex_lock(&fimc->lock);
542 
543 	if (close && vc->streaming) {
544 		media_entity_pipeline_stop(&vc->ve.vdev.entity);
545 		vc->streaming = false;
546 	}
547 
548 	ret = _vb2_fop_release(file, NULL);
549 
550 	if (close) {
551 		clear_bit(ST_CAPT_BUSY, &fimc->state);
552 		fimc_pipeline_call(&vc->ve, close);
553 		clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
554 
555 		fimc_md_graph_lock(&vc->ve);
556 		vc->ve.vdev.entity.use_count--;
557 		fimc_md_graph_unlock(&vc->ve);
558 	}
559 
560 	pm_runtime_put_sync(&fimc->pdev->dev);
561 	mutex_unlock(&fimc->lock);
562 
563 	return ret;
564 }
565 
566 static const struct v4l2_file_operations fimc_capture_fops = {
567 	.owner		= THIS_MODULE,
568 	.open		= fimc_capture_open,
569 	.release	= fimc_capture_release,
570 	.poll		= vb2_fop_poll,
571 	.unlocked_ioctl	= video_ioctl2,
572 	.mmap		= vb2_fop_mmap,
573 };
574 
575 /*
576  * Format and crop negotiation helpers
577  */
578 
fimc_capture_try_format(struct fimc_ctx * ctx,u32 * width,u32 * height,u32 * code,u32 * fourcc,int pad)579 static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
580 						u32 *width, u32 *height,
581 						u32 *code, u32 *fourcc, int pad)
582 {
583 	bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
584 	struct fimc_dev *fimc = ctx->fimc_dev;
585 	const struct fimc_variant *var = fimc->variant;
586 	const struct fimc_pix_limit *pl = var->pix_limit;
587 	struct fimc_frame *dst = &ctx->d_frame;
588 	u32 depth, min_w, max_w, min_h, align_h = 3;
589 	u32 mask = FMT_FLAGS_CAM;
590 	struct fimc_fmt *ffmt;
591 
592 	/* Conversion from/to JPEG or User Defined format is not supported */
593 	if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
594 	    fimc_fmt_is_user_defined(ctx->s_frame.fmt->color))
595 		*code = ctx->s_frame.fmt->mbus_code;
596 
597 	if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad == FIMC_SD_PAD_SOURCE)
598 		mask |= FMT_FLAGS_M2M;
599 
600 	if (pad == FIMC_SD_PAD_SINK_FIFO)
601 		mask = FMT_FLAGS_WRITEBACK;
602 
603 	ffmt = fimc_find_format(fourcc, code, mask, 0);
604 	if (WARN_ON(!ffmt))
605 		return NULL;
606 
607 	if (code)
608 		*code = ffmt->mbus_code;
609 	if (fourcc)
610 		*fourcc = ffmt->fourcc;
611 
612 	if (pad != FIMC_SD_PAD_SOURCE) {
613 		max_w = fimc_fmt_is_user_defined(ffmt->color) ?
614 			pl->scaler_dis_w : pl->scaler_en_w;
615 		/* Apply the camera input interface pixel constraints */
616 		v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
617 				      height, max_t(u32, *height, 32),
618 				      FIMC_CAMIF_MAX_HEIGHT,
619 				      fimc_fmt_is_user_defined(ffmt->color) ?
620 				      3 : 1,
621 				      0);
622 		return ffmt;
623 	}
624 	/* Can't scale or crop in transparent (JPEG) transfer mode */
625 	if (fimc_fmt_is_user_defined(ffmt->color)) {
626 		*width  = ctx->s_frame.f_width;
627 		*height = ctx->s_frame.f_height;
628 		return ffmt;
629 	}
630 	/* Apply the scaler and the output DMA constraints */
631 	max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
632 	if (ctx->state & FIMC_COMPOSE) {
633 		min_w = dst->offs_h + dst->width;
634 		min_h = dst->offs_v + dst->height;
635 	} else {
636 		min_w = var->min_out_pixsize;
637 		min_h = var->min_out_pixsize;
638 	}
639 	if (var->min_vsize_align == 1 && !rotation)
640 		align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
641 
642 	depth = fimc_get_format_depth(ffmt);
643 	v4l_bound_align_image(width, min_w, max_w,
644 			      ffs(var->min_out_pixsize) - 1,
645 			      height, min_h, FIMC_CAMIF_MAX_HEIGHT,
646 			      align_h,
647 			      64/(ALIGN(depth, 8)));
648 
649 	dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
650 	    pad, code ? *code : 0, *width, *height,
651 	    dst->f_width, dst->f_height);
652 
653 	return ffmt;
654 }
655 
fimc_capture_try_selection(struct fimc_ctx * ctx,struct v4l2_rect * r,int target)656 static void fimc_capture_try_selection(struct fimc_ctx *ctx,
657 				       struct v4l2_rect *r,
658 				       int target)
659 {
660 	bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
661 	struct fimc_dev *fimc = ctx->fimc_dev;
662 	const struct fimc_variant *var = fimc->variant;
663 	const struct fimc_pix_limit *pl = var->pix_limit;
664 	struct fimc_frame *sink = &ctx->s_frame;
665 	u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
666 	u32 align_sz = 0, align_h = 4;
667 	u32 max_sc_h, max_sc_v;
668 
669 	/* In JPEG transparent transfer mode cropping is not supported */
670 	if (fimc_fmt_is_user_defined(ctx->d_frame.fmt->color)) {
671 		r->width  = sink->f_width;
672 		r->height = sink->f_height;
673 		r->left   = r->top = 0;
674 		return;
675 	}
676 	if (target == V4L2_SEL_TGT_COMPOSE) {
677 		if (ctx->rotation != 90 && ctx->rotation != 270)
678 			align_h = 1;
679 		max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
680 		max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
681 		min_sz = var->min_out_pixsize;
682 	} else {
683 		u32 depth = fimc_get_format_depth(sink->fmt);
684 		align_sz = 64/ALIGN(depth, 8);
685 		min_sz = var->min_inp_pixsize;
686 		min_w = min_h = min_sz;
687 		max_sc_h = max_sc_v = 1;
688 	}
689 	/*
690 	 * For the compose rectangle the following constraints must be met:
691 	 * - it must fit in the sink pad format rectangle (f_width/f_height);
692 	 * - maximum downscaling ratio is 64;
693 	 * - maximum crop size depends if the rotator is used or not;
694 	 * - the sink pad format width/height must be 4 multiple of the
695 	 *   prescaler ratios determined by sink pad size and source pad crop,
696 	 *   the prescaler ratio is returned by fimc_get_scaler_factor().
697 	 */
698 	max_w = min_t(u32,
699 		      rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
700 		      rotate ? sink->f_height : sink->f_width);
701 	max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
702 
703 	if (target == V4L2_SEL_TGT_COMPOSE) {
704 		min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
705 		min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
706 		if (rotate) {
707 			swap(max_sc_h, max_sc_v);
708 			swap(min_w, min_h);
709 		}
710 	}
711 	v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
712 			      &r->height, min_h, max_h, align_h,
713 			      align_sz);
714 	/* Adjust left/top if crop/compose rectangle is out of bounds */
715 	r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
716 	r->top  = clamp_t(u32, r->top, 0, sink->f_height - r->height);
717 	r->left = round_down(r->left, var->hor_offs_align);
718 
719 	dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
720 	    target, r->left, r->top, r->width, r->height,
721 	    sink->f_width, sink->f_height);
722 }
723 
724 /*
725  * The video node ioctl operations
726  */
fimc_cap_querycap(struct file * file,void * priv,struct v4l2_capability * cap)727 static int fimc_cap_querycap(struct file *file, void *priv,
728 					struct v4l2_capability *cap)
729 {
730 	struct fimc_dev *fimc = video_drvdata(file);
731 
732 	__fimc_vidioc_querycap(&fimc->pdev->dev, cap, V4L2_CAP_STREAMING |
733 					V4L2_CAP_VIDEO_CAPTURE_MPLANE);
734 	return 0;
735 }
736 
fimc_cap_enum_fmt_mplane(struct file * file,void * priv,struct v4l2_fmtdesc * f)737 static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
738 				    struct v4l2_fmtdesc *f)
739 {
740 	struct fimc_fmt *fmt;
741 
742 	fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
743 			       f->index);
744 	if (!fmt)
745 		return -EINVAL;
746 	strncpy(f->description, fmt->name, sizeof(f->description) - 1);
747 	f->pixelformat = fmt->fourcc;
748 	if (fmt->fourcc == MEDIA_BUS_FMT_JPEG_1X8)
749 		f->flags |= V4L2_FMT_FLAG_COMPRESSED;
750 	return 0;
751 }
752 
fimc_pipeline_get_head(struct media_entity * me)753 static struct media_entity *fimc_pipeline_get_head(struct media_entity *me)
754 {
755 	struct media_pad *pad = &me->pads[0];
756 
757 	while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
758 		pad = media_entity_remote_pad(pad);
759 		if (!pad)
760 			break;
761 		me = pad->entity;
762 		pad = &me->pads[0];
763 	}
764 
765 	return me;
766 }
767 
768 /**
769  * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
770  *                            elements
771  * @ctx: FIMC capture context
772  * @tfmt: media bus format to try/set on subdevs
773  * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
774  * @set: true to set format on subdevs, false to try only
775  */
fimc_pipeline_try_format(struct fimc_ctx * ctx,struct v4l2_mbus_framefmt * tfmt,struct fimc_fmt ** fmt_id,bool set)776 static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
777 				    struct v4l2_mbus_framefmt *tfmt,
778 				    struct fimc_fmt **fmt_id,
779 				    bool set)
780 {
781 	struct fimc_dev *fimc = ctx->fimc_dev;
782 	struct fimc_pipeline *p = to_fimc_pipeline(fimc->vid_cap.ve.pipe);
783 	struct v4l2_subdev *sd = p->subdevs[IDX_SENSOR];
784 	struct v4l2_subdev_format sfmt;
785 	struct v4l2_mbus_framefmt *mf = &sfmt.format;
786 	struct media_entity *me;
787 	struct fimc_fmt *ffmt;
788 	struct media_pad *pad;
789 	int ret, i = 1;
790 	u32 fcc;
791 
792 	if (WARN_ON(!sd || !tfmt))
793 		return -EINVAL;
794 
795 	memset(&sfmt, 0, sizeof(sfmt));
796 	sfmt.format = *tfmt;
797 	sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
798 
799 	me = fimc_pipeline_get_head(&sd->entity);
800 
801 	while (1) {
802 		ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
803 					FMT_FLAGS_CAM, i++);
804 		if (ffmt == NULL) {
805 			/*
806 			 * Notify user-space if common pixel code for
807 			 * host and sensor does not exist.
808 			 */
809 			return -EINVAL;
810 		}
811 		mf->code = tfmt->code = ffmt->mbus_code;
812 
813 		/* set format on all pipeline subdevs */
814 		while (me != &fimc->vid_cap.subdev.entity) {
815 			sd = media_entity_to_v4l2_subdev(me);
816 
817 			sfmt.pad = 0;
818 			ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
819 			if (ret)
820 				return ret;
821 
822 			if (me->pads[0].flags & MEDIA_PAD_FL_SINK) {
823 				sfmt.pad = me->num_pads - 1;
824 				mf->code = tfmt->code;
825 				ret = v4l2_subdev_call(sd, pad, set_fmt, NULL,
826 									&sfmt);
827 				if (ret)
828 					return ret;
829 			}
830 
831 			pad = media_entity_remote_pad(&me->pads[sfmt.pad]);
832 			if (!pad)
833 				return -EINVAL;
834 			me = pad->entity;
835 		}
836 
837 		if (mf->code != tfmt->code)
838 			continue;
839 
840 		fcc = ffmt->fourcc;
841 		tfmt->width  = mf->width;
842 		tfmt->height = mf->height;
843 		ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
844 					NULL, &fcc, FIMC_SD_PAD_SINK_CAM);
845 		ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
846 					NULL, &fcc, FIMC_SD_PAD_SOURCE);
847 		if (ffmt && ffmt->mbus_code)
848 			mf->code = ffmt->mbus_code;
849 		if (mf->width != tfmt->width || mf->height != tfmt->height)
850 			continue;
851 		tfmt->code = mf->code;
852 		break;
853 	}
854 
855 	if (fmt_id && ffmt)
856 		*fmt_id = ffmt;
857 	*tfmt = *mf;
858 
859 	return 0;
860 }
861 
862 /**
863  * fimc_get_sensor_frame_desc - query the sensor for media bus frame parameters
864  * @sensor: pointer to the sensor subdev
865  * @plane_fmt: provides plane sizes corresponding to the frame layout entries
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  *
1110  * Return 0 if all formats match or -EPIPE otherwise.
1111  */
fimc_pipeline_validate(struct fimc_dev * fimc)1112 static int fimc_pipeline_validate(struct fimc_dev *fimc)
1113 {
1114 	struct v4l2_subdev_format sink_fmt, src_fmt;
1115 	struct fimc_vid_cap *vc = &fimc->vid_cap;
1116 	struct v4l2_subdev *sd = &vc->subdev;
1117 	struct fimc_pipeline *p = to_fimc_pipeline(vc->ve.pipe);
1118 	struct media_pad *sink_pad, *src_pad;
1119 	int i, ret;
1120 
1121 	while (1) {
1122 		/*
1123 		 * Find current entity sink pad and any remote sink pad linked
1124 		 * to it. We stop if there is no sink pad in current entity or
1125 		 * it is not linked to any other remote entity.
1126 		 */
1127 		src_pad = NULL;
1128 
1129 		for (i = 0; i < sd->entity.num_pads; i++) {
1130 			struct media_pad *p = &sd->entity.pads[i];
1131 
1132 			if (p->flags & MEDIA_PAD_FL_SINK) {
1133 				sink_pad = p;
1134 				src_pad = media_entity_remote_pad(sink_pad);
1135 				if (src_pad)
1136 					break;
1137 			}
1138 		}
1139 
1140 		if (src_pad == NULL ||
1141 		    media_entity_type(src_pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
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_entity_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_entity_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_entity_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_MPLANE)
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 	case V4L2_SEL_TGT_CROP_BOUNDS:
1287 	case V4L2_SEL_TGT_CROP_DEFAULT:
1288 		s->r.left = 0;
1289 		s->r.top = 0;
1290 		s->r.width = f->o_width;
1291 		s->r.height = f->o_height;
1292 		return 0;
1293 
1294 	case V4L2_SEL_TGT_COMPOSE:
1295 		f = &ctx->d_frame;
1296 	case V4L2_SEL_TGT_CROP:
1297 		s->r.left = f->offs_h;
1298 		s->r.top = f->offs_v;
1299 		s->r.width = f->width;
1300 		s->r.height = f->height;
1301 		return 0;
1302 	}
1303 
1304 	return -EINVAL;
1305 }
1306 
1307 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
enclosed_rectangle(struct v4l2_rect * a,struct v4l2_rect * b)1308 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1309 {
1310 	if (a->left < b->left || a->top < b->top)
1311 		return 0;
1312 	if (a->left + a->width > b->left + b->width)
1313 		return 0;
1314 	if (a->top + a->height > b->top + b->height)
1315 		return 0;
1316 
1317 	return 1;
1318 }
1319 
fimc_cap_s_selection(struct file * file,void * fh,struct v4l2_selection * s)1320 static int fimc_cap_s_selection(struct file *file, void *fh,
1321 				struct v4l2_selection *s)
1322 {
1323 	struct fimc_dev *fimc = video_drvdata(file);
1324 	struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1325 	struct v4l2_rect rect = s->r;
1326 	struct fimc_frame *f;
1327 	unsigned long flags;
1328 
1329 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1330 		return -EINVAL;
1331 
1332 	if (s->target == V4L2_SEL_TGT_COMPOSE)
1333 		f = &ctx->d_frame;
1334 	else if (s->target == V4L2_SEL_TGT_CROP)
1335 		f = &ctx->s_frame;
1336 	else
1337 		return -EINVAL;
1338 
1339 	fimc_capture_try_selection(ctx, &rect, s->target);
1340 
1341 	if (s->flags & V4L2_SEL_FLAG_LE &&
1342 	    !enclosed_rectangle(&rect, &s->r))
1343 		return -ERANGE;
1344 
1345 	if (s->flags & V4L2_SEL_FLAG_GE &&
1346 	    !enclosed_rectangle(&s->r, &rect))
1347 		return -ERANGE;
1348 
1349 	s->r = rect;
1350 	spin_lock_irqsave(&fimc->slock, flags);
1351 	set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1352 		       s->r.height);
1353 	spin_unlock_irqrestore(&fimc->slock, flags);
1354 
1355 	set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1356 	return 0;
1357 }
1358 
1359 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1360 	.vidioc_querycap		= fimc_cap_querycap,
1361 
1362 	.vidioc_enum_fmt_vid_cap_mplane	= fimc_cap_enum_fmt_mplane,
1363 	.vidioc_try_fmt_vid_cap_mplane	= fimc_cap_try_fmt_mplane,
1364 	.vidioc_s_fmt_vid_cap_mplane	= fimc_cap_s_fmt_mplane,
1365 	.vidioc_g_fmt_vid_cap_mplane	= fimc_cap_g_fmt_mplane,
1366 
1367 	.vidioc_reqbufs			= fimc_cap_reqbufs,
1368 	.vidioc_querybuf		= vb2_ioctl_querybuf,
1369 	.vidioc_qbuf			= vb2_ioctl_qbuf,
1370 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
1371 	.vidioc_expbuf			= vb2_ioctl_expbuf,
1372 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
1373 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
1374 
1375 	.vidioc_streamon		= fimc_cap_streamon,
1376 	.vidioc_streamoff		= fimc_cap_streamoff,
1377 
1378 	.vidioc_g_selection		= fimc_cap_g_selection,
1379 	.vidioc_s_selection		= fimc_cap_s_selection,
1380 
1381 	.vidioc_enum_input		= fimc_cap_enum_input,
1382 	.vidioc_s_input			= fimc_cap_s_input,
1383 	.vidioc_g_input			= fimc_cap_g_input,
1384 };
1385 
1386 /* Capture subdev media entity operations */
fimc_link_setup(struct media_entity * entity,const struct media_pad * local,const struct media_pad * remote,u32 flags)1387 static int fimc_link_setup(struct media_entity *entity,
1388 			   const struct media_pad *local,
1389 			   const struct media_pad *remote, u32 flags)
1390 {
1391 	struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1392 	struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1393 	struct fimc_vid_cap *vc = &fimc->vid_cap;
1394 	struct v4l2_subdev *sensor;
1395 
1396 	if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1397 		return -EINVAL;
1398 
1399 	if (WARN_ON(fimc == NULL))
1400 		return 0;
1401 
1402 	dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1403 	    local->entity->name, remote->entity->name, flags,
1404 	    fimc->vid_cap.input);
1405 
1406 	if (!(flags & MEDIA_LNK_FL_ENABLED)) {
1407 		fimc->vid_cap.input = 0;
1408 		return 0;
1409 	}
1410 
1411 	if (vc->input != 0)
1412 		return -EBUSY;
1413 
1414 	vc->input = sd->grp_id;
1415 
1416 	if (vc->user_subdev_api || vc->inh_sensor_ctrls)
1417 		return 0;
1418 
1419 	/* Inherit V4L2 controls from the image sensor subdev. */
1420 	sensor = fimc_find_remote_sensor(&vc->subdev.entity);
1421 	if (sensor == NULL)
1422 		return 0;
1423 
1424 	return v4l2_ctrl_add_handler(&vc->ctx->ctrls.handler,
1425 				     sensor->ctrl_handler, NULL);
1426 }
1427 
1428 static const struct media_entity_operations fimc_sd_media_ops = {
1429 	.link_setup = fimc_link_setup,
1430 };
1431 
1432 /**
1433  * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1434  * @sd: pointer to a subdev generating the notification
1435  * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1436  * @arg: pointer to an u32 type integer that stores the frame payload value
1437  *
1438  * The End Of Frame notification sent by sensor subdev in its still capture
1439  * mode. If there is only a single VSYNC generated by the sensor at the
1440  * beginning of a frame transmission, FIMC does not issue the LastIrq
1441  * (end of frame) interrupt. And this notification is used to complete the
1442  * frame capture and returning a buffer to user-space. Subdev drivers should
1443  * call this notification from their last 'End of frame capture' interrupt.
1444  */
fimc_sensor_notify(struct v4l2_subdev * sd,unsigned int notification,void * arg)1445 void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1446 			void *arg)
1447 {
1448 	struct fimc_source_info	*si;
1449 	struct fimc_vid_buffer *buf;
1450 	struct fimc_md *fmd;
1451 	struct fimc_dev *fimc;
1452 	unsigned long flags;
1453 
1454 	if (sd == NULL)
1455 		return;
1456 
1457 	si = v4l2_get_subdev_hostdata(sd);
1458 	fmd = entity_to_fimc_mdev(&sd->entity);
1459 
1460 	spin_lock_irqsave(&fmd->slock, flags);
1461 
1462 	fimc = si ? source_to_sensor_info(si)->host : NULL;
1463 
1464 	if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1465 	    test_bit(ST_CAPT_PEND, &fimc->state)) {
1466 		unsigned long irq_flags;
1467 		spin_lock_irqsave(&fimc->slock, irq_flags);
1468 		if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1469 			buf = list_entry(fimc->vid_cap.active_buf_q.next,
1470 					 struct fimc_vid_buffer, list);
1471 			vb2_set_plane_payload(&buf->vb.vb2_buf, 0,
1472 					      *((u32 *)arg));
1473 		}
1474 		fimc_capture_irq_handler(fimc, 1);
1475 		fimc_deactivate_capture(fimc);
1476 		spin_unlock_irqrestore(&fimc->slock, irq_flags);
1477 	}
1478 	spin_unlock_irqrestore(&fmd->slock, flags);
1479 }
1480 
fimc_subdev_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)1481 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1482 				      struct v4l2_subdev_pad_config *cfg,
1483 				      struct v4l2_subdev_mbus_code_enum *code)
1484 {
1485 	struct fimc_fmt *fmt;
1486 
1487 	fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1488 	if (!fmt)
1489 		return -EINVAL;
1490 	code->code = fmt->mbus_code;
1491 	return 0;
1492 }
1493 
fimc_subdev_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)1494 static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1495 			       struct v4l2_subdev_pad_config *cfg,
1496 			       struct v4l2_subdev_format *fmt)
1497 {
1498 	struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1499 	struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1500 	struct fimc_frame *ff = &ctx->s_frame;
1501 	struct v4l2_mbus_framefmt *mf;
1502 
1503 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1504 		mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1505 		fmt->format = *mf;
1506 		return 0;
1507 	}
1508 
1509 	mf = &fmt->format;
1510 	mutex_lock(&fimc->lock);
1511 
1512 	switch (fmt->pad) {
1513 	case FIMC_SD_PAD_SOURCE:
1514 		if (!WARN_ON(ff->fmt == NULL))
1515 			mf->code = ff->fmt->mbus_code;
1516 		/* Sink pads crop rectangle size */
1517 		mf->width = ff->width;
1518 		mf->height = ff->height;
1519 		break;
1520 	case FIMC_SD_PAD_SINK_FIFO:
1521 		*mf = fimc->vid_cap.wb_fmt;
1522 		break;
1523 	case FIMC_SD_PAD_SINK_CAM:
1524 	default:
1525 		*mf = fimc->vid_cap.ci_fmt;
1526 		break;
1527 	}
1528 
1529 	mutex_unlock(&fimc->lock);
1530 	mf->colorspace = V4L2_COLORSPACE_JPEG;
1531 
1532 	return 0;
1533 }
1534 
fimc_subdev_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)1535 static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1536 			       struct v4l2_subdev_pad_config *cfg,
1537 			       struct v4l2_subdev_format *fmt)
1538 {
1539 	struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1540 	struct v4l2_mbus_framefmt *mf = &fmt->format;
1541 	struct fimc_vid_cap *vc = &fimc->vid_cap;
1542 	struct fimc_ctx *ctx = vc->ctx;
1543 	struct fimc_frame *ff;
1544 	struct fimc_fmt *ffmt;
1545 
1546 	dbg("pad%d: code: 0x%x, %dx%d",
1547 	    fmt->pad, mf->code, mf->width, mf->height);
1548 
1549 	if (fmt->pad == FIMC_SD_PAD_SOURCE && vb2_is_busy(&vc->vbq))
1550 		return -EBUSY;
1551 
1552 	mutex_lock(&fimc->lock);
1553 	ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1554 				       &mf->code, NULL, fmt->pad);
1555 	mutex_unlock(&fimc->lock);
1556 	mf->colorspace = V4L2_COLORSPACE_JPEG;
1557 
1558 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1559 		mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1560 		*mf = fmt->format;
1561 		return 0;
1562 	}
1563 	/* There must be a bug in the driver if this happens */
1564 	if (WARN_ON(ffmt == NULL))
1565 		return -EINVAL;
1566 
1567 	/* Update RGB Alpha control state and value range */
1568 	fimc_alpha_ctrl_update(ctx);
1569 
1570 	fimc_capture_mark_jpeg_xfer(ctx, ffmt->color);
1571 	if (fmt->pad == FIMC_SD_PAD_SOURCE) {
1572 		ff = &ctx->d_frame;
1573 		/* Sink pads crop rectangle size */
1574 		mf->width = ctx->s_frame.width;
1575 		mf->height = ctx->s_frame.height;
1576 	} else {
1577 		ff = &ctx->s_frame;
1578 	}
1579 
1580 	mutex_lock(&fimc->lock);
1581 	set_frame_bounds(ff, mf->width, mf->height);
1582 
1583 	if (fmt->pad == FIMC_SD_PAD_SINK_FIFO)
1584 		vc->wb_fmt = *mf;
1585 	else if (fmt->pad == FIMC_SD_PAD_SINK_CAM)
1586 		vc->ci_fmt = *mf;
1587 
1588 	ff->fmt = ffmt;
1589 
1590 	/* Reset the crop rectangle if required. */
1591 	if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_COMPOSE)))
1592 		set_frame_crop(ff, 0, 0, mf->width, mf->height);
1593 
1594 	if (fmt->pad != FIMC_SD_PAD_SOURCE)
1595 		ctx->state &= ~FIMC_COMPOSE;
1596 
1597 	mutex_unlock(&fimc->lock);
1598 	return 0;
1599 }
1600 
fimc_subdev_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)1601 static int fimc_subdev_get_selection(struct v4l2_subdev *sd,
1602 				     struct v4l2_subdev_pad_config *cfg,
1603 				     struct v4l2_subdev_selection *sel)
1604 {
1605 	struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1606 	struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1607 	struct fimc_frame *f = &ctx->s_frame;
1608 	struct v4l2_rect *r = &sel->r;
1609 	struct v4l2_rect *try_sel;
1610 
1611 	if (sel->pad == FIMC_SD_PAD_SOURCE)
1612 		return -EINVAL;
1613 
1614 	mutex_lock(&fimc->lock);
1615 
1616 	switch (sel->target) {
1617 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1618 		f = &ctx->d_frame;
1619 	case V4L2_SEL_TGT_CROP_BOUNDS:
1620 		r->width = f->o_width;
1621 		r->height = f->o_height;
1622 		r->left = 0;
1623 		r->top = 0;
1624 		mutex_unlock(&fimc->lock);
1625 		return 0;
1626 
1627 	case V4L2_SEL_TGT_CROP:
1628 		try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
1629 		break;
1630 	case V4L2_SEL_TGT_COMPOSE:
1631 		try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
1632 		f = &ctx->d_frame;
1633 		break;
1634 	default:
1635 		mutex_unlock(&fimc->lock);
1636 		return -EINVAL;
1637 	}
1638 
1639 	if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1640 		sel->r = *try_sel;
1641 	} else {
1642 		r->left = f->offs_h;
1643 		r->top = f->offs_v;
1644 		r->width = f->width;
1645 		r->height = f->height;
1646 	}
1647 
1648 	dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1649 	    sel->pad, r->left, r->top, r->width, r->height,
1650 	    f->f_width, f->f_height);
1651 
1652 	mutex_unlock(&fimc->lock);
1653 	return 0;
1654 }
1655 
fimc_subdev_set_selection(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)1656 static int fimc_subdev_set_selection(struct v4l2_subdev *sd,
1657 				     struct v4l2_subdev_pad_config *cfg,
1658 				     struct v4l2_subdev_selection *sel)
1659 {
1660 	struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1661 	struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1662 	struct fimc_frame *f = &ctx->s_frame;
1663 	struct v4l2_rect *r = &sel->r;
1664 	struct v4l2_rect *try_sel;
1665 	unsigned long flags;
1666 
1667 	if (sel->pad == FIMC_SD_PAD_SOURCE)
1668 		return -EINVAL;
1669 
1670 	mutex_lock(&fimc->lock);
1671 	fimc_capture_try_selection(ctx, r, V4L2_SEL_TGT_CROP);
1672 
1673 	switch (sel->target) {
1674 	case V4L2_SEL_TGT_CROP:
1675 		try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
1676 		break;
1677 	case V4L2_SEL_TGT_COMPOSE:
1678 		try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
1679 		f = &ctx->d_frame;
1680 		break;
1681 	default:
1682 		mutex_unlock(&fimc->lock);
1683 		return -EINVAL;
1684 	}
1685 
1686 	if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1687 		*try_sel = sel->r;
1688 	} else {
1689 		spin_lock_irqsave(&fimc->slock, flags);
1690 		set_frame_crop(f, r->left, r->top, r->width, r->height);
1691 		set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1692 		if (sel->target == V4L2_SEL_TGT_COMPOSE)
1693 			ctx->state |= FIMC_COMPOSE;
1694 		spin_unlock_irqrestore(&fimc->slock, flags);
1695 	}
1696 
1697 	dbg("target %#x: (%d,%d)/%dx%d", sel->target, r->left, r->top,
1698 	    r->width, r->height);
1699 
1700 	mutex_unlock(&fimc->lock);
1701 	return 0;
1702 }
1703 
1704 static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1705 	.enum_mbus_code = fimc_subdev_enum_mbus_code,
1706 	.get_selection = fimc_subdev_get_selection,
1707 	.set_selection = fimc_subdev_set_selection,
1708 	.get_fmt = fimc_subdev_get_fmt,
1709 	.set_fmt = fimc_subdev_set_fmt,
1710 };
1711 
1712 static struct v4l2_subdev_ops fimc_subdev_ops = {
1713 	.pad = &fimc_subdev_pad_ops,
1714 };
1715 
1716 /* Set default format at the sensor and host interface */
fimc_capture_set_default_format(struct fimc_dev * fimc)1717 static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1718 {
1719 	struct v4l2_format fmt = {
1720 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1721 		.fmt.pix_mp = {
1722 			.width		= FIMC_DEFAULT_WIDTH,
1723 			.height		= FIMC_DEFAULT_HEIGHT,
1724 			.pixelformat	= V4L2_PIX_FMT_YUYV,
1725 			.field		= V4L2_FIELD_NONE,
1726 			.colorspace	= V4L2_COLORSPACE_JPEG,
1727 		},
1728 	};
1729 
1730 	return __fimc_capture_set_format(fimc, &fmt);
1731 }
1732 
1733 /* fimc->lock must be already initialized */
fimc_register_capture_device(struct fimc_dev * fimc,struct v4l2_device * v4l2_dev)1734 static int fimc_register_capture_device(struct fimc_dev *fimc,
1735 				 struct v4l2_device *v4l2_dev)
1736 {
1737 	struct video_device *vfd = &fimc->vid_cap.ve.vdev;
1738 	struct vb2_queue *q = &fimc->vid_cap.vbq;
1739 	struct fimc_ctx *ctx;
1740 	struct fimc_vid_cap *vid_cap;
1741 	struct fimc_fmt *fmt;
1742 	int ret = -ENOMEM;
1743 
1744 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1745 	if (!ctx)
1746 		return -ENOMEM;
1747 
1748 	ctx->fimc_dev	 = fimc;
1749 	ctx->in_path	 = FIMC_IO_CAMERA;
1750 	ctx->out_path	 = FIMC_IO_DMA;
1751 	ctx->state	 = FIMC_CTX_CAP;
1752 	ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1753 	ctx->d_frame.fmt = ctx->s_frame.fmt;
1754 
1755 	memset(vfd, 0, sizeof(*vfd));
1756 	snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
1757 
1758 	vfd->fops	= &fimc_capture_fops;
1759 	vfd->ioctl_ops	= &fimc_capture_ioctl_ops;
1760 	vfd->v4l2_dev	= v4l2_dev;
1761 	vfd->minor	= -1;
1762 	vfd->release	= video_device_release_empty;
1763 	vfd->queue	= q;
1764 	vfd->lock	= &fimc->lock;
1765 
1766 	video_set_drvdata(vfd, fimc);
1767 	vid_cap = &fimc->vid_cap;
1768 	vid_cap->active_buf_cnt = 0;
1769 	vid_cap->reqbufs_count = 0;
1770 	vid_cap->ctx = ctx;
1771 
1772 	INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1773 	INIT_LIST_HEAD(&vid_cap->active_buf_q);
1774 
1775 	memset(q, 0, sizeof(*q));
1776 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1777 	q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1778 	q->drv_priv = ctx;
1779 	q->ops = &fimc_capture_qops;
1780 	q->mem_ops = &vb2_dma_contig_memops;
1781 	q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1782 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1783 	q->lock = &fimc->lock;
1784 
1785 	ret = vb2_queue_init(q);
1786 	if (ret)
1787 		goto err_free_ctx;
1788 
1789 	/* Default format configuration */
1790 	fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1791 	vid_cap->ci_fmt.width = FIMC_DEFAULT_WIDTH;
1792 	vid_cap->ci_fmt.height = FIMC_DEFAULT_HEIGHT;
1793 	vid_cap->ci_fmt.code = fmt->mbus_code;
1794 
1795 	ctx->s_frame.width = FIMC_DEFAULT_WIDTH;
1796 	ctx->s_frame.height = FIMC_DEFAULT_HEIGHT;
1797 	ctx->s_frame.fmt = fmt;
1798 
1799 	fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_WRITEBACK, 0);
1800 	vid_cap->wb_fmt = vid_cap->ci_fmt;
1801 	vid_cap->wb_fmt.code = fmt->mbus_code;
1802 
1803 	vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
1804 	ret = media_entity_init(&vfd->entity, 1, &vid_cap->vd_pad, 0);
1805 	if (ret)
1806 		goto err_free_ctx;
1807 
1808 	ret = fimc_ctrls_create(ctx);
1809 	if (ret)
1810 		goto err_me_cleanup;
1811 
1812 	ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1813 	if (ret)
1814 		goto err_ctrl_free;
1815 
1816 	v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1817 		  vfd->name, video_device_node_name(vfd));
1818 
1819 	vfd->ctrl_handler = &ctx->ctrls.handler;
1820 	return 0;
1821 
1822 err_ctrl_free:
1823 	fimc_ctrls_delete(ctx);
1824 err_me_cleanup:
1825 	media_entity_cleanup(&vfd->entity);
1826 err_free_ctx:
1827 	kfree(ctx);
1828 	return ret;
1829 }
1830 
fimc_capture_subdev_registered(struct v4l2_subdev * sd)1831 static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
1832 {
1833 	struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1834 	int ret;
1835 
1836 	if (fimc == NULL)
1837 		return -ENXIO;
1838 
1839 	ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1840 	if (ret)
1841 		return ret;
1842 
1843 	fimc->vid_cap.ve.pipe = v4l2_get_subdev_hostdata(sd);
1844 
1845 	ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
1846 	if (ret) {
1847 		fimc_unregister_m2m_device(fimc);
1848 		fimc->vid_cap.ve.pipe = NULL;
1849 	}
1850 
1851 	return ret;
1852 }
1853 
fimc_capture_subdev_unregistered(struct v4l2_subdev * sd)1854 static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1855 {
1856 	struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1857 	struct video_device *vdev;
1858 
1859 	if (fimc == NULL)
1860 		return;
1861 
1862 	mutex_lock(&fimc->lock);
1863 
1864 	fimc_unregister_m2m_device(fimc);
1865 	vdev = &fimc->vid_cap.ve.vdev;
1866 
1867 	if (video_is_registered(vdev)) {
1868 		video_unregister_device(vdev);
1869 		media_entity_cleanup(&vdev->entity);
1870 		fimc_ctrls_delete(fimc->vid_cap.ctx);
1871 		fimc->vid_cap.ve.pipe = NULL;
1872 	}
1873 	kfree(fimc->vid_cap.ctx);
1874 	fimc->vid_cap.ctx = NULL;
1875 
1876 	mutex_unlock(&fimc->lock);
1877 }
1878 
1879 static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1880 	.registered = fimc_capture_subdev_registered,
1881 	.unregistered = fimc_capture_subdev_unregistered,
1882 };
1883 
fimc_initialize_capture_subdev(struct fimc_dev * fimc)1884 int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1885 {
1886 	struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1887 	int ret;
1888 
1889 	v4l2_subdev_init(sd, &fimc_subdev_ops);
1890 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1891 	snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->id);
1892 
1893 	fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_CAM].flags = MEDIA_PAD_FL_SINK;
1894 	fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_FIFO].flags = MEDIA_PAD_FL_SINK;
1895 	fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1896 	ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1897 				fimc->vid_cap.sd_pads, 0);
1898 	if (ret)
1899 		return ret;
1900 
1901 	sd->entity.ops = &fimc_sd_media_ops;
1902 	sd->internal_ops = &fimc_capture_sd_internal_ops;
1903 	v4l2_set_subdevdata(sd, fimc);
1904 	return 0;
1905 }
1906 
fimc_unregister_capture_subdev(struct fimc_dev * fimc)1907 void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1908 {
1909 	struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1910 
1911 	v4l2_device_unregister_subdev(sd);
1912 	media_entity_cleanup(&sd->entity);
1913 	v4l2_set_subdevdata(sd, NULL);
1914 }
1915