blob: 92ea5350dfe96dd6907ea235bb55b2d25faac08c [file] [log] [blame]
Mark Brown2955c302010-01-29 18:20:30 +00001/*
Grant Likelyc103de22011-06-04 18:38:28 -06002 * gpiolib support for Wolfson WM8994
Mark Brown2955c302010-01-29 18:20:30 +00003 *
4 * Copyright 2009 Wolfson Microelectronics PLC.
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
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.
12 *
13 */
14
15#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Mark Brown2955c302010-01-29 18:20:30 +000017#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>
22
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>
27
28struct wm8994_gpio {
29 struct wm8994 *wm8994;
30 struct gpio_chip gpio_chip;
31};
32
33static inline struct wm8994_gpio *to_wm8994_gpio(struct gpio_chip *chip)
34{
35 return container_of(chip, struct wm8994_gpio, gpio_chip);
36}
37
Mark Brown4b666722010-09-01 10:47:16 +010038static int wm8994_gpio_request(struct gpio_chip *chip, unsigned offset)
39{
40 struct wm8994_gpio *wm8994_gpio = to_wm8994_gpio(chip);
41 struct wm8994 *wm8994 = wm8994_gpio->wm8994;
42
43 switch (wm8994->type) {
44 case WM8958:
45 switch (offset) {
46 case 1:
47 case 2:
48 case 3:
49 case 4:
50 case 6:
51 return -EINVAL;
52 }
53 break;
54 default:
55 break;
56 }
57
58 return 0;
59}
60
Mark Brown2955c302010-01-29 18:20:30 +000061static int wm8994_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
62{
63 struct wm8994_gpio *wm8994_gpio = to_wm8994_gpio(chip);
64 struct wm8994 *wm8994 = wm8994_gpio->wm8994;
65
66 return wm8994_set_bits(wm8994, WM8994_GPIO_1 + offset,
67 WM8994_GPN_DIR, WM8994_GPN_DIR);
68}
69
70static int wm8994_gpio_get(struct gpio_chip *chip, unsigned offset)
71{
72 struct wm8994_gpio *wm8994_gpio = to_wm8994_gpio(chip);
73 struct wm8994 *wm8994 = wm8994_gpio->wm8994;
74 int ret;
75
76 ret = wm8994_reg_read(wm8994, WM8994_GPIO_1 + offset);
77 if (ret < 0)
78 return ret;
79
80 if (ret & WM8994_GPN_LVL)
81 return 1;
82 else
83 return 0;
84}
85
86static int wm8994_gpio_direction_out(struct gpio_chip *chip,
87 unsigned offset, int value)
88{
89 struct wm8994_gpio *wm8994_gpio = to_wm8994_gpio(chip);
90 struct wm8994 *wm8994 = wm8994_gpio->wm8994;
91
92 return wm8994_set_bits(wm8994, WM8994_GPIO_1 + offset,
93 WM8994_GPN_DIR, 0);
94}
95
96static void wm8994_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
97{
98 struct wm8994_gpio *wm8994_gpio = to_wm8994_gpio(chip);
99 struct wm8994 *wm8994 = wm8994_gpio->wm8994;
100
101 if (value)
102 value = WM8994_GPN_LVL;
103
104 wm8994_set_bits(wm8994, WM8994_GPIO_1 + offset, WM8994_GPN_LVL, value);
105}
106
Mark Brownddf438c2010-04-02 14:51:59 +0100107static int wm8994_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
108{
109 struct wm8994_gpio *wm8994_gpio = to_wm8994_gpio(chip);
110 struct wm8994 *wm8994 = wm8994_gpio->wm8994;
111
112 if (!wm8994->irq_base)
113 return -EINVAL;
114
115 return wm8994->irq_base + offset;
116}
117
118
Mark Brown2955c302010-01-29 18:20:30 +0000119#ifdef CONFIG_DEBUG_FS
Mark Brownd0ad5e82011-12-08 00:24:01 +0800120static const char *wm8994_gpio_fn(u16 fn)
121{
122 switch (fn) {
123 case WM8994_GP_FN_PIN_SPECIFIC:
124 return "pin-specific";
125 case WM8994_GP_FN_GPIO:
126 return "GPIO";
127 case WM8994_GP_FN_SDOUT:
128 return "SDOUT";
129 case WM8994_GP_FN_IRQ:
130 return "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:
142 return "FLL1 lock";
143 case WM8994_GP_FN_FLL2_LOCK:
144 return "FLL2 lock";
145 case WM8994_GP_FN_SRC1_LOCK:
146 return "SRC1 lock";
147 case WM8994_GP_FN_SRC2_LOCK:
148 return "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:
158 return "FIFO error";
159 case WM8994_GP_FN_OPCLK:
160 return "OPCLK";
161 case WM8994_GP_FN_THW:
162 return "Thermal warning";
163 case WM8994_GP_FN_DCS_DONE:
164 return "DC servo";
165 case WM8994_GP_FN_FLL1_OUT:
166 return "FLL1 output";
167 case WM8994_GP_FN_FLL2_OUT:
168 return "FLL1 output";
169 default:
170 return "Unknown";
171 }
172}
173
Mark Brown2955c302010-01-29 18:20:30 +0000174static void wm8994_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
175{
176 struct wm8994_gpio *wm8994_gpio = to_wm8994_gpio(chip);
177 struct wm8994 *wm8994 = wm8994_gpio->wm8994;
178 int i;
179
180 for (i = 0; i < chip->ngpio; i++) {
181 int gpio = i + chip->base;
182 int reg;
183 const char *label;
184
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
188 * use as a GPIO.
189 */
190 label = gpiochip_is_requested(chip, i);
191 if (!label)
192 label = "Unrequested";
193
194 seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
195
196 reg = wm8994_reg_read(wm8994, WM8994_GPIO_1 + i);
197 if (reg < 0) {
198 dev_err(wm8994->dev,
199 "GPIO control %d read failed: %d\n",
200 gpio, reg);
201 seq_printf(s, "\n");
202 continue;
203 }
204
Mark Brownd0ad5e82011-12-08 00:24:01 +0800205 if (reg & WM8994_GPN_DIR)
206 seq_printf(s, "in ");
207 else
208 seq_printf(s, "out ");
209
210 if (reg & WM8994_GPN_PU)
211 seq_printf(s, "pull up ");
212
213 if (reg & WM8994_GPN_PD)
214 seq_printf(s, "pull down ");
215
216 if (reg & WM8994_GPN_POL)
217 seq_printf(s, "inverted ");
218 else
219 seq_printf(s, "noninverted ");
220
221 if (reg & WM8994_GPN_OP_CFG)
222 seq_printf(s, "open drain ");
223 else
224 seq_printf(s, "CMOS ");
225
226 seq_printf(s, "%s (%x)\n",
227 wm8994_gpio_fn(reg & WM8994_GPN_FN_MASK), reg);
Mark Brown2955c302010-01-29 18:20:30 +0000228 }
229}
230#else
231#define wm8994_gpio_dbg_show NULL
232#endif
233
234static struct gpio_chip template_chip = {
235 .label = "wm8994",
236 .owner = THIS_MODULE,
Mark Brown4b666722010-09-01 10:47:16 +0100237 .request = wm8994_gpio_request,
Mark Brown2955c302010-01-29 18:20:30 +0000238 .direction_input = wm8994_gpio_direction_in,
239 .get = wm8994_gpio_get,
240 .direction_output = wm8994_gpio_direction_out,
241 .set = wm8994_gpio_set,
Mark Browndeb26e92010-08-17 13:13:37 +0100242 .to_irq = wm8994_gpio_to_irq,
Mark Brown2955c302010-01-29 18:20:30 +0000243 .dbg_show = wm8994_gpio_dbg_show,
244 .can_sleep = 1,
245};
246
247static int __devinit wm8994_gpio_probe(struct platform_device *pdev)
248{
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;
252 int ret;
253
254 wm8994_gpio = kzalloc(sizeof(*wm8994_gpio), GFP_KERNEL);
255 if (wm8994_gpio == NULL)
256 return -ENOMEM;
257
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;
264 else
265 wm8994_gpio->gpio_chip.base = -1;
266
267 ret = gpiochip_add(&wm8994_gpio->gpio_chip);
268 if (ret < 0) {
269 dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
270 ret);
271 goto err;
272 }
273
274 platform_set_drvdata(pdev, wm8994_gpio);
275
276 return ret;
277
278err:
279 kfree(wm8994_gpio);
280 return ret;
281}
282
283static int __devexit wm8994_gpio_remove(struct platform_device *pdev)
284{
285 struct wm8994_gpio *wm8994_gpio = platform_get_drvdata(pdev);
286 int ret;
287
288 ret = gpiochip_remove(&wm8994_gpio->gpio_chip);
289 if (ret == 0)
290 kfree(wm8994_gpio);
291
292 return ret;
293}
294
295static 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),
300};
301
302static int __init wm8994_gpio_init(void)
303{
304 return platform_driver_register(&wm8994_gpio_driver);
305}
306subsys_initcall(wm8994_gpio_init);
307
308static void __exit wm8994_gpio_exit(void)
309{
310 platform_driver_unregister(&wm8994_gpio_driver);
311}
312module_exit(wm8994_gpio_exit);
313
314MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
315MODULE_DESCRIPTION("GPIO interface for WM8994");
316MODULE_LICENSE("GPL");
317MODULE_ALIAS("platform:wm8994-gpio");