preliminaryDataProcessing.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Atmel PIO2 Port Multiplexer support
  3. *
  4. * Copyright (C) 2004-2006 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/export.h>
  13. #include <linux/fs.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/irq.h>
  16. #include <asm/gpio.h>
  17. #include <asm/io.h>
  18. #include <mach/portmux.h>
  19. #include "pio.h"
  20. #define MAX_NR_PIO_DEVICES 8
  21. struct pio_device {
  22. struct gpio_chip chip;
  23. void __iomem *regs;
  24. const struct platform_device *pdev;
  25. struct clk *clk;
  26. u32 pinmux_mask;
  27. char name[8];
  28. };
  29. static struct pio_device pio_dev[MAX_NR_PIO_DEVICES];
  30. static struct pio_device *gpio_to_pio(unsigned int gpio)
  31. {
  32. struct pio_device *pio;
  33. unsigned int index;
  34. index = gpio >> 5;
  35. if (index >= MAX_NR_PIO_DEVICES)
  36. return NULL;
  37. pio = &pio_dev[index];
  38. if (!pio->regs)
  39. return NULL;
  40. return pio;
  41. }
  42. /* Pin multiplexing API */
  43. static DEFINE_SPINLOCK(pio_lock);
  44. void __init at32_select_periph(unsigned int port, u32 pin_mask,
  45. unsigned int periph, unsigned long flags)
  46. {
  47. struct pio_device *pio;
  48. /* assign and verify pio */
  49. pio = gpio_to_pio(port);
  50. if (unlikely(!pio)) {
  51. printk(KERN_WARNING "pio: invalid port %u\n", port);
  52. goto fail;
  53. }
  54. /* Test if any of the requested pins is already muxed */
  55. spin_lock(&pio_lock);
  56. if (unlikely(pio->pinmux_mask & pin_mask)) {
  57. printk(KERN_WARNING "%s: pin(s) busy (requested 0x%x, busy 0x%x)\n",
  58. pio->name, pin_mask, pio->pinmux_mask & pin_mask);
  59. spin_unlock(&pio_lock);
  60. goto fail;
  61. }
  62. pio->pinmux_mask |= pin_mask;
  63. /* enable pull ups */
  64. pio_writel(pio, PUER, pin_mask);
  65. /* select either peripheral A or B */
  66. if (periph)
  67. pio_writel(pio, BSR, pin_mask);
  68. else
  69. pio_writel(pio, ASR, pin_mask);
  70. /* enable peripheral control */
  71. pio_writel(pio, PDR, pin_mask);
  72. /* Disable pull ups if not requested. */
  73. if (!(flags & AT32_GPIOF_PULLUP))
  74. pio_writel(pio, PUDR, pin_mask);
  75. spin_unlock(&pio_lock);
  76. return;
  77. fail:
  78. dump_stack();
  79. }
  80. void __init at32_select_gpio(unsigned int pin, unsigned long flags)
  81. {
  82. struct pio_device *pio;
  83. unsigned int pin_index = pin & 0x1f;
  84. u32 mask = 1 << pin_index;
  85. pio = gpio_to_pio(pin);
  86. if (unlikely(!pio)) {
  87. printk("pio: invalid pin %u\n", pin);
  88. goto fail;
  89. }
  90. if (unlikely(test_and_set_bit(pin_index, &pio->pinmux_mask))) {
  91. printk("%s: pin %u is busy\n", pio->name, pin_index);
  92. goto fail;
  93. }
  94. if (flags & AT32_GPIOF_OUTPUT) {
  95. if (flags & AT32_GPIOF_HIGH)
  96. pio_writel(pio, SODR, mask);
  97. else
  98. pio_writel(pio, CODR, mask);
  99. if (flags & AT32_GPIOF_MULTIDRV)
  100. pio_writel(pio, MDER, mask);
  101. else
  102. pio_writel(pio, MDDR, mask);
  103. pio_writel(pio, PUDR, mask);
  104. pio_writel(pio, OER, mask);
  105. } else {
  106. if (flags & AT32_GPIOF_PULLUP)
  107. pio_writel(pio, PUER, mask);
  108. else
  109. pio_writel(pio, PUDR, mask);
  110. if (flags & AT32_GPIOF_DEGLITCH)
  111. pio_writel(pio, IFER, mask);
  112. else
  113. pio_writel(pio, IFDR, mask);
  114. pio_writel(pio, ODR, mask);
  115. }
  116. pio_writel(pio, PER, mask);
  117. return;
  118. fail:
  119. dump_stack();
  120. }
  121. /*
  122. * Undo a previous pin reservation. Will not affect the hardware
  123. * configuration.
  124. */
  125. void at32_deselect_pin(unsigned int pin)
  126. {
  127. struct pio_device *pio;
  128. unsigned int pin_index = pin & 0x1f;
  129. pio = gpio_to_pio(pin);
  130. if (unlikely(!pio)) {