nsc_gpio.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* linux/drivers/char/nsc_gpio.c
  2. National Semiconductor common GPIO device-file/VFS methods.
  3. Allows a user space process to control the GPIO pins.
  4. Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
  5. Copyright (c) 2005 Jim Cromie <jim.cromie@gmail.com>
  6. */
  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/nsc_gpio.h>
  13. #include <linux/platform_device.h>
  14. #include <asm/uaccess.h>
  15. #include <asm/io.h>
  16. #define NAME "nsc_gpio"
  17. void nsc_gpio_dump(struct nsc_gpio_ops *amp, unsigned index)
  18. {
  19. /* retrieve current config w/o changing it */
  20. u32 config = amp->gpio_config(index, ~0, 0);
  21. /* user requested via 'v' command, so its INFO */
  22. dev_info(amp->dev, "io%02u: 0x%04x %s %s %s %s %s %s %s\tio:%d/%d\n",
  23. index, config,
  24. (config & 1) ? "OE" : "TS", /* output-enabled/tristate */
  25. (config & 2) ? "PP" : "OD", /* push pull / open drain */
  26. (config & 4) ? "PUE" : "PUD", /* pull up enabled/disabled */
  27. (config & 8) ? "LOCKED" : "", /* locked / unlocked */
  28. (config & 16) ? "LEVEL" : "EDGE",/* level/edge input */
  29. (config & 32) ? "HI" : "LO", /* trigger on rise/fall edge */
  30. (config & 64) ? "DEBOUNCE" : "", /* debounce */
  31. amp->gpio_get(index), amp->gpio_current(index));
  32. }
  33. ssize_t nsc_gpio_write(struct file *file, const char __user *data,
  34. size_t len, loff_t *ppos)
  35. {
  36. unsigned m = iminor(file_inode(file));
  37. struct nsc_gpio_ops *amp = file->private_data;
  38. struct device *dev = amp->dev;
  39. size_t i;
  40. int err = 0;
  41. for (i = 0; i < len; ++i) {
  42. char c;
  43. if (get_user(c, data + i))
  44. return -EFAULT;
  45. switch (c) {
  46. case '0':
  47. amp->gpio_set(m, 0);
  48. break;
  49. case '1':
  50. amp->gpio_set(m, 1);
  51. break;
  52. case 'O':
  53. dev_dbg(dev, "GPIO%d output enabled\n", m);
  54. amp->gpio_config(m, ~1, 1);
  55. break;
  56. case 'o':
  57. dev_dbg(dev, "GPIO%d output disabled\n", m);
  58. amp->gpio_config(m, ~1, 0);
  59. break;
  60. case 'T':
  61. dev_dbg(dev, "GPIO%d output is push pull\n", m);
  62. amp->gpio_config(m, ~2, 2);
  63. break;
  64. case 't':
  65. dev_dbg(dev, "GPIO%d output is open drain\n", m);
  66. amp->gpio_config(m, ~2, 0);
  67. break;
  68. case 'P':
  69. dev_dbg(dev, "GPIO%d pull up enabled\n", m);
  70. amp->gpio_config(m, ~4, 4);
  71. break;
  72. case 'p':
  73. dev_dbg(dev, "GPIO%d pull up disabled\n", m);
  74. amp->gpio_config(m, ~4, 0);
  75. break;
  76. case 'v':
  77. /* View Current pin settings */
  78. amp->gpio_dump(amp, m);
  79. break;
  80. case '\n':
  81. /* end of settings string, do nothing */
  82. break;
  83. default:
  84. dev_err(dev, "io%2d bad setting: chr<0x%2x>\n",
  85. m, (int)c);
  86. err++;
  87. }
  88. }
  89. if (err)
  90. return -EINVAL; /* full string handled, report error */
  91. return len;
  92. }
  93. ssize_t nsc_gpio_read(struct file *file, char __user * buf,
  94. size_t len, loff_t * ppos)
  95. {
  96. unsigned m = iminor(file_inode(file));
  97. int value;
  98. struct nsc_gpio_ops *amp = file->private_data;
  99. value = amp->gpio_get(m);
  100. if (put_user(value ? '1' : '0', buf))
  101. return -EFAULT;
  102. return 1;
  103. }
  104. /* common file-ops routines for both scx200_gpio and pc87360_gpio */
  105. EXPORT_SYMBOL(nsc_gpio_write);
  106. EXPORT_SYMBOL(nsc_gpio_read);
  107. EXPORT_SYMBOL(nsc_gpio_dump);
  108. static int __init nsc_gpio_init(void)
  109. {
  110. printk(KERN_DEBUG NAME " initializing\n");
  111. return 0;
  112. }
  113. static void __exit nsc_gpio_cleanup(void)
  114. {
  115. printk(KERN_DEBUG NAME " cleanup\n");
  116. }
  117. module_init(nsc_gpio_init);
  118. module_exit(nsc_gpio_cleanup);
  119. MODULE_AUTHOR("Jim Cromie <jim.cromie@gmail.com>");
  120. MODULE_DESCRIPTION("NatSemi GPIO Common Methods");
  121. MODULE_LICENSE("GPL");