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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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_array(base, uint, &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_array(irq, uint, 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: Interrupt line number
  48. * @irq_mask: I/O bits affected by interrupts
  49. */
  50. struct dio48e_gpio {
  51. struct gpio_chip chip;
  52. unsigned char io_state[6];
  53. unsigned char out_state[6];
  54. unsigned char control[2];
  55. spinlock_t lock;
  56. unsigned base;
  57. unsigned irq;
  58. unsigned char irq_mask;
  59. };
  60. static int dio48e_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  61. {
  62. struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
  63. const unsigned port = offset / 8;
  64. const unsigned mask = BIT(offset % 8);
  65. return !!(dio48egpio->io_state[port] & mask);
  66. }
  67. static int dio48e_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  68. {
  69. struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
  70. const unsigned io_port = offset / 8;
  71. const unsigned int control_port = io_port / 3;
  72. const unsigned control_addr = dio48egpio->base + 3 + control_port*4;
  73. unsigned long flags;
  74. unsigned control;
  75. spin_lock_irqsave(&dio48egpio->lock, flags);
  76. /* Check if configuring Port C */
  77. if (io_port == 2 || io_port == 5) {
  78. /* Port C can be configured by nibble */
  79. if (offset % 8 > 3) {
  80. dio48egpio->io_state[io_port] |= 0xF0;
  81. dio48egpio->control[control_port] |= BIT(3);
  82. } else {
  83. dio48egpio->io_state[io_port] |= 0x0F;
  84. dio48egpio->control[control_port] |= BIT(0);
  85. }
  86. } else {
  87. dio48egpio->io_state[io_port] |= 0xFF;
  88. if (io_port == 0 || io_port == 3)
  89. dio48egpio->control[control_port] |= BIT(4);
  90. else
  91. dio48egpio->control[control_port] |= BIT(1);
  92. }
  93. control = BIT(7) | dio48egpio->control[control_port];
  94. outb(control, control_addr);
  95. control &= ~BIT(7);
  96. outb(control, control_addr);
  97. spin_unlock_irqrestore(&dio48egpio->lock, flags);
  98. return 0;
  99. }
  100. static int dio48e_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  101. int value)
  102. {
  103. struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
  104. const unsigned io_port = offset / 8;
  105. const unsigned int control_port = io_port / 3;
  106. const unsigned mask = BIT(offset % 8);
  107. const unsigned control_addr = dio48egpio->base + 3 + control_port*4;
  108. const unsigned out_port = (io_port > 2) ? io_port + 1 : io_port;
  109. unsigned long flags;
  110. unsigned control;
  111. spin_lock_irqsave(&dio48egpio->lock, flags);
  112. /* Check if configuring Port C */
  113. if (io_port == 2 || io_port == 5) {
  114. /* Port C can be configured by nibble */
  115. if (offset % 8 > 3) {
  116. dio48egpio->io_state[io_port] &= 0x0F;
  117. dio48egpio->control[control_port] &= ~BIT(3);
  118. } else {
  119. dio48egpio->io_state[io_port] &= 0xF0;
  120. dio48egpio->control[control_port] &= ~BIT(0);
  121. }
  122. } else {
  123. dio48egpio->io_state[io_port] &= 0x00;
  124. if (io_port == 0 || io_port == 3)
  125. dio48egpio->control[control_port] &= ~BIT(4);
  126. else
  127. dio48egpio->control[control_port] &= ~BIT(1);
  128. }
  129. if (value)
  130. dio48egpio->out_state[io_port] |= mask;
  131. else
  132. dio48egpio->out_state[io_port] &= ~mask;
  133. control = BIT(7) | dio48egpio->control[control_port];
  134. outb(control, control_addr);
  135. outb(dio48egpio->out_state[io_port], dio48egpio->base + out_port);
  136. control &= ~BIT(7);
  137. outb(control, control_addr);
  138. spin_unlock_irqrestore(&dio48egpio->lock, flags);
  139. return 0;
  140. }
  141. static int dio48e_gpio_get(struct gpio_chip *chip, unsigned offset)
  142. {
  143. struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
  144. const unsigned port = offset / 8;
  145. const unsigned mask = BIT(offset % 8);
  146. const unsigned in_port = (port > 2) ? port + 1 : port;
  147. unsigned long flags;
  148. unsigned port_state;
  149. spin_lock_irqsave(&dio48egpio->lock, flags);
  150. /* ensure that GPIO is set for input */
  151. if (!(dio48egpio->io_state[port] & mask)) {
  152. spin_unlock_irqrestore(&dio48egpio->lock, flags);
  153. return -EINVAL;
  154. }
  155. port_state = inb(dio48egpio->base + in_port);
  156. spin_unlock_irqrestore(&dio48egpio->lock, flags);
  157. return !!(port_state & mask);
  158. }
  159. static void dio48e_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  160. {
  161. struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
  162. const unsigned port = offset / 8;
  163. const unsigned mask = BIT(offset % 8);
  164. const unsigned out_port = (port > 2) ? port + 1 : port;
  165. unsigned long flags;
  166. spin_lock_irqsave(&dio48egpio->lock, flags);
  167. if (value)
  168. dio48egpio->out_state[port] |= mask;
  169. else
  170. dio48egpio->out_state[port] &= ~mask;
  171. outb(dio48egpio->out_state[port], dio48egpio->base + out_port);
  172. spin_unlock_irqrestore(&dio48egpio->lock, flags);
  173. }
  174. static void dio48e_irq_ack(struct irq_data *data)
  175. {
  176. }
  177. static void dio48e_irq_mask(struct irq_data *data)
  178. {
  179. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  180. struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
  181. const unsigned long offset = irqd_to_hwirq(data);
  182. unsigned long flags;
  183. /* only bit 3 on each respective Port C supports interrupts */
  184. if (offset != 19 && offset != 43)
  185. return;
  186. spin_lock_irqsave(&dio48egpio->lock, flags);
  187. if (offset == 19)
  188. dio48egpio->irq_mask &= ~BIT(0);
  189. else
  190. dio48egpio->irq_mask &= ~BIT(1);
  191. if (!dio48egpio->irq_mask)
  192. /* disable interrupts */
  193. inb(dio48egpio->base + 0xB);
  194. spin_unlock_irqrestore(&dio48egpio->lock, flags);
  195. }
  196. static void dio48e_irq_unmask(struct irq_data *data)
  197. {
  198. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  199. struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
  200. const unsigned long offset = irqd_to_hwirq(data);
  201. unsigned long flags;
  202. /* only bit 3 on each respective Port C supports interrupts */
  203. if (offset != 19 && offset != 43)
  204. return;
  205. spin_lock_irqsave(&dio48egpio->lock, flags);
  206. if (!dio48egpio->irq_mask) {
  207. /* enable interrupts */
  208. outb(0x00, dio48egpio->base + 0xF);
  209. outb(0x00, dio48egpio->base + 0xB);
  210. }
  211. if (offset == 19)
  212. dio48egpio->irq_mask |= BIT(0);
  213. else
  214. dio48egpio->irq_mask |= BIT(1);
  215. spin_unlock_irqrestore(&dio48egpio->lock, flags);
  216. }
  217. static int dio48e_irq_set_type(struct irq_data *data, unsigned flow_type)
  218. {
  219. const unsigned long offset = irqd_to_hwirq(data);
  220. /* only bit 3 on each respective Port C supports interrupts */
  221. if (offset != 19 && offset != 43)
  222. return -EINVAL;
  223. if (flow_type != IRQ_TYPE_NONE && flow_type != IRQ_TYPE_EDGE_RISING)
  224. return -EINVAL;
  225. return 0;
  226. }
  227. static struct irq_chip dio48e_irqchip = {
  228. .name = "104-dio-48e",
  229. .irq_ack = dio48e_irq_ack,
  230. .irq_mask = dio48e_irq_mask,
  231. .irq_unmask = dio48e_irq_unmask,
  232. .irq_set_type = dio48e_irq_set_type
  233. };
  234. static irqreturn_t dio48e_irq_handler(int irq, void *dev_id)
  235. {
  236. struct dio48e_gpio *const dio48egpio = dev_id;
  237. struct gpio_chip *const chip = &dio48egpio->chip;
  238. const unsigned long irq_mask = dio48egpio->irq_mask;
  239. unsigned long gpio;
  240. for_each_set_bit(gpio, &irq_mask, 2)
  241. generic_handle_irq(irq_find_mapping(chip->irqdomain,
  242. 19 + gpio*24));
  243. spin_lock(&dio48egpio->lock);
  244. outb(0x00, dio48egpio->base + 0xF);
  245. spin_unlock(&dio48egpio->lock);
  246. return IRQ_HANDLED;
  247. }
  248. static int dio48e_probe(struct device *dev, unsigned int id)
  249. {
  250. struct dio48e_gpio *dio48egpio;
  251. const char *const name = dev_name(dev);
  252. int err;
  253. dio48egpio = devm_kzalloc(dev, sizeof(*dio48egpio), GFP_KERNEL);
  254. if (!dio48egpio)
  255. return -ENOMEM;
  256. if (!devm_request_region(dev, base[id], DIO48E_EXTENT, name)) {
  257. dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
  258. base[id], base[id] + DIO48E_EXTENT);
  259. return -EBUSY;
  260. }
  261. dio48egpio->chip.label = name;
  262. dio48egpio->chip.parent = dev;
  263. dio48egpio->chip.owner = THIS_MODULE;
  264. dio48egpio->chip.base = -1;
  265. dio48egpio->chip.ngpio = 48;
  266. dio48egpio->chip.get_direction = dio48e_gpio_get_direction;
  267. dio48egpio->chip.direction_input = dio48e_gpio_direction_input;
  268. dio48egpio->chip.direction_output = dio48e_gpio_direction_output;
  269. dio48egpio->chip.get = dio48e_gpio_get;
  270. dio48egpio->chip.set = dio48e_gpio_set;
  271. dio48egpio->base = base[id];
  272. dio48egpio->irq = irq[id];
  273. spin_lock_init(&dio48egpio->lock);
  274. dev_set_drvdata(dev, dio48egpio);
  275. err = gpiochip_add_data(&dio48egpio->chip, dio48egpio);
  276. if (err) {
  277. dev_err(dev, "GPIO registering failed (%d)\n", err);
  278. return err;
  279. }
  280. /* initialize all GPIO as output */
  281. outb(0x80, base[id] + 3);
  282. outb(0x00, base[id]);
  283. outb(0x00, base[id] + 1);
  284. outb(0x00, base[id] + 2);
  285. outb(0x00, base[id] + 3);
  286. outb(0x80, base[id] + 7);
  287. outb(0x00, base[id] + 4);
  288. outb(0x00, base[id] + 5);
  289. outb(0x00, base[id] + 6);
  290. outb(0x00, base[id] + 7);
  291. /* disable IRQ by default */
  292. inb(base[id] + 0xB);
  293. err = gpiochip_irqchip_add(&dio48egpio->chip, &dio48e_irqchip, 0,
  294. handle_edge_irq, IRQ_TYPE_NONE);
  295. if (err) {
  296. dev_err(dev, "Could not add irqchip (%d)\n", err);
  297. goto err_gpiochip_remove;
  298. }
  299. err = request_irq(irq[id], dio48e_irq_handler, 0, name, dio48egpio);
  300. if (err) {
  301. dev_err(dev, "IRQ handler registering failed (%d)\n", err);
  302. goto err_gpiochip_remove;
  303. }
  304. return 0;
  305. err_gpiochip_remove:
  306. gpiochip_remove(&dio48egpio->chip);
  307. return err;
  308. }
  309. static int dio48e_remove(struct device *dev, unsigned int id)
  310. {
  311. struct dio48e_gpio *const dio48egpio = dev_get_drvdata(dev);
  312. free_irq(dio48egpio->irq, dio48egpio);
  313. gpiochip_remove(&dio48egpio->chip);
  314. return 0;
  315. }
  316. static struct isa_driver dio48e_driver = {
  317. .probe = dio48e_probe,
  318. .driver = {
  319. .name = "104-dio-48e"
  320. },
  321. .remove = dio48e_remove
  322. };
  323. module_isa_driver(dio48e_driver, num_dio48e);
  324. MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
  325. MODULE_DESCRIPTION("ACCES 104-DIO-48E GPIO driver");
  326. MODULE_LICENSE("GPL v2");