iomux.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/module.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/io.h>
  23. #include <linux/gpio.h>
  24. #include <linux/kernel.h>
  25. #include <mach/hardware.h>
  26. #include <mach/gpio.h>
  27. #include <mach/iomux-mx3.h>
  28. /*
  29. * IOMUX register (base) addresses
  30. */
  31. #define IOMUX_BASE IO_ADDRESS(IOMUXC_BASE_ADDR)
  32. #define IOMUXINT_OBS1 (IOMUX_BASE + 0x000)
  33. #define IOMUXINT_OBS2 (IOMUX_BASE + 0x004)
  34. #define IOMUXGPR (IOMUX_BASE + 0x008)
  35. #define IOMUXSW_MUX_CTL (IOMUX_BASE + 0x00C)
  36. #define IOMUXSW_PAD_CTL (IOMUX_BASE + 0x154)
  37. static DEFINE_SPINLOCK(gpio_mux_lock);
  38. #define IOMUX_REG_MASK (IOMUX_PADNUM_MASK & ~0x3)
  39. unsigned long mxc_pin_alloc_map[NB_PORTS * 32 / BITS_PER_LONG];
  40. /*
  41. * set the mode for a IOMUX pin.
  42. */
  43. int mxc_iomux_mode(unsigned int pin_mode)
  44. {
  45. u32 field, l, mode, ret = 0;
  46. void __iomem *reg;
  47. reg = IOMUXSW_MUX_CTL + (pin_mode & IOMUX_REG_MASK);
  48. field = pin_mode & 0x3;
  49. mode = (pin_mode & IOMUX_MODE_MASK) >> IOMUX_MODE_SHIFT;
  50. spin_lock(&gpio_mux_lock);
  51. l = __raw_readl(reg);
  52. l &= ~(0xff << (field * 8));
  53. l |= mode << (field * 8);
  54. __raw_writel(l, reg);
  55. spin_unlock(&gpio_mux_lock);
  56. return ret;
  57. }
  58. EXPORT_SYMBOL(mxc_iomux_mode);
  59. /*
  60. * This function configures the pad value for a IOMUX pin.
  61. */
  62. void mxc_iomux_set_pad(enum iomux_pins pin, u32 config)
  63. {
  64. u32 field, l;
  65. void __iomem *reg;
  66. pin &= IOMUX_PADNUM_MASK;
  67. reg = IOMUXSW_PAD_CTL + (pin + 2) / 3 * 4;
  68. field = (pin + 2) % 3;
  69. pr_debug("%s: reg offset = 0x%x, field = %d\n",
  70. __func__, (pin + 2) / 3, field);
  71. spin_lock(&gpio_mux_lock);
  72. l = __raw_readl(reg);
  73. l &= ~(0x1ff << (field * 10));
  74. l |= config << (field * 10);
  75. __raw_writel(l, reg);
  76. spin_unlock(&gpio_mux_lock);
  77. }
  78. EXPORT_SYMBOL(mxc_iomux_set_pad);
  79. /*
  80. * setups a single pin:
  81. * - reserves the pin so that it is not claimed by another driver
  82. * - setups the iomux according to the configuration
  83. * - if the pin is configured as a GPIO, we claim it through kernel gpiolib
  84. */
  85. int mxc_iomux_setup_pin(const unsigned int pin, const char *label)
  86. {
  87. unsigned pad = pin & IOMUX_PADNUM_MASK;
  88. unsigned gpio;
  89. if (pad >= (PIN_MAX + 1)) {
  90. printk(KERN_ERR "mxc_iomux: Attempt to request nonexistant pin %u for \"%s\"\n",
  91. pad, label ? label : "?");
  92. return -EINVAL;
  93. }
  94. if (test_and_set_bit(pad, mxc_pin_alloc_map)) {
  95. printk(KERN_ERR "mxc_iomux: pin %u already used. Allocation for \"%s\" failed\n",
  96. pad, label ? label : "?");
  97. return -EINVAL;
  98. }
  99. mxc_iomux_mode(pin);
  100. /* if we have a gpio, we can allocate it */
  101. gpio = (pin & IOMUX_GPIONUM_MASK) >> IOMUX_GPIONUM_SHIFT;
  102. if (gpio < (GPIO_PORT_MAX + 1) * 32)
  103. if (gpio_request(gpio, label))
  104. return -EINVAL;
  105. return 0;
  106. }
  107. EXPORT_SYMBOL(mxc_iomux_setup_pin);
  108. int mxc_iomux_setup_multiple_pins(unsigned int *pin_list, unsigned count,
  109. const char *label)
  110. {
  111. unsigned int *p = pin_list;
  112. int i;
  113. int ret = -EINVAL;
  114. for (i = 0; i < count; i++) {
  115. if (mxc_iomux_setup_pin(*p, label))
  116. goto setup_error;
  117. p++;
  118. }
  119. return 0;
  120. setup_error:
  121. mxc_iomux_release_multiple_pins(pin_list, i);
  122. return ret;
  123. }
  124. EXPORT_SYMBOL(mxc_iomux_setup_multiple_pins);
  125. void mxc_iomux_release_pin(const unsigned int pin)
  126. {
  127. unsigned pad = pin & IOMUX_PADNUM_MASK;
  128. unsigned gpio;
  129. if (pad < (PIN_MAX + 1))
  130. clear_bit(pad, mxc_pin_alloc_map);
  131. gpio = (pin & IOMUX_GPIONUM_MASK) >> IOMUX_GPIONUM_SHIFT;
  132. if (gpio < (GPIO_PORT_MAX + 1) * 32)
  133. gpio_free(gpio);
  134. }
  135. EXPORT_SYMBOL(mxc_iomux_release_pin);
  136. void mxc_iomux_release_multiple_pins(unsigned int *pin_list, int count)
  137. {
  138. unsigned int *p = pin_list;
  139. int i;
  140. for (i = 0; i < count; i++) {
  141. mxc_iomux_release_pin(*p);
  142. p++;
  143. }
  144. }
  145. EXPORT_SYMBOL(mxc_iomux_release_multiple_pins);
  146. /*
  147. * This function enables/disables the general purpose function for a particular
  148. * signal.
  149. */
  150. void mxc_iomux_set_gpr(enum iomux_gp_func gp, bool en)
  151. {
  152. u32 l;
  153. spin_lock(&gpio_mux_lock);
  154. l = __raw_readl(IOMUXGPR);
  155. if (en)
  156. l |= gp;
  157. else
  158. l &= ~gp;
  159. __raw_writel(l, IOMUXGPR);
  160. spin_unlock(&gpio_mux_lock);
  161. }
  162. EXPORT_SYMBOL(mxc_iomux_set_gpr);