Root/target/linux/rdc/files-2.6.30/arch/x86/mach-rdc321x/gpio.c

1/*
2 * RDC321x GPIO driver
3 *
4 * Copyright (C) 2008, Volker Weiss <dev@tintuc.de>
5 * Copyright (C) 2007-2010 Florian Fainelli <florian@openwrt.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/init.h>
25#include <linux/spinlock.h>
26#include <linux/platform_device.h>
27#include <linux/pci.h>
28#include <linux/gpio.h>
29
30#include <asm/rdc321x_defs.h>
31
32struct rdc321x_gpio {
33    spinlock_t lock;
34    u32 data_reg[2];
35} rdc321x_gpio_dev;
36
37extern int rdc321x_pci_write(int reg, u32 val);
38extern int rdc321x_pci_read(int reg, u32 *val);
39
40/* read GPIO pin */
41static int rdc_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
42{
43    u32 value = 0;
44    int reg;
45
46    reg = gpio < 32 ? RDC321X_GPIO_DATA_REG1 : RDC321X_GPIO_DATA_REG2;
47
48    spin_lock(&rdc321x_gpio_dev.lock);
49    rdc321x_pci_write(reg, rdc321x_gpio_dev.data_reg[gpio < 32 ? 0 : 1]);
50    rdc321x_pci_read(reg, &value);
51    spin_unlock(&rdc321x_gpio_dev.lock);
52
53    return (1 << (gpio & 0x1f)) & value ? 1 : 0;
54}
55
56static void rdc_gpio_set_value_impl(struct gpio_chip *chip,
57                unsigned gpio, int value)
58{
59    int reg = (gpio < 32) ? 0 : 1;
60
61    if (value)
62        rdc321x_gpio_dev.data_reg[reg] |= 1 << (gpio & 0x1f);
63    else
64        rdc321x_gpio_dev.data_reg[reg] &= ~(1 << (gpio & 0x1f));
65
66    rdc321x_pci_write(reg ? RDC321X_GPIO_DATA_REG2 : RDC321X_GPIO_DATA_REG1,
67                   rdc321x_gpio_dev.data_reg[reg]);
68}
69
70/* set GPIO pin to value */
71static void rdc_gpio_set_value(struct gpio_chip *chip,
72                unsigned gpio, int value)
73{
74    spin_lock(&rdc321x_gpio_dev.lock);
75    rdc_gpio_set_value_impl(chip, gpio, value);
76    spin_unlock(&rdc321x_gpio_dev.lock);
77}
78
79static int rdc_gpio_config(struct gpio_chip *chip,
80                unsigned gpio, int value)
81{
82    int err;
83    u32 reg;
84
85    spin_lock(&rdc321x_gpio_dev.lock);
86    err = rdc321x_pci_read(gpio < 32 ? RDC321X_GPIO_CTRL_REG1 : RDC321X_GPIO_CTRL_REG2,
87                                        &reg);
88    if (err)
89        goto unlock;
90
91    reg |= 1 << (gpio & 0x1f);
92
93    err = rdc321x_pci_write(gpio < 32 ? RDC321X_GPIO_CTRL_REG1 : RDC321X_GPIO_CTRL_REG2,
94                                        reg);
95    if (err)
96        goto unlock;
97
98    rdc_gpio_set_value_impl(chip, gpio, value);
99
100unlock:
101    spin_unlock(&rdc321x_gpio_dev.lock);
102
103    return err;
104}
105
106/* configure GPIO pin as input */
107static int rdc_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
108{
109    return rdc_gpio_config(chip, gpio, 1);
110}
111
112static struct gpio_chip rdc321x_gpio_chip = {
113    .label = "rdc321x-gpio",
114    .direction_input = rdc_gpio_direction_input,
115    .direction_output = rdc_gpio_config,
116    .get = rdc_gpio_get_value,
117    .set = rdc_gpio_set_value,
118    .base = 0,
119    .ngpio = RDC321X_MAX_GPIO,
120};
121
122/* initially setup the 2 copies of the gpio data registers.
123   This function is called before the platform setup code. */
124static int __devinit rdc321x_gpio_probe(struct platform_device *pdev)
125{
126    int err;
127
128    /* this might not be, what others (BIOS, bootloader, etc.)
129       wrote to these registers before, but it's a good guess. Still
130       better than just using 0xffffffff. */
131    err = rdc321x_pci_read(RDC321X_GPIO_DATA_REG1, &rdc321x_gpio_dev.data_reg[0]);
132    if (err)
133        return err;
134
135    err = rdc321x_pci_read(RDC321X_GPIO_DATA_REG2, &rdc321x_gpio_dev.data_reg[1]);
136    if (err)
137        return err;
138
139    spin_lock_init(&rdc321x_gpio_dev.lock);
140
141    printk(KERN_INFO "rdc321x: registering %d GPIOs\n", rdc321x_gpio_chip.ngpio);
142    return gpiochip_add(&rdc321x_gpio_chip);
143}
144
145static int __devexit rdc321x_gpio_remove(struct platform_device *pdev)
146{
147    gpiochip_remove(&rdc321x_gpio_chip);
148    return 0;
149}
150
151static struct platform_driver rdc321x_gpio_driver = {
152    .driver.name = "rdc321x-gpio",
153    .driver.owner = THIS_MODULE,
154    .probe = rdc321x_gpio_probe,
155    .remove = __devexit_p(rdc321x_gpio_remove),
156};
157
158static int __init rdc321x_gpio_init(void)
159{
160    return platform_driver_register(&rdc321x_gpio_driver);
161}
162
163static void __exit rdc321x_gpio_exit(void)
164{
165    platform_driver_unregister(&rdc321x_gpio_driver);
166}
167
168module_init(rdc321x_gpio_init);
169module_exit(rdc321x_gpio_exit);
170
171MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
172MODULE_DESCRIPTION("RDC321x GPIO driver");
173MODULE_LICENSE("GPL");
174MODULE_ALIAS("platform:rdc321x-gpio");
175

Archive Download this file



interactive