scx200_gpio.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* linux/drivers/char/scx200_gpio.c
  3. National Semiconductor SCx200 GPIO driver. Allows a user space
  4. process to play with the GPIO pins.
  5. Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com> */
  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 <linux/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 DRVNAME "scx200_gpio"
  20. static struct platform_device *pdev;
  21. MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
  22. MODULE_DESCRIPTION("NatSemi/AMD 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. #define MAX_PINS 32 /* 64 later, when known ok */
  28. struct nsc_gpio_ops scx200_gpio_ops = {
  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_change = scx200_gpio_change,
  35. .gpio_current = scx200_gpio_current
  36. };
  37. EXPORT_SYMBOL_GPL(scx200_gpio_ops);
  38. static int scx200_gpio_open(struct inode *inode, struct file *file)
  39. {
  40. unsigned m = iminor(inode);
  41. file->private_data = &scx200_gpio_ops;
  42. if (m >= MAX_PINS)
  43. return -EINVAL;
  44. return nonseekable_open(inode, file);
  45. }
  46. static int scx200_gpio_release(struct inode *inode, struct file *file)
  47. {
  48. return 0;
  49. }
  50. static const struct file_operations scx200_gpio_fileops = {
  51. .owner = THIS_MODULE,
  52. .write = nsc_gpio_write,
  53. .read = nsc_gpio_read,
  54. .open = scx200_gpio_open,
  55. .release = scx200_gpio_release,
  56. .llseek = no_llseek,
  57. };
  58. static struct cdev scx200_gpio_cdev; /* use 1 cdev for all pins */
  59. static int __init scx200_gpio_init(void)
  60. {
  61. int rc;
  62. dev_t devid;
  63. if (!scx200_gpio_present()) {
  64. printk(KERN_ERR DRVNAME ": no SCx200 gpio present\n");
  65. return -ENODEV;
  66. }
  67. /* support dev_dbg() with pdev->dev */
  68. pdev = platform_device_alloc(DRVNAME, 0);
  69. if (!pdev)
  70. return -ENOMEM;
  71. rc = platform_device_add(pdev);
  72. if (rc)
  73. goto undo_malloc;
  74. /* nsc_gpio uses dev_dbg(), so needs this */
  75. scx200_gpio_ops.dev = &pdev->dev;
  76. if (major) {
  77. devid = MKDEV(major, 0);
  78. rc = register_chrdev_region(devid, MAX_PINS, "scx200_gpio");
  79. } else {
  80. rc = alloc_chrdev_region(&devid, 0, MAX_PINS, "scx200_gpio");
  81. major = MAJOR(devid);
  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. cdev_init(&scx200_gpio_cdev, &scx200_gpio_fileops);
  88. cdev_add(&scx200_gpio_cdev, devid, MAX_PINS);
  89. return 0; /* succeed */
  90. undo_platform_device_add:
  91. platform_device_del(pdev);
  92. undo_malloc:
  93. platform_device_put(pdev);
  94. return rc;
  95. }
  96. static void __exit scx200_gpio_cleanup(void)
  97. {
  98. cdev_del(&scx200_gpio_cdev);
  99. /* cdev_put(&scx200_gpio_cdev); */
  100. unregister_chrdev_region(MKDEV(major, 0), MAX_PINS);
  101. platform_device_unregister(pdev);
  102. }
  103. module_init(scx200_gpio_init);
  104. module_exit(scx200_gpio_cleanup);