gpio-ws16c48.c 14 KB

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