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/slab.h>
17 #include <linux/module.h>
18 #include <linux/gpio.h>
19 #include <linux/mfd/core.h>
20 #include <linux/platform_device.h>
21 #include <linux/seq_file.h>
23 #include <linux/mfd/wm831x/core.h>
24 #include <linux/mfd/wm831x/pdata.h>
25 #include <linux/mfd/wm831x/gpio.h>
26 #include <linux/mfd/wm831x/irq.h>
29 struct wm831x *wm831x;
30 struct gpio_chip gpio_chip;
33 static inline struct wm831x_gpio *to_wm831x_gpio(struct gpio_chip *chip)
35 return container_of(chip, struct wm831x_gpio, gpio_chip);
38 static int wm831x_gpio_pull_up_down(struct gpio_chip *chip, unsigned offset, unsigned value)
40 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
41 struct wm831x *wm831x = wm831x_gpio->wm831x;
43 if(value == GPIOPullUp)
44 value = WM831X_GPIO_PULL_UP;
45 else if(value == GPIOPullDown)
46 value = WM831X_GPIO_PULL_DOWN;
47 else if(value == GPIONormal)
48 value = WM831X_GPIO_PULL_NONE;
49 //printk("wm831x_gpio_pull_up_down=%x,%x\n",WM831X_GPIO1_CONTROL + offset,value);
50 return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
51 WM831X_GPN_PULL_MASK, value);
54 static int wm831x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
56 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
57 struct wm831x *wm831x = wm831x_gpio->wm831x;
58 int val = WM831X_GPN_DIR;
60 if (wm831x->has_gpio_ena)
61 val |= WM831X_GPN_TRI;
62 //printk("wm831x_gpio_direction_in=%x,%x\n",WM831X_GPIO1_CONTROL + offset,val);
63 return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
64 WM831X_GPN_DIR | WM831X_GPN_TRI |
65 WM831X_GPN_FN_MASK, val);
68 static int wm831x_gpio_get(struct gpio_chip *chip, unsigned offset)
70 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
71 struct wm831x *wm831x = wm831x_gpio->wm831x;
75 ret = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + offset);
78 gpn_pol = (ret & WM831X_GPN_POL_MASK) >> WM831X_GPN_POL_SHIFT;
80 ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL);
81 //printk("wm831x_gpio_get=%x,%d,%d\n",ret,offset,gpn_pol);
85 return !((ret>>offset)^gpn_pol);
88 static void wm831x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
90 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
91 struct wm831x *wm831x = wm831x_gpio->wm831x;
93 wm831x_set_bits(wm831x, WM831X_GPIO_LEVEL, 1 << offset,
97 static int wm831x_gpio_direction_out(struct gpio_chip *chip,
98 unsigned offset, int value)
100 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
101 struct wm831x *wm831x = wm831x_gpio->wm831x;
105 if (wm831x->has_gpio_ena)
106 val |= WM831X_GPN_TRI;
107 //printk("wm831x_gpio_direction_out=%x,%x\n",WM831X_GPIO1_CONTROL + offset,val);
108 ret = wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
109 WM831X_GPN_DIR | WM831X_GPN_TRI |
110 WM831X_GPN_FN_MASK | WM831X_GPN_POL_MASK, val|WM831X_GPN_POL);
114 /* Can only set GPIO state once it's in output mode */
115 wm831x_gpio_set(chip, offset, value);
120 static int wm831x_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
122 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
123 struct wm831x *wm831x = wm831x_gpio->wm831x;
125 if (!wm831x->irq_base)
128 return wm831x->irq_base + WM831X_IRQ_GPIO_1 + offset;
131 static int wm831x_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
134 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
135 struct wm831x *wm831x = wm831x_gpio->wm831x;
136 int reg = WM831X_GPIO1_CONTROL + offset;
139 ret = wm831x_reg_read(wm831x, reg);
143 switch (ret & WM831X_GPN_FN_MASK) {
148 /* Not in GPIO mode */
152 if (debounce >= 32 && debounce <= 64)
154 else if (debounce >= 4000 && debounce <= 8000)
158 //printk("wm831x_gpio_set_debounce=%x,%x\n",WM831X_GPIO1_CONTROL + offset,fn);
159 return wm831x_set_bits(wm831x, reg, WM831X_GPN_FN_MASK, fn);
162 #ifdef CONFIG_DEBUG_FS
163 static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
165 struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
166 struct wm831x *wm831x = wm831x_gpio->wm831x;
169 for (i = 0; i < chip->ngpio; i++) {
170 int gpio = i + chip->base;
172 const char *label, *pull, *powerdomain;
174 /* We report the GPIO even if it's not requested since
175 * we're also reporting things like alternate
176 * functions which apply even when the GPIO is not in
179 label = gpiochip_is_requested(chip, i);
181 label = "Unrequested";
183 seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
185 reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i);
188 "GPIO control %d read failed: %d\n",
194 switch (reg & WM831X_GPN_PULL_MASK) {
195 case WM831X_GPIO_PULL_NONE:
198 case WM831X_GPIO_PULL_DOWN:
201 case WM831X_GPIO_PULL_UP:
204 pull = "INVALID PULL";
211 if (reg & WM831X_GPN_PWR_DOM)
212 powerdomain = "VPMIC";
214 powerdomain = "DBVDD";
219 if (reg & WM831X_GPN_PWR_DOM)
220 powerdomain = "SYSVDD";
222 powerdomain = "DBVDD";
226 powerdomain = "TPVDD";
234 tristated = reg & WM831X_GPN_TRI;
235 if (wm831x->has_gpio_ena)
236 tristated = !tristated;
238 seq_printf(s, " %s %s %s %s%s\n"
240 reg & WM831X_GPN_DIR ? "in" : "out",
241 wm831x_gpio_get(chip, i) ? "high" : "low",
244 reg & WM831X_GPN_POL ? "" : " inverted",
245 reg & WM831X_GPN_OD ? "open-drain" : "CMOS",
246 tristated ? " tristated" : "",
251 #define wm831x_gpio_dbg_show NULL
254 static struct gpio_chip template_chip = {
256 .owner = THIS_MODULE,
257 .direction_input = wm831x_gpio_direction_in,
258 .get = wm831x_gpio_get,
259 .direction_output = wm831x_gpio_direction_out,
260 .set = wm831x_gpio_set,
261 .pull_updown = wm831x_gpio_pull_up_down,
262 .to_irq = wm831x_gpio_to_irq,
263 .set_debounce = wm831x_gpio_set_debounce,
264 .dbg_show = wm831x_gpio_dbg_show,
268 static int __devinit wm831x_gpio_probe(struct platform_device *pdev)
270 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
271 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
272 struct wm831x_gpio *wm831x_gpio;
274 printk("%s\n",__FUNCTION__);
275 wm831x_gpio = kzalloc(sizeof(*wm831x_gpio), GFP_KERNEL);
276 if (wm831x_gpio == NULL)
279 wm831x_gpio->wm831x = wm831x;
280 wm831x_gpio->gpio_chip = template_chip;
281 wm831x_gpio->gpio_chip.ngpio = wm831x->num_gpio;
282 wm831x_gpio->gpio_chip.dev = &pdev->dev;
283 if (pdata && pdata->gpio_base)
284 wm831x_gpio->gpio_chip.base = pdata->gpio_base;
286 wm831x_gpio->gpio_chip.base = -1;
288 ret = gpiochip_add(&wm831x_gpio->gpio_chip);
290 dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
295 platform_set_drvdata(pdev, wm831x_gpio);
304 static int __devexit wm831x_gpio_remove(struct platform_device *pdev)
306 struct wm831x_gpio *wm831x_gpio = platform_get_drvdata(pdev);
309 ret = gpiochip_remove(&wm831x_gpio->gpio_chip);
316 static struct platform_driver wm831x_gpio_driver = {
317 .driver.name = "wm831x-gpio",
318 .driver.owner = THIS_MODULE,
319 .probe = wm831x_gpio_probe,
320 .remove = __devexit_p(wm831x_gpio_remove),
323 static int __init wm831x_gpio_init(void)
325 return platform_driver_register(&wm831x_gpio_driver);
327 subsys_initcall(wm831x_gpio_init);
329 static void __exit wm831x_gpio_exit(void)
331 platform_driver_unregister(&wm831x_gpio_driver);
333 module_exit(wm831x_gpio_exit);
335 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
336 MODULE_DESCRIPTION("GPIO interface for WM831x PMICs");
337 MODULE_LICENSE("GPL");
338 MODULE_ALIAS("platform:wm831x-gpio");