055d843f99df92b3c5639bc32d556f0cbdd6d2df
[pandora-kernel.git] / drivers / media / video / marvell-ccic / mcam-core.c
1 /*
2  * The Marvell camera core.  This device appears in a number of settings,
3  * so it needs platform-specific support outside of the core.
4  *
5  * Copyright 2011 Jonathan Corbet corbet@lwn.net
6  */
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/fs.h>
10 #include <linux/mm.h>
11 #include <linux/i2c.h>
12 #include <linux/interrupt.h>
13 #include <linux/spinlock.h>
14 #include <linux/videodev2.h>
15 #include <linux/slab.h>
16 #include <media/v4l2-device.h>
17 #include <media/v4l2-ioctl.h>
18 #include <media/v4l2-chip-ident.h>
19 #include <media/ov7670.h>
20 #include <media/videobuf2-vmalloc.h>
21 #include <linux/device.h>
22 #include <linux/wait.h>
23 #include <linux/list.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/delay.h>
26 #include <linux/jiffies.h>
27 #include <linux/vmalloc.h>
28 #include <linux/uaccess.h>
29 #include <linux/io.h>
30
31 #include "mcam-core.h"
32
33
34 /*
35  * Internal DMA buffer management.  Since the controller cannot do S/G I/O,
36  * we must have physically contiguous buffers to bring frames into.
37  * These parameters control how many buffers we use, whether we
38  * allocate them at load time (better chance of success, but nails down
39  * memory) or when somebody tries to use the camera (riskier), and,
40  * for load-time allocation, how big they should be.
41  *
42  * The controller can cycle through three buffers.  We could use
43  * more by flipping pointers around, but it probably makes little
44  * sense.
45  */
46
47 static int alloc_bufs_at_read;
48 module_param(alloc_bufs_at_read, bool, 0444);
49 MODULE_PARM_DESC(alloc_bufs_at_read,
50                 "Non-zero value causes DMA buffers to be allocated when the "
51                 "video capture device is read, rather than at module load "
52                 "time.  This saves memory, but decreases the chances of "
53                 "successfully getting those buffers.");
54
55 static int n_dma_bufs = 3;
56 module_param(n_dma_bufs, uint, 0644);
57 MODULE_PARM_DESC(n_dma_bufs,
58                 "The number of DMA buffers to allocate.  Can be either two "
59                 "(saves memory, makes timing tighter) or three.");
60
61 static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2;  /* Worst case */
62 module_param(dma_buf_size, uint, 0444);
63 MODULE_PARM_DESC(dma_buf_size,
64                 "The size of the allocated DMA buffers.  If actual operating "
65                 "parameters require larger buffers, an attempt to reallocate "
66                 "will be made.");
67
68 static int min_buffers = 1;
69 module_param(min_buffers, uint, 0644);
70 MODULE_PARM_DESC(min_buffers,
71                 "The minimum number of streaming I/O buffers we are willing "
72                 "to work with.");
73
74 static int max_buffers = 10;
75 module_param(max_buffers, uint, 0644);
76 MODULE_PARM_DESC(max_buffers,
77                 "The maximum number of streaming I/O buffers an application "
78                 "will be allowed to allocate.  These buffers are big and live "
79                 "in vmalloc space.");
80
81 static int flip;
82 module_param(flip, bool, 0444);
83 MODULE_PARM_DESC(flip,
84                 "If set, the sensor will be instructed to flip the image "
85                 "vertically.");
86
87 /*
88  * Status flags.  Always manipulated with bit operations.
89  */
90 #define CF_BUF0_VALID    0      /* Buffers valid - first three */
91 #define CF_BUF1_VALID    1
92 #define CF_BUF2_VALID    2
93 #define CF_DMA_ACTIVE    3      /* A frame is incoming */
94 #define CF_CONFIG_NEEDED 4      /* Must configure hardware */
95
96 #define sensor_call(cam, o, f, args...) \
97         v4l2_subdev_call(cam->sensor, o, f, ##args)
98
99 static struct mcam_format_struct {
100         __u8 *desc;
101         __u32 pixelformat;
102         int bpp;   /* Bytes per pixel */
103         enum v4l2_mbus_pixelcode mbus_code;
104 } mcam_formats[] = {
105         {
106                 .desc           = "YUYV 4:2:2",
107                 .pixelformat    = V4L2_PIX_FMT_YUYV,
108                 .mbus_code      = V4L2_MBUS_FMT_YUYV8_2X8,
109                 .bpp            = 2,
110         },
111         {
112                 .desc           = "RGB 444",
113                 .pixelformat    = V4L2_PIX_FMT_RGB444,
114                 .mbus_code      = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE,
115                 .bpp            = 2,
116         },
117         {
118                 .desc           = "RGB 565",
119                 .pixelformat    = V4L2_PIX_FMT_RGB565,
120                 .mbus_code      = V4L2_MBUS_FMT_RGB565_2X8_LE,
121                 .bpp            = 2,
122         },
123         {
124                 .desc           = "Raw RGB Bayer",
125                 .pixelformat    = V4L2_PIX_FMT_SBGGR8,
126                 .mbus_code      = V4L2_MBUS_FMT_SBGGR8_1X8,
127                 .bpp            = 1
128         },
129 };
130 #define N_MCAM_FMTS ARRAY_SIZE(mcam_formats)
131
132 static struct mcam_format_struct *mcam_find_format(u32 pixelformat)
133 {
134         unsigned i;
135
136         for (i = 0; i < N_MCAM_FMTS; i++)
137                 if (mcam_formats[i].pixelformat == pixelformat)
138                         return mcam_formats + i;
139         /* Not found? Then return the first format. */
140         return mcam_formats;
141 }
142
143 /*
144  * Start over with DMA buffers - dev_lock needed.
145  */
146 static void mcam_reset_buffers(struct mcam_camera *cam)
147 {
148         int i;
149
150         cam->next_buf = -1;
151         for (i = 0; i < cam->nbufs; i++)
152                 clear_bit(i, &cam->flags);
153 }
154
155 static inline int mcam_needs_config(struct mcam_camera *cam)
156 {
157         return test_bit(CF_CONFIG_NEEDED, &cam->flags);
158 }
159
160 static void mcam_set_config_needed(struct mcam_camera *cam, int needed)
161 {
162         if (needed)
163                 set_bit(CF_CONFIG_NEEDED, &cam->flags);
164         else
165                 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
166 }
167
168 /*
169  * Our buffer type for working with videobuf2.  Note that the vb2
170  * developers have decreed that struct vb2_buffer must be at the
171  * beginning of this structure.
172  */
173 struct mcam_vb_buffer {
174         struct vb2_buffer vb_buf;
175         struct list_head queue;
176 };
177
178 static inline struct mcam_vb_buffer *vb_to_mvb(struct vb2_buffer *vb)
179 {
180         return container_of(vb, struct mcam_vb_buffer, vb_buf);
181 }
182
183
184 /*
185  * Debugging and related.
186  */
187 #define cam_err(cam, fmt, arg...) \
188         dev_err((cam)->dev, fmt, ##arg);
189 #define cam_warn(cam, fmt, arg...) \
190         dev_warn((cam)->dev, fmt, ##arg);
191 #define cam_dbg(cam, fmt, arg...) \
192         dev_dbg((cam)->dev, fmt, ##arg);
193
194
195
196 /* ------------------------------------------------------------------- */
197 /*
198  * Deal with the controller.
199  */
200
201 /*
202  * Do everything we think we need to have the interface operating
203  * according to the desired format.
204  */
205 static void mcam_ctlr_dma(struct mcam_camera *cam)
206 {
207         /*
208          * Store the first two Y buffers (we aren't supporting
209          * planar formats for now, so no UV bufs).  Then either
210          * set the third if it exists, or tell the controller
211          * to just use two.
212          */
213         mcam_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
214         mcam_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
215         if (cam->nbufs > 2) {
216                 mcam_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
217                 mcam_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
218         } else
219                 mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
220         if (cam->chip_id == V4L2_IDENT_CAFE)
221                 mcam_reg_write(cam, REG_UBAR, 0); /* 32 bits only */
222 }
223
224 static void mcam_ctlr_image(struct mcam_camera *cam)
225 {
226         int imgsz;
227         struct v4l2_pix_format *fmt = &cam->pix_format;
228
229         imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
230                 (fmt->bytesperline & IMGSZ_H_MASK);
231         mcam_reg_write(cam, REG_IMGSIZE, imgsz);
232         mcam_reg_write(cam, REG_IMGOFFSET, 0);
233         /* YPITCH just drops the last two bits */
234         mcam_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
235                         IMGP_YP_MASK);
236         /*
237          * Tell the controller about the image format we are using.
238          */
239         switch (cam->pix_format.pixelformat) {
240         case V4L2_PIX_FMT_YUYV:
241             mcam_reg_write_mask(cam, REG_CTRL0,
242                             C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
243                             C0_DF_MASK);
244             break;
245
246         case V4L2_PIX_FMT_RGB444:
247             mcam_reg_write_mask(cam, REG_CTRL0,
248                             C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
249                             C0_DF_MASK);
250                 /* Alpha value? */
251             break;
252
253         case V4L2_PIX_FMT_RGB565:
254             mcam_reg_write_mask(cam, REG_CTRL0,
255                             C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
256                             C0_DF_MASK);
257             break;
258
259         default:
260             cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
261             break;
262         }
263         /*
264          * Make sure it knows we want to use hsync/vsync.
265          */
266         mcam_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
267                         C0_SIFM_MASK);
268 }
269
270
271 /*
272  * Configure the controller for operation; caller holds the
273  * device mutex.
274  */
275 static int mcam_ctlr_configure(struct mcam_camera *cam)
276 {
277         unsigned long flags;
278
279         spin_lock_irqsave(&cam->dev_lock, flags);
280         mcam_ctlr_dma(cam);
281         mcam_ctlr_image(cam);
282         mcam_set_config_needed(cam, 0);
283         spin_unlock_irqrestore(&cam->dev_lock, flags);
284         return 0;
285 }
286
287 static void mcam_ctlr_irq_enable(struct mcam_camera *cam)
288 {
289         /*
290          * Clear any pending interrupts, since we do not
291          * expect to have I/O active prior to enabling.
292          */
293         mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
294         mcam_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
295 }
296
297 static void mcam_ctlr_irq_disable(struct mcam_camera *cam)
298 {
299         mcam_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
300 }
301
302 /*
303  * Make the controller start grabbing images.  Everything must
304  * be set up before doing this.
305  */
306 static void mcam_ctlr_start(struct mcam_camera *cam)
307 {
308         /* set_bit performs a read, so no other barrier should be
309            needed here */
310         mcam_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
311 }
312
313 static void mcam_ctlr_stop(struct mcam_camera *cam)
314 {
315         mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
316 }
317
318 static void mcam_ctlr_init(struct mcam_camera *cam)
319 {
320         unsigned long flags;
321
322         spin_lock_irqsave(&cam->dev_lock, flags);
323         /*
324          * Make sure it's not powered down.
325          */
326         mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
327         /*
328          * Turn off the enable bit.  It sure should be off anyway,
329          * but it's good to be sure.
330          */
331         mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
332         /*
333          * Clock the sensor appropriately.  Controller clock should
334          * be 48MHz, sensor "typical" value is half that.
335          */
336         mcam_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
337         spin_unlock_irqrestore(&cam->dev_lock, flags);
338 }
339
340
341 /*
342  * Stop the controller, and don't return until we're really sure that no
343  * further DMA is going on.
344  */
345 static void mcam_ctlr_stop_dma(struct mcam_camera *cam)
346 {
347         unsigned long flags;
348
349         /*
350          * Theory: stop the camera controller (whether it is operating
351          * or not).  Delay briefly just in case we race with the SOF
352          * interrupt, then wait until no DMA is active.
353          */
354         spin_lock_irqsave(&cam->dev_lock, flags);
355         mcam_ctlr_stop(cam);
356         spin_unlock_irqrestore(&cam->dev_lock, flags);
357         msleep(10);
358         if (test_bit(CF_DMA_ACTIVE, &cam->flags))
359                 cam_err(cam, "Timeout waiting for DMA to end\n");
360                 /* This would be bad news - what now? */
361         spin_lock_irqsave(&cam->dev_lock, flags);
362         cam->state = S_IDLE;
363         mcam_ctlr_irq_disable(cam);
364         spin_unlock_irqrestore(&cam->dev_lock, flags);
365 }
366
367 /*
368  * Power up and down.
369  */
370 static void mcam_ctlr_power_up(struct mcam_camera *cam)
371 {
372         unsigned long flags;
373
374         spin_lock_irqsave(&cam->dev_lock, flags);
375         cam->plat_power_up(cam);
376         mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
377         spin_unlock_irqrestore(&cam->dev_lock, flags);
378         msleep(5); /* Just to be sure */
379 }
380
381 static void mcam_ctlr_power_down(struct mcam_camera *cam)
382 {
383         unsigned long flags;
384
385         spin_lock_irqsave(&cam->dev_lock, flags);
386         /*
387          * School of hard knocks department: be sure we do any register
388          * twiddling on the controller *before* calling the platform
389          * power down routine.
390          */
391         mcam_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
392         cam->plat_power_down(cam);
393         spin_unlock_irqrestore(&cam->dev_lock, flags);
394 }
395
396 /* -------------------------------------------------------------------- */
397 /*
398  * Communications with the sensor.
399  */
400
401 static int __mcam_cam_reset(struct mcam_camera *cam)
402 {
403         return sensor_call(cam, core, reset, 0);
404 }
405
406 /*
407  * We have found the sensor on the i2c.  Let's try to have a
408  * conversation.
409  */
410 static int mcam_cam_init(struct mcam_camera *cam)
411 {
412         struct v4l2_dbg_chip_ident chip;
413         int ret;
414
415         mutex_lock(&cam->s_mutex);
416         if (cam->state != S_NOTREADY)
417                 cam_warn(cam, "Cam init with device in funky state %d",
418                                 cam->state);
419         ret = __mcam_cam_reset(cam);
420         if (ret)
421                 goto out;
422         chip.ident = V4L2_IDENT_NONE;
423         chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
424         chip.match.addr = cam->sensor_addr;
425         ret = sensor_call(cam, core, g_chip_ident, &chip);
426         if (ret)
427                 goto out;
428         cam->sensor_type = chip.ident;
429         if (cam->sensor_type != V4L2_IDENT_OV7670) {
430                 cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
431                 ret = -EINVAL;
432                 goto out;
433         }
434 /* Get/set parameters? */
435         ret = 0;
436         cam->state = S_IDLE;
437 out:
438         mcam_ctlr_power_down(cam);
439         mutex_unlock(&cam->s_mutex);
440         return ret;
441 }
442
443 /*
444  * Configure the sensor to match the parameters we have.  Caller should
445  * hold s_mutex
446  */
447 static int mcam_cam_set_flip(struct mcam_camera *cam)
448 {
449         struct v4l2_control ctrl;
450
451         memset(&ctrl, 0, sizeof(ctrl));
452         ctrl.id = V4L2_CID_VFLIP;
453         ctrl.value = flip;
454         return sensor_call(cam, core, s_ctrl, &ctrl);
455 }
456
457
458 static int mcam_cam_configure(struct mcam_camera *cam)
459 {
460         struct v4l2_mbus_framefmt mbus_fmt;
461         int ret;
462
463         v4l2_fill_mbus_format(&mbus_fmt, &cam->pix_format, cam->mbus_code);
464         ret = sensor_call(cam, core, init, 0);
465         if (ret == 0)
466                 ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
467         /*
468          * OV7670 does weird things if flip is set *before* format...
469          */
470         ret += mcam_cam_set_flip(cam);
471         return ret;
472 }
473
474 /* -------------------------------------------------------------------- */
475 /*
476  * DMA buffer management.  These functions need s_mutex held.
477  */
478
479 /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
480  * does a get_free_pages() call, and we waste a good chunk of an orderN
481  * allocation.  Should try to allocate the whole set in one chunk.
482  */
483 static int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
484 {
485         int i;
486
487         mcam_set_config_needed(cam, 1);
488         if (loadtime)
489                 cam->dma_buf_size = dma_buf_size;
490         else
491                 cam->dma_buf_size = cam->pix_format.sizeimage;
492         if (n_dma_bufs > 3)
493                 n_dma_bufs = 3;
494
495         cam->nbufs = 0;
496         for (i = 0; i < n_dma_bufs; i++) {
497                 cam->dma_bufs[i] = dma_alloc_coherent(cam->dev,
498                                 cam->dma_buf_size, cam->dma_handles + i,
499                                 GFP_KERNEL);
500                 if (cam->dma_bufs[i] == NULL) {
501                         cam_warn(cam, "Failed to allocate DMA buffer\n");
502                         break;
503                 }
504                 /* For debug, remove eventually */
505                 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
506                 (cam->nbufs)++;
507         }
508
509         switch (cam->nbufs) {
510         case 1:
511                 dma_free_coherent(cam->dev, cam->dma_buf_size,
512                                 cam->dma_bufs[0], cam->dma_handles[0]);
513                 cam->nbufs = 0;
514         case 0:
515                 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
516                 return -ENOMEM;
517
518         case 2:
519                 if (n_dma_bufs > 2)
520                         cam_warn(cam, "Will limp along with only 2 buffers\n");
521                 break;
522         }
523         return 0;
524 }
525
526 static void mcam_free_dma_bufs(struct mcam_camera *cam)
527 {
528         int i;
529
530         for (i = 0; i < cam->nbufs; i++) {
531                 dma_free_coherent(cam->dev, cam->dma_buf_size,
532                                 cam->dma_bufs[i], cam->dma_handles[i]);
533                 cam->dma_bufs[i] = NULL;
534         }
535         cam->nbufs = 0;
536 }
537
538
539
540 /* ----------------------------------------------------------------------- */
541 /*
542  * Here starts the V4L2 interface code.
543  */
544
545
546 /*
547  * Get everything ready, and start grabbing frames.
548  */
549 static int mcam_read_setup(struct mcam_camera *cam, enum mcam_state state)
550 {
551         int ret;
552         unsigned long flags;
553
554         /*
555          * Configuration.  If we still don't have DMA buffers,
556          * make one last, desperate attempt.
557          */
558         if (cam->nbufs == 0)
559                 if (mcam_alloc_dma_bufs(cam, 0))
560                         return -ENOMEM;
561
562         if (mcam_needs_config(cam)) {
563                 mcam_cam_configure(cam);
564                 ret = mcam_ctlr_configure(cam);
565                 if (ret)
566                         return ret;
567         }
568
569         /*
570          * Turn it loose.
571          */
572         spin_lock_irqsave(&cam->dev_lock, flags);
573         mcam_reset_buffers(cam);
574         mcam_ctlr_irq_enable(cam);
575         cam->state = state;
576         mcam_ctlr_start(cam);
577         spin_unlock_irqrestore(&cam->dev_lock, flags);
578         return 0;
579 }
580
581 /* ----------------------------------------------------------------------- */
582 /*
583  * Videobuf2 interface code.
584  */
585
586 static int mcam_vb_queue_setup(struct vb2_queue *vq, unsigned int *nbufs,
587                 unsigned int *num_planes, unsigned long sizes[],
588                 void *alloc_ctxs[])
589 {
590         struct mcam_camera *cam = vb2_get_drv_priv(vq);
591
592         sizes[0] = cam->pix_format.sizeimage;
593         *num_planes = 1; /* Someday we have to support planar formats... */
594         if (*nbufs < 2 || *nbufs > 32)
595                 *nbufs = 6;  /* semi-arbitrary numbers */
596         return 0;
597 }
598
599 static int mcam_vb_buf_init(struct vb2_buffer *vb)
600 {
601         struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
602
603         INIT_LIST_HEAD(&mvb->queue);
604         return 0;
605 }
606
607 static void mcam_vb_buf_queue(struct vb2_buffer *vb)
608 {
609         struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
610         struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
611         unsigned long flags;
612
613         spin_lock_irqsave(&cam->dev_lock, flags);
614         list_add(&cam->buffers, &mvb->queue);
615         spin_unlock_irqrestore(&cam->dev_lock, flags);
616 }
617
618 /*
619  * vb2 uses these to release the mutex when waiting in dqbuf.  I'm
620  * not actually sure we need to do this (I'm not sure that vb2_dqbuf() needs
621  * to be called with the mutex held), but better safe than sorry.
622  */
623 static void mcam_vb_wait_prepare(struct vb2_queue *vq)
624 {
625         struct mcam_camera *cam = vb2_get_drv_priv(vq);
626
627         mutex_unlock(&cam->s_mutex);
628 }
629
630 static void mcam_vb_wait_finish(struct vb2_queue *vq)
631 {
632         struct mcam_camera *cam = vb2_get_drv_priv(vq);
633
634         mutex_lock(&cam->s_mutex);
635 }
636
637 /*
638  * These need to be called with the mutex held from vb2
639  */
640 static int mcam_vb_start_streaming(struct vb2_queue *vq)
641 {
642         struct mcam_camera *cam = vb2_get_drv_priv(vq);
643         int ret = -EINVAL;
644
645         if (cam->state == S_IDLE) {
646                 cam->sequence = 0;
647                 ret = mcam_read_setup(cam, S_STREAMING);
648         }
649         return ret;
650 }
651
652 static int mcam_vb_stop_streaming(struct vb2_queue *vq)
653 {
654         struct mcam_camera *cam = vb2_get_drv_priv(vq);
655         unsigned long flags;
656
657         if (cam->state != S_STREAMING)
658                 return -EINVAL;
659         mcam_ctlr_stop_dma(cam);
660         /*
661          * VB2 reclaims the buffers, so we need to forget
662          * about them.
663          */
664         spin_lock_irqsave(&cam->dev_lock, flags);
665         INIT_LIST_HEAD(&cam->buffers);
666         spin_unlock_irqrestore(&cam->dev_lock, flags);
667         return 0;
668 }
669
670
671 static const struct vb2_ops mcam_vb2_ops = {
672         .queue_setup            = mcam_vb_queue_setup,
673         .buf_init               = mcam_vb_buf_init,
674         .buf_queue              = mcam_vb_buf_queue,
675         .start_streaming        = mcam_vb_start_streaming,
676         .stop_streaming         = mcam_vb_stop_streaming,
677         .wait_prepare           = mcam_vb_wait_prepare,
678         .wait_finish            = mcam_vb_wait_finish,
679 };
680
681 static int mcam_setup_vb2(struct mcam_camera *cam)
682 {
683         struct vb2_queue *vq = &cam->vb_queue;
684
685         memset(vq, 0, sizeof(*vq));
686         vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
687         vq->io_modes = VB2_MMAP;  /* Add userptr */
688         vq->drv_priv = cam;
689         vq->ops = &mcam_vb2_ops;
690         vq->mem_ops = &vb2_vmalloc_memops;
691         vq->buf_struct_size = sizeof(struct mcam_vb_buffer);
692
693         return vb2_queue_init(vq);
694 }
695
696 static void mcam_cleanup_vb2(struct mcam_camera *cam)
697 {
698         vb2_queue_release(&cam->vb_queue);
699 }
700
701 static ssize_t mcam_v4l_read(struct file *filp,
702                 char __user *buffer, size_t len, loff_t *pos)
703 {
704         struct mcam_camera *cam = filp->private_data;
705         int ret;
706
707         mutex_lock(&cam->s_mutex);
708         ret = vb2_read(&cam->vb_queue, buffer, len, pos,
709                         filp->f_flags & O_NONBLOCK);
710         mutex_unlock(&cam->s_mutex);
711         return ret;
712 }
713
714
715
716 /*
717  * Streaming I/O support.
718  */
719
720 static int mcam_vidioc_streamon(struct file *filp, void *priv,
721                 enum v4l2_buf_type type)
722 {
723         struct mcam_camera *cam = filp->private_data;
724         int ret;
725
726         mutex_lock(&cam->s_mutex);
727         ret = vb2_streamon(&cam->vb_queue, type);
728         mutex_unlock(&cam->s_mutex);
729         return ret;
730 }
731
732
733 static int mcam_vidioc_streamoff(struct file *filp, void *priv,
734                 enum v4l2_buf_type type)
735 {
736         struct mcam_camera *cam = filp->private_data;
737         int ret;
738
739         mutex_lock(&cam->s_mutex);
740         ret = vb2_streamoff(&cam->vb_queue, type);
741         mutex_unlock(&cam->s_mutex);
742         return ret;
743 }
744
745
746 static int mcam_vidioc_reqbufs(struct file *filp, void *priv,
747                 struct v4l2_requestbuffers *req)
748 {
749         struct mcam_camera *cam = filp->private_data;
750         int ret;
751
752         mutex_lock(&cam->s_mutex);
753         ret = vb2_reqbufs(&cam->vb_queue, req);
754         mutex_unlock(&cam->s_mutex);
755         return ret;
756 }
757
758
759 static int mcam_vidioc_querybuf(struct file *filp, void *priv,
760                 struct v4l2_buffer *buf)
761 {
762         struct mcam_camera *cam = filp->private_data;
763         int ret;
764
765         mutex_lock(&cam->s_mutex);
766         ret = vb2_querybuf(&cam->vb_queue, buf);
767         mutex_unlock(&cam->s_mutex);
768         return ret;
769 }
770
771 static int mcam_vidioc_qbuf(struct file *filp, void *priv,
772                 struct v4l2_buffer *buf)
773 {
774         struct mcam_camera *cam = filp->private_data;
775         int ret;
776
777         mutex_lock(&cam->s_mutex);
778         ret = vb2_qbuf(&cam->vb_queue, buf);
779         mutex_unlock(&cam->s_mutex);
780         return ret;
781 }
782
783 static int mcam_vidioc_dqbuf(struct file *filp, void *priv,
784                 struct v4l2_buffer *buf)
785 {
786         struct mcam_camera *cam = filp->private_data;
787         int ret;
788
789         mutex_lock(&cam->s_mutex);
790         ret = vb2_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
791         mutex_unlock(&cam->s_mutex);
792         return ret;
793 }
794
795
796 static int mcam_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
797 {
798         struct mcam_camera *cam = filp->private_data;
799         int ret;
800
801         mutex_lock(&cam->s_mutex);
802         ret = vb2_mmap(&cam->vb_queue, vma);
803         mutex_unlock(&cam->s_mutex);
804         return ret;
805 }
806
807
808
809 static int mcam_v4l_open(struct file *filp)
810 {
811         struct mcam_camera *cam = video_drvdata(filp);
812         int ret = 0;
813
814         filp->private_data = cam;
815
816         mutex_lock(&cam->s_mutex);
817         if (cam->users == 0) {
818                 ret = mcam_setup_vb2(cam);
819                 if (ret)
820                         goto out;
821                 mcam_ctlr_power_up(cam);
822                 __mcam_cam_reset(cam);
823                 mcam_set_config_needed(cam, 1);
824         }
825         (cam->users)++;
826 out:
827         mutex_unlock(&cam->s_mutex);
828         return ret;
829 }
830
831
832 static int mcam_v4l_release(struct file *filp)
833 {
834         struct mcam_camera *cam = filp->private_data;
835
836         mutex_lock(&cam->s_mutex);
837         (cam->users)--;
838         if (filp == cam->owner) {
839                 mcam_ctlr_stop_dma(cam);
840                 cam->owner = NULL;
841         }
842         if (cam->users == 0) {
843                 mcam_cleanup_vb2(cam);
844                 mcam_ctlr_power_down(cam);
845                 if (alloc_bufs_at_read)
846                         mcam_free_dma_bufs(cam);
847         }
848         mutex_unlock(&cam->s_mutex);
849         return 0;
850 }
851
852
853
854 static unsigned int mcam_v4l_poll(struct file *filp,
855                 struct poll_table_struct *pt)
856 {
857         struct mcam_camera *cam = filp->private_data;
858         int ret;
859
860         mutex_lock(&cam->s_mutex);
861         ret = vb2_poll(&cam->vb_queue, filp, pt);
862         mutex_unlock(&cam->s_mutex);
863         return ret;
864 }
865
866
867
868 static int mcam_vidioc_queryctrl(struct file *filp, void *priv,
869                 struct v4l2_queryctrl *qc)
870 {
871         struct mcam_camera *cam = priv;
872         int ret;
873
874         mutex_lock(&cam->s_mutex);
875         ret = sensor_call(cam, core, queryctrl, qc);
876         mutex_unlock(&cam->s_mutex);
877         return ret;
878 }
879
880
881 static int mcam_vidioc_g_ctrl(struct file *filp, void *priv,
882                 struct v4l2_control *ctrl)
883 {
884         struct mcam_camera *cam = priv;
885         int ret;
886
887         mutex_lock(&cam->s_mutex);
888         ret = sensor_call(cam, core, g_ctrl, ctrl);
889         mutex_unlock(&cam->s_mutex);
890         return ret;
891 }
892
893
894 static int mcam_vidioc_s_ctrl(struct file *filp, void *priv,
895                 struct v4l2_control *ctrl)
896 {
897         struct mcam_camera *cam = priv;
898         int ret;
899
900         mutex_lock(&cam->s_mutex);
901         ret = sensor_call(cam, core, s_ctrl, ctrl);
902         mutex_unlock(&cam->s_mutex);
903         return ret;
904 }
905
906
907 static int mcam_vidioc_querycap(struct file *file, void *priv,
908                 struct v4l2_capability *cap)
909 {
910         strcpy(cap->driver, "marvell_ccic");
911         strcpy(cap->card, "marvell_ccic");
912         cap->version = 1;
913         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
914                 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
915         return 0;
916 }
917
918
919 /*
920  * The default format we use until somebody says otherwise.
921  */
922 static const struct v4l2_pix_format mcam_def_pix_format = {
923         .width          = VGA_WIDTH,
924         .height         = VGA_HEIGHT,
925         .pixelformat    = V4L2_PIX_FMT_YUYV,
926         .field          = V4L2_FIELD_NONE,
927         .bytesperline   = VGA_WIDTH*2,
928         .sizeimage      = VGA_WIDTH*VGA_HEIGHT*2,
929 };
930
931 static const enum v4l2_mbus_pixelcode mcam_def_mbus_code =
932                                         V4L2_MBUS_FMT_YUYV8_2X8;
933
934 static int mcam_vidioc_enum_fmt_vid_cap(struct file *filp,
935                 void *priv, struct v4l2_fmtdesc *fmt)
936 {
937         if (fmt->index >= N_MCAM_FMTS)
938                 return -EINVAL;
939         strlcpy(fmt->description, mcam_formats[fmt->index].desc,
940                         sizeof(fmt->description));
941         fmt->pixelformat = mcam_formats[fmt->index].pixelformat;
942         return 0;
943 }
944
945 static int mcam_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
946                 struct v4l2_format *fmt)
947 {
948         struct mcam_camera *cam = priv;
949         struct mcam_format_struct *f;
950         struct v4l2_pix_format *pix = &fmt->fmt.pix;
951         struct v4l2_mbus_framefmt mbus_fmt;
952         int ret;
953
954         f = mcam_find_format(pix->pixelformat);
955         pix->pixelformat = f->pixelformat;
956         v4l2_fill_mbus_format(&mbus_fmt, pix, f->mbus_code);
957         mutex_lock(&cam->s_mutex);
958         ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
959         mutex_unlock(&cam->s_mutex);
960         v4l2_fill_pix_format(pix, &mbus_fmt);
961         pix->bytesperline = pix->width * f->bpp;
962         pix->sizeimage = pix->height * pix->bytesperline;
963         return ret;
964 }
965
966 static int mcam_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
967                 struct v4l2_format *fmt)
968 {
969         struct mcam_camera *cam = priv;
970         struct mcam_format_struct *f;
971         int ret;
972
973         /*
974          * Can't do anything if the device is not idle
975          * Also can't if there are streaming buffers in place.
976          */
977         if (cam->state != S_IDLE || cam->vb_queue.num_buffers > 0)
978                 return -EBUSY;
979
980         f = mcam_find_format(fmt->fmt.pix.pixelformat);
981
982         /*
983          * See if the formatting works in principle.
984          */
985         ret = mcam_vidioc_try_fmt_vid_cap(filp, priv, fmt);
986         if (ret)
987                 return ret;
988         /*
989          * Now we start to change things for real, so let's do it
990          * under lock.
991          */
992         mutex_lock(&cam->s_mutex);
993         cam->pix_format = fmt->fmt.pix;
994         cam->mbus_code = f->mbus_code;
995
996         /*
997          * Make sure we have appropriate DMA buffers.
998          */
999         ret = -ENOMEM;
1000         if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1001                 mcam_free_dma_bufs(cam);
1002         if (cam->nbufs == 0) {
1003                 if (mcam_alloc_dma_bufs(cam, 0))
1004                         goto out;
1005         }
1006         /*
1007          * It looks like this might work, so let's program the sensor.
1008          */
1009         ret = mcam_cam_configure(cam);
1010         if (!ret)
1011                 ret = mcam_ctlr_configure(cam);
1012 out:
1013         mutex_unlock(&cam->s_mutex);
1014         return ret;
1015 }
1016
1017 /*
1018  * Return our stored notion of how the camera is/should be configured.
1019  * The V4l2 spec wants us to be smarter, and actually get this from
1020  * the camera (and not mess with it at open time).  Someday.
1021  */
1022 static int mcam_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
1023                 struct v4l2_format *f)
1024 {
1025         struct mcam_camera *cam = priv;
1026
1027         f->fmt.pix = cam->pix_format;
1028         return 0;
1029 }
1030
1031 /*
1032  * We only have one input - the sensor - so minimize the nonsense here.
1033  */
1034 static int mcam_vidioc_enum_input(struct file *filp, void *priv,
1035                 struct v4l2_input *input)
1036 {
1037         if (input->index != 0)
1038                 return -EINVAL;
1039
1040         input->type = V4L2_INPUT_TYPE_CAMERA;
1041         input->std = V4L2_STD_ALL; /* Not sure what should go here */
1042         strcpy(input->name, "Camera");
1043         return 0;
1044 }
1045
1046 static int mcam_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1047 {
1048         *i = 0;
1049         return 0;
1050 }
1051
1052 static int mcam_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1053 {
1054         if (i != 0)
1055                 return -EINVAL;
1056         return 0;
1057 }
1058
1059 /* from vivi.c */
1060 static int mcam_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1061 {
1062         return 0;
1063 }
1064
1065 /*
1066  * G/S_PARM.  Most of this is done by the sensor, but we are
1067  * the level which controls the number of read buffers.
1068  */
1069 static int mcam_vidioc_g_parm(struct file *filp, void *priv,
1070                 struct v4l2_streamparm *parms)
1071 {
1072         struct mcam_camera *cam = priv;
1073         int ret;
1074
1075         mutex_lock(&cam->s_mutex);
1076         ret = sensor_call(cam, video, g_parm, parms);
1077         mutex_unlock(&cam->s_mutex);
1078         parms->parm.capture.readbuffers = n_dma_bufs;
1079         return ret;
1080 }
1081
1082 static int mcam_vidioc_s_parm(struct file *filp, void *priv,
1083                 struct v4l2_streamparm *parms)
1084 {
1085         struct mcam_camera *cam = priv;
1086         int ret;
1087
1088         mutex_lock(&cam->s_mutex);
1089         ret = sensor_call(cam, video, s_parm, parms);
1090         mutex_unlock(&cam->s_mutex);
1091         parms->parm.capture.readbuffers = n_dma_bufs;
1092         return ret;
1093 }
1094
1095 static int mcam_vidioc_g_chip_ident(struct file *file, void *priv,
1096                 struct v4l2_dbg_chip_ident *chip)
1097 {
1098         struct mcam_camera *cam = priv;
1099
1100         chip->ident = V4L2_IDENT_NONE;
1101         chip->revision = 0;
1102         if (v4l2_chip_match_host(&chip->match)) {
1103                 chip->ident = cam->chip_id;
1104                 return 0;
1105         }
1106         return sensor_call(cam, core, g_chip_ident, chip);
1107 }
1108
1109 static int mcam_vidioc_enum_framesizes(struct file *filp, void *priv,
1110                 struct v4l2_frmsizeenum *sizes)
1111 {
1112         struct mcam_camera *cam = priv;
1113         int ret;
1114
1115         mutex_lock(&cam->s_mutex);
1116         ret = sensor_call(cam, video, enum_framesizes, sizes);
1117         mutex_unlock(&cam->s_mutex);
1118         return ret;
1119 }
1120
1121 static int mcam_vidioc_enum_frameintervals(struct file *filp, void *priv,
1122                 struct v4l2_frmivalenum *interval)
1123 {
1124         struct mcam_camera *cam = priv;
1125         int ret;
1126
1127         mutex_lock(&cam->s_mutex);
1128         ret = sensor_call(cam, video, enum_frameintervals, interval);
1129         mutex_unlock(&cam->s_mutex);
1130         return ret;
1131 }
1132
1133 #ifdef CONFIG_VIDEO_ADV_DEBUG
1134 static int mcam_vidioc_g_register(struct file *file, void *priv,
1135                 struct v4l2_dbg_register *reg)
1136 {
1137         struct mcam_camera *cam = priv;
1138
1139         if (v4l2_chip_match_host(&reg->match)) {
1140                 reg->val = mcam_reg_read(cam, reg->reg);
1141                 reg->size = 4;
1142                 return 0;
1143         }
1144         return sensor_call(cam, core, g_register, reg);
1145 }
1146
1147 static int mcam_vidioc_s_register(struct file *file, void *priv,
1148                 struct v4l2_dbg_register *reg)
1149 {
1150         struct mcam_camera *cam = priv;
1151
1152         if (v4l2_chip_match_host(&reg->match)) {
1153                 mcam_reg_write(cam, reg->reg, reg->val);
1154                 return 0;
1155         }
1156         return sensor_call(cam, core, s_register, reg);
1157 }
1158 #endif
1159
1160 /*
1161  * This template device holds all of those v4l2 methods; we
1162  * clone it for specific real devices.
1163  */
1164
1165 static const struct v4l2_file_operations mcam_v4l_fops = {
1166         .owner = THIS_MODULE,
1167         .open = mcam_v4l_open,
1168         .release = mcam_v4l_release,
1169         .read = mcam_v4l_read,
1170         .poll = mcam_v4l_poll,
1171         .mmap = mcam_v4l_mmap,
1172         .unlocked_ioctl = video_ioctl2,
1173 };
1174
1175 static const struct v4l2_ioctl_ops mcam_v4l_ioctl_ops = {
1176         .vidioc_querycap        = mcam_vidioc_querycap,
1177         .vidioc_enum_fmt_vid_cap = mcam_vidioc_enum_fmt_vid_cap,
1178         .vidioc_try_fmt_vid_cap = mcam_vidioc_try_fmt_vid_cap,
1179         .vidioc_s_fmt_vid_cap   = mcam_vidioc_s_fmt_vid_cap,
1180         .vidioc_g_fmt_vid_cap   = mcam_vidioc_g_fmt_vid_cap,
1181         .vidioc_enum_input      = mcam_vidioc_enum_input,
1182         .vidioc_g_input         = mcam_vidioc_g_input,
1183         .vidioc_s_input         = mcam_vidioc_s_input,
1184         .vidioc_s_std           = mcam_vidioc_s_std,
1185         .vidioc_reqbufs         = mcam_vidioc_reqbufs,
1186         .vidioc_querybuf        = mcam_vidioc_querybuf,
1187         .vidioc_qbuf            = mcam_vidioc_qbuf,
1188         .vidioc_dqbuf           = mcam_vidioc_dqbuf,
1189         .vidioc_streamon        = mcam_vidioc_streamon,
1190         .vidioc_streamoff       = mcam_vidioc_streamoff,
1191         .vidioc_queryctrl       = mcam_vidioc_queryctrl,
1192         .vidioc_g_ctrl          = mcam_vidioc_g_ctrl,
1193         .vidioc_s_ctrl          = mcam_vidioc_s_ctrl,
1194         .vidioc_g_parm          = mcam_vidioc_g_parm,
1195         .vidioc_s_parm          = mcam_vidioc_s_parm,
1196         .vidioc_enum_framesizes = mcam_vidioc_enum_framesizes,
1197         .vidioc_enum_frameintervals = mcam_vidioc_enum_frameintervals,
1198         .vidioc_g_chip_ident    = mcam_vidioc_g_chip_ident,
1199 #ifdef CONFIG_VIDEO_ADV_DEBUG
1200         .vidioc_g_register      = mcam_vidioc_g_register,
1201         .vidioc_s_register      = mcam_vidioc_s_register,
1202 #endif
1203 };
1204
1205 static struct video_device mcam_v4l_template = {
1206         .name = "mcam",
1207         .tvnorms = V4L2_STD_NTSC_M,
1208         .current_norm = V4L2_STD_NTSC_M,  /* make mplayer happy */
1209
1210         .fops = &mcam_v4l_fops,
1211         .ioctl_ops = &mcam_v4l_ioctl_ops,
1212         .release = video_device_release_empty,
1213 };
1214
1215 /* ---------------------------------------------------------------------- */
1216 /*
1217  * Interrupt handler stuff
1218  */
1219
1220
1221
1222 static void mcam_frame_tasklet(unsigned long data)
1223 {
1224         struct mcam_camera *cam = (struct mcam_camera *) data;
1225         int i;
1226         unsigned long flags;
1227         struct mcam_vb_buffer *buf;
1228
1229         spin_lock_irqsave(&cam->dev_lock, flags);
1230         for (i = 0; i < cam->nbufs; i++) {
1231                 int bufno = cam->next_buf;
1232
1233                 if (cam->state != S_STREAMING || bufno < 0)
1234                         break;  /* I/O got stopped */
1235                 if (++(cam->next_buf) >= cam->nbufs)
1236                         cam->next_buf = 0;
1237                 if (!test_bit(bufno, &cam->flags))
1238                         continue;
1239                 if (list_empty(&cam->buffers))
1240                         break;  /* Leave it valid, hope for better later */
1241                 clear_bit(bufno, &cam->flags);
1242                 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer,
1243                                 queue);
1244                 list_del_init(&buf->queue);
1245                 /*
1246                  * Drop the lock during the big copy.  This *should* be safe...
1247                  */
1248                 spin_unlock_irqrestore(&cam->dev_lock, flags);
1249                 memcpy(vb2_plane_vaddr(&buf->vb_buf, 0), cam->dma_bufs[bufno],
1250                                 cam->pix_format.sizeimage);
1251                 buf->vb_buf.v4l2_buf.bytesused = cam->pix_format.sizeimage;
1252                 buf->vb_buf.v4l2_buf.sequence = cam->buf_seq[bufno];
1253                 buf->vb_buf.v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1254                 buf->vb_buf.v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
1255                 vb2_set_plane_payload(&buf->vb_buf, 0,
1256                                 cam->pix_format.sizeimage);
1257                 vb2_buffer_done(&buf->vb_buf, VB2_BUF_STATE_DONE);
1258                 spin_lock_irqsave(&cam->dev_lock, flags);
1259         }
1260         spin_unlock_irqrestore(&cam->dev_lock, flags);
1261 }
1262
1263
1264
1265 static void mcam_frame_complete(struct mcam_camera *cam, int frame)
1266 {
1267         /*
1268          * Basic frame housekeeping.
1269          */
1270         if (test_bit(frame, &cam->flags) && printk_ratelimit())
1271                 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1272         set_bit(frame, &cam->flags);
1273         clear_bit(CF_DMA_ACTIVE, &cam->flags);
1274         if (cam->next_buf < 0)
1275                 cam->next_buf = frame;
1276         cam->buf_seq[frame] = ++(cam->sequence);
1277
1278         switch (cam->state) {
1279         /*
1280          * For the streaming case, we defer the real work to the
1281          * camera tasklet.
1282          *
1283          * FIXME: if the application is not consuming the buffers,
1284          * we should eventually put things on hold and restart in
1285          * vidioc_dqbuf().
1286          */
1287         case S_STREAMING:
1288                 tasklet_schedule(&cam->s_tasklet);
1289                 break;
1290
1291         default:
1292                 cam_err(cam, "Frame interrupt in non-operational state\n");
1293                 break;
1294         }
1295 }
1296
1297
1298
1299
1300 int mccic_irq(struct mcam_camera *cam, unsigned int irqs)
1301 {
1302         unsigned int frame, handled = 0;
1303
1304         mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1305         /*
1306          * Handle any frame completions.  There really should
1307          * not be more than one of these, or we have fallen
1308          * far behind.
1309          */
1310         for (frame = 0; frame < cam->nbufs; frame++)
1311                 if (irqs & (IRQ_EOF0 << frame)) {
1312                         mcam_frame_complete(cam, frame);
1313                         handled = 1;
1314                 }
1315         /*
1316          * If a frame starts, note that we have DMA active.  This
1317          * code assumes that we won't get multiple frame interrupts
1318          * at once; may want to rethink that.
1319          */
1320         if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2)) {
1321                 set_bit(CF_DMA_ACTIVE, &cam->flags);
1322                 handled = 1;
1323         }
1324         return handled;
1325 }
1326
1327 /*
1328  * Registration and such.
1329  */
1330
1331 static struct ov7670_config sensor_cfg = {
1332         /*
1333          * Exclude QCIF mode, because it only captures a tiny portion
1334          * of the sensor FOV
1335          */
1336         .min_width = 320,
1337         .min_height = 240,
1338 };
1339
1340
1341 int mccic_register(struct mcam_camera *cam)
1342 {
1343         struct i2c_board_info ov7670_info = {
1344                 .type = "ov7670",
1345                 .addr = 0x42 >> 1,
1346                 .platform_data = &sensor_cfg,
1347         };
1348         int ret;
1349
1350         /*
1351          * Register with V4L
1352          */
1353         ret = v4l2_device_register(cam->dev, &cam->v4l2_dev);
1354         if (ret)
1355                 return ret;
1356
1357         mutex_init(&cam->s_mutex);
1358         cam->state = S_NOTREADY;
1359         mcam_set_config_needed(cam, 1);
1360         cam->pix_format = mcam_def_pix_format;
1361         cam->mbus_code = mcam_def_mbus_code;
1362         INIT_LIST_HEAD(&cam->dev_list);
1363         INIT_LIST_HEAD(&cam->buffers);
1364         tasklet_init(&cam->s_tasklet, mcam_frame_tasklet, (unsigned long) cam);
1365
1366         mcam_ctlr_init(cam);
1367
1368         /*
1369          * Try to find the sensor.
1370          */
1371         sensor_cfg.clock_speed = cam->clock_speed;
1372         sensor_cfg.use_smbus = cam->use_smbus;
1373         cam->sensor_addr = ov7670_info.addr;
1374         cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev,
1375                         cam->i2c_adapter, &ov7670_info, NULL);
1376         if (cam->sensor == NULL) {
1377                 ret = -ENODEV;
1378                 goto out_unregister;
1379         }
1380
1381         ret = mcam_cam_init(cam);
1382         if (ret)
1383                 goto out_unregister;
1384         /*
1385          * Get the v4l2 setup done.
1386          */
1387         mutex_lock(&cam->s_mutex);
1388         cam->vdev = mcam_v4l_template;
1389         cam->vdev.debug = 0;
1390         cam->vdev.v4l2_dev = &cam->v4l2_dev;
1391         ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1392         if (ret)
1393                 goto out;
1394         video_set_drvdata(&cam->vdev, cam);
1395
1396         /*
1397          * If so requested, try to get our DMA buffers now.
1398          */
1399         if (!alloc_bufs_at_read) {
1400                 if (mcam_alloc_dma_bufs(cam, 1))
1401                         cam_warn(cam, "Unable to alloc DMA buffers at load"
1402                                         " will try again later.");
1403         }
1404
1405 out:
1406         mutex_unlock(&cam->s_mutex);
1407         return ret;
1408 out_unregister:
1409         v4l2_device_unregister(&cam->v4l2_dev);
1410         return ret;
1411 }
1412
1413
1414 void mccic_shutdown(struct mcam_camera *cam)
1415 {
1416         /*
1417          * If we have no users (and we really, really should have no
1418          * users) the device will already be powered down.  Trying to
1419          * take it down again will wedge the machine, which is frowned
1420          * upon.
1421          */
1422         if (cam->users > 0) {
1423                 cam_warn(cam, "Removing a device with users!\n");
1424                 mcam_ctlr_power_down(cam);
1425         }
1426         vb2_queue_release(&cam->vb_queue);
1427         mcam_free_dma_bufs(cam);
1428         video_unregister_device(&cam->vdev);
1429         v4l2_device_unregister(&cam->v4l2_dev);
1430 }
1431
1432 /*
1433  * Power management
1434  */
1435 #ifdef CONFIG_PM
1436
1437 void mccic_suspend(struct mcam_camera *cam)
1438 {
1439         enum mcam_state cstate = cam->state;
1440
1441         mcam_ctlr_stop_dma(cam);
1442         mcam_ctlr_power_down(cam);
1443         cam->state = cstate;
1444 }
1445
1446 int mccic_resume(struct mcam_camera *cam)
1447 {
1448         int ret = 0;
1449
1450         mutex_lock(&cam->s_mutex);
1451         if (cam->users > 0) {
1452                 mcam_ctlr_power_up(cam);
1453                 __mcam_cam_reset(cam);
1454         } else {
1455                 mcam_ctlr_power_down(cam);
1456         }
1457         mutex_unlock(&cam->s_mutex);
1458
1459         set_bit(CF_CONFIG_NEEDED, &cam->flags);
1460         if (cam->state == S_STREAMING)
1461                 ret = mcam_read_setup(cam, cam->state);
1462         return ret;
1463 }
1464 #endif /* CONFIG_PM */