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 int wm831x_gpio_direction_out(struct gpio_chip
*chip
,
64 unsigned offset
, int value
)
66 struct wm831x_gpio
*wm831x_gpio
= to_wm831x_gpio(chip
);
67 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
69 return wm831x_set_bits(wm831x
, WM831X_GPIO1_CONTROL
+ offset
,
70 WM831X_GPN_DIR
| WM831X_GPN_TRI
, 0);
73 static void wm831x_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
75 struct wm831x_gpio
*wm831x_gpio
= to_wm831x_gpio(chip
);
76 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
78 wm831x_set_bits(wm831x
, WM831X_GPIO_LEVEL
, 1 << offset
,
82 static int wm831x_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
84 struct wm831x_gpio
*wm831x_gpio
= to_wm831x_gpio(chip
);
85 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
87 if (!wm831x
->irq_base
)
90 return wm831x
->irq_base
+ WM831X_IRQ_GPIO_1
+ offset
;
93 #ifdef CONFIG_DEBUG_FS
94 static void wm831x_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
96 struct wm831x_gpio
*wm831x_gpio
= to_wm831x_gpio(chip
);
97 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
100 for (i
= 0; i
< chip
->ngpio
; i
++) {
101 int gpio
= i
+ chip
->base
;
103 const char *label
, *pull
, *powerdomain
;
105 /* We report the GPIO even if it's not requested since
106 * we're also reporting things like alternate
107 * functions which apply even when the GPIO is not in
110 label
= gpiochip_is_requested(chip
, i
);
112 label
= "Unrequested";
114 seq_printf(s
, " gpio-%-3d (%-20.20s) ", gpio
, label
);
116 reg
= wm831x_reg_read(wm831x
, WM831X_GPIO1_CONTROL
+ i
);
119 "GPIO control %d read failed: %d\n",
125 switch (reg
& WM831X_GPN_PULL_MASK
) {
126 case WM831X_GPIO_PULL_NONE
:
129 case WM831X_GPIO_PULL_DOWN
:
132 case WM831X_GPIO_PULL_UP
:
135 pull
= "INVALID PULL";
142 if (reg
& WM831X_GPN_PWR_DOM
)
143 powerdomain
= "VPMIC";
145 powerdomain
= "DBVDD";
150 if (reg
& WM831X_GPN_PWR_DOM
)
151 powerdomain
= "SYSVDD";
153 powerdomain
= "DBVDD";
157 powerdomain
= "TPVDD";
165 seq_printf(s
, " %s %s %s %s%s\n"
167 reg
& WM831X_GPN_DIR
? "in" : "out",
168 wm831x_gpio_get(chip
, i
) ? "high" : "low",
171 reg
& WM831X_GPN_POL
? " inverted" : "",
172 reg
& WM831X_GPN_OD
? "open-drain" : "CMOS",
173 reg
& WM831X_GPN_TRI
? " tristated" : "",
178 #define wm831x_gpio_dbg_show NULL
181 static struct gpio_chip template_chip
= {
183 .owner
= THIS_MODULE
,
184 .direction_input
= wm831x_gpio_direction_in
,
185 .get
= wm831x_gpio_get
,
186 .direction_output
= wm831x_gpio_direction_out
,
187 .set
= wm831x_gpio_set
,
188 .to_irq
= wm831x_gpio_to_irq
,
189 .dbg_show
= wm831x_gpio_dbg_show
,
193 static int __devinit
wm831x_gpio_probe(struct platform_device
*pdev
)
195 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
196 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
197 struct wm831x_gpio
*wm831x_gpio
;
200 wm831x_gpio
= kzalloc(sizeof(*wm831x_gpio
), GFP_KERNEL
);
201 if (wm831x_gpio
== NULL
)
204 wm831x_gpio
->wm831x
= wm831x
;
205 wm831x_gpio
->gpio_chip
= template_chip
;
206 wm831x_gpio
->gpio_chip
.ngpio
= wm831x
->num_gpio
;
207 wm831x_gpio
->gpio_chip
.dev
= &pdev
->dev
;
208 if (pdata
&& pdata
->gpio_base
)
209 wm831x_gpio
->gpio_chip
.base
= pdata
->gpio_base
;
211 wm831x_gpio
->gpio_chip
.base
= -1;
213 ret
= gpiochip_add(&wm831x_gpio
->gpio_chip
);
215 dev_err(&pdev
->dev
, "Could not register gpiochip, %d\n",
220 platform_set_drvdata(pdev
, wm831x_gpio
);
229 static int __devexit
wm831x_gpio_remove(struct platform_device
*pdev
)
231 struct wm831x_gpio
*wm831x_gpio
= platform_get_drvdata(pdev
);
234 ret
= gpiochip_remove(&wm831x_gpio
->gpio_chip
);
241 static struct platform_driver wm831x_gpio_driver
= {
242 .driver
.name
= "wm831x-gpio",
243 .driver
.owner
= THIS_MODULE
,
244 .probe
= wm831x_gpio_probe
,
245 .remove
= __devexit_p(wm831x_gpio_remove
),
248 static int __init
wm831x_gpio_init(void)
250 return platform_driver_register(&wm831x_gpio_driver
);
252 subsys_initcall(wm831x_gpio_init
);
254 static void __exit
wm831x_gpio_exit(void)
256 platform_driver_unregister(&wm831x_gpio_driver
);
258 module_exit(wm831x_gpio_exit
);
260 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
261 MODULE_DESCRIPTION("GPIO interface for WM831x PMICs");
262 MODULE_LICENSE("GPL");
263 MODULE_ALIAS("platform:wm831x-gpio");