gpio-gpio-mm.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * GPIO driver for the Diamond Systems GPIO-MM
  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. * This driver supports the following Diamond Systems devices: GPIO-MM and
  15. * GPIO-MM-12.
  16. */
  17. #include <linux/bitops.h>
  18. #include <linux/device.h>
  19. #include <linux/errno.h>
  20. #include <linux/gpio/driver.h>
  21. #include <linux/io.h>
  22. #include <linux/ioport.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 GPIOMM_EXTENT 8
  29. #define MAX_NUM_GPIOMM max_num_isa_dev(GPIOMM_EXTENT)
  30. static unsigned int base[MAX_NUM_GPIOMM];
  31. static unsigned int num_gpiomm;
  32. module_param_hw_array(base, uint, ioport, &num_gpiomm, 0);
  33. MODULE_PARM_DESC(base, "Diamond Systems GPIO-MM base addresses");
  34. /**
  35. * struct gpiomm_gpio - GPIO device private data structure
  36. * @chip: instance of the gpio_chip
  37. * @io_state: bit I/O state (whether bit is set to input or output)
  38. * @out_state: output bits state
  39. * @control: Control registers state
  40. * @lock: synchronization lock to prevent I/O race conditions
  41. * @base: base port address of the GPIO device
  42. */
  43. struct gpiomm_gpio {
  44. struct gpio_chip chip;
  45. unsigned char io_state[6];
  46. unsigned char out_state[6];
  47. unsigned char control[2];
  48. spinlock_t lock;
  49. unsigned int base;
  50. };
  51. static int gpiomm_gpio_get_direction(struct gpio_chip *chip,
  52. unsigned int offset)
  53. {
  54. struct gpiomm_gpio *const gpiommgpio = gpiochip_get_data(chip);
  55. const unsigned int port = offset / 8;
  56. const unsigned int mask = BIT(offset % 8);
  57. return !!(gpiommgpio->io_state[port] & mask);
  58. }
  59. static int gpiomm_gpio_direction_input(struct gpio_chip *chip,
  60. unsigned int offset)
  61. {
  62. struct gpiomm_gpio *const gpiommgpio = gpiochip_get_data(chip);
  63. const unsigned int io_port = offset / 8;
  64. const unsigned int control_port = io_port / 3;
  65. const unsigned int control_addr = gpiommgpio->base + 3 + control_port*4;
  66. unsigned long flags;
  67. unsigned int control;
  68. spin_lock_irqsave(&gpiommgpio->lock, flags);
  69. /* Check if configuring Port C */
  70. if (io_port == 2 || io_port == 5) {
  71. /* Port C can be configured by nibble */
  72. if (offset % 8 > 3) {
  73. gpiommgpio->io_state[io_port] |= 0xF0;
  74. gpiommgpio->control[control_port] |= BIT(3);
  75. } else {
  76. gpiommgpio->io_state[io_port] |= 0x0F;
  77. gpiommgpio->control[control_port] |= BIT(0);
  78. }
  79. } else {
  80. gpiommgpio->io_state[io_port] |= 0xFF;
  81. if (io_port == 0 || io_port == 3)
  82. gpiommgpio->control[control_port] |= BIT(4);
  83. else
  84. gpiommgpio->control[control_port] |= BIT(1);
  85. }
  86. control = BIT(7) | gpiommgpio->control[control_port];
  87. outb(control, control_addr);
  88. spin_unlock_irqrestore(&gpiommgpio->lock, flags);
  89. return 0;
  90. }
  91. static int gpiomm_gpio_direction_output(struct gpio_chip *chip,
  92. unsigned int offset, int value)
  93. {
  94. struct gpiomm_gpio *const gpiommgpio = gpiochip_get_data(chip);
  95. const unsigned int io_port = offset / 8;
  96. const unsigned int control_port = io_port / 3;
  97. const unsigned int mask = BIT(offset % 8);
  98. const unsigned int control_addr = gpiommgpio->base + 3 + control_port*4;
  99. const unsigned int out_port = (io_port > 2) ? io_port + 1 : io_port;
  100. unsigned long flags;
  101. unsigned int control;
  102. spin_lock_irqsave(&gpiommgpio->lock, flags);
  103. /* Check if configuring Port C */
  104. if (io_port == 2 || io_port == 5) {
  105. /* Port C can be configured by nibble */
  106. if (offset % 8 > 3) {
  107. gpiommgpio->io_state[io_port] &= 0x0F;
  108. gpiommgpio->control[control_port] &= ~BIT(3);
  109. } else {
  110. gpiommgpio->io_state[io_port] &= 0xF0;
  111. gpiommgpio->control[control_port] &= ~BIT(0);
  112. }
  113. } else {
  114. gpiommgpio->io_state[io_port] &= 0x00;
  115. if (io_port == 0 || io_port == 3)
  116. gpiommgpio->control[control_port] &= ~BIT(4);
  117. else
  118. gpiommgpio->control[control_port] &= ~BIT(1);
  119. }
  120. if (value)
  121. gpiommgpio->out_state[io_port] |= mask;
  122. else
  123. gpiommgpio->out_state[io_port] &= ~mask;
  124. control = BIT(7) | gpiommgpio->control[control_port];
  125. outb(control, control_addr);
  126. outb(gpiommgpio->out_state[io_port], gpiommgpio->base + out_port);
  127. spin_unlock_irqrestore(&gpiommgpio->lock, flags);
  128. return 0;
  129. }
  130. static int gpiomm_gpio_get(struct gpio_chip *chip, unsigned int offset)
  131. {
  132. struct gpiomm_gpio *const gpiommgpio = gpiochip_get_data(chip);
  133. const unsigned int port = offset / 8;
  134. const unsigned int mask = BIT(offset % 8);
  135. const unsigned int in_port = (port > 2) ? port + 1 : port;
  136. unsigned long flags;
  137. unsigned int port_state;
  138. spin_lock_irqsave(&gpiommgpio->lock, flags);
  139. /* ensure that GPIO is set for input */
  140. if (!(gpiommgpio->io_state[port] & mask)) {
  141. spin_unlock_irqrestore(&gpiommgpio->lock, flags);
  142. return -EINVAL;
  143. }
  144. port_state = inb(gpiommgpio->base + in_port);
  145. spin_unlock_irqrestore(&gpiommgpio->lock, flags);
  146. return !!(port_state & mask);
  147. }
  148. static void gpiomm_gpio_set(struct gpio_chip *chip, unsigned int offset,
  149. int value)
  150. {
  151. struct gpiomm_gpio *const gpiommgpio = gpiochip_get_data(chip);
  152. const unsigned int port = offset / 8;
  153. const unsigned int mask = BIT(offset % 8);
  154. const unsigned int out_port = (port > 2) ? port + 1 : port;
  155. unsigned long flags;
  156. spin_lock_irqsave(&gpiommgpio->lock, flags);
  157. if (value)
  158. gpiommgpio->out_state[port] |= mask;
  159. else
  160. gpiommgpio->out_state[port] &= ~mask;
  161. outb(gpiommgpio->out_state[port], gpiommgpio->base + out_port);
  162. spin_unlock_irqrestore(&gpiommgpio->lock, flags);
  163. }
  164. static void gpiomm_gpio_set_multiple(struct gpio_chip *chip,
  165. unsigned long *mask, unsigned long *bits)
  166. {
  167. struct gpiomm_gpio *const gpiommgpio = gpiochip_get_data(chip);
  168. unsigned int i;
  169. const unsigned int gpio_reg_size = 8;
  170. unsigned int port;
  171. unsigned int out_port;
  172. unsigned int bitmask;
  173. unsigned long flags;
  174. /* set bits are evaluated a gpio register size at a time */
  175. for (i = 0; i < chip->ngpio; i += gpio_reg_size) {
  176. /* no more set bits in this mask word; skip to the next word */
  177. if (!mask[BIT_WORD(i)]) {
  178. i = (BIT_WORD(i) + 1) * BITS_PER_LONG - gpio_reg_size;
  179. continue;
  180. }
  181. port = i / gpio_reg_size;
  182. out_port = (port > 2) ? port + 1 : port;
  183. bitmask = mask[BIT_WORD(i)] & bits[BIT_WORD(i)];
  184. spin_lock_irqsave(&gpiommgpio->lock, flags);
  185. /* update output state data and set device gpio register */
  186. gpiommgpio->out_state[port] &= ~mask[BIT_WORD(i)];
  187. gpiommgpio->out_state[port] |= bitmask;
  188. outb(gpiommgpio->out_state[port], gpiommgpio->base + out_port);
  189. spin_unlock_irqrestore(&gpiommgpio->lock, flags);
  190. /* prepare for next gpio register set */
  191. mask[BIT_WORD(i)] >>= gpio_reg_size;
  192. bits[BIT_WORD(i)] >>= gpio_reg_size;
  193. }
  194. }
  195. #define GPIOMM_NGPIO 48
  196. static const char *gpiomm_names[GPIOMM_NGPIO] = {
  197. "Port 1A0", "Port 1A1", "Port 1A2", "Port 1A3", "Port 1A4", "Port 1A5",
  198. "Port 1A6", "Port 1A7", "Port 1B0", "Port 1B1", "Port 1B2", "Port 1B3",
  199. "Port 1B4", "Port 1B5", "Port 1B6", "Port 1B7", "Port 1C0", "Port 1C1",
  200. "Port 1C2", "Port 1C3", "Port 1C4", "Port 1C5", "Port 1C6", "Port 1C7",
  201. "Port 2A0", "Port 2A1", "Port 2A2", "Port 2A3", "Port 2A4", "Port 2A5",
  202. "Port 2A6", "Port 2A7", "Port 2B0", "Port 2B1", "Port 2B2", "Port 2B3",
  203. "Port 2B4", "Port 2B5", "Port 2B6", "Port 2B7", "Port 2C0", "Port 2C1",
  204. "Port 2C2", "Port 2C3", "Port 2C4", "Port 2C5", "Port 2C6", "Port 2C7",
  205. };
  206. static int gpiomm_probe(struct device *dev, unsigned int id)
  207. {
  208. struct gpiomm_gpio *gpiommgpio;
  209. const char *const name = dev_name(dev);
  210. int err;
  211. gpiommgpio = devm_kzalloc(dev, sizeof(*gpiommgpio), GFP_KERNEL);
  212. if (!gpiommgpio)
  213. return -ENOMEM;
  214. if (!devm_request_region(dev, base[id], GPIOMM_EXTENT, name)) {
  215. dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
  216. base[id], base[id] + GPIOMM_EXTENT);
  217. return -EBUSY;
  218. }
  219. gpiommgpio->chip.label = name;
  220. gpiommgpio->chip.parent = dev;
  221. gpiommgpio->chip.owner = THIS_MODULE;
  222. gpiommgpio->chip.base = -1;
  223. gpiommgpio->chip.ngpio = GPIOMM_NGPIO;
  224. gpiommgpio->chip.names = gpiomm_names;
  225. gpiommgpio->chip.get_direction = gpiomm_gpio_get_direction;
  226. gpiommgpio->chip.direction_input = gpiomm_gpio_direction_input;
  227. gpiommgpio->chip.direction_output = gpiomm_gpio_direction_output;
  228. gpiommgpio->chip.get = gpiomm_gpio_get;
  229. gpiommgpio->chip.set = gpiomm_gpio_set;
  230. gpiommgpio->chip.set_multiple = gpiomm_gpio_set_multiple;
  231. gpiommgpio->base = base[id];
  232. spin_lock_init(&gpiommgpio->lock);
  233. err = devm_gpiochip_add_data(dev, &gpiommgpio->chip, gpiommgpio);
  234. if (err) {
  235. dev_err(dev, "GPIO registering failed (%d)\n", err);
  236. return err;
  237. }
  238. /* initialize all GPIO as output */
  239. outb(0x80, base[id] + 3);
  240. outb(0x00, base[id]);
  241. outb(0x00, base[id] + 1);
  242. outb(0x00, base[id] + 2);
  243. outb(0x80, base[id] + 7);
  244. outb(0x00, base[id] + 4);
  245. outb(0x00, base[id] + 5);
  246. outb(0x00, base[id] + 6);
  247. return 0;
  248. }
  249. static struct isa_driver gpiomm_driver = {
  250. .probe = gpiomm_probe,
  251. .driver = {
  252. .name = "gpio-mm"
  253. },
  254. };
  255. module_isa_driver(gpiomm_driver, num_gpiomm);
  256. MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
  257. MODULE_DESCRIPTION("Diamond Systems GPIO-MM GPIO driver");
  258. MODULE_LICENSE("GPL v2");