4 * Copyright (C) 2008, Volker Weiss <dev@tintuc.de>
5 * Copyright (C) 2007-2010 Florian Fainelli <florian@openwrt.org>
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.
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.
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.
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>
30 #include <asm/rdc321x_defs.h>
37 extern int rdc321x_pci_write(int reg
, u32 val
);
38 extern int rdc321x_pci_read(int reg
, u32
*val
);
41 static int rdc_gpio_get_value(struct gpio_chip
*chip
, unsigned gpio
)
46 reg
= gpio
< 32 ? RDC321X_GPIO_DATA_REG1
: RDC321X_GPIO_DATA_REG2
;
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
);
53 return (1 << (gpio
& 0x1f)) & value
? 1 : 0;
56 static void rdc_gpio_set_value_impl(struct gpio_chip
*chip
,
57 unsigned gpio
, int value
)
59 int reg
= (gpio
< 32) ? 0 : 1;
62 rdc321x_gpio_dev
.data_reg
[reg
] |= 1 << (gpio
& 0x1f);
64 rdc321x_gpio_dev
.data_reg
[reg
] &= ~(1 << (gpio
& 0x1f));
66 rdc321x_pci_write(reg
? RDC321X_GPIO_DATA_REG2
: RDC321X_GPIO_DATA_REG1
,
67 rdc321x_gpio_dev
.data_reg
[reg
]);
70 /* set GPIO pin to value */
71 static void rdc_gpio_set_value(struct gpio_chip
*chip
,
72 unsigned gpio
, int value
)
74 spin_lock(&rdc321x_gpio_dev
.lock
);
75 rdc_gpio_set_value_impl(chip
, gpio
, value
);
76 spin_unlock(&rdc321x_gpio_dev
.lock
);
79 static int rdc_gpio_config(struct gpio_chip
*chip
,
80 unsigned gpio
, int value
)
85 spin_lock(&rdc321x_gpio_dev
.lock
);
86 err
= rdc321x_pci_read(gpio
< 32 ? RDC321X_GPIO_CTRL_REG1
: RDC321X_GPIO_CTRL_REG2
,
91 reg
|= 1 << (gpio
& 0x1f);
93 err
= rdc321x_pci_write(gpio
< 32 ? RDC321X_GPIO_CTRL_REG1
: RDC321X_GPIO_CTRL_REG2
,
98 rdc_gpio_set_value_impl(chip
, gpio
, value
);
101 spin_unlock(&rdc321x_gpio_dev
.lock
);
106 /* configure GPIO pin as input */
107 static int rdc_gpio_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
109 return rdc_gpio_config(chip
, gpio
, 1);
112 static 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
,
119 .ngpio
= RDC321X_MAX_GPIO
,
122 /* initially setup the 2 copies of the gpio data registers.
123 This function is called before the platform setup code. */
124 static int __devinit
rdc321x_gpio_probe(struct platform_device
*pdev
)
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]);
135 err
= rdc321x_pci_read(RDC321X_GPIO_DATA_REG2
, &rdc321x_gpio_dev
.data_reg
[1]);
139 spin_lock_init(&rdc321x_gpio_dev
.lock
);
141 printk(KERN_INFO
"rdc321x: registering %d GPIOs\n", rdc321x_gpio_chip
.ngpio
);
142 return gpiochip_add(&rdc321x_gpio_chip
);
145 static int __devexit
rdc321x_gpio_remove(struct platform_device
*pdev
)
147 gpiochip_remove(&rdc321x_gpio_chip
);
151 static 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
),
158 static int __init
rdc321x_gpio_init(void)
160 return platform_driver_register(&rdc321x_gpio_driver
);
163 static void __exit
rdc321x_gpio_exit(void)
165 platform_driver_unregister(&rdc321x_gpio_driver
);
168 module_init(rdc321x_gpio_init
);
169 module_exit(rdc321x_gpio_exit
);
171 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
172 MODULE_DESCRIPTION("RDC321x GPIO driver");
173 MODULE_LICENSE("GPL");
174 MODULE_ALIAS("platform:rdc321x-gpio");