spear_gpio.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (C) 2012 Stefan Roese <sr@denx.de>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. /*
  7. * Driver for SPEAr600 GPIO controller
  8. */
  9. #include <common.h>
  10. #include <asm/arch/hardware.h>
  11. #include <asm/gpio.h>
  12. #include <asm/io.h>
  13. #include <errno.h>
  14. static int gpio_direction(unsigned gpio,
  15. enum gpio_direction direction)
  16. {
  17. struct gpio_regs *regs = (struct gpio_regs *)CONFIG_GPIO_BASE;
  18. u32 val;
  19. val = readl(&regs->gpiodir);
  20. if (direction == GPIO_DIRECTION_OUT)
  21. val |= 1 << gpio;
  22. else
  23. val &= ~(1 << gpio);
  24. writel(val, &regs->gpiodir);
  25. return 0;
  26. }
  27. int gpio_set_value(unsigned gpio, int value)
  28. {
  29. struct gpio_regs *regs = (struct gpio_regs *)CONFIG_GPIO_BASE;
  30. writel(1 << gpio, &regs->gpiodata[DATA_REG_ADDR(gpio)]);
  31. return 0;
  32. }
  33. int gpio_get_value(unsigned gpio)
  34. {
  35. struct gpio_regs *regs = (struct gpio_regs *)CONFIG_GPIO_BASE;
  36. u32 val;
  37. val = readl(&regs->gpiodata[DATA_REG_ADDR(gpio)]);
  38. return !!val;
  39. }
  40. int gpio_request(unsigned gpio, const char *label)
  41. {
  42. if (gpio >= SPEAR_GPIO_COUNT)
  43. return -EINVAL;
  44. return 0;
  45. }
  46. int gpio_free(unsigned gpio)
  47. {
  48. return 0;
  49. }
  50. void gpio_toggle_value(unsigned gpio)
  51. {
  52. gpio_set_value(gpio, !gpio_get_value(gpio));
  53. }
  54. int gpio_direction_input(unsigned gpio)
  55. {
  56. return gpio_direction(gpio, GPIO_DIRECTION_IN);
  57. }
  58. int gpio_direction_output(unsigned gpio, int value)
  59. {
  60. int ret = gpio_direction(gpio, GPIO_DIRECTION_OUT);
  61. if (ret < 0)
  62. return ret;
  63. gpio_set_value(gpio, value);
  64. return 0;
  65. }