Merge branch 'rmobile-latest' of git://git.kernel.org/pub/scm/linux/kernel/git/lethal...
[pandora-kernel.git] / drivers / gpio / 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 #include <linux/mfd/rdc321x.h>
30 #include <linux/mfd/core.h>
31 #include <linux/slab.h>
32
33 struct rdc321x_gpio {
34         spinlock_t              lock;
35         struct pci_dev          *sb_pdev;
36         u32                     data_reg[2];
37         int                     reg1_ctrl_base;
38         int                     reg1_data_base;
39         int                     reg2_ctrl_base;
40         int                     reg2_data_base;
41         struct gpio_chip        chip;
42 };
43
44 /* read GPIO pin */
45 static int rdc_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
46 {
47         struct rdc321x_gpio *gpch;
48         u32 value = 0;
49         int reg;
50
51         gpch = container_of(chip, struct rdc321x_gpio, chip);
52         reg = gpio < 32 ? gpch->reg1_data_base : gpch->reg2_data_base;
53
54         spin_lock(&gpch->lock);
55         pci_write_config_dword(gpch->sb_pdev, reg,
56                                         gpch->data_reg[gpio < 32 ? 0 : 1]);
57         pci_read_config_dword(gpch->sb_pdev, reg, &value);
58         spin_unlock(&gpch->lock);
59
60         return (1 << (gpio & 0x1f)) & value ? 1 : 0;
61 }
62
63 static void rdc_gpio_set_value_impl(struct gpio_chip *chip,
64                                 unsigned gpio, int value)
65 {
66         struct rdc321x_gpio *gpch;
67         int reg = (gpio < 32) ? 0 : 1;
68
69         gpch = container_of(chip, struct rdc321x_gpio, chip);
70
71         if (value)
72                 gpch->data_reg[reg] |= 1 << (gpio & 0x1f);
73         else
74                 gpch->data_reg[reg] &= ~(1 << (gpio & 0x1f));
75
76         pci_write_config_dword(gpch->sb_pdev,
77                         reg ? gpch->reg2_data_base : gpch->reg1_data_base,
78                         gpch->data_reg[reg]);
79 }
80
81 /* set GPIO pin to value */
82 static void rdc_gpio_set_value(struct gpio_chip *chip,
83                                 unsigned gpio, int value)
84 {
85         struct rdc321x_gpio *gpch;
86
87         gpch = container_of(chip, struct rdc321x_gpio, chip);
88         spin_lock(&gpch->lock);
89         rdc_gpio_set_value_impl(chip, gpio, value);
90         spin_unlock(&gpch->lock);
91 }
92
93 static int rdc_gpio_config(struct gpio_chip *chip,
94                                 unsigned gpio, int value)
95 {
96         struct rdc321x_gpio *gpch;
97         int err;
98         u32 reg;
99
100         gpch = container_of(chip, struct rdc321x_gpio, chip);
101
102         spin_lock(&gpch->lock);
103         err = pci_read_config_dword(gpch->sb_pdev, gpio < 32 ?
104                         gpch->reg1_ctrl_base : gpch->reg2_ctrl_base, &reg);
105         if (err)
106                 goto unlock;
107
108         reg |= 1 << (gpio & 0x1f);
109
110         err = pci_write_config_dword(gpch->sb_pdev, gpio < 32 ?
111                         gpch->reg1_ctrl_base : gpch->reg2_ctrl_base, reg);
112         if (err)
113                 goto unlock;
114
115         rdc_gpio_set_value_impl(chip, gpio, value);
116
117 unlock:
118         spin_unlock(&gpch->lock);
119
120         return err;
121 }
122
123 /* configure GPIO pin as input */
124 static int rdc_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
125 {
126         return rdc_gpio_config(chip, gpio, 1);
127 }
128
129 /*
130  * Cache the initial value of both GPIO data registers
131  */
132 static int __devinit rdc321x_gpio_probe(struct platform_device *pdev)
133 {
134         int err;
135         struct resource *r;
136         struct rdc321x_gpio *rdc321x_gpio_dev;
137         struct rdc321x_gpio_pdata *pdata;
138
139         pdata = mfd_get_data(pdev);
140         if (!pdata) {
141                 dev_err(&pdev->dev, "no platform data supplied\n");
142                 return -ENODEV;
143         }
144
145         rdc321x_gpio_dev = kzalloc(sizeof(struct rdc321x_gpio), GFP_KERNEL);
146         if (!rdc321x_gpio_dev) {
147                 dev_err(&pdev->dev, "failed to allocate private data\n");
148                 return -ENOMEM;
149         }
150
151         r = platform_get_resource_byname(pdev, IORESOURCE_IO, "gpio-reg1");
152         if (!r) {
153                 dev_err(&pdev->dev, "failed to get gpio-reg1 resource\n");
154                 err = -ENODEV;
155                 goto out_free;
156         }
157
158         spin_lock_init(&rdc321x_gpio_dev->lock);
159         rdc321x_gpio_dev->sb_pdev = pdata->sb_pdev;
160         rdc321x_gpio_dev->reg1_ctrl_base = r->start;
161         rdc321x_gpio_dev->reg1_data_base = r->start + 0x4;
162
163         r = platform_get_resource_byname(pdev, IORESOURCE_IO, "gpio-reg2");
164         if (!r) {
165                 dev_err(&pdev->dev, "failed to get gpio-reg2 resource\n");
166                 err = -ENODEV;
167                 goto out_free;
168         }
169
170         rdc321x_gpio_dev->reg2_ctrl_base = r->start;
171         rdc321x_gpio_dev->reg2_data_base = r->start + 0x4;
172
173         rdc321x_gpio_dev->chip.label = "rdc321x-gpio";
174         rdc321x_gpio_dev->chip.direction_input = rdc_gpio_direction_input;
175         rdc321x_gpio_dev->chip.direction_output = rdc_gpio_config;
176         rdc321x_gpio_dev->chip.get = rdc_gpio_get_value;
177         rdc321x_gpio_dev->chip.set = rdc_gpio_set_value;
178         rdc321x_gpio_dev->chip.base = 0;
179         rdc321x_gpio_dev->chip.ngpio = pdata->max_gpios;
180
181         platform_set_drvdata(pdev, rdc321x_gpio_dev);
182
183         /* This might not be, what others (BIOS, bootloader, etc.)
184            wrote to these registers before, but it's a good guess. Still
185            better than just using 0xffffffff. */
186         err = pci_read_config_dword(rdc321x_gpio_dev->sb_pdev,
187                                         rdc321x_gpio_dev->reg1_data_base,
188                                         &rdc321x_gpio_dev->data_reg[0]);
189         if (err)
190                 goto out_drvdata;
191
192         err = pci_read_config_dword(rdc321x_gpio_dev->sb_pdev,
193                                         rdc321x_gpio_dev->reg2_data_base,
194                                         &rdc321x_gpio_dev->data_reg[1]);
195         if (err)
196                 goto out_drvdata;
197
198         dev_info(&pdev->dev, "registering %d GPIOs\n",
199                                         rdc321x_gpio_dev->chip.ngpio);
200         return gpiochip_add(&rdc321x_gpio_dev->chip);
201
202 out_drvdata:
203         platform_set_drvdata(pdev, NULL);
204 out_free:
205         kfree(rdc321x_gpio_dev);
206         return err;
207 }
208
209 static int __devexit rdc321x_gpio_remove(struct platform_device *pdev)
210 {
211         int ret;
212         struct rdc321x_gpio *rdc321x_gpio_dev = platform_get_drvdata(pdev);
213
214         ret = gpiochip_remove(&rdc321x_gpio_dev->chip);
215         if (ret)
216                 dev_err(&pdev->dev, "failed to unregister chip\n");
217
218         kfree(rdc321x_gpio_dev);
219         platform_set_drvdata(pdev, NULL);
220
221         return ret;
222 }
223
224 static struct platform_driver rdc321x_gpio_driver = {
225         .driver.name    = "rdc321x-gpio",
226         .driver.owner   = THIS_MODULE,
227         .probe          = rdc321x_gpio_probe,
228         .remove         = __devexit_p(rdc321x_gpio_remove),
229 };
230
231 static int __init rdc321x_gpio_init(void)
232 {
233         return platform_driver_register(&rdc321x_gpio_driver);
234 }
235
236 static void __exit rdc321x_gpio_exit(void)
237 {
238         platform_driver_unregister(&rdc321x_gpio_driver);
239 }
240
241 module_init(rdc321x_gpio_init);
242 module_exit(rdc321x_gpio_exit);
243
244 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
245 MODULE_DESCRIPTION("RDC321x GPIO driver");
246 MODULE_LICENSE("GPL");
247 MODULE_ALIAS("platform:rdc321x-gpio");