s3c2440_gpio.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (C) 2012
  3. * Gabriel Huau <contact@huau-gabriel.fr>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/arch/s3c2440.h>
  25. #include <asm/gpio.h>
  26. #include <asm/io.h>
  27. #define GPIO_INPUT 0x0
  28. #define GPIO_OUTPUT 0x1
  29. /* 0x4 means that we want DAT and not CON register */
  30. #define GPIO_PORT(x) ((((x) >> 5) & 0x3) + 0x4)
  31. #define GPIO_BIT(x) ((x) & 0x3f)
  32. /*
  33. * It's how we calculate the full port address
  34. * We have to get the number of the port + 1 (Port A is at 0x56000001 ...)
  35. * We move it at the second digit, and finally we add 0x4 because we want
  36. * to modify GPIO DAT and not CON
  37. */
  38. #define GPIO_FULLPORT(x) (S3C24X0_GPIO_BASE | ((GPIO_PORT(gpio) + 1) << 1))
  39. int gpio_set_value(unsigned gpio, int value)
  40. {
  41. unsigned l = readl(GPIO_FULLPORT(gpio));
  42. unsigned bit;
  43. unsigned port = GPIO_FULLPORT(gpio);
  44. /*
  45. * All GPIO Port have a configuration on
  46. * 2 bits excepted the first GPIO (A) which
  47. * have only 1 bit of configuration.
  48. */
  49. if (!GPIO_PORT(gpio))
  50. bit = (0x1 << GPIO_BIT(gpio));
  51. else
  52. bit = (0x3 << GPIO_BIT(gpio));
  53. if (value)
  54. l |= bit;
  55. else
  56. l &= ~bit;
  57. return writel(port, l);
  58. }
  59. int gpio_get_value(unsigned gpio)
  60. {
  61. unsigned l = readl(GPIO_FULLPORT(gpio));
  62. if (GPIO_PORT(gpio) == 0) /* PORT A */
  63. return (l >> GPIO_BIT(gpio)) & 0x1;
  64. return (l >> GPIO_BIT(gpio)) & 0x3;
  65. }
  66. int gpio_request(unsigned gpio, const char *label)
  67. {
  68. return 0;
  69. }
  70. int gpio_free(unsigned gpio)
  71. {
  72. return 0;
  73. }
  74. int gpio_direction_input(unsigned gpio)
  75. {
  76. return writel(GPIO_FULLPORT(gpio), GPIO_INPUT << GPIO_BIT(gpio));
  77. }
  78. int gpio_direction_output(unsigned gpio, int value)
  79. {
  80. writel(GPIO_FULLPORT(gpio), GPIO_OUTPUT << GPIO_BIT(gpio));
  81. return gpio_set_value(gpio, value);
  82. }