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>
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-core.h>
38 #include <media/videobuf2-dma-contig.h>
40 #include "camif-core.h"
41 #include "camif-regs.h"
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)
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;
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)
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;
113 static int sensor_set_power(struct camif_dev *camif, int on)
115 struct cam_sensor *sensor = &camif->sensor;
118 if (camif->sensor.power_count == !on)
119 err = v4l2_subdev_call(sensor->sd, core, s_power, on);
121 sensor->power_count += on ? 1 : -1;
123 pr_debug("on: %d, power_count: %d, err: %d\n",
124 on, sensor->power_count, err);
129 static int sensor_set_streaming(struct camif_dev *camif, int on)
131 struct cam_sensor *sensor = &camif->sensor;
134 if (camif->sensor.stream_count == !on)
135 err = v4l2_subdev_call(sensor->sd, video, s_stream, on);
137 sensor->stream_count += on ? 1 : -1;
139 pr_debug("on: %d, stream_count: %d, err: %d\n",
140 on, sensor->stream_count, 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;
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_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_STATE_ERROR);
175 spin_unlock_irqrestore(&camif->slock, flags);
180 return sensor_set_streaming(camif, 0);
183 static bool s3c_vp_active(struct camif_vp *vp)
185 struct camif_dev *camif = vp->camif;
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);
196 static bool camif_is_streaming(struct camif_dev *camif)
201 spin_lock_irqsave(&camif->slock, flags);
202 status = camif->stream_count > 0;
203 spin_unlock_irqrestore(&camif->slock, flags);
208 static int camif_stop_capture(struct camif_vp *vp)
210 struct camif_dev *camif = vp->camif;
214 if (!s3c_vp_active(vp))
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 |
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;
248 if (vb == NULL || frame == NULL)
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) {
264 /* decompose Y into Y/Cb */
265 paddr->cb = (u32)(paddr->y + pix_size);
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));
274 paddr->cr = (u32)(paddr->cb + (pix_size >> 2));
276 if (vp->out_fmt->color == IMG_FMT_YCRCB420)
277 swap(paddr->cb, paddr->cr);
283 pr_debug("DMA address: y: %pad cb: %pad cr: %pad\n",
284 &paddr->y, &paddr->cb, &paddr->cr);
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;
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);
308 if (vp->state & ST_VP_ABORTING) {
309 if (vp->state & ST_VP_OFF) {
311 vp->state &= ~(ST_VP_OFF | ST_VP_ABORTING |
313 wake_up(&vp->irq_queue);
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;
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)) {
330 struct camif_buffer *vbuf;
334 * Get previous DMA write buffer index:
335 * 0 => DMA buffer 0, 2;
336 * 1 => DMA buffer 1, 3.
338 index = (CISTATUS_FRAMECNT(status) + 2) & 1;
341 vbuf = camif_active_queue_peek(vp, index);
343 if (!WARN_ON(vbuf == NULL)) {
344 /* Dequeue a filled buffer */
345 tv = &vbuf->vb.v4l2_buf.timestamp;
346 tv->tv_sec = ts.tv_sec;
347 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
348 vbuf->vb.v4l2_buf.sequence = vp->frame_sequence++;
349 vb2_buffer_done(&vbuf->vb, VB2_BUF_STATE_DONE);
351 /* Set up an empty buffer at the DMA engine */
352 vbuf = camif_pending_queue_pop(vp);
354 camif_hw_set_output_addr(vp, &vbuf->paddr, index);
355 camif_hw_set_output_addr(vp, &vbuf->paddr, index + 2);
357 /* Scheduled in H/W, add to the queue */
358 camif_active_queue_add(vp, vbuf);
360 } else if (!(vp->state & ST_VP_ABORTING) &&
361 (vp->state & ST_VP_PENDING)) {
362 vp->state |= ST_VP_RUNNING;
365 if (vp->state & ST_VP_CONFIG) {
366 camif_prepare_dma_offset(vp);
367 camif_hw_set_camera_crop(camif);
368 camif_hw_set_scaler(vp);
369 camif_hw_set_flip(vp);
370 camif_hw_set_test_pattern(camif, camif->test_pattern);
371 if (camif->variant->has_img_effect)
372 camif_hw_set_effect(camif, camif->colorfx,
373 camif->colorfx_cb, camif->colorfx_cr);
374 vp->state &= ~ST_VP_CONFIG;
377 spin_unlock(&camif->slock);
381 static int start_streaming(struct vb2_queue *vq, unsigned int count)
383 struct camif_vp *vp = vb2_get_drv_priv(vq);
384 struct camif_dev *camif = vp->camif;
389 * We assume the codec capture path is always activated
390 * first, before the preview path starts streaming.
391 * This is required to avoid internal FIFO overflow and
392 * a need for CAMIF software reset.
394 spin_lock_irqsave(&camif->slock, flags);
396 if (camif->stream_count == 0) {
397 camif_hw_reset(camif);
398 ret = s3c_camif_hw_init(camif, vp);
400 ret = s3c_camif_hw_vp_init(camif, vp);
402 spin_unlock_irqrestore(&camif->slock, flags);
405 camif_reinitialize(vp);
409 spin_lock_irqsave(&camif->slock, flags);
410 vp->frame_sequence = 0;
411 vp->state |= ST_VP_PENDING;
413 if (!list_empty(&vp->pending_buf_q) &&
414 (!(vp->state & ST_VP_STREAMING) ||
415 !(vp->state & ST_VP_SENSOR_STREAMING))) {
417 camif_hw_enable_scaler(vp, vp->scaler.enable);
418 camif_hw_enable_capture(vp);
419 vp->state |= ST_VP_STREAMING;
421 if (!(vp->state & ST_VP_SENSOR_STREAMING)) {
422 vp->state |= ST_VP_SENSOR_STREAMING;
423 spin_unlock_irqrestore(&camif->slock, flags);
424 ret = sensor_set_streaming(camif, 1);
426 v4l2_err(&vp->vdev, "Sensor s_stream failed\n");
428 camif_hw_dump_regs(camif, __func__);
434 spin_unlock_irqrestore(&camif->slock, flags);
438 static void stop_streaming(struct vb2_queue *vq)
440 struct camif_vp *vp = vb2_get_drv_priv(vq);
441 camif_stop_capture(vp);
444 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
445 unsigned int *num_buffers, unsigned int *num_planes,
446 unsigned int sizes[], void *allocators[])
448 const struct v4l2_pix_format *pix = NULL;
449 struct camif_vp *vp = vb2_get_drv_priv(vq);
450 struct camif_dev *camif = vp->camif;
451 struct camif_frame *frame = &vp->out_frame;
452 const struct camif_fmt *fmt;
456 pix = &pfmt->fmt.pix;
457 fmt = s3c_camif_find_format(vp, &pix->pixelformat, -1);
460 size = (pix->width * pix->height * fmt->depth) / 8;
465 size = (frame->f_width * frame->f_height * fmt->depth) / 8;
471 sizes[0] = max(size, pix->sizeimage);
474 allocators[0] = camif->alloc_ctx;
476 pr_debug("size: %u\n", sizes[0]);
480 static int buffer_prepare(struct vb2_buffer *vb)
482 struct camif_vp *vp = vb2_get_drv_priv(vb->vb2_queue);
484 if (vp->out_fmt == NULL)
487 if (vb2_plane_size(vb, 0) < vp->payload) {
488 v4l2_err(&vp->vdev, "buffer too small: %lu, required: %u\n",
489 vb2_plane_size(vb, 0), vp->payload);
492 vb2_set_plane_payload(vb, 0, vp->payload);
497 static void buffer_queue(struct vb2_buffer *vb)
499 struct camif_buffer *buf = container_of(vb, struct camif_buffer, vb);
500 struct camif_vp *vp = vb2_get_drv_priv(vb->vb2_queue);
501 struct camif_dev *camif = vp->camif;
504 spin_lock_irqsave(&camif->slock, flags);
505 WARN_ON(camif_prepare_addr(vp, &buf->vb, &buf->paddr));
507 if (!(vp->state & ST_VP_STREAMING) && vp->active_buffers < 2) {
508 /* Schedule an empty buffer in H/W */
509 buf->index = vp->buf_index;
511 camif_hw_set_output_addr(vp, &buf->paddr, buf->index);
512 camif_hw_set_output_addr(vp, &buf->paddr, buf->index + 2);
514 camif_active_queue_add(vp, buf);
515 vp->buf_index = !vp->buf_index;
517 camif_pending_queue_add(vp, buf);
520 if (vb2_is_streaming(&vp->vb_queue) && !list_empty(&vp->pending_buf_q)
521 && !(vp->state & ST_VP_STREAMING)) {
523 vp->state |= ST_VP_STREAMING;
524 camif_hw_enable_scaler(vp, vp->scaler.enable);
525 camif_hw_enable_capture(vp);
526 spin_unlock_irqrestore(&camif->slock, flags);
528 if (!(vp->state & ST_VP_SENSOR_STREAMING)) {
529 if (sensor_set_streaming(camif, 1) == 0)
530 vp->state |= ST_VP_SENSOR_STREAMING;
532 v4l2_err(&vp->vdev, "Sensor s_stream failed\n");
535 camif_hw_dump_regs(camif, __func__);
539 spin_unlock_irqrestore(&camif->slock, flags);
542 static const struct vb2_ops s3c_camif_qops = {
543 .queue_setup = queue_setup,
544 .buf_prepare = buffer_prepare,
545 .buf_queue = buffer_queue,
546 .wait_prepare = vb2_ops_wait_prepare,
547 .wait_finish = vb2_ops_wait_finish,
548 .start_streaming = start_streaming,
549 .stop_streaming = stop_streaming,
552 static int s3c_camif_open(struct file *file)
554 struct camif_vp *vp = video_drvdata(file);
555 struct camif_dev *camif = vp->camif;
558 pr_debug("[vp%d] state: %#x, owner: %p, pid: %d\n", vp->id,
559 vp->state, vp->owner, task_pid_nr(current));
561 if (mutex_lock_interruptible(&camif->lock))
564 ret = v4l2_fh_open(file);
568 ret = pm_runtime_get_sync(camif->dev);
572 ret = sensor_set_power(camif, 1);
576 pm_runtime_put(camif->dev);
578 v4l2_fh_release(file);
580 mutex_unlock(&camif->lock);
584 static int s3c_camif_close(struct file *file)
586 struct camif_vp *vp = video_drvdata(file);
587 struct camif_dev *camif = vp->camif;
590 pr_debug("[vp%d] state: %#x, owner: %p, pid: %d\n", vp->id,
591 vp->state, vp->owner, task_pid_nr(current));
593 mutex_lock(&camif->lock);
595 if (vp->owner == file->private_data) {
596 camif_stop_capture(vp);
597 vb2_queue_release(&vp->vb_queue);
601 sensor_set_power(camif, 0);
603 pm_runtime_put(camif->dev);
604 ret = v4l2_fh_release(file);
606 mutex_unlock(&camif->lock);
610 static unsigned int s3c_camif_poll(struct file *file,
611 struct poll_table_struct *wait)
613 struct camif_vp *vp = video_drvdata(file);
614 struct camif_dev *camif = vp->camif;
617 mutex_lock(&camif->lock);
618 if (vp->owner && vp->owner != file->private_data)
621 ret = vb2_poll(&vp->vb_queue, file, wait);
623 mutex_unlock(&camif->lock);
627 static int s3c_camif_mmap(struct file *file, struct vm_area_struct *vma)
629 struct camif_vp *vp = video_drvdata(file);
632 if (vp->owner && vp->owner != file->private_data)
635 ret = vb2_mmap(&vp->vb_queue, vma);
640 static const struct v4l2_file_operations s3c_camif_fops = {
641 .owner = THIS_MODULE,
642 .open = s3c_camif_open,
643 .release = s3c_camif_close,
644 .poll = s3c_camif_poll,
645 .unlocked_ioctl = video_ioctl2,
646 .mmap = s3c_camif_mmap,
653 static int s3c_camif_vidioc_querycap(struct file *file, void *priv,
654 struct v4l2_capability *cap)
656 struct camif_vp *vp = video_drvdata(file);
658 strlcpy(cap->driver, S3C_CAMIF_DRIVER_NAME, sizeof(cap->driver));
659 strlcpy(cap->card, S3C_CAMIF_DRIVER_NAME, sizeof(cap->card));
660 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s.%d",
661 dev_name(vp->camif->dev), vp->id);
663 cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
664 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
669 static int s3c_camif_vidioc_enum_input(struct file *file, void *priv,
670 struct v4l2_input *input)
672 struct camif_vp *vp = video_drvdata(file);
673 struct v4l2_subdev *sensor = vp->camif->sensor.sd;
675 if (input->index || sensor == NULL)
678 input->type = V4L2_INPUT_TYPE_CAMERA;
679 strlcpy(input->name, sensor->name, sizeof(input->name));
683 static int s3c_camif_vidioc_s_input(struct file *file, void *priv,
686 return i == 0 ? 0 : -EINVAL;
689 static int s3c_camif_vidioc_g_input(struct file *file, void *priv,
696 static int s3c_camif_vidioc_enum_fmt(struct file *file, void *priv,
697 struct v4l2_fmtdesc *f)
699 struct camif_vp *vp = video_drvdata(file);
700 const struct camif_fmt *fmt;
702 fmt = s3c_camif_find_format(vp, NULL, f->index);
706 strlcpy(f->description, fmt->name, sizeof(f->description));
707 f->pixelformat = fmt->fourcc;
709 pr_debug("fmt(%d): %s\n", f->index, f->description);
713 static int s3c_camif_vidioc_g_fmt(struct file *file, void *priv,
714 struct v4l2_format *f)
716 struct camif_vp *vp = video_drvdata(file);
717 struct v4l2_pix_format *pix = &f->fmt.pix;
718 struct camif_frame *frame = &vp->out_frame;
719 const struct camif_fmt *fmt = vp->out_fmt;
721 pix->bytesperline = frame->f_width * fmt->ybpp;
722 pix->sizeimage = vp->payload;
724 pix->pixelformat = fmt->fourcc;
725 pix->width = frame->f_width;
726 pix->height = frame->f_height;
727 pix->field = V4L2_FIELD_NONE;
728 pix->colorspace = V4L2_COLORSPACE_JPEG;
733 static int __camif_video_try_format(struct camif_vp *vp,
734 struct v4l2_pix_format *pix,
735 const struct camif_fmt **ffmt)
737 struct camif_dev *camif = vp->camif;
738 struct v4l2_rect *crop = &camif->camif_crop;
739 unsigned int wmin, hmin, sc_hrmax, sc_vrmax;
740 const struct vp_pix_limits *pix_lim;
741 const struct camif_fmt *fmt;
743 fmt = s3c_camif_find_format(vp, &pix->pixelformat, 0);
745 if (WARN_ON(fmt == NULL))
751 pix_lim = &camif->variant->vp_pix_limits[vp->id];
753 pr_debug("fmt: %ux%u, crop: %ux%u, bytesperline: %u\n",
754 pix->width, pix->height, crop->width, crop->height,
757 * Calculate minimum width and height according to the configured
758 * camera input interface crop rectangle and the resizer's capabilities.
760 sc_hrmax = min(SCALER_MAX_RATIO, 1 << (ffs(crop->width) - 3));
761 sc_vrmax = min(SCALER_MAX_RATIO, 1 << (ffs(crop->height) - 1));
763 wmin = max_t(u32, pix_lim->min_out_width, crop->width / sc_hrmax);
764 wmin = round_up(wmin, pix_lim->out_width_align);
765 hmin = max_t(u32, 8, crop->height / sc_vrmax);
766 hmin = round_up(hmin, 8);
768 v4l_bound_align_image(&pix->width, wmin, pix_lim->max_sc_out_width,
769 ffs(pix_lim->out_width_align) - 1,
770 &pix->height, hmin, pix_lim->max_height, 0, 0);
772 pix->bytesperline = pix->width * fmt->ybpp;
773 pix->sizeimage = (pix->width * pix->height * fmt->depth) / 8;
774 pix->pixelformat = fmt->fourcc;
775 pix->colorspace = V4L2_COLORSPACE_JPEG;
776 pix->field = V4L2_FIELD_NONE;
778 pr_debug("%ux%u, wmin: %d, hmin: %d, sc_hrmax: %d, sc_vrmax: %d\n",
779 pix->width, pix->height, wmin, hmin, sc_hrmax, sc_vrmax);
784 static int s3c_camif_vidioc_try_fmt(struct file *file, void *priv,
785 struct v4l2_format *f)
787 struct camif_vp *vp = video_drvdata(file);
788 return __camif_video_try_format(vp, &f->fmt.pix, NULL);
791 static int s3c_camif_vidioc_s_fmt(struct file *file, void *priv,
792 struct v4l2_format *f)
794 struct v4l2_pix_format *pix = &f->fmt.pix;
795 struct camif_vp *vp = video_drvdata(file);
796 struct camif_frame *out_frame = &vp->out_frame;
797 const struct camif_fmt *fmt = NULL;
800 pr_debug("[vp%d]\n", vp->id);
802 if (vb2_is_busy(&vp->vb_queue))
805 ret = __camif_video_try_format(vp, &f->fmt.pix, &fmt);
810 vp->payload = pix->sizeimage;
811 out_frame->f_width = pix->width;
812 out_frame->f_height = pix->height;
814 /* Reset composition rectangle */
815 out_frame->rect.width = pix->width;
816 out_frame->rect.height = pix->height;
817 out_frame->rect.left = 0;
818 out_frame->rect.top = 0;
820 if (vp->owner == NULL)
823 pr_debug("%ux%u. payload: %u. fmt: %s. %d %d. sizeimage: %d. bpl: %d\n",
824 out_frame->f_width, out_frame->f_height, vp->payload, fmt->name,
825 pix->width * pix->height * fmt->depth, fmt->depth,
826 pix->sizeimage, pix->bytesperline);
831 /* Only check pixel formats at the sensor and the camif subdev pads */
832 static int camif_pipeline_validate(struct camif_dev *camif)
834 struct v4l2_subdev_format src_fmt;
835 struct media_pad *pad;
838 /* Retrieve format at the sensor subdev source pad */
839 pad = media_entity_remote_pad(&camif->pads[0]);
840 if (!pad || media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
843 src_fmt.pad = pad->index;
844 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
845 ret = v4l2_subdev_call(camif->sensor.sd, pad, get_fmt, NULL, &src_fmt);
846 if (ret < 0 && ret != -ENOIOCTLCMD)
849 if (src_fmt.format.width != camif->mbus_fmt.width ||
850 src_fmt.format.height != camif->mbus_fmt.height ||
851 src_fmt.format.code != camif->mbus_fmt.code)
857 static int s3c_camif_streamon(struct file *file, void *priv,
858 enum v4l2_buf_type type)
860 struct camif_vp *vp = video_drvdata(file);
861 struct camif_dev *camif = vp->camif;
862 struct media_entity *sensor = &camif->sensor.sd->entity;
865 pr_debug("[vp%d]\n", vp->id);
867 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
870 if (vp->owner && vp->owner != priv)
873 if (s3c_vp_active(vp))
876 ret = media_entity_pipeline_start(sensor, camif->m_pipeline);
880 ret = camif_pipeline_validate(camif);
882 media_entity_pipeline_stop(sensor);
886 return vb2_streamon(&vp->vb_queue, type);
889 static int s3c_camif_streamoff(struct file *file, void *priv,
890 enum v4l2_buf_type type)
892 struct camif_vp *vp = video_drvdata(file);
893 struct camif_dev *camif = vp->camif;
896 pr_debug("[vp%d]\n", vp->id);
898 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
901 if (vp->owner && vp->owner != priv)
904 ret = vb2_streamoff(&vp->vb_queue, type);
906 media_entity_pipeline_stop(&camif->sensor.sd->entity);
910 static int s3c_camif_reqbufs(struct file *file, void *priv,
911 struct v4l2_requestbuffers *rb)
913 struct camif_vp *vp = video_drvdata(file);
916 pr_debug("[vp%d] rb count: %d, owner: %p, priv: %p\n",
917 vp->id, rb->count, vp->owner, priv);
919 if (vp->owner && vp->owner != priv)
923 rb->count = max_t(u32, CAMIF_REQ_BUFS_MIN, rb->count);
927 ret = vb2_reqbufs(&vp->vb_queue, rb);
931 if (rb->count && rb->count < CAMIF_REQ_BUFS_MIN) {
933 vb2_reqbufs(&vp->vb_queue, rb);
937 vp->reqbufs_count = rb->count;
938 if (vp->owner == NULL && rb->count > 0)
944 static int s3c_camif_querybuf(struct file *file, void *priv,
945 struct v4l2_buffer *buf)
947 struct camif_vp *vp = video_drvdata(file);
948 return vb2_querybuf(&vp->vb_queue, buf);
951 static int s3c_camif_qbuf(struct file *file, void *priv,
952 struct v4l2_buffer *buf)
954 struct camif_vp *vp = video_drvdata(file);
956 pr_debug("[vp%d]\n", vp->id);
958 if (vp->owner && vp->owner != priv)
961 return vb2_qbuf(&vp->vb_queue, buf);
964 static int s3c_camif_dqbuf(struct file *file, void *priv,
965 struct v4l2_buffer *buf)
967 struct camif_vp *vp = video_drvdata(file);
969 pr_debug("[vp%d] sequence: %d\n", vp->id, vp->frame_sequence);
971 if (vp->owner && vp->owner != priv)
974 return vb2_dqbuf(&vp->vb_queue, buf, file->f_flags & O_NONBLOCK);
977 static int s3c_camif_create_bufs(struct file *file, void *priv,
978 struct v4l2_create_buffers *create)
980 struct camif_vp *vp = video_drvdata(file);
983 if (vp->owner && vp->owner != priv)
986 create->count = max_t(u32, 1, create->count);
987 ret = vb2_create_bufs(&vp->vb_queue, create);
989 if (!ret && vp->owner == NULL)
995 static int s3c_camif_prepare_buf(struct file *file, void *priv,
996 struct v4l2_buffer *b)
998 struct camif_vp *vp = video_drvdata(file);
999 return vb2_prepare_buf(&vp->vb_queue, b);
1002 static int s3c_camif_g_selection(struct file *file, void *priv,
1003 struct v4l2_selection *sel)
1005 struct camif_vp *vp = video_drvdata(file);
1007 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1010 switch (sel->target) {
1011 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1012 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1015 sel->r.width = vp->out_frame.f_width;
1016 sel->r.height = vp->out_frame.f_height;
1019 case V4L2_SEL_TGT_COMPOSE:
1020 sel->r = vp->out_frame.rect;
1027 static void __camif_try_compose(struct camif_dev *camif, struct camif_vp *vp,
1028 struct v4l2_rect *r)
1030 /* s3c244x doesn't support composition */
1031 if (camif->variant->ip_revision == S3C244X_CAMIF_IP_REV) {
1032 *r = vp->out_frame.rect;
1039 static int s3c_camif_s_selection(struct file *file, void *priv,
1040 struct v4l2_selection *sel)
1042 struct camif_vp *vp = video_drvdata(file);
1043 struct camif_dev *camif = vp->camif;
1044 struct v4l2_rect rect = sel->r;
1045 unsigned long flags;
1047 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1048 sel->target != V4L2_SEL_TGT_COMPOSE)
1051 __camif_try_compose(camif, vp, &rect);
1054 spin_lock_irqsave(&camif->slock, flags);
1055 vp->out_frame.rect = rect;
1056 vp->state |= ST_VP_CONFIG;
1057 spin_unlock_irqrestore(&camif->slock, flags);
1059 pr_debug("type: %#x, target: %#x, flags: %#x, (%d,%d)/%dx%d\n",
1060 sel->type, sel->target, sel->flags,
1061 sel->r.left, sel->r.top, sel->r.width, sel->r.height);
1066 static const struct v4l2_ioctl_ops s3c_camif_ioctl_ops = {
1067 .vidioc_querycap = s3c_camif_vidioc_querycap,
1068 .vidioc_enum_input = s3c_camif_vidioc_enum_input,
1069 .vidioc_g_input = s3c_camif_vidioc_g_input,
1070 .vidioc_s_input = s3c_camif_vidioc_s_input,
1071 .vidioc_enum_fmt_vid_cap = s3c_camif_vidioc_enum_fmt,
1072 .vidioc_try_fmt_vid_cap = s3c_camif_vidioc_try_fmt,
1073 .vidioc_s_fmt_vid_cap = s3c_camif_vidioc_s_fmt,
1074 .vidioc_g_fmt_vid_cap = s3c_camif_vidioc_g_fmt,
1075 .vidioc_g_selection = s3c_camif_g_selection,
1076 .vidioc_s_selection = s3c_camif_s_selection,
1077 .vidioc_reqbufs = s3c_camif_reqbufs,
1078 .vidioc_querybuf = s3c_camif_querybuf,
1079 .vidioc_prepare_buf = s3c_camif_prepare_buf,
1080 .vidioc_create_bufs = s3c_camif_create_bufs,
1081 .vidioc_qbuf = s3c_camif_qbuf,
1082 .vidioc_dqbuf = s3c_camif_dqbuf,
1083 .vidioc_streamon = s3c_camif_streamon,
1084 .vidioc_streamoff = s3c_camif_streamoff,
1085 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1086 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1087 .vidioc_log_status = v4l2_ctrl_log_status,
1091 * Video node controls
1093 static int s3c_camif_video_s_ctrl(struct v4l2_ctrl *ctrl)
1095 struct camif_vp *vp = ctrl->priv;
1096 struct camif_dev *camif = vp->camif;
1097 unsigned long flags;
1099 pr_debug("[vp%d] ctrl: %s, value: %d\n", vp->id,
1100 ctrl->name, ctrl->val);
1102 spin_lock_irqsave(&camif->slock, flags);
1105 case V4L2_CID_HFLIP:
1106 vp->hflip = ctrl->val;
1109 case V4L2_CID_VFLIP:
1110 vp->vflip = ctrl->val;
1114 vp->state |= ST_VP_CONFIG;
1115 spin_unlock_irqrestore(&camif->slock, flags);
1119 /* Codec and preview video node control ops */
1120 static const struct v4l2_ctrl_ops s3c_camif_video_ctrl_ops = {
1121 .s_ctrl = s3c_camif_video_s_ctrl,
1124 int s3c_camif_register_video_node(struct camif_dev *camif, int idx)
1126 struct camif_vp *vp = &camif->vp[idx];
1127 struct vb2_queue *q = &vp->vb_queue;
1128 struct video_device *vfd = &vp->vdev;
1129 struct v4l2_ctrl *ctrl;
1132 memset(vfd, 0, sizeof(*vfd));
1133 snprintf(vfd->name, sizeof(vfd->name), "camif-%s",
1134 vp->id == 0 ? "codec" : "preview");
1136 vfd->fops = &s3c_camif_fops;
1137 vfd->ioctl_ops = &s3c_camif_ioctl_ops;
1138 vfd->v4l2_dev = &camif->v4l2_dev;
1140 vfd->release = video_device_release_empty;
1141 vfd->lock = &camif->lock;
1142 vp->reqbufs_count = 0;
1144 INIT_LIST_HEAD(&vp->pending_buf_q);
1145 INIT_LIST_HEAD(&vp->active_buf_q);
1147 memset(q, 0, sizeof(*q));
1148 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1149 q->io_modes = VB2_MMAP | VB2_USERPTR;
1150 q->ops = &s3c_camif_qops;
1151 q->mem_ops = &vb2_dma_contig_memops;
1152 q->buf_struct_size = sizeof(struct camif_buffer);
1154 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1155 q->lock = &vp->camif->lock;
1157 ret = vb2_queue_init(q);
1161 vp->pad.flags = MEDIA_PAD_FL_SINK;
1162 ret = media_entity_init(&vfd->entity, 1, &vp->pad, 0);
1166 video_set_drvdata(vfd, vp);
1168 v4l2_ctrl_handler_init(&vp->ctrl_handler, 1);
1169 ctrl = v4l2_ctrl_new_std(&vp->ctrl_handler, &s3c_camif_video_ctrl_ops,
1170 V4L2_CID_HFLIP, 0, 1, 1, 0);
1173 ctrl = v4l2_ctrl_new_std(&vp->ctrl_handler, &s3c_camif_video_ctrl_ops,
1174 V4L2_CID_VFLIP, 0, 1, 1, 0);
1178 ret = vp->ctrl_handler.error;
1180 goto err_me_cleanup;
1182 vfd->ctrl_handler = &vp->ctrl_handler;
1184 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1186 goto err_ctrlh_free;
1188 v4l2_info(&camif->v4l2_dev, "registered %s as /dev/%s\n",
1189 vfd->name, video_device_node_name(vfd));
1193 v4l2_ctrl_handler_free(&vp->ctrl_handler);
1195 media_entity_cleanup(&vfd->entity);
1197 video_device_release(vfd);
1201 void s3c_camif_unregister_video_node(struct camif_dev *camif, int idx)
1203 struct video_device *vfd = &camif->vp[idx].vdev;
1205 if (video_is_registered(vfd)) {
1206 video_unregister_device(vfd);
1207 media_entity_cleanup(&vfd->entity);
1208 v4l2_ctrl_handler_free(vfd->ctrl_handler);
1212 /* Media bus pixel formats supported at the camif input */
1213 static const u32 camif_mbus_formats[] = {
1214 MEDIA_BUS_FMT_YUYV8_2X8,
1215 MEDIA_BUS_FMT_YVYU8_2X8,
1216 MEDIA_BUS_FMT_UYVY8_2X8,
1217 MEDIA_BUS_FMT_VYUY8_2X8,
1221 * Camera input interface subdev operations
1224 static int s3c_camif_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1225 struct v4l2_subdev_pad_config *cfg,
1226 struct v4l2_subdev_mbus_code_enum *code)
1228 if (code->index >= ARRAY_SIZE(camif_mbus_formats))
1231 code->code = camif_mbus_formats[code->index];
1235 static int s3c_camif_subdev_get_fmt(struct v4l2_subdev *sd,
1236 struct v4l2_subdev_pad_config *cfg,
1237 struct v4l2_subdev_format *fmt)
1239 struct camif_dev *camif = v4l2_get_subdevdata(sd);
1240 struct v4l2_mbus_framefmt *mf = &fmt->format;
1242 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1243 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1248 mutex_lock(&camif->lock);
1251 case CAMIF_SD_PAD_SINK:
1252 /* full camera input pixel size */
1253 *mf = camif->mbus_fmt;
1256 case CAMIF_SD_PAD_SOURCE_C...CAMIF_SD_PAD_SOURCE_P:
1257 /* crop rectangle at camera interface input */
1258 mf->width = camif->camif_crop.width;
1259 mf->height = camif->camif_crop.height;
1260 mf->code = camif->mbus_fmt.code;
1264 mutex_unlock(&camif->lock);
1265 mf->field = V4L2_FIELD_NONE;
1266 mf->colorspace = V4L2_COLORSPACE_JPEG;
1270 static void __camif_subdev_try_format(struct camif_dev *camif,
1271 struct v4l2_mbus_framefmt *mf, int pad)
1273 const struct s3c_camif_variant *variant = camif->variant;
1274 const struct vp_pix_limits *pix_lim;
1275 int i = ARRAY_SIZE(camif_mbus_formats);
1277 /* FIXME: constraints against codec or preview path ? */
1278 pix_lim = &variant->vp_pix_limits[VP_CODEC];
1281 if (camif_mbus_formats[i] == mf->code)
1284 mf->code = camif_mbus_formats[i];
1286 if (pad == CAMIF_SD_PAD_SINK) {
1287 v4l_bound_align_image(&mf->width, 8, CAMIF_MAX_PIX_WIDTH,
1288 ffs(pix_lim->out_width_align) - 1,
1289 &mf->height, 8, CAMIF_MAX_PIX_HEIGHT, 0,
1292 struct v4l2_rect *crop = &camif->camif_crop;
1293 v4l_bound_align_image(&mf->width, 8, crop->width,
1294 ffs(pix_lim->out_width_align) - 1,
1295 &mf->height, 8, crop->height,
1299 v4l2_dbg(1, debug, &camif->subdev, "%ux%u\n", mf->width, mf->height);
1302 static int s3c_camif_subdev_set_fmt(struct v4l2_subdev *sd,
1303 struct v4l2_subdev_pad_config *cfg,
1304 struct v4l2_subdev_format *fmt)
1306 struct camif_dev *camif = v4l2_get_subdevdata(sd);
1307 struct v4l2_mbus_framefmt *mf = &fmt->format;
1308 struct v4l2_rect *crop = &camif->camif_crop;
1311 v4l2_dbg(1, debug, sd, "pad%d: code: 0x%x, %ux%u\n",
1312 fmt->pad, mf->code, mf->width, mf->height);
1314 mf->field = V4L2_FIELD_NONE;
1315 mf->colorspace = V4L2_COLORSPACE_JPEG;
1316 mutex_lock(&camif->lock);
1319 * No pixel format change at the camera input is allowed
1322 if (vb2_is_busy(&camif->vp[VP_CODEC].vb_queue) ||
1323 vb2_is_busy(&camif->vp[VP_PREVIEW].vb_queue)) {
1324 mutex_unlock(&camif->lock);
1328 __camif_subdev_try_format(camif, mf, fmt->pad);
1330 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1331 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1333 mutex_unlock(&camif->lock);
1338 case CAMIF_SD_PAD_SINK:
1339 camif->mbus_fmt = *mf;
1340 /* Reset sink crop rectangle. */
1341 crop->width = mf->width;
1342 crop->height = mf->height;
1346 * Reset source format (the camif's crop rectangle)
1347 * and the video output resolution.
1349 for (i = 0; i < CAMIF_VP_NUM; i++) {
1350 struct camif_frame *frame = &camif->vp[i].out_frame;
1351 frame->rect = *crop;
1352 frame->f_width = mf->width;
1353 frame->f_height = mf->height;
1357 case CAMIF_SD_PAD_SOURCE_C...CAMIF_SD_PAD_SOURCE_P:
1358 /* Pixel format can be only changed on the sink pad. */
1359 mf->code = camif->mbus_fmt.code;
1360 mf->width = crop->width;
1361 mf->height = crop->height;
1365 mutex_unlock(&camif->lock);
1369 static int s3c_camif_subdev_get_selection(struct v4l2_subdev *sd,
1370 struct v4l2_subdev_pad_config *cfg,
1371 struct v4l2_subdev_selection *sel)
1373 struct camif_dev *camif = v4l2_get_subdevdata(sd);
1374 struct v4l2_rect *crop = &camif->camif_crop;
1375 struct v4l2_mbus_framefmt *mf = &camif->mbus_fmt;
1377 if ((sel->target != V4L2_SEL_TGT_CROP &&
1378 sel->target != V4L2_SEL_TGT_CROP_BOUNDS) ||
1379 sel->pad != CAMIF_SD_PAD_SINK)
1382 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1383 sel->r = *v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
1387 mutex_lock(&camif->lock);
1389 if (sel->target == V4L2_SEL_TGT_CROP) {
1391 } else { /* crop bounds */
1392 sel->r.width = mf->width;
1393 sel->r.height = mf->height;
1398 mutex_unlock(&camif->lock);
1400 v4l2_dbg(1, debug, sd, "%s: crop: (%d,%d) %dx%d, size: %ux%u\n",
1401 __func__, crop->left, crop->top, crop->width,
1402 crop->height, mf->width, mf->height);
1407 static void __camif_try_crop(struct camif_dev *camif, struct v4l2_rect *r)
1409 struct v4l2_mbus_framefmt *mf = &camif->mbus_fmt;
1410 const struct camif_pix_limits *pix_lim = &camif->variant->pix_limits;
1411 unsigned int left = 2 * r->left;
1412 unsigned int top = 2 * r->top;
1415 * Following constraints must be met:
1416 * - r->width + 2 * r->left = mf->width;
1417 * - r->height + 2 * r->top = mf->height;
1418 * - crop rectangle size and position must be aligned
1419 * to 8 or 2 pixels, depending on SoC version.
1421 v4l_bound_align_image(&r->width, 0, mf->width,
1422 ffs(pix_lim->win_hor_offset_align) - 1,
1423 &r->height, 0, mf->height, 1, 0);
1425 v4l_bound_align_image(&left, 0, mf->width - r->width,
1426 ffs(pix_lim->win_hor_offset_align),
1427 &top, 0, mf->height - r->height, 2, 0);
1431 r->width = mf->width - left;
1432 r->height = mf->height - top;
1434 * Make sure we either downscale or upscale both the pixel
1435 * width and height. Just return current crop rectangle if
1436 * this scaler constraint is not met.
1438 if (camif->variant->ip_revision == S3C244X_CAMIF_IP_REV &&
1439 camif_is_streaming(camif)) {
1442 for (i = 0; i < CAMIF_VP_NUM; i++) {
1443 struct v4l2_rect *or = &camif->vp[i].out_frame.rect;
1444 if ((or->width > r->width) == (or->height > r->height))
1446 *r = camif->camif_crop;
1447 pr_debug("Width/height scaling direction limitation\n");
1452 v4l2_dbg(1, debug, &camif->v4l2_dev, "crop: (%d,%d)/%dx%d, fmt: %ux%u\n",
1453 r->left, r->top, r->width, r->height, mf->width, mf->height);
1456 static int s3c_camif_subdev_set_selection(struct v4l2_subdev *sd,
1457 struct v4l2_subdev_pad_config *cfg,
1458 struct v4l2_subdev_selection *sel)
1460 struct camif_dev *camif = v4l2_get_subdevdata(sd);
1461 struct v4l2_rect *crop = &camif->camif_crop;
1462 struct camif_scaler scaler;
1464 if (sel->target != V4L2_SEL_TGT_CROP || sel->pad != CAMIF_SD_PAD_SINK)
1467 mutex_lock(&camif->lock);
1468 __camif_try_crop(camif, &sel->r);
1470 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1471 *v4l2_subdev_get_try_crop(sd, cfg, sel->pad) = sel->r;
1473 unsigned long flags;
1476 spin_lock_irqsave(&camif->slock, flags);
1479 for (i = 0; i < CAMIF_VP_NUM; i++) {
1480 struct camif_vp *vp = &camif->vp[i];
1481 scaler = vp->scaler;
1482 if (s3c_camif_get_scaler_config(vp, &scaler))
1484 vp->scaler = scaler;
1485 vp->state |= ST_VP_CONFIG;
1488 spin_unlock_irqrestore(&camif->slock, flags);
1490 mutex_unlock(&camif->lock);
1492 v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %u, f_h: %u\n",
1493 __func__, crop->left, crop->top, crop->width, crop->height,
1494 camif->mbus_fmt.width, camif->mbus_fmt.height);
1499 static const struct v4l2_subdev_pad_ops s3c_camif_subdev_pad_ops = {
1500 .enum_mbus_code = s3c_camif_subdev_enum_mbus_code,
1501 .get_selection = s3c_camif_subdev_get_selection,
1502 .set_selection = s3c_camif_subdev_set_selection,
1503 .get_fmt = s3c_camif_subdev_get_fmt,
1504 .set_fmt = s3c_camif_subdev_set_fmt,
1507 static struct v4l2_subdev_ops s3c_camif_subdev_ops = {
1508 .pad = &s3c_camif_subdev_pad_ops,
1511 static int s3c_camif_subdev_s_ctrl(struct v4l2_ctrl *ctrl)
1513 struct camif_dev *camif = container_of(ctrl->handler, struct camif_dev,
1515 unsigned long flags;
1517 spin_lock_irqsave(&camif->slock, flags);
1520 case V4L2_CID_COLORFX:
1521 camif->colorfx = camif->ctrl_colorfx->val;
1523 switch (ctrl->val) {
1524 case V4L2_COLORFX_SEPIA:
1525 camif->colorfx_cb = 115;
1526 camif->colorfx_cr = 145;
1528 case V4L2_COLORFX_SET_CBCR:
1529 camif->colorfx_cb = camif->ctrl_colorfx_cbcr->val >> 8;
1530 camif->colorfx_cr = camif->ctrl_colorfx_cbcr->val & 0xff;
1533 /* for V4L2_COLORFX_BW and others */
1534 camif->colorfx_cb = 128;
1535 camif->colorfx_cr = 128;
1538 case V4L2_CID_TEST_PATTERN:
1539 camif->test_pattern = camif->ctrl_test_pattern->val;
1545 camif->vp[VP_CODEC].state |= ST_VP_CONFIG;
1546 camif->vp[VP_PREVIEW].state |= ST_VP_CONFIG;
1547 spin_unlock_irqrestore(&camif->slock, flags);
1552 static const struct v4l2_ctrl_ops s3c_camif_subdev_ctrl_ops = {
1553 .s_ctrl = s3c_camif_subdev_s_ctrl,
1556 static const char * const s3c_camif_test_pattern_menu[] = {
1559 "Horizontal increment",
1560 "Vertical increment",
1563 int s3c_camif_create_subdev(struct camif_dev *camif)
1565 struct v4l2_ctrl_handler *handler = &camif->ctrl_handler;
1566 struct v4l2_subdev *sd = &camif->subdev;
1569 v4l2_subdev_init(sd, &s3c_camif_subdev_ops);
1570 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1571 strlcpy(sd->name, "S3C-CAMIF", sizeof(sd->name));
1573 camif->pads[CAMIF_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1574 camif->pads[CAMIF_SD_PAD_SOURCE_C].flags = MEDIA_PAD_FL_SOURCE;
1575 camif->pads[CAMIF_SD_PAD_SOURCE_P].flags = MEDIA_PAD_FL_SOURCE;
1577 ret = media_entity_init(&sd->entity, CAMIF_SD_PADS_NUM,
1582 v4l2_ctrl_handler_init(handler, 3);
1583 camif->ctrl_test_pattern = v4l2_ctrl_new_std_menu_items(handler,
1584 &s3c_camif_subdev_ctrl_ops, V4L2_CID_TEST_PATTERN,
1585 ARRAY_SIZE(s3c_camif_test_pattern_menu) - 1, 0, 0,
1586 s3c_camif_test_pattern_menu);
1588 if (camif->variant->has_img_effect) {
1589 camif->ctrl_colorfx = v4l2_ctrl_new_std_menu(handler,
1590 &s3c_camif_subdev_ctrl_ops,
1591 V4L2_CID_COLORFX, V4L2_COLORFX_SET_CBCR,
1592 ~0x981f, V4L2_COLORFX_NONE);
1594 camif->ctrl_colorfx_cbcr = v4l2_ctrl_new_std(handler,
1595 &s3c_camif_subdev_ctrl_ops,
1596 V4L2_CID_COLORFX_CBCR, 0, 0xffff, 1, 0);
1599 if (handler->error) {
1600 v4l2_ctrl_handler_free(handler);
1601 media_entity_cleanup(&sd->entity);
1602 return handler->error;
1605 if (camif->variant->has_img_effect)
1606 v4l2_ctrl_auto_cluster(2, &camif->ctrl_colorfx,
1607 V4L2_COLORFX_SET_CBCR, false);
1609 sd->ctrl_handler = handler;
1610 v4l2_set_subdevdata(sd, camif);
1615 void s3c_camif_unregister_subdev(struct camif_dev *camif)
1617 struct v4l2_subdev *sd = &camif->subdev;
1619 /* Return if not registered */
1620 if (v4l2_get_subdevdata(sd) == NULL)
1623 v4l2_device_unregister_subdev(sd);
1624 media_entity_cleanup(&sd->entity);
1625 v4l2_ctrl_handler_free(&camif->ctrl_handler);
1626 v4l2_set_subdevdata(sd, NULL);
1629 int s3c_camif_set_defaults(struct camif_dev *camif)
1631 unsigned int ip_rev = camif->variant->ip_revision;
1634 for (i = 0; i < CAMIF_VP_NUM; i++) {
1635 struct camif_vp *vp = &camif->vp[i];
1636 struct camif_frame *f = &vp->out_frame;
1640 vp->offset = camif->variant->vp_offset;
1642 if (ip_rev == S3C244X_CAMIF_IP_REV)
1643 vp->fmt_flags = i ? FMT_FL_S3C24XX_PREVIEW :
1644 FMT_FL_S3C24XX_CODEC;
1646 vp->fmt_flags = FMT_FL_S3C64XX;
1648 vp->out_fmt = s3c_camif_find_format(vp, NULL, 0);
1649 BUG_ON(vp->out_fmt == NULL);
1651 memset(f, 0, sizeof(*f));
1652 f->f_width = CAMIF_DEF_WIDTH;
1653 f->f_height = CAMIF_DEF_HEIGHT;
1654 f->rect.width = CAMIF_DEF_WIDTH;
1655 f->rect.height = CAMIF_DEF_HEIGHT;
1657 /* Scaler is always enabled */
1658 vp->scaler.enable = 1;
1660 vp->payload = (f->f_width * f->f_height *
1661 vp->out_fmt->depth) / 8;
1664 memset(&camif->mbus_fmt, 0, sizeof(camif->mbus_fmt));
1665 camif->mbus_fmt.width = CAMIF_DEF_WIDTH;
1666 camif->mbus_fmt.height = CAMIF_DEF_HEIGHT;
1667 camif->mbus_fmt.code = camif_mbus_formats[0];
1669 memset(&camif->camif_crop, 0, sizeof(camif->camif_crop));
1670 camif->camif_crop.width = CAMIF_DEF_WIDTH;
1671 camif->camif_crop.height = CAMIF_DEF_HEIGHT;