scx200_gpio.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/device.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 <linux/platform_device.h>
  12. #include <asm/uaccess.h>
  13. #include <asm/io.h>
  14. #include <linux/types.h>
  15. #include <linux/cdev.h>
  16. #include <linux/scx200_gpio.h>
  17. #include <linux/nsc_gpio.h>
  18. #define DRVNAME "scx200_gpio"
  19. static struct platform_device *pdev;
  20. MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
  21. MODULE_DESCRIPTION("NatSemi/AMD SCx200 GPIO Pin Driver");
  22. MODULE_LICENSE("GPL");
  23. static int major = 0; /* default to dynamic major */
  24. module_param(major, int, 0);
  25. MODULE_PARM_DESC(major, "Major device number");
  26. #define MAX_PINS 32 /* 64 later, when known ok */
  27. struct nsc_gpio_ops scx200_gpio_ops = {
  28. .owner = THIS_MODULE,
  29. .gpio_config = scx200_gpio_configure,
  30. .gpio_dump = nsc_gpio_dump,
  31. .gpio_get = scx200_gpio_get,
  32. .gpio_set = scx200_gpio_set,
  33. .gpio_change = scx200_gpio_change,
  34. .gpio_current = scx200_gpio_current
  35. };
  36. EXPORT_SYMBOL_GPL(scx200_gpio_ops);
  37. static int scx200_gpio_open(struct inode *inode, struct file *file)
  38. {
  39. unsigned m = iminor(inode);
  40. file->private_data = &scx200_gpio_ops;
  41. if (m >= MAX_PINS)
  42. return -EINVAL;
  43. return nonseekable_open(inode, file);
  44. }
  45. static int scx200_gpio_release(struct inode *inode, struct file *file)
  46. {
  47. return 0;
  48. }
  49. static const struct file_operations scx200_gpio_fileops = {
  50. .owner = THIS_MODULE,
  51. .write = nsc_gpio_write,
  52. .read = nsc_gpio_read,
  53. .open = scx200_gpio_open,
  54. .release = scx200_gpio_release,
  55. };
  56. static struct cdev scx200_gpio_cdev; /* use 1 cdev for all pins */
  57. static int __init scx200_gpio_init(void)
  58. {
  59. int rc;
  60. dev_t devid;
  61. if (!scx200_gpio_present()) {
  62. printk(KERN_ERR DRVNAME ": no SCx200 gpio present\n");
  63. return -ENODEV;
  64. }
  65. /* support dev_dbg() with pdev->dev */
  66. pdev = platform_device_alloc(DRVNAME, 0);
  67. if (!pdev)
  68. return -ENOMEM;
  69. rc = platform_device_add(pdev);
  70. if (rc)
  71. goto undo_malloc;
  72. /* nsc_gpio uses dev_dbg(), so needs this */
  73. scx200_gpio_ops.dev = &pdev->dev;
  74. if (major) {
  75. devid = MKDEV(major, 0);
  76. rc = register_chrdev_region(devid, MAX_PINS, "scx200_gpio");
  77. } else {
  78. rc = alloc_chrdev_region(&devid, 0, MAX_PINS, "scx200_gpio");
  79. major = MAJOR(devid);
  80. }
  81. if (rc < 0) {
  82. dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc);
  83. goto undo_platform_device_add;
  84. }
  85. cdev_init(&scx200_gpio_cdev, &scx200_gpio_fileops);
  86. cdev_add(&scx200_gpio_cdev, devid, MAX_PINS);
  87. return 0; /* succeed */
  88. undo_platform_device_add:
  89. platform_device_del(pdev);
  90. undo_malloc:
  91. platform_device_put(pdev);
  92. return rc;
  93. }
  94. static void __exit scx200_gpio_cleanup(void)
  95. {
  96. cdev_del(&scx200_gpio_cdev);
  97. /* cdev_put(&scx200_gpio_cdev); */
  98. unregister_chrdev_region(MKDEV(major, 0), MAX_PINS);
  99. platform_device_unregister(pdev);
  100. }
  101. module_init(scx200_gpio_init);
  102. module_exit(scx200_gpio_cleanup);