gpio-ep93xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Generic EP93xx GPIO handling
  3. *
  4. * Copyright (c) 2008 Ryan Mallon <ryan@bluewatersys.com>
  5. *
  6. * Based on code originally from:
  7. * linux/arch/arm/mach-ep93xx/core.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/io.h>
  18. #include <linux/gpio.h>
  19. #include <linux/irq.h>
  20. #include <mach/hardware.h>
  21. /*************************************************************************
  22. * Interrupt handling for EP93xx on-chip GPIOs
  23. *************************************************************************/
  24. static unsigned char gpio_int_unmasked[3];
  25. static unsigned char gpio_int_enabled[3];
  26. static unsigned char gpio_int_type1[3];
  27. static unsigned char gpio_int_type2[3];
  28. static unsigned char gpio_int_debounce[3];
  29. /* Port ordering is: A B F */
  30. static const u8 int_type1_register_offset[3] = { 0x90, 0xac, 0x4c };
  31. static const u8 int_type2_register_offset[3] = { 0x94, 0xb0, 0x50 };
  32. static const u8 eoi_register_offset[3] = { 0x98, 0xb4, 0x54 };
  33. static const u8 int_en_register_offset[3] = { 0x9c, 0xb8, 0x58 };
  34. static const u8 int_debounce_register_offset[3] = { 0xa8, 0xc4, 0x64 };
  35. static void ep93xx_gpio_update_int_params(unsigned port)
  36. {
  37. BUG_ON(port > 2);
  38. __raw_writeb(0, EP93XX_GPIO_REG(int_en_register_offset[port]));
  39. __raw_writeb(gpio_int_type2[port],
  40. EP93XX_GPIO_REG(int_type2_register_offset[port]));
  41. __raw_writeb(gpio_int_type1[port],
  42. EP93XX_GPIO_REG(int_type1_register_offset[port]));
  43. __raw_writeb(gpio_int_unmasked[port] & gpio_int_enabled[port],
  44. EP93XX_GPIO_REG(int_en_register_offset[port]));
  45. }
  46. static inline void ep93xx_gpio_int_mask(unsigned line)
  47. {
  48. gpio_int_unmasked[line >> 3] &= ~(1 << (line & 7));
  49. }
  50. static void ep93xx_gpio_int_debounce(unsigned int irq, bool enable)
  51. {
  52. int line = irq_to_gpio(irq);
  53. int port = line >> 3;
  54. int port_mask = 1 << (line & 7);
  55. if (enable)
  56. gpio_int_debounce[port] |= port_mask;
  57. else
  58. gpio_int_debounce[port] &= ~port_mask;
  59. __raw_writeb(gpio_int_debounce[port],
  60. EP93XX_GPIO_REG(int_debounce_register_offset[port]));
  61. }
  62. static void ep93xx_gpio_ab_irq_handler(unsigned int irq, struct irq_desc *desc)
  63. {
  64. unsigned char status;
  65. int i;
  66. status = __raw_readb(EP93XX_GPIO_A_INT_STATUS);
  67. for (i = 0; i < 8; i++) {
  68. if (status & (1 << i)) {
  69. int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_A(0)) + i;
  70. generic_handle_irq(gpio_irq);
  71. }
  72. }
  73. status = __raw_readb(EP93XX_GPIO_B_INT_STATUS);
  74. for (i = 0; i < 8; i++) {
  75. if (status & (1 << i)) {
  76. int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_B(0)) + i;
  77. generic_handle_irq(gpio_irq);
  78. }
  79. }
  80. }
  81. static void ep93xx_gpio_f_irq_handler(unsigned int irq, struct irq_desc *desc)
  82. {
  83. /*
  84. * map discontiguous hw irq range to continuous sw irq range:
  85. *
  86. * IRQ_EP93XX_GPIO{0..7}MUX -> gpio_to_irq(EP93XX_GPIO_LINE_F({0..7})
  87. */
  88. int port_f_idx = ((irq + 1) & 7) ^ 4; /* {19..22,47..50} -> {0..7} */
  89. int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_F(0)) + port_f_idx;
  90. generic_handle_irq(gpio_irq);
  91. }
  92. static void ep93xx_gpio_irq_ack(struct irq_data *d)
  93. {
  94. int line = irq_to_gpio(d->irq);
  95. int port = line >> 3;
  96. int port_mask = 1 << (line & 7);
  97. if (irqd_get_trigger_type(d) == IRQ_TYPE_EDGE_BOTH) {
  98. gpio_int_type2[port] ^= port_mask; /* switch edge direction */
  99. ep93xx_gpio_update_int_params(port);
  100. }
  101. __raw_writeb(port_mask, EP93XX_GPIO_REG(eoi_register_offset[port]));
  102. }
  103. static void ep93xx_gpio_irq_mask_ack(struct irq_data *d)
  104. {
  105. int line = irq_to_gpio(d->irq);
  106. int port = line >> 3;
  107. int port_mask = 1 << (line & 7);
  108. if (irqd_get_trigger_type(d) == IRQ_TYPE_EDGE_BOTH)
  109. gpio_int_type2[port] ^= port_mask; /* switch edge direction */
  110. gpio_int_unmasked[port] &= ~port_mask;
  111. ep93xx_gpio_update_int_params(port);
  112. __raw_writeb(port_mask, EP93XX_GPIO_REG(eoi_register_offset[port]));
  113. }
  114. static void ep93xx_gpio_irq_mask(struct irq_data *d)
  115. {
  116. int line = irq_to_gpio(d->irq);
  117. int port = line >> 3;
  118. gpio_int_unmasked[port] &= ~(1 << (line & 7));
  119. ep93xx_gpio_update_int_params(port);
  120. }
  121. static void ep93xx_gpio_irq_unmask(struct irq_data *d)
  122. {
  123. int line = irq_to_gpio(d->irq);
  124. int port = line >> 3;
  125. gpio_int_unmasked[port] |= 1 << (line & 7);
  126. ep93xx_gpio_update_int_params(port);
  127. }
  128. /*
  129. * gpio_int_type1 controls whether the interrupt is level (0) or
  130. * edge (1) triggered, while gpio_int_type2 controls whether it
  131. * triggers on low/falling (0) or high/rising (1).
  132. */
  133. static int ep93xx_gpio_irq_type(struct irq_data *d, unsigned int type)
  134. {
  135. const int gpio = irq_to_gpio(d->irq);
  136. const int port = gpio >> 3;
  137. const int port_mask = 1 << (gpio & 7);
  138. irq_flow_handler_t handler;
  139. gpio_direction_input(gpio);
  140. switch (type) {
  141. case IRQ_TYPE_EDGE_RISING:
  142. gpio_int_type1[port] |= port_mask;
  143. gpio_int_type2[port] |= port_mask;
  144. handler = handle_edge_irq;
  145. break;
  146. case IRQ_TYPE_EDGE_FALLING:
  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_LEVEL_HIGH:
  152. gpio_int_type1[port] &= ~port_mask;
  153. gpio_int_type2[port] |= port_mask;
  154. handler = handle_level_irq;
  155. break;
  156. case IRQ_TYPE_LEVEL_LOW:
  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_EDGE_BOTH:
  162. gpio_int_type1[port] |= port_mask;
  163. /* set initial polarity based on current input level */
  164. if (gpio_get_value(gpio))
  165. gpio_int_type2[port] &= ~port_mask; /* falling */
  166. else
  167. gpio_int_type2[port] |= port_mask; /* rising */
  168. handler = handle_edge_irq;
  169. break;
  170. default:
  171. pr_err("failed to set irq type %d for gpio %d\n", type, gpio);
  172. return -EINVAL;
  173. }
  174. __irq_set_handler_locked(d->irq, handler);
  175. gpio_int_enabled[port] |= port_mask;
  176. ep93xx_gpio_update_int_params(port);
  177. return 0;
  178. }
  179. static struct irq_chip ep93xx_gpio_irq_chip = {
  180. .name = "GPIO",
  181. .irq_ack = ep93xx_gpio_irq_ack,
  182. .irq_mask_ack = ep93xx_gpio_irq_mask_ack,
  183. .irq_mask = ep93xx_gpio_irq_mask,
  184. .irq_unmask = ep93xx_gpio_irq_unmask,
  185. .irq_set_type = ep93xx_gpio_irq_type,
  186. };
  187. void __init ep93xx_gpio_init_irq(void)
  188. {
  189. int gpio_irq;
  190. for (gpio_irq = gpio_to_irq(0);
  191. gpio_irq <= gpio_to_irq(EP93XX_GPIO_LINE_MAX_IRQ); ++gpio_irq) {
  192. irq_set_chip_and_handler(gpio_irq, &ep93xx_gpio_irq_chip,
  193. handle_level_irq);
  194. set_irq_flags(gpio_irq, IRQF_VALID);
  195. }
  196. irq_set_chained_handler(IRQ_EP93XX_GPIO_AB,
  197. ep93xx_gpio_ab_irq_handler);
  198. irq_set_chained_handler(IRQ_EP93XX_GPIO0MUX,
  199. ep93xx_gpio_f_irq_handler);
  200. irq_set_chained_handler(IRQ_EP93XX_GPIO1MUX,
  201. ep93xx_gpio_f_irq_handler);
  202. irq_set_chained_handler(IRQ_EP93XX_GPIO2MUX,
  203. ep93xx_gpio_f_irq_handler);
  204. irq_set_chained_handler(IRQ_EP93XX_GPIO3MUX,
  205. ep93xx_gpio_f_irq_handler);
  206. irq_set_chained_handler(IRQ_EP93XX_GPIO4MUX,
  207. ep93xx_gpio_f_irq_handler);
  208. irq_set_chained_handler(IRQ_EP93XX_GPIO5MUX,
  209. ep93xx_gpio_f_irq_handler);
  210. irq_set_chained_handler(IRQ_EP93XX_GPIO6MUX,
  211. ep93xx_gpio_f_irq_handler);
  212. irq_set_chained_handler(IRQ_EP93XX_GPIO7MUX,
  213. ep93xx_gpio_f_irq_handler);
  214. }
  215. /*************************************************************************
  216. * gpiolib interface for EP93xx on-chip GPIOs
  217. *************************************************************************/
  218. struct ep93xx_gpio_chip {
  219. struct gpio_chip chip;
  220. void __iomem *data_reg;
  221. void __iomem *data_dir_reg;
  222. };
  223. #define to_ep93xx_gpio_chip(c) container_of(c, struct ep93xx_gpio_chip, chip)
  224. static int ep93xx_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  225. {
  226. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  227. unsigned long flags;
  228. u8 v;
  229. local_irq_save(flags);
  230. v = __raw_readb(ep93xx_chip->data_dir_reg);
  231. v &= ~(1 << offset);
  232. __raw_writeb(v, ep93xx_chip->data_dir_reg);
  233. local_irq_restore(flags);
  234. return 0;
  235. }
  236. static int ep93xx_gpio_direction_output(struct gpio_chip *chip,
  237. unsigned offset, int val)
  238. {
  239. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  240. unsigned long flags;
  241. int line;
  242. u8 v;
  243. local_irq_save(flags);
  244. /* Set the value */
  245. v = __raw_readb(ep93xx_chip->data_reg);
  246. if (val)
  247. v |= (1 << offset);
  248. else
  249. v &= ~(1 << offset);
  250. __raw_writeb(v, ep93xx_chip->data_reg);
  251. /* Drive as an output */
  252. line = chip->base + offset;
  253. if (line <= EP93XX_GPIO_LINE_MAX_IRQ) {
  254. /* Ports A/B/F */
  255. ep93xx_gpio_int_mask(line);
  256. ep93xx_gpio_update_int_params(line >> 3);
  257. }
  258. v = __raw_readb(ep93xx_chip->data_dir_reg);
  259. v |= (1 << offset);
  260. __raw_writeb(v, ep93xx_chip->data_dir_reg);
  261. local_irq_restore(flags);
  262. return 0;
  263. }
  264. static int ep93xx_gpio_get(struct gpio_chip *chip, unsigned offset)
  265. {
  266. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  267. return !!(__raw_readb(ep93xx_chip->data_reg) & (1 << offset));
  268. }
  269. static void ep93xx_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  270. {
  271. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  272. unsigned long flags;
  273. u8 v;
  274. local_irq_save(flags);
  275. v = __raw_readb(ep93xx_chip->data_reg);
  276. if (val)
  277. v |= (1 << offset);
  278. else
  279. v &= ~(1 << offset);
  280. __raw_writeb(v, ep93xx_chip->data_reg);
  281. local_irq_restore(flags);
  282. }
  283. static int ep93xx_gpio_set_debounce(struct gpio_chip *chip,
  284. unsigned offset, unsigned debounce)
  285. {
  286. int gpio = chip->base + offset;
  287. int irq = gpio_to_irq(gpio);
  288. if (irq < 0)
  289. return -EINVAL;
  290. ep93xx_gpio_int_debounce(irq, debounce ? true : false);
  291. return 0;
  292. }
  293. #define EP93XX_GPIO_BANK(name, dr, ddr, base_gpio) \
  294. { \
  295. .chip = { \
  296. .label = name, \
  297. .direction_input = ep93xx_gpio_direction_input, \
  298. .direction_output = ep93xx_gpio_direction_output, \
  299. .get = ep93xx_gpio_get, \
  300. .set = ep93xx_gpio_set, \
  301. .base = base_gpio, \
  302. .ngpio = 8, \
  303. }, \
  304. .data_reg = EP93XX_GPIO_REG(dr), \
  305. .data_dir_reg = EP93XX_GPIO_REG(ddr), \
  306. }
  307. static struct ep93xx_gpio_chip ep93xx_gpio_banks[] = {
  308. EP93XX_GPIO_BANK("A", 0x00, 0x10, 0),
  309. EP93XX_GPIO_BANK("B", 0x04, 0x14, 8),
  310. EP93XX_GPIO_BANK("C", 0x08, 0x18, 40),
  311. EP93XX_GPIO_BANK("D", 0x0c, 0x1c, 24),
  312. EP93XX_GPIO_BANK("E", 0x20, 0x24, 32),
  313. EP93XX_GPIO_BANK("F", 0x30, 0x34, 16),
  314. EP93XX_GPIO_BANK("G", 0x38, 0x3c, 48),
  315. EP93XX_GPIO_BANK("H", 0x40, 0x44, 56),
  316. };
  317. void __init ep93xx_gpio_init(void)
  318. {
  319. int i;
  320. /* Set Ports C, D, E, G, and H for GPIO use */
  321. ep93xx_devcfg_set_bits(EP93XX_SYSCON_DEVCFG_KEYS |
  322. EP93XX_SYSCON_DEVCFG_GONK |
  323. EP93XX_SYSCON_DEVCFG_EONIDE |
  324. EP93XX_SYSCON_DEVCFG_GONIDE |
  325. EP93XX_SYSCON_DEVCFG_HONIDE);
  326. for (i = 0; i < ARRAY_SIZE(ep93xx_gpio_banks); i++) {
  327. struct gpio_chip *chip = &ep93xx_gpio_banks[i].chip;
  328. /*
  329. * Ports A, B, and F support input debouncing when
  330. * used as interrupts.
  331. */
  332. if (!strcmp(chip->label, "A") ||
  333. !strcmp(chip->label, "B") ||
  334. !strcmp(chip->label, "F"))
  335. chip->set_debounce = ep93xx_gpio_set_debounce;
  336. gpiochip_add(chip);
  337. }
  338. }