1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2004-2006 Freescale Semiconductor, Inc. All Rights Reserved.
4 * Copyright (C) 2008 by Sascha Hauer <kernel@pengutronix.de>
5 * Copyright (C) 2009 by Valentin Longchamp <valentin.longchamp@epfl.ch>
7 #include <linux/gpio.h>
8 #include <linux/module.h>
9 #include <linux/spinlock.h>
11 #include <linux/kernel.h>
14 #include "iomux-mx3.h"
17 * IOMUX register (base) addresses
19 #define IOMUX_BASE MX31_IO_ADDRESS(MX31_IOMUXC_BASE_ADDR)
20 #define IOMUXINT_OBS1 (IOMUX_BASE + 0x000)
21 #define IOMUXINT_OBS2 (IOMUX_BASE + 0x004)
22 #define IOMUXGPR (IOMUX_BASE + 0x008)
23 #define IOMUXSW_MUX_CTL (IOMUX_BASE + 0x00C)
24 #define IOMUXSW_PAD_CTL (IOMUX_BASE + 0x154)
26 static DEFINE_SPINLOCK(gpio_mux_lock
);
28 #define IOMUX_REG_MASK (IOMUX_PADNUM_MASK & ~0x3)
30 static DECLARE_BITMAP(mxc_pin_alloc_map
, NB_PORTS
* 32);
32 * set the mode for a IOMUX pin.
34 void mxc_iomux_mode(unsigned int pin_mode
)
41 reg
= IOMUXSW_MUX_CTL
+ (pin_mode
& IOMUX_REG_MASK
);
42 field
= pin_mode
& 0x3;
43 mode
= (pin_mode
& IOMUX_MODE_MASK
) >> IOMUX_MODE_SHIFT
;
45 spin_lock(&gpio_mux_lock
);
48 l
&= ~(0xff << (field
* 8));
49 l
|= mode
<< (field
* 8);
52 spin_unlock(&gpio_mux_lock
);
56 * This function configures the pad value for a IOMUX pin.
58 void mxc_iomux_set_pad(enum iomux_pins pin
, u32 config
)
63 pin
&= IOMUX_PADNUM_MASK
;
64 reg
= IOMUXSW_PAD_CTL
+ (pin
+ 2) / 3 * 4;
65 field
= (pin
+ 2) % 3;
67 pr_debug("%s: reg offset = 0x%x, field = %d\n",
68 __func__
, (pin
+ 2) / 3, field
);
70 spin_lock(&gpio_mux_lock
);
73 l
&= ~(0x1ff << (field
* 10));
74 l
|= config
<< (field
* 10);
77 spin_unlock(&gpio_mux_lock
);
81 * allocs a single pin:
82 * - reserves the pin so that it is not claimed by another driver
83 * - setups the iomux according to the configuration
85 int mxc_iomux_alloc_pin(unsigned int pin
, const char *label
)
87 unsigned pad
= pin
& IOMUX_PADNUM_MASK
;
89 if (pad
>= (PIN_MAX
+ 1)) {
90 printk(KERN_ERR
"mxc_iomux: Attempt to request nonexistent pin %u for \"%s\"\n",
91 pad
, label
? label
: "?");
95 if (test_and_set_bit(pad
, mxc_pin_alloc_map
)) {
96 printk(KERN_ERR
"mxc_iomux: pin %u already used. Allocation for \"%s\" failed\n",
97 pad
, label
? label
: "?");
105 int mxc_iomux_setup_multiple_pins(const unsigned int *pin_list
, unsigned count
,
108 const unsigned int *p
= pin_list
;
112 for (i
= 0; i
< count
; i
++) {
113 ret
= mxc_iomux_alloc_pin(*p
, label
);
121 mxc_iomux_release_multiple_pins(pin_list
, i
);
125 void mxc_iomux_release_pin(unsigned int pin
)
127 unsigned pad
= pin
& IOMUX_PADNUM_MASK
;
129 if (pad
< (PIN_MAX
+ 1))
130 clear_bit(pad
, mxc_pin_alloc_map
);
133 void mxc_iomux_release_multiple_pins(const unsigned int *pin_list
, int count
)
135 const unsigned int *p
= pin_list
;
138 for (i
= 0; i
< count
; i
++) {
139 mxc_iomux_release_pin(*p
);
145 * This function enables/disables the general purpose function for a particular
148 void mxc_iomux_set_gpr(enum iomux_gp_func gp
, bool en
)
152 spin_lock(&gpio_mux_lock
);
153 l
= imx_readl(IOMUXGPR
);
159 imx_writel(l
, IOMUXGPR
);
160 spin_unlock(&gpio_mux_lock
);