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