2 * Linux GPIOlib driver for the VIA VX855 integrated southbridge GPIO
4 * Copyright (C) 2009 VIA Technologies, Inc.
5 * Copyright (C) 2010 One Laptop per Child
6 * Author: Harald Welte <HaraldWelte@viatech.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/gpio.h>
29 #include <linux/slab.h>
30 #include <linux/device.h>
31 #include <linux/platform_device.h>
32 #include <linux/pci.h>
35 #define MODULE_NAME "vx855_gpio"
37 /* The VX855 south bridge has the following GPIO pins:
38 * GPI 0...13 General Purpose Input
39 * GPO 0...12 General Purpose Output
40 * GPIO 0...14 General Purpose I/O (Open-Drain)
43 #define NR_VX855_GPI 14
44 #define NR_VX855_GPO 13
45 #define NR_VX855_GPIO 15
47 #define NR_VX855_GPInO (NR_VX855_GPI + NR_VX855_GPO)
48 #define NR_VX855_GP (NR_VX855_GPI + NR_VX855_GPO + NR_VX855_GPIO)
51 struct gpio_chip gpio
;
59 /* resolve a GPIx into the corresponding bit position */
60 static inline u_int32_t
gpi_i_bit(int i
)
68 static inline u_int32_t
gpo_o_bit(int i
)
76 static inline u_int32_t
gpio_i_bit(int i
)
84 static inline u_int32_t
gpio_o_bit(int i
)
92 /* Mapping betwee numeric GPIO ID and the actual GPIO hardware numbering:
98 static int vx855gpio_direction_input(struct gpio_chip
*gpio
,
101 struct vx855_gpio
*vg
= container_of(gpio
, struct vx855_gpio
, gpio
);
105 /* Real GPI bits are always in input direction */
106 if (nr
< NR_VX855_GPI
)
109 /* Real GPO bits cannot be put in output direction */
110 if (nr
< NR_VX855_GPInO
)
113 /* Open Drain GPIO have to be set to one */
114 spin_lock_irqsave(&vg
->lock
, flags
);
115 reg_out
= inl(vg
->io_gpo
);
116 reg_out
|= gpio_o_bit(nr
- NR_VX855_GPInO
);
117 outl(reg_out
, vg
->io_gpo
);
118 spin_unlock_irqrestore(&vg
->lock
, flags
);
123 static int vx855gpio_get(struct gpio_chip
*gpio
, unsigned int nr
)
125 struct vx855_gpio
*vg
= container_of(gpio
, struct vx855_gpio
, gpio
);
129 if (nr
< NR_VX855_GPI
) {
130 reg_in
= inl(vg
->io_gpi
);
131 if (reg_in
& gpi_i_bit(nr
))
133 } else if (nr
< NR_VX855_GPInO
) {
134 /* GPO don't have an input bit, we need to read it
135 * back from the output register */
136 reg_in
= inl(vg
->io_gpo
);
137 if (reg_in
& gpo_o_bit(nr
- NR_VX855_GPI
))
140 reg_in
= inl(vg
->io_gpi
);
141 if (reg_in
& gpio_i_bit(nr
- NR_VX855_GPInO
))
148 static void vx855gpio_set(struct gpio_chip
*gpio
, unsigned int nr
,
151 struct vx855_gpio
*vg
= container_of(gpio
, struct vx855_gpio
, gpio
);
155 /* True GPI cannot be switched to output mode */
156 if (nr
< NR_VX855_GPI
)
159 spin_lock_irqsave(&vg
->lock
, flags
);
160 reg_out
= inl(vg
->io_gpo
);
161 if (nr
< NR_VX855_GPInO
) {
163 reg_out
|= gpo_o_bit(nr
- NR_VX855_GPI
);
165 reg_out
&= ~gpo_o_bit(nr
- NR_VX855_GPI
);
168 reg_out
|= gpio_o_bit(nr
- NR_VX855_GPInO
);
170 reg_out
&= ~gpio_o_bit(nr
- NR_VX855_GPInO
);
172 outl(reg_out
, vg
->io_gpo
);
173 spin_unlock_irqrestore(&vg
->lock
, flags
);
176 static int vx855gpio_direction_output(struct gpio_chip
*gpio
,
177 unsigned int nr
, int val
)
179 /* True GPI cannot be switched to output mode */
180 if (nr
< NR_VX855_GPI
)
183 /* True GPO don't need to be switched to output mode,
184 * and GPIO are open-drain, i.e. also need no switching,
185 * so all we do is set the level */
186 vx855gpio_set(gpio
, nr
, val
);
191 static const char *vx855gpio_names
[NR_VX855_GP
] = {
192 "VX855_GPI0", "VX855_GPI1", "VX855_GPI2", "VX855_GPI3", "VX855_GPI4",
193 "VX855_GPI5", "VX855_GPI6", "VX855_GPI7", "VX855_GPI8", "VX855_GPI9",
194 "VX855_GPI10", "VX855_GPI11", "VX855_GPI12", "VX855_GPI13",
195 "VX855_GPO0", "VX855_GPO1", "VX855_GPO2", "VX855_GPO3", "VX855_GPO4",
196 "VX855_GPO5", "VX855_GPO6", "VX855_GPO7", "VX855_GPO8", "VX855_GPO9",
197 "VX855_GPO10", "VX855_GPO11", "VX855_GPO12",
198 "VX855_GPIO0", "VX855_GPIO1", "VX855_GPIO2", "VX855_GPIO3",
199 "VX855_GPIO4", "VX855_GPIO5", "VX855_GPIO6", "VX855_GPIO7",
200 "VX855_GPIO8", "VX855_GPIO9", "VX855_GPIO10", "VX855_GPIO11",
201 "VX855_GPIO12", "VX855_GPIO13", "VX855_GPIO14"
204 static void vx855gpio_gpio_setup(struct vx855_gpio
*vg
)
206 struct gpio_chip
*c
= &vg
->gpio
;
208 c
->label
= "VX855 South Bridge";
209 c
->owner
= THIS_MODULE
;
210 c
->direction_input
= vx855gpio_direction_input
;
211 c
->direction_output
= vx855gpio_direction_output
;
212 c
->get
= vx855gpio_get
;
213 c
->set
= vx855gpio_set
;
216 c
->ngpio
= NR_VX855_GP
;
218 c
->names
= vx855gpio_names
;
221 /* This platform device is ordinarily registered by the vx855 mfd driver */
222 static __devinit
int vx855gpio_probe(struct platform_device
*pdev
)
224 struct resource
*res_gpi
;
225 struct resource
*res_gpo
;
226 struct vx855_gpio
*vg
;
229 res_gpi
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
230 res_gpo
= platform_get_resource(pdev
, IORESOURCE_IO
, 1);
231 if (!res_gpi
|| !res_gpo
)
234 vg
= kzalloc(sizeof(*vg
), GFP_KERNEL
);
238 platform_set_drvdata(pdev
, vg
);
240 dev_info(&pdev
->dev
, "found VX855 GPIO controller\n");
241 vg
->io_gpi
= res_gpi
->start
;
242 vg
->io_gpo
= res_gpo
->start
;
243 spin_lock_init(&vg
->lock
);
246 * A single byte is used to control various GPIO ports on the VX855,
247 * and in the case of the OLPC XO-1.5, some of those ports are used
248 * for switches that are interpreted and exposed through ACPI. ACPI
249 * will have reserved the region, so our own reservation will not
250 * succeed. Ignore and continue.
253 if (!request_region(res_gpi
->start
, resource_size(res_gpi
),
256 "GPI I/O resource busy, probably claimed by ACPI\n");
258 vg
->gpi_reserved
= true;
260 if (!request_region(res_gpo
->start
, resource_size(res_gpo
),
263 "GPO I/O resource busy, probably claimed by ACPI\n");
265 vg
->gpo_reserved
= true;
267 vx855gpio_gpio_setup(vg
);
269 ret
= gpiochip_add(&vg
->gpio
);
271 dev_err(&pdev
->dev
, "failed to register GPIOs\n");
278 if (vg
->gpi_reserved
)
279 release_region(res_gpi
->start
, resource_size(res_gpi
));
280 if (vg
->gpo_reserved
)
281 release_region(res_gpi
->start
, resource_size(res_gpo
));
282 platform_set_drvdata(pdev
, NULL
);
287 static int __devexit
vx855gpio_remove(struct platform_device
*pdev
)
289 struct vx855_gpio
*vg
= platform_get_drvdata(pdev
);
290 struct resource
*res
;
292 if (gpiochip_remove(&vg
->gpio
))
293 dev_err(&pdev
->dev
, "unable to remove gpio_chip?\n");
295 if (vg
->gpi_reserved
) {
296 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
297 release_region(res
->start
, resource_size(res
));
299 if (vg
->gpo_reserved
) {
300 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 1);
301 release_region(res
->start
, resource_size(res
));
304 platform_set_drvdata(pdev
, NULL
);
309 static struct platform_driver vx855gpio_driver
= {
312 .owner
= THIS_MODULE
,
314 .probe
= vx855gpio_probe
,
315 .remove
= __devexit_p(vx855gpio_remove
),
318 module_platform_driver(vx855gpio_driver
);
320 MODULE_LICENSE("GPL");
321 MODULE_AUTHOR("Harald Welte <HaraldWelte@viatech.com>");
322 MODULE_DESCRIPTION("GPIO driver for the VIA VX855 chipset");
323 MODULE_ALIAS("platform:vx855_gpio");