blob: 3982e91b2f3ea49d993bcf2bc32472304c3dadc6 [file] [log] [blame]
Sascha Hauer90292ea2008-07-05 10:02:50 +02001/*
2 * Copyright 2004-2006 Freescale Semiconductor, Inc. All Rights Reserved.
3 * Copyright (C) 2008 by Sascha Hauer <kernel@pengutronix.de>
Valentin Longchampb7222632009-01-28 15:13:50 +01004 * Copyright (C) 2009 by Valentin Longchamp <valentin.longchamp@epfl.ch>
Sascha Hauer90292ea2008-07-05 10:02:50 +02005 *
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 */
Russell King2f8163b2011-07-26 10:53:52 +010020#include <linux/gpio.h>
Sascha Hauer90292ea2008-07-05 10:02:50 +020021#include <linux/module.h>
22#include <linux/spinlock.h>
23#include <linux/io.h>
Valentin Longchampb7222632009-01-28 15:13:50 +010024#include <linux/kernel.h>
Shawn Guo267dd342012-09-13 13:26:00 +080025
Shawn Guo50f2de62012-09-14 14:14:45 +080026#include "hardware.h"
Shawn Guo267dd342012-09-13 13:26:00 +080027#include "iomux-mx3.h"
Sascha Hauer90292ea2008-07-05 10:02:50 +020028
29/*
30 * IOMUX register (base) addresses
31 */
Uwe Kleine-König1273e762009-12-16 19:06:12 +010032#define IOMUX_BASE MX31_IO_ADDRESS(MX31_IOMUXC_BASE_ADDR)
Sascha Hauer90292ea2008-07-05 10:02:50 +020033#define IOMUXINT_OBS1 (IOMUX_BASE + 0x000)
34#define IOMUXINT_OBS2 (IOMUX_BASE + 0x004)
35#define IOMUXGPR (IOMUX_BASE + 0x008)
36#define IOMUXSW_MUX_CTL (IOMUX_BASE + 0x00C)
37#define IOMUXSW_PAD_CTL (IOMUX_BASE + 0x154)
38
39static DEFINE_SPINLOCK(gpio_mux_lock);
40
41#define IOMUX_REG_MASK (IOMUX_PADNUM_MASK & ~0x3)
Valentin Longchampb7222632009-01-28 15:13:50 +010042
Joe Perchesf6d47502015-05-19 18:37:49 -070043static DECLARE_BITMAP(mxc_pin_alloc_map, NB_PORTS * 32);
Sascha Hauer90292ea2008-07-05 10:02:50 +020044/*
45 * set the mode for a IOMUX pin.
46 */
Dmitry Voytikc3008732014-11-06 22:55:04 +040047void mxc_iomux_mode(unsigned int pin_mode)
Sascha Hauer90292ea2008-07-05 10:02:50 +020048{
Dmitry Voytikc3008732014-11-06 22:55:04 +040049 u32 field;
50 u32 l;
51 u32 mode;
Luotao Fudefa8c32008-09-09 10:19:43 +020052 void __iomem *reg;
Sascha Hauer90292ea2008-07-05 10:02:50 +020053
54 reg = IOMUXSW_MUX_CTL + (pin_mode & IOMUX_REG_MASK);
55 field = pin_mode & 0x3;
56 mode = (pin_mode & IOMUX_MODE_MASK) >> IOMUX_MODE_SHIFT;
57
Sascha Hauer90292ea2008-07-05 10:02:50 +020058 spin_lock(&gpio_mux_lock);
59
Johannes Bergc5531382016-01-27 17:59:35 +010060 l = imx_readl(reg);
Sascha Hauer90292ea2008-07-05 10:02:50 +020061 l &= ~(0xff << (field * 8));
62 l |= mode << (field * 8);
Johannes Bergc5531382016-01-27 17:59:35 +010063 imx_writel(l, reg);
Sascha Hauer90292ea2008-07-05 10:02:50 +020064
65 spin_unlock(&gpio_mux_lock);
Sascha Hauer90292ea2008-07-05 10:02:50 +020066}
Sascha Hauer90292ea2008-07-05 10:02:50 +020067
68/*
69 * This function configures the pad value for a IOMUX pin.
70 */
71void mxc_iomux_set_pad(enum iomux_pins pin, u32 config)
72{
Luotao Fudefa8c32008-09-09 10:19:43 +020073 u32 field, l;
74 void __iomem *reg;
Sascha Hauer90292ea2008-07-05 10:02:50 +020075
Guennadi Liakhovetski4a7b98d2008-11-13 12:20:49 +010076 pin &= IOMUX_PADNUM_MASK;
77 reg = IOMUXSW_PAD_CTL + (pin + 2) / 3 * 4;
Sascha Hauer90292ea2008-07-05 10:02:50 +020078 field = (pin + 2) % 3;
79
Guennadi Liakhovetski4a7b98d2008-11-13 12:20:49 +010080 pr_debug("%s: reg offset = 0x%x, field = %d\n",
Sascha Hauer90292ea2008-07-05 10:02:50 +020081 __func__, (pin + 2) / 3, field);
82
83 spin_lock(&gpio_mux_lock);
84
Johannes Bergc5531382016-01-27 17:59:35 +010085 l = imx_readl(reg);
Guennadi Liakhovetski4a7b98d2008-11-13 12:20:49 +010086 l &= ~(0x1ff << (field * 10));
87 l |= config << (field * 10);
Johannes Bergc5531382016-01-27 17:59:35 +010088 imx_writel(l, reg);
Sascha Hauer90292ea2008-07-05 10:02:50 +020089
90 spin_unlock(&gpio_mux_lock);
91}
Sascha Hauer90292ea2008-07-05 10:02:50 +020092
93/*
Valentin Longchampef754d62009-05-06 11:44:20 +020094 * allocs a single pin:
Valentin Longchampb7222632009-01-28 15:13:50 +010095 * - reserves the pin so that it is not claimed by another driver
96 * - setups the iomux according to the configuration
Valentin Longchampb7222632009-01-28 15:13:50 +010097 */
Uwe Kleine-König10a3c452011-03-02 10:59:48 +010098int mxc_iomux_alloc_pin(unsigned int pin, const char *label)
Valentin Longchampb7222632009-01-28 15:13:50 +010099{
100 unsigned pad = pin & IOMUX_PADNUM_MASK;
Valentin Longchampb7222632009-01-28 15:13:50 +0100101
102 if (pad >= (PIN_MAX + 1)) {
Colin Ian Kingaa9fff52015-11-28 16:27:34 +0000103 printk(KERN_ERR "mxc_iomux: Attempt to request nonexistent pin %u for \"%s\"\n",
Valentin Longchampb7222632009-01-28 15:13:50 +0100104 pad, label ? label : "?");
105 return -EINVAL;
106 }
107
108 if (test_and_set_bit(pad, mxc_pin_alloc_map)) {
109 printk(KERN_ERR "mxc_iomux: pin %u already used. Allocation for \"%s\" failed\n",
110 pad, label ? label : "?");
Valentin Longchampef754d62009-05-06 11:44:20 +0200111 return -EBUSY;
Valentin Longchampb7222632009-01-28 15:13:50 +0100112 }
113 mxc_iomux_mode(pin);
114
Valentin Longchampb7222632009-01-28 15:13:50 +0100115 return 0;
116}
Valentin Longchampb7222632009-01-28 15:13:50 +0100117
Uwe Kleine-König10a3c452011-03-02 10:59:48 +0100118int mxc_iomux_setup_multiple_pins(const unsigned int *pin_list, unsigned count,
Valentin Longchampb7222632009-01-28 15:13:50 +0100119 const char *label)
120{
Uwe Kleine-König10a3c452011-03-02 10:59:48 +0100121 const unsigned int *p = pin_list;
Valentin Longchampb7222632009-01-28 15:13:50 +0100122 int i;
123 int ret = -EINVAL;
124
125 for (i = 0; i < count; i++) {
Valentin Longchampef754d62009-05-06 11:44:20 +0200126 ret = mxc_iomux_alloc_pin(*p, label);
127 if (ret)
Valentin Longchampb7222632009-01-28 15:13:50 +0100128 goto setup_error;
129 p++;
130 }
131 return 0;
132
133setup_error:
134 mxc_iomux_release_multiple_pins(pin_list, i);
135 return ret;
136}
Valentin Longchampb7222632009-01-28 15:13:50 +0100137
Uwe Kleine-König10a3c452011-03-02 10:59:48 +0100138void mxc_iomux_release_pin(unsigned int pin)
Valentin Longchampb7222632009-01-28 15:13:50 +0100139{
140 unsigned pad = pin & IOMUX_PADNUM_MASK;
Valentin Longchampb7222632009-01-28 15:13:50 +0100141
142 if (pad < (PIN_MAX + 1))
143 clear_bit(pad, mxc_pin_alloc_map);
Valentin Longchampb7222632009-01-28 15:13:50 +0100144}
Valentin Longchampb7222632009-01-28 15:13:50 +0100145
Uwe Kleine-König10a3c452011-03-02 10:59:48 +0100146void mxc_iomux_release_multiple_pins(const unsigned int *pin_list, int count)
Valentin Longchampb7222632009-01-28 15:13:50 +0100147{
Uwe Kleine-König10a3c452011-03-02 10:59:48 +0100148 const unsigned int *p = pin_list;
Valentin Longchampb7222632009-01-28 15:13:50 +0100149 int i;
150
151 for (i = 0; i < count; i++) {
152 mxc_iomux_release_pin(*p);
153 p++;
154 }
155}
Valentin Longchampb7222632009-01-28 15:13:50 +0100156
157/*
Sascha Hauer90292ea2008-07-05 10:02:50 +0200158 * This function enables/disables the general purpose function for a particular
159 * signal.
160 */
161void mxc_iomux_set_gpr(enum iomux_gp_func gp, bool en)
162{
163 u32 l;
164
165 spin_lock(&gpio_mux_lock);
Johannes Bergc5531382016-01-27 17:59:35 +0100166 l = imx_readl(IOMUXGPR);
Sascha Hauer90292ea2008-07-05 10:02:50 +0200167 if (en)
168 l |= gp;
169 else
170 l &= ~gp;
171
Johannes Bergc5531382016-01-27 17:59:35 +0100172 imx_writel(l, IOMUXGPR);
Sascha Hauer90292ea2008-07-05 10:02:50 +0200173 spin_unlock(&gpio_mux_lock);
174}