summaryrefslogtreecommitdiff
path: root/arch/arm/mach-mx27/gpio_mux.c
blob: 78e740870992d108e3e558c4416718ddf20054cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

/*!
 *@file gpio_mux.c
 *@brief This file contains the IOMUX implementation details. 
 * @ingroup GPIO
 */

#include <linux/kernel.h>
#include <linux/cache.h>
#include <linux/spinlock.h>

#include <asm/io.h>
#include <asm/hardware.h>
#include <asm/arch/gpio.h>
#include "gpio_mux.h"

/*!
 * This structure defines the offset of registers in gpio module.
 */
enum gpio_reg {
	GPIO_GIUS = 0x20,
	GPIO_GPR = 0x38,
	GPIO_PUEN = 0x40,
	GPIO_DDIR = 0x00,
	GPIO_OCR1 = 0x04,
	GPIO_OCR2 = 0x08,
	GPIO_ICONFA1 = 0x0C,
	GPIO_ICONFA2 = 0x10,
	GPIO_ICONFB1 = 0x14,
	GPIO_ICONFB2 = 0x18,
};

/*!
 * This enumeration data type defines the configuration for input mode.
 */
typedef enum {
	GPIO_INPUT_GPIO = 0x00,
	GPIO_INPUT_INTR = 0x01,
	GPIO_INPUT_LOW = 0x02,
	GPIO_INPUT_HIGH = 0x03
} gpio_input_cfg_t;

/*!
 * This enumeration data type defines the configuration for output mode.
 */
typedef enum {
	GPIO_OUTPUT_A = 0x00,
	GPIO_OUTPUT_B = 0x01,
	GPIO_OUTPUT_C = 0x02,
	GPIO_OUTPUT_DR = 0x03
} gpio_output_cfg_t;

extern struct mxc_gpio_port mxc_gpio_ports[];

/*!
 * defines a spinlock to protected the accessing to gpio pin.
 */
DEFINE_SPINLOCK(gpio_mux_lock);

/*!
 * This function enable or disable the pullup feature to the pin. 
 * @param port  	a pointer of gpio port
 * @param index 	the index of the  pin in the port
 * @param en		0 if disable pullup, otherwise enable it.
 * @return		none
 */
static inline void _gpio_set_puen(struct mxc_gpio_port *port, u32 index,
				  bool en)
{
	u32 reg;

	reg = __raw_readl(port->base + GPIO_PUEN);
	if (en) {
		reg |= 1 << index;
	} else {
		reg &= ~(1 << index);
	}
	__raw_writel(reg, port->base + GPIO_PUEN);
}

/*!
 * This function set the input configuration A. 
 * @param port  	a pointer of gpio port
 * @param index 	the index of the  pin in the port
 * @param config	a mode as define in \b #gpio_input_cfg_t
 * @return		none
 */
static inline void _gpio_set_iconfa(struct mxc_gpio_port *port, u32 index,
				    gpio_input_cfg_t config)
{
	u32 reg, val;
	u32 mask;

	mask = 0x3 << ((index % 16) << 1);

	if (index >= 16) {
		reg = port->base + GPIO_ICONFA2;
		val = config << ((index - 16) * 2);
	} else {
		reg = port->base + GPIO_ICONFA1;
		val = config << (index * 2);
	}
	val |= __raw_readl(reg) & ~(mask);
	__raw_writel(val, reg);
}

/*!
 * This function set the input configuration B. 
 * @param port  	a pointer of gpio port
 * @param index 	the index of the  pin in the port
 * @param config	a mode as define in \b #gpio_input_cfg_t
 * @return		none
 */
static inline void _gpio_set_iconfb(struct mxc_gpio_port *port, u32 index,
				    gpio_input_cfg_t config)
{
	u32 reg, val;
	u32 mask;

	mask = 0x3 << ((index % 16) << 1);

	if (index >= 16) {
		reg = port->base + GPIO_ICONFB2;
		val = config << ((index - 16) * 2);
	} else {
		reg = port->base + GPIO_ICONFB1;
		val = config << (index * 2);
	}
	val |= __raw_readl(reg) & (~mask);
	__raw_writel(val, reg);
}

/*!
 * This function set the output configuration. 
 * @param port  	a pointer of gpio port
 * @param index 	the index of the  pin in the port
 * @param config	a mode as define in \b #gpio_output_cfg_t
 * @return		none
 */
static inline void _gpio_set_ocr(struct mxc_gpio_port *port, u32 index,
				 gpio_output_cfg_t config)
{
	u32 reg, val;
	u32 mask;

	mask = 0x3 << ((index % 16) << 1);
	if (index >= 16) {
		reg = port->base + GPIO_OCR2;
		val = config << ((index - 16) * 2);
	} else {
		reg = port->base + GPIO_OCR1;
		val = config << (index * 2);
	}
	val |= __raw_readl(reg) & (~mask);
	__raw_writel(val, reg);
}

/*!
 *@brief gpio_config_mux - just configure the mode of the gpio pin.
 *@param pin   a pin number as defined in \b #iomux_pin_name_t
 *@param mode  a module as define in \b #gpio_mux_mode_t;
 *	GPIO_MUX_PRIMARY set pin to work as primary function.
 *	GPIO_MUX_ALT set pin to work as alternate function.
 *	GPIO_MUX_GPIO set pin to work as output function based the data register
 *	GPIO_MUX_INPUT1 set pin to work as input function connected with  A_OUT
 *	GPIO_MUX_INPUT2 set pin to work as input function connected with B_OUT
 *	GPIO_MUX_OUTPUT1 set pin to work as output function connected with A_IN
 *	GPIO_MUX_OUTPUT2 set pin to work as output function connected with B_IN
 *	GPIO_MUX_OUTPUT3 set pin to work as output function connected with C_IN
 *@return      0 if successful, Non-zero otherwise
 */

int gpio_config_mux(iomux_pin_name_t pin, gpio_mux_mode_t mode)
{
	unsigned long lock_flags;
	u32 gius_reg, gpr_reg;
	struct mxc_gpio_port *port;
	u32 index, gpio = IOMUX_TO_GPIO(pin);

	port = &(mxc_gpio_ports[GPIO_TO_PORT(gpio)]);
	index = GPIO_TO_INDEX(gpio);

	pr_debug("%s: Configuring PORT %c, bit %d\n",
		 __FUNCTION__, port->num + 'A', index);

	spin_lock_irqsave(&gpio_mux_lock, lock_flags);

	gius_reg = __raw_readl(port->base + GPIO_GIUS);
	gpr_reg = __raw_readl(port->base + GPIO_GPR);

	switch (mode) {
	case GPIO_MUX_PRIMARY:
		gius_reg &= ~(1L << index);
		gpr_reg &= ~(1L << index);
		break;
	case GPIO_MUX_ALT:
		gius_reg &= ~(1L << index);
		gpr_reg |= (1L << index);
		break;
	case GPIO_MUX_GPIO:
		gius_reg |= (1L << index);
		_gpio_set_ocr(port, index, GPIO_OUTPUT_DR);
		break;
	case GPIO_MUX_INPUT1:
		gius_reg |= (1L << index);
		_gpio_set_iconfa(port, index, GPIO_INPUT_GPIO);
		break;
	case GPIO_MUX_INPUT2:
		gius_reg |= (1L << index);
		_gpio_set_iconfb(port, index, GPIO_INPUT_GPIO);
		break;
	case GPIO_MUX_OUTPUT1:
		gius_reg |= (1L << index);
		_gpio_set_ocr(port, index, GPIO_OUTPUT_A);
		break;
	case GPIO_MUX_OUTPUT2:
		gius_reg |= (1L << index);
		_gpio_set_ocr(port, index, GPIO_OUTPUT_B);
		break;
	case GPIO_MUX_OUTPUT3:
		gius_reg |= (1L << index);
		_gpio_set_ocr(port, index, GPIO_OUTPUT_C);
		break;
	default:
		spin_unlock_irqrestore(&gpio_mux_lock, lock_flags);
		return -1;
	}

	__raw_writel(gius_reg, port->base + GPIO_GIUS);
	__raw_writel(gpr_reg, port->base + GPIO_GPR);

	spin_unlock_irqrestore(&gpio_mux_lock, lock_flags);
	return 0;
}

/*!
 * This function is just used to enable or disable the pull up feature .
 * @param pin   a pin number as defined in \b #iomux_pin_name_t
 * @param en    0 if disable, Non-zero enable
 * @return      0 if successful, Non-zero otherwise
 */
int gpio_set_puen(iomux_pin_name_t pin, bool en)
{
	unsigned long lock_flags;

	struct mxc_gpio_port *port;
	u32 index, gpio = IOMUX_TO_GPIO(pin);

	port = &(mxc_gpio_ports[GPIO_TO_PORT(gpio)]);
	index = GPIO_TO_INDEX(gpio);

	pr_debug("%s: Configuring output mode of PORT %c, bit %d\n",
		 __FUNCTION__, port->num + 'A', index);

	spin_lock_irqsave(&gpio_mux_lock, lock_flags);

	_gpio_set_puen(port, index, en);
	spin_unlock_irqrestore(&gpio_mux_lock, lock_flags);
	return 0;

}

/*!
 * This function is just used to request a pin and configure it.
 * @param pin	a pin number as defined in \b #iomux_pin_name_t
 * @param mode	a module as define in \b #gpio_mux_mode_t;
 * @return	0 if successful, Non-zero otherwise
 */
int gpio_request_mux(iomux_pin_name_t pin, gpio_mux_mode_t mode)
{
	int ret;
	ret = mxc_request_gpio(pin);
	if (ret == 0) {
		ret = gpio_config_mux(pin, mode);
		if (ret) {
			mxc_free_gpio(pin);
		}
	}
	return ret;
}

/*!
 * This function is just used to release a pin.
 * @param pin	a pin number as defined in \b #iomux_pin_name_t
 * @return	none	
 */
void gpio_free_mux(iomux_pin_name_t pin)
{
	mxc_free_gpio(pin);
}