ARM: fix put_user() for gcc-8
[linux/fpc-iii.git] / drivers / media / platform / s3c-camif / camif-capture.c
blobfa6af4a7dae1fa6788ce2ff2d8104c691f0b1fae
1 /*
2 * s3c24xx/s3c64xx SoC series Camera Interface (CAMIF) driver
4 * Copyright (C) 2012 Sylwester Nawrocki <sylvester.nawrocki@gmail.com>
5 * Copyright (C) 2012 Tomasz Figa <tomasz.figa@gmail.com>
7 * Based on drivers/media/platform/s5p-fimc,
8 * Copyright (C) 2010 - 2012 Samsung Electronics Co., Ltd.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 #define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__
16 #include <linux/bug.h>
17 #include <linux/clk.h>
18 #include <linux/device.h>
19 #include <linux/errno.h>
20 #include <linux/i2c.h>
21 #include <linux/interrupt.h>
22 #include <linux/io.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/ratelimit.h>
29 #include <linux/slab.h>
30 #include <linux/types.h>
31 #include <linux/videodev2.h>
33 #include <media/media-device.h>
34 #include <media/v4l2-ctrls.h>
35 #include <media/v4l2-event.h>
36 #include <media/v4l2-ioctl.h>
37 #include <media/videobuf2-v4l2.h>
38 #include <media/videobuf2-dma-contig.h>
40 #include "camif-core.h"
41 #include "camif-regs.h"
43 static int debug;
44 module_param(debug, int, 0644);
46 /* Locking: called with vp->camif->slock spinlock held */
47 static void camif_cfg_video_path(struct camif_vp *vp)
49 WARN_ON(s3c_camif_get_scaler_config(vp, &vp->scaler));
50 camif_hw_set_scaler(vp);
51 camif_hw_set_flip(vp);
52 camif_hw_set_target_format(vp);
53 camif_hw_set_output_dma(vp);
56 static void camif_prepare_dma_offset(struct camif_vp *vp)
58 struct camif_frame *f = &vp->out_frame;
60 f->dma_offset.initial = f->rect.top * f->f_width + f->rect.left;
61 f->dma_offset.line = f->f_width - (f->rect.left + f->rect.width);
63 pr_debug("dma_offset: initial: %d, line: %d\n",
64 f->dma_offset.initial, f->dma_offset.line);
67 /* Locking: called with camif->slock spinlock held */
68 static int s3c_camif_hw_init(struct camif_dev *camif, struct camif_vp *vp)
70 const struct s3c_camif_variant *variant = camif->variant;
72 if (camif->sensor.sd == NULL || vp->out_fmt == NULL)
73 return -EINVAL;
75 if (variant->ip_revision == S3C244X_CAMIF_IP_REV)
76 camif_hw_clear_fifo_overflow(vp);
77 camif_hw_set_camera_bus(camif);
78 camif_hw_set_source_format(camif);
79 camif_hw_set_camera_crop(camif);
80 camif_hw_set_test_pattern(camif, camif->test_pattern);
81 if (variant->has_img_effect)
82 camif_hw_set_effect(camif, camif->colorfx,
83 camif->colorfx_cb, camif->colorfx_cr);
84 if (variant->ip_revision == S3C6410_CAMIF_IP_REV)
85 camif_hw_set_input_path(vp);
86 camif_cfg_video_path(vp);
87 vp->state &= ~ST_VP_CONFIG;
89 return 0;
93 * Initialize the video path, only up from the scaler stage. The camera
94 * input interface set up is skipped. This is useful to enable one of the
95 * video paths when the other is already running.
96 * Locking: called with camif->slock spinlock held.
98 static int s3c_camif_hw_vp_init(struct camif_dev *camif, struct camif_vp *vp)
100 unsigned int ip_rev = camif->variant->ip_revision;
102 if (vp->out_fmt == NULL)
103 return -EINVAL;
105 camif_prepare_dma_offset(vp);
106 if (ip_rev == S3C244X_CAMIF_IP_REV)
107 camif_hw_clear_fifo_overflow(vp);
108 camif_cfg_video_path(vp);
109 vp->state &= ~ST_VP_CONFIG;
110 return 0;
113 static int sensor_set_power(struct camif_dev *camif, int on)
115 struct cam_sensor *sensor = &camif->sensor;
116 int err = 0;
118 if (camif->sensor.power_count == !on)
119 err = v4l2_subdev_call(sensor->sd, core, s_power, on);
120 if (!err)
121 sensor->power_count += on ? 1 : -1;
123 pr_debug("on: %d, power_count: %d, err: %d\n",
124 on, sensor->power_count, err);
126 return err;
129 static int sensor_set_streaming(struct camif_dev *camif, int on)
131 struct cam_sensor *sensor = &camif->sensor;
132 int err = 0;
134 if (camif->sensor.stream_count == !on)
135 err = v4l2_subdev_call(sensor->sd, video, s_stream, on);
136 if (!err)
137 sensor->stream_count += on ? 1 : -1;
139 pr_debug("on: %d, stream_count: %d, err: %d\n",
140 on, sensor->stream_count, err);
142 return err;
146 * Reinitialize the driver so it is ready to start streaming again.
147 * Return any buffers to vb2, perform CAMIF software reset and
148 * turn off streaming at the data pipeline (sensor) if required.
150 static int camif_reinitialize(struct camif_vp *vp)
152 struct camif_dev *camif = vp->camif;
153 struct camif_buffer *buf;
154 unsigned long flags;
155 bool streaming;
157 spin_lock_irqsave(&camif->slock, flags);
158 streaming = vp->state & ST_VP_SENSOR_STREAMING;
160 vp->state &= ~(ST_VP_PENDING | ST_VP_RUNNING | ST_VP_OFF |
161 ST_VP_ABORTING | ST_VP_STREAMING |
162 ST_VP_SENSOR_STREAMING | ST_VP_LASTIRQ);
164 /* Release unused buffers */
165 while (!list_empty(&vp->pending_buf_q)) {
166 buf = camif_pending_queue_pop(vp);
167 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
170 while (!list_empty(&vp->active_buf_q)) {
171 buf = camif_active_queue_pop(vp);
172 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
175 spin_unlock_irqrestore(&camif->slock, flags);
177 if (!streaming)
178 return 0;
180 return sensor_set_streaming(camif, 0);
183 static bool s3c_vp_active(struct camif_vp *vp)
185 struct camif_dev *camif = vp->camif;
186 unsigned long flags;
187 bool ret;
189 spin_lock_irqsave(&camif->slock, flags);
190 ret = (vp->state & ST_VP_RUNNING) || (vp->state & ST_VP_PENDING);
191 spin_unlock_irqrestore(&camif->slock, flags);
193 return ret;
196 static bool camif_is_streaming(struct camif_dev *camif)
198 unsigned long flags;
199 bool status;
201 spin_lock_irqsave(&camif->slock, flags);
202 status = camif->stream_count > 0;
203 spin_unlock_irqrestore(&camif->slock, flags);
205 return status;
208 static int camif_stop_capture(struct camif_vp *vp)
210 struct camif_dev *camif = vp->camif;
211 unsigned long flags;
212 int ret;
214 if (!s3c_vp_active(vp))
215 return 0;
217 spin_lock_irqsave(&camif->slock, flags);
218 vp->state &= ~(ST_VP_OFF | ST_VP_LASTIRQ);
219 vp->state |= ST_VP_ABORTING;
220 spin_unlock_irqrestore(&camif->slock, flags);
222 ret = wait_event_timeout(vp->irq_queue,
223 !(vp->state & ST_VP_ABORTING),
224 msecs_to_jiffies(CAMIF_STOP_TIMEOUT));
226 spin_lock_irqsave(&camif->slock, flags);
228 if (ret == 0 && !(vp->state & ST_VP_OFF)) {
229 /* Timed out, forcibly stop capture */
230 vp->state &= ~(ST_VP_OFF | ST_VP_ABORTING |
231 ST_VP_LASTIRQ);
233 camif_hw_disable_capture(vp);
234 camif_hw_enable_scaler(vp, false);
237 spin_unlock_irqrestore(&camif->slock, flags);
239 return camif_reinitialize(vp);
242 static int camif_prepare_addr(struct camif_vp *vp, struct vb2_buffer *vb,
243 struct camif_addr *paddr)
245 struct camif_frame *frame = &vp->out_frame;
246 u32 pix_size;
248 if (vb == NULL || frame == NULL)
249 return -EINVAL;
251 pix_size = frame->rect.width * frame->rect.height;
253 pr_debug("colplanes: %d, pix_size: %u\n",
254 vp->out_fmt->colplanes, pix_size);
256 paddr->y = vb2_dma_contig_plane_dma_addr(vb, 0);
258 switch (vp->out_fmt->colplanes) {
259 case 1:
260 paddr->cb = 0;
261 paddr->cr = 0;
262 break;
263 case 2:
264 /* decompose Y into Y/Cb */
265 paddr->cb = (u32)(paddr->y + pix_size);
266 paddr->cr = 0;
267 break;
268 case 3:
269 paddr->cb = (u32)(paddr->y + pix_size);
270 /* decompose Y into Y/Cb/Cr */
271 if (vp->out_fmt->color == IMG_FMT_YCBCR422P)
272 paddr->cr = (u32)(paddr->cb + (pix_size >> 1));
273 else /* 420 */
274 paddr->cr = (u32)(paddr->cb + (pix_size >> 2));
276 if (vp->out_fmt->color == IMG_FMT_YCRCB420)
277 swap(paddr->cb, paddr->cr);
278 break;
279 default:
280 return -EINVAL;
283 pr_debug("DMA address: y: %pad cb: %pad cr: %pad\n",
284 &paddr->y, &paddr->cb, &paddr->cr);
286 return 0;
289 irqreturn_t s3c_camif_irq_handler(int irq, void *priv)
291 struct camif_vp *vp = priv;
292 struct camif_dev *camif = vp->camif;
293 unsigned int ip_rev = camif->variant->ip_revision;
294 unsigned int status;
296 spin_lock(&camif->slock);
298 if (ip_rev == S3C6410_CAMIF_IP_REV)
299 camif_hw_clear_pending_irq(vp);
301 status = camif_hw_get_status(vp);
303 if (ip_rev == S3C244X_CAMIF_IP_REV && (status & CISTATUS_OVF_MASK)) {
304 camif_hw_clear_fifo_overflow(vp);
305 goto unlock;
308 if (vp->state & ST_VP_ABORTING) {
309 if (vp->state & ST_VP_OFF) {
310 /* Last IRQ */
311 vp->state &= ~(ST_VP_OFF | ST_VP_ABORTING |
312 ST_VP_LASTIRQ);
313 wake_up(&vp->irq_queue);
314 goto unlock;
315 } else if (vp->state & ST_VP_LASTIRQ) {
316 camif_hw_disable_capture(vp);
317 camif_hw_enable_scaler(vp, false);
318 camif_hw_set_lastirq(vp, false);
319 vp->state |= ST_VP_OFF;
320 } else {
321 /* Disable capture, enable last IRQ */
322 camif_hw_set_lastirq(vp, true);
323 vp->state |= ST_VP_LASTIRQ;
327 if (!list_empty(&vp->pending_buf_q) && (vp->state & ST_VP_RUNNING) &&
328 !list_empty(&vp->active_buf_q)) {
329 unsigned int index;
330 struct camif_buffer *vbuf;
332 * Get previous DMA write buffer index:
333 * 0 => DMA buffer 0, 2;
334 * 1 => DMA buffer 1, 3.
336 index = (CISTATUS_FRAMECNT(status) + 2) & 1;
337 vbuf = camif_active_queue_peek(vp, index);
339 if (!WARN_ON(vbuf == NULL)) {
340 /* Dequeue a filled buffer */
341 v4l2_get_timestamp(&vbuf->vb.timestamp);
342 vbuf->vb.sequence = vp->frame_sequence++;
343 vb2_buffer_done(&vbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);
345 /* Set up an empty buffer at the DMA engine */
346 vbuf = camif_pending_queue_pop(vp);
347 vbuf->index = index;
348 camif_hw_set_output_addr(vp, &vbuf->paddr, index);
349 camif_hw_set_output_addr(vp, &vbuf->paddr, index + 2);
351 /* Scheduled in H/W, add to the queue */
352 camif_active_queue_add(vp, vbuf);
354 } else if (!(vp->state & ST_VP_ABORTING) &&
355 (vp->state & ST_VP_PENDING)) {
356 vp->state |= ST_VP_RUNNING;
359 if (vp->state & ST_VP_CONFIG) {
360 camif_prepare_dma_offset(vp);
361 camif_hw_set_camera_crop(camif);
362 camif_hw_set_scaler(vp);
363 camif_hw_set_flip(vp);
364 camif_hw_set_test_pattern(camif, camif->test_pattern);
365 if (camif->variant->has_img_effect)
366 camif_hw_set_effect(camif, camif->colorfx,
367 camif->colorfx_cb, camif->colorfx_cr);
368 vp->state &= ~ST_VP_CONFIG;
370 unlock:
371 spin_unlock(&camif->slock);
372 return IRQ_HANDLED;
375 static int start_streaming(struct vb2_queue *vq, unsigned int count)
377 struct camif_vp *vp = vb2_get_drv_priv(vq);
378 struct camif_dev *camif = vp->camif;
379 unsigned long flags;
380 int ret;
383 * We assume the codec capture path is always activated
384 * first, before the preview path starts streaming.
385 * This is required to avoid internal FIFO overflow and
386 * a need for CAMIF software reset.
388 spin_lock_irqsave(&camif->slock, flags);
390 if (camif->stream_count == 0) {
391 camif_hw_reset(camif);
392 ret = s3c_camif_hw_init(camif, vp);
393 } else {
394 ret = s3c_camif_hw_vp_init(camif, vp);
396 spin_unlock_irqrestore(&camif->slock, flags);
398 if (ret < 0) {
399 camif_reinitialize(vp);
400 return ret;
403 spin_lock_irqsave(&camif->slock, flags);
404 vp->frame_sequence = 0;
405 vp->state |= ST_VP_PENDING;
407 if (!list_empty(&vp->pending_buf_q) &&
408 (!(vp->state & ST_VP_STREAMING) ||
409 !(vp->state & ST_VP_SENSOR_STREAMING))) {
411 camif_hw_enable_scaler(vp, vp->scaler.enable);
412 camif_hw_enable_capture(vp);
413 vp->state |= ST_VP_STREAMING;
415 if (!(vp->state & ST_VP_SENSOR_STREAMING)) {
416 vp->state |= ST_VP_SENSOR_STREAMING;
417 spin_unlock_irqrestore(&camif->slock, flags);
418 ret = sensor_set_streaming(camif, 1);
419 if (ret)
420 v4l2_err(&vp->vdev, "Sensor s_stream failed\n");
421 if (debug)
422 camif_hw_dump_regs(camif, __func__);
424 return ret;
428 spin_unlock_irqrestore(&camif->slock, flags);
429 return 0;
432 static void stop_streaming(struct vb2_queue *vq)
434 struct camif_vp *vp = vb2_get_drv_priv(vq);
435 camif_stop_capture(vp);
438 static int queue_setup(struct vb2_queue *vq, const void *parg,
439 unsigned int *num_buffers, unsigned int *num_planes,
440 unsigned int sizes[], void *allocators[])
442 const struct v4l2_format *pfmt = parg;
443 const struct v4l2_pix_format *pix = NULL;
444 struct camif_vp *vp = vb2_get_drv_priv(vq);
445 struct camif_dev *camif = vp->camif;
446 struct camif_frame *frame = &vp->out_frame;
447 const struct camif_fmt *fmt;
448 unsigned int size;
450 if (pfmt) {
451 pix = &pfmt->fmt.pix;
452 fmt = s3c_camif_find_format(vp, &pix->pixelformat, -1);
453 if (fmt == NULL)
454 return -EINVAL;
455 size = (pix->width * pix->height * fmt->depth) / 8;
456 } else {
457 fmt = vp->out_fmt;
458 if (fmt == NULL)
459 return -EINVAL;
460 size = (frame->f_width * frame->f_height * fmt->depth) / 8;
463 *num_planes = 1;
465 if (pix)
466 sizes[0] = max(size, pix->sizeimage);
467 else
468 sizes[0] = size;
469 allocators[0] = camif->alloc_ctx;
471 pr_debug("size: %u\n", sizes[0]);
472 return 0;
475 static int buffer_prepare(struct vb2_buffer *vb)
477 struct camif_vp *vp = vb2_get_drv_priv(vb->vb2_queue);
479 if (vp->out_fmt == NULL)
480 return -EINVAL;
482 if (vb2_plane_size(vb, 0) < vp->payload) {
483 v4l2_err(&vp->vdev, "buffer too small: %lu, required: %u\n",
484 vb2_plane_size(vb, 0), vp->payload);
485 return -EINVAL;
487 vb2_set_plane_payload(vb, 0, vp->payload);
489 return 0;
492 static void buffer_queue(struct vb2_buffer *vb)
494 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
495 struct camif_buffer *buf = container_of(vbuf, struct camif_buffer, vb);
496 struct camif_vp *vp = vb2_get_drv_priv(vb->vb2_queue);
497 struct camif_dev *camif = vp->camif;
498 unsigned long flags;
500 spin_lock_irqsave(&camif->slock, flags);
501 WARN_ON(camif_prepare_addr(vp, &buf->vb.vb2_buf, &buf->paddr));
503 if (!(vp->state & ST_VP_STREAMING) && vp->active_buffers < 2) {
504 /* Schedule an empty buffer in H/W */
505 buf->index = vp->buf_index;
507 camif_hw_set_output_addr(vp, &buf->paddr, buf->index);
508 camif_hw_set_output_addr(vp, &buf->paddr, buf->index + 2);
510 camif_active_queue_add(vp, buf);
511 vp->buf_index = !vp->buf_index;
512 } else {
513 camif_pending_queue_add(vp, buf);
516 if (vb2_is_streaming(&vp->vb_queue) && !list_empty(&vp->pending_buf_q)
517 && !(vp->state & ST_VP_STREAMING)) {
519 vp->state |= ST_VP_STREAMING;
520 camif_hw_enable_scaler(vp, vp->scaler.enable);
521 camif_hw_enable_capture(vp);
522 spin_unlock_irqrestore(&camif->slock, flags);
524 if (!(vp->state & ST_VP_SENSOR_STREAMING)) {
525 if (sensor_set_streaming(camif, 1) == 0)
526 vp->state |= ST_VP_SENSOR_STREAMING;
527 else
528 v4l2_err(&vp->vdev, "Sensor s_stream failed\n");
530 if (debug)
531 camif_hw_dump_regs(camif, __func__);
533 return;
535 spin_unlock_irqrestore(&camif->slock, flags);
538 static const struct vb2_ops s3c_camif_qops = {
539 .queue_setup = queue_setup,
540 .buf_prepare = buffer_prepare,
541 .buf_queue = buffer_queue,
542 .wait_prepare = vb2_ops_wait_prepare,
543 .wait_finish = vb2_ops_wait_finish,
544 .start_streaming = start_streaming,
545 .stop_streaming = stop_streaming,
548 static int s3c_camif_open(struct file *file)
550 struct camif_vp *vp = video_drvdata(file);
551 struct camif_dev *camif = vp->camif;
552 int ret;
554 pr_debug("[vp%d] state: %#x, owner: %p, pid: %d\n", vp->id,
555 vp->state, vp->owner, task_pid_nr(current));
557 if (mutex_lock_interruptible(&camif->lock))
558 return -ERESTARTSYS;
560 ret = v4l2_fh_open(file);
561 if (ret < 0)
562 goto unlock;
564 ret = pm_runtime_get_sync(camif->dev);
565 if (ret < 0)
566 goto err_pm;
568 ret = sensor_set_power(camif, 1);
569 if (!ret)
570 goto unlock;
572 pm_runtime_put(camif->dev);
573 err_pm:
574 v4l2_fh_release(file);
575 unlock:
576 mutex_unlock(&camif->lock);
577 return ret;
580 static int s3c_camif_close(struct file *file)
582 struct camif_vp *vp = video_drvdata(file);
583 struct camif_dev *camif = vp->camif;
584 int ret;
586 pr_debug("[vp%d] state: %#x, owner: %p, pid: %d\n", vp->id,
587 vp->state, vp->owner, task_pid_nr(current));
589 mutex_lock(&camif->lock);
591 if (vp->owner == file->private_data) {
592 camif_stop_capture(vp);
593 vb2_queue_release(&vp->vb_queue);
594 vp->owner = NULL;
597 sensor_set_power(camif, 0);
599 pm_runtime_put(camif->dev);
600 ret = v4l2_fh_release(file);
602 mutex_unlock(&camif->lock);
603 return ret;
606 static unsigned int s3c_camif_poll(struct file *file,
607 struct poll_table_struct *wait)
609 struct camif_vp *vp = video_drvdata(file);
610 struct camif_dev *camif = vp->camif;
611 int ret;
613 mutex_lock(&camif->lock);
614 if (vp->owner && vp->owner != file->private_data)
615 ret = -EBUSY;
616 else
617 ret = vb2_poll(&vp->vb_queue, file, wait);
619 mutex_unlock(&camif->lock);
620 return ret;
623 static int s3c_camif_mmap(struct file *file, struct vm_area_struct *vma)
625 struct camif_vp *vp = video_drvdata(file);
626 int ret;
628 if (vp->owner && vp->owner != file->private_data)
629 ret = -EBUSY;
630 else
631 ret = vb2_mmap(&vp->vb_queue, vma);
633 return ret;
636 static const struct v4l2_file_operations s3c_camif_fops = {
637 .owner = THIS_MODULE,
638 .open = s3c_camif_open,
639 .release = s3c_camif_close,
640 .poll = s3c_camif_poll,
641 .unlocked_ioctl = video_ioctl2,
642 .mmap = s3c_camif_mmap,
646 * Video node IOCTLs
649 static int s3c_camif_vidioc_querycap(struct file *file, void *priv,
650 struct v4l2_capability *cap)
652 struct camif_vp *vp = video_drvdata(file);
654 strlcpy(cap->driver, S3C_CAMIF_DRIVER_NAME, sizeof(cap->driver));
655 strlcpy(cap->card, S3C_CAMIF_DRIVER_NAME, sizeof(cap->card));
656 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s.%d",
657 dev_name(vp->camif->dev), vp->id);
659 cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
660 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
662 return 0;
665 static int s3c_camif_vidioc_enum_input(struct file *file, void *priv,
666 struct v4l2_input *input)
668 struct camif_vp *vp = video_drvdata(file);
669 struct v4l2_subdev *sensor = vp->camif->sensor.sd;
671 if (input->index || sensor == NULL)
672 return -EINVAL;
674 input->type = V4L2_INPUT_TYPE_CAMERA;
675 strlcpy(input->name, sensor->name, sizeof(input->name));
676 return 0;
679 static int s3c_camif_vidioc_s_input(struct file *file, void *priv,
680 unsigned int i)
682 return i == 0 ? 0 : -EINVAL;
685 static int s3c_camif_vidioc_g_input(struct file *file, void *priv,
686 unsigned int *i)
688 *i = 0;
689 return 0;
692 static int s3c_camif_vidioc_enum_fmt(struct file *file, void *priv,
693 struct v4l2_fmtdesc *f)
695 struct camif_vp *vp = video_drvdata(file);
696 const struct camif_fmt *fmt;
698 fmt = s3c_camif_find_format(vp, NULL, f->index);
699 if (!fmt)
700 return -EINVAL;
702 strlcpy(f->description, fmt->name, sizeof(f->description));
703 f->pixelformat = fmt->fourcc;
705 pr_debug("fmt(%d): %s\n", f->index, f->description);
706 return 0;
709 static int s3c_camif_vidioc_g_fmt(struct file *file, void *priv,
710 struct v4l2_format *f)
712 struct camif_vp *vp = video_drvdata(file);
713 struct v4l2_pix_format *pix = &f->fmt.pix;
714 struct camif_frame *frame = &vp->out_frame;
715 const struct camif_fmt *fmt = vp->out_fmt;
717 pix->bytesperline = frame->f_width * fmt->ybpp;
718 pix->sizeimage = vp->payload;
720 pix->pixelformat = fmt->fourcc;
721 pix->width = frame->f_width;
722 pix->height = frame->f_height;
723 pix->field = V4L2_FIELD_NONE;
724 pix->colorspace = V4L2_COLORSPACE_JPEG;
726 return 0;
729 static int __camif_video_try_format(struct camif_vp *vp,
730 struct v4l2_pix_format *pix,
731 const struct camif_fmt **ffmt)
733 struct camif_dev *camif = vp->camif;
734 struct v4l2_rect *crop = &camif->camif_crop;
735 unsigned int wmin, hmin, sc_hrmax, sc_vrmax;
736 const struct vp_pix_limits *pix_lim;
737 const struct camif_fmt *fmt;
739 fmt = s3c_camif_find_format(vp, &pix->pixelformat, 0);
741 if (WARN_ON(fmt == NULL))
742 return -EINVAL;
744 if (ffmt)
745 *ffmt = fmt;
747 pix_lim = &camif->variant->vp_pix_limits[vp->id];
749 pr_debug("fmt: %ux%u, crop: %ux%u, bytesperline: %u\n",
750 pix->width, pix->height, crop->width, crop->height,
751 pix->bytesperline);
753 * Calculate minimum width and height according to the configured
754 * camera input interface crop rectangle and the resizer's capabilities.
756 sc_hrmax = min(SCALER_MAX_RATIO, 1 << (ffs(crop->width) - 3));
757 sc_vrmax = min(SCALER_MAX_RATIO, 1 << (ffs(crop->height) - 1));
759 wmin = max_t(u32, pix_lim->min_out_width, crop->width / sc_hrmax);
760 wmin = round_up(wmin, pix_lim->out_width_align);
761 hmin = max_t(u32, 8, crop->height / sc_vrmax);
762 hmin = round_up(hmin, 8);
764 v4l_bound_align_image(&pix->width, wmin, pix_lim->max_sc_out_width,
765 ffs(pix_lim->out_width_align) - 1,
766 &pix->height, hmin, pix_lim->max_height, 0, 0);
768 pix->bytesperline = pix->width * fmt->ybpp;
769 pix->sizeimage = (pix->width * pix->height * fmt->depth) / 8;
770 pix->pixelformat = fmt->fourcc;
771 pix->colorspace = V4L2_COLORSPACE_JPEG;
772 pix->field = V4L2_FIELD_NONE;
774 pr_debug("%ux%u, wmin: %d, hmin: %d, sc_hrmax: %d, sc_vrmax: %d\n",
775 pix->width, pix->height, wmin, hmin, sc_hrmax, sc_vrmax);
777 return 0;
780 static int s3c_camif_vidioc_try_fmt(struct file *file, void *priv,
781 struct v4l2_format *f)
783 struct camif_vp *vp = video_drvdata(file);
784 return __camif_video_try_format(vp, &f->fmt.pix, NULL);
787 static int s3c_camif_vidioc_s_fmt(struct file *file, void *priv,
788 struct v4l2_format *f)
790 struct v4l2_pix_format *pix = &f->fmt.pix;
791 struct camif_vp *vp = video_drvdata(file);
792 struct camif_frame *out_frame = &vp->out_frame;
793 const struct camif_fmt *fmt = NULL;
794 int ret;
796 pr_debug("[vp%d]\n", vp->id);
798 if (vb2_is_busy(&vp->vb_queue))
799 return -EBUSY;
801 ret = __camif_video_try_format(vp, &f->fmt.pix, &fmt);
802 if (ret < 0)
803 return ret;
805 vp->out_fmt = fmt;
806 vp->payload = pix->sizeimage;
807 out_frame->f_width = pix->width;
808 out_frame->f_height = pix->height;
810 /* Reset composition rectangle */
811 out_frame->rect.width = pix->width;
812 out_frame->rect.height = pix->height;
813 out_frame->rect.left = 0;
814 out_frame->rect.top = 0;
816 if (vp->owner == NULL)
817 vp->owner = priv;
819 pr_debug("%ux%u. payload: %u. fmt: %s. %d %d. sizeimage: %d. bpl: %d\n",
820 out_frame->f_width, out_frame->f_height, vp->payload, fmt->name,
821 pix->width * pix->height * fmt->depth, fmt->depth,
822 pix->sizeimage, pix->bytesperline);
824 return 0;
827 /* Only check pixel formats at the sensor and the camif subdev pads */
828 static int camif_pipeline_validate(struct camif_dev *camif)
830 struct v4l2_subdev_format src_fmt;
831 struct media_pad *pad;
832 int ret;
834 /* Retrieve format at the sensor subdev source pad */
835 pad = media_entity_remote_pad(&camif->pads[0]);
836 if (!pad || media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
837 return -EPIPE;
839 src_fmt.pad = pad->index;
840 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
841 ret = v4l2_subdev_call(camif->sensor.sd, pad, get_fmt, NULL, &src_fmt);
842 if (ret < 0 && ret != -ENOIOCTLCMD)
843 return -EPIPE;
845 if (src_fmt.format.width != camif->mbus_fmt.width ||
846 src_fmt.format.height != camif->mbus_fmt.height ||
847 src_fmt.format.code != camif->mbus_fmt.code)
848 return -EPIPE;
850 return 0;
853 static int s3c_camif_streamon(struct file *file, void *priv,
854 enum v4l2_buf_type type)
856 struct camif_vp *vp = video_drvdata(file);
857 struct camif_dev *camif = vp->camif;
858 struct media_entity *sensor = &camif->sensor.sd->entity;
859 int ret;
861 pr_debug("[vp%d]\n", vp->id);
863 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
864 return -EINVAL;
866 if (vp->owner && vp->owner != priv)
867 return -EBUSY;
869 if (s3c_vp_active(vp))
870 return 0;
872 ret = media_entity_pipeline_start(sensor, camif->m_pipeline);
873 if (ret < 0)
874 return ret;
876 ret = camif_pipeline_validate(camif);
877 if (ret < 0) {
878 media_entity_pipeline_stop(sensor);
879 return ret;
882 return vb2_streamon(&vp->vb_queue, type);
885 static int s3c_camif_streamoff(struct file *file, void *priv,
886 enum v4l2_buf_type type)
888 struct camif_vp *vp = video_drvdata(file);
889 struct camif_dev *camif = vp->camif;
890 int ret;
892 pr_debug("[vp%d]\n", vp->id);
894 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
895 return -EINVAL;
897 if (vp->owner && vp->owner != priv)
898 return -EBUSY;
900 ret = vb2_streamoff(&vp->vb_queue, type);
901 if (ret == 0)
902 media_entity_pipeline_stop(&camif->sensor.sd->entity);
903 return ret;
906 static int s3c_camif_reqbufs(struct file *file, void *priv,
907 struct v4l2_requestbuffers *rb)
909 struct camif_vp *vp = video_drvdata(file);
910 int ret;
912 pr_debug("[vp%d] rb count: %d, owner: %p, priv: %p\n",
913 vp->id, rb->count, vp->owner, priv);
915 if (vp->owner && vp->owner != priv)
916 return -EBUSY;
918 if (rb->count)
919 rb->count = max_t(u32, CAMIF_REQ_BUFS_MIN, rb->count);
920 else
921 vp->owner = NULL;
923 ret = vb2_reqbufs(&vp->vb_queue, rb);
924 if (ret < 0)
925 return ret;
927 if (rb->count && rb->count < CAMIF_REQ_BUFS_MIN) {
928 rb->count = 0;
929 vb2_reqbufs(&vp->vb_queue, rb);
930 ret = -ENOMEM;
933 vp->reqbufs_count = rb->count;
934 if (vp->owner == NULL && rb->count > 0)
935 vp->owner = priv;
937 return ret;
940 static int s3c_camif_querybuf(struct file *file, void *priv,
941 struct v4l2_buffer *buf)
943 struct camif_vp *vp = video_drvdata(file);
944 return vb2_querybuf(&vp->vb_queue, buf);
947 static int s3c_camif_qbuf(struct file *file, void *priv,
948 struct v4l2_buffer *buf)
950 struct camif_vp *vp = video_drvdata(file);
952 pr_debug("[vp%d]\n", vp->id);
954 if (vp->owner && vp->owner != priv)
955 return -EBUSY;
957 return vb2_qbuf(&vp->vb_queue, buf);
960 static int s3c_camif_dqbuf(struct file *file, void *priv,
961 struct v4l2_buffer *buf)
963 struct camif_vp *vp = video_drvdata(file);
965 pr_debug("[vp%d] sequence: %d\n", vp->id, vp->frame_sequence);
967 if (vp->owner && vp->owner != priv)
968 return -EBUSY;
970 return vb2_dqbuf(&vp->vb_queue, buf, file->f_flags & O_NONBLOCK);
973 static int s3c_camif_create_bufs(struct file *file, void *priv,
974 struct v4l2_create_buffers *create)
976 struct camif_vp *vp = video_drvdata(file);
977 int ret;
979 if (vp->owner && vp->owner != priv)
980 return -EBUSY;
982 create->count = max_t(u32, 1, create->count);
983 ret = vb2_create_bufs(&vp->vb_queue, create);
985 if (!ret && vp->owner == NULL)
986 vp->owner = priv;
988 return ret;
991 static int s3c_camif_prepare_buf(struct file *file, void *priv,
992 struct v4l2_buffer *b)
994 struct camif_vp *vp = video_drvdata(file);
995 return vb2_prepare_buf(&vp->vb_queue, b);
998 static int s3c_camif_g_selection(struct file *file, void *priv,
999 struct v4l2_selection *sel)
1001 struct camif_vp *vp = video_drvdata(file);
1003 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1004 return -EINVAL;
1006 switch (sel->target) {
1007 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1008 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1009 sel->r.left = 0;
1010 sel->r.top = 0;
1011 sel->r.width = vp->out_frame.f_width;
1012 sel->r.height = vp->out_frame.f_height;
1013 return 0;
1015 case V4L2_SEL_TGT_COMPOSE:
1016 sel->r = vp->out_frame.rect;
1017 return 0;
1020 return -EINVAL;
1023 static void __camif_try_compose(struct camif_dev *camif, struct camif_vp *vp,
1024 struct v4l2_rect *r)
1026 /* s3c244x doesn't support composition */
1027 if (camif->variant->ip_revision == S3C244X_CAMIF_IP_REV) {
1028 *r = vp->out_frame.rect;
1029 return;
1032 /* TODO: s3c64xx */
1035 static int s3c_camif_s_selection(struct file *file, void *priv,
1036 struct v4l2_selection *sel)
1038 struct camif_vp *vp = video_drvdata(file);
1039 struct camif_dev *camif = vp->camif;
1040 struct v4l2_rect rect = sel->r;
1041 unsigned long flags;
1043 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1044 sel->target != V4L2_SEL_TGT_COMPOSE)
1045 return -EINVAL;
1047 __camif_try_compose(camif, vp, &rect);
1049 sel->r = rect;
1050 spin_lock_irqsave(&camif->slock, flags);
1051 vp->out_frame.rect = rect;
1052 vp->state |= ST_VP_CONFIG;
1053 spin_unlock_irqrestore(&camif->slock, flags);
1055 pr_debug("type: %#x, target: %#x, flags: %#x, (%d,%d)/%dx%d\n",
1056 sel->type, sel->target, sel->flags,
1057 sel->r.left, sel->r.top, sel->r.width, sel->r.height);
1059 return 0;
1062 static const struct v4l2_ioctl_ops s3c_camif_ioctl_ops = {
1063 .vidioc_querycap = s3c_camif_vidioc_querycap,
1064 .vidioc_enum_input = s3c_camif_vidioc_enum_input,
1065 .vidioc_g_input = s3c_camif_vidioc_g_input,
1066 .vidioc_s_input = s3c_camif_vidioc_s_input,
1067 .vidioc_enum_fmt_vid_cap = s3c_camif_vidioc_enum_fmt,
1068 .vidioc_try_fmt_vid_cap = s3c_camif_vidioc_try_fmt,
1069 .vidioc_s_fmt_vid_cap = s3c_camif_vidioc_s_fmt,
1070 .vidioc_g_fmt_vid_cap = s3c_camif_vidioc_g_fmt,
1071 .vidioc_g_selection = s3c_camif_g_selection,
1072 .vidioc_s_selection = s3c_camif_s_selection,
1073 .vidioc_reqbufs = s3c_camif_reqbufs,
1074 .vidioc_querybuf = s3c_camif_querybuf,
1075 .vidioc_prepare_buf = s3c_camif_prepare_buf,
1076 .vidioc_create_bufs = s3c_camif_create_bufs,
1077 .vidioc_qbuf = s3c_camif_qbuf,
1078 .vidioc_dqbuf = s3c_camif_dqbuf,
1079 .vidioc_streamon = s3c_camif_streamon,
1080 .vidioc_streamoff = s3c_camif_streamoff,
1081 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1082 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1083 .vidioc_log_status = v4l2_ctrl_log_status,
1087 * Video node controls
1089 static int s3c_camif_video_s_ctrl(struct v4l2_ctrl *ctrl)
1091 struct camif_vp *vp = ctrl->priv;
1092 struct camif_dev *camif = vp->camif;
1093 unsigned long flags;
1095 pr_debug("[vp%d] ctrl: %s, value: %d\n", vp->id,
1096 ctrl->name, ctrl->val);
1098 spin_lock_irqsave(&camif->slock, flags);
1100 switch (ctrl->id) {
1101 case V4L2_CID_HFLIP:
1102 vp->hflip = ctrl->val;
1103 break;
1105 case V4L2_CID_VFLIP:
1106 vp->vflip = ctrl->val;
1107 break;
1110 vp->state |= ST_VP_CONFIG;
1111 spin_unlock_irqrestore(&camif->slock, flags);
1112 return 0;
1115 /* Codec and preview video node control ops */
1116 static const struct v4l2_ctrl_ops s3c_camif_video_ctrl_ops = {
1117 .s_ctrl = s3c_camif_video_s_ctrl,
1120 int s3c_camif_register_video_node(struct camif_dev *camif, int idx)
1122 struct camif_vp *vp = &camif->vp[idx];
1123 struct vb2_queue *q = &vp->vb_queue;
1124 struct video_device *vfd = &vp->vdev;
1125 struct v4l2_ctrl *ctrl;
1126 int ret;
1128 memset(vfd, 0, sizeof(*vfd));
1129 snprintf(vfd->name, sizeof(vfd->name), "camif-%s",
1130 vp->id == 0 ? "codec" : "preview");
1132 vfd->fops = &s3c_camif_fops;
1133 vfd->ioctl_ops = &s3c_camif_ioctl_ops;
1134 vfd->v4l2_dev = &camif->v4l2_dev;
1135 vfd->minor = -1;
1136 vfd->release = video_device_release_empty;
1137 vfd->lock = &camif->lock;
1138 vp->reqbufs_count = 0;
1140 INIT_LIST_HEAD(&vp->pending_buf_q);
1141 INIT_LIST_HEAD(&vp->active_buf_q);
1143 memset(q, 0, sizeof(*q));
1144 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1145 q->io_modes = VB2_MMAP | VB2_USERPTR;
1146 q->ops = &s3c_camif_qops;
1147 q->mem_ops = &vb2_dma_contig_memops;
1148 q->buf_struct_size = sizeof(struct camif_buffer);
1149 q->drv_priv = vp;
1150 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1151 q->lock = &vp->camif->lock;
1153 ret = vb2_queue_init(q);
1154 if (ret)
1155 goto err_vd_rel;
1157 vp->pad.flags = MEDIA_PAD_FL_SINK;
1158 ret = media_entity_init(&vfd->entity, 1, &vp->pad, 0);
1159 if (ret)
1160 goto err_vd_rel;
1162 video_set_drvdata(vfd, vp);
1164 v4l2_ctrl_handler_init(&vp->ctrl_handler, 1);
1165 ctrl = v4l2_ctrl_new_std(&vp->ctrl_handler, &s3c_camif_video_ctrl_ops,
1166 V4L2_CID_HFLIP, 0, 1, 1, 0);
1167 if (ctrl)
1168 ctrl->priv = vp;
1169 ctrl = v4l2_ctrl_new_std(&vp->ctrl_handler, &s3c_camif_video_ctrl_ops,
1170 V4L2_CID_VFLIP, 0, 1, 1, 0);
1171 if (ctrl)
1172 ctrl->priv = vp;
1174 ret = vp->ctrl_handler.error;
1175 if (ret < 0)
1176 goto err_me_cleanup;
1178 vfd->ctrl_handler = &vp->ctrl_handler;
1180 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1181 if (ret)
1182 goto err_ctrlh_free;
1184 v4l2_info(&camif->v4l2_dev, "registered %s as /dev/%s\n",
1185 vfd->name, video_device_node_name(vfd));
1186 return 0;
1188 err_ctrlh_free:
1189 v4l2_ctrl_handler_free(&vp->ctrl_handler);
1190 err_me_cleanup:
1191 media_entity_cleanup(&vfd->entity);
1192 err_vd_rel:
1193 video_device_release(vfd);
1194 return ret;
1197 void s3c_camif_unregister_video_node(struct camif_dev *camif, int idx)
1199 struct video_device *vfd = &camif->vp[idx].vdev;
1201 if (video_is_registered(vfd)) {
1202 video_unregister_device(vfd);
1203 media_entity_cleanup(&vfd->entity);
1204 v4l2_ctrl_handler_free(vfd->ctrl_handler);
1208 /* Media bus pixel formats supported at the camif input */
1209 static const u32 camif_mbus_formats[] = {
1210 MEDIA_BUS_FMT_YUYV8_2X8,
1211 MEDIA_BUS_FMT_YVYU8_2X8,
1212 MEDIA_BUS_FMT_UYVY8_2X8,
1213 MEDIA_BUS_FMT_VYUY8_2X8,
1217 * Camera input interface subdev operations
1220 static int s3c_camif_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1221 struct v4l2_subdev_pad_config *cfg,
1222 struct v4l2_subdev_mbus_code_enum *code)
1224 if (code->index >= ARRAY_SIZE(camif_mbus_formats))
1225 return -EINVAL;
1227 code->code = camif_mbus_formats[code->index];
1228 return 0;
1231 static int s3c_camif_subdev_get_fmt(struct v4l2_subdev *sd,
1232 struct v4l2_subdev_pad_config *cfg,
1233 struct v4l2_subdev_format *fmt)
1235 struct camif_dev *camif = v4l2_get_subdevdata(sd);
1236 struct v4l2_mbus_framefmt *mf = &fmt->format;
1238 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1239 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1240 fmt->format = *mf;
1241 return 0;
1244 mutex_lock(&camif->lock);
1246 switch (fmt->pad) {
1247 case CAMIF_SD_PAD_SINK:
1248 /* full camera input pixel size */
1249 *mf = camif->mbus_fmt;
1250 break;
1252 case CAMIF_SD_PAD_SOURCE_C...CAMIF_SD_PAD_SOURCE_P:
1253 /* crop rectangle at camera interface input */
1254 mf->width = camif->camif_crop.width;
1255 mf->height = camif->camif_crop.height;
1256 mf->code = camif->mbus_fmt.code;
1257 break;
1260 mutex_unlock(&camif->lock);
1261 mf->field = V4L2_FIELD_NONE;
1262 mf->colorspace = V4L2_COLORSPACE_JPEG;
1263 return 0;
1266 static void __camif_subdev_try_format(struct camif_dev *camif,
1267 struct v4l2_mbus_framefmt *mf, int pad)
1269 const struct s3c_camif_variant *variant = camif->variant;
1270 const struct vp_pix_limits *pix_lim;
1271 unsigned int i;
1273 /* FIXME: constraints against codec or preview path ? */
1274 pix_lim = &variant->vp_pix_limits[VP_CODEC];
1276 for (i = 0; i < ARRAY_SIZE(camif_mbus_formats); i++)
1277 if (camif_mbus_formats[i] == mf->code)
1278 break;
1280 if (i == ARRAY_SIZE(camif_mbus_formats))
1281 mf->code = camif_mbus_formats[0];
1283 if (pad == CAMIF_SD_PAD_SINK) {
1284 v4l_bound_align_image(&mf->width, 8, CAMIF_MAX_PIX_WIDTH,
1285 ffs(pix_lim->out_width_align) - 1,
1286 &mf->height, 8, CAMIF_MAX_PIX_HEIGHT, 0,
1288 } else {
1289 struct v4l2_rect *crop = &camif->camif_crop;
1290 v4l_bound_align_image(&mf->width, 8, crop->width,
1291 ffs(pix_lim->out_width_align) - 1,
1292 &mf->height, 8, crop->height,
1293 0, 0);
1296 v4l2_dbg(1, debug, &camif->subdev, "%ux%u\n", mf->width, mf->height);
1299 static int s3c_camif_subdev_set_fmt(struct v4l2_subdev *sd,
1300 struct v4l2_subdev_pad_config *cfg,
1301 struct v4l2_subdev_format *fmt)
1303 struct camif_dev *camif = v4l2_get_subdevdata(sd);
1304 struct v4l2_mbus_framefmt *mf = &fmt->format;
1305 struct v4l2_rect *crop = &camif->camif_crop;
1306 int i;
1308 v4l2_dbg(1, debug, sd, "pad%d: code: 0x%x, %ux%u\n",
1309 fmt->pad, mf->code, mf->width, mf->height);
1311 mf->field = V4L2_FIELD_NONE;
1312 mf->colorspace = V4L2_COLORSPACE_JPEG;
1313 mutex_lock(&camif->lock);
1316 * No pixel format change at the camera input is allowed
1317 * while streaming.
1319 if (vb2_is_busy(&camif->vp[VP_CODEC].vb_queue) ||
1320 vb2_is_busy(&camif->vp[VP_PREVIEW].vb_queue)) {
1321 mutex_unlock(&camif->lock);
1322 return -EBUSY;
1325 __camif_subdev_try_format(camif, mf, fmt->pad);
1327 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1328 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1329 *mf = fmt->format;
1330 mutex_unlock(&camif->lock);
1331 return 0;
1334 switch (fmt->pad) {
1335 case CAMIF_SD_PAD_SINK:
1336 camif->mbus_fmt = *mf;
1337 /* Reset sink crop rectangle. */
1338 crop->width = mf->width;
1339 crop->height = mf->height;
1340 crop->left = 0;
1341 crop->top = 0;
1343 * Reset source format (the camif's crop rectangle)
1344 * and the video output resolution.
1346 for (i = 0; i < CAMIF_VP_NUM; i++) {
1347 struct camif_frame *frame = &camif->vp[i].out_frame;
1348 frame->rect = *crop;
1349 frame->f_width = mf->width;
1350 frame->f_height = mf->height;
1352 break;
1354 case CAMIF_SD_PAD_SOURCE_C...CAMIF_SD_PAD_SOURCE_P:
1355 /* Pixel format can be only changed on the sink pad. */
1356 mf->code = camif->mbus_fmt.code;
1357 mf->width = crop->width;
1358 mf->height = crop->height;
1359 break;
1362 mutex_unlock(&camif->lock);
1363 return 0;
1366 static int s3c_camif_subdev_get_selection(struct v4l2_subdev *sd,
1367 struct v4l2_subdev_pad_config *cfg,
1368 struct v4l2_subdev_selection *sel)
1370 struct camif_dev *camif = v4l2_get_subdevdata(sd);
1371 struct v4l2_rect *crop = &camif->camif_crop;
1372 struct v4l2_mbus_framefmt *mf = &camif->mbus_fmt;
1374 if ((sel->target != V4L2_SEL_TGT_CROP &&
1375 sel->target != V4L2_SEL_TGT_CROP_BOUNDS) ||
1376 sel->pad != CAMIF_SD_PAD_SINK)
1377 return -EINVAL;
1379 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1380 sel->r = *v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
1381 return 0;
1384 mutex_lock(&camif->lock);
1386 if (sel->target == V4L2_SEL_TGT_CROP) {
1387 sel->r = *crop;
1388 } else { /* crop bounds */
1389 sel->r.width = mf->width;
1390 sel->r.height = mf->height;
1391 sel->r.left = 0;
1392 sel->r.top = 0;
1395 mutex_unlock(&camif->lock);
1397 v4l2_dbg(1, debug, sd, "%s: crop: (%d,%d) %dx%d, size: %ux%u\n",
1398 __func__, crop->left, crop->top, crop->width,
1399 crop->height, mf->width, mf->height);
1401 return 0;
1404 static void __camif_try_crop(struct camif_dev *camif, struct v4l2_rect *r)
1406 struct v4l2_mbus_framefmt *mf = &camif->mbus_fmt;
1407 const struct camif_pix_limits *pix_lim = &camif->variant->pix_limits;
1408 unsigned int left = 2 * r->left;
1409 unsigned int top = 2 * r->top;
1412 * Following constraints must be met:
1413 * - r->width + 2 * r->left = mf->width;
1414 * - r->height + 2 * r->top = mf->height;
1415 * - crop rectangle size and position must be aligned
1416 * to 8 or 2 pixels, depending on SoC version.
1418 v4l_bound_align_image(&r->width, 0, mf->width,
1419 ffs(pix_lim->win_hor_offset_align) - 1,
1420 &r->height, 0, mf->height, 1, 0);
1422 v4l_bound_align_image(&left, 0, mf->width - r->width,
1423 ffs(pix_lim->win_hor_offset_align),
1424 &top, 0, mf->height - r->height, 2, 0);
1426 r->left = left / 2;
1427 r->top = top / 2;
1428 r->width = mf->width - left;
1429 r->height = mf->height - top;
1431 * Make sure we either downscale or upscale both the pixel
1432 * width and height. Just return current crop rectangle if
1433 * this scaler constraint is not met.
1435 if (camif->variant->ip_revision == S3C244X_CAMIF_IP_REV &&
1436 camif_is_streaming(camif)) {
1437 unsigned int i;
1439 for (i = 0; i < CAMIF_VP_NUM; i++) {
1440 struct v4l2_rect *or = &camif->vp[i].out_frame.rect;
1441 if ((or->width > r->width) == (or->height > r->height))
1442 continue;
1443 *r = camif->camif_crop;
1444 pr_debug("Width/height scaling direction limitation\n");
1445 break;
1449 v4l2_dbg(1, debug, &camif->v4l2_dev, "crop: (%d,%d)/%dx%d, fmt: %ux%u\n",
1450 r->left, r->top, r->width, r->height, mf->width, mf->height);
1453 static int s3c_camif_subdev_set_selection(struct v4l2_subdev *sd,
1454 struct v4l2_subdev_pad_config *cfg,
1455 struct v4l2_subdev_selection *sel)
1457 struct camif_dev *camif = v4l2_get_subdevdata(sd);
1458 struct v4l2_rect *crop = &camif->camif_crop;
1459 struct camif_scaler scaler;
1461 if (sel->target != V4L2_SEL_TGT_CROP || sel->pad != CAMIF_SD_PAD_SINK)
1462 return -EINVAL;
1464 mutex_lock(&camif->lock);
1465 __camif_try_crop(camif, &sel->r);
1467 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1468 *v4l2_subdev_get_try_crop(sd, cfg, sel->pad) = sel->r;
1469 } else {
1470 unsigned long flags;
1471 unsigned int i;
1473 spin_lock_irqsave(&camif->slock, flags);
1474 *crop = sel->r;
1476 for (i = 0; i < CAMIF_VP_NUM; i++) {
1477 struct camif_vp *vp = &camif->vp[i];
1478 scaler = vp->scaler;
1479 if (s3c_camif_get_scaler_config(vp, &scaler))
1480 continue;
1481 vp->scaler = scaler;
1482 vp->state |= ST_VP_CONFIG;
1485 spin_unlock_irqrestore(&camif->slock, flags);
1487 mutex_unlock(&camif->lock);
1489 v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %u, f_h: %u\n",
1490 __func__, crop->left, crop->top, crop->width, crop->height,
1491 camif->mbus_fmt.width, camif->mbus_fmt.height);
1493 return 0;
1496 static const struct v4l2_subdev_pad_ops s3c_camif_subdev_pad_ops = {
1497 .enum_mbus_code = s3c_camif_subdev_enum_mbus_code,
1498 .get_selection = s3c_camif_subdev_get_selection,
1499 .set_selection = s3c_camif_subdev_set_selection,
1500 .get_fmt = s3c_camif_subdev_get_fmt,
1501 .set_fmt = s3c_camif_subdev_set_fmt,
1504 static struct v4l2_subdev_ops s3c_camif_subdev_ops = {
1505 .pad = &s3c_camif_subdev_pad_ops,
1508 static int s3c_camif_subdev_s_ctrl(struct v4l2_ctrl *ctrl)
1510 struct camif_dev *camif = container_of(ctrl->handler, struct camif_dev,
1511 ctrl_handler);
1512 unsigned long flags;
1514 spin_lock_irqsave(&camif->slock, flags);
1516 switch (ctrl->id) {
1517 case V4L2_CID_COLORFX:
1518 camif->colorfx = camif->ctrl_colorfx->val;
1519 /* Set Cb, Cr */
1520 switch (ctrl->val) {
1521 case V4L2_COLORFX_SEPIA:
1522 camif->colorfx_cb = 115;
1523 camif->colorfx_cr = 145;
1524 break;
1525 case V4L2_COLORFX_SET_CBCR:
1526 camif->colorfx_cb = camif->ctrl_colorfx_cbcr->val >> 8;
1527 camif->colorfx_cr = camif->ctrl_colorfx_cbcr->val & 0xff;
1528 break;
1529 default:
1530 /* for V4L2_COLORFX_BW and others */
1531 camif->colorfx_cb = 128;
1532 camif->colorfx_cr = 128;
1534 break;
1535 case V4L2_CID_TEST_PATTERN:
1536 camif->test_pattern = camif->ctrl_test_pattern->val;
1537 break;
1538 default:
1539 WARN_ON(1);
1542 camif->vp[VP_CODEC].state |= ST_VP_CONFIG;
1543 camif->vp[VP_PREVIEW].state |= ST_VP_CONFIG;
1544 spin_unlock_irqrestore(&camif->slock, flags);
1546 return 0;
1549 static const struct v4l2_ctrl_ops s3c_camif_subdev_ctrl_ops = {
1550 .s_ctrl = s3c_camif_subdev_s_ctrl,
1553 static const char * const s3c_camif_test_pattern_menu[] = {
1554 "Disabled",
1555 "Color bars",
1556 "Horizontal increment",
1557 "Vertical increment",
1560 int s3c_camif_create_subdev(struct camif_dev *camif)
1562 struct v4l2_ctrl_handler *handler = &camif->ctrl_handler;
1563 struct v4l2_subdev *sd = &camif->subdev;
1564 int ret;
1566 v4l2_subdev_init(sd, &s3c_camif_subdev_ops);
1567 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1568 strlcpy(sd->name, "S3C-CAMIF", sizeof(sd->name));
1570 camif->pads[CAMIF_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1571 camif->pads[CAMIF_SD_PAD_SOURCE_C].flags = MEDIA_PAD_FL_SOURCE;
1572 camif->pads[CAMIF_SD_PAD_SOURCE_P].flags = MEDIA_PAD_FL_SOURCE;
1574 ret = media_entity_init(&sd->entity, CAMIF_SD_PADS_NUM,
1575 camif->pads, 0);
1576 if (ret)
1577 return ret;
1579 v4l2_ctrl_handler_init(handler, 3);
1580 camif->ctrl_test_pattern = v4l2_ctrl_new_std_menu_items(handler,
1581 &s3c_camif_subdev_ctrl_ops, V4L2_CID_TEST_PATTERN,
1582 ARRAY_SIZE(s3c_camif_test_pattern_menu) - 1, 0, 0,
1583 s3c_camif_test_pattern_menu);
1585 if (camif->variant->has_img_effect) {
1586 camif->ctrl_colorfx = v4l2_ctrl_new_std_menu(handler,
1587 &s3c_camif_subdev_ctrl_ops,
1588 V4L2_CID_COLORFX, V4L2_COLORFX_SET_CBCR,
1589 ~0x981f, V4L2_COLORFX_NONE);
1591 camif->ctrl_colorfx_cbcr = v4l2_ctrl_new_std(handler,
1592 &s3c_camif_subdev_ctrl_ops,
1593 V4L2_CID_COLORFX_CBCR, 0, 0xffff, 1, 0);
1596 if (handler->error) {
1597 v4l2_ctrl_handler_free(handler);
1598 media_entity_cleanup(&sd->entity);
1599 return handler->error;
1602 if (camif->variant->has_img_effect)
1603 v4l2_ctrl_auto_cluster(2, &camif->ctrl_colorfx,
1604 V4L2_COLORFX_SET_CBCR, false);
1606 sd->ctrl_handler = handler;
1607 v4l2_set_subdevdata(sd, camif);
1609 return 0;
1612 void s3c_camif_unregister_subdev(struct camif_dev *camif)
1614 struct v4l2_subdev *sd = &camif->subdev;
1616 /* Return if not registered */
1617 if (v4l2_get_subdevdata(sd) == NULL)
1618 return;
1620 v4l2_device_unregister_subdev(sd);
1621 media_entity_cleanup(&sd->entity);
1622 v4l2_ctrl_handler_free(&camif->ctrl_handler);
1623 v4l2_set_subdevdata(sd, NULL);
1626 int s3c_camif_set_defaults(struct camif_dev *camif)
1628 unsigned int ip_rev = camif->variant->ip_revision;
1629 int i;
1631 for (i = 0; i < CAMIF_VP_NUM; i++) {
1632 struct camif_vp *vp = &camif->vp[i];
1633 struct camif_frame *f = &vp->out_frame;
1635 vp->camif = camif;
1636 vp->id = i;
1637 vp->offset = camif->variant->vp_offset;
1639 if (ip_rev == S3C244X_CAMIF_IP_REV)
1640 vp->fmt_flags = i ? FMT_FL_S3C24XX_PREVIEW :
1641 FMT_FL_S3C24XX_CODEC;
1642 else
1643 vp->fmt_flags = FMT_FL_S3C64XX;
1645 vp->out_fmt = s3c_camif_find_format(vp, NULL, 0);
1646 BUG_ON(vp->out_fmt == NULL);
1648 memset(f, 0, sizeof(*f));
1649 f->f_width = CAMIF_DEF_WIDTH;
1650 f->f_height = CAMIF_DEF_HEIGHT;
1651 f->rect.width = CAMIF_DEF_WIDTH;
1652 f->rect.height = CAMIF_DEF_HEIGHT;
1654 /* Scaler is always enabled */
1655 vp->scaler.enable = 1;
1657 vp->payload = (f->f_width * f->f_height *
1658 vp->out_fmt->depth) / 8;
1661 memset(&camif->mbus_fmt, 0, sizeof(camif->mbus_fmt));
1662 camif->mbus_fmt.width = CAMIF_DEF_WIDTH;
1663 camif->mbus_fmt.height = CAMIF_DEF_HEIGHT;
1664 camif->mbus_fmt.code = camif_mbus_formats[0];
1666 memset(&camif->camif_crop, 0, sizeof(camif->camif_crop));
1667 camif->camif_crop.width = CAMIF_DEF_WIDTH;
1668 camif->camif_crop.height = CAMIF_DEF_HEIGHT;
1670 return 0;