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