gpio-104-dio-48e.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * GPIO driver for the ACCES 104-DIO-48E series
  3. * Copyright (C) 2016 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-DIO-48E and
  15. * 104-DIO-24E.
  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 DIO48E_EXTENT 16
  31. #define MAX_NUM_DIO48E max_num_isa_dev(DIO48E_EXTENT)
  32. static unsigned int base[MAX_NUM_DIO48E];
  33. static unsigned int num_dio48e;
  34. module_param_hw_array(base, uint, ioport, &num_dio48e, 0);
  35. MODULE_PARM_DESC(base, "ACCES 104-DIO-48E base addresses");
  36. static unsigned int irq[MAX_NUM_DIO48E];
  37. module_param_hw_array(irq, uint, irq, NULL, 0);
  38. MODULE_PARM_DESC(irq, "ACCES 104-DIO-48E interrupt line numbers");
  39. /**
  40. * struct dio48e_gpio - GPIO device private data structure
  41. * @chip: instance of the gpio_chip
  42. * @io_state: bit I/O state (whether bit is set to input or output)
  43. * @out_state: output bits state
  44. * @control: Control registers state
  45. * @lock: synchronization lock to prevent I/O race conditions
  46. * @base: base port address of the GPIO device
  47. * @irq_mask: I/O bits affected by interrupts
  48. */
  49. struct dio48e_gpio {
  50. struct gpio_chip chip;
  51. unsigned char io_state[6];
  52. unsigned char out_state[6];
  53. unsigned char control[2];
  54. raw_spinlock_t lock;
  55. unsigned base;
  56. unsigned char irq_mask;
  57. };
  58. static int dio48e_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  59. {
  60. struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
  61. const unsigned port = offset / 8;
  62. const unsigned mask = BIT(offset % 8);
  63. return !!(dio48egpio->io_state[port] & mask);
  64. }
  65. static int dio48e_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  66. {
  67. struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
  68. const unsigned io_port = offset / 8;
  69. const unsigned int control_port = io_port / 3;
  70. const unsigned control_addr = dio48egpio->base + 3 + control_port*4;
  71. unsigned long flags;
  72. unsigned control;
  73. raw_spin_lock_irqsave(&dio48egpio->lock, flags);
  74. /* Check if configuring Port C */
  75. if (io_port == 2 || io_port == 5) {
  76. /* Port C can be configured by nibble */
  77. if (offset % 8 > 3) {
  78. dio48egpio->io_state[io_port] |= 0xF0;
  79. dio48egpio->control[control_port] |= BIT(3);
  80. } else {
  81. dio48egpio->io_state[io_port] |= 0x0F;
  82. dio48egpio->control[control_port] |= BIT(0);
  83. }
  84. } else {
  85. dio48egpio->io_state[io_port] |= 0xFF;
  86. if (io_port == 0 || io_port == 3)
  87. dio48egpio->control[control_port] |= BIT(4);
  88. else
  89. dio48egpio->control[control_port] |= BIT(1);
  90. }
  91. control = BIT(7) | dio48egpio->control[control_port];
  92. outb(control, control_addr);
  93. control &= ~BIT(7);
  94. outb(control, control_addr);
  95. raw_spin_unlock_irqrestore(&dio48egpio->lock, flags);
  96. return 0;
  97. }
  98. static int dio48e_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  99. int value)
  100. {
  101. struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
  102. const unsigned io_port = offset / 8;
  103. const unsigned int control_port = io_port / 3;
  104. const unsigned mask = BIT(offset % 8);
  105. const unsigned control_addr = dio48egpio->base + 3 + control_port*4;
  106. const unsigned out_port = (io_port > 2) ? io_port + 1 : io_port;
  107. unsigned long flags;
  108. unsigned control;
  109. raw_spin_lock_irqsave(&dio48egpio->lock, flags);
  110. /* Check if configuring Port C */
  111. if (io_port == 2 || io_port == 5) {
  112. /* Port C can be configured by nibble */
  113. if (offset % 8 > 3) {
  114. dio48egpio->io_state[io_port] &= 0x0F;
  115. dio48egpio->control[control_port] &= ~BIT(3);
  116. } else {
  117. dio48egpio->io_state[io_port] &= 0xF0;
  118. dio48egpio->control[control_port] &= ~BIT(0);
  119. }
  120. } else {
  121. dio48egpio->io_state[io_port] &= 0x00;
  122. if (io_port == 0 || io_port == 3)
  123. dio48egpio->control[control_port] &= ~BIT(4);
  124. else
  125. dio48egpio->control[control_port] &= ~BIT(1);
  126. }
  127. if (value)
  128. dio48egpio->out_state[io_port] |= mask;
  129. else
  130. dio48egpio->out_state[io_port] &= ~mask;
  131. control = BIT(7) | dio48egpio->control[control_port];
  132. outb(control, control_addr);
  133. outb(dio48egpio->out_state[io_port], dio48egpio->base + out_port);
  134. control &= ~BIT(7);
  135. outb(control, control_addr);
  136. raw_spin_unlock_irqrestore(&dio48egpio->lock, flags);
  137. return 0;
  138. }
  139. static int dio48e_gpio_get(struct gpio_chip *chip, unsigned offset)
  140. {
  141. struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
  142. const unsigned port = offset / 8;
  143. const unsigned mask = BIT(offset % 8);
  144. const unsigned in_port = (port > 2) ? port + 1 : port;
  145. unsigned long flags;
  146. unsigned port_state;
  147. raw_spin_lock_irqsave(&dio48egpio->lock, flags);
  148. /* ensure that GPIO is set for input */
  149. if (!(dio48egpio->io_state[port] & mask)) {
  150. raw_spin_unlock_irqrestore(&dio48egpio->lock, flags);
  151. return -EINVAL;
  152. }
  153. port_state = inb(dio48egpio->base + in_port);
  154. raw_spin_unlock_irqrestore(&dio48egpio->lock, flags);
  155. return !!(port_state & mask);
  156. }
  157. static void dio48e_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  158. {
  159. struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
  160. const unsigned port = offset / 8;
  161. const unsigned mask = BIT(offset % 8);
  162. const unsigned out_port = (port > 2) ? port + 1 : port;
  163. unsigned long flags;
  164. raw_spin_lock_irqsave(&dio48egpio->lock, flags);
  165. if (value)
  166. dio48egpio->out_state[port] |= mask;
  167. else
  168. dio48egpio->out_state[port] &= ~mask;
  169. outb(dio48egpio->out_state[port], dio48egpio->base + out_port);
  170. raw_spin_unlock_irqrestore(&dio48egpio->lock, flags);
  171. }
  172. static void dio48e_gpio_set_multiple(struct gpio_chip *chip,
  173. unsigned long *mask, unsigned long *bits)
  174. {
  175. struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
  176. unsigned int i;
  177. const unsigned int gpio_reg_size = 8;
  178. unsigned int port;
  179. unsigned int out_port;
  180. unsigned int bitmask;
  181. unsigned long flags;
  182. /* set bits are evaluated a gpio register size at a time */
  183. for (i = 0; i < chip->ngpio; i += gpio_reg_size) {
  184. /* no more set bits in this mask word; skip to the next word */
  185. if (!mask[BIT_WORD(i)]) {
  186. i = (BIT_WORD(i) + 1) * BITS_PER_LONG - gpio_reg_size;
  187. continue;
  188. }
  189. port = i / gpio_reg_size;
  190. out_port = (port > 2) ? port + 1 : port;
  191. bitmask = mask[BIT_WORD(i)] & bits[BIT_WORD(i)];
  192. raw_spin_lock_irqsave(&dio48egpio->lock, flags);
  193. /* update output state data and set device gpio register */
  194. dio48egpio->out_state[port] &= ~mask[BIT_WORD(i)];
  195. dio48egpio->out_state[port] |= bitmask;
  196. outb(dio48egpio->out_state[port], dio48egpio->base + out_port);
  197. raw_spin_unlock_irqrestore(&dio48egpio->lock, flags);
  198. /* prepare for next gpio register set */
  199. mask[BIT_WORD(i)] >>= gpio_reg_size;
  200. bits[BIT_WORD(i)] >>= gpio_reg_size;
  201. }
  202. }
  203. static void dio48e_irq_ack(struct irq_data *data)
  204. {
  205. }
  206. static void dio48e_irq_mask(struct irq_data *data)
  207. {
  208. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  209. struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
  210. const unsigned long offset = irqd_to_hwirq(data);
  211. unsigned long flags;
  212. /* only bit 3 on each respective Port C supports interrupts */
  213. if (offset != 19 && offset != 43)
  214. return;
  215. raw_spin_lock_irqsave(&dio48egpio->lock, flags);
  216. if (offset == 19)
  217. dio48egpio->irq_mask &= ~BIT(0);
  218. else
  219. dio48egpio->irq_mask &= ~BIT(1);
  220. if (!dio48egpio->irq_mask)
  221. /* disable interrupts */
  222. inb(dio48egpio->base + 0xB);
  223. raw_spin_unlock_irqrestore(&dio48egpio->lock, flags);
  224. }
  225. static void dio48e_irq_unmask(struct irq_data *data)
  226. {
  227. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  228. struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
  229. const unsigned long offset = irqd_to_hwirq(data);
  230. unsigned long flags;
  231. /* only bit 3 on each respective Port C supports interrupts */
  232. if (offset != 19 && offset != 43)
  233. return;
  234. raw_spin_lock_irqsave(&dio48egpio->lock, flags);
  235. if (!dio48egpio->irq_mask) {
  236. /* enable interrupts */
  237. outb(0x00, dio48egpio->base + 0xF);
  238. outb(0x00, dio48egpio->base + 0xB);
  239. }
  240. if (offset == 19)
  241. dio48egpio->irq_mask |= BIT(0);
  242. else
  243. dio48egpio->irq_mask |= BIT(1);
  244. raw_spin_unlock_irqrestore(&dio48egpio->lock, flags);
  245. }
  246. static int dio48e_irq_set_type(struct irq_data *data, unsigned flow_type)
  247. {
  248. const unsigned long offset = irqd_to_hwirq(data);
  249. /* only bit 3 on each respective Port C supports interrupts */
  250. if (offset != 19 && offset != 43)
  251. return -EINVAL;
  252. if (flow_type != IRQ_TYPE_NONE && flow_type != IRQ_TYPE_EDGE_RISING)
  253. return -EINVAL;
  254. return 0;
  255. }
  256. static struct irq_chip dio48e_irqchip = {
  257. .name = "104-dio-48e",
  258. .irq_ack = dio48e_irq_ack,
  259. .irq_mask = dio48e_irq_mask,
  260. .irq_unmask = dio48e_irq_unmask,
  261. .irq_set_type = dio48e_irq_set_type
  262. };
  263. static irqreturn_t dio48e_irq_handler(int irq, void *dev_id)
  264. {
  265. struct dio48e_gpio *const dio48egpio = dev_id;
  266. struct gpio_chip *const chip = &dio48egpio->chip;
  267. const unsigned long irq_mask = dio48egpio->irq_mask;
  268. unsigned long gpio;
  269. for_each_set_bit(gpio, &irq_mask, 2)
  270. generic_handle_irq(irq_find_mapping(chip->irqdomain,
  271. 19 + gpio*24));
  272. raw_spin_lock(&dio48egpio->lock);
  273. outb(0x00, dio48egpio->base + 0xF);
  274. raw_spin_unlock(&dio48egpio->lock);
  275. return IRQ_HANDLED;
  276. }
  277. #define DIO48E_NGPIO 48
  278. static const char *dio48e_names[DIO48E_NGPIO] = {
  279. "PPI Group 0 Port A 0", "PPI Group 0 Port A 1", "PPI Group 0 Port A 2",
  280. "PPI Group 0 Port A 3", "PPI Group 0 Port A 4", "PPI Group 0 Port A 5",
  281. "PPI Group 0 Port A 6", "PPI Group 0 Port A 7", "PPI Group 0 Port B 0",
  282. "PPI Group 0 Port B 1", "PPI Group 0 Port B 2", "PPI Group 0 Port B 3",
  283. "PPI Group 0 Port B 4", "PPI Group 0 Port B 5", "PPI Group 0 Port B 6",
  284. "PPI Group 0 Port B 7", "PPI Group 0 Port C 0", "PPI Group 0 Port C 1",
  285. "PPI Group 0 Port C 2", "PPI Group 0 Port C 3", "PPI Group 0 Port C 4",
  286. "PPI Group 0 Port C 5", "PPI Group 0 Port C 6", "PPI Group 0 Port C 7",
  287. "PPI Group 1 Port A 0", "PPI Group 1 Port A 1", "PPI Group 1 Port A 2",
  288. "PPI Group 1 Port A 3", "PPI Group 1 Port A 4", "PPI Group 1 Port A 5",
  289. "PPI Group 1 Port A 6", "PPI Group 1 Port A 7", "PPI Group 1 Port B 0",
  290. "PPI Group 1 Port B 1", "PPI Group 1 Port B 2", "PPI Group 1 Port B 3",
  291. "PPI Group 1 Port B 4", "PPI Group 1 Port B 5", "PPI Group 1 Port B 6",
  292. "PPI Group 1 Port B 7", "PPI Group 1 Port C 0", "PPI Group 1 Port C 1",
  293. "PPI Group 1 Port C 2", "PPI Group 1 Port C 3", "PPI Group 1 Port C 4",
  294. "PPI Group 1 Port C 5", "PPI Group 1 Port C 6", "PPI Group 1 Port C 7"
  295. };
  296. static int dio48e_probe(struct device *dev, unsigned int id)
  297. {
  298. struct dio48e_gpio *dio48egpio;
  299. const char *const name = dev_name(dev);
  300. int err;
  301. dio48egpio = devm_kzalloc(dev, sizeof(*dio48egpio), GFP_KERNEL);
  302. if (!dio48egpio)
  303. return -ENOMEM;
  304. if (!devm_request_region(dev, base[id], DIO48E_EXTENT, name)) {
  305. dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
  306. base[id], base[id] + DIO48E_EXTENT);
  307. return -EBUSY;
  308. }
  309. dio48egpio->chip.label = name;
  310. dio48egpio->chip.parent = dev;
  311. dio48egpio->chip.owner = THIS_MODULE;
  312. dio48egpio->chip.base = -1;
  313. dio48egpio->chip.ngpio = DIO48E_NGPIO;
  314. dio48egpio->chip.names = dio48e_names;
  315. dio48egpio->chip.get_direction = dio48e_gpio_get_direction;
  316. dio48egpio->chip.direction_input = dio48e_gpio_direction_input;
  317. dio48egpio->chip.direction_output = dio48e_gpio_direction_output;
  318. dio48egpio->chip.get = dio48e_gpio_get;
  319. dio48egpio->chip.set = dio48e_gpio_set;
  320. dio48egpio->chip.set_multiple = dio48e_gpio_set_multiple;
  321. dio48egpio->base = base[id];
  322. raw_spin_lock_init(&dio48egpio->lock);
  323. err = devm_gpiochip_add_data(dev, &dio48egpio->chip, dio48egpio);
  324. if (err) {
  325. dev_err(dev, "GPIO registering failed (%d)\n", err);
  326. return err;
  327. }
  328. /* initialize all GPIO as output */
  329. outb(0x80, base[id] + 3);
  330. outb(0x00, base[id]);
  331. outb(0x00, base[id] + 1);
  332. outb(0x00, base[id] + 2);
  333. outb(0x00, base[id] + 3);
  334. outb(0x80, base[id] + 7);
  335. outb(0x00, base[id] + 4);
  336. outb(0x00, base[id] + 5);
  337. outb(0x00, base[id] + 6);
  338. outb(0x00, base[id] + 7);
  339. /* disable IRQ by default */
  340. inb(base[id] + 0xB);
  341. err = gpiochip_irqchip_add(&dio48egpio->chip, &dio48e_irqchip, 0,
  342. handle_edge_irq, IRQ_TYPE_NONE);
  343. if (err) {
  344. dev_err(dev, "Could not add irqchip (%d)\n", err);
  345. return err;
  346. }
  347. err = devm_request_irq(dev, irq[id], dio48e_irq_handler, 0, name,
  348. dio48egpio);
  349. if (err) {
  350. dev_err(dev, "IRQ handler registering failed (%d)\n", err);
  351. return err;
  352. }
  353. return 0;
  354. }
  355. static struct isa_driver dio48e_driver = {
  356. .probe = dio48e_probe,
  357. .driver = {
  358. .name = "104-dio-48e"
  359. },
  360. };
  361. module_isa_driver(dio48e_driver, num_dio48e);
  362. MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
  363. MODULE_DESCRIPTION("ACCES 104-DIO-48E GPIO driver");
  364. MODULE_LICENSE("GPL v2");