upload tizen1.0 source
[kernel/linux-2.6.36.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-cma.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         if (vid_cap->fb_sd) {
75                 v4l2_device_unregister_subdev(vid_cap->fb_sd);
76                 vid_cap->fb_sd = NULL;
77         }
78         vid_cap->input_index = -1;
79 }
80
81 static int mipi_csi_register_callback(struct device *dev, void *p)
82 {
83         struct v4l2_subdev **sd = p;
84
85         /*
86          * FIXME: detect platform device id and handle multiple
87          * MIPI-CSI devices.
88          */
89         *sd = dev_get_drvdata(dev);
90
91         if (*sd) {
92                 struct platform_device *pdev = v4l2_get_subdevdata(*sd);
93                 if (pdev)
94                         dbg("pdev->id: %d", pdev->id);
95         }
96
97         return 0; /* non-zero value stops iteration */
98 }
99
100 static struct v4l2_subdev * s5p_mipi_get_subdev(int id)
101 {
102         const char *module_name = "s5p-mipi-csis";
103         struct device_driver *drv;
104         struct v4l2_subdev *sd = NULL;
105         int ret;
106
107         drv = driver_find(module_name, &platform_bus_type);
108         if (!drv)  {
109                 request_module(module_name);
110                 drv = driver_find(module_name, &platform_bus_type);
111         }
112         if (!drv)
113                 return ERR_PTR(-ENODEV);
114
115         /*
116          * FIXME: detect platform device id and handle multiple
117          * MIPI-CSI devices. Now always a subdev from the last
118          * found device is returned.
119          */
120         ret = driver_for_each_device(drv, NULL, &sd,
121                                      mipi_csi_register_callback);
122         put_driver(drv);
123
124         return ret ? NULL : sd;
125 }
126
127 static int s3cfb_register_callback(struct device *dev, void *p)
128 {
129         struct v4l2_subdev **sd = p;
130
131         /*
132          * FIXME: detect platform device id and handle multiple
133          * MIPI-CSI devices.
134          */
135         *sd = dev_get_drvdata(dev);
136
137         if (*sd) {
138                 struct platform_device *pdev = v4l2_get_subdevdata(*sd);
139                 if (pdev)
140                         dbg("pdev->id: %d", pdev->id);
141         }
142
143         return 0; /* non-zero value stops iteration */
144 }
145
146 static struct v4l2_subdev *s3c_fb_get_subdev(int id)
147 {
148         const char *module_name = FIMD_MODULE_NAME;
149         struct device_driver *drv;
150         struct v4l2_subdev *sd = NULL;
151         int ret;
152
153         drv = driver_find(module_name, &platform_bus_type);
154         if (!drv) {
155                 request_module(module_name);
156                 drv = driver_find(module_name, &platform_bus_type);
157         }
158         if (!drv)
159                 return ERR_PTR(-ENODEV);
160         /*
161          * FIXME: detect platform device id and handle multiple
162          * MIPI-CSI devices. Now always a subdev from the last
163          * found device is returned.
164          */
165         ret = driver_for_each_device(drv, NULL, &sd,
166                                      s3cfb_register_callback);
167         put_driver(drv);
168
169         return ret ? NULL : sd;
170 }
171
172 /**
173  * fimc_subdev_attach - attach v4l2_subdev to camera host interface
174  *
175  * @fimc: FIMC device information
176  * @index: index to the array of available subdevices,
177  *         -1 for full array search or non negative value
178  *         to select specific subdevice
179  */
180 static int fimc_subdev_attach(struct fimc_dev *fimc, int index)
181 {
182         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
183         struct s5p_platform_fimc *pdata = fimc->pdata;
184         struct s5p_fimc_isp_info *isp_info;
185         struct v4l2_subdev *sd;
186         int i;
187
188         for (i = 0; i < pdata->num_clients; ++i) {
189                 isp_info = pdata->isp_info[i];
190                 if (!isp_info || (index >= 0 && i != index))
191                         continue;
192                 if (isp_info->bus_type == FIMC_MIPI_CSI2) {
193                         vid_cap->mipi_sd = s5p_mipi_get_subdev(0);
194                         if (IS_ERR_OR_NULL(vid_cap->mipi_sd)) {
195                                 fimc->vid_cap.mipi_sd = NULL;
196                                 return PTR_ERR(vid_cap->mipi_sd);
197                         }
198                 }
199                 else if (isp_info->bus_type == FIMC_LCD_WB) {
200                         vid_cap->fb_sd = s3c_fb_get_subdev(0);
201                         if (IS_ERR_OR_NULL(vid_cap->fb_sd))
202                                 return PTR_ERR(vid_cap->fb_sd);
203                         vid_cap->ctx->in_path = FIMC_LCD_WB;
204                         vid_cap->input_index = i;
205
206                         return 0;
207                 }
208
209                 sd = fimc_subdev_register(fimc, isp_info);
210                 if (sd) {
211                         vid_cap->sd = sd;
212                         vid_cap->input_index = i;
213
214                         return 0;
215                 }
216         }
217
218         vid_cap->input_index = -1;
219         vid_cap->sd = NULL;
220         v4l2_err(&vid_cap->v4l2_dev, "fimc%d: sensor attach failed\n",
221                  fimc->id);
222         return -ENODEV;
223 }
224 static int fimc_isp_subdev_init(struct fimc_dev *fimc, unsigned int index)
225 {
226         struct s5p_fimc_isp_info *isp_info;
227         int ret;
228         if (index >= fimc->pdata->num_clients)
229                 return -EINVAL;
230
231         isp_info = fimc->pdata->isp_info[index];
232         if (!isp_info)
233                 return -EINVAL;
234
235         if (isp_info->clk_frequency) {
236                 ret = fimc_clk_setrate(fimc, CLK_CAM, isp_info);
237                 if (ret < 0)
238                         return -EINVAL;
239         }
240
241         ret = clk_enable(fimc->clock[CLK_CAM]);
242         if (ret)
243                 return ret;
244
245         ret = fimc_subdev_attach(fimc, index);
246         if (ret)
247                 return ret;
248
249         ret = fimc_hw_set_camera_polarity(fimc, isp_info);
250         if (ret)
251                 return ret;
252
253         if (fimc->vid_cap.mipi_sd)
254                 ret = v4l2_subdev_call(fimc->vid_cap.mipi_sd, core, s_power, 1);
255
256         if(!ret)
257                 ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 1);
258
259         if (fimc->vid_cap.fb_sd) {
260                 unsigned int wb_on = 1;
261                 dbg("write-back mode\n");
262                 ret = v4l2_subdev_call(fimc->vid_cap.fb_sd, core, ioctl,
263                                         (unsigned int)NULL, &wb_on);
264         }
265         if (!ret)
266                 return ret;
267
268         /* enabling power failed so unregister subdev */
269         fimc_subdev_unregister(fimc);
270
271         v4l2_err(&fimc->vid_cap.v4l2_dev, "ISP initialization failed: %d\n",
272                  ret);
273
274         return ret;
275 }
276
277 /*
278  * At least one buffer on the pending_buf_q queue is required.
279  * Locking: The caller holds fimc->slock spinlock.
280  */
281 int fimc_vid_cap_buf_queue(struct fimc_dev *fimc,
282                            struct fimc_vid_buffer *fimc_vb)
283 {
284         struct fimc_vid_cap *cap = &fimc->vid_cap;
285         struct fimc_ctx *ctx = cap->ctx;
286         int ret = 0;
287
288         BUG_ON(!fimc || !fimc_vb);
289
290         ret = fimc_prepare_addr(ctx, &fimc_vb->vb, &ctx->d_frame,
291                                 &fimc_vb->paddr);
292         if (ret)
293                 return ret;
294
295         if (test_bit(ST_CAPT_STREAM, &fimc->state)) {
296                 fimc_pending_queue_add(cap, fimc_vb);
297         } else {
298                 /* Setup the buffer directly for processing. */
299                 int buf_id = (cap->reqbufs_count == 1) ? -1 : cap->buf_index;
300                 fimc_hw_set_output_addr(fimc, &fimc_vb->paddr, buf_id);
301
302                 fimc_vb->index = cap->buf_index;
303                 active_queue_add(cap, fimc_vb);
304
305                 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
306                         cap->buf_index = 0;
307         }
308         return ret;
309 }
310
311 static int fimc_stop_capture(struct fimc_dev *fimc)
312 {
313         unsigned long flags;
314         struct fimc_vid_cap *cap;
315         struct fimc_vid_buffer *buf;
316
317         cap = &fimc->vid_cap;
318
319         if (!fimc_capture_active(fimc))
320                 return 0;
321
322         spin_lock_irqsave(&fimc->slock, flags);
323         set_bit(ST_CAPT_SHUT, &fimc->state);
324         fimc_deactivate_capture(fimc);
325         spin_unlock_irqrestore(&fimc->slock, flags);
326
327         wait_event_timeout(fimc->irq_queue,
328                            !test_bit(ST_CAPT_SHUT, &fimc->state),
329                            FIMC_SHUTDOWN_TIMEOUT);
330         if (cap->sd)
331                 v4l2_subdev_call(cap->sd, video, s_stream, 0);
332
333         if (fimc->vid_cap.mipi_sd)
334                 v4l2_subdev_call(fimc->vid_cap.mipi_sd, video, s_stream, 0);
335
336         spin_lock_irqsave(&fimc->slock, flags);
337         fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_PEND |
338                         1 << ST_CAPT_STREAM);
339
340         fimc->vid_cap.active_buf_cnt = 0;
341
342         /* Release buffers that were enqueued in the driver by videobuf2. */
343         while (!list_empty(&cap->pending_buf_q)) {
344                 buf = pending_queue_pop(cap);
345                 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
346         }
347
348         while (!list_empty(&cap->active_buf_q)) {
349                 buf = active_queue_pop(cap);
350                 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
351         }
352         spin_unlock_irqrestore(&fimc->slock, flags);
353
354         dbg("state: 0x%lx", fimc->state);
355         return 0;
356 }
357
358 static int start_streaming(struct vb2_queue *q)
359 {
360         struct fimc_ctx *ctx = q->drv_priv;
361         struct fimc_dev *fimc = ctx->fimc_dev;
362         struct s5p_fimc_isp_info *isp_info;
363         int ret = 0;
364
365         if (test_and_clear_bit(ST_SUSPEND, &fimc->state))
366                 pm_runtime_get_sync(&fimc->pdev->dev);
367
368         if (ret && ret != -ENOIOCTLCMD)
369                 return ret;
370
371         ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_stream, 1);
372         if (ret && ret != -ENOIOCTLCMD) {
373                 err("sd s_stream error");
374                 return ret;
375         }
376
377         if (fimc->vid_cap.mipi_sd) {
378                 ret = v4l2_subdev_call(fimc->vid_cap.mipi_sd, video, s_stream, 1);
379                 if (ret) {
380                         err("mipi s_stream error");
381                         return ret;
382                 }
383         }
384
385         ret = fimc_prepare_config(ctx, ctx->state);
386         if (ret)
387                 return ret;
388
389         isp_info = fimc->pdata->isp_info[fimc->vid_cap.input_index];
390         fimc_hw_set_camera_type(fimc, isp_info);
391
392         if (ctx->in_path == FIMC_LCD_WB) {
393                 if (isp_info->mux_id == 0)
394                         fimc_hwset_sysreg_camblk_fimd0_wb(fimc);
395                 else
396                         fimc_hwset_sysreg_camblk_fimd1_wb(fimc);
397         }
398         fimc_hw_set_camera_source(fimc, isp_info);
399         fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
400
401         if (ctx->state & FIMC_PARAMS) {
402                 ret = fimc_set_scaler_info(ctx);
403                 if (ret) {
404                         err("Scaler setup error");
405                         return ret;
406                 }
407                 fimc_hw_set_input_path(ctx);
408                 fimc_hw_set_prescaler(ctx);
409                 fimc_hw_set_mainscaler(ctx);
410                 fimc_hw_set_target_format(ctx);
411                 fimc_hw_set_rotation(ctx);
412                 fimc_hw_set_effect(ctx);
413         }
414
415         fimc_hw_set_out_dma(ctx);
416
417         INIT_LIST_HEAD(&fimc->vid_cap.pending_buf_q);
418         INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
419         fimc->vid_cap.active_buf_cnt = 0;
420         fimc->vid_cap.frame_count = 0;
421         fimc->vid_cap.buf_index = fimc_hw_get_frame_index(fimc);
422
423         set_bit(ST_CAPT_PEND, &fimc->state);
424         return 0;
425 }
426
427 static int stop_streaming(struct vb2_queue *q)
428 {
429         struct fimc_ctx *ctx = q->drv_priv;
430         struct fimc_dev *fimc = ctx->fimc_dev;
431         unsigned long flags;
432
433         if (test_and_clear_bit(ST_SUSPEND, &fimc->state))
434                 pm_runtime_get_sync(&fimc->pdev->dev);
435
436         spin_lock_irqsave(&fimc->slock, flags);
437         if (!fimc_capture_running(fimc) && !fimc_capture_pending(fimc)) {
438                 spin_unlock_irqrestore(&fimc->slock, flags);
439                 return -EINVAL;
440         }
441         spin_unlock_irqrestore(&fimc->slock, flags);
442
443         fimc_stop_capture(fimc);
444 #ifdef CONFIG_VIDEOBUF2_SDVMM
445         vb2_sdvmm_suspend(fimc->alloc_ctx);
446 #endif
447         if (!test_and_set_bit(ST_SUSPEND, &fimc->state))
448                 pm_runtime_put_sync(&fimc->pdev->dev);
449
450         return 0;
451 }
452
453 static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
454 {
455         if (!fr || plane >= fr->fmt->memplanes)
456                 return 0;
457
458         dbg("%s: w: %d. h: %d. depth[%d]: %d",
459             __func__, fr->width, fr->height, plane, fr->fmt->depth[plane]);
460
461         return fr->payload[plane];
462 }
463
464 static int queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
465                        unsigned int *num_planes, unsigned long sizes[],
466                        void *allocators[])
467 {
468         struct fimc_ctx *ctx = vq->drv_priv;
469         struct fimc_fmt *fmt = ctx->d_frame.fmt;
470         int i;
471
472         if (!fmt)
473                 return -EINVAL;
474
475         *num_planes = fmt->memplanes;
476
477         dbg("%s, buffer count=%d, plane count=%d",
478             __func__, *num_buffers, *num_planes);
479
480         for (i = 0; i < fmt->memplanes; i++) {
481                 sizes[i] = get_plane_size(&ctx->d_frame, i);
482                 dbg("plane: %u, plane_size: %lu", i, sizes[i]);
483                 allocators[i] = ctx->fimc_dev->alloc_ctx;
484         }
485
486         return 0;
487 }
488
489 static int buffer_init(struct vb2_buffer *vb)
490 {
491         /* TODO: */
492         return 0;
493 }
494
495 static int buffer_prepare(struct vb2_buffer *vb)
496 {
497         struct vb2_queue *vq = vb->vb2_queue;
498         struct fimc_ctx *ctx = vq->drv_priv;
499         struct fimc_frame *frame = &ctx->d_frame;
500         struct v4l2_device *v4l2_dev = &ctx->fimc_dev->m2m.v4l2_dev;
501         int i;
502
503         if (!frame->fmt || vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
504                 return -EINVAL;
505
506         for (i = 0; i < frame->fmt->memplanes; i++) {
507                 unsigned long size = get_plane_size(frame, i);
508                 dbg("vb2_plane_size(%d) : %ld", i, size);
509                 if (vb2_plane_size(vb, i) < size) {
510                         v4l2_err(v4l2_dev, "User buffer too small(%ld < %ld)\n",
511                                  vb2_plane_size(vb, i), size);
512                         return -EINVAL;
513                 }
514
515                 vb2_set_plane_payload(vb, i, size);
516                 if (frame->cacheable)
517                         vb2_sdvmm_cache_flush(ctx->fimc_dev->alloc_ctx, vb, i);
518         }
519
520         return 0;
521 }
522
523 static void buffer_queue(struct vb2_buffer *vb)
524 {
525         struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
526         struct fimc_dev *fimc = ctx->fimc_dev;
527         struct fimc_vid_buffer *buf
528                 = container_of(vb, struct fimc_vid_buffer, vb);
529         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
530         unsigned long flags;
531
532         spin_lock_irqsave(&fimc->slock, flags);
533         if (test_and_clear_bit(ST_SUSPEND, &fimc->state))
534                 pm_runtime_get_sync(&fimc->pdev->dev);
535
536         fimc_vid_cap_buf_queue(fimc, buf);
537
538         dbg("active_buf_cnt: %d", fimc->vid_cap.active_buf_cnt);
539
540         if (vid_cap->active_buf_cnt == FIMC_MAX_OUT_BUFS)
541                 set_bit(ST_CAPT_STREAM, &fimc->state);
542
543         if (test_bit(ST_CAPT_LAST_IRQ, &fimc->state) ||
544             !test_bit(ST_CAPT_RUN, &fimc->state)) {
545 #ifdef CONFIG_VIDEOBUF2_SDVMM
546                 if (vid_cap->active_buf_cnt == 1)
547                         vb2_sdvmm_resume(fimc->alloc_ctx);
548 #endif
549                 fimc_activate_capture(ctx);
550                 clear_bit(ST_CAPT_LAST_IRQ, &fimc->state);
551         }
552         spin_unlock_irqrestore(&fimc->slock, flags);
553 }
554
555 static void fimc_lock(struct vb2_queue *vq)
556 {
557 #ifdef FOR_DIFF_VER
558         struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
559         mutex_lock(&ctx->fimc_dev->lock);
560 #endif
561 }
562
563 static void fimc_unlock(struct vb2_queue *vq)
564 {
565 #ifdef FOR_DIFF_VER
566         struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
567         mutex_unlock(&ctx->fimc_dev->lock);
568 #endif
569 }
570
571 static struct vb2_ops fimc_capture_qops = {
572         .queue_setup            = queue_setup,
573         .buf_prepare            = buffer_prepare,
574         .buf_queue              = buffer_queue,
575         .buf_init               = buffer_init,
576         .wait_prepare           = fimc_unlock,
577         .wait_finish            = fimc_lock,
578         .start_streaming        = start_streaming,
579         .stop_streaming         = stop_streaming,
580 };
581
582 static int fimc_capture_open(struct file *file)
583 {
584         struct fimc_dev *fimc = video_drvdata(file);
585         int ret = 0;
586
587         dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
588         if (test_and_clear_bit(ST_SUSPEND, &fimc->state))
589                 pm_runtime_get_sync(&fimc->pdev->dev);
590
591         fimc_hw_reset(fimc);
592         fimc_hw_set_irq_level(fimc);
593
594         if (fimc->variant->out_buf_count > 4)
595                 fimc_hw_set_dma_seq(fimc, 0xF);
596
597         /* Return if the corresponding video mem2mem node is already opened. */
598         if (fimc_m2m_active(fimc))
599                 return -EBUSY;
600
601         if (++fimc->vid_cap.refcnt == 1) {
602                 ret = fimc_isp_subdev_init(fimc, 0);
603                 if (ret) {
604                         fimc->vid_cap.refcnt--;
605                         return -EIO;
606                 }
607         }
608
609         file->private_data = fimc->vid_cap.ctx;
610
611         if (!test_and_set_bit(ST_SUSPEND, &fimc->state))
612                 pm_runtime_put_sync(&fimc->pdev->dev);
613         return 0;
614 }
615
616 static int fimc_capture_close(struct file *file)
617 {
618         struct fimc_dev *fimc = video_drvdata(file);
619
620         dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
621
622         if (test_and_clear_bit(ST_SUSPEND, &fimc->state))
623                 pm_runtime_get_sync(&fimc->pdev->dev);
624
625         if (--fimc->vid_cap.refcnt == 0) {
626                 fimc_stop_capture(fimc);
627                 vb2_queue_release(&fimc->vid_cap.vbq);
628
629                 v4l2_err(&fimc->vid_cap.v4l2_dev, "releasing ISP\n");
630
631                 if (fimc->vid_cap.sd)
632                         v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
633
634                 if (fimc->vid_cap.mipi_sd)
635                         v4l2_subdev_call(fimc->vid_cap.mipi_sd, core, s_power, 0);
636
637                 clk_disable(fimc->clock[CLK_CAM]);
638                 fimc_subdev_unregister(fimc);
639         }
640
641         if (!test_and_set_bit(ST_SUSPEND, &fimc->state))
642                 pm_runtime_put_sync(&fimc->pdev->dev);
643
644         return 0;
645 }
646
647 static unsigned int fimc_capture_poll(struct file *file,
648                                       struct poll_table_struct *wait)
649 {
650         struct fimc_ctx *ctx = file->private_data;
651         struct fimc_dev *fimc = ctx->fimc_dev;
652
653         return vb2_poll(&fimc->vid_cap.vbq, file, wait);
654 }
655
656 static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
657 {
658         struct fimc_ctx *ctx = file->private_data;
659         struct fimc_dev *fimc = ctx->fimc_dev;
660
661         return vb2_mmap(&fimc->vid_cap.vbq, vma);
662 }
663
664 /* video device file operations */
665 static const struct v4l2_file_operations fimc_capture_fops = {
666         .owner          = THIS_MODULE,
667         .open           = fimc_capture_open,
668         .release        = fimc_capture_close,
669         .poll           = fimc_capture_poll,
670         .unlocked_ioctl = video_ioctl2,
671         .mmap           = fimc_capture_mmap,
672 };
673
674 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
675                                         struct v4l2_capability *cap)
676 {
677         struct fimc_ctx *ctx = file->private_data;
678         struct fimc_dev *fimc = ctx->fimc_dev;
679
680         strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
681         strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
682         cap->bus_info[0] = 0;
683         cap->version = KERNEL_VERSION(1, 0, 0);
684         cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
685                             V4L2_CAP_VIDEO_CAPTURE_MPLANE;
686
687         return 0;
688 }
689
690 /* Synchronize formats of the camera interface input and attached  sensor. */
691 static int sync_capture_fmt(struct fimc_ctx *ctx)
692 {
693         struct fimc_frame *frame = &ctx->s_frame;
694         struct fimc_dev *fimc = ctx->fimc_dev;
695         struct v4l2_mbus_framefmt *fmt = &fimc->vid_cap.fmt;
696         int ret = 0;
697
698         fmt->width  = ctx->d_frame.o_width;
699         fmt->height = ctx->d_frame.o_height;
700
701         if (fimc->vid_cap.sd)
702                 ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_mbus_fmt, fmt);
703
704         if (ret == -ENOIOCTLCMD) {
705                 err("s_mbus_fmt failed");
706                 return ret;
707         }
708
709         if (fimc->vid_cap.mipi_sd) {
710                 ret = v4l2_subdev_call(fimc->vid_cap.mipi_sd, video, s_mbus_fmt, fmt);
711                 if (ret) {
712                         err("s_mbus_fmt failed: %d", ret);
713                         return ret;
714                 }
715         }
716
717         dbg("w= %d, h= %d, code= %d", fmt->width, fmt->height, fmt->code);
718
719         frame->fmt = find_mbus_format(fmt, FMT_FLAGS_CAM);
720         if (!frame->fmt) {
721                 err("fimc source format not found\n");
722                 return -EINVAL;
723         }
724
725         frame->f_width  = fmt->width;
726         frame->f_height = fmt->height;
727         frame->width    = fmt->width;
728         frame->height   = fmt->height;
729         frame->o_width  = fmt->width;
730         frame->o_height = fmt->height;
731         frame->offs_h   = 0;
732         frame->offs_v   = 0;
733
734         dbg("frame->width : %d, frame->f_height : %d, frame->width : %d,\
735                 frame->height : %d, frame->o_width : %d, frame->o_height :\
736                 %d\n", frame->width, frame->f_height, frame->width,\
737                 frame->height, frame->o_width, frame->o_height);
738         return 0;
739 }
740
741 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
742                                  struct v4l2_format *f)
743 {
744         struct fimc_ctx *ctx = priv;
745         struct fimc_dev *fimc = ctx->fimc_dev;
746         struct fimc_frame *frame;
747         struct v4l2_pix_format_mplane *pix;
748         int ret;
749         int i;
750
751         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
752                 return -EINVAL;
753
754         ret = fimc_vidioc_try_fmt_mplane(file, priv, f);
755         if (ret)
756                 return ret;
757
758         if (vb2_is_streaming(&fimc->vid_cap.vbq) || fimc_capture_active(fimc))
759                 return -EBUSY;
760
761         frame = &ctx->d_frame;
762
763         pix = &f->fmt.pix_mp;
764         frame->fmt = find_format(f, FMT_FLAGS_M2M | FMT_FLAGS_CAM);
765         if (!frame->fmt) {
766                 err("fimc target format not found\n");
767                 return -EINVAL;
768         }
769
770         for (i = 0; i < frame->fmt->colplanes; i++)
771                 frame->payload[i] = pix->plane_fmt[i].sizeimage;
772
773         /* Output DMA frame pixel size and offsets. */
774         frame->f_width = pix->plane_fmt[0].bytesperline * 8
775                         / frame->fmt->depth[0];
776         frame->f_height = pix->height;
777         frame->width    = pix->width;
778         frame->height   = pix->height;
779         frame->o_width  = pix->width;
780         frame->o_height = pix->height;
781         frame->offs_h   = 0;
782         frame->offs_v   = 0;
783
784         ctx->state |= (FIMC_PARAMS | FIMC_DST_FMT);
785
786         ret = sync_capture_fmt(ctx);
787         return ret;
788 }
789
790 static int fimc_cap_enum_input(struct file *file, void *priv,
791                                      struct v4l2_input *i)
792 {
793         struct fimc_ctx *ctx = priv;
794         struct s5p_platform_fimc *pldata = ctx->fimc_dev->pdata;
795         struct s5p_fimc_isp_info *isp_info;
796
797         if (i->index >= pldata->num_clients)
798                 return -EINVAL;
799
800         isp_info = pldata->isp_info[i->index];
801         if (isp_info == NULL)
802                 return -EINVAL;
803
804         i->type = V4L2_INPUT_TYPE_CAMERA;
805         strncpy(i->name, isp_info->board_info->type, 32);
806         return 0;
807 }
808
809 static int fimc_cap_s_input(struct file *file, void *priv,
810                                   unsigned int i)
811 {
812         struct fimc_ctx *ctx = priv;
813         struct fimc_dev *fimc = ctx->fimc_dev;
814         struct s5p_platform_fimc *pdata = fimc->pdata;
815         int ret;
816         if (fimc_capture_active(ctx->fimc_dev))
817                 return -EBUSY;
818
819         if (i >= pdata->num_clients || !pdata->isp_info[i])
820                 return -EINVAL;
821
822         if (fimc->vid_cap.mipi_sd) {
823                 ret = v4l2_subdev_call(fimc->vid_cap.mipi_sd, core, s_power, 0);
824                 if (ret)
825                         err("s_power failed: %d", ret);
826         }
827
828         if (fimc->vid_cap.sd) {
829                 ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
830                 if (ret)
831                         err("s_power failed: %d", ret);
832
833                 clk_disable(fimc->clock[CLK_CAM]);
834         }
835         /* Release the attached sensor subdevice. */
836         fimc_subdev_unregister(fimc);
837
838         if (test_and_clear_bit(ST_SUSPEND, &fimc->state))
839                 pm_runtime_get_sync(&fimc->pdev->dev);
840
841         ret = fimc_isp_subdev_init(fimc, i);
842         if (ret)
843                 err("subdev init failed");
844
845         if (!test_and_set_bit(ST_SUSPEND, &fimc->state))
846                 pm_runtime_put_sync(&fimc->pdev->dev);
847
848         return ret;
849 }
850
851 static int fimc_cap_g_input(struct file *file, void *priv,
852                                        unsigned int *i)
853 {
854         struct fimc_ctx *ctx = priv;
855         struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
856
857         *i = cap->input_index;
858         return 0;
859 }
860
861 static int fimc_cap_streamon(struct file *file, void *priv,
862                              enum v4l2_buf_type type)
863 {
864         struct fimc_ctx *ctx = priv;
865         struct fimc_dev *fimc = ctx->fimc_dev;
866
867         if (fimc_capture_active(fimc) ||
868                 (!fimc->vid_cap.sd && !fimc->vid_cap.fb_sd))
869                 return -EBUSY;
870
871         if (!(ctx->state & FIMC_DST_FMT)) {
872                 v4l2_err(&fimc->vid_cap.v4l2_dev, "Format is not set\n");
873                 return -EINVAL;
874         }
875
876         return vb2_streamon(&fimc->vid_cap.vbq, type);
877 }
878
879 static int fimc_cap_streamoff(struct file *file, void *priv,
880                             enum v4l2_buf_type type)
881 {
882         struct fimc_ctx *ctx = priv;
883         struct fimc_dev *fimc = ctx->fimc_dev;
884
885         return vb2_streamoff(&fimc->vid_cap.vbq, type);
886 }
887
888 static int fimc_cap_reqbufs(struct file *file, void *priv,
889                             struct v4l2_requestbuffers *reqbufs)
890 {
891         struct fimc_ctx *ctx = priv;
892         struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
893         struct fimc_frame *frame;
894         int ret;
895
896         frame = ctx_get_frame(ctx, reqbufs->type);
897         frame->cacheable = ctx->cacheable;
898         vb2_sdvmm_set_cacheable(ctx->fimc_dev->alloc_ctx, frame->cacheable);
899
900         ret = vb2_reqbufs(&cap->vbq, reqbufs);
901         if (!ret)
902                 cap->reqbufs_count = reqbufs->count;
903
904         return ret;
905 }
906
907 static int fimc_cap_querybuf(struct file *file, void *priv,
908                            struct v4l2_buffer *buf)
909 {
910         struct fimc_ctx *ctx = priv;
911         struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
912
913         return vb2_querybuf(&cap->vbq, buf);
914 }
915
916 static int fimc_cap_qbuf(struct file *file, void *priv,
917                           struct v4l2_buffer *buf)
918 {
919         struct fimc_ctx *ctx = priv;
920         struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
921
922         return vb2_qbuf(&cap->vbq, buf);
923 }
924
925 static int fimc_cap_dqbuf(struct file *file, void *priv,
926                            struct v4l2_buffer *buf)
927 {
928         struct fimc_ctx *ctx = priv;
929         return vb2_dqbuf(&ctx->fimc_dev->vid_cap.vbq, buf,
930                 file->f_flags & O_NONBLOCK);
931 }
932
933 static int fimc_cap_s_ctrl(struct file *file, void *priv,
934                          struct v4l2_control *ctrl)
935 {
936         struct fimc_ctx *ctx = priv;
937         int ret = -EINVAL;
938
939         /* Allow any controls but 90/270 rotation while streaming */
940         if (!fimc_capture_active(ctx->fimc_dev) ||
941             ctrl->id != V4L2_CID_ROTATE ||
942             (ctrl->value != 90 && ctrl->value != 270)) {
943                 ret = check_ctrl_val(ctx, ctrl);
944                 if (!ret) {
945                         ret = fimc_s_ctrl(ctx, ctrl);
946                         if (!ret)
947                                 ctx->state |= FIMC_PARAMS;
948                 }
949         }
950         if (ret == -EINVAL && ctx->fimc_dev->vid_cap.sd)
951                 ret = v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd,
952                                        core, s_ctrl, ctrl);
953         return ret;
954 }
955
956 static int fimc_cap_cropcap(struct file *file, void *fh,
957                             struct v4l2_cropcap *cr)
958 {
959         struct fimc_frame *f;
960         struct fimc_ctx *ctx = fh;
961
962         if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
963                 return -EINVAL;
964
965         f = &ctx->s_frame;
966
967         cr->bounds.left         = 0;
968         cr->bounds.top          = 0;
969         cr->bounds.width        = f->o_width;
970         cr->bounds.height       = f->o_height;
971         cr->defrect             = cr->bounds;
972
973         return 0;
974 }
975
976 static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
977 {
978         struct fimc_frame *f;
979         struct fimc_ctx *ctx = file->private_data;
980
981         f = &ctx->s_frame;
982
983         cr->c.left      = f->offs_h;
984         cr->c.top       = f->offs_v;
985         cr->c.width     = f->width;
986         cr->c.height    = f->height;
987
988         return 0;
989 }
990
991 static int fimc_cap_s_crop(struct file *file, void *fh,
992                                struct v4l2_crop *cr)
993 {
994         struct fimc_frame *f;
995         struct fimc_ctx *ctx = file->private_data;
996         struct fimc_dev *fimc = ctx->fimc_dev;
997         int ret = -EINVAL;
998
999         if (fimc_capture_active(fimc))
1000                 return -EBUSY;
1001
1002         ret = fimc_try_crop(ctx, cr);
1003         if (ret)
1004                 return ret;
1005
1006         if (!(ctx->state & FIMC_DST_FMT)) {
1007                 v4l2_err(&fimc->vid_cap.v4l2_dev,
1008                          "Capture color format not set\n");
1009                 return -EINVAL; /* TODO: make sure this is the right value */
1010         }
1011
1012         f = &ctx->s_frame;
1013         /* Check for the pixel scaling ratio when cropping input image. */
1014         ret = fimc_check_scaler_ratio(ctx, cr->c.width, cr->c.height,
1015                                       ctx->d_frame.width, ctx->d_frame.height,
1016                                       ctx->rotation);
1017         if (ret) {
1018                 v4l2_err(&fimc->vid_cap.v4l2_dev, "Out of the scaler range");
1019                 return ret;
1020         }
1021
1022         f->offs_h = cr->c.left;
1023         f->offs_v = cr->c.top;
1024         f->width  = cr->c.width;
1025         f->height = cr->c.height;
1026
1027         return 0;
1028 }
1029
1030 static int fimc_cap_g_parm(struct file *filp, void *priv,
1031                            struct v4l2_streamparm *sp)
1032 {
1033         struct fimc_ctx *ctx = priv;
1034         struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
1035         int ret;
1036
1037         if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1038                 sp->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1039
1040         ret = v4l2_subdev_call(cap->sd, video, g_parm, sp);
1041         return ret ? -EINVAL : 0;
1042 }
1043
1044 static int fimc_cap_s_parm(struct file *filp, void *priv,
1045                            struct v4l2_streamparm *sp)
1046 {
1047         struct fimc_ctx *ctx = priv;
1048         struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
1049         int ret;
1050
1051         if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1052                 return -EINVAL;
1053
1054         ret = v4l2_subdev_call(cap->sd, video, s_parm, sp);
1055         return ret ? -EINVAL : 0;
1056 }
1057
1058 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1059         .vidioc_querycap                = fimc_vidioc_querycap_capture,
1060
1061         .vidioc_enum_fmt_vid_cap_mplane = fimc_vidioc_enum_fmt_mplane,
1062         .vidioc_try_fmt_vid_cap_mplane  = fimc_vidioc_try_fmt_mplane,
1063         .vidioc_s_fmt_vid_cap_mplane    = fimc_cap_s_fmt_mplane,
1064         .vidioc_g_fmt_vid_cap_mplane    = fimc_vidioc_g_fmt_mplane,
1065
1066         .vidioc_reqbufs                 = fimc_cap_reqbufs,
1067         .vidioc_querybuf                = fimc_cap_querybuf,
1068
1069         .vidioc_qbuf                    = fimc_cap_qbuf,
1070         .vidioc_dqbuf                   = fimc_cap_dqbuf,
1071
1072         .vidioc_streamon                = fimc_cap_streamon,
1073         .vidioc_streamoff               = fimc_cap_streamoff,
1074
1075         .vidioc_queryctrl               = fimc_vidioc_queryctrl,
1076         .vidioc_g_ctrl                  = fimc_vidioc_g_ctrl,
1077         .vidioc_s_ctrl                  = fimc_cap_s_ctrl,
1078
1079         .vidioc_g_crop                  = fimc_cap_g_crop,
1080         .vidioc_s_crop                  = fimc_cap_s_crop,
1081         .vidioc_cropcap                 = fimc_cap_cropcap,
1082
1083         .vidioc_enum_input              = fimc_cap_enum_input,
1084         .vidioc_s_input                 = fimc_cap_s_input,
1085         .vidioc_g_input                 = fimc_cap_g_input,
1086
1087         .vidioc_g_parm                  = fimc_cap_g_parm,
1088         .vidioc_s_parm                  = fimc_cap_s_parm,
1089 };
1090
1091 /* fimc->lock must be already initialized */
1092 int fimc_register_capture_device(struct fimc_dev *fimc)
1093 {
1094         struct v4l2_device *v4l2_dev = &fimc->vid_cap.v4l2_dev;
1095         struct video_device *vfd;
1096         struct fimc_vid_cap *vid_cap;
1097         struct fimc_ctx *ctx;
1098         struct v4l2_format f;
1099         struct fimc_frame *fr;
1100         struct vb2_queue *q;
1101         int ret;
1102
1103         ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1104         if (!ctx)
1105                 return -ENOMEM;
1106
1107         ctx->fimc_dev    = fimc;
1108         ctx->in_path     = FIMC_CAMERA;
1109         ctx->out_path    = FIMC_DMA;
1110         ctx->state       = FIMC_CTX_CAP;
1111
1112         /* Default format of the output frames */
1113         f.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32;
1114         fr = &ctx->d_frame;
1115         fr->fmt = find_format(&f, FMT_FLAGS_M2M);
1116         fr->width = fr->f_width = fr->o_width = 640;
1117         fr->height = fr->f_height = fr->o_height = 480;
1118
1119         if (!v4l2_dev->name[0])
1120                 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
1121                          "%s.capture", dev_name(&fimc->pdev->dev));
1122
1123         ret = v4l2_device_register(NULL, v4l2_dev);
1124         if (ret)
1125                 goto err_info;
1126
1127         vfd = video_device_alloc();
1128         if (!vfd) {
1129                 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
1130                 goto err_v4l2_reg;
1131         }
1132
1133         snprintf(vfd->name, sizeof(vfd->name), "%s:cap",
1134                  dev_name(&fimc->pdev->dev));
1135
1136         vfd->fops       = &fimc_capture_fops;
1137         vfd->ioctl_ops  = &fimc_capture_ioctl_ops;
1138         vfd->minor      = -1;
1139         vfd->release    = video_device_release;
1140 #ifdef FOR_DIFF_VER
1141         vfd->lock       = &fimc->lock;
1142 #endif
1143         video_set_drvdata(vfd, fimc);
1144
1145         vid_cap = &fimc->vid_cap;
1146         vid_cap->vfd = vfd;
1147         vid_cap->active_buf_cnt = 0;
1148         vid_cap->reqbufs_count  = 0;
1149         vid_cap->refcnt = 0;
1150         vid_cap->sd = NULL;
1151         vid_cap->fb_sd = NULL;
1152         vid_cap->mipi_sd = NULL;
1153
1154         /* Default color format for 4BA image sensor */
1155         /* vid_cap->fmt.code = V4L2_MBUS_FMT_UYVY8_2X8; */
1156
1157         /* Default color format for 4EA image sensor */
1158         vid_cap->fmt.code = V4L2_MBUS_FMT_VYUY8_2X8;
1159
1160         INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1161         INIT_LIST_HEAD(&vid_cap->active_buf_q);
1162         spin_lock_init(&ctx->slock);
1163         vid_cap->ctx = ctx;
1164
1165         q = &fimc->vid_cap.vbq;
1166         memset(q, 0, sizeof(*q));
1167         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1168         q->io_modes = VB2_MMAP | VB2_USERPTR;
1169         q->drv_priv = fimc->vid_cap.ctx;
1170         q->ops = &fimc_capture_qops;
1171 #ifdef CONFIG_VIDEOBUF2_SDVMM
1172         q->mem_ops = &vb2_sdvmm_memops;
1173 #else
1174         q->mem_ops = &vb2_cma_memops;
1175 #endif
1176         q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1177
1178         vb2_queue_init(q);
1179
1180         ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1181         if (ret) {
1182                 v4l2_err(v4l2_dev, "Failed to register video device\n");
1183                 goto err_vd_reg;
1184         }
1185
1186         v4l2_info(v4l2_dev,
1187                   "FIMC capture driver registered as /dev/video%d\n",
1188                   vfd->num);
1189
1190         return 0;
1191
1192 err_vd_reg:
1193         video_device_release(vfd);
1194 err_v4l2_reg:
1195         v4l2_device_unregister(v4l2_dev);
1196 err_info:
1197         dev_err(&fimc->pdev->dev, "failed to install\n");
1198         return ret;
1199 }
1200
1201 void fimc_unregister_capture_device(struct fimc_dev *fimc)
1202 {
1203         struct fimc_vid_cap *capture = &fimc->vid_cap;
1204
1205         if (capture->vfd)
1206                 video_unregister_device(capture->vfd);
1207
1208         kfree(capture->ctx);
1209 }