spear_gpio.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <malloc.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. if (value)
  31. writel(1 << gpio, &regs->gpiodata[DATA_REG_ADDR(gpio)]);
  32. else
  33. writel(0, &regs->gpiodata[DATA_REG_ADDR(gpio)]);
  34. return 0;
  35. }
  36. int gpio_get_value(unsigned gpio)
  37. {
  38. struct gpio_regs *regs = (struct gpio_regs *)CONFIG_GPIO_BASE;
  39. u32 val;
  40. val = readl(&regs->gpiodata[DATA_REG_ADDR(gpio)]);
  41. return !!val;
  42. }
  43. int gpio_request(unsigned gpio, const char *label)
  44. {
  45. if (gpio >= SPEAR_GPIO_COUNT)
  46. return -EINVAL;
  47. return 0;
  48. }
  49. int gpio_free(unsigned gpio)
  50. {
  51. return 0;
  52. }
  53. void gpio_toggle_value(unsigned gpio)
  54. {
  55. gpio_set_value(gpio, !gpio_get_value(gpio));
  56. }
  57. int gpio_direction_input(unsigned gpio)
  58. {
  59. return gpio_direction(gpio, GPIO_DIRECTION_IN);
  60. }
  61. int gpio_direction_output(unsigned gpio, int value)
  62. {
  63. int ret = gpio_direction(gpio, GPIO_DIRECTION_OUT);
  64. if (ret < 0)
  65. return ret;
  66. gpio_set_value(gpio, value);
  67. return 0;
  68. }