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 int wm831x_gpio_direction_out(struct gpio_chip
*chip
,
65 unsigned offset
, int value
)
67 struct wm831x_gpio
*wm831x_gpio
= to_wm831x_gpio(chip
);
68 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
70 return wm831x_set_bits(wm831x
, WM831X_GPIO1_CONTROL
+ offset
,
71 WM831X_GPN_DIR
| WM831X_GPN_TRI
, 0);
74 static void wm831x_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
76 struct wm831x_gpio
*wm831x_gpio
= to_wm831x_gpio(chip
);
77 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
79 wm831x_set_bits(wm831x
, WM831X_GPIO_LEVEL
, 1 << offset
,
83 #ifdef CONFIG_DEBUG_FS
84 static void wm831x_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
86 struct wm831x_gpio
*wm831x_gpio
= to_wm831x_gpio(chip
);
87 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
90 for (i
= 0; i
< chip
->ngpio
; i
++) {
91 int gpio
= i
+ chip
->base
;
93 const char *label
, *pull
, *powerdomain
;
95 /* We report the GPIO even if it's not requested since
96 * we're also reporting things like alternate
97 * functions which apply even when the GPIO is not in
100 label
= gpiochip_is_requested(chip
, i
);
102 label
= "Unrequested";
104 seq_printf(s
, " gpio-%-3d (%-20.20s) ", gpio
, label
);
106 reg
= wm831x_reg_read(wm831x
, WM831X_GPIO1_CONTROL
+ i
);
109 "GPIO control %d read failed: %d\n",
115 switch (reg
& WM831X_GPN_PULL_MASK
) {
116 case WM831X_GPIO_PULL_NONE
:
119 case WM831X_GPIO_PULL_DOWN
:
122 case WM831X_GPIO_PULL_UP
:
125 pull
= "INVALID PULL";
132 if (reg
& WM831X_GPN_PWR_DOM
)
133 powerdomain
= "VPMIC";
135 powerdomain
= "DBVDD";
140 if (reg
& WM831X_GPN_PWR_DOM
)
141 powerdomain
= "SYSVDD";
143 powerdomain
= "DBVDD";
147 powerdomain
= "TPVDD";
155 seq_printf(s
, " %s %s %s %s%s\n"
157 reg
& WM831X_GPN_DIR
? "in" : "out",
158 wm831x_gpio_get(chip
, i
) ? "high" : "low",
161 reg
& WM831X_GPN_POL
? " inverted" : "",
162 reg
& WM831X_GPN_OD
? "open-drain" : "CMOS",
163 reg
& WM831X_GPN_TRI
? " tristated" : "",
168 #define wm831x_gpio_dbg_show NULL
171 static struct gpio_chip template_chip
= {
173 .owner
= THIS_MODULE
,
174 .direction_input
= wm831x_gpio_direction_in
,
175 .get
= wm831x_gpio_get
,
176 .direction_output
= wm831x_gpio_direction_out
,
177 .set
= wm831x_gpio_set
,
178 .dbg_show
= wm831x_gpio_dbg_show
,
182 static int __devinit
wm831x_gpio_probe(struct platform_device
*pdev
)
184 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
185 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
186 struct wm831x_gpio
*wm831x_gpio
;
189 wm831x_gpio
= kzalloc(sizeof(*wm831x_gpio
), GFP_KERNEL
);
190 if (wm831x_gpio
== NULL
)
193 wm831x_gpio
->wm831x
= wm831x
;
194 wm831x_gpio
->gpio_chip
= template_chip
;
195 wm831x_gpio
->gpio_chip
.ngpio
= WM831X_GPIO_MAX
;
196 wm831x_gpio
->gpio_chip
.dev
= &pdev
->dev
;
197 if (pdata
&& pdata
->gpio_base
)
198 wm831x_gpio
->gpio_chip
.base
= pdata
->gpio_base
;
200 wm831x_gpio
->gpio_chip
.base
= -1;
202 ret
= gpiochip_add(&wm831x_gpio
->gpio_chip
);
204 dev_err(&pdev
->dev
, "Could not register gpiochip, %d\n",
209 platform_set_drvdata(pdev
, wm831x_gpio
);
218 static int __devexit
wm831x_gpio_remove(struct platform_device
*pdev
)
220 struct wm831x_gpio
*wm831x_gpio
= platform_get_drvdata(pdev
);
223 ret
= gpiochip_remove(&wm831x_gpio
->gpio_chip
);
230 static struct platform_driver wm831x_gpio_driver
= {
231 .driver
.name
= "wm831x-gpio",
232 .driver
.owner
= THIS_MODULE
,
233 .probe
= wm831x_gpio_probe
,
234 .remove
= __devexit_p(wm831x_gpio_remove
),
237 static int __init
wm831x_gpio_init(void)
239 return platform_driver_register(&wm831x_gpio_driver
);
241 subsys_initcall(wm831x_gpio_init
);
243 static void __exit
wm831x_gpio_exit(void)
245 platform_driver_unregister(&wm831x_gpio_driver
);
247 module_exit(wm831x_gpio_exit
);
249 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
250 MODULE_DESCRIPTION("GPIO interface for WM831x PMICs");
251 MODULE_LICENSE("GPL");
252 MODULE_ALIAS("platform:wm831x-gpio");