RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / arch / mips / brcm-boards / bcm947xx / gen_gpio.c
blobcbf8448b7d062ad0a2cd6a5e93c6f0b671158c9d
1 /*
2 * Generic GPIO
4 * Copyright (C) 2012, Broadcom Corporation. All Rights Reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
13 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
15 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * $Id: gen_gpio.c,v 1.1 2009-10-30 20:51:47 $
22 #include <linux/module.h>
23 #include <linux/init.h>
25 #include <typedefs.h>
26 #include <bcmutils.h>
27 #include <siutils.h>
28 #include <bcmdevs.h>
31 #define BCM947XX_GENGPIO_DEBUG 0
32 #if BCM947XX_GENGPIO_DEBUG
33 //#define DBG(x...) printk(KERN_DEBUG x)
34 #define DBG(x...) printk(KERN_ERR x)
35 #else
36 #define DBG(x...)
37 #endif
41 static si_t *gpio_sih;
42 int mask;
45 static int
46 gen_gpio_init(void)
48 if (!(gpio_sih = si_kattach(SI_OSH))) {
49 DBG("%s: si_kattach failed\n", __FUNCTION__);
50 return -ENODEV;
53 si_gpiosetcore(gpio_sih);
55 return 0;
58 static void
59 gen_gpio_exit(void)
61 si_detach(gpio_sih);
64 /* GENERIC_GPIO calls */
65 int gpio_direction_input(unsigned gpio)
67 int ret;
69 ret = si_gpioouten(gpio_sih, (1<<gpio), 0, GPIO_APP_PRIORITY);
70 DBG("%s: gpio %d - input 0x%x\n", __FUNCTION__, gpio, ret);
71 return 0;
73 EXPORT_SYMBOL(gpio_direction_input);
76 int gpio_direction_output(unsigned gpio, int value)
78 int out, outen;
80 outen = si_gpioouten(gpio_sih, (1<<gpio), (1<<gpio), GPIO_APP_PRIORITY);
81 out = si_gpioout(gpio_sih, (1<<gpio), (value ? (1<<gpio) : 0), GPIO_APP_PRIORITY);
82 DBG("%s: gpio %d, value %d - out 0x%x outen 0x%x\n", __FUNCTION__, gpio, value, out, outen);
83 return 0;
85 EXPORT_SYMBOL(gpio_direction_output);
87 int gpio_get_value(unsigned int gpio)
89 uint32 get;
90 get = si_gpioin(gpio_sih);
92 get &= (1<<gpio);
94 return (get ? 1 : 0);
96 EXPORT_SYMBOL(gpio_get_value);
98 void gpio_set_value(unsigned int gpio, int value)
100 si_gpioout(gpio_sih, (1<<gpio), (value ? (1<<gpio) : 0), GPIO_APP_PRIORITY);
101 return;
103 EXPORT_SYMBOL(gpio_set_value);
105 int gpio_request(unsigned int gpio, const char *label)
107 int ret;
109 mask |= (1<<gpio);
111 ret = si_gpioreserve(gpio_sih, (1<<gpio), GPIO_APP_PRIORITY);
112 DBG("%s: gpio %d label %s mask 0x%x reserve 0x%x\n", __FUNCTION__, gpio,
113 label, mask, ret);
115 ret = si_gpiocontrol(gpio_sih, (1<<gpio), 0, GPIO_APP_PRIORITY);
116 DBG("%s: si_gpiocontrol 0x%x\n", __FUNCTION__, ret);
118 /* clear pulldown */
119 ret = si_gpiopull(gpio_sih, 1/*pulldown*/, (1<<gpio), 0);
120 DBG("%s: si_gpiopull (down) 0x%x\n", __FUNCTION__, ret);
121 /* Set pullup */
122 ret = si_gpiopull(gpio_sih, 0/*pullup*/, (1<<gpio), (1<<gpio));
123 DBG("%s: si_gpiopull (up) 0x%x\n", __FUNCTION__, ret);
125 return 0;
127 EXPORT_SYMBOL(gpio_request);
129 void gpio_free(unsigned int gpio)
131 mask &= ~(1<<gpio);
133 /* clear pullup */
134 si_gpiopull(gpio_sih, 0/*pullup*/, (1<<gpio), GPIO_APP_PRIORITY);
135 si_gpiorelease(gpio_sih, (1<<gpio), GPIO_APP_PRIORITY);
137 DBG("%s: gpio %d mask 0x%x\n", __FUNCTION__, gpio, mask);
138 return;
140 EXPORT_SYMBOL(gpio_free);
143 module_init(gen_gpio_init);
144 module_exit(gen_gpio_exit);