GNU Linux-libre 4.4.289-gnu1
[releases.git] / drivers / media / platform / 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/slab.h>
15 #include <linux/device.h>
16 #include <linux/wait.h>
17 #include <linux/list.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/delay.h>
20 #include <linux/vmalloc.h>
21 #include <linux/io.h>
22 #include <linux/clk.h>
23 #include <linux/videodev2.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-event.h>
28 #include <media/ov7670.h>
29 #include <media/videobuf2-vmalloc.h>
30 #include <media/videobuf2-dma-contig.h>
31 #include <media/videobuf2-dma-sg.h>
32
33 #include "mcam-core.h"
34
35 #ifdef MCAM_MODE_VMALLOC
36 /*
37  * Internal DMA buffer management.  Since the controller cannot do S/G I/O,
38  * we must have physically contiguous buffers to bring frames into.
39  * These parameters control how many buffers we use, whether we
40  * allocate them at load time (better chance of success, but nails down
41  * memory) or when somebody tries to use the camera (riskier), and,
42  * for load-time allocation, how big they should be.
43  *
44  * The controller can cycle through three buffers.  We could use
45  * more by flipping pointers around, but it probably makes little
46  * sense.
47  */
48
49 static bool alloc_bufs_at_read;
50 module_param(alloc_bufs_at_read, bool, 0444);
51 MODULE_PARM_DESC(alloc_bufs_at_read,
52                 "Non-zero value causes DMA buffers to be allocated when the "
53                 "video capture device is read, rather than at module load "
54                 "time.  This saves memory, but decreases the chances of "
55                 "successfully getting those buffers.  This parameter is "
56                 "only used in the vmalloc buffer mode");
57
58 static int n_dma_bufs = 3;
59 module_param(n_dma_bufs, uint, 0644);
60 MODULE_PARM_DESC(n_dma_bufs,
61                 "The number of DMA buffers to allocate.  Can be either two "
62                 "(saves memory, makes timing tighter) or three.");
63
64 static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2;  /* Worst case */
65 module_param(dma_buf_size, uint, 0444);
66 MODULE_PARM_DESC(dma_buf_size,
67                 "The size of the allocated DMA buffers.  If actual operating "
68                 "parameters require larger buffers, an attempt to reallocate "
69                 "will be made.");
70 #else /* MCAM_MODE_VMALLOC */
71 static const bool alloc_bufs_at_read;
72 static const int n_dma_bufs = 3;  /* Used by S/G_PARM */
73 #endif /* MCAM_MODE_VMALLOC */
74
75 static bool flip;
76 module_param(flip, bool, 0444);
77 MODULE_PARM_DESC(flip,
78                 "If set, the sensor will be instructed to flip the image "
79                 "vertically.");
80
81 static int buffer_mode = -1;
82 module_param(buffer_mode, int, 0444);
83 MODULE_PARM_DESC(buffer_mode,
84                 "Set the buffer mode to be used; default is to go with what "
85                 "the platform driver asks for.  Set to 0 for vmalloc, 1 for "
86                 "DMA contiguous.");
87
88 /*
89  * Status flags.  Always manipulated with bit operations.
90  */
91 #define CF_BUF0_VALID    0      /* Buffers valid - first three */
92 #define CF_BUF1_VALID    1
93 #define CF_BUF2_VALID    2
94 #define CF_DMA_ACTIVE    3      /* A frame is incoming */
95 #define CF_CONFIG_NEEDED 4      /* Must configure hardware */
96 #define CF_SINGLE_BUFFER 5      /* Running with a single buffer */
97 #define CF_SG_RESTART    6      /* SG restart needed */
98 #define CF_FRAME_SOF0    7      /* Frame 0 started */
99 #define CF_FRAME_SOF1    8
100 #define CF_FRAME_SOF2    9
101
102 #define sensor_call(cam, o, f, args...) \
103         v4l2_subdev_call(cam->sensor, o, f, ##args)
104
105 static struct mcam_format_struct {
106         __u8 *desc;
107         __u32 pixelformat;
108         int bpp;   /* Bytes per pixel */
109         bool planar;
110         u32 mbus_code;
111 } mcam_formats[] = {
112         {
113                 .desc           = "YUYV 4:2:2",
114                 .pixelformat    = V4L2_PIX_FMT_YUYV,
115                 .mbus_code      = MEDIA_BUS_FMT_YUYV8_2X8,
116                 .bpp            = 2,
117                 .planar         = false,
118         },
119         {
120                 .desc           = "YVYU 4:2:2",
121                 .pixelformat    = V4L2_PIX_FMT_YVYU,
122                 .mbus_code      = MEDIA_BUS_FMT_YUYV8_2X8,
123                 .bpp            = 2,
124                 .planar         = false,
125         },
126         {
127                 .desc           = "YUV 4:2:0 PLANAR",
128                 .pixelformat    = V4L2_PIX_FMT_YUV420,
129                 .mbus_code      = MEDIA_BUS_FMT_YUYV8_2X8,
130                 .bpp            = 1,
131                 .planar         = true,
132         },
133         {
134                 .desc           = "YVU 4:2:0 PLANAR",
135                 .pixelformat    = V4L2_PIX_FMT_YVU420,
136                 .mbus_code      = MEDIA_BUS_FMT_YUYV8_2X8,
137                 .bpp            = 1,
138                 .planar         = true,
139         },
140         {
141                 .desc           = "XRGB 444",
142                 .pixelformat    = V4L2_PIX_FMT_XRGB444,
143                 .mbus_code      = MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE,
144                 .bpp            = 2,
145                 .planar         = false,
146         },
147         {
148                 .desc           = "RGB 565",
149                 .pixelformat    = V4L2_PIX_FMT_RGB565,
150                 .mbus_code      = MEDIA_BUS_FMT_RGB565_2X8_LE,
151                 .bpp            = 2,
152                 .planar         = false,
153         },
154         {
155                 .desc           = "Raw RGB Bayer",
156                 .pixelformat    = V4L2_PIX_FMT_SBGGR8,
157                 .mbus_code      = MEDIA_BUS_FMT_SBGGR8_1X8,
158                 .bpp            = 1,
159                 .planar         = false,
160         },
161 };
162 #define N_MCAM_FMTS ARRAY_SIZE(mcam_formats)
163
164 static struct mcam_format_struct *mcam_find_format(u32 pixelformat)
165 {
166         unsigned i;
167
168         for (i = 0; i < N_MCAM_FMTS; i++)
169                 if (mcam_formats[i].pixelformat == pixelformat)
170                         return mcam_formats + i;
171         /* Not found? Then return the first format. */
172         return mcam_formats;
173 }
174
175 /*
176  * The default format we use until somebody says otherwise.
177  */
178 static const struct v4l2_pix_format mcam_def_pix_format = {
179         .width          = VGA_WIDTH,
180         .height         = VGA_HEIGHT,
181         .pixelformat    = V4L2_PIX_FMT_YUYV,
182         .field          = V4L2_FIELD_NONE,
183         .bytesperline   = VGA_WIDTH*2,
184         .sizeimage      = VGA_WIDTH*VGA_HEIGHT*2,
185         .colorspace     = V4L2_COLORSPACE_SRGB,
186 };
187
188 static const u32 mcam_def_mbus_code = MEDIA_BUS_FMT_YUYV8_2X8;
189
190
191 /*
192  * The two-word DMA descriptor format used by the Armada 610 and like.  There
193  * Is a three-word format as well (set C1_DESC_3WORD) where the third
194  * word is a pointer to the next descriptor, but we don't use it.  Two-word
195  * descriptors have to be contiguous in memory.
196  */
197 struct mcam_dma_desc {
198         u32 dma_addr;
199         u32 segment_len;
200 };
201
202 /*
203  * Our buffer type for working with videobuf2.  Note that the vb2
204  * developers have decreed that struct vb2_v4l2_buffer must be at the
205  * beginning of this structure.
206  */
207 struct mcam_vb_buffer {
208         struct vb2_v4l2_buffer vb_buf;
209         struct list_head queue;
210         struct mcam_dma_desc *dma_desc; /* Descriptor virtual address */
211         dma_addr_t dma_desc_pa;         /* Descriptor physical address */
212 };
213
214 static inline struct mcam_vb_buffer *vb_to_mvb(struct vb2_v4l2_buffer *vb)
215 {
216         return container_of(vb, struct mcam_vb_buffer, vb_buf);
217 }
218
219 /*
220  * Hand a completed buffer back to user space.
221  */
222 static void mcam_buffer_done(struct mcam_camera *cam, int frame,
223                 struct vb2_v4l2_buffer *vbuf)
224 {
225         vbuf->vb2_buf.planes[0].bytesused = cam->pix_format.sizeimage;
226         vbuf->sequence = cam->buf_seq[frame];
227         vbuf->field = V4L2_FIELD_NONE;
228         v4l2_get_timestamp(&vbuf->timestamp);
229         vb2_set_plane_payload(&vbuf->vb2_buf, 0, cam->pix_format.sizeimage);
230         vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_DONE);
231 }
232
233
234
235 /*
236  * Debugging and related.
237  */
238 #define cam_err(cam, fmt, arg...) \
239         dev_err((cam)->dev, fmt, ##arg);
240 #define cam_warn(cam, fmt, arg...) \
241         dev_warn((cam)->dev, fmt, ##arg);
242 #define cam_dbg(cam, fmt, arg...) \
243         dev_dbg((cam)->dev, fmt, ##arg);
244
245
246 /*
247  * Flag manipulation helpers
248  */
249 static void mcam_reset_buffers(struct mcam_camera *cam)
250 {
251         int i;
252
253         cam->next_buf = -1;
254         for (i = 0; i < cam->nbufs; i++) {
255                 clear_bit(i, &cam->flags);
256                 clear_bit(CF_FRAME_SOF0 + i, &cam->flags);
257         }
258 }
259
260 static inline int mcam_needs_config(struct mcam_camera *cam)
261 {
262         return test_bit(CF_CONFIG_NEEDED, &cam->flags);
263 }
264
265 static void mcam_set_config_needed(struct mcam_camera *cam, int needed)
266 {
267         if (needed)
268                 set_bit(CF_CONFIG_NEEDED, &cam->flags);
269         else
270                 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
271 }
272
273 /* ------------------------------------------------------------------- */
274 /*
275  * Make the controller start grabbing images.  Everything must
276  * be set up before doing this.
277  */
278 static void mcam_ctlr_start(struct mcam_camera *cam)
279 {
280         /* set_bit performs a read, so no other barrier should be
281            needed here */
282         mcam_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
283 }
284
285 static void mcam_ctlr_stop(struct mcam_camera *cam)
286 {
287         mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
288 }
289
290 static void mcam_enable_mipi(struct mcam_camera *mcam)
291 {
292         /* Using MIPI mode and enable MIPI */
293         cam_dbg(mcam, "camera: DPHY3=0x%x, DPHY5=0x%x, DPHY6=0x%x\n",
294                         mcam->dphy[0], mcam->dphy[1], mcam->dphy[2]);
295         mcam_reg_write(mcam, REG_CSI2_DPHY3, mcam->dphy[0]);
296         mcam_reg_write(mcam, REG_CSI2_DPHY5, mcam->dphy[1]);
297         mcam_reg_write(mcam, REG_CSI2_DPHY6, mcam->dphy[2]);
298
299         if (!mcam->mipi_enabled) {
300                 if (mcam->lane > 4 || mcam->lane <= 0) {
301                         cam_warn(mcam, "lane number error\n");
302                         mcam->lane = 1; /* set the default value */
303                 }
304                 /*
305                  * 0x41 actives 1 lane
306                  * 0x43 actives 2 lanes
307                  * 0x45 actives 3 lanes (never happen)
308                  * 0x47 actives 4 lanes
309                  */
310                 mcam_reg_write(mcam, REG_CSI2_CTRL0,
311                         CSI2_C0_MIPI_EN | CSI2_C0_ACT_LANE(mcam->lane));
312                 mcam_reg_write(mcam, REG_CLKCTRL,
313                         (mcam->mclk_src << 29) | mcam->mclk_div);
314
315                 mcam->mipi_enabled = true;
316         }
317 }
318
319 static void mcam_disable_mipi(struct mcam_camera *mcam)
320 {
321         /* Using Parallel mode or disable MIPI */
322         mcam_reg_write(mcam, REG_CSI2_CTRL0, 0x0);
323         mcam_reg_write(mcam, REG_CSI2_DPHY3, 0x0);
324         mcam_reg_write(mcam, REG_CSI2_DPHY5, 0x0);
325         mcam_reg_write(mcam, REG_CSI2_DPHY6, 0x0);
326         mcam->mipi_enabled = false;
327 }
328
329 static bool mcam_fmt_is_planar(__u32 pfmt)
330 {
331         struct mcam_format_struct *f;
332
333         f = mcam_find_format(pfmt);
334         return f->planar;
335 }
336
337 static void mcam_write_yuv_bases(struct mcam_camera *cam,
338                                  unsigned frame, dma_addr_t base)
339 {
340         struct v4l2_pix_format *fmt = &cam->pix_format;
341         u32 pixel_count = fmt->width * fmt->height;
342         dma_addr_t y, u = 0, v = 0;
343
344         y = base;
345
346         switch (fmt->pixelformat) {
347         case V4L2_PIX_FMT_YUV420:
348                 u = y + pixel_count;
349                 v = u + pixel_count / 4;
350                 break;
351         case V4L2_PIX_FMT_YVU420:
352                 v = y + pixel_count;
353                 u = v + pixel_count / 4;
354                 break;
355         default:
356                 break;
357         }
358
359         mcam_reg_write(cam, REG_Y0BAR + frame * 4, y);
360         if (mcam_fmt_is_planar(fmt->pixelformat)) {
361                 mcam_reg_write(cam, REG_U0BAR + frame * 4, u);
362                 mcam_reg_write(cam, REG_V0BAR + frame * 4, v);
363         }
364 }
365
366 /* ------------------------------------------------------------------- */
367
368 #ifdef MCAM_MODE_VMALLOC
369 /*
370  * Code specific to the vmalloc buffer mode.
371  */
372
373 /*
374  * Allocate in-kernel DMA buffers for vmalloc mode.
375  */
376 static int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
377 {
378         int i;
379
380         mcam_set_config_needed(cam, 1);
381         if (loadtime)
382                 cam->dma_buf_size = dma_buf_size;
383         else
384                 cam->dma_buf_size = cam->pix_format.sizeimage;
385         if (n_dma_bufs > 3)
386                 n_dma_bufs = 3;
387
388         cam->nbufs = 0;
389         for (i = 0; i < n_dma_bufs; i++) {
390                 cam->dma_bufs[i] = dma_alloc_coherent(cam->dev,
391                                 cam->dma_buf_size, cam->dma_handles + i,
392                                 GFP_KERNEL);
393                 if (cam->dma_bufs[i] == NULL) {
394                         cam_warn(cam, "Failed to allocate DMA buffer\n");
395                         break;
396                 }
397                 (cam->nbufs)++;
398         }
399
400         switch (cam->nbufs) {
401         case 1:
402                 dma_free_coherent(cam->dev, cam->dma_buf_size,
403                                 cam->dma_bufs[0], cam->dma_handles[0]);
404                 cam->nbufs = 0;
405         case 0:
406                 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
407                 return -ENOMEM;
408
409         case 2:
410                 if (n_dma_bufs > 2)
411                         cam_warn(cam, "Will limp along with only 2 buffers\n");
412                 break;
413         }
414         return 0;
415 }
416
417 static void mcam_free_dma_bufs(struct mcam_camera *cam)
418 {
419         int i;
420
421         for (i = 0; i < cam->nbufs; i++) {
422                 dma_free_coherent(cam->dev, cam->dma_buf_size,
423                                 cam->dma_bufs[i], cam->dma_handles[i]);
424                 cam->dma_bufs[i] = NULL;
425         }
426         cam->nbufs = 0;
427 }
428
429
430 /*
431  * Set up DMA buffers when operating in vmalloc mode
432  */
433 static void mcam_ctlr_dma_vmalloc(struct mcam_camera *cam)
434 {
435         /*
436          * Store the first two YUV buffers. Then either
437          * set the third if it exists, or tell the controller
438          * to just use two.
439          */
440         mcam_write_yuv_bases(cam, 0, cam->dma_handles[0]);
441         mcam_write_yuv_bases(cam, 1, cam->dma_handles[1]);
442         if (cam->nbufs > 2) {
443                 mcam_write_yuv_bases(cam, 2, cam->dma_handles[2]);
444                 mcam_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
445         } else
446                 mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
447         if (cam->chip_id == MCAM_CAFE)
448                 mcam_reg_write(cam, REG_UBAR, 0); /* 32 bits only */
449 }
450
451 /*
452  * Copy data out to user space in the vmalloc case
453  */
454 static void mcam_frame_tasklet(unsigned long data)
455 {
456         struct mcam_camera *cam = (struct mcam_camera *) data;
457         int i;
458         unsigned long flags;
459         struct mcam_vb_buffer *buf;
460
461         spin_lock_irqsave(&cam->dev_lock, flags);
462         for (i = 0; i < cam->nbufs; i++) {
463                 int bufno = cam->next_buf;
464
465                 if (cam->state != S_STREAMING || bufno < 0)
466                         break;  /* I/O got stopped */
467                 if (++(cam->next_buf) >= cam->nbufs)
468                         cam->next_buf = 0;
469                 if (!test_bit(bufno, &cam->flags))
470                         continue;
471                 if (list_empty(&cam->buffers)) {
472                         cam->frame_state.singles++;
473                         break;  /* Leave it valid, hope for better later */
474                 }
475                 cam->frame_state.delivered++;
476                 clear_bit(bufno, &cam->flags);
477                 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer,
478                                 queue);
479                 list_del_init(&buf->queue);
480                 /*
481                  * Drop the lock during the big copy.  This *should* be safe...
482                  */
483                 spin_unlock_irqrestore(&cam->dev_lock, flags);
484                 memcpy(vb2_plane_vaddr(&buf->vb_buf.vb2_buf, 0),
485                                 cam->dma_bufs[bufno],
486                                 cam->pix_format.sizeimage);
487                 mcam_buffer_done(cam, bufno, &buf->vb_buf);
488                 spin_lock_irqsave(&cam->dev_lock, flags);
489         }
490         spin_unlock_irqrestore(&cam->dev_lock, flags);
491 }
492
493
494 /*
495  * Make sure our allocated buffers are up to the task.
496  */
497 static int mcam_check_dma_buffers(struct mcam_camera *cam)
498 {
499         if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
500                         mcam_free_dma_bufs(cam);
501         if (cam->nbufs == 0)
502                 return mcam_alloc_dma_bufs(cam, 0);
503         return 0;
504 }
505
506 static void mcam_vmalloc_done(struct mcam_camera *cam, int frame)
507 {
508         tasklet_schedule(&cam->s_tasklet);
509 }
510
511 #else /* MCAM_MODE_VMALLOC */
512
513 static inline int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
514 {
515         return 0;
516 }
517
518 static inline void mcam_free_dma_bufs(struct mcam_camera *cam)
519 {
520         return;
521 }
522
523 static inline int mcam_check_dma_buffers(struct mcam_camera *cam)
524 {
525         return 0;
526 }
527
528
529
530 #endif /* MCAM_MODE_VMALLOC */
531
532
533 #ifdef MCAM_MODE_DMA_CONTIG
534 /* ---------------------------------------------------------------------- */
535 /*
536  * DMA-contiguous code.
537  */
538
539 /*
540  * Set up a contiguous buffer for the given frame.  Here also is where
541  * the underrun strategy is set: if there is no buffer available, reuse
542  * the buffer from the other BAR and set the CF_SINGLE_BUFFER flag to
543  * keep the interrupt handler from giving that buffer back to user
544  * space.  In this way, we always have a buffer to DMA to and don't
545  * have to try to play games stopping and restarting the controller.
546  */
547 static void mcam_set_contig_buffer(struct mcam_camera *cam, int frame)
548 {
549         struct mcam_vb_buffer *buf;
550         dma_addr_t dma_handle;
551         struct vb2_v4l2_buffer *vb;
552
553         /*
554          * If there are no available buffers, go into single mode
555          */
556         if (list_empty(&cam->buffers)) {
557                 buf = cam->vb_bufs[frame ^ 0x1];
558                 set_bit(CF_SINGLE_BUFFER, &cam->flags);
559                 cam->frame_state.singles++;
560         } else {
561                 /*
562                  * OK, we have a buffer we can use.
563                  */
564                 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer,
565                                         queue);
566                 list_del_init(&buf->queue);
567                 clear_bit(CF_SINGLE_BUFFER, &cam->flags);
568         }
569
570         cam->vb_bufs[frame] = buf;
571         vb = &buf->vb_buf;
572
573         dma_handle = vb2_dma_contig_plane_dma_addr(&vb->vb2_buf, 0);
574         mcam_write_yuv_bases(cam, frame, dma_handle);
575 }
576
577 /*
578  * Initial B_DMA_contig setup.
579  */
580 static void mcam_ctlr_dma_contig(struct mcam_camera *cam)
581 {
582         mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
583         cam->nbufs = 2;
584         mcam_set_contig_buffer(cam, 0);
585         mcam_set_contig_buffer(cam, 1);
586 }
587
588 /*
589  * Frame completion handling.
590  */
591 static void mcam_dma_contig_done(struct mcam_camera *cam, int frame)
592 {
593         struct mcam_vb_buffer *buf = cam->vb_bufs[frame];
594
595         if (!test_bit(CF_SINGLE_BUFFER, &cam->flags)) {
596                 cam->frame_state.delivered++;
597                 cam->vb_bufs[frame] = NULL;
598                 mcam_buffer_done(cam, frame, &buf->vb_buf);
599         }
600         mcam_set_contig_buffer(cam, frame);
601 }
602
603 #endif /* MCAM_MODE_DMA_CONTIG */
604
605 #ifdef MCAM_MODE_DMA_SG
606 /* ---------------------------------------------------------------------- */
607 /*
608  * Scatter/gather-specific code.
609  */
610
611 /*
612  * Set up the next buffer for S/G I/O; caller should be sure that
613  * the controller is stopped and a buffer is available.
614  */
615 static void mcam_sg_next_buffer(struct mcam_camera *cam)
616 {
617         struct mcam_vb_buffer *buf;
618         struct sg_table *sg_table;
619
620         buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer, queue);
621         list_del_init(&buf->queue);
622         sg_table = vb2_dma_sg_plane_desc(&buf->vb_buf.vb2_buf, 0);
623         /*
624          * Very Bad Not Good Things happen if you don't clear
625          * C1_DESC_ENA before making any descriptor changes.
626          */
627         mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_ENA);
628         mcam_reg_write(cam, REG_DMA_DESC_Y, buf->dma_desc_pa);
629         mcam_reg_write(cam, REG_DESC_LEN_Y,
630                         sg_table->nents * sizeof(struct mcam_dma_desc));
631         mcam_reg_write(cam, REG_DESC_LEN_U, 0);
632         mcam_reg_write(cam, REG_DESC_LEN_V, 0);
633         mcam_reg_set_bit(cam, REG_CTRL1, C1_DESC_ENA);
634         cam->vb_bufs[0] = buf;
635 }
636
637 /*
638  * Initial B_DMA_sg setup
639  */
640 static void mcam_ctlr_dma_sg(struct mcam_camera *cam)
641 {
642         /*
643          * The list-empty condition can hit us at resume time
644          * if the buffer list was empty when the system was suspended.
645          */
646         if (list_empty(&cam->buffers)) {
647                 set_bit(CF_SG_RESTART, &cam->flags);
648                 return;
649         }
650
651         mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_3WORD);
652         mcam_sg_next_buffer(cam);
653         cam->nbufs = 3;
654 }
655
656
657 /*
658  * Frame completion with S/G is trickier.  We can't muck with
659  * a descriptor chain on the fly, since the controller buffers it
660  * internally.  So we have to actually stop and restart; Marvell
661  * says this is the way to do it.
662  *
663  * Of course, stopping is easier said than done; experience shows
664  * that the controller can start a frame *after* C0_ENABLE has been
665  * cleared.  So when running in S/G mode, the controller is "stopped"
666  * on receipt of the start-of-frame interrupt.  That means we can
667  * safely change the DMA descriptor array here and restart things
668  * (assuming there's another buffer waiting to go).
669  */
670 static void mcam_dma_sg_done(struct mcam_camera *cam, int frame)
671 {
672         struct mcam_vb_buffer *buf = cam->vb_bufs[0];
673
674         /*
675          * If we're no longer supposed to be streaming, don't do anything.
676          */
677         if (cam->state != S_STREAMING)
678                 return;
679         /*
680          * If we have another buffer available, put it in and
681          * restart the engine.
682          */
683         if (!list_empty(&cam->buffers)) {
684                 mcam_sg_next_buffer(cam);
685                 mcam_ctlr_start(cam);
686         /*
687          * Otherwise set CF_SG_RESTART and the controller will
688          * be restarted once another buffer shows up.
689          */
690         } else {
691                 set_bit(CF_SG_RESTART, &cam->flags);
692                 cam->frame_state.singles++;
693                 cam->vb_bufs[0] = NULL;
694         }
695         /*
696          * Now we can give the completed frame back to user space.
697          */
698         cam->frame_state.delivered++;
699         mcam_buffer_done(cam, frame, &buf->vb_buf);
700 }
701
702
703 /*
704  * Scatter/gather mode requires stopping the controller between
705  * frames so we can put in a new DMA descriptor array.  If no new
706  * buffer exists at frame completion, the controller is left stopped;
707  * this function is charged with gettig things going again.
708  */
709 static void mcam_sg_restart(struct mcam_camera *cam)
710 {
711         mcam_ctlr_dma_sg(cam);
712         mcam_ctlr_start(cam);
713         clear_bit(CF_SG_RESTART, &cam->flags);
714 }
715
716 #else /* MCAM_MODE_DMA_SG */
717
718 static inline void mcam_sg_restart(struct mcam_camera *cam)
719 {
720         return;
721 }
722
723 #endif /* MCAM_MODE_DMA_SG */
724
725 /* ---------------------------------------------------------------------- */
726 /*
727  * Buffer-mode-independent controller code.
728  */
729
730 /*
731  * Image format setup
732  */
733 static void mcam_ctlr_image(struct mcam_camera *cam)
734 {
735         struct v4l2_pix_format *fmt = &cam->pix_format;
736         u32 widthy = 0, widthuv = 0, imgsz_h, imgsz_w;
737
738         cam_dbg(cam, "camera: bytesperline = %d; height = %d\n",
739                 fmt->bytesperline, fmt->sizeimage / fmt->bytesperline);
740         imgsz_h = (fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK;
741         imgsz_w = (fmt->width * 2) & IMGSZ_H_MASK;
742
743         switch (fmt->pixelformat) {
744         case V4L2_PIX_FMT_YUYV:
745         case V4L2_PIX_FMT_YVYU:
746                 widthy = fmt->width * 2;
747                 widthuv = 0;
748                 break;
749         case V4L2_PIX_FMT_YUV420:
750         case V4L2_PIX_FMT_YVU420:
751                 widthy = fmt->width;
752                 widthuv = fmt->width / 2;
753                 break;
754         default:
755                 widthy = fmt->bytesperline;
756                 widthuv = 0;
757                 break;
758         }
759
760         mcam_reg_write_mask(cam, REG_IMGPITCH, widthuv << 16 | widthy,
761                         IMGP_YP_MASK | IMGP_UVP_MASK);
762         mcam_reg_write(cam, REG_IMGSIZE, imgsz_h | imgsz_w);
763         mcam_reg_write(cam, REG_IMGOFFSET, 0x0);
764
765         /*
766          * Tell the controller about the image format we are using.
767          */
768         switch (fmt->pixelformat) {
769         case V4L2_PIX_FMT_YUV420:
770         case V4L2_PIX_FMT_YVU420:
771                 mcam_reg_write_mask(cam, REG_CTRL0,
772                         C0_DF_YUV | C0_YUV_420PL | C0_YUVE_VYUY, C0_DF_MASK);
773                 break;
774         case V4L2_PIX_FMT_YUYV:
775                 mcam_reg_write_mask(cam, REG_CTRL0,
776                         C0_DF_YUV | C0_YUV_PACKED | C0_YUVE_NOSWAP, C0_DF_MASK);
777                 break;
778         case V4L2_PIX_FMT_YVYU:
779                 mcam_reg_write_mask(cam, REG_CTRL0,
780                         C0_DF_YUV | C0_YUV_PACKED | C0_YUVE_SWAP24, C0_DF_MASK);
781                 break;
782         case V4L2_PIX_FMT_XRGB444:
783                 mcam_reg_write_mask(cam, REG_CTRL0,
784                         C0_DF_RGB | C0_RGBF_444 | C0_RGB4_XBGR, C0_DF_MASK);
785                 break;
786         case V4L2_PIX_FMT_RGB565:
787                 mcam_reg_write_mask(cam, REG_CTRL0,
788                         C0_DF_RGB | C0_RGBF_565 | C0_RGB5_BGGR, C0_DF_MASK);
789                 break;
790         case V4L2_PIX_FMT_SBGGR8:
791                 mcam_reg_write_mask(cam, REG_CTRL0,
792                         C0_DF_RGB | C0_RGB5_GRBG, C0_DF_MASK);
793                 break;
794         default:
795                 cam_err(cam, "camera: unknown format: %#x\n", fmt->pixelformat);
796                 break;
797         }
798
799         /*
800          * Make sure it knows we want to use hsync/vsync.
801          */
802         mcam_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC, C0_SIFM_MASK);
803         /*
804          * This field controls the generation of EOF(DVP only)
805          */
806         if (cam->bus_type != V4L2_MBUS_CSI2)
807                 mcam_reg_set_bit(cam, REG_CTRL0,
808                                 C0_EOF_VSYNC | C0_VEDGE_CTRL);
809 }
810
811
812 /*
813  * Configure the controller for operation; caller holds the
814  * device mutex.
815  */
816 static int mcam_ctlr_configure(struct mcam_camera *cam)
817 {
818         unsigned long flags;
819
820         spin_lock_irqsave(&cam->dev_lock, flags);
821         clear_bit(CF_SG_RESTART, &cam->flags);
822         cam->dma_setup(cam);
823         mcam_ctlr_image(cam);
824         mcam_set_config_needed(cam, 0);
825         spin_unlock_irqrestore(&cam->dev_lock, flags);
826         return 0;
827 }
828
829 static void mcam_ctlr_irq_enable(struct mcam_camera *cam)
830 {
831         /*
832          * Clear any pending interrupts, since we do not
833          * expect to have I/O active prior to enabling.
834          */
835         mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
836         mcam_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
837 }
838
839 static void mcam_ctlr_irq_disable(struct mcam_camera *cam)
840 {
841         mcam_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
842 }
843
844
845
846 static void mcam_ctlr_init(struct mcam_camera *cam)
847 {
848         unsigned long flags;
849
850         spin_lock_irqsave(&cam->dev_lock, flags);
851         /*
852          * Make sure it's not powered down.
853          */
854         mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
855         /*
856          * Turn off the enable bit.  It sure should be off anyway,
857          * but it's good to be sure.
858          */
859         mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
860         /*
861          * Clock the sensor appropriately.  Controller clock should
862          * be 48MHz, sensor "typical" value is half that.
863          */
864         mcam_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
865         spin_unlock_irqrestore(&cam->dev_lock, flags);
866 }
867
868
869 /*
870  * Stop the controller, and don't return until we're really sure that no
871  * further DMA is going on.
872  */
873 static void mcam_ctlr_stop_dma(struct mcam_camera *cam)
874 {
875         unsigned long flags;
876
877         /*
878          * Theory: stop the camera controller (whether it is operating
879          * or not).  Delay briefly just in case we race with the SOF
880          * interrupt, then wait until no DMA is active.
881          */
882         spin_lock_irqsave(&cam->dev_lock, flags);
883         clear_bit(CF_SG_RESTART, &cam->flags);
884         mcam_ctlr_stop(cam);
885         cam->state = S_IDLE;
886         spin_unlock_irqrestore(&cam->dev_lock, flags);
887         /*
888          * This is a brutally long sleep, but experience shows that
889          * it can take the controller a while to get the message that
890          * it needs to stop grabbing frames.  In particular, we can
891          * sometimes (on mmp) get a frame at the end WITHOUT the
892          * start-of-frame indication.
893          */
894         msleep(150);
895         if (test_bit(CF_DMA_ACTIVE, &cam->flags))
896                 cam_err(cam, "Timeout waiting for DMA to end\n");
897                 /* This would be bad news - what now? */
898         spin_lock_irqsave(&cam->dev_lock, flags);
899         mcam_ctlr_irq_disable(cam);
900         spin_unlock_irqrestore(&cam->dev_lock, flags);
901 }
902
903 /*
904  * Power up and down.
905  */
906 static int mcam_ctlr_power_up(struct mcam_camera *cam)
907 {
908         unsigned long flags;
909         int ret;
910
911         spin_lock_irqsave(&cam->dev_lock, flags);
912         ret = cam->plat_power_up(cam);
913         if (ret) {
914                 spin_unlock_irqrestore(&cam->dev_lock, flags);
915                 return ret;
916         }
917         mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
918         spin_unlock_irqrestore(&cam->dev_lock, flags);
919         msleep(5); /* Just to be sure */
920         return 0;
921 }
922
923 static void mcam_ctlr_power_down(struct mcam_camera *cam)
924 {
925         unsigned long flags;
926
927         spin_lock_irqsave(&cam->dev_lock, flags);
928         /*
929          * School of hard knocks department: be sure we do any register
930          * twiddling on the controller *before* calling the platform
931          * power down routine.
932          */
933         mcam_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
934         cam->plat_power_down(cam);
935         spin_unlock_irqrestore(&cam->dev_lock, flags);
936 }
937
938 /* -------------------------------------------------------------------- */
939 /*
940  * Communications with the sensor.
941  */
942
943 static int __mcam_cam_reset(struct mcam_camera *cam)
944 {
945         return sensor_call(cam, core, reset, 0);
946 }
947
948 /*
949  * We have found the sensor on the i2c.  Let's try to have a
950  * conversation.
951  */
952 static int mcam_cam_init(struct mcam_camera *cam)
953 {
954         int ret;
955
956         if (cam->state != S_NOTREADY)
957                 cam_warn(cam, "Cam init with device in funky state %d",
958                                 cam->state);
959         ret = __mcam_cam_reset(cam);
960         /* Get/set parameters? */
961         cam->state = S_IDLE;
962         mcam_ctlr_power_down(cam);
963         return ret;
964 }
965
966 /*
967  * Configure the sensor to match the parameters we have.  Caller should
968  * hold s_mutex
969  */
970 static int mcam_cam_set_flip(struct mcam_camera *cam)
971 {
972         struct v4l2_control ctrl;
973
974         memset(&ctrl, 0, sizeof(ctrl));
975         ctrl.id = V4L2_CID_VFLIP;
976         ctrl.value = flip;
977         return sensor_call(cam, core, s_ctrl, &ctrl);
978 }
979
980
981 static int mcam_cam_configure(struct mcam_camera *cam)
982 {
983         struct v4l2_subdev_format format = {
984                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
985         };
986         int ret;
987
988         v4l2_fill_mbus_format(&format.format, &cam->pix_format, cam->mbus_code);
989         ret = sensor_call(cam, core, init, 0);
990         if (ret == 0)
991                 ret = sensor_call(cam, pad, set_fmt, NULL, &format);
992         /*
993          * OV7670 does weird things if flip is set *before* format...
994          */
995         ret += mcam_cam_set_flip(cam);
996         return ret;
997 }
998
999 /*
1000  * Get everything ready, and start grabbing frames.
1001  */
1002 static int mcam_read_setup(struct mcam_camera *cam)
1003 {
1004         int ret;
1005         unsigned long flags;
1006
1007         /*
1008          * Configuration.  If we still don't have DMA buffers,
1009          * make one last, desperate attempt.
1010          */
1011         if (cam->buffer_mode == B_vmalloc && cam->nbufs == 0 &&
1012                         mcam_alloc_dma_bufs(cam, 0))
1013                 return -ENOMEM;
1014
1015         if (mcam_needs_config(cam)) {
1016                 mcam_cam_configure(cam);
1017                 ret = mcam_ctlr_configure(cam);
1018                 if (ret)
1019                         return ret;
1020         }
1021
1022         /*
1023          * Turn it loose.
1024          */
1025         spin_lock_irqsave(&cam->dev_lock, flags);
1026         clear_bit(CF_DMA_ACTIVE, &cam->flags);
1027         mcam_reset_buffers(cam);
1028         /*
1029          * Update CSI2_DPHY value
1030          */
1031         if (cam->calc_dphy)
1032                 cam->calc_dphy(cam);
1033         cam_dbg(cam, "camera: DPHY sets: dphy3=0x%x, dphy5=0x%x, dphy6=0x%x\n",
1034                         cam->dphy[0], cam->dphy[1], cam->dphy[2]);
1035         if (cam->bus_type == V4L2_MBUS_CSI2)
1036                 mcam_enable_mipi(cam);
1037         else
1038                 mcam_disable_mipi(cam);
1039         mcam_ctlr_irq_enable(cam);
1040         cam->state = S_STREAMING;
1041         if (!test_bit(CF_SG_RESTART, &cam->flags))
1042                 mcam_ctlr_start(cam);
1043         spin_unlock_irqrestore(&cam->dev_lock, flags);
1044         return 0;
1045 }
1046
1047 /* ----------------------------------------------------------------------- */
1048 /*
1049  * Videobuf2 interface code.
1050  */
1051
1052 static int mcam_vb_queue_setup(struct vb2_queue *vq,
1053                 const void *parg, unsigned int *nbufs,
1054                 unsigned int *num_planes, unsigned int sizes[],
1055                 void *alloc_ctxs[])
1056 {
1057         const struct v4l2_format *fmt = parg;
1058         struct mcam_camera *cam = vb2_get_drv_priv(vq);
1059         int minbufs = (cam->buffer_mode == B_DMA_contig) ? 3 : 2;
1060
1061         if (fmt && fmt->fmt.pix.sizeimage < cam->pix_format.sizeimage)
1062                 return -EINVAL;
1063         sizes[0] = fmt ? fmt->fmt.pix.sizeimage : cam->pix_format.sizeimage;
1064         *num_planes = 1; /* Someday we have to support planar formats... */
1065         if (*nbufs < minbufs)
1066                 *nbufs = minbufs;
1067         if (cam->buffer_mode == B_DMA_contig)
1068                 alloc_ctxs[0] = cam->vb_alloc_ctx;
1069         else if (cam->buffer_mode == B_DMA_sg)
1070                 alloc_ctxs[0] = cam->vb_alloc_ctx_sg;
1071         return 0;
1072 }
1073
1074
1075 static void mcam_vb_buf_queue(struct vb2_buffer *vb)
1076 {
1077         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1078         struct mcam_vb_buffer *mvb = vb_to_mvb(vbuf);
1079         struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
1080         unsigned long flags;
1081         int start;
1082
1083         spin_lock_irqsave(&cam->dev_lock, flags);
1084         start = (cam->state == S_BUFWAIT) && !list_empty(&cam->buffers);
1085         list_add(&mvb->queue, &cam->buffers);
1086         if (cam->state == S_STREAMING && test_bit(CF_SG_RESTART, &cam->flags))
1087                 mcam_sg_restart(cam);
1088         spin_unlock_irqrestore(&cam->dev_lock, flags);
1089         if (start)
1090                 mcam_read_setup(cam);
1091 }
1092
1093 static void mcam_vb_requeue_bufs(struct vb2_queue *vq,
1094                                  enum vb2_buffer_state state)
1095 {
1096         struct mcam_camera *cam = vb2_get_drv_priv(vq);
1097         struct mcam_vb_buffer *buf, *node;
1098         unsigned long flags;
1099         unsigned i;
1100
1101         spin_lock_irqsave(&cam->dev_lock, flags);
1102         list_for_each_entry_safe(buf, node, &cam->buffers, queue) {
1103                 vb2_buffer_done(&buf->vb_buf.vb2_buf, state);
1104                 list_del(&buf->queue);
1105         }
1106         for (i = 0; i < MAX_DMA_BUFS; i++) {
1107                 buf = cam->vb_bufs[i];
1108
1109                 if (buf) {
1110                         vb2_buffer_done(&buf->vb_buf.vb2_buf, state);
1111                         cam->vb_bufs[i] = NULL;
1112                 }
1113         }
1114         spin_unlock_irqrestore(&cam->dev_lock, flags);
1115 }
1116
1117 /*
1118  * These need to be called with the mutex held from vb2
1119  */
1120 static int mcam_vb_start_streaming(struct vb2_queue *vq, unsigned int count)
1121 {
1122         struct mcam_camera *cam = vb2_get_drv_priv(vq);
1123         unsigned int frame;
1124         int ret;
1125
1126         if (cam->state != S_IDLE) {
1127                 mcam_vb_requeue_bufs(vq, VB2_BUF_STATE_QUEUED);
1128                 return -EINVAL;
1129         }
1130         cam->frame_state.frames = 0;
1131         cam->frame_state.singles = 0;
1132         cam->frame_state.delivered = 0;
1133         cam->sequence = 0;
1134         /*
1135          * Videobuf2 sneakily hoards all the buffers and won't
1136          * give them to us until *after* streaming starts.  But
1137          * we can't actually start streaming until we have a
1138          * destination.  So go into a wait state and hope they
1139          * give us buffers soon.
1140          */
1141         if (cam->buffer_mode != B_vmalloc && list_empty(&cam->buffers)) {
1142                 cam->state = S_BUFWAIT;
1143                 return 0;
1144         }
1145
1146         /*
1147          * Ensure clear the left over frame flags
1148          * before every really start streaming
1149          */
1150         for (frame = 0; frame < cam->nbufs; frame++)
1151                 clear_bit(CF_FRAME_SOF0 + frame, &cam->flags);
1152
1153         ret = mcam_read_setup(cam);
1154         if (ret)
1155                 mcam_vb_requeue_bufs(vq, VB2_BUF_STATE_QUEUED);
1156         return ret;
1157 }
1158
1159 static void mcam_vb_stop_streaming(struct vb2_queue *vq)
1160 {
1161         struct mcam_camera *cam = vb2_get_drv_priv(vq);
1162
1163         cam_dbg(cam, "stop_streaming: %d frames, %d singles, %d delivered\n",
1164                         cam->frame_state.frames, cam->frame_state.singles,
1165                         cam->frame_state.delivered);
1166         if (cam->state == S_BUFWAIT) {
1167                 /* They never gave us buffers */
1168                 cam->state = S_IDLE;
1169                 return;
1170         }
1171         if (cam->state != S_STREAMING)
1172                 return;
1173         mcam_ctlr_stop_dma(cam);
1174         /*
1175          * Reset the CCIC PHY after stopping streaming,
1176          * otherwise, the CCIC may be unstable.
1177          */
1178         if (cam->ctlr_reset)
1179                 cam->ctlr_reset(cam);
1180         /*
1181          * VB2 reclaims the buffers, so we need to forget
1182          * about them.
1183          */
1184         mcam_vb_requeue_bufs(vq, VB2_BUF_STATE_ERROR);
1185 }
1186
1187
1188 static const struct vb2_ops mcam_vb2_ops = {
1189         .queue_setup            = mcam_vb_queue_setup,
1190         .buf_queue              = mcam_vb_buf_queue,
1191         .start_streaming        = mcam_vb_start_streaming,
1192         .stop_streaming         = mcam_vb_stop_streaming,
1193         .wait_prepare           = vb2_ops_wait_prepare,
1194         .wait_finish            = vb2_ops_wait_finish,
1195 };
1196
1197
1198 #ifdef MCAM_MODE_DMA_SG
1199 /*
1200  * Scatter/gather mode uses all of the above functions plus a
1201  * few extras to deal with DMA mapping.
1202  */
1203 static int mcam_vb_sg_buf_init(struct vb2_buffer *vb)
1204 {
1205         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1206         struct mcam_vb_buffer *mvb = vb_to_mvb(vbuf);
1207         struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
1208         int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;
1209
1210         mvb->dma_desc = dma_alloc_coherent(cam->dev,
1211                         ndesc * sizeof(struct mcam_dma_desc),
1212                         &mvb->dma_desc_pa, GFP_KERNEL);
1213         if (mvb->dma_desc == NULL) {
1214                 cam_err(cam, "Unable to get DMA descriptor array\n");
1215                 return -ENOMEM;
1216         }
1217         return 0;
1218 }
1219
1220 static int mcam_vb_sg_buf_prepare(struct vb2_buffer *vb)
1221 {
1222         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1223         struct mcam_vb_buffer *mvb = vb_to_mvb(vbuf);
1224         struct sg_table *sg_table = vb2_dma_sg_plane_desc(vb, 0);
1225         struct mcam_dma_desc *desc = mvb->dma_desc;
1226         struct scatterlist *sg;
1227         int i;
1228
1229         for_each_sg(sg_table->sgl, sg, sg_table->nents, i) {
1230                 desc->dma_addr = sg_dma_address(sg);
1231                 desc->segment_len = sg_dma_len(sg);
1232                 desc++;
1233         }
1234         return 0;
1235 }
1236
1237 static void mcam_vb_sg_buf_cleanup(struct vb2_buffer *vb)
1238 {
1239         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1240         struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
1241         struct mcam_vb_buffer *mvb = vb_to_mvb(vbuf);
1242         int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;
1243
1244         dma_free_coherent(cam->dev, ndesc * sizeof(struct mcam_dma_desc),
1245                         mvb->dma_desc, mvb->dma_desc_pa);
1246 }
1247
1248
1249 static const struct vb2_ops mcam_vb2_sg_ops = {
1250         .queue_setup            = mcam_vb_queue_setup,
1251         .buf_init               = mcam_vb_sg_buf_init,
1252         .buf_prepare            = mcam_vb_sg_buf_prepare,
1253         .buf_queue              = mcam_vb_buf_queue,
1254         .buf_cleanup            = mcam_vb_sg_buf_cleanup,
1255         .start_streaming        = mcam_vb_start_streaming,
1256         .stop_streaming         = mcam_vb_stop_streaming,
1257         .wait_prepare           = vb2_ops_wait_prepare,
1258         .wait_finish            = vb2_ops_wait_finish,
1259 };
1260
1261 #endif /* MCAM_MODE_DMA_SG */
1262
1263 static int mcam_setup_vb2(struct mcam_camera *cam)
1264 {
1265         struct vb2_queue *vq = &cam->vb_queue;
1266
1267         memset(vq, 0, sizeof(*vq));
1268         vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1269         vq->drv_priv = cam;
1270         vq->lock = &cam->s_mutex;
1271         vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1272         vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1273         vq->buf_struct_size = sizeof(struct mcam_vb_buffer);
1274         INIT_LIST_HEAD(&cam->buffers);
1275         switch (cam->buffer_mode) {
1276         case B_DMA_contig:
1277 #ifdef MCAM_MODE_DMA_CONTIG
1278                 vq->ops = &mcam_vb2_ops;
1279                 vq->mem_ops = &vb2_dma_contig_memops;
1280                 cam->dma_setup = mcam_ctlr_dma_contig;
1281                 cam->frame_complete = mcam_dma_contig_done;
1282                 cam->vb_alloc_ctx = vb2_dma_contig_init_ctx(cam->dev);
1283                 if (IS_ERR(cam->vb_alloc_ctx))
1284                         return PTR_ERR(cam->vb_alloc_ctx);
1285 #endif
1286                 break;
1287         case B_DMA_sg:
1288 #ifdef MCAM_MODE_DMA_SG
1289                 vq->ops = &mcam_vb2_sg_ops;
1290                 vq->mem_ops = &vb2_dma_sg_memops;
1291                 cam->dma_setup = mcam_ctlr_dma_sg;
1292                 cam->frame_complete = mcam_dma_sg_done;
1293                 cam->vb_alloc_ctx_sg = vb2_dma_sg_init_ctx(cam->dev);
1294                 if (IS_ERR(cam->vb_alloc_ctx_sg))
1295                         return PTR_ERR(cam->vb_alloc_ctx_sg);
1296 #endif
1297                 break;
1298         case B_vmalloc:
1299 #ifdef MCAM_MODE_VMALLOC
1300                 tasklet_init(&cam->s_tasklet, mcam_frame_tasklet,
1301                                 (unsigned long) cam);
1302                 vq->ops = &mcam_vb2_ops;
1303                 vq->mem_ops = &vb2_vmalloc_memops;
1304                 cam->dma_setup = mcam_ctlr_dma_vmalloc;
1305                 cam->frame_complete = mcam_vmalloc_done;
1306 #endif
1307                 break;
1308         }
1309         return vb2_queue_init(vq);
1310 }
1311
1312 static void mcam_cleanup_vb2(struct mcam_camera *cam)
1313 {
1314 #ifdef MCAM_MODE_DMA_CONTIG
1315         if (cam->buffer_mode == B_DMA_contig)
1316                 vb2_dma_contig_cleanup_ctx(cam->vb_alloc_ctx);
1317 #endif
1318 #ifdef MCAM_MODE_DMA_SG
1319         if (cam->buffer_mode == B_DMA_sg)
1320                 vb2_dma_sg_cleanup_ctx(cam->vb_alloc_ctx_sg);
1321 #endif
1322 }
1323
1324
1325 /* ---------------------------------------------------------------------- */
1326 /*
1327  * The long list of V4L2 ioctl() operations.
1328  */
1329
1330 static int mcam_vidioc_querycap(struct file *file, void *priv,
1331                 struct v4l2_capability *cap)
1332 {
1333         struct mcam_camera *cam = video_drvdata(file);
1334
1335         strcpy(cap->driver, "marvell_ccic");
1336         strcpy(cap->card, "marvell_ccic");
1337         strlcpy(cap->bus_info, cam->bus_info, sizeof(cap->bus_info));
1338         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
1339                 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1340         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1341         return 0;
1342 }
1343
1344
1345 static int mcam_vidioc_enum_fmt_vid_cap(struct file *filp,
1346                 void *priv, struct v4l2_fmtdesc *fmt)
1347 {
1348         if (fmt->index >= N_MCAM_FMTS)
1349                 return -EINVAL;
1350         strlcpy(fmt->description, mcam_formats[fmt->index].desc,
1351                         sizeof(fmt->description));
1352         fmt->pixelformat = mcam_formats[fmt->index].pixelformat;
1353         return 0;
1354 }
1355
1356 static int mcam_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
1357                 struct v4l2_format *fmt)
1358 {
1359         struct mcam_camera *cam = video_drvdata(filp);
1360         struct mcam_format_struct *f;
1361         struct v4l2_pix_format *pix = &fmt->fmt.pix;
1362         struct v4l2_subdev_pad_config pad_cfg;
1363         struct v4l2_subdev_format format = {
1364                 .which = V4L2_SUBDEV_FORMAT_TRY,
1365         };
1366         int ret;
1367
1368         f = mcam_find_format(pix->pixelformat);
1369         pix->pixelformat = f->pixelformat;
1370         v4l2_fill_mbus_format(&format.format, pix, f->mbus_code);
1371         ret = sensor_call(cam, pad, set_fmt, &pad_cfg, &format);
1372         v4l2_fill_pix_format(pix, &format.format);
1373         pix->bytesperline = pix->width * f->bpp;
1374         switch (f->pixelformat) {
1375         case V4L2_PIX_FMT_YUV420:
1376         case V4L2_PIX_FMT_YVU420:
1377                 pix->sizeimage = pix->height * pix->bytesperline * 3 / 2;
1378                 break;
1379         default:
1380                 pix->sizeimage = pix->height * pix->bytesperline;
1381                 break;
1382         }
1383         pix->colorspace = V4L2_COLORSPACE_SRGB;
1384         return ret;
1385 }
1386
1387 static int mcam_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
1388                 struct v4l2_format *fmt)
1389 {
1390         struct mcam_camera *cam = video_drvdata(filp);
1391         struct mcam_format_struct *f;
1392         int ret;
1393
1394         /*
1395          * Can't do anything if the device is not idle
1396          * Also can't if there are streaming buffers in place.
1397          */
1398         if (cam->state != S_IDLE || vb2_is_busy(&cam->vb_queue))
1399                 return -EBUSY;
1400
1401         f = mcam_find_format(fmt->fmt.pix.pixelformat);
1402
1403         /*
1404          * See if the formatting works in principle.
1405          */
1406         ret = mcam_vidioc_try_fmt_vid_cap(filp, priv, fmt);
1407         if (ret)
1408                 return ret;
1409         /*
1410          * Now we start to change things for real, so let's do it
1411          * under lock.
1412          */
1413         cam->pix_format = fmt->fmt.pix;
1414         cam->mbus_code = f->mbus_code;
1415
1416         /*
1417          * Make sure we have appropriate DMA buffers.
1418          */
1419         if (cam->buffer_mode == B_vmalloc) {
1420                 ret = mcam_check_dma_buffers(cam);
1421                 if (ret)
1422                         goto out;
1423         }
1424         mcam_set_config_needed(cam, 1);
1425 out:
1426         return ret;
1427 }
1428
1429 /*
1430  * Return our stored notion of how the camera is/should be configured.
1431  * The V4l2 spec wants us to be smarter, and actually get this from
1432  * the camera (and not mess with it at open time).  Someday.
1433  */
1434 static int mcam_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
1435                 struct v4l2_format *f)
1436 {
1437         struct mcam_camera *cam = video_drvdata(filp);
1438
1439         f->fmt.pix = cam->pix_format;
1440         return 0;
1441 }
1442
1443 /*
1444  * We only have one input - the sensor - so minimize the nonsense here.
1445  */
1446 static int mcam_vidioc_enum_input(struct file *filp, void *priv,
1447                 struct v4l2_input *input)
1448 {
1449         if (input->index != 0)
1450                 return -EINVAL;
1451
1452         input->type = V4L2_INPUT_TYPE_CAMERA;
1453         strcpy(input->name, "Camera");
1454         return 0;
1455 }
1456
1457 static int mcam_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1458 {
1459         *i = 0;
1460         return 0;
1461 }
1462
1463 static int mcam_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1464 {
1465         if (i != 0)
1466                 return -EINVAL;
1467         return 0;
1468 }
1469
1470 /*
1471  * G/S_PARM.  Most of this is done by the sensor, but we are
1472  * the level which controls the number of read buffers.
1473  */
1474 static int mcam_vidioc_g_parm(struct file *filp, void *priv,
1475                 struct v4l2_streamparm *parms)
1476 {
1477         struct mcam_camera *cam = video_drvdata(filp);
1478         int ret;
1479
1480         ret = sensor_call(cam, video, g_parm, parms);
1481         parms->parm.capture.readbuffers = n_dma_bufs;
1482         return ret;
1483 }
1484
1485 static int mcam_vidioc_s_parm(struct file *filp, void *priv,
1486                 struct v4l2_streamparm *parms)
1487 {
1488         struct mcam_camera *cam = video_drvdata(filp);
1489         int ret;
1490
1491         ret = sensor_call(cam, video, s_parm, parms);
1492         parms->parm.capture.readbuffers = n_dma_bufs;
1493         return ret;
1494 }
1495
1496 static int mcam_vidioc_enum_framesizes(struct file *filp, void *priv,
1497                 struct v4l2_frmsizeenum *sizes)
1498 {
1499         struct mcam_camera *cam = video_drvdata(filp);
1500         struct mcam_format_struct *f;
1501         struct v4l2_subdev_frame_size_enum fse = {
1502                 .index = sizes->index,
1503                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1504         };
1505         int ret;
1506
1507         f = mcam_find_format(sizes->pixel_format);
1508         if (f->pixelformat != sizes->pixel_format)
1509                 return -EINVAL;
1510         fse.code = f->mbus_code;
1511         ret = sensor_call(cam, pad, enum_frame_size, NULL, &fse);
1512         if (ret)
1513                 return ret;
1514         if (fse.min_width == fse.max_width &&
1515             fse.min_height == fse.max_height) {
1516                 sizes->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1517                 sizes->discrete.width = fse.min_width;
1518                 sizes->discrete.height = fse.min_height;
1519                 return 0;
1520         }
1521         sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1522         sizes->stepwise.min_width = fse.min_width;
1523         sizes->stepwise.max_width = fse.max_width;
1524         sizes->stepwise.min_height = fse.min_height;
1525         sizes->stepwise.max_height = fse.max_height;
1526         sizes->stepwise.step_width = 1;
1527         sizes->stepwise.step_height = 1;
1528         return 0;
1529 }
1530
1531 static int mcam_vidioc_enum_frameintervals(struct file *filp, void *priv,
1532                 struct v4l2_frmivalenum *interval)
1533 {
1534         struct mcam_camera *cam = video_drvdata(filp);
1535         struct mcam_format_struct *f;
1536         struct v4l2_subdev_frame_interval_enum fie = {
1537                 .index = interval->index,
1538                 .width = interval->width,
1539                 .height = interval->height,
1540                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1541         };
1542         int ret;
1543
1544         f = mcam_find_format(interval->pixel_format);
1545         if (f->pixelformat != interval->pixel_format)
1546                 return -EINVAL;
1547         fie.code = f->mbus_code;
1548         ret = sensor_call(cam, pad, enum_frame_interval, NULL, &fie);
1549         if (ret)
1550                 return ret;
1551         interval->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1552         interval->discrete = fie.interval;
1553         return 0;
1554 }
1555
1556 #ifdef CONFIG_VIDEO_ADV_DEBUG
1557 static int mcam_vidioc_g_register(struct file *file, void *priv,
1558                 struct v4l2_dbg_register *reg)
1559 {
1560         struct mcam_camera *cam = video_drvdata(file);
1561
1562         if (reg->reg > cam->regs_size - 4)
1563                 return -EINVAL;
1564         reg->val = mcam_reg_read(cam, reg->reg);
1565         reg->size = 4;
1566         return 0;
1567 }
1568
1569 static int mcam_vidioc_s_register(struct file *file, void *priv,
1570                 const struct v4l2_dbg_register *reg)
1571 {
1572         struct mcam_camera *cam = video_drvdata(file);
1573
1574         if (reg->reg > cam->regs_size - 4)
1575                 return -EINVAL;
1576         mcam_reg_write(cam, reg->reg, reg->val);
1577         return 0;
1578 }
1579 #endif
1580
1581 static const struct v4l2_ioctl_ops mcam_v4l_ioctl_ops = {
1582         .vidioc_querycap        = mcam_vidioc_querycap,
1583         .vidioc_enum_fmt_vid_cap = mcam_vidioc_enum_fmt_vid_cap,
1584         .vidioc_try_fmt_vid_cap = mcam_vidioc_try_fmt_vid_cap,
1585         .vidioc_s_fmt_vid_cap   = mcam_vidioc_s_fmt_vid_cap,
1586         .vidioc_g_fmt_vid_cap   = mcam_vidioc_g_fmt_vid_cap,
1587         .vidioc_enum_input      = mcam_vidioc_enum_input,
1588         .vidioc_g_input         = mcam_vidioc_g_input,
1589         .vidioc_s_input         = mcam_vidioc_s_input,
1590         .vidioc_reqbufs         = vb2_ioctl_reqbufs,
1591         .vidioc_create_bufs     = vb2_ioctl_create_bufs,
1592         .vidioc_querybuf        = vb2_ioctl_querybuf,
1593         .vidioc_qbuf            = vb2_ioctl_qbuf,
1594         .vidioc_dqbuf           = vb2_ioctl_dqbuf,
1595         .vidioc_expbuf          = vb2_ioctl_expbuf,
1596         .vidioc_streamon        = vb2_ioctl_streamon,
1597         .vidioc_streamoff       = vb2_ioctl_streamoff,
1598         .vidioc_g_parm          = mcam_vidioc_g_parm,
1599         .vidioc_s_parm          = mcam_vidioc_s_parm,
1600         .vidioc_enum_framesizes = mcam_vidioc_enum_framesizes,
1601         .vidioc_enum_frameintervals = mcam_vidioc_enum_frameintervals,
1602         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1603         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1604 #ifdef CONFIG_VIDEO_ADV_DEBUG
1605         .vidioc_g_register      = mcam_vidioc_g_register,
1606         .vidioc_s_register      = mcam_vidioc_s_register,
1607 #endif
1608 };
1609
1610 /* ---------------------------------------------------------------------- */
1611 /*
1612  * Our various file operations.
1613  */
1614 static int mcam_v4l_open(struct file *filp)
1615 {
1616         struct mcam_camera *cam = video_drvdata(filp);
1617         int ret;
1618
1619         mutex_lock(&cam->s_mutex);
1620         ret = v4l2_fh_open(filp);
1621         if (ret)
1622                 goto out;
1623         if (v4l2_fh_is_singular_file(filp)) {
1624                 ret = mcam_ctlr_power_up(cam);
1625                 if (ret)
1626                         goto out;
1627                 __mcam_cam_reset(cam);
1628                 mcam_set_config_needed(cam, 1);
1629         }
1630 out:
1631         mutex_unlock(&cam->s_mutex);
1632         if (ret)
1633                 v4l2_fh_release(filp);
1634         return ret;
1635 }
1636
1637
1638 static int mcam_v4l_release(struct file *filp)
1639 {
1640         struct mcam_camera *cam = video_drvdata(filp);
1641         bool last_open;
1642
1643         mutex_lock(&cam->s_mutex);
1644         last_open = v4l2_fh_is_singular_file(filp);
1645         _vb2_fop_release(filp, NULL);
1646         if (last_open) {
1647                 mcam_disable_mipi(cam);
1648                 mcam_ctlr_power_down(cam);
1649                 if (cam->buffer_mode == B_vmalloc && alloc_bufs_at_read)
1650                         mcam_free_dma_bufs(cam);
1651         }
1652
1653         mutex_unlock(&cam->s_mutex);
1654         return 0;
1655 }
1656
1657 static const struct v4l2_file_operations mcam_v4l_fops = {
1658         .owner = THIS_MODULE,
1659         .open = mcam_v4l_open,
1660         .release = mcam_v4l_release,
1661         .read = vb2_fop_read,
1662         .poll = vb2_fop_poll,
1663         .mmap = vb2_fop_mmap,
1664         .unlocked_ioctl = video_ioctl2,
1665 };
1666
1667
1668 /*
1669  * This template device holds all of those v4l2 methods; we
1670  * clone it for specific real devices.
1671  */
1672 static struct video_device mcam_v4l_template = {
1673         .name = "mcam",
1674         .fops = &mcam_v4l_fops,
1675         .ioctl_ops = &mcam_v4l_ioctl_ops,
1676         .release = video_device_release_empty,
1677 };
1678
1679 /* ---------------------------------------------------------------------- */
1680 /*
1681  * Interrupt handler stuff
1682  */
1683 static void mcam_frame_complete(struct mcam_camera *cam, int frame)
1684 {
1685         /*
1686          * Basic frame housekeeping.
1687          */
1688         set_bit(frame, &cam->flags);
1689         clear_bit(CF_DMA_ACTIVE, &cam->flags);
1690         cam->next_buf = frame;
1691         cam->buf_seq[frame] = cam->sequence++;
1692         cam->frame_state.frames++;
1693         /*
1694          * "This should never happen"
1695          */
1696         if (cam->state != S_STREAMING)
1697                 return;
1698         /*
1699          * Process the frame and set up the next one.
1700          */
1701         cam->frame_complete(cam, frame);
1702 }
1703
1704
1705 /*
1706  * The interrupt handler; this needs to be called from the
1707  * platform irq handler with the lock held.
1708  */
1709 int mccic_irq(struct mcam_camera *cam, unsigned int irqs)
1710 {
1711         unsigned int frame, handled = 0;
1712
1713         mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1714         /*
1715          * Handle any frame completions.  There really should
1716          * not be more than one of these, or we have fallen
1717          * far behind.
1718          *
1719          * When running in S/G mode, the frame number lacks any
1720          * real meaning - there's only one descriptor array - but
1721          * the controller still picks a different one to signal
1722          * each time.
1723          */
1724         for (frame = 0; frame < cam->nbufs; frame++)
1725                 if (irqs & (IRQ_EOF0 << frame) &&
1726                         test_bit(CF_FRAME_SOF0 + frame, &cam->flags)) {
1727                         mcam_frame_complete(cam, frame);
1728                         handled = 1;
1729                         clear_bit(CF_FRAME_SOF0 + frame, &cam->flags);
1730                         if (cam->buffer_mode == B_DMA_sg)
1731                                 break;
1732                 }
1733         /*
1734          * If a frame starts, note that we have DMA active.  This
1735          * code assumes that we won't get multiple frame interrupts
1736          * at once; may want to rethink that.
1737          */
1738         for (frame = 0; frame < cam->nbufs; frame++) {
1739                 if (irqs & (IRQ_SOF0 << frame)) {
1740                         set_bit(CF_FRAME_SOF0 + frame, &cam->flags);
1741                         handled = IRQ_HANDLED;
1742                 }
1743         }
1744
1745         if (handled == IRQ_HANDLED) {
1746                 set_bit(CF_DMA_ACTIVE, &cam->flags);
1747                 if (cam->buffer_mode == B_DMA_sg)
1748                         mcam_ctlr_stop(cam);
1749         }
1750         return handled;
1751 }
1752
1753 /* ---------------------------------------------------------------------- */
1754 /*
1755  * Registration and such.
1756  */
1757 static struct ov7670_config sensor_cfg = {
1758         /*
1759          * Exclude QCIF mode, because it only captures a tiny portion
1760          * of the sensor FOV
1761          */
1762         .min_width = 320,
1763         .min_height = 240,
1764 };
1765
1766
1767 int mccic_register(struct mcam_camera *cam)
1768 {
1769         struct i2c_board_info ov7670_info = {
1770                 .type = "ov7670",
1771                 .addr = 0x42 >> 1,
1772                 .platform_data = &sensor_cfg,
1773         };
1774         int ret;
1775
1776         /*
1777          * Validate the requested buffer mode.
1778          */
1779         if (buffer_mode >= 0)
1780                 cam->buffer_mode = buffer_mode;
1781         if (cam->buffer_mode == B_DMA_sg &&
1782                         cam->chip_id == MCAM_CAFE) {
1783                 printk(KERN_ERR "marvell-cam: Cafe can't do S/G I/O, "
1784                         "attempting vmalloc mode instead\n");
1785                 cam->buffer_mode = B_vmalloc;
1786         }
1787         if (!mcam_buffer_mode_supported(cam->buffer_mode)) {
1788                 printk(KERN_ERR "marvell-cam: buffer mode %d unsupported\n",
1789                                 cam->buffer_mode);
1790                 return -EINVAL;
1791         }
1792         /*
1793          * Register with V4L
1794          */
1795         ret = v4l2_device_register(cam->dev, &cam->v4l2_dev);
1796         if (ret)
1797                 return ret;
1798
1799         mutex_init(&cam->s_mutex);
1800         cam->state = S_NOTREADY;
1801         mcam_set_config_needed(cam, 1);
1802         cam->pix_format = mcam_def_pix_format;
1803         cam->mbus_code = mcam_def_mbus_code;
1804         mcam_ctlr_init(cam);
1805
1806         /*
1807          * Get the v4l2 setup done.
1808          */
1809         ret = v4l2_ctrl_handler_init(&cam->ctrl_handler, 10);
1810         if (ret)
1811                 goto out_unregister;
1812         cam->v4l2_dev.ctrl_handler = &cam->ctrl_handler;
1813
1814         /*
1815          * Try to find the sensor.
1816          */
1817         sensor_cfg.clock_speed = cam->clock_speed;
1818         sensor_cfg.use_smbus = cam->use_smbus;
1819         cam->sensor_addr = ov7670_info.addr;
1820         cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev,
1821                         cam->i2c_adapter, &ov7670_info, NULL);
1822         if (cam->sensor == NULL) {
1823                 ret = -ENODEV;
1824                 goto out_unregister;
1825         }
1826
1827         ret = mcam_cam_init(cam);
1828         if (ret)
1829                 goto out_unregister;
1830
1831         ret = mcam_setup_vb2(cam);
1832         if (ret)
1833                 goto out_unregister;
1834
1835         mutex_lock(&cam->s_mutex);
1836         cam->vdev = mcam_v4l_template;
1837         cam->vdev.v4l2_dev = &cam->v4l2_dev;
1838         cam->vdev.lock = &cam->s_mutex;
1839         cam->vdev.queue = &cam->vb_queue;
1840         video_set_drvdata(&cam->vdev, cam);
1841         ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1842         if (ret) {
1843                 mutex_unlock(&cam->s_mutex);
1844                 goto out_unregister;
1845         }
1846
1847         /*
1848          * If so requested, try to get our DMA buffers now.
1849          */
1850         if (cam->buffer_mode == B_vmalloc && !alloc_bufs_at_read) {
1851                 if (mcam_alloc_dma_bufs(cam, 1))
1852                         cam_warn(cam, "Unable to alloc DMA buffers at load"
1853                                         " will try again later.");
1854         }
1855
1856         mutex_unlock(&cam->s_mutex);
1857         return 0;
1858
1859 out_unregister:
1860         v4l2_ctrl_handler_free(&cam->ctrl_handler);
1861         v4l2_device_unregister(&cam->v4l2_dev);
1862         return ret;
1863 }
1864
1865
1866 void mccic_shutdown(struct mcam_camera *cam)
1867 {
1868         /*
1869          * If we have no users (and we really, really should have no
1870          * users) the device will already be powered down.  Trying to
1871          * take it down again will wedge the machine, which is frowned
1872          * upon.
1873          */
1874         if (!list_empty(&cam->vdev.fh_list)) {
1875                 cam_warn(cam, "Removing a device with users!\n");
1876                 mcam_ctlr_power_down(cam);
1877         }
1878         mcam_cleanup_vb2(cam);
1879         if (cam->buffer_mode == B_vmalloc)
1880                 mcam_free_dma_bufs(cam);
1881         video_unregister_device(&cam->vdev);
1882         v4l2_ctrl_handler_free(&cam->ctrl_handler);
1883         v4l2_device_unregister(&cam->v4l2_dev);
1884 }
1885
1886 /*
1887  * Power management
1888  */
1889 #ifdef CONFIG_PM
1890
1891 void mccic_suspend(struct mcam_camera *cam)
1892 {
1893         mutex_lock(&cam->s_mutex);
1894         if (!list_empty(&cam->vdev.fh_list)) {
1895                 enum mcam_state cstate = cam->state;
1896
1897                 mcam_ctlr_stop_dma(cam);
1898                 mcam_ctlr_power_down(cam);
1899                 cam->state = cstate;
1900         }
1901         mutex_unlock(&cam->s_mutex);
1902 }
1903
1904 int mccic_resume(struct mcam_camera *cam)
1905 {
1906         int ret = 0;
1907
1908         mutex_lock(&cam->s_mutex);
1909         if (!list_empty(&cam->vdev.fh_list)) {
1910                 ret = mcam_ctlr_power_up(cam);
1911                 if (ret) {
1912                         mutex_unlock(&cam->s_mutex);
1913                         return ret;
1914                 }
1915                 __mcam_cam_reset(cam);
1916         } else {
1917                 mcam_ctlr_power_down(cam);
1918         }
1919         mutex_unlock(&cam->s_mutex);
1920
1921         set_bit(CF_CONFIG_NEEDED, &cam->flags);
1922         if (cam->state == S_STREAMING) {
1923                 /*
1924                  * If there was a buffer in the DMA engine at suspend
1925                  * time, put it back on the queue or we'll forget about it.
1926                  */
1927                 if (cam->buffer_mode == B_DMA_sg && cam->vb_bufs[0])
1928                         list_add(&cam->vb_bufs[0]->queue, &cam->buffers);
1929                 ret = mcam_read_setup(cam);
1930         }
1931         return ret;
1932 }
1933 #endif /* CONFIG_PM */