gpio-ep93xx.c 11 KB

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