scx200_gpio.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. struct nsc_gpio_ops scx200_access = {
  29. .owner = THIS_MODULE,
  30. .gpio_config = scx200_gpio_configure,
  31. .gpio_dump = nsc_gpio_dump,
  32. .gpio_get = scx200_gpio_get,
  33. .gpio_set = scx200_gpio_set,
  34. .gpio_set_high = scx200_gpio_set_high,
  35. .gpio_set_low = scx200_gpio_set_low,
  36. .gpio_change = scx200_gpio_change,
  37. .gpio_current = scx200_gpio_current
  38. };
  39. static int scx200_gpio_open(struct inode *inode, struct file *file)
  40. {
  41. unsigned m = iminor(inode);
  42. file->private_data = &scx200_access;
  43. if (m > 63)
  44. return -EINVAL;
  45. return nonseekable_open(inode, file);
  46. }
  47. static int scx200_gpio_release(struct inode *inode, struct file *file)
  48. {
  49. return 0;
  50. }
  51. static const struct file_operations scx200_gpio_fops = {
  52. .owner = THIS_MODULE,
  53. .write = nsc_gpio_write,
  54. .read = nsc_gpio_read,
  55. .open = scx200_gpio_open,
  56. .release = scx200_gpio_release,
  57. };
  58. struct cdev *scx200_devices;
  59. static int num_pins = 32;
  60. static int __init scx200_gpio_init(void)
  61. {
  62. int rc, i;
  63. dev_t dev = MKDEV(major, 0);
  64. if (!scx200_gpio_present()) {
  65. printk(KERN_ERR NAME ": no SCx200 gpio present\n");
  66. return -ENODEV;
  67. }
  68. /* support dev_dbg() with pdev->dev */
  69. pdev = platform_device_alloc(DEVNAME, 0);
  70. if (!pdev)
  71. return -ENOMEM;
  72. rc = platform_device_add(pdev);
  73. if (rc)
  74. goto undo_malloc;
  75. /* nsc_gpio uses dev_dbg(), so needs this */
  76. scx200_access.dev = &pdev->dev;
  77. if (major)
  78. rc = register_chrdev_region(dev, num_pins, "scx200_gpio");
  79. else {
  80. rc = alloc_chrdev_region(&dev, 0, num_pins, "scx200_gpio");
  81. major = MAJOR(dev);
  82. }
  83. if (rc < 0) {
  84. dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc);
  85. goto undo_platform_device_add;
  86. }
  87. scx200_devices = kzalloc(num_pins * sizeof(struct cdev), GFP_KERNEL);
  88. if (!scx200_devices) {
  89. rc = -ENOMEM;
  90. goto undo_chrdev_region;
  91. }
  92. for (i = 0; i < num_pins; i++) {
  93. struct cdev *cdev = &scx200_devices[i];
  94. cdev_init(cdev, &scx200_gpio_fops);
  95. cdev->owner = THIS_MODULE;
  96. rc = cdev_add(cdev, MKDEV(major, i), 1);
  97. /* tolerate 'minor' errors */
  98. if (rc)
  99. dev_err(&pdev->dev, "Error %d on minor %d", rc, i);
  100. }
  101. return 0; /* succeed */
  102. undo_chrdev_region:
  103. unregister_chrdev_region(dev, num_pins);
  104. undo_platform_device_add:
  105. platform_device_del(pdev);
  106. undo_malloc:
  107. platform_device_put(pdev);
  108. return rc;
  109. }
  110. static void __exit scx200_gpio_cleanup(void)
  111. {
  112. kfree(scx200_devices);
  113. unregister_chrdev_region(MKDEV(major, 0), num_pins);
  114. platform_device_unregister(pdev);
  115. /* kfree(pdev); */
  116. }
  117. module_init(scx200_gpio_init);
  118. module_exit(scx200_gpio_cleanup);