V4L/DVB (6026): Avoid powering up the camera on resume
[deliverable/linux.git] / drivers / media / video / cafe_ccic.c
CommitLineData
d905b382
JC
1/*
2 * A driver for the CMOS camera controller in the Marvell 88ALP01 "cafe"
3 * multifunction chip. Currently works with the Omnivision OV7670
4 * sensor.
5 *
6 * Copyright 2006 One Laptop Per Child Association, Inc.
77d5140f 7 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
d905b382
JC
8 *
9 * Written by Jonathan Corbet, corbet@lwn.net.
10 *
11 * This file may be distributed under the terms of the GNU General
12 * Public License, version 2.
13 */
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/init.h>
19#include <linux/fs.h>
20#include <linux/pci.h>
21#include <linux/i2c.h>
22#include <linux/interrupt.h>
23#include <linux/spinlock.h>
24#include <linux/videodev2.h>
25#include <media/v4l2-common.h>
3434eb7e 26#include <media/v4l2-chip-ident.h>
d905b382
JC
27#include <linux/device.h>
28#include <linux/wait.h>
29#include <linux/list.h>
30#include <linux/dma-mapping.h>
31#include <linux/delay.h>
32#include <linux/debugfs.h>
33#include <linux/jiffies.h>
34#include <linux/vmalloc.h>
35
36#include <asm/uaccess.h>
37#include <asm/io.h>
38
39#include "cafe_ccic-regs.h"
40
ff68defa 41#define CAFE_VERSION 0x000002
d905b382
JC
42
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 */
3434eb7e 169 u32 sensor_type; /* Currently ov7670 only */
d905b382
JC
170 struct v4l2_pix_format pix_format;
171
172 /* Locks */
173 struct mutex s_mutex; /* Access to this structure */
174 spinlock_t dev_lock; /* Access to device */
175
176 /* Misc */
177 wait_queue_head_t smbus_wait; /* Waiting on i2c events */
178 wait_queue_head_t iowait; /* Waiting on frame data */
179#ifdef CONFIG_VIDEO_ADV_DEBUG
180 struct dentry *dfs_regs;
181 struct dentry *dfs_cam_regs;
182#endif
183};
184
185/*
186 * Status flags. Always manipulated with bit operations.
187 */
188#define CF_BUF0_VALID 0 /* Buffers valid - first three */
189#define CF_BUF1_VALID 1
190#define CF_BUF2_VALID 2
191#define CF_DMA_ACTIVE 3 /* A frame is incoming */
192#define CF_CONFIG_NEEDED 4 /* Must configure hardware */
193
194
195
196/*
197 * Start over with DMA buffers - dev_lock needed.
198 */
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);
f9a76156
JC
510static void cafe_ctlr_stop_dma(struct cafe_camera *cam);
511static void cafe_ctlr_power_down(struct cafe_camera *cam);
d905b382
JC
512
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 */
d905b382
JC
520 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
f9a76156
JC
531 if (cam->sensor == client) {
532 cafe_ctlr_stop_dma(cam);
533 cafe_ctlr_power_down(cam);
534 cam_err(cam, "lost the sensor!\n");
d905b382 535 cam->sensor = NULL; /* Bummer, no camera */
f9a76156
JC
536 cam->state = S_NOTREADY;
537 }
d905b382
JC
538 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");
12a917f6 554 adap->dev.parent = &cam->pdev->dev;
d905b382
JC
555 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
d905b382
JC
618 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);
5b50ed7c
JC
709 /*
710 * Here we must wait a bit for the controller to come around.
711 */
712 spin_unlock_irqrestore(&cam->dev_lock, flags);
d905b382 713 mdelay(5); /* FIXME revisit this */
5b50ed7c
JC
714 spin_lock_irqsave(&cam->dev_lock, flags);
715
d905b382
JC
716 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);
7acf90c7
JC
777 /*
778 * Part one of the sensor dance: turn the global
779 * GPIO signal on.
780 */
781 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
782 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
d905b382
JC
783 /*
784 * Put the sensor into operational mode (assumes OLPC-style
785 * wiring). Control 0 is reset - set to 1 to operate.
786 * Control 1 is power down, set to 0 to operate.
787 */
f9a76156 788 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
5b50ed7c 789// mdelay(1); /* Marvell says 1ms will do it */
d905b382 790 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
5b50ed7c 791// mdelay(1); /* Enough? */
d905b382 792 spin_unlock_irqrestore(&cam->dev_lock, flags);
7acf90c7 793 msleep(5); /* Just to be sure */
d905b382
JC
794}
795
796static void cafe_ctlr_power_down(struct cafe_camera *cam)
797{
798 unsigned long flags;
799
800 spin_lock_irqsave(&cam->dev_lock, flags);
801 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
7acf90c7
JC
802 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
803 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT);
d905b382
JC
804 cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
805 spin_unlock_irqrestore(&cam->dev_lock, flags);
806}
807
808/* -------------------------------------------------------------------- */
809/*
810 * Communications with the sensor.
811 */
812
813static int __cafe_cam_cmd(struct cafe_camera *cam, int cmd, void *arg)
814{
815 struct i2c_client *sc = cam->sensor;
816 int ret;
817
818 if (sc == NULL || sc->driver == NULL || sc->driver->command == NULL)
819 return -EINVAL;
820 ret = sc->driver->command(sc, cmd, arg);
821 if (ret == -EPERM) /* Unsupported command */
822 return 0;
823 return ret;
824}
825
826static int __cafe_cam_reset(struct cafe_camera *cam)
827{
828 int zero = 0;
829 return __cafe_cam_cmd(cam, VIDIOC_INT_RESET, &zero);
830}
831
832/*
833 * We have found the sensor on the i2c. Let's try to have a
834 * conversation.
835 */
836static int cafe_cam_init(struct cafe_camera *cam)
837{
3434eb7e 838 struct v4l2_chip_ident chip = { V4L2_CHIP_MATCH_I2C_ADDR, 0, 0, 0 };
d905b382
JC
839 int ret;
840
841 mutex_lock(&cam->s_mutex);
842 if (cam->state != S_NOTREADY)
843 cam_warn(cam, "Cam init with device in funky state %d",
844 cam->state);
845 ret = __cafe_cam_reset(cam);
846 if (ret)
847 goto out;
3434eb7e
HV
848 chip.match_chip = cam->sensor->addr;
849 ret = __cafe_cam_cmd(cam, VIDIOC_G_CHIP_IDENT, &chip);
d905b382
JC
850 if (ret)
851 goto out;
3434eb7e 852 cam->sensor_type = chip.ident;
d905b382
JC
853// if (cam->sensor->addr != OV7xx0_SID) {
854 if (cam->sensor_type != V4L2_IDENT_OV7670) {
855 cam_err(cam, "Unsupported sensor type %d", cam->sensor->addr);
856 ret = -EINVAL;
857 goto out;
858 }
859/* Get/set parameters? */
860 ret = 0;
861 cam->state = S_IDLE;
862 out:
7acf90c7 863 cafe_ctlr_power_down(cam);
d905b382
JC
864 mutex_unlock(&cam->s_mutex);
865 return ret;
866}
867
868/*
869 * Configure the sensor to match the parameters we have. Caller should
870 * hold s_mutex
871 */
872static int cafe_cam_set_flip(struct cafe_camera *cam)
873{
874 struct v4l2_control ctrl;
875
876 memset(&ctrl, 0, sizeof(ctrl));
877 ctrl.id = V4L2_CID_VFLIP;
878 ctrl.value = flip;
879 return __cafe_cam_cmd(cam, VIDIOC_S_CTRL, &ctrl);
880}
881
882
883static int cafe_cam_configure(struct cafe_camera *cam)
884{
885 struct v4l2_format fmt;
886 int ret, zero = 0;
887
888 if (cam->state != S_IDLE)
889 return -EINVAL;
890 fmt.fmt.pix = cam->pix_format;
891 ret = __cafe_cam_cmd(cam, VIDIOC_INT_INIT, &zero);
892 if (ret == 0)
893 ret = __cafe_cam_cmd(cam, VIDIOC_S_FMT, &fmt);
894 /*
895 * OV7670 does weird things if flip is set *before* format...
896 */
897 ret += cafe_cam_set_flip(cam);
898 return ret;
899}
900
901/* -------------------------------------------------------------------- */
902/*
903 * DMA buffer management. These functions need s_mutex held.
904 */
905
906/* FIXME: this is inefficient as hell, since dma_alloc_coherent just
907 * does a get_free_pages() call, and we waste a good chunk of an orderN
908 * allocation. Should try to allocate the whole set in one chunk.
909 */
910static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
911{
912 int i;
913
914 cafe_set_config_needed(cam, 1);
915 if (loadtime)
916 cam->dma_buf_size = dma_buf_size;
a66d2336 917 else
d905b382 918 cam->dma_buf_size = cam->pix_format.sizeimage;
d905b382
JC
919 if (n_dma_bufs > 3)
920 n_dma_bufs = 3;
921
922 cam->nbufs = 0;
923 for (i = 0; i < n_dma_bufs; i++) {
924 cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
925 cam->dma_buf_size, cam->dma_handles + i,
926 GFP_KERNEL);
927 if (cam->dma_bufs[i] == NULL) {
928 cam_warn(cam, "Failed to allocate DMA buffer\n");
929 break;
930 }
931 /* For debug, remove eventually */
932 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
933 (cam->nbufs)++;
934 }
935
936 switch (cam->nbufs) {
937 case 1:
938 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
939 cam->dma_bufs[0], cam->dma_handles[0]);
940 cam->nbufs = 0;
941 case 0:
942 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
943 return -ENOMEM;
944
945 case 2:
946 if (n_dma_bufs > 2)
947 cam_warn(cam, "Will limp along with only 2 buffers\n");
948 break;
949 }
950 return 0;
951}
952
953static void cafe_free_dma_bufs(struct cafe_camera *cam)
954{
955 int i;
956
957 for (i = 0; i < cam->nbufs; i++) {
958 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
959 cam->dma_bufs[i], cam->dma_handles[i]);
960 cam->dma_bufs[i] = NULL;
961 }
962 cam->nbufs = 0;
963}
964
965
966
967
968
969/* ----------------------------------------------------------------------- */
970/*
971 * Here starts the V4L2 interface code.
972 */
973
974/*
975 * Read an image from the device.
976 */
977static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
978 char __user *buffer, size_t len, loff_t *pos)
979{
980 int bufno;
981 unsigned long flags;
982
983 spin_lock_irqsave(&cam->dev_lock, flags);
984 if (cam->next_buf < 0) {
985 cam_err(cam, "deliver_buffer: No next buffer\n");
986 spin_unlock_irqrestore(&cam->dev_lock, flags);
987 return -EIO;
988 }
989 bufno = cam->next_buf;
990 clear_bit(bufno, &cam->flags);
991 if (++(cam->next_buf) >= cam->nbufs)
992 cam->next_buf = 0;
993 if (! test_bit(cam->next_buf, &cam->flags))
994 cam->next_buf = -1;
995 cam->specframes = 0;
996 spin_unlock_irqrestore(&cam->dev_lock, flags);
997
998 if (len > cam->pix_format.sizeimage)
999 len = cam->pix_format.sizeimage;
1000 if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
1001 return -EFAULT;
1002 (*pos) += len;
1003 return len;
1004}
1005
1006/*
1007 * Get everything ready, and start grabbing frames.
1008 */
1009static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
1010{
1011 int ret;
1012 unsigned long flags;
1013
1014 /*
1015 * Configuration. If we still don't have DMA buffers,
1016 * make one last, desperate attempt.
1017 */
1018 if (cam->nbufs == 0)
1019 if (cafe_alloc_dma_bufs(cam, 0))
1020 return -ENOMEM;
1021
1022 if (cafe_needs_config(cam)) {
1023 cafe_cam_configure(cam);
1024 ret = cafe_ctlr_configure(cam);
1025 if (ret)
1026 return ret;
1027 }
1028
1029 /*
1030 * Turn it loose.
1031 */
1032 spin_lock_irqsave(&cam->dev_lock, flags);
1033 cafe_reset_buffers(cam);
1034 cafe_ctlr_irq_enable(cam);
1035 cam->state = state;
1036 cafe_ctlr_start(cam);
1037 spin_unlock_irqrestore(&cam->dev_lock, flags);
1038 return 0;
1039}
1040
1041
1042static ssize_t cafe_v4l_read(struct file *filp,
1043 char __user *buffer, size_t len, loff_t *pos)
1044{
1045 struct cafe_camera *cam = filp->private_data;
b9109b75 1046 int ret = 0;
d905b382
JC
1047
1048 /*
1049 * Perhaps we're in speculative read mode and already
1050 * have data?
1051 */
1052 mutex_lock(&cam->s_mutex);
1053 if (cam->state == S_SPECREAD) {
1054 if (cam->next_buf >= 0) {
1055 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1056 if (ret != 0)
1057 goto out_unlock;
1058 }
1059 } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
1060 ret = -EIO;
1061 goto out_unlock;
1062 } else if (cam->state != S_IDLE) {
1063 ret = -EBUSY;
1064 goto out_unlock;
1065 }
1066
1067 /*
1068 * v4l2: multiple processes can open the device, but only
1069 * one gets to grab data from it.
1070 */
1071 if (cam->owner && cam->owner != filp) {
1072 ret = -EBUSY;
1073 goto out_unlock;
1074 }
1075 cam->owner = filp;
1076
1077 /*
1078 * Do setup if need be.
1079 */
1080 if (cam->state != S_SPECREAD) {
1081 ret = cafe_read_setup(cam, S_SINGLEREAD);
1082 if (ret)
1083 goto out_unlock;
1084 }
1085 /*
1086 * Wait for something to happen. This should probably
1087 * be interruptible (FIXME).
1088 */
1089 wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
1090 if (cam->next_buf < 0) {
1091 cam_err(cam, "read() operation timed out\n");
1092 cafe_ctlr_stop_dma(cam);
1093 ret = -EIO;
1094 goto out_unlock;
1095 }
1096 /*
1097 * Give them their data and we should be done.
1098 */
1099 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1100
1101 out_unlock:
1102 mutex_unlock(&cam->s_mutex);
1103 return ret;
1104}
1105
1106
1107
1108
1109
1110
1111
1112
1113/*
1114 * Streaming I/O support.
1115 */
1116
1117
1118
1119static int cafe_vidioc_streamon(struct file *filp, void *priv,
1120 enum v4l2_buf_type type)
1121{
1122 struct cafe_camera *cam = filp->private_data;
1123 int ret = -EINVAL;
1124
1125 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1126 goto out;
1127 mutex_lock(&cam->s_mutex);
1128 if (cam->state != S_IDLE || cam->n_sbufs == 0)
1129 goto out_unlock;
1130
1131 cam->sequence = 0;
1132 ret = cafe_read_setup(cam, S_STREAMING);
1133
1134 out_unlock:
1135 mutex_unlock(&cam->s_mutex);
1136 out:
1137 return ret;
1138}
1139
1140
1141static int cafe_vidioc_streamoff(struct file *filp, void *priv,
1142 enum v4l2_buf_type type)
1143{
1144 struct cafe_camera *cam = filp->private_data;
1145 int ret = -EINVAL;
1146
1147 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1148 goto out;
1149 mutex_lock(&cam->s_mutex);
1150 if (cam->state != S_STREAMING)
1151 goto out_unlock;
1152
1153 cafe_ctlr_stop_dma(cam);
1154 ret = 0;
1155
1156 out_unlock:
1157 mutex_unlock(&cam->s_mutex);
1158 out:
1159 return ret;
1160}
1161
1162
1163
1164static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1165{
1166 struct cafe_sio_buffer *buf = cam->sb_bufs + index;
1167
1168 INIT_LIST_HEAD(&buf->list);
1169 buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
1170 buf->buffer = vmalloc_user(buf->v4lbuf.length);
1171 if (buf->buffer == NULL)
1172 return -ENOMEM;
1173 buf->mapcount = 0;
1174 buf->cam = cam;
1175
1176 buf->v4lbuf.index = index;
1177 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1178 buf->v4lbuf.field = V4L2_FIELD_NONE;
1179 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
1180 /*
1181 * Offset: must be 32-bit even on a 64-bit system. video-buf
1182 * just uses the length times the index, but the spec warns
1183 * against doing just that - vma merging problems. So we
1184 * leave a gap between each pair of buffers.
1185 */
1186 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1187 return 0;
1188}
1189
1190static int cafe_free_sio_buffers(struct cafe_camera *cam)
1191{
1192 int i;
1193
1194 /*
1195 * If any buffers are mapped, we cannot free them at all.
1196 */
1197 for (i = 0; i < cam->n_sbufs; i++)
1198 if (cam->sb_bufs[i].mapcount > 0)
1199 return -EBUSY;
1200 /*
1201 * OK, let's do it.
1202 */
1203 for (i = 0; i < cam->n_sbufs; i++)
1204 vfree(cam->sb_bufs[i].buffer);
1205 cam->n_sbufs = 0;
1206 kfree(cam->sb_bufs);
1207 cam->sb_bufs = NULL;
1208 INIT_LIST_HEAD(&cam->sb_avail);
1209 INIT_LIST_HEAD(&cam->sb_full);
1210 return 0;
1211}
1212
1213
1214
1215static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1216 struct v4l2_requestbuffers *req)
1217{
1218 struct cafe_camera *cam = filp->private_data;
3198cf67 1219 int ret = 0; /* Silence warning */
d905b382
JC
1220
1221 /*
1222 * Make sure it's something we can do. User pointers could be
1223 * implemented without great pain, but that's not been done yet.
1224 */
1225 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1226 return -EINVAL;
1227 if (req->memory != V4L2_MEMORY_MMAP)
1228 return -EINVAL;
1229 /*
1230 * If they ask for zero buffers, they really want us to stop streaming
1231 * (if it's happening) and free everything. Should we check owner?
1232 */
1233 mutex_lock(&cam->s_mutex);
1234 if (req->count == 0) {
1235 if (cam->state == S_STREAMING)
1236 cafe_ctlr_stop_dma(cam);
1237 ret = cafe_free_sio_buffers (cam);
1238 goto out;
1239 }
1240 /*
1241 * Device needs to be idle and working. We *could* try to do the
1242 * right thing in S_SPECREAD by shutting things down, but it
1243 * probably doesn't matter.
1244 */
1245 if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1246 ret = -EBUSY;
1247 goto out;
1248 }
1249 cam->owner = filp;
1250
1251 if (req->count < min_buffers)
1252 req->count = min_buffers;
1253 else if (req->count > max_buffers)
1254 req->count = max_buffers;
1255 if (cam->n_sbufs > 0) {
1256 ret = cafe_free_sio_buffers(cam);
1257 if (ret)
1258 goto out;
1259 }
1260
1261 cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1262 GFP_KERNEL);
1263 if (cam->sb_bufs == NULL) {
1264 ret = -ENOMEM;
1265 goto out;
1266 }
1267 for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1268 ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1269 if (ret)
1270 break;
1271 }
1272
1273 if (cam->n_sbufs == 0) /* no luck at all - ret already set */
1274 kfree(cam->sb_bufs);
d905b382
JC
1275 req->count = cam->n_sbufs; /* In case of partial success */
1276
1277 out:
1278 mutex_unlock(&cam->s_mutex);
1279 return ret;
1280}
1281
1282
1283static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1284 struct v4l2_buffer *buf)
1285{
1286 struct cafe_camera *cam = filp->private_data;
1287 int ret = -EINVAL;
1288
1289 mutex_lock(&cam->s_mutex);
1290 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1291 goto out;
1292 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1293 goto out;
1294 *buf = cam->sb_bufs[buf->index].v4lbuf;
1295 ret = 0;
1296 out:
1297 mutex_unlock(&cam->s_mutex);
1298 return ret;
1299}
1300
1301static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1302 struct v4l2_buffer *buf)
1303{
1304 struct cafe_camera *cam = filp->private_data;
1305 struct cafe_sio_buffer *sbuf;
1306 int ret = -EINVAL;
1307 unsigned long flags;
1308
1309 mutex_lock(&cam->s_mutex);
1310 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1311 goto out;
1312 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1313 goto out;
1314 sbuf = cam->sb_bufs + buf->index;
1315 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1316 ret = 0; /* Already queued?? */
1317 goto out;
1318 }
1319 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1320 /* Spec doesn't say anything, seems appropriate tho */
1321 ret = -EBUSY;
1322 goto out;
1323 }
1324 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1325 spin_lock_irqsave(&cam->dev_lock, flags);
1326 list_add(&sbuf->list, &cam->sb_avail);
1327 spin_unlock_irqrestore(&cam->dev_lock, flags);
1328 ret = 0;
1329 out:
1330 mutex_unlock(&cam->s_mutex);
1331 return ret;
1332}
1333
1334static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1335 struct v4l2_buffer *buf)
1336{
1337 struct cafe_camera *cam = filp->private_data;
1338 struct cafe_sio_buffer *sbuf;
1339 int ret = -EINVAL;
1340 unsigned long flags;
1341
1342 mutex_lock(&cam->s_mutex);
1343 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1344 goto out_unlock;
1345 if (cam->state != S_STREAMING)
1346 goto out_unlock;
1347 if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1348 ret = -EAGAIN;
1349 goto out_unlock;
1350 }
1351
1352 while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1353 mutex_unlock(&cam->s_mutex);
1354 if (wait_event_interruptible(cam->iowait,
1355 !list_empty(&cam->sb_full))) {
1356 ret = -ERESTARTSYS;
1357 goto out;
1358 }
1359 mutex_lock(&cam->s_mutex);
1360 }
1361
1362 if (cam->state != S_STREAMING)
1363 ret = -EINTR;
1364 else {
1365 spin_lock_irqsave(&cam->dev_lock, flags);
1366 /* Should probably recheck !list_empty() here */
1367 sbuf = list_entry(cam->sb_full.next,
1368 struct cafe_sio_buffer, list);
1369 list_del_init(&sbuf->list);
1370 spin_unlock_irqrestore(&cam->dev_lock, flags);
1371 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1372 *buf = sbuf->v4lbuf;
1373 ret = 0;
1374 }
1375
1376 out_unlock:
1377 mutex_unlock(&cam->s_mutex);
1378 out:
1379 return ret;
1380}
1381
1382
1383
1384static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1385{
1386 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1387 /*
1388 * Locking: done under mmap_sem, so we don't need to
1389 * go back to the camera lock here.
1390 */
1391 sbuf->mapcount++;
1392}
1393
1394
1395static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1396{
1397 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1398
1399 mutex_lock(&sbuf->cam->s_mutex);
1400 sbuf->mapcount--;
1401 /* Docs say we should stop I/O too... */
1402 if (sbuf->mapcount == 0)
1403 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1404 mutex_unlock(&sbuf->cam->s_mutex);
1405}
1406
1407static struct vm_operations_struct cafe_v4l_vm_ops = {
1408 .open = cafe_v4l_vm_open,
1409 .close = cafe_v4l_vm_close
1410};
1411
1412
1413static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1414{
1415 struct cafe_camera *cam = filp->private_data;
1416 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1417 int ret = -EINVAL;
1418 int i;
1419 struct cafe_sio_buffer *sbuf = NULL;
1420
1421 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1422 return -EINVAL;
1423 /*
1424 * Find the buffer they are looking for.
1425 */
1426 mutex_lock(&cam->s_mutex);
1427 for (i = 0; i < cam->n_sbufs; i++)
1428 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1429 sbuf = cam->sb_bufs + i;
1430 break;
1431 }
1432 if (sbuf == NULL)
1433 goto out;
1434
1435 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1436 if (ret)
1437 goto out;
1438 vma->vm_flags |= VM_DONTEXPAND;
1439 vma->vm_private_data = sbuf;
1440 vma->vm_ops = &cafe_v4l_vm_ops;
1441 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1442 cafe_v4l_vm_open(vma);
1443 ret = 0;
1444 out:
1445 mutex_unlock(&cam->s_mutex);
1446 return ret;
1447}
1448
1449
1450
1451static int cafe_v4l_open(struct inode *inode, struct file *filp)
1452{
1453 struct cafe_camera *cam;
1454
1455 cam = cafe_find_dev(iminor(inode));
1456 if (cam == NULL)
1457 return -ENODEV;
1458 filp->private_data = cam;
1459
1460 mutex_lock(&cam->s_mutex);
1461 if (cam->users == 0) {
1462 cafe_ctlr_power_up(cam);
1463 __cafe_cam_reset(cam);
1464 cafe_set_config_needed(cam, 1);
1465 /* FIXME make sure this is complete */
1466 }
1467 (cam->users)++;
1468 mutex_unlock(&cam->s_mutex);
1469 return 0;
1470}
1471
1472
1473static int cafe_v4l_release(struct inode *inode, struct file *filp)
1474{
1475 struct cafe_camera *cam = filp->private_data;
1476
1477 mutex_lock(&cam->s_mutex);
1478 (cam->users)--;
1479 if (filp == cam->owner) {
1480 cafe_ctlr_stop_dma(cam);
1481 cafe_free_sio_buffers(cam);
1482 cam->owner = NULL;
1483 }
f9a76156 1484 if (cam->users == 0) {
d905b382 1485 cafe_ctlr_power_down(cam);
f9a76156
JC
1486 if (! alloc_bufs_at_load)
1487 cafe_free_dma_bufs(cam);
1488 }
d905b382
JC
1489 mutex_unlock(&cam->s_mutex);
1490 return 0;
1491}
1492
1493
1494
1495static unsigned int cafe_v4l_poll(struct file *filp,
1496 struct poll_table_struct *pt)
1497{
1498 struct cafe_camera *cam = filp->private_data;
1499
1500 poll_wait(filp, &cam->iowait, pt);
1501 if (cam->next_buf >= 0)
1502 return POLLIN | POLLRDNORM;
1503 return 0;
1504}
1505
1506
1507
1508static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1509 struct v4l2_queryctrl *qc)
1510{
1511 struct cafe_camera *cam = filp->private_data;
1512 int ret;
1513
1514 mutex_lock(&cam->s_mutex);
1515 ret = __cafe_cam_cmd(cam, VIDIOC_QUERYCTRL, qc);
1516 mutex_unlock(&cam->s_mutex);
1517 return ret;
1518}
1519
1520
1521static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1522 struct v4l2_control *ctrl)
1523{
1524 struct cafe_camera *cam = filp->private_data;
1525 int ret;
1526
1527 mutex_lock(&cam->s_mutex);
1528 ret = __cafe_cam_cmd(cam, VIDIOC_G_CTRL, ctrl);
1529 mutex_unlock(&cam->s_mutex);
1530 return ret;
1531}
1532
1533
1534static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1535 struct v4l2_control *ctrl)
1536{
1537 struct cafe_camera *cam = filp->private_data;
1538 int ret;
1539
1540 mutex_lock(&cam->s_mutex);
1541 ret = __cafe_cam_cmd(cam, VIDIOC_S_CTRL, ctrl);
1542 mutex_unlock(&cam->s_mutex);
1543 return ret;
1544}
1545
1546
1547
1548
1549
1550static int cafe_vidioc_querycap(struct file *file, void *priv,
1551 struct v4l2_capability *cap)
1552{
1553 strcpy(cap->driver, "cafe_ccic");
1554 strcpy(cap->card, "cafe_ccic");
1555 cap->version = CAFE_VERSION;
1556 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1557 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1558 return 0;
1559}
1560
1561
1562/*
1563 * The default format we use until somebody says otherwise.
1564 */
1565static struct v4l2_pix_format cafe_def_pix_format = {
1566 .width = VGA_WIDTH,
1567 .height = VGA_HEIGHT,
1568 .pixelformat = V4L2_PIX_FMT_YUYV,
1569 .field = V4L2_FIELD_NONE,
1570 .bytesperline = VGA_WIDTH*2,
1571 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
1572};
1573
1574static int cafe_vidioc_enum_fmt_cap(struct file *filp,
1575 void *priv, struct v4l2_fmtdesc *fmt)
1576{
1577 struct cafe_camera *cam = priv;
1578 int ret;
1579
1580 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1581 return -EINVAL;
1582 mutex_lock(&cam->s_mutex);
1583 ret = __cafe_cam_cmd(cam, VIDIOC_ENUM_FMT, fmt);
1584 mutex_unlock(&cam->s_mutex);
1585 return ret;
1586}
1587
1588
1589static int cafe_vidioc_try_fmt_cap (struct file *filp, void *priv,
1590 struct v4l2_format *fmt)
1591{
1592 struct cafe_camera *cam = priv;
1593 int ret;
1594
1595 mutex_lock(&cam->s_mutex);
1596 ret = __cafe_cam_cmd(cam, VIDIOC_TRY_FMT, fmt);
1597 mutex_unlock(&cam->s_mutex);
1598 return ret;
1599}
1600
1601static int cafe_vidioc_s_fmt_cap(struct file *filp, void *priv,
1602 struct v4l2_format *fmt)
1603{
1604 struct cafe_camera *cam = priv;
1605 int ret;
1606
1607 /*
1608 * Can't do anything if the device is not idle
1609 * Also can't if there are streaming buffers in place.
1610 */
1611 if (cam->state != S_IDLE || cam->n_sbufs > 0)
1612 return -EBUSY;
1613 /*
1614 * See if the formatting works in principle.
1615 */
1616 ret = cafe_vidioc_try_fmt_cap(filp, priv, fmt);
1617 if (ret)
1618 return ret;
1619 /*
1620 * Now we start to change things for real, so let's do it
1621 * under lock.
1622 */
1623 mutex_lock(&cam->s_mutex);
1624 cam->pix_format = fmt->fmt.pix;
1625 /*
1626 * Make sure we have appropriate DMA buffers.
1627 */
1628 ret = -ENOMEM;
1629 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1630 cafe_free_dma_bufs(cam);
1631 if (cam->nbufs == 0) {
1632 if (cafe_alloc_dma_bufs(cam, 0))
1633 goto out;
1634 }
1635 /*
1636 * It looks like this might work, so let's program the sensor.
1637 */
1638 ret = cafe_cam_configure(cam);
1639 if (! ret)
1640 ret = cafe_ctlr_configure(cam);
1641 out:
1642 mutex_unlock(&cam->s_mutex);
1643 return ret;
1644}
1645
1646/*
1647 * Return our stored notion of how the camera is/should be configured.
1648 * The V4l2 spec wants us to be smarter, and actually get this from
1649 * the camera (and not mess with it at open time). Someday.
1650 */
1651static int cafe_vidioc_g_fmt_cap(struct file *filp, void *priv,
1652 struct v4l2_format *f)
1653{
1654 struct cafe_camera *cam = priv;
1655
1656 f->fmt.pix = cam->pix_format;
1657 return 0;
1658}
1659
1660/*
1661 * We only have one input - the sensor - so minimize the nonsense here.
1662 */
1663static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1664 struct v4l2_input *input)
1665{
1666 if (input->index != 0)
1667 return -EINVAL;
1668
1669 input->type = V4L2_INPUT_TYPE_CAMERA;
1670 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1671 strcpy(input->name, "Camera");
1672 return 0;
1673}
1674
1675static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1676{
1677 *i = 0;
1678 return 0;
1679}
1680
1681static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1682{
1683 if (i != 0)
1684 return -EINVAL;
1685 return 0;
1686}
1687
1688/* from vivi.c */
e75f9cee 1689static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
d905b382
JC
1690{
1691 return 0;
1692}
1693
c8f5b2f5
JC
1694/*
1695 * G/S_PARM. Most of this is done by the sensor, but we are
1696 * the level which controls the number of read buffers.
1697 */
1698static int cafe_vidioc_g_parm(struct file *filp, void *priv,
1699 struct v4l2_streamparm *parms)
1700{
1701 struct cafe_camera *cam = priv;
1702 int ret;
1703
1704 mutex_lock(&cam->s_mutex);
1705 ret = __cafe_cam_cmd(cam, VIDIOC_G_PARM, parms);
1706 mutex_unlock(&cam->s_mutex);
1707 parms->parm.capture.readbuffers = n_dma_bufs;
1708 return ret;
1709}
1710
1711static int cafe_vidioc_s_parm(struct file *filp, void *priv,
1712 struct v4l2_streamparm *parms)
1713{
1714 struct cafe_camera *cam = priv;
1715 int ret;
1716
1717 mutex_lock(&cam->s_mutex);
1718 ret = __cafe_cam_cmd(cam, VIDIOC_S_PARM, parms);
1719 mutex_unlock(&cam->s_mutex);
1720 parms->parm.capture.readbuffers = n_dma_bufs;
1721 return ret;
1722}
1723
1724
ab33668f 1725static void cafe_v4l_dev_release(struct video_device *vd)
d905b382
JC
1726{
1727 struct cafe_camera *cam = container_of(vd, struct cafe_camera, v4ldev);
1728
1729 kfree(cam);
1730}
1731
1732
1733/*
1734 * This template device holds all of those v4l2 methods; we
1735 * clone it for specific real devices.
1736 */
1737
fa027c2a 1738static const struct file_operations cafe_v4l_fops = {
d905b382
JC
1739 .owner = THIS_MODULE,
1740 .open = cafe_v4l_open,
1741 .release = cafe_v4l_release,
1742 .read = cafe_v4l_read,
1743 .poll = cafe_v4l_poll,
1744 .mmap = cafe_v4l_mmap,
1745 .ioctl = video_ioctl2,
1746 .llseek = no_llseek,
1747};
1748
1749static struct video_device cafe_v4l_template = {
1750 .name = "cafe",
1751 .type = VFL_TYPE_GRABBER,
1752 .type2 = VID_TYPE_CAPTURE,
1753 .minor = -1, /* Get one dynamically */
e75f9cee 1754 .tvnorms = V4L2_STD_NTSC_M,
d905b382
JC
1755 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1756
1757 .fops = &cafe_v4l_fops,
1758 .release = cafe_v4l_dev_release,
1759
1760 .vidioc_querycap = cafe_vidioc_querycap,
1761 .vidioc_enum_fmt_cap = cafe_vidioc_enum_fmt_cap,
1762 .vidioc_try_fmt_cap = cafe_vidioc_try_fmt_cap,
1763 .vidioc_s_fmt_cap = cafe_vidioc_s_fmt_cap,
1764 .vidioc_g_fmt_cap = cafe_vidioc_g_fmt_cap,
1765 .vidioc_enum_input = cafe_vidioc_enum_input,
1766 .vidioc_g_input = cafe_vidioc_g_input,
1767 .vidioc_s_input = cafe_vidioc_s_input,
1768 .vidioc_s_std = cafe_vidioc_s_std,
1769 .vidioc_reqbufs = cafe_vidioc_reqbufs,
1770 .vidioc_querybuf = cafe_vidioc_querybuf,
1771 .vidioc_qbuf = cafe_vidioc_qbuf,
1772 .vidioc_dqbuf = cafe_vidioc_dqbuf,
1773 .vidioc_streamon = cafe_vidioc_streamon,
1774 .vidioc_streamoff = cafe_vidioc_streamoff,
1775 .vidioc_queryctrl = cafe_vidioc_queryctrl,
1776 .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
1777 .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
c8f5b2f5
JC
1778 .vidioc_g_parm = cafe_vidioc_g_parm,
1779 .vidioc_s_parm = cafe_vidioc_s_parm,
d905b382
JC
1780};
1781
1782
1783
1784
1785
1786
1787
1788/* ---------------------------------------------------------------------- */
1789/*
1790 * Interrupt handler stuff
1791 */
1792
d905b382
JC
1793
1794
1795static void cafe_frame_tasklet(unsigned long data)
1796{
1797 struct cafe_camera *cam = (struct cafe_camera *) data;
1798 int i;
1799 unsigned long flags;
1800 struct cafe_sio_buffer *sbuf;
1801
1802 spin_lock_irqsave(&cam->dev_lock, flags);
1803 for (i = 0; i < cam->nbufs; i++) {
1804 int bufno = cam->next_buf;
1805 if (bufno < 0) { /* "will never happen" */
1806 cam_err(cam, "No valid bufs in tasklet!\n");
1807 break;
1808 }
1809 if (++(cam->next_buf) >= cam->nbufs)
1810 cam->next_buf = 0;
1811 if (! test_bit(bufno, &cam->flags))
1812 continue;
1813 if (list_empty(&cam->sb_avail))
1814 break; /* Leave it valid, hope for better later */
1815 clear_bit(bufno, &cam->flags);
d905b382
JC
1816 sbuf = list_entry(cam->sb_avail.next,
1817 struct cafe_sio_buffer, list);
5b50ed7c
JC
1818 /*
1819 * Drop the lock during the big copy. This *should* be safe...
1820 */
1821 spin_unlock_irqrestore(&cam->dev_lock, flags);
a66d2336
JC
1822 memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1823 cam->pix_format.sizeimage);
d905b382
JC
1824 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1825 sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1826 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1827 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
5b50ed7c 1828 spin_lock_irqsave(&cam->dev_lock, flags);
d905b382
JC
1829 list_move_tail(&sbuf->list, &cam->sb_full);
1830 }
1831 if (! list_empty(&cam->sb_full))
1832 wake_up(&cam->iowait);
1833 spin_unlock_irqrestore(&cam->dev_lock, flags);
1834}
1835
1836
1837
1838static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1839{
1840 /*
1841 * Basic frame housekeeping.
1842 */
1843 if (test_bit(frame, &cam->flags) && printk_ratelimit())
1844 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1845 set_bit(frame, &cam->flags);
1846 clear_bit(CF_DMA_ACTIVE, &cam->flags);
1847 if (cam->next_buf < 0)
1848 cam->next_buf = frame;
1849 cam->buf_seq[frame] = ++(cam->sequence);
1850
1851 switch (cam->state) {
1852 /*
1853 * If in single read mode, try going speculative.
1854 */
1855 case S_SINGLEREAD:
1856 cam->state = S_SPECREAD;
1857 cam->specframes = 0;
1858 wake_up(&cam->iowait);
1859 break;
1860
1861 /*
1862 * If we are already doing speculative reads, and nobody is
1863 * reading them, just stop.
1864 */
1865 case S_SPECREAD:
1866 if (++(cam->specframes) >= cam->nbufs) {
1867 cafe_ctlr_stop(cam);
1868 cafe_ctlr_irq_disable(cam);
1869 cam->state = S_IDLE;
1870 }
1871 wake_up(&cam->iowait);
1872 break;
1873 /*
1874 * For the streaming case, we defer the real work to the
1875 * camera tasklet.
1876 *
1877 * FIXME: if the application is not consuming the buffers,
1878 * we should eventually put things on hold and restart in
1879 * vidioc_dqbuf().
1880 */
1881 case S_STREAMING:
1882 tasklet_schedule(&cam->s_tasklet);
1883 break;
1884
1885 default:
1886 cam_err(cam, "Frame interrupt in non-operational state\n");
1887 break;
1888 }
1889}
1890
1891
1892
1893
1894static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1895{
1896 unsigned int frame;
1897
1898 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1899 /*
1900 * Handle any frame completions. There really should
1901 * not be more than one of these, or we have fallen
1902 * far behind.
1903 */
1904 for (frame = 0; frame < cam->nbufs; frame++)
1905 if (irqs & (IRQ_EOF0 << frame))
1906 cafe_frame_complete(cam, frame);
1907 /*
1908 * If a frame starts, note that we have DMA active. This
1909 * code assumes that we won't get multiple frame interrupts
1910 * at once; may want to rethink that.
1911 */
1912 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1913 set_bit(CF_DMA_ACTIVE, &cam->flags);
1914}
1915
1916
1917
1918static irqreturn_t cafe_irq(int irq, void *data)
1919{
1920 struct cafe_camera *cam = data;
1921 unsigned int irqs;
1922
1923 spin_lock(&cam->dev_lock);
1924 irqs = cafe_reg_read(cam, REG_IRQSTAT);
1925 if ((irqs & ALLIRQS) == 0) {
1926 spin_unlock(&cam->dev_lock);
1927 return IRQ_NONE;
1928 }
1929 if (irqs & FRAMEIRQS)
1930 cafe_frame_irq(cam, irqs);
1931 if (irqs & TWSIIRQS) {
1932 cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1933 wake_up(&cam->smbus_wait);
1934 }
1935 spin_unlock(&cam->dev_lock);
1936 return IRQ_HANDLED;
1937}
1938
1939
1940/* -------------------------------------------------------------------------- */
1941#ifdef CONFIG_VIDEO_ADV_DEBUG
1942/*
1943 * Debugfs stuff.
1944 */
1945
1946static char cafe_debug_buf[1024];
1947static struct dentry *cafe_dfs_root;
1948
1949static void cafe_dfs_setup(void)
1950{
1951 cafe_dfs_root = debugfs_create_dir("cafe_ccic", NULL);
1952 if (IS_ERR(cafe_dfs_root)) {
1953 cafe_dfs_root = NULL; /* Never mind */
1954 printk(KERN_NOTICE "cafe_ccic unable to set up debugfs\n");
1955 }
1956}
1957
1958static void cafe_dfs_shutdown(void)
1959{
1960 if (cafe_dfs_root)
1961 debugfs_remove(cafe_dfs_root);
1962}
1963
1964static int cafe_dfs_open(struct inode *inode, struct file *file)
1965{
1966 file->private_data = inode->i_private;
1967 return 0;
1968}
1969
1970static ssize_t cafe_dfs_read_regs(struct file *file,
1971 char __user *buf, size_t count, loff_t *ppos)
1972{
1973 struct cafe_camera *cam = file->private_data;
1974 char *s = cafe_debug_buf;
1975 int offset;
1976
1977 for (offset = 0; offset < 0x44; offset += 4)
1978 s += sprintf(s, "%02x: %08x\n", offset,
1979 cafe_reg_read(cam, offset));
1980 for (offset = 0x88; offset <= 0x90; offset += 4)
1981 s += sprintf(s, "%02x: %08x\n", offset,
1982 cafe_reg_read(cam, offset));
1983 for (offset = 0xb4; offset <= 0xbc; offset += 4)
1984 s += sprintf(s, "%02x: %08x\n", offset,
1985 cafe_reg_read(cam, offset));
1986 for (offset = 0x3000; offset <= 0x300c; offset += 4)
1987 s += sprintf(s, "%04x: %08x\n", offset,
1988 cafe_reg_read(cam, offset));
1989 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
1990 s - cafe_debug_buf);
1991}
1992
fa027c2a 1993static const struct file_operations cafe_dfs_reg_ops = {
d905b382
JC
1994 .owner = THIS_MODULE,
1995 .read = cafe_dfs_read_regs,
1996 .open = cafe_dfs_open
1997};
1998
1999static ssize_t cafe_dfs_read_cam(struct file *file,
2000 char __user *buf, size_t count, loff_t *ppos)
2001{
2002 struct cafe_camera *cam = file->private_data;
2003 char *s = cafe_debug_buf;
2004 int offset;
2005
2006 if (! cam->sensor)
2007 return -EINVAL;
2008 for (offset = 0x0; offset < 0x8a; offset++)
2009 {
2010 u8 v;
2011
2012 cafe_smbus_read_data(cam, cam->sensor->addr, offset, &v);
2013 s += sprintf(s, "%02x: %02x\n", offset, v);
2014 }
2015 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
2016 s - cafe_debug_buf);
2017}
2018
fa027c2a 2019static const struct file_operations cafe_dfs_cam_ops = {
d905b382
JC
2020 .owner = THIS_MODULE,
2021 .read = cafe_dfs_read_cam,
2022 .open = cafe_dfs_open
2023};
2024
2025
2026
2027static void cafe_dfs_cam_setup(struct cafe_camera *cam)
2028{
2029 char fname[40];
2030
2031 if (!cafe_dfs_root)
2032 return;
2033 sprintf(fname, "regs-%d", cam->v4ldev.minor);
2034 cam->dfs_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2035 cam, &cafe_dfs_reg_ops);
2036 sprintf(fname, "cam-%d", cam->v4ldev.minor);
2037 cam->dfs_cam_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2038 cam, &cafe_dfs_cam_ops);
2039}
2040
2041
2042static void cafe_dfs_cam_shutdown(struct cafe_camera *cam)
2043{
2044 if (! IS_ERR(cam->dfs_regs))
2045 debugfs_remove(cam->dfs_regs);
2046 if (! IS_ERR(cam->dfs_cam_regs))
2047 debugfs_remove(cam->dfs_cam_regs);
2048}
2049
2050#else
2051
2052#define cafe_dfs_setup()
2053#define cafe_dfs_shutdown()
2054#define cafe_dfs_cam_setup(cam)
2055#define cafe_dfs_cam_shutdown(cam)
2056#endif /* CONFIG_VIDEO_ADV_DEBUG */
2057
2058
2059
2060
2061/* ------------------------------------------------------------------------*/
2062/*
2063 * PCI interface stuff.
2064 */
2065
2066static int cafe_pci_probe(struct pci_dev *pdev,
2067 const struct pci_device_id *id)
2068{
2069 int ret;
2070 u16 classword;
2071 struct cafe_camera *cam;
2072 /*
2073 * Make sure we have a camera here - we'll get calls for
2074 * the other cafe devices as well.
2075 */
2076 pci_read_config_word(pdev, PCI_CLASS_DEVICE, &classword);
2077 if (classword != PCI_CLASS_MULTIMEDIA_VIDEO)
2078 return -ENODEV;
2079 /*
2080 * Start putting together one of our big camera structures.
2081 */
2082 ret = -ENOMEM;
2083 cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
2084 if (cam == NULL)
2085 goto out;
2086 mutex_init(&cam->s_mutex);
2087 mutex_lock(&cam->s_mutex);
2088 spin_lock_init(&cam->dev_lock);
2089 cam->state = S_NOTREADY;
2090 cafe_set_config_needed(cam, 1);
2091 init_waitqueue_head(&cam->smbus_wait);
2092 init_waitqueue_head(&cam->iowait);
2093 cam->pdev = pdev;
2094 cam->pix_format = cafe_def_pix_format;
2095 INIT_LIST_HEAD(&cam->dev_list);
2096 INIT_LIST_HEAD(&cam->sb_avail);
2097 INIT_LIST_HEAD(&cam->sb_full);
2098 tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
2099 /*
2100 * Get set up on the PCI bus.
2101 */
2102 ret = pci_enable_device(pdev);
2103 if (ret)
2104 goto out_free;
2105 pci_set_master(pdev);
2106
2107 ret = -EIO;
2108 cam->regs = pci_iomap(pdev, 0, 0);
2109 if (! cam->regs) {
2110 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
2111 goto out_free;
2112 }
2113 ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
2114 if (ret)
2115 goto out_iounmap;
7acf90c7
JC
2116 /*
2117 * Initialize the controller and leave it powered up. It will
2118 * stay that way until the sensor driver shows up.
2119 */
d905b382
JC
2120 cafe_ctlr_init(cam);
2121 cafe_ctlr_power_up(cam);
2122 /*
7acf90c7
JC
2123 * Set up I2C/SMBUS communications. We have to drop the mutex here
2124 * because the sensor could attach in this call chain, leading to
2125 * unsightly deadlocks.
d905b382
JC
2126 */
2127 mutex_unlock(&cam->s_mutex); /* attach can deadlock */
2128 ret = cafe_smbus_setup(cam);
2129 if (ret)
2130 goto out_freeirq;
2131 /*
2132 * Get the v4l2 setup done.
2133 */
2134 mutex_lock(&cam->s_mutex);
2135 cam->v4ldev = cafe_v4l_template;
2136 cam->v4ldev.debug = 0;
2137// cam->v4ldev.debug = V4L2_DEBUG_IOCTL_ARG;
018cfd51 2138 cam->v4ldev.dev = &pdev->dev;
d905b382
JC
2139 ret = video_register_device(&cam->v4ldev, VFL_TYPE_GRABBER, -1);
2140 if (ret)
2141 goto out_smbus;
2142 /*
2143 * If so requested, try to get our DMA buffers now.
2144 */
2145 if (alloc_bufs_at_load) {
2146 if (cafe_alloc_dma_bufs(cam, 1))
2147 cam_warn(cam, "Unable to alloc DMA buffers at load"
2148 " will try again later.");
2149 }
2150
2151 cafe_dfs_cam_setup(cam);
2152 mutex_unlock(&cam->s_mutex);
2153 cafe_add_dev(cam);
2154 return 0;
2155
2156 out_smbus:
2157 cafe_smbus_shutdown(cam);
2158 out_freeirq:
2159 cafe_ctlr_power_down(cam);
2160 free_irq(pdev->irq, cam);
2161 out_iounmap:
2162 pci_iounmap(pdev, cam->regs);
2163 out_free:
2164 kfree(cam);
2165 out:
2166 return ret;
2167}
2168
2169
2170/*
2171 * Shut down an initialized device
2172 */
2173static void cafe_shutdown(struct cafe_camera *cam)
2174{
2175/* FIXME: Make sure we take care of everything here */
2176 cafe_dfs_cam_shutdown(cam);
2177 if (cam->n_sbufs > 0)
2178 /* What if they are still mapped? Shouldn't be, but... */
2179 cafe_free_sio_buffers(cam);
2180 cafe_remove_dev(cam);
2181 cafe_ctlr_stop_dma(cam);
2182 cafe_ctlr_power_down(cam);
2183 cafe_smbus_shutdown(cam);
2184 cafe_free_dma_bufs(cam);
2185 free_irq(cam->pdev->irq, cam);
2186 pci_iounmap(cam->pdev, cam->regs);
2187 video_unregister_device(&cam->v4ldev);
2188 /* kfree(cam); done in v4l_release () */
2189}
2190
2191
2192static void cafe_pci_remove(struct pci_dev *pdev)
2193{
2194 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2195
2196 if (cam == NULL) {
d4f60baf 2197 printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
d905b382
JC
2198 return;
2199 }
2200 mutex_lock(&cam->s_mutex);
2201 if (cam->users > 0)
2202 cam_warn(cam, "Removing a device with users!\n");
2203 cafe_shutdown(cam);
2204/* No unlock - it no longer exists */
2205}
2206
2207
ff68defa
JC
2208#ifdef CONFIG_PM
2209/*
2210 * Basic power management.
2211 */
2212static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2213{
2214 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2215 int ret;
2216
2217 ret = pci_save_state(pdev);
2218 if (ret)
2219 return ret;
2220 cafe_ctlr_stop_dma(cam);
2221 cafe_ctlr_power_down(cam);
2222 pci_disable_device(pdev);
2223 return 0;
2224}
2225
2226
2227static int cafe_pci_resume(struct pci_dev *pdev)
2228{
2229 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2230 int ret = 0;
2231
2232 ret = pci_restore_state(pdev);
2233 if (ret)
2234 return ret;
12df2f54 2235 ret = pci_enable_device(pdev);
01659f2a 2236
12df2f54
TP
2237 if (ret) {
2238 cam_warn(cam, "Unable to re-enable device on resume!\n");
2239 return ret;
2240 }
ff68defa 2241 cafe_ctlr_init(cam);
01659f2a
CB
2242 cafe_ctlr_power_down(cam);
2243
2244 mutex_lock(&cam->s_mutex);
2245 if (cam->users > 0) {
2246 cafe_ctlr_power_up(cam);
2247 __cafe_cam_reset(cam);
2248 }
2249 mutex_unlock(&cam->s_mutex);
2250
ff68defa
JC
2251 set_bit(CF_CONFIG_NEEDED, &cam->flags);
2252 if (cam->state == S_SPECREAD)
2253 cam->state = S_IDLE; /* Don't bother restarting */
2254 else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
2255 ret = cafe_read_setup(cam, cam->state);
2256 return ret;
2257}
2258
2259#endif /* CONFIG_PM */
d905b382
JC
2260
2261
2262static struct pci_device_id cafe_ids[] = {
d905b382
JC
2263 { PCI_DEVICE(0x11ab, 0x4100) }, /* Eventual real ID */
2264 { PCI_DEVICE(0x11ab, 0x4102) }, /* Really eventual real ID */
2265 { 0, }
2266};
2267
2268MODULE_DEVICE_TABLE(pci, cafe_ids);
2269
2270static struct pci_driver cafe_pci_driver = {
2271 .name = "cafe1000-ccic",
2272 .id_table = cafe_ids,
2273 .probe = cafe_pci_probe,
2274 .remove = cafe_pci_remove,
ff68defa
JC
2275#ifdef CONFIG_PM
2276 .suspend = cafe_pci_suspend,
2277 .resume = cafe_pci_resume,
2278#endif
d905b382
JC
2279};
2280
2281
2282
2283
2284static int __init cafe_init(void)
2285{
2286 int ret;
2287
2288 printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2289 CAFE_VERSION);
2290 cafe_dfs_setup();
2291 ret = pci_register_driver(&cafe_pci_driver);
2292 if (ret) {
2293 printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2294 goto out;
2295 }
2296 request_module("ov7670"); /* FIXME want something more general */
2297 ret = 0;
2298
2299 out:
2300 return ret;
2301}
2302
2303
2304static void __exit cafe_exit(void)
2305{
2306 pci_unregister_driver(&cafe_pci_driver);
2307 cafe_dfs_shutdown();
2308}
2309
2310module_init(cafe_init);
2311module_exit(cafe_exit);
This page took 0.204117 seconds and 5 git commands to generate.