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