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