]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/gpio/wm831x-gpio.c
gpiolib: Actually set output state in wm831x_gpio_direction_output()
[net-next-2.6.git] / drivers / gpio / wm831x-gpio.c
1 /*
2  * wm831x-gpio.c  --  gpiolib support for Wolfson WM831x PMICs
3  *
4  * Copyright 2009 Wolfson Microelectronics PLC.
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
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.
12  *
13  */
14
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>
21
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>
26
27 struct wm831x_gpio {
28         struct wm831x *wm831x;
29         struct gpio_chip gpio_chip;
30 };
31
32 static inline struct wm831x_gpio *to_wm831x_gpio(struct gpio_chip *chip)
33 {
34         return container_of(chip, struct wm831x_gpio, gpio_chip);
35 }
36
37 static int wm831x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
38 {
39         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
40         struct wm831x *wm831x = wm831x_gpio->wm831x;
41
42         return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
43                                WM831X_GPN_DIR | WM831X_GPN_TRI,
44                                WM831X_GPN_DIR);
45 }
46
47 static int wm831x_gpio_get(struct gpio_chip *chip, unsigned offset)
48 {
49         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
50         struct wm831x *wm831x = wm831x_gpio->wm831x;
51         int ret;
52
53         ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL);
54         if (ret < 0)
55                 return ret;
56
57         if (ret & 1 << offset)
58                 return 1;
59         else
60                 return 0;
61 }
62
63 static void wm831x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
64 {
65         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
66         struct wm831x *wm831x = wm831x_gpio->wm831x;
67
68         wm831x_set_bits(wm831x, WM831X_GPIO_LEVEL, 1 << offset,
69                         value << offset);
70 }
71
72 static int wm831x_gpio_direction_out(struct gpio_chip *chip,
73                                      unsigned offset, int value)
74 {
75         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
76         struct wm831x *wm831x = wm831x_gpio->wm831x;
77         int ret;
78
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;
83
84         /* Can only set GPIO state once it's in output mode */
85         wm831x_gpio_set(chip, offset, value);
86
87         return 0;
88 }
89
90 static int wm831x_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
91 {
92         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
93         struct wm831x *wm831x = wm831x_gpio->wm831x;
94
95         if (!wm831x->irq_base)
96                 return -EINVAL;
97
98         return wm831x->irq_base + WM831X_IRQ_GPIO_1 + offset;
99 }
100
101 #ifdef CONFIG_DEBUG_FS
102 static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
103 {
104         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
105         struct wm831x *wm831x = wm831x_gpio->wm831x;
106         int i;
107
108         for (i = 0; i < chip->ngpio; i++) {
109                 int gpio = i + chip->base;
110                 int reg;
111                 const char *label, *pull, *powerdomain;
112
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.
117                  */
118                 label = gpiochip_is_requested(chip, i);
119                 if (!label)
120                         label = "Unrequested";
121
122                 seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
123
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;
131                 }
132
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;
145                 }
146
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;
155
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;
163
164                 case 13 ... 16:
165                         powerdomain = "TPVDD";
166                         break;
167
168                 default:
169                         BUG();
170                         break;
171                 }
172
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);
183         }
184 }
185 #else
186 #define wm831x_gpio_dbg_show NULL
187 #endif
188
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,
199 };
200
201 static int __devinit wm831x_gpio_probe(struct platform_device *pdev)
202 {
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;
207
208         wm831x_gpio = kzalloc(sizeof(*wm831x_gpio), GFP_KERNEL);
209         if (wm831x_gpio == NULL)
210                 return -ENOMEM;
211
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;
220
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;
226         }
227
228         platform_set_drvdata(pdev, wm831x_gpio);
229
230         return ret;
231
232 err:
233         kfree(wm831x_gpio);
234         return ret;
235 }
236
237 static int __devexit wm831x_gpio_remove(struct platform_device *pdev)
238 {
239         struct wm831x_gpio *wm831x_gpio = platform_get_drvdata(pdev);
240         int ret;
241
242         ret = gpiochip_remove(&wm831x_gpio->gpio_chip);
243         if (ret == 0)
244                 kfree(wm831x_gpio);
245
246         return ret;
247 }
248
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),
254 };
255
256 static int __init wm831x_gpio_init(void)
257 {
258         return platform_driver_register(&wm831x_gpio_driver);
259 }
260 subsys_initcall(wm831x_gpio_init);
261
262 static void __exit wm831x_gpio_exit(void)
263 {
264         platform_driver_unregister(&wm831x_gpio_driver);
265 }
266 module_exit(wm831x_gpio_exit);
267
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");