gpio.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
  7. * Copyright (C) 2008-2011 Florian Fainelli <florian@openwrt.org>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/gpio.h>
  14. #include <bcm63xx_cpu.h>
  15. #include <bcm63xx_gpio.h>
  16. #include <bcm63xx_io.h>
  17. #include <bcm63xx_regs.h>
  18. #ifndef BCMCPU_RUNTIME_DETECT
  19. #define gpio_out_low_reg GPIO_DATA_LO_REG
  20. #ifdef CONFIG_BCM63XX_CPU_6345
  21. #ifdef gpio_out_low_reg
  22. #undef gpio_out_low_reg
  23. #define gpio_out_low_reg GPIO_DATA_LO_REG_6345
  24. #endif /* gpio_out_low_reg */
  25. #endif /* CONFIG_BCM63XX_CPU_6345 */
  26. static inline void bcm63xx_gpio_out_low_reg_init(void)
  27. {
  28. }
  29. #else /* ! BCMCPU_RUNTIME_DETECT */
  30. static u32 gpio_out_low_reg;
  31. static void bcm63xx_gpio_out_low_reg_init(void)
  32. {
  33. switch (bcm63xx_get_cpu_id()) {
  34. case BCM6345_CPU_ID:
  35. gpio_out_low_reg = GPIO_DATA_LO_REG_6345;
  36. break;
  37. default:
  38. gpio_out_low_reg = GPIO_DATA_LO_REG;
  39. break;
  40. }
  41. }
  42. #endif /* ! BCMCPU_RUNTIME_DETECT */
  43. static DEFINE_SPINLOCK(bcm63xx_gpio_lock);
  44. static u32 gpio_out_low, gpio_out_high;
  45. static void bcm63xx_gpio_set(struct gpio_chip *chip,
  46. unsigned gpio, int val)
  47. {
  48. u32 reg;
  49. u32 mask;
  50. u32 *v;
  51. unsigned long flags;
  52. if (gpio >= chip->ngpio)
  53. BUG();
  54. if (gpio < 32) {
  55. reg = gpio_out_low_reg;
  56. mask = 1 << gpio;
  57. v = &gpio_out_low;
  58. } else {
  59. reg = GPIO_DATA_HI_REG;
  60. mask = 1 << (gpio - 32);
  61. v = &gpio_out_high;
  62. }
  63. spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
  64. if (val)
  65. *v |= mask;
  66. else
  67. *v &= ~mask;
  68. bcm_gpio_writel(*v, reg);
  69. spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
  70. }
  71. static int bcm63xx_gpio_get(struct gpio_chip *chip, unsigned gpio)
  72. {
  73. u32 reg;
  74. u32 mask;
  75. if (gpio >= chip->ngpio)
  76. BUG();
  77. if (gpio < 32) {
  78. reg = gpio_out_low_reg;
  79. mask = 1 << gpio;
  80. } else {
  81. reg = GPIO_DATA_HI_REG;
  82. mask = 1 << (gpio - 32);
  83. }
  84. return !!(bcm_gpio_readl(reg) & mask);
  85. }
  86. static int bcm63xx_gpio_set_direction(struct gpio_chip *chip,
  87. unsigned gpio, int dir)
  88. {
  89. u32 reg;
  90. u32 mask;
  91. u32 tmp;
  92. unsigned long flags;
  93. if (gpio >= chip->ngpio)
  94. BUG();
  95. if (gpio < 32) {
  96. reg = GPIO_CTL_LO_REG;
  97. mask = 1 << gpio;
  98. } else {
  99. reg = GPIO_CTL_HI_REG;
  100. mask = 1 << (gpio - 32);
  101. }
  102. spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
  103. tmp = bcm_gpio_readl(reg);
  104. if (dir == BCM63XX_GPIO_DIR_IN)
  105. tmp &= ~mask;
  106. else
  107. tmp |= mask;
  108. bcm_gpio_writel(tmp, reg);
  109. spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
  110. return 0;
  111. }
  112. static int bcm63xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  113. {
  114. return bcm63xx_gpio_set_direction(chip, gpio, BCM63XX_GPIO_DIR_IN);
  115. }
  116. static int bcm63xx_gpio_direction_output(struct gpio_chip *chip,
  117. unsigned gpio, int value)
  118. {
  119. bcm63xx_gpio_set(chip, gpio, value);
  120. return bcm63xx_gpio_set_direction(chip, gpio, BCM63XX_GPIO_DIR_OUT);
  121. }
  122. static struct gpio_chip bcm63xx_gpio_chip = {
  123. .label = "bcm63xx-gpio",
  124. .direction_input = bcm63xx_gpio_direction_input,
  125. .direction_output = bcm63xx_gpio_direction_output,
  126. .get = bcm63xx_gpio_get,
  127. .set = bcm63xx_gpio_set,
  128. .base = 0,
  129. };
  130. int __init bcm63xx_gpio_init(void)
  131. {
  132. bcm63xx_gpio_out_low_reg_init();
  133. gpio_out_low = bcm_gpio_readl(gpio_out_low_reg);
  134. if (!BCMCPU_IS_6345())
  135. gpio_out_high = bcm_gpio_readl(GPIO_DATA_HI_REG);
  136. bcm63xx_gpio_chip.ngpio = bcm63xx_gpio_count();
  137. pr_info("registering %d GPIOs\n", bcm63xx_gpio_chip.ngpio);
  138. return gpiochip_add(&bcm63xx_gpio_chip);
  139. }