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