scx200_gpio.c 4.3 KB

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