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>
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,
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;
53 ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL);
57 if (ret & 1 << offset)
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,
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;
79 ret = wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
80 WM831X_GPN_DIR | WM831X_GPN_TRI, 0);
84 /* Can only set GPIO state once it's in output mode */
85 wm831x_gpio_set(chip, offset, value);
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)
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;
108 for (i = 0; i < chip->ngpio; i++) {
109 int gpio = i + chip->base;
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
118 label = gpiochip_is_requested(chip, i);
120 label = "Unrequested";
122 seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
124 reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i);
127 "GPIO control %d read failed: %d\n",
133 switch (reg & WM831X_GPN_PULL_MASK) {
134 case WM831X_GPIO_PULL_NONE:
137 case WM831X_GPIO_PULL_DOWN:
140 case WM831X_GPIO_PULL_UP:
143 pull = "INVALID PULL";
150 if (reg & WM831X_GPN_PWR_DOM)
151 powerdomain = "VPMIC";
153 powerdomain = "DBVDD";
158 if (reg & WM831X_GPN_PWR_DOM)
159 powerdomain = "SYSVDD";
161 powerdomain = "DBVDD";
165 powerdomain = "TPVDD";
173 seq_printf(s, " %s %s %s %s%s\n"
175 reg & WM831X_GPN_DIR ? "in" : "out",
176 wm831x_gpio_get(chip, i) ? "high" : "low",
179 reg & WM831X_GPN_POL ? " inverted" : "",
180 reg & WM831X_GPN_OD ? "open-drain" : "CMOS",
181 reg & WM831X_GPN_TRI ? " tristated" : "",
186 #define wm831x_gpio_dbg_show NULL
189 static struct gpio_chip template_chip = {
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,
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;
208 wm831x_gpio = kzalloc(sizeof(*wm831x_gpio), GFP_KERNEL);
209 if (wm831x_gpio == NULL)
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;
219 wm831x_gpio->gpio_chip.base = -1;
221 ret = gpiochip_add(&wm831x_gpio->gpio_chip);
223 dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
228 platform_set_drvdata(pdev, wm831x_gpio);
237 static int __devexit wm831x_gpio_remove(struct platform_device *pdev)
239 struct wm831x_gpio *wm831x_gpio = platform_get_drvdata(pdev);
242 ret = gpiochip_remove(&wm831x_gpio->gpio_chip);
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");