aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media
diff options
context:
space:
mode:
authorJonathan Corbet <corbet@lwn.net>2010-10-19 20:32:11 -0400
committerMauro Carvalho Chehab <mchehab@redhat.com>2010-10-21 11:45:28 -0400
commit024fafbac36b176d978ccd0fb1cae1fbc38c7fee (patch)
tree02b52bf8c6104ab4a8f7bcdf87741aee4b219f3b /drivers/media
parentdc4589c814a3a50a4cfc2077690fc7fd397308c8 (diff)
[media] Add the via framebuffer camera controller driver
Add a driver for the video capture port on VIA integrated chipsets. This version has a remaining OLPCism or two and expects to be talking to an ov7670; those can be improved as the need arises. This work was supported by the One Laptop Per Child project. Thanks to Laurent Pinchart for a number of useful comments. Cc: Florian Tobias Schandinat <FlorianSchandinat@gmx.de> Signed-off-by: Jonathan Corbet <corbet@lwn.net> Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Diffstat (limited to 'drivers/media')
-rw-r--r--drivers/media/video/Kconfig10
-rw-r--r--drivers/media/video/Makefile2
-rw-r--r--drivers/media/video/via-camera.c1474
-rw-r--r--drivers/media/video/via-camera.h93
4 files changed, 1579 insertions, 0 deletions
diff --git a/drivers/media/video/Kconfig b/drivers/media/video/Kconfig
index 2d4226710ed5..0efbb29a1a08 100644
--- a/drivers/media/video/Kconfig
+++ b/drivers/media/video/Kconfig
@@ -712,6 +712,16 @@ config VIDEO_SR030PC30
712 ---help--- 712 ---help---
713 This driver supports SR030PC30 VGA camera from Siliconfile 713 This driver supports SR030PC30 VGA camera from Siliconfile
714 714
715config VIDEO_VIA_CAMERA
716 tristate "VIAFB camera controller support"
717 depends on FB_VIA
718 select VIDEOBUF_DMA_SG
719 select VIDEO_OV7670
720 help
721 Driver support for the integrated camera controller in VIA
722 Chrome9 chipsets. Currently only tested on OLPC xo-1.5 systems
723 with ov7670 sensors.
724
715config SOC_CAMERA 725config SOC_CAMERA
716 tristate "SoC camera support" 726 tristate "SoC camera support"
717 depends on VIDEO_V4L2 && HAS_DMA && I2C 727 depends on VIDEO_V4L2 && HAS_DMA && I2C
diff --git a/drivers/media/video/Makefile b/drivers/media/video/Makefile
index d5e49ddbf9dd..af79d476a4c8 100644
--- a/drivers/media/video/Makefile
+++ b/drivers/media/video/Makefile
@@ -122,6 +122,8 @@ obj-$(CONFIG_VIDEO_CX2341X) += cx2341x.o
122 122
123obj-$(CONFIG_VIDEO_CAFE_CCIC) += cafe_ccic.o 123obj-$(CONFIG_VIDEO_CAFE_CCIC) += cafe_ccic.o
124 124
125obj-$(CONFIG_VIDEO_VIA_CAMERA) += via-camera.o
126
125obj-$(CONFIG_USB_DABUSB) += dabusb.o 127obj-$(CONFIG_USB_DABUSB) += dabusb.o
126obj-$(CONFIG_USB_SE401) += se401.o 128obj-$(CONFIG_USB_SE401) += se401.o
127obj-$(CONFIG_USB_ZR364XX) += zr364xx.o 129obj-$(CONFIG_USB_ZR364XX) += zr364xx.o
diff --git a/drivers/media/video/via-camera.c b/drivers/media/video/via-camera.c
new file mode 100644
index 000000000000..02a21bccae18
--- /dev/null
+++ b/drivers/media/video/via-camera.c
@@ -0,0 +1,1474 @@
1/*
2 * Driver for the VIA Chrome integrated camera controller.
3 *
4 * Copyright 2009,2010 Jonathan Corbet <corbet@lwn.net>
5 * Distributable under the terms of the GNU General Public License, version 2
6 *
7 * This work was supported by the One Laptop Per Child project
8 */
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/device.h>
12#include <linux/list.h>
13#include <linux/pci.h>
14#include <linux/gpio.h>
15#include <linux/interrupt.h>
16#include <linux/pci.h>
17#include <linux/platform_device.h>
18#include <linux/videodev2.h>
19#include <media/v4l2-device.h>
20#include <media/v4l2-ioctl.h>
21#include <media/v4l2-chip-ident.h>
22#include <media/videobuf-dma-sg.h>
23#include <linux/device.h>
24#include <linux/delay.h>
25#include <linux/dma-mapping.h>
26#include <linux/pm_qos_params.h>
27#include <linux/via-core.h>
28#include <linux/via-gpio.h>
29#include <linux/via_i2c.h>
30
31#include "via-camera.h"
32
33MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
34MODULE_DESCRIPTION("VIA framebuffer-based camera controller driver");
35MODULE_LICENSE("GPL");
36
37static int flip_image;
38module_param(flip_image, bool, 0444);
39MODULE_PARM_DESC(flip_image,
40 "If set, the sensor will be instructed to flip the image "
41 "vertically.");
42
43#ifdef CONFIG_OLPC_XO_1_5
44static int override_serial;
45module_param(override_serial, bool, 0444);
46MODULE_PARM_DESC(override_serial,
47 "The camera driver will normally refuse to load if "
48 "the XO 1.5 serial port is enabled. Set this option "
49 "to force the issue.");
50#endif
51
52/*
53 * Basic window sizes.
54 */
55#define VGA_WIDTH 640
56#define VGA_HEIGHT 480
57#define QCIF_WIDTH 176
58#define QCIF_HEIGHT 144
59
60/*
61 * The structure describing our camera.
62 */
63enum viacam_opstate { S_IDLE = 0, S_RUNNING = 1 };
64
65struct via_camera {
66 struct v4l2_device v4l2_dev;
67 struct video_device vdev;
68 struct v4l2_subdev *sensor;
69 struct platform_device *platdev;
70 struct viafb_dev *viadev;
71 struct mutex lock;
72 enum viacam_opstate opstate;
73 unsigned long flags;
74 struct pm_qos_request_list qos_request;
75 /*
76 * GPIO info for power/reset management
77 */
78 int power_gpio;
79 int reset_gpio;
80 /*
81 * I/O memory stuff.
82 */
83 void __iomem *mmio; /* Where the registers live */
84 void __iomem *fbmem; /* Frame buffer memory */
85 u32 fb_offset; /* Reserved memory offset (FB) */
86 /*
87 * Capture buffers and related. The controller supports
88 * up to three, so that's what we have here. These buffers
89 * live in frame buffer memory, so we don't call them "DMA".
90 */
91 unsigned int cb_offsets[3]; /* offsets into fb mem */
92 u8 *cb_addrs[3]; /* Kernel-space addresses */
93 int n_cap_bufs; /* How many are we using? */
94 int next_buf;
95 struct videobuf_queue vb_queue;
96 struct list_head buffer_queue; /* prot. by reg_lock */
97 /*
98 * User tracking.
99 */
100 int users;
101 struct file *owner;
102 /*
103 * Video format information. sensor_format is kept in a form
104 * that we can use to pass to the sensor. We always run the
105 * sensor in VGA resolution, though, and let the controller
106 * downscale things if need be. So we keep the "real*
107 * dimensions separately.
108 */
109 struct v4l2_pix_format sensor_format;
110 struct v4l2_pix_format user_format;
111 enum v4l2_mbus_pixelcode mbus_code;
112};
113
114/*
115 * Yes, this is a hack, but there's only going to be one of these
116 * on any system we know of.
117 */
118static struct via_camera *via_cam_info;
119
120/*
121 * Flag values, manipulated with bitops
122 */
123#define CF_DMA_ACTIVE 0 /* A frame is incoming */
124#define CF_CONFIG_NEEDED 1 /* Must configure hardware */
125
126
127/*
128 * Nasty ugly v4l2 boilerplate.
129 */
130#define sensor_call(cam, optype, func, args...) \
131 v4l2_subdev_call(cam->sensor, optype, func, ##args)
132
133/*
134 * Debugging and related.
135 */
136#define cam_err(cam, fmt, arg...) \
137 dev_err(&(cam)->platdev->dev, fmt, ##arg);
138#define cam_warn(cam, fmt, arg...) \
139 dev_warn(&(cam)->platdev->dev, fmt, ##arg);
140#define cam_dbg(cam, fmt, arg...) \
141 dev_dbg(&(cam)->platdev->dev, fmt, ##arg);
142
143/*
144 * Format handling. This is ripped almost directly from Hans's changes
145 * to cafe_ccic.c. It's a little unfortunate; until this change, we
146 * didn't need to know anything about the format except its byte depth;
147 * now this information must be managed at this level too.
148 */
149static struct via_format {
150 __u8 *desc;
151 __u32 pixelformat;
152 int bpp; /* Bytes per pixel */
153 enum v4l2_mbus_pixelcode mbus_code;
154} via_formats[] = {
155 {
156 .desc = "YUYV 4:2:2",
157 .pixelformat = V4L2_PIX_FMT_YUYV,
158 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
159 .bpp = 2,
160 },
161 {
162 .desc = "RGB 565",
163 .pixelformat = V4L2_PIX_FMT_RGB565,
164 .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE,
165 .bpp = 2,
166 },
167 /* RGB444 and Bayer should be doable, but have never been
168 tested with this driver. */
169};
170#define N_VIA_FMTS ARRAY_SIZE(via_formats)
171
172static struct via_format *via_find_format(u32 pixelformat)
173{
174 unsigned i;
175
176 for (i = 0; i < N_VIA_FMTS; i++)
177 if (via_formats[i].pixelformat == pixelformat)
178 return via_formats + i;
179 /* Not found? Then return the first format. */
180 return via_formats;
181}
182
183
184/*--------------------------------------------------------------------------*/
185/*
186 * Sensor power/reset management. This piece is OLPC-specific for
187 * sure; other configurations will have things connected differently.
188 */
189static int via_sensor_power_setup(struct via_camera *cam)
190{
191 int ret;
192
193 cam->power_gpio = viafb_gpio_lookup("VGPIO3");
194 cam->reset_gpio = viafb_gpio_lookup("VGPIO2");
195 if (cam->power_gpio < 0 || cam->reset_gpio < 0) {
196 dev_err(&cam->platdev->dev, "Unable to find GPIO lines\n");
197 return -EINVAL;
198 }
199 ret = gpio_request(cam->power_gpio, "viafb-camera");
200 if (ret) {
201 dev_err(&cam->platdev->dev, "Unable to request power GPIO\n");
202 return ret;
203 }
204 ret = gpio_request(cam->reset_gpio, "viafb-camera");
205 if (ret) {
206 dev_err(&cam->platdev->dev, "Unable to request reset GPIO\n");
207 gpio_free(cam->power_gpio);
208 return ret;
209 }
210 gpio_direction_output(cam->power_gpio, 0);
211 gpio_direction_output(cam->reset_gpio, 0);
212 return 0;
213}
214
215/*
216 * Power up the sensor and perform the reset dance.
217 */
218static void via_sensor_power_up(struct via_camera *cam)
219{
220 gpio_set_value(cam->power_gpio, 1);
221 gpio_set_value(cam->reset_gpio, 0);
222 msleep(20); /* Probably excessive */
223 gpio_set_value(cam->reset_gpio, 1);
224 msleep(20);
225}
226
227static void via_sensor_power_down(struct via_camera *cam)
228{
229 gpio_set_value(cam->power_gpio, 0);
230 gpio_set_value(cam->reset_gpio, 0);
231}
232
233
234static void via_sensor_power_release(struct via_camera *cam)
235{
236 via_sensor_power_down(cam);
237 gpio_free(cam->power_gpio);
238 gpio_free(cam->reset_gpio);
239}
240
241/* --------------------------------------------------------------------------*/
242/* Sensor ops */
243
244/*
245 * Manage the ov7670 "flip" bit, which needs special help.
246 */
247static int viacam_set_flip(struct via_camera *cam)
248{
249 struct v4l2_control ctrl;
250
251 memset(&ctrl, 0, sizeof(ctrl));
252 ctrl.id = V4L2_CID_VFLIP;
253 ctrl.value = flip_image;
254 return sensor_call(cam, core, s_ctrl, &ctrl);
255}
256
257/*
258 * Configure the sensor. It's up to the caller to ensure
259 * that the camera is in the correct operating state.
260 */
261static int viacam_configure_sensor(struct via_camera *cam)
262{
263 struct v4l2_mbus_framefmt mbus_fmt;
264 int ret;
265
266 v4l2_fill_mbus_format(&mbus_fmt, &cam->sensor_format, cam->mbus_code);
267 ret = sensor_call(cam, core, init, 0);
268 if (ret == 0)
269 ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
270 /*
271 * OV7670 does weird things if flip is set *before* format...
272 */
273 if (ret == 0)
274 ret = viacam_set_flip(cam);
275 return ret;
276}
277
278
279
280/* --------------------------------------------------------------------------*/
281/*
282 * Some simple register accessors; they assume that the lock is held.
283 *
284 * Should we want to support the second capture engine, we could
285 * hide the register difference by adding 0x1000 to registers in the
286 * 0x300-350 range.
287 */
288static inline void viacam_write_reg(struct via_camera *cam,
289 int reg, int value)
290{
291 iowrite32(value, cam->mmio + reg);
292}
293
294static inline int viacam_read_reg(struct via_camera *cam, int reg)
295{
296 return ioread32(cam->mmio + reg);
297}
298
299static inline void viacam_write_reg_mask(struct via_camera *cam,
300 int reg, int value, int mask)
301{
302 int tmp = viacam_read_reg(cam, reg);
303
304 tmp = (tmp & ~mask) | (value & mask);
305 viacam_write_reg(cam, reg, tmp);
306}
307
308
309/* --------------------------------------------------------------------------*/
310/* Interrupt management and handling */
311
312static irqreturn_t viacam_quick_irq(int irq, void *data)
313{
314 struct via_camera *cam = data;
315 irqreturn_t ret = IRQ_NONE;
316 int icv;
317
318 /*
319 * All we do here is to clear the interrupts and tell
320 * the handler thread to wake up.
321 */
322 spin_lock(&cam->viadev->reg_lock);
323 icv = viacam_read_reg(cam, VCR_INTCTRL);
324 if (icv & VCR_IC_EAV) {
325 icv |= VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL;
326 viacam_write_reg(cam, VCR_INTCTRL, icv);
327 ret = IRQ_WAKE_THREAD;
328 }
329 spin_unlock(&cam->viadev->reg_lock);
330 return ret;
331}
332
333/*
334 * Find the next videobuf buffer which has somebody waiting on it.
335 */
336static struct videobuf_buffer *viacam_next_buffer(struct via_camera *cam)
337{
338 unsigned long flags;
339 struct videobuf_buffer *buf = NULL;
340
341 spin_lock_irqsave(&cam->viadev->reg_lock, flags);
342 if (cam->opstate != S_RUNNING)
343 goto out;
344 if (list_empty(&cam->buffer_queue))
345 goto out;
346 buf = list_entry(cam->buffer_queue.next, struct videobuf_buffer, queue);
347 if (!waitqueue_active(&buf->done)) {/* Nobody waiting */
348 buf = NULL;
349 goto out;
350 }
351 list_del(&buf->queue);
352 buf->state = VIDEOBUF_ACTIVE;
353out:
354 spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
355 return buf;
356}
357
358/*
359 * The threaded IRQ handler.
360 */
361static irqreturn_t viacam_irq(int irq, void *data)
362{
363 int bufn;
364 struct videobuf_buffer *vb;
365 struct via_camera *cam = data;
366 struct videobuf_dmabuf *vdma;
367
368 /*
369 * If there is no place to put the data frame, don't bother
370 * with anything else.
371 */
372 vb = viacam_next_buffer(cam);
373 if (vb == NULL)
374 goto done;
375 /*
376 * Figure out which buffer we just completed.
377 */
378 bufn = (viacam_read_reg(cam, VCR_INTCTRL) & VCR_IC_ACTBUF) >> 3;
379 bufn -= 1;
380 if (bufn < 0)
381 bufn = cam->n_cap_bufs - 1;
382 /*
383 * Copy over the data and let any waiters know.
384 */
385 vdma = videobuf_to_dma(vb);
386 viafb_dma_copy_out_sg(cam->cb_offsets[bufn], vdma->sglist, vdma->sglen);
387 vb->state = VIDEOBUF_DONE;
388 vb->size = cam->user_format.sizeimage;
389 wake_up(&vb->done);
390done:
391 return IRQ_HANDLED;
392}
393
394
395/*
396 * These functions must mess around with the general interrupt
397 * control register, which is relevant to much more than just the
398 * camera. Nothing else uses interrupts, though, as of this writing.
399 * Should that situation change, we'll have to improve support at
400 * the via-core level.
401 */
402static void viacam_int_enable(struct via_camera *cam)
403{
404 viacam_write_reg(cam, VCR_INTCTRL,
405 VCR_IC_INTEN|VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL);
406 viafb_irq_enable(VDE_I_C0AVEN);
407}
408
409static void viacam_int_disable(struct via_camera *cam)
410{
411 viafb_irq_disable(VDE_I_C0AVEN);
412 viacam_write_reg(cam, VCR_INTCTRL, 0);
413}
414
415
416
417/* --------------------------------------------------------------------------*/
418/* Controller operations */
419
420/*
421 * Set up our capture buffers in framebuffer memory.
422 */
423static int viacam_ctlr_cbufs(struct via_camera *cam)
424{
425 int nbuf = cam->viadev->camera_fbmem_size/cam->sensor_format.sizeimage;
426 int i;
427 unsigned int offset;
428
429 /*
430 * See how many buffers we can work with.
431 */
432 if (nbuf >= 3) {
433 cam->n_cap_bufs = 3;
434 viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_3BUFS,
435 VCR_CI_3BUFS);
436 } else if (nbuf == 2) {
437 cam->n_cap_bufs = 2;
438 viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_3BUFS);
439 } else {
440 cam_warn(cam, "Insufficient frame buffer memory\n");
441 return -ENOMEM;
442 }
443 /*
444 * Set them up.
445 */
446 offset = cam->fb_offset;
447 for (i = 0; i < cam->n_cap_bufs; i++) {
448 cam->cb_offsets[i] = offset;
449 cam->cb_addrs[i] = cam->fbmem + offset;
450 viacam_write_reg(cam, VCR_VBUF1 + i*4, offset & VCR_VBUF_MASK);
451 offset += cam->sensor_format.sizeimage;
452 }
453 return 0;
454}
455
456/*
457 * Set the scaling register for downscaling the image.
458 *
459 * This register works like this... Vertical scaling is enabled
460 * by bit 26; if that bit is set, downscaling is controlled by the
461 * value in bits 16:25. Those bits are divided by 1024 to get
462 * the scaling factor; setting just bit 25 thus cuts the height
463 * in half.
464 *
465 * Horizontal scaling works about the same, but it's enabled by
466 * bit 11, with bits 0:10 giving the numerator of a fraction
467 * (over 2048) for the scaling value.
468 *
469 * This function is naive in that, if the user departs from
470 * the 3x4 VGA scaling factor, the image will distort. We
471 * could work around that if it really seemed important.
472 */
473static void viacam_set_scale(struct via_camera *cam)
474{
475 unsigned int avscale;
476 int sf;
477
478 if (cam->user_format.width == VGA_WIDTH)
479 avscale = 0;
480 else {
481 sf = (cam->user_format.width*2048)/VGA_WIDTH;
482 avscale = VCR_AVS_HEN | sf;
483 }
484 if (cam->user_format.height < VGA_HEIGHT) {
485 sf = (1024*cam->user_format.height)/VGA_HEIGHT;
486 avscale |= VCR_AVS_VEN | (sf << 16);
487 }
488 viacam_write_reg(cam, VCR_AVSCALE, avscale);
489}
490
491
492/*
493 * Configure image-related information into the capture engine.
494 */
495static void viacam_ctlr_image(struct via_camera *cam)
496{
497 int cicreg;
498
499 /*
500 * Disable clock before messing with stuff - from the via
501 * sample driver.
502 */
503 viacam_write_reg(cam, VCR_CAPINTC, ~(VCR_CI_ENABLE|VCR_CI_CLKEN));
504 /*
505 * Set up the controller for VGA resolution, modulo magic
506 * offsets from the via sample driver.
507 */
508 viacam_write_reg(cam, VCR_HORRANGE, 0x06200120);
509 viacam_write_reg(cam, VCR_VERTRANGE, 0x01de0000);
510 viacam_set_scale(cam);
511 /*
512 * Image size info.
513 */
514 viacam_write_reg(cam, VCR_MAXDATA,
515 (cam->sensor_format.height << 16) |
516 (cam->sensor_format.bytesperline >> 3));
517 viacam_write_reg(cam, VCR_MAXVBI, 0);
518 viacam_write_reg(cam, VCR_VSTRIDE,
519 cam->user_format.bytesperline & VCR_VS_STRIDE);
520 /*
521 * Set up the capture interface control register,
522 * everything but the "go" bit.
523 *
524 * The FIFO threshold is a bit of a magic number; 8 is what
525 * VIA's sample code uses.
526 */
527 cicreg = VCR_CI_CLKEN |
528 0x08000000 | /* FIFO threshold */
529 VCR_CI_FLDINV | /* OLPC-specific? */
530 VCR_CI_VREFINV | /* OLPC-specific? */
531 VCR_CI_DIBOTH | /* Capture both fields */
532 VCR_CI_CCIR601_8;
533 if (cam->n_cap_bufs == 3)
534 cicreg |= VCR_CI_3BUFS;
535 /*
536 * YUV formats need different byte swapping than RGB.
537 */
538 if (cam->user_format.pixelformat == V4L2_PIX_FMT_YUYV)
539 cicreg |= VCR_CI_YUYV;
540 else
541 cicreg |= VCR_CI_UYVY;
542 viacam_write_reg(cam, VCR_CAPINTC, cicreg);
543}
544
545
546static int viacam_config_controller(struct via_camera *cam)
547{
548 int ret;
549 unsigned long flags;
550
551 spin_lock_irqsave(&cam->viadev->reg_lock, flags);
552 ret = viacam_ctlr_cbufs(cam);
553 if (!ret)
554 viacam_ctlr_image(cam);
555 spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
556 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
557 return ret;
558}
559
560/*
561 * Make it start grabbing data.
562 */
563static void viacam_start_engine(struct via_camera *cam)
564{
565 spin_lock_irq(&cam->viadev->reg_lock);
566 cam->next_buf = 0;
567 viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_ENABLE, VCR_CI_ENABLE);
568 viacam_int_enable(cam);
569 (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
570 cam->opstate = S_RUNNING;
571 spin_unlock_irq(&cam->viadev->reg_lock);
572}
573
574
575static void viacam_stop_engine(struct via_camera *cam)
576{
577 spin_lock_irq(&cam->viadev->reg_lock);
578 viacam_int_disable(cam);
579 viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_ENABLE);
580 (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
581 cam->opstate = S_IDLE;
582 spin_unlock_irq(&cam->viadev->reg_lock);
583}
584
585
586/* --------------------------------------------------------------------------*/
587/* Videobuf callback ops */
588
589/*
590 * buffer_setup. The purpose of this one would appear to be to tell
591 * videobuf how big a single image is. It's also evidently up to us
592 * to put some sort of limit on the maximum number of buffers allowed.
593 */
594static int viacam_vb_buf_setup(struct videobuf_queue *q,
595 unsigned int *count, unsigned int *size)
596{
597 struct via_camera *cam = q->priv_data;
598
599 *size = cam->user_format.sizeimage;
600 if (*count == 0 || *count > 6) /* Arbitrary number */
601 *count = 6;
602 return 0;
603}
604
605/*
606 * Prepare a buffer.
607 */
608static int viacam_vb_buf_prepare(struct videobuf_queue *q,
609 struct videobuf_buffer *vb, enum v4l2_field field)
610{
611 struct via_camera *cam = q->priv_data;
612
613 vb->size = cam->user_format.sizeimage;
614 vb->width = cam->user_format.width; /* bytesperline???? */
615 vb->height = cam->user_format.height;
616 vb->field = field;
617 if (vb->state == VIDEOBUF_NEEDS_INIT) {
618 int ret = videobuf_iolock(q, vb, NULL);
619 if (ret)
620 return ret;
621 }
622 vb->state = VIDEOBUF_PREPARED;
623 return 0;
624}
625
626/*
627 * We've got a buffer to put data into.
628 *
629 * FIXME: check for a running engine and valid buffers?
630 */
631static void viacam_vb_buf_queue(struct videobuf_queue *q,
632 struct videobuf_buffer *vb)
633{
634 struct via_camera *cam = q->priv_data;
635
636 /*
637 * Note that videobuf holds the lock when it calls
638 * us, so we need not (indeed, cannot) take it here.
639 */
640 vb->state = VIDEOBUF_QUEUED;
641 list_add_tail(&vb->queue, &cam->buffer_queue);
642}
643
644/*
645 * Free a buffer.
646 */
647static void viacam_vb_buf_release(struct videobuf_queue *q,
648 struct videobuf_buffer *vb)
649{
650 struct via_camera *cam = q->priv_data;
651
652 videobuf_dma_unmap(&cam->platdev->dev, videobuf_to_dma(vb));
653 videobuf_dma_free(videobuf_to_dma(vb));
654 vb->state = VIDEOBUF_NEEDS_INIT;
655}
656
657static const struct videobuf_queue_ops viacam_vb_ops = {
658 .buf_setup = viacam_vb_buf_setup,
659 .buf_prepare = viacam_vb_buf_prepare,
660 .buf_queue = viacam_vb_buf_queue,
661 .buf_release = viacam_vb_buf_release,
662};
663
664/* --------------------------------------------------------------------------*/
665/* File operations */
666
667static int viacam_open(struct file *filp)
668{
669 struct via_camera *cam = video_drvdata(filp);
670
671 filp->private_data = cam;
672 /*
673 * Note the new user. If this is the first one, we'll also
674 * need to power up the sensor.
675 */
676 mutex_lock(&cam->lock);
677 if (cam->users == 0) {
678 int ret = viafb_request_dma();
679
680 if (ret) {
681 mutex_unlock(&cam->lock);
682 return ret;
683 }
684 via_sensor_power_up(cam);
685 set_bit(CF_CONFIG_NEEDED, &cam->flags);
686 /*
687 * Hook into videobuf. Evidently this cannot fail.
688 */
689 videobuf_queue_sg_init(&cam->vb_queue, &viacam_vb_ops,
690 &cam->platdev->dev, &cam->viadev->reg_lock,
691 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
692 sizeof(struct videobuf_buffer), cam, NULL);
693 }
694 (cam->users)++;
695 mutex_unlock(&cam->lock);
696 return 0;
697}
698
699static int viacam_release(struct file *filp)
700{
701 struct via_camera *cam = video_drvdata(filp);
702
703 mutex_lock(&cam->lock);
704 (cam->users)--;
705 /*
706 * If the "owner" is closing, shut down any ongoing
707 * operations.
708 */
709 if (filp == cam->owner) {
710 videobuf_stop(&cam->vb_queue);
711 /*
712 * We don't hold the spinlock here, but, if release()
713 * is being called by the owner, nobody else will
714 * be changing the state. And an extra stop would
715 * not hurt anyway.
716 */
717 if (cam->opstate != S_IDLE)
718 viacam_stop_engine(cam);
719 cam->owner = NULL;
720 }
721 /*
722 * Last one out needs to turn out the lights.
723 */
724 if (cam->users == 0) {
725 videobuf_mmap_free(&cam->vb_queue);
726 via_sensor_power_down(cam);
727 viafb_release_dma();
728 }
729 mutex_unlock(&cam->lock);
730 return 0;
731}
732
733/*
734 * Read a frame from the device.
735 */
736static ssize_t viacam_read(struct file *filp, char __user *buffer,
737 size_t len, loff_t *pos)
738{
739 struct via_camera *cam = video_drvdata(filp);
740 int ret;
741
742 mutex_lock(&cam->lock);
743 /*
744 * Enforce the V4l2 "only one owner gets to read data" rule.
745 */
746 if (cam->owner && cam->owner != filp) {
747 ret = -EBUSY;
748 goto out_unlock;
749 }
750 cam->owner = filp;
751 /*
752 * Do we need to configure the hardware?
753 */
754 if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
755 ret = viacam_configure_sensor(cam);
756 if (!ret)
757 ret = viacam_config_controller(cam);
758 if (ret)
759 goto out_unlock;
760 }
761 /*
762 * Fire up the capture engine, then have videobuf do
763 * the heavy lifting. Someday it would be good to avoid
764 * stopping and restarting the engine each time.
765 */
766 INIT_LIST_HEAD(&cam->buffer_queue);
767 viacam_start_engine(cam);
768 ret = videobuf_read_stream(&cam->vb_queue, buffer, len, pos, 0,
769 filp->f_flags & O_NONBLOCK);
770 viacam_stop_engine(cam);
771 /* videobuf_stop() ?? */
772
773out_unlock:
774 mutex_unlock(&cam->lock);
775 return ret;
776}
777
778
779static unsigned int viacam_poll(struct file *filp, struct poll_table_struct *pt)
780{
781 struct via_camera *cam = video_drvdata(filp);
782
783 return videobuf_poll_stream(filp, &cam->vb_queue, pt);
784}
785
786
787static int viacam_mmap(struct file *filp, struct vm_area_struct *vma)
788{
789 struct via_camera *cam = video_drvdata(filp);
790
791 return videobuf_mmap_mapper(&cam->vb_queue, vma);
792}
793
794
795
796static const struct v4l2_file_operations viacam_fops = {
797 .owner = THIS_MODULE,
798 .open = viacam_open,
799 .release = viacam_release,
800 .read = viacam_read,
801 .poll = viacam_poll,
802 .mmap = viacam_mmap,
803 .unlocked_ioctl = video_ioctl2,
804};
805
806/*----------------------------------------------------------------------------*/
807/*
808 * The long list of v4l2 ioctl ops
809 */
810
811static int viacam_g_chip_ident(struct file *file, void *priv,
812 struct v4l2_dbg_chip_ident *ident)
813{
814 struct via_camera *cam = priv;
815
816 ident->ident = V4L2_IDENT_NONE;
817 ident->revision = 0;
818 if (v4l2_chip_match_host(&ident->match)) {
819 ident->ident = V4L2_IDENT_VIA_VX855;
820 return 0;
821 }
822 return sensor_call(cam, core, g_chip_ident, ident);
823}
824
825/*
826 * Control ops are passed through to the sensor.
827 */
828static int viacam_queryctrl(struct file *filp, void *priv,
829 struct v4l2_queryctrl *qc)
830{
831 struct via_camera *cam = priv;
832 int ret;
833
834 mutex_lock(&cam->lock);
835 ret = sensor_call(cam, core, queryctrl, qc);
836 mutex_unlock(&cam->lock);
837 return ret;
838}
839
840
841static int viacam_g_ctrl(struct file *filp, void *priv,
842 struct v4l2_control *ctrl)
843{
844 struct via_camera *cam = priv;
845 int ret;
846
847 mutex_lock(&cam->lock);
848 ret = sensor_call(cam, core, g_ctrl, ctrl);
849 mutex_unlock(&cam->lock);
850 return ret;
851}
852
853
854static int viacam_s_ctrl(struct file *filp, void *priv,
855 struct v4l2_control *ctrl)
856{
857 struct via_camera *cam = priv;
858 int ret;
859
860 mutex_lock(&cam->lock);
861 ret = sensor_call(cam, core, s_ctrl, ctrl);
862 mutex_unlock(&cam->lock);
863 return ret;
864}
865
866/*
867 * Only one input.
868 */
869static int viacam_enum_input(struct file *filp, void *priv,
870 struct v4l2_input *input)
871{
872 if (input->index != 0)
873 return -EINVAL;
874
875 input->type = V4L2_INPUT_TYPE_CAMERA;
876 input->std = V4L2_STD_ALL; /* Not sure what should go here */
877 strcpy(input->name, "Camera");
878 return 0;
879}
880
881static int viacam_g_input(struct file *filp, void *priv, unsigned int *i)
882{
883 *i = 0;
884 return 0;
885}
886
887static int viacam_s_input(struct file *filp, void *priv, unsigned int i)
888{
889 if (i != 0)
890 return -EINVAL;
891 return 0;
892}
893
894static int viacam_s_std(struct file *filp, void *priv, v4l2_std_id *std)
895{
896 return 0;
897}
898
899/*
900 * Video format stuff. Here is our default format until
901 * user space messes with things.
902 */
903static const struct v4l2_pix_format viacam_def_pix_format = {
904 .width = VGA_WIDTH,
905 .height = VGA_HEIGHT,
906 .pixelformat = V4L2_PIX_FMT_YUYV,
907 .field = V4L2_FIELD_NONE,
908 .bytesperline = VGA_WIDTH * 2,
909 .sizeimage = VGA_WIDTH * VGA_HEIGHT * 2,
910};
911
912static const enum v4l2_mbus_pixelcode via_def_mbus_code = V4L2_MBUS_FMT_YUYV8_2X8;
913
914static int viacam_enum_fmt_vid_cap(struct file *filp, void *priv,
915 struct v4l2_fmtdesc *fmt)
916{
917 if (fmt->index >= N_VIA_FMTS)
918 return -EINVAL;
919 strlcpy(fmt->description, via_formats[fmt->index].desc,
920 sizeof(fmt->description));
921 fmt->pixelformat = via_formats[fmt->index].pixelformat;
922 return 0;
923}
924
925/*
926 * Figure out proper image dimensions, but always force the
927 * sensor to VGA.
928 */
929static void viacam_fmt_pre(struct v4l2_pix_format *userfmt,
930 struct v4l2_pix_format *sensorfmt)
931{
932 *sensorfmt = *userfmt;
933 if (userfmt->width < QCIF_WIDTH || userfmt->height < QCIF_HEIGHT) {
934 userfmt->width = QCIF_WIDTH;
935 userfmt->height = QCIF_HEIGHT;
936 }
937 if (userfmt->width > VGA_WIDTH || userfmt->height > VGA_HEIGHT) {
938 userfmt->width = VGA_WIDTH;
939 userfmt->height = VGA_HEIGHT;
940 }
941 sensorfmt->width = VGA_WIDTH;
942 sensorfmt->height = VGA_HEIGHT;
943}
944
945static void viacam_fmt_post(struct v4l2_pix_format *userfmt,
946 struct v4l2_pix_format *sensorfmt)
947{
948 struct via_format *f = via_find_format(userfmt->pixelformat);
949
950 sensorfmt->bytesperline = sensorfmt->width * f->bpp;
951 sensorfmt->sizeimage = sensorfmt->height * sensorfmt->bytesperline;
952 userfmt->pixelformat = sensorfmt->pixelformat;
953 userfmt->field = sensorfmt->field;
954 userfmt->bytesperline = 2 * userfmt->width;
955 userfmt->sizeimage = userfmt->bytesperline * userfmt->height;
956}
957
958
959/*
960 * The real work of figuring out a workable format.
961 */
962static int viacam_do_try_fmt(struct via_camera *cam,
963 struct v4l2_pix_format *upix, struct v4l2_pix_format *spix)
964{
965 int ret;
966 struct v4l2_mbus_framefmt mbus_fmt;
967 struct via_format *f = via_find_format(upix->pixelformat);
968
969 upix->pixelformat = f->pixelformat;
970 viacam_fmt_pre(upix, spix);
971 v4l2_fill_mbus_format(&mbus_fmt, upix, f->mbus_code);
972 ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
973 v4l2_fill_pix_format(spix, &mbus_fmt);
974 viacam_fmt_post(upix, spix);
975 return ret;
976}
977
978
979
980static int viacam_try_fmt_vid_cap(struct file *filp, void *priv,
981 struct v4l2_format *fmt)
982{
983 struct via_camera *cam = priv;
984 struct v4l2_format sfmt;
985 int ret;
986
987 mutex_lock(&cam->lock);
988 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
989 mutex_unlock(&cam->lock);
990 return ret;
991}
992
993
994static int viacam_g_fmt_vid_cap(struct file *filp, void *priv,
995 struct v4l2_format *fmt)
996{
997 struct via_camera *cam = priv;
998
999 mutex_lock(&cam->lock);
1000 fmt->fmt.pix = cam->user_format;
1001 mutex_unlock(&cam->lock);
1002 return 0;
1003}
1004
1005static int viacam_s_fmt_vid_cap(struct file *filp, void *priv,
1006 struct v4l2_format *fmt)
1007{
1008 struct via_camera *cam = priv;
1009 int ret;
1010 struct v4l2_format sfmt;
1011 struct via_format *f = via_find_format(fmt->fmt.pix.pixelformat);
1012
1013 /*
1014 * Camera must be idle or we can't mess with the
1015 * video setup.
1016 */
1017 mutex_lock(&cam->lock);
1018 if (cam->opstate != S_IDLE) {
1019 ret = -EBUSY;
1020 goto out;
1021 }
1022 /*
1023 * Let the sensor code look over and tweak the
1024 * requested formatting.
1025 */
1026 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
1027 if (ret)
1028 goto out;
1029 /*
1030 * OK, let's commit to the new format.
1031 */
1032 cam->user_format = fmt->fmt.pix;
1033 cam->sensor_format = sfmt.fmt.pix;
1034 cam->mbus_code = f->mbus_code;
1035 ret = viacam_configure_sensor(cam);
1036 if (!ret)
1037 ret = viacam_config_controller(cam);
1038out:
1039 mutex_unlock(&cam->lock);
1040 return ret;
1041}
1042
1043static int viacam_querycap(struct file *filp, void *priv,
1044 struct v4l2_capability *cap)
1045{
1046 strcpy(cap->driver, "via-camera");
1047 strcpy(cap->card, "via-camera");
1048 cap->version = 1;
1049 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1050 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1051 return 0;
1052}
1053
1054/*
1055 * Streaming operations - pure videobuf stuff.
1056 */
1057static int viacam_reqbufs(struct file *filp, void *priv,
1058 struct v4l2_requestbuffers *rb)
1059{
1060 struct via_camera *cam = priv;
1061
1062 return videobuf_reqbufs(&cam->vb_queue, rb);
1063}
1064
1065static int viacam_querybuf(struct file *filp, void *priv,
1066 struct v4l2_buffer *buf)
1067{
1068 struct via_camera *cam = priv;
1069
1070 return videobuf_querybuf(&cam->vb_queue, buf);
1071}
1072
1073static int viacam_qbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1074{
1075 struct via_camera *cam = priv;
1076
1077 return videobuf_qbuf(&cam->vb_queue, buf);
1078}
1079
1080static int viacam_dqbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1081{
1082 struct via_camera *cam = priv;
1083
1084 return videobuf_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
1085}
1086
1087static int viacam_streamon(struct file *filp, void *priv, enum v4l2_buf_type t)
1088{
1089 struct via_camera *cam = priv;
1090 int ret = 0;
1091
1092 if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1093 return -EINVAL;
1094
1095 mutex_lock(&cam->lock);
1096 if (cam->opstate != S_IDLE) {
1097 ret = -EBUSY;
1098 goto out;
1099 }
1100 /*
1101 * Enforce the V4l2 "only one owner gets to read data" rule.
1102 */
1103 if (cam->owner && cam->owner != filp) {
1104 ret = -EBUSY;
1105 goto out;
1106 }
1107 cam->owner = filp;
1108 /*
1109 * Configure things if need be.
1110 */
1111 if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
1112 ret = viacam_configure_sensor(cam);
1113 if (ret)
1114 goto out;
1115 ret = viacam_config_controller(cam);
1116 if (ret)
1117 goto out;
1118 }
1119 /*
1120 * If the CPU goes into C3, the DMA transfer gets corrupted and
1121 * users start filing unsightly bug reports. Put in a "latency"
1122 * requirement which will keep the CPU out of the deeper sleep
1123 * states.
1124 */
1125 pm_qos_add_request(&cam->qos_request, PM_QOS_CPU_DMA_LATENCY, 50);
1126 /*
1127 * Fire things up.
1128 */
1129 INIT_LIST_HEAD(&cam->buffer_queue);
1130 ret = videobuf_streamon(&cam->vb_queue);
1131 if (!ret)
1132 viacam_start_engine(cam);
1133out:
1134 mutex_unlock(&cam->lock);
1135 return ret;
1136}
1137
1138static int viacam_streamoff(struct file *filp, void *priv, enum v4l2_buf_type t)
1139{
1140 struct via_camera *cam = priv;
1141 int ret;
1142
1143 if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1144 return -EINVAL;
1145 mutex_lock(&cam->lock);
1146 if (cam->opstate != S_RUNNING) {
1147 ret = -EINVAL;
1148 goto out;
1149 }
1150 pm_qos_remove_request(&cam->qos_request);
1151 viacam_stop_engine(cam);
1152 /*
1153 * Videobuf will recycle all of the outstanding buffers, but
1154 * we should be sure we don't retain any references to
1155 * any of them.
1156 */
1157 ret = videobuf_streamoff(&cam->vb_queue);
1158 INIT_LIST_HEAD(&cam->buffer_queue);
1159out:
1160 mutex_unlock(&cam->lock);
1161 return ret;
1162}
1163
1164#ifdef CONFIG_VIDEO_V4L1_COMPAT
1165static int viacam_vidiocgmbuf(struct file *filp, void *priv,
1166 struct video_mbuf *mbuf)
1167{
1168 struct via_camera *cam = priv;
1169
1170 return videobuf_cgmbuf(&cam->vb_queue, mbuf, 6);
1171}
1172#endif
1173
1174/* G/S_PARM */
1175
1176static int viacam_g_parm(struct file *filp, void *priv,
1177 struct v4l2_streamparm *parm)
1178{
1179 struct via_camera *cam = priv;
1180 int ret;
1181
1182 mutex_lock(&cam->lock);
1183 ret = sensor_call(cam, video, g_parm, parm);
1184 mutex_unlock(&cam->lock);
1185 parm->parm.capture.readbuffers = cam->n_cap_bufs;
1186 return ret;
1187}
1188
1189static int viacam_s_parm(struct file *filp, void *priv,
1190 struct v4l2_streamparm *parm)
1191{
1192 struct via_camera *cam = priv;
1193 int ret;
1194
1195 mutex_lock(&cam->lock);
1196 ret = sensor_call(cam, video, s_parm, parm);
1197 mutex_unlock(&cam->lock);
1198 parm->parm.capture.readbuffers = cam->n_cap_bufs;
1199 return ret;
1200}
1201
1202static int viacam_enum_framesizes(struct file *filp, void *priv,
1203 struct v4l2_frmsizeenum *sizes)
1204{
1205 if (sizes->index != 0)
1206 return -EINVAL;
1207 sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1208 sizes->stepwise.min_width = QCIF_WIDTH;
1209 sizes->stepwise.min_height = QCIF_HEIGHT;
1210 sizes->stepwise.max_width = VGA_WIDTH;
1211 sizes->stepwise.max_height = VGA_HEIGHT;
1212 sizes->stepwise.step_width = sizes->stepwise.step_height = 1;
1213 return 0;
1214}
1215
1216static int viacam_enum_frameintervals(struct file *filp, void *priv,
1217 struct v4l2_frmivalenum *interval)
1218{
1219 struct via_camera *cam = priv;
1220 int ret;
1221
1222 mutex_lock(&cam->lock);
1223 ret = sensor_call(cam, video, enum_frameintervals, interval);
1224 mutex_unlock(&cam->lock);
1225 return ret;
1226}
1227
1228
1229
1230static const struct v4l2_ioctl_ops viacam_ioctl_ops = {
1231 .vidioc_g_chip_ident = viacam_g_chip_ident,
1232 .vidioc_queryctrl = viacam_queryctrl,
1233 .vidioc_g_ctrl = viacam_g_ctrl,
1234 .vidioc_s_ctrl = viacam_s_ctrl,
1235 .vidioc_enum_input = viacam_enum_input,
1236 .vidioc_g_input = viacam_g_input,
1237 .vidioc_s_input = viacam_s_input,
1238 .vidioc_s_std = viacam_s_std,
1239 .vidioc_enum_fmt_vid_cap = viacam_enum_fmt_vid_cap,
1240 .vidioc_try_fmt_vid_cap = viacam_try_fmt_vid_cap,
1241 .vidioc_g_fmt_vid_cap = viacam_g_fmt_vid_cap,
1242 .vidioc_s_fmt_vid_cap = viacam_s_fmt_vid_cap,
1243 .vidioc_querycap = viacam_querycap,
1244 .vidioc_reqbufs = viacam_reqbufs,
1245 .vidioc_querybuf = viacam_querybuf,
1246 .vidioc_qbuf = viacam_qbuf,
1247 .vidioc_dqbuf = viacam_dqbuf,
1248 .vidioc_streamon = viacam_streamon,
1249 .vidioc_streamoff = viacam_streamoff,
1250 .vidioc_g_parm = viacam_g_parm,
1251 .vidioc_s_parm = viacam_s_parm,
1252 .vidioc_enum_framesizes = viacam_enum_framesizes,
1253 .vidioc_enum_frameintervals = viacam_enum_frameintervals,
1254#ifdef CONFIG_VIDEO_V4L1_COMPAT
1255 .vidiocgmbuf = viacam_vidiocgmbuf,
1256#endif
1257};
1258
1259/*----------------------------------------------------------------------------*/
1260
1261/*
1262 * Power management.
1263 */
1264
1265/*
1266 * Setup stuff.
1267 */
1268
1269static struct video_device viacam_v4l_template = {
1270 .name = "via-camera",
1271 .minor = -1,
1272 .tvnorms = V4L2_STD_NTSC_M,
1273 .current_norm = V4L2_STD_NTSC_M,
1274 .fops = &viacam_fops,
1275 .ioctl_ops = &viacam_ioctl_ops,
1276 .release = video_device_release_empty, /* Check this */
1277};
1278
1279
1280static __devinit int viacam_probe(struct platform_device *pdev)
1281{
1282 int ret;
1283 struct i2c_adapter *sensor_adapter;
1284 struct viafb_dev *viadev = pdev->dev.platform_data;
1285
1286 /*
1287 * Note that there are actually two capture channels on
1288 * the device. We only deal with one for now. That
1289 * is encoded here; nothing else assumes it's dealing with
1290 * a unique capture device.
1291 */
1292 struct via_camera *cam;
1293
1294 /*
1295 * Ensure that frame buffer memory has been set aside for
1296 * this purpose. As an arbitrary limit, refuse to work
1297 * with less than two frames of VGA 16-bit data.
1298 *
1299 * If we ever support the second port, we'll need to set
1300 * aside more memory.
1301 */
1302 if (viadev->camera_fbmem_size < (VGA_HEIGHT*VGA_WIDTH*4)) {
1303 printk(KERN_ERR "viacam: insufficient FB memory reserved\n");
1304 return -ENOMEM;
1305 }
1306 if (viadev->engine_mmio == NULL) {
1307 printk(KERN_ERR "viacam: No I/O memory, so no pictures\n");
1308 return -ENOMEM;
1309 }
1310 /*
1311 * Basic structure initialization.
1312 */
1313 cam = kzalloc (sizeof(struct via_camera), GFP_KERNEL);
1314 if (cam == NULL)
1315 return -ENOMEM;
1316 via_cam_info = cam;
1317 cam->platdev = pdev;
1318 cam->viadev = viadev;
1319 cam->users = 0;
1320 cam->owner = NULL;
1321 cam->opstate = S_IDLE;
1322 cam->user_format = cam->sensor_format = viacam_def_pix_format;
1323 mutex_init(&cam->lock);
1324 INIT_LIST_HEAD(&cam->buffer_queue);
1325 cam->mmio = viadev->engine_mmio;
1326 cam->fbmem = viadev->fbmem;
1327 cam->fb_offset = viadev->camera_fbmem_offset;
1328 cam->flags = 1 << CF_CONFIG_NEEDED;
1329 cam->mbus_code = via_def_mbus_code;
1330 /*
1331 * Tell V4L that we exist.
1332 */
1333 ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
1334 if (ret) {
1335 dev_err(&pdev->dev, "Unable to register v4l2 device\n");
1336 return ret;
1337 }
1338 /*
1339 * Convince the system that we can do DMA.
1340 */
1341 pdev->dev.dma_mask = &viadev->pdev->dma_mask;
1342 dma_set_mask(&pdev->dev, 0xffffffff);
1343 /*
1344 * Fire up the capture port. The write to 0x78 looks purely
1345 * OLPCish; any system will need to tweak 0x1e.
1346 */
1347 via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1348 via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1349 /*
1350 * Get the sensor powered up.
1351 */
1352 ret = via_sensor_power_setup(cam);
1353 if (ret)
1354 goto out_unregister;
1355 via_sensor_power_up(cam);
1356
1357 /*
1358 * See if we can't find it on the bus. The VIA_PORT_31 assumption
1359 * is OLPC-specific. 0x42 assumption is ov7670-specific.
1360 */
1361 sensor_adapter = viafb_find_i2c_adapter(VIA_PORT_31);
1362 cam->sensor = v4l2_i2c_new_subdev(&cam->v4l2_dev, sensor_adapter,
1363 "ov7670", "ov7670", 0x42 >> 1, NULL);
1364 if (cam->sensor == NULL) {
1365 dev_err(&pdev->dev, "Unable to find the sensor!\n");
1366 ret = -ENODEV;
1367 goto out_power_down;
1368 }
1369 /*
1370 * Get the IRQ.
1371 */
1372 viacam_int_disable(cam);
1373 ret = request_threaded_irq(viadev->pdev->irq, viacam_quick_irq,
1374 viacam_irq, IRQF_SHARED, "via-camera", cam);
1375 if (ret)
1376 goto out_power_down;
1377 /*
1378 * Tell V4l2 that we exist.
1379 */
1380 cam->vdev = viacam_v4l_template;
1381 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1382 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1383 if (ret)
1384 goto out_irq;
1385 video_set_drvdata(&cam->vdev, cam);
1386
1387 /* Power the sensor down until somebody opens the device */
1388 via_sensor_power_down(cam);
1389 return 0;
1390
1391out_irq:
1392 free_irq(viadev->pdev->irq, cam);
1393out_power_down:
1394 via_sensor_power_release(cam);
1395out_unregister:
1396 v4l2_device_unregister(&cam->v4l2_dev);
1397 return ret;
1398}
1399
1400static __devexit int viacam_remove(struct platform_device *pdev)
1401{
1402 struct via_camera *cam = via_cam_info;
1403 struct viafb_dev *viadev = pdev->dev.platform_data;
1404
1405 video_unregister_device(&cam->vdev);
1406 v4l2_device_unregister(&cam->v4l2_dev);
1407 free_irq(viadev->pdev->irq, cam);
1408 via_sensor_power_release(cam);
1409 via_cam_info = NULL;
1410 return 0;
1411}
1412
1413
1414static struct platform_driver viacam_driver = {
1415 .driver = {
1416 .name = "viafb-camera",
1417 },
1418 .probe = viacam_probe,
1419 .remove = viacam_remove,
1420};
1421
1422
1423#ifdef CONFIG_OLPC_XO_1_5
1424/*
1425 * The OLPC folks put the serial port on the same pin as
1426 * the camera. They also get grumpy if we break the
1427 * serial port and keep them from using it. So we have
1428 * to check the serial enable bit and not step on it.
1429 */
1430#define VIACAM_SERIAL_DEVFN 0x88
1431#define VIACAM_SERIAL_CREG 0x46
1432#define VIACAM_SERIAL_BIT 0x40
1433
1434static __devinit int viacam_check_serial_port(void)
1435{
1436 struct pci_bus *pbus = pci_find_bus(0, 0);
1437 u8 cbyte;
1438
1439 pci_bus_read_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1440 VIACAM_SERIAL_CREG, &cbyte);
1441 if ((cbyte & VIACAM_SERIAL_BIT) == 0)
1442 return 0; /* Not enabled */
1443 if (override_serial == 0) {
1444 printk(KERN_NOTICE "Via camera: serial port is enabled, " \
1445 "refusing to load.\n");
1446 printk(KERN_NOTICE "Specify override_serial=1 to force " \
1447 "module loading.\n");
1448 return -EBUSY;
1449 }
1450 printk(KERN_NOTICE "Via camera: overriding serial port\n");
1451 pci_bus_write_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1452 VIACAM_SERIAL_CREG, cbyte & ~VIACAM_SERIAL_BIT);
1453 return 0;
1454}
1455#endif
1456
1457
1458
1459
1460static int viacam_init(void)
1461{
1462#ifdef CONFIG_OLPC_XO_1_5
1463 if (viacam_check_serial_port())
1464 return -EBUSY;
1465#endif
1466 return platform_driver_register(&viacam_driver);
1467}
1468module_init(viacam_init);
1469
1470static void viacam_exit(void)
1471{
1472 platform_driver_unregister(&viacam_driver);
1473}
1474module_exit(viacam_exit);
diff --git a/drivers/media/video/via-camera.h b/drivers/media/video/via-camera.h
new file mode 100644
index 000000000000..b12a4b3d616f
--- /dev/null
+++ b/drivers/media/video/via-camera.h
@@ -0,0 +1,93 @@
1/*
2 * VIA Camera register definitions.
3 */
4#define VCR_INTCTRL 0x300 /* Capture interrupt control */
5#define VCR_IC_EAV 0x0001 /* End of active video status */
6#define VCR_IC_EVBI 0x0002 /* End of VBI status */
7#define VCR_IC_FBOTFLD 0x0004 /* "flipping" Bottom field is active */
8#define VCR_IC_ACTBUF 0x0018 /* Active video buffer */
9#define VCR_IC_VSYNC 0x0020 /* 0 = VB, 1 = active video */
10#define VCR_IC_BOTFLD 0x0040 /* Bottom field is active */
11#define VCR_IC_FFULL 0x0080 /* FIFO full */
12#define VCR_IC_INTEN 0x0100 /* End of active video int. enable */
13#define VCR_IC_VBIINT 0x0200 /* End of VBI int enable */
14#define VCR_IC_VBIBUF 0x0400 /* Current VBI buffer */
15
16#define VCR_TSC 0x308 /* Transport stream control */
17#define VCR_TSC_ENABLE 0x000001 /* Transport stream input enable */
18#define VCR_TSC_DROPERR 0x000002 /* Drop error packets */
19#define VCR_TSC_METHOD 0x00000c /* DMA method (non-functional) */
20#define VCR_TSC_COUNT 0x07fff0 /* KByte or packet count */
21#define VCR_TSC_CBMODE 0x080000 /* Change buffer by byte count */
22#define VCR_TSC_PSSIG 0x100000 /* Packet starting signal disable */
23#define VCR_TSC_BE 0x200000 /* MSB first (serial mode) */
24#define VCR_TSC_SERIAL 0x400000 /* Serial input (0 = parallel) */
25
26#define VCR_CAPINTC 0x310 /* Capture interface control */
27#define VCR_CI_ENABLE 0x00000001 /* Capture enable */
28#define VCR_CI_BSS 0x00000002 /* WTF "bit stream selection" */
29#define VCR_CI_3BUFS 0x00000004 /* 1 = 3 buffers, 0 = 2 buffers */
30#define VCR_CI_VIPEN 0x00000008 /* VIP enable */
31#define VCR_CI_CCIR601_8 0 /* CCIR601 input stream, 8 bit */
32#define VCR_CI_CCIR656_8 0x00000010 /* ... CCIR656, 8 bit */
33#define VCR_CI_CCIR601_16 0x00000020 /* ... CCIR601, 16 bit */
34#define VCR_CI_CCIR656_16 0x00000030 /* ... CCIR656, 16 bit */
35#define VCR_CI_HDMODE 0x00000040 /* CCIR656-16 hdr decode mode; 1=16b */
36#define VCR_CI_BSWAP 0x00000080 /* Swap bytes (16-bit) */
37#define VCR_CI_YUYV 0 /* Byte order 0123 */
38#define VCR_CI_UYVY 0x00000100 /* Byte order 1032 */
39#define VCR_CI_YVYU 0x00000200 /* Byte order 0321 */
40#define VCR_CI_VYUY 0x00000300 /* Byte order 3012 */
41#define VCR_CI_VIPTYPE 0x00000400 /* VIP type */
42#define VCR_CI_IFSEN 0x00000800 /* Input field signal enable */
43#define VCR_CI_DIODD 0 /* De-interlace odd, 30fps */
44#define VCR_CI_DIEVEN 0x00001000 /* ...even field, 30fps */
45#define VCR_CI_DIBOTH 0x00002000 /* ...both fields, 60fps */
46#define VCR_CI_DIBOTH30 0x00003000 /* ...both fields, 30fps interlace */
47#define VCR_CI_CONVTYPE 0x00004000 /* 4:2:2 to 4:4:4; 1 = interpolate */
48#define VCR_CI_CFC 0x00008000 /* Capture flipping control */
49#define VCR_CI_FILTER 0x00070000 /* Horiz filter mode select
50 000 = none
51 001 = 2 tap
52 010 = 3 tap
53 011 = 4 tap
54 100 = 5 tap */
55#define VCR_CI_CLKINV 0x00080000 /* Input CLK inverted */
56#define VCR_CI_VREFINV 0x00100000 /* VREF inverted */
57#define VCR_CI_HREFINV 0x00200000 /* HREF inverted */
58#define VCR_CI_FLDINV 0x00400000 /* Field inverted */
59#define VCR_CI_CLKPIN 0x00800000 /* Capture clock pin */
60#define VCR_CI_THRESH 0x0f000000 /* Capture fifo threshold */
61#define VCR_CI_HRLE 0x10000000 /* Positive edge of HREF */
62#define VCR_CI_VRLE 0x20000000 /* Positive edge of VREF */
63#define VCR_CI_OFLDINV 0x40000000 /* Field output inverted */
64#define VCR_CI_CLKEN 0x80000000 /* Capture clock enable */
65
66#define VCR_HORRANGE 0x314 /* Active video horizontal range */
67#define VCR_VERTRANGE 0x318 /* Active video vertical range */
68#define VCR_AVSCALE 0x31c /* Active video scaling control */
69#define VCR_AVS_HEN 0x00000800 /* Horizontal scale enable */
70#define VCR_AVS_VEN 0x04000000 /* Vertical enable */
71#define VCR_VBIHOR 0x320 /* VBI Data horizontal range */
72#define VCR_VBIVERT 0x324 /* VBI data vertical range */
73#define VCR_VBIBUF1 0x328 /* First VBI buffer */
74#define VCR_VBISTRIDE 0x32c /* VBI stride */
75#define VCR_ANCDATACNT 0x330 /* Ancillary data count setting */
76#define VCR_MAXDATA 0x334 /* Active data count of active video */
77#define VCR_MAXVBI 0x338 /* Maximum data count of VBI */
78#define VCR_CAPDATA 0x33c /* Capture data count */
79#define VCR_VBUF1 0x340 /* First video buffer */
80#define VCR_VBUF2 0x344 /* Second video buffer */
81#define VCR_VBUF3 0x348 /* Third video buffer */
82#define VCR_VBUF_MASK 0x1ffffff0 /* Bits 28:4 */
83#define VCR_VBIBUF2 0x34c /* Second VBI buffer */
84#define VCR_VSTRIDE 0x350 /* Stride of video + coring control */
85#define VCR_VS_STRIDE_SHIFT 4
86#define VCR_VS_STRIDE 0x00001ff0 /* Stride (8-byte units) */
87#define VCR_VS_CCD 0x007f0000 /* Coring compare data */
88#define VCR_VS_COREEN 0x00800000 /* Coring enable */
89#define VCR_TS0ERR 0x354 /* TS buffer 0 error indicator */
90#define VCR_TS1ERR 0x358 /* TS buffer 0 error indicator */
91#define VCR_TS2ERR 0x35c /* TS buffer 0 error indicator */
92
93/* Add 0x1000 for the second capture engine registers */