iomux-imx31.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright 2004-2006 Freescale Semiconductor, Inc. All Rights Reserved.
  3. * Copyright (C) 2008 by Sascha Hauer <kernel@pengutronix.de>
  4. * Copyright (C) 2009 by Valentin Longchamp <valentin.longchamp@epfl.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. * MA 02110-1301, USA.
  19. */
  20. #include <linux/gpio.h>
  21. #include <linux/module.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/io.h>
  24. #include <linux/kernel.h>
  25. #include <mach/hardware.h>
  26. #include <mach/iomux-mx3.h>
  27. /*
  28. * IOMUX register (base) addresses
  29. */
  30. #define IOMUX_BASE MX31_IO_ADDRESS(MX31_IOMUXC_BASE_ADDR)
  31. #define IOMUXINT_OBS1 (IOMUX_BASE + 0x000)
  32. #define IOMUXINT_OBS2 (IOMUX_BASE + 0x004)
  33. #define IOMUXGPR (IOMUX_BASE + 0x008)
  34. #define IOMUXSW_MUX_CTL (IOMUX_BASE + 0x00C)
  35. #define IOMUXSW_PAD_CTL (IOMUX_BASE + 0x154)
  36. static DEFINE_SPINLOCK(gpio_mux_lock);
  37. #define IOMUX_REG_MASK (IOMUX_PADNUM_MASK & ~0x3)
  38. unsigned long mxc_pin_alloc_map[NB_PORTS * 32 / BITS_PER_LONG];
  39. /*
  40. * set the mode for a IOMUX pin.
  41. */
  42. int mxc_iomux_mode(unsigned int pin_mode)
  43. {
  44. u32 field, l, mode, ret = 0;
  45. void __iomem *reg;
  46. reg = IOMUXSW_MUX_CTL + (pin_mode & IOMUX_REG_MASK);
  47. field = pin_mode & 0x3;
  48. mode = (pin_mode & IOMUX_MODE_MASK) >> IOMUX_MODE_SHIFT;
  49. spin_lock(&gpio_mux_lock);
  50. l = __raw_readl(reg);
  51. l &= ~(0xff << (field * 8));
  52. l |= mode << (field * 8);
  53. __raw_writel(l, reg);
  54. spin_unlock(&gpio_mux_lock);
  55. return ret;
  56. }
  57. EXPORT_SYMBOL(mxc_iomux_mode);
  58. /*
  59. * This function configures the pad value for a IOMUX pin.
  60. */
  61. void mxc_iomux_set_pad(enum iomux_pins pin, u32 config)
  62. {
  63. u32 field, l;
  64. void __iomem *reg;
  65. pin &= IOMUX_PADNUM_MASK;
  66. reg = IOMUXSW_PAD_CTL + (pin + 2) / 3 * 4;
  67. field = (pin + 2) % 3;
  68. pr_debug("%s: reg offset = 0x%x, field = %d\n",
  69. __func__, (pin + 2) / 3, field);
  70. spin_lock(&gpio_mux_lock);
  71. l = __raw_readl(reg);
  72. l &= ~(0x1ff << (field * 10));
  73. l |= config << (field * 10);
  74. __raw_writel(l, reg);
  75. spin_unlock(&gpio_mux_lock);
  76. }
  77. EXPORT_SYMBOL(mxc_iomux_set_pad);
  78. /*
  79. * allocs a single pin:
  80. * - reserves the pin so that it is not claimed by another driver
  81. * - setups the iomux according to the configuration
  82. */
  83. int mxc_iomux_alloc_pin(unsigned int pin, const char *label)
  84. {
  85. unsigned pad = pin & IOMUX_PADNUM_MASK;
  86. if (pad >= (PIN_MAX + 1)) {
  87. printk(KERN_ERR "mxc_iomux: Attempt to request nonexistant pin %u for \"%s\"\n",
  88. pad, label ? label : "?");
  89. return -EINVAL;
  90. }
  91. if (test_and_set_bit(pad, mxc_pin_alloc_map)) {
  92. printk(KERN_ERR "mxc_iomux: pin %u already used. Allocation for \"%s\" failed\n",
  93. pad, label ? label : "?");
  94. return -EBUSY;
  95. }
  96. mxc_iomux_mode(pin);
  97. return 0;
  98. }
  99. EXPORT_SYMBOL(mxc_iomux_alloc_pin);
  100. int mxc_iomux_setup_multiple_pins(const unsigned int *pin_list, unsigned count,
  101. const char *label)
  102. {
  103. const unsigned int *p = pin_list;
  104. int i;
  105. int ret = -EINVAL;
  106. for (i = 0; i < count; i++) {
  107. ret = mxc_iomux_alloc_pin(*p, label);
  108. if (ret)
  109. goto setup_error;
  110. p++;
  111. }
  112. return 0;
  113. setup_error:
  114. mxc_iomux_release_multiple_pins(pin_list, i);
  115. return ret;
  116. }
  117. EXPORT_SYMBOL(mxc_iomux_setup_multiple_pins);
  118. void mxc_iomux_release_pin(unsigned int pin)
  119. {
  120. unsigned pad = pin & IOMUX_PADNUM_MASK;
  121. if (pad < (PIN_MAX + 1))
  122. clear_bit(pad, mxc_pin_alloc_map);
  123. }
  124. EXPORT_SYMBOL(mxc_iomux_release_pin);
  125. void mxc_iomux_release_multiple_pins(const unsigned int *pin_list, int count)
  126. {
  127. const unsigned int *p = pin_list;
  128. int i;
  129. for (i = 0; i < count; i++) {
  130. mxc_iomux_release_pin(*p);
  131. p++;
  132. }
  133. }
  134. EXPORT_SYMBOL(mxc_iomux_release_multiple_pins);
  135. /*
  136. * This function enables/disables the general purpose function for a particular
  137. * signal.
  138. */
  139. void mxc_iomux_set_gpr(enum iomux_gp_func gp, bool en)
  140. {
  141. u32 l;
  142. spin_lock(&gpio_mux_lock);
  143. l = __raw_readl(IOMUXGPR);
  144. if (en)
  145. l |= gp;
  146. else
  147. l &= ~gp;
  148. __raw_writel(l, IOMUXGPR);
  149. spin_unlock(&gpio_mux_lock);
  150. }
  151. EXPORT_SYMBOL(mxc_iomux_set_gpr);