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