gpio-104-idi-48.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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/bitmap.h>
  18. #include <linux/bitops.h>
  19. #include <linux/device.h>
  20. #include <linux/errno.h>
  21. #include <linux/gpio/driver.h>
  22. #include <linux/io.h>
  23. #include <linux/ioport.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/irqdesc.h>
  26. #include <linux/isa.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/spinlock.h>
  31. #define IDI_48_EXTENT 8
  32. #define MAX_NUM_IDI_48 max_num_isa_dev(IDI_48_EXTENT)
  33. static unsigned int base[MAX_NUM_IDI_48];
  34. static unsigned int num_idi_48;
  35. module_param_hw_array(base, uint, ioport, &num_idi_48, 0);
  36. MODULE_PARM_DESC(base, "ACCES 104-IDI-48 base addresses");
  37. static unsigned int irq[MAX_NUM_IDI_48];
  38. module_param_hw_array(irq, uint, irq, NULL, 0);
  39. MODULE_PARM_DESC(irq, "ACCES 104-IDI-48 interrupt line numbers");
  40. /**
  41. * struct idi_48_gpio - GPIO device private data structure
  42. * @chip: instance of the gpio_chip
  43. * @lock: synchronization lock to prevent I/O race conditions
  44. * @ack_lock: synchronization lock to prevent IRQ handler race conditions
  45. * @irq_mask: input bits affected by interrupts
  46. * @base: base port address of the GPIO device
  47. * @cos_enb: Change-Of-State IRQ enable boundaries mask
  48. */
  49. struct idi_48_gpio {
  50. struct gpio_chip chip;
  51. raw_spinlock_t lock;
  52. spinlock_t ack_lock;
  53. unsigned char irq_mask[6];
  54. unsigned base;
  55. unsigned char cos_enb;
  56. };
  57. static int idi_48_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  58. {
  59. return 1;
  60. }
  61. static int idi_48_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  62. {
  63. return 0;
  64. }
  65. static int idi_48_gpio_get(struct gpio_chip *chip, unsigned offset)
  66. {
  67. struct idi_48_gpio *const idi48gpio = gpiochip_get_data(chip);
  68. unsigned i;
  69. const unsigned register_offset[6] = { 0, 1, 2, 4, 5, 6 };
  70. unsigned base_offset;
  71. unsigned mask;
  72. for (i = 0; i < 48; i += 8)
  73. if (offset < i + 8) {
  74. base_offset = register_offset[i / 8];
  75. mask = BIT(offset - i);
  76. return !!(inb(idi48gpio->base + base_offset) & mask);
  77. }
  78. /* The following line should never execute since offset < 48 */
  79. return 0;
  80. }
  81. static int idi_48_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask,
  82. unsigned long *bits)
  83. {
  84. struct idi_48_gpio *const idi48gpio = gpiochip_get_data(chip);
  85. size_t i;
  86. static const size_t ports[] = { 0, 1, 2, 4, 5, 6 };
  87. const unsigned int gpio_reg_size = 8;
  88. unsigned int bits_offset;
  89. size_t word_index;
  90. unsigned int word_offset;
  91. unsigned long word_mask;
  92. const unsigned long port_mask = GENMASK(gpio_reg_size - 1, 0);
  93. unsigned long port_state;
  94. /* clear bits array to a clean slate */
  95. bitmap_zero(bits, chip->ngpio);
  96. /* get bits are evaluated a gpio port register at a time */
  97. for (i = 0; i < ARRAY_SIZE(ports); i++) {
  98. /* gpio offset in bits array */
  99. bits_offset = i * gpio_reg_size;
  100. /* word index for bits array */
  101. word_index = BIT_WORD(bits_offset);
  102. /* gpio offset within current word of bits array */
  103. word_offset = bits_offset % BITS_PER_LONG;
  104. /* mask of get bits for current gpio within current word */
  105. word_mask = mask[word_index] & (port_mask << word_offset);
  106. if (!word_mask) {
  107. /* no get bits in this port so skip to next one */
  108. continue;
  109. }
  110. /* read bits from current gpio port */
  111. port_state = inb(idi48gpio->base + ports[i]);
  112. /* store acquired bits at respective bits array offset */
  113. bits[word_index] |= port_state << word_offset;
  114. }
  115. return 0;
  116. }
  117. static void idi_48_irq_ack(struct irq_data *data)
  118. {
  119. }
  120. static void idi_48_irq_mask(struct irq_data *data)
  121. {
  122. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  123. struct idi_48_gpio *const idi48gpio = gpiochip_get_data(chip);
  124. const unsigned offset = irqd_to_hwirq(data);
  125. unsigned i;
  126. unsigned mask;
  127. unsigned boundary;
  128. unsigned long flags;
  129. for (i = 0; i < 48; i += 8)
  130. if (offset < i + 8) {
  131. mask = BIT(offset - i);
  132. boundary = i / 8;
  133. idi48gpio->irq_mask[boundary] &= ~mask;
  134. if (!idi48gpio->irq_mask[boundary]) {
  135. idi48gpio->cos_enb &= ~BIT(boundary);
  136. raw_spin_lock_irqsave(&idi48gpio->lock, flags);
  137. outb(idi48gpio->cos_enb, idi48gpio->base + 7);
  138. raw_spin_unlock_irqrestore(&idi48gpio->lock,
  139. flags);
  140. }
  141. return;
  142. }
  143. }
  144. static void idi_48_irq_unmask(struct irq_data *data)
  145. {
  146. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  147. struct idi_48_gpio *const idi48gpio = gpiochip_get_data(chip);
  148. const unsigned offset = irqd_to_hwirq(data);
  149. unsigned i;
  150. unsigned mask;
  151. unsigned boundary;
  152. unsigned prev_irq_mask;
  153. unsigned long flags;
  154. for (i = 0; i < 48; i += 8)
  155. if (offset < i + 8) {
  156. mask = BIT(offset - i);
  157. boundary = i / 8;
  158. prev_irq_mask = idi48gpio->irq_mask[boundary];
  159. idi48gpio->irq_mask[boundary] |= mask;
  160. if (!prev_irq_mask) {
  161. idi48gpio->cos_enb |= BIT(boundary);
  162. raw_spin_lock_irqsave(&idi48gpio->lock, flags);
  163. outb(idi48gpio->cos_enb, idi48gpio->base + 7);
  164. raw_spin_unlock_irqrestore(&idi48gpio->lock,
  165. flags);
  166. }
  167. return;
  168. }
  169. }
  170. static int idi_48_irq_set_type(struct irq_data *data, unsigned flow_type)
  171. {
  172. /* The only valid irq types are none and both-edges */
  173. if (flow_type != IRQ_TYPE_NONE &&
  174. (flow_type & IRQ_TYPE_EDGE_BOTH) != IRQ_TYPE_EDGE_BOTH)
  175. return -EINVAL;
  176. return 0;
  177. }
  178. static struct irq_chip idi_48_irqchip = {
  179. .name = "104-idi-48",
  180. .irq_ack = idi_48_irq_ack,
  181. .irq_mask = idi_48_irq_mask,
  182. .irq_unmask = idi_48_irq_unmask,
  183. .irq_set_type = idi_48_irq_set_type
  184. };
  185. static irqreturn_t idi_48_irq_handler(int irq, void *dev_id)
  186. {
  187. struct idi_48_gpio *const idi48gpio = dev_id;
  188. unsigned long cos_status;
  189. unsigned long boundary;
  190. unsigned long irq_mask;
  191. unsigned long bit_num;
  192. unsigned long gpio;
  193. struct gpio_chip *const chip = &idi48gpio->chip;
  194. spin_lock(&idi48gpio->ack_lock);
  195. raw_spin_lock(&idi48gpio->lock);
  196. cos_status = inb(idi48gpio->base + 7);
  197. raw_spin_unlock(&idi48gpio->lock);
  198. /* IRQ Status (bit 6) is active low (0 = IRQ generated by device) */
  199. if (cos_status & BIT(6)) {
  200. spin_unlock(&idi48gpio->ack_lock);
  201. return IRQ_NONE;
  202. }
  203. /* Bit 0-5 indicate which Change-Of-State boundary triggered the IRQ */
  204. cos_status &= 0x3F;
  205. for_each_set_bit(boundary, &cos_status, 6) {
  206. irq_mask = idi48gpio->irq_mask[boundary];
  207. for_each_set_bit(bit_num, &irq_mask, 8) {
  208. gpio = bit_num + boundary * 8;
  209. generic_handle_irq(irq_find_mapping(chip->irq.domain,
  210. gpio));
  211. }
  212. }
  213. spin_unlock(&idi48gpio->ack_lock);
  214. return IRQ_HANDLED;
  215. }
  216. #define IDI48_NGPIO 48
  217. static const char *idi48_names[IDI48_NGPIO] = {
  218. "Bit 0 A", "Bit 1 A", "Bit 2 A", "Bit 3 A", "Bit 4 A", "Bit 5 A",
  219. "Bit 6 A", "Bit 7 A", "Bit 8 A", "Bit 9 A", "Bit 10 A", "Bit 11 A",
  220. "Bit 12 A", "Bit 13 A", "Bit 14 A", "Bit 15 A", "Bit 16 A", "Bit 17 A",
  221. "Bit 18 A", "Bit 19 A", "Bit 20 A", "Bit 21 A", "Bit 22 A", "Bit 23 A",
  222. "Bit 0 B", "Bit 1 B", "Bit 2 B", "Bit 3 B", "Bit 4 B", "Bit 5 B",
  223. "Bit 6 B", "Bit 7 B", "Bit 8 B", "Bit 9 B", "Bit 10 B", "Bit 11 B",
  224. "Bit 12 B", "Bit 13 B", "Bit 14 B", "Bit 15 B", "Bit 16 B", "Bit 17 B",
  225. "Bit 18 B", "Bit 19 B", "Bit 20 B", "Bit 21 B", "Bit 22 B", "Bit 23 B"
  226. };
  227. static int idi_48_probe(struct device *dev, unsigned int id)
  228. {
  229. struct idi_48_gpio *idi48gpio;
  230. const char *const name = dev_name(dev);
  231. int err;
  232. idi48gpio = devm_kzalloc(dev, sizeof(*idi48gpio), GFP_KERNEL);
  233. if (!idi48gpio)
  234. return -ENOMEM;
  235. if (!devm_request_region(dev, base[id], IDI_48_EXTENT, name)) {
  236. dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
  237. base[id], base[id] + IDI_48_EXTENT);
  238. return -EBUSY;
  239. }
  240. idi48gpio->chip.label = name;
  241. idi48gpio->chip.parent = dev;
  242. idi48gpio->chip.owner = THIS_MODULE;
  243. idi48gpio->chip.base = -1;
  244. idi48gpio->chip.ngpio = IDI48_NGPIO;
  245. idi48gpio->chip.names = idi48_names;
  246. idi48gpio->chip.get_direction = idi_48_gpio_get_direction;
  247. idi48gpio->chip.direction_input = idi_48_gpio_direction_input;
  248. idi48gpio->chip.get = idi_48_gpio_get;
  249. idi48gpio->chip.get_multiple = idi_48_gpio_get_multiple;
  250. idi48gpio->base = base[id];
  251. raw_spin_lock_init(&idi48gpio->lock);
  252. spin_lock_init(&idi48gpio->ack_lock);
  253. err = devm_gpiochip_add_data(dev, &idi48gpio->chip, idi48gpio);
  254. if (err) {
  255. dev_err(dev, "GPIO registering failed (%d)\n", err);
  256. return err;
  257. }
  258. /* Disable IRQ by default */
  259. outb(0, base[id] + 7);
  260. inb(base[id] + 7);
  261. err = gpiochip_irqchip_add(&idi48gpio->chip, &idi_48_irqchip, 0,
  262. handle_edge_irq, IRQ_TYPE_NONE);
  263. if (err) {
  264. dev_err(dev, "Could not add irqchip (%d)\n", err);
  265. return err;
  266. }
  267. err = devm_request_irq(dev, irq[id], idi_48_irq_handler, IRQF_SHARED,
  268. name, idi48gpio);
  269. if (err) {
  270. dev_err(dev, "IRQ handler registering failed (%d)\n", err);
  271. return err;
  272. }
  273. return 0;
  274. }
  275. static struct isa_driver idi_48_driver = {
  276. .probe = idi_48_probe,
  277. .driver = {
  278. .name = "104-idi-48"
  279. },
  280. };
  281. module_isa_driver(idi_48_driver, num_idi_48);
  282. MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
  283. MODULE_DESCRIPTION("ACCES 104-IDI-48 GPIO driver");
  284. MODULE_LICENSE("GPL v2");