gpio-ws16c48.c 15 KB

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