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