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>
23 #include <linux/delay.h>
24 #include <linux/spinlock.h>
25 #include <linux/mutex.h>
28 #include <asm/openprom.h>
29 #include <asm/oplib.h>
31 #include <asm/system.h>
34 #include <asm/pgtable.h>
35 #include <asm/uaccess.h>
37 #define VFC_MAJOR (60)
40 #define VFC_IOCTL_DEBUG
44 #include <asm/vfc_ioctls.h>
46 static const struct file_operations vfc_fops;
47 struct vfc_dev **vfc_dev_lst;
48 static char vfcstr[]="vfc";
49 static unsigned char saa9051_init_array[VFC_SAA9051_NR] = {
50 0x00, 0x64, 0x72, 0x52,
51 0x36, 0x18, 0xff, 0x20,
52 0xfc, 0x77, 0xe3, 0x50,
56 void vfc_lock_device(struct vfc_dev *dev)
58 mutex_lock(&dev->device_lock_mtx);
61 void vfc_unlock_device(struct vfc_dev *dev)
63 mutex_unlock(&dev->device_lock_mtx);
67 void vfc_captstat_reset(struct vfc_dev *dev)
69 dev->control_reg |= VFC_CONTROL_CAPTRESET;
70 sbus_writel(dev->control_reg, &dev->regs->control);
71 dev->control_reg &= ~VFC_CONTROL_CAPTRESET;
72 sbus_writel(dev->control_reg, &dev->regs->control);
73 dev->control_reg |= VFC_CONTROL_CAPTRESET;
74 sbus_writel(dev->control_reg, &dev->regs->control);
77 void vfc_memptr_reset(struct vfc_dev *dev)
79 dev->control_reg |= VFC_CONTROL_MEMPTR;
80 sbus_writel(dev->control_reg, &dev->regs->control);
81 dev->control_reg &= ~VFC_CONTROL_MEMPTR;
82 sbus_writel(dev->control_reg, &dev->regs->control);
83 dev->control_reg |= VFC_CONTROL_MEMPTR;
84 sbus_writel(dev->control_reg, &dev->regs->control);
87 int vfc_csr_init(struct vfc_dev *dev)
89 dev->control_reg = 0x80000000;
90 sbus_writel(dev->control_reg, &dev->regs->control);
92 dev->control_reg &= ~0x80000000;
93 sbus_writel(dev->control_reg, &dev->regs->control);
95 sbus_writel(0x0f000000, &dev->regs->i2c_magic2);
97 vfc_memptr_reset(dev);
99 dev->control_reg &= ~VFC_CONTROL_DIAGMODE;
100 dev->control_reg &= ~VFC_CONTROL_CAPTURE;
101 dev->control_reg |= 0x40000000;
102 sbus_writel(dev->control_reg, &dev->regs->control);
104 vfc_captstat_reset(dev);
109 int vfc_saa9051_init(struct vfc_dev *dev)
113 for (i = 0; i < VFC_SAA9051_NR; i++)
114 dev->saa9051_state_array[i] = saa9051_init_array[i];
116 vfc_i2c_sendbuf(dev,VFC_SAA9051_ADDR,
117 dev->saa9051_state_array, VFC_SAA9051_NR);
121 int init_vfc_hw(struct vfc_dev *dev)
123 vfc_lock_device(dev);
126 vfc_pcf8584_init(dev);
127 vfc_init_i2c_bus(dev); /* hopefully this doesn't undo the magic
129 vfc_saa9051_init(dev);
130 vfc_unlock_device(dev);
134 int init_vfc_devstruct(struct vfc_dev *dev, int instance)
136 dev->instance=instance;
137 init_MUTEX(&dev->device_lock_sem);
143 int init_vfc_device(struct sbus_dev *sdev,struct vfc_dev *dev, int instance)
146 printk(KERN_ERR "VFC: Bogus pointer passed\n");
149 printk("Initializing vfc%d\n",instance);
151 dev->regs = (volatile struct vfc_regs __iomem *)
152 sbus_ioremap(&sdev->resource[0], 0,
153 sizeof(struct vfc_regs), vfcstr);
154 dev->which_io = sdev->reg_addrs[0].which_io;
155 dev->phys_regs = (struct vfc_regs *) sdev->reg_addrs[0].phys_addr;
156 if (dev->regs == NULL)
159 printk("vfc%d: registers mapped at phys_addr: 0x%lx\n virt_addr: 0x%lx\n",
160 instance,(unsigned long)sdev->reg_addrs[0].phys_addr,(unsigned long)dev->regs);
162 if (init_vfc_devstruct(dev, instance))
164 if (init_vfc_hw(dev))
170 struct vfc_dev *vfc_get_dev_ptr(int instance)
172 return vfc_dev_lst[instance];
175 static DEFINE_SPINLOCK(vfc_dev_lock);
177 static int vfc_open(struct inode *inode, struct file *file)
181 spin_lock(&vfc_dev_lock);
182 dev = vfc_get_dev_ptr(iminor(inode));
184 spin_unlock(&vfc_dev_lock);
188 spin_unlock(&vfc_dev_lock);
193 spin_unlock(&vfc_dev_lock);
195 vfc_lock_device(dev);
198 vfc_pcf8584_init(dev);
199 vfc_init_i2c_bus(dev);
200 vfc_saa9051_init(dev);
201 vfc_memptr_reset(dev);
202 vfc_captstat_reset(dev);
204 vfc_unlock_device(dev);
208 static int vfc_release(struct inode *inode,struct file *file)
212 spin_lock(&vfc_dev_lock);
213 dev = vfc_get_dev_ptr(iminor(inode));
214 if (!dev || !dev->busy) {
215 spin_unlock(&vfc_dev_lock);
219 spin_unlock(&vfc_dev_lock);
223 static int vfc_debug(struct vfc_dev *dev, int cmd, void __user *argp)
225 struct vfc_debug_inout inout;
226 unsigned char *buffer;
228 if (!capable(CAP_SYS_ADMIN))
233 if(copy_from_user(&inout, argp, sizeof(inout)))
236 buffer = kmalloc(inout.len, GFP_KERNEL);
240 if(copy_from_user(buffer, inout.buffer, inout.len)) {
246 vfc_lock_device(dev);
248 vfc_i2c_sendbuf(dev,inout.addr & 0xff,
251 if (copy_to_user(argp,&inout,sizeof(inout))) {
252 vfc_unlock_device(dev);
256 vfc_unlock_device(dev);
260 if (copy_from_user(&inout, argp, sizeof(inout)))
263 buffer = kzalloc(inout.len, GFP_KERNEL);
267 vfc_lock_device(dev);
269 vfc_i2c_recvbuf(dev,inout.addr & 0xff
271 vfc_unlock_device(dev);
273 if (copy_to_user(inout.buffer, buffer, inout.len)) {
277 if (copy_to_user(argp,&inout,sizeof(inout))) {
290 int vfc_capture_start(struct vfc_dev *dev)
292 vfc_captstat_reset(dev);
293 dev->control_reg = sbus_readl(&dev->regs->control);
294 if((dev->control_reg & VFC_STATUS_CAPTURE)) {
295 printk(KERN_ERR "vfc%d: vfc capture status not reset\n",
300 vfc_lock_device(dev);
301 dev->control_reg &= ~VFC_CONTROL_CAPTURE;
302 sbus_writel(dev->control_reg, &dev->regs->control);
303 dev->control_reg |= VFC_CONTROL_CAPTURE;
304 sbus_writel(dev->control_reg, &dev->regs->control);
305 dev->control_reg &= ~VFC_CONTROL_CAPTURE;
306 sbus_writel(dev->control_reg, &dev->regs->control);
307 vfc_unlock_device(dev);
312 int vfc_capture_poll(struct vfc_dev *dev)
317 if (sbus_readl(&dev->regs->control) & VFC_STATUS_CAPTURE)
319 vfc_i2c_delay_no_busy(dev, 100);
322 printk(KERN_WARNING "vfc%d: capture timed out\n",
331 static int vfc_set_control_ioctl(struct inode *inode, struct file *file,
332 struct vfc_dev *dev, unsigned long arg)
336 if (copy_from_user(&setcmd,(void __user *)arg,sizeof(unsigned int)))
339 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCSCTRL) arg=0x%x\n",
340 dev->instance,setcmd));
344 vfc_lock_device(dev);
345 vfc_memptr_reset(dev);
346 vfc_unlock_device(dev);
350 vfc_capture_start(dev);
351 vfc_capture_poll(dev);
354 if(capable(CAP_SYS_ADMIN)) {
355 vfc_lock_device(dev);
356 dev->control_reg |= VFC_CONTROL_DIAGMODE;
357 sbus_writel(dev->control_reg, &dev->regs->control);
358 vfc_unlock_device(dev);
365 vfc_lock_device(dev);
366 dev->control_reg &= ~VFC_CONTROL_DIAGMODE;
367 sbus_writel(dev->control_reg, &dev->regs->control);
368 vfc_unlock_device(dev);
372 vfc_capture_start(dev);
376 vfc_capture_poll(dev);
388 int vfc_port_change_ioctl(struct inode *inode, struct file *file,
389 struct vfc_dev *dev, unsigned long arg)
394 if(copy_from_user(&cmd, (void __user *)arg, sizeof(unsigned int))) {
395 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to "
396 "vfc_port_change_ioctl\n",
401 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCPORTCHG) arg=0x%x\n",
402 dev->instance, cmd));
407 VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_START) = 0x72;
408 VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_STOP) = 0x52;
409 VFC_SAA9051_SA(dev,VFC_SAA9051_HC_START) = 0x36;
410 VFC_SAA9051_SA(dev,VFC_SAA9051_HC_STOP) = 0x18;
411 VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) = VFC_SAA9051_BP2;
412 VFC_SAA9051_SA(dev,VFC_SAA9051_C3) = VFC_SAA9051_CT | VFC_SAA9051_SS3;
413 VFC_SAA9051_SA(dev,VFC_SAA9051_SECAM_DELAY) = 0x3e;
416 VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_START) = 0x3a;
417 VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_STOP) = 0x17;
418 VFC_SAA9051_SA(dev,VFC_SAA9051_HC_START) = 0xfa;
419 VFC_SAA9051_SA(dev,VFC_SAA9051_HC_STOP) = 0xde;
420 VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) =
421 VFC_SAA9051_BY | VFC_SAA9051_PF | VFC_SAA9051_BP2;
422 VFC_SAA9051_SA(dev,VFC_SAA9051_C3) = VFC_SAA9051_YC;
423 VFC_SAA9051_SA(dev,VFC_SAA9051_SECAM_DELAY) = 0;
424 VFC_SAA9051_SA(dev,VFC_SAA9051_C2) &=
425 ~(VFC_SAA9051_SS0 | VFC_SAA9051_SS1);
435 VFC_SAA9051_SA(dev,VFC_SAA9051_C2) |=
436 (VFC_SAA9051_SS0 | VFC_SAA9051_SS1);
439 VFC_SAA9051_SA(dev,VFC_SAA9051_C2) &=
440 ~(VFC_SAA9051_SS0 | VFC_SAA9051_SS1);
441 VFC_SAA9051_SA(dev,VFC_SAA9051_C2) |= VFC_SAA9051_SS0;
450 VFC_SAA9051_SA(dev,VFC_SAA9051_C3) &= ~(VFC_SAA9051_SS2);
451 ret=vfc_update_saa9051(dev);
453 VFC_SAA9051_SA(dev,VFC_SAA9051_C3) |= (VFC_SAA9051_SS2);
454 ret=vfc_update_saa9051(dev);
458 int vfc_set_video_ioctl(struct inode *inode, struct file *file,
459 struct vfc_dev *dev, unsigned long arg)
464 if(copy_from_user(&cmd, (void __user *)arg, sizeof(unsigned int))) {
465 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to "
466 "vfc_set_video_ioctl\n",
471 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCSVID) arg=0x%x\n",
472 dev->instance, cmd));
475 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~VFC_SAA9051_ALT;
476 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_YPN |
477 VFC_SAA9051_CCFR0 | VFC_SAA9051_CCFR1 | VFC_SAA9051_FS;
478 ret = vfc_update_saa9051(dev);
481 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~(VFC_SAA9051_YPN |
485 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_ALT;
486 ret = vfc_update_saa9051(dev);
490 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_CO;
491 VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) &=
492 ~(VFC_SAA9051_BY | VFC_SAA9051_PF);
493 ret = vfc_update_saa9051(dev);
496 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~(VFC_SAA9051_CO);
497 VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) |=
498 (VFC_SAA9051_BY | VFC_SAA9051_PF);
499 ret = vfc_update_saa9051(dev);
509 int vfc_get_video_ioctl(struct inode *inode, struct file *file,
510 struct vfc_dev *dev, unsigned long arg)
513 unsigned int status = NO_LOCK;
514 unsigned char buf[1];
516 if(vfc_i2c_recvbuf(dev, VFC_SAA9051_ADDR, buf, 1)) {
517 printk(KERN_ERR "vfc%d: Unable to get status\n",
522 if(buf[0] & VFC_SAA9051_HLOCK) {
524 } else if(buf[0] & VFC_SAA9051_FD) {
525 if(buf[0] & VFC_SAA9051_CD)
528 status = NTSC_NOCOLOR;
530 if(buf[0] & VFC_SAA9051_CD)
533 status = PAL_NOCOLOR;
535 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCGVID) returning status 0x%x; "
536 "buf[0]=%x\n", dev->instance, status, buf[0]));
538 if (copy_to_user((void __user *)arg,&status,sizeof(unsigned int))) {
539 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to "
540 "vfc_get_video_ioctl\n",
547 static int vfc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
553 void __user *argp = (void __user *)arg;
555 dev = vfc_get_dev_ptr(iminor(inode));
559 switch(cmd & 0x0000ffff) {
562 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCGCTRL)\n", dev->instance));
564 tmp = sbus_readl(&dev->regs->control);
565 if(copy_to_user(argp, &tmp, sizeof(unsigned int))) {
572 ret = vfc_set_control_ioctl(inode, file, dev, arg);
575 ret = vfc_get_video_ioctl(inode, file, dev, arg);
578 ret = vfc_set_video_ioctl(inode, file, dev, arg);
581 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCHUE)\n", dev->instance));
582 if(copy_from_user(&tmp,argp,sizeof(unsigned int))) {
583 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer "
584 "to IOCTL(VFCHUE)", dev->instance));
587 VFC_SAA9051_SA(dev,VFC_SAA9051_HUE) = tmp;
588 vfc_update_saa9051(dev);
593 ret = vfc_port_change_ioctl(inode, file, dev, arg);
597 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCRDINFO)\n", dev->instance));
600 ret = vfc_debug(vfc_get_dev_ptr(iminor(inode)), cmd, argp);
607 static int vfc_mmap(struct file *file, struct vm_area_struct *vma)
609 unsigned int map_size, ret, map_offset;
612 dev = vfc_get_dev_ptr(iminor(file->f_path.dentry->d_inode));
616 map_size = vma->vm_end - vma->vm_start;
617 if(map_size > sizeof(struct vfc_regs))
618 map_size = sizeof(struct vfc_regs);
621 (VM_MAYREAD | VM_MAYWRITE | VM_MAYSHARE);
622 map_offset = (unsigned int) (long)dev->phys_regs;
623 ret = io_remap_pfn_range(vma, vma->vm_start,
624 MK_IOSPACE_PFN(dev->which_io,
625 map_offset >> PAGE_SHIFT),
626 map_size, vma->vm_page_prot);
635 static const struct file_operations vfc_fops = {
636 .owner = THIS_MODULE,
641 .release = vfc_release,
644 static int vfc_probe(void)
646 struct sbus_bus *sbus;
647 struct sbus_dev *sdev = NULL;
649 int instance = 0, cards = 0;
651 for_all_sbusdev(sdev, sbus) {
652 if (strcmp(sdev->prom_name, "vfc") == 0) {
661 vfc_dev_lst = kcalloc(cards + 1, sizeof(struct vfc_dev*), GFP_KERNEL);
662 if (vfc_dev_lst == NULL)
664 vfc_dev_lst[cards] = NULL;
666 ret = register_chrdev(VFC_MAJOR, vfcstr, &vfc_fops);
668 printk(KERN_ERR "Unable to get major number %d\n", VFC_MAJOR);
673 for_all_sbusdev(sdev, sbus) {
674 if (strcmp(sdev->prom_name, "vfc") == 0) {
675 vfc_dev_lst[instance]=(struct vfc_dev *)
676 kmalloc(sizeof(struct vfc_dev), GFP_KERNEL);
677 if (vfc_dev_lst[instance] == NULL)
679 ret = init_vfc_device(sdev,
680 vfc_dev_lst[instance],
683 printk(KERN_ERR "Unable to initialize"
698 int init_module(void)
707 static void deinit_vfc_device(struct vfc_dev *dev)
711 sbus_iounmap(dev->regs, sizeof(struct vfc_regs));
715 void cleanup_module(void)
717 struct vfc_dev **devp;
719 unregister_chrdev(VFC_MAJOR,vfcstr);
721 for (devp = vfc_dev_lst; *devp; devp++)
722 deinit_vfc_device(*devp);
729 MODULE_LICENSE("GPL");