gpiolib: Make WM831x GPIO count dynamic
[safe/jmp/linux-2.6] / 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
26 struct wm831x_gpio {
27         struct wm831x *wm831x;
28         struct gpio_chip gpio_chip;
29 };
30
31 static inline struct wm831x_gpio *to_wm831x_gpio(struct gpio_chip *chip)
32 {
33         return container_of(chip, struct wm831x_gpio, gpio_chip);
34 }
35
36 static int wm831x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
37 {
38         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
39         struct wm831x *wm831x = wm831x_gpio->wm831x;
40
41         return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
42                                WM831X_GPN_DIR | WM831X_GPN_TRI,
43                                WM831X_GPN_DIR);
44 }
45
46 static int wm831x_gpio_get(struct gpio_chip *chip, unsigned offset)
47 {
48         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
49         struct wm831x *wm831x = wm831x_gpio->wm831x;
50         int ret;
51
52         ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL);
53         if (ret < 0)
54                 return ret;
55
56         if (ret & 1 << offset)
57                 return 1;
58         else
59                 return 0;
60 }
61
62 static int wm831x_gpio_direction_out(struct gpio_chip *chip,
63                                      unsigned offset, int value)
64 {
65         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
66         struct wm831x *wm831x = wm831x_gpio->wm831x;
67
68         return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
69                                WM831X_GPN_DIR | WM831X_GPN_TRI, 0);
70 }
71
72 static void wm831x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
73 {
74         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
75         struct wm831x *wm831x = wm831x_gpio->wm831x;
76
77         wm831x_set_bits(wm831x, WM831X_GPIO_LEVEL, 1 << offset,
78                         value << offset);
79 }
80
81 #ifdef CONFIG_DEBUG_FS
82 static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
83 {
84         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
85         struct wm831x *wm831x = wm831x_gpio->wm831x;
86         int i;
87
88         for (i = 0; i < chip->ngpio; i++) {
89                 int gpio = i + chip->base;
90                 int reg;
91                 const char *label, *pull, *powerdomain;
92
93                 /* We report the GPIO even if it's not requested since
94                  * we're also reporting things like alternate
95                  * functions which apply even when the GPIO is not in
96                  * use as a GPIO.
97                  */
98                 label = gpiochip_is_requested(chip, i);
99                 if (!label)
100                         label = "Unrequested";
101
102                 seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
103
104                 reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i);
105                 if (reg < 0) {
106                         dev_err(wm831x->dev,
107                                 "GPIO control %d read failed: %d\n",
108                                 gpio, reg);
109                         seq_printf(s, "\n");
110                         continue;
111                 }
112
113                 switch (reg & WM831X_GPN_PULL_MASK) {
114                 case WM831X_GPIO_PULL_NONE:
115                         pull = "nopull";
116                         break;
117                 case WM831X_GPIO_PULL_DOWN:
118                         pull = "pulldown";
119                         break;
120                 case WM831X_GPIO_PULL_UP:
121                         pull = "pullup";
122                 default:
123                         pull = "INVALID PULL";
124                         break;
125                 }
126
127                 switch (i + 1) {
128                 case 1 ... 3:
129                 case 7 ... 9:
130                         if (reg & WM831X_GPN_PWR_DOM)
131                                 powerdomain = "VPMIC";
132                         else
133                                 powerdomain = "DBVDD";
134                         break;
135
136                 case 4 ... 6:
137                 case 10 ... 12:
138                         if (reg & WM831X_GPN_PWR_DOM)
139                                 powerdomain = "SYSVDD";
140                         else
141                                 powerdomain = "DBVDD";
142                         break;
143
144                 case 13 ... 16:
145                         powerdomain = "TPVDD";
146                         break;
147
148                 default:
149                         BUG();
150                         break;
151                 }
152
153                 seq_printf(s, " %s %s %s %s%s\n"
154                            "                                  %s%s (0x%4x)\n",
155                            reg & WM831X_GPN_DIR ? "in" : "out",
156                            wm831x_gpio_get(chip, i) ? "high" : "low",
157                            pull,
158                            powerdomain,
159                            reg & WM831X_GPN_POL ? " inverted" : "",
160                            reg & WM831X_GPN_OD ? "open-drain" : "CMOS",
161                            reg & WM831X_GPN_TRI ? " tristated" : "",
162                            reg);
163         }
164 }
165 #else
166 #define wm831x_gpio_dbg_show NULL
167 #endif
168
169 static struct gpio_chip template_chip = {
170         .label                  = "wm831x",
171         .owner                  = THIS_MODULE,
172         .direction_input        = wm831x_gpio_direction_in,
173         .get                    = wm831x_gpio_get,
174         .direction_output       = wm831x_gpio_direction_out,
175         .set                    = wm831x_gpio_set,
176         .dbg_show               = wm831x_gpio_dbg_show,
177         .can_sleep              = 1,
178 };
179
180 static int __devinit wm831x_gpio_probe(struct platform_device *pdev)
181 {
182         struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
183         struct wm831x_pdata *pdata = wm831x->dev->platform_data;
184         struct wm831x_gpio *wm831x_gpio;
185         int ret;
186
187         wm831x_gpio = kzalloc(sizeof(*wm831x_gpio), GFP_KERNEL);
188         if (wm831x_gpio == NULL)
189                 return -ENOMEM;
190
191         wm831x_gpio->wm831x = wm831x;
192         wm831x_gpio->gpio_chip = template_chip;
193         wm831x_gpio->gpio_chip.ngpio = wm831x->num_gpio;
194         wm831x_gpio->gpio_chip.dev = &pdev->dev;
195         if (pdata && pdata->gpio_base)
196                 wm831x_gpio->gpio_chip.base = pdata->gpio_base;
197         else
198                 wm831x_gpio->gpio_chip.base = -1;
199
200         ret = gpiochip_add(&wm831x_gpio->gpio_chip);
201         if (ret < 0) {
202                 dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
203                         ret);
204                 goto err;
205         }
206
207         platform_set_drvdata(pdev, wm831x_gpio);
208
209         return ret;
210
211 err:
212         kfree(wm831x_gpio);
213         return ret;
214 }
215
216 static int __devexit wm831x_gpio_remove(struct platform_device *pdev)
217 {
218         struct wm831x_gpio *wm831x_gpio = platform_get_drvdata(pdev);
219         int ret;
220
221         ret = gpiochip_remove(&wm831x_gpio->gpio_chip);
222         if (ret == 0)
223                 kfree(wm831x_gpio);
224
225         return ret;
226 }
227
228 static struct platform_driver wm831x_gpio_driver = {
229         .driver.name    = "wm831x-gpio",
230         .driver.owner   = THIS_MODULE,
231         .probe          = wm831x_gpio_probe,
232         .remove         = __devexit_p(wm831x_gpio_remove),
233 };
234
235 static int __init wm831x_gpio_init(void)
236 {
237         return platform_driver_register(&wm831x_gpio_driver);
238 }
239 subsys_initcall(wm831x_gpio_init);
240
241 static void __exit wm831x_gpio_exit(void)
242 {
243         platform_driver_unregister(&wm831x_gpio_driver);
244 }
245 module_exit(wm831x_gpio_exit);
246
247 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
248 MODULE_DESCRIPTION("GPIO interface for WM831x PMICs");
249 MODULE_LICENSE("GPL");
250 MODULE_ALIAS("platform:wm831x-gpio");