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