gpio.c 12 KB

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