iomux-imx31.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "hardware.h"
  26. #include "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. static DECLARE_BITMAP(mxc_pin_alloc_map, NB_PORTS * 32);
  39. /*
  40. * set the mode for a IOMUX pin.
  41. */
  42. void mxc_iomux_mode(unsigned int pin_mode)
  43. {
  44. u32 field;
  45. u32 l;
  46. u32 mode;
  47. void __iomem *reg;
  48. reg = IOMUXSW_MUX_CTL + (pin_mode & IOMUX_REG_MASK);
  49. field = pin_mode & 0x3;
  50. mode = (pin_mode & IOMUX_MODE_MASK) >> IOMUX_MODE_SHIFT;
  51. spin_lock(&gpio_mux_lock);
  52. l = imx_readl(reg);
  53. l &= ~(0xff << (field * 8));
  54. l |= mode << (field * 8);
  55. imx_writel(l, reg);
  56. spin_unlock(&gpio_mux_lock);
  57. }
  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 = imx_readl(reg);
  72. l &= ~(0x1ff << (field * 10));
  73. l |= config << (field * 10);
  74. imx_writel(l, reg);
  75. spin_unlock(&gpio_mux_lock);
  76. }
  77. /*
  78. * allocs a single pin:
  79. * - reserves the pin so that it is not claimed by another driver
  80. * - setups the iomux according to the configuration
  81. */
  82. int mxc_iomux_alloc_pin(unsigned int pin, const char *label)
  83. {
  84. unsigned pad = pin & IOMUX_PADNUM_MASK;
  85. if (pad >= (PIN_MAX + 1)) {
  86. printk(KERN_ERR "mxc_iomux: Attempt to request nonexistent pin %u for \"%s\"\n",
  87. pad, label ? label : "?");
  88. return -EINVAL;
  89. }
  90. if (test_and_set_bit(pad, mxc_pin_alloc_map)) {
  91. printk(KERN_ERR "mxc_iomux: pin %u already used. Allocation for \"%s\" failed\n",
  92. pad, label ? label : "?");
  93. return -EBUSY;
  94. }
  95. mxc_iomux_mode(pin);
  96. return 0;
  97. }
  98. int mxc_iomux_setup_multiple_pins(const unsigned int *pin_list, unsigned count,
  99. const char *label)
  100. {
  101. const unsigned int *p = pin_list;
  102. int i;
  103. int ret = -EINVAL;
  104. for (i = 0; i < count; i++) {
  105. ret = mxc_iomux_alloc_pin(*p, label);
  106. if (ret)
  107. goto setup_error;
  108. p++;
  109. }
  110. return 0;
  111. setup_error:
  112. mxc_iomux_release_multiple_pins(pin_list, i);
  113. return ret;
  114. }
  115. void mxc_iomux_release_pin(unsigned int pin)
  116. {
  117. unsigned pad = pin & IOMUX_PADNUM_MASK;
  118. if (pad < (PIN_MAX + 1))
  119. clear_bit(pad, mxc_pin_alloc_map);
  120. }
  121. void mxc_iomux_release_multiple_pins(const unsigned int *pin_list, int count)
  122. {
  123. const unsigned int *p = pin_list;
  124. int i;
  125. for (i = 0; i < count; i++) {
  126. mxc_iomux_release_pin(*p);
  127. p++;
  128. }
  129. }
  130. /*
  131. * This function enables/disables the general purpose function for a particular
  132. * signal.
  133. */
  134. void mxc_iomux_set_gpr(enum iomux_gp_func gp, bool en)
  135. {
  136. u32 l;
  137. spin_lock(&gpio_mux_lock);
  138. l = imx_readl(IOMUXGPR);
  139. if (en)
  140. l |= gp;
  141. else
  142. l &= ~gp;
  143. imx_writel(l, IOMUXGPR);
  144. spin_unlock(&gpio_mux_lock);
  145. }