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