gpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 (irqd_get_trigger_type(d) == 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 (irqd_get_trigger_type(d) == 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. const int gpio = irq_to_gpio(d->irq);
  138. const int port = gpio >> 3;
  139. const int port_mask = 1 << (gpio & 7);
  140. irq_flow_handler_t handler;
  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. handler = 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. handler = 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. handler = 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. handler = 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. handler = 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. __irq_set_handler_locked(d->irq, handler);
  177. gpio_int_enabled[port] |= port_mask;
  178. ep93xx_gpio_update_int_params(port);
  179. return 0;
  180. }
  181. static struct irq_chip ep93xx_gpio_irq_chip = {
  182. .name = "GPIO",
  183. .irq_ack = ep93xx_gpio_irq_ack,
  184. .irq_mask_ack = ep93xx_gpio_irq_mask_ack,
  185. .irq_mask = ep93xx_gpio_irq_mask,
  186. .irq_unmask = ep93xx_gpio_irq_unmask,
  187. .irq_set_type = ep93xx_gpio_irq_type,
  188. };
  189. void __init ep93xx_gpio_init_irq(void)
  190. {
  191. int gpio_irq;
  192. for (gpio_irq = gpio_to_irq(0);
  193. gpio_irq <= gpio_to_irq(EP93XX_GPIO_LINE_MAX_IRQ); ++gpio_irq) {
  194. irq_set_chip_and_handler(gpio_irq, &ep93xx_gpio_irq_chip,
  195. handle_level_irq);
  196. set_irq_flags(gpio_irq, IRQF_VALID);
  197. }
  198. irq_set_chained_handler(IRQ_EP93XX_GPIO_AB,
  199. ep93xx_gpio_ab_irq_handler);
  200. irq_set_chained_handler(IRQ_EP93XX_GPIO0MUX,
  201. ep93xx_gpio_f_irq_handler);
  202. irq_set_chained_handler(IRQ_EP93XX_GPIO1MUX,
  203. ep93xx_gpio_f_irq_handler);
  204. irq_set_chained_handler(IRQ_EP93XX_GPIO2MUX,
  205. ep93xx_gpio_f_irq_handler);
  206. irq_set_chained_handler(IRQ_EP93XX_GPIO3MUX,
  207. ep93xx_gpio_f_irq_handler);
  208. irq_set_chained_handler(IRQ_EP93XX_GPIO4MUX,
  209. ep93xx_gpio_f_irq_handler);
  210. irq_set_chained_handler(IRQ_EP93XX_GPIO5MUX,
  211. ep93xx_gpio_f_irq_handler);
  212. irq_set_chained_handler(IRQ_EP93XX_GPIO6MUX,
  213. ep93xx_gpio_f_irq_handler);
  214. irq_set_chained_handler(IRQ_EP93XX_GPIO7MUX,
  215. ep93xx_gpio_f_irq_handler);
  216. }
  217. /*************************************************************************
  218. * gpiolib interface for EP93xx on-chip GPIOs
  219. *************************************************************************/
  220. struct ep93xx_gpio_chip {
  221. struct gpio_chip chip;
  222. void __iomem *data_reg;
  223. void __iomem *data_dir_reg;
  224. };
  225. #define to_ep93xx_gpio_chip(c) container_of(c, struct ep93xx_gpio_chip, chip)
  226. static int ep93xx_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  227. {
  228. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  229. unsigned long flags;
  230. u8 v;
  231. local_irq_save(flags);
  232. v = __raw_readb(ep93xx_chip->data_dir_reg);
  233. v &= ~(1 << offset);
  234. __raw_writeb(v, ep93xx_chip->data_dir_reg);
  235. local_irq_restore(flags);
  236. return 0;
  237. }
  238. static int ep93xx_gpio_direction_output(struct gpio_chip *chip,
  239. unsigned offset, int val)
  240. {
  241. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  242. unsigned long flags;
  243. int line;
  244. u8 v;
  245. local_irq_save(flags);
  246. /* Set the value */
  247. v = __raw_readb(ep93xx_chip->data_reg);
  248. if (val)
  249. v |= (1 << offset);
  250. else
  251. v &= ~(1 << offset);
  252. __raw_writeb(v, ep93xx_chip->data_reg);
  253. /* Drive as an output */
  254. line = chip->base + offset;
  255. if (line <= EP93XX_GPIO_LINE_MAX_IRQ) {
  256. /* Ports A/B/F */
  257. ep93xx_gpio_int_mask(line);
  258. ep93xx_gpio_update_int_params(line >> 3);
  259. }
  260. v = __raw_readb(ep93xx_chip->data_dir_reg);
  261. v |= (1 << offset);
  262. __raw_writeb(v, ep93xx_chip->data_dir_reg);
  263. local_irq_restore(flags);
  264. return 0;
  265. }
  266. static int ep93xx_gpio_get(struct gpio_chip *chip, unsigned offset)
  267. {
  268. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  269. return !!(__raw_readb(ep93xx_chip->data_reg) & (1 << offset));
  270. }
  271. static void ep93xx_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  272. {
  273. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  274. unsigned long flags;
  275. u8 v;
  276. local_irq_save(flags);
  277. v = __raw_readb(ep93xx_chip->data_reg);
  278. if (val)
  279. v |= (1 << offset);
  280. else
  281. v &= ~(1 << offset);
  282. __raw_writeb(v, ep93xx_chip->data_reg);
  283. local_irq_restore(flags);
  284. }
  285. static int ep93xx_gpio_set_debounce(struct gpio_chip *chip,
  286. unsigned offset, unsigned debounce)
  287. {
  288. int gpio = chip->base + offset;
  289. int irq = gpio_to_irq(gpio);
  290. if (irq < 0)
  291. return -EINVAL;
  292. ep93xx_gpio_int_debounce(irq, debounce ? true : false);
  293. return 0;
  294. }
  295. static void ep93xx_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  296. {
  297. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  298. u8 data_reg, data_dir_reg;
  299. int gpio, i;
  300. data_reg = __raw_readb(ep93xx_chip->data_reg);
  301. data_dir_reg = __raw_readb(ep93xx_chip->data_dir_reg);
  302. gpio = ep93xx_chip->chip.base;
  303. for (i = 0; i < chip->ngpio; i++, gpio++) {
  304. int is_out = data_dir_reg & (1 << i);
  305. int irq = gpio_to_irq(gpio);
  306. seq_printf(s, " %s%d gpio-%-3d (%-12s) %s %s %s\n",
  307. chip->label, i, gpio,
  308. gpiochip_is_requested(chip, i) ? : "",
  309. is_out ? "out" : "in ",
  310. (data_reg & (1<< i)) ? "hi" : "lo",
  311. (!is_out && irq>= 0) ? "(interrupt)" : "");
  312. }
  313. }
  314. #define EP93XX_GPIO_BANK(name, dr, ddr, base_gpio) \
  315. { \
  316. .chip = { \
  317. .label = name, \
  318. .direction_input = ep93xx_gpio_direction_input, \
  319. .direction_output = ep93xx_gpio_direction_output, \
  320. .get = ep93xx_gpio_get, \
  321. .set = ep93xx_gpio_set, \
  322. .dbg_show = ep93xx_gpio_dbg_show, \
  323. .base = base_gpio, \
  324. .ngpio = 8, \
  325. }, \
  326. .data_reg = EP93XX_GPIO_REG(dr), \
  327. .data_dir_reg = EP93XX_GPIO_REG(ddr), \
  328. }
  329. static struct ep93xx_gpio_chip ep93xx_gpio_banks[] = {
  330. EP93XX_GPIO_BANK("A", 0x00, 0x10, 0),
  331. EP93XX_GPIO_BANK("B", 0x04, 0x14, 8),
  332. EP93XX_GPIO_BANK("C", 0x08, 0x18, 40),
  333. EP93XX_GPIO_BANK("D", 0x0c, 0x1c, 24),
  334. EP93XX_GPIO_BANK("E", 0x20, 0x24, 32),
  335. EP93XX_GPIO_BANK("F", 0x30, 0x34, 16),
  336. EP93XX_GPIO_BANK("G", 0x38, 0x3c, 48),
  337. EP93XX_GPIO_BANK("H", 0x40, 0x44, 56),
  338. };
  339. void __init ep93xx_gpio_init(void)
  340. {
  341. int i;
  342. /* Set Ports C, D, E, G, and H for GPIO use */
  343. ep93xx_devcfg_set_bits(EP93XX_SYSCON_DEVCFG_KEYS |
  344. EP93XX_SYSCON_DEVCFG_GONK |
  345. EP93XX_SYSCON_DEVCFG_EONIDE |
  346. EP93XX_SYSCON_DEVCFG_GONIDE |
  347. EP93XX_SYSCON_DEVCFG_HONIDE);
  348. for (i = 0; i < ARRAY_SIZE(ep93xx_gpio_banks); i++) {
  349. struct gpio_chip *chip = &ep93xx_gpio_banks[i].chip;
  350. /*
  351. * Ports A, B, and F support input debouncing when
  352. * used as interrupts.
  353. */
  354. if (!strcmp(chip->label, "A") ||
  355. !strcmp(chip->label, "B") ||
  356. !strcmp(chip->label, "F"))
  357. chip->set_debounce = ep93xx_gpio_set_debounce;
  358. gpiochip_add(chip);
  359. }
  360. }