usb: io_ti: Make edge_remove_sysfs_attrs the port_remove method.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpio / wm831x-gpio.c
blob9a270478acacb352e7c19dd458638a0cce2ac93f
1 /*
2 * wm831x-gpio.c -- gpiolib support for Wolfson WM831x PMICs
4 * Copyright 2009 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/gpio.h>
18 #include <linux/mfd/core.h>
19 #include <linux/platform_device.h>
20 #include <linux/seq_file.h>
22 #include <linux/mfd/wm831x/core.h>
23 #include <linux/mfd/wm831x/pdata.h>
24 #include <linux/mfd/wm831x/gpio.h>
26 #define WM831X_GPIO_MAX 16
28 struct wm831x_gpio {
29 struct wm831x *wm831x;
30 struct gpio_chip gpio_chip;
33 static inline struct wm831x_gpio *to_wm831x_gpio(struct gpio_chip *chip)
35 return container_of(chip, struct wm831x_gpio, gpio_chip);
38 static int wm831x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
40 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
41 struct wm831x *wm831x = wm831x_gpio->wm831x;
43 return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
44 WM831X_GPN_DIR | WM831X_GPN_TRI,
45 WM831X_GPN_DIR);
48 static int wm831x_gpio_get(struct gpio_chip *chip, unsigned offset)
50 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
51 struct wm831x *wm831x = wm831x_gpio->wm831x;
52 int ret;
54 ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL);
55 if (ret < 0)
56 return ret;
58 if (ret & 1 << offset)
59 return 1;
60 else
61 return 0;
64 static void wm831x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
66 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
67 struct wm831x *wm831x = wm831x_gpio->wm831x;
69 wm831x_set_bits(wm831x, WM831X_GPIO_LEVEL, 1 << offset,
70 value << offset);
73 static int wm831x_gpio_direction_out(struct gpio_chip *chip,
74 unsigned offset, int value)
76 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
77 struct wm831x *wm831x = wm831x_gpio->wm831x;
78 int ret;
80 ret = wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
81 WM831X_GPN_DIR | WM831X_GPN_TRI, 0);
82 if (ret < 0)
83 return ret;
85 /* Can only set GPIO state once it's in output mode */
86 wm831x_gpio_set(chip, offset, value);
88 return 0;
91 #ifdef CONFIG_DEBUG_FS
92 static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
94 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
95 struct wm831x *wm831x = wm831x_gpio->wm831x;
96 int i;
98 for (i = 0; i < chip->ngpio; i++) {
99 int gpio = i + chip->base;
100 int reg;
101 const char *label, *pull, *powerdomain;
103 /* We report the GPIO even if it's not requested since
104 * we're also reporting things like alternate
105 * functions which apply even when the GPIO is not in
106 * use as a GPIO.
108 label = gpiochip_is_requested(chip, i);
109 if (!label)
110 label = "Unrequested";
112 seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
114 reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i);
115 if (reg < 0) {
116 dev_err(wm831x->dev,
117 "GPIO control %d read failed: %d\n",
118 gpio, reg);
119 seq_printf(s, "\n");
120 continue;
123 switch (reg & WM831X_GPN_PULL_MASK) {
124 case WM831X_GPIO_PULL_NONE:
125 pull = "nopull";
126 break;
127 case WM831X_GPIO_PULL_DOWN:
128 pull = "pulldown";
129 break;
130 case WM831X_GPIO_PULL_UP:
131 pull = "pullup";
132 default:
133 pull = "INVALID PULL";
134 break;
137 switch (i + 1) {
138 case 1 ... 3:
139 case 7 ... 9:
140 if (reg & WM831X_GPN_PWR_DOM)
141 powerdomain = "VPMIC";
142 else
143 powerdomain = "DBVDD";
144 break;
146 case 4 ... 6:
147 case 10 ... 12:
148 if (reg & WM831X_GPN_PWR_DOM)
149 powerdomain = "SYSVDD";
150 else
151 powerdomain = "DBVDD";
152 break;
154 case 13 ... 16:
155 powerdomain = "TPVDD";
156 break;
158 default:
159 BUG();
160 break;
163 seq_printf(s, " %s %s %s %s%s\n"
164 " %s%s (0x%4x)\n",
165 reg & WM831X_GPN_DIR ? "in" : "out",
166 wm831x_gpio_get(chip, i) ? "high" : "low",
167 pull,
168 powerdomain,
169 reg & WM831X_GPN_POL ? " inverted" : "",
170 reg & WM831X_GPN_OD ? "open-drain" : "CMOS",
171 reg & WM831X_GPN_TRI ? " tristated" : "",
172 reg);
175 #else
176 #define wm831x_gpio_dbg_show NULL
177 #endif
179 static struct gpio_chip template_chip = {
180 .label = "wm831x",
181 .owner = THIS_MODULE,
182 .direction_input = wm831x_gpio_direction_in,
183 .get = wm831x_gpio_get,
184 .direction_output = wm831x_gpio_direction_out,
185 .set = wm831x_gpio_set,
186 .dbg_show = wm831x_gpio_dbg_show,
187 .can_sleep = 1,
190 static int __devinit wm831x_gpio_probe(struct platform_device *pdev)
192 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
193 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
194 struct wm831x_gpio *wm831x_gpio;
195 int ret;
197 wm831x_gpio = kzalloc(sizeof(*wm831x_gpio), GFP_KERNEL);
198 if (wm831x_gpio == NULL)
199 return -ENOMEM;
201 wm831x_gpio->wm831x = wm831x;
202 wm831x_gpio->gpio_chip = template_chip;
203 wm831x_gpio->gpio_chip.ngpio = WM831X_GPIO_MAX;
204 wm831x_gpio->gpio_chip.dev = &pdev->dev;
205 if (pdata && pdata->gpio_base)
206 wm831x_gpio->gpio_chip.base = pdata->gpio_base;
207 else
208 wm831x_gpio->gpio_chip.base = -1;
210 ret = gpiochip_add(&wm831x_gpio->gpio_chip);
211 if (ret < 0) {
212 dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
213 ret);
214 goto err;
217 platform_set_drvdata(pdev, wm831x_gpio);
219 return ret;
221 err:
222 kfree(wm831x_gpio);
223 return ret;
226 static int __devexit wm831x_gpio_remove(struct platform_device *pdev)
228 struct wm831x_gpio *wm831x_gpio = platform_get_drvdata(pdev);
229 int ret;
231 ret = gpiochip_remove(&wm831x_gpio->gpio_chip);
232 if (ret == 0)
233 kfree(wm831x_gpio);
235 return ret;
238 static struct platform_driver wm831x_gpio_driver = {
239 .driver.name = "wm831x-gpio",
240 .driver.owner = THIS_MODULE,
241 .probe = wm831x_gpio_probe,
242 .remove = __devexit_p(wm831x_gpio_remove),
245 static int __init wm831x_gpio_init(void)
247 return platform_driver_register(&wm831x_gpio_driver);
249 subsys_initcall(wm831x_gpio_init);
251 static void __exit wm831x_gpio_exit(void)
253 platform_driver_unregister(&wm831x_gpio_driver);
255 module_exit(wm831x_gpio_exit);
257 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
258 MODULE_DESCRIPTION("GPIO interface for WM831x PMICs");
259 MODULE_LICENSE("GPL");
260 MODULE_ALIAS("platform:wm831x-gpio");