gpio-ws16c48.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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_array(base, uint, &num_ws16c48, 0);
  32. MODULE_PARM_DESC(base, "WinSystems WS16C48 base addresses");
  33. static unsigned int irq[MAX_NUM_WS16C48];
  34. module_param_array(irq, uint, 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. * @irq: Interrupt line number
  46. */
  47. struct ws16c48_gpio {
  48. struct gpio_chip chip;
  49. unsigned char io_state[6];
  50. unsigned char out_state[6];
  51. spinlock_t lock;
  52. unsigned long irq_mask;
  53. unsigned long flow_mask;
  54. unsigned base;
  55. unsigned irq;
  56. };
  57. static int ws16c48_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  58. {
  59. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  60. const unsigned port = offset / 8;
  61. const unsigned mask = BIT(offset % 8);
  62. return !!(ws16c48gpio->io_state[port] & mask);
  63. }
  64. static int ws16c48_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  65. {
  66. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  67. const unsigned port = offset / 8;
  68. const unsigned mask = BIT(offset % 8);
  69. unsigned long flags;
  70. spin_lock_irqsave(&ws16c48gpio->lock, flags);
  71. ws16c48gpio->io_state[port] |= mask;
  72. ws16c48gpio->out_state[port] &= ~mask;
  73. outb(ws16c48gpio->out_state[port], ws16c48gpio->base + port);
  74. spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  75. return 0;
  76. }
  77. static int ws16c48_gpio_direction_output(struct gpio_chip *chip,
  78. unsigned offset, int value)
  79. {
  80. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  81. const unsigned port = offset / 8;
  82. const unsigned mask = BIT(offset % 8);
  83. unsigned long flags;
  84. spin_lock_irqsave(&ws16c48gpio->lock, flags);
  85. ws16c48gpio->io_state[port] &= ~mask;
  86. if (value)
  87. ws16c48gpio->out_state[port] |= mask;
  88. else
  89. ws16c48gpio->out_state[port] &= ~mask;
  90. outb(ws16c48gpio->out_state[port], ws16c48gpio->base + port);
  91. spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  92. return 0;
  93. }
  94. static int ws16c48_gpio_get(struct gpio_chip *chip, unsigned offset)
  95. {
  96. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  97. const unsigned port = offset / 8;
  98. const unsigned mask = BIT(offset % 8);
  99. unsigned long flags;
  100. unsigned port_state;
  101. spin_lock_irqsave(&ws16c48gpio->lock, flags);
  102. /* ensure that GPIO is set for input */
  103. if (!(ws16c48gpio->io_state[port] & mask)) {
  104. spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  105. return -EINVAL;
  106. }
  107. port_state = inb(ws16c48gpio->base + port);
  108. spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  109. return !!(port_state & mask);
  110. }
  111. static void ws16c48_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  112. {
  113. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  114. const unsigned port = offset / 8;
  115. const unsigned mask = BIT(offset % 8);
  116. unsigned long flags;
  117. spin_lock_irqsave(&ws16c48gpio->lock, flags);
  118. /* ensure that GPIO is set for output */
  119. if (ws16c48gpio->io_state[port] & mask) {
  120. spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  121. return;
  122. }
  123. if (value)
  124. ws16c48gpio->out_state[port] |= mask;
  125. else
  126. ws16c48gpio->out_state[port] &= ~mask;
  127. outb(ws16c48gpio->out_state[port], ws16c48gpio->base + port);
  128. spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  129. }
  130. static void ws16c48_irq_ack(struct irq_data *data)
  131. {
  132. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  133. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  134. const unsigned long offset = irqd_to_hwirq(data);
  135. const unsigned port = offset / 8;
  136. const unsigned mask = BIT(offset % 8);
  137. unsigned long flags;
  138. unsigned port_state;
  139. /* only the first 3 ports support interrupts */
  140. if (port > 2)
  141. return;
  142. spin_lock_irqsave(&ws16c48gpio->lock, flags);
  143. port_state = ws16c48gpio->irq_mask >> (8*port);
  144. outb(0x80, ws16c48gpio->base + 7);
  145. outb(port_state & ~mask, ws16c48gpio->base + 8 + port);
  146. outb(port_state | mask, ws16c48gpio->base + 8 + port);
  147. outb(0xC0, ws16c48gpio->base + 7);
  148. spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  149. }
  150. static void ws16c48_irq_mask(struct irq_data *data)
  151. {
  152. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  153. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  154. const unsigned long offset = irqd_to_hwirq(data);
  155. const unsigned long mask = BIT(offset);
  156. const unsigned port = offset / 8;
  157. unsigned long flags;
  158. /* only the first 3 ports support interrupts */
  159. if (port > 2)
  160. return;
  161. spin_lock_irqsave(&ws16c48gpio->lock, flags);
  162. ws16c48gpio->irq_mask &= ~mask;
  163. outb(0x80, ws16c48gpio->base + 7);
  164. outb(ws16c48gpio->irq_mask >> (8*port), ws16c48gpio->base + 8 + port);
  165. outb(0xC0, ws16c48gpio->base + 7);
  166. spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  167. }
  168. static void ws16c48_irq_unmask(struct irq_data *data)
  169. {
  170. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  171. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  172. const unsigned long offset = irqd_to_hwirq(data);
  173. const unsigned long mask = BIT(offset);
  174. const unsigned port = offset / 8;
  175. unsigned long flags;
  176. /* only the first 3 ports support interrupts */
  177. if (port > 2)
  178. return;
  179. spin_lock_irqsave(&ws16c48gpio->lock, flags);
  180. ws16c48gpio->irq_mask |= mask;
  181. outb(0x80, ws16c48gpio->base + 7);
  182. outb(ws16c48gpio->irq_mask >> (8*port), ws16c48gpio->base + 8 + port);
  183. outb(0xC0, ws16c48gpio->base + 7);
  184. spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  185. }
  186. static int ws16c48_irq_set_type(struct irq_data *data, unsigned flow_type)
  187. {
  188. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  189. struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
  190. const unsigned long offset = irqd_to_hwirq(data);
  191. const unsigned long mask = BIT(offset);
  192. const unsigned port = offset / 8;
  193. unsigned long flags;
  194. /* only the first 3 ports support interrupts */
  195. if (port > 2)
  196. return -EINVAL;
  197. spin_lock_irqsave(&ws16c48gpio->lock, flags);
  198. switch (flow_type) {
  199. case IRQ_TYPE_NONE:
  200. break;
  201. case IRQ_TYPE_EDGE_RISING:
  202. ws16c48gpio->flow_mask |= mask;
  203. break;
  204. case IRQ_TYPE_EDGE_FALLING:
  205. ws16c48gpio->flow_mask &= ~mask;
  206. break;
  207. default:
  208. spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  209. return -EINVAL;
  210. }
  211. outb(0x40, ws16c48gpio->base + 7);
  212. outb(ws16c48gpio->flow_mask >> (8*port), ws16c48gpio->base + 8 + port);
  213. outb(0xC0, ws16c48gpio->base + 7);
  214. spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
  215. return 0;
  216. }
  217. static struct irq_chip ws16c48_irqchip = {
  218. .name = "ws16c48",
  219. .irq_ack = ws16c48_irq_ack,
  220. .irq_mask = ws16c48_irq_mask,
  221. .irq_unmask = ws16c48_irq_unmask,
  222. .irq_set_type = ws16c48_irq_set_type
  223. };
  224. static irqreturn_t ws16c48_irq_handler(int irq, void *dev_id)
  225. {
  226. struct ws16c48_gpio *const ws16c48gpio = dev_id;
  227. struct gpio_chip *const chip = &ws16c48gpio->chip;
  228. unsigned long int_pending;
  229. unsigned long port;
  230. unsigned long int_id;
  231. unsigned long gpio;
  232. int_pending = inb(ws16c48gpio->base + 6) & 0x7;
  233. if (!int_pending)
  234. return IRQ_NONE;
  235. /* loop until all pending interrupts are handled */
  236. do {
  237. for_each_set_bit(port, &int_pending, 3) {
  238. int_id = inb(ws16c48gpio->base + 8 + port);
  239. for_each_set_bit(gpio, &int_id, 8)
  240. generic_handle_irq(irq_find_mapping(
  241. chip->irqdomain, gpio + 8*port));
  242. }
  243. int_pending = inb(ws16c48gpio->base + 6) & 0x7;
  244. } while (int_pending);
  245. return IRQ_HANDLED;
  246. }
  247. static int ws16c48_probe(struct device *dev, unsigned int id)
  248. {
  249. struct ws16c48_gpio *ws16c48gpio;
  250. const char *const name = dev_name(dev);
  251. int err;
  252. ws16c48gpio = devm_kzalloc(dev, sizeof(*ws16c48gpio), GFP_KERNEL);
  253. if (!ws16c48gpio)
  254. return -ENOMEM;
  255. if (!devm_request_region(dev, base[id], WS16C48_EXTENT, name)) {
  256. dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
  257. base[id], base[id] + WS16C48_EXTENT);
  258. return -EBUSY;
  259. }
  260. ws16c48gpio->chip.label = name;
  261. ws16c48gpio->chip.parent = dev;
  262. ws16c48gpio->chip.owner = THIS_MODULE;
  263. ws16c48gpio->chip.base = -1;
  264. ws16c48gpio->chip.ngpio = 48;
  265. ws16c48gpio->chip.get_direction = ws16c48_gpio_get_direction;
  266. ws16c48gpio->chip.direction_input = ws16c48_gpio_direction_input;
  267. ws16c48gpio->chip.direction_output = ws16c48_gpio_direction_output;
  268. ws16c48gpio->chip.get = ws16c48_gpio_get;
  269. ws16c48gpio->chip.set = ws16c48_gpio_set;
  270. ws16c48gpio->base = base[id];
  271. ws16c48gpio->irq = irq[id];
  272. spin_lock_init(&ws16c48gpio->lock);
  273. dev_set_drvdata(dev, ws16c48gpio);
  274. err = gpiochip_add_data(&ws16c48gpio->chip, ws16c48gpio);
  275. if (err) {
  276. dev_err(dev, "GPIO registering failed (%d)\n", err);
  277. return err;
  278. }
  279. /* Disable IRQ by default */
  280. outb(0x80, base[id] + 7);
  281. outb(0, base[id] + 8);
  282. outb(0, base[id] + 9);
  283. outb(0, base[id] + 10);
  284. outb(0xC0, base[id] + 7);
  285. err = gpiochip_irqchip_add(&ws16c48gpio->chip, &ws16c48_irqchip, 0,
  286. handle_edge_irq, IRQ_TYPE_NONE);
  287. if (err) {
  288. dev_err(dev, "Could not add irqchip (%d)\n", err);
  289. goto err_gpiochip_remove;
  290. }
  291. err = request_irq(irq[id], ws16c48_irq_handler, IRQF_SHARED, name,
  292. ws16c48gpio);
  293. if (err) {
  294. dev_err(dev, "IRQ handler registering failed (%d)\n", err);
  295. goto err_gpiochip_remove;
  296. }
  297. return 0;
  298. err_gpiochip_remove:
  299. gpiochip_remove(&ws16c48gpio->chip);
  300. return err;
  301. }
  302. static int ws16c48_remove(struct device *dev, unsigned int id)
  303. {
  304. struct ws16c48_gpio *const ws16c48gpio = dev_get_drvdata(dev);
  305. free_irq(ws16c48gpio->irq, ws16c48gpio);
  306. gpiochip_remove(&ws16c48gpio->chip);
  307. return 0;
  308. }
  309. static struct isa_driver ws16c48_driver = {
  310. .probe = ws16c48_probe,
  311. .driver = {
  312. .name = "ws16c48"
  313. },
  314. .remove = ws16c48_remove
  315. };
  316. module_isa_driver(ws16c48_driver, num_ws16c48);
  317. MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
  318. MODULE_DESCRIPTION("WinSystems WS16C48 GPIO driver");
  319. MODULE_LICENSE("GPL v2");