2 * drivers/sbus/char/vfc_dev.c
4 * Driver for the Videopix Frame Grabber.
6 * In order to use the VFC you need to program the video controller
7 * chip. This chip is the Phillips SAA9051. You need to call their
8 * documentation ordering line to get the docs.
10 * There is very little documentation on the VFC itself. There is
11 * some useful info that can be found in the manuals that come with
12 * the card. I will hopefully write some better docs at a later date.
14 * Copyright (C) 1996 Manish Vachharajani (mvachhar@noc.rutgers.edu)
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/slab.h>
21 #include <linux/errno.h>
22 #include <linux/sched.h>
24 #include <linux/smp_lock.h>
25 #include <linux/delay.h>
26 #include <linux/spinlock.h>
29 #include <asm/openprom.h>
30 #include <asm/oplib.h>
32 #include <asm/system.h>
35 #include <asm/pgtable.h>
36 #include <asm/uaccess.h>
38 #define VFC_MAJOR (60)
41 #define VFC_IOCTL_DEBUG
45 #include <asm/vfc_ioctls.h>
47 static struct file_operations vfc_fops;
48 struct vfc_dev **vfc_dev_lst;
49 static char vfcstr[]="vfc";
50 static unsigned char saa9051_init_array[VFC_SAA9051_NR] = {
51 0x00, 0x64, 0x72, 0x52,
52 0x36, 0x18, 0xff, 0x20,
53 0xfc, 0x77, 0xe3, 0x50,
57 void vfc_lock_device(struct vfc_dev *dev)
59 down(&dev->device_lock_sem);
62 void vfc_unlock_device(struct vfc_dev *dev)
64 up(&dev->device_lock_sem);
68 void vfc_captstat_reset(struct vfc_dev *dev)
70 dev->control_reg |= VFC_CONTROL_CAPTRESET;
71 sbus_writel(dev->control_reg, &dev->regs->control);
72 dev->control_reg &= ~VFC_CONTROL_CAPTRESET;
73 sbus_writel(dev->control_reg, &dev->regs->control);
74 dev->control_reg |= VFC_CONTROL_CAPTRESET;
75 sbus_writel(dev->control_reg, &dev->regs->control);
78 void vfc_memptr_reset(struct vfc_dev *dev)
80 dev->control_reg |= VFC_CONTROL_MEMPTR;
81 sbus_writel(dev->control_reg, &dev->regs->control);
82 dev->control_reg &= ~VFC_CONTROL_MEMPTR;
83 sbus_writel(dev->control_reg, &dev->regs->control);
84 dev->control_reg |= VFC_CONTROL_MEMPTR;
85 sbus_writel(dev->control_reg, &dev->regs->control);
88 int vfc_csr_init(struct vfc_dev *dev)
90 dev->control_reg = 0x80000000;
91 sbus_writel(dev->control_reg, &dev->regs->control);
93 dev->control_reg &= ~0x80000000;
94 sbus_writel(dev->control_reg, &dev->regs->control);
96 sbus_writel(0x0f000000, &dev->regs->i2c_magic2);
98 vfc_memptr_reset(dev);
100 dev->control_reg &= ~VFC_CONTROL_DIAGMODE;
101 dev->control_reg &= ~VFC_CONTROL_CAPTURE;
102 dev->control_reg |= 0x40000000;
103 sbus_writel(dev->control_reg, &dev->regs->control);
105 vfc_captstat_reset(dev);
110 int vfc_saa9051_init(struct vfc_dev *dev)
114 for (i = 0; i < VFC_SAA9051_NR; i++)
115 dev->saa9051_state_array[i] = saa9051_init_array[i];
117 vfc_i2c_sendbuf(dev,VFC_SAA9051_ADDR,
118 dev->saa9051_state_array, VFC_SAA9051_NR);
122 int init_vfc_hw(struct vfc_dev *dev)
124 vfc_lock_device(dev);
127 vfc_pcf8584_init(dev);
128 vfc_init_i2c_bus(dev); /* hopefully this doesn't undo the magic
130 vfc_saa9051_init(dev);
131 vfc_unlock_device(dev);
135 int init_vfc_devstruct(struct vfc_dev *dev, int instance)
137 dev->instance=instance;
138 init_MUTEX(&dev->device_lock_sem);
144 int init_vfc_device(struct sbus_dev *sdev,struct vfc_dev *dev, int instance)
147 printk(KERN_ERR "VFC: Bogus pointer passed\n");
150 printk("Initializing vfc%d\n",instance);
152 dev->regs = (volatile struct vfc_regs __iomem *)
153 sbus_ioremap(&sdev->resource[0], 0,
154 sizeof(struct vfc_regs), vfcstr);
155 dev->which_io = sdev->reg_addrs[0].which_io;
156 dev->phys_regs = (struct vfc_regs *) sdev->reg_addrs[0].phys_addr;
157 if (dev->regs == NULL)
160 printk("vfc%d: registers mapped at phys_addr: 0x%lx\n virt_addr: 0x%lx\n",
161 instance,(unsigned long)sdev->reg_addrs[0].phys_addr,(unsigned long)dev->regs);
163 if (init_vfc_devstruct(dev, instance))
165 if (init_vfc_hw(dev))
168 devfs_mk_cdev(MKDEV(VFC_MAJOR, instance),
169 S_IFCHR | S_IRUSR | S_IWUSR,
175 struct vfc_dev *vfc_get_dev_ptr(int instance)
177 return vfc_dev_lst[instance];
180 static DEFINE_SPINLOCK(vfc_dev_lock);
182 static int vfc_open(struct inode *inode, struct file *file)
186 spin_lock(&vfc_dev_lock);
187 dev = vfc_get_dev_ptr(iminor(inode));
189 spin_unlock(&vfc_dev_lock);
193 spin_unlock(&vfc_dev_lock);
198 spin_unlock(&vfc_dev_lock);
200 vfc_lock_device(dev);
203 vfc_pcf8584_init(dev);
204 vfc_init_i2c_bus(dev);
205 vfc_saa9051_init(dev);
206 vfc_memptr_reset(dev);
207 vfc_captstat_reset(dev);
209 vfc_unlock_device(dev);
213 static int vfc_release(struct inode *inode,struct file *file)
217 spin_lock(&vfc_dev_lock);
218 dev = vfc_get_dev_ptr(iminor(inode));
219 if (!dev || !dev->busy) {
220 spin_unlock(&vfc_dev_lock);
224 spin_unlock(&vfc_dev_lock);
228 static int vfc_debug(struct vfc_dev *dev, int cmd, void __user *argp)
230 struct vfc_debug_inout inout;
231 unsigned char *buffer;
233 if (!capable(CAP_SYS_ADMIN))
238 if(copy_from_user(&inout, argp, sizeof(inout)))
241 buffer = kmalloc(inout.len, GFP_KERNEL);
245 if(copy_from_user(buffer, inout.buffer, inout.len)) {
251 vfc_lock_device(dev);
253 vfc_i2c_sendbuf(dev,inout.addr & 0xff,
256 if (copy_to_user(argp,&inout,sizeof(inout))) {
260 vfc_unlock_device(dev);
264 if (copy_from_user(&inout, argp, sizeof(inout)))
267 buffer = kmalloc(inout.len, GFP_KERNEL);
271 memset(buffer,0,inout.len);
272 vfc_lock_device(dev);
274 vfc_i2c_recvbuf(dev,inout.addr & 0xff
276 vfc_unlock_device(dev);
278 if (copy_to_user(inout.buffer, buffer, inout.len)) {
282 if (copy_to_user(argp,&inout,sizeof(inout))) {
295 int vfc_capture_start(struct vfc_dev *dev)
297 vfc_captstat_reset(dev);
298 dev->control_reg = sbus_readl(&dev->regs->control);
299 if((dev->control_reg & VFC_STATUS_CAPTURE)) {
300 printk(KERN_ERR "vfc%d: vfc capture status not reset\n",
305 vfc_lock_device(dev);
306 dev->control_reg &= ~VFC_CONTROL_CAPTURE;
307 sbus_writel(dev->control_reg, &dev->regs->control);
308 dev->control_reg |= VFC_CONTROL_CAPTURE;
309 sbus_writel(dev->control_reg, &dev->regs->control);
310 dev->control_reg &= ~VFC_CONTROL_CAPTURE;
311 sbus_writel(dev->control_reg, &dev->regs->control);
312 vfc_unlock_device(dev);
317 int vfc_capture_poll(struct vfc_dev *dev)
322 if (sbus_readl(&dev->regs->control) & VFC_STATUS_CAPTURE)
324 vfc_i2c_delay_no_busy(dev, 100);
327 printk(KERN_WARNING "vfc%d: capture timed out\n",
336 static int vfc_set_control_ioctl(struct inode *inode, struct file *file,
337 struct vfc_dev *dev, unsigned long arg)
341 if (copy_from_user(&setcmd,(void __user *)arg,sizeof(unsigned int)))
344 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCSCTRL) arg=0x%x\n",
345 dev->instance,setcmd));
349 vfc_lock_device(dev);
350 vfc_memptr_reset(dev);
351 vfc_unlock_device(dev);
355 vfc_capture_start(dev);
356 vfc_capture_poll(dev);
359 if(capable(CAP_SYS_ADMIN)) {
360 vfc_lock_device(dev);
361 dev->control_reg |= VFC_CONTROL_DIAGMODE;
362 sbus_writel(dev->control_reg, &dev->regs->control);
363 vfc_unlock_device(dev);
370 vfc_lock_device(dev);
371 dev->control_reg &= ~VFC_CONTROL_DIAGMODE;
372 sbus_writel(dev->control_reg, &dev->regs->control);
373 vfc_unlock_device(dev);
377 vfc_capture_start(dev);
381 vfc_capture_poll(dev);
393 int vfc_port_change_ioctl(struct inode *inode, struct file *file,
394 struct vfc_dev *dev, unsigned long arg)
399 if(copy_from_user(&cmd, (void __user *)arg, sizeof(unsigned int))) {
400 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to "
401 "vfc_port_change_ioctl\n",
406 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCPORTCHG) arg=0x%x\n",
407 dev->instance, cmd));
412 VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_START) = 0x72;
413 VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_STOP) = 0x52;
414 VFC_SAA9051_SA(dev,VFC_SAA9051_HC_START) = 0x36;
415 VFC_SAA9051_SA(dev,VFC_SAA9051_HC_STOP) = 0x18;
416 VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) = VFC_SAA9051_BP2;
417 VFC_SAA9051_SA(dev,VFC_SAA9051_C3) = VFC_SAA9051_CT | VFC_SAA9051_SS3;
418 VFC_SAA9051_SA(dev,VFC_SAA9051_SECAM_DELAY) = 0x3e;
421 VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_START) = 0x3a;
422 VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_STOP) = 0x17;
423 VFC_SAA9051_SA(dev,VFC_SAA9051_HC_START) = 0xfa;
424 VFC_SAA9051_SA(dev,VFC_SAA9051_HC_STOP) = 0xde;
425 VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) =
426 VFC_SAA9051_BY | VFC_SAA9051_PF | VFC_SAA9051_BP2;
427 VFC_SAA9051_SA(dev,VFC_SAA9051_C3) = VFC_SAA9051_YC;
428 VFC_SAA9051_SA(dev,VFC_SAA9051_SECAM_DELAY) = 0;
429 VFC_SAA9051_SA(dev,VFC_SAA9051_C2) &=
430 ~(VFC_SAA9051_SS0 | VFC_SAA9051_SS1);
440 VFC_SAA9051_SA(dev,VFC_SAA9051_C2) |=
441 (VFC_SAA9051_SS0 | VFC_SAA9051_SS1);
444 VFC_SAA9051_SA(dev,VFC_SAA9051_C2) &=
445 ~(VFC_SAA9051_SS0 | VFC_SAA9051_SS1);
446 VFC_SAA9051_SA(dev,VFC_SAA9051_C2) |= VFC_SAA9051_SS0;
455 VFC_SAA9051_SA(dev,VFC_SAA9051_C3) &= ~(VFC_SAA9051_SS2);
456 ret=vfc_update_saa9051(dev);
458 VFC_SAA9051_SA(dev,VFC_SAA9051_C3) |= (VFC_SAA9051_SS2);
459 ret=vfc_update_saa9051(dev);
463 int vfc_set_video_ioctl(struct inode *inode, struct file *file,
464 struct vfc_dev *dev, unsigned long arg)
469 if(copy_from_user(&cmd, (void __user *)arg, sizeof(unsigned int))) {
470 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to "
471 "vfc_set_video_ioctl\n",
476 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCSVID) arg=0x%x\n",
477 dev->instance, cmd));
480 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~VFC_SAA9051_ALT;
481 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_YPN |
482 VFC_SAA9051_CCFR0 | VFC_SAA9051_CCFR1 | VFC_SAA9051_FS;
483 ret = vfc_update_saa9051(dev);
486 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~(VFC_SAA9051_YPN |
490 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_ALT;
491 ret = vfc_update_saa9051(dev);
495 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_CO;
496 VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) &=
497 ~(VFC_SAA9051_BY | VFC_SAA9051_PF);
498 ret = vfc_update_saa9051(dev);
501 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~(VFC_SAA9051_CO);
502 VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) |=
503 (VFC_SAA9051_BY | VFC_SAA9051_PF);
504 ret = vfc_update_saa9051(dev);
514 int vfc_get_video_ioctl(struct inode *inode, struct file *file,
515 struct vfc_dev *dev, unsigned long arg)
518 unsigned int status = NO_LOCK;
519 unsigned char buf[1];
521 if(vfc_i2c_recvbuf(dev, VFC_SAA9051_ADDR, buf, 1)) {
522 printk(KERN_ERR "vfc%d: Unable to get status\n",
527 if(buf[0] & VFC_SAA9051_HLOCK) {
529 } else if(buf[0] & VFC_SAA9051_FD) {
530 if(buf[0] & VFC_SAA9051_CD)
533 status = NTSC_NOCOLOR;
535 if(buf[0] & VFC_SAA9051_CD)
538 status = PAL_NOCOLOR;
540 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCGVID) returning status 0x%x; "
541 "buf[0]=%x\n", dev->instance, status, buf[0]));
543 if (copy_to_user((void __user *)arg,&status,sizeof(unsigned int))) {
544 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to "
545 "vfc_get_video_ioctl\n",
552 static int vfc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
558 void __user *argp = (void __user *)arg;
560 dev = vfc_get_dev_ptr(iminor(inode));
564 switch(cmd & 0x0000ffff) {
567 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCGCTRL)\n", dev->instance));
569 tmp = sbus_readl(&dev->regs->control);
570 if(copy_to_user(argp, &tmp, sizeof(unsigned int))) {
577 ret = vfc_set_control_ioctl(inode, file, dev, arg);
580 ret = vfc_get_video_ioctl(inode, file, dev, arg);
583 ret = vfc_set_video_ioctl(inode, file, dev, arg);
586 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCHUE)\n", dev->instance));
587 if(copy_from_user(&tmp,argp,sizeof(unsigned int))) {
588 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer "
589 "to IOCTL(VFCHUE)", dev->instance));
592 VFC_SAA9051_SA(dev,VFC_SAA9051_HUE) = tmp;
593 vfc_update_saa9051(dev);
598 ret = vfc_port_change_ioctl(inode, file, dev, arg);
602 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCRDINFO)\n", dev->instance));
605 ret = vfc_debug(vfc_get_dev_ptr(iminor(inode)), cmd, argp);
612 static int vfc_mmap(struct file *file, struct vm_area_struct *vma)
614 unsigned int map_size, ret, map_offset;
617 dev = vfc_get_dev_ptr(iminor(file->f_dentry->d_inode));
621 map_size = vma->vm_end - vma->vm_start;
622 if(map_size > sizeof(struct vfc_regs))
623 map_size = sizeof(struct vfc_regs);
626 (VM_MAYREAD | VM_MAYWRITE | VM_MAYSHARE);
627 map_offset = (unsigned int) (long)dev->phys_regs;
628 ret = io_remap_pfn_range(vma, vma->vm_start,
629 MK_IOSPACE_PFN(dev->which_io,
630 map_offset >> PAGE_SHIFT),
631 map_size, vma->vm_page_prot);
640 static struct file_operations vfc_fops = {
641 .owner = THIS_MODULE,
646 .release = vfc_release,
649 static int vfc_probe(void)
651 struct sbus_bus *sbus;
652 struct sbus_dev *sdev = NULL;
654 int instance = 0, cards = 0;
656 for_all_sbusdev(sdev, sbus) {
657 if (strcmp(sdev->prom_name, "vfc") == 0) {
666 vfc_dev_lst = (struct vfc_dev **)kmalloc(sizeof(struct vfc_dev *) *
669 if (vfc_dev_lst == NULL)
671 memset(vfc_dev_lst, 0, sizeof(struct vfc_dev *) * (cards + 1));
672 vfc_dev_lst[cards] = NULL;
674 ret = register_chrdev(VFC_MAJOR, vfcstr, &vfc_fops);
676 printk(KERN_ERR "Unable to get major number %d\n", VFC_MAJOR);
682 for_all_sbusdev(sdev, sbus) {
683 if (strcmp(sdev->prom_name, "vfc") == 0) {
684 vfc_dev_lst[instance]=(struct vfc_dev *)
685 kmalloc(sizeof(struct vfc_dev), GFP_KERNEL);
686 if (vfc_dev_lst[instance] == NULL)
688 ret = init_vfc_device(sdev,
689 vfc_dev_lst[instance],
692 printk(KERN_ERR "Unable to initialize"
707 int init_module(void)
716 static void deinit_vfc_device(struct vfc_dev *dev)
720 devfs_remove("vfc/%d", dev->instance);
721 sbus_iounmap(dev->regs, sizeof(struct vfc_regs));
725 void cleanup_module(void)
727 struct vfc_dev **devp;
729 unregister_chrdev(VFC_MAJOR,vfcstr);
731 for (devp = vfc_dev_lst; *devp; devp++)
732 deinit_vfc_device(*devp);
740 MODULE_LICENSE("GPL");