pio.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2006 Atmel Corporation
  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. #include <common.h>
  23. #include <asm/errno.h>
  24. #include <asm/io.h>
  25. #include <asm/arch/platform.h>
  26. #include "pio2.h"
  27. struct pio_state {
  28. const struct device *dev;
  29. u32 alloc_mask;
  30. };
  31. static struct pio_state pio_state[CFG_NR_PIOS];
  32. int gpio_set_func(enum device_id gpio_devid, unsigned int start,
  33. unsigned int nr_pins, enum gpio_func func)
  34. {
  35. const struct device *gpio;
  36. struct pio_state *state;
  37. u32 mask;
  38. state = &pio_state[gpio_devid - DEVICE_PIOA];
  39. gpio = get_device(gpio_devid);
  40. if (!gpio)
  41. return -EBUSY;
  42. state->dev = gpio;
  43. mask = ((1 << nr_pins) - 1) << start;
  44. if (mask & state->alloc_mask) {
  45. put_device(gpio);
  46. return -EBUSY;
  47. }
  48. state->alloc_mask |= mask;
  49. switch (func) {
  50. case GPIO_FUNC_GPIO:
  51. /* TODO */
  52. return -EINVAL;
  53. case GPIO_FUNC_A:
  54. pio2_writel(gpio, ASR, mask);
  55. pio2_writel(gpio, PDR, mask);
  56. pio2_writel(gpio, PUDR, mask);
  57. break;
  58. case GPIO_FUNC_B:
  59. pio2_writel(gpio, BSR, mask);
  60. pio2_writel(gpio, PDR, mask);
  61. pio2_writel(gpio, PUDR, mask);
  62. break;
  63. }
  64. return 0;
  65. }
  66. void gpio_free(enum device_id gpio_devid, unsigned int start,
  67. unsigned int nr_pins)
  68. {
  69. const struct device *gpio;
  70. struct pio_state *state;
  71. u32 mask;
  72. state = &pio_state[gpio_devid - DEVICE_PIOA];
  73. gpio = state->dev;
  74. mask = ((1 << nr_pins) - 1) << start;
  75. pio2_writel(gpio, ODR, mask);
  76. pio2_writel(gpio, PER, mask);
  77. state->alloc_mask &= ~mask;
  78. put_device(gpio);
  79. }