gpio.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C) 2007 Florian Fainelli <florian@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef __AR7_GPIO_H__
  19. #define __AR7_GPIO_H__
  20. #include <asm/mach-ar7/ar7.h>
  21. #define AR7_GPIO_MAX 32
  22. extern int gpio_request(unsigned gpio, const char *label);
  23. extern void gpio_free(unsigned gpio);
  24. /* Common GPIO layer */
  25. static inline int gpio_get_value(unsigned gpio)
  26. {
  27. void __iomem *gpio_in =
  28. (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_INPUT);
  29. return readl(gpio_in) & (1 << gpio);
  30. }
  31. static inline void gpio_set_value(unsigned gpio, int value)
  32. {
  33. void __iomem *gpio_out =
  34. (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_OUTPUT);
  35. unsigned tmp;
  36. tmp = readl(gpio_out) & ~(1 << gpio);
  37. if (value)
  38. tmp |= 1 << gpio;
  39. writel(tmp, gpio_out);
  40. }
  41. static inline int gpio_direction_input(unsigned gpio)
  42. {
  43. void __iomem *gpio_dir =
  44. (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_DIR);
  45. if (gpio >= AR7_GPIO_MAX)
  46. return -EINVAL;
  47. writel(readl(gpio_dir) | (1 << gpio), gpio_dir);
  48. return 0;
  49. }
  50. static inline int gpio_direction_output(unsigned gpio, int value)
  51. {
  52. void __iomem *gpio_dir =
  53. (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_DIR);
  54. if (gpio >= AR7_GPIO_MAX)
  55. return -EINVAL;
  56. gpio_set_value(gpio, value);
  57. writel(readl(gpio_dir) & ~(1 << gpio), gpio_dir);
  58. return 0;
  59. }
  60. static inline int gpio_to_irq(unsigned gpio)
  61. {
  62. return -EINVAL;
  63. }
  64. static inline int irq_to_gpio(unsigned irq)
  65. {
  66. return -EINVAL;
  67. }
  68. /* Board specific GPIO functions */
  69. static inline int ar7_gpio_enable(unsigned gpio)
  70. {
  71. void __iomem *gpio_en =
  72. (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_ENABLE);
  73. writel(readl(gpio_en) | (1 << gpio), gpio_en);
  74. return 0;
  75. }
  76. static inline int ar7_gpio_disable(unsigned gpio)
  77. {
  78. void __iomem *gpio_en =
  79. (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_ENABLE);
  80. writel(readl(gpio_en) & ~(1 << gpio), gpio_en);
  81. return 0;
  82. }
  83. #include <asm-generic/gpio.h>
  84. #endif