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