gpio.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 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. static DEFINE_SPINLOCK(bcm63xx_gpio_lock);
  19. static u32 gpio_out_low, gpio_out_high;
  20. static void bcm63xx_gpio_set(struct gpio_chip *chip,
  21. unsigned gpio, int val)
  22. {
  23. u32 reg;
  24. u32 mask;
  25. u32 *v;
  26. unsigned long flags;
  27. if (gpio >= chip->ngpio)
  28. BUG();
  29. if (gpio < 32) {
  30. reg = GPIO_DATA_LO_REG;
  31. mask = 1 << gpio;
  32. v = &gpio_out_low;
  33. } else {
  34. reg = GPIO_DATA_HI_REG;
  35. mask = 1 << (gpio - 32);
  36. v = &gpio_out_high;
  37. }
  38. spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
  39. if (val)
  40. *v |= mask;
  41. else
  42. *v &= ~mask;
  43. bcm_gpio_writel(*v, reg);
  44. spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
  45. }
  46. static int bcm63xx_gpio_get(struct gpio_chip *chip, unsigned gpio)
  47. {
  48. u32 reg;
  49. u32 mask;
  50. if (gpio >= chip->ngpio)
  51. BUG();
  52. if (gpio < 32) {
  53. reg = GPIO_DATA_LO_REG;
  54. mask = 1 << gpio;
  55. } else {
  56. reg = GPIO_DATA_HI_REG;
  57. mask = 1 << (gpio - 32);
  58. }
  59. return !!(bcm_gpio_readl(reg) & mask);
  60. }
  61. static int bcm63xx_gpio_set_direction(struct gpio_chip *chip,
  62. unsigned gpio, int dir)
  63. {
  64. u32 reg;
  65. u32 mask;
  66. u32 tmp;
  67. unsigned long flags;
  68. if (gpio >= chip->ngpio)
  69. BUG();
  70. if (gpio < 32) {
  71. reg = GPIO_CTL_LO_REG;
  72. mask = 1 << gpio;
  73. } else {
  74. reg = GPIO_CTL_HI_REG;
  75. mask = 1 << (gpio - 32);
  76. }
  77. spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
  78. tmp = bcm_gpio_readl(reg);
  79. if (dir == GPIO_DIR_IN)
  80. tmp &= ~mask;
  81. else
  82. tmp |= mask;
  83. bcm_gpio_writel(tmp, reg);
  84. spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
  85. return 0;
  86. }
  87. static int bcm63xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  88. {
  89. return bcm63xx_gpio_set_direction(chip, gpio, GPIO_DIR_IN);
  90. }
  91. static int bcm63xx_gpio_direction_output(struct gpio_chip *chip,
  92. unsigned gpio, int value)
  93. {
  94. bcm63xx_gpio_set(chip, gpio, value);
  95. return bcm63xx_gpio_set_direction(chip, gpio, GPIO_DIR_OUT);
  96. }
  97. static struct gpio_chip bcm63xx_gpio_chip = {
  98. .label = "bcm63xx-gpio",
  99. .direction_input = bcm63xx_gpio_direction_input,
  100. .direction_output = bcm63xx_gpio_direction_output,
  101. .get = bcm63xx_gpio_get,
  102. .set = bcm63xx_gpio_set,
  103. .base = 0,
  104. };
  105. int __init bcm63xx_gpio_init(void)
  106. {
  107. bcm63xx_gpio_chip.ngpio = bcm63xx_gpio_count();
  108. pr_info("registering %d GPIOs\n", bcm63xx_gpio_chip.ngpio);
  109. return gpiochip_add(&bcm63xx_gpio_chip);
  110. }
  111. arch_initcall(bcm63xx_gpio_init);