[media] saa7164: code cleanup
[linux-2.6-block.git] / drivers / media / video / cafe_ccic.c
CommitLineData
d905b382
JC
1/*
2 * A driver for the CMOS camera controller in the Marvell 88ALP01 "cafe"
3 * multifunction chip. Currently works with the Omnivision OV7670
4 * sensor.
5 *
bb8d56a4
JC
6 * The data sheet for this device can be found at:
7 * http://www.marvell.com/products/pcconn/88ALP01.jsp
8 *
d905b382 9 * Copyright 2006 One Laptop Per Child Association, Inc.
77d5140f 10 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
d905b382
JC
11 *
12 * Written by Jonathan Corbet, corbet@lwn.net.
13 *
acc5d851
HV
14 * v4l2_device/v4l2_subdev conversion by:
15 * Copyright (C) 2009 Hans Verkuil <hverkuil@xs4all.nl>
16 *
17 * Note: this conversion is untested! Please contact the linux-media
18 * mailinglist if you can test this, together with the test results.
19 *
d905b382
JC
20 * This file may be distributed under the terms of the GNU General
21 * Public License, version 2.
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
d905b382
JC
26#include <linux/init.h>
27#include <linux/fs.h>
ec16d020 28#include <linux/mm.h>
d905b382
JC
29#include <linux/pci.h>
30#include <linux/i2c.h>
31#include <linux/interrupt.h>
32#include <linux/spinlock.h>
33#include <linux/videodev2.h>
5a0e3ad6 34#include <linux/slab.h>
21508b90 35#include <media/v4l2-device.h>
35ea11ff 36#include <media/v4l2-ioctl.h>
3434eb7e 37#include <media/v4l2-chip-ident.h>
d905b382
JC
38#include <linux/device.h>
39#include <linux/wait.h>
40#include <linux/list.h>
41#include <linux/dma-mapping.h>
42#include <linux/delay.h>
d905b382
JC
43#include <linux/jiffies.h>
44#include <linux/vmalloc.h>
45
46#include <asm/uaccess.h>
47#include <asm/io.h>
48
49#include "cafe_ccic-regs.h"
50
ff68defa 51#define CAFE_VERSION 0x000002
d905b382
JC
52
53
54/*
55 * Parameters.
56 */
57MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
58MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
59MODULE_LICENSE("GPL");
60MODULE_SUPPORTED_DEVICE("Video");
61
62/*
63 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
64 * we must have physically contiguous buffers to bring frames into.
65 * These parameters control how many buffers we use, whether we
66 * allocate them at load time (better chance of success, but nails down
67 * memory) or when somebody tries to use the camera (riskier), and,
68 * for load-time allocation, how big they should be.
69 *
70 * The controller can cycle through three buffers. We could use
71 * more by flipping pointers around, but it probably makes little
72 * sense.
73 */
74
75#define MAX_DMA_BUFS 3
ff699e6b 76static int alloc_bufs_at_read;
23869e23
AS
77module_param(alloc_bufs_at_read, bool, 0444);
78MODULE_PARM_DESC(alloc_bufs_at_read,
79 "Non-zero value causes DMA buffers to be allocated when the "
80 "video capture device is read, rather than at module load "
81 "time. This saves memory, but decreases the chances of "
82 "successfully getting those buffers.");
d905b382
JC
83
84static int n_dma_bufs = 3;
85module_param(n_dma_bufs, uint, 0644);
86MODULE_PARM_DESC(n_dma_bufs,
87 "The number of DMA buffers to allocate. Can be either two "
88 "(saves memory, makes timing tighter) or three.");
89
90static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
91module_param(dma_buf_size, uint, 0444);
92MODULE_PARM_DESC(dma_buf_size,
93 "The size of the allocated DMA buffers. If actual operating "
94 "parameters require larger buffers, an attempt to reallocate "
95 "will be made.");
96
97static int min_buffers = 1;
98module_param(min_buffers, uint, 0644);
99MODULE_PARM_DESC(min_buffers,
100 "The minimum number of streaming I/O buffers we are willing "
101 "to work with.");
102
103static int max_buffers = 10;
104module_param(max_buffers, uint, 0644);
105MODULE_PARM_DESC(max_buffers,
106 "The maximum number of streaming I/O buffers an application "
107 "will be allowed to allocate. These buffers are big and live "
108 "in vmalloc space.");
109
ff699e6b 110static int flip;
d905b382
JC
111module_param(flip, bool, 0444);
112MODULE_PARM_DESC(flip,
113 "If set, the sensor will be instructed to flip the image "
114 "vertically.");
115
116
117enum cafe_state {
118 S_NOTREADY, /* Not yet initialized */
119 S_IDLE, /* Just hanging around */
120 S_FLAKED, /* Some sort of problem */
121 S_SINGLEREAD, /* In read() */
122 S_SPECREAD, /* Speculative read (for future read()) */
123 S_STREAMING /* Streaming data */
124};
125
126/*
127 * Tracking of streaming I/O buffers.
128 */
129struct cafe_sio_buffer {
130 struct list_head list;
131 struct v4l2_buffer v4lbuf;
132 char *buffer; /* Where it lives in kernel space */
133 int mapcount;
134 struct cafe_camera *cam;
135};
136
137/*
138 * A description of one of our devices.
139 * Locking: controlled by s_mutex. Certain fields, however, require
140 * the dev_lock spinlock; they are marked as such by comments.
141 * dev_lock is also required for access to device registers.
142 */
143struct cafe_camera
144{
21508b90 145 struct v4l2_device v4l2_dev;
d905b382
JC
146 enum cafe_state state;
147 unsigned long flags; /* Buffer status, mainly (dev_lock) */
148 int users; /* How many open FDs */
149 struct file *owner; /* Who has data access (v4l2) */
150
151 /*
152 * Subsystem structures.
153 */
154 struct pci_dev *pdev;
21508b90 155 struct video_device vdev;
d905b382 156 struct i2c_adapter i2c_adapter;
8bcfd7af
HV
157 struct v4l2_subdev *sensor;
158 unsigned short sensor_addr;
d905b382
JC
159
160 unsigned char __iomem *regs;
161 struct list_head dev_list; /* link to other devices */
162
163 /* DMA buffers */
164 unsigned int nbufs; /* How many are alloc'd */
165 int next_buf; /* Next to consume (dev_lock) */
166 unsigned int dma_buf_size; /* allocated size */
167 void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */
168 dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
169 unsigned int specframes; /* Unconsumed spec frames (dev_lock) */
170 unsigned int sequence; /* Frame sequence number */
171 unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */
172
173 /* Streaming buffers */
174 unsigned int n_sbufs; /* How many we have */
175 struct cafe_sio_buffer *sb_bufs; /* The array of housekeeping structs */
176 struct list_head sb_avail; /* Available for data (we own) (dev_lock) */
177 struct list_head sb_full; /* With data (user space owns) (dev_lock) */
178 struct tasklet_struct s_tasklet;
179
180 /* Current operating parameters */
3434eb7e 181 u32 sensor_type; /* Currently ov7670 only */
d905b382 182 struct v4l2_pix_format pix_format;
1e426228 183 enum v4l2_mbus_pixelcode mbus_code;
d905b382
JC
184
185 /* Locks */
186 struct mutex s_mutex; /* Access to this structure */
187 spinlock_t dev_lock; /* Access to device */
188
189 /* Misc */
190 wait_queue_head_t smbus_wait; /* Waiting on i2c events */
191 wait_queue_head_t iowait; /* Waiting on frame data */
d905b382
JC
192};
193
194/*
195 * Status flags. Always manipulated with bit operations.
196 */
197#define CF_BUF0_VALID 0 /* Buffers valid - first three */
198#define CF_BUF1_VALID 1
199#define CF_BUF2_VALID 2
200#define CF_DMA_ACTIVE 3 /* A frame is incoming */
201#define CF_CONFIG_NEEDED 4 /* Must configure hardware */
202
8bcfd7af
HV
203#define sensor_call(cam, o, f, args...) \
204 v4l2_subdev_call(cam->sensor, o, f, ##args)
d905b382 205
21508b90
HV
206static inline struct cafe_camera *to_cam(struct v4l2_device *dev)
207{
208 return container_of(dev, struct cafe_camera, v4l2_dev);
209}
210
1e426228
HV
211static struct cafe_format_struct {
212 __u8 *desc;
213 __u32 pixelformat;
214 int bpp; /* Bytes per pixel */
215 enum v4l2_mbus_pixelcode mbus_code;
216} cafe_formats[] = {
217 {
218 .desc = "YUYV 4:2:2",
219 .pixelformat = V4L2_PIX_FMT_YUYV,
220 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
221 .bpp = 2,
222 },
223 {
224 .desc = "RGB 444",
225 .pixelformat = V4L2_PIX_FMT_RGB444,
226 .mbus_code = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE,
227 .bpp = 2,
228 },
229 {
230 .desc = "RGB 565",
231 .pixelformat = V4L2_PIX_FMT_RGB565,
232 .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE,
233 .bpp = 2,
234 },
235 {
236 .desc = "Raw RGB Bayer",
237 .pixelformat = V4L2_PIX_FMT_SBGGR8,
238 .mbus_code = V4L2_MBUS_FMT_SBGGR8_1X8,
239 .bpp = 1
240 },
241};
242#define N_CAFE_FMTS ARRAY_SIZE(cafe_formats)
243
244static struct cafe_format_struct *cafe_find_format(u32 pixelformat)
245{
246 unsigned i;
247
248 for (i = 0; i < N_CAFE_FMTS; i++)
249 if (cafe_formats[i].pixelformat == pixelformat)
250 return cafe_formats + i;
251 /* Not found? Then return the first format. */
252 return cafe_formats;
253}
d905b382
JC
254
255/*
256 * Start over with DMA buffers - dev_lock needed.
257 */
258static void cafe_reset_buffers(struct cafe_camera *cam)
259{
260 int i;
261
262 cam->next_buf = -1;
263 for (i = 0; i < cam->nbufs; i++)
264 clear_bit(i, &cam->flags);
265 cam->specframes = 0;
266}
267
268static inline int cafe_needs_config(struct cafe_camera *cam)
269{
270 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
271}
272
273static void cafe_set_config_needed(struct cafe_camera *cam, int needed)
274{
275 if (needed)
276 set_bit(CF_CONFIG_NEEDED, &cam->flags);
277 else
278 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
279}
280
281
282
283
284/*
285 * Debugging and related.
286 */
287#define cam_err(cam, fmt, arg...) \
288 dev_err(&(cam)->pdev->dev, fmt, ##arg);
289#define cam_warn(cam, fmt, arg...) \
290 dev_warn(&(cam)->pdev->dev, fmt, ##arg);
291#define cam_dbg(cam, fmt, arg...) \
292 dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
293
294
295/* ---------------------------------------------------------------------*/
d905b382 296
d905b382
JC
297/*
298 * Device register I/O
299 */
300static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg,
301 unsigned int val)
302{
303 iowrite32(val, cam->regs + reg);
304}
305
306static inline unsigned int cafe_reg_read(struct cafe_camera *cam,
307 unsigned int reg)
308{
309 return ioread32(cam->regs + reg);
310}
311
312
313static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg,
314 unsigned int val, unsigned int mask)
315{
316 unsigned int v = cafe_reg_read(cam, reg);
317
318 v = (v & ~mask) | (val & mask);
319 cafe_reg_write(cam, reg, v);
320}
321
322static inline void cafe_reg_clear_bit(struct cafe_camera *cam,
323 unsigned int reg, unsigned int val)
324{
325 cafe_reg_write_mask(cam, reg, 0, val);
326}
327
328static inline void cafe_reg_set_bit(struct cafe_camera *cam,
329 unsigned int reg, unsigned int val)
330{
331 cafe_reg_write_mask(cam, reg, val, val);
332}
333
334
335
336/* -------------------------------------------------------------------- */
337/*
338 * The I2C/SMBUS interface to the camera itself starts here. The
339 * controller handles SMBUS itself, presenting a relatively simple register
340 * interface; all we have to do is to tell it where to route the data.
341 */
342#define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
343
344static int cafe_smbus_write_done(struct cafe_camera *cam)
345{
346 unsigned long flags;
347 int c1;
348
349 /*
350 * We must delay after the interrupt, or the controller gets confused
351 * and never does give us good status. Fortunately, we don't do this
352 * often.
353 */
354 udelay(20);
355 spin_lock_irqsave(&cam->dev_lock, flags);
356 c1 = cafe_reg_read(cam, REG_TWSIC1);
357 spin_unlock_irqrestore(&cam->dev_lock, flags);
358 return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT;
359}
360
361static int cafe_smbus_write_data(struct cafe_camera *cam,
362 u16 addr, u8 command, u8 value)
363{
364 unsigned int rval;
365 unsigned long flags;
366
367 spin_lock_irqsave(&cam->dev_lock, flags);
368 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
369 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
370 /*
371 * Marvell sez set clkdiv to all 1's for now.
372 */
373 rval |= TWSIC0_CLKDIV;
374 cafe_reg_write(cam, REG_TWSIC0, rval);
375 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
376 rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
377 cafe_reg_write(cam, REG_TWSIC1, rval);
378 spin_unlock_irqrestore(&cam->dev_lock, flags);
d905b382 379
df2b9b0f
DD
380 /* Unfortunately, reading TWSIC1 too soon after sending a command
381 * causes the device to die.
382 * Use a busy-wait because we often send a large quantity of small
383 * commands at-once; using msleep() would cause a lot of context
384 * switches which take longer than 2ms, resulting in a noticable
385 * boot-time and capture-start delays.
386 */
387 mdelay(2);
388
6d77444a 389 /*
df2b9b0f
DD
390 * Another sad fact is that sometimes, commands silently complete but
391 * cafe_smbus_write_done() never becomes aware of this.
392 * This happens at random and appears to possible occur with any
393 * command.
394 * We don't understand why this is. We work around this issue
395 * with the timeout in the wait below, assuming that all commands
396 * complete within the timeout.
6d77444a 397 */
d905b382
JC
398 wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam),
399 CAFE_SMBUS_TIMEOUT);
df2b9b0f 400
d905b382
JC
401 spin_lock_irqsave(&cam->dev_lock, flags);
402 rval = cafe_reg_read(cam, REG_TWSIC1);
403 spin_unlock_irqrestore(&cam->dev_lock, flags);
404
405 if (rval & TWSIC1_WSTAT) {
406 cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
407 command, value);
408 return -EIO;
409 }
410 if (rval & TWSIC1_ERROR) {
411 cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
412 command, value);
413 return -EIO;
414 }
415 return 0;
416}
417
418
419
420static int cafe_smbus_read_done(struct cafe_camera *cam)
421{
422 unsigned long flags;
423 int c1;
424
425 /*
426 * We must delay after the interrupt, or the controller gets confused
427 * and never does give us good status. Fortunately, we don't do this
428 * often.
429 */
430 udelay(20);
431 spin_lock_irqsave(&cam->dev_lock, flags);
432 c1 = cafe_reg_read(cam, REG_TWSIC1);
433 spin_unlock_irqrestore(&cam->dev_lock, flags);
434 return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
435}
436
437
438
439static int cafe_smbus_read_data(struct cafe_camera *cam,
440 u16 addr, u8 command, u8 *value)
441{
442 unsigned int rval;
443 unsigned long flags;
444
445 spin_lock_irqsave(&cam->dev_lock, flags);
446 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
447 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
448 /*
449 * Marvel sez set clkdiv to all 1's for now.
450 */
451 rval |= TWSIC0_CLKDIV;
452 cafe_reg_write(cam, REG_TWSIC0, rval);
453 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
454 rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
455 cafe_reg_write(cam, REG_TWSIC1, rval);
456 spin_unlock_irqrestore(&cam->dev_lock, flags);
457
458 wait_event_timeout(cam->smbus_wait,
459 cafe_smbus_read_done(cam), CAFE_SMBUS_TIMEOUT);
460 spin_lock_irqsave(&cam->dev_lock, flags);
461 rval = cafe_reg_read(cam, REG_TWSIC1);
462 spin_unlock_irqrestore(&cam->dev_lock, flags);
463
464 if (rval & TWSIC1_ERROR) {
465 cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
466 return -EIO;
467 }
468 if (! (rval & TWSIC1_RVALID)) {
469 cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
470 command);
471 return -EIO;
472 }
473 *value = rval & 0xff;
474 return 0;
475}
476
477/*
478 * Perform a transfer over SMBUS. This thing is called under
479 * the i2c bus lock, so we shouldn't race with ourselves...
480 */
481static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
482 unsigned short flags, char rw, u8 command,
483 int size, union i2c_smbus_data *data)
484{
21508b90
HV
485 struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter);
486 struct cafe_camera *cam = to_cam(v4l2_dev);
d905b382
JC
487 int ret = -EINVAL;
488
d905b382
JC
489 /*
490 * This interface would appear to only do byte data ops. OK
491 * it can do word too, but the cam chip has no use for that.
492 */
493 if (size != I2C_SMBUS_BYTE_DATA) {
494 cam_err(cam, "funky xfer size %d\n", size);
495 return -EINVAL;
496 }
497
498 if (rw == I2C_SMBUS_WRITE)
499 ret = cafe_smbus_write_data(cam, addr, command, data->byte);
500 else if (rw == I2C_SMBUS_READ)
501 ret = cafe_smbus_read_data(cam, addr, command, &data->byte);
502 return ret;
503}
504
505
506static void cafe_smbus_enable_irq(struct cafe_camera *cam)
507{
508 unsigned long flags;
509
510 spin_lock_irqsave(&cam->dev_lock, flags);
511 cafe_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS);
512 spin_unlock_irqrestore(&cam->dev_lock, flags);
513}
514
515static u32 cafe_smbus_func(struct i2c_adapter *adapter)
516{
517 return I2C_FUNC_SMBUS_READ_BYTE_DATA |
518 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
519}
520
521static struct i2c_algorithm cafe_smbus_algo = {
522 .smbus_xfer = cafe_smbus_xfer,
523 .functionality = cafe_smbus_func
524};
525
526/* Somebody is on the bus */
f9a76156
JC
527static void cafe_ctlr_stop_dma(struct cafe_camera *cam);
528static void cafe_ctlr_power_down(struct cafe_camera *cam);
d905b382 529
d905b382
JC
530static int cafe_smbus_setup(struct cafe_camera *cam)
531{
532 struct i2c_adapter *adap = &cam->i2c_adapter;
533 int ret;
534
535 cafe_smbus_enable_irq(cam);
d905b382 536 adap->owner = THIS_MODULE;
d905b382
JC
537 adap->algo = &cafe_smbus_algo;
538 strcpy(adap->name, "cafe_ccic");
12a917f6 539 adap->dev.parent = &cam->pdev->dev;
21508b90 540 i2c_set_adapdata(adap, &cam->v4l2_dev);
d905b382
JC
541 ret = i2c_add_adapter(adap);
542 if (ret)
543 printk(KERN_ERR "Unable to register cafe i2c adapter\n");
544 return ret;
545}
546
547static void cafe_smbus_shutdown(struct cafe_camera *cam)
548{
549 i2c_del_adapter(&cam->i2c_adapter);
550}
551
552
553/* ------------------------------------------------------------------- */
554/*
555 * Deal with the controller.
556 */
557
558/*
559 * Do everything we think we need to have the interface operating
560 * according to the desired format.
561 */
562static void cafe_ctlr_dma(struct cafe_camera *cam)
563{
564 /*
565 * Store the first two Y buffers (we aren't supporting
566 * planar formats for now, so no UV bufs). Then either
567 * set the third if it exists, or tell the controller
568 * to just use two.
569 */
570 cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
571 cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
572 if (cam->nbufs > 2) {
573 cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
574 cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
575 }
576 else
577 cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
578 cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
579}
580
581static void cafe_ctlr_image(struct cafe_camera *cam)
582{
583 int imgsz;
584 struct v4l2_pix_format *fmt = &cam->pix_format;
585
586 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
587 (fmt->bytesperline & IMGSZ_H_MASK);
588 cafe_reg_write(cam, REG_IMGSIZE, imgsz);
589 cafe_reg_write(cam, REG_IMGOFFSET, 0);
590 /* YPITCH just drops the last two bits */
591 cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
592 IMGP_YP_MASK);
593 /*
594 * Tell the controller about the image format we are using.
595 */
596 switch (cam->pix_format.pixelformat) {
597 case V4L2_PIX_FMT_YUYV:
598 cafe_reg_write_mask(cam, REG_CTRL0,
599 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
600 C0_DF_MASK);
601 break;
602
d905b382
JC
603 case V4L2_PIX_FMT_RGB444:
604 cafe_reg_write_mask(cam, REG_CTRL0,
605 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
606 C0_DF_MASK);
607 /* Alpha value? */
608 break;
609
610 case V4L2_PIX_FMT_RGB565:
611 cafe_reg_write_mask(cam, REG_CTRL0,
612 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
613 C0_DF_MASK);
614 break;
615
616 default:
617 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
618 break;
619 }
620 /*
621 * Make sure it knows we want to use hsync/vsync.
622 */
623 cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
624 C0_SIFM_MASK);
625}
626
627
628/*
629 * Configure the controller for operation; caller holds the
630 * device mutex.
631 */
632static int cafe_ctlr_configure(struct cafe_camera *cam)
633{
634 unsigned long flags;
635
636 spin_lock_irqsave(&cam->dev_lock, flags);
637 cafe_ctlr_dma(cam);
638 cafe_ctlr_image(cam);
639 cafe_set_config_needed(cam, 0);
640 spin_unlock_irqrestore(&cam->dev_lock, flags);
641 return 0;
642}
643
644static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
645{
646 /*
647 * Clear any pending interrupts, since we do not
648 * expect to have I/O active prior to enabling.
649 */
650 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
651 cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
652}
653
654static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
655{
656 cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
657}
658
659/*
660 * Make the controller start grabbing images. Everything must
661 * be set up before doing this.
662 */
663static void cafe_ctlr_start(struct cafe_camera *cam)
664{
665 /* set_bit performs a read, so no other barrier should be
666 needed here */
667 cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
668}
669
670static void cafe_ctlr_stop(struct cafe_camera *cam)
671{
672 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
673}
674
675static void cafe_ctlr_init(struct cafe_camera *cam)
676{
677 unsigned long flags;
678
679 spin_lock_irqsave(&cam->dev_lock, flags);
680 /*
681 * Added magic to bring up the hardware on the B-Test board
682 */
683 cafe_reg_write(cam, 0x3038, 0x8);
684 cafe_reg_write(cam, 0x315c, 0x80008);
685 /*
686 * Go through the dance needed to wake the device up.
687 * Note that these registers are global and shared
688 * with the NAND and SD devices. Interaction between the
689 * three still needs to be examined.
690 */
691 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
692 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
693 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
5b50ed7c
JC
694 /*
695 * Here we must wait a bit for the controller to come around.
696 */
697 spin_unlock_irqrestore(&cam->dev_lock, flags);
70cd685d 698 msleep(5);
5b50ed7c
JC
699 spin_lock_irqsave(&cam->dev_lock, flags);
700
d905b382
JC
701 cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
702 cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
703 /*
704 * Make sure it's not powered down.
705 */
706 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
707 /*
708 * Turn off the enable bit. It sure should be off anyway,
709 * but it's good to be sure.
710 */
711 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
712 /*
713 * Mask all interrupts.
714 */
715 cafe_reg_write(cam, REG_IRQMASK, 0);
716 /*
717 * Clock the sensor appropriately. Controller clock should
718 * be 48MHz, sensor "typical" value is half that.
719 */
720 cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
721 spin_unlock_irqrestore(&cam->dev_lock, flags);
722}
723
724
725/*
726 * Stop the controller, and don't return until we're really sure that no
727 * further DMA is going on.
728 */
729static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
730{
731 unsigned long flags;
732
733 /*
734 * Theory: stop the camera controller (whether it is operating
735 * or not). Delay briefly just in case we race with the SOF
736 * interrupt, then wait until no DMA is active.
737 */
738 spin_lock_irqsave(&cam->dev_lock, flags);
739 cafe_ctlr_stop(cam);
740 spin_unlock_irqrestore(&cam->dev_lock, flags);
741 mdelay(1);
742 wait_event_timeout(cam->iowait,
743 !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
744 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
745 cam_err(cam, "Timeout waiting for DMA to end\n");
746 /* This would be bad news - what now? */
747 spin_lock_irqsave(&cam->dev_lock, flags);
748 cam->state = S_IDLE;
749 cafe_ctlr_irq_disable(cam);
750 spin_unlock_irqrestore(&cam->dev_lock, flags);
751}
752
753/*
754 * Power up and down.
755 */
756static void cafe_ctlr_power_up(struct cafe_camera *cam)
757{
758 unsigned long flags;
759
760 spin_lock_irqsave(&cam->dev_lock, flags);
761 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
7acf90c7
JC
762 /*
763 * Part one of the sensor dance: turn the global
764 * GPIO signal on.
765 */
766 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
767 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
d905b382
JC
768 /*
769 * Put the sensor into operational mode (assumes OLPC-style
770 * wiring). Control 0 is reset - set to 1 to operate.
771 * Control 1 is power down, set to 0 to operate.
772 */
f9a76156 773 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
21508b90 774/* mdelay(1); */ /* Marvell says 1ms will do it */
d905b382 775 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
21508b90 776/* mdelay(1); */ /* Enough? */
d905b382 777 spin_unlock_irqrestore(&cam->dev_lock, flags);
7acf90c7 778 msleep(5); /* Just to be sure */
d905b382
JC
779}
780
781static void cafe_ctlr_power_down(struct cafe_camera *cam)
782{
783 unsigned long flags;
784
785 spin_lock_irqsave(&cam->dev_lock, flags);
786 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
7acf90c7
JC
787 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
788 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT);
d905b382
JC
789 cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
790 spin_unlock_irqrestore(&cam->dev_lock, flags);
791}
792
793/* -------------------------------------------------------------------- */
794/*
795 * Communications with the sensor.
796 */
797
d905b382
JC
798static int __cafe_cam_reset(struct cafe_camera *cam)
799{
8bcfd7af 800 return sensor_call(cam, core, reset, 0);
d905b382
JC
801}
802
803/*
804 * We have found the sensor on the i2c. Let's try to have a
805 * conversation.
806 */
807static int cafe_cam_init(struct cafe_camera *cam)
808{
aecde8b5 809 struct v4l2_dbg_chip_ident chip;
d905b382
JC
810 int ret;
811
812 mutex_lock(&cam->s_mutex);
813 if (cam->state != S_NOTREADY)
814 cam_warn(cam, "Cam init with device in funky state %d",
815 cam->state);
816 ret = __cafe_cam_reset(cam);
817 if (ret)
818 goto out;
90c69f29 819 chip.ident = V4L2_IDENT_NONE;
aecde8b5 820 chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
8bcfd7af
HV
821 chip.match.addr = cam->sensor_addr;
822 ret = sensor_call(cam, core, g_chip_ident, &chip);
d905b382
JC
823 if (ret)
824 goto out;
3434eb7e 825 cam->sensor_type = chip.ident;
d905b382 826 if (cam->sensor_type != V4L2_IDENT_OV7670) {
8bcfd7af 827 cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
d905b382
JC
828 ret = -EINVAL;
829 goto out;
830 }
831/* Get/set parameters? */
832 ret = 0;
833 cam->state = S_IDLE;
834 out:
7acf90c7 835 cafe_ctlr_power_down(cam);
d905b382
JC
836 mutex_unlock(&cam->s_mutex);
837 return ret;
838}
839
840/*
841 * Configure the sensor to match the parameters we have. Caller should
842 * hold s_mutex
843 */
844static int cafe_cam_set_flip(struct cafe_camera *cam)
845{
846 struct v4l2_control ctrl;
847
848 memset(&ctrl, 0, sizeof(ctrl));
849 ctrl.id = V4L2_CID_VFLIP;
850 ctrl.value = flip;
8bcfd7af 851 return sensor_call(cam, core, s_ctrl, &ctrl);
d905b382
JC
852}
853
854
855static int cafe_cam_configure(struct cafe_camera *cam)
856{
1e426228 857 struct v4l2_mbus_framefmt mbus_fmt;
8bcfd7af 858 int ret;
d905b382
JC
859
860 if (cam->state != S_IDLE)
861 return -EINVAL;
1e426228 862 v4l2_fill_mbus_format(&mbus_fmt, &cam->pix_format, cam->mbus_code);
8bcfd7af 863 ret = sensor_call(cam, core, init, 0);
d905b382 864 if (ret == 0)
1e426228 865 ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
d905b382
JC
866 /*
867 * OV7670 does weird things if flip is set *before* format...
868 */
869 ret += cafe_cam_set_flip(cam);
870 return ret;
871}
872
873/* -------------------------------------------------------------------- */
874/*
875 * DMA buffer management. These functions need s_mutex held.
876 */
877
878/* FIXME: this is inefficient as hell, since dma_alloc_coherent just
879 * does a get_free_pages() call, and we waste a good chunk of an orderN
880 * allocation. Should try to allocate the whole set in one chunk.
881 */
882static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
883{
884 int i;
885
886 cafe_set_config_needed(cam, 1);
887 if (loadtime)
888 cam->dma_buf_size = dma_buf_size;
a66d2336 889 else
d905b382 890 cam->dma_buf_size = cam->pix_format.sizeimage;
d905b382
JC
891 if (n_dma_bufs > 3)
892 n_dma_bufs = 3;
893
894 cam->nbufs = 0;
895 for (i = 0; i < n_dma_bufs; i++) {
896 cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
897 cam->dma_buf_size, cam->dma_handles + i,
898 GFP_KERNEL);
899 if (cam->dma_bufs[i] == NULL) {
900 cam_warn(cam, "Failed to allocate DMA buffer\n");
901 break;
902 }
903 /* For debug, remove eventually */
904 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
905 (cam->nbufs)++;
906 }
907
908 switch (cam->nbufs) {
909 case 1:
910 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
911 cam->dma_bufs[0], cam->dma_handles[0]);
912 cam->nbufs = 0;
913 case 0:
914 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
915 return -ENOMEM;
916
917 case 2:
918 if (n_dma_bufs > 2)
919 cam_warn(cam, "Will limp along with only 2 buffers\n");
920 break;
921 }
922 return 0;
923}
924
925static void cafe_free_dma_bufs(struct cafe_camera *cam)
926{
927 int i;
928
929 for (i = 0; i < cam->nbufs; i++) {
930 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
931 cam->dma_bufs[i], cam->dma_handles[i]);
932 cam->dma_bufs[i] = NULL;
933 }
934 cam->nbufs = 0;
935}
936
937
938
939
940
941/* ----------------------------------------------------------------------- */
942/*
943 * Here starts the V4L2 interface code.
944 */
945
946/*
947 * Read an image from the device.
948 */
949static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
950 char __user *buffer, size_t len, loff_t *pos)
951{
952 int bufno;
953 unsigned long flags;
954
955 spin_lock_irqsave(&cam->dev_lock, flags);
956 if (cam->next_buf < 0) {
957 cam_err(cam, "deliver_buffer: No next buffer\n");
958 spin_unlock_irqrestore(&cam->dev_lock, flags);
959 return -EIO;
960 }
961 bufno = cam->next_buf;
962 clear_bit(bufno, &cam->flags);
963 if (++(cam->next_buf) >= cam->nbufs)
964 cam->next_buf = 0;
965 if (! test_bit(cam->next_buf, &cam->flags))
966 cam->next_buf = -1;
967 cam->specframes = 0;
968 spin_unlock_irqrestore(&cam->dev_lock, flags);
969
970 if (len > cam->pix_format.sizeimage)
971 len = cam->pix_format.sizeimage;
972 if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
973 return -EFAULT;
974 (*pos) += len;
975 return len;
976}
977
978/*
979 * Get everything ready, and start grabbing frames.
980 */
981static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
982{
983 int ret;
984 unsigned long flags;
985
986 /*
987 * Configuration. If we still don't have DMA buffers,
988 * make one last, desperate attempt.
989 */
990 if (cam->nbufs == 0)
991 if (cafe_alloc_dma_bufs(cam, 0))
992 return -ENOMEM;
993
994 if (cafe_needs_config(cam)) {
995 cafe_cam_configure(cam);
996 ret = cafe_ctlr_configure(cam);
997 if (ret)
998 return ret;
999 }
1000
1001 /*
1002 * Turn it loose.
1003 */
1004 spin_lock_irqsave(&cam->dev_lock, flags);
1005 cafe_reset_buffers(cam);
1006 cafe_ctlr_irq_enable(cam);
1007 cam->state = state;
1008 cafe_ctlr_start(cam);
1009 spin_unlock_irqrestore(&cam->dev_lock, flags);
1010 return 0;
1011}
1012
1013
1014static ssize_t cafe_v4l_read(struct file *filp,
1015 char __user *buffer, size_t len, loff_t *pos)
1016{
1017 struct cafe_camera *cam = filp->private_data;
b9109b75 1018 int ret = 0;
d905b382
JC
1019
1020 /*
1021 * Perhaps we're in speculative read mode and already
1022 * have data?
1023 */
1024 mutex_lock(&cam->s_mutex);
1025 if (cam->state == S_SPECREAD) {
1026 if (cam->next_buf >= 0) {
1027 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1028 if (ret != 0)
1029 goto out_unlock;
1030 }
1031 } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
1032 ret = -EIO;
1033 goto out_unlock;
1034 } else if (cam->state != S_IDLE) {
1035 ret = -EBUSY;
1036 goto out_unlock;
1037 }
1038
1039 /*
1040 * v4l2: multiple processes can open the device, but only
1041 * one gets to grab data from it.
1042 */
1043 if (cam->owner && cam->owner != filp) {
1044 ret = -EBUSY;
1045 goto out_unlock;
1046 }
1047 cam->owner = filp;
1048
1049 /*
1050 * Do setup if need be.
1051 */
1052 if (cam->state != S_SPECREAD) {
1053 ret = cafe_read_setup(cam, S_SINGLEREAD);
1054 if (ret)
1055 goto out_unlock;
1056 }
1057 /*
1058 * Wait for something to happen. This should probably
1059 * be interruptible (FIXME).
1060 */
1061 wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
1062 if (cam->next_buf < 0) {
1063 cam_err(cam, "read() operation timed out\n");
1064 cafe_ctlr_stop_dma(cam);
1065 ret = -EIO;
1066 goto out_unlock;
1067 }
1068 /*
1069 * Give them their data and we should be done.
1070 */
1071 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1072
1073 out_unlock:
1074 mutex_unlock(&cam->s_mutex);
1075 return ret;
1076}
1077
1078
1079
1080
1081
1082
1083
1084
1085/*
1086 * Streaming I/O support.
1087 */
1088
1089
1090
1091static int cafe_vidioc_streamon(struct file *filp, void *priv,
1092 enum v4l2_buf_type type)
1093{
1094 struct cafe_camera *cam = filp->private_data;
1095 int ret = -EINVAL;
1096
1097 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1098 goto out;
1099 mutex_lock(&cam->s_mutex);
1100 if (cam->state != S_IDLE || cam->n_sbufs == 0)
1101 goto out_unlock;
1102
1103 cam->sequence = 0;
1104 ret = cafe_read_setup(cam, S_STREAMING);
1105
1106 out_unlock:
1107 mutex_unlock(&cam->s_mutex);
1108 out:
1109 return ret;
1110}
1111
1112
1113static int cafe_vidioc_streamoff(struct file *filp, void *priv,
1114 enum v4l2_buf_type type)
1115{
1116 struct cafe_camera *cam = filp->private_data;
1117 int ret = -EINVAL;
1118
1119 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1120 goto out;
1121 mutex_lock(&cam->s_mutex);
1122 if (cam->state != S_STREAMING)
1123 goto out_unlock;
1124
1125 cafe_ctlr_stop_dma(cam);
1126 ret = 0;
1127
1128 out_unlock:
1129 mutex_unlock(&cam->s_mutex);
1130 out:
1131 return ret;
1132}
1133
1134
1135
1136static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1137{
1138 struct cafe_sio_buffer *buf = cam->sb_bufs + index;
1139
1140 INIT_LIST_HEAD(&buf->list);
1141 buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
1142 buf->buffer = vmalloc_user(buf->v4lbuf.length);
1143 if (buf->buffer == NULL)
1144 return -ENOMEM;
1145 buf->mapcount = 0;
1146 buf->cam = cam;
1147
1148 buf->v4lbuf.index = index;
1149 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1150 buf->v4lbuf.field = V4L2_FIELD_NONE;
1151 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
1152 /*
c1accaa2 1153 * Offset: must be 32-bit even on a 64-bit system. videobuf-dma-sg
d905b382
JC
1154 * just uses the length times the index, but the spec warns
1155 * against doing just that - vma merging problems. So we
1156 * leave a gap between each pair of buffers.
1157 */
1158 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1159 return 0;
1160}
1161
1162static int cafe_free_sio_buffers(struct cafe_camera *cam)
1163{
1164 int i;
1165
1166 /*
1167 * If any buffers are mapped, we cannot free them at all.
1168 */
1169 for (i = 0; i < cam->n_sbufs; i++)
1170 if (cam->sb_bufs[i].mapcount > 0)
1171 return -EBUSY;
1172 /*
1173 * OK, let's do it.
1174 */
1175 for (i = 0; i < cam->n_sbufs; i++)
1176 vfree(cam->sb_bufs[i].buffer);
1177 cam->n_sbufs = 0;
1178 kfree(cam->sb_bufs);
1179 cam->sb_bufs = NULL;
1180 INIT_LIST_HEAD(&cam->sb_avail);
1181 INIT_LIST_HEAD(&cam->sb_full);
1182 return 0;
1183}
1184
1185
1186
1187static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1188 struct v4l2_requestbuffers *req)
1189{
1190 struct cafe_camera *cam = filp->private_data;
3198cf67 1191 int ret = 0; /* Silence warning */
d905b382
JC
1192
1193 /*
1194 * Make sure it's something we can do. User pointers could be
1195 * implemented without great pain, but that's not been done yet.
1196 */
d905b382
JC
1197 if (req->memory != V4L2_MEMORY_MMAP)
1198 return -EINVAL;
1199 /*
1200 * If they ask for zero buffers, they really want us to stop streaming
1201 * (if it's happening) and free everything. Should we check owner?
1202 */
1203 mutex_lock(&cam->s_mutex);
1204 if (req->count == 0) {
1205 if (cam->state == S_STREAMING)
1206 cafe_ctlr_stop_dma(cam);
1207 ret = cafe_free_sio_buffers (cam);
1208 goto out;
1209 }
1210 /*
1211 * Device needs to be idle and working. We *could* try to do the
1212 * right thing in S_SPECREAD by shutting things down, but it
1213 * probably doesn't matter.
1214 */
1215 if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1216 ret = -EBUSY;
1217 goto out;
1218 }
1219 cam->owner = filp;
1220
1221 if (req->count < min_buffers)
1222 req->count = min_buffers;
1223 else if (req->count > max_buffers)
1224 req->count = max_buffers;
1225 if (cam->n_sbufs > 0) {
1226 ret = cafe_free_sio_buffers(cam);
1227 if (ret)
1228 goto out;
1229 }
1230
1231 cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1232 GFP_KERNEL);
1233 if (cam->sb_bufs == NULL) {
1234 ret = -ENOMEM;
1235 goto out;
1236 }
1237 for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1238 ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1239 if (ret)
1240 break;
1241 }
1242
1243 if (cam->n_sbufs == 0) /* no luck at all - ret already set */
1244 kfree(cam->sb_bufs);
d905b382
JC
1245 req->count = cam->n_sbufs; /* In case of partial success */
1246
1247 out:
1248 mutex_unlock(&cam->s_mutex);
1249 return ret;
1250}
1251
1252
1253static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1254 struct v4l2_buffer *buf)
1255{
1256 struct cafe_camera *cam = filp->private_data;
1257 int ret = -EINVAL;
1258
1259 mutex_lock(&cam->s_mutex);
e33ee31a 1260 if (buf->index >= cam->n_sbufs)
d905b382
JC
1261 goto out;
1262 *buf = cam->sb_bufs[buf->index].v4lbuf;
1263 ret = 0;
1264 out:
1265 mutex_unlock(&cam->s_mutex);
1266 return ret;
1267}
1268
1269static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1270 struct v4l2_buffer *buf)
1271{
1272 struct cafe_camera *cam = filp->private_data;
1273 struct cafe_sio_buffer *sbuf;
1274 int ret = -EINVAL;
1275 unsigned long flags;
1276
1277 mutex_lock(&cam->s_mutex);
e33ee31a 1278 if (buf->index >= cam->n_sbufs)
d905b382
JC
1279 goto out;
1280 sbuf = cam->sb_bufs + buf->index;
1281 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1282 ret = 0; /* Already queued?? */
1283 goto out;
1284 }
1285 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1286 /* Spec doesn't say anything, seems appropriate tho */
1287 ret = -EBUSY;
1288 goto out;
1289 }
1290 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1291 spin_lock_irqsave(&cam->dev_lock, flags);
1292 list_add(&sbuf->list, &cam->sb_avail);
1293 spin_unlock_irqrestore(&cam->dev_lock, flags);
1294 ret = 0;
1295 out:
1296 mutex_unlock(&cam->s_mutex);
1297 return ret;
1298}
1299
1300static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1301 struct v4l2_buffer *buf)
1302{
1303 struct cafe_camera *cam = filp->private_data;
1304 struct cafe_sio_buffer *sbuf;
1305 int ret = -EINVAL;
1306 unsigned long flags;
1307
1308 mutex_lock(&cam->s_mutex);
d905b382
JC
1309 if (cam->state != S_STREAMING)
1310 goto out_unlock;
1311 if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1312 ret = -EAGAIN;
1313 goto out_unlock;
1314 }
1315
1316 while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1317 mutex_unlock(&cam->s_mutex);
1318 if (wait_event_interruptible(cam->iowait,
1319 !list_empty(&cam->sb_full))) {
1320 ret = -ERESTARTSYS;
1321 goto out;
1322 }
1323 mutex_lock(&cam->s_mutex);
1324 }
1325
1326 if (cam->state != S_STREAMING)
1327 ret = -EINTR;
1328 else {
1329 spin_lock_irqsave(&cam->dev_lock, flags);
1330 /* Should probably recheck !list_empty() here */
1331 sbuf = list_entry(cam->sb_full.next,
1332 struct cafe_sio_buffer, list);
1333 list_del_init(&sbuf->list);
1334 spin_unlock_irqrestore(&cam->dev_lock, flags);
1335 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1336 *buf = sbuf->v4lbuf;
1337 ret = 0;
1338 }
1339
1340 out_unlock:
1341 mutex_unlock(&cam->s_mutex);
1342 out:
1343 return ret;
1344}
1345
1346
1347
1348static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1349{
1350 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1351 /*
1352 * Locking: done under mmap_sem, so we don't need to
1353 * go back to the camera lock here.
1354 */
1355 sbuf->mapcount++;
1356}
1357
1358
1359static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1360{
1361 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1362
1363 mutex_lock(&sbuf->cam->s_mutex);
1364 sbuf->mapcount--;
1365 /* Docs say we should stop I/O too... */
1366 if (sbuf->mapcount == 0)
1367 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1368 mutex_unlock(&sbuf->cam->s_mutex);
1369}
1370
f0f37e2f 1371static const struct vm_operations_struct cafe_v4l_vm_ops = {
d905b382
JC
1372 .open = cafe_v4l_vm_open,
1373 .close = cafe_v4l_vm_close
1374};
1375
1376
1377static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1378{
1379 struct cafe_camera *cam = filp->private_data;
1380 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1381 int ret = -EINVAL;
1382 int i;
1383 struct cafe_sio_buffer *sbuf = NULL;
1384
1385 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1386 return -EINVAL;
1387 /*
1388 * Find the buffer they are looking for.
1389 */
1390 mutex_lock(&cam->s_mutex);
1391 for (i = 0; i < cam->n_sbufs; i++)
1392 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1393 sbuf = cam->sb_bufs + i;
1394 break;
1395 }
1396 if (sbuf == NULL)
1397 goto out;
1398
1399 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1400 if (ret)
1401 goto out;
1402 vma->vm_flags |= VM_DONTEXPAND;
1403 vma->vm_private_data = sbuf;
1404 vma->vm_ops = &cafe_v4l_vm_ops;
1405 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1406 cafe_v4l_vm_open(vma);
1407 ret = 0;
1408 out:
1409 mutex_unlock(&cam->s_mutex);
1410 return ret;
1411}
1412
1413
1414
bec43661 1415static int cafe_v4l_open(struct file *filp)
d905b382 1416{
21508b90 1417 struct cafe_camera *cam = video_drvdata(filp);
d905b382 1418
d905b382
JC
1419 filp->private_data = cam;
1420
1421 mutex_lock(&cam->s_mutex);
1422 if (cam->users == 0) {
1423 cafe_ctlr_power_up(cam);
1424 __cafe_cam_reset(cam);
1425 cafe_set_config_needed(cam, 1);
1426 /* FIXME make sure this is complete */
1427 }
1428 (cam->users)++;
1429 mutex_unlock(&cam->s_mutex);
1430 return 0;
1431}
1432
1433
bec43661 1434static int cafe_v4l_release(struct file *filp)
d905b382
JC
1435{
1436 struct cafe_camera *cam = filp->private_data;
1437
1438 mutex_lock(&cam->s_mutex);
1439 (cam->users)--;
1440 if (filp == cam->owner) {
1441 cafe_ctlr_stop_dma(cam);
1442 cafe_free_sio_buffers(cam);
1443 cam->owner = NULL;
1444 }
f9a76156 1445 if (cam->users == 0) {
d905b382 1446 cafe_ctlr_power_down(cam);
23869e23 1447 if (alloc_bufs_at_read)
f9a76156
JC
1448 cafe_free_dma_bufs(cam);
1449 }
d905b382
JC
1450 mutex_unlock(&cam->s_mutex);
1451 return 0;
1452}
1453
1454
1455
1456static unsigned int cafe_v4l_poll(struct file *filp,
1457 struct poll_table_struct *pt)
1458{
1459 struct cafe_camera *cam = filp->private_data;
1460
1461 poll_wait(filp, &cam->iowait, pt);
1462 if (cam->next_buf >= 0)
1463 return POLLIN | POLLRDNORM;
1464 return 0;
1465}
1466
1467
1468
1469static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1470 struct v4l2_queryctrl *qc)
1471{
21508b90 1472 struct cafe_camera *cam = priv;
d905b382
JC
1473 int ret;
1474
1475 mutex_lock(&cam->s_mutex);
8bcfd7af 1476 ret = sensor_call(cam, core, queryctrl, qc);
d905b382
JC
1477 mutex_unlock(&cam->s_mutex);
1478 return ret;
1479}
1480
1481
1482static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1483 struct v4l2_control *ctrl)
1484{
21508b90 1485 struct cafe_camera *cam = priv;
d905b382
JC
1486 int ret;
1487
1488 mutex_lock(&cam->s_mutex);
8bcfd7af 1489 ret = sensor_call(cam, core, g_ctrl, ctrl);
d905b382
JC
1490 mutex_unlock(&cam->s_mutex);
1491 return ret;
1492}
1493
1494
1495static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1496 struct v4l2_control *ctrl)
1497{
21508b90 1498 struct cafe_camera *cam = priv;
d905b382
JC
1499 int ret;
1500
1501 mutex_lock(&cam->s_mutex);
8bcfd7af 1502 ret = sensor_call(cam, core, s_ctrl, ctrl);
d905b382
JC
1503 mutex_unlock(&cam->s_mutex);
1504 return ret;
1505}
1506
1507
1508
1509
1510
1511static int cafe_vidioc_querycap(struct file *file, void *priv,
1512 struct v4l2_capability *cap)
1513{
1514 strcpy(cap->driver, "cafe_ccic");
1515 strcpy(cap->card, "cafe_ccic");
1516 cap->version = CAFE_VERSION;
1517 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1518 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1519 return 0;
1520}
1521
1522
1523/*
1524 * The default format we use until somebody says otherwise.
1525 */
1e426228 1526static const struct v4l2_pix_format cafe_def_pix_format = {
d905b382
JC
1527 .width = VGA_WIDTH,
1528 .height = VGA_HEIGHT,
1529 .pixelformat = V4L2_PIX_FMT_YUYV,
1530 .field = V4L2_FIELD_NONE,
1531 .bytesperline = VGA_WIDTH*2,
1532 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
1533};
1534
1e426228
HV
1535static const enum v4l2_mbus_pixelcode cafe_def_mbus_code =
1536 V4L2_MBUS_FMT_YUYV8_2X8;
1537
78b526a4 1538static int cafe_vidioc_enum_fmt_vid_cap(struct file *filp,
d905b382
JC
1539 void *priv, struct v4l2_fmtdesc *fmt)
1540{
1e426228
HV
1541 if (fmt->index >= N_CAFE_FMTS)
1542 return -EINVAL;
1543 strlcpy(fmt->description, cafe_formats[fmt->index].desc,
1544 sizeof(fmt->description));
1545 fmt->pixelformat = cafe_formats[fmt->index].pixelformat;
1546 return 0;
d905b382
JC
1547}
1548
78b526a4 1549static int cafe_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
d905b382
JC
1550 struct v4l2_format *fmt)
1551{
1552 struct cafe_camera *cam = priv;
1e426228
HV
1553 struct cafe_format_struct *f;
1554 struct v4l2_pix_format *pix = &fmt->fmt.pix;
1555 struct v4l2_mbus_framefmt mbus_fmt;
d905b382
JC
1556 int ret;
1557
1e426228
HV
1558 f = cafe_find_format(pix->pixelformat);
1559 pix->pixelformat = f->pixelformat;
1560 v4l2_fill_mbus_format(&mbus_fmt, pix, f->mbus_code);
d905b382 1561 mutex_lock(&cam->s_mutex);
1e426228 1562 ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
d905b382 1563 mutex_unlock(&cam->s_mutex);
1e426228
HV
1564 v4l2_fill_pix_format(pix, &mbus_fmt);
1565 pix->bytesperline = pix->width * f->bpp;
1566 pix->sizeimage = pix->height * pix->bytesperline;
d905b382
JC
1567 return ret;
1568}
1569
78b526a4 1570static int cafe_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
d905b382
JC
1571 struct v4l2_format *fmt)
1572{
1573 struct cafe_camera *cam = priv;
1e426228 1574 struct cafe_format_struct *f;
d905b382
JC
1575 int ret;
1576
1577 /*
1578 * Can't do anything if the device is not idle
1579 * Also can't if there are streaming buffers in place.
1580 */
1581 if (cam->state != S_IDLE || cam->n_sbufs > 0)
1582 return -EBUSY;
1e426228
HV
1583
1584 f = cafe_find_format(fmt->fmt.pix.pixelformat);
1585
d905b382
JC
1586 /*
1587 * See if the formatting works in principle.
1588 */
78b526a4 1589 ret = cafe_vidioc_try_fmt_vid_cap(filp, priv, fmt);
d905b382
JC
1590 if (ret)
1591 return ret;
1592 /*
1593 * Now we start to change things for real, so let's do it
1594 * under lock.
1595 */
1596 mutex_lock(&cam->s_mutex);
1597 cam->pix_format = fmt->fmt.pix;
1e426228
HV
1598 cam->mbus_code = f->mbus_code;
1599
d905b382
JC
1600 /*
1601 * Make sure we have appropriate DMA buffers.
1602 */
1603 ret = -ENOMEM;
1604 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1605 cafe_free_dma_bufs(cam);
1606 if (cam->nbufs == 0) {
1607 if (cafe_alloc_dma_bufs(cam, 0))
1608 goto out;
1609 }
1610 /*
1611 * It looks like this might work, so let's program the sensor.
1612 */
1613 ret = cafe_cam_configure(cam);
1614 if (! ret)
1615 ret = cafe_ctlr_configure(cam);
1616 out:
1617 mutex_unlock(&cam->s_mutex);
1618 return ret;
1619}
1620
1621/*
1622 * Return our stored notion of how the camera is/should be configured.
1623 * The V4l2 spec wants us to be smarter, and actually get this from
1624 * the camera (and not mess with it at open time). Someday.
1625 */
78b526a4 1626static int cafe_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
d905b382
JC
1627 struct v4l2_format *f)
1628{
1629 struct cafe_camera *cam = priv;
1630
1631 f->fmt.pix = cam->pix_format;
1632 return 0;
1633}
1634
1635/*
1636 * We only have one input - the sensor - so minimize the nonsense here.
1637 */
1638static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1639 struct v4l2_input *input)
1640{
1641 if (input->index != 0)
1642 return -EINVAL;
1643
1644 input->type = V4L2_INPUT_TYPE_CAMERA;
1645 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1646 strcpy(input->name, "Camera");
1647 return 0;
1648}
1649
1650static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1651{
1652 *i = 0;
1653 return 0;
1654}
1655
1656static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1657{
1658 if (i != 0)
1659 return -EINVAL;
1660 return 0;
1661}
1662
1663/* from vivi.c */
e75f9cee 1664static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
d905b382
JC
1665{
1666 return 0;
1667}
1668
c8f5b2f5
JC
1669/*
1670 * G/S_PARM. Most of this is done by the sensor, but we are
1671 * the level which controls the number of read buffers.
1672 */
1673static int cafe_vidioc_g_parm(struct file *filp, void *priv,
1674 struct v4l2_streamparm *parms)
1675{
1676 struct cafe_camera *cam = priv;
1677 int ret;
1678
1679 mutex_lock(&cam->s_mutex);
8bcfd7af 1680 ret = sensor_call(cam, video, g_parm, parms);
c8f5b2f5
JC
1681 mutex_unlock(&cam->s_mutex);
1682 parms->parm.capture.readbuffers = n_dma_bufs;
1683 return ret;
1684}
1685
1686static int cafe_vidioc_s_parm(struct file *filp, void *priv,
1687 struct v4l2_streamparm *parms)
1688{
1689 struct cafe_camera *cam = priv;
1690 int ret;
1691
1692 mutex_lock(&cam->s_mutex);
8bcfd7af 1693 ret = sensor_call(cam, video, s_parm, parms);
c8f5b2f5
JC
1694 mutex_unlock(&cam->s_mutex);
1695 parms->parm.capture.readbuffers = n_dma_bufs;
1696 return ret;
1697}
1698
69d94f7e
HV
1699static int cafe_vidioc_g_chip_ident(struct file *file, void *priv,
1700 struct v4l2_dbg_chip_ident *chip)
1701{
1702 struct cafe_camera *cam = priv;
1703
1704 chip->ident = V4L2_IDENT_NONE;
1705 chip->revision = 0;
1706 if (v4l2_chip_match_host(&chip->match)) {
1707 chip->ident = V4L2_IDENT_CAFE;
1708 return 0;
1709 }
1710 return sensor_call(cam, core, g_chip_ident, chip);
1711}
1712
96eb729a
DD
1713static int cafe_vidioc_enum_framesizes(struct file *filp, void *priv,
1714 struct v4l2_frmsizeenum *sizes)
1715{
1716 struct cafe_camera *cam = priv;
1717 int ret;
1718
1719 mutex_lock(&cam->s_mutex);
1720 ret = sensor_call(cam, video, enum_framesizes, sizes);
1721 mutex_unlock(&cam->s_mutex);
1722 return ret;
1723}
1724
1725static int cafe_vidioc_enum_frameintervals(struct file *filp, void *priv,
1726 struct v4l2_frmivalenum *interval)
1727{
1728 struct cafe_camera *cam = priv;
1729 int ret;
1730
1731 mutex_lock(&cam->s_mutex);
1732 ret = sensor_call(cam, video, enum_frameintervals, interval);
1733 mutex_unlock(&cam->s_mutex);
1734 return ret;
1735}
1736
69d94f7e
HV
1737#ifdef CONFIG_VIDEO_ADV_DEBUG
1738static int cafe_vidioc_g_register(struct file *file, void *priv,
1739 struct v4l2_dbg_register *reg)
1740{
1741 struct cafe_camera *cam = priv;
1742
1743 if (v4l2_chip_match_host(&reg->match)) {
1744 reg->val = cafe_reg_read(cam, reg->reg);
1745 reg->size = 4;
1746 return 0;
1747 }
1748 return sensor_call(cam, core, g_register, reg);
1749}
1750
1751static int cafe_vidioc_s_register(struct file *file, void *priv,
1752 struct v4l2_dbg_register *reg)
1753{
1754 struct cafe_camera *cam = priv;
1755
1756 if (v4l2_chip_match_host(&reg->match)) {
1757 cafe_reg_write(cam, reg->reg, reg->val);
1758 return 0;
1759 }
1760 return sensor_call(cam, core, s_register, reg);
1761}
1762#endif
1763
d905b382
JC
1764/*
1765 * This template device holds all of those v4l2 methods; we
1766 * clone it for specific real devices.
1767 */
1768
bec43661 1769static const struct v4l2_file_operations cafe_v4l_fops = {
d905b382
JC
1770 .owner = THIS_MODULE,
1771 .open = cafe_v4l_open,
1772 .release = cafe_v4l_release,
1773 .read = cafe_v4l_read,
1774 .poll = cafe_v4l_poll,
1775 .mmap = cafe_v4l_mmap,
1776 .ioctl = video_ioctl2,
d905b382
JC
1777};
1778
a399810c 1779static const struct v4l2_ioctl_ops cafe_v4l_ioctl_ops = {
d905b382 1780 .vidioc_querycap = cafe_vidioc_querycap,
78b526a4
HV
1781 .vidioc_enum_fmt_vid_cap = cafe_vidioc_enum_fmt_vid_cap,
1782 .vidioc_try_fmt_vid_cap = cafe_vidioc_try_fmt_vid_cap,
1783 .vidioc_s_fmt_vid_cap = cafe_vidioc_s_fmt_vid_cap,
1784 .vidioc_g_fmt_vid_cap = cafe_vidioc_g_fmt_vid_cap,
d905b382
JC
1785 .vidioc_enum_input = cafe_vidioc_enum_input,
1786 .vidioc_g_input = cafe_vidioc_g_input,
1787 .vidioc_s_input = cafe_vidioc_s_input,
1788 .vidioc_s_std = cafe_vidioc_s_std,
1789 .vidioc_reqbufs = cafe_vidioc_reqbufs,
1790 .vidioc_querybuf = cafe_vidioc_querybuf,
1791 .vidioc_qbuf = cafe_vidioc_qbuf,
1792 .vidioc_dqbuf = cafe_vidioc_dqbuf,
1793 .vidioc_streamon = cafe_vidioc_streamon,
1794 .vidioc_streamoff = cafe_vidioc_streamoff,
1795 .vidioc_queryctrl = cafe_vidioc_queryctrl,
1796 .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
1797 .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
c8f5b2f5
JC
1798 .vidioc_g_parm = cafe_vidioc_g_parm,
1799 .vidioc_s_parm = cafe_vidioc_s_parm,
96eb729a
DD
1800 .vidioc_enum_framesizes = cafe_vidioc_enum_framesizes,
1801 .vidioc_enum_frameintervals = cafe_vidioc_enum_frameintervals,
69d94f7e
HV
1802 .vidioc_g_chip_ident = cafe_vidioc_g_chip_ident,
1803#ifdef CONFIG_VIDEO_ADV_DEBUG
1804 .vidioc_g_register = cafe_vidioc_g_register,
1805 .vidioc_s_register = cafe_vidioc_s_register,
1806#endif
d905b382
JC
1807};
1808
a399810c
HV
1809static struct video_device cafe_v4l_template = {
1810 .name = "cafe",
a399810c
HV
1811 .tvnorms = V4L2_STD_NTSC_M,
1812 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1813
1814 .fops = &cafe_v4l_fops,
1815 .ioctl_ops = &cafe_v4l_ioctl_ops,
21508b90 1816 .release = video_device_release_empty,
a399810c
HV
1817};
1818
d905b382 1819
d905b382
JC
1820/* ---------------------------------------------------------------------- */
1821/*
1822 * Interrupt handler stuff
1823 */
1824
d905b382
JC
1825
1826
1827static void cafe_frame_tasklet(unsigned long data)
1828{
1829 struct cafe_camera *cam = (struct cafe_camera *) data;
1830 int i;
1831 unsigned long flags;
1832 struct cafe_sio_buffer *sbuf;
1833
1834 spin_lock_irqsave(&cam->dev_lock, flags);
1835 for (i = 0; i < cam->nbufs; i++) {
1836 int bufno = cam->next_buf;
1837 if (bufno < 0) { /* "will never happen" */
1838 cam_err(cam, "No valid bufs in tasklet!\n");
1839 break;
1840 }
1841 if (++(cam->next_buf) >= cam->nbufs)
1842 cam->next_buf = 0;
1843 if (! test_bit(bufno, &cam->flags))
1844 continue;
1845 if (list_empty(&cam->sb_avail))
1846 break; /* Leave it valid, hope for better later */
1847 clear_bit(bufno, &cam->flags);
d905b382
JC
1848 sbuf = list_entry(cam->sb_avail.next,
1849 struct cafe_sio_buffer, list);
5b50ed7c
JC
1850 /*
1851 * Drop the lock during the big copy. This *should* be safe...
1852 */
1853 spin_unlock_irqrestore(&cam->dev_lock, flags);
a66d2336
JC
1854 memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1855 cam->pix_format.sizeimage);
d905b382
JC
1856 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1857 sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1858 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1859 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
5b50ed7c 1860 spin_lock_irqsave(&cam->dev_lock, flags);
d905b382
JC
1861 list_move_tail(&sbuf->list, &cam->sb_full);
1862 }
1863 if (! list_empty(&cam->sb_full))
1864 wake_up(&cam->iowait);
1865 spin_unlock_irqrestore(&cam->dev_lock, flags);
1866}
1867
1868
1869
1870static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1871{
1872 /*
1873 * Basic frame housekeeping.
1874 */
1875 if (test_bit(frame, &cam->flags) && printk_ratelimit())
1876 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1877 set_bit(frame, &cam->flags);
1878 clear_bit(CF_DMA_ACTIVE, &cam->flags);
1879 if (cam->next_buf < 0)
1880 cam->next_buf = frame;
1881 cam->buf_seq[frame] = ++(cam->sequence);
1882
1883 switch (cam->state) {
1884 /*
1885 * If in single read mode, try going speculative.
1886 */
1887 case S_SINGLEREAD:
1888 cam->state = S_SPECREAD;
1889 cam->specframes = 0;
1890 wake_up(&cam->iowait);
1891 break;
1892
1893 /*
1894 * If we are already doing speculative reads, and nobody is
1895 * reading them, just stop.
1896 */
1897 case S_SPECREAD:
1898 if (++(cam->specframes) >= cam->nbufs) {
1899 cafe_ctlr_stop(cam);
1900 cafe_ctlr_irq_disable(cam);
1901 cam->state = S_IDLE;
1902 }
1903 wake_up(&cam->iowait);
1904 break;
1905 /*
1906 * For the streaming case, we defer the real work to the
1907 * camera tasklet.
1908 *
1909 * FIXME: if the application is not consuming the buffers,
1910 * we should eventually put things on hold and restart in
1911 * vidioc_dqbuf().
1912 */
1913 case S_STREAMING:
1914 tasklet_schedule(&cam->s_tasklet);
1915 break;
1916
1917 default:
1918 cam_err(cam, "Frame interrupt in non-operational state\n");
1919 break;
1920 }
1921}
1922
1923
1924
1925
1926static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1927{
1928 unsigned int frame;
1929
1930 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1931 /*
1932 * Handle any frame completions. There really should
1933 * not be more than one of these, or we have fallen
1934 * far behind.
1935 */
1936 for (frame = 0; frame < cam->nbufs; frame++)
1937 if (irqs & (IRQ_EOF0 << frame))
1938 cafe_frame_complete(cam, frame);
1939 /*
1940 * If a frame starts, note that we have DMA active. This
1941 * code assumes that we won't get multiple frame interrupts
1942 * at once; may want to rethink that.
1943 */
1944 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1945 set_bit(CF_DMA_ACTIVE, &cam->flags);
1946}
1947
1948
1949
1950static irqreturn_t cafe_irq(int irq, void *data)
1951{
1952 struct cafe_camera *cam = data;
1953 unsigned int irqs;
1954
1955 spin_lock(&cam->dev_lock);
1956 irqs = cafe_reg_read(cam, REG_IRQSTAT);
1957 if ((irqs & ALLIRQS) == 0) {
1958 spin_unlock(&cam->dev_lock);
1959 return IRQ_NONE;
1960 }
1961 if (irqs & FRAMEIRQS)
1962 cafe_frame_irq(cam, irqs);
1963 if (irqs & TWSIIRQS) {
1964 cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1965 wake_up(&cam->smbus_wait);
1966 }
1967 spin_unlock(&cam->dev_lock);
1968 return IRQ_HANDLED;
1969}
1970
1971
1972/* -------------------------------------------------------------------------- */
d905b382
JC
1973/*
1974 * PCI interface stuff.
1975 */
1976
1977static int cafe_pci_probe(struct pci_dev *pdev,
1978 const struct pci_device_id *id)
1979{
1980 int ret;
d905b382 1981 struct cafe_camera *cam;
aa7a7fb3 1982
d905b382
JC
1983 /*
1984 * Start putting together one of our big camera structures.
1985 */
1986 ret = -ENOMEM;
1987 cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
1988 if (cam == NULL)
1989 goto out;
21508b90
HV
1990 ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
1991 if (ret)
1992 goto out_free;
1993
d905b382 1994 mutex_init(&cam->s_mutex);
d905b382
JC
1995 spin_lock_init(&cam->dev_lock);
1996 cam->state = S_NOTREADY;
1997 cafe_set_config_needed(cam, 1);
1998 init_waitqueue_head(&cam->smbus_wait);
1999 init_waitqueue_head(&cam->iowait);
2000 cam->pdev = pdev;
2001 cam->pix_format = cafe_def_pix_format;
1e426228 2002 cam->mbus_code = cafe_def_mbus_code;
d905b382
JC
2003 INIT_LIST_HEAD(&cam->dev_list);
2004 INIT_LIST_HEAD(&cam->sb_avail);
2005 INIT_LIST_HEAD(&cam->sb_full);
2006 tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
2007 /*
2008 * Get set up on the PCI bus.
2009 */
2010 ret = pci_enable_device(pdev);
2011 if (ret)
21508b90 2012 goto out_unreg;
d905b382
JC
2013 pci_set_master(pdev);
2014
2015 ret = -EIO;
2016 cam->regs = pci_iomap(pdev, 0, 0);
2017 if (! cam->regs) {
2018 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
21508b90 2019 goto out_unreg;
d905b382
JC
2020 }
2021 ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
2022 if (ret)
2023 goto out_iounmap;
7acf90c7
JC
2024 /*
2025 * Initialize the controller and leave it powered up. It will
2026 * stay that way until the sensor driver shows up.
2027 */
d905b382
JC
2028 cafe_ctlr_init(cam);
2029 cafe_ctlr_power_up(cam);
2030 /*
7acf90c7
JC
2031 * Set up I2C/SMBUS communications. We have to drop the mutex here
2032 * because the sensor could attach in this call chain, leading to
2033 * unsightly deadlocks.
d905b382 2034 */
d905b382
JC
2035 ret = cafe_smbus_setup(cam);
2036 if (ret)
2037 goto out_freeirq;
8bcfd7af
HV
2038
2039 cam->sensor_addr = 0x42;
e6574f2f 2040 cam->sensor = v4l2_i2c_new_subdev(&cam->v4l2_dev, &cam->i2c_adapter,
53dacb15 2041 "ov7670", "ov7670", cam->sensor_addr, NULL);
8bcfd7af
HV
2042 if (cam->sensor == NULL) {
2043 ret = -ENODEV;
2044 goto out_smbus;
2045 }
2046 ret = cafe_cam_init(cam);
2047 if (ret)
2048 goto out_smbus;
2049
d905b382
JC
2050 /*
2051 * Get the v4l2 setup done.
2052 */
2053 mutex_lock(&cam->s_mutex);
21508b90
HV
2054 cam->vdev = cafe_v4l_template;
2055 cam->vdev.debug = 0;
2056/* cam->vdev.debug = V4L2_DEBUG_IOCTL_ARG;*/
2057 cam->vdev.v4l2_dev = &cam->v4l2_dev;
2058 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
d905b382 2059 if (ret)
0faf6f6b 2060 goto out_unlock;
21508b90
HV
2061 video_set_drvdata(&cam->vdev, cam);
2062
d905b382
JC
2063 /*
2064 * If so requested, try to get our DMA buffers now.
2065 */
23869e23 2066 if (!alloc_bufs_at_read) {
d905b382
JC
2067 if (cafe_alloc_dma_bufs(cam, 1))
2068 cam_warn(cam, "Unable to alloc DMA buffers at load"
2069 " will try again later.");
2070 }
2071
d905b382 2072 mutex_unlock(&cam->s_mutex);
d905b382
JC
2073 return 0;
2074
0faf6f6b
AS
2075out_unlock:
2076 mutex_unlock(&cam->s_mutex);
21508b90 2077out_smbus:
d905b382 2078 cafe_smbus_shutdown(cam);
21508b90 2079out_freeirq:
d905b382
JC
2080 cafe_ctlr_power_down(cam);
2081 free_irq(pdev->irq, cam);
21508b90 2082out_iounmap:
d905b382 2083 pci_iounmap(pdev, cam->regs);
21508b90
HV
2084out_free:
2085 v4l2_device_unregister(&cam->v4l2_dev);
2086out_unreg:
d905b382 2087 kfree(cam);
21508b90 2088out:
d905b382
JC
2089 return ret;
2090}
2091
2092
2093/*
2094 * Shut down an initialized device
2095 */
2096static void cafe_shutdown(struct cafe_camera *cam)
2097{
2098/* FIXME: Make sure we take care of everything here */
d905b382
JC
2099 if (cam->n_sbufs > 0)
2100 /* What if they are still mapped? Shouldn't be, but... */
2101 cafe_free_sio_buffers(cam);
d905b382
JC
2102 cafe_ctlr_stop_dma(cam);
2103 cafe_ctlr_power_down(cam);
2104 cafe_smbus_shutdown(cam);
2105 cafe_free_dma_bufs(cam);
2106 free_irq(cam->pdev->irq, cam);
2107 pci_iounmap(cam->pdev, cam->regs);
21508b90 2108 video_unregister_device(&cam->vdev);
d905b382
JC
2109}
2110
2111
2112static void cafe_pci_remove(struct pci_dev *pdev)
2113{
21508b90
HV
2114 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2115 struct cafe_camera *cam = to_cam(v4l2_dev);
d905b382
JC
2116
2117 if (cam == NULL) {
d4f60baf 2118 printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
d905b382
JC
2119 return;
2120 }
2121 mutex_lock(&cam->s_mutex);
2122 if (cam->users > 0)
2123 cam_warn(cam, "Removing a device with users!\n");
2124 cafe_shutdown(cam);
21508b90
HV
2125 v4l2_device_unregister(&cam->v4l2_dev);
2126 kfree(cam);
d905b382
JC
2127/* No unlock - it no longer exists */
2128}
2129
2130
ff68defa
JC
2131#ifdef CONFIG_PM
2132/*
2133 * Basic power management.
2134 */
2135static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2136{
21508b90
HV
2137 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2138 struct cafe_camera *cam = to_cam(v4l2_dev);
ff68defa 2139 int ret;
c3034497 2140 enum cafe_state cstate;
ff68defa
JC
2141
2142 ret = pci_save_state(pdev);
2143 if (ret)
2144 return ret;
c3034497 2145 cstate = cam->state; /* HACK - stop_dma sets to idle */
ff68defa
JC
2146 cafe_ctlr_stop_dma(cam);
2147 cafe_ctlr_power_down(cam);
2148 pci_disable_device(pdev);
c3034497 2149 cam->state = cstate;
ff68defa
JC
2150 return 0;
2151}
2152
2153
2154static int cafe_pci_resume(struct pci_dev *pdev)
2155{
21508b90
HV
2156 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2157 struct cafe_camera *cam = to_cam(v4l2_dev);
ff68defa
JC
2158 int ret = 0;
2159
2160 ret = pci_restore_state(pdev);
2161 if (ret)
2162 return ret;
12df2f54 2163 ret = pci_enable_device(pdev);
01659f2a 2164
12df2f54
TP
2165 if (ret) {
2166 cam_warn(cam, "Unable to re-enable device on resume!\n");
2167 return ret;
2168 }
ff68defa 2169 cafe_ctlr_init(cam);
01659f2a
CB
2170 cafe_ctlr_power_down(cam);
2171
2172 mutex_lock(&cam->s_mutex);
2173 if (cam->users > 0) {
2174 cafe_ctlr_power_up(cam);
2175 __cafe_cam_reset(cam);
2176 }
2177 mutex_unlock(&cam->s_mutex);
2178
ff68defa
JC
2179 set_bit(CF_CONFIG_NEEDED, &cam->flags);
2180 if (cam->state == S_SPECREAD)
2181 cam->state = S_IDLE; /* Don't bother restarting */
2182 else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
2183 ret = cafe_read_setup(cam, cam->state);
2184 return ret;
2185}
2186
2187#endif /* CONFIG_PM */
d905b382
JC
2188
2189
2190static struct pci_device_id cafe_ids[] = {
aa7a7fb3
DW
2191 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL,
2192 PCI_DEVICE_ID_MARVELL_88ALP01_CCIC) },
d905b382
JC
2193 { 0, }
2194};
2195
2196MODULE_DEVICE_TABLE(pci, cafe_ids);
2197
2198static struct pci_driver cafe_pci_driver = {
2199 .name = "cafe1000-ccic",
2200 .id_table = cafe_ids,
2201 .probe = cafe_pci_probe,
2202 .remove = cafe_pci_remove,
ff68defa
JC
2203#ifdef CONFIG_PM
2204 .suspend = cafe_pci_suspend,
2205 .resume = cafe_pci_resume,
2206#endif
d905b382
JC
2207};
2208
2209
2210
2211
2212static int __init cafe_init(void)
2213{
2214 int ret;
2215
2216 printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2217 CAFE_VERSION);
d905b382
JC
2218 ret = pci_register_driver(&cafe_pci_driver);
2219 if (ret) {
2220 printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2221 goto out;
2222 }
d905b382
JC
2223 ret = 0;
2224
2225 out:
2226 return ret;
2227}
2228
2229
2230static void __exit cafe_exit(void)
2231{
2232 pci_unregister_driver(&cafe_pci_driver);
d905b382
JC
2233}
2234
2235module_init(cafe_init);
2236module_exit(cafe_exit);