2 * 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 int wm831x_gpio_direction_in(struct gpio_chip
*chip
, unsigned offset
)
35 struct wm831x_gpio
*wm831x_gpio
= gpiochip_get_data(chip
);
36 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
37 int val
= WM831X_GPN_DIR
;
39 if (wm831x
->has_gpio_ena
)
40 val
|= WM831X_GPN_TRI
;
42 return wm831x_set_bits(wm831x
, WM831X_GPIO1_CONTROL
+ offset
,
43 WM831X_GPN_DIR
| WM831X_GPN_TRI
|
44 WM831X_GPN_FN_MASK
, val
);
47 static int wm831x_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
49 struct wm831x_gpio
*wm831x_gpio
= gpiochip_get_data(chip
);
50 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
53 ret
= wm831x_reg_read(wm831x
, WM831X_GPIO_LEVEL
);
57 if (ret
& 1 << offset
)
63 static void wm831x_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
65 struct wm831x_gpio
*wm831x_gpio
= gpiochip_get_data(chip
);
66 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
68 wm831x_set_bits(wm831x
, WM831X_GPIO_LEVEL
, 1 << offset
,
72 static int wm831x_gpio_direction_out(struct gpio_chip
*chip
,
73 unsigned offset
, int value
)
75 struct wm831x_gpio
*wm831x_gpio
= gpiochip_get_data(chip
);
76 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
80 if (wm831x
->has_gpio_ena
)
81 val
|= WM831X_GPN_TRI
;
83 ret
= wm831x_set_bits(wm831x
, WM831X_GPIO1_CONTROL
+ offset
,
84 WM831X_GPN_DIR
| WM831X_GPN_TRI
|
85 WM831X_GPN_FN_MASK
, val
);
89 /* Can only set GPIO state once it's in output mode */
90 wm831x_gpio_set(chip
, offset
, value
);
95 static int wm831x_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
97 struct wm831x_gpio
*wm831x_gpio
= gpiochip_get_data(chip
);
98 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
100 return irq_create_mapping(wm831x
->irq_domain
,
101 WM831X_IRQ_GPIO_1
+ offset
);
104 static int wm831x_gpio_set_debounce(struct gpio_chip
*chip
, unsigned offset
,
107 struct wm831x_gpio
*wm831x_gpio
= gpiochip_get_data(chip
);
108 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
109 int reg
= WM831X_GPIO1_CONTROL
+ offset
;
112 ret
= wm831x_reg_read(wm831x
, reg
);
116 switch (ret
& WM831X_GPN_FN_MASK
) {
121 /* Not in GPIO mode */
125 if (debounce
>= 32 && debounce
<= 64)
127 else if (debounce
>= 4000 && debounce
<= 8000)
132 return wm831x_set_bits(wm831x
, reg
, WM831X_GPN_FN_MASK
, fn
);
135 #ifdef CONFIG_DEBUG_FS
136 static void wm831x_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
138 struct wm831x_gpio
*wm831x_gpio
= gpiochip_get_data(chip
);
139 struct wm831x
*wm831x
= wm831x_gpio
->wm831x
;
142 for (i
= 0; i
< chip
->ngpio
; i
++) {
143 int gpio
= i
+ chip
->base
;
145 const char *label
, *pull
, *powerdomain
;
147 /* We report the GPIO even if it's not requested since
148 * we're also reporting things like alternate
149 * functions which apply even when the GPIO is not in
152 label
= gpiochip_is_requested(chip
, i
);
154 label
= "Unrequested";
156 seq_printf(s
, " gpio-%-3d (%-20.20s) ", gpio
, label
);
158 reg
= wm831x_reg_read(wm831x
, WM831X_GPIO1_CONTROL
+ i
);
161 "GPIO control %d read failed: %d\n",
167 switch (reg
& WM831X_GPN_PULL_MASK
) {
168 case WM831X_GPIO_PULL_NONE
:
171 case WM831X_GPIO_PULL_DOWN
:
174 case WM831X_GPIO_PULL_UP
:
178 pull
= "INVALID PULL";
185 if (reg
& WM831X_GPN_PWR_DOM
)
186 powerdomain
= "VPMIC";
188 powerdomain
= "DBVDD";
193 if (reg
& WM831X_GPN_PWR_DOM
)
194 powerdomain
= "SYSVDD";
196 powerdomain
= "DBVDD";
200 powerdomain
= "TPVDD";
208 tristated
= reg
& WM831X_GPN_TRI
;
209 if (wm831x
->has_gpio_ena
)
210 tristated
= !tristated
;
212 seq_printf(s
, " %s %s %s %s%s\n"
214 reg
& WM831X_GPN_DIR
? "in" : "out",
215 wm831x_gpio_get(chip
, i
) ? "high" : "low",
218 reg
& WM831X_GPN_POL
? "" : " inverted",
219 reg
& WM831X_GPN_OD
? "open-drain" : "CMOS",
220 tristated
? " tristated" : "",
225 #define wm831x_gpio_dbg_show NULL
228 static struct gpio_chip template_chip
= {
230 .owner
= THIS_MODULE
,
231 .direction_input
= wm831x_gpio_direction_in
,
232 .get
= wm831x_gpio_get
,
233 .direction_output
= wm831x_gpio_direction_out
,
234 .set
= wm831x_gpio_set
,
235 .to_irq
= wm831x_gpio_to_irq
,
236 .set_debounce
= wm831x_gpio_set_debounce
,
237 .dbg_show
= wm831x_gpio_dbg_show
,
241 static int wm831x_gpio_probe(struct platform_device
*pdev
)
243 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
244 struct wm831x_pdata
*pdata
= dev_get_platdata(wm831x
->dev
);
245 struct wm831x_gpio
*wm831x_gpio
;
248 wm831x_gpio
= devm_kzalloc(&pdev
->dev
, sizeof(*wm831x_gpio
),
250 if (wm831x_gpio
== NULL
)
253 wm831x_gpio
->wm831x
= wm831x
;
254 wm831x_gpio
->gpio_chip
= template_chip
;
255 wm831x_gpio
->gpio_chip
.ngpio
= wm831x
->num_gpio
;
256 wm831x_gpio
->gpio_chip
.parent
= &pdev
->dev
;
257 if (pdata
&& pdata
->gpio_base
)
258 wm831x_gpio
->gpio_chip
.base
= pdata
->gpio_base
;
260 wm831x_gpio
->gpio_chip
.base
= -1;
262 ret
= devm_gpiochip_add_data(&pdev
->dev
, &wm831x_gpio
->gpio_chip
,
265 dev_err(&pdev
->dev
, "Could not register gpiochip, %d\n", ret
);
269 platform_set_drvdata(pdev
, wm831x_gpio
);
274 static struct platform_driver wm831x_gpio_driver
= {
275 .driver
.name
= "wm831x-gpio",
276 .driver
.owner
= THIS_MODULE
,
277 .probe
= wm831x_gpio_probe
,
280 static int __init
wm831x_gpio_init(void)
282 return platform_driver_register(&wm831x_gpio_driver
);
284 subsys_initcall(wm831x_gpio_init
);
286 static void __exit
wm831x_gpio_exit(void)
288 platform_driver_unregister(&wm831x_gpio_driver
);
290 module_exit(wm831x_gpio_exit
);
292 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
293 MODULE_DESCRIPTION("GPIO interface for WM831x PMICs");
294 MODULE_LICENSE("GPL");
295 MODULE_ALIAS("platform:wm831x-gpio");