spear_gpio.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2012 Stefan Roese <sr@denx.de>
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. /*
  23. * Driver for SPEAr600 GPIO controller
  24. */
  25. #include <common.h>
  26. #include <asm/arch/hardware.h>
  27. #include <asm/gpio.h>
  28. #include <asm/io.h>
  29. #include <errno.h>
  30. static int gpio_direction(unsigned gpio,
  31. enum gpio_direction direction)
  32. {
  33. struct gpio_regs *regs = (struct gpio_regs *)CONFIG_GPIO_BASE;
  34. u32 val;
  35. val = readl(&regs->gpiodir);
  36. if (direction == GPIO_DIRECTION_OUT)
  37. val |= 1 << gpio;
  38. else
  39. val &= ~(1 << gpio);
  40. writel(val, &regs->gpiodir);
  41. return 0;
  42. }
  43. int gpio_set_value(unsigned gpio, int value)
  44. {
  45. struct gpio_regs *regs = (struct gpio_regs *)CONFIG_GPIO_BASE;
  46. writel(1 << gpio, &regs->gpiodata[DATA_REG_ADDR(gpio)]);
  47. return 0;
  48. }
  49. int gpio_get_value(unsigned gpio)
  50. {
  51. struct gpio_regs *regs = (struct gpio_regs *)CONFIG_GPIO_BASE;
  52. u32 val;
  53. val = readl(&regs->gpiodata[DATA_REG_ADDR(gpio)]);
  54. return !!val;
  55. }
  56. int gpio_request(unsigned gpio, const char *label)
  57. {
  58. if (gpio >= SPEAR_GPIO_COUNT)
  59. return -EINVAL;
  60. return 0;
  61. }
  62. int gpio_free(unsigned gpio)
  63. {
  64. return 0;
  65. }
  66. void gpio_toggle_value(unsigned gpio)
  67. {
  68. gpio_set_value(gpio, !gpio_get_value(gpio));
  69. }
  70. int gpio_direction_input(unsigned gpio)
  71. {
  72. return gpio_direction(gpio, GPIO_DIRECTION_IN);
  73. }
  74. int gpio_direction_output(unsigned gpio, int value)
  75. {
  76. int ret = gpio_direction(gpio, GPIO_DIRECTION_OUT);
  77. if (ret < 0)
  78. return ret;
  79. gpio_set_value(gpio, value);
  80. return 0;
  81. }