gpio.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/driver.h>
  14. #include <bcm63xx_cpu.h>
  15. #include <bcm63xx_gpio.h>
  16. #include <bcm63xx_io.h>
  17. #include <bcm63xx_regs.h>
  18. static u32 gpio_out_low_reg;
  19. static void bcm63xx_gpio_out_low_reg_init(void)
  20. {
  21. switch (bcm63xx_get_cpu_id()) {
  22. case BCM6345_CPU_ID:
  23. gpio_out_low_reg = GPIO_DATA_LO_REG_6345;
  24. break;
  25. default:
  26. gpio_out_low_reg = GPIO_DATA_LO_REG;
  27. break;
  28. }
  29. }
  30. static DEFINE_SPINLOCK(bcm63xx_gpio_lock);
  31. static u32 gpio_out_low, gpio_out_high;
  32. static void bcm63xx_gpio_set(struct gpio_chip *chip,
  33. unsigned gpio, int val)
  34. {
  35. u32 reg;
  36. u32 mask;
  37. u32 *v;
  38. unsigned long flags;
  39. if (gpio >= chip->ngpio)
  40. BUG();
  41. if (gpio < 32) {
  42. reg = gpio_out_low_reg;
  43. mask = 1 << gpio;
  44. v = &gpio_out_low;
  45. } else {
  46. reg = GPIO_DATA_HI_REG;
  47. mask = 1 << (gpio - 32);
  48. v = &gpio_out_high;
  49. }
  50. spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
  51. if (val)
  52. *v |= mask;
  53. else
  54. *v &= ~mask;
  55. bcm_gpio_writel(*v, reg);
  56. spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
  57. }
  58. static int bcm63xx_gpio_get(struct gpio_chip *chip, unsigned gpio)
  59. {
  60. u32 reg;
  61. u32 mask;
  62. if (gpio >= chip->ngpio)
  63. BUG();
  64. if (gpio < 32) {
  65. reg = gpio_out_low_reg;
  66. mask = 1 << gpio;
  67. } else {
  68. reg = GPIO_DATA_HI_REG;
  69. mask = 1 << (gpio - 32);
  70. }
  71. return !!(bcm_gpio_readl(reg) & mask);
  72. }
  73. static int bcm63xx_gpio_set_direction(struct gpio_chip *chip,
  74. unsigned gpio, int dir)
  75. {
  76. u32 reg;
  77. u32 mask;
  78. u32 tmp;
  79. unsigned long flags;
  80. if (gpio >= chip->ngpio)
  81. BUG();
  82. if (gpio < 32) {
  83. reg = GPIO_CTL_LO_REG;
  84. mask = 1 << gpio;
  85. } else {
  86. reg = GPIO_CTL_HI_REG;
  87. mask = 1 << (gpio - 32);
  88. }
  89. spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
  90. tmp = bcm_gpio_readl(reg);
  91. if (dir == BCM63XX_GPIO_DIR_IN)
  92. tmp &= ~mask;
  93. else
  94. tmp |= mask;
  95. bcm_gpio_writel(tmp, reg);
  96. spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
  97. return 0;
  98. }
  99. static int bcm63xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  100. {
  101. return bcm63xx_gpio_set_direction(chip, gpio, BCM63XX_GPIO_DIR_IN);
  102. }
  103. static int bcm63xx_gpio_direction_output(struct gpio_chip *chip,
  104. unsigned gpio, int value)
  105. {
  106. bcm63xx_gpio_set(chip, gpio, value);
  107. return bcm63xx_gpio_set_direction(chip, gpio, BCM63XX_GPIO_DIR_OUT);
  108. }
  109. static struct gpio_chip bcm63xx_gpio_chip = {
  110. .label = "bcm63xx-gpio",
  111. .direction_input = bcm63xx_gpio_direction_input,
  112. .direction_output = bcm63xx_gpio_direction_output,
  113. .get = bcm63xx_gpio_get,
  114. .set = bcm63xx_gpio_set,
  115. .base = 0,
  116. };
  117. int __init bcm63xx_gpio_init(void)
  118. {
  119. bcm63xx_gpio_out_low_reg_init();
  120. gpio_out_low = bcm_gpio_readl(gpio_out_low_reg);
  121. if (!BCMCPU_IS_6345())
  122. gpio_out_high = bcm_gpio_readl(GPIO_DATA_HI_REG);
  123. bcm63xx_gpio_chip.ngpio = bcm63xx_gpio_count();
  124. pr_info("registering %d GPIOs\n", bcm63xx_gpio_chip.ngpio);
  125. return gpiochip_add_data(&bcm63xx_gpio_chip, NULL);
  126. }