summaryrefslogtreecommitdiff
path: root/arch/arm/mach-nexell/nx_gpio.c
blob: dfba3a2374cb3a91de9fe8aafe4dc9fddf5ea802 (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2016 Nexell
 * Youngbok, Park <ybpark@nexell.co.kr>
 */

/*
 * FIXME : will be remove after support pinctrl
 */
#include <linux/types.h>
#include <asm/io.h>
#include <asm/arch/nexell.h>
#include "asm/arch/nx_gpio.h"
#define NUMBER_OF_GPIO_MODULE 5
u32 __g_nx_gpio_valid_bit[NUMBER_OF_GPIO_MODULE] = {
	0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF};

static struct {
	struct nx_gpio_register_set *pregister;
} __g_module_variables[NUMBER_OF_GPIO_MODULE] = {
	{ (struct nx_gpio_register_set *)PHY_BASEADDR_GPIOA },
	{ (struct nx_gpio_register_set *)PHY_BASEADDR_GPIOB },
	{ (struct nx_gpio_register_set *)PHY_BASEADDR_GPIOC },
	{ (struct nx_gpio_register_set *)PHY_BASEADDR_GPIOD },
	{ (struct nx_gpio_register_set *)PHY_BASEADDR_GPIOE },
};

enum { nx_gpio_max_bit = 32 };

void nx_gpio_set_bit(u32 *value, u32 bit, int enable)
{
	register u32 newvalue;

	newvalue = *value;
	newvalue &= ~(1ul << bit);
	newvalue |= (u32)enable << bit;
	writel(newvalue, value);
}

int nx_gpio_get_bit(u32 value, u32 bit)
{
	return (int)((value >> bit) & (1ul));
}

void nx_gpio_set_bit2(u32 *value, u32 bit, u32 bit_value)
{
	register u32 newvalue = *value;

	newvalue = (u32)(newvalue & ~(3ul << (bit * 2)));
	newvalue = (u32)(newvalue | (bit_value << (bit * 2)));

	writel(newvalue, value);
}

u32 nx_gpio_get_bit2(u32 value, u32 bit)
{
	return (u32)((u32)(value >> (bit * 2)) & 3ul);
}

int nx_gpio_initialize(void)
{
	static int binit;
	u32 i;

	binit = 0;

	if (binit == 0) {
		for (i = 0; i < NUMBER_OF_GPIO_MODULE; i++)
			__g_module_variables[i].pregister = NULL;
		binit = true;
	}
	for (i = 0; i < NUMBER_OF_GPIO_MODULE; i++) {
		__g_nx_gpio_valid_bit[i] = 0xFFFFFFFF;
	};
	return true;
}

u32 nx_gpio_get_number_of_module(void)
{
	return NUMBER_OF_GPIO_MODULE;
}

u32 nx_gpio_get_size_of_register_set(void)
{
	return sizeof(struct nx_gpio_register_set);
}

void nx_gpio_set_base_address(u32 module_index, void *base_address)
{
	__g_module_variables[module_index].pregister =
		(struct nx_gpio_register_set *)base_address;
}

void *nx_gpio_get_base_address(u32 module_index)
{
	return (void *)__g_module_variables[module_index].pregister;
}

int nx_gpio_open_module(u32 module_index)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	writel(0xFFFFFFFF, &pregister->gpiox_slew_disable_default);
	writel(0xFFFFFFFF, &pregister->gpiox_drv1_disable_default);
	writel(0xFFFFFFFF, &pregister->gpiox_drv0_disable_default);
	writel(0xFFFFFFFF, &pregister->gpiox_pullsel_disable_default);
	writel(0xFFFFFFFF, &pregister->gpiox_pullenb_disable_default);
	return true;
}

int nx_gpio_close_module(u32 module_index) { return true; }

int nx_gpio_check_busy(u32 module_index) { return false; }

void nx_gpio_set_pad_function(u32 module_index, u32 bit_number,
			      u32 padfunc)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	nx_gpio_set_bit2(&pregister->gpioxaltfn[bit_number / 16],
			 bit_number % 16, padfunc);
}

void nx_gpio_set_pad_function32(u32 module_index, u32 msbvalue, u32 lsbvalue)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	writel(lsbvalue, &pregister->gpioxaltfn[0]);
	writel(msbvalue, &pregister->gpioxaltfn[1]);
}

int nx_gpio_get_pad_function(u32 module_index, u32 bit_number)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	return (int)nx_gpio_get_bit2
		(readl(&pregister->gpioxaltfn[bit_number / 16]),
		 bit_number % 16);
}

void nx_gpio_set_output_enable(u32 module_index, u32 bit_number,
			       int output_enb)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	nx_gpio_set_bit(&pregister->gpioxoutenb, bit_number, output_enb);
}

int nx_gpio_get_detect_enable(u32 module_index, u32 bit_number)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	return nx_gpio_get_bit(readl(&pregister->gpioxdetenb), bit_number);
}

u32 nx_gpio_get_detect_enable32(u32 module_index)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	return readl(&pregister->gpioxdetenb);
}

void nx_gpio_set_detect_enable(u32 module_index, u32 bit_number,
			       int detect_enb)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	nx_gpio_set_bit(&pregister->gpioxdetenb, bit_number, detect_enb);
}

void nx_gpio_set_detect_enable32(u32 module_index, u32 enable_flag)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	writel(enable_flag, &pregister->gpioxdetenb);
}

int nx_gpio_get_output_enable(u32 module_index, u32 bit_number)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	return nx_gpio_get_bit(readl(&pregister->gpioxoutenb), bit_number);
}

void nx_gpio_set_output_enable32(u32 module_index, int output_enb)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	if (output_enb)
		writel(0xFFFFFFFF, &pregister->gpioxoutenb);
	else
		writel(0x0, &pregister->gpioxoutenb);
}

u32 nx_gpio_get_output_enable32(u32 module_index)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	return readl(&pregister->gpioxoutenb);
}

void nx_gpio_set_output_value(u32 module_index, u32 bit_number, int value)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	nx_gpio_set_bit(&pregister->gpioxout, bit_number, value);
}

int nx_gpio_get_output_value(u32 module_index, u32 bit_number)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	return nx_gpio_get_bit(readl(&pregister->gpioxout), bit_number);
}

void nx_gpio_set_output_value32(u32 module_index, u32 value)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	writel(value, &pregister->gpioxout);
}

u32 nx_gpio_get_output_value32(u32 module_index)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	return readl(&pregister->gpioxout);
}

int nx_gpio_get_input_value(u32 module_index, u32 bit_number)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	return nx_gpio_get_bit(readl(&pregister->gpioxpad), bit_number);
}

void nx_gpio_set_pull_select(u32 module_index, u32 bit_number, int enable)
{
	nx_gpio_set_bit(&__g_module_variables[module_index]
			.pregister->gpiox_pullsel_disable_default,
			bit_number, true);
	nx_gpio_set_bit
		(&__g_module_variables[module_index].pregister->gpiox_pullsel,
		 bit_number, enable);
}

void nx_gpio_set_pull_select32(u32 module_index, u32 value)
{
	writel(value,
	       &__g_module_variables[module_index].pregister->gpiox_pullsel);
}

int nx_gpio_get_pull_select(u32 module_index, u32 bit_number)
{
	return nx_gpio_get_bit
		(__g_module_variables[module_index].pregister->gpiox_pullsel,
		 bit_number);
}

u32 nx_gpio_get_pull_select32(u32 module_index)
{
	return __g_module_variables[module_index].pregister->gpiox_pullsel;
}

void nx_gpio_set_pull_mode(u32 module_index, u32 bit_number, u32 mode)
{
	nx_gpio_set_bit(&__g_module_variables[module_index]
			.pregister->gpiox_pullsel_disable_default,
			bit_number, true);
	nx_gpio_set_bit(&__g_module_variables[module_index]
			.pregister->gpiox_pullenb_disable_default,
			bit_number, true);
	if (mode == nx_gpio_pull_off) {
		nx_gpio_set_bit
		 (&__g_module_variables[module_index].pregister->gpiox_pullenb,
		 bit_number, false);
		nx_gpio_set_bit
		 (&__g_module_variables[module_index].pregister->gpiox_pullsel,
		 bit_number, false);
	} else {
		nx_gpio_set_bit
		 (&__g_module_variables[module_index].pregister->gpiox_pullsel,
		 bit_number, (mode & 1 ? true : false));
		nx_gpio_set_bit
		 (&__g_module_variables[module_index].pregister->gpiox_pullenb,
		 bit_number, true);
	}
}

void nx_gpio_set_fast_slew(u32 module_index, u32 bit_number,
			   int enable)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	nx_gpio_set_bit(&pregister->gpiox_slew, bit_number,
			(int)(!enable));
}

void nx_gpio_set_drive_strength(u32 module_index, u32 bit_number,
				u32 drvstrength)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	nx_gpio_set_bit(&pregister->gpiox_drv1, bit_number,
			(int)(((u32)drvstrength >> 0) & 0x1));
	nx_gpio_set_bit(&pregister->gpiox_drv0, bit_number,
			(int)(((u32)drvstrength >> 1) & 0x1));
}

void nx_gpio_set_drive_strength_disable_default(u32 module_index,
						u32 bit_number, int enable)
{
	register struct nx_gpio_register_set *pregister;

	pregister = __g_module_variables[module_index].pregister;
	nx_gpio_set_bit(&pregister->gpiox_drv1_disable_default, bit_number,
			(int)(enable));
	nx_gpio_set_bit(&pregister->gpiox_drv0_disable_default, bit_number,
			(int)(enable));
}

u32 nx_gpio_get_drive_strength(u32 module_index, u32 bit_number)
{
	register struct nx_gpio_register_set *pregister;
	register u32 retvalue;

	pregister = __g_module_variables[module_index].pregister;
	retvalue =
		nx_gpio_get_bit(readl(&pregister->gpiox_drv0), bit_number) << 1;
	retvalue |=
		nx_gpio_get_bit(readl(&pregister->gpiox_drv1), bit_number) << 0;
	return retvalue;
}