gpio-104-idio-16.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * GPIO driver for the ACCES 104-IDIO-16 family
  3. * Copyright (C) 2015 William Breathitt Gray
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License, version 2, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. */
  14. #include <linux/device.h>
  15. #include <linux/errno.h>
  16. #include <linux/gpio/driver.h>
  17. #include <linux/io.h>
  18. #include <linux/ioport.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/spinlock.h>
  24. static unsigned idio_16_base;
  25. module_param(idio_16_base, uint, 0);
  26. MODULE_PARM_DESC(idio_16_base, "ACCES 104-IDIO-16 base address");
  27. /**
  28. * struct idio_16_gpio - GPIO device private data structure
  29. * @chip: instance of the gpio_chip
  30. * @lock: synchronization lock to prevent gpio_set race conditions
  31. * @base: base port address of the GPIO device
  32. * @extent: extent of port address region of the GPIO device
  33. * @out_state: output bits state
  34. */
  35. struct idio_16_gpio {
  36. struct gpio_chip chip;
  37. spinlock_t lock;
  38. unsigned base;
  39. unsigned extent;
  40. unsigned out_state;
  41. };
  42. static int idio_16_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  43. {
  44. if (offset > 15)
  45. return 1;
  46. return 0;
  47. }
  48. static int idio_16_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  49. {
  50. return 0;
  51. }
  52. static int idio_16_gpio_direction_output(struct gpio_chip *chip,
  53. unsigned offset, int value)
  54. {
  55. chip->set(chip, offset, value);
  56. return 0;
  57. }
  58. static struct idio_16_gpio *to_idio16gpio(struct gpio_chip *gc)
  59. {
  60. return container_of(gc, struct idio_16_gpio, chip);
  61. }
  62. static int idio_16_gpio_get(struct gpio_chip *chip, unsigned offset)
  63. {
  64. struct idio_16_gpio *const idio16gpio = to_idio16gpio(chip);
  65. const unsigned BIT_MASK = 1U << (offset-16);
  66. if (offset < 16)
  67. return -EINVAL;
  68. if (offset < 24)
  69. return !!(inb(idio16gpio->base + 1) & BIT_MASK);
  70. return !!(inb(idio16gpio->base + 5) & (BIT_MASK>>8));
  71. }
  72. static void idio_16_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  73. {
  74. struct idio_16_gpio *const idio16gpio = to_idio16gpio(chip);
  75. const unsigned BIT_MASK = 1U << offset;
  76. unsigned long flags;
  77. if (offset > 15)
  78. return;
  79. spin_lock_irqsave(&idio16gpio->lock, flags);
  80. if (value)
  81. idio16gpio->out_state |= BIT_MASK;
  82. else
  83. idio16gpio->out_state &= ~BIT_MASK;
  84. if (offset > 7)
  85. outb(idio16gpio->out_state >> 8, idio16gpio->base + 4);
  86. else
  87. outb(idio16gpio->out_state, idio16gpio->base);
  88. spin_unlock_irqrestore(&idio16gpio->lock, flags);
  89. }
  90. static int __init idio_16_probe(struct platform_device *pdev)
  91. {
  92. struct device *dev = &pdev->dev;
  93. struct idio_16_gpio *idio16gpio;
  94. int err;
  95. const unsigned BASE = idio_16_base;
  96. const unsigned EXTENT = 8;
  97. const char *const NAME = dev_name(dev);
  98. idio16gpio = devm_kzalloc(dev, sizeof(*idio16gpio), GFP_KERNEL);
  99. if (!idio16gpio)
  100. return -ENOMEM;
  101. if (!request_region(BASE, EXTENT, NAME)) {
  102. dev_err(dev, "Unable to lock %s port addresses (0x%X-0x%X)\n",
  103. NAME, BASE, BASE + EXTENT);
  104. err = -EBUSY;
  105. goto err_lock_io_port;
  106. }
  107. idio16gpio->chip.label = NAME;
  108. idio16gpio->chip.dev = dev;
  109. idio16gpio->chip.owner = THIS_MODULE;
  110. idio16gpio->chip.base = -1;
  111. idio16gpio->chip.ngpio = 32;
  112. idio16gpio->chip.get_direction = idio_16_gpio_get_direction;
  113. idio16gpio->chip.direction_input = idio_16_gpio_direction_input;
  114. idio16gpio->chip.direction_output = idio_16_gpio_direction_output;
  115. idio16gpio->chip.get = idio_16_gpio_get;
  116. idio16gpio->chip.set = idio_16_gpio_set;
  117. idio16gpio->base = BASE;
  118. idio16gpio->extent = EXTENT;
  119. idio16gpio->out_state = 0xFFFF;
  120. spin_lock_init(&idio16gpio->lock);
  121. dev_set_drvdata(dev, idio16gpio);
  122. err = gpiochip_add(&idio16gpio->chip);
  123. if (err) {
  124. dev_err(dev, "GPIO registering failed (%d)\n", err);
  125. goto err_gpio_register;
  126. }
  127. return 0;
  128. err_gpio_register:
  129. release_region(BASE, EXTENT);
  130. err_lock_io_port:
  131. return err;
  132. }
  133. static int idio_16_remove(struct platform_device *pdev)
  134. {
  135. struct idio_16_gpio *const idio16gpio = platform_get_drvdata(pdev);
  136. gpiochip_remove(&idio16gpio->chip);
  137. release_region(idio16gpio->base, idio16gpio->extent);
  138. return 0;
  139. }
  140. static struct platform_device *idio_16_device;
  141. static struct platform_driver idio_16_driver = {
  142. .driver = {
  143. .name = "104-idio-16"
  144. },
  145. .remove = idio_16_remove
  146. };
  147. static void __exit idio_16_exit(void)
  148. {
  149. platform_device_unregister(idio_16_device);
  150. platform_driver_unregister(&idio_16_driver);
  151. }
  152. static int __init idio_16_init(void)
  153. {
  154. int err;
  155. idio_16_device = platform_device_alloc(idio_16_driver.driver.name, -1);
  156. if (!idio_16_device)
  157. return -ENOMEM;
  158. err = platform_device_add(idio_16_device);
  159. if (err)
  160. goto err_platform_device;
  161. err = platform_driver_probe(&idio_16_driver, idio_16_probe);
  162. if (err)
  163. goto err_platform_driver;
  164. return 0;
  165. err_platform_driver:
  166. platform_device_del(idio_16_device);
  167. err_platform_device:
  168. platform_device_put(idio_16_device);
  169. return err;
  170. }
  171. module_init(idio_16_init);
  172. module_exit(idio_16_exit);
  173. MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
  174. MODULE_DESCRIPTION("ACCES 104-IDIO-16 GPIO driver");
  175. MODULE_LICENSE("GPL");