gpio-104-idio-16.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. * This driver supports the following ACCES devices: 104-IDIO-16,
  15. * 104-IDIO-16E, 104-IDO-16, 104-IDIO-8, 104-IDIO-8E, and 104-IDO-8.
  16. */
  17. #include <linux/bitops.h>
  18. #include <linux/device.h>
  19. #include <linux/errno.h>
  20. #include <linux/gpio/driver.h>
  21. #include <linux/io.h>
  22. #include <linux/ioport.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irqdesc.h>
  25. #include <linux/isa.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/spinlock.h>
  30. #define IDIO_16_EXTENT 8
  31. #define MAX_NUM_IDIO_16 max_num_isa_dev(IDIO_16_EXTENT)
  32. static unsigned int base[MAX_NUM_IDIO_16];
  33. static unsigned int num_idio_16;
  34. module_param_array(base, uint, &num_idio_16, 0);
  35. MODULE_PARM_DESC(base, "ACCES 104-IDIO-16 base addresses");
  36. static unsigned int irq[MAX_NUM_IDIO_16];
  37. module_param_array(irq, uint, NULL, 0);
  38. MODULE_PARM_DESC(irq, "ACCES 104-IDIO-16 interrupt line numbers");
  39. /**
  40. * struct idio_16_gpio - GPIO device private data structure
  41. * @chip: instance of the gpio_chip
  42. * @lock: synchronization lock to prevent I/O race conditions
  43. * @irq_mask: I/O bits affected by interrupts
  44. * @base: base port address of the GPIO device
  45. * @irq: Interrupt line number
  46. * @out_state: output bits state
  47. */
  48. struct idio_16_gpio {
  49. struct gpio_chip chip;
  50. spinlock_t lock;
  51. unsigned long irq_mask;
  52. unsigned base;
  53. unsigned irq;
  54. unsigned out_state;
  55. };
  56. static int idio_16_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  57. {
  58. if (offset > 15)
  59. return 1;
  60. return 0;
  61. }
  62. static int idio_16_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  63. {
  64. return 0;
  65. }
  66. static int idio_16_gpio_direction_output(struct gpio_chip *chip,
  67. unsigned offset, int value)
  68. {
  69. chip->set(chip, offset, value);
  70. return 0;
  71. }
  72. static int idio_16_gpio_get(struct gpio_chip *chip, unsigned offset)
  73. {
  74. struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
  75. const unsigned mask = BIT(offset-16);
  76. if (offset < 16)
  77. return -EINVAL;
  78. if (offset < 24)
  79. return !!(inb(idio16gpio->base + 1) & mask);
  80. return !!(inb(idio16gpio->base + 5) & (mask>>8));
  81. }
  82. static void idio_16_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  83. {
  84. struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
  85. const unsigned mask = BIT(offset);
  86. unsigned long flags;
  87. if (offset > 15)
  88. return;
  89. spin_lock_irqsave(&idio16gpio->lock, flags);
  90. if (value)
  91. idio16gpio->out_state |= mask;
  92. else
  93. idio16gpio->out_state &= ~mask;
  94. if (offset > 7)
  95. outb(idio16gpio->out_state >> 8, idio16gpio->base + 4);
  96. else
  97. outb(idio16gpio->out_state, idio16gpio->base);
  98. spin_unlock_irqrestore(&idio16gpio->lock, flags);
  99. }
  100. static void idio_16_irq_ack(struct irq_data *data)
  101. {
  102. }
  103. static void idio_16_irq_mask(struct irq_data *data)
  104. {
  105. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  106. struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
  107. const unsigned long mask = BIT(irqd_to_hwirq(data));
  108. unsigned long flags;
  109. idio16gpio->irq_mask &= ~mask;
  110. if (!idio16gpio->irq_mask) {
  111. spin_lock_irqsave(&idio16gpio->lock, flags);
  112. outb(0, idio16gpio->base + 2);
  113. spin_unlock_irqrestore(&idio16gpio->lock, flags);
  114. }
  115. }
  116. static void idio_16_irq_unmask(struct irq_data *data)
  117. {
  118. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  119. struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
  120. const unsigned long mask = BIT(irqd_to_hwirq(data));
  121. const unsigned long prev_irq_mask = idio16gpio->irq_mask;
  122. unsigned long flags;
  123. idio16gpio->irq_mask |= mask;
  124. if (!prev_irq_mask) {
  125. spin_lock_irqsave(&idio16gpio->lock, flags);
  126. inb(idio16gpio->base + 2);
  127. spin_unlock_irqrestore(&idio16gpio->lock, flags);
  128. }
  129. }
  130. static int idio_16_irq_set_type(struct irq_data *data, unsigned flow_type)
  131. {
  132. /* The only valid irq types are none and both-edges */
  133. if (flow_type != IRQ_TYPE_NONE &&
  134. (flow_type & IRQ_TYPE_EDGE_BOTH) != IRQ_TYPE_EDGE_BOTH)
  135. return -EINVAL;
  136. return 0;
  137. }
  138. static struct irq_chip idio_16_irqchip = {
  139. .name = "104-idio-16",
  140. .irq_ack = idio_16_irq_ack,
  141. .irq_mask = idio_16_irq_mask,
  142. .irq_unmask = idio_16_irq_unmask,
  143. .irq_set_type = idio_16_irq_set_type
  144. };
  145. static irqreturn_t idio_16_irq_handler(int irq, void *dev_id)
  146. {
  147. struct idio_16_gpio *const idio16gpio = dev_id;
  148. struct gpio_chip *const chip = &idio16gpio->chip;
  149. int gpio;
  150. for_each_set_bit(gpio, &idio16gpio->irq_mask, chip->ngpio)
  151. generic_handle_irq(irq_find_mapping(chip->irqdomain, gpio));
  152. spin_lock(&idio16gpio->lock);
  153. outb(0, idio16gpio->base + 1);
  154. spin_unlock(&idio16gpio->lock);
  155. return IRQ_HANDLED;
  156. }
  157. static int idio_16_probe(struct device *dev, unsigned int id)
  158. {
  159. struct idio_16_gpio *idio16gpio;
  160. const char *const name = dev_name(dev);
  161. int err;
  162. idio16gpio = devm_kzalloc(dev, sizeof(*idio16gpio), GFP_KERNEL);
  163. if (!idio16gpio)
  164. return -ENOMEM;
  165. if (!devm_request_region(dev, base[id], IDIO_16_EXTENT, name)) {
  166. dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
  167. base[id], base[id] + IDIO_16_EXTENT);
  168. return -EBUSY;
  169. }
  170. idio16gpio->chip.label = name;
  171. idio16gpio->chip.parent = dev;
  172. idio16gpio->chip.owner = THIS_MODULE;
  173. idio16gpio->chip.base = -1;
  174. idio16gpio->chip.ngpio = 32;
  175. idio16gpio->chip.get_direction = idio_16_gpio_get_direction;
  176. idio16gpio->chip.direction_input = idio_16_gpio_direction_input;
  177. idio16gpio->chip.direction_output = idio_16_gpio_direction_output;
  178. idio16gpio->chip.get = idio_16_gpio_get;
  179. idio16gpio->chip.set = idio_16_gpio_set;
  180. idio16gpio->base = base[id];
  181. idio16gpio->irq = irq[id];
  182. idio16gpio->out_state = 0xFFFF;
  183. spin_lock_init(&idio16gpio->lock);
  184. dev_set_drvdata(dev, idio16gpio);
  185. err = gpiochip_add_data(&idio16gpio->chip, idio16gpio);
  186. if (err) {
  187. dev_err(dev, "GPIO registering failed (%d)\n", err);
  188. return err;
  189. }
  190. /* Disable IRQ by default */
  191. outb(0, base[id] + 2);
  192. outb(0, base[id] + 1);
  193. err = gpiochip_irqchip_add(&idio16gpio->chip, &idio_16_irqchip, 0,
  194. handle_edge_irq, IRQ_TYPE_NONE);
  195. if (err) {
  196. dev_err(dev, "Could not add irqchip (%d)\n", err);
  197. goto err_gpiochip_remove;
  198. }
  199. err = request_irq(irq[id], idio_16_irq_handler, 0, name, idio16gpio);
  200. if (err) {
  201. dev_err(dev, "IRQ handler registering failed (%d)\n", err);
  202. goto err_gpiochip_remove;
  203. }
  204. return 0;
  205. err_gpiochip_remove:
  206. gpiochip_remove(&idio16gpio->chip);
  207. return err;
  208. }
  209. static int idio_16_remove(struct device *dev, unsigned int id)
  210. {
  211. struct idio_16_gpio *const idio16gpio = dev_get_drvdata(dev);
  212. free_irq(idio16gpio->irq, idio16gpio);
  213. gpiochip_remove(&idio16gpio->chip);
  214. return 0;
  215. }
  216. static struct isa_driver idio_16_driver = {
  217. .probe = idio_16_probe,
  218. .driver = {
  219. .name = "104-idio-16"
  220. },
  221. .remove = idio_16_remove
  222. };
  223. module_isa_driver(idio_16_driver, num_idio_16);
  224. MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
  225. MODULE_DESCRIPTION("ACCES 104-IDIO-16 GPIO driver");
  226. MODULE_LICENSE("GPL v2");