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 #define WM831X_GPIO_MAX 16
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_direction_in(struct gpio_chip
*chip
, unsigned offset
)
40 struct wm831x_gpio
*wm831x_gpio
= to_wm831x_gpio(chip
);
41 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
43 return wm831x_set_bits(wm831x
, WM831X_GPIO1_CONTROL
+ offset
,
44 WM831X_GPN_DIR
| WM831X_GPN_TRI
,
48 static int wm831x_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
50 struct wm831x_gpio
*wm831x_gpio
= to_wm831x_gpio(chip
);
51 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
54 ret
= wm831x_reg_read(wm831x
, WM831X_GPIO_LEVEL
);
58 if (ret
& 1 << offset
)
64 static void wm831x_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
66 struct wm831x_gpio
*wm831x_gpio
= to_wm831x_gpio(chip
);
67 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
69 wm831x_set_bits(wm831x
, WM831X_GPIO_LEVEL
, 1 << offset
,
73 static int wm831x_gpio_direction_out(struct gpio_chip
*chip
,
74 unsigned offset
, int value
)
76 struct wm831x_gpio
*wm831x_gpio
= to_wm831x_gpio(chip
);
77 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
80 ret
= wm831x_set_bits(wm831x
, WM831X_GPIO1_CONTROL
+ offset
,
81 WM831X_GPN_DIR
| WM831X_GPN_TRI
, 0);
85 /* Can only set GPIO state once it's in output mode */
86 wm831x_gpio_set(chip
, offset
, value
);
91 #ifdef CONFIG_DEBUG_FS
92 static void wm831x_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
94 struct wm831x_gpio
*wm831x_gpio
= to_wm831x_gpio(chip
);
95 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
98 for (i
= 0; i
< chip
->ngpio
; i
++) {
99 int gpio
= i
+ chip
->base
;
101 const char *label
, *pull
, *powerdomain
;
103 /* We report the GPIO even if it's not requested since
104 * we're also reporting things like alternate
105 * functions which apply even when the GPIO is not in
108 label
= gpiochip_is_requested(chip
, i
);
110 label
= "Unrequested";
112 seq_printf(s
, " gpio-%-3d (%-20.20s) ", gpio
, label
);
114 reg
= wm831x_reg_read(wm831x
, WM831X_GPIO1_CONTROL
+ i
);
117 "GPIO control %d read failed: %d\n",
123 switch (reg
& WM831X_GPN_PULL_MASK
) {
124 case WM831X_GPIO_PULL_NONE
:
127 case WM831X_GPIO_PULL_DOWN
:
130 case WM831X_GPIO_PULL_UP
:
133 pull
= "INVALID PULL";
140 if (reg
& WM831X_GPN_PWR_DOM
)
141 powerdomain
= "VPMIC";
143 powerdomain
= "DBVDD";
148 if (reg
& WM831X_GPN_PWR_DOM
)
149 powerdomain
= "SYSVDD";
151 powerdomain
= "DBVDD";
155 powerdomain
= "TPVDD";
163 seq_printf(s
, " %s %s %s %s%s\n"
165 reg
& WM831X_GPN_DIR
? "in" : "out",
166 wm831x_gpio_get(chip
, i
) ? "high" : "low",
169 reg
& WM831X_GPN_POL
? " inverted" : "",
170 reg
& WM831X_GPN_OD
? "open-drain" : "CMOS",
171 reg
& WM831X_GPN_TRI
? " tristated" : "",
176 #define wm831x_gpio_dbg_show NULL
179 static struct gpio_chip template_chip
= {
181 .owner
= THIS_MODULE
,
182 .direction_input
= wm831x_gpio_direction_in
,
183 .get
= wm831x_gpio_get
,
184 .direction_output
= wm831x_gpio_direction_out
,
185 .set
= wm831x_gpio_set
,
186 .dbg_show
= wm831x_gpio_dbg_show
,
190 static int __devinit
wm831x_gpio_probe(struct platform_device
*pdev
)
192 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
193 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
194 struct wm831x_gpio
*wm831x_gpio
;
197 wm831x_gpio
= kzalloc(sizeof(*wm831x_gpio
), GFP_KERNEL
);
198 if (wm831x_gpio
== NULL
)
201 wm831x_gpio
->wm831x
= wm831x
;
202 wm831x_gpio
->gpio_chip
= template_chip
;
203 wm831x_gpio
->gpio_chip
.ngpio
= WM831X_GPIO_MAX
;
204 wm831x_gpio
->gpio_chip
.dev
= &pdev
->dev
;
205 if (pdata
&& pdata
->gpio_base
)
206 wm831x_gpio
->gpio_chip
.base
= pdata
->gpio_base
;
208 wm831x_gpio
->gpio_chip
.base
= -1;
210 ret
= gpiochip_add(&wm831x_gpio
->gpio_chip
);
212 dev_err(&pdev
->dev
, "Could not register gpiochip, %d\n",
217 platform_set_drvdata(pdev
, wm831x_gpio
);
226 static int __devexit
wm831x_gpio_remove(struct platform_device
*pdev
)
228 struct wm831x_gpio
*wm831x_gpio
= platform_get_drvdata(pdev
);
231 ret
= gpiochip_remove(&wm831x_gpio
->gpio_chip
);
238 static struct platform_driver wm831x_gpio_driver
= {
239 .driver
.name
= "wm831x-gpio",
240 .driver
.owner
= THIS_MODULE
,
241 .probe
= wm831x_gpio_probe
,
242 .remove
= __devexit_p(wm831x_gpio_remove
),
245 static int __init
wm831x_gpio_init(void)
247 return platform_driver_register(&wm831x_gpio_driver
);
249 subsys_initcall(wm831x_gpio_init
);
251 static void __exit
wm831x_gpio_exit(void)
253 platform_driver_unregister(&wm831x_gpio_driver
);
255 module_exit(wm831x_gpio_exit
);
257 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
258 MODULE_DESCRIPTION("GPIO interface for WM831x PMICs");
259 MODULE_LICENSE("GPL");
260 MODULE_ALIAS("platform:wm831x-gpio");