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