]> Pileus Git - ~andy/linux/blob - drivers/media/video/s5p-fimc/fimc-capture.c
03ddf5d6164fb0afc74ac03c5cc07d740d69b51a
[~andy/linux] / 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         int ret;
241
242         ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_stream, 1);
243         if (ret && ret != -ENOIOCTLCMD)
244                 return ret;
245
246         ret = fimc_prepare_config(ctx, ctx->state);
247         if (ret)
248                 return ret;
249
250         isp_info = fimc->pdata->isp_info[fimc->vid_cap.input_index];
251         fimc_hw_set_camera_type(fimc, isp_info);
252         fimc_hw_set_camera_source(fimc, isp_info);
253         fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
254
255         if (ctx->state & FIMC_PARAMS) {
256                 ret = fimc_set_scaler_info(ctx);
257                 if (ret) {
258                         err("Scaler setup error");
259                         return ret;
260                 }
261                 fimc_hw_set_input_path(ctx);
262                 fimc_hw_set_scaler(ctx);
263                 fimc_hw_set_target_format(ctx);
264                 fimc_hw_set_rotation(ctx);
265                 fimc_hw_set_effect(ctx);
266         }
267
268         fimc_hw_set_output_path(ctx);
269         fimc_hw_set_out_dma(ctx);
270
271         INIT_LIST_HEAD(&fimc->vid_cap.pending_buf_q);
272         INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
273         fimc->vid_cap.active_buf_cnt = 0;
274         fimc->vid_cap.frame_count = 0;
275         fimc->vid_cap.buf_index = fimc_hw_get_frame_index(fimc);
276
277         set_bit(ST_CAPT_PEND, &fimc->state);
278
279         return 0;
280 }
281
282 static int stop_streaming(struct vb2_queue *q)
283 {
284         struct fimc_ctx *ctx = q->drv_priv;
285         struct fimc_dev *fimc = ctx->fimc_dev;
286         unsigned long flags;
287
288         spin_lock_irqsave(&fimc->slock, flags);
289         if (!fimc_capture_running(fimc) && !fimc_capture_pending(fimc)) {
290                 spin_unlock_irqrestore(&fimc->slock, flags);
291                 return -EINVAL;
292         }
293         spin_unlock_irqrestore(&fimc->slock, flags);
294
295         return fimc_stop_capture(fimc);
296 }
297
298 static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
299 {
300         if (!fr || plane >= fr->fmt->memplanes)
301                 return 0;
302
303         dbg("%s: w: %d. h: %d. depth[%d]: %d",
304             __func__, fr->width, fr->height, plane, fr->fmt->depth[plane]);
305
306         return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8;
307
308 }
309
310 static int queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
311                        unsigned int *num_planes, unsigned long sizes[],
312                        void *allocators[])
313 {
314         struct fimc_ctx *ctx = vq->drv_priv;
315         struct fimc_fmt *fmt = ctx->d_frame.fmt;
316         int i;
317
318         if (!fmt)
319                 return -EINVAL;
320
321         *num_planes = fmt->memplanes;
322
323         dbg("%s, buffer count=%d, plane count=%d",
324             __func__, *num_buffers, *num_planes);
325
326         for (i = 0; i < fmt->memplanes; i++) {
327                 sizes[i] = get_plane_size(&ctx->d_frame, i);
328                 dbg("plane: %u, plane_size: %lu", i, sizes[i]);
329                 allocators[i] = ctx->fimc_dev->alloc_ctx;
330         }
331
332         return 0;
333 }
334
335 static int buffer_init(struct vb2_buffer *vb)
336 {
337         /* TODO: */
338         return 0;
339 }
340
341 static int buffer_prepare(struct vb2_buffer *vb)
342 {
343         struct vb2_queue *vq = vb->vb2_queue;
344         struct fimc_ctx *ctx = vq->drv_priv;
345         struct v4l2_device *v4l2_dev = &ctx->fimc_dev->m2m.v4l2_dev;
346         int i;
347
348         if (!ctx->d_frame.fmt || vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
349                 return -EINVAL;
350
351         for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
352                 unsigned long size = get_plane_size(&ctx->d_frame, i);
353
354                 if (vb2_plane_size(vb, i) < size) {
355                         v4l2_err(v4l2_dev, "User buffer too small (%ld < %ld)\n",
356                                  vb2_plane_size(vb, i), size);
357                         return -EINVAL;
358                 }
359
360                 vb2_set_plane_payload(vb, i, size);
361         }
362
363         return 0;
364 }
365
366 static void buffer_queue(struct vb2_buffer *vb)
367 {
368         struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
369         struct fimc_dev *fimc = ctx->fimc_dev;
370         struct fimc_vid_buffer *buf
371                 = container_of(vb, struct fimc_vid_buffer, vb);
372         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
373         unsigned long flags;
374
375         spin_lock_irqsave(&fimc->slock, flags);
376         fimc_vid_cap_buf_queue(fimc, buf);
377
378         dbg("active_buf_cnt: %d", fimc->vid_cap.active_buf_cnt);
379
380         if (vid_cap->active_buf_cnt >= vid_cap->reqbufs_count ||
381            vid_cap->active_buf_cnt >= FIMC_MAX_OUT_BUFS) {
382                 if (!test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
383                         fimc_activate_capture(ctx);
384                         dbg("");
385                 }
386         }
387         spin_unlock_irqrestore(&fimc->slock, flags);
388 }
389
390 static void fimc_lock(struct vb2_queue *vq)
391 {
392         struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
393         mutex_lock(&ctx->fimc_dev->lock);
394 }
395
396 static void fimc_unlock(struct vb2_queue *vq)
397 {
398         struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
399         mutex_unlock(&ctx->fimc_dev->lock);
400 }
401
402 static struct vb2_ops fimc_capture_qops = {
403         .queue_setup            = queue_setup,
404         .buf_prepare            = buffer_prepare,
405         .buf_queue              = buffer_queue,
406         .buf_init               = buffer_init,
407         .wait_prepare           = fimc_unlock,
408         .wait_finish            = fimc_lock,
409         .start_streaming        = start_streaming,
410         .stop_streaming         = stop_streaming,
411 };
412
413 static int fimc_capture_open(struct file *file)
414 {
415         struct fimc_dev *fimc = video_drvdata(file);
416         int ret = 0;
417
418         dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
419
420         /* Return if the corresponding video mem2mem node is already opened. */
421         if (fimc_m2m_active(fimc))
422                 return -EBUSY;
423
424         if (++fimc->vid_cap.refcnt == 1) {
425                 ret = fimc_isp_subdev_init(fimc, 0);
426                 if (ret) {
427                         fimc->vid_cap.refcnt--;
428                         return -EIO;
429                 }
430         }
431
432         file->private_data = fimc->vid_cap.ctx;
433
434         return 0;
435 }
436
437 static int fimc_capture_close(struct file *file)
438 {
439         struct fimc_dev *fimc = video_drvdata(file);
440
441         dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
442
443         if (--fimc->vid_cap.refcnt == 0) {
444                 fimc_stop_capture(fimc);
445                 vb2_queue_release(&fimc->vid_cap.vbq);
446
447                 v4l2_err(&fimc->vid_cap.v4l2_dev, "releasing ISP\n");
448
449                 v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
450                 clk_disable(fimc->clock[CLK_CAM]);
451                 fimc_subdev_unregister(fimc);
452         }
453
454         return 0;
455 }
456
457 static unsigned int fimc_capture_poll(struct file *file,
458                                       struct poll_table_struct *wait)
459 {
460         struct fimc_ctx *ctx = file->private_data;
461         struct fimc_dev *fimc = ctx->fimc_dev;
462
463         return vb2_poll(&fimc->vid_cap.vbq, file, wait);
464 }
465
466 static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
467 {
468         struct fimc_ctx *ctx = file->private_data;
469         struct fimc_dev *fimc = ctx->fimc_dev;
470
471         return vb2_mmap(&fimc->vid_cap.vbq, vma);
472 }
473
474 /* video device file operations */
475 static const struct v4l2_file_operations fimc_capture_fops = {
476         .owner          = THIS_MODULE,
477         .open           = fimc_capture_open,
478         .release        = fimc_capture_close,
479         .poll           = fimc_capture_poll,
480         .unlocked_ioctl = video_ioctl2,
481         .mmap           = fimc_capture_mmap,
482 };
483
484 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
485                                         struct v4l2_capability *cap)
486 {
487         struct fimc_ctx *ctx = file->private_data;
488         struct fimc_dev *fimc = ctx->fimc_dev;
489
490         strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
491         strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
492         cap->bus_info[0] = 0;
493         cap->version = KERNEL_VERSION(1, 0, 0);
494         cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
495                             V4L2_CAP_VIDEO_CAPTURE_MPLANE;
496
497         return 0;
498 }
499
500 /* Synchronize formats of the camera interface input and attached  sensor. */
501 static int sync_capture_fmt(struct fimc_ctx *ctx)
502 {
503         struct fimc_frame *frame = &ctx->s_frame;
504         struct fimc_dev *fimc = ctx->fimc_dev;
505         struct v4l2_mbus_framefmt *fmt = &fimc->vid_cap.fmt;
506         int ret;
507
508         fmt->width  = ctx->d_frame.o_width;
509         fmt->height = ctx->d_frame.o_height;
510
511         ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_mbus_fmt, fmt);
512         if (ret == -ENOIOCTLCMD) {
513                 err("s_mbus_fmt failed");
514                 return ret;
515         }
516         dbg("w: %d, h: %d, code= %d", fmt->width, fmt->height, fmt->code);
517
518         frame->fmt = find_mbus_format(fmt, FMT_FLAGS_CAM);
519         if (!frame->fmt) {
520                 err("fimc source format not found\n");
521                 return -EINVAL;
522         }
523
524         frame->f_width  = fmt->width;
525         frame->f_height = fmt->height;
526         frame->width    = fmt->width;
527         frame->height   = fmt->height;
528         frame->o_width  = fmt->width;
529         frame->o_height = fmt->height;
530         frame->offs_h   = 0;
531         frame->offs_v   = 0;
532
533         return 0;
534 }
535
536 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
537                                  struct v4l2_format *f)
538 {
539         struct fimc_ctx *ctx = priv;
540         struct fimc_dev *fimc = ctx->fimc_dev;
541         struct fimc_frame *frame;
542         struct v4l2_pix_format_mplane *pix;
543         int ret;
544         int i;
545
546         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
547                 return -EINVAL;
548
549         ret = fimc_vidioc_try_fmt_mplane(file, priv, f);
550         if (ret)
551                 return ret;
552
553         if (vb2_is_streaming(&fimc->vid_cap.vbq) || fimc_capture_active(fimc))
554                 return -EBUSY;
555
556         frame = &ctx->d_frame;
557
558         pix = &f->fmt.pix_mp;
559         frame->fmt = find_format(f, FMT_FLAGS_M2M | FMT_FLAGS_CAM);
560         if (!frame->fmt) {
561                 err("fimc target format not found\n");
562                 return -EINVAL;
563         }
564
565         for (i = 0; i < frame->fmt->colplanes; i++)
566                 frame->payload[i] = pix->plane_fmt[i].bytesperline * pix->height;
567
568         /* Output DMA frame pixel size and offsets. */
569         frame->f_width = pix->plane_fmt[0].bytesperline * 8
570                         / frame->fmt->depth[0];
571         frame->f_height = pix->height;
572         frame->width    = pix->width;
573         frame->height   = pix->height;
574         frame->o_width  = pix->width;
575         frame->o_height = pix->height;
576         frame->offs_h   = 0;
577         frame->offs_v   = 0;
578
579         ctx->state |= (FIMC_PARAMS | FIMC_DST_FMT);
580
581         ret = sync_capture_fmt(ctx);
582         return ret;
583 }
584
585 static int fimc_cap_enum_input(struct file *file, void *priv,
586                                      struct v4l2_input *i)
587 {
588         struct fimc_ctx *ctx = priv;
589         struct s5p_platform_fimc *pldata = ctx->fimc_dev->pdata;
590         struct s5p_fimc_isp_info *isp_info;
591
592         if (i->index >= FIMC_MAX_CAMIF_CLIENTS)
593                 return -EINVAL;
594
595         isp_info = pldata->isp_info[i->index];
596         if (isp_info == NULL)
597                 return -EINVAL;
598
599         i->type = V4L2_INPUT_TYPE_CAMERA;
600         strncpy(i->name, isp_info->board_info->type, 32);
601         return 0;
602 }
603
604 static int fimc_cap_s_input(struct file *file, void *priv,
605                                   unsigned int i)
606 {
607         struct fimc_ctx *ctx = priv;
608         struct fimc_dev *fimc = ctx->fimc_dev;
609         struct s5p_platform_fimc *pdata = fimc->pdata;
610
611         if (fimc_capture_active(ctx->fimc_dev))
612                 return -EBUSY;
613
614         if (i >= FIMC_MAX_CAMIF_CLIENTS || !pdata->isp_info[i])
615                 return -EINVAL;
616
617
618         if (fimc->vid_cap.sd) {
619                 int ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
620                 if (ret)
621                         err("s_power failed: %d", ret);
622
623                 clk_disable(fimc->clock[CLK_CAM]);
624         }
625
626         /* Release the attached sensor subdevice. */
627         fimc_subdev_unregister(fimc);
628
629         return fimc_isp_subdev_init(fimc, i);
630 }
631
632 static int fimc_cap_g_input(struct file *file, void *priv,
633                                        unsigned int *i)
634 {
635         struct fimc_ctx *ctx = priv;
636         struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
637
638         *i = cap->input_index;
639         return 0;
640 }
641
642 static int fimc_cap_streamon(struct file *file, void *priv,
643                              enum v4l2_buf_type type)
644 {
645         struct fimc_ctx *ctx = priv;
646         struct fimc_dev *fimc = ctx->fimc_dev;
647
648         if (fimc_capture_active(fimc) || !fimc->vid_cap.sd)
649                 return -EBUSY;
650
651         if (!(ctx->state & FIMC_DST_FMT)) {
652                 v4l2_err(&fimc->vid_cap.v4l2_dev, "Format is not set\n");
653                 return -EINVAL;
654         }
655
656         return vb2_streamon(&fimc->vid_cap.vbq, type);
657 }
658
659 static int fimc_cap_streamoff(struct file *file, void *priv,
660                             enum v4l2_buf_type type)
661 {
662         struct fimc_ctx *ctx = priv;
663         struct fimc_dev *fimc = ctx->fimc_dev;
664
665         return vb2_streamoff(&fimc->vid_cap.vbq, type);
666 }
667
668 static int fimc_cap_reqbufs(struct file *file, void *priv,
669                             struct v4l2_requestbuffers *reqbufs)
670 {
671         struct fimc_ctx *ctx = priv;
672         struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
673         int ret;
674
675
676         ret = vb2_reqbufs(&cap->vbq, reqbufs);
677         if (!ret)
678                 cap->reqbufs_count = reqbufs->count;
679
680         return ret;
681 }
682
683 static int fimc_cap_querybuf(struct file *file, void *priv,
684                            struct v4l2_buffer *buf)
685 {
686         struct fimc_ctx *ctx = priv;
687         struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
688
689         return vb2_querybuf(&cap->vbq, buf);
690 }
691
692 static int fimc_cap_qbuf(struct file *file, void *priv,
693                           struct v4l2_buffer *buf)
694 {
695         struct fimc_ctx *ctx = priv;
696         struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
697         return vb2_qbuf(&cap->vbq, buf);
698 }
699
700 static int fimc_cap_dqbuf(struct file *file, void *priv,
701                            struct v4l2_buffer *buf)
702 {
703         struct fimc_ctx *ctx = priv;
704         return vb2_dqbuf(&ctx->fimc_dev->vid_cap.vbq, buf,
705                 file->f_flags & O_NONBLOCK);
706 }
707
708 static int fimc_cap_s_ctrl(struct file *file, void *priv,
709                          struct v4l2_control *ctrl)
710 {
711         struct fimc_ctx *ctx = priv;
712         int ret = -EINVAL;
713
714         /* Allow any controls but 90/270 rotation while streaming */
715         if (!fimc_capture_active(ctx->fimc_dev) ||
716             ctrl->id != V4L2_CID_ROTATE ||
717             (ctrl->value != 90 && ctrl->value != 270)) {
718                 ret = check_ctrl_val(ctx, ctrl);
719                 if (!ret) {
720                         ret = fimc_s_ctrl(ctx, ctrl);
721                         if (!ret)
722                                 ctx->state |= FIMC_PARAMS;
723                 }
724         }
725         if (ret == -EINVAL)
726                 ret = v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd,
727                                        core, s_ctrl, ctrl);
728         return ret;
729 }
730
731 static int fimc_cap_cropcap(struct file *file, void *fh,
732                             struct v4l2_cropcap *cr)
733 {
734         struct fimc_frame *f;
735         struct fimc_ctx *ctx = fh;
736
737         if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
738                 return -EINVAL;
739
740         f = &ctx->s_frame;
741
742         cr->bounds.left         = 0;
743         cr->bounds.top          = 0;
744         cr->bounds.width        = f->o_width;
745         cr->bounds.height       = f->o_height;
746         cr->defrect             = cr->bounds;
747
748         return 0;
749 }
750
751 static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
752 {
753         struct fimc_frame *f;
754         struct fimc_ctx *ctx = file->private_data;
755
756         f = &ctx->s_frame;
757
758         cr->c.left      = f->offs_h;
759         cr->c.top       = f->offs_v;
760         cr->c.width     = f->width;
761         cr->c.height    = f->height;
762
763         return 0;
764 }
765
766 static int fimc_cap_s_crop(struct file *file, void *fh,
767                                struct v4l2_crop *cr)
768 {
769         struct fimc_frame *f;
770         struct fimc_ctx *ctx = file->private_data;
771         struct fimc_dev *fimc = ctx->fimc_dev;
772         int ret = -EINVAL;
773
774         if (fimc_capture_active(fimc))
775                 return -EBUSY;
776
777         ret = fimc_try_crop(ctx, cr);
778         if (ret)
779                 return ret;
780
781         if (!(ctx->state & FIMC_DST_FMT)) {
782                 v4l2_err(&fimc->vid_cap.v4l2_dev,
783                          "Capture color format not set\n");
784                 return -EINVAL; /* TODO: make sure this is the right value */
785         }
786
787         f = &ctx->s_frame;
788         /* Check for the pixel scaling ratio when cropping input image. */
789         ret = fimc_check_scaler_ratio(&cr->c, &ctx->d_frame);
790         if (ret) {
791                 v4l2_err(&fimc->vid_cap.v4l2_dev, "Out of the scaler range");
792                 return ret;
793         }
794
795         f->offs_h = cr->c.left;
796         f->offs_v = cr->c.top;
797         f->width  = cr->c.width;
798         f->height = cr->c.height;
799
800         return 0;
801 }
802
803
804 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
805         .vidioc_querycap                = fimc_vidioc_querycap_capture,
806
807         .vidioc_enum_fmt_vid_cap_mplane = fimc_vidioc_enum_fmt_mplane,
808         .vidioc_try_fmt_vid_cap_mplane  = fimc_vidioc_try_fmt_mplane,
809         .vidioc_s_fmt_vid_cap_mplane    = fimc_cap_s_fmt_mplane,
810         .vidioc_g_fmt_vid_cap_mplane    = fimc_vidioc_g_fmt_mplane,
811
812         .vidioc_reqbufs                 = fimc_cap_reqbufs,
813         .vidioc_querybuf                = fimc_cap_querybuf,
814
815         .vidioc_qbuf                    = fimc_cap_qbuf,
816         .vidioc_dqbuf                   = fimc_cap_dqbuf,
817
818         .vidioc_streamon                = fimc_cap_streamon,
819         .vidioc_streamoff               = fimc_cap_streamoff,
820
821         .vidioc_queryctrl               = fimc_vidioc_queryctrl,
822         .vidioc_g_ctrl                  = fimc_vidioc_g_ctrl,
823         .vidioc_s_ctrl                  = fimc_cap_s_ctrl,
824
825         .vidioc_g_crop                  = fimc_cap_g_crop,
826         .vidioc_s_crop                  = fimc_cap_s_crop,
827         .vidioc_cropcap                 = fimc_cap_cropcap,
828
829         .vidioc_enum_input              = fimc_cap_enum_input,
830         .vidioc_s_input                 = fimc_cap_s_input,
831         .vidioc_g_input                 = fimc_cap_g_input,
832 };
833
834 /* fimc->lock must be already initialized */
835 int fimc_register_capture_device(struct fimc_dev *fimc)
836 {
837         struct v4l2_device *v4l2_dev = &fimc->vid_cap.v4l2_dev;
838         struct video_device *vfd;
839         struct fimc_vid_cap *vid_cap;
840         struct fimc_ctx *ctx;
841         struct v4l2_format f;
842         struct fimc_frame *fr;
843         struct vb2_queue *q;
844         int ret;
845
846         ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
847         if (!ctx)
848                 return -ENOMEM;
849
850         ctx->fimc_dev    = fimc;
851         ctx->in_path     = FIMC_CAMERA;
852         ctx->out_path    = FIMC_DMA;
853         ctx->state       = FIMC_CTX_CAP;
854
855         /* Default format of the output frames */
856         f.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32;
857         fr = &ctx->d_frame;
858         fr->fmt = find_format(&f, FMT_FLAGS_M2M);
859         fr->width = fr->f_width = fr->o_width = 640;
860         fr->height = fr->f_height = fr->o_height = 480;
861
862         if (!v4l2_dev->name[0])
863                 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
864                          "%s.capture", dev_name(&fimc->pdev->dev));
865
866         ret = v4l2_device_register(NULL, v4l2_dev);
867         if (ret)
868                 goto err_info;
869
870         vfd = video_device_alloc();
871         if (!vfd) {
872                 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
873                 goto err_v4l2_reg;
874         }
875
876         snprintf(vfd->name, sizeof(vfd->name), "%s:cap",
877                  dev_name(&fimc->pdev->dev));
878
879         vfd->fops       = &fimc_capture_fops;
880         vfd->ioctl_ops  = &fimc_capture_ioctl_ops;
881         vfd->minor      = -1;
882         vfd->release    = video_device_release;
883         vfd->lock       = &fimc->lock;
884         video_set_drvdata(vfd, fimc);
885
886         vid_cap = &fimc->vid_cap;
887         vid_cap->vfd = vfd;
888         vid_cap->active_buf_cnt = 0;
889         vid_cap->reqbufs_count  = 0;
890         vid_cap->refcnt = 0;
891         /* Default color format for image sensor */
892         vid_cap->fmt.code = V4L2_MBUS_FMT_YUYV8_2X8;
893
894         INIT_LIST_HEAD(&vid_cap->pending_buf_q);
895         INIT_LIST_HEAD(&vid_cap->active_buf_q);
896         spin_lock_init(&ctx->slock);
897         vid_cap->ctx = ctx;
898
899         q = &fimc->vid_cap.vbq;
900         memset(q, 0, sizeof(*q));
901         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
902         q->io_modes = VB2_MMAP | VB2_USERPTR;
903         q->drv_priv = fimc->vid_cap.ctx;
904         q->ops = &fimc_capture_qops;
905         q->mem_ops = &vb2_dma_contig_memops;
906         q->buf_struct_size = sizeof(struct fimc_vid_buffer);
907
908         vb2_queue_init(q);
909
910         ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
911         if (ret) {
912                 v4l2_err(v4l2_dev, "Failed to register video device\n");
913                 goto err_vd_reg;
914         }
915
916         v4l2_info(v4l2_dev,
917                   "FIMC capture driver registered as /dev/video%d\n",
918                   vfd->num);
919
920         return 0;
921
922 err_vd_reg:
923         video_device_release(vfd);
924 err_v4l2_reg:
925         v4l2_device_unregister(v4l2_dev);
926 err_info:
927         dev_err(&fimc->pdev->dev, "failed to install\n");
928         return ret;
929 }
930
931 void fimc_unregister_capture_device(struct fimc_dev *fimc)
932 {
933         struct fimc_vid_cap *capture = &fimc->vid_cap;
934
935         if (capture->vfd)
936                 video_unregister_device(capture->vfd);
937
938         kfree(capture->ctx);
939 }