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

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