aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/ubicom32/files/arch/ubicom32/mach-common/ubi32-gpio.c
blob: 5fa22f52b4de8b76fd406fbc3e4947fb1cfb3554 (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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * arch/ubicom32/mach-common/ubi32-gpio.c
 *   Ubicom gpio driver
 *
 * (C) Copyright 2009, Ubicom, Inc.
 *
 * This file is part of the Ubicom32 Linux Kernel Port.
 *
 * The Ubicom32 Linux Kernel Port 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.
 *
 * The Ubicom32 Linux Kernel Port 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 the Ubicom32 Linux Kernel Port.  If not,
 * see <http://www.gnu.org/licenses/>.
 *
 * Ubicom32 implementation derived from (with many thanks):
 *   arch/m68knommu
 *   arch/blackfin
 *   arch/parisc
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <linux/irq.h>
#include <linux/version.h>

#if defined(CONFIG_PROC_FS)
#include <linux/proc_fs.h>
#endif

#include <linux/io.h>
#include <asm/ip5000.h>
#include <linux/gpio.h>

#define UBI_GPIO_CHECK_RANGE     0  /* !0 enables range checking */


/*
 * Each I/O port can be configured to operate in one of several
 * functional modes. One of these modes is GPIO, which causes the
 * entire port to function as a GPIO port.  Since the various port
 * registers serve the system with other important functions, such as
 * ethernet, serial, USB, etc., it isn't advantageous to set any of
 * the ports to be entirely dedicated for GPIO use.  The processor
 * alternatively allows individual bits of a port to be assigned to be
 * used as GPIO independently from the overall port function.  This
 * bit-by-bit assignment is selected by setting the corresponding bit
 * in the port's gpio_mask register.  When set, the selected bit is
 * then enabled as a GPIO.  If the corresponding bit is set in the
 * gpio_ctl register of the port, the bit is configured as a GPIO
 * output.  Otherwise, it is an input.
 *
 * NOTE: This driver uses the bit-by-bit GPIO function assignment
 * exclusively and *never* sets the port function registers to the
 * GPIO function.
 *
 * GPIO is not the main function of any of the I/O ports.  The port
 * bit widths are variable from one port to the next, determined by
 * the more common I/O functions of the ports.  For simplicity, this
 * driver assumes all the ports are 32 bits wide regardless of the
 * real bit width of the port.  GPIO bits are numbered from zero to
 * MAX_UBICOM_GPIOS.  Within a port, the least significant bit is
 * numbered bit zero, the most significant is bit 31.  Since the ports
 * are considered logically contiguous, GPIO #32 is the zeroth bit in
 * port #1, and so on.  Due to the hardware definition, there are
 * large gaps in the GPIO numbers representing real pins.
 *
 * NOTE: It is up to the programmer to refer to the processor data
 * sheet to determine which bits in which ports can be accessed and
 * used for GPIO.
 *
 */


/* There are 9 ports, A through I. Not all 32 bits in each
 * port can be a GPIO, but we pretend they are.  Its up to the
 * programmer to refer to the processor data sheet.
 */
#define MAX_UBICOM_GPIOS   (9 * 32) /* ARCH_NR_GPIOS */
#define NUM_GPIO_PORTS     (gpio_bank(MAX_UBICOM_GPIOS))


/* GPIO reservation bit map array */
static int reserved_gpio_map[NUM_GPIO_PORTS];


/* Array of hardware io_port addresses */
static struct ubicom32_io_port *gpio_bank_addr[NUM_GPIO_PORTS] =
{
	UBICOM32_IO_PORT(RA),
	UBICOM32_IO_PORT(RB),
	UBICOM32_IO_PORT(RC),
	UBICOM32_IO_PORT(RD),
	UBICOM32_IO_PORT(RE),
	UBICOM32_IO_PORT(RF),
	UBICOM32_IO_PORT(RG),
	UBICOM32_IO_PORT(RH),
	UBICOM32_IO_PORT(RI)
};


struct ubi_gpio_chip {
	/*
	 * Right now, nothing else lives here.
	 */
	struct gpio_chip gpio_chip;
};


#if UBI_GPIO_CHECK_RANGE
inline int check_gpio(unsigned gpio)
{
	if (gpio >= MAX_UBICOM_GPIOS)
		return -EINVAL;
	return 0;
}
#else
#define check_gpio(n)   (0)
#endif

/*
 * ubi_gpio_get_port
 *	Get the IO port associated with a certain gpio
 */
struct ubicom32_io_port *ubi_gpio_get_port(unsigned gpio)
{
	if (gpio_bank(gpio) > NUM_GPIO_PORTS) {
		return NULL;
	}
	return gpio_bank_addr[gpio_bank(gpio)];
}

/*
 * ubi_gpio_error()
 */
static void ubi_gpio_error(unsigned gpio)
{
	printk(KERN_ERR "ubicom-gpio: GPIO %d wasn't requested!\n", gpio);
}

/*
 * ubi_port_setup()
 */
static void ubi_port_setup(unsigned gpio, unsigned short usage)
{
	if (!check_gpio(gpio)) {
		if (usage) {
			UBICOM32_GPIO_ENABLE(gpio);
		} else {
			UBICOM32_GPIO_DISABLE(gpio);
		}
	}
}

/*
 * ubi_gpio_request()
 */
static int ubi_gpio_request(struct gpio_chip *chip, unsigned gpio)
{
	unsigned long flags;

	if (check_gpio(gpio) < 0)
		return -EINVAL;

	local_irq_save(flags);

	if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
		printk(KERN_ERR "ubi-gpio: GPIO %d is already reserved!\n",
		       gpio);
		local_irq_restore(flags);
		return -EBUSY;
	}

	reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);

	ubi_port_setup(gpio, 1);

	local_irq_restore(flags);

	return 0;
}

/*
 * ubi_gpio_free()
 */
static void ubi_gpio_free(struct gpio_chip *chip, unsigned gpio)
{
	unsigned long flags;

	if (check_gpio(gpio) < 0)
		return;

	local_irq_save(flags);

	if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
		ubi_gpio_error(gpio);
		local_irq_restore(flags);
		return;
	}

	/* Assert the pin is no longer claimed */
	reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);

	/* Revert port bit to use specified by port->function */
	ubi_port_setup(gpio, 0);

	local_irq_restore(flags);
}

/*
 * ubi_gpio_direction_input()
 */
static int ubi_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
{
	unsigned long flags;

	if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
		ubi_gpio_error(gpio);
		return -EINVAL;
	}

	local_irq_save(flags);

	/* Configure pin as gpio */
	ubi_port_setup(gpio, 1);

	/* Assert pin is an input */
	UBICOM32_GPIO_SET_PIN_INPUT(gpio);

	local_irq_restore(flags);

	return 0;
}


/*
 * ubi_gpio_direction_output()
 */
static int ubi_gpio_direction_output(struct gpio_chip *chip,
				     unsigned gpio, int value)
{
	unsigned long flags;

	if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
		ubi_gpio_error(gpio);
		return -EINVAL;
	}

	local_irq_save(flags);

	/* Configure pin as gpio and set initial value in gpio_out register
	 * so that when we enable it as an output, it will have the correct
	 * initial value.
	 */
	ubi_port_setup(gpio, 1);
	if (value) {
		UBICOM32_GPIO_SET_PIN_HIGH(gpio);
	} else {
		UBICOM32_GPIO_SET_PIN_LOW(gpio);
	}

	/* Enable the pin as an output */
	UBICOM32_GPIO_SET_PIN_OUTPUT(gpio);

	local_irq_restore(flags);

	return 0;
}


/*
 * ubi_gpio_get_value()
 */
static int ubi_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
{
	return 0 != (gpio_bank_addr[gpio_bank(gpio)]->gpio_in & gpio_bit(gpio));
}


/*
 * ubi_gpio_set_value()
 */
static void ubi_gpio_set_value(struct gpio_chip *chip, unsigned gpio,
			       int arg)
{
	unsigned long flags;
	local_irq_save(flags);

	if (arg) {
		UBICOM32_GPIO_SET_PIN_HIGH(gpio);
	} else {
		UBICOM32_GPIO_SET_PIN_LOW(gpio);
	}

	local_irq_restore(flags);
}


/*
 * ubi_gpio_to_irq()
 */
static int ubi_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
{
	return gpio_to_irq(gpio);
}


/*
 * ubi_gpio_init()
 */
int __init ubi_gpio_init(void)
{
	int k;
	int status;
	struct ubi_gpio_chip *chip;
	struct gpio_chip *gc;

	printk(KERN_INFO "Ubicom GPIO Controller\n");

	chip = kzalloc(sizeof(struct ubi_gpio_chip), GFP_KERNEL);
	if (chip == NULL)
		return -ENOMEM;

	gc = &chip->gpio_chip;
	gc->request          = ubi_gpio_request;
	gc->free             = ubi_gpio_free;
	gc->to_irq           = ubi_gpio_to_irq;
	gc->direction_input  = ubi_gpio_direction_input;
	gc->direction_output = ubi_gpio_direction_output;
	gc->get              = ubi_gpio_get_value;
	gc->set              = ubi_gpio_set_value;
	gc->can_sleep        = 0;
	gc->base             = 0;
	gc->ngpio            = MAX_UBICOM_GPIOS; /* ARCH_NR_GPIOS - 1 */
	gc->label            = "ubi_gpio";

	status = gpiochip_add(gc);
	if (status != 0) {
		kfree(chip);
		return status;
	}

	/* Assert all pins are free */
	for (k = 0; k < NUM_GPIO_PORTS; k++) {
		reserved_gpio_map[k] = 0;
	}

	return 0;
}

#if defined(CONFIG_PROC_FS)
/*
 * ubi_get_gpio_dir()
 */
static int ubi_get_gpio_dir(unsigned gpio)
{
	if (gpio_bank_addr[gpio_bank(gpio)]->gpio_ctl & gpio_bit(gpio))
		return 1;
	else
		return 0;
}

/*
 * gpio_proc_read()
 */
static int ubi_gpio_proc_read(char *buf, char **start, off_t offset,
			  int len, int *unused_i, void *unused_v)
{
	int c, outlen = 0;

	for (c = 0; c < MAX_UBICOM_GPIOS; c++) {
		if (!check_gpio(c) &&
		    (reserved_gpio_map[gpio_bank(c)] & gpio_bit(c))) {
			len = sprintf(buf, "GPIO_%d:\t\tGPIO %s\n", c,
				      ubi_get_gpio_dir(c) ? "OUTPUT" : "INPUT");
		} else {
			continue;
		}

		buf += len;
		outlen += len;
	}
	return outlen;
}

/*
 * ubi_gpio_register_proc()
 */
static __init int ubi_gpio_register_proc(void)
{
	struct proc_dir_entry *proc_gpio;

	proc_gpio = create_proc_entry("gpio", S_IRUGO, NULL);
	if (proc_gpio)
		proc_gpio->read_proc = ubi_gpio_proc_read;

	return proc_gpio != NULL;
}
device_initcall(ubi_gpio_register_proc);
#endif