gpio-ws16c48.c 11 KB

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