[media] s5p-fimc: Configure scaler registers depending on FIMC version
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / video / s5p-fimc / fimc-capture.c
1 /*
2 * Samsung S5P SoC series camera interface (camera capture) driver
3 *
4 * Copyright (c) 2010 Samsung Electronics Co., Ltd
5 * Author: Sylwester Nawrocki, <s.nawrocki@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/version.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/bug.h>
18 #include <linux/interrupt.h>
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/list.h>
22 #include <linux/slab.h>
23 #include <linux/clk.h>
24 #include <linux/i2c.h>
25
26 #include <linux/videodev2.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/v4l2-mem2mem.h>
30 #include <media/videobuf2-core.h>
31 #include <media/videobuf2-dma-contig.h>
32
33 #include "fimc-core.h"
34
35 static struct v4l2_subdev *fimc_subdev_register(struct fimc_dev *fimc,
36 struct s5p_fimc_isp_info *isp_info)
37 {
38 struct i2c_adapter *i2c_adap;
39 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
40 struct v4l2_subdev *sd = NULL;
41
42 i2c_adap = i2c_get_adapter(isp_info->i2c_bus_num);
43 if (!i2c_adap)
44 return ERR_PTR(-ENOMEM);
45
46 sd = v4l2_i2c_new_subdev_board(&vid_cap->v4l2_dev, i2c_adap,
47 isp_info->board_info, NULL);
48 if (!sd) {
49 v4l2_err(&vid_cap->v4l2_dev, "failed to acquire subdev\n");
50 return NULL;
51 }
52
53 v4l2_info(&vid_cap->v4l2_dev, "subdevice %s registered successfuly\n",
54 isp_info->board_info->type);
55
56 return sd;
57 }
58
59 static void fimc_subdev_unregister(struct fimc_dev *fimc)
60 {
61 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
62 struct i2c_client *client;
63
64 if (vid_cap->input_index < 0)
65 return; /* Subdevice already released or not registered. */
66
67 if (vid_cap->sd) {
68 v4l2_device_unregister_subdev(vid_cap->sd);
69 client = v4l2_get_subdevdata(vid_cap->sd);
70 i2c_unregister_device(client);
71 i2c_put_adapter(client->adapter);
72 vid_cap->sd = NULL;
73 }
74
75 vid_cap->input_index = -1;
76 }
77
78 /**
79 * fimc_subdev_attach - attach v4l2_subdev to camera host interface
80 *
81 * @fimc: FIMC device information
82 * @index: index to the array of available subdevices,
83 * -1 for full array search or non negative value
84 * to select specific subdevice
85 */
86 static int fimc_subdev_attach(struct fimc_dev *fimc, int index)
87 {
88 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
89 struct s5p_platform_fimc *pdata = fimc->pdata;
90 struct s5p_fimc_isp_info *isp_info;
91 struct v4l2_subdev *sd;
92 int i;
93
94 for (i = 0; i < FIMC_MAX_CAMIF_CLIENTS; ++i) {
95 isp_info = pdata->isp_info[i];
96
97 if (!isp_info || (index >= 0 && i != index))
98 continue;
99
100 sd = fimc_subdev_register(fimc, isp_info);
101 if (sd) {
102 vid_cap->sd = sd;
103 vid_cap->input_index = i;
104
105 return 0;
106 }
107 }
108
109 vid_cap->input_index = -1;
110 vid_cap->sd = NULL;
111 v4l2_err(&vid_cap->v4l2_dev, "fimc%d: sensor attach failed\n",
112 fimc->id);
113 return -ENODEV;
114 }
115
116 static int fimc_isp_subdev_init(struct fimc_dev *fimc, unsigned int index)
117 {
118 struct s5p_fimc_isp_info *isp_info;
119 int ret;
120
121 if (index >= FIMC_MAX_CAMIF_CLIENTS)
122 return -EINVAL;
123
124 isp_info = fimc->pdata->isp_info[index];
125 if (!isp_info)
126 return -EINVAL;
127
128 if (isp_info->clk_frequency)
129 clk_set_rate(fimc->clock[CLK_CAM], isp_info->clk_frequency);
130
131 ret = clk_enable(fimc->clock[CLK_CAM]);
132 if (ret)
133 return ret;
134
135 ret = fimc_subdev_attach(fimc, index);
136 if (ret)
137 return ret;
138
139 ret = fimc_hw_set_camera_polarity(fimc, isp_info);
140 if (ret)
141 return ret;
142
143 ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 1);
144 if (!ret)
145 return ret;
146
147 /* enabling power failed so unregister subdev */
148 fimc_subdev_unregister(fimc);
149
150 v4l2_err(&fimc->vid_cap.v4l2_dev, "ISP initialization failed: %d\n",
151 ret);
152
153 return ret;
154 }
155
156 /*
157 * At least one buffer on the pending_buf_q queue is required.
158 * Locking: The caller holds fimc->slock spinlock.
159 */
160 int fimc_vid_cap_buf_queue(struct fimc_dev *fimc,
161 struct fimc_vid_buffer *fimc_vb)
162 {
163 struct fimc_vid_cap *cap = &fimc->vid_cap;
164 struct fimc_ctx *ctx = cap->ctx;
165 int ret = 0;
166
167 BUG_ON(!fimc || !fimc_vb);
168
169 ret = fimc_prepare_addr(ctx, &fimc_vb->vb, &ctx->d_frame,
170 &fimc_vb->paddr);
171 if (ret)
172 return ret;
173
174 if (test_bit(ST_CAPT_STREAM, &fimc->state)) {
175 fimc_pending_queue_add(cap, fimc_vb);
176 } else {
177 /* Setup the buffer directly for processing. */
178 int buf_id = (cap->reqbufs_count == 1) ? -1 : cap->buf_index;
179 fimc_hw_set_output_addr(fimc, &fimc_vb->paddr, buf_id);
180
181 fimc_vb->index = cap->buf_index;
182 active_queue_add(cap, fimc_vb);
183
184 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
185 cap->buf_index = 0;
186 }
187 return ret;
188 }
189
190 static int fimc_stop_capture(struct fimc_dev *fimc)
191 {
192 unsigned long flags;
193 struct fimc_vid_cap *cap;
194 struct fimc_vid_buffer *buf;
195
196 cap = &fimc->vid_cap;
197
198 if (!fimc_capture_active(fimc))
199 return 0;
200
201 spin_lock_irqsave(&fimc->slock, flags);
202 set_bit(ST_CAPT_SHUT, &fimc->state);
203 fimc_deactivate_capture(fimc);
204 spin_unlock_irqrestore(&fimc->slock, flags);
205
206 wait_event_timeout(fimc->irq_queue,
207 test_bit(ST_CAPT_SHUT, &fimc->state),
208 FIMC_SHUTDOWN_TIMEOUT);
209
210 v4l2_subdev_call(cap->sd, video, s_stream, 0);
211
212 spin_lock_irqsave(&fimc->slock, flags);
213 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_PEND |
214 1 << ST_CAPT_STREAM);
215
216 fimc->vid_cap.active_buf_cnt = 0;
217
218 /* Release buffers that were enqueued in the driver by videobuf2. */
219 while (!list_empty(&cap->pending_buf_q)) {
220 buf = pending_queue_pop(cap);
221 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
222 }
223
224 while (!list_empty(&cap->active_buf_q)) {
225 buf = active_queue_pop(cap);
226 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
227 }
228
229 spin_unlock_irqrestore(&fimc->slock, flags);
230
231 dbg("state: 0x%lx", fimc->state);
232 return 0;
233 }
234
235 static int start_streaming(struct vb2_queue *q)
236 {
237 struct fimc_ctx *ctx = q->drv_priv;
238 struct fimc_dev *fimc = ctx->fimc_dev;
239 struct s5p_fimc_isp_info *isp_info;
240 struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
241 int ret;
242
243 ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_stream, 1);
244 if (ret && ret != -ENOIOCTLCMD)
245 return ret;
246
247 ret = fimc_prepare_config(ctx, ctx->state);
248 if (ret)
249 return ret;
250
251 isp_info = fimc->pdata->isp_info[fimc->vid_cap.input_index];
252 fimc_hw_set_camera_type(fimc, isp_info);
253 fimc_hw_set_camera_source(fimc, isp_info);
254 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
255
256 if (ctx->state & FIMC_PARAMS) {
257 ret = fimc_set_scaler_info(ctx);
258 if (ret) {
259 err("Scaler setup error");
260 return ret;
261 }
262 fimc_hw_set_input_path(ctx);
263 fimc_hw_set_prescaler(ctx);
264 if (variant->has_mainscaler_ext)
265 fimc_hw_set_mainscaler_ext(ctx);
266 else
267 fimc_hw_set_mainscaler(ctx);
268 fimc_hw_set_target_format(ctx);
269 fimc_hw_set_rotation(ctx);
270 fimc_hw_set_effect(ctx);
271 }
272
273 fimc_hw_set_output_path(ctx);
274 fimc_hw_set_out_dma(ctx);
275
276 INIT_LIST_HEAD(&fimc->vid_cap.pending_buf_q);
277 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
278 fimc->vid_cap.active_buf_cnt = 0;
279 fimc->vid_cap.frame_count = 0;
280 fimc->vid_cap.buf_index = fimc_hw_get_frame_index(fimc);
281
282 set_bit(ST_CAPT_PEND, &fimc->state);
283
284 return 0;
285 }
286
287 static int stop_streaming(struct vb2_queue *q)
288 {
289 struct fimc_ctx *ctx = q->drv_priv;
290 struct fimc_dev *fimc = ctx->fimc_dev;
291 unsigned long flags;
292
293 spin_lock_irqsave(&fimc->slock, flags);
294 if (!fimc_capture_running(fimc) && !fimc_capture_pending(fimc)) {
295 spin_unlock_irqrestore(&fimc->slock, flags);
296 return -EINVAL;
297 }
298 spin_unlock_irqrestore(&fimc->slock, flags);
299
300 return fimc_stop_capture(fimc);
301 }
302
303 static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
304 {
305 if (!fr || plane >= fr->fmt->memplanes)
306 return 0;
307
308 dbg("%s: w: %d. h: %d. depth[%d]: %d",
309 __func__, fr->width, fr->height, plane, fr->fmt->depth[plane]);
310
311 return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8;
312
313 }
314
315 static int queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
316 unsigned int *num_planes, unsigned long sizes[],
317 void *allocators[])
318 {
319 struct fimc_ctx *ctx = vq->drv_priv;
320 struct fimc_fmt *fmt = ctx->d_frame.fmt;
321 int i;
322
323 if (!fmt)
324 return -EINVAL;
325
326 *num_planes = fmt->memplanes;
327
328 dbg("%s, buffer count=%d, plane count=%d",
329 __func__, *num_buffers, *num_planes);
330
331 for (i = 0; i < fmt->memplanes; i++) {
332 sizes[i] = get_plane_size(&ctx->d_frame, i);
333 dbg("plane: %u, plane_size: %lu", i, sizes[i]);
334 allocators[i] = ctx->fimc_dev->alloc_ctx;
335 }
336
337 return 0;
338 }
339
340 static int buffer_init(struct vb2_buffer *vb)
341 {
342 /* TODO: */
343 return 0;
344 }
345
346 static int buffer_prepare(struct vb2_buffer *vb)
347 {
348 struct vb2_queue *vq = vb->vb2_queue;
349 struct fimc_ctx *ctx = vq->drv_priv;
350 struct v4l2_device *v4l2_dev = &ctx->fimc_dev->m2m.v4l2_dev;
351 int i;
352
353 if (!ctx->d_frame.fmt || vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
354 return -EINVAL;
355
356 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
357 unsigned long size = get_plane_size(&ctx->d_frame, i);
358
359 if (vb2_plane_size(vb, i) < size) {
360 v4l2_err(v4l2_dev, "User buffer too small (%ld < %ld)\n",
361 vb2_plane_size(vb, i), size);
362 return -EINVAL;
363 }
364
365 vb2_set_plane_payload(vb, i, size);
366 }
367
368 return 0;
369 }
370
371 static void buffer_queue(struct vb2_buffer *vb)
372 {
373 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
374 struct fimc_dev *fimc = ctx->fimc_dev;
375 struct fimc_vid_buffer *buf
376 = container_of(vb, struct fimc_vid_buffer, vb);
377 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
378 unsigned long flags;
379
380 spin_lock_irqsave(&fimc->slock, flags);
381 fimc_vid_cap_buf_queue(fimc, buf);
382
383 dbg("active_buf_cnt: %d", fimc->vid_cap.active_buf_cnt);
384
385 if (vid_cap->active_buf_cnt >= vid_cap->reqbufs_count ||
386 vid_cap->active_buf_cnt >= FIMC_MAX_OUT_BUFS) {
387 if (!test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
388 fimc_activate_capture(ctx);
389 dbg("");
390 }
391 }
392 spin_unlock_irqrestore(&fimc->slock, flags);
393 }
394
395 static void fimc_lock(struct vb2_queue *vq)
396 {
397 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
398 mutex_lock(&ctx->fimc_dev->lock);
399 }
400
401 static void fimc_unlock(struct vb2_queue *vq)
402 {
403 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
404 mutex_unlock(&ctx->fimc_dev->lock);
405 }
406
407 static struct vb2_ops fimc_capture_qops = {
408 .queue_setup = queue_setup,
409 .buf_prepare = buffer_prepare,
410 .buf_queue = buffer_queue,
411 .buf_init = buffer_init,
412 .wait_prepare = fimc_unlock,
413 .wait_finish = fimc_lock,
414 .start_streaming = start_streaming,
415 .stop_streaming = stop_streaming,
416 };
417
418 static int fimc_capture_open(struct file *file)
419 {
420 struct fimc_dev *fimc = video_drvdata(file);
421 int ret = 0;
422
423 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
424
425 /* Return if the corresponding video mem2mem node is already opened. */
426 if (fimc_m2m_active(fimc))
427 return -EBUSY;
428
429 if (++fimc->vid_cap.refcnt == 1) {
430 ret = fimc_isp_subdev_init(fimc, 0);
431 if (ret) {
432 fimc->vid_cap.refcnt--;
433 return -EIO;
434 }
435 }
436
437 file->private_data = fimc->vid_cap.ctx;
438
439 return 0;
440 }
441
442 static int fimc_capture_close(struct file *file)
443 {
444 struct fimc_dev *fimc = video_drvdata(file);
445
446 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
447
448 if (--fimc->vid_cap.refcnt == 0) {
449 fimc_stop_capture(fimc);
450 vb2_queue_release(&fimc->vid_cap.vbq);
451
452 v4l2_err(&fimc->vid_cap.v4l2_dev, "releasing ISP\n");
453
454 v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
455 clk_disable(fimc->clock[CLK_CAM]);
456 fimc_subdev_unregister(fimc);
457 }
458
459 return 0;
460 }
461
462 static unsigned int fimc_capture_poll(struct file *file,
463 struct poll_table_struct *wait)
464 {
465 struct fimc_ctx *ctx = file->private_data;
466 struct fimc_dev *fimc = ctx->fimc_dev;
467
468 return vb2_poll(&fimc->vid_cap.vbq, file, wait);
469 }
470
471 static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
472 {
473 struct fimc_ctx *ctx = file->private_data;
474 struct fimc_dev *fimc = ctx->fimc_dev;
475
476 return vb2_mmap(&fimc->vid_cap.vbq, vma);
477 }
478
479 /* video device file operations */
480 static const struct v4l2_file_operations fimc_capture_fops = {
481 .owner = THIS_MODULE,
482 .open = fimc_capture_open,
483 .release = fimc_capture_close,
484 .poll = fimc_capture_poll,
485 .unlocked_ioctl = video_ioctl2,
486 .mmap = fimc_capture_mmap,
487 };
488
489 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
490 struct v4l2_capability *cap)
491 {
492 struct fimc_ctx *ctx = file->private_data;
493 struct fimc_dev *fimc = ctx->fimc_dev;
494
495 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
496 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
497 cap->bus_info[0] = 0;
498 cap->version = KERNEL_VERSION(1, 0, 0);
499 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
500 V4L2_CAP_VIDEO_CAPTURE_MPLANE;
501
502 return 0;
503 }
504
505 /* Synchronize formats of the camera interface input and attached sensor. */
506 static int sync_capture_fmt(struct fimc_ctx *ctx)
507 {
508 struct fimc_frame *frame = &ctx->s_frame;
509 struct fimc_dev *fimc = ctx->fimc_dev;
510 struct v4l2_mbus_framefmt *fmt = &fimc->vid_cap.fmt;
511 int ret;
512
513 fmt->width = ctx->d_frame.o_width;
514 fmt->height = ctx->d_frame.o_height;
515
516 ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_mbus_fmt, fmt);
517 if (ret == -ENOIOCTLCMD) {
518 err("s_mbus_fmt failed");
519 return ret;
520 }
521 dbg("w: %d, h: %d, code= %d", fmt->width, fmt->height, fmt->code);
522
523 frame->fmt = find_mbus_format(fmt, FMT_FLAGS_CAM);
524 if (!frame->fmt) {
525 err("fimc source format not found\n");
526 return -EINVAL;
527 }
528
529 frame->f_width = fmt->width;
530 frame->f_height = fmt->height;
531 frame->width = fmt->width;
532 frame->height = fmt->height;
533 frame->o_width = fmt->width;
534 frame->o_height = fmt->height;
535 frame->offs_h = 0;
536 frame->offs_v = 0;
537
538 return 0;
539 }
540
541 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
542 struct v4l2_format *f)
543 {
544 struct fimc_ctx *ctx = priv;
545 struct fimc_dev *fimc = ctx->fimc_dev;
546 struct fimc_frame *frame;
547 struct v4l2_pix_format_mplane *pix;
548 int ret;
549 int i;
550
551 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
552 return -EINVAL;
553
554 ret = fimc_vidioc_try_fmt_mplane(file, priv, f);
555 if (ret)
556 return ret;
557
558 if (vb2_is_streaming(&fimc->vid_cap.vbq) || fimc_capture_active(fimc))
559 return -EBUSY;
560
561 frame = &ctx->d_frame;
562
563 pix = &f->fmt.pix_mp;
564 frame->fmt = find_format(f, FMT_FLAGS_M2M | FMT_FLAGS_CAM);
565 if (!frame->fmt) {
566 err("fimc target format not found\n");
567 return -EINVAL;
568 }
569
570 for (i = 0; i < frame->fmt->colplanes; i++)
571 frame->payload[i] = pix->plane_fmt[i].bytesperline * pix->height;
572
573 /* Output DMA frame pixel size and offsets. */
574 frame->f_width = pix->plane_fmt[0].bytesperline * 8
575 / frame->fmt->depth[0];
576 frame->f_height = pix->height;
577 frame->width = pix->width;
578 frame->height = pix->height;
579 frame->o_width = pix->width;
580 frame->o_height = pix->height;
581 frame->offs_h = 0;
582 frame->offs_v = 0;
583
584 ctx->state |= (FIMC_PARAMS | FIMC_DST_FMT);
585
586 ret = sync_capture_fmt(ctx);
587 return ret;
588 }
589
590 static int fimc_cap_enum_input(struct file *file, void *priv,
591 struct v4l2_input *i)
592 {
593 struct fimc_ctx *ctx = priv;
594 struct s5p_platform_fimc *pldata = ctx->fimc_dev->pdata;
595 struct s5p_fimc_isp_info *isp_info;
596
597 if (i->index >= FIMC_MAX_CAMIF_CLIENTS)
598 return -EINVAL;
599
600 isp_info = pldata->isp_info[i->index];
601 if (isp_info == NULL)
602 return -EINVAL;
603
604 i->type = V4L2_INPUT_TYPE_CAMERA;
605 strncpy(i->name, isp_info->board_info->type, 32);
606 return 0;
607 }
608
609 static int fimc_cap_s_input(struct file *file, void *priv,
610 unsigned int i)
611 {
612 struct fimc_ctx *ctx = priv;
613 struct fimc_dev *fimc = ctx->fimc_dev;
614 struct s5p_platform_fimc *pdata = fimc->pdata;
615
616 if (fimc_capture_active(ctx->fimc_dev))
617 return -EBUSY;
618
619 if (i >= FIMC_MAX_CAMIF_CLIENTS || !pdata->isp_info[i])
620 return -EINVAL;
621
622
623 if (fimc->vid_cap.sd) {
624 int ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
625 if (ret)
626 err("s_power failed: %d", ret);
627
628 clk_disable(fimc->clock[CLK_CAM]);
629 }
630
631 /* Release the attached sensor subdevice. */
632 fimc_subdev_unregister(fimc);
633
634 return fimc_isp_subdev_init(fimc, i);
635 }
636
637 static int fimc_cap_g_input(struct file *file, void *priv,
638 unsigned int *i)
639 {
640 struct fimc_ctx *ctx = priv;
641 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
642
643 *i = cap->input_index;
644 return 0;
645 }
646
647 static int fimc_cap_streamon(struct file *file, void *priv,
648 enum v4l2_buf_type type)
649 {
650 struct fimc_ctx *ctx = priv;
651 struct fimc_dev *fimc = ctx->fimc_dev;
652
653 if (fimc_capture_active(fimc) || !fimc->vid_cap.sd)
654 return -EBUSY;
655
656 if (!(ctx->state & FIMC_DST_FMT)) {
657 v4l2_err(&fimc->vid_cap.v4l2_dev, "Format is not set\n");
658 return -EINVAL;
659 }
660
661 return vb2_streamon(&fimc->vid_cap.vbq, type);
662 }
663
664 static int fimc_cap_streamoff(struct file *file, void *priv,
665 enum v4l2_buf_type type)
666 {
667 struct fimc_ctx *ctx = priv;
668 struct fimc_dev *fimc = ctx->fimc_dev;
669
670 return vb2_streamoff(&fimc->vid_cap.vbq, type);
671 }
672
673 static int fimc_cap_reqbufs(struct file *file, void *priv,
674 struct v4l2_requestbuffers *reqbufs)
675 {
676 struct fimc_ctx *ctx = priv;
677 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
678 int ret;
679
680
681 ret = vb2_reqbufs(&cap->vbq, reqbufs);
682 if (!ret)
683 cap->reqbufs_count = reqbufs->count;
684
685 return ret;
686 }
687
688 static int fimc_cap_querybuf(struct file *file, void *priv,
689 struct v4l2_buffer *buf)
690 {
691 struct fimc_ctx *ctx = priv;
692 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
693
694 return vb2_querybuf(&cap->vbq, buf);
695 }
696
697 static int fimc_cap_qbuf(struct file *file, void *priv,
698 struct v4l2_buffer *buf)
699 {
700 struct fimc_ctx *ctx = priv;
701 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
702 return vb2_qbuf(&cap->vbq, buf);
703 }
704
705 static int fimc_cap_dqbuf(struct file *file, void *priv,
706 struct v4l2_buffer *buf)
707 {
708 struct fimc_ctx *ctx = priv;
709 return vb2_dqbuf(&ctx->fimc_dev->vid_cap.vbq, buf,
710 file->f_flags & O_NONBLOCK);
711 }
712
713 static int fimc_cap_s_ctrl(struct file *file, void *priv,
714 struct v4l2_control *ctrl)
715 {
716 struct fimc_ctx *ctx = priv;
717 int ret = -EINVAL;
718
719 /* Allow any controls but 90/270 rotation while streaming */
720 if (!fimc_capture_active(ctx->fimc_dev) ||
721 ctrl->id != V4L2_CID_ROTATE ||
722 (ctrl->value != 90 && ctrl->value != 270)) {
723 ret = check_ctrl_val(ctx, ctrl);
724 if (!ret) {
725 ret = fimc_s_ctrl(ctx, ctrl);
726 if (!ret)
727 ctx->state |= FIMC_PARAMS;
728 }
729 }
730 if (ret == -EINVAL)
731 ret = v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd,
732 core, s_ctrl, ctrl);
733 return ret;
734 }
735
736 static int fimc_cap_cropcap(struct file *file, void *fh,
737 struct v4l2_cropcap *cr)
738 {
739 struct fimc_frame *f;
740 struct fimc_ctx *ctx = fh;
741
742 if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
743 return -EINVAL;
744
745 f = &ctx->s_frame;
746
747 cr->bounds.left = 0;
748 cr->bounds.top = 0;
749 cr->bounds.width = f->o_width;
750 cr->bounds.height = f->o_height;
751 cr->defrect = cr->bounds;
752
753 return 0;
754 }
755
756 static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
757 {
758 struct fimc_frame *f;
759 struct fimc_ctx *ctx = file->private_data;
760
761 f = &ctx->s_frame;
762
763 cr->c.left = f->offs_h;
764 cr->c.top = f->offs_v;
765 cr->c.width = f->width;
766 cr->c.height = f->height;
767
768 return 0;
769 }
770
771 static int fimc_cap_s_crop(struct file *file, void *fh,
772 struct v4l2_crop *cr)
773 {
774 struct fimc_frame *f;
775 struct fimc_ctx *ctx = file->private_data;
776 struct fimc_dev *fimc = ctx->fimc_dev;
777 int ret = -EINVAL;
778
779 if (fimc_capture_active(fimc))
780 return -EBUSY;
781
782 ret = fimc_try_crop(ctx, cr);
783 if (ret)
784 return ret;
785
786 if (!(ctx->state & FIMC_DST_FMT)) {
787 v4l2_err(&fimc->vid_cap.v4l2_dev,
788 "Capture color format not set\n");
789 return -EINVAL; /* TODO: make sure this is the right value */
790 }
791
792 f = &ctx->s_frame;
793 /* Check for the pixel scaling ratio when cropping input image. */
794 ret = fimc_check_scaler_ratio(&cr->c, &ctx->d_frame);
795 if (ret) {
796 v4l2_err(&fimc->vid_cap.v4l2_dev, "Out of the scaler range");
797 return ret;
798 }
799
800 f->offs_h = cr->c.left;
801 f->offs_v = cr->c.top;
802 f->width = cr->c.width;
803 f->height = cr->c.height;
804
805 return 0;
806 }
807
808
809 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
810 .vidioc_querycap = fimc_vidioc_querycap_capture,
811
812 .vidioc_enum_fmt_vid_cap_mplane = fimc_vidioc_enum_fmt_mplane,
813 .vidioc_try_fmt_vid_cap_mplane = fimc_vidioc_try_fmt_mplane,
814 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
815 .vidioc_g_fmt_vid_cap_mplane = fimc_vidioc_g_fmt_mplane,
816
817 .vidioc_reqbufs = fimc_cap_reqbufs,
818 .vidioc_querybuf = fimc_cap_querybuf,
819
820 .vidioc_qbuf = fimc_cap_qbuf,
821 .vidioc_dqbuf = fimc_cap_dqbuf,
822
823 .vidioc_streamon = fimc_cap_streamon,
824 .vidioc_streamoff = fimc_cap_streamoff,
825
826 .vidioc_queryctrl = fimc_vidioc_queryctrl,
827 .vidioc_g_ctrl = fimc_vidioc_g_ctrl,
828 .vidioc_s_ctrl = fimc_cap_s_ctrl,
829
830 .vidioc_g_crop = fimc_cap_g_crop,
831 .vidioc_s_crop = fimc_cap_s_crop,
832 .vidioc_cropcap = fimc_cap_cropcap,
833
834 .vidioc_enum_input = fimc_cap_enum_input,
835 .vidioc_s_input = fimc_cap_s_input,
836 .vidioc_g_input = fimc_cap_g_input,
837 };
838
839 /* fimc->lock must be already initialized */
840 int fimc_register_capture_device(struct fimc_dev *fimc)
841 {
842 struct v4l2_device *v4l2_dev = &fimc->vid_cap.v4l2_dev;
843 struct video_device *vfd;
844 struct fimc_vid_cap *vid_cap;
845 struct fimc_ctx *ctx;
846 struct v4l2_format f;
847 struct fimc_frame *fr;
848 struct vb2_queue *q;
849 int ret;
850
851 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
852 if (!ctx)
853 return -ENOMEM;
854
855 ctx->fimc_dev = fimc;
856 ctx->in_path = FIMC_CAMERA;
857 ctx->out_path = FIMC_DMA;
858 ctx->state = FIMC_CTX_CAP;
859
860 /* Default format of the output frames */
861 f.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32;
862 fr = &ctx->d_frame;
863 fr->fmt = find_format(&f, FMT_FLAGS_M2M);
864 fr->width = fr->f_width = fr->o_width = 640;
865 fr->height = fr->f_height = fr->o_height = 480;
866
867 if (!v4l2_dev->name[0])
868 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
869 "%s.capture", dev_name(&fimc->pdev->dev));
870
871 ret = v4l2_device_register(NULL, v4l2_dev);
872 if (ret)
873 goto err_info;
874
875 vfd = video_device_alloc();
876 if (!vfd) {
877 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
878 goto err_v4l2_reg;
879 }
880
881 snprintf(vfd->name, sizeof(vfd->name), "%s:cap",
882 dev_name(&fimc->pdev->dev));
883
884 vfd->fops = &fimc_capture_fops;
885 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
886 vfd->minor = -1;
887 vfd->release = video_device_release;
888 vfd->lock = &fimc->lock;
889 video_set_drvdata(vfd, fimc);
890
891 vid_cap = &fimc->vid_cap;
892 vid_cap->vfd = vfd;
893 vid_cap->active_buf_cnt = 0;
894 vid_cap->reqbufs_count = 0;
895 vid_cap->refcnt = 0;
896 /* Default color format for image sensor */
897 vid_cap->fmt.code = V4L2_MBUS_FMT_YUYV8_2X8;
898
899 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
900 INIT_LIST_HEAD(&vid_cap->active_buf_q);
901 spin_lock_init(&ctx->slock);
902 vid_cap->ctx = ctx;
903
904 q = &fimc->vid_cap.vbq;
905 memset(q, 0, sizeof(*q));
906 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
907 q->io_modes = VB2_MMAP | VB2_USERPTR;
908 q->drv_priv = fimc->vid_cap.ctx;
909 q->ops = &fimc_capture_qops;
910 q->mem_ops = &vb2_dma_contig_memops;
911 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
912
913 vb2_queue_init(q);
914
915 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
916 if (ret) {
917 v4l2_err(v4l2_dev, "Failed to register video device\n");
918 goto err_vd_reg;
919 }
920
921 v4l2_info(v4l2_dev,
922 "FIMC capture driver registered as /dev/video%d\n",
923 vfd->num);
924
925 return 0;
926
927 err_vd_reg:
928 video_device_release(vfd);
929 err_v4l2_reg:
930 v4l2_device_unregister(v4l2_dev);
931 err_info:
932 dev_err(&fimc->pdev->dev, "failed to install\n");
933 return ret;
934 }
935
936 void fimc_unregister_capture_device(struct fimc_dev *fimc)
937 {
938 struct fimc_vid_cap *capture = &fimc->vid_cap;
939
940 if (capture->vfd)
941 video_unregister_device(capture->vfd);
942
943 kfree(capture->ctx);
944 }