| 
									
										
										
										
											2008-07-05 10:02:50 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright 2004-2006 Freescale Semiconductor, Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * Copyright (C) 2008 by Sascha Hauer <kernel@pengutronix.de> | 
					
						
							| 
									
										
										
										
											2009-01-28 15:13:50 +01:00
										 |  |  |  * Copyright (C) 2009 by Valentin Longchamp <valentin.longchamp@epfl.ch> | 
					
						
							| 
									
										
										
										
											2008-07-05 10:02:50 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software | 
					
						
							|  |  |  |  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | 
					
						
							|  |  |  |  * MA 02110-1301, USA. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2011-07-26 10:53:52 +01:00
										 |  |  | #include <linux/gpio.h>
 | 
					
						
							| 
									
										
										
										
											2008-07-05 10:02:50 +02:00
										 |  |  | #include <linux/module.h>
 | 
					
						
							|  |  |  | #include <linux/spinlock.h>
 | 
					
						
							|  |  |  | #include <linux/io.h>
 | 
					
						
							| 
									
										
										
										
											2009-01-28 15:13:50 +01:00
										 |  |  | #include <linux/kernel.h>
 | 
					
						
							| 
									
										
										
										
											2012-09-13 13:26:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-14 14:14:45 +08:00
										 |  |  | #include "hardware.h"
 | 
					
						
							| 
									
										
										
										
											2012-09-13 13:26:00 +08:00
										 |  |  | #include "iomux-mx3.h"
 | 
					
						
							| 
									
										
										
										
											2008-07-05 10:02:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * IOMUX register (base) addresses | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-12-16 19:06:12 +01:00
										 |  |  | #define IOMUX_BASE	MX31_IO_ADDRESS(MX31_IOMUXC_BASE_ADDR)
 | 
					
						
							| 
									
										
										
										
											2008-07-05 10:02:50 +02:00
										 |  |  | #define IOMUXINT_OBS1	(IOMUX_BASE + 0x000)
 | 
					
						
							|  |  |  | #define IOMUXINT_OBS2	(IOMUX_BASE + 0x004)
 | 
					
						
							|  |  |  | #define IOMUXGPR	(IOMUX_BASE + 0x008)
 | 
					
						
							|  |  |  | #define IOMUXSW_MUX_CTL	(IOMUX_BASE + 0x00C)
 | 
					
						
							|  |  |  | #define IOMUXSW_PAD_CTL	(IOMUX_BASE + 0x154)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static DEFINE_SPINLOCK(gpio_mux_lock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define IOMUX_REG_MASK (IOMUX_PADNUM_MASK & ~0x3)
 | 
					
						
							| 
									
										
										
										
											2009-01-28 15:13:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-25 09:20:30 -03:00
										 |  |  | static unsigned long mxc_pin_alloc_map[NB_PORTS * 32 / BITS_PER_LONG]; | 
					
						
							| 
									
										
										
										
											2008-07-05 10:02:50 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * set the mode for a IOMUX pin. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | int mxc_iomux_mode(unsigned int pin_mode) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-09-09 10:19:43 +02:00
										 |  |  | 	u32 field, l, mode, ret = 0; | 
					
						
							|  |  |  | 	void __iomem *reg; | 
					
						
							| 
									
										
										
										
											2008-07-05 10:02:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	reg = IOMUXSW_MUX_CTL + (pin_mode & IOMUX_REG_MASK); | 
					
						
							|  |  |  | 	field = pin_mode & 0x3; | 
					
						
							|  |  |  | 	mode = (pin_mode & IOMUX_MODE_MASK) >> IOMUX_MODE_SHIFT; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spin_lock(&gpio_mux_lock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	l = __raw_readl(reg); | 
					
						
							|  |  |  | 	l &= ~(0xff << (field * 8)); | 
					
						
							|  |  |  | 	l |= mode << (field * 8); | 
					
						
							|  |  |  | 	__raw_writel(l, reg); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spin_unlock(&gpio_mux_lock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * This function configures the pad value for a IOMUX pin. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void mxc_iomux_set_pad(enum iomux_pins pin, u32 config) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-09-09 10:19:43 +02:00
										 |  |  | 	u32 field, l; | 
					
						
							|  |  |  | 	void __iomem *reg; | 
					
						
							| 
									
										
										
										
											2008-07-05 10:02:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-13 12:20:49 +01:00
										 |  |  | 	pin &= IOMUX_PADNUM_MASK; | 
					
						
							|  |  |  | 	reg = IOMUXSW_PAD_CTL + (pin + 2) / 3 * 4; | 
					
						
							| 
									
										
										
										
											2008-07-05 10:02:50 +02:00
										 |  |  | 	field = (pin + 2) % 3; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-13 12:20:49 +01:00
										 |  |  | 	pr_debug("%s: reg offset = 0x%x, field = %d\n", | 
					
						
							| 
									
										
										
										
											2008-07-05 10:02:50 +02:00
										 |  |  | 			__func__, (pin + 2) / 3, field); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spin_lock(&gpio_mux_lock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	l = __raw_readl(reg); | 
					
						
							| 
									
										
										
										
											2008-11-13 12:20:49 +01:00
										 |  |  | 	l &= ~(0x1ff << (field * 10)); | 
					
						
							|  |  |  | 	l |= config << (field * 10); | 
					
						
							| 
									
										
										
										
											2008-07-05 10:02:50 +02:00
										 |  |  | 	__raw_writel(l, reg); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spin_unlock(&gpio_mux_lock); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 15:13:50 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2009-05-06 11:44:20 +02:00
										 |  |  |  * allocs a single pin: | 
					
						
							| 
									
										
										
										
											2009-01-28 15:13:50 +01:00
										 |  |  |  * 	- reserves the pin so that it is not claimed by another driver | 
					
						
							|  |  |  |  * 	- setups the iomux according to the configuration | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2011-03-02 10:59:48 +01:00
										 |  |  | int mxc_iomux_alloc_pin(unsigned int pin, const char *label) | 
					
						
							| 
									
										
										
										
											2009-01-28 15:13:50 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	unsigned pad = pin & IOMUX_PADNUM_MASK; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (pad >= (PIN_MAX + 1)) { | 
					
						
							|  |  |  | 		printk(KERN_ERR "mxc_iomux: Attempt to request nonexistant pin %u for \"%s\"\n", | 
					
						
							|  |  |  | 			pad, label ? label : "?"); | 
					
						
							|  |  |  | 		return -EINVAL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (test_and_set_bit(pad, mxc_pin_alloc_map)) { | 
					
						
							|  |  |  | 		printk(KERN_ERR "mxc_iomux: pin %u already used. Allocation for \"%s\" failed\n", | 
					
						
							|  |  |  | 			pad, label ? label : "?"); | 
					
						
							| 
									
										
										
										
											2009-05-06 11:44:20 +02:00
										 |  |  | 		return -EBUSY; | 
					
						
							| 
									
										
										
										
											2009-01-28 15:13:50 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	mxc_iomux_mode(pin); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-02 10:59:48 +01:00
										 |  |  | int mxc_iomux_setup_multiple_pins(const unsigned int *pin_list, unsigned count, | 
					
						
							| 
									
										
										
										
											2009-01-28 15:13:50 +01:00
										 |  |  | 		const char *label) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-02 10:59:48 +01:00
										 |  |  | 	const unsigned int *p = pin_list; | 
					
						
							| 
									
										
										
										
											2009-01-28 15:13:50 +01:00
										 |  |  | 	int i; | 
					
						
							|  |  |  | 	int ret = -EINVAL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < count; i++) { | 
					
						
							| 
									
										
										
										
											2009-05-06 11:44:20 +02:00
										 |  |  | 		ret = mxc_iomux_alloc_pin(*p, label); | 
					
						
							|  |  |  | 		if (ret) | 
					
						
							| 
									
										
										
										
											2009-01-28 15:13:50 +01:00
										 |  |  | 			goto setup_error; | 
					
						
							|  |  |  | 		p++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | setup_error: | 
					
						
							|  |  |  | 	mxc_iomux_release_multiple_pins(pin_list, i); | 
					
						
							|  |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-02 10:59:48 +01:00
										 |  |  | void mxc_iomux_release_pin(unsigned int pin) | 
					
						
							| 
									
										
										
										
											2009-01-28 15:13:50 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	unsigned pad = pin & IOMUX_PADNUM_MASK; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (pad < (PIN_MAX + 1)) | 
					
						
							|  |  |  | 		clear_bit(pad, mxc_pin_alloc_map); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-02 10:59:48 +01:00
										 |  |  | void mxc_iomux_release_multiple_pins(const unsigned int *pin_list, int count) | 
					
						
							| 
									
										
										
										
											2009-01-28 15:13:50 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-02 10:59:48 +01:00
										 |  |  | 	const unsigned int *p = pin_list; | 
					
						
							| 
									
										
										
										
											2009-01-28 15:13:50 +01:00
										 |  |  | 	int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < count; i++) { | 
					
						
							|  |  |  | 		mxc_iomux_release_pin(*p); | 
					
						
							|  |  |  | 		p++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-05 10:02:50 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * This function enables/disables the general purpose function for a particular | 
					
						
							|  |  |  |  * signal. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void mxc_iomux_set_gpr(enum iomux_gp_func gp, bool en) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	u32 l; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spin_lock(&gpio_mux_lock); | 
					
						
							|  |  |  | 	l = __raw_readl(IOMUXGPR); | 
					
						
							|  |  |  | 	if (en) | 
					
						
							|  |  |  | 		l |= gp; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		l &= ~gp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	__raw_writel(l, IOMUXGPR); | 
					
						
							|  |  |  | 	spin_unlock(&gpio_mux_lock); | 
					
						
							|  |  |  | } |