1
2
3
4
5
6
7
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/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>
21#include <media/videobuf-dma-sg.h>
22#include <linux/delay.h>
23#include <linux/dma-mapping.h>
24#include <linux/pm_qos_params.h>
25#include <linux/via-core.h>
26#include <linux/via-gpio.h>
27#include <linux/via_i2c.h>
28#include <asm/olpc.h>
29
30#include "via-camera.h"
31
32MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
33MODULE_DESCRIPTION("VIA framebuffer-based camera controller driver");
34MODULE_LICENSE("GPL");
35
36static int flip_image;
37module_param(flip_image, bool, 0444);
38MODULE_PARM_DESC(flip_image,
39 "If set, the sensor will be instructed to flip the image "
40 "vertically.");
41
42static int override_serial;
43module_param(override_serial, bool, 0444);
44MODULE_PARM_DESC(override_serial,
45 "The camera driver will normally refuse to load if "
46 "the XO 1.5 serial port is enabled. Set this option "
47 "to force-enable the camera.");
48
49
50
51
52#define VGA_WIDTH 640
53#define VGA_HEIGHT 480
54#define QCIF_WIDTH 176
55#define QCIF_HEIGHT 144
56
57
58
59
60enum viacam_opstate { S_IDLE = 0, S_RUNNING = 1 };
61
62struct via_camera {
63 struct v4l2_device v4l2_dev;
64 struct video_device vdev;
65 struct v4l2_subdev *sensor;
66 struct platform_device *platdev;
67 struct viafb_dev *viadev;
68 struct mutex lock;
69 enum viacam_opstate opstate;
70 unsigned long flags;
71 struct pm_qos_request_list qos_request;
72
73
74
75 int power_gpio;
76 int reset_gpio;
77
78
79
80 void __iomem *mmio;
81 void __iomem *fbmem;
82 u32 fb_offset;
83
84
85
86
87
88 unsigned int cb_offsets[3];
89 u8 *cb_addrs[3];
90 int n_cap_bufs;
91 int next_buf;
92 struct videobuf_queue vb_queue;
93 struct list_head buffer_queue;
94
95
96
97 int users;
98 struct file *owner;
99
100
101
102
103
104
105
106 struct v4l2_pix_format sensor_format;
107 struct v4l2_pix_format user_format;
108 enum v4l2_mbus_pixelcode mbus_code;
109};
110
111
112
113
114
115static struct via_camera *via_cam_info;
116
117
118
119
120#define CF_DMA_ACTIVE 0
121#define CF_CONFIG_NEEDED 1
122
123
124
125
126
127#define sensor_call(cam, optype, func, args...) \
128 v4l2_subdev_call(cam->sensor, optype, func, ##args)
129
130
131
132
133#define cam_err(cam, fmt, arg...) \
134 dev_err(&(cam)->platdev->dev, fmt, ##arg);
135#define cam_warn(cam, fmt, arg...) \
136 dev_warn(&(cam)->platdev->dev, fmt, ##arg);
137#define cam_dbg(cam, fmt, arg...) \
138 dev_dbg(&(cam)->platdev->dev, fmt, ##arg);
139
140
141
142
143
144
145
146static struct via_format {
147 __u8 *desc;
148 __u32 pixelformat;
149 int bpp;
150 enum v4l2_mbus_pixelcode mbus_code;
151} via_formats[] = {
152 {
153 .desc = "YUYV 4:2:2",
154 .pixelformat = V4L2_PIX_FMT_YUYV,
155 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
156 .bpp = 2,
157 },
158 {
159 .desc = "RGB 565",
160 .pixelformat = V4L2_PIX_FMT_RGB565,
161 .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE,
162 .bpp = 2,
163 },
164
165
166};
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
177 return via_formats;
178}
179
180
181
182
183
184
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
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);
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
240
241
242
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
256
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
269
270 if (ret == 0)
271 ret = viacam_set_flip(cam);
272 return ret;
273}
274
275
276
277
278
279
280
281
282
283
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
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
317
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
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)) {
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
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
367
368
369 vb = viacam_next_buffer(cam);
370 if (vb == NULL)
371 goto done;
372
373
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
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
394
395
396
397
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
416
417
418
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
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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
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
491
492static void viacam_ctlr_image(struct via_camera *cam)
493{
494 int cicreg;
495
496
497
498
499
500 viacam_write_reg(cam, VCR_CAPINTC, ~(VCR_CI_ENABLE|VCR_CI_CLKEN));
501
502
503
504
505 viacam_write_reg(cam, VCR_HORRANGE, 0x06200120);
506 viacam_write_reg(cam, VCR_VERTRANGE, 0x01de0000);
507 viacam_set_scale(cam);
508
509
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
519
520
521
522
523
524 cicreg = VCR_CI_CLKEN |
525 0x08000000 |
526 VCR_CI_FLDINV |
527 VCR_CI_VREFINV |
528 VCR_CI_DIBOTH |
529 VCR_CI_CCIR601_8;
530 if (cam->n_cap_bufs == 3)
531 cicreg |= VCR_CI_3BUFS;
532
533
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
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);
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);
578 cam->opstate = S_IDLE;
579 spin_unlock_irq(&cam->viadev->reg_lock);
580}
581
582
583
584
585
586
587
588
589
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)
598 *count = 6;
599 return 0;
600}
601
602
603
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;
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
625
626
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
635
636
637 vb->state = VIDEOBUF_QUEUED;
638 list_add_tail(&vb->queue, &cam->buffer_queue);
639}
640
641
642
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
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
671
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
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
704
705
706 if (filp == cam->owner) {
707 videobuf_stop(&cam->vb_queue);
708
709
710
711
712
713
714 if (cam->opstate != S_IDLE)
715 viacam_stop_engine(cam);
716 cam->owner = NULL;
717 }
718
719
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
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
742
743 if (cam->owner && cam->owner != filp) {
744 ret = -EBUSY;
745 goto out_unlock;
746 }
747 cam->owner = filp;
748
749
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
760
761
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
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
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
823
824
825static int viacam_queryctrl(struct file *filp, void *priv,
826 struct v4l2_queryctrl *qc)
827{
828 struct via_camera *cam = priv;
829 int ret;
830
831 mutex_lock(&cam->lock);
832 ret = sensor_call(cam, core, queryctrl, qc);
833 mutex_unlock(&cam->lock);
834 return ret;
835}
836
837
838static int viacam_g_ctrl(struct file *filp, void *priv,
839 struct v4l2_control *ctrl)
840{
841 struct via_camera *cam = priv;
842 int ret;
843
844 mutex_lock(&cam->lock);
845 ret = sensor_call(cam, core, g_ctrl, ctrl);
846 mutex_unlock(&cam->lock);
847 return ret;
848}
849
850
851static int viacam_s_ctrl(struct file *filp, void *priv,
852 struct v4l2_control *ctrl)
853{
854 struct via_camera *cam = priv;
855 int ret;
856
857 mutex_lock(&cam->lock);
858 ret = sensor_call(cam, core, s_ctrl, ctrl);
859 mutex_unlock(&cam->lock);
860 return ret;
861}
862
863
864
865
866static int viacam_enum_input(struct file *filp, void *priv,
867 struct v4l2_input *input)
868{
869 if (input->index != 0)
870 return -EINVAL;
871
872 input->type = V4L2_INPUT_TYPE_CAMERA;
873 input->std = V4L2_STD_ALL;
874 strcpy(input->name, "Camera");
875 return 0;
876}
877
878static int viacam_g_input(struct file *filp, void *priv, unsigned int *i)
879{
880 *i = 0;
881 return 0;
882}
883
884static int viacam_s_input(struct file *filp, void *priv, unsigned int i)
885{
886 if (i != 0)
887 return -EINVAL;
888 return 0;
889}
890
891static int viacam_s_std(struct file *filp, void *priv, v4l2_std_id *std)
892{
893 return 0;
894}
895
896
897
898
899
900static const struct v4l2_pix_format viacam_def_pix_format = {
901 .width = VGA_WIDTH,
902 .height = VGA_HEIGHT,
903 .pixelformat = V4L2_PIX_FMT_YUYV,
904 .field = V4L2_FIELD_NONE,
905 .bytesperline = VGA_WIDTH * 2,
906 .sizeimage = VGA_WIDTH * VGA_HEIGHT * 2,
907};
908
909static const enum v4l2_mbus_pixelcode via_def_mbus_code = V4L2_MBUS_FMT_YUYV8_2X8;
910
911static int viacam_enum_fmt_vid_cap(struct file *filp, void *priv,
912 struct v4l2_fmtdesc *fmt)
913{
914 if (fmt->index >= N_VIA_FMTS)
915 return -EINVAL;
916 strlcpy(fmt->description, via_formats[fmt->index].desc,
917 sizeof(fmt->description));
918 fmt->pixelformat = via_formats[fmt->index].pixelformat;
919 return 0;
920}
921
922
923
924
925
926static void viacam_fmt_pre(struct v4l2_pix_format *userfmt,
927 struct v4l2_pix_format *sensorfmt)
928{
929 *sensorfmt = *userfmt;
930 if (userfmt->width < QCIF_WIDTH || userfmt->height < QCIF_HEIGHT) {
931 userfmt->width = QCIF_WIDTH;
932 userfmt->height = QCIF_HEIGHT;
933 }
934 if (userfmt->width > VGA_WIDTH || userfmt->height > VGA_HEIGHT) {
935 userfmt->width = VGA_WIDTH;
936 userfmt->height = VGA_HEIGHT;
937 }
938 sensorfmt->width = VGA_WIDTH;
939 sensorfmt->height = VGA_HEIGHT;
940}
941
942static void viacam_fmt_post(struct v4l2_pix_format *userfmt,
943 struct v4l2_pix_format *sensorfmt)
944{
945 struct via_format *f = via_find_format(userfmt->pixelformat);
946
947 sensorfmt->bytesperline = sensorfmt->width * f->bpp;
948 sensorfmt->sizeimage = sensorfmt->height * sensorfmt->bytesperline;
949 userfmt->pixelformat = sensorfmt->pixelformat;
950 userfmt->field = sensorfmt->field;
951 userfmt->bytesperline = 2 * userfmt->width;
952 userfmt->sizeimage = userfmt->bytesperline * userfmt->height;
953}
954
955
956
957
958
959static int viacam_do_try_fmt(struct via_camera *cam,
960 struct v4l2_pix_format *upix, struct v4l2_pix_format *spix)
961{
962 int ret;
963 struct v4l2_mbus_framefmt mbus_fmt;
964 struct via_format *f = via_find_format(upix->pixelformat);
965
966 upix->pixelformat = f->pixelformat;
967 viacam_fmt_pre(upix, spix);
968 v4l2_fill_mbus_format(&mbus_fmt, upix, f->mbus_code);
969 ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
970 v4l2_fill_pix_format(spix, &mbus_fmt);
971 viacam_fmt_post(upix, spix);
972 return ret;
973}
974
975
976
977static int viacam_try_fmt_vid_cap(struct file *filp, void *priv,
978 struct v4l2_format *fmt)
979{
980 struct via_camera *cam = priv;
981 struct v4l2_format sfmt;
982 int ret;
983
984 mutex_lock(&cam->lock);
985 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
986 mutex_unlock(&cam->lock);
987 return ret;
988}
989
990
991static int viacam_g_fmt_vid_cap(struct file *filp, void *priv,
992 struct v4l2_format *fmt)
993{
994 struct via_camera *cam = priv;
995
996 mutex_lock(&cam->lock);
997 fmt->fmt.pix = cam->user_format;
998 mutex_unlock(&cam->lock);
999 return 0;
1000}
1001
1002static int viacam_s_fmt_vid_cap(struct file *filp, void *priv,
1003 struct v4l2_format *fmt)
1004{
1005 struct via_camera *cam = priv;
1006 int ret;
1007 struct v4l2_format sfmt;
1008 struct via_format *f = via_find_format(fmt->fmt.pix.pixelformat);
1009
1010
1011
1012
1013
1014 mutex_lock(&cam->lock);
1015 if (cam->opstate != S_IDLE) {
1016 ret = -EBUSY;
1017 goto out;
1018 }
1019
1020
1021
1022
1023 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
1024 if (ret)
1025 goto out;
1026
1027
1028
1029 cam->user_format = fmt->fmt.pix;
1030 cam->sensor_format = sfmt.fmt.pix;
1031 cam->mbus_code = f->mbus_code;
1032 ret = viacam_configure_sensor(cam);
1033 if (!ret)
1034 ret = viacam_config_controller(cam);
1035out:
1036 mutex_unlock(&cam->lock);
1037 return ret;
1038}
1039
1040static int viacam_querycap(struct file *filp, void *priv,
1041 struct v4l2_capability *cap)
1042{
1043 strcpy(cap->driver, "via-camera");
1044 strcpy(cap->card, "via-camera");
1045 cap->version = 1;
1046 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1047 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1048 return 0;
1049}
1050
1051
1052
1053
1054static int viacam_reqbufs(struct file *filp, void *priv,
1055 struct v4l2_requestbuffers *rb)
1056{
1057 struct via_camera *cam = priv;
1058
1059 return videobuf_reqbufs(&cam->vb_queue, rb);
1060}
1061
1062static int viacam_querybuf(struct file *filp, void *priv,
1063 struct v4l2_buffer *buf)
1064{
1065 struct via_camera *cam = priv;
1066
1067 return videobuf_querybuf(&cam->vb_queue, buf);
1068}
1069
1070static int viacam_qbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1071{
1072 struct via_camera *cam = priv;
1073
1074 return videobuf_qbuf(&cam->vb_queue, buf);
1075}
1076
1077static int viacam_dqbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1078{
1079 struct via_camera *cam = priv;
1080
1081 return videobuf_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
1082}
1083
1084static int viacam_streamon(struct file *filp, void *priv, enum v4l2_buf_type t)
1085{
1086 struct via_camera *cam = priv;
1087 int ret = 0;
1088
1089 if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1090 return -EINVAL;
1091
1092 mutex_lock(&cam->lock);
1093 if (cam->opstate != S_IDLE) {
1094 ret = -EBUSY;
1095 goto out;
1096 }
1097
1098
1099
1100 if (cam->owner && cam->owner != filp) {
1101 ret = -EBUSY;
1102 goto out;
1103 }
1104 cam->owner = filp;
1105
1106
1107
1108 if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
1109 ret = viacam_configure_sensor(cam);
1110 if (ret)
1111 goto out;
1112 ret = viacam_config_controller(cam);
1113 if (ret)
1114 goto out;
1115 }
1116
1117
1118
1119
1120
1121
1122 pm_qos_add_request(&cam->qos_request, PM_QOS_CPU_DMA_LATENCY, 50);
1123
1124
1125
1126 INIT_LIST_HEAD(&cam->buffer_queue);
1127 ret = videobuf_streamon(&cam->vb_queue);
1128 if (!ret)
1129 viacam_start_engine(cam);
1130out:
1131 mutex_unlock(&cam->lock);
1132 return ret;
1133}
1134
1135static int viacam_streamoff(struct file *filp, void *priv, enum v4l2_buf_type t)
1136{
1137 struct via_camera *cam = priv;
1138 int ret;
1139
1140 if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1141 return -EINVAL;
1142 mutex_lock(&cam->lock);
1143 if (cam->opstate != S_RUNNING) {
1144 ret = -EINVAL;
1145 goto out;
1146 }
1147 pm_qos_remove_request(&cam->qos_request);
1148 viacam_stop_engine(cam);
1149
1150
1151
1152
1153
1154 ret = videobuf_streamoff(&cam->vb_queue);
1155 INIT_LIST_HEAD(&cam->buffer_queue);
1156out:
1157 mutex_unlock(&cam->lock);
1158 return ret;
1159}
1160
1161
1162
1163static int viacam_g_parm(struct file *filp, void *priv,
1164 struct v4l2_streamparm *parm)
1165{
1166 struct via_camera *cam = priv;
1167 int ret;
1168
1169 mutex_lock(&cam->lock);
1170 ret = sensor_call(cam, video, g_parm, parm);
1171 mutex_unlock(&cam->lock);
1172 parm->parm.capture.readbuffers = cam->n_cap_bufs;
1173 return ret;
1174}
1175
1176static int viacam_s_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, s_parm, parm);
1184 mutex_unlock(&cam->lock);
1185 parm->parm.capture.readbuffers = cam->n_cap_bufs;
1186 return ret;
1187}
1188
1189static int viacam_enum_framesizes(struct file *filp, void *priv,
1190 struct v4l2_frmsizeenum *sizes)
1191{
1192 if (sizes->index != 0)
1193 return -EINVAL;
1194 sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1195 sizes->stepwise.min_width = QCIF_WIDTH;
1196 sizes->stepwise.min_height = QCIF_HEIGHT;
1197 sizes->stepwise.max_width = VGA_WIDTH;
1198 sizes->stepwise.max_height = VGA_HEIGHT;
1199 sizes->stepwise.step_width = sizes->stepwise.step_height = 1;
1200 return 0;
1201}
1202
1203static int viacam_enum_frameintervals(struct file *filp, void *priv,
1204 struct v4l2_frmivalenum *interval)
1205{
1206 struct via_camera *cam = priv;
1207 int ret;
1208
1209 mutex_lock(&cam->lock);
1210 ret = sensor_call(cam, video, enum_frameintervals, interval);
1211 mutex_unlock(&cam->lock);
1212 return ret;
1213}
1214
1215
1216
1217static const struct v4l2_ioctl_ops viacam_ioctl_ops = {
1218 .vidioc_g_chip_ident = viacam_g_chip_ident,
1219 .vidioc_queryctrl = viacam_queryctrl,
1220 .vidioc_g_ctrl = viacam_g_ctrl,
1221 .vidioc_s_ctrl = viacam_s_ctrl,
1222 .vidioc_enum_input = viacam_enum_input,
1223 .vidioc_g_input = viacam_g_input,
1224 .vidioc_s_input = viacam_s_input,
1225 .vidioc_s_std = viacam_s_std,
1226 .vidioc_enum_fmt_vid_cap = viacam_enum_fmt_vid_cap,
1227 .vidioc_try_fmt_vid_cap = viacam_try_fmt_vid_cap,
1228 .vidioc_g_fmt_vid_cap = viacam_g_fmt_vid_cap,
1229 .vidioc_s_fmt_vid_cap = viacam_s_fmt_vid_cap,
1230 .vidioc_querycap = viacam_querycap,
1231 .vidioc_reqbufs = viacam_reqbufs,
1232 .vidioc_querybuf = viacam_querybuf,
1233 .vidioc_qbuf = viacam_qbuf,
1234 .vidioc_dqbuf = viacam_dqbuf,
1235 .vidioc_streamon = viacam_streamon,
1236 .vidioc_streamoff = viacam_streamoff,
1237 .vidioc_g_parm = viacam_g_parm,
1238 .vidioc_s_parm = viacam_s_parm,
1239 .vidioc_enum_framesizes = viacam_enum_framesizes,
1240 .vidioc_enum_frameintervals = viacam_enum_frameintervals,
1241};
1242
1243
1244
1245
1246
1247
1248#ifdef CONFIG_PM
1249
1250static int viacam_suspend(void *priv)
1251{
1252 struct via_camera *cam = priv;
1253 enum viacam_opstate state = cam->opstate;
1254
1255 if (cam->opstate != S_IDLE) {
1256 viacam_stop_engine(cam);
1257 cam->opstate = state;
1258 }
1259
1260 return 0;
1261}
1262
1263static int viacam_resume(void *priv)
1264{
1265 struct via_camera *cam = priv;
1266 int ret = 0;
1267
1268
1269
1270
1271 via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1272 via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1273 viacam_int_disable(cam);
1274 set_bit(CF_CONFIG_NEEDED, &cam->flags);
1275
1276
1277
1278 if (cam->users > 0)
1279 via_sensor_power_up(cam);
1280 else
1281 via_sensor_power_down(cam);
1282
1283
1284
1285 if (cam->opstate != S_IDLE) {
1286 mutex_lock(&cam->lock);
1287 ret = viacam_configure_sensor(cam);
1288 if (!ret)
1289 ret = viacam_config_controller(cam);
1290 mutex_unlock(&cam->lock);
1291 if (!ret)
1292 viacam_start_engine(cam);
1293 }
1294
1295 return ret;
1296}
1297
1298static struct viafb_pm_hooks viacam_pm_hooks = {
1299 .suspend = viacam_suspend,
1300 .resume = viacam_resume
1301};
1302
1303#endif
1304
1305
1306
1307
1308
1309static struct video_device viacam_v4l_template = {
1310 .name = "via-camera",
1311 .minor = -1,
1312 .tvnorms = V4L2_STD_NTSC_M,
1313 .current_norm = V4L2_STD_NTSC_M,
1314 .fops = &viacam_fops,
1315 .ioctl_ops = &viacam_ioctl_ops,
1316 .release = video_device_release_empty,
1317};
1318
1319
1320
1321
1322
1323
1324
1325#define VIACAM_SERIAL_DEVFN 0x88
1326#define VIACAM_SERIAL_CREG 0x46
1327#define VIACAM_SERIAL_BIT 0x40
1328
1329static __devinit bool viacam_serial_is_enabled(void)
1330{
1331 struct pci_bus *pbus = pci_find_bus(0, 0);
1332 u8 cbyte;
1333
1334 pci_bus_read_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1335 VIACAM_SERIAL_CREG, &cbyte);
1336 if ((cbyte & VIACAM_SERIAL_BIT) == 0)
1337 return false;
1338 if (override_serial == 0) {
1339 printk(KERN_NOTICE "Via camera: serial port is enabled, " \
1340 "refusing to load.\n");
1341 printk(KERN_NOTICE "Specify override_serial=1 to force " \
1342 "module loading.\n");
1343 return true;
1344 }
1345 printk(KERN_NOTICE "Via camera: overriding serial port\n");
1346 pci_bus_write_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1347 VIACAM_SERIAL_CREG, cbyte & ~VIACAM_SERIAL_BIT);
1348 return false;
1349}
1350
1351static __devinit int viacam_probe(struct platform_device *pdev)
1352{
1353 int ret;
1354 struct i2c_adapter *sensor_adapter;
1355 struct viafb_dev *viadev = pdev->dev.platform_data;
1356
1357
1358
1359
1360
1361
1362
1363 struct via_camera *cam;
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373 if (viadev->camera_fbmem_size < (VGA_HEIGHT*VGA_WIDTH*4)) {
1374 printk(KERN_ERR "viacam: insufficient FB memory reserved\n");
1375 return -ENOMEM;
1376 }
1377 if (viadev->engine_mmio == NULL) {
1378 printk(KERN_ERR "viacam: No I/O memory, so no pictures\n");
1379 return -ENOMEM;
1380 }
1381
1382 if (machine_is_olpc() && viacam_serial_is_enabled())
1383 return -EBUSY;
1384
1385
1386
1387
1388 cam = kzalloc (sizeof(struct via_camera), GFP_KERNEL);
1389 if (cam == NULL)
1390 return -ENOMEM;
1391 via_cam_info = cam;
1392 cam->platdev = pdev;
1393 cam->viadev = viadev;
1394 cam->users = 0;
1395 cam->owner = NULL;
1396 cam->opstate = S_IDLE;
1397 cam->user_format = cam->sensor_format = viacam_def_pix_format;
1398 mutex_init(&cam->lock);
1399 INIT_LIST_HEAD(&cam->buffer_queue);
1400 cam->mmio = viadev->engine_mmio;
1401 cam->fbmem = viadev->fbmem;
1402 cam->fb_offset = viadev->camera_fbmem_offset;
1403 cam->flags = 1 << CF_CONFIG_NEEDED;
1404 cam->mbus_code = via_def_mbus_code;
1405
1406
1407
1408 ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
1409 if (ret) {
1410 dev_err(&pdev->dev, "Unable to register v4l2 device\n");
1411 return ret;
1412 }
1413
1414
1415
1416 pdev->dev.dma_mask = &viadev->pdev->dma_mask;
1417 dma_set_mask(&pdev->dev, 0xffffffff);
1418
1419
1420
1421
1422 via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1423 via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1424
1425
1426
1427 ret = via_sensor_power_setup(cam);
1428 if (ret)
1429 goto out_unregister;
1430 via_sensor_power_up(cam);
1431
1432
1433
1434
1435
1436 sensor_adapter = viafb_find_i2c_adapter(VIA_PORT_31);
1437 cam->sensor = v4l2_i2c_new_subdev(&cam->v4l2_dev, sensor_adapter,
1438 "ov7670", 0x42 >> 1, NULL);
1439 if (cam->sensor == NULL) {
1440 dev_err(&pdev->dev, "Unable to find the sensor!\n");
1441 ret = -ENODEV;
1442 goto out_power_down;
1443 }
1444
1445
1446
1447 viacam_int_disable(cam);
1448 ret = request_threaded_irq(viadev->pdev->irq, viacam_quick_irq,
1449 viacam_irq, IRQF_SHARED, "via-camera", cam);
1450 if (ret)
1451 goto out_power_down;
1452
1453
1454
1455 cam->vdev = viacam_v4l_template;
1456 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1457 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1458 if (ret)
1459 goto out_irq;
1460 video_set_drvdata(&cam->vdev, cam);
1461
1462#ifdef CONFIG_PM
1463
1464
1465
1466 viacam_pm_hooks.private = cam;
1467 viafb_pm_register(&viacam_pm_hooks);
1468#endif
1469
1470
1471 via_sensor_power_down(cam);
1472 return 0;
1473
1474out_irq:
1475 free_irq(viadev->pdev->irq, cam);
1476out_power_down:
1477 via_sensor_power_release(cam);
1478out_unregister:
1479 v4l2_device_unregister(&cam->v4l2_dev);
1480 return ret;
1481}
1482
1483static __devexit int viacam_remove(struct platform_device *pdev)
1484{
1485 struct via_camera *cam = via_cam_info;
1486 struct viafb_dev *viadev = pdev->dev.platform_data;
1487
1488 video_unregister_device(&cam->vdev);
1489 v4l2_device_unregister(&cam->v4l2_dev);
1490 free_irq(viadev->pdev->irq, cam);
1491 via_sensor_power_release(cam);
1492 via_cam_info = NULL;
1493 return 0;
1494}
1495
1496static struct platform_driver viacam_driver = {
1497 .driver = {
1498 .name = "viafb-camera",
1499 },
1500 .probe = viacam_probe,
1501 .remove = viacam_remove,
1502};
1503
1504static int viacam_init(void)
1505{
1506 return platform_driver_register(&viacam_driver);
1507}
1508module_init(viacam_init);
1509
1510static void viacam_exit(void)
1511{
1512 platform_driver_unregister(&viacam_driver);
1513}
1514module_exit(viacam_exit);
1515