iwlwifi: fix skb usage after free
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpio / wm831x-gpio.c
blobc5a00f7a7bf0f029a353a91d778510c3fc110924
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>
25 #include <linux/mfd/wm831x/irq.h>
27 struct wm831x_gpio {
28 struct wm831x *wm831x;
29 struct gpio_chip gpio_chip;
32 static inline struct wm831x_gpio *to_wm831x_gpio(struct gpio_chip *chip)
34 return container_of(chip, struct wm831x_gpio, gpio_chip);
37 static int wm831x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
39 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
40 struct wm831x *wm831x = wm831x_gpio->wm831x;
42 return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
43 WM831X_GPN_DIR | WM831X_GPN_TRI,
44 WM831X_GPN_DIR);
47 static int wm831x_gpio_get(struct gpio_chip *chip, unsigned offset)
49 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
50 struct wm831x *wm831x = wm831x_gpio->wm831x;
51 int ret;
53 ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL);
54 if (ret < 0)
55 return ret;
57 if (ret & 1 << offset)
58 return 1;
59 else
60 return 0;
63 static void wm831x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
65 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
66 struct wm831x *wm831x = wm831x_gpio->wm831x;
68 wm831x_set_bits(wm831x, WM831X_GPIO_LEVEL, 1 << offset,
69 value << offset);
72 static int wm831x_gpio_direction_out(struct gpio_chip *chip,
73 unsigned offset, int value)
75 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
76 struct wm831x *wm831x = wm831x_gpio->wm831x;
77 int ret;
79 ret = wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
80 WM831X_GPN_DIR | WM831X_GPN_TRI, 0);
81 if (ret < 0)
82 return ret;
84 /* Can only set GPIO state once it's in output mode */
85 wm831x_gpio_set(chip, offset, value);
87 return 0;
90 static int wm831x_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
92 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
93 struct wm831x *wm831x = wm831x_gpio->wm831x;
95 if (!wm831x->irq_base)
96 return -EINVAL;
98 return wm831x->irq_base + WM831X_IRQ_GPIO_1 + offset;
101 #ifdef CONFIG_DEBUG_FS
102 static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
104 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
105 struct wm831x *wm831x = wm831x_gpio->wm831x;
106 int i;
108 for (i = 0; i < chip->ngpio; i++) {
109 int gpio = i + chip->base;
110 int reg;
111 const char *label, *pull, *powerdomain;
113 /* We report the GPIO even if it's not requested since
114 * we're also reporting things like alternate
115 * functions which apply even when the GPIO is not in
116 * use as a GPIO.
118 label = gpiochip_is_requested(chip, i);
119 if (!label)
120 label = "Unrequested";
122 seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
124 reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i);
125 if (reg < 0) {
126 dev_err(wm831x->dev,
127 "GPIO control %d read failed: %d\n",
128 gpio, reg);
129 seq_printf(s, "\n");
130 continue;
133 switch (reg & WM831X_GPN_PULL_MASK) {
134 case WM831X_GPIO_PULL_NONE:
135 pull = "nopull";
136 break;
137 case WM831X_GPIO_PULL_DOWN:
138 pull = "pulldown";
139 break;
140 case WM831X_GPIO_PULL_UP:
141 pull = "pullup";
142 default:
143 pull = "INVALID PULL";
144 break;
147 switch (i + 1) {
148 case 1 ... 3:
149 case 7 ... 9:
150 if (reg & WM831X_GPN_PWR_DOM)
151 powerdomain = "VPMIC";
152 else
153 powerdomain = "DBVDD";
154 break;
156 case 4 ... 6:
157 case 10 ... 12:
158 if (reg & WM831X_GPN_PWR_DOM)
159 powerdomain = "SYSVDD";
160 else
161 powerdomain = "DBVDD";
162 break;
164 case 13 ... 16:
165 powerdomain = "TPVDD";
166 break;
168 default:
169 BUG();
170 break;
173 seq_printf(s, " %s %s %s %s%s\n"
174 " %s%s (0x%4x)\n",
175 reg & WM831X_GPN_DIR ? "in" : "out",
176 wm831x_gpio_get(chip, i) ? "high" : "low",
177 pull,
178 powerdomain,
179 reg & WM831X_GPN_POL ? " inverted" : "",
180 reg & WM831X_GPN_OD ? "open-drain" : "CMOS",
181 reg & WM831X_GPN_TRI ? " tristated" : "",
182 reg);
185 #else
186 #define wm831x_gpio_dbg_show NULL
187 #endif
189 static struct gpio_chip template_chip = {
190 .label = "wm831x",
191 .owner = THIS_MODULE,
192 .direction_input = wm831x_gpio_direction_in,
193 .get = wm831x_gpio_get,
194 .direction_output = wm831x_gpio_direction_out,
195 .set = wm831x_gpio_set,
196 .to_irq = wm831x_gpio_to_irq,
197 .dbg_show = wm831x_gpio_dbg_show,
198 .can_sleep = 1,
201 static int __devinit wm831x_gpio_probe(struct platform_device *pdev)
203 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
204 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
205 struct wm831x_gpio *wm831x_gpio;
206 int ret;
208 wm831x_gpio = kzalloc(sizeof(*wm831x_gpio), GFP_KERNEL);
209 if (wm831x_gpio == NULL)
210 return -ENOMEM;
212 wm831x_gpio->wm831x = wm831x;
213 wm831x_gpio->gpio_chip = template_chip;
214 wm831x_gpio->gpio_chip.ngpio = wm831x->num_gpio;
215 wm831x_gpio->gpio_chip.dev = &pdev->dev;
216 if (pdata && pdata->gpio_base)
217 wm831x_gpio->gpio_chip.base = pdata->gpio_base;
218 else
219 wm831x_gpio->gpio_chip.base = -1;
221 ret = gpiochip_add(&wm831x_gpio->gpio_chip);
222 if (ret < 0) {
223 dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
224 ret);
225 goto err;
228 platform_set_drvdata(pdev, wm831x_gpio);
230 return ret;
232 err:
233 kfree(wm831x_gpio);
234 return ret;
237 static int __devexit wm831x_gpio_remove(struct platform_device *pdev)
239 struct wm831x_gpio *wm831x_gpio = platform_get_drvdata(pdev);
240 int ret;
242 ret = gpiochip_remove(&wm831x_gpio->gpio_chip);
243 if (ret == 0)
244 kfree(wm831x_gpio);
246 return ret;
249 static struct platform_driver wm831x_gpio_driver = {
250 .driver.name = "wm831x-gpio",
251 .driver.owner = THIS_MODULE,
252 .probe = wm831x_gpio_probe,
253 .remove = __devexit_p(wm831x_gpio_remove),
256 static int __init wm831x_gpio_init(void)
258 return platform_driver_register(&wm831x_gpio_driver);
260 subsys_initcall(wm831x_gpio_init);
262 static void __exit wm831x_gpio_exit(void)
264 platform_driver_unregister(&wm831x_gpio_driver);
266 module_exit(wm831x_gpio_exit);
268 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
269 MODULE_DESCRIPTION("GPIO interface for WM831x PMICs");
270 MODULE_LICENSE("GPL");
271 MODULE_ALIAS("platform:wm831x-gpio");