gpio_dev.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * character device wrapper for generic gpio layer
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
  17. *
  18. * Feedback, Bugs... [email protected]
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/errno.h>
  23. #include <linux/init.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/io.h>
  26. #include <asm/gpio.h>
  27. #include <asm/atomic.h>
  28. #include <linux/init.h>
  29. #include <linux/genhd.h>
  30. #include <linux/device.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/gpio_dev.h>
  33. #define DRVNAME "gpiodev"
  34. #define DEVNAME "gpio"
  35. static int dev_major;
  36. static unsigned int gpio_access_mask;
  37. static struct class *gpiodev_class;
  38. /* Counter is 1, if the device is not opened and zero (or less) if opened. */
  39. static atomic_t gpio_open_cnt = ATOMIC_INIT(1);
  40. static int
  41. gpio_ioctl(struct inode * inode, struct file * file, unsigned int cmd, unsigned long arg)
  42. {
  43. int retval = 0;
  44. if (((1 << arg) & gpio_access_mask) != (1 << arg))
  45. {
  46. retval = -EINVAL;
  47. goto out;
  48. }
  49. switch (cmd)
  50. {
  51. case GPIO_GET:
  52. retval = gpio_get_value(arg);
  53. break;
  54. case GPIO_SET:
  55. gpio_set_value(arg, 1);
  56. break;
  57. case GPIO_CLEAR:
  58. gpio_set_value(arg, 0);
  59. break;
  60. case GPIO_DIR_IN:
  61. gpio_direction_input(arg);
  62. break;
  63. case GPIO_DIR_OUT:
  64. gpio_direction_output(arg, 0);
  65. break;
  66. default:
  67. retval = -EINVAL;
  68. break;
  69. }
  70. out:
  71. return retval;
  72. }
  73. static int
  74. gpio_open(struct inode *inode, struct file *file)
  75. {
  76. int result = 0;
  77. unsigned int dev_minor = MINOR(inode->i_rdev);
  78. if (dev_minor != 0)
  79. {
  80. printk(KERN_ERR DRVNAME ": trying to access unknown minor device -> %d\n", dev_minor);
  81. result = -ENODEV;
  82. goto out;
  83. }
  84. /* FIXME: We should really allow multiple applications to open the device
  85. * at the same time, as long as the apps access different IO pins.
  86. * The generic gpio-registration functions can be used for that.
  87. * Two new IOCTLs have to be introduced for that. Need to check userspace
  88. * compatibility first. --mb */
  89. if (!atomic_dec_and_test(&gpio_open_cnt)) {
  90. atomic_inc(&gpio_open_cnt);
  91. printk(KERN_ERR DRVNAME ": Device with minor ID %d already in use\n", dev_minor);
  92. result = -EBUSY;
  93. goto out;
  94. }
  95. out:
  96. return result;
  97. }
  98. static int
  99. gpio_close(struct inode * inode, struct file * file)
  100. {
  101. smp_mb__before_atomic_inc();
  102. atomic_inc(&gpio_open_cnt);
  103. return 0;
  104. }
  105. struct file_operations gpio_fops = {
  106. ioctl: gpio_ioctl,
  107. open: gpio_open,
  108. release: gpio_close
  109. };
  110. static int
  111. gpio_probe(struct platform_device *dev)
  112. {
  113. int result = 0;
  114. dev_major = register_chrdev(0, DEVNAME, &gpio_fops);
  115. if (!dev_major)
  116. {
  117. printk(KERN_ERR DRVNAME ": Error whilst opening %s \n", DEVNAME);
  118. result = -ENODEV;
  119. goto out;
  120. }
  121. gpiodev_class = class_create(THIS_MODULE, DRVNAME);
  122. class_device_create(gpiodev_class, NULL, MKDEV(dev_major, 0), NULL, DEVNAME);
  123. printk(KERN_INFO DRVNAME ": gpio device registered with major %d\n", dev_major);
  124. if (dev->num_resources != 1)
  125. {
  126. printk(KERN_ERR DRVNAME ": device may only have 1 resource\n");
  127. result = -ENODEV;
  128. goto out;
  129. }
  130. gpio_access_mask = dev->resource[0].start;
  131. printk(KERN_INFO DRVNAME ": gpio platform device registered with access mask %08X\n", gpio_access_mask);
  132. out:
  133. return result;
  134. }
  135. static int
  136. gpio_remove(struct platform_device *dev)
  137. {
  138. unregister_chrdev(dev_major, DEVNAME);
  139. return 0;
  140. }
  141. static struct
  142. platform_driver gpio_driver = {
  143. .probe = gpio_probe,
  144. .remove = gpio_remove,
  145. .driver = {
  146. .name = "GPIODEV",
  147. .owner = THIS_MODULE,
  148. },
  149. };
  150. static int __init
  151. gpio_mod_init(void)
  152. {
  153. int ret = platform_driver_register(&gpio_driver);
  154. if (ret)
  155. printk(KERN_INFO DRVNAME ": Error registering platfom driver!");
  156. return ret;
  157. }
  158. static void __exit
  159. gpio_mod_exit(void)
  160. {
  161. platform_driver_unregister(&gpio_driver);
  162. }
  163. module_init (gpio_mod_init);
  164. module_exit (gpio_mod_exit);
  165. MODULE_LICENSE("GPL");
  166. MODULE_AUTHOR("John Crispin / OpenWrt");
  167. MODULE_DESCRIPTION("Character device for for generic gpio api");