gpio-104-idi-48.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * GPIO driver for the ACCES 104-IDI-48 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-IDI-48A,
  15. * 104-IDI-48AC, 104-IDI-48B, and 104-IDI-48BC.
  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 IDI_48_EXTENT 8
  31. #define MAX_NUM_IDI_48 max_num_isa_dev(IDI_48_EXTENT)
  32. static unsigned int base[MAX_NUM_IDI_48];
  33. static unsigned int num_idi_48;
  34. module_param_array(base, uint, &num_idi_48, 0);
  35. MODULE_PARM_DESC(base, "ACCES 104-IDI-48 base addresses");
  36. static unsigned int irq[MAX_NUM_IDI_48];
  37. module_param_array(irq, uint, NULL, 0);
  38. MODULE_PARM_DESC(irq, "ACCES 104-IDI-48 interrupt line numbers");
  39. /**
  40. * struct idi_48_gpio - GPIO device private data structure
  41. * @chip: instance of the gpio_chip
  42. * @lock: synchronization lock to prevent I/O race conditions
  43. * @ack_lock: synchronization lock to prevent IRQ handler race conditions
  44. * @irq_mask: input bits affected by interrupts
  45. * @base: base port address of the GPIO device
  46. * @cos_enb: Change-Of-State IRQ enable boundaries mask
  47. */
  48. struct idi_48_gpio {
  49. struct gpio_chip chip;
  50. spinlock_t lock;
  51. spinlock_t ack_lock;
  52. unsigned char irq_mask[6];
  53. unsigned base;
  54. unsigned char cos_enb;
  55. };
  56. static int idi_48_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  57. {
  58. return 1;
  59. }
  60. static int idi_48_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  61. {
  62. return 0;
  63. }
  64. static int idi_48_gpio_get(struct gpio_chip *chip, unsigned offset)
  65. {
  66. struct idi_48_gpio *const idi48gpio = gpiochip_get_data(chip);
  67. unsigned i;
  68. const unsigned register_offset[6] = { 0, 1, 2, 4, 5, 6 };
  69. unsigned base_offset;
  70. unsigned mask;
  71. for (i = 0; i < 48; i += 8)
  72. if (offset < i + 8) {
  73. base_offset = register_offset[i / 8];
  74. mask = BIT(offset - i);
  75. return !!(inb(idi48gpio->base + base_offset) & mask);
  76. }
  77. /* The following line should never execute since offset < 48 */
  78. return 0;
  79. }
  80. static void idi_48_irq_ack(struct irq_data *data)
  81. {
  82. }
  83. static void idi_48_irq_mask(struct irq_data *data)
  84. {
  85. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  86. struct idi_48_gpio *const idi48gpio = gpiochip_get_data(chip);
  87. const unsigned offset = irqd_to_hwirq(data);
  88. unsigned i;
  89. unsigned mask;
  90. unsigned boundary;
  91. unsigned long flags;
  92. for (i = 0; i < 48; i += 8)
  93. if (offset < i + 8) {
  94. mask = BIT(offset - i);
  95. boundary = i / 8;
  96. idi48gpio->irq_mask[boundary] &= ~mask;
  97. if (!idi48gpio->irq_mask[boundary]) {
  98. idi48gpio->cos_enb &= ~BIT(boundary);
  99. spin_lock_irqsave(&idi48gpio->lock, flags);
  100. outb(idi48gpio->cos_enb, idi48gpio->base + 7);
  101. spin_unlock_irqrestore(&idi48gpio->lock, flags);
  102. }
  103. return;
  104. }
  105. }
  106. static void idi_48_irq_unmask(struct irq_data *data)
  107. {
  108. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  109. struct idi_48_gpio *const idi48gpio = gpiochip_get_data(chip);
  110. const unsigned offset = irqd_to_hwirq(data);
  111. unsigned i;
  112. unsigned mask;
  113. unsigned boundary;
  114. unsigned prev_irq_mask;
  115. unsigned long flags;
  116. for (i = 0; i < 48; i += 8)
  117. if (offset < i + 8) {
  118. mask = BIT(offset - i);
  119. boundary = i / 8;
  120. prev_irq_mask = idi48gpio->irq_mask[boundary];
  121. idi48gpio->irq_mask[boundary] |= mask;
  122. if (!prev_irq_mask) {
  123. idi48gpio->cos_enb |= BIT(boundary);
  124. spin_lock_irqsave(&idi48gpio->lock, flags);
  125. outb(idi48gpio->cos_enb, idi48gpio->base + 7);
  126. spin_unlock_irqrestore(&idi48gpio->lock, flags);
  127. }
  128. return;
  129. }
  130. }
  131. static int idi_48_irq_set_type(struct irq_data *data, unsigned flow_type)
  132. {
  133. /* The only valid irq types are none and both-edges */
  134. if (flow_type != IRQ_TYPE_NONE &&
  135. (flow_type & IRQ_TYPE_EDGE_BOTH) != IRQ_TYPE_EDGE_BOTH)
  136. return -EINVAL;
  137. return 0;
  138. }
  139. static struct irq_chip idi_48_irqchip = {
  140. .name = "104-idi-48",
  141. .irq_ack = idi_48_irq_ack,
  142. .irq_mask = idi_48_irq_mask,
  143. .irq_unmask = idi_48_irq_unmask,
  144. .irq_set_type = idi_48_irq_set_type
  145. };
  146. static irqreturn_t idi_48_irq_handler(int irq, void *dev_id)
  147. {
  148. struct idi_48_gpio *const idi48gpio = dev_id;
  149. unsigned long cos_status;
  150. unsigned long boundary;
  151. unsigned long irq_mask;
  152. unsigned long bit_num;
  153. unsigned long gpio;
  154. struct gpio_chip *const chip = &idi48gpio->chip;
  155. spin_lock(&idi48gpio->ack_lock);
  156. spin_lock(&idi48gpio->lock);
  157. cos_status = inb(idi48gpio->base + 7);
  158. spin_unlock(&idi48gpio->lock);
  159. /* IRQ Status (bit 6) is active low (0 = IRQ generated by device) */
  160. if (cos_status & BIT(6)) {
  161. spin_unlock(&idi48gpio->ack_lock);
  162. return IRQ_NONE;
  163. }
  164. /* Bit 0-5 indicate which Change-Of-State boundary triggered the IRQ */
  165. cos_status &= 0x3F;
  166. for_each_set_bit(boundary, &cos_status, 6) {
  167. irq_mask = idi48gpio->irq_mask[boundary];
  168. for_each_set_bit(bit_num, &irq_mask, 8) {
  169. gpio = bit_num + boundary * 8;
  170. generic_handle_irq(irq_find_mapping(chip->irqdomain,
  171. gpio));
  172. }
  173. }
  174. spin_unlock(&idi48gpio->ack_lock);
  175. return IRQ_HANDLED;
  176. }
  177. static int idi_48_probe(struct device *dev, unsigned int id)
  178. {
  179. struct idi_48_gpio *idi48gpio;
  180. const char *const name = dev_name(dev);
  181. int err;
  182. idi48gpio = devm_kzalloc(dev, sizeof(*idi48gpio), GFP_KERNEL);
  183. if (!idi48gpio)
  184. return -ENOMEM;
  185. if (!devm_request_region(dev, base[id], IDI_48_EXTENT, name)) {
  186. dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
  187. base[id], base[id] + IDI_48_EXTENT);
  188. return -EBUSY;
  189. }
  190. idi48gpio->chip.label = name;
  191. idi48gpio->chip.parent = dev;
  192. idi48gpio->chip.owner = THIS_MODULE;
  193. idi48gpio->chip.base = -1;
  194. idi48gpio->chip.ngpio = 48;
  195. idi48gpio->chip.get_direction = idi_48_gpio_get_direction;
  196. idi48gpio->chip.direction_input = idi_48_gpio_direction_input;
  197. idi48gpio->chip.get = idi_48_gpio_get;
  198. idi48gpio->base = base[id];
  199. spin_lock_init(&idi48gpio->lock);
  200. spin_lock_init(&idi48gpio->ack_lock);
  201. err = devm_gpiochip_add_data(dev, &idi48gpio->chip, idi48gpio);
  202. if (err) {
  203. dev_err(dev, "GPIO registering failed (%d)\n", err);
  204. return err;
  205. }
  206. /* Disable IRQ by default */
  207. outb(0, base[id] + 7);
  208. inb(base[id] + 7);
  209. err = gpiochip_irqchip_add(&idi48gpio->chip, &idi_48_irqchip, 0,
  210. handle_edge_irq, IRQ_TYPE_NONE);
  211. if (err) {
  212. dev_err(dev, "Could not add irqchip (%d)\n", err);
  213. return err;
  214. }
  215. err = devm_request_irq(dev, irq[id], idi_48_irq_handler, IRQF_SHARED,
  216. name, idi48gpio);
  217. if (err) {
  218. dev_err(dev, "IRQ handler registering failed (%d)\n", err);
  219. return err;
  220. }
  221. return 0;
  222. }
  223. static struct isa_driver idi_48_driver = {
  224. .probe = idi_48_probe,
  225. .driver = {
  226. .name = "104-idi-48"
  227. },
  228. };
  229. module_isa_driver(idi_48_driver, num_idi_48);
  230. MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
  231. MODULE_DESCRIPTION("ACCES 104-IDI-48 GPIO driver");
  232. MODULE_LICENSE("GPL v2");