gpio.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <mach/ep93xx-regs.h>
  20. #include <asm/gpio.h>
  21. struct ep93xx_gpio_chip {
  22. struct gpio_chip chip;
  23. unsigned int data_reg;
  24. unsigned int data_dir_reg;
  25. };
  26. #define to_ep93xx_gpio_chip(c) container_of(c, struct ep93xx_gpio_chip, chip)
  27. /* From core.c */
  28. extern void ep93xx_gpio_int_mask(unsigned line);
  29. extern void ep93xx_gpio_update_int_params(unsigned port);
  30. static int ep93xx_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  31. {
  32. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  33. unsigned long flags;
  34. u8 v;
  35. local_irq_save(flags);
  36. v = __raw_readb(ep93xx_chip->data_dir_reg);
  37. v &= ~(1 << offset);
  38. __raw_writeb(v, ep93xx_chip->data_dir_reg);
  39. local_irq_restore(flags);
  40. return 0;
  41. }
  42. static int ep93xx_gpio_direction_output(struct gpio_chip *chip,
  43. unsigned offset, int val)
  44. {
  45. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  46. unsigned long flags;
  47. int line;
  48. u8 v;
  49. local_irq_save(flags);
  50. /* Set the value */
  51. v = __raw_readb(ep93xx_chip->data_reg);
  52. if (val)
  53. v |= (1 << offset);
  54. else
  55. v &= ~(1 << offset);
  56. __raw_writeb(v, ep93xx_chip->data_reg);
  57. /* Drive as an output */
  58. line = chip->base + offset;
  59. if (line <= EP93XX_GPIO_LINE_MAX_IRQ) {
  60. /* Ports A/B/F */
  61. ep93xx_gpio_int_mask(line);
  62. ep93xx_gpio_update_int_params(line >> 3);
  63. }
  64. v = __raw_readb(ep93xx_chip->data_dir_reg);
  65. v |= (1 << offset);
  66. __raw_writeb(v, ep93xx_chip->data_dir_reg);
  67. local_irq_restore(flags);
  68. return 0;
  69. }
  70. static int ep93xx_gpio_get(struct gpio_chip *chip, unsigned offset)
  71. {
  72. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  73. return !!(__raw_readb(ep93xx_chip->data_reg) & (1 << offset));
  74. }
  75. static void ep93xx_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  76. {
  77. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  78. unsigned long flags;
  79. u8 v;
  80. local_irq_save(flags);
  81. v = __raw_readb(ep93xx_chip->data_reg);
  82. if (val)
  83. v |= (1 << offset);
  84. else
  85. v &= ~(1 << offset);
  86. __raw_writeb(v, ep93xx_chip->data_reg);
  87. local_irq_restore(flags);
  88. }
  89. static void ep93xx_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  90. {
  91. struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
  92. u8 data_reg, data_dir_reg;
  93. int i;
  94. data_reg = __raw_readb(ep93xx_chip->data_reg);
  95. data_dir_reg = __raw_readb(ep93xx_chip->data_dir_reg);
  96. for (i = 0; i < chip->ngpio; i++)
  97. seq_printf(s, "GPIO %s%d: %s %s\n", chip->label, i,
  98. (data_reg & (1 << i)) ? "set" : "clear",
  99. (data_dir_reg & (1 << i)) ? "out" : "in");
  100. }
  101. #define EP93XX_GPIO_BANK(name, dr, ddr, base_gpio) \
  102. { \
  103. .chip = { \
  104. .label = name, \
  105. .direction_input = ep93xx_gpio_direction_input, \
  106. .direction_output = ep93xx_gpio_direction_output, \
  107. .get = ep93xx_gpio_get, \
  108. .set = ep93xx_gpio_set, \
  109. .dbg_show = ep93xx_gpio_dbg_show, \
  110. .base = base_gpio, \
  111. .ngpio = 8, \
  112. }, \
  113. .data_reg = EP93XX_GPIO_REG(dr), \
  114. .data_dir_reg = EP93XX_GPIO_REG(ddr), \
  115. }
  116. static struct ep93xx_gpio_chip ep93xx_gpio_banks[] = {
  117. EP93XX_GPIO_BANK("A", 0x00, 0x10, 0),
  118. EP93XX_GPIO_BANK("B", 0x04, 0x14, 8),
  119. EP93XX_GPIO_BANK("C", 0x08, 0x18, 40),
  120. EP93XX_GPIO_BANK("D", 0x0c, 0x1c, 24),
  121. EP93XX_GPIO_BANK("E", 0x20, 0x24, 32),
  122. EP93XX_GPIO_BANK("F", 0x30, 0x34, 16),
  123. EP93XX_GPIO_BANK("G", 0x38, 0x3c, 48),
  124. EP93XX_GPIO_BANK("H", 0x40, 0x44, 56),
  125. };
  126. void __init ep93xx_gpio_init(void)
  127. {
  128. int i;
  129. for (i = 0; i < ARRAY_SIZE(ep93xx_gpio_banks); i++)
  130. gpiochip_add(&ep93xx_gpio_banks[i].chip);
  131. }