scx200_gpio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. int err = 0;
  34. for (i = 0; i < len; ++i) {
  35. char c;
  36. if (get_user(c, data + i))
  37. return -EFAULT;
  38. switch (c) {
  39. case '0':
  40. scx200_gpio_set(m, 0);
  41. break;
  42. case '1':
  43. scx200_gpio_set(m, 1);
  44. break;
  45. case 'O':
  46. printk(KERN_INFO NAME ": GPIO%d output enabled\n", m);
  47. scx200_gpio_configure(m, ~1, 1);
  48. break;
  49. case 'o':
  50. printk(KERN_INFO NAME ": GPIO%d output disabled\n", m);
  51. scx200_gpio_configure(m, ~1, 0);
  52. break;
  53. case 'T':
  54. printk(KERN_INFO NAME ": GPIO%d output is push pull\n", m);
  55. scx200_gpio_configure(m, ~2, 2);
  56. break;
  57. case 't':
  58. printk(KERN_INFO NAME ": GPIO%d output is open drain\n", m);
  59. scx200_gpio_configure(m, ~2, 0);
  60. break;
  61. case 'P':
  62. printk(KERN_INFO NAME ": GPIO%d pull up enabled\n", m);
  63. scx200_gpio_configure(m, ~4, 4);
  64. break;
  65. case 'p':
  66. printk(KERN_INFO NAME ": GPIO%d pull up disabled\n", m);
  67. scx200_gpio_configure(m, ~4, 0);
  68. break;
  69. case 'v':
  70. /* View Current pin settings */
  71. scx200_gpio_dump(m);
  72. break;
  73. case '\n':
  74. /* end of settings string, do nothing */
  75. break;
  76. default:
  77. printk(KERN_ERR NAME
  78. ": GPIO-%2d bad setting: chr<0x%2x>\n", m,
  79. (int)c);
  80. err++;
  81. }
  82. }
  83. if (err)
  84. return -EINVAL; /* full string handled, report error */
  85. return len;
  86. }
  87. static ssize_t scx200_gpio_read(struct file *file, char __user *buf,
  88. size_t len, loff_t *ppos)
  89. {
  90. unsigned m = iminor(file->f_dentry->d_inode);
  91. int value;
  92. value = scx200_gpio_get(m);
  93. if (put_user(value ? '1' : '0', buf))
  94. return -EFAULT;
  95. return 1;
  96. }
  97. static int scx200_gpio_open(struct inode *inode, struct file *file)
  98. {
  99. unsigned m = iminor(inode);
  100. if (m > 63)
  101. return -EINVAL;
  102. return nonseekable_open(inode, file);
  103. }
  104. static int scx200_gpio_release(struct inode *inode, struct file *file)
  105. {
  106. return 0;
  107. }
  108. static struct file_operations scx200_gpio_fops = {
  109. .owner = THIS_MODULE,
  110. .write = scx200_gpio_write,
  111. .read = scx200_gpio_read,
  112. .open = scx200_gpio_open,
  113. .release = scx200_gpio_release,
  114. };
  115. struct cdev *scx200_devices;
  116. static int num_pins = 32;
  117. static int __init scx200_gpio_init(void)
  118. {
  119. int rc, i;
  120. dev_t dev = MKDEV(major, 0);
  121. if (!scx200_gpio_present()) {
  122. printk(KERN_ERR NAME ": no SCx200 gpio present\n");
  123. return -ENODEV;
  124. }
  125. /* support dev_dbg() with pdev->dev */
  126. pdev = platform_device_alloc(DEVNAME, 0);
  127. if (!pdev)
  128. return -ENOMEM;
  129. rc = platform_device_add(pdev);
  130. if (rc)
  131. goto undo_malloc;
  132. if (major)
  133. rc = register_chrdev_region(dev, num_pins, "scx200_gpio");
  134. else {
  135. rc = alloc_chrdev_region(&dev, 0, num_pins, "scx200_gpio");
  136. major = MAJOR(dev);
  137. }
  138. if (rc < 0) {
  139. dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc);
  140. goto undo_platform_device_add;
  141. }
  142. scx200_devices = kzalloc(num_pins * sizeof(struct cdev), GFP_KERNEL);
  143. if (!scx200_devices) {
  144. rc = -ENOMEM;
  145. goto undo_chrdev_region;
  146. }
  147. for (i = 0; i < num_pins; i++) {
  148. struct cdev *cdev = &scx200_devices[i];
  149. cdev_init(cdev, &scx200_gpio_fops);
  150. cdev->owner = THIS_MODULE;
  151. rc = cdev_add(cdev, MKDEV(major, i), 1);
  152. /* tolerate 'minor' errors */
  153. if (rc)
  154. dev_err(&pdev->dev, "Error %d on minor %d", rc, i);
  155. }
  156. return 0; /* succeed */
  157. undo_chrdev_region:
  158. unregister_chrdev_region(dev, num_pins);
  159. undo_platform_device_add:
  160. platform_device_put(pdev);
  161. undo_malloc:
  162. kfree(pdev);
  163. return rc;
  164. }
  165. static void __exit scx200_gpio_cleanup(void)
  166. {
  167. kfree(scx200_devices);
  168. unregister_chrdev_region(MKDEV(major, 0), num_pins);
  169. platform_device_put(pdev);
  170. platform_device_unregister(pdev);
  171. /* kfree(pdev); */
  172. }
  173. module_init(scx200_gpio_init);
  174. module_exit(scx200_gpio_cleanup);