spear_gpio.c 1.5 KB

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