blob: af5faa6d7bc0533e8a52d40aaee6b628d70f93dc [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
40/*
41 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
42 * we must have physically contiguous buffers to bring frames into.
43 * These parameters control how many buffers we use, whether we
44 * allocate them at load time (better chance of success, but nails down
45 * memory) or when somebody tries to use the camera (riskier), and,
46 * for load-time allocation, how big they should be.
47 *
48 * The controller can cycle through three buffers. We could use
49 * more by flipping pointers around, but it probably makes little
50 * sense.
51 */
52
53static int alloc_bufs_at_read;
54module_param(alloc_bufs_at_read, bool, 0444);
55MODULE_PARM_DESC(alloc_bufs_at_read,
56 "Non-zero value causes DMA buffers to be allocated when the "
57 "video capture device is read, rather than at module load "
58 "time. This saves memory, but decreases the chances of "
Jonathan Corbeta9b36e82011-06-20 16:14:40 -030059 "successfully getting those buffers. This parameter is "
60 "only used in the vmalloc buffer mode");
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030061
62static int n_dma_bufs = 3;
63module_param(n_dma_bufs, uint, 0644);
64MODULE_PARM_DESC(n_dma_bufs,
65 "The number of DMA buffers to allocate. Can be either two "
66 "(saves memory, makes timing tighter) or three.");
67
68static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
69module_param(dma_buf_size, uint, 0444);
70MODULE_PARM_DESC(dma_buf_size,
71 "The size of the allocated DMA buffers. If actual operating "
72 "parameters require larger buffers, an attempt to reallocate "
73 "will be made.");
74
75static int min_buffers = 1;
76module_param(min_buffers, uint, 0644);
77MODULE_PARM_DESC(min_buffers,
78 "The minimum number of streaming I/O buffers we are willing "
79 "to work with.");
80
81static int max_buffers = 10;
82module_param(max_buffers, uint, 0644);
83MODULE_PARM_DESC(max_buffers,
84 "The maximum number of streaming I/O buffers an application "
85 "will be allowed to allocate. These buffers are big and live "
86 "in vmalloc space.");
87
88static int flip;
89module_param(flip, bool, 0444);
90MODULE_PARM_DESC(flip,
91 "If set, the sensor will be instructed to flip the image "
92 "vertically.");
93
Jonathan Corbeta9b36e82011-06-20 16:14:40 -030094static int buffer_mode = -1;
95module_param(buffer_mode, int, 0444);
96MODULE_PARM_DESC(buffer_mode,
97 "Set the buffer mode to be used; default is to go with what "
98 "the platform driver asks for. Set to 0 for vmalloc, 1 for "
99 "DMA contiguous.");
100
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300101/*
102 * Status flags. Always manipulated with bit operations.
103 */
104#define CF_BUF0_VALID 0 /* Buffers valid - first three */
105#define CF_BUF1_VALID 1
106#define CF_BUF2_VALID 2
107#define CF_DMA_ACTIVE 3 /* A frame is incoming */
108#define CF_CONFIG_NEEDED 4 /* Must configure hardware */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300109#define CF_SINGLE_BUFFER 5 /* Running with a single buffer */
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300110#define CF_SG_RESTART 6 /* SG restart needed */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300111
112#define sensor_call(cam, o, f, args...) \
113 v4l2_subdev_call(cam->sensor, o, f, ##args)
114
115static struct mcam_format_struct {
116 __u8 *desc;
117 __u32 pixelformat;
118 int bpp; /* Bytes per pixel */
119 enum v4l2_mbus_pixelcode mbus_code;
120} mcam_formats[] = {
121 {
122 .desc = "YUYV 4:2:2",
123 .pixelformat = V4L2_PIX_FMT_YUYV,
124 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
125 .bpp = 2,
126 },
127 {
128 .desc = "RGB 444",
129 .pixelformat = V4L2_PIX_FMT_RGB444,
130 .mbus_code = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE,
131 .bpp = 2,
132 },
133 {
134 .desc = "RGB 565",
135 .pixelformat = V4L2_PIX_FMT_RGB565,
136 .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE,
137 .bpp = 2,
138 },
139 {
140 .desc = "Raw RGB Bayer",
141 .pixelformat = V4L2_PIX_FMT_SBGGR8,
142 .mbus_code = V4L2_MBUS_FMT_SBGGR8_1X8,
143 .bpp = 1
144 },
145};
146#define N_MCAM_FMTS ARRAY_SIZE(mcam_formats)
147
148static struct mcam_format_struct *mcam_find_format(u32 pixelformat)
149{
150 unsigned i;
151
152 for (i = 0; i < N_MCAM_FMTS; i++)
153 if (mcam_formats[i].pixelformat == pixelformat)
154 return mcam_formats + i;
155 /* Not found? Then return the first format. */
156 return mcam_formats;
157}
158
159/*
160 * Start over with DMA buffers - dev_lock needed.
161 */
162static void mcam_reset_buffers(struct mcam_camera *cam)
163{
164 int i;
165
166 cam->next_buf = -1;
167 for (i = 0; i < cam->nbufs; i++)
168 clear_bit(i, &cam->flags);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300169}
170
171static inline int mcam_needs_config(struct mcam_camera *cam)
172{
173 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
174}
175
176static void mcam_set_config_needed(struct mcam_camera *cam, int needed)
177{
178 if (needed)
179 set_bit(CF_CONFIG_NEEDED, &cam->flags);
180 else
181 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
182}
183
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300184/*
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300185 * The two-word DMA descriptor format used by the Armada 610 and like. There
186 * Is a three-word format as well (set C1_DESC_3WORD) where the third
187 * word is a pointer to the next descriptor, but we don't use it. Two-word
188 * descriptors have to be contiguous in memory.
189 */
190struct mcam_dma_desc {
191 u32 dma_addr;
192 u32 segment_len;
193};
194
195/*
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300196 * Our buffer type for working with videobuf2. Note that the vb2
197 * developers have decreed that struct vb2_buffer must be at the
198 * beginning of this structure.
199 */
200struct mcam_vb_buffer {
201 struct vb2_buffer vb_buf;
202 struct list_head queue;
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300203 struct mcam_dma_desc *dma_desc; /* Descriptor virtual address */
204 dma_addr_t dma_desc_pa; /* Descriptor physical address */
205 int dma_desc_nent; /* Number of mapped descriptors */
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300206};
207
208static inline struct mcam_vb_buffer *vb_to_mvb(struct vb2_buffer *vb)
209{
210 return container_of(vb, struct mcam_vb_buffer, vb_buf);
211}
212
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300213
214/*
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300215 * Debugging and related.
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300216 */
217#define cam_err(cam, fmt, arg...) \
218 dev_err((cam)->dev, fmt, ##arg);
219#define cam_warn(cam, fmt, arg...) \
220 dev_warn((cam)->dev, fmt, ##arg);
221#define cam_dbg(cam, fmt, arg...) \
222 dev_dbg((cam)->dev, fmt, ##arg);
223
224
225
226/* ------------------------------------------------------------------- */
227/*
228 * Deal with the controller.
229 */
230
231/*
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300232 * Set up DMA buffers when operating in vmalloc mode
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300233 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300234static void mcam_ctlr_dma_vmalloc(struct mcam_camera *cam)
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300235{
236 /*
237 * Store the first two Y buffers (we aren't supporting
238 * planar formats for now, so no UV bufs). Then either
239 * set the third if it exists, or tell the controller
240 * to just use two.
241 */
242 mcam_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
243 mcam_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
244 if (cam->nbufs > 2) {
245 mcam_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
246 mcam_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
247 } else
248 mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300249 if (cam->chip_id == V4L2_IDENT_CAFE)
250 mcam_reg_write(cam, REG_UBAR, 0); /* 32 bits only */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300251}
252
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300253/*
254 * Set up a contiguous buffer for the given frame. Here also is where
255 * the underrun strategy is set: if there is no buffer available, reuse
256 * the buffer from the other BAR and set the CF_SINGLE_BUFFER flag to
257 * keep the interrupt handler from giving that buffer back to user
258 * space. In this way, we always have a buffer to DMA to and don't
259 * have to try to play games stopping and restarting the controller.
260 */
261static void mcam_set_contig_buffer(struct mcam_camera *cam, int frame)
262{
263 struct mcam_vb_buffer *buf;
264 /*
265 * If there are no available buffers, go into single mode
266 */
267 if (list_empty(&cam->buffers)) {
268 buf = cam->vb_bufs[frame ^ 0x1];
269 cam->vb_bufs[frame] = buf;
270 mcam_reg_write(cam, frame == 0 ? REG_Y0BAR : REG_Y1BAR,
271 vb2_dma_contig_plane_paddr(&buf->vb_buf, 0));
272 set_bit(CF_SINGLE_BUFFER, &cam->flags);
273 singles++;
274 return;
275 }
276 /*
277 * OK, we have a buffer we can use.
278 */
279 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer, queue);
280 list_del_init(&buf->queue);
281 mcam_reg_write(cam, frame == 0 ? REG_Y0BAR : REG_Y1BAR,
282 vb2_dma_contig_plane_paddr(&buf->vb_buf, 0));
283 cam->vb_bufs[frame] = buf;
284 clear_bit(CF_SINGLE_BUFFER, &cam->flags);
285}
286
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300287/*
288 * Initial B_DMA_contig setup.
289 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300290static void mcam_ctlr_dma_contig(struct mcam_camera *cam)
291{
292 mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
293 cam->nbufs = 2;
294 mcam_set_contig_buffer(cam, 0);
295 mcam_set_contig_buffer(cam, 1);
296}
297
298
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300299/*
300 * Set up the next buffer for S/G I/O; caller should be sure that
301 * the controller is stopped and a buffer is available.
302 */
303static void mcam_sg_next_buffer(struct mcam_camera *cam)
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300304{
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300305 struct mcam_vb_buffer *buf;
306
307 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer, queue);
308 list_del_init(&buf->queue);
309 mcam_reg_write(cam, REG_DMA_DESC_Y, buf->dma_desc_pa);
310 mcam_reg_write(cam, REG_DESC_LEN_Y,
311 buf->dma_desc_nent*sizeof(struct mcam_dma_desc));
312 mcam_reg_write(cam, REG_DESC_LEN_U, 0);
313 mcam_reg_write(cam, REG_DESC_LEN_V, 0);
314 cam->vb_bufs[0] = buf;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300315}
316
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300317/*
318 * Initial B_DMA_sg setup
319 */
320static void mcam_ctlr_dma_sg(struct mcam_camera *cam)
321{
322 mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_3WORD);
323 mcam_sg_next_buffer(cam);
324 mcam_reg_set_bit(cam, REG_CTRL1, C1_DESC_ENA);
325 cam->nbufs = 3;
326}
327
328/*
329 * Image format setup, independent of DMA scheme.
330 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300331static void mcam_ctlr_image(struct mcam_camera *cam)
332{
333 int imgsz;
334 struct v4l2_pix_format *fmt = &cam->pix_format;
335
336 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
337 (fmt->bytesperline & IMGSZ_H_MASK);
338 mcam_reg_write(cam, REG_IMGSIZE, imgsz);
339 mcam_reg_write(cam, REG_IMGOFFSET, 0);
340 /* YPITCH just drops the last two bits */
341 mcam_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
342 IMGP_YP_MASK);
343 /*
344 * Tell the controller about the image format we are using.
345 */
346 switch (cam->pix_format.pixelformat) {
347 case V4L2_PIX_FMT_YUYV:
348 mcam_reg_write_mask(cam, REG_CTRL0,
349 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
350 C0_DF_MASK);
351 break;
352
353 case V4L2_PIX_FMT_RGB444:
354 mcam_reg_write_mask(cam, REG_CTRL0,
355 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
356 C0_DF_MASK);
357 /* Alpha value? */
358 break;
359
360 case V4L2_PIX_FMT_RGB565:
361 mcam_reg_write_mask(cam, REG_CTRL0,
362 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
363 C0_DF_MASK);
364 break;
365
366 default:
367 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
368 break;
369 }
370 /*
371 * Make sure it knows we want to use hsync/vsync.
372 */
373 mcam_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
374 C0_SIFM_MASK);
375}
376
377
378/*
379 * Configure the controller for operation; caller holds the
380 * device mutex.
381 */
382static int mcam_ctlr_configure(struct mcam_camera *cam)
383{
384 unsigned long flags;
385
386 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300387 switch (cam->buffer_mode) {
388 case B_vmalloc:
389 mcam_ctlr_dma_vmalloc(cam);
390 break;
391 case B_DMA_contig:
392 mcam_ctlr_dma_contig(cam);
393 break;
394 case B_DMA_sg:
395 mcam_ctlr_dma_sg(cam);
396 break;
397 }
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300398 mcam_ctlr_image(cam);
399 mcam_set_config_needed(cam, 0);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300400 clear_bit(CF_SG_RESTART, &cam->flags);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300401 spin_unlock_irqrestore(&cam->dev_lock, flags);
402 return 0;
403}
404
405static void mcam_ctlr_irq_enable(struct mcam_camera *cam)
406{
407 /*
408 * Clear any pending interrupts, since we do not
409 * expect to have I/O active prior to enabling.
410 */
411 mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
412 mcam_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
413}
414
415static void mcam_ctlr_irq_disable(struct mcam_camera *cam)
416{
417 mcam_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
418}
419
420/*
421 * Make the controller start grabbing images. Everything must
422 * be set up before doing this.
423 */
424static void mcam_ctlr_start(struct mcam_camera *cam)
425{
426 /* set_bit performs a read, so no other barrier should be
427 needed here */
428 mcam_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
429}
430
431static void mcam_ctlr_stop(struct mcam_camera *cam)
432{
433 mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
434}
435
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300436/*
437 * Scatter/gather mode requires stopping the controller between
438 * frames so we can put in a new DMA descriptor array. If no new
439 * buffer exists at frame completion, the controller is left stopped;
440 * this function is charged with gettig things going again.
441 */
442static void mcam_sg_restart(struct mcam_camera *cam)
443{
444 mcam_ctlr_dma_sg(cam);
445 mcam_ctlr_start(cam);
446 clear_bit(CF_SG_RESTART, &cam->flags);
447}
448
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300449static void mcam_ctlr_init(struct mcam_camera *cam)
450{
451 unsigned long flags;
452
453 spin_lock_irqsave(&cam->dev_lock, flags);
454 /*
455 * Make sure it's not powered down.
456 */
457 mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
458 /*
459 * Turn off the enable bit. It sure should be off anyway,
460 * but it's good to be sure.
461 */
462 mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
463 /*
464 * Clock the sensor appropriately. Controller clock should
465 * be 48MHz, sensor "typical" value is half that.
466 */
467 mcam_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
468 spin_unlock_irqrestore(&cam->dev_lock, flags);
469}
470
471
472/*
473 * Stop the controller, and don't return until we're really sure that no
474 * further DMA is going on.
475 */
476static void mcam_ctlr_stop_dma(struct mcam_camera *cam)
477{
478 unsigned long flags;
479
480 /*
481 * Theory: stop the camera controller (whether it is operating
482 * or not). Delay briefly just in case we race with the SOF
483 * interrupt, then wait until no DMA is active.
484 */
485 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300486 clear_bit(CF_SG_RESTART, &cam->flags);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300487 mcam_ctlr_stop(cam);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300488 cam->state = S_IDLE;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300489 spin_unlock_irqrestore(&cam->dev_lock, flags);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300490 msleep(40);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300491 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
492 cam_err(cam, "Timeout waiting for DMA to end\n");
493 /* This would be bad news - what now? */
494 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300495 mcam_ctlr_irq_disable(cam);
496 spin_unlock_irqrestore(&cam->dev_lock, flags);
497}
498
499/*
500 * Power up and down.
501 */
502static void mcam_ctlr_power_up(struct mcam_camera *cam)
503{
504 unsigned long flags;
505
506 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300507 cam->plat_power_up(cam);
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300508 mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300509 spin_unlock_irqrestore(&cam->dev_lock, flags);
510 msleep(5); /* Just to be sure */
511}
512
513static void mcam_ctlr_power_down(struct mcam_camera *cam)
514{
515 unsigned long flags;
516
517 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300518 /*
519 * School of hard knocks department: be sure we do any register
520 * twiddling on the controller *before* calling the platform
521 * power down routine.
522 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300523 mcam_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300524 cam->plat_power_down(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300525 spin_unlock_irqrestore(&cam->dev_lock, flags);
526}
527
528/* -------------------------------------------------------------------- */
529/*
530 * Communications with the sensor.
531 */
532
533static int __mcam_cam_reset(struct mcam_camera *cam)
534{
535 return sensor_call(cam, core, reset, 0);
536}
537
538/*
539 * We have found the sensor on the i2c. Let's try to have a
540 * conversation.
541 */
542static int mcam_cam_init(struct mcam_camera *cam)
543{
544 struct v4l2_dbg_chip_ident chip;
545 int ret;
546
547 mutex_lock(&cam->s_mutex);
548 if (cam->state != S_NOTREADY)
549 cam_warn(cam, "Cam init with device in funky state %d",
550 cam->state);
551 ret = __mcam_cam_reset(cam);
552 if (ret)
553 goto out;
554 chip.ident = V4L2_IDENT_NONE;
555 chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
556 chip.match.addr = cam->sensor_addr;
557 ret = sensor_call(cam, core, g_chip_ident, &chip);
558 if (ret)
559 goto out;
560 cam->sensor_type = chip.ident;
561 if (cam->sensor_type != V4L2_IDENT_OV7670) {
562 cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
563 ret = -EINVAL;
564 goto out;
565 }
566/* Get/set parameters? */
567 ret = 0;
568 cam->state = S_IDLE;
569out:
570 mcam_ctlr_power_down(cam);
571 mutex_unlock(&cam->s_mutex);
572 return ret;
573}
574
575/*
576 * Configure the sensor to match the parameters we have. Caller should
577 * hold s_mutex
578 */
579static int mcam_cam_set_flip(struct mcam_camera *cam)
580{
581 struct v4l2_control ctrl;
582
583 memset(&ctrl, 0, sizeof(ctrl));
584 ctrl.id = V4L2_CID_VFLIP;
585 ctrl.value = flip;
586 return sensor_call(cam, core, s_ctrl, &ctrl);
587}
588
589
590static int mcam_cam_configure(struct mcam_camera *cam)
591{
592 struct v4l2_mbus_framefmt mbus_fmt;
593 int ret;
594
595 v4l2_fill_mbus_format(&mbus_fmt, &cam->pix_format, cam->mbus_code);
596 ret = sensor_call(cam, core, init, 0);
597 if (ret == 0)
598 ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
599 /*
600 * OV7670 does weird things if flip is set *before* format...
601 */
602 ret += mcam_cam_set_flip(cam);
603 return ret;
604}
605
606/* -------------------------------------------------------------------- */
607/*
608 * DMA buffer management. These functions need s_mutex held.
609 */
610
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300611/*
612 * Allocate in-kernel DMA buffers for vmalloc mode.
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300613 */
614static int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
615{
616 int i;
617
618 mcam_set_config_needed(cam, 1);
619 if (loadtime)
620 cam->dma_buf_size = dma_buf_size;
621 else
622 cam->dma_buf_size = cam->pix_format.sizeimage;
623 if (n_dma_bufs > 3)
624 n_dma_bufs = 3;
625
626 cam->nbufs = 0;
627 for (i = 0; i < n_dma_bufs; i++) {
628 cam->dma_bufs[i] = dma_alloc_coherent(cam->dev,
629 cam->dma_buf_size, cam->dma_handles + i,
630 GFP_KERNEL);
631 if (cam->dma_bufs[i] == NULL) {
632 cam_warn(cam, "Failed to allocate DMA buffer\n");
633 break;
634 }
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300635 (cam->nbufs)++;
636 }
637
638 switch (cam->nbufs) {
639 case 1:
640 dma_free_coherent(cam->dev, cam->dma_buf_size,
641 cam->dma_bufs[0], cam->dma_handles[0]);
642 cam->nbufs = 0;
643 case 0:
644 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
645 return -ENOMEM;
646
647 case 2:
648 if (n_dma_bufs > 2)
649 cam_warn(cam, "Will limp along with only 2 buffers\n");
650 break;
651 }
652 return 0;
653}
654
655static void mcam_free_dma_bufs(struct mcam_camera *cam)
656{
657 int i;
658
659 for (i = 0; i < cam->nbufs; i++) {
660 dma_free_coherent(cam->dev, cam->dma_buf_size,
661 cam->dma_bufs[i], cam->dma_handles[i]);
662 cam->dma_bufs[i] = NULL;
663 }
664 cam->nbufs = 0;
665}
666
667
668
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300669/* ----------------------------------------------------------------------- */
670/*
671 * Here starts the V4L2 interface code.
672 */
673
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300674
675/*
676 * Get everything ready, and start grabbing frames.
677 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300678static int mcam_read_setup(struct mcam_camera *cam)
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300679{
680 int ret;
681 unsigned long flags;
682
683 /*
684 * Configuration. If we still don't have DMA buffers,
685 * make one last, desperate attempt.
686 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300687 if (cam->buffer_mode == B_vmalloc && cam->nbufs == 0 &&
688 mcam_alloc_dma_bufs(cam, 0))
689 return -ENOMEM;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300690
691 if (mcam_needs_config(cam)) {
692 mcam_cam_configure(cam);
693 ret = mcam_ctlr_configure(cam);
694 if (ret)
695 return ret;
696 }
697
698 /*
699 * Turn it loose.
700 */
701 spin_lock_irqsave(&cam->dev_lock, flags);
702 mcam_reset_buffers(cam);
703 mcam_ctlr_irq_enable(cam);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300704 cam->state = S_STREAMING;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300705 mcam_ctlr_start(cam);
706 spin_unlock_irqrestore(&cam->dev_lock, flags);
707 return 0;
708}
709
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300710/* ----------------------------------------------------------------------- */
711/*
712 * Videobuf2 interface code.
713 */
714
715static int mcam_vb_queue_setup(struct vb2_queue *vq, unsigned int *nbufs,
716 unsigned int *num_planes, unsigned long sizes[],
717 void *alloc_ctxs[])
718{
719 struct mcam_camera *cam = vb2_get_drv_priv(vq);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300720 int minbufs = (cam->buffer_mode == B_DMA_contig) ? 3 : 2;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300721
722 sizes[0] = cam->pix_format.sizeimage;
723 *num_planes = 1; /* Someday we have to support planar formats... */
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300724 if (*nbufs < minbufs)
725 *nbufs = minbufs;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300726 if (cam->buffer_mode == B_DMA_contig)
727 alloc_ctxs[0] = cam->vb_alloc_ctx;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300728 return 0;
729}
730
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300731/* DMA_sg only */
732static int mcam_vb_sg_buf_init(struct vb2_buffer *vb)
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300733{
734 struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300735 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
736 int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300737
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300738 mvb->dma_desc = dma_alloc_coherent(cam->dev,
739 ndesc * sizeof(struct mcam_dma_desc),
740 &mvb->dma_desc_pa, GFP_KERNEL);
741 if (mvb->dma_desc == NULL) {
742 cam_err(cam, "Unable to get DMA descriptor array\n");
743 return -ENOMEM;
744 }
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300745 return 0;
746}
747
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300748static int mcam_vb_sg_buf_prepare(struct vb2_buffer *vb)
749{
750 struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
751 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
752 struct vb2_dma_sg_desc *sgd = vb2_dma_sg_plane_desc(vb, 0);
753 struct mcam_dma_desc *desc = mvb->dma_desc;
754 struct scatterlist *sg;
755 int i;
756
757 mvb->dma_desc_nent = dma_map_sg(cam->dev, sgd->sglist, sgd->num_pages,
758 DMA_FROM_DEVICE);
759 if (mvb->dma_desc_nent <= 0)
760 return -EIO; /* Not sure what's right here */
761 for_each_sg(sgd->sglist, sg, mvb->dma_desc_nent, i) {
762 desc->dma_addr = sg_dma_address(sg);
763 desc->segment_len = sg_dma_len(sg);
764 desc++;
765 }
766 return 0;
767}
768
769
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300770static void mcam_vb_buf_queue(struct vb2_buffer *vb)
771{
772 struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
773 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
774 unsigned long flags;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300775 int start;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300776
777 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300778 start = (cam->state == S_BUFWAIT) && !list_empty(&cam->buffers);
779 list_add(&mvb->queue, &cam->buffers);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300780 if (test_bit(CF_SG_RESTART, &cam->flags))
781 mcam_sg_restart(cam);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300782 spin_unlock_irqrestore(&cam->dev_lock, flags);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300783 if (start)
784 mcam_read_setup(cam);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300785}
786
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300787
788static int mcam_vb_sg_buf_finish(struct vb2_buffer *vb)
789{
790 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
791 struct vb2_dma_sg_desc *sgd = vb2_dma_sg_plane_desc(vb, 0);
792
793 dma_unmap_sg(cam->dev, sgd->sglist, sgd->num_pages, DMA_FROM_DEVICE);
794 return 0;
795}
796
797static void mcam_vb_sg_buf_cleanup(struct vb2_buffer *vb)
798{
799 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
800 struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
801 int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;
802
803 dma_free_coherent(cam->dev, ndesc * sizeof(struct mcam_dma_desc),
804 mvb->dma_desc, mvb->dma_desc_pa);
805}
806
807
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300808/*
809 * vb2 uses these to release the mutex when waiting in dqbuf. I'm
810 * not actually sure we need to do this (I'm not sure that vb2_dqbuf() needs
811 * to be called with the mutex held), but better safe than sorry.
812 */
813static void mcam_vb_wait_prepare(struct vb2_queue *vq)
814{
815 struct mcam_camera *cam = vb2_get_drv_priv(vq);
816
817 mutex_unlock(&cam->s_mutex);
818}
819
820static void mcam_vb_wait_finish(struct vb2_queue *vq)
821{
822 struct mcam_camera *cam = vb2_get_drv_priv(vq);
823
824 mutex_lock(&cam->s_mutex);
825}
826
827/*
828 * These need to be called with the mutex held from vb2
829 */
830static int mcam_vb_start_streaming(struct vb2_queue *vq)
831{
832 struct mcam_camera *cam = vb2_get_drv_priv(vq);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300833
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300834 if (cam->state != S_IDLE)
835 return -EINVAL;
836 cam->sequence = 0;
837 /*
838 * Videobuf2 sneakily hoards all the buffers and won't
839 * give them to us until *after* streaming starts. But
840 * we can't actually start streaming until we have a
841 * destination. So go into a wait state and hope they
842 * give us buffers soon.
843 */
844 if (cam->buffer_mode != B_vmalloc && list_empty(&cam->buffers)) {
845 cam->state = S_BUFWAIT;
846 return 0;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300847 }
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300848 return mcam_read_setup(cam);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300849}
850
851static int mcam_vb_stop_streaming(struct vb2_queue *vq)
852{
853 struct mcam_camera *cam = vb2_get_drv_priv(vq);
854 unsigned long flags;
855
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300856 if (cam->state == S_BUFWAIT) {
857 /* They never gave us buffers */
858 cam->state = S_IDLE;
859 return 0;
860 }
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300861 if (cam->state != S_STREAMING)
862 return -EINVAL;
863 mcam_ctlr_stop_dma(cam);
864 /*
865 * VB2 reclaims the buffers, so we need to forget
866 * about them.
867 */
868 spin_lock_irqsave(&cam->dev_lock, flags);
869 INIT_LIST_HEAD(&cam->buffers);
870 spin_unlock_irqrestore(&cam->dev_lock, flags);
871 return 0;
872}
873
874
875static const struct vb2_ops mcam_vb2_ops = {
876 .queue_setup = mcam_vb_queue_setup,
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300877 .buf_queue = mcam_vb_buf_queue,
878 .start_streaming = mcam_vb_start_streaming,
879 .stop_streaming = mcam_vb_stop_streaming,
880 .wait_prepare = mcam_vb_wait_prepare,
881 .wait_finish = mcam_vb_wait_finish,
882};
883
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300884/*
885 * Scatter/gather mode complicates things somewhat.
886 */
887static const struct vb2_ops mcam_vb2_sg_ops = {
888 .queue_setup = mcam_vb_queue_setup,
889 .buf_init = mcam_vb_sg_buf_init,
890 .buf_prepare = mcam_vb_sg_buf_prepare,
891 .buf_queue = mcam_vb_buf_queue,
892 .buf_finish = mcam_vb_sg_buf_finish,
893 .buf_cleanup = mcam_vb_sg_buf_cleanup,
894 .start_streaming = mcam_vb_start_streaming,
895 .stop_streaming = mcam_vb_stop_streaming,
896 .wait_prepare = mcam_vb_wait_prepare,
897 .wait_finish = mcam_vb_wait_finish,
898};
899
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300900static int mcam_setup_vb2(struct mcam_camera *cam)
901{
902 struct vb2_queue *vq = &cam->vb_queue;
903
904 memset(vq, 0, sizeof(*vq));
905 vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300906 vq->drv_priv = cam;
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300907 INIT_LIST_HEAD(&cam->buffers);
908 switch (cam->buffer_mode) {
909 case B_DMA_contig:
910 vq->ops = &mcam_vb2_ops;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300911 vq->mem_ops = &vb2_dma_contig_memops;
912 cam->vb_alloc_ctx = vb2_dma_contig_init_ctx(cam->dev);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300913 vq->io_modes = VB2_MMAP | VB2_USERPTR;
914 break;
915 case B_DMA_sg:
916 vq->ops = &mcam_vb2_sg_ops;
917 vq->mem_ops = &vb2_dma_sg_memops;
918 vq->io_modes = VB2_MMAP | VB2_USERPTR;
919 break;
920 case B_vmalloc:
921 vq->ops = &mcam_vb2_ops;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300922 vq->mem_ops = &vb2_vmalloc_memops;
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300923 vq->buf_struct_size = sizeof(struct mcam_vb_buffer);
924 vq->io_modes = VB2_MMAP;
925 break;
926 }
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300927 return vb2_queue_init(vq);
928}
929
930static void mcam_cleanup_vb2(struct mcam_camera *cam)
931{
932 vb2_queue_release(&cam->vb_queue);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300933 if (cam->buffer_mode == B_DMA_contig)
934 vb2_dma_contig_cleanup_ctx(cam->vb_alloc_ctx);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300935}
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300936
937static ssize_t mcam_v4l_read(struct file *filp,
938 char __user *buffer, size_t len, loff_t *pos)
939{
940 struct mcam_camera *cam = filp->private_data;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300941 int ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300942
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300943 mutex_lock(&cam->s_mutex);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300944 ret = vb2_read(&cam->vb_queue, buffer, len, pos,
945 filp->f_flags & O_NONBLOCK);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300946 mutex_unlock(&cam->s_mutex);
947 return ret;
948}
949
950
951
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300952/*
953 * Streaming I/O support.
954 */
955
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300956static int mcam_vidioc_streamon(struct file *filp, void *priv,
957 enum v4l2_buf_type type)
958{
959 struct mcam_camera *cam = filp->private_data;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300960 int ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300961
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300962 mutex_lock(&cam->s_mutex);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300963 ret = vb2_streamon(&cam->vb_queue, type);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300964 mutex_unlock(&cam->s_mutex);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300965 return ret;
966}
967
968
969static int mcam_vidioc_streamoff(struct file *filp, void *priv,
970 enum v4l2_buf_type type)
971{
972 struct mcam_camera *cam = filp->private_data;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300973 int ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300974
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300975 mutex_lock(&cam->s_mutex);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300976 ret = vb2_streamoff(&cam->vb_queue, type);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300977 mutex_unlock(&cam->s_mutex);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300978 return ret;
979}
980
981
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300982static int mcam_vidioc_reqbufs(struct file *filp, void *priv,
983 struct v4l2_requestbuffers *req)
984{
985 struct mcam_camera *cam = filp->private_data;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300986 int ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300987
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300988 mutex_lock(&cam->s_mutex);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300989 ret = vb2_reqbufs(&cam->vb_queue, req);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300990 mutex_unlock(&cam->s_mutex);
991 return ret;
992}
993
994
995static int mcam_vidioc_querybuf(struct file *filp, void *priv,
996 struct v4l2_buffer *buf)
997{
998 struct mcam_camera *cam = filp->private_data;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300999 int ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001000
1001 mutex_lock(&cam->s_mutex);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001002 ret = vb2_querybuf(&cam->vb_queue, buf);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001003 mutex_unlock(&cam->s_mutex);
1004 return ret;
1005}
1006
1007static int mcam_vidioc_qbuf(struct file *filp, void *priv,
1008 struct v4l2_buffer *buf)
1009{
1010 struct mcam_camera *cam = filp->private_data;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001011 int ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001012
1013 mutex_lock(&cam->s_mutex);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001014 ret = vb2_qbuf(&cam->vb_queue, buf);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001015 mutex_unlock(&cam->s_mutex);
1016 return ret;
1017}
1018
1019static int mcam_vidioc_dqbuf(struct file *filp, void *priv,
1020 struct v4l2_buffer *buf)
1021{
1022 struct mcam_camera *cam = filp->private_data;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001023 int ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001024
1025 mutex_lock(&cam->s_mutex);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001026 ret = vb2_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001027 mutex_unlock(&cam->s_mutex);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001028 return ret;
1029}
1030
1031
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001032static int mcam_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1033{
1034 struct mcam_camera *cam = filp->private_data;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001035 int ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001036
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001037 mutex_lock(&cam->s_mutex);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001038 ret = vb2_mmap(&cam->vb_queue, vma);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001039 mutex_unlock(&cam->s_mutex);
1040 return ret;
1041}
1042
1043
1044
1045static int mcam_v4l_open(struct file *filp)
1046{
1047 struct mcam_camera *cam = video_drvdata(filp);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001048 int ret = 0;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001049
1050 filp->private_data = cam;
1051
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001052 frames = singles = delivered = 0;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001053 mutex_lock(&cam->s_mutex);
1054 if (cam->users == 0) {
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001055 ret = mcam_setup_vb2(cam);
1056 if (ret)
1057 goto out;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001058 mcam_ctlr_power_up(cam);
1059 __mcam_cam_reset(cam);
1060 mcam_set_config_needed(cam, 1);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001061 }
1062 (cam->users)++;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001063out:
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001064 mutex_unlock(&cam->s_mutex);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001065 return ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001066}
1067
1068
1069static int mcam_v4l_release(struct file *filp)
1070{
1071 struct mcam_camera *cam = filp->private_data;
1072
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001073 cam_err(cam, "Release, %d frames, %d singles, %d delivered\n", frames,
1074 singles, delivered);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001075 mutex_lock(&cam->s_mutex);
1076 (cam->users)--;
1077 if (filp == cam->owner) {
1078 mcam_ctlr_stop_dma(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001079 cam->owner = NULL;
1080 }
1081 if (cam->users == 0) {
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001082 mcam_cleanup_vb2(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001083 mcam_ctlr_power_down(cam);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001084 if (cam->buffer_mode == B_vmalloc && alloc_bufs_at_read)
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001085 mcam_free_dma_bufs(cam);
1086 }
1087 mutex_unlock(&cam->s_mutex);
1088 return 0;
1089}
1090
1091
1092
1093static unsigned int mcam_v4l_poll(struct file *filp,
1094 struct poll_table_struct *pt)
1095{
1096 struct mcam_camera *cam = filp->private_data;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001097 int ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001098
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001099 mutex_lock(&cam->s_mutex);
1100 ret = vb2_poll(&cam->vb_queue, filp, pt);
1101 mutex_unlock(&cam->s_mutex);
1102 return ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001103}
1104
1105
1106
1107static int mcam_vidioc_queryctrl(struct file *filp, void *priv,
1108 struct v4l2_queryctrl *qc)
1109{
1110 struct mcam_camera *cam = priv;
1111 int ret;
1112
1113 mutex_lock(&cam->s_mutex);
1114 ret = sensor_call(cam, core, queryctrl, qc);
1115 mutex_unlock(&cam->s_mutex);
1116 return ret;
1117}
1118
1119
1120static int mcam_vidioc_g_ctrl(struct file *filp, void *priv,
1121 struct v4l2_control *ctrl)
1122{
1123 struct mcam_camera *cam = priv;
1124 int ret;
1125
1126 mutex_lock(&cam->s_mutex);
1127 ret = sensor_call(cam, core, g_ctrl, ctrl);
1128 mutex_unlock(&cam->s_mutex);
1129 return ret;
1130}
1131
1132
1133static int mcam_vidioc_s_ctrl(struct file *filp, void *priv,
1134 struct v4l2_control *ctrl)
1135{
1136 struct mcam_camera *cam = priv;
1137 int ret;
1138
1139 mutex_lock(&cam->s_mutex);
1140 ret = sensor_call(cam, core, s_ctrl, ctrl);
1141 mutex_unlock(&cam->s_mutex);
1142 return ret;
1143}
1144
1145
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001146static int mcam_vidioc_querycap(struct file *file, void *priv,
1147 struct v4l2_capability *cap)
1148{
1149 strcpy(cap->driver, "marvell_ccic");
1150 strcpy(cap->card, "marvell_ccic");
1151 cap->version = 1;
1152 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1153 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1154 return 0;
1155}
1156
1157
1158/*
1159 * The default format we use until somebody says otherwise.
1160 */
1161static const struct v4l2_pix_format mcam_def_pix_format = {
1162 .width = VGA_WIDTH,
1163 .height = VGA_HEIGHT,
1164 .pixelformat = V4L2_PIX_FMT_YUYV,
1165 .field = V4L2_FIELD_NONE,
1166 .bytesperline = VGA_WIDTH*2,
1167 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
1168};
1169
1170static const enum v4l2_mbus_pixelcode mcam_def_mbus_code =
1171 V4L2_MBUS_FMT_YUYV8_2X8;
1172
1173static int mcam_vidioc_enum_fmt_vid_cap(struct file *filp,
1174 void *priv, struct v4l2_fmtdesc *fmt)
1175{
1176 if (fmt->index >= N_MCAM_FMTS)
1177 return -EINVAL;
1178 strlcpy(fmt->description, mcam_formats[fmt->index].desc,
1179 sizeof(fmt->description));
1180 fmt->pixelformat = mcam_formats[fmt->index].pixelformat;
1181 return 0;
1182}
1183
1184static int mcam_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
1185 struct v4l2_format *fmt)
1186{
1187 struct mcam_camera *cam = priv;
1188 struct mcam_format_struct *f;
1189 struct v4l2_pix_format *pix = &fmt->fmt.pix;
1190 struct v4l2_mbus_framefmt mbus_fmt;
1191 int ret;
1192
1193 f = mcam_find_format(pix->pixelformat);
1194 pix->pixelformat = f->pixelformat;
1195 v4l2_fill_mbus_format(&mbus_fmt, pix, f->mbus_code);
1196 mutex_lock(&cam->s_mutex);
1197 ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
1198 mutex_unlock(&cam->s_mutex);
1199 v4l2_fill_pix_format(pix, &mbus_fmt);
1200 pix->bytesperline = pix->width * f->bpp;
1201 pix->sizeimage = pix->height * pix->bytesperline;
1202 return ret;
1203}
1204
1205static int mcam_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
1206 struct v4l2_format *fmt)
1207{
1208 struct mcam_camera *cam = priv;
1209 struct mcam_format_struct *f;
1210 int ret;
1211
1212 /*
1213 * Can't do anything if the device is not idle
1214 * Also can't if there are streaming buffers in place.
1215 */
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001216 if (cam->state != S_IDLE || cam->vb_queue.num_buffers > 0)
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001217 return -EBUSY;
1218
1219 f = mcam_find_format(fmt->fmt.pix.pixelformat);
1220
1221 /*
1222 * See if the formatting works in principle.
1223 */
1224 ret = mcam_vidioc_try_fmt_vid_cap(filp, priv, fmt);
1225 if (ret)
1226 return ret;
1227 /*
1228 * Now we start to change things for real, so let's do it
1229 * under lock.
1230 */
1231 mutex_lock(&cam->s_mutex);
1232 cam->pix_format = fmt->fmt.pix;
1233 cam->mbus_code = f->mbus_code;
1234
1235 /*
1236 * Make sure we have appropriate DMA buffers.
1237 */
1238 ret = -ENOMEM;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001239 if (cam->buffer_mode == B_vmalloc) {
1240 if (cam->nbufs > 0 &&
1241 cam->dma_buf_size < cam->pix_format.sizeimage)
1242 mcam_free_dma_bufs(cam);
1243 if (cam->nbufs == 0) {
1244 if (mcam_alloc_dma_bufs(cam, 0))
1245 goto out;
1246 }
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001247 }
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001248 mcam_set_config_needed(cam, 1);
1249 ret = 0;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001250out:
1251 mutex_unlock(&cam->s_mutex);
1252 return ret;
1253}
1254
1255/*
1256 * Return our stored notion of how the camera is/should be configured.
1257 * The V4l2 spec wants us to be smarter, and actually get this from
1258 * the camera (and not mess with it at open time). Someday.
1259 */
1260static int mcam_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
1261 struct v4l2_format *f)
1262{
1263 struct mcam_camera *cam = priv;
1264
1265 f->fmt.pix = cam->pix_format;
1266 return 0;
1267}
1268
1269/*
1270 * We only have one input - the sensor - so minimize the nonsense here.
1271 */
1272static int mcam_vidioc_enum_input(struct file *filp, void *priv,
1273 struct v4l2_input *input)
1274{
1275 if (input->index != 0)
1276 return -EINVAL;
1277
1278 input->type = V4L2_INPUT_TYPE_CAMERA;
1279 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1280 strcpy(input->name, "Camera");
1281 return 0;
1282}
1283
1284static int mcam_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1285{
1286 *i = 0;
1287 return 0;
1288}
1289
1290static int mcam_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1291{
1292 if (i != 0)
1293 return -EINVAL;
1294 return 0;
1295}
1296
1297/* from vivi.c */
1298static int mcam_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1299{
1300 return 0;
1301}
1302
1303/*
1304 * G/S_PARM. Most of this is done by the sensor, but we are
1305 * the level which controls the number of read buffers.
1306 */
1307static int mcam_vidioc_g_parm(struct file *filp, void *priv,
1308 struct v4l2_streamparm *parms)
1309{
1310 struct mcam_camera *cam = priv;
1311 int ret;
1312
1313 mutex_lock(&cam->s_mutex);
1314 ret = sensor_call(cam, video, g_parm, parms);
1315 mutex_unlock(&cam->s_mutex);
1316 parms->parm.capture.readbuffers = n_dma_bufs;
1317 return ret;
1318}
1319
1320static int mcam_vidioc_s_parm(struct file *filp, void *priv,
1321 struct v4l2_streamparm *parms)
1322{
1323 struct mcam_camera *cam = priv;
1324 int ret;
1325
1326 mutex_lock(&cam->s_mutex);
1327 ret = sensor_call(cam, video, s_parm, parms);
1328 mutex_unlock(&cam->s_mutex);
1329 parms->parm.capture.readbuffers = n_dma_bufs;
1330 return ret;
1331}
1332
1333static int mcam_vidioc_g_chip_ident(struct file *file, void *priv,
1334 struct v4l2_dbg_chip_ident *chip)
1335{
1336 struct mcam_camera *cam = priv;
1337
1338 chip->ident = V4L2_IDENT_NONE;
1339 chip->revision = 0;
1340 if (v4l2_chip_match_host(&chip->match)) {
1341 chip->ident = cam->chip_id;
1342 return 0;
1343 }
1344 return sensor_call(cam, core, g_chip_ident, chip);
1345}
1346
1347static int mcam_vidioc_enum_framesizes(struct file *filp, void *priv,
1348 struct v4l2_frmsizeenum *sizes)
1349{
1350 struct mcam_camera *cam = priv;
1351 int ret;
1352
1353 mutex_lock(&cam->s_mutex);
1354 ret = sensor_call(cam, video, enum_framesizes, sizes);
1355 mutex_unlock(&cam->s_mutex);
1356 return ret;
1357}
1358
1359static int mcam_vidioc_enum_frameintervals(struct file *filp, void *priv,
1360 struct v4l2_frmivalenum *interval)
1361{
1362 struct mcam_camera *cam = priv;
1363 int ret;
1364
1365 mutex_lock(&cam->s_mutex);
1366 ret = sensor_call(cam, video, enum_frameintervals, interval);
1367 mutex_unlock(&cam->s_mutex);
1368 return ret;
1369}
1370
1371#ifdef CONFIG_VIDEO_ADV_DEBUG
1372static int mcam_vidioc_g_register(struct file *file, void *priv,
1373 struct v4l2_dbg_register *reg)
1374{
1375 struct mcam_camera *cam = priv;
1376
1377 if (v4l2_chip_match_host(&reg->match)) {
1378 reg->val = mcam_reg_read(cam, reg->reg);
1379 reg->size = 4;
1380 return 0;
1381 }
1382 return sensor_call(cam, core, g_register, reg);
1383}
1384
1385static int mcam_vidioc_s_register(struct file *file, void *priv,
1386 struct v4l2_dbg_register *reg)
1387{
1388 struct mcam_camera *cam = priv;
1389
1390 if (v4l2_chip_match_host(&reg->match)) {
1391 mcam_reg_write(cam, reg->reg, reg->val);
1392 return 0;
1393 }
1394 return sensor_call(cam, core, s_register, reg);
1395}
1396#endif
1397
1398/*
1399 * This template device holds all of those v4l2 methods; we
1400 * clone it for specific real devices.
1401 */
1402
1403static const struct v4l2_file_operations mcam_v4l_fops = {
1404 .owner = THIS_MODULE,
1405 .open = mcam_v4l_open,
1406 .release = mcam_v4l_release,
1407 .read = mcam_v4l_read,
1408 .poll = mcam_v4l_poll,
1409 .mmap = mcam_v4l_mmap,
1410 .unlocked_ioctl = video_ioctl2,
1411};
1412
1413static const struct v4l2_ioctl_ops mcam_v4l_ioctl_ops = {
1414 .vidioc_querycap = mcam_vidioc_querycap,
1415 .vidioc_enum_fmt_vid_cap = mcam_vidioc_enum_fmt_vid_cap,
1416 .vidioc_try_fmt_vid_cap = mcam_vidioc_try_fmt_vid_cap,
1417 .vidioc_s_fmt_vid_cap = mcam_vidioc_s_fmt_vid_cap,
1418 .vidioc_g_fmt_vid_cap = mcam_vidioc_g_fmt_vid_cap,
1419 .vidioc_enum_input = mcam_vidioc_enum_input,
1420 .vidioc_g_input = mcam_vidioc_g_input,
1421 .vidioc_s_input = mcam_vidioc_s_input,
1422 .vidioc_s_std = mcam_vidioc_s_std,
1423 .vidioc_reqbufs = mcam_vidioc_reqbufs,
1424 .vidioc_querybuf = mcam_vidioc_querybuf,
1425 .vidioc_qbuf = mcam_vidioc_qbuf,
1426 .vidioc_dqbuf = mcam_vidioc_dqbuf,
1427 .vidioc_streamon = mcam_vidioc_streamon,
1428 .vidioc_streamoff = mcam_vidioc_streamoff,
1429 .vidioc_queryctrl = mcam_vidioc_queryctrl,
1430 .vidioc_g_ctrl = mcam_vidioc_g_ctrl,
1431 .vidioc_s_ctrl = mcam_vidioc_s_ctrl,
1432 .vidioc_g_parm = mcam_vidioc_g_parm,
1433 .vidioc_s_parm = mcam_vidioc_s_parm,
1434 .vidioc_enum_framesizes = mcam_vidioc_enum_framesizes,
1435 .vidioc_enum_frameintervals = mcam_vidioc_enum_frameintervals,
1436 .vidioc_g_chip_ident = mcam_vidioc_g_chip_ident,
1437#ifdef CONFIG_VIDEO_ADV_DEBUG
1438 .vidioc_g_register = mcam_vidioc_g_register,
1439 .vidioc_s_register = mcam_vidioc_s_register,
1440#endif
1441};
1442
1443static struct video_device mcam_v4l_template = {
1444 .name = "mcam",
1445 .tvnorms = V4L2_STD_NTSC_M,
1446 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1447
1448 .fops = &mcam_v4l_fops,
1449 .ioctl_ops = &mcam_v4l_ioctl_ops,
1450 .release = video_device_release_empty,
1451};
1452
1453/* ---------------------------------------------------------------------- */
1454/*
1455 * Interrupt handler stuff
1456 */
1457
1458
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001459static void mcam_buffer_done(struct mcam_camera *cam, int frame,
1460 struct vb2_buffer *vbuf)
1461{
1462 vbuf->v4l2_buf.bytesused = cam->pix_format.sizeimage;
1463 vbuf->v4l2_buf.sequence = cam->buf_seq[frame];
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001464 vb2_set_plane_payload(vbuf, 0, cam->pix_format.sizeimage);
1465 vb2_buffer_done(vbuf, VB2_BUF_STATE_DONE);
1466}
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001467
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001468/*
1469 * Copy data out to user space in the vmalloc case
1470 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001471static void mcam_frame_tasklet(unsigned long data)
1472{
1473 struct mcam_camera *cam = (struct mcam_camera *) data;
1474 int i;
1475 unsigned long flags;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001476 struct mcam_vb_buffer *buf;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001477
1478 spin_lock_irqsave(&cam->dev_lock, flags);
1479 for (i = 0; i < cam->nbufs; i++) {
1480 int bufno = cam->next_buf;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001481
1482 if (cam->state != S_STREAMING || bufno < 0)
1483 break; /* I/O got stopped */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001484 if (++(cam->next_buf) >= cam->nbufs)
1485 cam->next_buf = 0;
1486 if (!test_bit(bufno, &cam->flags))
1487 continue;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001488 if (list_empty(&cam->buffers)) {
1489 singles++;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001490 break; /* Leave it valid, hope for better later */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001491 }
1492 delivered++;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001493 clear_bit(bufno, &cam->flags);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001494 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer,
1495 queue);
1496 list_del_init(&buf->queue);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001497 /*
1498 * Drop the lock during the big copy. This *should* be safe...
1499 */
1500 spin_unlock_irqrestore(&cam->dev_lock, flags);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001501 memcpy(vb2_plane_vaddr(&buf->vb_buf, 0), cam->dma_bufs[bufno],
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001502 cam->pix_format.sizeimage);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001503 mcam_buffer_done(cam, bufno, &buf->vb_buf);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001504 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001505 }
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001506 spin_unlock_irqrestore(&cam->dev_lock, flags);
1507}
1508
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001509/*
1510 * For direct DMA, mark the buffer ready and set up another one.
1511 */
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001512static void mcam_dma_contig_done(struct mcam_camera *cam, int frame)
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001513{
1514 struct mcam_vb_buffer *buf = cam->vb_bufs[frame];
1515
1516 if (!test_bit(CF_SINGLE_BUFFER, &cam->flags)) {
1517 delivered++;
1518 mcam_buffer_done(cam, frame, &buf->vb_buf);
1519 }
1520 mcam_set_contig_buffer(cam, frame);
1521}
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001522
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001523/*
1524 * Frame completion with S/G is trickier. We can't muck with
1525 * a descriptor chain on the fly, since the controller buffers it
1526 * internally. So we have to actually stop and restart; Marvell
1527 * says this is the way to do it.
1528 *
1529 * Of course, stopping is easier said than done; experience shows
1530 * that the controller can start a frame *after* C0_ENABLE has been
1531 * cleared. So when running in S/G mode, the controller is "stopped"
1532 * on receipt of the start-of-frame interrupt. That means we can
1533 * safely change the DMA descriptor array here and restart things
1534 * (assuming there's another buffer waiting to go).
1535 */
1536static void mcam_dma_sg_done(struct mcam_camera *cam, int frame)
1537{
1538 struct mcam_vb_buffer *buf = cam->vb_bufs[0];
1539
1540 /*
1541 * Very Bad Not Good Things happen if you don't clear
1542 * C1_DESC_ENA before making any descriptor changes.
1543 */
1544 mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_ENA);
1545 /*
1546 * If we have another buffer available, put it in and
1547 * restart the engine.
1548 */
1549 if (!list_empty(&cam->buffers)) {
1550 mcam_sg_next_buffer(cam);
1551 mcam_reg_set_bit(cam, REG_CTRL1, C1_DESC_ENA);
1552 mcam_ctlr_start(cam);
1553 /*
1554 * Otherwise set CF_SG_RESTART and the controller will
1555 * be restarted once another buffer shows up.
1556 */
1557 } else {
1558 set_bit(CF_SG_RESTART, &cam->flags);
1559 singles++;
1560 }
1561 /*
1562 * Now we can give the completed frame back to user space.
1563 */
1564 delivered++;
1565 mcam_buffer_done(cam, frame, &buf->vb_buf);
1566}
1567
1568
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001569
1570static void mcam_frame_complete(struct mcam_camera *cam, int frame)
1571{
1572 /*
1573 * Basic frame housekeeping.
1574 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001575 set_bit(frame, &cam->flags);
1576 clear_bit(CF_DMA_ACTIVE, &cam->flags);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001577 cam->next_buf = frame;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001578 cam->buf_seq[frame] = ++(cam->sequence);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001579 cam->last_delivered = frame;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001580 frames++;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001581 /*
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001582 * "This should never happen"
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001583 */
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001584 if (cam->state != S_STREAMING)
1585 return;
1586 /*
1587 * Process the frame and set up the next one.
1588 */
1589 switch (cam->buffer_mode) {
1590 case B_vmalloc:
1591 tasklet_schedule(&cam->s_tasklet);
1592 break;
1593 case B_DMA_contig:
1594 mcam_dma_contig_done(cam, frame);
1595 break;
1596 case B_DMA_sg:
1597 mcam_dma_sg_done(cam, frame);
1598 break;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001599 }
1600}
1601
1602
1603
1604
1605int mccic_irq(struct mcam_camera *cam, unsigned int irqs)
1606{
1607 unsigned int frame, handled = 0;
1608
1609 mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1610 /*
1611 * Handle any frame completions. There really should
1612 * not be more than one of these, or we have fallen
1613 * far behind.
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001614 *
1615 * When running in S/G mode, the frame number lacks any
1616 * real meaning - there's only one descriptor array - but
1617 * the controller still picks a different one to signal
1618 * each time.
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001619 */
1620 for (frame = 0; frame < cam->nbufs; frame++)
1621 if (irqs & (IRQ_EOF0 << frame)) {
1622 mcam_frame_complete(cam, frame);
1623 handled = 1;
1624 }
1625 /*
1626 * If a frame starts, note that we have DMA active. This
1627 * code assumes that we won't get multiple frame interrupts
1628 * at once; may want to rethink that.
1629 */
1630 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2)) {
1631 set_bit(CF_DMA_ACTIVE, &cam->flags);
1632 handled = 1;
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001633 if (cam->buffer_mode == B_DMA_sg)
1634 mcam_ctlr_stop(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001635 }
1636 return handled;
1637}
1638
1639/*
1640 * Registration and such.
1641 */
1642
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001643static struct ov7670_config sensor_cfg = {
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001644 /*
1645 * Exclude QCIF mode, because it only captures a tiny portion
1646 * of the sensor FOV
1647 */
1648 .min_width = 320,
1649 .min_height = 240,
1650};
1651
1652
1653int mccic_register(struct mcam_camera *cam)
1654{
1655 struct i2c_board_info ov7670_info = {
1656 .type = "ov7670",
Jonathan Corbet1c68f882011-06-11 14:46:47 -03001657 .addr = 0x42 >> 1,
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001658 .platform_data = &sensor_cfg,
1659 };
1660 int ret;
1661
1662 /*
1663 * Register with V4L
1664 */
1665 ret = v4l2_device_register(cam->dev, &cam->v4l2_dev);
1666 if (ret)
1667 return ret;
1668
1669 mutex_init(&cam->s_mutex);
1670 cam->state = S_NOTREADY;
1671 mcam_set_config_needed(cam, 1);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001672 cam->pix_format = mcam_def_pix_format;
1673 cam->mbus_code = mcam_def_mbus_code;
1674 INIT_LIST_HEAD(&cam->dev_list);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001675 INIT_LIST_HEAD(&cam->buffers);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001676 tasklet_init(&cam->s_tasklet, mcam_frame_tasklet, (unsigned long) cam);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001677 /*
1678 * User space may want to override the asked-for buffer mode;
1679 * here's hoping they know what they're doing.
1680 */
1681 if (buffer_mode == 0)
1682 cam->buffer_mode = B_vmalloc;
1683 else if (buffer_mode == 1)
1684 cam->buffer_mode = B_DMA_contig;
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001685 else if (buffer_mode == 2) {
1686 if (cam->chip_id == V4L2_IDENT_ARMADA610)
1687 cam->buffer_mode = B_DMA_sg;
1688 else {
1689 printk(KERN_ERR "marvell-cam: Cafe can't do S/G I/O\n");
1690 cam->buffer_mode = B_vmalloc;
1691 }
1692 } else if (buffer_mode != -1)
1693 printk(KERN_ERR "marvell-cam: "
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001694 "Strange module buffer mode %d - ignoring\n",
1695 buffer_mode);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001696 mcam_ctlr_init(cam);
1697
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001698 /*
1699 * Try to find the sensor.
1700 */
Jonathan Corbet2164b5a2011-06-11 14:46:44 -03001701 sensor_cfg.clock_speed = cam->clock_speed;
1702 sensor_cfg.use_smbus = cam->use_smbus;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001703 cam->sensor_addr = ov7670_info.addr;
1704 cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev,
Jonathan Corbet595a93a2011-06-11 14:46:48 -03001705 cam->i2c_adapter, &ov7670_info, NULL);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001706 if (cam->sensor == NULL) {
1707 ret = -ENODEV;
1708 goto out_unregister;
1709 }
1710
1711 ret = mcam_cam_init(cam);
1712 if (ret)
1713 goto out_unregister;
1714 /*
1715 * Get the v4l2 setup done.
1716 */
1717 mutex_lock(&cam->s_mutex);
1718 cam->vdev = mcam_v4l_template;
1719 cam->vdev.debug = 0;
1720 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1721 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1722 if (ret)
1723 goto out;
1724 video_set_drvdata(&cam->vdev, cam);
1725
1726 /*
1727 * If so requested, try to get our DMA buffers now.
1728 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001729 if (cam->buffer_mode == B_vmalloc && !alloc_bufs_at_read) {
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001730 if (mcam_alloc_dma_bufs(cam, 1))
1731 cam_warn(cam, "Unable to alloc DMA buffers at load"
1732 " will try again later.");
1733 }
1734
1735out:
1736 mutex_unlock(&cam->s_mutex);
1737 return ret;
1738out_unregister:
1739 v4l2_device_unregister(&cam->v4l2_dev);
1740 return ret;
1741}
1742
1743
1744void mccic_shutdown(struct mcam_camera *cam)
1745{
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -03001746 /*
1747 * If we have no users (and we really, really should have no
1748 * users) the device will already be powered down. Trying to
1749 * take it down again will wedge the machine, which is frowned
1750 * upon.
1751 */
1752 if (cam->users > 0) {
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001753 cam_warn(cam, "Removing a device with users!\n");
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -03001754 mcam_ctlr_power_down(cam);
1755 }
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001756 vb2_queue_release(&cam->vb_queue);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001757 if (cam->buffer_mode == B_vmalloc)
1758 mcam_free_dma_bufs(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001759 video_unregister_device(&cam->vdev);
1760 v4l2_device_unregister(&cam->v4l2_dev);
1761}
1762
1763/*
1764 * Power management
1765 */
1766#ifdef CONFIG_PM
1767
1768void mccic_suspend(struct mcam_camera *cam)
1769{
1770 enum mcam_state cstate = cam->state;
1771
1772 mcam_ctlr_stop_dma(cam);
1773 mcam_ctlr_power_down(cam);
1774 cam->state = cstate;
1775}
1776
1777int mccic_resume(struct mcam_camera *cam)
1778{
1779 int ret = 0;
1780
1781 mutex_lock(&cam->s_mutex);
1782 if (cam->users > 0) {
1783 mcam_ctlr_power_up(cam);
1784 __mcam_cam_reset(cam);
1785 } else {
1786 mcam_ctlr_power_down(cam);
1787 }
1788 mutex_unlock(&cam->s_mutex);
1789
1790 set_bit(CF_CONFIG_NEEDED, &cam->flags);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001791 if (cam->state == S_STREAMING)
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001792 ret = mcam_read_setup(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001793 return ret;
1794}
1795#endif /* CONFIG_PM */