scx200_gpio.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* linux/drivers/char/scx200_gpio.c
  2. National Semiconductor SCx200 GPIO driver. Allows a user space
  3. process to play with the GPIO pins.
  4. Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com> */
  5. #include <linux/config.h>
  6. #include <linux/fs.h>
  7. #include <linux/module.h>
  8. #include <linux/errno.h>
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <asm/uaccess.h>
  12. #include <asm/io.h>
  13. #include <linux/types.h>
  14. #include <linux/cdev.h>
  15. #include <linux/scx200_gpio.h>
  16. #define NAME "scx200_gpio"
  17. MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
  18. MODULE_DESCRIPTION("NatSemi SCx200 GPIO Pin Driver");
  19. MODULE_LICENSE("GPL");
  20. static int major = 0; /* default to dynamic major */
  21. module_param(major, int, 0);
  22. MODULE_PARM_DESC(major, "Major device number");
  23. extern void scx200_gpio_dump(unsigned index);
  24. static ssize_t scx200_gpio_write(struct file *file, const char __user *data,
  25. size_t len, loff_t *ppos)
  26. {
  27. unsigned m = iminor(file->f_dentry->d_inode);
  28. size_t i;
  29. for (i = 0; i < len; ++i) {
  30. char c;
  31. if (get_user(c, data + i))
  32. return -EFAULT;
  33. switch (c) {
  34. case '0':
  35. scx200_gpio_set(m, 0);
  36. break;
  37. case '1':
  38. scx200_gpio_set(m, 1);
  39. break;
  40. case 'O':
  41. printk(KERN_INFO NAME ": GPIO%d output enabled\n", m);
  42. scx200_gpio_configure(m, ~1, 1);
  43. break;
  44. case 'o':
  45. printk(KERN_INFO NAME ": GPIO%d output disabled\n", m);
  46. scx200_gpio_configure(m, ~1, 0);
  47. break;
  48. case 'T':
  49. printk(KERN_INFO NAME ": GPIO%d output is push pull\n", m);
  50. scx200_gpio_configure(m, ~2, 2);
  51. break;
  52. case 't':
  53. printk(KERN_INFO NAME ": GPIO%d output is open drain\n", m);
  54. scx200_gpio_configure(m, ~2, 0);
  55. break;
  56. case 'P':
  57. printk(KERN_INFO NAME ": GPIO%d pull up enabled\n", m);
  58. scx200_gpio_configure(m, ~4, 4);
  59. break;
  60. case 'p':
  61. printk(KERN_INFO NAME ": GPIO%d pull up disabled\n", m);
  62. scx200_gpio_configure(m, ~4, 0);
  63. break;
  64. }
  65. }
  66. return len;
  67. }
  68. static ssize_t scx200_gpio_read(struct file *file, char __user *buf,
  69. size_t len, loff_t *ppos)
  70. {
  71. unsigned m = iminor(file->f_dentry->d_inode);
  72. int value;
  73. value = scx200_gpio_get(m);
  74. if (put_user(value ? '1' : '0', buf))
  75. return -EFAULT;
  76. return 1;
  77. }
  78. static int scx200_gpio_open(struct inode *inode, struct file *file)
  79. {
  80. unsigned m = iminor(inode);
  81. if (m > 63)
  82. return -EINVAL;
  83. return nonseekable_open(inode, file);
  84. }
  85. static int scx200_gpio_release(struct inode *inode, struct file *file)
  86. {
  87. return 0;
  88. }
  89. static struct file_operations scx200_gpio_fops = {
  90. .owner = THIS_MODULE,
  91. .write = scx200_gpio_write,
  92. .read = scx200_gpio_read,
  93. .open = scx200_gpio_open,
  94. .release = scx200_gpio_release,
  95. };
  96. struct cdev *scx200_devices;
  97. int num_devs = 32;
  98. static int __init scx200_gpio_init(void)
  99. {
  100. int rc, i;
  101. dev_t dev = MKDEV(major, 0);
  102. printk(KERN_DEBUG NAME ": NatSemi SCx200 GPIO Driver\n");
  103. if (!scx200_gpio_present()) {
  104. printk(KERN_ERR NAME ": no SCx200 gpio present\n");
  105. return -ENODEV;
  106. }
  107. if (major)
  108. rc = register_chrdev_region(dev, num_devs, "scx200_gpio");
  109. else {
  110. rc = alloc_chrdev_region(&dev, 0, num_devs, "scx200_gpio");
  111. major = MAJOR(dev);
  112. }
  113. if (rc < 0) {
  114. printk(KERN_ERR NAME ": SCx200 chrdev_region: %d\n", rc);
  115. return rc;
  116. }
  117. scx200_devices = kzalloc(num_devs * sizeof(struct cdev), GFP_KERNEL);
  118. if (!scx200_devices) {
  119. rc = -ENOMEM;
  120. goto fail_malloc;
  121. }
  122. for (i = 0; i < num_devs; i++) {
  123. struct cdev *cdev = &scx200_devices[i];
  124. cdev_init(cdev, &scx200_gpio_fops);
  125. cdev->owner = THIS_MODULE;
  126. cdev->ops = &scx200_gpio_fops;
  127. rc = cdev_add(cdev, MKDEV(major, i), 1);
  128. /* Fail gracefully if need be */
  129. if (rc)
  130. printk(KERN_ERR NAME "Error %d on minor %d", rc, i);
  131. }
  132. return 0; /* succeed */
  133. fail_malloc:
  134. unregister_chrdev_region(dev, num_devs);
  135. return rc;
  136. }
  137. static void __exit scx200_gpio_cleanup(void)
  138. {
  139. kfree(scx200_devices);
  140. unregister_chrdev_region(MKDEV(major, 0), num_devs);
  141. }
  142. module_init(scx200_gpio_init);
  143. module_exit(scx200_gpio_cleanup);