xref: /linux/drivers/media/platform/samsung/s3c-camif/camif-capture.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * s3c24xx/s3c64xx SoC series Camera Interface (CAMIF) driver
4  *
5  * Copyright (C) 2012 Sylwester Nawrocki <sylvester.nawrocki@gmail.com>
6  * Copyright (C) 2012 Tomasz Figa <tomasz.figa@gmail.com>
7  *
8  * Based on drivers/media/platform/s5p-fimc,
9  * Copyright (C) 2010 - 2012 Samsung Electronics Co., Ltd.
10 */
11 #define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__
12 
13 #include <linux/bug.h>
14 #include <linux/clk.h>
15 #include <linux/device.h>
16 #include <linux/errno.h>
17 #include <linux/i2c.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/kernel.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/ratelimit.h>
26 #include <linux/slab.h>
27 #include <linux/types.h>
28 #include <linux/videodev2.h>
29 
30 #include <media/media-device.h>
31 #include <media/v4l2-ctrls.h>
32 #include <media/v4l2-event.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/videobuf2-v4l2.h>
35 #include <media/videobuf2-dma-contig.h>
36 
37 #include "camif-core.h"
38 #include "camif-regs.h"
39 
40 static int debug;
41 module_param(debug, int, 0644);
42 
43 /* Locking: called with vp->camif->slock spinlock held */
44 static void camif_cfg_video_path(struct camif_vp *vp)
45 {
46 	WARN_ON(s3c_camif_get_scaler_config(vp, &vp->scaler));
47 	camif_hw_set_scaler(vp);
48 	camif_hw_set_flip(vp);
49 	camif_hw_set_target_format(vp);
50 	camif_hw_set_output_dma(vp);
51 }
52 
53 static void camif_prepare_dma_offset(struct camif_vp *vp)
54 {
55 	struct camif_frame *f = &vp->out_frame;
56 
57 	f->dma_offset.initial = f->rect.top * f->f_width + f->rect.left;
58 	f->dma_offset.line = f->f_width - (f->rect.left + f->rect.width);
59 
60 	pr_debug("dma_offset: initial: %d, line: %d\n",
61 		 f->dma_offset.initial, f->dma_offset.line);
62 }
63 
64 /* Locking: called with camif->slock spinlock held */
65 static int s3c_camif_hw_init(struct camif_dev *camif, struct camif_vp *vp)
66 {
67 	const struct s3c_camif_variant *variant = camif->variant;
68 
69 	if (camif->sensor.sd == NULL || vp->out_fmt == NULL)
70 		return -EINVAL;
71 
72 	if (variant->ip_revision == S3C244X_CAMIF_IP_REV)
73 		camif_hw_clear_fifo_overflow(vp);
74 	camif_hw_set_camera_bus(camif);
75 	camif_hw_set_source_format(camif);
76 	camif_hw_set_camera_crop(camif);
77 	camif_hw_set_test_pattern(camif, camif->test_pattern);
78 	if (variant->has_img_effect)
79 		camif_hw_set_effect(camif, camif->colorfx,
80 				camif->colorfx_cr, camif->colorfx_cb);
81 	if (variant->ip_revision == S3C6410_CAMIF_IP_REV)
82 		camif_hw_set_input_path(vp);
83 	camif_cfg_video_path(vp);
84 	vp->state &= ~ST_VP_CONFIG;
85 
86 	return 0;
87 }
88 
89 /*
90  * Initialize the video path, only up from the scaler stage. The camera
91  * input interface set up is skipped. This is useful to enable one of the
92  * video paths when the other is already running.
93  * Locking: called with camif->slock spinlock held.
94  */
95 static int s3c_camif_hw_vp_init(struct camif_dev *camif, struct camif_vp *vp)
96 {
97 	unsigned int ip_rev = camif->variant->ip_revision;
98 
99 	if (vp->out_fmt == NULL)
100 		return -EINVAL;
101 
102 	camif_prepare_dma_offset(vp);
103 	if (ip_rev == S3C244X_CAMIF_IP_REV)
104 		camif_hw_clear_fifo_overflow(vp);
105 	camif_cfg_video_path(vp);
106 	vp->state &= ~ST_VP_CONFIG;
107 	return 0;
108 }
109 
110 static int sensor_set_power(struct camif_dev *camif, int on)
111 {
112 	struct cam_sensor *sensor = &camif->sensor;
113 	int err = 0;
114 
115 	if (camif->sensor.power_count == !on)
116 		err = v4l2_subdev_call(sensor->sd, core, s_power, on);
117 	if (err == -ENOIOCTLCMD)
118 		err = 0;
119 	if (!err)
120 		sensor->power_count += on ? 1 : -1;
121 
122 	pr_debug("on: %d, power_count: %d, err: %d\n",
123 		 on, sensor->power_count, err);
124 
125 	return err;
126 }
127 
128 static int sensor_set_streaming(struct camif_dev *camif, int on)
129 {
130 	struct cam_sensor *sensor = &camif->sensor;
131 	int err = 0;
132 
133 	if (camif->sensor.stream_count == !on)
134 		err = v4l2_subdev_call(sensor->sd, video, s_stream, on);
135 	if (!err)
136 		sensor->stream_count += on ? 1 : -1;
137 
138 	pr_debug("on: %d, stream_count: %d, err: %d\n",
139 		 on, sensor->stream_count, err);
140 
141 	return err;
142 }
143 
144 /*
145  * Reinitialize the driver so it is ready to start streaming again.
146  * Return any buffers to vb2, perform CAMIF software reset and
147  * turn off streaming at the data pipeline (sensor) if required.
148  */
149 static int camif_reinitialize(struct camif_vp *vp)
150 {
151 	struct camif_dev *camif = vp->camif;
152 	struct camif_buffer *buf;
153 	unsigned long flags;
154 	bool streaming;
155 
156 	spin_lock_irqsave(&camif->slock, flags);
157 	streaming = vp->state & ST_VP_SENSOR_STREAMING;
158 
159 	vp->state &= ~(ST_VP_PENDING | ST_VP_RUNNING | ST_VP_OFF |
160 		       ST_VP_ABORTING | ST_VP_STREAMING |
161 		       ST_VP_SENSOR_STREAMING | ST_VP_LASTIRQ);
162 
163 	/* Release unused buffers */
164 	while (!list_empty(&vp->pending_buf_q)) {
165 		buf = camif_pending_queue_pop(vp);
166 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
167 	}
168 
169 	while (!list_empty(&vp->active_buf_q)) {
170 		buf = camif_active_queue_pop(vp);
171 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
172 	}
173 
174 	spin_unlock_irqrestore(&camif->slock, flags);
175 
176 	if (!streaming)
177 		return 0;
178 
179 	return sensor_set_streaming(camif, 0);
180 }
181 
182 static bool s3c_vp_active(struct camif_vp *vp)
183 {
184 	struct camif_dev *camif = vp->camif;
185 	unsigned long flags;
186 	bool ret;
187 
188 	spin_lock_irqsave(&camif->slock, flags);
189 	ret = (vp->state & ST_VP_RUNNING) || (vp->state & ST_VP_PENDING);
190 	spin_unlock_irqrestore(&camif->slock, flags);
191 
192 	return ret;
193 }
194 
195 static bool camif_is_streaming(struct camif_dev *camif)
196 {
197 	unsigned long flags;
198 	bool status;
199 
200 	spin_lock_irqsave(&camif->slock, flags);
201 	status = camif->stream_count > 0;
202 	spin_unlock_irqrestore(&camif->slock, flags);
203 
204 	return status;
205 }
206 
207 static int camif_stop_capture(struct camif_vp *vp)
208 {
209 	struct camif_dev *camif = vp->camif;
210 	unsigned long flags;
211 	int ret;
212 
213 	if (!s3c_vp_active(vp))
214 		return 0;
215 
216 	spin_lock_irqsave(&camif->slock, flags);
217 	vp->state &= ~(ST_VP_OFF | ST_VP_LASTIRQ);
218 	vp->state |= ST_VP_ABORTING;
219 	spin_unlock_irqrestore(&camif->slock, flags);
220 
221 	ret = wait_event_timeout(vp->irq_queue,
222 			   !(vp->state & ST_VP_ABORTING),
223 			   msecs_to_jiffies(CAMIF_STOP_TIMEOUT));
224 
225 	spin_lock_irqsave(&camif->slock, flags);
226 
227 	if (ret == 0 && !(vp->state & ST_VP_OFF)) {
228 		/* Timed out, forcibly stop capture */
229 		vp->state &= ~(ST_VP_OFF | ST_VP_ABORTING |
230 			       ST_VP_LASTIRQ);
231 
232 		camif_hw_disable_capture(vp);
233 		camif_hw_enable_scaler(vp, false);
234 	}
235 
236 	spin_unlock_irqrestore(&camif->slock, flags);
237 
238 	return camif_reinitialize(vp);
239 }
240 
241 static int camif_prepare_addr(struct camif_vp *vp, struct vb2_buffer *vb,
242 			      struct camif_addr *paddr)
243 {
244 	struct camif_frame *frame = &vp->out_frame;
245 	u32 pix_size;
246 
247 	if (vb == NULL || frame == NULL)
248 		return -EINVAL;
249 
250 	pix_size = frame->rect.width * frame->rect.height;
251 
252 	pr_debug("colplanes: %d, pix_size: %u\n",
253 		 vp->out_fmt->colplanes, pix_size);
254 
255 	paddr->y = vb2_dma_contig_plane_dma_addr(vb, 0);
256 
257 	switch (vp->out_fmt->colplanes) {
258 	case 1:
259 		paddr->cb = 0;
260 		paddr->cr = 0;
261 		break;
262 	case 2:
263 		/* decompose Y into Y/Cb */
264 		paddr->cb = (u32)(paddr->y + pix_size);
265 		paddr->cr = 0;
266 		break;
267 	case 3:
268 		paddr->cb = (u32)(paddr->y + pix_size);
269 		/* decompose Y into Y/Cb/Cr */
270 		if (vp->out_fmt->color == IMG_FMT_YCBCR422P)
271 			paddr->cr = (u32)(paddr->cb + (pix_size >> 1));
272 		else /* 420 */
273 			paddr->cr = (u32)(paddr->cb + (pix_size >> 2));
274 
275 		if (vp->out_fmt->color == IMG_FMT_YCRCB420)
276 			swap(paddr->cb, paddr->cr);
277 		break;
278 	default:
279 		return -EINVAL;
280 	}
281 
282 	pr_debug("DMA address: y: %pad  cb: %pad cr: %pad\n",
283 		 &paddr->y, &paddr->cb, &paddr->cr);
284 
285 	return 0;
286 }
287 
288 irqreturn_t s3c_camif_irq_handler(int irq, void *priv)
289 {
290 	struct camif_vp *vp = priv;
291 	struct camif_dev *camif = vp->camif;
292 	unsigned int ip_rev = camif->variant->ip_revision;
293 	unsigned int status;
294 
295 	spin_lock(&camif->slock);
296 
297 	if (ip_rev == S3C6410_CAMIF_IP_REV)
298 		camif_hw_clear_pending_irq(vp);
299 
300 	status = camif_hw_get_status(vp);
301 
302 	if (ip_rev == S3C244X_CAMIF_IP_REV && (status & CISTATUS_OVF_MASK)) {
303 		camif_hw_clear_fifo_overflow(vp);
304 		goto unlock;
305 	}
306 
307 	if (vp->state & ST_VP_ABORTING) {
308 		if (vp->state & ST_VP_OFF) {
309 			/* Last IRQ */
310 			vp->state &= ~(ST_VP_OFF | ST_VP_ABORTING |
311 				       ST_VP_LASTIRQ);
312 			wake_up(&vp->irq_queue);
313 			goto unlock;
314 		} else if (vp->state & ST_VP_LASTIRQ) {
315 			camif_hw_disable_capture(vp);
316 			camif_hw_enable_scaler(vp, false);
317 			camif_hw_set_lastirq(vp, false);
318 			vp->state |= ST_VP_OFF;
319 		} else {
320 			/* Disable capture, enable last IRQ */
321 			camif_hw_set_lastirq(vp, true);
322 			vp->state |= ST_VP_LASTIRQ;
323 		}
324 	}
325 
326 	if (!list_empty(&vp->pending_buf_q) && (vp->state & ST_VP_RUNNING) &&
327 	    !list_empty(&vp->active_buf_q)) {
328 		unsigned int index;
329 		struct camif_buffer *vbuf;
330 		/*
331 		 * Get previous DMA write buffer index:
332 		 * 0 => DMA buffer 0, 2;
333 		 * 1 => DMA buffer 1, 3.
334 		 */
335 		index = (CISTATUS_FRAMECNT(status) + 2) & 1;
336 		vbuf = camif_active_queue_peek(vp, index);
337 
338 		if (!WARN_ON(vbuf == NULL)) {
339 			/* Dequeue a filled buffer */
340 			vbuf->vb.vb2_buf.timestamp = ktime_get_ns();
341 			vbuf->vb.sequence = vp->frame_sequence++;
342 			vb2_buffer_done(&vbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);
343 
344 			/* Set up an empty buffer at the DMA engine */
345 			vbuf = camif_pending_queue_pop(vp);
346 			vbuf->index = index;
347 			camif_hw_set_output_addr(vp, &vbuf->paddr, index);
348 			camif_hw_set_output_addr(vp, &vbuf->paddr, index + 2);
349 
350 			/* Scheduled in H/W, add to the queue */
351 			camif_active_queue_add(vp, vbuf);
352 		}
353 	} else if (!(vp->state & ST_VP_ABORTING) &&
354 		   (vp->state & ST_VP_PENDING))  {
355 		vp->state |= ST_VP_RUNNING;
356 	}
357 
358 	if (vp->state & ST_VP_CONFIG) {
359 		camif_prepare_dma_offset(vp);
360 		camif_hw_set_camera_crop(camif);
361 		camif_hw_set_scaler(vp);
362 		camif_hw_set_flip(vp);
363 		camif_hw_set_test_pattern(camif, camif->test_pattern);
364 		if (camif->variant->has_img_effect)
365 			camif_hw_set_effect(camif, camif->colorfx,
366 				    camif->colorfx_cr, camif->colorfx_cb);
367 		vp->state &= ~ST_VP_CONFIG;
368 	}
369 unlock:
370 	spin_unlock(&camif->slock);
371 	return IRQ_HANDLED;
372 }
373 
374 static int start_streaming(struct vb2_queue *vq, unsigned int count)
375 {
376 	struct camif_vp *vp = vb2_get_drv_priv(vq);
377 	struct camif_dev *camif = vp->camif;
378 	unsigned long flags;
379 	int ret;
380 
381 	/*
382 	 * We assume the codec capture path is always activated
383 	 * first, before the preview path starts streaming.
384 	 * This is required to avoid internal FIFO overflow and
385 	 * a need for CAMIF software reset.
386 	 */
387 	spin_lock_irqsave(&camif->slock, flags);
388 
389 	if (camif->stream_count == 0) {
390 		camif_hw_reset(camif);
391 		ret = s3c_camif_hw_init(camif, vp);
392 	} else {
393 		ret = s3c_camif_hw_vp_init(camif, vp);
394 	}
395 	spin_unlock_irqrestore(&camif->slock, flags);
396 
397 	if (ret < 0) {
398 		camif_reinitialize(vp);
399 		return ret;
400 	}
401 
402 	spin_lock_irqsave(&camif->slock, flags);
403 	vp->frame_sequence = 0;
404 	vp->state |= ST_VP_PENDING;
405 
406 	if (!list_empty(&vp->pending_buf_q) &&
407 	    (!(vp->state & ST_VP_STREAMING) ||
408 	     !(vp->state & ST_VP_SENSOR_STREAMING))) {
409 
410 		camif_hw_enable_scaler(vp, vp->scaler.enable);
411 		camif_hw_enable_capture(vp);
412 		vp->state |= ST_VP_STREAMING;
413 
414 		if (!(vp->state & ST_VP_SENSOR_STREAMING)) {
415 			vp->state |= ST_VP_SENSOR_STREAMING;
416 			spin_unlock_irqrestore(&camif->slock, flags);
417 			ret = sensor_set_streaming(camif, 1);
418 			if (ret)
419 				v4l2_err(&vp->vdev, "Sensor s_stream failed\n");
420 			if (debug)
421 				camif_hw_dump_regs(camif, __func__);
422 
423 			return ret;
424 		}
425 	}
426 
427 	spin_unlock_irqrestore(&camif->slock, flags);
428 	return 0;
429 }
430 
431 static void stop_streaming(struct vb2_queue *vq)
432 {
433 	struct camif_vp *vp = vb2_get_drv_priv(vq);
434 	camif_stop_capture(vp);
435 }
436 
437 static int queue_setup(struct vb2_queue *vq,
438 		       unsigned int *num_buffers, unsigned int *num_planes,
439 		       unsigned int sizes[], struct device *alloc_devs[])
440 {
441 	struct camif_vp *vp = vb2_get_drv_priv(vq);
442 	struct camif_frame *frame = &vp->out_frame;
443 	const struct camif_fmt *fmt = vp->out_fmt;
444 	unsigned int size;
445 
446 	if (fmt == NULL)
447 		return -EINVAL;
448 
449 	size = (frame->f_width * frame->f_height * fmt->depth) / 8;
450 
451 	if (*num_planes)
452 		return sizes[0] < size ? -EINVAL : 0;
453 
454 	*num_planes = 1;
455 	sizes[0] = size;
456 
457 	pr_debug("size: %u\n", sizes[0]);
458 	return 0;
459 }
460 
461 static int buffer_prepare(struct vb2_buffer *vb)
462 {
463 	struct camif_vp *vp = vb2_get_drv_priv(vb->vb2_queue);
464 
465 	if (vp->out_fmt == NULL)
466 		return -EINVAL;
467 
468 	if (vb2_plane_size(vb, 0) < vp->payload) {
469 		v4l2_err(&vp->vdev, "buffer too small: %lu, required: %u\n",
470 			 vb2_plane_size(vb, 0), vp->payload);
471 		return -EINVAL;
472 	}
473 	vb2_set_plane_payload(vb, 0, vp->payload);
474 
475 	return 0;
476 }
477 
478 static void buffer_queue(struct vb2_buffer *vb)
479 {
480 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
481 	struct camif_buffer *buf = container_of(vbuf, struct camif_buffer, vb);
482 	struct camif_vp *vp = vb2_get_drv_priv(vb->vb2_queue);
483 	struct camif_dev *camif = vp->camif;
484 	unsigned long flags;
485 
486 	spin_lock_irqsave(&camif->slock, flags);
487 	WARN_ON(camif_prepare_addr(vp, &buf->vb.vb2_buf, &buf->paddr));
488 
489 	if (!(vp->state & ST_VP_STREAMING) && vp->active_buffers < 2) {
490 		/* Schedule an empty buffer in H/W */
491 		buf->index = vp->buf_index;
492 
493 		camif_hw_set_output_addr(vp, &buf->paddr, buf->index);
494 		camif_hw_set_output_addr(vp, &buf->paddr, buf->index + 2);
495 
496 		camif_active_queue_add(vp, buf);
497 		vp->buf_index = !vp->buf_index;
498 	} else {
499 		camif_pending_queue_add(vp, buf);
500 	}
501 
502 	if (vb2_is_streaming(&vp->vb_queue) && !list_empty(&vp->pending_buf_q)
503 		&& !(vp->state & ST_VP_STREAMING)) {
504 
505 		vp->state |= ST_VP_STREAMING;
506 		camif_hw_enable_scaler(vp, vp->scaler.enable);
507 		camif_hw_enable_capture(vp);
508 		spin_unlock_irqrestore(&camif->slock, flags);
509 
510 		if (!(vp->state & ST_VP_SENSOR_STREAMING)) {
511 			if (sensor_set_streaming(camif, 1) == 0)
512 				vp->state |= ST_VP_SENSOR_STREAMING;
513 			else
514 				v4l2_err(&vp->vdev, "Sensor s_stream failed\n");
515 
516 			if (debug)
517 				camif_hw_dump_regs(camif, __func__);
518 		}
519 		return;
520 	}
521 	spin_unlock_irqrestore(&camif->slock, flags);
522 }
523 
524 static const struct vb2_ops s3c_camif_qops = {
525 	.queue_setup	 = queue_setup,
526 	.buf_prepare	 = buffer_prepare,
527 	.buf_queue	 = buffer_queue,
528 	.start_streaming = start_streaming,
529 	.stop_streaming	 = stop_streaming,
530 };
531 
532 static int s3c_camif_open(struct file *file)
533 {
534 	struct camif_vp *vp = video_drvdata(file);
535 	struct camif_dev *camif = vp->camif;
536 	int ret;
537 
538 	pr_debug("[vp%d] state: %#x,  owner: %p, pid: %d\n", vp->id,
539 		 vp->state, vp->owner, task_pid_nr(current));
540 
541 	if (mutex_lock_interruptible(&camif->lock))
542 		return -ERESTARTSYS;
543 
544 	ret = v4l2_fh_open(file);
545 	if (ret < 0)
546 		goto unlock;
547 
548 	ret = pm_runtime_resume_and_get(camif->dev);
549 	if (ret < 0)
550 		goto err_pm;
551 
552 	ret = sensor_set_power(camif, 1);
553 	if (!ret)
554 		goto unlock;
555 
556 	pm_runtime_put(camif->dev);
557 err_pm:
558 	v4l2_fh_release(file);
559 unlock:
560 	mutex_unlock(&camif->lock);
561 	return ret;
562 }
563 
564 static int s3c_camif_close(struct file *file)
565 {
566 	struct camif_vp *vp = video_drvdata(file);
567 	struct camif_dev *camif = vp->camif;
568 	int ret;
569 
570 	pr_debug("[vp%d] state: %#x, owner: %p, pid: %d\n", vp->id,
571 		 vp->state, vp->owner, task_pid_nr(current));
572 
573 	mutex_lock(&camif->lock);
574 
575 	if (vp->owner == file->private_data) {
576 		camif_stop_capture(vp);
577 		vb2_queue_release(&vp->vb_queue);
578 		vp->owner = NULL;
579 	}
580 
581 	sensor_set_power(camif, 0);
582 
583 	pm_runtime_put(camif->dev);
584 	ret = v4l2_fh_release(file);
585 
586 	mutex_unlock(&camif->lock);
587 	return ret;
588 }
589 
590 static __poll_t s3c_camif_poll(struct file *file,
591 				   struct poll_table_struct *wait)
592 {
593 	struct camif_vp *vp = video_drvdata(file);
594 	struct camif_dev *camif = vp->camif;
595 	__poll_t ret;
596 
597 	mutex_lock(&camif->lock);
598 	if (vp->owner && vp->owner != file->private_data)
599 		ret = EPOLLERR;
600 	else
601 		ret = vb2_poll(&vp->vb_queue, file, wait);
602 
603 	mutex_unlock(&camif->lock);
604 	return ret;
605 }
606 
607 static int s3c_camif_mmap(struct file *file, struct vm_area_struct *vma)
608 {
609 	struct camif_vp *vp = video_drvdata(file);
610 	int ret;
611 
612 	if (vp->owner && vp->owner != file->private_data)
613 		ret = -EBUSY;
614 	else
615 		ret = vb2_mmap(&vp->vb_queue, vma);
616 
617 	return ret;
618 }
619 
620 static const struct v4l2_file_operations s3c_camif_fops = {
621 	.owner		= THIS_MODULE,
622 	.open		= s3c_camif_open,
623 	.release	= s3c_camif_close,
624 	.poll		= s3c_camif_poll,
625 	.unlocked_ioctl	= video_ioctl2,
626 	.mmap		= s3c_camif_mmap,
627 };
628 
629 /*
630  * Video node IOCTLs
631  */
632 
633 static int s3c_camif_vidioc_querycap(struct file *file, void *priv,
634 				     struct v4l2_capability *cap)
635 {
636 	struct camif_vp *vp = video_drvdata(file);
637 
638 	strscpy(cap->driver, S3C_CAMIF_DRIVER_NAME, sizeof(cap->driver));
639 	strscpy(cap->card, S3C_CAMIF_DRIVER_NAME, sizeof(cap->card));
640 	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s.%d",
641 		 dev_name(vp->camif->dev), vp->id);
642 	return 0;
643 }
644 
645 static int s3c_camif_vidioc_enum_input(struct file *file, void *priv,
646 				       struct v4l2_input *input)
647 {
648 	struct camif_vp *vp = video_drvdata(file);
649 	struct v4l2_subdev *sensor = vp->camif->sensor.sd;
650 
651 	if (input->index || sensor == NULL)
652 		return -EINVAL;
653 
654 	input->type = V4L2_INPUT_TYPE_CAMERA;
655 	strscpy(input->name, sensor->name, sizeof(input->name));
656 	return 0;
657 }
658 
659 static int s3c_camif_vidioc_s_input(struct file *file, void *priv,
660 				    unsigned int i)
661 {
662 	return i == 0 ? 0 : -EINVAL;
663 }
664 
665 static int s3c_camif_vidioc_g_input(struct file *file, void *priv,
666 				    unsigned int *i)
667 {
668 	*i = 0;
669 	return 0;
670 }
671 
672 static int s3c_camif_vidioc_enum_fmt(struct file *file, void *priv,
673 				     struct v4l2_fmtdesc *f)
674 {
675 	struct camif_vp *vp = video_drvdata(file);
676 	const struct camif_fmt *fmt;
677 
678 	fmt = s3c_camif_find_format(vp, NULL, f->index);
679 	if (!fmt)
680 		return -EINVAL;
681 
682 	f->pixelformat = fmt->fourcc;
683 	return 0;
684 }
685 
686 static int s3c_camif_vidioc_g_fmt(struct file *file, void *priv,
687 				  struct v4l2_format *f)
688 {
689 	struct camif_vp *vp = video_drvdata(file);
690 	struct v4l2_pix_format *pix = &f->fmt.pix;
691 	struct camif_frame *frame = &vp->out_frame;
692 	const struct camif_fmt *fmt = vp->out_fmt;
693 
694 	pix->bytesperline = frame->f_width * fmt->ybpp;
695 	pix->sizeimage = vp->payload;
696 
697 	pix->pixelformat = fmt->fourcc;
698 	pix->width = frame->f_width;
699 	pix->height = frame->f_height;
700 	pix->field = V4L2_FIELD_NONE;
701 	pix->colorspace = V4L2_COLORSPACE_JPEG;
702 
703 	return 0;
704 }
705 
706 static int __camif_video_try_format(struct camif_vp *vp,
707 				    struct v4l2_pix_format *pix,
708 				    const struct camif_fmt **ffmt)
709 {
710 	struct camif_dev *camif = vp->camif;
711 	struct v4l2_rect *crop = &camif->camif_crop;
712 	unsigned int wmin, hmin, sc_hrmax, sc_vrmax;
713 	const struct vp_pix_limits *pix_lim;
714 	const struct camif_fmt *fmt;
715 
716 	fmt = s3c_camif_find_format(vp, &pix->pixelformat, 0);
717 
718 	if (WARN_ON(fmt == NULL))
719 		return -EINVAL;
720 
721 	if (ffmt)
722 		*ffmt = fmt;
723 
724 	pix_lim = &camif->variant->vp_pix_limits[vp->id];
725 
726 	pr_debug("fmt: %ux%u, crop: %ux%u, bytesperline: %u\n",
727 		 pix->width, pix->height, crop->width, crop->height,
728 		 pix->bytesperline);
729 	/*
730 	 * Calculate minimum width and height according to the configured
731 	 * camera input interface crop rectangle and the resizer's capabilities.
732 	 */
733 	sc_hrmax = min(SCALER_MAX_RATIO, 1 << (ffs(crop->width) - 3));
734 	sc_vrmax = min(SCALER_MAX_RATIO, 1 << (ffs(crop->height) - 1));
735 
736 	wmin = max_t(u32, pix_lim->min_out_width, crop->width / sc_hrmax);
737 	wmin = round_up(wmin, pix_lim->out_width_align);
738 	hmin = max_t(u32, 8, crop->height / sc_vrmax);
739 	hmin = round_up(hmin, 8);
740 
741 	v4l_bound_align_image(&pix->width, wmin, pix_lim->max_sc_out_width,
742 			      ffs(pix_lim->out_width_align) - 1,
743 			      &pix->height, hmin, pix_lim->max_height, 0, 0);
744 
745 	pix->bytesperline = pix->width * fmt->ybpp;
746 	pix->sizeimage = (pix->width * pix->height * fmt->depth) / 8;
747 	pix->pixelformat = fmt->fourcc;
748 	pix->colorspace = V4L2_COLORSPACE_JPEG;
749 	pix->field = V4L2_FIELD_NONE;
750 
751 	pr_debug("%ux%u, wmin: %d, hmin: %d, sc_hrmax: %d, sc_vrmax: %d\n",
752 		 pix->width, pix->height, wmin, hmin, sc_hrmax, sc_vrmax);
753 
754 	return 0;
755 }
756 
757 static int s3c_camif_vidioc_try_fmt(struct file *file, void *priv,
758 				    struct v4l2_format *f)
759 {
760 	struct camif_vp *vp = video_drvdata(file);
761 	return __camif_video_try_format(vp, &f->fmt.pix, NULL);
762 }
763 
764 static int s3c_camif_vidioc_s_fmt(struct file *file, void *priv,
765 				  struct v4l2_format *f)
766 {
767 	struct v4l2_pix_format *pix = &f->fmt.pix;
768 	struct camif_vp *vp = video_drvdata(file);
769 	struct camif_frame *out_frame = &vp->out_frame;
770 	const struct camif_fmt *fmt = NULL;
771 	int ret;
772 
773 	pr_debug("[vp%d]\n", vp->id);
774 
775 	if (vb2_is_busy(&vp->vb_queue))
776 		return -EBUSY;
777 
778 	ret = __camif_video_try_format(vp, &f->fmt.pix, &fmt);
779 	if (ret < 0)
780 		return ret;
781 
782 	vp->out_fmt = fmt;
783 	vp->payload = pix->sizeimage;
784 	out_frame->f_width = pix->width;
785 	out_frame->f_height = pix->height;
786 
787 	/* Reset composition rectangle */
788 	out_frame->rect.width = pix->width;
789 	out_frame->rect.height = pix->height;
790 	out_frame->rect.left = 0;
791 	out_frame->rect.top = 0;
792 
793 	if (vp->owner == NULL)
794 		vp->owner = priv;
795 
796 	pr_debug("%ux%u. payload: %u. fmt: 0x%08x. %d %d. sizeimage: %d. bpl: %d\n",
797 		 out_frame->f_width, out_frame->f_height, vp->payload,
798 		 fmt->fourcc, pix->width * pix->height * fmt->depth,
799 		 fmt->depth, pix->sizeimage, pix->bytesperline);
800 
801 	return 0;
802 }
803 
804 /* Only check pixel formats at the sensor and the camif subdev pads */
805 static int camif_pipeline_validate(struct camif_dev *camif)
806 {
807 	struct v4l2_subdev_format src_fmt = {
808 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
809 	};
810 	struct media_pad *pad;
811 	int ret;
812 
813 	/* Retrieve format at the sensor subdev source pad */
814 	pad = media_pad_remote_pad_first(&camif->pads[0]);
815 	if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
816 		return -EPIPE;
817 
818 	src_fmt.pad = pad->index;
819 	ret = v4l2_subdev_call(camif->sensor.sd, pad, get_fmt, NULL, &src_fmt);
820 	if (ret < 0 && ret != -ENOIOCTLCMD)
821 		return -EPIPE;
822 
823 	if (src_fmt.format.width != camif->mbus_fmt.width ||
824 	    src_fmt.format.height != camif->mbus_fmt.height ||
825 	    src_fmt.format.code != camif->mbus_fmt.code)
826 		return -EPIPE;
827 
828 	return 0;
829 }
830 
831 static int s3c_camif_streamon(struct file *file, void *priv,
832 			      enum v4l2_buf_type type)
833 {
834 	struct camif_vp *vp = video_drvdata(file);
835 	struct camif_dev *camif = vp->camif;
836 	struct media_entity *sensor = &camif->sensor.sd->entity;
837 	int ret;
838 
839 	pr_debug("[vp%d]\n", vp->id);
840 
841 	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
842 		return -EINVAL;
843 
844 	if (vp->owner && vp->owner != priv)
845 		return -EBUSY;
846 
847 	if (s3c_vp_active(vp))
848 		return 0;
849 
850 	ret = media_pipeline_start(sensor->pads, camif->m_pipeline);
851 	if (ret < 0)
852 		return ret;
853 
854 	ret = camif_pipeline_validate(camif);
855 	if (ret < 0) {
856 		media_pipeline_stop(sensor->pads);
857 		return ret;
858 	}
859 
860 	return vb2_streamon(&vp->vb_queue, type);
861 }
862 
863 static int s3c_camif_streamoff(struct file *file, void *priv,
864 			       enum v4l2_buf_type type)
865 {
866 	struct camif_vp *vp = video_drvdata(file);
867 	struct camif_dev *camif = vp->camif;
868 	int ret;
869 
870 	pr_debug("[vp%d]\n", vp->id);
871 
872 	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
873 		return -EINVAL;
874 
875 	if (vp->owner && vp->owner != priv)
876 		return -EBUSY;
877 
878 	ret = vb2_streamoff(&vp->vb_queue, type);
879 	if (ret == 0)
880 		media_pipeline_stop(camif->sensor.sd->entity.pads);
881 	return ret;
882 }
883 
884 static int s3c_camif_reqbufs(struct file *file, void *priv,
885 			     struct v4l2_requestbuffers *rb)
886 {
887 	struct camif_vp *vp = video_drvdata(file);
888 	int ret;
889 
890 	pr_debug("[vp%d] rb count: %d, owner: %p, priv: %p\n",
891 		 vp->id, rb->count, vp->owner, priv);
892 
893 	if (vp->owner && vp->owner != priv)
894 		return -EBUSY;
895 
896 	if (rb->count)
897 		rb->count = max_t(u32, CAMIF_REQ_BUFS_MIN, rb->count);
898 	else
899 		vp->owner = NULL;
900 
901 	ret = vb2_reqbufs(&vp->vb_queue, rb);
902 	if (ret < 0)
903 		return ret;
904 
905 	if (rb->count && rb->count < CAMIF_REQ_BUFS_MIN) {
906 		rb->count = 0;
907 		vb2_reqbufs(&vp->vb_queue, rb);
908 		ret = -ENOMEM;
909 	}
910 
911 	vp->reqbufs_count = rb->count;
912 	if (vp->owner == NULL && rb->count > 0)
913 		vp->owner = priv;
914 
915 	return ret;
916 }
917 
918 static int s3c_camif_querybuf(struct file *file, void *priv,
919 			      struct v4l2_buffer *buf)
920 {
921 	struct camif_vp *vp = video_drvdata(file);
922 	return vb2_querybuf(&vp->vb_queue, buf);
923 }
924 
925 static int s3c_camif_qbuf(struct file *file, void *priv,
926 			  struct v4l2_buffer *buf)
927 {
928 	struct camif_vp *vp = video_drvdata(file);
929 
930 	pr_debug("[vp%d]\n", vp->id);
931 
932 	if (vp->owner && vp->owner != priv)
933 		return -EBUSY;
934 
935 	return vb2_qbuf(&vp->vb_queue, vp->vdev.v4l2_dev->mdev, buf);
936 }
937 
938 static int s3c_camif_dqbuf(struct file *file, void *priv,
939 			   struct v4l2_buffer *buf)
940 {
941 	struct camif_vp *vp = video_drvdata(file);
942 
943 	pr_debug("[vp%d] sequence: %d\n", vp->id, vp->frame_sequence);
944 
945 	if (vp->owner && vp->owner != priv)
946 		return -EBUSY;
947 
948 	return vb2_dqbuf(&vp->vb_queue, buf, file->f_flags & O_NONBLOCK);
949 }
950 
951 static int s3c_camif_create_bufs(struct file *file, void *priv,
952 				 struct v4l2_create_buffers *create)
953 {
954 	struct camif_vp *vp = video_drvdata(file);
955 	int ret;
956 
957 	if (vp->owner && vp->owner != priv)
958 		return -EBUSY;
959 
960 	create->count = max_t(u32, 1, create->count);
961 	ret = vb2_create_bufs(&vp->vb_queue, create);
962 
963 	if (!ret && vp->owner == NULL)
964 		vp->owner = priv;
965 
966 	return ret;
967 }
968 
969 static int s3c_camif_prepare_buf(struct file *file, void *priv,
970 				 struct v4l2_buffer *b)
971 {
972 	struct camif_vp *vp = video_drvdata(file);
973 	return vb2_prepare_buf(&vp->vb_queue, vp->vdev.v4l2_dev->mdev, b);
974 }
975 
976 static int s3c_camif_g_selection(struct file *file, void *priv,
977 				 struct v4l2_selection *sel)
978 {
979 	struct camif_vp *vp = video_drvdata(file);
980 
981 	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
982 		return -EINVAL;
983 
984 	switch (sel->target) {
985 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
986 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
987 		sel->r.left = 0;
988 		sel->r.top = 0;
989 		sel->r.width = vp->out_frame.f_width;
990 		sel->r.height = vp->out_frame.f_height;
991 		return 0;
992 
993 	case V4L2_SEL_TGT_COMPOSE:
994 		sel->r = vp->out_frame.rect;
995 		return 0;
996 	}
997 
998 	return -EINVAL;
999 }
1000 
1001 static void __camif_try_compose(struct camif_dev *camif, struct camif_vp *vp,
1002 				struct v4l2_rect *r)
1003 {
1004 	/* s3c244x doesn't support composition */
1005 	if (camif->variant->ip_revision == S3C244X_CAMIF_IP_REV) {
1006 		*r = vp->out_frame.rect;
1007 		return;
1008 	}
1009 
1010 	/* TODO: s3c64xx */
1011 }
1012 
1013 static int s3c_camif_s_selection(struct file *file, void *priv,
1014 				 struct v4l2_selection *sel)
1015 {
1016 	struct camif_vp *vp = video_drvdata(file);
1017 	struct camif_dev *camif = vp->camif;
1018 	struct v4l2_rect rect = sel->r;
1019 	unsigned long flags;
1020 
1021 	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1022 	    sel->target != V4L2_SEL_TGT_COMPOSE)
1023 		return -EINVAL;
1024 
1025 	__camif_try_compose(camif, vp, &rect);
1026 
1027 	sel->r = rect;
1028 	spin_lock_irqsave(&camif->slock, flags);
1029 	vp->out_frame.rect = rect;
1030 	vp->state |= ST_VP_CONFIG;
1031 	spin_unlock_irqrestore(&camif->slock, flags);
1032 
1033 	pr_debug("type: %#x, target: %#x, flags: %#x, (%d,%d)/%dx%d\n",
1034 		sel->type, sel->target, sel->flags,
1035 		sel->r.left, sel->r.top, sel->r.width, sel->r.height);
1036 
1037 	return 0;
1038 }
1039 
1040 static const struct v4l2_ioctl_ops s3c_camif_ioctl_ops = {
1041 	.vidioc_querycap	  = s3c_camif_vidioc_querycap,
1042 	.vidioc_enum_input	  = s3c_camif_vidioc_enum_input,
1043 	.vidioc_g_input		  = s3c_camif_vidioc_g_input,
1044 	.vidioc_s_input		  = s3c_camif_vidioc_s_input,
1045 	.vidioc_enum_fmt_vid_cap  = s3c_camif_vidioc_enum_fmt,
1046 	.vidioc_try_fmt_vid_cap	  = s3c_camif_vidioc_try_fmt,
1047 	.vidioc_s_fmt_vid_cap	  = s3c_camif_vidioc_s_fmt,
1048 	.vidioc_g_fmt_vid_cap	  = s3c_camif_vidioc_g_fmt,
1049 	.vidioc_g_selection	  = s3c_camif_g_selection,
1050 	.vidioc_s_selection	  = s3c_camif_s_selection,
1051 	.vidioc_reqbufs		  = s3c_camif_reqbufs,
1052 	.vidioc_querybuf	  = s3c_camif_querybuf,
1053 	.vidioc_prepare_buf	  = s3c_camif_prepare_buf,
1054 	.vidioc_create_bufs	  = s3c_camif_create_bufs,
1055 	.vidioc_qbuf		  = s3c_camif_qbuf,
1056 	.vidioc_dqbuf		  = s3c_camif_dqbuf,
1057 	.vidioc_streamon	  = s3c_camif_streamon,
1058 	.vidioc_streamoff	  = s3c_camif_streamoff,
1059 	.vidioc_subscribe_event	  = v4l2_ctrl_subscribe_event,
1060 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1061 	.vidioc_log_status	  = v4l2_ctrl_log_status,
1062 };
1063 
1064 /*
1065  * Video node controls
1066  */
1067 static int s3c_camif_video_s_ctrl(struct v4l2_ctrl *ctrl)
1068 {
1069 	struct camif_vp *vp = ctrl->priv;
1070 	struct camif_dev *camif = vp->camif;
1071 	unsigned long flags;
1072 
1073 	pr_debug("[vp%d] ctrl: %s, value: %d\n", vp->id,
1074 		 ctrl->name, ctrl->val);
1075 
1076 	spin_lock_irqsave(&camif->slock, flags);
1077 
1078 	switch (ctrl->id) {
1079 	case V4L2_CID_HFLIP:
1080 		vp->hflip = ctrl->val;
1081 		break;
1082 
1083 	case V4L2_CID_VFLIP:
1084 		vp->vflip = ctrl->val;
1085 		break;
1086 	}
1087 
1088 	vp->state |= ST_VP_CONFIG;
1089 	spin_unlock_irqrestore(&camif->slock, flags);
1090 	return 0;
1091 }
1092 
1093 /* Codec and preview video node control ops */
1094 static const struct v4l2_ctrl_ops s3c_camif_video_ctrl_ops = {
1095 	.s_ctrl = s3c_camif_video_s_ctrl,
1096 };
1097 
1098 int s3c_camif_register_video_node(struct camif_dev *camif, int idx)
1099 {
1100 	struct camif_vp *vp = &camif->vp[idx];
1101 	struct vb2_queue *q = &vp->vb_queue;
1102 	struct video_device *vfd = &vp->vdev;
1103 	struct v4l2_ctrl *ctrl;
1104 	int ret;
1105 
1106 	memset(vfd, 0, sizeof(*vfd));
1107 	snprintf(vfd->name, sizeof(vfd->name), "camif-%s",
1108 		 vp->id == 0 ? "codec" : "preview");
1109 
1110 	vfd->fops = &s3c_camif_fops;
1111 	vfd->ioctl_ops = &s3c_camif_ioctl_ops;
1112 	vfd->v4l2_dev = &camif->v4l2_dev;
1113 	vfd->minor = -1;
1114 	vfd->release = video_device_release_empty;
1115 	vfd->lock = &camif->lock;
1116 	vp->reqbufs_count = 0;
1117 
1118 	INIT_LIST_HEAD(&vp->pending_buf_q);
1119 	INIT_LIST_HEAD(&vp->active_buf_q);
1120 
1121 	memset(q, 0, sizeof(*q));
1122 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1123 	q->io_modes = VB2_MMAP | VB2_USERPTR;
1124 	q->ops = &s3c_camif_qops;
1125 	q->mem_ops = &vb2_dma_contig_memops;
1126 	q->buf_struct_size = sizeof(struct camif_buffer);
1127 	q->drv_priv = vp;
1128 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1129 	q->lock = &vp->camif->lock;
1130 	q->dev = camif->v4l2_dev.dev;
1131 
1132 	ret = vb2_queue_init(q);
1133 	if (ret)
1134 		return ret;
1135 
1136 	vp->pad.flags = MEDIA_PAD_FL_SINK;
1137 	ret = media_entity_pads_init(&vfd->entity, 1, &vp->pad);
1138 	if (ret)
1139 		return ret;
1140 
1141 	video_set_drvdata(vfd, vp);
1142 
1143 	v4l2_ctrl_handler_init(&vp->ctrl_handler, 1);
1144 	ctrl = v4l2_ctrl_new_std(&vp->ctrl_handler, &s3c_camif_video_ctrl_ops,
1145 				 V4L2_CID_HFLIP, 0, 1, 1, 0);
1146 	if (ctrl)
1147 		ctrl->priv = vp;
1148 	ctrl = v4l2_ctrl_new_std(&vp->ctrl_handler, &s3c_camif_video_ctrl_ops,
1149 				 V4L2_CID_VFLIP, 0, 1, 1, 0);
1150 	if (ctrl)
1151 		ctrl->priv = vp;
1152 
1153 	ret = vp->ctrl_handler.error;
1154 	if (ret < 0)
1155 		goto err_me_cleanup;
1156 
1157 	vfd->ctrl_handler = &vp->ctrl_handler;
1158 	vfd->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
1159 
1160 	ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1);
1161 	if (ret)
1162 		goto err_ctrlh_free;
1163 
1164 	v4l2_info(&camif->v4l2_dev, "registered %s as /dev/%s\n",
1165 		  vfd->name, video_device_node_name(vfd));
1166 	return 0;
1167 
1168 err_ctrlh_free:
1169 	v4l2_ctrl_handler_free(&vp->ctrl_handler);
1170 err_me_cleanup:
1171 	media_entity_cleanup(&vfd->entity);
1172 	return ret;
1173 }
1174 
1175 void s3c_camif_unregister_video_node(struct camif_dev *camif, int idx)
1176 {
1177 	struct video_device *vfd = &camif->vp[idx].vdev;
1178 
1179 	if (video_is_registered(vfd)) {
1180 		video_unregister_device(vfd);
1181 		media_entity_cleanup(&vfd->entity);
1182 		v4l2_ctrl_handler_free(vfd->ctrl_handler);
1183 	}
1184 }
1185 
1186 /* Media bus pixel formats supported at the camif input */
1187 static const u32 camif_mbus_formats[] = {
1188 	MEDIA_BUS_FMT_YUYV8_2X8,
1189 	MEDIA_BUS_FMT_YVYU8_2X8,
1190 	MEDIA_BUS_FMT_UYVY8_2X8,
1191 	MEDIA_BUS_FMT_VYUY8_2X8,
1192 };
1193 
1194 /*
1195  *  Camera input interface subdev operations
1196  */
1197 
1198 static int s3c_camif_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1199 					struct v4l2_subdev_state *sd_state,
1200 					struct v4l2_subdev_mbus_code_enum *code)
1201 {
1202 	if (code->index >= ARRAY_SIZE(camif_mbus_formats))
1203 		return -EINVAL;
1204 
1205 	code->code = camif_mbus_formats[code->index];
1206 	return 0;
1207 }
1208 
1209 static int s3c_camif_subdev_get_fmt(struct v4l2_subdev *sd,
1210 				    struct v4l2_subdev_state *sd_state,
1211 				    struct v4l2_subdev_format *fmt)
1212 {
1213 	struct camif_dev *camif = v4l2_get_subdevdata(sd);
1214 	struct v4l2_mbus_framefmt *mf = &fmt->format;
1215 
1216 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1217 		mf = v4l2_subdev_state_get_format(sd_state, fmt->pad);
1218 		fmt->format = *mf;
1219 		return 0;
1220 	}
1221 
1222 	mutex_lock(&camif->lock);
1223 
1224 	switch (fmt->pad) {
1225 	case CAMIF_SD_PAD_SINK:
1226 		/* full camera input pixel size */
1227 		*mf = camif->mbus_fmt;
1228 		break;
1229 
1230 	case CAMIF_SD_PAD_SOURCE_C...CAMIF_SD_PAD_SOURCE_P:
1231 		/* crop rectangle at camera interface input */
1232 		mf->width = camif->camif_crop.width;
1233 		mf->height = camif->camif_crop.height;
1234 		mf->code = camif->mbus_fmt.code;
1235 		break;
1236 	}
1237 
1238 	mutex_unlock(&camif->lock);
1239 	mf->field = V4L2_FIELD_NONE;
1240 	mf->colorspace = V4L2_COLORSPACE_JPEG;
1241 	return 0;
1242 }
1243 
1244 static void __camif_subdev_try_format(struct camif_dev *camif,
1245 				struct v4l2_mbus_framefmt *mf, int pad)
1246 {
1247 	const struct s3c_camif_variant *variant = camif->variant;
1248 	const struct vp_pix_limits *pix_lim;
1249 	unsigned int i;
1250 
1251 	/* FIXME: constraints against codec or preview path ? */
1252 	pix_lim = &variant->vp_pix_limits[VP_CODEC];
1253 
1254 	for (i = 0; i < ARRAY_SIZE(camif_mbus_formats); i++)
1255 		if (camif_mbus_formats[i] == mf->code)
1256 			break;
1257 
1258 	if (i == ARRAY_SIZE(camif_mbus_formats))
1259 		mf->code = camif_mbus_formats[0];
1260 
1261 	if (pad == CAMIF_SD_PAD_SINK) {
1262 		v4l_bound_align_image(&mf->width, 8, CAMIF_MAX_PIX_WIDTH,
1263 				      ffs(pix_lim->out_width_align) - 1,
1264 				      &mf->height, 8, CAMIF_MAX_PIX_HEIGHT, 0,
1265 				      0);
1266 	} else {
1267 		struct v4l2_rect *crop = &camif->camif_crop;
1268 		v4l_bound_align_image(&mf->width, 8, crop->width,
1269 				      ffs(pix_lim->out_width_align) - 1,
1270 				      &mf->height, 8, crop->height,
1271 				      0, 0);
1272 	}
1273 
1274 	v4l2_dbg(1, debug, &camif->subdev, "%ux%u\n", mf->width, mf->height);
1275 }
1276 
1277 static int s3c_camif_subdev_set_fmt(struct v4l2_subdev *sd,
1278 				    struct v4l2_subdev_state *sd_state,
1279 				    struct v4l2_subdev_format *fmt)
1280 {
1281 	struct camif_dev *camif = v4l2_get_subdevdata(sd);
1282 	struct v4l2_mbus_framefmt *mf = &fmt->format;
1283 	struct v4l2_rect *crop = &camif->camif_crop;
1284 	int i;
1285 
1286 	v4l2_dbg(1, debug, sd, "pad%d: code: 0x%x, %ux%u\n",
1287 		 fmt->pad, mf->code, mf->width, mf->height);
1288 
1289 	mf->field = V4L2_FIELD_NONE;
1290 	mf->colorspace = V4L2_COLORSPACE_JPEG;
1291 	mutex_lock(&camif->lock);
1292 
1293 	/*
1294 	 * No pixel format change at the camera input is allowed
1295 	 * while streaming.
1296 	 */
1297 	if (vb2_is_busy(&camif->vp[VP_CODEC].vb_queue) ||
1298 	    vb2_is_busy(&camif->vp[VP_PREVIEW].vb_queue)) {
1299 		mutex_unlock(&camif->lock);
1300 		return -EBUSY;
1301 	}
1302 
1303 	__camif_subdev_try_format(camif, mf, fmt->pad);
1304 
1305 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1306 		mf = v4l2_subdev_state_get_format(sd_state, fmt->pad);
1307 		*mf = fmt->format;
1308 		mutex_unlock(&camif->lock);
1309 		return 0;
1310 	}
1311 
1312 	switch (fmt->pad) {
1313 	case CAMIF_SD_PAD_SINK:
1314 		camif->mbus_fmt = *mf;
1315 		/* Reset sink crop rectangle. */
1316 		crop->width = mf->width;
1317 		crop->height = mf->height;
1318 		crop->left = 0;
1319 		crop->top = 0;
1320 		/*
1321 		 * Reset source format (the camif's crop rectangle)
1322 		 * and the video output resolution.
1323 		 */
1324 		for (i = 0; i < CAMIF_VP_NUM; i++) {
1325 			struct camif_frame *frame = &camif->vp[i].out_frame;
1326 			frame->rect = *crop;
1327 			frame->f_width = mf->width;
1328 			frame->f_height = mf->height;
1329 		}
1330 		break;
1331 
1332 	case CAMIF_SD_PAD_SOURCE_C...CAMIF_SD_PAD_SOURCE_P:
1333 		/* Pixel format can be only changed on the sink pad. */
1334 		mf->code = camif->mbus_fmt.code;
1335 		mf->width = crop->width;
1336 		mf->height = crop->height;
1337 		break;
1338 	}
1339 
1340 	mutex_unlock(&camif->lock);
1341 	return 0;
1342 }
1343 
1344 static int s3c_camif_subdev_get_selection(struct v4l2_subdev *sd,
1345 					  struct v4l2_subdev_state *sd_state,
1346 					  struct v4l2_subdev_selection *sel)
1347 {
1348 	struct camif_dev *camif = v4l2_get_subdevdata(sd);
1349 	struct v4l2_rect *crop = &camif->camif_crop;
1350 	struct v4l2_mbus_framefmt *mf = &camif->mbus_fmt;
1351 
1352 	if ((sel->target != V4L2_SEL_TGT_CROP &&
1353 	    sel->target != V4L2_SEL_TGT_CROP_BOUNDS) ||
1354 	    sel->pad != CAMIF_SD_PAD_SINK)
1355 		return -EINVAL;
1356 
1357 	if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1358 		sel->r = *v4l2_subdev_state_get_crop(sd_state, sel->pad);
1359 		return 0;
1360 	}
1361 
1362 	mutex_lock(&camif->lock);
1363 
1364 	if (sel->target == V4L2_SEL_TGT_CROP) {
1365 		sel->r = *crop;
1366 	} else { /* crop bounds */
1367 		sel->r.width = mf->width;
1368 		sel->r.height = mf->height;
1369 		sel->r.left = 0;
1370 		sel->r.top = 0;
1371 	}
1372 
1373 	mutex_unlock(&camif->lock);
1374 
1375 	v4l2_dbg(1, debug, sd, "%s: crop: (%d,%d) %dx%d, size: %ux%u\n",
1376 		 __func__, crop->left, crop->top, crop->width,
1377 		 crop->height, mf->width, mf->height);
1378 
1379 	return 0;
1380 }
1381 
1382 static void __camif_try_crop(struct camif_dev *camif, struct v4l2_rect *r)
1383 {
1384 	struct v4l2_mbus_framefmt *mf = &camif->mbus_fmt;
1385 	const struct camif_pix_limits *pix_lim = &camif->variant->pix_limits;
1386 	unsigned int left = 2 * r->left;
1387 	unsigned int top = 2 * r->top;
1388 
1389 	/*
1390 	 * Following constraints must be met:
1391 	 *  - r->width + 2 * r->left = mf->width;
1392 	 *  - r->height + 2 * r->top = mf->height;
1393 	 *  - crop rectangle size and position must be aligned
1394 	 *    to 8 or 2 pixels, depending on SoC version.
1395 	 */
1396 	v4l_bound_align_image(&r->width, 0, mf->width,
1397 			      ffs(pix_lim->win_hor_offset_align) - 1,
1398 			      &r->height, 0, mf->height, 1, 0);
1399 
1400 	v4l_bound_align_image(&left, 0, mf->width - r->width,
1401 			      ffs(pix_lim->win_hor_offset_align),
1402 			      &top, 0, mf->height - r->height, 2, 0);
1403 
1404 	r->left = left / 2;
1405 	r->top = top / 2;
1406 	r->width = mf->width - left;
1407 	r->height = mf->height - top;
1408 	/*
1409 	 * Make sure we either downscale or upscale both the pixel
1410 	 * width and height. Just return current crop rectangle if
1411 	 * this scaler constraint is not met.
1412 	 */
1413 	if (camif->variant->ip_revision == S3C244X_CAMIF_IP_REV &&
1414 	    camif_is_streaming(camif)) {
1415 		unsigned int i;
1416 
1417 		for (i = 0; i < CAMIF_VP_NUM; i++) {
1418 			struct v4l2_rect *or = &camif->vp[i].out_frame.rect;
1419 			if ((or->width > r->width) == (or->height > r->height))
1420 				continue;
1421 			*r = camif->camif_crop;
1422 			pr_debug("Width/height scaling direction limitation\n");
1423 			break;
1424 		}
1425 	}
1426 
1427 	v4l2_dbg(1, debug, &camif->v4l2_dev, "crop: (%d,%d)/%dx%d, fmt: %ux%u\n",
1428 		 r->left, r->top, r->width, r->height, mf->width, mf->height);
1429 }
1430 
1431 static int s3c_camif_subdev_set_selection(struct v4l2_subdev *sd,
1432 					  struct v4l2_subdev_state *sd_state,
1433 					  struct v4l2_subdev_selection *sel)
1434 {
1435 	struct camif_dev *camif = v4l2_get_subdevdata(sd);
1436 	struct v4l2_rect *crop = &camif->camif_crop;
1437 	struct camif_scaler scaler;
1438 
1439 	if (sel->target != V4L2_SEL_TGT_CROP || sel->pad != CAMIF_SD_PAD_SINK)
1440 		return -EINVAL;
1441 
1442 	mutex_lock(&camif->lock);
1443 	__camif_try_crop(camif, &sel->r);
1444 
1445 	if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1446 		*v4l2_subdev_state_get_crop(sd_state, sel->pad) = sel->r;
1447 	} else {
1448 		unsigned long flags;
1449 		unsigned int i;
1450 
1451 		spin_lock_irqsave(&camif->slock, flags);
1452 		*crop = sel->r;
1453 
1454 		for (i = 0; i < CAMIF_VP_NUM; i++) {
1455 			struct camif_vp *vp = &camif->vp[i];
1456 			scaler = vp->scaler;
1457 			if (s3c_camif_get_scaler_config(vp, &scaler))
1458 				continue;
1459 			vp->scaler = scaler;
1460 			vp->state |= ST_VP_CONFIG;
1461 		}
1462 
1463 		spin_unlock_irqrestore(&camif->slock, flags);
1464 	}
1465 	mutex_unlock(&camif->lock);
1466 
1467 	v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %u, f_h: %u\n",
1468 		 __func__, crop->left, crop->top, crop->width, crop->height,
1469 		 camif->mbus_fmt.width, camif->mbus_fmt.height);
1470 
1471 	return 0;
1472 }
1473 
1474 static const struct v4l2_subdev_pad_ops s3c_camif_subdev_pad_ops = {
1475 	.enum_mbus_code = s3c_camif_subdev_enum_mbus_code,
1476 	.get_selection = s3c_camif_subdev_get_selection,
1477 	.set_selection = s3c_camif_subdev_set_selection,
1478 	.get_fmt = s3c_camif_subdev_get_fmt,
1479 	.set_fmt = s3c_camif_subdev_set_fmt,
1480 };
1481 
1482 static const struct v4l2_subdev_ops s3c_camif_subdev_ops = {
1483 	.pad = &s3c_camif_subdev_pad_ops,
1484 };
1485 
1486 static int s3c_camif_subdev_s_ctrl(struct v4l2_ctrl *ctrl)
1487 {
1488 	struct camif_dev *camif = container_of(ctrl->handler, struct camif_dev,
1489 					       ctrl_handler);
1490 	unsigned long flags;
1491 
1492 	spin_lock_irqsave(&camif->slock, flags);
1493 
1494 	switch (ctrl->id) {
1495 	case V4L2_CID_COLORFX:
1496 		camif->colorfx = camif->ctrl_colorfx->val;
1497 		/* Set Cb, Cr */
1498 		switch (ctrl->val) {
1499 		case V4L2_COLORFX_SEPIA:
1500 			camif->colorfx_cb = 115;
1501 			camif->colorfx_cr = 145;
1502 			break;
1503 		case V4L2_COLORFX_SET_CBCR:
1504 			camif->colorfx_cb = camif->ctrl_colorfx_cbcr->val >> 8;
1505 			camif->colorfx_cr = camif->ctrl_colorfx_cbcr->val & 0xff;
1506 			break;
1507 		default:
1508 			/* for V4L2_COLORFX_BW and others */
1509 			camif->colorfx_cb = 128;
1510 			camif->colorfx_cr = 128;
1511 		}
1512 		break;
1513 	case V4L2_CID_TEST_PATTERN:
1514 		camif->test_pattern = camif->ctrl_test_pattern->val;
1515 		break;
1516 	default:
1517 		WARN_ON(1);
1518 	}
1519 
1520 	camif->vp[VP_CODEC].state |= ST_VP_CONFIG;
1521 	camif->vp[VP_PREVIEW].state |= ST_VP_CONFIG;
1522 	spin_unlock_irqrestore(&camif->slock, flags);
1523 
1524 	return 0;
1525 }
1526 
1527 static const struct v4l2_ctrl_ops s3c_camif_subdev_ctrl_ops = {
1528 	.s_ctrl	= s3c_camif_subdev_s_ctrl,
1529 };
1530 
1531 static const char * const s3c_camif_test_pattern_menu[] = {
1532 	"Disabled",
1533 	"Color bars",
1534 	"Horizontal increment",
1535 	"Vertical increment",
1536 };
1537 
1538 int s3c_camif_create_subdev(struct camif_dev *camif)
1539 {
1540 	struct v4l2_ctrl_handler *handler = &camif->ctrl_handler;
1541 	struct v4l2_subdev *sd = &camif->subdev;
1542 	int ret;
1543 
1544 	v4l2_subdev_init(sd, &s3c_camif_subdev_ops);
1545 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1546 	strscpy(sd->name, "S3C-CAMIF", sizeof(sd->name));
1547 
1548 	camif->pads[CAMIF_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1549 	camif->pads[CAMIF_SD_PAD_SOURCE_C].flags = MEDIA_PAD_FL_SOURCE;
1550 	camif->pads[CAMIF_SD_PAD_SOURCE_P].flags = MEDIA_PAD_FL_SOURCE;
1551 
1552 	ret = media_entity_pads_init(&sd->entity, CAMIF_SD_PADS_NUM,
1553 				camif->pads);
1554 	if (ret)
1555 		return ret;
1556 
1557 	v4l2_ctrl_handler_init(handler, 3);
1558 	camif->ctrl_test_pattern = v4l2_ctrl_new_std_menu_items(handler,
1559 			&s3c_camif_subdev_ctrl_ops, V4L2_CID_TEST_PATTERN,
1560 			ARRAY_SIZE(s3c_camif_test_pattern_menu) - 1, 0, 0,
1561 			s3c_camif_test_pattern_menu);
1562 
1563 	if (camif->variant->has_img_effect) {
1564 		camif->ctrl_colorfx = v4l2_ctrl_new_std_menu(handler,
1565 				&s3c_camif_subdev_ctrl_ops,
1566 				V4L2_CID_COLORFX, V4L2_COLORFX_SET_CBCR,
1567 				~0x981f, V4L2_COLORFX_NONE);
1568 
1569 		camif->ctrl_colorfx_cbcr = v4l2_ctrl_new_std(handler,
1570 				&s3c_camif_subdev_ctrl_ops,
1571 				V4L2_CID_COLORFX_CBCR, 0, 0xffff, 1, 0);
1572 	}
1573 
1574 	if (handler->error) {
1575 		v4l2_ctrl_handler_free(handler);
1576 		media_entity_cleanup(&sd->entity);
1577 		return handler->error;
1578 	}
1579 
1580 	if (camif->variant->has_img_effect)
1581 		v4l2_ctrl_auto_cluster(2, &camif->ctrl_colorfx,
1582 			       V4L2_COLORFX_SET_CBCR, false);
1583 
1584 	sd->ctrl_handler = handler;
1585 	v4l2_set_subdevdata(sd, camif);
1586 
1587 	return 0;
1588 }
1589 
1590 void s3c_camif_unregister_subdev(struct camif_dev *camif)
1591 {
1592 	struct v4l2_subdev *sd = &camif->subdev;
1593 
1594 	/* Return if not registered */
1595 	if (v4l2_get_subdevdata(sd) == NULL)
1596 		return;
1597 
1598 	v4l2_device_unregister_subdev(sd);
1599 	media_entity_cleanup(&sd->entity);
1600 	v4l2_ctrl_handler_free(&camif->ctrl_handler);
1601 	v4l2_set_subdevdata(sd, NULL);
1602 }
1603 
1604 int s3c_camif_set_defaults(struct camif_dev *camif)
1605 {
1606 	unsigned int ip_rev = camif->variant->ip_revision;
1607 	int i;
1608 
1609 	for (i = 0; i < CAMIF_VP_NUM; i++) {
1610 		struct camif_vp *vp = &camif->vp[i];
1611 		struct camif_frame *f = &vp->out_frame;
1612 
1613 		vp->camif = camif;
1614 		vp->id = i;
1615 		vp->offset = camif->variant->vp_offset;
1616 
1617 		if (ip_rev == S3C244X_CAMIF_IP_REV)
1618 			vp->fmt_flags = i ? FMT_FL_S3C24XX_PREVIEW :
1619 					FMT_FL_S3C24XX_CODEC;
1620 		else
1621 			vp->fmt_flags = FMT_FL_S3C64XX;
1622 
1623 		vp->out_fmt = s3c_camif_find_format(vp, NULL, 0);
1624 		BUG_ON(vp->out_fmt == NULL);
1625 
1626 		memset(f, 0, sizeof(*f));
1627 		f->f_width = CAMIF_DEF_WIDTH;
1628 		f->f_height = CAMIF_DEF_HEIGHT;
1629 		f->rect.width = CAMIF_DEF_WIDTH;
1630 		f->rect.height = CAMIF_DEF_HEIGHT;
1631 
1632 		/* Scaler is always enabled */
1633 		vp->scaler.enable = 1;
1634 
1635 		vp->payload = (f->f_width * f->f_height *
1636 			       vp->out_fmt->depth) / 8;
1637 	}
1638 
1639 	memset(&camif->mbus_fmt, 0, sizeof(camif->mbus_fmt));
1640 	camif->mbus_fmt.width = CAMIF_DEF_WIDTH;
1641 	camif->mbus_fmt.height = CAMIF_DEF_HEIGHT;
1642 	camif->mbus_fmt.code  = camif_mbus_formats[0];
1643 
1644 	memset(&camif->camif_crop, 0, sizeof(camif->camif_crop));
1645 	camif->camif_crop.width = CAMIF_DEF_WIDTH;
1646 	camif->camif_crop.height = CAMIF_DEF_HEIGHT;
1647 
1648 	return 0;
1649 }
1650