gpio.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (C) 2007 Felix Fietkau <nbd@openwrt.org>
  3. * Copyright (C) 2007 Eugene Konev <ejka@openwrt.org>
  4. * Copyright (C) 2009 Florian Fainelli <florian@openwrt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <linux/module.h>
  21. #include <linux/gpio.h>
  22. #include <asm/mach-ar7/gpio.h>
  23. struct ar7_gpio_chip {
  24. void __iomem *regs;
  25. struct gpio_chip chip;
  26. };
  27. static int ar7_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
  28. {
  29. struct ar7_gpio_chip *gpch =
  30. container_of(chip, struct ar7_gpio_chip, chip);
  31. void __iomem *gpio_in = gpch->regs + AR7_GPIO_INPUT;
  32. return readl(gpio_in) & (1 << gpio);
  33. }
  34. static void ar7_gpio_set_value(struct gpio_chip *chip,
  35. unsigned gpio, int value)
  36. {
  37. struct ar7_gpio_chip *gpch =
  38. container_of(chip, struct ar7_gpio_chip, chip);
  39. void __iomem *gpio_out = gpch->regs + AR7_GPIO_OUTPUT;
  40. unsigned tmp;
  41. tmp = readl(gpio_out) & ~(1 << gpio);
  42. if (value)
  43. tmp |= 1 << gpio;
  44. writel(tmp, gpio_out);
  45. }
  46. static int ar7_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  47. {
  48. struct ar7_gpio_chip *gpch =
  49. container_of(chip, struct ar7_gpio_chip, chip);
  50. void __iomem *gpio_dir = gpch->regs + AR7_GPIO_DIR;
  51. writel(readl(gpio_dir) | (1 << gpio), gpio_dir);
  52. return 0;
  53. }
  54. static int ar7_gpio_direction_output(struct gpio_chip *chip,
  55. unsigned gpio, int value)
  56. {
  57. struct ar7_gpio_chip *gpch =
  58. container_of(chip, struct ar7_gpio_chip, chip);
  59. void __iomem *gpio_dir = gpch->regs + AR7_GPIO_DIR;
  60. ar7_gpio_set_value(chip, gpio, value);
  61. writel(readl(gpio_dir) & ~(1 << gpio), gpio_dir);
  62. return 0;
  63. }
  64. static struct ar7_gpio_chip ar7_gpio_chip = {
  65. .chip = {
  66. .label = "ar7-gpio",
  67. .direction_input = ar7_gpio_direction_input,
  68. .direction_output = ar7_gpio_direction_output,
  69. .set = ar7_gpio_set_value,
  70. .get = ar7_gpio_get_value,
  71. .base = 0,
  72. .ngpio = AR7_GPIO_MAX,
  73. }
  74. };
  75. int ar7_gpio_enable(unsigned gpio)
  76. {
  77. void __iomem *gpio_en = ar7_gpio_chip.regs + AR7_GPIO_ENABLE;
  78. writel(readl(gpio_en) | (1 << gpio), gpio_en);
  79. return 0;
  80. }
  81. EXPORT_SYMBOL(ar7_gpio_enable);
  82. int ar7_gpio_disable(unsigned gpio)
  83. {
  84. void __iomem *gpio_en = ar7_gpio_chip.regs + AR7_GPIO_ENABLE;
  85. writel(readl(gpio_en) & ~(1 << gpio), gpio_en);
  86. return 0;
  87. }
  88. EXPORT_SYMBOL(ar7_gpio_disable);
  89. static int __init ar7_gpio_init(void)
  90. {
  91. int ret;
  92. ar7_gpio_chip.regs = ioremap_nocache(AR7_REGS_GPIO,
  93. AR7_REGS_GPIO + 0x10);
  94. if (!ar7_gpio_chip.regs) {
  95. printk(KERN_ERR "ar7-gpio: failed to ioremap regs\n");
  96. return -ENOMEM;
  97. }
  98. ret = gpiochip_add(&ar7_gpio_chip.chip);
  99. if (ret) {
  100. printk(KERN_ERR "ar7-gpio: failed to add gpiochip\n");
  101. return ret;
  102. }
  103. printk(KERN_INFO "ar7-gpio: registered %d GPIOs\n",
  104. ar7_gpio_chip.chip.ngpio);
  105. return ret;
  106. }
  107. arch_initcall(ar7_gpio_init);