gpio-pci-idio-16.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * GPIO driver for the ACCES PCI-IDIO-16
  3. * Copyright (C) 2017 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/bitmap.h>
  15. #include <linux/bitops.h>
  16. #include <linux/device.h>
  17. #include <linux/errno.h>
  18. #include <linux/gpio/driver.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irqdesc.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/pci.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/types.h>
  26. /**
  27. * struct idio_16_gpio_reg - GPIO device registers structure
  28. * @out0_7: Read: FET Drive Outputs 0-7
  29. * Write: FET Drive Outputs 0-7
  30. * @in0_7: Read: Isolated Inputs 0-7
  31. * Write: Clear Interrupt
  32. * @irq_ctl: Read: Enable IRQ
  33. * Write: Disable IRQ
  34. * @filter_ctl: Read: Activate Input Filters 0-15
  35. * Write: Deactivate Input Filters 0-15
  36. * @out8_15: Read: FET Drive Outputs 8-15
  37. * Write: FET Drive Outputs 8-15
  38. * @in8_15: Read: Isolated Inputs 8-15
  39. * Write: Unused
  40. * @irq_status: Read: Interrupt status
  41. * Write: Unused
  42. */
  43. struct idio_16_gpio_reg {
  44. u8 out0_7;
  45. u8 in0_7;
  46. u8 irq_ctl;
  47. u8 filter_ctl;
  48. u8 out8_15;
  49. u8 in8_15;
  50. u8 irq_status;
  51. };
  52. /**
  53. * struct idio_16_gpio - GPIO device private data structure
  54. * @chip: instance of the gpio_chip
  55. * @lock: synchronization lock to prevent I/O race conditions
  56. * @reg: I/O address offset for the GPIO device registers
  57. * @irq_mask: I/O bits affected by interrupts
  58. */
  59. struct idio_16_gpio {
  60. struct gpio_chip chip;
  61. raw_spinlock_t lock;
  62. struct idio_16_gpio_reg __iomem *reg;
  63. unsigned long irq_mask;
  64. };
  65. static int idio_16_gpio_get_direction(struct gpio_chip *chip,
  66. unsigned int offset)
  67. {
  68. if (offset > 15)
  69. return 1;
  70. return 0;
  71. }
  72. static int idio_16_gpio_direction_input(struct gpio_chip *chip,
  73. unsigned int offset)
  74. {
  75. return 0;
  76. }
  77. static int idio_16_gpio_direction_output(struct gpio_chip *chip,
  78. unsigned int offset, int value)
  79. {
  80. chip->set(chip, offset, value);
  81. return 0;
  82. }
  83. static int idio_16_gpio_get(struct gpio_chip *chip, unsigned int offset)
  84. {
  85. struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
  86. unsigned long mask = BIT(offset);
  87. if (offset < 8)
  88. return !!(ioread8(&idio16gpio->reg->out0_7) & mask);
  89. if (offset < 16)
  90. return !!(ioread8(&idio16gpio->reg->out8_15) & (mask >> 8));
  91. if (offset < 24)
  92. return !!(ioread8(&idio16gpio->reg->in0_7) & (mask >> 16));
  93. return !!(ioread8(&idio16gpio->reg->in8_15) & (mask >> 24));
  94. }
  95. static int idio_16_gpio_get_multiple(struct gpio_chip *chip,
  96. unsigned long *mask, unsigned long *bits)
  97. {
  98. struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
  99. size_t i;
  100. const unsigned int gpio_reg_size = 8;
  101. unsigned int bits_offset;
  102. size_t word_index;
  103. unsigned int word_offset;
  104. unsigned long word_mask;
  105. const unsigned long port_mask = GENMASK(gpio_reg_size - 1, 0);
  106. unsigned long port_state;
  107. void __iomem *ports[] = {
  108. &idio16gpio->reg->out0_7, &idio16gpio->reg->out8_15,
  109. &idio16gpio->reg->in0_7, &idio16gpio->reg->in8_15,
  110. };
  111. /* clear bits array to a clean slate */
  112. bitmap_zero(bits, chip->ngpio);
  113. /* get bits are evaluated a gpio port register at a time */
  114. for (i = 0; i < ARRAY_SIZE(ports); i++) {
  115. /* gpio offset in bits array */
  116. bits_offset = i * gpio_reg_size;
  117. /* word index for bits array */
  118. word_index = BIT_WORD(bits_offset);
  119. /* gpio offset within current word of bits array */
  120. word_offset = bits_offset % BITS_PER_LONG;
  121. /* mask of get bits for current gpio within current word */
  122. word_mask = mask[word_index] & (port_mask << word_offset);
  123. if (!word_mask) {
  124. /* no get bits in this port so skip to next one */
  125. continue;
  126. }
  127. /* read bits from current gpio port */
  128. port_state = ioread8(ports[i]);
  129. /* store acquired bits at respective bits array offset */
  130. bits[word_index] |= port_state << word_offset;
  131. }
  132. return 0;
  133. }
  134. static void idio_16_gpio_set(struct gpio_chip *chip, unsigned int offset,
  135. int value)
  136. {
  137. struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
  138. unsigned int mask = BIT(offset);
  139. void __iomem *base;
  140. unsigned long flags;
  141. unsigned int out_state;
  142. if (offset > 15)
  143. return;
  144. if (offset > 7) {
  145. mask >>= 8;
  146. base = &idio16gpio->reg->out8_15;
  147. } else
  148. base = &idio16gpio->reg->out0_7;
  149. raw_spin_lock_irqsave(&idio16gpio->lock, flags);
  150. if (value)
  151. out_state = ioread8(base) | mask;
  152. else
  153. out_state = ioread8(base) & ~mask;
  154. iowrite8(out_state, base);
  155. raw_spin_unlock_irqrestore(&idio16gpio->lock, flags);
  156. }
  157. static void idio_16_gpio_set_multiple(struct gpio_chip *chip,
  158. unsigned long *mask, unsigned long *bits)
  159. {
  160. struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
  161. unsigned long flags;
  162. unsigned int out_state;
  163. raw_spin_lock_irqsave(&idio16gpio->lock, flags);
  164. /* process output lines 0-7 */
  165. if (*mask & 0xFF) {
  166. out_state = ioread8(&idio16gpio->reg->out0_7) & ~*mask;
  167. out_state |= *mask & *bits;
  168. iowrite8(out_state, &idio16gpio->reg->out0_7);
  169. }
  170. /* shift to next output line word */
  171. *mask >>= 8;
  172. /* process output lines 8-15 */
  173. if (*mask & 0xFF) {
  174. *bits >>= 8;
  175. out_state = ioread8(&idio16gpio->reg->out8_15) & ~*mask;
  176. out_state |= *mask & *bits;
  177. iowrite8(out_state, &idio16gpio->reg->out8_15);
  178. }
  179. raw_spin_unlock_irqrestore(&idio16gpio->lock, flags);
  180. }
  181. static void idio_16_irq_ack(struct irq_data *data)
  182. {
  183. }
  184. static void idio_16_irq_mask(struct irq_data *data)
  185. {
  186. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  187. struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
  188. const unsigned long mask = BIT(irqd_to_hwirq(data));
  189. unsigned long flags;
  190. idio16gpio->irq_mask &= ~mask;
  191. if (!idio16gpio->irq_mask) {
  192. raw_spin_lock_irqsave(&idio16gpio->lock, flags);
  193. iowrite8(0, &idio16gpio->reg->irq_ctl);
  194. raw_spin_unlock_irqrestore(&idio16gpio->lock, flags);
  195. }
  196. }
  197. static void idio_16_irq_unmask(struct irq_data *data)
  198. {
  199. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  200. struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
  201. const unsigned long mask = BIT(irqd_to_hwirq(data));
  202. const unsigned long prev_irq_mask = idio16gpio->irq_mask;
  203. unsigned long flags;
  204. idio16gpio->irq_mask |= mask;
  205. if (!prev_irq_mask) {
  206. raw_spin_lock_irqsave(&idio16gpio->lock, flags);
  207. ioread8(&idio16gpio->reg->irq_ctl);
  208. raw_spin_unlock_irqrestore(&idio16gpio->lock, flags);
  209. }
  210. }
  211. static int idio_16_irq_set_type(struct irq_data *data, unsigned int flow_type)
  212. {
  213. /* The only valid irq types are none and both-edges */
  214. if (flow_type != IRQ_TYPE_NONE &&
  215. (flow_type & IRQ_TYPE_EDGE_BOTH) != IRQ_TYPE_EDGE_BOTH)
  216. return -EINVAL;
  217. return 0;
  218. }
  219. static struct irq_chip idio_16_irqchip = {
  220. .name = "pci-idio-16",
  221. .irq_ack = idio_16_irq_ack,
  222. .irq_mask = idio_16_irq_mask,
  223. .irq_unmask = idio_16_irq_unmask,
  224. .irq_set_type = idio_16_irq_set_type
  225. };
  226. static irqreturn_t idio_16_irq_handler(int irq, void *dev_id)
  227. {
  228. struct idio_16_gpio *const idio16gpio = dev_id;
  229. unsigned int irq_status;
  230. struct gpio_chip *const chip = &idio16gpio->chip;
  231. int gpio;
  232. raw_spin_lock(&idio16gpio->lock);
  233. irq_status = ioread8(&idio16gpio->reg->irq_status);
  234. raw_spin_unlock(&idio16gpio->lock);
  235. /* Make sure our device generated IRQ */
  236. if (!(irq_status & 0x3) || !(irq_status & 0x4))
  237. return IRQ_NONE;
  238. for_each_set_bit(gpio, &idio16gpio->irq_mask, chip->ngpio)
  239. generic_handle_irq(irq_find_mapping(chip->irq.domain, gpio));
  240. raw_spin_lock(&idio16gpio->lock);
  241. /* Clear interrupt */
  242. iowrite8(0, &idio16gpio->reg->in0_7);
  243. raw_spin_unlock(&idio16gpio->lock);
  244. return IRQ_HANDLED;
  245. }
  246. #define IDIO_16_NGPIO 32
  247. static const char *idio_16_names[IDIO_16_NGPIO] = {
  248. "OUT0", "OUT1", "OUT2", "OUT3", "OUT4", "OUT5", "OUT6", "OUT7",
  249. "OUT8", "OUT9", "OUT10", "OUT11", "OUT12", "OUT13", "OUT14", "OUT15",
  250. "IIN0", "IIN1", "IIN2", "IIN3", "IIN4", "IIN5", "IIN6", "IIN7",
  251. "IIN8", "IIN9", "IIN10", "IIN11", "IIN12", "IIN13", "IIN14", "IIN15"
  252. };
  253. static int idio_16_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  254. {
  255. struct device *const dev = &pdev->dev;
  256. struct idio_16_gpio *idio16gpio;
  257. int err;
  258. const size_t pci_bar_index = 2;
  259. const char *const name = pci_name(pdev);
  260. idio16gpio = devm_kzalloc(dev, sizeof(*idio16gpio), GFP_KERNEL);
  261. if (!idio16gpio)
  262. return -ENOMEM;
  263. err = pcim_enable_device(pdev);
  264. if (err) {
  265. dev_err(dev, "Failed to enable PCI device (%d)\n", err);
  266. return err;
  267. }
  268. err = pcim_iomap_regions(pdev, BIT(pci_bar_index), name);
  269. if (err) {
  270. dev_err(dev, "Unable to map PCI I/O addresses (%d)\n", err);
  271. return err;
  272. }
  273. idio16gpio->reg = pcim_iomap_table(pdev)[pci_bar_index];
  274. /* Deactivate input filters */
  275. iowrite8(0, &idio16gpio->reg->filter_ctl);
  276. idio16gpio->chip.label = name;
  277. idio16gpio->chip.parent = dev;
  278. idio16gpio->chip.owner = THIS_MODULE;
  279. idio16gpio->chip.base = -1;
  280. idio16gpio->chip.ngpio = IDIO_16_NGPIO;
  281. idio16gpio->chip.names = idio_16_names;
  282. idio16gpio->chip.get_direction = idio_16_gpio_get_direction;
  283. idio16gpio->chip.direction_input = idio_16_gpio_direction_input;
  284. idio16gpio->chip.direction_output = idio_16_gpio_direction_output;
  285. idio16gpio->chip.get = idio_16_gpio_get;
  286. idio16gpio->chip.get_multiple = idio_16_gpio_get_multiple;
  287. idio16gpio->chip.set = idio_16_gpio_set;
  288. idio16gpio->chip.set_multiple = idio_16_gpio_set_multiple;
  289. raw_spin_lock_init(&idio16gpio->lock);
  290. err = devm_gpiochip_add_data(dev, &idio16gpio->chip, idio16gpio);
  291. if (err) {
  292. dev_err(dev, "GPIO registering failed (%d)\n", err);
  293. return err;
  294. }
  295. /* Disable IRQ by default and clear any pending interrupt */
  296. iowrite8(0, &idio16gpio->reg->irq_ctl);
  297. iowrite8(0, &idio16gpio->reg->in0_7);
  298. err = gpiochip_irqchip_add(&idio16gpio->chip, &idio_16_irqchip, 0,
  299. handle_edge_irq, IRQ_TYPE_NONE);
  300. if (err) {
  301. dev_err(dev, "Could not add irqchip (%d)\n", err);
  302. return err;
  303. }
  304. err = devm_request_irq(dev, pdev->irq, idio_16_irq_handler, IRQF_SHARED,
  305. name, idio16gpio);
  306. if (err) {
  307. dev_err(dev, "IRQ handler registering failed (%d)\n", err);
  308. return err;
  309. }
  310. return 0;
  311. }
  312. static const struct pci_device_id idio_16_pci_dev_id[] = {
  313. { PCI_DEVICE(0x494F, 0x0DC8) }, { 0 }
  314. };
  315. MODULE_DEVICE_TABLE(pci, idio_16_pci_dev_id);
  316. static struct pci_driver idio_16_driver = {
  317. .name = "pci-idio-16",
  318. .id_table = idio_16_pci_dev_id,
  319. .probe = idio_16_probe
  320. };
  321. module_pci_driver(idio_16_driver);
  322. MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
  323. MODULE_DESCRIPTION("ACCES PCI-IDIO-16 GPIO driver");
  324. MODULE_LICENSE("GPL v2");