scx200_gpio.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. .llseek = no_llseek,
  56. };
  57. static struct cdev scx200_gpio_cdev; /* use 1 cdev for all pins */
  58. static int __init scx200_gpio_init(void)
  59. {
  60. int rc;
  61. dev_t devid;
  62. if (!scx200_gpio_present()) {
  63. printk(KERN_ERR DRVNAME ": no SCx200 gpio present\n");
  64. return -ENODEV;
  65. }
  66. /* support dev_dbg() with pdev->dev */
  67. pdev = platform_device_alloc(DRVNAME, 0);
  68. if (!pdev)
  69. return -ENOMEM;
  70. rc = platform_device_add(pdev);
  71. if (rc)
  72. goto undo_malloc;
  73. /* nsc_gpio uses dev_dbg(), so needs this */
  74. scx200_gpio_ops.dev = &pdev->dev;
  75. if (major) {
  76. devid = MKDEV(major, 0);
  77. rc = register_chrdev_region(devid, MAX_PINS, "scx200_gpio");
  78. } else {
  79. rc = alloc_chrdev_region(&devid, 0, MAX_PINS, "scx200_gpio");
  80. major = MAJOR(devid);
  81. }
  82. if (rc < 0) {
  83. dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc);
  84. goto undo_platform_device_add;
  85. }
  86. cdev_init(&scx200_gpio_cdev, &scx200_gpio_fileops);
  87. cdev_add(&scx200_gpio_cdev, devid, MAX_PINS);
  88. return 0; /* succeed */
  89. undo_platform_device_add:
  90. platform_device_del(pdev);
  91. undo_malloc:
  92. platform_device_put(pdev);
  93. return rc;
  94. }
  95. static void __exit scx200_gpio_cleanup(void)
  96. {
  97. cdev_del(&scx200_gpio_cdev);
  98. /* cdev_put(&scx200_gpio_cdev); */
  99. unregister_chrdev_region(MKDEV(major, 0), MAX_PINS);
  100. platform_device_unregister(pdev);
  101. }
  102. module_init(scx200_gpio_init);
  103. module_exit(scx200_gpio_cleanup);