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