gpio.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * OpenRISC gpio driver
  3. *
  4. * Copyright (C) 2011 Stefan Kristiansson <stefan.kristiansson@saunalahti.fi>
  5. *
  6. * based on nios2 gpio driver
  7. * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
  8. *
  9. * when CONFIG_SYS_GPIO_BASE is not defined, board may provide
  10. * its own driver.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #ifdef CONFIG_SYS_GPIO_BASE
  28. #include <asm/io.h>
  29. static inline int gpio_request(unsigned gpio, const char *label)
  30. {
  31. return 0;
  32. }
  33. static inline int gpio_free(unsigned gpio)
  34. {
  35. return 0;
  36. }
  37. static inline int gpio_get_value(unsigned gpio)
  38. {
  39. return (readb(CONFIG_SYS_GPIO_BASE + gpio/8) >> gpio%8) & 0x1;
  40. }
  41. static inline void gpio_set_value(unsigned gpio, int value)
  42. {
  43. u8 tmp = readb(CONFIG_SYS_GPIO_BASE + gpio/8);
  44. if (value)
  45. tmp |= (1 << gpio%8);
  46. else
  47. tmp &= ~(1 << gpio%8);
  48. writeb(tmp, CONFIG_SYS_GPIO_BASE + gpio/8);
  49. }
  50. static inline int gpio_direction_input(unsigned gpio)
  51. {
  52. gpio_set_value(gpio + CONFIG_SYS_GPIO_WIDTH, 0);
  53. return 0;
  54. }
  55. static inline int gpio_direction_output(unsigned gpio, int value)
  56. {
  57. gpio_set_value(gpio + CONFIG_SYS_GPIO_WIDTH, 1);
  58. gpio_set_value(gpio, value);
  59. return 0;
  60. }
  61. static inline int gpio_is_valid(int number)
  62. {
  63. return ((unsigned)number) < CONFIG_SYS_GPIO_WIDTH;
  64. }
  65. #else
  66. extern int gpio_request(unsigned gpio, const char *label);
  67. extern int gpio_free(unsigned gpio);
  68. extern int gpio_direction_input(unsigned gpio);
  69. extern int gpio_direction_output(unsigned gpio, int value);
  70. extern int gpio_get_value(unsigned gpio);
  71. extern void gpio_set_value(unsigned gpio, int value);
  72. extern int gpio_is_valid(int number);
  73. #endif /* CONFIG_SYS_GPIO_BASE */