gpio.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <[email protected]>
  7. * Copyright (C) 2008-2011 Florian Fainelli <[email protected]>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/init.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. BUG_ON(gpio >= chip->ngpio);
  40. if (gpio < 32) {
  41. reg = gpio_out_low_reg;
  42. mask = 1 << gpio;
  43. v = &gpio_out_low;
  44. } else {
  45. reg = GPIO_DATA_HI_REG;
  46. mask = 1 << (gpio - 32);
  47. v = &gpio_out_high;
  48. }
  49. spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
  50. if (val)
  51. *v |= mask;
  52. else
  53. *v &= ~mask;
  54. bcm_gpio_writel(*v, reg);
  55. spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
  56. }
  57. static int bcm63xx_gpio_get(struct gpio_chip *chip, unsigned gpio)
  58. {
  59. u32 reg;
  60. u32 mask;
  61. BUG_ON(gpio >= chip->ngpio);
  62. if (gpio < 32) {
  63. reg = gpio_out_low_reg;
  64. mask = 1 << gpio;
  65. } else {
  66. reg = GPIO_DATA_HI_REG;
  67. mask = 1 << (gpio - 32);
  68. }
  69. return !!(bcm_gpio_readl(reg) & mask);
  70. }
  71. static int bcm63xx_gpio_set_direction(struct gpio_chip *chip,
  72. unsigned gpio, int dir)
  73. {
  74. u32 reg;
  75. u32 mask;
  76. u32 tmp;
  77. unsigned long flags;
  78. BUG_ON(gpio >= chip->ngpio);
  79. if (gpio < 32) {
  80. reg = GPIO_CTL_LO_REG;
  81. mask = 1 << gpio;
  82. } else {
  83. reg = GPIO_CTL_HI_REG;
  84. mask = 1 << (gpio - 32);
  85. }
  86. spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
  87. tmp = bcm_gpio_readl(reg);
  88. if (dir == BCM63XX_GPIO_DIR_IN)
  89. tmp &= ~mask;
  90. else
  91. tmp |= mask;
  92. bcm_gpio_writel(tmp, reg);
  93. spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
  94. return 0;
  95. }
  96. static int bcm63xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  97. {
  98. return bcm63xx_gpio_set_direction(chip, gpio, BCM63XX_GPIO_DIR_IN);
  99. }
  100. static int bcm63xx_gpio_direction_output(struct gpio_chip *chip,
  101. unsigned gpio, int value)
  102. {
  103. bcm63xx_gpio_set(chip, gpio, value);
  104. return bcm63xx_gpio_set_direction(chip, gpio, BCM63XX_GPIO_DIR_OUT);
  105. }
  106. static struct gpio_chip bcm63xx_gpio_chip = {
  107. .label = "bcm63xx-gpio",
  108. .direction_input = bcm63xx_gpio_direction_input,
  109. .direction_output = bcm63xx_gpio_direction_output,
  110. .get = bcm63xx_gpio_get,
  111. .set = bcm63xx_gpio_set,
  112. .base = 0,
  113. };
  114. int __init bcm63xx_gpio_init(void)
  115. {
  116. bcm63xx_gpio_out_low_reg_init();
  117. gpio_out_low = bcm_gpio_readl(gpio_out_low_reg);
  118. if (!BCMCPU_IS_6345())
  119. gpio_out_high = bcm_gpio_readl(GPIO_DATA_HI_REG);
  120. bcm63xx_gpio_chip.ngpio = bcm63xx_gpio_count();
  121. pr_info("registering %d GPIOs\n", bcm63xx_gpio_chip.ngpio);
  122. return gpiochip_add_data(&bcm63xx_gpio_chip, NULL);
  123. }