gpio.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/io.h>
  19. #include <linux/gpio.h>
  20. #include <linux/irq.h>
  21. #include <mach/hardware.h>
  22. struct ep93xx_gpio_chip {
  23. struct gpio_chip chip;
  24. void __iomem *data_reg;
  25. void __iomem *data_dir_reg;
  26. };
  27. #define to_ep93xx_gpio_chip(c) container_of(c, struct ep93xx_gpio_chip, chip)
  28. /* From core.c */
  29. extern void ep93xx_gpio_int_mask(unsigned line);
  30. extern void ep93xx_gpio_update_int_params(unsigned port);
  31. static int ep93xx_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  32. {
  33. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  34. unsigned long flags;
  35. u8 v;
  36. local_irq_save(flags);
  37. v = __raw_readb(ep93xx_chip->data_dir_reg);
  38. v &= ~(1 << offset);
  39. __raw_writeb(v, ep93xx_chip->data_dir_reg);
  40. local_irq_restore(flags);
  41. return 0;
  42. }
  43. static int ep93xx_gpio_direction_output(struct gpio_chip *chip,
  44. unsigned offset, int val)
  45. {
  46. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  47. unsigned long flags;
  48. int line;
  49. u8 v;
  50. local_irq_save(flags);
  51. /* Set the value */
  52. v = __raw_readb(ep93xx_chip->data_reg);
  53. if (val)
  54. v |= (1 << offset);
  55. else
  56. v &= ~(1 << offset);
  57. __raw_writeb(v, ep93xx_chip->data_reg);
  58. /* Drive as an output */
  59. line = chip->base + offset;
  60. if (line <= EP93XX_GPIO_LINE_MAX_IRQ) {
  61. /* Ports A/B/F */
  62. ep93xx_gpio_int_mask(line);
  63. ep93xx_gpio_update_int_params(line >> 3);
  64. }
  65. v = __raw_readb(ep93xx_chip->data_dir_reg);
  66. v |= (1 << offset);
  67. __raw_writeb(v, ep93xx_chip->data_dir_reg);
  68. local_irq_restore(flags);
  69. return 0;
  70. }
  71. static int ep93xx_gpio_get(struct gpio_chip *chip, unsigned offset)
  72. {
  73. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  74. return !!(__raw_readb(ep93xx_chip->data_reg) & (1 << offset));
  75. }
  76. static void ep93xx_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  77. {
  78. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  79. unsigned long flags;
  80. u8 v;
  81. local_irq_save(flags);
  82. v = __raw_readb(ep93xx_chip->data_reg);
  83. if (val)
  84. v |= (1 << offset);
  85. else
  86. v &= ~(1 << offset);
  87. __raw_writeb(v, ep93xx_chip->data_reg);
  88. local_irq_restore(flags);
  89. }
  90. static void ep93xx_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  91. {
  92. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  93. u8 data_reg, data_dir_reg;
  94. int gpio, i;
  95. data_reg = __raw_readb(ep93xx_chip->data_reg);
  96. data_dir_reg = __raw_readb(ep93xx_chip->data_dir_reg);
  97. gpio = ep93xx_chip->chip.base;
  98. for (i = 0; i < chip->ngpio; i++, gpio++) {
  99. int is_out = data_dir_reg & (1 << i);
  100. seq_printf(s, " %s%d gpio-%-3d (%-12s) %s %s",
  101. chip->label, i, gpio,
  102. gpiochip_is_requested(chip, i) ? : "",
  103. is_out ? "out" : "in ",
  104. (data_reg & (1 << i)) ? "hi" : "lo");
  105. if (!is_out) {
  106. int irq = gpio_to_irq(gpio);
  107. struct irq_desc *desc = irq_desc + irq;
  108. if (irq >= 0 && desc->action) {
  109. char *trigger;
  110. switch (desc->status & IRQ_TYPE_SENSE_MASK) {
  111. case IRQ_TYPE_NONE:
  112. trigger = "(default)";
  113. break;
  114. case IRQ_TYPE_EDGE_FALLING:
  115. trigger = "edge-falling";
  116. break;
  117. case IRQ_TYPE_EDGE_RISING:
  118. trigger = "edge-rising";
  119. break;
  120. case IRQ_TYPE_EDGE_BOTH:
  121. trigger = "edge-both";
  122. break;
  123. case IRQ_TYPE_LEVEL_HIGH:
  124. trigger = "level-high";
  125. break;
  126. case IRQ_TYPE_LEVEL_LOW:
  127. trigger = "level-low";
  128. break;
  129. default:
  130. trigger = "?trigger?";
  131. break;
  132. }
  133. seq_printf(s, " irq-%d %s%s",
  134. irq, trigger,
  135. (desc->status & IRQ_WAKEUP)
  136. ? " wakeup" : "");
  137. }
  138. }
  139. seq_printf(s, "\n");
  140. }
  141. }
  142. #define EP93XX_GPIO_BANK(name, dr, ddr, base_gpio) \
  143. { \
  144. .chip = { \
  145. .label = name, \
  146. .direction_input = ep93xx_gpio_direction_input, \
  147. .direction_output = ep93xx_gpio_direction_output, \
  148. .get = ep93xx_gpio_get, \
  149. .set = ep93xx_gpio_set, \
  150. .dbg_show = ep93xx_gpio_dbg_show, \
  151. .base = base_gpio, \
  152. .ngpio = 8, \
  153. }, \
  154. .data_reg = EP93XX_GPIO_REG(dr), \
  155. .data_dir_reg = EP93XX_GPIO_REG(ddr), \
  156. }
  157. static struct ep93xx_gpio_chip ep93xx_gpio_banks[] = {
  158. EP93XX_GPIO_BANK("A", 0x00, 0x10, 0),
  159. EP93XX_GPIO_BANK("B", 0x04, 0x14, 8),
  160. EP93XX_GPIO_BANK("C", 0x08, 0x18, 40),
  161. EP93XX_GPIO_BANK("D", 0x0c, 0x1c, 24),
  162. EP93XX_GPIO_BANK("E", 0x20, 0x24, 32),
  163. EP93XX_GPIO_BANK("F", 0x30, 0x34, 16),
  164. EP93XX_GPIO_BANK("G", 0x38, 0x3c, 48),
  165. EP93XX_GPIO_BANK("H", 0x40, 0x44, 56),
  166. };
  167. void __init ep93xx_gpio_init(void)
  168. {
  169. int i;
  170. for (i = 0; i < ARRAY_SIZE(ep93xx_gpio_banks); i++)
  171. gpiochip_add(&ep93xx_gpio_banks[i].chip);
  172. }