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