bcm2835_gpio.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (C) 2012 Vikram Narayananan
  3. * <vikram186@gmail.com>
  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. #include <common.h>
  19. #include <asm/gpio.h>
  20. #include <asm/io.h>
  21. inline int gpio_is_valid(unsigned gpio)
  22. {
  23. return (gpio < BCM2835_GPIO_COUNT);
  24. }
  25. int gpio_request(unsigned gpio, const char *label)
  26. {
  27. return !gpio_is_valid(gpio);
  28. }
  29. int gpio_free(unsigned gpio)
  30. {
  31. return 0;
  32. }
  33. int gpio_direction_input(unsigned gpio)
  34. {
  35. struct bcm2835_gpio_regs *reg =
  36. (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
  37. unsigned val;
  38. val = readl(&reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
  39. val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
  40. val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
  41. writel(val, &reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
  42. return 0;
  43. }
  44. int gpio_direction_output(unsigned gpio, int value)
  45. {
  46. struct bcm2835_gpio_regs *reg =
  47. (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
  48. unsigned val;
  49. gpio_set_value(gpio, value);
  50. val = readl(&reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
  51. val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
  52. val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
  53. writel(val, &reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
  54. return 0;
  55. }
  56. int gpio_get_value(unsigned gpio)
  57. {
  58. struct bcm2835_gpio_regs *reg =
  59. (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
  60. unsigned val;
  61. val = readl(&reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
  62. return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
  63. }
  64. int gpio_set_value(unsigned gpio, int value)
  65. {
  66. struct bcm2835_gpio_regs *reg =
  67. (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
  68. u32 *output_reg = value ? reg->gpset : reg->gpclr;
  69. writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio),
  70. &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]);
  71. return 0;
  72. }