gpio-ws16c48.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * GPIO driver for the WinSystems WS16C48
  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. #include <linux/bitops.h>
  15. #include <linux/device.h>
  16. #include <linux/errno.h>
  17. #include <linux/gpio/driver.h>
  18. #include <linux/io.h>
  19. #include <linux/ioport.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irqdesc.h>
  22. #include <linux/isa.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/spinlock.h>
  27. #define WS16C48_EXTENT 16
  28. #define MAX_NUM_WS16C48 max_num_isa_dev(WS16C48_EXTENT)
  29. static unsigned int base[MAX_NUM_WS16C48];
  30. static unsigned int num_ws16c48;
  31. module_param_hw_array(base, uint, ioport, &num_ws16c48, 0);
  32. MODULE_PARM_DESC(base, "WinSystems WS16C48 base addresses");
  33. static unsigned int irq[MAX_NUM_WS16C48];
  34. module_param_hw_array(irq, uint, irq, NULL, 0);
  35. MODULE_PARM_DESC(irq, "WinSystems WS16C48 interrupt line numbers");
  36. /**
  37. * struct ws16c48_gpio - GPIO device private data structure
  38. * @chip: instance of the gpio_chip
  39. * @io_state: bit I/O state (whether bit is set to input or output)
  40. * @out_state: output bits state
  41. * @lock: synchronization lock to prevent I/O race conditions
  42. * @irq_mask: I/O bits affected by interrupts
  43. * @flow_mask: IRQ flow type mask for the respective I/O bits
  44. * @base: base port address of the GPIO device
  45. */
  46. struct ws16c48_gpio {
  47. struct gpio_chip chip;
  48. unsigned char io_state[6];
  49. unsigned char out_state[6];
  50. raw_spinlock_t lock;
  51. unsigned long irq_mask;
  52. unsigned long flow_mask;
  53. unsigned base;
  54. };
  55. static int ws16c48_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  56. {
  57. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  58. const unsigned port = offset / 8;
  59. const unsigned mask = BIT(offset % 8);
  60. return !!(ws16c48gpio->io_state[port] & mask);
  61. }
  62. static int ws16c48_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  63. {
  64. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  65. const unsigned port = offset / 8;
  66. const unsigned mask = BIT(offset % 8);
  67. unsigned long flags;
  68. raw_spin_lock_irqsave(&ws16c48gpio->lock, flags);
  69. ws16c48gpio->io_state[port] |= mask;
  70. ws16c48gpio->out_state[port] &= ~mask;
  71. outb(ws16c48gpio->out_state[port], ws16c48gpio->base + port);
  72. raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  73. return 0;
  74. }
  75. static int ws16c48_gpio_direction_output(struct gpio_chip *chip,
  76. unsigned offset, int value)
  77. {
  78. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  79. const unsigned port = offset / 8;
  80. const unsigned mask = BIT(offset % 8);
  81. unsigned long flags;
  82. raw_spin_lock_irqsave(&ws16c48gpio->lock, flags);
  83. ws16c48gpio->io_state[port] &= ~mask;
  84. if (value)
  85. ws16c48gpio->out_state[port] |= mask;
  86. else
  87. ws16c48gpio->out_state[port] &= ~mask;
  88. outb(ws16c48gpio->out_state[port], ws16c48gpio->base + port);
  89. raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  90. return 0;
  91. }
  92. static int ws16c48_gpio_get(struct gpio_chip *chip, unsigned offset)
  93. {
  94. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  95. const unsigned port = offset / 8;
  96. const unsigned mask = BIT(offset % 8);
  97. unsigned long flags;
  98. unsigned port_state;
  99. raw_spin_lock_irqsave(&ws16c48gpio->lock, flags);
  100. /* ensure that GPIO is set for input */
  101. if (!(ws16c48gpio->io_state[port] & mask)) {
  102. raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  103. return -EINVAL;
  104. }
  105. port_state = inb(ws16c48gpio->base + port);
  106. raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  107. return !!(port_state & mask);
  108. }
  109. static void ws16c48_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  110. {
  111. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  112. const unsigned port = offset / 8;
  113. const unsigned mask = BIT(offset % 8);
  114. unsigned long flags;
  115. raw_spin_lock_irqsave(&ws16c48gpio->lock, flags);
  116. /* ensure that GPIO is set for output */
  117. if (ws16c48gpio->io_state[port] & mask) {
  118. raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  119. return;
  120. }
  121. if (value)
  122. ws16c48gpio->out_state[port] |= mask;
  123. else
  124. ws16c48gpio->out_state[port] &= ~mask;
  125. outb(ws16c48gpio->out_state[port], ws16c48gpio->base + port);
  126. raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  127. }
  128. static void ws16c48_gpio_set_multiple(struct gpio_chip *chip,
  129. unsigned long *mask, unsigned long *bits)
  130. {
  131. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  132. unsigned int i;
  133. const unsigned int gpio_reg_size = 8;
  134. unsigned int port;
  135. unsigned int iomask;
  136. unsigned int bitmask;
  137. unsigned long flags;
  138. /* set bits are evaluated a gpio register size at a time */
  139. for (i = 0; i < chip->ngpio; i += gpio_reg_size) {
  140. /* no more set bits in this mask word; skip to the next word */
  141. if (!mask[BIT_WORD(i)]) {
  142. i = (BIT_WORD(i) + 1) * BITS_PER_LONG - gpio_reg_size;
  143. continue;
  144. }
  145. port = i / gpio_reg_size;
  146. /* mask out GPIO configured for input */
  147. iomask = mask[BIT_WORD(i)] & ~ws16c48gpio->io_state[port];
  148. bitmask = iomask & bits[BIT_WORD(i)];
  149. raw_spin_lock_irqsave(&ws16c48gpio->lock, flags);
  150. /* update output state data and set device gpio register */
  151. ws16c48gpio->out_state[port] &= ~iomask;
  152. ws16c48gpio->out_state[port] |= bitmask;
  153. outb(ws16c48gpio->out_state[port], ws16c48gpio->base + port);
  154. raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  155. /* prepare for next gpio register set */
  156. mask[BIT_WORD(i)] >>= gpio_reg_size;
  157. bits[BIT_WORD(i)] >>= gpio_reg_size;
  158. }
  159. }
  160. static void ws16c48_irq_ack(struct irq_data *data)
  161. {
  162. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  163. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  164. const unsigned long offset = irqd_to_hwirq(data);
  165. const unsigned port = offset / 8;
  166. const unsigned mask = BIT(offset % 8);
  167. unsigned long flags;
  168. unsigned port_state;
  169. /* only the first 3 ports support interrupts */
  170. if (port > 2)
  171. return;
  172. raw_spin_lock_irqsave(&ws16c48gpio->lock, flags);
  173. port_state = ws16c48gpio->irq_mask >> (8*port);
  174. outb(0x80, ws16c48gpio->base + 7);
  175. outb(port_state & ~mask, ws16c48gpio->base + 8 + port);
  176. outb(port_state | mask, ws16c48gpio->base + 8 + port);
  177. outb(0xC0, ws16c48gpio->base + 7);
  178. raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  179. }
  180. static void ws16c48_irq_mask(struct irq_data *data)
  181. {
  182. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  183. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  184. const unsigned long offset = irqd_to_hwirq(data);
  185. const unsigned long mask = BIT(offset);
  186. const unsigned port = offset / 8;
  187. unsigned long flags;
  188. /* only the first 3 ports support interrupts */
  189. if (port > 2)
  190. return;
  191. raw_spin_lock_irqsave(&ws16c48gpio->lock, flags);
  192. ws16c48gpio->irq_mask &= ~mask;
  193. outb(0x80, ws16c48gpio->base + 7);
  194. outb(ws16c48gpio->irq_mask >> (8*port), ws16c48gpio->base + 8 + port);
  195. outb(0xC0, ws16c48gpio->base + 7);
  196. raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  197. }
  198. static void ws16c48_irq_unmask(struct irq_data *data)
  199. {
  200. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  201. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  202. const unsigned long offset = irqd_to_hwirq(data);
  203. const unsigned long mask = BIT(offset);
  204. const unsigned port = offset / 8;
  205. unsigned long flags;
  206. /* only the first 3 ports support interrupts */
  207. if (port > 2)
  208. return;
  209. raw_spin_lock_irqsave(&ws16c48gpio->lock, flags);
  210. ws16c48gpio->irq_mask |= mask;
  211. outb(0x80, ws16c48gpio->base + 7);
  212. outb(ws16c48gpio->irq_mask >> (8*port), ws16c48gpio->base + 8 + port);
  213. outb(0xC0, ws16c48gpio->base + 7);
  214. raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  215. }
  216. static int ws16c48_irq_set_type(struct irq_data *data, unsigned flow_type)
  217. {
  218. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  219. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  220. const unsigned long offset = irqd_to_hwirq(data);
  221. const unsigned long mask = BIT(offset);
  222. const unsigned port = offset / 8;
  223. unsigned long flags;
  224. /* only the first 3 ports support interrupts */
  225. if (port > 2)
  226. return -EINVAL;
  227. raw_spin_lock_irqsave(&ws16c48gpio->lock, flags);
  228. switch (flow_type) {
  229. case IRQ_TYPE_NONE:
  230. break;
  231. case IRQ_TYPE_EDGE_RISING:
  232. ws16c48gpio->flow_mask |= mask;
  233. break;
  234. case IRQ_TYPE_EDGE_FALLING:
  235. ws16c48gpio->flow_mask &= ~mask;
  236. break;
  237. default:
  238. raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  239. return -EINVAL;
  240. }
  241. outb(0x40, ws16c48gpio->base + 7);
  242. outb(ws16c48gpio->flow_mask >> (8*port), ws16c48gpio->base + 8 + port);
  243. outb(0xC0, ws16c48gpio->base + 7);
  244. raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  245. return 0;
  246. }
  247. static struct irq_chip ws16c48_irqchip = {
  248. .name = "ws16c48",
  249. .irq_ack = ws16c48_irq_ack,
  250. .irq_mask = ws16c48_irq_mask,
  251. .irq_unmask = ws16c48_irq_unmask,
  252. .irq_set_type = ws16c48_irq_set_type
  253. };
  254. static irqreturn_t ws16c48_irq_handler(int irq, void *dev_id)
  255. {
  256. struct ws16c48_gpio *const ws16c48gpio = dev_id;
  257. struct gpio_chip *const chip = &ws16c48gpio->chip;
  258. unsigned long int_pending;
  259. unsigned long port;
  260. unsigned long int_id;
  261. unsigned long gpio;
  262. int_pending = inb(ws16c48gpio->base + 6) & 0x7;
  263. if (!int_pending)
  264. return IRQ_NONE;
  265. /* loop until all pending interrupts are handled */
  266. do {
  267. for_each_set_bit(port, &int_pending, 3) {
  268. int_id = inb(ws16c48gpio->base + 8 + port);
  269. for_each_set_bit(gpio, &int_id, 8)
  270. generic_handle_irq(irq_find_mapping(
  271. chip->irq.domain, gpio + 8*port));
  272. }
  273. int_pending = inb(ws16c48gpio->base + 6) & 0x7;
  274. } while (int_pending);
  275. return IRQ_HANDLED;
  276. }
  277. #define WS16C48_NGPIO 48
  278. static const char *ws16c48_names[WS16C48_NGPIO] = {
  279. "Port 0 Bit 0", "Port 0 Bit 1", "Port 0 Bit 2", "Port 0 Bit 3",
  280. "Port 0 Bit 4", "Port 0 Bit 5", "Port 0 Bit 6", "Port 0 Bit 7",
  281. "Port 1 Bit 0", "Port 1 Bit 1", "Port 1 Bit 2", "Port 1 Bit 3",
  282. "Port 1 Bit 4", "Port 1 Bit 5", "Port 1 Bit 6", "Port 1 Bit 7",
  283. "Port 2 Bit 0", "Port 2 Bit 1", "Port 2 Bit 2", "Port 2 Bit 3",
  284. "Port 2 Bit 4", "Port 2 Bit 5", "Port 2 Bit 6", "Port 2 Bit 7",
  285. "Port 3 Bit 0", "Port 3 Bit 1", "Port 3 Bit 2", "Port 3 Bit 3",
  286. "Port 3 Bit 4", "Port 3 Bit 5", "Port 3 Bit 6", "Port 3 Bit 7",
  287. "Port 4 Bit 0", "Port 4 Bit 1", "Port 4 Bit 2", "Port 4 Bit 3",
  288. "Port 4 Bit 4", "Port 4 Bit 5", "Port 4 Bit 6", "Port 4 Bit 7",
  289. "Port 5 Bit 0", "Port 5 Bit 1", "Port 5 Bit 2", "Port 5 Bit 3",
  290. "Port 5 Bit 4", "Port 5 Bit 5", "Port 5 Bit 6", "Port 5 Bit 7"
  291. };
  292. static int ws16c48_probe(struct device *dev, unsigned int id)
  293. {
  294. struct ws16c48_gpio *ws16c48gpio;
  295. const char *const name = dev_name(dev);
  296. int err;
  297. ws16c48gpio = devm_kzalloc(dev, sizeof(*ws16c48gpio), GFP_KERNEL);
  298. if (!ws16c48gpio)
  299. return -ENOMEM;
  300. if (!devm_request_region(dev, base[id], WS16C48_EXTENT, name)) {
  301. dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
  302. base[id], base[id] + WS16C48_EXTENT);
  303. return -EBUSY;
  304. }
  305. ws16c48gpio->chip.label = name;
  306. ws16c48gpio->chip.parent = dev;
  307. ws16c48gpio->chip.owner = THIS_MODULE;
  308. ws16c48gpio->chip.base = -1;
  309. ws16c48gpio->chip.ngpio = WS16C48_NGPIO;
  310. ws16c48gpio->chip.names = ws16c48_names;
  311. ws16c48gpio->chip.get_direction = ws16c48_gpio_get_direction;
  312. ws16c48gpio->chip.direction_input = ws16c48_gpio_direction_input;
  313. ws16c48gpio->chip.direction_output = ws16c48_gpio_direction_output;
  314. ws16c48gpio->chip.get = ws16c48_gpio_get;
  315. ws16c48gpio->chip.set = ws16c48_gpio_set;
  316. ws16c48gpio->chip.set_multiple = ws16c48_gpio_set_multiple;
  317. ws16c48gpio->base = base[id];
  318. raw_spin_lock_init(&ws16c48gpio->lock);
  319. err = devm_gpiochip_add_data(dev, &ws16c48gpio->chip, ws16c48gpio);
  320. if (err) {
  321. dev_err(dev, "GPIO registering failed (%d)\n", err);
  322. return err;
  323. }
  324. /* Disable IRQ by default */
  325. outb(0x80, base[id] + 7);
  326. outb(0, base[id] + 8);
  327. outb(0, base[id] + 9);
  328. outb(0, base[id] + 10);
  329. outb(0xC0, base[id] + 7);
  330. err = gpiochip_irqchip_add(&ws16c48gpio->chip, &ws16c48_irqchip, 0,
  331. handle_edge_irq, IRQ_TYPE_NONE);
  332. if (err) {
  333. dev_err(dev, "Could not add irqchip (%d)\n", err);
  334. return err;
  335. }
  336. err = devm_request_irq(dev, irq[id], ws16c48_irq_handler, IRQF_SHARED,
  337. name, ws16c48gpio);
  338. if (err) {
  339. dev_err(dev, "IRQ handler registering failed (%d)\n", err);
  340. return err;
  341. }
  342. return 0;
  343. }
  344. static struct isa_driver ws16c48_driver = {
  345. .probe = ws16c48_probe,
  346. .driver = {
  347. .name = "ws16c48"
  348. },
  349. };
  350. module_isa_driver(ws16c48_driver, num_ws16c48);
  351. MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
  352. MODULE_DESCRIPTION("WinSystems WS16C48 GPIO driver");
  353. MODULE_LICENSE("GPL v2");