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>
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
,
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
;
52 ret
= wm831x_reg_read(wm831x
, WM831X_GPIO_LEVEL
);
56 if (ret
& 1 << offset
)
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
,
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
;
88 for (i
= 0; i
< chip
->ngpio
; i
++) {
89 int gpio
= i
+ chip
->base
;
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
98 label
= gpiochip_is_requested(chip
, i
);
100 label
= "Unrequested";
102 seq_printf(s
, " gpio-%-3d (%-20.20s) ", gpio
, label
);
104 reg
= wm831x_reg_read(wm831x
, WM831X_GPIO1_CONTROL
+ i
);
107 "GPIO control %d read failed: %d\n",
113 switch (reg
& WM831X_GPN_PULL_MASK
) {
114 case WM831X_GPIO_PULL_NONE
:
117 case WM831X_GPIO_PULL_DOWN
:
120 case WM831X_GPIO_PULL_UP
:
123 pull
= "INVALID PULL";
130 if (reg
& WM831X_GPN_PWR_DOM
)
131 powerdomain
= "VPMIC";
133 powerdomain
= "DBVDD";
138 if (reg
& WM831X_GPN_PWR_DOM
)
139 powerdomain
= "SYSVDD";
141 powerdomain
= "DBVDD";
145 powerdomain
= "TPVDD";
153 seq_printf(s
, " %s %s %s %s%s\n"
155 reg
& WM831X_GPN_DIR
? "in" : "out",
156 wm831x_gpio_get(chip
, i
) ? "high" : "low",
159 reg
& WM831X_GPN_POL
? " inverted" : "",
160 reg
& WM831X_GPN_OD
? "open-drain" : "CMOS",
161 reg
& WM831X_GPN_TRI
? " tristated" : "",
166 #define wm831x_gpio_dbg_show NULL
169 static struct gpio_chip template_chip
= {
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
,
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
;
187 wm831x_gpio
= kzalloc(sizeof(*wm831x_gpio
), GFP_KERNEL
);
188 if (wm831x_gpio
== NULL
)
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
;
198 wm831x_gpio
->gpio_chip
.base
= -1;
200 ret
= gpiochip_add(&wm831x_gpio
->gpio_chip
);
202 dev_err(&pdev
->dev
, "Could not register gpiochip, %d\n",
207 platform_set_drvdata(pdev
, wm831x_gpio
);
216 static int __devexit
wm831x_gpio_remove(struct platform_device
*pdev
)
218 struct wm831x_gpio
*wm831x_gpio
= platform_get_drvdata(pdev
);
221 ret
= gpiochip_remove(&wm831x_gpio
->gpio_chip
);
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");