gpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. void ep93xx_gpio_int_debounce(unsigned int irq, int 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. EXPORT_SYMBOL(ep93xx_gpio_int_debounce);
  65. static void ep93xx_gpio_ab_irq_handler(unsigned int irq, struct irq_desc *desc)
  66. {
  67. unsigned char status;
  68. int i;
  69. status = __raw_readb(EP93XX_GPIO_A_INT_STATUS);
  70. for (i = 0; i < 8; i++) {
  71. if (status & (1 << i)) {
  72. int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_A(0)) + i;
  73. generic_handle_irq(gpio_irq);
  74. }
  75. }
  76. status = __raw_readb(EP93XX_GPIO_B_INT_STATUS);
  77. for (i = 0; i < 8; i++) {
  78. if (status & (1 << i)) {
  79. int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_B(0)) + i;
  80. generic_handle_irq(gpio_irq);
  81. }
  82. }
  83. }
  84. static void ep93xx_gpio_f_irq_handler(unsigned int irq, struct irq_desc *desc)
  85. {
  86. /*
  87. * map discontiguous hw irq range to continous sw irq range:
  88. *
  89. * IRQ_EP93XX_GPIO{0..7}MUX -> gpio_to_irq(EP93XX_GPIO_LINE_F({0..7})
  90. */
  91. int port_f_idx = ((irq + 1) & 7) ^ 4; /* {19..22,47..50} -> {0..7} */
  92. int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_F(0)) + port_f_idx;
  93. generic_handle_irq(gpio_irq);
  94. }
  95. static void ep93xx_gpio_irq_ack(unsigned int irq)
  96. {
  97. int line = irq_to_gpio(irq);
  98. int port = line >> 3;
  99. int port_mask = 1 << (line & 7);
  100. if ((irq_desc[irq].status & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
  101. gpio_int_type2[port] ^= port_mask; /* switch edge direction */
  102. ep93xx_gpio_update_int_params(port);
  103. }
  104. __raw_writeb(port_mask, EP93XX_GPIO_REG(eoi_register_offset[port]));
  105. }
  106. static void ep93xx_gpio_irq_mask_ack(unsigned int irq)
  107. {
  108. int line = irq_to_gpio(irq);
  109. int port = line >> 3;
  110. int port_mask = 1 << (line & 7);
  111. if ((irq_desc[irq].status & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
  112. gpio_int_type2[port] ^= port_mask; /* switch edge direction */
  113. gpio_int_unmasked[port] &= ~port_mask;
  114. ep93xx_gpio_update_int_params(port);
  115. __raw_writeb(port_mask, EP93XX_GPIO_REG(eoi_register_offset[port]));
  116. }
  117. static void ep93xx_gpio_irq_mask(unsigned int irq)
  118. {
  119. int line = irq_to_gpio(irq);
  120. int port = line >> 3;
  121. gpio_int_unmasked[port] &= ~(1 << (line & 7));
  122. ep93xx_gpio_update_int_params(port);
  123. }
  124. static void ep93xx_gpio_irq_unmask(unsigned int irq)
  125. {
  126. int line = irq_to_gpio(irq);
  127. int port = line >> 3;
  128. gpio_int_unmasked[port] |= 1 << (line & 7);
  129. ep93xx_gpio_update_int_params(port);
  130. }
  131. /*
  132. * gpio_int_type1 controls whether the interrupt is level (0) or
  133. * edge (1) triggered, while gpio_int_type2 controls whether it
  134. * triggers on low/falling (0) or high/rising (1).
  135. */
  136. static int ep93xx_gpio_irq_type(unsigned int irq, unsigned int type)
  137. {
  138. struct irq_desc *desc = irq_desc + irq;
  139. const int gpio = irq_to_gpio(irq);
  140. const int port = gpio >> 3;
  141. const int port_mask = 1 << (gpio & 7);
  142. gpio_direction_input(gpio);
  143. switch (type) {
  144. case IRQ_TYPE_EDGE_RISING:
  145. gpio_int_type1[port] |= port_mask;
  146. gpio_int_type2[port] |= port_mask;
  147. desc->handle_irq = handle_edge_irq;
  148. break;
  149. case IRQ_TYPE_EDGE_FALLING:
  150. gpio_int_type1[port] |= port_mask;
  151. gpio_int_type2[port] &= ~port_mask;
  152. desc->handle_irq = handle_edge_irq;
  153. break;
  154. case IRQ_TYPE_LEVEL_HIGH:
  155. gpio_int_type1[port] &= ~port_mask;
  156. gpio_int_type2[port] |= port_mask;
  157. desc->handle_irq = handle_level_irq;
  158. break;
  159. case IRQ_TYPE_LEVEL_LOW:
  160. gpio_int_type1[port] &= ~port_mask;
  161. gpio_int_type2[port] &= ~port_mask;
  162. desc->handle_irq = handle_level_irq;
  163. break;
  164. case IRQ_TYPE_EDGE_BOTH:
  165. gpio_int_type1[port] |= port_mask;
  166. /* set initial polarity based on current input level */
  167. if (gpio_get_value(gpio))
  168. gpio_int_type2[port] &= ~port_mask; /* falling */
  169. else
  170. gpio_int_type2[port] |= port_mask; /* rising */
  171. desc->handle_irq = handle_edge_irq;
  172. break;
  173. default:
  174. pr_err("failed to set irq type %d for gpio %d\n", type, gpio);
  175. return -EINVAL;
  176. }
  177. gpio_int_enabled[port] |= port_mask;
  178. desc->status &= ~IRQ_TYPE_SENSE_MASK;
  179. desc->status |= type & IRQ_TYPE_SENSE_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. .ack = ep93xx_gpio_irq_ack,
  186. .mask_ack = ep93xx_gpio_irq_mask_ack,
  187. .mask = ep93xx_gpio_irq_mask,
  188. .unmask = ep93xx_gpio_irq_unmask,
  189. .set_type = ep93xx_gpio_irq_type,
  190. };
  191. void __init 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. set_irq_chip(gpio_irq, &ep93xx_gpio_irq_chip);
  197. set_irq_handler(gpio_irq, handle_level_irq);
  198. set_irq_flags(gpio_irq, IRQF_VALID);
  199. }
  200. set_irq_chained_handler(IRQ_EP93XX_GPIO_AB, ep93xx_gpio_ab_irq_handler);
  201. set_irq_chained_handler(IRQ_EP93XX_GPIO0MUX, ep93xx_gpio_f_irq_handler);
  202. set_irq_chained_handler(IRQ_EP93XX_GPIO1MUX, ep93xx_gpio_f_irq_handler);
  203. set_irq_chained_handler(IRQ_EP93XX_GPIO2MUX, ep93xx_gpio_f_irq_handler);
  204. set_irq_chained_handler(IRQ_EP93XX_GPIO3MUX, ep93xx_gpio_f_irq_handler);
  205. set_irq_chained_handler(IRQ_EP93XX_GPIO4MUX, ep93xx_gpio_f_irq_handler);
  206. set_irq_chained_handler(IRQ_EP93XX_GPIO5MUX, ep93xx_gpio_f_irq_handler);
  207. set_irq_chained_handler(IRQ_EP93XX_GPIO6MUX, ep93xx_gpio_f_irq_handler);
  208. set_irq_chained_handler(IRQ_EP93XX_GPIO7MUX, ep93xx_gpio_f_irq_handler);
  209. }
  210. /*************************************************************************
  211. * gpiolib interface for EP93xx on-chip GPIOs
  212. *************************************************************************/
  213. struct ep93xx_gpio_chip {
  214. struct gpio_chip chip;
  215. void __iomem *data_reg;
  216. void __iomem *data_dir_reg;
  217. };
  218. #define to_ep93xx_gpio_chip(c) container_of(c, struct ep93xx_gpio_chip, chip)
  219. static int ep93xx_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  220. {
  221. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  222. unsigned long flags;
  223. u8 v;
  224. local_irq_save(flags);
  225. v = __raw_readb(ep93xx_chip->data_dir_reg);
  226. v &= ~(1 << offset);
  227. __raw_writeb(v, ep93xx_chip->data_dir_reg);
  228. local_irq_restore(flags);
  229. return 0;
  230. }
  231. static int ep93xx_gpio_direction_output(struct gpio_chip *chip,
  232. unsigned offset, int val)
  233. {
  234. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  235. unsigned long flags;
  236. int line;
  237. u8 v;
  238. local_irq_save(flags);
  239. /* Set the value */
  240. v = __raw_readb(ep93xx_chip->data_reg);
  241. if (val)
  242. v |= (1 << offset);
  243. else
  244. v &= ~(1 << offset);
  245. __raw_writeb(v, ep93xx_chip->data_reg);
  246. /* Drive as an output */
  247. line = chip->base + offset;
  248. if (line <= EP93XX_GPIO_LINE_MAX_IRQ) {
  249. /* Ports A/B/F */
  250. ep93xx_gpio_int_mask(line);
  251. ep93xx_gpio_update_int_params(line >> 3);
  252. }
  253. v = __raw_readb(ep93xx_chip->data_dir_reg);
  254. v |= (1 << offset);
  255. __raw_writeb(v, ep93xx_chip->data_dir_reg);
  256. local_irq_restore(flags);
  257. return 0;
  258. }
  259. static int ep93xx_gpio_get(struct gpio_chip *chip, unsigned offset)
  260. {
  261. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  262. return !!(__raw_readb(ep93xx_chip->data_reg) & (1 << offset));
  263. }
  264. static void ep93xx_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  265. {
  266. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  267. unsigned long flags;
  268. u8 v;
  269. local_irq_save(flags);
  270. v = __raw_readb(ep93xx_chip->data_reg);
  271. if (val)
  272. v |= (1 << offset);
  273. else
  274. v &= ~(1 << offset);
  275. __raw_writeb(v, ep93xx_chip->data_reg);
  276. local_irq_restore(flags);
  277. }
  278. static void ep93xx_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  279. {
  280. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  281. u8 data_reg, data_dir_reg;
  282. int gpio, i;
  283. data_reg = __raw_readb(ep93xx_chip->data_reg);
  284. data_dir_reg = __raw_readb(ep93xx_chip->data_dir_reg);
  285. gpio = ep93xx_chip->chip.base;
  286. for (i = 0; i < chip->ngpio; i++, gpio++) {
  287. int is_out = data_dir_reg & (1 << i);
  288. seq_printf(s, " %s%d gpio-%-3d (%-12s) %s %s",
  289. chip->label, i, gpio,
  290. gpiochip_is_requested(chip, i) ? : "",
  291. is_out ? "out" : "in ",
  292. (data_reg & (1 << i)) ? "hi" : "lo");
  293. if (!is_out) {
  294. int irq = gpio_to_irq(gpio);
  295. struct irq_desc *desc = irq_desc + irq;
  296. if (irq >= 0 && desc->action) {
  297. char *trigger;
  298. switch (desc->status & IRQ_TYPE_SENSE_MASK) {
  299. case IRQ_TYPE_NONE:
  300. trigger = "(default)";
  301. break;
  302. case IRQ_TYPE_EDGE_FALLING:
  303. trigger = "edge-falling";
  304. break;
  305. case IRQ_TYPE_EDGE_RISING:
  306. trigger = "edge-rising";
  307. break;
  308. case IRQ_TYPE_EDGE_BOTH:
  309. trigger = "edge-both";
  310. break;
  311. case IRQ_TYPE_LEVEL_HIGH:
  312. trigger = "level-high";
  313. break;
  314. case IRQ_TYPE_LEVEL_LOW:
  315. trigger = "level-low";
  316. break;
  317. default:
  318. trigger = "?trigger?";
  319. break;
  320. }
  321. seq_printf(s, " irq-%d %s%s",
  322. irq, trigger,
  323. (desc->status & IRQ_WAKEUP)
  324. ? " wakeup" : "");
  325. }
  326. }
  327. seq_printf(s, "\n");
  328. }
  329. }
  330. #define EP93XX_GPIO_BANK(name, dr, ddr, base_gpio) \
  331. { \
  332. .chip = { \
  333. .label = name, \
  334. .direction_input = ep93xx_gpio_direction_input, \
  335. .direction_output = ep93xx_gpio_direction_output, \
  336. .get = ep93xx_gpio_get, \
  337. .set = ep93xx_gpio_set, \
  338. .dbg_show = ep93xx_gpio_dbg_show, \
  339. .base = base_gpio, \
  340. .ngpio = 8, \
  341. }, \
  342. .data_reg = EP93XX_GPIO_REG(dr), \
  343. .data_dir_reg = EP93XX_GPIO_REG(ddr), \
  344. }
  345. static struct ep93xx_gpio_chip ep93xx_gpio_banks[] = {
  346. EP93XX_GPIO_BANK("A", 0x00, 0x10, 0),
  347. EP93XX_GPIO_BANK("B", 0x04, 0x14, 8),
  348. EP93XX_GPIO_BANK("C", 0x08, 0x18, 40),
  349. EP93XX_GPIO_BANK("D", 0x0c, 0x1c, 24),
  350. EP93XX_GPIO_BANK("E", 0x20, 0x24, 32),
  351. EP93XX_GPIO_BANK("F", 0x30, 0x34, 16),
  352. EP93XX_GPIO_BANK("G", 0x38, 0x3c, 48),
  353. EP93XX_GPIO_BANK("H", 0x40, 0x44, 56),
  354. };
  355. void __init ep93xx_gpio_init(void)
  356. {
  357. int i;
  358. for (i = 0; i < ARRAY_SIZE(ep93xx_gpio_banks); i++)
  359. gpiochip_add(&ep93xx_gpio_banks[i].chip);
  360. }