mm-only debug patch...
[mmotm.git] / drivers / gpio / wm831x-gpio.c
blobf5e4934f1da1698b7aaaac7fb42ab69e25791cdc
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 struct wm831x_gpio {
27 struct wm831x *wm831x;
28 struct gpio_chip gpio_chip;
31 static inline struct wm831x_gpio *to_wm831x_gpio(struct gpio_chip *chip)
33 return container_of(chip, struct wm831x_gpio, gpio_chip);
36 static int wm831x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
38 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
39 struct wm831x *wm831x = wm831x_gpio->wm831x;
41 return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
42 WM831X_GPN_DIR | WM831X_GPN_TRI,
43 WM831X_GPN_DIR);
46 static int wm831x_gpio_get(struct gpio_chip *chip, unsigned offset)
48 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
49 struct wm831x *wm831x = wm831x_gpio->wm831x;
50 int ret;
52 ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL);
53 if (ret < 0)
54 return ret;
56 if (ret & 1 << offset)
57 return 1;
58 else
59 return 0;
62 static int wm831x_gpio_direction_out(struct gpio_chip *chip,
63 unsigned offset, int value)
65 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
66 struct wm831x *wm831x = wm831x_gpio->wm831x;
68 return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
69 WM831X_GPN_DIR | WM831X_GPN_TRI, 0);
72 static void wm831x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
74 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
75 struct wm831x *wm831x = wm831x_gpio->wm831x;
77 wm831x_set_bits(wm831x, WM831X_GPIO_LEVEL, 1 << offset,
78 value << offset);
81 #ifdef CONFIG_DEBUG_FS
82 static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
84 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
85 struct wm831x *wm831x = wm831x_gpio->wm831x;
86 int i;
88 for (i = 0; i < chip->ngpio; i++) {
89 int gpio = i + chip->base;
90 int reg;
91 const char *label, *pull, *powerdomain;
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.
98 label = gpiochip_is_requested(chip, i);
99 if (!label)
100 label = "Unrequested";
102 seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
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;
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;
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;
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;
144 case 13 ... 16:
145 powerdomain = "TPVDD";
146 break;
148 default:
149 BUG();
150 break;
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);
165 #else
166 #define wm831x_gpio_dbg_show NULL
167 #endif
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,
180 static int __devinit wm831x_gpio_probe(struct platform_device *pdev)
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;
187 wm831x_gpio = kzalloc(sizeof(*wm831x_gpio), GFP_KERNEL);
188 if (wm831x_gpio == NULL)
189 return -ENOMEM;
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;
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;
207 platform_set_drvdata(pdev, wm831x_gpio);
209 return ret;
211 err:
212 kfree(wm831x_gpio);
213 return ret;
216 static int __devexit wm831x_gpio_remove(struct platform_device *pdev)
218 struct wm831x_gpio *wm831x_gpio = platform_get_drvdata(pdev);
219 int ret;
221 ret = gpiochip_remove(&wm831x_gpio->gpio_chip);
222 if (ret == 0)
223 kfree(wm831x_gpio);
225 return ret;
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),
235 static int __init wm831x_gpio_init(void)
237 return platform_driver_register(&wm831x_gpio_driver);
239 subsys_initcall(wm831x_gpio_init);
241 static void __exit wm831x_gpio_exit(void)
243 platform_driver_unregister(&wm831x_gpio_driver);
245 module_exit(wm831x_gpio_exit);
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");