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