gpio-104-idi-48.c 7.6 KB

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