gpio.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. * SPDX-License-Identifier: GPL-2.0+
  13. */
  14. #ifdef CONFIG_SYS_GPIO_BASE
  15. #include <asm/io.h>
  16. static inline int gpio_request(unsigned gpio, const char *label)
  17. {
  18. return 0;
  19. }
  20. static inline int gpio_free(unsigned gpio)
  21. {
  22. return 0;
  23. }
  24. static inline int gpio_get_value(unsigned gpio)
  25. {
  26. return (readb(CONFIG_SYS_GPIO_BASE + gpio/8) >> gpio%8) & 0x1;
  27. }
  28. static inline void gpio_set_value(unsigned gpio, int value)
  29. {
  30. u8 tmp = readb(CONFIG_SYS_GPIO_BASE + gpio/8);
  31. if (value)
  32. tmp |= (1 << gpio%8);
  33. else
  34. tmp &= ~(1 << gpio%8);
  35. writeb(tmp, CONFIG_SYS_GPIO_BASE + gpio/8);
  36. }
  37. static inline int gpio_direction_input(unsigned gpio)
  38. {
  39. gpio_set_value(gpio + CONFIG_SYS_GPIO_WIDTH, 0);
  40. return 0;
  41. }
  42. static inline int gpio_direction_output(unsigned gpio, int value)
  43. {
  44. gpio_set_value(gpio + CONFIG_SYS_GPIO_WIDTH, 1);
  45. gpio_set_value(gpio, value);
  46. return 0;
  47. }
  48. static inline int gpio_is_valid(int number)
  49. {
  50. return ((unsigned)number) < CONFIG_SYS_GPIO_WIDTH;
  51. }
  52. #else
  53. extern int gpio_request(unsigned gpio, const char *label);
  54. extern int gpio_free(unsigned gpio);
  55. extern int gpio_direction_input(unsigned gpio);
  56. extern int gpio_direction_output(unsigned gpio, int value);
  57. extern int gpio_get_value(unsigned gpio);
  58. extern void gpio_set_value(unsigned gpio, int value);
  59. extern int gpio_is_valid(int number);
  60. #endif /* CONFIG_SYS_GPIO_BASE */