scx200_gpio.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #include <linux/nsc_gpio.h>
  19. #define NAME "scx200_gpio"
  20. #define DEVNAME NAME
  21. static struct platform_device *pdev;
  22. MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
  23. MODULE_DESCRIPTION("NatSemi SCx200 GPIO Pin Driver");
  24. MODULE_LICENSE("GPL");
  25. static int major = 0; /* default to dynamic major */
  26. module_param(major, int, 0);
  27. MODULE_PARM_DESC(major, "Major device number");
  28. extern void scx200_gpio_dump(unsigned index);
  29. extern ssize_t nsc_gpio_write(struct file *file, const char __user *data,
  30. size_t len, loff_t *ppos);
  31. extern ssize_t nsc_gpio_read(struct file *file, char __user *buf,
  32. size_t len, loff_t *ppos);
  33. struct nsc_gpio_ops scx200_access = {
  34. .owner = THIS_MODULE,
  35. .gpio_config = scx200_gpio_configure,
  36. .gpio_dump = scx200_gpio_dump,
  37. .gpio_get = scx200_gpio_get,
  38. .gpio_set = scx200_gpio_set,
  39. .gpio_set_high = scx200_gpio_set_high,
  40. .gpio_set_low = scx200_gpio_set_low,
  41. .gpio_change = scx200_gpio_change,
  42. .gpio_current = scx200_gpio_current
  43. };
  44. static int scx200_gpio_open(struct inode *inode, struct file *file)
  45. {
  46. unsigned m = iminor(inode);
  47. file->private_data = &scx200_access;
  48. if (m > 63)
  49. return -EINVAL;
  50. return nonseekable_open(inode, file);
  51. }
  52. static int scx200_gpio_release(struct inode *inode, struct file *file)
  53. {
  54. return 0;
  55. }
  56. static struct file_operations scx200_gpio_fops = {
  57. .owner = THIS_MODULE,
  58. .write = nsc_gpio_write,
  59. .read = nsc_gpio_read,
  60. .open = scx200_gpio_open,
  61. .release = scx200_gpio_release,
  62. };
  63. struct cdev *scx200_devices;
  64. static int num_pins = 32;
  65. static int __init scx200_gpio_init(void)
  66. {
  67. int rc, i;
  68. dev_t dev = MKDEV(major, 0);
  69. if (!scx200_gpio_present()) {
  70. printk(KERN_ERR NAME ": no SCx200 gpio present\n");
  71. return -ENODEV;
  72. }
  73. /* support dev_dbg() with pdev->dev */
  74. pdev = platform_device_alloc(DEVNAME, 0);
  75. if (!pdev)
  76. return -ENOMEM;
  77. rc = platform_device_add(pdev);
  78. if (rc)
  79. goto undo_malloc;
  80. if (major)
  81. rc = register_chrdev_region(dev, num_pins, "scx200_gpio");
  82. else {
  83. rc = alloc_chrdev_region(&dev, 0, num_pins, "scx200_gpio");
  84. major = MAJOR(dev);
  85. }
  86. if (rc < 0) {
  87. dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc);
  88. goto undo_platform_device_add;
  89. }
  90. scx200_devices = kzalloc(num_pins * sizeof(struct cdev), GFP_KERNEL);
  91. if (!scx200_devices) {
  92. rc = -ENOMEM;
  93. goto undo_chrdev_region;
  94. }
  95. for (i = 0; i < num_pins; i++) {
  96. struct cdev *cdev = &scx200_devices[i];
  97. cdev_init(cdev, &scx200_gpio_fops);
  98. cdev->owner = THIS_MODULE;
  99. rc = cdev_add(cdev, MKDEV(major, i), 1);
  100. /* tolerate 'minor' errors */
  101. if (rc)
  102. dev_err(&pdev->dev, "Error %d on minor %d", rc, i);
  103. }
  104. return 0; /* succeed */
  105. undo_chrdev_region:
  106. unregister_chrdev_region(dev, num_pins);
  107. undo_platform_device_add:
  108. platform_device_put(pdev);
  109. undo_malloc:
  110. kfree(pdev);
  111. return rc;
  112. }
  113. static void __exit scx200_gpio_cleanup(void)
  114. {
  115. kfree(scx200_devices);
  116. unregister_chrdev_region(MKDEV(major, 0), num_pins);
  117. platform_device_put(pdev);
  118. platform_device_unregister(pdev);
  119. /* kfree(pdev); */
  120. }
  121. module_init(scx200_gpio_init);
  122. module_exit(scx200_gpio_cleanup);