blob: 050724f8d3e68e31efbf4baf2faf9f3234f2fe55 [file] [log] [blame]
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001/*
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>
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030010#include <linux/mm.h>
11#include <linux/i2c.h>
12#include <linux/interrupt.h>
13#include <linux/spinlock.h>
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030014#include <linux/slab.h>
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030015#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>
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030020#include <linux/vmalloc.h>
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030021#include <linux/io.h>
Jonathan Corbet362d45b2011-06-20 16:14:37 -030022#include <linux/videodev2.h>
23#include <media/v4l2-device.h>
24#include <media/v4l2-ioctl.h>
25#include <media/v4l2-chip-ident.h>
26#include <media/ov7670.h>
27#include <media/videobuf2-vmalloc.h>
Jonathan Corbeta9b36e82011-06-20 16:14:40 -030028#include <media/videobuf2-dma-contig.h>
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -030029#include <media/videobuf2-dma-sg.h>
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030030
31#include "mcam-core.h"
32
Jonathan Corbeta9b36e82011-06-20 16:14:40 -030033/*
34 * Basic frame stats - to be deleted shortly
35 */
36static int frames;
37static int singles;
38static int delivered;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030039
Jonathan Corbet74984692011-07-08 17:50:49 -030040#ifdef MCAM_MODE_VMALLOC
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030041/*
42 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
43 * we must have physically contiguous buffers to bring frames into.
44 * These parameters control how many buffers we use, whether we
45 * allocate them at load time (better chance of success, but nails down
46 * memory) or when somebody tries to use the camera (riskier), and,
47 * for load-time allocation, how big they should be.
48 *
49 * The controller can cycle through three buffers. We could use
50 * more by flipping pointers around, but it probably makes little
51 * sense.
52 */
53
Rusty Russell90ab5ee2012-01-13 09:32:20 +103054static bool alloc_bufs_at_read;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030055module_param(alloc_bufs_at_read, bool, 0444);
56MODULE_PARM_DESC(alloc_bufs_at_read,
57 "Non-zero value causes DMA buffers to be allocated when the "
58 "video capture device is read, rather than at module load "
59 "time. This saves memory, but decreases the chances of "
Jonathan Corbeta9b36e82011-06-20 16:14:40 -030060 "successfully getting those buffers. This parameter is "
61 "only used in the vmalloc buffer mode");
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030062
63static int n_dma_bufs = 3;
64module_param(n_dma_bufs, uint, 0644);
65MODULE_PARM_DESC(n_dma_bufs,
66 "The number of DMA buffers to allocate. Can be either two "
67 "(saves memory, makes timing tighter) or three.");
68
69static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
70module_param(dma_buf_size, uint, 0444);
71MODULE_PARM_DESC(dma_buf_size,
72 "The size of the allocated DMA buffers. If actual operating "
73 "parameters require larger buffers, an attempt to reallocate "
74 "will be made.");
Jonathan Corbet74984692011-07-08 17:50:49 -030075#else /* MCAM_MODE_VMALLOC */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103076static const bool alloc_bufs_at_read = 0;
Jonathan Corbet74984692011-07-08 17:50:49 -030077static const int n_dma_bufs = 3; /* Used by S/G_PARM */
78#endif /* MCAM_MODE_VMALLOC */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030079
Rusty Russell90ab5ee2012-01-13 09:32:20 +103080static bool flip;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030081module_param(flip, bool, 0444);
82MODULE_PARM_DESC(flip,
83 "If set, the sensor will be instructed to flip the image "
84 "vertically.");
85
Jonathan Corbeta9b36e82011-06-20 16:14:40 -030086static int buffer_mode = -1;
87module_param(buffer_mode, int, 0444);
88MODULE_PARM_DESC(buffer_mode,
89 "Set the buffer mode to be used; default is to go with what "
90 "the platform driver asks for. Set to 0 for vmalloc, 1 for "
91 "DMA contiguous.");
92
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030093/*
94 * Status flags. Always manipulated with bit operations.
95 */
96#define CF_BUF0_VALID 0 /* Buffers valid - first three */
97#define CF_BUF1_VALID 1
98#define CF_BUF2_VALID 2
99#define CF_DMA_ACTIVE 3 /* A frame is incoming */
100#define CF_CONFIG_NEEDED 4 /* Must configure hardware */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300101#define CF_SINGLE_BUFFER 5 /* Running with a single buffer */
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300102#define CF_SG_RESTART 6 /* SG restart needed */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300103
104#define sensor_call(cam, o, f, args...) \
105 v4l2_subdev_call(cam->sensor, o, f, ##args)
106
107static struct mcam_format_struct {
108 __u8 *desc;
109 __u32 pixelformat;
110 int bpp; /* Bytes per pixel */
111 enum v4l2_mbus_pixelcode mbus_code;
112} mcam_formats[] = {
113 {
114 .desc = "YUYV 4:2:2",
115 .pixelformat = V4L2_PIX_FMT_YUYV,
116 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
117 .bpp = 2,
118 },
119 {
120 .desc = "RGB 444",
121 .pixelformat = V4L2_PIX_FMT_RGB444,
122 .mbus_code = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE,
123 .bpp = 2,
124 },
125 {
126 .desc = "RGB 565",
127 .pixelformat = V4L2_PIX_FMT_RGB565,
128 .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE,
129 .bpp = 2,
130 },
131 {
132 .desc = "Raw RGB Bayer",
133 .pixelformat = V4L2_PIX_FMT_SBGGR8,
134 .mbus_code = V4L2_MBUS_FMT_SBGGR8_1X8,
135 .bpp = 1
136 },
137};
138#define N_MCAM_FMTS ARRAY_SIZE(mcam_formats)
139
140static struct mcam_format_struct *mcam_find_format(u32 pixelformat)
141{
142 unsigned i;
143
144 for (i = 0; i < N_MCAM_FMTS; i++)
145 if (mcam_formats[i].pixelformat == pixelformat)
146 return mcam_formats + i;
147 /* Not found? Then return the first format. */
148 return mcam_formats;
149}
150
151/*
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300152 * The default format we use until somebody says otherwise.
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300153 */
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300154static const struct v4l2_pix_format mcam_def_pix_format = {
155 .width = VGA_WIDTH,
156 .height = VGA_HEIGHT,
157 .pixelformat = V4L2_PIX_FMT_YUYV,
158 .field = V4L2_FIELD_NONE,
159 .bytesperline = VGA_WIDTH*2,
160 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
161};
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300162
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300163static const enum v4l2_mbus_pixelcode mcam_def_mbus_code =
164 V4L2_MBUS_FMT_YUYV8_2X8;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300165
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300166
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300167/*
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300168 * The two-word DMA descriptor format used by the Armada 610 and like. There
169 * Is a three-word format as well (set C1_DESC_3WORD) where the third
170 * word is a pointer to the next descriptor, but we don't use it. Two-word
171 * descriptors have to be contiguous in memory.
172 */
173struct mcam_dma_desc {
174 u32 dma_addr;
175 u32 segment_len;
176};
177
178/*
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300179 * Our buffer type for working with videobuf2. Note that the vb2
180 * developers have decreed that struct vb2_buffer must be at the
181 * beginning of this structure.
182 */
183struct mcam_vb_buffer {
184 struct vb2_buffer vb_buf;
185 struct list_head queue;
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300186 struct mcam_dma_desc *dma_desc; /* Descriptor virtual address */
187 dma_addr_t dma_desc_pa; /* Descriptor physical address */
188 int dma_desc_nent; /* Number of mapped descriptors */
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300189};
190
191static inline struct mcam_vb_buffer *vb_to_mvb(struct vb2_buffer *vb)
192{
193 return container_of(vb, struct mcam_vb_buffer, vb_buf);
194}
195
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300196/*
197 * Hand a completed buffer back to user space.
198 */
199static void mcam_buffer_done(struct mcam_camera *cam, int frame,
200 struct vb2_buffer *vbuf)
201{
202 vbuf->v4l2_buf.bytesused = cam->pix_format.sizeimage;
203 vbuf->v4l2_buf.sequence = cam->buf_seq[frame];
204 vb2_set_plane_payload(vbuf, 0, cam->pix_format.sizeimage);
205 vb2_buffer_done(vbuf, VB2_BUF_STATE_DONE);
206}
207
208
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300209
210/*
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300211 * Debugging and related.
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300212 */
213#define cam_err(cam, fmt, arg...) \
214 dev_err((cam)->dev, fmt, ##arg);
215#define cam_warn(cam, fmt, arg...) \
216 dev_warn((cam)->dev, fmt, ##arg);
217#define cam_dbg(cam, fmt, arg...) \
218 dev_dbg((cam)->dev, fmt, ##arg);
219
220
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300221/*
222 * Flag manipulation helpers
223 */
224static void mcam_reset_buffers(struct mcam_camera *cam)
225{
226 int i;
227
228 cam->next_buf = -1;
229 for (i = 0; i < cam->nbufs; i++)
230 clear_bit(i, &cam->flags);
231}
232
233static inline int mcam_needs_config(struct mcam_camera *cam)
234{
235 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
236}
237
238static void mcam_set_config_needed(struct mcam_camera *cam, int needed)
239{
240 if (needed)
241 set_bit(CF_CONFIG_NEEDED, &cam->flags);
242 else
243 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
244}
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300245
246/* ------------------------------------------------------------------- */
247/*
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300248 * Make the controller start grabbing images. Everything must
249 * be set up before doing this.
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300250 */
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300251static void mcam_ctlr_start(struct mcam_camera *cam)
252{
253 /* set_bit performs a read, so no other barrier should be
254 needed here */
255 mcam_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
256}
257
258static void mcam_ctlr_stop(struct mcam_camera *cam)
259{
260 mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
261}
262
263/* ------------------------------------------------------------------- */
Jonathan Corbet74984692011-07-08 17:50:49 -0300264
265#ifdef MCAM_MODE_VMALLOC
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300266/*
267 * Code specific to the vmalloc buffer mode.
268 */
269
270/*
271 * Allocate in-kernel DMA buffers for vmalloc mode.
272 */
273static int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
274{
275 int i;
276
277 mcam_set_config_needed(cam, 1);
278 if (loadtime)
279 cam->dma_buf_size = dma_buf_size;
280 else
281 cam->dma_buf_size = cam->pix_format.sizeimage;
282 if (n_dma_bufs > 3)
283 n_dma_bufs = 3;
284
285 cam->nbufs = 0;
286 for (i = 0; i < n_dma_bufs; i++) {
287 cam->dma_bufs[i] = dma_alloc_coherent(cam->dev,
288 cam->dma_buf_size, cam->dma_handles + i,
289 GFP_KERNEL);
290 if (cam->dma_bufs[i] == NULL) {
291 cam_warn(cam, "Failed to allocate DMA buffer\n");
292 break;
293 }
294 (cam->nbufs)++;
295 }
296
297 switch (cam->nbufs) {
298 case 1:
299 dma_free_coherent(cam->dev, cam->dma_buf_size,
300 cam->dma_bufs[0], cam->dma_handles[0]);
301 cam->nbufs = 0;
302 case 0:
303 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
304 return -ENOMEM;
305
306 case 2:
307 if (n_dma_bufs > 2)
308 cam_warn(cam, "Will limp along with only 2 buffers\n");
309 break;
310 }
311 return 0;
312}
313
314static void mcam_free_dma_bufs(struct mcam_camera *cam)
315{
316 int i;
317
318 for (i = 0; i < cam->nbufs; i++) {
319 dma_free_coherent(cam->dev, cam->dma_buf_size,
320 cam->dma_bufs[i], cam->dma_handles[i]);
321 cam->dma_bufs[i] = NULL;
322 }
323 cam->nbufs = 0;
324}
325
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300326
327/*
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300328 * Set up DMA buffers when operating in vmalloc mode
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300329 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300330static void mcam_ctlr_dma_vmalloc(struct mcam_camera *cam)
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300331{
332 /*
333 * Store the first two Y buffers (we aren't supporting
334 * planar formats for now, so no UV bufs). Then either
335 * set the third if it exists, or tell the controller
336 * to just use two.
337 */
338 mcam_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
339 mcam_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
340 if (cam->nbufs > 2) {
341 mcam_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
342 mcam_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
343 } else
344 mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300345 if (cam->chip_id == V4L2_IDENT_CAFE)
346 mcam_reg_write(cam, REG_UBAR, 0); /* 32 bits only */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300347}
348
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300349/*
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300350 * Copy data out to user space in the vmalloc case
351 */
352static void mcam_frame_tasklet(unsigned long data)
353{
354 struct mcam_camera *cam = (struct mcam_camera *) data;
355 int i;
356 unsigned long flags;
357 struct mcam_vb_buffer *buf;
358
359 spin_lock_irqsave(&cam->dev_lock, flags);
360 for (i = 0; i < cam->nbufs; i++) {
361 int bufno = cam->next_buf;
362
363 if (cam->state != S_STREAMING || bufno < 0)
364 break; /* I/O got stopped */
365 if (++(cam->next_buf) >= cam->nbufs)
366 cam->next_buf = 0;
367 if (!test_bit(bufno, &cam->flags))
368 continue;
369 if (list_empty(&cam->buffers)) {
370 singles++;
371 break; /* Leave it valid, hope for better later */
372 }
373 delivered++;
374 clear_bit(bufno, &cam->flags);
375 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer,
376 queue);
377 list_del_init(&buf->queue);
378 /*
379 * Drop the lock during the big copy. This *should* be safe...
380 */
381 spin_unlock_irqrestore(&cam->dev_lock, flags);
382 memcpy(vb2_plane_vaddr(&buf->vb_buf, 0), cam->dma_bufs[bufno],
383 cam->pix_format.sizeimage);
384 mcam_buffer_done(cam, bufno, &buf->vb_buf);
385 spin_lock_irqsave(&cam->dev_lock, flags);
386 }
387 spin_unlock_irqrestore(&cam->dev_lock, flags);
388}
389
390
Jonathan Corbet74984692011-07-08 17:50:49 -0300391/*
392 * Make sure our allocated buffers are up to the task.
393 */
394static int mcam_check_dma_buffers(struct mcam_camera *cam)
395{
396 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
397 mcam_free_dma_bufs(cam);
398 if (cam->nbufs == 0)
399 return mcam_alloc_dma_bufs(cam, 0);
400 return 0;
401}
402
403static void mcam_vmalloc_done(struct mcam_camera *cam, int frame)
404{
405 tasklet_schedule(&cam->s_tasklet);
406}
407
408#else /* MCAM_MODE_VMALLOC */
409
410static inline int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
411{
412 return 0;
413}
414
415static inline void mcam_free_dma_bufs(struct mcam_camera *cam)
416{
417 return;
418}
419
420static inline int mcam_check_dma_buffers(struct mcam_camera *cam)
421{
422 return 0;
423}
424
425
426
427#endif /* MCAM_MODE_VMALLOC */
428
429
430#ifdef MCAM_MODE_DMA_CONTIG
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300431/* ---------------------------------------------------------------------- */
432/*
433 * DMA-contiguous code.
434 */
435/*
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300436 * Set up a contiguous buffer for the given frame. Here also is where
437 * the underrun strategy is set: if there is no buffer available, reuse
438 * the buffer from the other BAR and set the CF_SINGLE_BUFFER flag to
439 * keep the interrupt handler from giving that buffer back to user
440 * space. In this way, we always have a buffer to DMA to and don't
441 * have to try to play games stopping and restarting the controller.
442 */
443static void mcam_set_contig_buffer(struct mcam_camera *cam, int frame)
444{
445 struct mcam_vb_buffer *buf;
446 /*
447 * If there are no available buffers, go into single mode
448 */
449 if (list_empty(&cam->buffers)) {
450 buf = cam->vb_bufs[frame ^ 0x1];
451 cam->vb_bufs[frame] = buf;
452 mcam_reg_write(cam, frame == 0 ? REG_Y0BAR : REG_Y1BAR,
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300453 vb2_dma_contig_plane_dma_addr(&buf->vb_buf, 0));
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300454 set_bit(CF_SINGLE_BUFFER, &cam->flags);
455 singles++;
456 return;
457 }
458 /*
459 * OK, we have a buffer we can use.
460 */
461 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer, queue);
462 list_del_init(&buf->queue);
463 mcam_reg_write(cam, frame == 0 ? REG_Y0BAR : REG_Y1BAR,
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300464 vb2_dma_contig_plane_dma_addr(&buf->vb_buf, 0));
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300465 cam->vb_bufs[frame] = buf;
466 clear_bit(CF_SINGLE_BUFFER, &cam->flags);
467}
468
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300469/*
470 * Initial B_DMA_contig setup.
471 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300472static void mcam_ctlr_dma_contig(struct mcam_camera *cam)
473{
474 mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
475 cam->nbufs = 2;
476 mcam_set_contig_buffer(cam, 0);
477 mcam_set_contig_buffer(cam, 1);
478}
479
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300480/*
481 * Frame completion handling.
482 */
483static void mcam_dma_contig_done(struct mcam_camera *cam, int frame)
484{
485 struct mcam_vb_buffer *buf = cam->vb_bufs[frame];
486
487 if (!test_bit(CF_SINGLE_BUFFER, &cam->flags)) {
488 delivered++;
489 mcam_buffer_done(cam, frame, &buf->vb_buf);
490 }
491 mcam_set_contig_buffer(cam, frame);
492}
493
Jonathan Corbet74984692011-07-08 17:50:49 -0300494#endif /* MCAM_MODE_DMA_CONTIG */
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300495
Jonathan Corbet74984692011-07-08 17:50:49 -0300496#ifdef MCAM_MODE_DMA_SG
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300497/* ---------------------------------------------------------------------- */
498/*
499 * Scatter/gather-specific code.
500 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300501
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300502/*
503 * Set up the next buffer for S/G I/O; caller should be sure that
504 * the controller is stopped and a buffer is available.
505 */
506static void mcam_sg_next_buffer(struct mcam_camera *cam)
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300507{
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300508 struct mcam_vb_buffer *buf;
509
510 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer, queue);
511 list_del_init(&buf->queue);
512 mcam_reg_write(cam, REG_DMA_DESC_Y, buf->dma_desc_pa);
513 mcam_reg_write(cam, REG_DESC_LEN_Y,
514 buf->dma_desc_nent*sizeof(struct mcam_dma_desc));
515 mcam_reg_write(cam, REG_DESC_LEN_U, 0);
516 mcam_reg_write(cam, REG_DESC_LEN_V, 0);
517 cam->vb_bufs[0] = buf;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300518}
519
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300520/*
521 * Initial B_DMA_sg setup
522 */
523static void mcam_ctlr_dma_sg(struct mcam_camera *cam)
524{
Jonathan Corbetbb0a8962011-12-30 14:13:41 -0300525 /*
526 * The list-empty condition can hit us at resume time
527 * if the buffer list was empty when the system was suspended.
528 */
529 if (list_empty(&cam->buffers)) {
530 set_bit(CF_SG_RESTART, &cam->flags);
531 return;
532 }
533
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300534 mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_3WORD);
535 mcam_sg_next_buffer(cam);
536 mcam_reg_set_bit(cam, REG_CTRL1, C1_DESC_ENA);
537 cam->nbufs = 3;
538}
539
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300540
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300541/*
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300542 * Frame completion with S/G is trickier. We can't muck with
543 * a descriptor chain on the fly, since the controller buffers it
544 * internally. So we have to actually stop and restart; Marvell
545 * says this is the way to do it.
546 *
547 * Of course, stopping is easier said than done; experience shows
548 * that the controller can start a frame *after* C0_ENABLE has been
549 * cleared. So when running in S/G mode, the controller is "stopped"
550 * on receipt of the start-of-frame interrupt. That means we can
551 * safely change the DMA descriptor array here and restart things
552 * (assuming there's another buffer waiting to go).
553 */
554static void mcam_dma_sg_done(struct mcam_camera *cam, int frame)
555{
556 struct mcam_vb_buffer *buf = cam->vb_bufs[0];
557
558 /*
Jonathan Corbet49df19e2012-03-16 19:14:50 -0300559 * If we're no longer supposed to be streaming, don't do anything.
560 */
561 if (cam->state != S_STREAMING)
562 return;
563 /*
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300564 * Very Bad Not Good Things happen if you don't clear
565 * C1_DESC_ENA before making any descriptor changes.
566 */
567 mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_ENA);
568 /*
569 * If we have another buffer available, put it in and
570 * restart the engine.
571 */
572 if (!list_empty(&cam->buffers)) {
573 mcam_sg_next_buffer(cam);
574 mcam_reg_set_bit(cam, REG_CTRL1, C1_DESC_ENA);
575 mcam_ctlr_start(cam);
576 /*
577 * Otherwise set CF_SG_RESTART and the controller will
578 * be restarted once another buffer shows up.
579 */
580 } else {
581 set_bit(CF_SG_RESTART, &cam->flags);
582 singles++;
Jonathan Corbetbb0a8962011-12-30 14:13:41 -0300583 cam->vb_bufs[0] = NULL;
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300584 }
585 /*
586 * Now we can give the completed frame back to user space.
587 */
588 delivered++;
589 mcam_buffer_done(cam, frame, &buf->vb_buf);
590}
591
592
593/*
594 * Scatter/gather mode requires stopping the controller between
595 * frames so we can put in a new DMA descriptor array. If no new
596 * buffer exists at frame completion, the controller is left stopped;
597 * this function is charged with gettig things going again.
598 */
599static void mcam_sg_restart(struct mcam_camera *cam)
600{
601 mcam_ctlr_dma_sg(cam);
602 mcam_ctlr_start(cam);
603 clear_bit(CF_SG_RESTART, &cam->flags);
604}
605
Jonathan Corbet74984692011-07-08 17:50:49 -0300606#else /* MCAM_MODE_DMA_SG */
607
608static inline void mcam_sg_restart(struct mcam_camera *cam)
609{
610 return;
611}
612
613#endif /* MCAM_MODE_DMA_SG */
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300614
615/* ---------------------------------------------------------------------- */
616/*
617 * Buffer-mode-independent controller code.
618 */
619
620/*
621 * Image format setup
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300622 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300623static void mcam_ctlr_image(struct mcam_camera *cam)
624{
625 int imgsz;
626 struct v4l2_pix_format *fmt = &cam->pix_format;
627
628 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
629 (fmt->bytesperline & IMGSZ_H_MASK);
630 mcam_reg_write(cam, REG_IMGSIZE, imgsz);
631 mcam_reg_write(cam, REG_IMGOFFSET, 0);
632 /* YPITCH just drops the last two bits */
633 mcam_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
634 IMGP_YP_MASK);
635 /*
636 * Tell the controller about the image format we are using.
637 */
638 switch (cam->pix_format.pixelformat) {
639 case V4L2_PIX_FMT_YUYV:
640 mcam_reg_write_mask(cam, REG_CTRL0,
641 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
642 C0_DF_MASK);
643 break;
644
645 case V4L2_PIX_FMT_RGB444:
646 mcam_reg_write_mask(cam, REG_CTRL0,
647 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
648 C0_DF_MASK);
649 /* Alpha value? */
650 break;
651
652 case V4L2_PIX_FMT_RGB565:
653 mcam_reg_write_mask(cam, REG_CTRL0,
654 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
655 C0_DF_MASK);
656 break;
657
658 default:
659 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
660 break;
661 }
662 /*
663 * Make sure it knows we want to use hsync/vsync.
664 */
665 mcam_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
666 C0_SIFM_MASK);
667}
668
669
670/*
671 * Configure the controller for operation; caller holds the
672 * device mutex.
673 */
674static int mcam_ctlr_configure(struct mcam_camera *cam)
675{
676 unsigned long flags;
677
678 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbetbb0a8962011-12-30 14:13:41 -0300679 clear_bit(CF_SG_RESTART, &cam->flags);
Jonathan Corbet74984692011-07-08 17:50:49 -0300680 cam->dma_setup(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300681 mcam_ctlr_image(cam);
682 mcam_set_config_needed(cam, 0);
683 spin_unlock_irqrestore(&cam->dev_lock, flags);
684 return 0;
685}
686
687static void mcam_ctlr_irq_enable(struct mcam_camera *cam)
688{
689 /*
690 * Clear any pending interrupts, since we do not
691 * expect to have I/O active prior to enabling.
692 */
693 mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
694 mcam_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
695}
696
697static void mcam_ctlr_irq_disable(struct mcam_camera *cam)
698{
699 mcam_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
700}
701
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300702
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300703
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300704static void mcam_ctlr_init(struct mcam_camera *cam)
705{
706 unsigned long flags;
707
708 spin_lock_irqsave(&cam->dev_lock, flags);
709 /*
710 * Make sure it's not powered down.
711 */
712 mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
713 /*
714 * Turn off the enable bit. It sure should be off anyway,
715 * but it's good to be sure.
716 */
717 mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
718 /*
719 * Clock the sensor appropriately. Controller clock should
720 * be 48MHz, sensor "typical" value is half that.
721 */
722 mcam_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
723 spin_unlock_irqrestore(&cam->dev_lock, flags);
724}
725
726
727/*
728 * Stop the controller, and don't return until we're really sure that no
729 * further DMA is going on.
730 */
731static void mcam_ctlr_stop_dma(struct mcam_camera *cam)
732{
733 unsigned long flags;
734
735 /*
736 * Theory: stop the camera controller (whether it is operating
737 * or not). Delay briefly just in case we race with the SOF
738 * interrupt, then wait until no DMA is active.
739 */
740 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300741 clear_bit(CF_SG_RESTART, &cam->flags);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300742 mcam_ctlr_stop(cam);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300743 cam->state = S_IDLE;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300744 spin_unlock_irqrestore(&cam->dev_lock, flags);
Jonathan Corbet482d35c2012-03-16 19:14:52 -0300745 /*
746 * This is a brutally long sleep, but experience shows that
747 * it can take the controller a while to get the message that
748 * it needs to stop grabbing frames. In particular, we can
749 * sometimes (on mmp) get a frame at the end WITHOUT the
750 * start-of-frame indication.
751 */
752 msleep(150);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300753 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
754 cam_err(cam, "Timeout waiting for DMA to end\n");
755 /* This would be bad news - what now? */
756 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300757 mcam_ctlr_irq_disable(cam);
758 spin_unlock_irqrestore(&cam->dev_lock, flags);
759}
760
761/*
762 * Power up and down.
763 */
764static void mcam_ctlr_power_up(struct mcam_camera *cam)
765{
766 unsigned long flags;
767
768 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300769 cam->plat_power_up(cam);
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300770 mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300771 spin_unlock_irqrestore(&cam->dev_lock, flags);
772 msleep(5); /* Just to be sure */
773}
774
775static void mcam_ctlr_power_down(struct mcam_camera *cam)
776{
777 unsigned long flags;
778
779 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300780 /*
781 * School of hard knocks department: be sure we do any register
782 * twiddling on the controller *before* calling the platform
783 * power down routine.
784 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300785 mcam_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300786 cam->plat_power_down(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300787 spin_unlock_irqrestore(&cam->dev_lock, flags);
788}
789
790/* -------------------------------------------------------------------- */
791/*
792 * Communications with the sensor.
793 */
794
795static int __mcam_cam_reset(struct mcam_camera *cam)
796{
797 return sensor_call(cam, core, reset, 0);
798}
799
800/*
801 * We have found the sensor on the i2c. Let's try to have a
802 * conversation.
803 */
804static int mcam_cam_init(struct mcam_camera *cam)
805{
806 struct v4l2_dbg_chip_ident chip;
807 int ret;
808
809 mutex_lock(&cam->s_mutex);
810 if (cam->state != S_NOTREADY)
811 cam_warn(cam, "Cam init with device in funky state %d",
812 cam->state);
813 ret = __mcam_cam_reset(cam);
814 if (ret)
815 goto out;
816 chip.ident = V4L2_IDENT_NONE;
817 chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
818 chip.match.addr = cam->sensor_addr;
819 ret = sensor_call(cam, core, g_chip_ident, &chip);
820 if (ret)
821 goto out;
822 cam->sensor_type = chip.ident;
823 if (cam->sensor_type != V4L2_IDENT_OV7670) {
824 cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
825 ret = -EINVAL;
826 goto out;
827 }
828/* Get/set parameters? */
829 ret = 0;
830 cam->state = S_IDLE;
831out:
832 mcam_ctlr_power_down(cam);
833 mutex_unlock(&cam->s_mutex);
834 return ret;
835}
836
837/*
838 * Configure the sensor to match the parameters we have. Caller should
839 * hold s_mutex
840 */
841static int mcam_cam_set_flip(struct mcam_camera *cam)
842{
843 struct v4l2_control ctrl;
844
845 memset(&ctrl, 0, sizeof(ctrl));
846 ctrl.id = V4L2_CID_VFLIP;
847 ctrl.value = flip;
848 return sensor_call(cam, core, s_ctrl, &ctrl);
849}
850
851
852static int mcam_cam_configure(struct mcam_camera *cam)
853{
854 struct v4l2_mbus_framefmt mbus_fmt;
855 int ret;
856
857 v4l2_fill_mbus_format(&mbus_fmt, &cam->pix_format, cam->mbus_code);
858 ret = sensor_call(cam, core, init, 0);
859 if (ret == 0)
860 ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
861 /*
862 * OV7670 does weird things if flip is set *before* format...
863 */
864 ret += mcam_cam_set_flip(cam);
865 return ret;
866}
867
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300868/*
869 * Get everything ready, and start grabbing frames.
870 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300871static int mcam_read_setup(struct mcam_camera *cam)
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300872{
873 int ret;
874 unsigned long flags;
875
876 /*
877 * Configuration. If we still don't have DMA buffers,
878 * make one last, desperate attempt.
879 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300880 if (cam->buffer_mode == B_vmalloc && cam->nbufs == 0 &&
881 mcam_alloc_dma_bufs(cam, 0))
882 return -ENOMEM;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300883
884 if (mcam_needs_config(cam)) {
885 mcam_cam_configure(cam);
886 ret = mcam_ctlr_configure(cam);
887 if (ret)
888 return ret;
889 }
890
891 /*
892 * Turn it loose.
893 */
894 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbet482d35c2012-03-16 19:14:52 -0300895 clear_bit(CF_DMA_ACTIVE, &cam->flags);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300896 mcam_reset_buffers(cam);
897 mcam_ctlr_irq_enable(cam);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300898 cam->state = S_STREAMING;
Jonathan Corbetbb0a8962011-12-30 14:13:41 -0300899 if (!test_bit(CF_SG_RESTART, &cam->flags))
900 mcam_ctlr_start(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300901 spin_unlock_irqrestore(&cam->dev_lock, flags);
902 return 0;
903}
904
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300905/* ----------------------------------------------------------------------- */
906/*
907 * Videobuf2 interface code.
908 */
909
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300910static int mcam_vb_queue_setup(struct vb2_queue *vq,
911 const struct v4l2_format *fmt, unsigned int *nbufs,
Marek Szyprowski035aa142011-08-24 06:43:36 -0300912 unsigned int *num_planes, unsigned int sizes[],
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300913 void *alloc_ctxs[])
914{
915 struct mcam_camera *cam = vb2_get_drv_priv(vq);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300916 int minbufs = (cam->buffer_mode == B_DMA_contig) ? 3 : 2;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300917
918 sizes[0] = cam->pix_format.sizeimage;
919 *num_planes = 1; /* Someday we have to support planar formats... */
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300920 if (*nbufs < minbufs)
921 *nbufs = minbufs;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300922 if (cam->buffer_mode == B_DMA_contig)
923 alloc_ctxs[0] = cam->vb_alloc_ctx;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300924 return 0;
925}
926
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300927
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300928static void mcam_vb_buf_queue(struct vb2_buffer *vb)
929{
930 struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
931 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
932 unsigned long flags;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300933 int start;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300934
935 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300936 start = (cam->state == S_BUFWAIT) && !list_empty(&cam->buffers);
937 list_add(&mvb->queue, &cam->buffers);
Jonathan Corbet49df19e2012-03-16 19:14:50 -0300938 if (cam->state == S_STREAMING && test_bit(CF_SG_RESTART, &cam->flags))
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300939 mcam_sg_restart(cam);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300940 spin_unlock_irqrestore(&cam->dev_lock, flags);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300941 if (start)
942 mcam_read_setup(cam);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300943}
944
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300945
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300946/*
947 * vb2 uses these to release the mutex when waiting in dqbuf. I'm
948 * not actually sure we need to do this (I'm not sure that vb2_dqbuf() needs
949 * to be called with the mutex held), but better safe than sorry.
950 */
951static void mcam_vb_wait_prepare(struct vb2_queue *vq)
952{
953 struct mcam_camera *cam = vb2_get_drv_priv(vq);
954
955 mutex_unlock(&cam->s_mutex);
956}
957
958static void mcam_vb_wait_finish(struct vb2_queue *vq)
959{
960 struct mcam_camera *cam = vb2_get_drv_priv(vq);
961
962 mutex_lock(&cam->s_mutex);
963}
964
965/*
966 * These need to be called with the mutex held from vb2
967 */
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300968static int mcam_vb_start_streaming(struct vb2_queue *vq, unsigned int count)
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300969{
970 struct mcam_camera *cam = vb2_get_drv_priv(vq);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300971
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300972 if (cam->state != S_IDLE) {
973 INIT_LIST_HEAD(&cam->buffers);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300974 return -EINVAL;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300975 }
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300976 cam->sequence = 0;
977 /*
978 * Videobuf2 sneakily hoards all the buffers and won't
979 * give them to us until *after* streaming starts. But
980 * we can't actually start streaming until we have a
981 * destination. So go into a wait state and hope they
982 * give us buffers soon.
983 */
984 if (cam->buffer_mode != B_vmalloc && list_empty(&cam->buffers)) {
985 cam->state = S_BUFWAIT;
986 return 0;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300987 }
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300988 return mcam_read_setup(cam);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300989}
990
991static int mcam_vb_stop_streaming(struct vb2_queue *vq)
992{
993 struct mcam_camera *cam = vb2_get_drv_priv(vq);
994 unsigned long flags;
995
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300996 if (cam->state == S_BUFWAIT) {
997 /* They never gave us buffers */
998 cam->state = S_IDLE;
999 return 0;
1000 }
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001001 if (cam->state != S_STREAMING)
1002 return -EINVAL;
1003 mcam_ctlr_stop_dma(cam);
1004 /*
1005 * VB2 reclaims the buffers, so we need to forget
1006 * about them.
1007 */
1008 spin_lock_irqsave(&cam->dev_lock, flags);
1009 INIT_LIST_HEAD(&cam->buffers);
1010 spin_unlock_irqrestore(&cam->dev_lock, flags);
1011 return 0;
1012}
1013
1014
1015static const struct vb2_ops mcam_vb2_ops = {
1016 .queue_setup = mcam_vb_queue_setup,
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001017 .buf_queue = mcam_vb_buf_queue,
1018 .start_streaming = mcam_vb_start_streaming,
1019 .stop_streaming = mcam_vb_stop_streaming,
1020 .wait_prepare = mcam_vb_wait_prepare,
1021 .wait_finish = mcam_vb_wait_finish,
1022};
1023
Jonathan Corbet74984692011-07-08 17:50:49 -03001024
1025#ifdef MCAM_MODE_DMA_SG
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001026/*
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001027 * Scatter/gather mode uses all of the above functions plus a
1028 * few extras to deal with DMA mapping.
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001029 */
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001030static int mcam_vb_sg_buf_init(struct vb2_buffer *vb)
1031{
1032 struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
1033 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
1034 int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;
1035
1036 mvb->dma_desc = dma_alloc_coherent(cam->dev,
1037 ndesc * sizeof(struct mcam_dma_desc),
1038 &mvb->dma_desc_pa, GFP_KERNEL);
1039 if (mvb->dma_desc == NULL) {
1040 cam_err(cam, "Unable to get DMA descriptor array\n");
1041 return -ENOMEM;
1042 }
1043 return 0;
1044}
1045
1046static int mcam_vb_sg_buf_prepare(struct vb2_buffer *vb)
1047{
1048 struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
1049 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
1050 struct vb2_dma_sg_desc *sgd = vb2_dma_sg_plane_desc(vb, 0);
1051 struct mcam_dma_desc *desc = mvb->dma_desc;
1052 struct scatterlist *sg;
1053 int i;
1054
1055 mvb->dma_desc_nent = dma_map_sg(cam->dev, sgd->sglist, sgd->num_pages,
1056 DMA_FROM_DEVICE);
1057 if (mvb->dma_desc_nent <= 0)
1058 return -EIO; /* Not sure what's right here */
1059 for_each_sg(sgd->sglist, sg, mvb->dma_desc_nent, i) {
1060 desc->dma_addr = sg_dma_address(sg);
1061 desc->segment_len = sg_dma_len(sg);
1062 desc++;
1063 }
1064 return 0;
1065}
1066
1067static int mcam_vb_sg_buf_finish(struct vb2_buffer *vb)
1068{
1069 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
1070 struct vb2_dma_sg_desc *sgd = vb2_dma_sg_plane_desc(vb, 0);
1071
1072 dma_unmap_sg(cam->dev, sgd->sglist, sgd->num_pages, DMA_FROM_DEVICE);
1073 return 0;
1074}
1075
1076static void mcam_vb_sg_buf_cleanup(struct vb2_buffer *vb)
1077{
1078 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
1079 struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
1080 int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;
1081
1082 dma_free_coherent(cam->dev, ndesc * sizeof(struct mcam_dma_desc),
1083 mvb->dma_desc, mvb->dma_desc_pa);
1084}
1085
1086
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001087static const struct vb2_ops mcam_vb2_sg_ops = {
1088 .queue_setup = mcam_vb_queue_setup,
1089 .buf_init = mcam_vb_sg_buf_init,
1090 .buf_prepare = mcam_vb_sg_buf_prepare,
1091 .buf_queue = mcam_vb_buf_queue,
1092 .buf_finish = mcam_vb_sg_buf_finish,
1093 .buf_cleanup = mcam_vb_sg_buf_cleanup,
1094 .start_streaming = mcam_vb_start_streaming,
1095 .stop_streaming = mcam_vb_stop_streaming,
1096 .wait_prepare = mcam_vb_wait_prepare,
1097 .wait_finish = mcam_vb_wait_finish,
1098};
1099
Jonathan Corbet74984692011-07-08 17:50:49 -03001100#endif /* MCAM_MODE_DMA_SG */
1101
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001102static int mcam_setup_vb2(struct mcam_camera *cam)
1103{
1104 struct vb2_queue *vq = &cam->vb_queue;
1105
1106 memset(vq, 0, sizeof(*vq));
1107 vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001108 vq->drv_priv = cam;
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001109 INIT_LIST_HEAD(&cam->buffers);
1110 switch (cam->buffer_mode) {
1111 case B_DMA_contig:
Jonathan Corbet74984692011-07-08 17:50:49 -03001112#ifdef MCAM_MODE_DMA_CONTIG
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001113 vq->ops = &mcam_vb2_ops;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001114 vq->mem_ops = &vb2_dma_contig_memops;
1115 cam->vb_alloc_ctx = vb2_dma_contig_init_ctx(cam->dev);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001116 vq->io_modes = VB2_MMAP | VB2_USERPTR;
Jonathan Corbet74984692011-07-08 17:50:49 -03001117 cam->dma_setup = mcam_ctlr_dma_contig;
1118 cam->frame_complete = mcam_dma_contig_done;
1119#endif
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001120 break;
1121 case B_DMA_sg:
Jonathan Corbet74984692011-07-08 17:50:49 -03001122#ifdef MCAM_MODE_DMA_SG
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001123 vq->ops = &mcam_vb2_sg_ops;
1124 vq->mem_ops = &vb2_dma_sg_memops;
1125 vq->io_modes = VB2_MMAP | VB2_USERPTR;
Jonathan Corbet74984692011-07-08 17:50:49 -03001126 cam->dma_setup = mcam_ctlr_dma_sg;
1127 cam->frame_complete = mcam_dma_sg_done;
1128#endif
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001129 break;
1130 case B_vmalloc:
Jonathan Corbet74984692011-07-08 17:50:49 -03001131#ifdef MCAM_MODE_VMALLOC
1132 tasklet_init(&cam->s_tasklet, mcam_frame_tasklet,
1133 (unsigned long) cam);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001134 vq->ops = &mcam_vb2_ops;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001135 vq->mem_ops = &vb2_vmalloc_memops;
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001136 vq->buf_struct_size = sizeof(struct mcam_vb_buffer);
1137 vq->io_modes = VB2_MMAP;
Jonathan Corbet74984692011-07-08 17:50:49 -03001138 cam->dma_setup = mcam_ctlr_dma_vmalloc;
1139 cam->frame_complete = mcam_vmalloc_done;
1140#endif
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001141 break;
1142 }
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001143 return vb2_queue_init(vq);
1144}
1145
1146static void mcam_cleanup_vb2(struct mcam_camera *cam)
1147{
1148 vb2_queue_release(&cam->vb_queue);
Jonathan Corbet74984692011-07-08 17:50:49 -03001149#ifdef MCAM_MODE_DMA_CONTIG
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001150 if (cam->buffer_mode == B_DMA_contig)
1151 vb2_dma_contig_cleanup_ctx(cam->vb_alloc_ctx);
Jonathan Corbet74984692011-07-08 17:50:49 -03001152#endif
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001153}
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001154
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001155
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001156/* ---------------------------------------------------------------------- */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001157/*
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001158 * The long list of V4L2 ioctl() operations.
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001159 */
1160
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001161static int mcam_vidioc_streamon(struct file *filp, void *priv,
1162 enum v4l2_buf_type type)
1163{
1164 struct mcam_camera *cam = filp->private_data;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001165 int ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001166
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001167 mutex_lock(&cam->s_mutex);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001168 ret = vb2_streamon(&cam->vb_queue, type);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001169 mutex_unlock(&cam->s_mutex);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001170 return ret;
1171}
1172
1173
1174static int mcam_vidioc_streamoff(struct file *filp, void *priv,
1175 enum v4l2_buf_type type)
1176{
1177 struct mcam_camera *cam = filp->private_data;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001178 int ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001179
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001180 mutex_lock(&cam->s_mutex);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001181 ret = vb2_streamoff(&cam->vb_queue, type);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001182 mutex_unlock(&cam->s_mutex);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001183 return ret;
1184}
1185
1186
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001187static int mcam_vidioc_reqbufs(struct file *filp, void *priv,
1188 struct v4l2_requestbuffers *req)
1189{
1190 struct mcam_camera *cam = filp->private_data;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001191 int ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001192
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001193 mutex_lock(&cam->s_mutex);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001194 ret = vb2_reqbufs(&cam->vb_queue, req);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001195 mutex_unlock(&cam->s_mutex);
1196 return ret;
1197}
1198
1199
1200static int mcam_vidioc_querybuf(struct file *filp, void *priv,
1201 struct v4l2_buffer *buf)
1202{
1203 struct mcam_camera *cam = filp->private_data;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001204 int ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001205
1206 mutex_lock(&cam->s_mutex);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001207 ret = vb2_querybuf(&cam->vb_queue, buf);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001208 mutex_unlock(&cam->s_mutex);
1209 return ret;
1210}
1211
1212static int mcam_vidioc_qbuf(struct file *filp, void *priv,
1213 struct v4l2_buffer *buf)
1214{
1215 struct mcam_camera *cam = filp->private_data;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001216 int ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001217
1218 mutex_lock(&cam->s_mutex);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001219 ret = vb2_qbuf(&cam->vb_queue, buf);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001220 mutex_unlock(&cam->s_mutex);
1221 return ret;
1222}
1223
1224static int mcam_vidioc_dqbuf(struct file *filp, void *priv,
1225 struct v4l2_buffer *buf)
1226{
1227 struct mcam_camera *cam = filp->private_data;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001228 int ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001229
1230 mutex_lock(&cam->s_mutex);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001231 ret = vb2_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001232 mutex_unlock(&cam->s_mutex);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001233 return ret;
1234}
1235
1236
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001237
1238static int mcam_vidioc_queryctrl(struct file *filp, void *priv,
1239 struct v4l2_queryctrl *qc)
1240{
1241 struct mcam_camera *cam = priv;
1242 int ret;
1243
1244 mutex_lock(&cam->s_mutex);
1245 ret = sensor_call(cam, core, queryctrl, qc);
1246 mutex_unlock(&cam->s_mutex);
1247 return ret;
1248}
1249
1250
1251static int mcam_vidioc_g_ctrl(struct file *filp, void *priv,
1252 struct v4l2_control *ctrl)
1253{
1254 struct mcam_camera *cam = priv;
1255 int ret;
1256
1257 mutex_lock(&cam->s_mutex);
1258 ret = sensor_call(cam, core, g_ctrl, ctrl);
1259 mutex_unlock(&cam->s_mutex);
1260 return ret;
1261}
1262
1263
1264static int mcam_vidioc_s_ctrl(struct file *filp, void *priv,
1265 struct v4l2_control *ctrl)
1266{
1267 struct mcam_camera *cam = priv;
1268 int ret;
1269
1270 mutex_lock(&cam->s_mutex);
1271 ret = sensor_call(cam, core, s_ctrl, ctrl);
1272 mutex_unlock(&cam->s_mutex);
1273 return ret;
1274}
1275
1276
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001277static int mcam_vidioc_querycap(struct file *file, void *priv,
1278 struct v4l2_capability *cap)
1279{
1280 strcpy(cap->driver, "marvell_ccic");
1281 strcpy(cap->card, "marvell_ccic");
1282 cap->version = 1;
1283 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1284 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1285 return 0;
1286}
1287
1288
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001289static int mcam_vidioc_enum_fmt_vid_cap(struct file *filp,
1290 void *priv, struct v4l2_fmtdesc *fmt)
1291{
1292 if (fmt->index >= N_MCAM_FMTS)
1293 return -EINVAL;
1294 strlcpy(fmt->description, mcam_formats[fmt->index].desc,
1295 sizeof(fmt->description));
1296 fmt->pixelformat = mcam_formats[fmt->index].pixelformat;
1297 return 0;
1298}
1299
1300static int mcam_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
1301 struct v4l2_format *fmt)
1302{
1303 struct mcam_camera *cam = priv;
1304 struct mcam_format_struct *f;
1305 struct v4l2_pix_format *pix = &fmt->fmt.pix;
1306 struct v4l2_mbus_framefmt mbus_fmt;
1307 int ret;
1308
1309 f = mcam_find_format(pix->pixelformat);
1310 pix->pixelformat = f->pixelformat;
1311 v4l2_fill_mbus_format(&mbus_fmt, pix, f->mbus_code);
1312 mutex_lock(&cam->s_mutex);
1313 ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
1314 mutex_unlock(&cam->s_mutex);
1315 v4l2_fill_pix_format(pix, &mbus_fmt);
1316 pix->bytesperline = pix->width * f->bpp;
1317 pix->sizeimage = pix->height * pix->bytesperline;
1318 return ret;
1319}
1320
1321static int mcam_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
1322 struct v4l2_format *fmt)
1323{
1324 struct mcam_camera *cam = priv;
1325 struct mcam_format_struct *f;
1326 int ret;
1327
1328 /*
1329 * Can't do anything if the device is not idle
1330 * Also can't if there are streaming buffers in place.
1331 */
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001332 if (cam->state != S_IDLE || cam->vb_queue.num_buffers > 0)
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001333 return -EBUSY;
1334
1335 f = mcam_find_format(fmt->fmt.pix.pixelformat);
1336
1337 /*
1338 * See if the formatting works in principle.
1339 */
1340 ret = mcam_vidioc_try_fmt_vid_cap(filp, priv, fmt);
1341 if (ret)
1342 return ret;
1343 /*
1344 * Now we start to change things for real, so let's do it
1345 * under lock.
1346 */
1347 mutex_lock(&cam->s_mutex);
1348 cam->pix_format = fmt->fmt.pix;
1349 cam->mbus_code = f->mbus_code;
1350
1351 /*
1352 * Make sure we have appropriate DMA buffers.
1353 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001354 if (cam->buffer_mode == B_vmalloc) {
Jonathan Corbet74984692011-07-08 17:50:49 -03001355 ret = mcam_check_dma_buffers(cam);
1356 if (ret)
1357 goto out;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001358 }
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001359 mcam_set_config_needed(cam, 1);
1360 ret = 0;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001361out:
1362 mutex_unlock(&cam->s_mutex);
1363 return ret;
1364}
1365
1366/*
1367 * Return our stored notion of how the camera is/should be configured.
1368 * The V4l2 spec wants us to be smarter, and actually get this from
1369 * the camera (and not mess with it at open time). Someday.
1370 */
1371static int mcam_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
1372 struct v4l2_format *f)
1373{
1374 struct mcam_camera *cam = priv;
1375
1376 f->fmt.pix = cam->pix_format;
1377 return 0;
1378}
1379
1380/*
1381 * We only have one input - the sensor - so minimize the nonsense here.
1382 */
1383static int mcam_vidioc_enum_input(struct file *filp, void *priv,
1384 struct v4l2_input *input)
1385{
1386 if (input->index != 0)
1387 return -EINVAL;
1388
1389 input->type = V4L2_INPUT_TYPE_CAMERA;
1390 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1391 strcpy(input->name, "Camera");
1392 return 0;
1393}
1394
1395static int mcam_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1396{
1397 *i = 0;
1398 return 0;
1399}
1400
1401static int mcam_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1402{
1403 if (i != 0)
1404 return -EINVAL;
1405 return 0;
1406}
1407
1408/* from vivi.c */
1409static int mcam_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1410{
1411 return 0;
1412}
1413
1414/*
1415 * G/S_PARM. Most of this is done by the sensor, but we are
1416 * the level which controls the number of read buffers.
1417 */
1418static int mcam_vidioc_g_parm(struct file *filp, void *priv,
1419 struct v4l2_streamparm *parms)
1420{
1421 struct mcam_camera *cam = priv;
1422 int ret;
1423
1424 mutex_lock(&cam->s_mutex);
1425 ret = sensor_call(cam, video, g_parm, parms);
1426 mutex_unlock(&cam->s_mutex);
1427 parms->parm.capture.readbuffers = n_dma_bufs;
1428 return ret;
1429}
1430
1431static int mcam_vidioc_s_parm(struct file *filp, void *priv,
1432 struct v4l2_streamparm *parms)
1433{
1434 struct mcam_camera *cam = priv;
1435 int ret;
1436
1437 mutex_lock(&cam->s_mutex);
1438 ret = sensor_call(cam, video, s_parm, parms);
1439 mutex_unlock(&cam->s_mutex);
1440 parms->parm.capture.readbuffers = n_dma_bufs;
1441 return ret;
1442}
1443
1444static int mcam_vidioc_g_chip_ident(struct file *file, void *priv,
1445 struct v4l2_dbg_chip_ident *chip)
1446{
1447 struct mcam_camera *cam = priv;
1448
1449 chip->ident = V4L2_IDENT_NONE;
1450 chip->revision = 0;
1451 if (v4l2_chip_match_host(&chip->match)) {
1452 chip->ident = cam->chip_id;
1453 return 0;
1454 }
1455 return sensor_call(cam, core, g_chip_ident, chip);
1456}
1457
1458static int mcam_vidioc_enum_framesizes(struct file *filp, void *priv,
1459 struct v4l2_frmsizeenum *sizes)
1460{
1461 struct mcam_camera *cam = priv;
1462 int ret;
1463
1464 mutex_lock(&cam->s_mutex);
1465 ret = sensor_call(cam, video, enum_framesizes, sizes);
1466 mutex_unlock(&cam->s_mutex);
1467 return ret;
1468}
1469
1470static int mcam_vidioc_enum_frameintervals(struct file *filp, void *priv,
1471 struct v4l2_frmivalenum *interval)
1472{
1473 struct mcam_camera *cam = priv;
1474 int ret;
1475
1476 mutex_lock(&cam->s_mutex);
1477 ret = sensor_call(cam, video, enum_frameintervals, interval);
1478 mutex_unlock(&cam->s_mutex);
1479 return ret;
1480}
1481
1482#ifdef CONFIG_VIDEO_ADV_DEBUG
1483static int mcam_vidioc_g_register(struct file *file, void *priv,
1484 struct v4l2_dbg_register *reg)
1485{
1486 struct mcam_camera *cam = priv;
1487
1488 if (v4l2_chip_match_host(&reg->match)) {
1489 reg->val = mcam_reg_read(cam, reg->reg);
1490 reg->size = 4;
1491 return 0;
1492 }
1493 return sensor_call(cam, core, g_register, reg);
1494}
1495
1496static int mcam_vidioc_s_register(struct file *file, void *priv,
1497 struct v4l2_dbg_register *reg)
1498{
1499 struct mcam_camera *cam = priv;
1500
1501 if (v4l2_chip_match_host(&reg->match)) {
1502 mcam_reg_write(cam, reg->reg, reg->val);
1503 return 0;
1504 }
1505 return sensor_call(cam, core, s_register, reg);
1506}
1507#endif
1508
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001509static const struct v4l2_ioctl_ops mcam_v4l_ioctl_ops = {
1510 .vidioc_querycap = mcam_vidioc_querycap,
1511 .vidioc_enum_fmt_vid_cap = mcam_vidioc_enum_fmt_vid_cap,
1512 .vidioc_try_fmt_vid_cap = mcam_vidioc_try_fmt_vid_cap,
1513 .vidioc_s_fmt_vid_cap = mcam_vidioc_s_fmt_vid_cap,
1514 .vidioc_g_fmt_vid_cap = mcam_vidioc_g_fmt_vid_cap,
1515 .vidioc_enum_input = mcam_vidioc_enum_input,
1516 .vidioc_g_input = mcam_vidioc_g_input,
1517 .vidioc_s_input = mcam_vidioc_s_input,
1518 .vidioc_s_std = mcam_vidioc_s_std,
1519 .vidioc_reqbufs = mcam_vidioc_reqbufs,
1520 .vidioc_querybuf = mcam_vidioc_querybuf,
1521 .vidioc_qbuf = mcam_vidioc_qbuf,
1522 .vidioc_dqbuf = mcam_vidioc_dqbuf,
1523 .vidioc_streamon = mcam_vidioc_streamon,
1524 .vidioc_streamoff = mcam_vidioc_streamoff,
1525 .vidioc_queryctrl = mcam_vidioc_queryctrl,
1526 .vidioc_g_ctrl = mcam_vidioc_g_ctrl,
1527 .vidioc_s_ctrl = mcam_vidioc_s_ctrl,
1528 .vidioc_g_parm = mcam_vidioc_g_parm,
1529 .vidioc_s_parm = mcam_vidioc_s_parm,
1530 .vidioc_enum_framesizes = mcam_vidioc_enum_framesizes,
1531 .vidioc_enum_frameintervals = mcam_vidioc_enum_frameintervals,
1532 .vidioc_g_chip_ident = mcam_vidioc_g_chip_ident,
1533#ifdef CONFIG_VIDEO_ADV_DEBUG
1534 .vidioc_g_register = mcam_vidioc_g_register,
1535 .vidioc_s_register = mcam_vidioc_s_register,
1536#endif
1537};
1538
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001539/* ---------------------------------------------------------------------- */
1540/*
1541 * Our various file operations.
1542 */
1543static int mcam_v4l_open(struct file *filp)
1544{
1545 struct mcam_camera *cam = video_drvdata(filp);
1546 int ret = 0;
1547
1548 filp->private_data = cam;
1549
1550 frames = singles = delivered = 0;
1551 mutex_lock(&cam->s_mutex);
1552 if (cam->users == 0) {
1553 ret = mcam_setup_vb2(cam);
1554 if (ret)
1555 goto out;
1556 mcam_ctlr_power_up(cam);
1557 __mcam_cam_reset(cam);
1558 mcam_set_config_needed(cam, 1);
1559 }
1560 (cam->users)++;
1561out:
1562 mutex_unlock(&cam->s_mutex);
1563 return ret;
1564}
1565
1566
1567static int mcam_v4l_release(struct file *filp)
1568{
1569 struct mcam_camera *cam = filp->private_data;
1570
1571 cam_err(cam, "Release, %d frames, %d singles, %d delivered\n", frames,
1572 singles, delivered);
1573 mutex_lock(&cam->s_mutex);
1574 (cam->users)--;
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001575 if (cam->users == 0) {
Jonathan Corbet0770d072012-03-16 19:14:51 -03001576 mcam_ctlr_stop_dma(cam);
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001577 mcam_cleanup_vb2(cam);
1578 mcam_ctlr_power_down(cam);
1579 if (cam->buffer_mode == B_vmalloc && alloc_bufs_at_read)
1580 mcam_free_dma_bufs(cam);
1581 }
1582 mutex_unlock(&cam->s_mutex);
1583 return 0;
1584}
1585
1586static ssize_t mcam_v4l_read(struct file *filp,
1587 char __user *buffer, size_t len, loff_t *pos)
1588{
1589 struct mcam_camera *cam = filp->private_data;
1590 int ret;
1591
1592 mutex_lock(&cam->s_mutex);
1593 ret = vb2_read(&cam->vb_queue, buffer, len, pos,
1594 filp->f_flags & O_NONBLOCK);
1595 mutex_unlock(&cam->s_mutex);
1596 return ret;
1597}
1598
1599
1600
1601static unsigned int mcam_v4l_poll(struct file *filp,
1602 struct poll_table_struct *pt)
1603{
1604 struct mcam_camera *cam = filp->private_data;
1605 int ret;
1606
1607 mutex_lock(&cam->s_mutex);
1608 ret = vb2_poll(&cam->vb_queue, filp, pt);
1609 mutex_unlock(&cam->s_mutex);
1610 return ret;
1611}
1612
1613
1614static int mcam_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1615{
1616 struct mcam_camera *cam = filp->private_data;
1617 int ret;
1618
1619 mutex_lock(&cam->s_mutex);
1620 ret = vb2_mmap(&cam->vb_queue, vma);
1621 mutex_unlock(&cam->s_mutex);
1622 return ret;
1623}
1624
1625
1626
1627static const struct v4l2_file_operations mcam_v4l_fops = {
1628 .owner = THIS_MODULE,
1629 .open = mcam_v4l_open,
1630 .release = mcam_v4l_release,
1631 .read = mcam_v4l_read,
1632 .poll = mcam_v4l_poll,
1633 .mmap = mcam_v4l_mmap,
1634 .unlocked_ioctl = video_ioctl2,
1635};
1636
1637
1638/*
1639 * This template device holds all of those v4l2 methods; we
1640 * clone it for specific real devices.
1641 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001642static struct video_device mcam_v4l_template = {
1643 .name = "mcam",
1644 .tvnorms = V4L2_STD_NTSC_M,
1645 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1646
1647 .fops = &mcam_v4l_fops,
1648 .ioctl_ops = &mcam_v4l_ioctl_ops,
1649 .release = video_device_release_empty,
1650};
1651
1652/* ---------------------------------------------------------------------- */
1653/*
1654 * Interrupt handler stuff
1655 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001656static void mcam_frame_complete(struct mcam_camera *cam, int frame)
1657{
1658 /*
1659 * Basic frame housekeeping.
1660 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001661 set_bit(frame, &cam->flags);
1662 clear_bit(CF_DMA_ACTIVE, &cam->flags);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001663 cam->next_buf = frame;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001664 cam->buf_seq[frame] = ++(cam->sequence);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001665 frames++;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001666 /*
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001667 * "This should never happen"
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001668 */
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001669 if (cam->state != S_STREAMING)
1670 return;
1671 /*
1672 * Process the frame and set up the next one.
1673 */
Jonathan Corbet74984692011-07-08 17:50:49 -03001674 cam->frame_complete(cam, frame);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001675}
1676
1677
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001678/*
1679 * The interrupt handler; this needs to be called from the
1680 * platform irq handler with the lock held.
1681 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001682int mccic_irq(struct mcam_camera *cam, unsigned int irqs)
1683{
1684 unsigned int frame, handled = 0;
1685
1686 mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1687 /*
1688 * Handle any frame completions. There really should
1689 * not be more than one of these, or we have fallen
1690 * far behind.
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001691 *
1692 * When running in S/G mode, the frame number lacks any
1693 * real meaning - there's only one descriptor array - but
1694 * the controller still picks a different one to signal
1695 * each time.
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001696 */
1697 for (frame = 0; frame < cam->nbufs; frame++)
1698 if (irqs & (IRQ_EOF0 << frame)) {
1699 mcam_frame_complete(cam, frame);
1700 handled = 1;
1701 }
1702 /*
1703 * If a frame starts, note that we have DMA active. This
1704 * code assumes that we won't get multiple frame interrupts
1705 * at once; may want to rethink that.
1706 */
1707 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2)) {
1708 set_bit(CF_DMA_ACTIVE, &cam->flags);
1709 handled = 1;
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001710 if (cam->buffer_mode == B_DMA_sg)
1711 mcam_ctlr_stop(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001712 }
1713 return handled;
1714}
1715
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001716/* ---------------------------------------------------------------------- */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001717/*
1718 * Registration and such.
1719 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001720static struct ov7670_config sensor_cfg = {
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001721 /*
1722 * Exclude QCIF mode, because it only captures a tiny portion
1723 * of the sensor FOV
1724 */
1725 .min_width = 320,
1726 .min_height = 240,
1727};
1728
1729
1730int mccic_register(struct mcam_camera *cam)
1731{
1732 struct i2c_board_info ov7670_info = {
1733 .type = "ov7670",
Jonathan Corbet1c68f882011-06-11 14:46:47 -03001734 .addr = 0x42 >> 1,
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001735 .platform_data = &sensor_cfg,
1736 };
1737 int ret;
1738
1739 /*
Jonathan Corbet74984692011-07-08 17:50:49 -03001740 * Validate the requested buffer mode.
1741 */
1742 if (buffer_mode >= 0)
1743 cam->buffer_mode = buffer_mode;
1744 if (cam->buffer_mode == B_DMA_sg &&
1745 cam->chip_id == V4L2_IDENT_CAFE) {
1746 printk(KERN_ERR "marvell-cam: Cafe can't do S/G I/O, "
1747 "attempting vmalloc mode instead\n");
1748 cam->buffer_mode = B_vmalloc;
1749 }
1750 if (!mcam_buffer_mode_supported(cam->buffer_mode)) {
1751 printk(KERN_ERR "marvell-cam: buffer mode %d unsupported\n",
1752 cam->buffer_mode);
1753 return -EINVAL;
1754 }
1755 /*
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001756 * Register with V4L
1757 */
1758 ret = v4l2_device_register(cam->dev, &cam->v4l2_dev);
1759 if (ret)
1760 return ret;
1761
1762 mutex_init(&cam->s_mutex);
1763 cam->state = S_NOTREADY;
1764 mcam_set_config_needed(cam, 1);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001765 cam->pix_format = mcam_def_pix_format;
1766 cam->mbus_code = mcam_def_mbus_code;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001767 INIT_LIST_HEAD(&cam->buffers);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001768 mcam_ctlr_init(cam);
1769
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001770 /*
1771 * Try to find the sensor.
1772 */
Jonathan Corbet2164b5a2011-06-11 14:46:44 -03001773 sensor_cfg.clock_speed = cam->clock_speed;
1774 sensor_cfg.use_smbus = cam->use_smbus;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001775 cam->sensor_addr = ov7670_info.addr;
1776 cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev,
Jonathan Corbet595a93a2011-06-11 14:46:48 -03001777 cam->i2c_adapter, &ov7670_info, NULL);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001778 if (cam->sensor == NULL) {
1779 ret = -ENODEV;
1780 goto out_unregister;
1781 }
1782
1783 ret = mcam_cam_init(cam);
1784 if (ret)
1785 goto out_unregister;
1786 /*
1787 * Get the v4l2 setup done.
1788 */
1789 mutex_lock(&cam->s_mutex);
1790 cam->vdev = mcam_v4l_template;
1791 cam->vdev.debug = 0;
1792 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1793 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1794 if (ret)
1795 goto out;
1796 video_set_drvdata(&cam->vdev, cam);
1797
1798 /*
1799 * If so requested, try to get our DMA buffers now.
1800 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001801 if (cam->buffer_mode == B_vmalloc && !alloc_bufs_at_read) {
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001802 if (mcam_alloc_dma_bufs(cam, 1))
1803 cam_warn(cam, "Unable to alloc DMA buffers at load"
1804 " will try again later.");
1805 }
1806
1807out:
1808 mutex_unlock(&cam->s_mutex);
1809 return ret;
1810out_unregister:
1811 v4l2_device_unregister(&cam->v4l2_dev);
1812 return ret;
1813}
1814
1815
1816void mccic_shutdown(struct mcam_camera *cam)
1817{
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -03001818 /*
1819 * If we have no users (and we really, really should have no
1820 * users) the device will already be powered down. Trying to
1821 * take it down again will wedge the machine, which is frowned
1822 * upon.
1823 */
1824 if (cam->users > 0) {
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001825 cam_warn(cam, "Removing a device with users!\n");
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -03001826 mcam_ctlr_power_down(cam);
1827 }
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001828 vb2_queue_release(&cam->vb_queue);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001829 if (cam->buffer_mode == B_vmalloc)
1830 mcam_free_dma_bufs(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001831 video_unregister_device(&cam->vdev);
1832 v4l2_device_unregister(&cam->v4l2_dev);
1833}
1834
1835/*
1836 * Power management
1837 */
1838#ifdef CONFIG_PM
1839
1840void mccic_suspend(struct mcam_camera *cam)
1841{
Jonathan Corbetbb0a8962011-12-30 14:13:41 -03001842 mutex_lock(&cam->s_mutex);
1843 if (cam->users > 0) {
1844 enum mcam_state cstate = cam->state;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001845
Jonathan Corbetbb0a8962011-12-30 14:13:41 -03001846 mcam_ctlr_stop_dma(cam);
1847 mcam_ctlr_power_down(cam);
1848 cam->state = cstate;
1849 }
1850 mutex_unlock(&cam->s_mutex);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001851}
1852
1853int mccic_resume(struct mcam_camera *cam)
1854{
1855 int ret = 0;
1856
1857 mutex_lock(&cam->s_mutex);
1858 if (cam->users > 0) {
1859 mcam_ctlr_power_up(cam);
1860 __mcam_cam_reset(cam);
1861 } else {
1862 mcam_ctlr_power_down(cam);
1863 }
1864 mutex_unlock(&cam->s_mutex);
1865
1866 set_bit(CF_CONFIG_NEEDED, &cam->flags);
Jonathan Corbetbb0a8962011-12-30 14:13:41 -03001867 if (cam->state == S_STREAMING) {
1868 /*
1869 * If there was a buffer in the DMA engine at suspend
1870 * time, put it back on the queue or we'll forget about it.
1871 */
1872 if (cam->buffer_mode == B_DMA_sg && cam->vb_bufs[0])
1873 list_add(&cam->vb_bufs[0]->queue, &cam->buffers);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001874 ret = mcam_read_setup(cam);
Jonathan Corbetbb0a8962011-12-30 14:13:41 -03001875 }
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001876 return ret;
1877}
1878#endif /* CONFIG_PM */