blob: c4d181dde1cafb17ebf6ec80d2b20f9125b5cae9 [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 *
Jonathan Corbetbb8d56a2007-10-23 17:31:36 -03006 * The data sheet for this device can be found at:
7 * http://www.marvell.com/products/pcconn/88ALP01.jsp
8 *
Jonathan Corbetd905b382006-11-04 09:25:53 -03009 * Copyright 2006 One Laptop Per Child Association, Inc.
Jonathan Corbet77d51402007-03-22 19:44:17 -030010 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
Jonathan Corbetd905b382006-11-04 09:25:53 -030011 *
12 * Written by Jonathan Corbet, corbet@lwn.net.
13 *
Hans Verkuilacc5d852009-03-18 13:31:56 -030014 * v4l2_device/v4l2_subdev conversion by:
15 * Copyright (C) 2009 Hans Verkuil <hverkuil@xs4all.nl>
16 *
17 * Note: this conversion is untested! Please contact the linux-media
18 * mailinglist if you can test this, together with the test results.
19 *
Jonathan Corbetd905b382006-11-04 09:25:53 -030020 * This file may be distributed under the terms of the GNU General
21 * Public License, version 2.
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
Jonathan Corbetd905b382006-11-04 09:25:53 -030026#include <linux/init.h>
27#include <linux/fs.h>
Andrew Mortonec16d022008-09-03 02:15:39 -030028#include <linux/mm.h>
Jonathan Corbetd905b382006-11-04 09:25:53 -030029#include <linux/pci.h>
30#include <linux/i2c.h>
31#include <linux/interrupt.h>
32#include <linux/spinlock.h>
33#include <linux/videodev2.h>
Hans Verkuil21508b902009-03-18 13:13:09 -030034#include <media/v4l2-device.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030035#include <media/v4l2-ioctl.h>
Hans Verkuil3434eb72007-04-27 12:31:08 -030036#include <media/v4l2-chip-ident.h>
Jonathan Corbetd905b382006-11-04 09:25:53 -030037#include <linux/device.h>
38#include <linux/wait.h>
39#include <linux/list.h>
40#include <linux/dma-mapping.h>
41#include <linux/delay.h>
Jonathan Corbetd905b382006-11-04 09:25:53 -030042#include <linux/jiffies.h>
43#include <linux/vmalloc.h>
44
45#include <asm/uaccess.h>
46#include <asm/io.h>
47
48#include "cafe_ccic-regs.h"
49
Jonathan Corbetff68def2007-03-25 11:36:28 -030050#define CAFE_VERSION 0x000002
Jonathan Corbetd905b382006-11-04 09:25:53 -030051
52
53/*
54 * Parameters.
55 */
56MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
57MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
58MODULE_LICENSE("GPL");
59MODULE_SUPPORTED_DEVICE("Video");
60
61/*
62 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
63 * we must have physically contiguous buffers to bring frames into.
64 * These parameters control how many buffers we use, whether we
65 * allocate them at load time (better chance of success, but nails down
66 * memory) or when somebody tries to use the camera (riskier), and,
67 * for load-time allocation, how big they should be.
68 *
69 * The controller can cycle through three buffers. We could use
70 * more by flipping pointers around, but it probably makes little
71 * sense.
72 */
73
74#define MAX_DMA_BUFS 3
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030075static int alloc_bufs_at_read;
Andres Salomon23869e22007-09-19 02:44:18 -030076module_param(alloc_bufs_at_read, bool, 0444);
77MODULE_PARM_DESC(alloc_bufs_at_read,
78 "Non-zero value causes DMA buffers to be allocated when the "
79 "video capture device is read, rather than at module load "
80 "time. This saves memory, but decreases the chances of "
81 "successfully getting those buffers.");
Jonathan Corbetd905b382006-11-04 09:25:53 -030082
83static int n_dma_bufs = 3;
84module_param(n_dma_bufs, uint, 0644);
85MODULE_PARM_DESC(n_dma_bufs,
86 "The number of DMA buffers to allocate. Can be either two "
87 "(saves memory, makes timing tighter) or three.");
88
89static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
90module_param(dma_buf_size, uint, 0444);
91MODULE_PARM_DESC(dma_buf_size,
92 "The size of the allocated DMA buffers. If actual operating "
93 "parameters require larger buffers, an attempt to reallocate "
94 "will be made.");
95
96static int min_buffers = 1;
97module_param(min_buffers, uint, 0644);
98MODULE_PARM_DESC(min_buffers,
99 "The minimum number of streaming I/O buffers we are willing "
100 "to work with.");
101
102static int max_buffers = 10;
103module_param(max_buffers, uint, 0644);
104MODULE_PARM_DESC(max_buffers,
105 "The maximum number of streaming I/O buffers an application "
106 "will be allowed to allocate. These buffers are big and live "
107 "in vmalloc space.");
108
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300109static int flip;
Jonathan Corbetd905b382006-11-04 09:25:53 -0300110module_param(flip, bool, 0444);
111MODULE_PARM_DESC(flip,
112 "If set, the sensor will be instructed to flip the image "
113 "vertically.");
114
115
116enum cafe_state {
117 S_NOTREADY, /* Not yet initialized */
118 S_IDLE, /* Just hanging around */
119 S_FLAKED, /* Some sort of problem */
120 S_SINGLEREAD, /* In read() */
121 S_SPECREAD, /* Speculative read (for future read()) */
122 S_STREAMING /* Streaming data */
123};
124
125/*
126 * Tracking of streaming I/O buffers.
127 */
128struct cafe_sio_buffer {
129 struct list_head list;
130 struct v4l2_buffer v4lbuf;
131 char *buffer; /* Where it lives in kernel space */
132 int mapcount;
133 struct cafe_camera *cam;
134};
135
136/*
137 * A description of one of our devices.
138 * Locking: controlled by s_mutex. Certain fields, however, require
139 * the dev_lock spinlock; they are marked as such by comments.
140 * dev_lock is also required for access to device registers.
141 */
142struct cafe_camera
143{
Hans Verkuil21508b902009-03-18 13:13:09 -0300144 struct v4l2_device v4l2_dev;
Jonathan Corbetd905b382006-11-04 09:25:53 -0300145 enum cafe_state state;
146 unsigned long flags; /* Buffer status, mainly (dev_lock) */
147 int users; /* How many open FDs */
148 struct file *owner; /* Who has data access (v4l2) */
149
150 /*
151 * Subsystem structures.
152 */
153 struct pci_dev *pdev;
Hans Verkuil21508b902009-03-18 13:13:09 -0300154 struct video_device vdev;
Jonathan Corbetd905b382006-11-04 09:25:53 -0300155 struct i2c_adapter i2c_adapter;
Hans Verkuil8bcfd7a2009-03-18 13:16:44 -0300156 struct v4l2_subdev *sensor;
157 unsigned short sensor_addr;
Jonathan Corbetd905b382006-11-04 09:25:53 -0300158
159 unsigned char __iomem *regs;
160 struct list_head dev_list; /* link to other devices */
161
162 /* DMA buffers */
163 unsigned int nbufs; /* How many are alloc'd */
164 int next_buf; /* Next to consume (dev_lock) */
165 unsigned int dma_buf_size; /* allocated size */
166 void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */
167 dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
168 unsigned int specframes; /* Unconsumed spec frames (dev_lock) */
169 unsigned int sequence; /* Frame sequence number */
170 unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */
171
172 /* Streaming buffers */
173 unsigned int n_sbufs; /* How many we have */
174 struct cafe_sio_buffer *sb_bufs; /* The array of housekeeping structs */
175 struct list_head sb_avail; /* Available for data (we own) (dev_lock) */
176 struct list_head sb_full; /* With data (user space owns) (dev_lock) */
177 struct tasklet_struct s_tasklet;
178
179 /* Current operating parameters */
Hans Verkuil3434eb72007-04-27 12:31:08 -0300180 u32 sensor_type; /* Currently ov7670 only */
Jonathan Corbetd905b382006-11-04 09:25:53 -0300181 struct v4l2_pix_format pix_format;
182
183 /* Locks */
184 struct mutex s_mutex; /* Access to this structure */
185 spinlock_t dev_lock; /* Access to device */
186
187 /* Misc */
188 wait_queue_head_t smbus_wait; /* Waiting on i2c events */
189 wait_queue_head_t iowait; /* Waiting on frame data */
Jonathan Corbetd905b382006-11-04 09:25:53 -0300190};
191
192/*
193 * Status flags. Always manipulated with bit operations.
194 */
195#define CF_BUF0_VALID 0 /* Buffers valid - first three */
196#define CF_BUF1_VALID 1
197#define CF_BUF2_VALID 2
198#define CF_DMA_ACTIVE 3 /* A frame is incoming */
199#define CF_CONFIG_NEEDED 4 /* Must configure hardware */
200
Hans Verkuil8bcfd7a2009-03-18 13:16:44 -0300201#define sensor_call(cam, o, f, args...) \
202 v4l2_subdev_call(cam->sensor, o, f, ##args)
Jonathan Corbetd905b382006-11-04 09:25:53 -0300203
Hans Verkuil21508b902009-03-18 13:13:09 -0300204static inline struct cafe_camera *to_cam(struct v4l2_device *dev)
205{
206 return container_of(dev, struct cafe_camera, v4l2_dev);
207}
208
Jonathan Corbetd905b382006-11-04 09:25:53 -0300209
210/*
211 * Start over with DMA buffers - dev_lock needed.
212 */
213static void cafe_reset_buffers(struct cafe_camera *cam)
214{
215 int i;
216
217 cam->next_buf = -1;
218 for (i = 0; i < cam->nbufs; i++)
219 clear_bit(i, &cam->flags);
220 cam->specframes = 0;
221}
222
223static inline int cafe_needs_config(struct cafe_camera *cam)
224{
225 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
226}
227
228static void cafe_set_config_needed(struct cafe_camera *cam, int needed)
229{
230 if (needed)
231 set_bit(CF_CONFIG_NEEDED, &cam->flags);
232 else
233 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
234}
235
236
237
238
239/*
240 * Debugging and related.
241 */
242#define cam_err(cam, fmt, arg...) \
243 dev_err(&(cam)->pdev->dev, fmt, ##arg);
244#define cam_warn(cam, fmt, arg...) \
245 dev_warn(&(cam)->pdev->dev, fmt, ##arg);
246#define cam_dbg(cam, fmt, arg...) \
247 dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
248
249
250/* ---------------------------------------------------------------------*/
Jonathan Corbetd905b382006-11-04 09:25:53 -0300251
Jonathan Corbetd905b382006-11-04 09:25:53 -0300252/*
253 * Device register I/O
254 */
255static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg,
256 unsigned int val)
257{
258 iowrite32(val, cam->regs + reg);
259}
260
261static inline unsigned int cafe_reg_read(struct cafe_camera *cam,
262 unsigned int reg)
263{
264 return ioread32(cam->regs + reg);
265}
266
267
268static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg,
269 unsigned int val, unsigned int mask)
270{
271 unsigned int v = cafe_reg_read(cam, reg);
272
273 v = (v & ~mask) | (val & mask);
274 cafe_reg_write(cam, reg, v);
275}
276
277static inline void cafe_reg_clear_bit(struct cafe_camera *cam,
278 unsigned int reg, unsigned int val)
279{
280 cafe_reg_write_mask(cam, reg, 0, val);
281}
282
283static inline void cafe_reg_set_bit(struct cafe_camera *cam,
284 unsigned int reg, unsigned int val)
285{
286 cafe_reg_write_mask(cam, reg, val, val);
287}
288
289
290
291/* -------------------------------------------------------------------- */
292/*
293 * The I2C/SMBUS interface to the camera itself starts here. The
294 * controller handles SMBUS itself, presenting a relatively simple register
295 * interface; all we have to do is to tell it where to route the data.
296 */
297#define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
298
299static int cafe_smbus_write_done(struct cafe_camera *cam)
300{
301 unsigned long flags;
302 int c1;
303
304 /*
305 * We must delay after the interrupt, or the controller gets confused
306 * and never does give us good status. Fortunately, we don't do this
307 * often.
308 */
309 udelay(20);
310 spin_lock_irqsave(&cam->dev_lock, flags);
311 c1 = cafe_reg_read(cam, REG_TWSIC1);
312 spin_unlock_irqrestore(&cam->dev_lock, flags);
313 return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT;
314}
315
316static int cafe_smbus_write_data(struct cafe_camera *cam,
317 u16 addr, u8 command, u8 value)
318{
319 unsigned int rval;
320 unsigned long flags;
Jonathan Corbet6d774442007-08-17 01:02:33 -0300321 DEFINE_WAIT(the_wait);
Jonathan Corbetd905b382006-11-04 09:25:53 -0300322
323 spin_lock_irqsave(&cam->dev_lock, flags);
324 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
325 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
326 /*
327 * Marvell sez set clkdiv to all 1's for now.
328 */
329 rval |= TWSIC0_CLKDIV;
330 cafe_reg_write(cam, REG_TWSIC0, rval);
331 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
332 rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
333 cafe_reg_write(cam, REG_TWSIC1, rval);
334 spin_unlock_irqrestore(&cam->dev_lock, flags);
Jonathan Corbetd905b382006-11-04 09:25:53 -0300335
Jonathan Corbet6d774442007-08-17 01:02:33 -0300336 /*
337 * Time to wait for the write to complete. THIS IS A RACY
338 * WAY TO DO IT, but the sad fact is that reading the TWSIC1
339 * register too quickly after starting the operation sends
340 * the device into a place that may be kinder and better, but
341 * which is absolutely useless for controlling the sensor. In
342 * practice we have plenty of time to get into our sleep state
343 * before the interrupt hits, and the worst case is that we
344 * time out and then see that things completed, so this seems
345 * the best way for now.
346 */
347 do {
348 prepare_to_wait(&cam->smbus_wait, &the_wait,
349 TASK_UNINTERRUPTIBLE);
350 schedule_timeout(1); /* even 1 jiffy is too long */
351 finish_wait(&cam->smbus_wait, &the_wait);
352 } while (!cafe_smbus_write_done(cam));
353
354#ifdef IF_THE_CAFE_HARDWARE_WORKED_RIGHT
Jonathan Corbetd905b382006-11-04 09:25:53 -0300355 wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam),
356 CAFE_SMBUS_TIMEOUT);
Jonathan Corbet6d774442007-08-17 01:02:33 -0300357#endif
Jonathan Corbetd905b382006-11-04 09:25:53 -0300358 spin_lock_irqsave(&cam->dev_lock, flags);
359 rval = cafe_reg_read(cam, REG_TWSIC1);
360 spin_unlock_irqrestore(&cam->dev_lock, flags);
361
362 if (rval & TWSIC1_WSTAT) {
363 cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
364 command, value);
365 return -EIO;
366 }
367 if (rval & TWSIC1_ERROR) {
368 cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
369 command, value);
370 return -EIO;
371 }
372 return 0;
373}
374
375
376
377static int cafe_smbus_read_done(struct cafe_camera *cam)
378{
379 unsigned long flags;
380 int c1;
381
382 /*
383 * We must delay after the interrupt, or the controller gets confused
384 * and never does give us good status. Fortunately, we don't do this
385 * often.
386 */
387 udelay(20);
388 spin_lock_irqsave(&cam->dev_lock, flags);
389 c1 = cafe_reg_read(cam, REG_TWSIC1);
390 spin_unlock_irqrestore(&cam->dev_lock, flags);
391 return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
392}
393
394
395
396static int cafe_smbus_read_data(struct cafe_camera *cam,
397 u16 addr, u8 command, u8 *value)
398{
399 unsigned int rval;
400 unsigned long flags;
401
402 spin_lock_irqsave(&cam->dev_lock, flags);
403 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
404 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
405 /*
406 * Marvel sez set clkdiv to all 1's for now.
407 */
408 rval |= TWSIC0_CLKDIV;
409 cafe_reg_write(cam, REG_TWSIC0, rval);
410 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
411 rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
412 cafe_reg_write(cam, REG_TWSIC1, rval);
413 spin_unlock_irqrestore(&cam->dev_lock, flags);
414
415 wait_event_timeout(cam->smbus_wait,
416 cafe_smbus_read_done(cam), CAFE_SMBUS_TIMEOUT);
417 spin_lock_irqsave(&cam->dev_lock, flags);
418 rval = cafe_reg_read(cam, REG_TWSIC1);
419 spin_unlock_irqrestore(&cam->dev_lock, flags);
420
421 if (rval & TWSIC1_ERROR) {
422 cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
423 return -EIO;
424 }
425 if (! (rval & TWSIC1_RVALID)) {
426 cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
427 command);
428 return -EIO;
429 }
430 *value = rval & 0xff;
431 return 0;
432}
433
434/*
435 * Perform a transfer over SMBUS. This thing is called under
436 * the i2c bus lock, so we shouldn't race with ourselves...
437 */
438static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
439 unsigned short flags, char rw, u8 command,
440 int size, union i2c_smbus_data *data)
441{
Hans Verkuil21508b902009-03-18 13:13:09 -0300442 struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter);
443 struct cafe_camera *cam = to_cam(v4l2_dev);
Jonathan Corbetd905b382006-11-04 09:25:53 -0300444 int ret = -EINVAL;
445
446 /*
Jonathan Corbetd905b382006-11-04 09:25:53 -0300447 * This interface would appear to only do byte data ops. OK
448 * it can do word too, but the cam chip has no use for that.
449 */
450 if (size != I2C_SMBUS_BYTE_DATA) {
451 cam_err(cam, "funky xfer size %d\n", size);
452 return -EINVAL;
453 }
454
455 if (rw == I2C_SMBUS_WRITE)
456 ret = cafe_smbus_write_data(cam, addr, command, data->byte);
457 else if (rw == I2C_SMBUS_READ)
458 ret = cafe_smbus_read_data(cam, addr, command, &data->byte);
459 return ret;
460}
461
462
463static void cafe_smbus_enable_irq(struct cafe_camera *cam)
464{
465 unsigned long flags;
466
467 spin_lock_irqsave(&cam->dev_lock, flags);
468 cafe_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS);
469 spin_unlock_irqrestore(&cam->dev_lock, flags);
470}
471
472static u32 cafe_smbus_func(struct i2c_adapter *adapter)
473{
474 return I2C_FUNC_SMBUS_READ_BYTE_DATA |
475 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
476}
477
478static struct i2c_algorithm cafe_smbus_algo = {
479 .smbus_xfer = cafe_smbus_xfer,
480 .functionality = cafe_smbus_func
481};
482
483/* Somebody is on the bus */
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300484static void cafe_ctlr_stop_dma(struct cafe_camera *cam);
485static void cafe_ctlr_power_down(struct cafe_camera *cam);
Jonathan Corbetd905b382006-11-04 09:25:53 -0300486
Jonathan Corbetd905b382006-11-04 09:25:53 -0300487static int cafe_smbus_setup(struct cafe_camera *cam)
488{
489 struct i2c_adapter *adap = &cam->i2c_adapter;
490 int ret;
491
492 cafe_smbus_enable_irq(cam);
493 adap->id = I2C_HW_SMBUS_CAFE;
Jonathan Corbetd905b382006-11-04 09:25:53 -0300494 adap->owner = THIS_MODULE;
Jonathan Corbetd905b382006-11-04 09:25:53 -0300495 adap->algo = &cafe_smbus_algo;
496 strcpy(adap->name, "cafe_ccic");
Jean Delvare12a917f2007-02-13 22:09:03 +0100497 adap->dev.parent = &cam->pdev->dev;
Hans Verkuil21508b902009-03-18 13:13:09 -0300498 i2c_set_adapdata(adap, &cam->v4l2_dev);
Jonathan Corbetd905b382006-11-04 09:25:53 -0300499 ret = i2c_add_adapter(adap);
500 if (ret)
501 printk(KERN_ERR "Unable to register cafe i2c adapter\n");
502 return ret;
503}
504
505static void cafe_smbus_shutdown(struct cafe_camera *cam)
506{
507 i2c_del_adapter(&cam->i2c_adapter);
508}
509
510
511/* ------------------------------------------------------------------- */
512/*
513 * Deal with the controller.
514 */
515
516/*
517 * Do everything we think we need to have the interface operating
518 * according to the desired format.
519 */
520static void cafe_ctlr_dma(struct cafe_camera *cam)
521{
522 /*
523 * Store the first two Y buffers (we aren't supporting
524 * planar formats for now, so no UV bufs). Then either
525 * set the third if it exists, or tell the controller
526 * to just use two.
527 */
528 cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
529 cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
530 if (cam->nbufs > 2) {
531 cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
532 cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
533 }
534 else
535 cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
536 cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
537}
538
539static void cafe_ctlr_image(struct cafe_camera *cam)
540{
541 int imgsz;
542 struct v4l2_pix_format *fmt = &cam->pix_format;
543
544 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
545 (fmt->bytesperline & IMGSZ_H_MASK);
546 cafe_reg_write(cam, REG_IMGSIZE, imgsz);
547 cafe_reg_write(cam, REG_IMGOFFSET, 0);
548 /* YPITCH just drops the last two bits */
549 cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
550 IMGP_YP_MASK);
551 /*
552 * Tell the controller about the image format we are using.
553 */
554 switch (cam->pix_format.pixelformat) {
555 case V4L2_PIX_FMT_YUYV:
556 cafe_reg_write_mask(cam, REG_CTRL0,
557 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
558 C0_DF_MASK);
559 break;
560
Jonathan Corbetd905b382006-11-04 09:25:53 -0300561 case V4L2_PIX_FMT_RGB444:
562 cafe_reg_write_mask(cam, REG_CTRL0,
563 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
564 C0_DF_MASK);
565 /* Alpha value? */
566 break;
567
568 case V4L2_PIX_FMT_RGB565:
569 cafe_reg_write_mask(cam, REG_CTRL0,
570 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
571 C0_DF_MASK);
572 break;
573
574 default:
575 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
576 break;
577 }
578 /*
579 * Make sure it knows we want to use hsync/vsync.
580 */
581 cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
582 C0_SIFM_MASK);
583}
584
585
586/*
587 * Configure the controller for operation; caller holds the
588 * device mutex.
589 */
590static int cafe_ctlr_configure(struct cafe_camera *cam)
591{
592 unsigned long flags;
593
594 spin_lock_irqsave(&cam->dev_lock, flags);
595 cafe_ctlr_dma(cam);
596 cafe_ctlr_image(cam);
597 cafe_set_config_needed(cam, 0);
598 spin_unlock_irqrestore(&cam->dev_lock, flags);
599 return 0;
600}
601
602static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
603{
604 /*
605 * Clear any pending interrupts, since we do not
606 * expect to have I/O active prior to enabling.
607 */
608 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
609 cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
610}
611
612static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
613{
614 cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
615}
616
617/*
618 * Make the controller start grabbing images. Everything must
619 * be set up before doing this.
620 */
621static void cafe_ctlr_start(struct cafe_camera *cam)
622{
623 /* set_bit performs a read, so no other barrier should be
624 needed here */
625 cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
626}
627
628static void cafe_ctlr_stop(struct cafe_camera *cam)
629{
630 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
631}
632
633static void cafe_ctlr_init(struct cafe_camera *cam)
634{
635 unsigned long flags;
636
637 spin_lock_irqsave(&cam->dev_lock, flags);
638 /*
639 * Added magic to bring up the hardware on the B-Test board
640 */
641 cafe_reg_write(cam, 0x3038, 0x8);
642 cafe_reg_write(cam, 0x315c, 0x80008);
643 /*
644 * Go through the dance needed to wake the device up.
645 * Note that these registers are global and shared
646 * with the NAND and SD devices. Interaction between the
647 * three still needs to be examined.
648 */
649 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
650 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
651 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
Jonathan Corbet5b50ed72007-04-27 12:32:28 -0300652 /*
653 * Here we must wait a bit for the controller to come around.
654 */
655 spin_unlock_irqrestore(&cam->dev_lock, flags);
Marcelo Tosatti70cd6852007-08-17 01:03:22 -0300656 msleep(5);
Jonathan Corbet5b50ed72007-04-27 12:32:28 -0300657 spin_lock_irqsave(&cam->dev_lock, flags);
658
Jonathan Corbetd905b382006-11-04 09:25:53 -0300659 cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
660 cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
661 /*
662 * Make sure it's not powered down.
663 */
664 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
665 /*
666 * Turn off the enable bit. It sure should be off anyway,
667 * but it's good to be sure.
668 */
669 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
670 /*
671 * Mask all interrupts.
672 */
673 cafe_reg_write(cam, REG_IRQMASK, 0);
674 /*
675 * Clock the sensor appropriately. Controller clock should
676 * be 48MHz, sensor "typical" value is half that.
677 */
678 cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
679 spin_unlock_irqrestore(&cam->dev_lock, flags);
680}
681
682
683/*
684 * Stop the controller, and don't return until we're really sure that no
685 * further DMA is going on.
686 */
687static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
688{
689 unsigned long flags;
690
691 /*
692 * Theory: stop the camera controller (whether it is operating
693 * or not). Delay briefly just in case we race with the SOF
694 * interrupt, then wait until no DMA is active.
695 */
696 spin_lock_irqsave(&cam->dev_lock, flags);
697 cafe_ctlr_stop(cam);
698 spin_unlock_irqrestore(&cam->dev_lock, flags);
699 mdelay(1);
700 wait_event_timeout(cam->iowait,
701 !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
702 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
703 cam_err(cam, "Timeout waiting for DMA to end\n");
704 /* This would be bad news - what now? */
705 spin_lock_irqsave(&cam->dev_lock, flags);
706 cam->state = S_IDLE;
707 cafe_ctlr_irq_disable(cam);
708 spin_unlock_irqrestore(&cam->dev_lock, flags);
709}
710
711/*
712 * Power up and down.
713 */
714static void cafe_ctlr_power_up(struct cafe_camera *cam)
715{
716 unsigned long flags;
717
718 spin_lock_irqsave(&cam->dev_lock, flags);
719 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
720 /*
Jonathan Corbet7acf90c2007-05-22 00:37:58 -0300721 * Part one of the sensor dance: turn the global
722 * GPIO signal on.
723 */
724 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
725 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
726 /*
Jonathan Corbetd905b382006-11-04 09:25:53 -0300727 * Put the sensor into operational mode (assumes OLPC-style
728 * wiring). Control 0 is reset - set to 1 to operate.
729 * Control 1 is power down, set to 0 to operate.
730 */
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300731 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
Hans Verkuil21508b902009-03-18 13:13:09 -0300732/* mdelay(1); */ /* Marvell says 1ms will do it */
Jonathan Corbetd905b382006-11-04 09:25:53 -0300733 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
Hans Verkuil21508b902009-03-18 13:13:09 -0300734/* mdelay(1); */ /* Enough? */
Jonathan Corbetd905b382006-11-04 09:25:53 -0300735 spin_unlock_irqrestore(&cam->dev_lock, flags);
Jonathan Corbet7acf90c2007-05-22 00:37:58 -0300736 msleep(5); /* Just to be sure */
Jonathan Corbetd905b382006-11-04 09:25:53 -0300737}
738
739static void cafe_ctlr_power_down(struct cafe_camera *cam)
740{
741 unsigned long flags;
742
743 spin_lock_irqsave(&cam->dev_lock, flags);
744 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
Jonathan Corbet7acf90c2007-05-22 00:37:58 -0300745 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
746 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT);
Jonathan Corbetd905b382006-11-04 09:25:53 -0300747 cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
748 spin_unlock_irqrestore(&cam->dev_lock, flags);
749}
750
751/* -------------------------------------------------------------------- */
752/*
753 * Communications with the sensor.
754 */
755
Jonathan Corbetd905b382006-11-04 09:25:53 -0300756static int __cafe_cam_reset(struct cafe_camera *cam)
757{
Hans Verkuil8bcfd7a2009-03-18 13:16:44 -0300758 return sensor_call(cam, core, reset, 0);
Jonathan Corbetd905b382006-11-04 09:25:53 -0300759}
760
761/*
762 * We have found the sensor on the i2c. Let's try to have a
763 * conversation.
764 */
765static int cafe_cam_init(struct cafe_camera *cam)
766{
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300767 struct v4l2_dbg_chip_ident chip;
Jonathan Corbetd905b382006-11-04 09:25:53 -0300768 int ret;
769
770 mutex_lock(&cam->s_mutex);
771 if (cam->state != S_NOTREADY)
772 cam_warn(cam, "Cam init with device in funky state %d",
773 cam->state);
774 ret = __cafe_cam_reset(cam);
775 if (ret)
776 goto out;
Mauro Carvalho Chehab90c69f22009-05-05 08:08:38 -0300777 chip.ident = V4L2_IDENT_NONE;
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300778 chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
Hans Verkuil8bcfd7a2009-03-18 13:16:44 -0300779 chip.match.addr = cam->sensor_addr;
780 ret = sensor_call(cam, core, g_chip_ident, &chip);
Jonathan Corbetd905b382006-11-04 09:25:53 -0300781 if (ret)
782 goto out;
Hans Verkuil3434eb72007-04-27 12:31:08 -0300783 cam->sensor_type = chip.ident;
Jonathan Corbetd905b382006-11-04 09:25:53 -0300784 if (cam->sensor_type != V4L2_IDENT_OV7670) {
Hans Verkuil8bcfd7a2009-03-18 13:16:44 -0300785 cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
Jonathan Corbetd905b382006-11-04 09:25:53 -0300786 ret = -EINVAL;
787 goto out;
788 }
789/* Get/set parameters? */
790 ret = 0;
791 cam->state = S_IDLE;
792 out:
Jonathan Corbet7acf90c2007-05-22 00:37:58 -0300793 cafe_ctlr_power_down(cam);
Jonathan Corbetd905b382006-11-04 09:25:53 -0300794 mutex_unlock(&cam->s_mutex);
795 return ret;
796}
797
798/*
799 * Configure the sensor to match the parameters we have. Caller should
800 * hold s_mutex
801 */
802static int cafe_cam_set_flip(struct cafe_camera *cam)
803{
804 struct v4l2_control ctrl;
805
806 memset(&ctrl, 0, sizeof(ctrl));
807 ctrl.id = V4L2_CID_VFLIP;
808 ctrl.value = flip;
Hans Verkuil8bcfd7a2009-03-18 13:16:44 -0300809 return sensor_call(cam, core, s_ctrl, &ctrl);
Jonathan Corbetd905b382006-11-04 09:25:53 -0300810}
811
812
813static int cafe_cam_configure(struct cafe_camera *cam)
814{
815 struct v4l2_format fmt;
Hans Verkuil8bcfd7a2009-03-18 13:16:44 -0300816 int ret;
Jonathan Corbetd905b382006-11-04 09:25:53 -0300817
818 if (cam->state != S_IDLE)
819 return -EINVAL;
820 fmt.fmt.pix = cam->pix_format;
Hans Verkuil8bcfd7a2009-03-18 13:16:44 -0300821 ret = sensor_call(cam, core, init, 0);
Jonathan Corbetd905b382006-11-04 09:25:53 -0300822 if (ret == 0)
Hans Verkuil8bcfd7a2009-03-18 13:16:44 -0300823 ret = sensor_call(cam, video, s_fmt, &fmt);
Jonathan Corbetd905b382006-11-04 09:25:53 -0300824 /*
825 * OV7670 does weird things if flip is set *before* format...
826 */
827 ret += cafe_cam_set_flip(cam);
828 return ret;
829}
830
831/* -------------------------------------------------------------------- */
832/*
833 * DMA buffer management. These functions need s_mutex held.
834 */
835
836/* FIXME: this is inefficient as hell, since dma_alloc_coherent just
837 * does a get_free_pages() call, and we waste a good chunk of an orderN
838 * allocation. Should try to allocate the whole set in one chunk.
839 */
840static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
841{
842 int i;
843
844 cafe_set_config_needed(cam, 1);
845 if (loadtime)
846 cam->dma_buf_size = dma_buf_size;
Jonathan Corbeta66d2332006-12-01 15:37:49 -0300847 else
Jonathan Corbetd905b382006-11-04 09:25:53 -0300848 cam->dma_buf_size = cam->pix_format.sizeimage;
Jonathan Corbetd905b382006-11-04 09:25:53 -0300849 if (n_dma_bufs > 3)
850 n_dma_bufs = 3;
851
852 cam->nbufs = 0;
853 for (i = 0; i < n_dma_bufs; i++) {
854 cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
855 cam->dma_buf_size, cam->dma_handles + i,
856 GFP_KERNEL);
857 if (cam->dma_bufs[i] == NULL) {
858 cam_warn(cam, "Failed to allocate DMA buffer\n");
859 break;
860 }
861 /* For debug, remove eventually */
862 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
863 (cam->nbufs)++;
864 }
865
866 switch (cam->nbufs) {
867 case 1:
868 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
869 cam->dma_bufs[0], cam->dma_handles[0]);
870 cam->nbufs = 0;
871 case 0:
872 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
873 return -ENOMEM;
874
875 case 2:
876 if (n_dma_bufs > 2)
877 cam_warn(cam, "Will limp along with only 2 buffers\n");
878 break;
879 }
880 return 0;
881}
882
883static void cafe_free_dma_bufs(struct cafe_camera *cam)
884{
885 int i;
886
887 for (i = 0; i < cam->nbufs; i++) {
888 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
889 cam->dma_bufs[i], cam->dma_handles[i]);
890 cam->dma_bufs[i] = NULL;
891 }
892 cam->nbufs = 0;
893}
894
895
896
897
898
899/* ----------------------------------------------------------------------- */
900/*
901 * Here starts the V4L2 interface code.
902 */
903
904/*
905 * Read an image from the device.
906 */
907static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
908 char __user *buffer, size_t len, loff_t *pos)
909{
910 int bufno;
911 unsigned long flags;
912
913 spin_lock_irqsave(&cam->dev_lock, flags);
914 if (cam->next_buf < 0) {
915 cam_err(cam, "deliver_buffer: No next buffer\n");
916 spin_unlock_irqrestore(&cam->dev_lock, flags);
917 return -EIO;
918 }
919 bufno = cam->next_buf;
920 clear_bit(bufno, &cam->flags);
921 if (++(cam->next_buf) >= cam->nbufs)
922 cam->next_buf = 0;
923 if (! test_bit(cam->next_buf, &cam->flags))
924 cam->next_buf = -1;
925 cam->specframes = 0;
926 spin_unlock_irqrestore(&cam->dev_lock, flags);
927
928 if (len > cam->pix_format.sizeimage)
929 len = cam->pix_format.sizeimage;
930 if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
931 return -EFAULT;
932 (*pos) += len;
933 return len;
934}
935
936/*
937 * Get everything ready, and start grabbing frames.
938 */
939static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
940{
941 int ret;
942 unsigned long flags;
943
944 /*
945 * Configuration. If we still don't have DMA buffers,
946 * make one last, desperate attempt.
947 */
948 if (cam->nbufs == 0)
949 if (cafe_alloc_dma_bufs(cam, 0))
950 return -ENOMEM;
951
952 if (cafe_needs_config(cam)) {
953 cafe_cam_configure(cam);
954 ret = cafe_ctlr_configure(cam);
955 if (ret)
956 return ret;
957 }
958
959 /*
960 * Turn it loose.
961 */
962 spin_lock_irqsave(&cam->dev_lock, flags);
963 cafe_reset_buffers(cam);
964 cafe_ctlr_irq_enable(cam);
965 cam->state = state;
966 cafe_ctlr_start(cam);
967 spin_unlock_irqrestore(&cam->dev_lock, flags);
968 return 0;
969}
970
971
972static ssize_t cafe_v4l_read(struct file *filp,
973 char __user *buffer, size_t len, loff_t *pos)
974{
975 struct cafe_camera *cam = filp->private_data;
Jean Delvareb9109b752007-02-13 19:31:45 -0300976 int ret = 0;
Jonathan Corbetd905b382006-11-04 09:25:53 -0300977
978 /*
979 * Perhaps we're in speculative read mode and already
980 * have data?
981 */
982 mutex_lock(&cam->s_mutex);
983 if (cam->state == S_SPECREAD) {
984 if (cam->next_buf >= 0) {
985 ret = cafe_deliver_buffer(cam, buffer, len, pos);
986 if (ret != 0)
987 goto out_unlock;
988 }
989 } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
990 ret = -EIO;
991 goto out_unlock;
992 } else if (cam->state != S_IDLE) {
993 ret = -EBUSY;
994 goto out_unlock;
995 }
996
997 /*
998 * v4l2: multiple processes can open the device, but only
999 * one gets to grab data from it.
1000 */
1001 if (cam->owner && cam->owner != filp) {
1002 ret = -EBUSY;
1003 goto out_unlock;
1004 }
1005 cam->owner = filp;
1006
1007 /*
1008 * Do setup if need be.
1009 */
1010 if (cam->state != S_SPECREAD) {
1011 ret = cafe_read_setup(cam, S_SINGLEREAD);
1012 if (ret)
1013 goto out_unlock;
1014 }
1015 /*
1016 * Wait for something to happen. This should probably
1017 * be interruptible (FIXME).
1018 */
1019 wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
1020 if (cam->next_buf < 0) {
1021 cam_err(cam, "read() operation timed out\n");
1022 cafe_ctlr_stop_dma(cam);
1023 ret = -EIO;
1024 goto out_unlock;
1025 }
1026 /*
1027 * Give them their data and we should be done.
1028 */
1029 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1030
1031 out_unlock:
1032 mutex_unlock(&cam->s_mutex);
1033 return ret;
1034}
1035
1036
1037
1038
1039
1040
1041
1042
1043/*
1044 * Streaming I/O support.
1045 */
1046
1047
1048
1049static int cafe_vidioc_streamon(struct file *filp, void *priv,
1050 enum v4l2_buf_type type)
1051{
1052 struct cafe_camera *cam = filp->private_data;
1053 int ret = -EINVAL;
1054
1055 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1056 goto out;
1057 mutex_lock(&cam->s_mutex);
1058 if (cam->state != S_IDLE || cam->n_sbufs == 0)
1059 goto out_unlock;
1060
1061 cam->sequence = 0;
1062 ret = cafe_read_setup(cam, S_STREAMING);
1063
1064 out_unlock:
1065 mutex_unlock(&cam->s_mutex);
1066 out:
1067 return ret;
1068}
1069
1070
1071static int cafe_vidioc_streamoff(struct file *filp, void *priv,
1072 enum v4l2_buf_type type)
1073{
1074 struct cafe_camera *cam = filp->private_data;
1075 int ret = -EINVAL;
1076
1077 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1078 goto out;
1079 mutex_lock(&cam->s_mutex);
1080 if (cam->state != S_STREAMING)
1081 goto out_unlock;
1082
1083 cafe_ctlr_stop_dma(cam);
1084 ret = 0;
1085
1086 out_unlock:
1087 mutex_unlock(&cam->s_mutex);
1088 out:
1089 return ret;
1090}
1091
1092
1093
1094static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1095{
1096 struct cafe_sio_buffer *buf = cam->sb_bufs + index;
1097
1098 INIT_LIST_HEAD(&buf->list);
1099 buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
1100 buf->buffer = vmalloc_user(buf->v4lbuf.length);
1101 if (buf->buffer == NULL)
1102 return -ENOMEM;
1103 buf->mapcount = 0;
1104 buf->cam = cam;
1105
1106 buf->v4lbuf.index = index;
1107 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1108 buf->v4lbuf.field = V4L2_FIELD_NONE;
1109 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
1110 /*
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001111 * Offset: must be 32-bit even on a 64-bit system. videobuf-dma-sg
Jonathan Corbetd905b382006-11-04 09:25:53 -03001112 * just uses the length times the index, but the spec warns
1113 * against doing just that - vma merging problems. So we
1114 * leave a gap between each pair of buffers.
1115 */
1116 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1117 return 0;
1118}
1119
1120static int cafe_free_sio_buffers(struct cafe_camera *cam)
1121{
1122 int i;
1123
1124 /*
1125 * If any buffers are mapped, we cannot free them at all.
1126 */
1127 for (i = 0; i < cam->n_sbufs; i++)
1128 if (cam->sb_bufs[i].mapcount > 0)
1129 return -EBUSY;
1130 /*
1131 * OK, let's do it.
1132 */
1133 for (i = 0; i < cam->n_sbufs; i++)
1134 vfree(cam->sb_bufs[i].buffer);
1135 cam->n_sbufs = 0;
1136 kfree(cam->sb_bufs);
1137 cam->sb_bufs = NULL;
1138 INIT_LIST_HEAD(&cam->sb_avail);
1139 INIT_LIST_HEAD(&cam->sb_full);
1140 return 0;
1141}
1142
1143
1144
1145static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1146 struct v4l2_requestbuffers *req)
1147{
1148 struct cafe_camera *cam = filp->private_data;
Jonathan Corbet3198cf62007-02-06 21:52:36 -03001149 int ret = 0; /* Silence warning */
Jonathan Corbetd905b382006-11-04 09:25:53 -03001150
1151 /*
1152 * Make sure it's something we can do. User pointers could be
1153 * implemented without great pain, but that's not been done yet.
1154 */
Jonathan Corbetd905b382006-11-04 09:25:53 -03001155 if (req->memory != V4L2_MEMORY_MMAP)
1156 return -EINVAL;
1157 /*
1158 * If they ask for zero buffers, they really want us to stop streaming
1159 * (if it's happening) and free everything. Should we check owner?
1160 */
1161 mutex_lock(&cam->s_mutex);
1162 if (req->count == 0) {
1163 if (cam->state == S_STREAMING)
1164 cafe_ctlr_stop_dma(cam);
1165 ret = cafe_free_sio_buffers (cam);
1166 goto out;
1167 }
1168 /*
1169 * Device needs to be idle and working. We *could* try to do the
1170 * right thing in S_SPECREAD by shutting things down, but it
1171 * probably doesn't matter.
1172 */
1173 if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1174 ret = -EBUSY;
1175 goto out;
1176 }
1177 cam->owner = filp;
1178
1179 if (req->count < min_buffers)
1180 req->count = min_buffers;
1181 else if (req->count > max_buffers)
1182 req->count = max_buffers;
1183 if (cam->n_sbufs > 0) {
1184 ret = cafe_free_sio_buffers(cam);
1185 if (ret)
1186 goto out;
1187 }
1188
1189 cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1190 GFP_KERNEL);
1191 if (cam->sb_bufs == NULL) {
1192 ret = -ENOMEM;
1193 goto out;
1194 }
1195 for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1196 ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1197 if (ret)
1198 break;
1199 }
1200
1201 if (cam->n_sbufs == 0) /* no luck at all - ret already set */
1202 kfree(cam->sb_bufs);
Jonathan Corbetd905b382006-11-04 09:25:53 -03001203 req->count = cam->n_sbufs; /* In case of partial success */
1204
1205 out:
1206 mutex_unlock(&cam->s_mutex);
1207 return ret;
1208}
1209
1210
1211static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1212 struct v4l2_buffer *buf)
1213{
1214 struct cafe_camera *cam = filp->private_data;
1215 int ret = -EINVAL;
1216
1217 mutex_lock(&cam->s_mutex);
Trent Piephoe33ee312009-03-28 22:25:36 -03001218 if (buf->index >= cam->n_sbufs)
Jonathan Corbetd905b382006-11-04 09:25:53 -03001219 goto out;
1220 *buf = cam->sb_bufs[buf->index].v4lbuf;
1221 ret = 0;
1222 out:
1223 mutex_unlock(&cam->s_mutex);
1224 return ret;
1225}
1226
1227static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1228 struct v4l2_buffer *buf)
1229{
1230 struct cafe_camera *cam = filp->private_data;
1231 struct cafe_sio_buffer *sbuf;
1232 int ret = -EINVAL;
1233 unsigned long flags;
1234
1235 mutex_lock(&cam->s_mutex);
Trent Piephoe33ee312009-03-28 22:25:36 -03001236 if (buf->index >= cam->n_sbufs)
Jonathan Corbetd905b382006-11-04 09:25:53 -03001237 goto out;
1238 sbuf = cam->sb_bufs + buf->index;
1239 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1240 ret = 0; /* Already queued?? */
1241 goto out;
1242 }
1243 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1244 /* Spec doesn't say anything, seems appropriate tho */
1245 ret = -EBUSY;
1246 goto out;
1247 }
1248 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1249 spin_lock_irqsave(&cam->dev_lock, flags);
1250 list_add(&sbuf->list, &cam->sb_avail);
1251 spin_unlock_irqrestore(&cam->dev_lock, flags);
1252 ret = 0;
1253 out:
1254 mutex_unlock(&cam->s_mutex);
1255 return ret;
1256}
1257
1258static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1259 struct v4l2_buffer *buf)
1260{
1261 struct cafe_camera *cam = filp->private_data;
1262 struct cafe_sio_buffer *sbuf;
1263 int ret = -EINVAL;
1264 unsigned long flags;
1265
1266 mutex_lock(&cam->s_mutex);
Jonathan Corbetd905b382006-11-04 09:25:53 -03001267 if (cam->state != S_STREAMING)
1268 goto out_unlock;
1269 if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1270 ret = -EAGAIN;
1271 goto out_unlock;
1272 }
1273
1274 while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1275 mutex_unlock(&cam->s_mutex);
1276 if (wait_event_interruptible(cam->iowait,
1277 !list_empty(&cam->sb_full))) {
1278 ret = -ERESTARTSYS;
1279 goto out;
1280 }
1281 mutex_lock(&cam->s_mutex);
1282 }
1283
1284 if (cam->state != S_STREAMING)
1285 ret = -EINTR;
1286 else {
1287 spin_lock_irqsave(&cam->dev_lock, flags);
1288 /* Should probably recheck !list_empty() here */
1289 sbuf = list_entry(cam->sb_full.next,
1290 struct cafe_sio_buffer, list);
1291 list_del_init(&sbuf->list);
1292 spin_unlock_irqrestore(&cam->dev_lock, flags);
1293 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1294 *buf = sbuf->v4lbuf;
1295 ret = 0;
1296 }
1297
1298 out_unlock:
1299 mutex_unlock(&cam->s_mutex);
1300 out:
1301 return ret;
1302}
1303
1304
1305
1306static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1307{
1308 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1309 /*
1310 * Locking: done under mmap_sem, so we don't need to
1311 * go back to the camera lock here.
1312 */
1313 sbuf->mapcount++;
1314}
1315
1316
1317static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1318{
1319 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1320
1321 mutex_lock(&sbuf->cam->s_mutex);
1322 sbuf->mapcount--;
1323 /* Docs say we should stop I/O too... */
1324 if (sbuf->mapcount == 0)
1325 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1326 mutex_unlock(&sbuf->cam->s_mutex);
1327}
1328
1329static struct vm_operations_struct cafe_v4l_vm_ops = {
1330 .open = cafe_v4l_vm_open,
1331 .close = cafe_v4l_vm_close
1332};
1333
1334
1335static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1336{
1337 struct cafe_camera *cam = filp->private_data;
1338 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1339 int ret = -EINVAL;
1340 int i;
1341 struct cafe_sio_buffer *sbuf = NULL;
1342
1343 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1344 return -EINVAL;
1345 /*
1346 * Find the buffer they are looking for.
1347 */
1348 mutex_lock(&cam->s_mutex);
1349 for (i = 0; i < cam->n_sbufs; i++)
1350 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1351 sbuf = cam->sb_bufs + i;
1352 break;
1353 }
1354 if (sbuf == NULL)
1355 goto out;
1356
1357 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1358 if (ret)
1359 goto out;
1360 vma->vm_flags |= VM_DONTEXPAND;
1361 vma->vm_private_data = sbuf;
1362 vma->vm_ops = &cafe_v4l_vm_ops;
1363 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1364 cafe_v4l_vm_open(vma);
1365 ret = 0;
1366 out:
1367 mutex_unlock(&cam->s_mutex);
1368 return ret;
1369}
1370
1371
1372
Hans Verkuilbec43662008-12-30 06:58:20 -03001373static int cafe_v4l_open(struct file *filp)
Jonathan Corbetd905b382006-11-04 09:25:53 -03001374{
Hans Verkuil21508b902009-03-18 13:13:09 -03001375 struct cafe_camera *cam = video_drvdata(filp);
Jonathan Corbetd905b382006-11-04 09:25:53 -03001376
Jonathan Corbetd905b382006-11-04 09:25:53 -03001377 filp->private_data = cam;
1378
1379 mutex_lock(&cam->s_mutex);
1380 if (cam->users == 0) {
1381 cafe_ctlr_power_up(cam);
1382 __cafe_cam_reset(cam);
1383 cafe_set_config_needed(cam, 1);
1384 /* FIXME make sure this is complete */
1385 }
1386 (cam->users)++;
1387 mutex_unlock(&cam->s_mutex);
1388 return 0;
1389}
1390
1391
Hans Verkuilbec43662008-12-30 06:58:20 -03001392static int cafe_v4l_release(struct file *filp)
Jonathan Corbetd905b382006-11-04 09:25:53 -03001393{
1394 struct cafe_camera *cam = filp->private_data;
1395
1396 mutex_lock(&cam->s_mutex);
1397 (cam->users)--;
1398 if (filp == cam->owner) {
1399 cafe_ctlr_stop_dma(cam);
1400 cafe_free_sio_buffers(cam);
1401 cam->owner = NULL;
1402 }
Jonathan Corbetf9a76152006-11-19 19:04:55 -03001403 if (cam->users == 0) {
Jonathan Corbetd905b382006-11-04 09:25:53 -03001404 cafe_ctlr_power_down(cam);
Andres Salomon23869e22007-09-19 02:44:18 -03001405 if (alloc_bufs_at_read)
Jonathan Corbetf9a76152006-11-19 19:04:55 -03001406 cafe_free_dma_bufs(cam);
1407 }
Jonathan Corbetd905b382006-11-04 09:25:53 -03001408 mutex_unlock(&cam->s_mutex);
1409 return 0;
1410}
1411
1412
1413
1414static unsigned int cafe_v4l_poll(struct file *filp,
1415 struct poll_table_struct *pt)
1416{
1417 struct cafe_camera *cam = filp->private_data;
1418
1419 poll_wait(filp, &cam->iowait, pt);
1420 if (cam->next_buf >= 0)
1421 return POLLIN | POLLRDNORM;
1422 return 0;
1423}
1424
1425
1426
1427static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1428 struct v4l2_queryctrl *qc)
1429{
Hans Verkuil21508b902009-03-18 13:13:09 -03001430 struct cafe_camera *cam = priv;
Jonathan Corbetd905b382006-11-04 09:25:53 -03001431 int ret;
1432
1433 mutex_lock(&cam->s_mutex);
Hans Verkuil8bcfd7a2009-03-18 13:16:44 -03001434 ret = sensor_call(cam, core, queryctrl, qc);
Jonathan Corbetd905b382006-11-04 09:25:53 -03001435 mutex_unlock(&cam->s_mutex);
1436 return ret;
1437}
1438
1439
1440static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1441 struct v4l2_control *ctrl)
1442{
Hans Verkuil21508b902009-03-18 13:13:09 -03001443 struct cafe_camera *cam = priv;
Jonathan Corbetd905b382006-11-04 09:25:53 -03001444 int ret;
1445
1446 mutex_lock(&cam->s_mutex);
Hans Verkuil8bcfd7a2009-03-18 13:16:44 -03001447 ret = sensor_call(cam, core, g_ctrl, ctrl);
Jonathan Corbetd905b382006-11-04 09:25:53 -03001448 mutex_unlock(&cam->s_mutex);
1449 return ret;
1450}
1451
1452
1453static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1454 struct v4l2_control *ctrl)
1455{
Hans Verkuil21508b902009-03-18 13:13:09 -03001456 struct cafe_camera *cam = priv;
Jonathan Corbetd905b382006-11-04 09:25:53 -03001457 int ret;
1458
1459 mutex_lock(&cam->s_mutex);
Hans Verkuil8bcfd7a2009-03-18 13:16:44 -03001460 ret = sensor_call(cam, core, s_ctrl, ctrl);
Jonathan Corbetd905b382006-11-04 09:25:53 -03001461 mutex_unlock(&cam->s_mutex);
1462 return ret;
1463}
1464
1465
1466
1467
1468
1469static int cafe_vidioc_querycap(struct file *file, void *priv,
1470 struct v4l2_capability *cap)
1471{
1472 strcpy(cap->driver, "cafe_ccic");
1473 strcpy(cap->card, "cafe_ccic");
1474 cap->version = CAFE_VERSION;
1475 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1476 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1477 return 0;
1478}
1479
1480
1481/*
1482 * The default format we use until somebody says otherwise.
1483 */
1484static struct v4l2_pix_format cafe_def_pix_format = {
1485 .width = VGA_WIDTH,
1486 .height = VGA_HEIGHT,
1487 .pixelformat = V4L2_PIX_FMT_YUYV,
1488 .field = V4L2_FIELD_NONE,
1489 .bytesperline = VGA_WIDTH*2,
1490 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
1491};
1492
Hans Verkuil78b526a2008-05-28 12:16:41 -03001493static int cafe_vidioc_enum_fmt_vid_cap(struct file *filp,
Jonathan Corbetd905b382006-11-04 09:25:53 -03001494 void *priv, struct v4l2_fmtdesc *fmt)
1495{
1496 struct cafe_camera *cam = priv;
1497 int ret;
1498
Jonathan Corbetd905b382006-11-04 09:25:53 -03001499 mutex_lock(&cam->s_mutex);
Hans Verkuil8bcfd7a2009-03-18 13:16:44 -03001500 ret = sensor_call(cam, video, enum_fmt, fmt);
Jonathan Corbetd905b382006-11-04 09:25:53 -03001501 mutex_unlock(&cam->s_mutex);
1502 return ret;
1503}
1504
1505
Hans Verkuil78b526a2008-05-28 12:16:41 -03001506static int cafe_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
Jonathan Corbetd905b382006-11-04 09:25:53 -03001507 struct v4l2_format *fmt)
1508{
1509 struct cafe_camera *cam = priv;
1510 int ret;
1511
1512 mutex_lock(&cam->s_mutex);
Hans Verkuil8bcfd7a2009-03-18 13:16:44 -03001513 ret = sensor_call(cam, video, try_fmt, fmt);
Jonathan Corbetd905b382006-11-04 09:25:53 -03001514 mutex_unlock(&cam->s_mutex);
1515 return ret;
1516}
1517
Hans Verkuil78b526a2008-05-28 12:16:41 -03001518static int cafe_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
Jonathan Corbetd905b382006-11-04 09:25:53 -03001519 struct v4l2_format *fmt)
1520{
1521 struct cafe_camera *cam = priv;
1522 int ret;
1523
1524 /*
1525 * Can't do anything if the device is not idle
1526 * Also can't if there are streaming buffers in place.
1527 */
1528 if (cam->state != S_IDLE || cam->n_sbufs > 0)
1529 return -EBUSY;
1530 /*
1531 * See if the formatting works in principle.
1532 */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001533 ret = cafe_vidioc_try_fmt_vid_cap(filp, priv, fmt);
Jonathan Corbetd905b382006-11-04 09:25:53 -03001534 if (ret)
1535 return ret;
1536 /*
1537 * Now we start to change things for real, so let's do it
1538 * under lock.
1539 */
1540 mutex_lock(&cam->s_mutex);
1541 cam->pix_format = fmt->fmt.pix;
1542 /*
1543 * Make sure we have appropriate DMA buffers.
1544 */
1545 ret = -ENOMEM;
1546 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1547 cafe_free_dma_bufs(cam);
1548 if (cam->nbufs == 0) {
1549 if (cafe_alloc_dma_bufs(cam, 0))
1550 goto out;
1551 }
1552 /*
1553 * It looks like this might work, so let's program the sensor.
1554 */
1555 ret = cafe_cam_configure(cam);
1556 if (! ret)
1557 ret = cafe_ctlr_configure(cam);
1558 out:
1559 mutex_unlock(&cam->s_mutex);
1560 return ret;
1561}
1562
1563/*
1564 * Return our stored notion of how the camera is/should be configured.
1565 * The V4l2 spec wants us to be smarter, and actually get this from
1566 * the camera (and not mess with it at open time). Someday.
1567 */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001568static int cafe_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
Jonathan Corbetd905b382006-11-04 09:25:53 -03001569 struct v4l2_format *f)
1570{
1571 struct cafe_camera *cam = priv;
1572
1573 f->fmt.pix = cam->pix_format;
1574 return 0;
1575}
1576
1577/*
1578 * We only have one input - the sensor - so minimize the nonsense here.
1579 */
1580static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1581 struct v4l2_input *input)
1582{
1583 if (input->index != 0)
1584 return -EINVAL;
1585
1586 input->type = V4L2_INPUT_TYPE_CAMERA;
1587 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1588 strcpy(input->name, "Camera");
1589 return 0;
1590}
1591
1592static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1593{
1594 *i = 0;
1595 return 0;
1596}
1597
1598static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1599{
1600 if (i != 0)
1601 return -EINVAL;
1602 return 0;
1603}
1604
1605/* from vivi.c */
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -03001606static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
Jonathan Corbetd905b382006-11-04 09:25:53 -03001607{
1608 return 0;
1609}
1610
Jonathan Corbetc8f5b2f52006-12-01 15:50:59 -03001611/*
1612 * G/S_PARM. Most of this is done by the sensor, but we are
1613 * the level which controls the number of read buffers.
1614 */
1615static int cafe_vidioc_g_parm(struct file *filp, void *priv,
1616 struct v4l2_streamparm *parms)
1617{
1618 struct cafe_camera *cam = priv;
1619 int ret;
1620
1621 mutex_lock(&cam->s_mutex);
Hans Verkuil8bcfd7a2009-03-18 13:16:44 -03001622 ret = sensor_call(cam, video, g_parm, parms);
Jonathan Corbetc8f5b2f52006-12-01 15:50:59 -03001623 mutex_unlock(&cam->s_mutex);
1624 parms->parm.capture.readbuffers = n_dma_bufs;
1625 return ret;
1626}
1627
1628static int cafe_vidioc_s_parm(struct file *filp, void *priv,
1629 struct v4l2_streamparm *parms)
1630{
1631 struct cafe_camera *cam = priv;
1632 int ret;
1633
1634 mutex_lock(&cam->s_mutex);
Hans Verkuil8bcfd7a2009-03-18 13:16:44 -03001635 ret = sensor_call(cam, video, s_parm, parms);
Jonathan Corbetc8f5b2f52006-12-01 15:50:59 -03001636 mutex_unlock(&cam->s_mutex);
1637 parms->parm.capture.readbuffers = n_dma_bufs;
1638 return ret;
1639}
1640
Hans Verkuil69d94f72009-03-18 13:25:54 -03001641static int cafe_vidioc_g_chip_ident(struct file *file, void *priv,
1642 struct v4l2_dbg_chip_ident *chip)
1643{
1644 struct cafe_camera *cam = priv;
1645
1646 chip->ident = V4L2_IDENT_NONE;
1647 chip->revision = 0;
1648 if (v4l2_chip_match_host(&chip->match)) {
1649 chip->ident = V4L2_IDENT_CAFE;
1650 return 0;
1651 }
1652 return sensor_call(cam, core, g_chip_ident, chip);
1653}
1654
1655#ifdef CONFIG_VIDEO_ADV_DEBUG
1656static int cafe_vidioc_g_register(struct file *file, void *priv,
1657 struct v4l2_dbg_register *reg)
1658{
1659 struct cafe_camera *cam = priv;
1660
1661 if (v4l2_chip_match_host(&reg->match)) {
1662 reg->val = cafe_reg_read(cam, reg->reg);
1663 reg->size = 4;
1664 return 0;
1665 }
1666 return sensor_call(cam, core, g_register, reg);
1667}
1668
1669static int cafe_vidioc_s_register(struct file *file, void *priv,
1670 struct v4l2_dbg_register *reg)
1671{
1672 struct cafe_camera *cam = priv;
1673
1674 if (v4l2_chip_match_host(&reg->match)) {
1675 cafe_reg_write(cam, reg->reg, reg->val);
1676 return 0;
1677 }
1678 return sensor_call(cam, core, s_register, reg);
1679}
1680#endif
1681
Jonathan Corbetd905b382006-11-04 09:25:53 -03001682/*
1683 * This template device holds all of those v4l2 methods; we
1684 * clone it for specific real devices.
1685 */
1686
Hans Verkuilbec43662008-12-30 06:58:20 -03001687static const struct v4l2_file_operations cafe_v4l_fops = {
Jonathan Corbetd905b382006-11-04 09:25:53 -03001688 .owner = THIS_MODULE,
1689 .open = cafe_v4l_open,
1690 .release = cafe_v4l_release,
1691 .read = cafe_v4l_read,
1692 .poll = cafe_v4l_poll,
1693 .mmap = cafe_v4l_mmap,
1694 .ioctl = video_ioctl2,
Jonathan Corbetd905b382006-11-04 09:25:53 -03001695};
1696
Hans Verkuila3998102008-07-21 02:57:38 -03001697static const struct v4l2_ioctl_ops cafe_v4l_ioctl_ops = {
Jonathan Corbetd905b382006-11-04 09:25:53 -03001698 .vidioc_querycap = cafe_vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001699 .vidioc_enum_fmt_vid_cap = cafe_vidioc_enum_fmt_vid_cap,
1700 .vidioc_try_fmt_vid_cap = cafe_vidioc_try_fmt_vid_cap,
1701 .vidioc_s_fmt_vid_cap = cafe_vidioc_s_fmt_vid_cap,
1702 .vidioc_g_fmt_vid_cap = cafe_vidioc_g_fmt_vid_cap,
Jonathan Corbetd905b382006-11-04 09:25:53 -03001703 .vidioc_enum_input = cafe_vidioc_enum_input,
1704 .vidioc_g_input = cafe_vidioc_g_input,
1705 .vidioc_s_input = cafe_vidioc_s_input,
1706 .vidioc_s_std = cafe_vidioc_s_std,
1707 .vidioc_reqbufs = cafe_vidioc_reqbufs,
1708 .vidioc_querybuf = cafe_vidioc_querybuf,
1709 .vidioc_qbuf = cafe_vidioc_qbuf,
1710 .vidioc_dqbuf = cafe_vidioc_dqbuf,
1711 .vidioc_streamon = cafe_vidioc_streamon,
1712 .vidioc_streamoff = cafe_vidioc_streamoff,
1713 .vidioc_queryctrl = cafe_vidioc_queryctrl,
1714 .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
1715 .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
Jonathan Corbetc8f5b2f52006-12-01 15:50:59 -03001716 .vidioc_g_parm = cafe_vidioc_g_parm,
1717 .vidioc_s_parm = cafe_vidioc_s_parm,
Hans Verkuil69d94f72009-03-18 13:25:54 -03001718 .vidioc_g_chip_ident = cafe_vidioc_g_chip_ident,
1719#ifdef CONFIG_VIDEO_ADV_DEBUG
1720 .vidioc_g_register = cafe_vidioc_g_register,
1721 .vidioc_s_register = cafe_vidioc_s_register,
1722#endif
Jonathan Corbetd905b382006-11-04 09:25:53 -03001723};
1724
Hans Verkuila3998102008-07-21 02:57:38 -03001725static struct video_device cafe_v4l_template = {
1726 .name = "cafe",
Hans Verkuila3998102008-07-21 02:57:38 -03001727 .minor = -1, /* Get one dynamically */
1728 .tvnorms = V4L2_STD_NTSC_M,
1729 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1730
1731 .fops = &cafe_v4l_fops,
1732 .ioctl_ops = &cafe_v4l_ioctl_ops,
Hans Verkuil21508b902009-03-18 13:13:09 -03001733 .release = video_device_release_empty,
Hans Verkuila3998102008-07-21 02:57:38 -03001734};
1735
Jonathan Corbetd905b382006-11-04 09:25:53 -03001736
Jonathan Corbetd905b382006-11-04 09:25:53 -03001737/* ---------------------------------------------------------------------- */
1738/*
1739 * Interrupt handler stuff
1740 */
1741
Jonathan Corbetd905b382006-11-04 09:25:53 -03001742
1743
1744static void cafe_frame_tasklet(unsigned long data)
1745{
1746 struct cafe_camera *cam = (struct cafe_camera *) data;
1747 int i;
1748 unsigned long flags;
1749 struct cafe_sio_buffer *sbuf;
1750
1751 spin_lock_irqsave(&cam->dev_lock, flags);
1752 for (i = 0; i < cam->nbufs; i++) {
1753 int bufno = cam->next_buf;
1754 if (bufno < 0) { /* "will never happen" */
1755 cam_err(cam, "No valid bufs in tasklet!\n");
1756 break;
1757 }
1758 if (++(cam->next_buf) >= cam->nbufs)
1759 cam->next_buf = 0;
1760 if (! test_bit(bufno, &cam->flags))
1761 continue;
1762 if (list_empty(&cam->sb_avail))
1763 break; /* Leave it valid, hope for better later */
1764 clear_bit(bufno, &cam->flags);
Jonathan Corbetd905b382006-11-04 09:25:53 -03001765 sbuf = list_entry(cam->sb_avail.next,
1766 struct cafe_sio_buffer, list);
Jonathan Corbet5b50ed72007-04-27 12:32:28 -03001767 /*
1768 * Drop the lock during the big copy. This *should* be safe...
1769 */
1770 spin_unlock_irqrestore(&cam->dev_lock, flags);
Jonathan Corbeta66d2332006-12-01 15:37:49 -03001771 memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1772 cam->pix_format.sizeimage);
Jonathan Corbetd905b382006-11-04 09:25:53 -03001773 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1774 sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1775 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1776 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
Jonathan Corbet5b50ed72007-04-27 12:32:28 -03001777 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbetd905b382006-11-04 09:25:53 -03001778 list_move_tail(&sbuf->list, &cam->sb_full);
1779 }
1780 if (! list_empty(&cam->sb_full))
1781 wake_up(&cam->iowait);
1782 spin_unlock_irqrestore(&cam->dev_lock, flags);
1783}
1784
1785
1786
1787static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1788{
1789 /*
1790 * Basic frame housekeeping.
1791 */
1792 if (test_bit(frame, &cam->flags) && printk_ratelimit())
1793 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1794 set_bit(frame, &cam->flags);
1795 clear_bit(CF_DMA_ACTIVE, &cam->flags);
1796 if (cam->next_buf < 0)
1797 cam->next_buf = frame;
1798 cam->buf_seq[frame] = ++(cam->sequence);
1799
1800 switch (cam->state) {
1801 /*
1802 * If in single read mode, try going speculative.
1803 */
1804 case S_SINGLEREAD:
1805 cam->state = S_SPECREAD;
1806 cam->specframes = 0;
1807 wake_up(&cam->iowait);
1808 break;
1809
1810 /*
1811 * If we are already doing speculative reads, and nobody is
1812 * reading them, just stop.
1813 */
1814 case S_SPECREAD:
1815 if (++(cam->specframes) >= cam->nbufs) {
1816 cafe_ctlr_stop(cam);
1817 cafe_ctlr_irq_disable(cam);
1818 cam->state = S_IDLE;
1819 }
1820 wake_up(&cam->iowait);
1821 break;
1822 /*
1823 * For the streaming case, we defer the real work to the
1824 * camera tasklet.
1825 *
1826 * FIXME: if the application is not consuming the buffers,
1827 * we should eventually put things on hold and restart in
1828 * vidioc_dqbuf().
1829 */
1830 case S_STREAMING:
1831 tasklet_schedule(&cam->s_tasklet);
1832 break;
1833
1834 default:
1835 cam_err(cam, "Frame interrupt in non-operational state\n");
1836 break;
1837 }
1838}
1839
1840
1841
1842
1843static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1844{
1845 unsigned int frame;
1846
1847 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1848 /*
1849 * Handle any frame completions. There really should
1850 * not be more than one of these, or we have fallen
1851 * far behind.
1852 */
1853 for (frame = 0; frame < cam->nbufs; frame++)
1854 if (irqs & (IRQ_EOF0 << frame))
1855 cafe_frame_complete(cam, frame);
1856 /*
1857 * If a frame starts, note that we have DMA active. This
1858 * code assumes that we won't get multiple frame interrupts
1859 * at once; may want to rethink that.
1860 */
1861 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1862 set_bit(CF_DMA_ACTIVE, &cam->flags);
1863}
1864
1865
1866
1867static irqreturn_t cafe_irq(int irq, void *data)
1868{
1869 struct cafe_camera *cam = data;
1870 unsigned int irqs;
1871
1872 spin_lock(&cam->dev_lock);
1873 irqs = cafe_reg_read(cam, REG_IRQSTAT);
1874 if ((irqs & ALLIRQS) == 0) {
1875 spin_unlock(&cam->dev_lock);
1876 return IRQ_NONE;
1877 }
1878 if (irqs & FRAMEIRQS)
1879 cafe_frame_irq(cam, irqs);
1880 if (irqs & TWSIIRQS) {
1881 cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1882 wake_up(&cam->smbus_wait);
1883 }
1884 spin_unlock(&cam->dev_lock);
1885 return IRQ_HANDLED;
1886}
1887
1888
1889/* -------------------------------------------------------------------------- */
Jonathan Corbetd905b382006-11-04 09:25:53 -03001890/*
1891 * PCI interface stuff.
1892 */
1893
1894static int cafe_pci_probe(struct pci_dev *pdev,
1895 const struct pci_device_id *id)
1896{
1897 int ret;
Jonathan Corbetd905b382006-11-04 09:25:53 -03001898 struct cafe_camera *cam;
David Woodhouseaa7a7fb2008-09-03 09:49:20 +01001899
Jonathan Corbetd905b382006-11-04 09:25:53 -03001900 /*
1901 * Start putting together one of our big camera structures.
1902 */
1903 ret = -ENOMEM;
1904 cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
1905 if (cam == NULL)
1906 goto out;
Hans Verkuil21508b902009-03-18 13:13:09 -03001907 ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
1908 if (ret)
1909 goto out_free;
1910
Jonathan Corbetd905b382006-11-04 09:25:53 -03001911 mutex_init(&cam->s_mutex);
1912 mutex_lock(&cam->s_mutex);
1913 spin_lock_init(&cam->dev_lock);
1914 cam->state = S_NOTREADY;
1915 cafe_set_config_needed(cam, 1);
1916 init_waitqueue_head(&cam->smbus_wait);
1917 init_waitqueue_head(&cam->iowait);
1918 cam->pdev = pdev;
1919 cam->pix_format = cafe_def_pix_format;
1920 INIT_LIST_HEAD(&cam->dev_list);
1921 INIT_LIST_HEAD(&cam->sb_avail);
1922 INIT_LIST_HEAD(&cam->sb_full);
1923 tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
1924 /*
1925 * Get set up on the PCI bus.
1926 */
1927 ret = pci_enable_device(pdev);
1928 if (ret)
Hans Verkuil21508b902009-03-18 13:13:09 -03001929 goto out_unreg;
Jonathan Corbetd905b382006-11-04 09:25:53 -03001930 pci_set_master(pdev);
1931
1932 ret = -EIO;
1933 cam->regs = pci_iomap(pdev, 0, 0);
1934 if (! cam->regs) {
1935 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
Hans Verkuil21508b902009-03-18 13:13:09 -03001936 goto out_unreg;
Jonathan Corbetd905b382006-11-04 09:25:53 -03001937 }
1938 ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
1939 if (ret)
1940 goto out_iounmap;
Jonathan Corbet7acf90c2007-05-22 00:37:58 -03001941 /*
1942 * Initialize the controller and leave it powered up. It will
1943 * stay that way until the sensor driver shows up.
1944 */
Jonathan Corbetd905b382006-11-04 09:25:53 -03001945 cafe_ctlr_init(cam);
1946 cafe_ctlr_power_up(cam);
1947 /*
Jonathan Corbet7acf90c2007-05-22 00:37:58 -03001948 * Set up I2C/SMBUS communications. We have to drop the mutex here
1949 * because the sensor could attach in this call chain, leading to
1950 * unsightly deadlocks.
Jonathan Corbetd905b382006-11-04 09:25:53 -03001951 */
1952 mutex_unlock(&cam->s_mutex); /* attach can deadlock */
1953 ret = cafe_smbus_setup(cam);
1954 if (ret)
1955 goto out_freeirq;
Hans Verkuil8bcfd7a2009-03-18 13:16:44 -03001956
1957 cam->sensor_addr = 0x42;
Hans Verkuile6574f22009-04-01 03:57:53 -03001958 cam->sensor = v4l2_i2c_new_subdev(&cam->v4l2_dev, &cam->i2c_adapter,
Hans Verkuil8bcfd7a2009-03-18 13:16:44 -03001959 "ov7670", "ov7670", cam->sensor_addr);
1960 if (cam->sensor == NULL) {
1961 ret = -ENODEV;
1962 goto out_smbus;
1963 }
1964 ret = cafe_cam_init(cam);
1965 if (ret)
1966 goto out_smbus;
1967
Jonathan Corbetd905b382006-11-04 09:25:53 -03001968 /*
1969 * Get the v4l2 setup done.
1970 */
1971 mutex_lock(&cam->s_mutex);
Hans Verkuil21508b902009-03-18 13:13:09 -03001972 cam->vdev = cafe_v4l_template;
1973 cam->vdev.debug = 0;
1974/* cam->vdev.debug = V4L2_DEBUG_IOCTL_ARG;*/
1975 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1976 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
Jonathan Corbetd905b382006-11-04 09:25:53 -03001977 if (ret)
1978 goto out_smbus;
Hans Verkuil21508b902009-03-18 13:13:09 -03001979 video_set_drvdata(&cam->vdev, cam);
1980
Jonathan Corbetd905b382006-11-04 09:25:53 -03001981 /*
1982 * If so requested, try to get our DMA buffers now.
1983 */
Andres Salomon23869e22007-09-19 02:44:18 -03001984 if (!alloc_bufs_at_read) {
Jonathan Corbetd905b382006-11-04 09:25:53 -03001985 if (cafe_alloc_dma_bufs(cam, 1))
1986 cam_warn(cam, "Unable to alloc DMA buffers at load"
1987 " will try again later.");
1988 }
1989
Jonathan Corbetd905b382006-11-04 09:25:53 -03001990 mutex_unlock(&cam->s_mutex);
Jonathan Corbetd905b382006-11-04 09:25:53 -03001991 return 0;
1992
Hans Verkuil21508b902009-03-18 13:13:09 -03001993out_smbus:
Jonathan Corbetd905b382006-11-04 09:25:53 -03001994 cafe_smbus_shutdown(cam);
Hans Verkuil21508b902009-03-18 13:13:09 -03001995out_freeirq:
Jonathan Corbetd905b382006-11-04 09:25:53 -03001996 cafe_ctlr_power_down(cam);
1997 free_irq(pdev->irq, cam);
Hans Verkuil21508b902009-03-18 13:13:09 -03001998out_iounmap:
Jonathan Corbetd905b382006-11-04 09:25:53 -03001999 pci_iounmap(pdev, cam->regs);
Hans Verkuil21508b902009-03-18 13:13:09 -03002000out_free:
2001 v4l2_device_unregister(&cam->v4l2_dev);
2002out_unreg:
Jonathan Corbetd905b382006-11-04 09:25:53 -03002003 kfree(cam);
Hans Verkuil21508b902009-03-18 13:13:09 -03002004out:
Jonathan Corbetd905b382006-11-04 09:25:53 -03002005 return ret;
2006}
2007
2008
2009/*
2010 * Shut down an initialized device
2011 */
2012static void cafe_shutdown(struct cafe_camera *cam)
2013{
2014/* FIXME: Make sure we take care of everything here */
Jonathan Corbetd905b382006-11-04 09:25:53 -03002015 if (cam->n_sbufs > 0)
2016 /* What if they are still mapped? Shouldn't be, but... */
2017 cafe_free_sio_buffers(cam);
Jonathan Corbetd905b382006-11-04 09:25:53 -03002018 cafe_ctlr_stop_dma(cam);
2019 cafe_ctlr_power_down(cam);
2020 cafe_smbus_shutdown(cam);
2021 cafe_free_dma_bufs(cam);
2022 free_irq(cam->pdev->irq, cam);
2023 pci_iounmap(cam->pdev, cam->regs);
Hans Verkuil21508b902009-03-18 13:13:09 -03002024 video_unregister_device(&cam->vdev);
Jonathan Corbetd905b382006-11-04 09:25:53 -03002025}
2026
2027
2028static void cafe_pci_remove(struct pci_dev *pdev)
2029{
Hans Verkuil21508b902009-03-18 13:13:09 -03002030 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2031 struct cafe_camera *cam = to_cam(v4l2_dev);
Jonathan Corbetd905b382006-11-04 09:25:53 -03002032
2033 if (cam == NULL) {
Adrian Bunkd4f60baf2006-12-20 09:34:32 -03002034 printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
Jonathan Corbetd905b382006-11-04 09:25:53 -03002035 return;
2036 }
2037 mutex_lock(&cam->s_mutex);
2038 if (cam->users > 0)
2039 cam_warn(cam, "Removing a device with users!\n");
2040 cafe_shutdown(cam);
Hans Verkuil21508b902009-03-18 13:13:09 -03002041 v4l2_device_unregister(&cam->v4l2_dev);
2042 kfree(cam);
Jonathan Corbetd905b382006-11-04 09:25:53 -03002043/* No unlock - it no longer exists */
2044}
2045
2046
Jonathan Corbetff68def2007-03-25 11:36:28 -03002047#ifdef CONFIG_PM
2048/*
2049 * Basic power management.
2050 */
2051static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2052{
Hans Verkuil21508b902009-03-18 13:13:09 -03002053 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2054 struct cafe_camera *cam = to_cam(v4l2_dev);
Jonathan Corbetff68def2007-03-25 11:36:28 -03002055 int ret;
Jonathan Corbetc3034492007-10-23 17:30:27 -03002056 enum cafe_state cstate;
Jonathan Corbetff68def2007-03-25 11:36:28 -03002057
2058 ret = pci_save_state(pdev);
2059 if (ret)
2060 return ret;
Jonathan Corbetc3034492007-10-23 17:30:27 -03002061 cstate = cam->state; /* HACK - stop_dma sets to idle */
Jonathan Corbetff68def2007-03-25 11:36:28 -03002062 cafe_ctlr_stop_dma(cam);
2063 cafe_ctlr_power_down(cam);
2064 pci_disable_device(pdev);
Jonathan Corbetc3034492007-10-23 17:30:27 -03002065 cam->state = cstate;
Jonathan Corbetff68def2007-03-25 11:36:28 -03002066 return 0;
2067}
2068
2069
2070static int cafe_pci_resume(struct pci_dev *pdev)
2071{
Hans Verkuil21508b902009-03-18 13:13:09 -03002072 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2073 struct cafe_camera *cam = to_cam(v4l2_dev);
Jonathan Corbetff68def2007-03-25 11:36:28 -03002074 int ret = 0;
2075
2076 ret = pci_restore_state(pdev);
2077 if (ret)
2078 return ret;
Trent Piepho12df2f52007-04-25 00:20:13 -03002079 ret = pci_enable_device(pdev);
Chris Ball01659f22007-08-17 01:01:33 -03002080
Trent Piepho12df2f52007-04-25 00:20:13 -03002081 if (ret) {
2082 cam_warn(cam, "Unable to re-enable device on resume!\n");
2083 return ret;
2084 }
Jonathan Corbetff68def2007-03-25 11:36:28 -03002085 cafe_ctlr_init(cam);
Chris Ball01659f22007-08-17 01:01:33 -03002086 cafe_ctlr_power_down(cam);
2087
2088 mutex_lock(&cam->s_mutex);
2089 if (cam->users > 0) {
2090 cafe_ctlr_power_up(cam);
2091 __cafe_cam_reset(cam);
2092 }
2093 mutex_unlock(&cam->s_mutex);
2094
Jonathan Corbetff68def2007-03-25 11:36:28 -03002095 set_bit(CF_CONFIG_NEEDED, &cam->flags);
2096 if (cam->state == S_SPECREAD)
2097 cam->state = S_IDLE; /* Don't bother restarting */
2098 else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
2099 ret = cafe_read_setup(cam, cam->state);
2100 return ret;
2101}
2102
2103#endif /* CONFIG_PM */
Jonathan Corbetd905b382006-11-04 09:25:53 -03002104
2105
2106static struct pci_device_id cafe_ids[] = {
David Woodhouseaa7a7fb2008-09-03 09:49:20 +01002107 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL,
2108 PCI_DEVICE_ID_MARVELL_88ALP01_CCIC) },
Jonathan Corbetd905b382006-11-04 09:25:53 -03002109 { 0, }
2110};
2111
2112MODULE_DEVICE_TABLE(pci, cafe_ids);
2113
2114static struct pci_driver cafe_pci_driver = {
2115 .name = "cafe1000-ccic",
2116 .id_table = cafe_ids,
2117 .probe = cafe_pci_probe,
2118 .remove = cafe_pci_remove,
Jonathan Corbetff68def2007-03-25 11:36:28 -03002119#ifdef CONFIG_PM
2120 .suspend = cafe_pci_suspend,
2121 .resume = cafe_pci_resume,
2122#endif
Jonathan Corbetd905b382006-11-04 09:25:53 -03002123};
2124
2125
2126
2127
2128static int __init cafe_init(void)
2129{
2130 int ret;
2131
2132 printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2133 CAFE_VERSION);
Jonathan Corbetd905b382006-11-04 09:25:53 -03002134 ret = pci_register_driver(&cafe_pci_driver);
2135 if (ret) {
2136 printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2137 goto out;
2138 }
Jonathan Corbetd905b382006-11-04 09:25:53 -03002139 ret = 0;
2140
2141 out:
2142 return ret;
2143}
2144
2145
2146static void __exit cafe_exit(void)
2147{
2148 pci_unregister_driver(&cafe_pci_driver);
Jonathan Corbetd905b382006-11-04 09:25:53 -03002149}
2150
2151module_init(cafe_init);
2152module_exit(cafe_exit);