2 * gpiolib support for Wolfson WM8994
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/wm8994/core.h>
24 #include <linux/mfd/wm8994/pdata.h>
25 #include <linux/mfd/wm8994/gpio.h>
26 #include <linux/mfd/wm8994/registers.h>
29 struct wm8994
*wm8994
;
30 struct gpio_chip gpio_chip
;
33 static inline struct wm8994_gpio
*to_wm8994_gpio(struct gpio_chip
*chip
)
35 return container_of(chip
, struct wm8994_gpio
, gpio_chip
);
38 static int wm8994_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
40 struct wm8994_gpio
*wm8994_gpio
= to_wm8994_gpio(chip
);
41 struct wm8994
*wm8994
= wm8994_gpio
->wm8994
;
43 switch (wm8994
->type
) {
61 static int wm8994_gpio_direction_in(struct gpio_chip
*chip
, unsigned offset
)
63 struct wm8994_gpio
*wm8994_gpio
= to_wm8994_gpio(chip
);
64 struct wm8994
*wm8994
= wm8994_gpio
->wm8994
;
66 return wm8994_set_bits(wm8994
, WM8994_GPIO_1
+ offset
,
67 WM8994_GPN_DIR
, WM8994_GPN_DIR
);
70 static int wm8994_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
72 struct wm8994_gpio
*wm8994_gpio
= to_wm8994_gpio(chip
);
73 struct wm8994
*wm8994
= wm8994_gpio
->wm8994
;
76 ret
= wm8994_reg_read(wm8994
, WM8994_GPIO_1
+ offset
);
80 if (ret
& WM8994_GPN_LVL
)
86 static int wm8994_gpio_direction_out(struct gpio_chip
*chip
,
87 unsigned offset
, int value
)
89 struct wm8994_gpio
*wm8994_gpio
= to_wm8994_gpio(chip
);
90 struct wm8994
*wm8994
= wm8994_gpio
->wm8994
;
92 return wm8994_set_bits(wm8994
, WM8994_GPIO_1
+ offset
,
96 static void wm8994_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
98 struct wm8994_gpio
*wm8994_gpio
= to_wm8994_gpio(chip
);
99 struct wm8994
*wm8994
= wm8994_gpio
->wm8994
;
102 value
= WM8994_GPN_LVL
;
104 wm8994_set_bits(wm8994
, WM8994_GPIO_1
+ offset
, WM8994_GPN_LVL
, value
);
107 static int wm8994_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
109 struct wm8994_gpio
*wm8994_gpio
= to_wm8994_gpio(chip
);
110 struct wm8994
*wm8994
= wm8994_gpio
->wm8994
;
112 if (!wm8994
->irq_base
)
115 return wm8994
->irq_base
+ offset
;
119 #ifdef CONFIG_DEBUG_FS
120 static const char *wm8994_gpio_fn(u16 fn
)
123 case WM8994_GP_FN_PIN_SPECIFIC
:
124 return "pin-specific";
125 case WM8994_GP_FN_GPIO
:
127 case WM8994_GP_FN_SDOUT
:
129 case WM8994_GP_FN_IRQ
:
131 case WM8994_GP_FN_TEMPERATURE
:
132 return "Temperature";
133 case WM8994_GP_FN_MICBIAS1_DET
:
134 return "MICBIAS1 detect";
135 case WM8994_GP_FN_MICBIAS1_SHORT
:
136 return "MICBIAS1 short";
137 case WM8994_GP_FN_MICBIAS2_DET
:
138 return "MICBIAS2 detect";
139 case WM8994_GP_FN_MICBIAS2_SHORT
:
140 return "MICBIAS2 short";
141 case WM8994_GP_FN_FLL1_LOCK
:
143 case WM8994_GP_FN_FLL2_LOCK
:
145 case WM8994_GP_FN_SRC1_LOCK
:
147 case WM8994_GP_FN_SRC2_LOCK
:
149 case WM8994_GP_FN_DRC1_ACT
:
150 return "DRC1 activity";
151 case WM8994_GP_FN_DRC2_ACT
:
152 return "DRC2 activity";
153 case WM8994_GP_FN_DRC3_ACT
:
154 return "DRC3 activity";
155 case WM8994_GP_FN_WSEQ_STATUS
:
156 return "Write sequencer";
157 case WM8994_GP_FN_FIFO_ERROR
:
159 case WM8994_GP_FN_OPCLK
:
161 case WM8994_GP_FN_THW
:
162 return "Thermal warning";
163 case WM8994_GP_FN_DCS_DONE
:
165 case WM8994_GP_FN_FLL1_OUT
:
166 return "FLL1 output";
167 case WM8994_GP_FN_FLL2_OUT
:
168 return "FLL1 output";
174 static void wm8994_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
176 struct wm8994_gpio
*wm8994_gpio
= to_wm8994_gpio(chip
);
177 struct wm8994
*wm8994
= wm8994_gpio
->wm8994
;
180 for (i
= 0; i
< chip
->ngpio
; i
++) {
181 int gpio
= i
+ chip
->base
;
185 /* We report the GPIO even if it's not requested since
186 * we're also reporting things like alternate
187 * functions which apply even when the GPIO is not in
190 label
= gpiochip_is_requested(chip
, i
);
192 label
= "Unrequested";
194 seq_printf(s
, " gpio-%-3d (%-20.20s) ", gpio
, label
);
196 reg
= wm8994_reg_read(wm8994
, WM8994_GPIO_1
+ i
);
199 "GPIO control %d read failed: %d\n",
205 if (reg
& WM8994_GPN_DIR
)
206 seq_printf(s
, "in ");
208 seq_printf(s
, "out ");
210 if (reg
& WM8994_GPN_PU
)
211 seq_printf(s
, "pull up ");
213 if (reg
& WM8994_GPN_PD
)
214 seq_printf(s
, "pull down ");
216 if (reg
& WM8994_GPN_POL
)
217 seq_printf(s
, "inverted ");
219 seq_printf(s
, "noninverted ");
221 if (reg
& WM8994_GPN_OP_CFG
)
222 seq_printf(s
, "open drain ");
224 seq_printf(s
, "CMOS ");
226 seq_printf(s
, "%s (%x)\n",
227 wm8994_gpio_fn(reg
& WM8994_GPN_FN_MASK
), reg
);
231 #define wm8994_gpio_dbg_show NULL
234 static struct gpio_chip template_chip
= {
236 .owner
= THIS_MODULE
,
237 .request
= wm8994_gpio_request
,
238 .direction_input
= wm8994_gpio_direction_in
,
239 .get
= wm8994_gpio_get
,
240 .direction_output
= wm8994_gpio_direction_out
,
241 .set
= wm8994_gpio_set
,
242 .to_irq
= wm8994_gpio_to_irq
,
243 .dbg_show
= wm8994_gpio_dbg_show
,
247 static int __devinit
wm8994_gpio_probe(struct platform_device
*pdev
)
249 struct wm8994
*wm8994
= dev_get_drvdata(pdev
->dev
.parent
);
250 struct wm8994_pdata
*pdata
= wm8994
->dev
->platform_data
;
251 struct wm8994_gpio
*wm8994_gpio
;
254 wm8994_gpio
= kzalloc(sizeof(*wm8994_gpio
), GFP_KERNEL
);
255 if (wm8994_gpio
== NULL
)
258 wm8994_gpio
->wm8994
= wm8994
;
259 wm8994_gpio
->gpio_chip
= template_chip
;
260 wm8994_gpio
->gpio_chip
.ngpio
= WM8994_GPIO_MAX
;
261 wm8994_gpio
->gpio_chip
.dev
= &pdev
->dev
;
262 if (pdata
&& pdata
->gpio_base
)
263 wm8994_gpio
->gpio_chip
.base
= pdata
->gpio_base
;
265 wm8994_gpio
->gpio_chip
.base
= -1;
267 ret
= gpiochip_add(&wm8994_gpio
->gpio_chip
);
269 dev_err(&pdev
->dev
, "Could not register gpiochip, %d\n",
274 platform_set_drvdata(pdev
, wm8994_gpio
);
283 static int __devexit
wm8994_gpio_remove(struct platform_device
*pdev
)
285 struct wm8994_gpio
*wm8994_gpio
= platform_get_drvdata(pdev
);
288 ret
= gpiochip_remove(&wm8994_gpio
->gpio_chip
);
295 static struct platform_driver wm8994_gpio_driver
= {
296 .driver
.name
= "wm8994-gpio",
297 .driver
.owner
= THIS_MODULE
,
298 .probe
= wm8994_gpio_probe
,
299 .remove
= __devexit_p(wm8994_gpio_remove
),
302 static int __init
wm8994_gpio_init(void)
304 return platform_driver_register(&wm8994_gpio_driver
);
306 subsys_initcall(wm8994_gpio_init
);
308 static void __exit
wm8994_gpio_exit(void)
310 platform_driver_unregister(&wm8994_gpio_driver
);
312 module_exit(wm8994_gpio_exit
);
314 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
315 MODULE_DESCRIPTION("GPIO interface for WM8994");
316 MODULE_LICENSE("GPL");
317 MODULE_ALIAS("platform:wm8994-gpio");