gpio-ep93xx.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Generic EP93xx GPIO handling
  3. *
  4. * Copyright (c) 2008 Ryan Mallon
  5. * Copyright (c) 2011 H Hartley Sweeten <hsweeten@visionengravers.com>
  6. *
  7. * Based on code originally from:
  8. * linux/arch/arm/mach-ep93xx/core.c
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/io.h>
  18. #include <linux/gpio.h>
  19. #include <linux/irq.h>
  20. #include <linux/slab.h>
  21. #include <linux/basic_mmio_gpio.h>
  22. #include <mach/hardware.h>
  23. #include <mach/gpio-ep93xx.h>
  24. #define irq_to_gpio(irq) ((irq) - gpio_to_irq(0))
  25. struct ep93xx_gpio {
  26. void __iomem *mmio_base;
  27. struct bgpio_chip bgc[8];
  28. };
  29. /*************************************************************************
  30. * Interrupt handling for EP93xx on-chip GPIOs
  31. *************************************************************************/
  32. static unsigned char gpio_int_unmasked[3];
  33. static unsigned char gpio_int_enabled[3];
  34. static unsigned char gpio_int_type1[3];
  35. static unsigned char gpio_int_type2[3];
  36. static unsigned char gpio_int_debounce[3];
  37. /* Port ordering is: A B F */
  38. static const u8 int_type1_register_offset[3] = { 0x90, 0xac, 0x4c };
  39. static const u8 int_type2_register_offset[3] = { 0x94, 0xb0, 0x50 };
  40. static const u8 eoi_register_offset[3] = { 0x98, 0xb4, 0x54 };
  41. static const u8 int_en_register_offset[3] = { 0x9c, 0xb8, 0x58 };
  42. static const u8 int_debounce_register_offset[3] = { 0xa8, 0xc4, 0x64 };
  43. static void ep93xx_gpio_update_int_params(unsigned port)
  44. {
  45. BUG_ON(port > 2);
  46. writeb_relaxed(0, EP93XX_GPIO_REG(int_en_register_offset[port]));
  47. writeb_relaxed(gpio_int_type2[port],
  48. EP93XX_GPIO_REG(int_type2_register_offset[port]));
  49. writeb_relaxed(gpio_int_type1[port],
  50. EP93XX_GPIO_REG(int_type1_register_offset[port]));
  51. writeb(gpio_int_unmasked[port] & gpio_int_enabled[port],
  52. EP93XX_GPIO_REG(int_en_register_offset[port]));
  53. }
  54. static void ep93xx_gpio_int_debounce(unsigned int irq, bool enable)
  55. {
  56. int line = irq_to_gpio(irq);
  57. int port = line >> 3;
  58. int port_mask = 1 << (line & 7);
  59. if (enable)
  60. gpio_int_debounce[port] |= port_mask;
  61. else
  62. gpio_int_debounce[port] &= ~port_mask;
  63. writeb(gpio_int_debounce[port],
  64. EP93XX_GPIO_REG(int_debounce_register_offset[port]));
  65. }
  66. static void ep93xx_gpio_ab_irq_handler(struct irq_desc *desc)
  67. {
  68. unsigned char status;
  69. int i;
  70. status = readb(EP93XX_GPIO_A_INT_STATUS);
  71. for (i = 0; i < 8; i++) {
  72. if (status & (1 << i)) {
  73. int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_A(0)) + i;
  74. generic_handle_irq(gpio_irq);
  75. }
  76. }
  77. status = readb(EP93XX_GPIO_B_INT_STATUS);
  78. for (i = 0; i < 8; i++) {
  79. if (status & (1 << i)) {
  80. int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_B(0)) + i;
  81. generic_handle_irq(gpio_irq);
  82. }
  83. }
  84. }
  85. static void ep93xx_gpio_f_irq_handler(struct irq_desc *desc)
  86. {
  87. /*
  88. * map discontiguous hw irq range to continuous sw irq range:
  89. *
  90. * IRQ_EP93XX_GPIO{0..7}MUX -> gpio_to_irq(EP93XX_GPIO_LINE_F({0..7})
  91. */
  92. unsigned int irq = irq_desc_get_irq(desc);
  93. int port_f_idx = ((irq + 1) & 7) ^ 4; /* {19..22,47..50} -> {0..7} */
  94. int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_F(0)) + port_f_idx;
  95. generic_handle_irq(gpio_irq);
  96. }
  97. static void ep93xx_gpio_irq_ack(struct irq_data *d)
  98. {
  99. int line = irq_to_gpio(d->irq);
  100. int port = line >> 3;
  101. int port_mask = 1 << (line & 7);
  102. if (irqd_get_trigger_type(d) == IRQ_TYPE_EDGE_BOTH) {
  103. gpio_int_type2[port] ^= port_mask; /* switch edge direction */
  104. ep93xx_gpio_update_int_params(port);
  105. }
  106. writeb(port_mask, EP93XX_GPIO_REG(eoi_register_offset[port]));
  107. }
  108. static void ep93xx_gpio_irq_mask_ack(struct irq_data *d)
  109. {
  110. int line = irq_to_gpio(d->irq);
  111. int port = line >> 3;
  112. int port_mask = 1 << (line & 7);
  113. if (irqd_get_trigger_type(d) == IRQ_TYPE_EDGE_BOTH)
  114. gpio_int_type2[port] ^= port_mask; /* switch edge direction */
  115. gpio_int_unmasked[port] &= ~port_mask;
  116. ep93xx_gpio_update_int_params(port);
  117. writeb(port_mask, EP93XX_GPIO_REG(eoi_register_offset[port]));
  118. }
  119. static void ep93xx_gpio_irq_mask(struct irq_data *d)
  120. {
  121. int line = irq_to_gpio(d->irq);
  122. int port = line >> 3;
  123. gpio_int_unmasked[port] &= ~(1 << (line & 7));
  124. ep93xx_gpio_update_int_params(port);
  125. }
  126. static void ep93xx_gpio_irq_unmask(struct irq_data *d)
  127. {
  128. int line = irq_to_gpio(d->irq);
  129. int port = line >> 3;
  130. gpio_int_unmasked[port] |= 1 << (line & 7);
  131. ep93xx_gpio_update_int_params(port);
  132. }
  133. /*
  134. * gpio_int_type1 controls whether the interrupt is level (0) or
  135. * edge (1) triggered, while gpio_int_type2 controls whether it
  136. * triggers on low/falling (0) or high/rising (1).
  137. */
  138. static int ep93xx_gpio_irq_type(struct irq_data *d, unsigned int type)
  139. {
  140. const int gpio = irq_to_gpio(d->irq);
  141. const int port = gpio >> 3;
  142. const int port_mask = 1 << (gpio & 7);
  143. irq_flow_handler_t handler;
  144. gpio_direction_input(gpio);
  145. switch (type) {
  146. case IRQ_TYPE_EDGE_RISING:
  147. gpio_int_type1[port] |= port_mask;
  148. gpio_int_type2[port] |= port_mask;
  149. handler = handle_edge_irq;
  150. break;
  151. case IRQ_TYPE_EDGE_FALLING:
  152. gpio_int_type1[port] |= port_mask;
  153. gpio_int_type2[port] &= ~port_mask;
  154. handler = handle_edge_irq;
  155. break;
  156. case IRQ_TYPE_LEVEL_HIGH:
  157. gpio_int_type1[port] &= ~port_mask;
  158. gpio_int_type2[port] |= port_mask;
  159. handler = handle_level_irq;
  160. break;
  161. case IRQ_TYPE_LEVEL_LOW:
  162. gpio_int_type1[port] &= ~port_mask;
  163. gpio_int_type2[port] &= ~port_mask;
  164. handler = handle_level_irq;
  165. break;
  166. case IRQ_TYPE_EDGE_BOTH:
  167. gpio_int_type1[port] |= port_mask;
  168. /* set initial polarity based on current input level */
  169. if (gpio_get_value(gpio))
  170. gpio_int_type2[port] &= ~port_mask; /* falling */
  171. else
  172. gpio_int_type2[port] |= port_mask; /* rising */
  173. handler = handle_edge_irq;
  174. break;
  175. default:
  176. return -EINVAL;
  177. }
  178. irq_set_handler_locked(d, handler);
  179. gpio_int_enabled[port] |= port_mask;
  180. ep93xx_gpio_update_int_params(port);
  181. return 0;
  182. }
  183. static struct irq_chip ep93xx_gpio_irq_chip = {
  184. .name = "GPIO",
  185. .irq_ack = ep93xx_gpio_irq_ack,
  186. .irq_mask_ack = ep93xx_gpio_irq_mask_ack,
  187. .irq_mask = ep93xx_gpio_irq_mask,
  188. .irq_unmask = ep93xx_gpio_irq_unmask,
  189. .irq_set_type = ep93xx_gpio_irq_type,
  190. };
  191. static void ep93xx_gpio_init_irq(void)
  192. {
  193. int gpio_irq;
  194. for (gpio_irq = gpio_to_irq(0);
  195. gpio_irq <= gpio_to_irq(EP93XX_GPIO_LINE_MAX_IRQ); ++gpio_irq) {
  196. irq_set_chip_and_handler(gpio_irq, &ep93xx_gpio_irq_chip,
  197. handle_level_irq);
  198. irq_clear_status_flags(gpio_irq, IRQ_NOREQUEST);
  199. }
  200. irq_set_chained_handler(IRQ_EP93XX_GPIO_AB,
  201. ep93xx_gpio_ab_irq_handler);
  202. irq_set_chained_handler(IRQ_EP93XX_GPIO0MUX,
  203. ep93xx_gpio_f_irq_handler);
  204. irq_set_chained_handler(IRQ_EP93XX_GPIO1MUX,
  205. ep93xx_gpio_f_irq_handler);
  206. irq_set_chained_handler(IRQ_EP93XX_GPIO2MUX,
  207. ep93xx_gpio_f_irq_handler);
  208. irq_set_chained_handler(IRQ_EP93XX_GPIO3MUX,
  209. ep93xx_gpio_f_irq_handler);
  210. irq_set_chained_handler(IRQ_EP93XX_GPIO4MUX,
  211. ep93xx_gpio_f_irq_handler);
  212. irq_set_chained_handler(IRQ_EP93XX_GPIO5MUX,
  213. ep93xx_gpio_f_irq_handler);
  214. irq_set_chained_handler(IRQ_EP93XX_GPIO6MUX,
  215. ep93xx_gpio_f_irq_handler);
  216. irq_set_chained_handler(IRQ_EP93XX_GPIO7MUX,
  217. ep93xx_gpio_f_irq_handler);
  218. }
  219. /*************************************************************************
  220. * gpiolib interface for EP93xx on-chip GPIOs
  221. *************************************************************************/
  222. struct ep93xx_gpio_bank {
  223. const char *label;
  224. int data;
  225. int dir;
  226. int base;
  227. bool has_debounce;
  228. };
  229. #define EP93XX_GPIO_BANK(_label, _data, _dir, _base, _debounce) \
  230. { \
  231. .label = _label, \
  232. .data = _data, \
  233. .dir = _dir, \
  234. .base = _base, \
  235. .has_debounce = _debounce, \
  236. }
  237. static struct ep93xx_gpio_bank ep93xx_gpio_banks[] = {
  238. EP93XX_GPIO_BANK("A", 0x00, 0x10, 0, true),
  239. EP93XX_GPIO_BANK("B", 0x04, 0x14, 8, true),
  240. EP93XX_GPIO_BANK("C", 0x08, 0x18, 40, false),
  241. EP93XX_GPIO_BANK("D", 0x0c, 0x1c, 24, false),
  242. EP93XX_GPIO_BANK("E", 0x20, 0x24, 32, false),
  243. EP93XX_GPIO_BANK("F", 0x30, 0x34, 16, true),
  244. EP93XX_GPIO_BANK("G", 0x38, 0x3c, 48, false),
  245. EP93XX_GPIO_BANK("H", 0x40, 0x44, 56, false),
  246. };
  247. static int ep93xx_gpio_set_debounce(struct gpio_chip *chip,
  248. unsigned offset, unsigned debounce)
  249. {
  250. int gpio = chip->base + offset;
  251. int irq = gpio_to_irq(gpio);
  252. if (irq < 0)
  253. return -EINVAL;
  254. ep93xx_gpio_int_debounce(irq, debounce ? true : false);
  255. return 0;
  256. }
  257. /*
  258. * Map GPIO A0..A7 (0..7) to irq 64..71,
  259. * B0..B7 (7..15) to irq 72..79, and
  260. * F0..F7 (16..24) to irq 80..87.
  261. */
  262. static int ep93xx_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  263. {
  264. int gpio = chip->base + offset;
  265. if (gpio > EP93XX_GPIO_LINE_MAX_IRQ)
  266. return -EINVAL;
  267. return 64 + gpio;
  268. }
  269. static int ep93xx_gpio_add_bank(struct bgpio_chip *bgc, struct device *dev,
  270. void __iomem *mmio_base, struct ep93xx_gpio_bank *bank)
  271. {
  272. void __iomem *data = mmio_base + bank->data;
  273. void __iomem *dir = mmio_base + bank->dir;
  274. int err;
  275. err = bgpio_init(bgc, dev, 1, data, NULL, NULL, dir, NULL, 0);
  276. if (err)
  277. return err;
  278. bgc->gc.label = bank->label;
  279. bgc->gc.base = bank->base;
  280. if (bank->has_debounce) {
  281. bgc->gc.set_debounce = ep93xx_gpio_set_debounce;
  282. bgc->gc.to_irq = ep93xx_gpio_to_irq;
  283. }
  284. return gpiochip_add(&bgc->gc);
  285. }
  286. static int ep93xx_gpio_probe(struct platform_device *pdev)
  287. {
  288. struct ep93xx_gpio *ep93xx_gpio;
  289. struct resource *res;
  290. int i;
  291. struct device *dev = &pdev->dev;
  292. ep93xx_gpio = devm_kzalloc(dev, sizeof(struct ep93xx_gpio), GFP_KERNEL);
  293. if (!ep93xx_gpio)
  294. return -ENOMEM;
  295. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  296. ep93xx_gpio->mmio_base = devm_ioremap_resource(dev, res);
  297. if (IS_ERR(ep93xx_gpio->mmio_base))
  298. return PTR_ERR(ep93xx_gpio->mmio_base);
  299. for (i = 0; i < ARRAY_SIZE(ep93xx_gpio_banks); i++) {
  300. struct bgpio_chip *bgc = &ep93xx_gpio->bgc[i];
  301. struct ep93xx_gpio_bank *bank = &ep93xx_gpio_banks[i];
  302. if (ep93xx_gpio_add_bank(bgc, &pdev->dev,
  303. ep93xx_gpio->mmio_base, bank))
  304. dev_warn(&pdev->dev, "Unable to add gpio bank %s\n",
  305. bank->label);
  306. }
  307. ep93xx_gpio_init_irq();
  308. return 0;
  309. }
  310. static struct platform_driver ep93xx_gpio_driver = {
  311. .driver = {
  312. .name = "gpio-ep93xx",
  313. },
  314. .probe = ep93xx_gpio_probe,
  315. };
  316. static int __init ep93xx_gpio_init(void)
  317. {
  318. return platform_driver_register(&ep93xx_gpio_driver);
  319. }
  320. postcore_initcall(ep93xx_gpio_init);
  321. MODULE_AUTHOR("Ryan Mallon <ryan@bluewatersys.com> "
  322. "H Hartley Sweeten <hsweeten@visionengravers.com>");
  323. MODULE_DESCRIPTION("EP93XX GPIO driver");
  324. MODULE_LICENSE("GPL");