2 * arch/ubicom32/mach-common/ubi32-gpio.c
5 * (C) Copyright 2009, Ubicom, Inc.
7 * This file is part of the Ubicom32 Linux Kernel Port.
9 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
10 * it and/or modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation, either version 2 of the
12 * License, or (at your option) any later version.
14 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
15 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with the Ubicom32 Linux Kernel Port. If not,
21 * see <http://www.gnu.org/licenses/>.
23 * Ubicom32 implementation derived from (with many thanks):
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
34 #include <linux/gpio.h>
35 #include <linux/irq.h>
36 #include <linux/version.h>
38 #if defined(CONFIG_PROC_FS)
39 #include <linux/proc_fs.h>
43 #include <asm/ip5000.h>
44 #include <linux/gpio.h>
46 #define UBI_GPIO_CHECK_RANGE 0 /* !0 enables range checking */
50 * Each I/O port can be configured to operate in one of several
51 * functional modes. One of these modes is GPIO, which causes the
52 * entire port to function as a GPIO port. Since the various port
53 * registers serve the system with other important functions, such as
54 * ethernet, serial, USB, etc., it isn't advantageous to set any of
55 * the ports to be entirely dedicated for GPIO use. The processor
56 * alternatively allows individual bits of a port to be assigned to be
57 * used as GPIO independently from the overall port function. This
58 * bit-by-bit assignment is selected by setting the corresponding bit
59 * in the port's gpio_mask register. When set, the selected bit is
60 * then enabled as a GPIO. If the corresponding bit is set in the
61 * gpio_ctl register of the port, the bit is configured as a GPIO
62 * output. Otherwise, it is an input.
64 * NOTE: This driver uses the bit-by-bit GPIO function assignment
65 * exclusively and *never* sets the port function registers to the
68 * GPIO is not the main function of any of the I/O ports. The port
69 * bit widths are variable from one port to the next, determined by
70 * the more common I/O functions of the ports. For simplicity, this
71 * driver assumes all the ports are 32 bits wide regardless of the
72 * real bit width of the port. GPIO bits are numbered from zero to
73 * MAX_UBICOM_GPIOS. Within a port, the least significant bit is
74 * numbered bit zero, the most significant is bit 31. Since the ports
75 * are considered logically contiguous, GPIO #32 is the zeroth bit in
76 * port #1, and so on. Due to the hardware definition, there are
77 * large gaps in the GPIO numbers representing real pins.
79 * NOTE: It is up to the programmer to refer to the processor data
80 * sheet to determine which bits in which ports can be accessed and
86 /* There are 9 ports, A through I. Not all 32 bits in each
87 * port can be a GPIO, but we pretend they are. Its up to the
88 * programmer to refer to the processor data sheet.
90 #define MAX_UBICOM_GPIOS (9 * 32) /* ARCH_NR_GPIOS */
91 #define NUM_GPIO_PORTS (gpio_bank(MAX_UBICOM_GPIOS))
94 /* GPIO reservation bit map array */
95 static int reserved_gpio_map[NUM_GPIO_PORTS];
98 /* Array of hardware io_port addresses */
99 static struct ubicom32_io_port *gpio_bank_addr[NUM_GPIO_PORTS] =
101 UBICOM32_IO_PORT(RA),
102 UBICOM32_IO_PORT(RB),
103 UBICOM32_IO_PORT(RC),
104 UBICOM32_IO_PORT(RD),
105 UBICOM32_IO_PORT(RE),
106 UBICOM32_IO_PORT(RF),
107 UBICOM32_IO_PORT(RG),
108 UBICOM32_IO_PORT(RH),
113 struct ubi_gpio_chip {
115 * Right now, nothing else lives here.
117 struct gpio_chip gpio_chip;
121 #if UBI_GPIO_CHECK_RANGE
122 inline int check_gpio(unsigned gpio)
124 if (gpio >= MAX_UBICOM_GPIOS)
129 #define check_gpio(n) (0)
134 * Get the IO port associated with a certain gpio
136 struct ubicom32_io_port *ubi_gpio_get_port(unsigned gpio)
138 if (gpio_bank(gpio) > NUM_GPIO_PORTS) {
141 return gpio_bank_addr[gpio_bank(gpio)];
147 static void ubi_gpio_error(unsigned gpio)
149 printk(KERN_ERR "ubicom-gpio: GPIO %d wasn't requested!\n", gpio);
155 static void ubi_port_setup(unsigned gpio, unsigned short usage)
157 if (!check_gpio(gpio)) {
159 UBICOM32_GPIO_ENABLE(gpio);
161 UBICOM32_GPIO_DISABLE(gpio);
169 static int ubi_gpio_request(struct gpio_chip *chip, unsigned gpio)
173 if (check_gpio(gpio) < 0)
176 local_irq_save(flags);
178 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
179 printk(KERN_ERR "ubi-gpio: GPIO %d is already reserved!\n",
181 local_irq_restore(flags);
185 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
187 ubi_port_setup(gpio, 1);
189 local_irq_restore(flags);
197 static void ubi_gpio_free(struct gpio_chip *chip, unsigned gpio)
201 if (check_gpio(gpio) < 0)
204 local_irq_save(flags);
206 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
207 ubi_gpio_error(gpio);
208 local_irq_restore(flags);
212 /* Assert the pin is no longer claimed */
213 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
215 /* Revert port bit to use specified by port->function */
216 ubi_port_setup(gpio, 0);
218 local_irq_restore(flags);
222 * ubi_gpio_direction_input()
224 static int ubi_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
228 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
229 ubi_gpio_error(gpio);
233 local_irq_save(flags);
235 /* Configure pin as gpio */
236 ubi_port_setup(gpio, 1);
238 /* Assert pin is an input */
239 UBICOM32_GPIO_SET_PIN_INPUT(gpio);
241 local_irq_restore(flags);
248 * ubi_gpio_direction_output()
250 static int ubi_gpio_direction_output(struct gpio_chip *chip,
251 unsigned gpio, int value)
255 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
256 ubi_gpio_error(gpio);
260 local_irq_save(flags);
262 /* Configure pin as gpio and set initial value in gpio_out register
263 * so that when we enable it as an output, it will have the correct
266 ubi_port_setup(gpio, 1);
268 UBICOM32_GPIO_SET_PIN_HIGH(gpio);
270 UBICOM32_GPIO_SET_PIN_LOW(gpio);
273 /* Enable the pin as an output */
274 UBICOM32_GPIO_SET_PIN_OUTPUT(gpio);
276 local_irq_restore(flags);
283 * ubi_gpio_get_value()
285 static int ubi_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
287 return 0 != (gpio_bank_addr[gpio_bank(gpio)]->gpio_in & gpio_bit(gpio));
292 * ubi_gpio_set_value()
294 static void ubi_gpio_set_value(struct gpio_chip *chip, unsigned gpio,
298 local_irq_save(flags);
301 UBICOM32_GPIO_SET_PIN_HIGH(gpio);
303 UBICOM32_GPIO_SET_PIN_LOW(gpio);
306 local_irq_restore(flags);
313 static int ubi_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
315 return gpio_to_irq(gpio);
322 int __init ubi_gpio_init(void)
326 struct ubi_gpio_chip *chip;
327 struct gpio_chip *gc;
329 printk(KERN_INFO "Ubicom GPIO Controller\n");
331 chip = kzalloc(sizeof(struct ubi_gpio_chip), GFP_KERNEL);
335 gc = &chip->gpio_chip;
336 gc->request = ubi_gpio_request;
337 gc->free = ubi_gpio_free;
338 gc->to_irq = ubi_gpio_to_irq;
339 gc->direction_input = ubi_gpio_direction_input;
340 gc->direction_output = ubi_gpio_direction_output;
341 gc->get = ubi_gpio_get_value;
342 gc->set = ubi_gpio_set_value;
345 gc->ngpio = MAX_UBICOM_GPIOS; /* ARCH_NR_GPIOS - 1 */
346 gc->label = "ubi_gpio";
348 status = gpiochip_add(gc);
354 /* Assert all pins are free */
355 for (k = 0; k < NUM_GPIO_PORTS; k++) {
356 reserved_gpio_map[k] = 0;
362 #if defined(CONFIG_PROC_FS)
366 static int ubi_get_gpio_dir(unsigned gpio)
368 if (gpio_bank_addr[gpio_bank(gpio)]->gpio_ctl & gpio_bit(gpio))
377 static int ubi_gpio_proc_read(char *buf, char **start, off_t offset,
378 int len, int *unused_i, void *unused_v)
382 for (c = 0; c < MAX_UBICOM_GPIOS; c++) {
383 if (!check_gpio(c) &&
384 (reserved_gpio_map[gpio_bank(c)] & gpio_bit(c))) {
385 len = sprintf(buf, "GPIO_%d:\t\tGPIO %s\n", c,
386 ubi_get_gpio_dir(c) ? "OUTPUT" : "INPUT");
398 * ubi_gpio_register_proc()
400 static __init int ubi_gpio_register_proc(void)
402 struct proc_dir_entry *proc_gpio;
404 proc_gpio = create_proc_entry("gpio", S_IRUGO, NULL);
406 proc_gpio->read_proc = ubi_gpio_proc_read;
408 return proc_gpio != NULL;
410 device_initcall(ubi_gpio_register_proc);