Commit | Line | Data |
---|---|---|
e4b736f1 | 1 | /* |
c103de24 | 2 | * gpiolib support for Wolfson WM831x PMICs |
e4b736f1 MB |
3 | * |
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> | |
5a0e3ad6 | 16 | #include <linux/slab.h> |
e4b736f1 MB |
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> | |
22 | ||
23 | #include <linux/mfd/wm831x/core.h> | |
24 | #include <linux/mfd/wm831x/pdata.h> | |
25 | #include <linux/mfd/wm831x/gpio.h> | |
dc0fb25c | 26 | #include <linux/mfd/wm831x/irq.h> |
e4b736f1 | 27 | |
e4b736f1 MB |
28 | struct wm831x_gpio { |
29 | struct wm831x *wm831x; | |
30 | struct gpio_chip gpio_chip; | |
31 | }; | |
32 | ||
e4b736f1 MB |
33 | static int wm831x_gpio_direction_in(struct gpio_chip *chip, unsigned offset) |
34 | { | |
9b3c817b | 35 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
e4b736f1 | 36 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
f92e8f81 MB |
37 | int val = WM831X_GPN_DIR; |
38 | ||
39 | if (wm831x->has_gpio_ena) | |
40 | val |= WM831X_GPN_TRI; | |
e4b736f1 MB |
41 | |
42 | return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset, | |
1bca748c MB |
43 | WM831X_GPN_DIR | WM831X_GPN_TRI | |
44 | WM831X_GPN_FN_MASK, val); | |
e4b736f1 MB |
45 | } |
46 | ||
47 | static int wm831x_gpio_get(struct gpio_chip *chip, unsigned offset) | |
48 | { | |
9b3c817b | 49 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
e4b736f1 MB |
50 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
51 | int ret; | |
52 | ||
53 | ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL); | |
54 | if (ret < 0) | |
55 | return ret; | |
56 | ||
57 | if (ret & 1 << offset) | |
58 | return 1; | |
59 | else | |
60 | return 0; | |
61 | } | |
62 | ||
3383d23d | 63 | static void wm831x_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
e4b736f1 | 64 | { |
9b3c817b | 65 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
e4b736f1 MB |
66 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
67 | ||
3383d23d MB |
68 | wm831x_set_bits(wm831x, WM831X_GPIO_LEVEL, 1 << offset, |
69 | value << offset); | |
e4b736f1 MB |
70 | } |
71 | ||
3383d23d MB |
72 | static int wm831x_gpio_direction_out(struct gpio_chip *chip, |
73 | unsigned offset, int value) | |
e4b736f1 | 74 | { |
9b3c817b | 75 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
e4b736f1 | 76 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
f92e8f81 | 77 | int val = 0; |
3383d23d | 78 | int ret; |
e4b736f1 | 79 | |
f92e8f81 MB |
80 | if (wm831x->has_gpio_ena) |
81 | val |= WM831X_GPN_TRI; | |
82 | ||
3383d23d | 83 | ret = wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset, |
1bca748c MB |
84 | WM831X_GPN_DIR | WM831X_GPN_TRI | |
85 | WM831X_GPN_FN_MASK, val); | |
3383d23d MB |
86 | if (ret < 0) |
87 | return ret; | |
88 | ||
89 | /* Can only set GPIO state once it's in output mode */ | |
90 | wm831x_gpio_set(chip, offset, value); | |
91 | ||
92 | return 0; | |
e4b736f1 MB |
93 | } |
94 | ||
dc0fb25c MB |
95 | static int wm831x_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
96 | { | |
9b3c817b | 97 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
dc0fb25c MB |
98 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
99 | ||
cd99758b MB |
100 | return irq_create_mapping(wm831x->irq_domain, |
101 | WM831X_IRQ_GPIO_1 + offset); | |
dc0fb25c MB |
102 | } |
103 | ||
b12c35e2 MB |
104 | static int wm831x_gpio_set_debounce(struct gpio_chip *chip, unsigned offset, |
105 | unsigned debounce) | |
106 | { | |
9b3c817b | 107 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
b12c35e2 MB |
108 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
109 | int reg = WM831X_GPIO1_CONTROL + offset; | |
110 | int ret, fn; | |
111 | ||
112 | ret = wm831x_reg_read(wm831x, reg); | |
113 | if (ret < 0) | |
114 | return ret; | |
115 | ||
116 | switch (ret & WM831X_GPN_FN_MASK) { | |
117 | case 0: | |
118 | case 1: | |
119 | break; | |
120 | default: | |
121 | /* Not in GPIO mode */ | |
122 | return -EBUSY; | |
123 | } | |
124 | ||
125 | if (debounce >= 32 && debounce <= 64) | |
126 | fn = 0; | |
127 | else if (debounce >= 4000 && debounce <= 8000) | |
128 | fn = 1; | |
129 | else | |
130 | return -EINVAL; | |
131 | ||
132 | return wm831x_set_bits(wm831x, reg, WM831X_GPN_FN_MASK, fn); | |
133 | } | |
134 | ||
51c27da1 LW |
135 | static int wm831x_set_single_ended(struct gpio_chip *chip, |
136 | unsigned int offset, | |
137 | enum single_ended_mode mode) | |
138 | { | |
139 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); | |
140 | struct wm831x *wm831x = wm831x_gpio->wm831x; | |
141 | int reg = WM831X_GPIO1_CONTROL + offset; | |
142 | ||
143 | switch (mode) { | |
144 | case LINE_MODE_OPEN_DRAIN: | |
145 | return wm831x_set_bits(wm831x, reg, | |
146 | WM831X_GPN_OD_MASK, WM831X_GPN_OD); | |
147 | case LINE_MODE_PUSH_PULL: | |
148 | return wm831x_set_bits(wm831x, reg, | |
149 | WM831X_GPN_OD_MASK, 0); | |
150 | default: | |
151 | break; | |
152 | } | |
153 | ||
154 | return -ENOTSUPP; | |
155 | } | |
156 | ||
e4b736f1 MB |
157 | #ifdef CONFIG_DEBUG_FS |
158 | static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) | |
159 | { | |
9b3c817b | 160 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
e4b736f1 | 161 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
f92e8f81 | 162 | int i, tristated; |
e4b736f1 MB |
163 | |
164 | for (i = 0; i < chip->ngpio; i++) { | |
165 | int gpio = i + chip->base; | |
166 | int reg; | |
167 | const char *label, *pull, *powerdomain; | |
168 | ||
169 | /* We report the GPIO even if it's not requested since | |
170 | * we're also reporting things like alternate | |
171 | * functions which apply even when the GPIO is not in | |
172 | * use as a GPIO. | |
173 | */ | |
174 | label = gpiochip_is_requested(chip, i); | |
175 | if (!label) | |
176 | label = "Unrequested"; | |
177 | ||
178 | seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label); | |
179 | ||
180 | reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i); | |
181 | if (reg < 0) { | |
182 | dev_err(wm831x->dev, | |
183 | "GPIO control %d read failed: %d\n", | |
184 | gpio, reg); | |
185 | seq_printf(s, "\n"); | |
186 | continue; | |
187 | } | |
188 | ||
189 | switch (reg & WM831X_GPN_PULL_MASK) { | |
190 | case WM831X_GPIO_PULL_NONE: | |
191 | pull = "nopull"; | |
192 | break; | |
193 | case WM831X_GPIO_PULL_DOWN: | |
194 | pull = "pulldown"; | |
195 | break; | |
196 | case WM831X_GPIO_PULL_UP: | |
197 | pull = "pullup"; | |
164d5c39 | 198 | break; |
e4b736f1 MB |
199 | default: |
200 | pull = "INVALID PULL"; | |
201 | break; | |
202 | } | |
203 | ||
204 | switch (i + 1) { | |
205 | case 1 ... 3: | |
206 | case 7 ... 9: | |
207 | if (reg & WM831X_GPN_PWR_DOM) | |
208 | powerdomain = "VPMIC"; | |
209 | else | |
210 | powerdomain = "DBVDD"; | |
211 | break; | |
212 | ||
213 | case 4 ... 6: | |
214 | case 10 ... 12: | |
215 | if (reg & WM831X_GPN_PWR_DOM) | |
216 | powerdomain = "SYSVDD"; | |
217 | else | |
218 | powerdomain = "DBVDD"; | |
219 | break; | |
220 | ||
221 | case 13 ... 16: | |
222 | powerdomain = "TPVDD"; | |
223 | break; | |
224 | ||
225 | default: | |
226 | BUG(); | |
227 | break; | |
228 | } | |
229 | ||
f92e8f81 MB |
230 | tristated = reg & WM831X_GPN_TRI; |
231 | if (wm831x->has_gpio_ena) | |
232 | tristated = !tristated; | |
233 | ||
e4b736f1 MB |
234 | seq_printf(s, " %s %s %s %s%s\n" |
235 | " %s%s (0x%4x)\n", | |
236 | reg & WM831X_GPN_DIR ? "in" : "out", | |
237 | wm831x_gpio_get(chip, i) ? "high" : "low", | |
238 | pull, | |
239 | powerdomain, | |
6b8274fa | 240 | reg & WM831X_GPN_POL ? "" : " inverted", |
51c27da1 | 241 | reg & WM831X_GPN_OD ? "open-drain" : "push-pull", |
f92e8f81 | 242 | tristated ? " tristated" : "", |
e4b736f1 MB |
243 | reg); |
244 | } | |
245 | } | |
246 | #else | |
247 | #define wm831x_gpio_dbg_show NULL | |
248 | #endif | |
249 | ||
250 | static struct gpio_chip template_chip = { | |
251 | .label = "wm831x", | |
252 | .owner = THIS_MODULE, | |
253 | .direction_input = wm831x_gpio_direction_in, | |
254 | .get = wm831x_gpio_get, | |
255 | .direction_output = wm831x_gpio_direction_out, | |
256 | .set = wm831x_gpio_set, | |
dc0fb25c | 257 | .to_irq = wm831x_gpio_to_irq, |
b12c35e2 | 258 | .set_debounce = wm831x_gpio_set_debounce, |
51c27da1 | 259 | .set_single_ended = wm831x_set_single_ended, |
e4b736f1 | 260 | .dbg_show = wm831x_gpio_dbg_show, |
9fb1f39e | 261 | .can_sleep = true, |
e4b736f1 MB |
262 | }; |
263 | ||
3836309d | 264 | static int wm831x_gpio_probe(struct platform_device *pdev) |
e4b736f1 MB |
265 | { |
266 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); | |
e56aee18 | 267 | struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev); |
e4b736f1 MB |
268 | struct wm831x_gpio *wm831x_gpio; |
269 | int ret; | |
270 | ||
718bc6e3 AL |
271 | wm831x_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm831x_gpio), |
272 | GFP_KERNEL); | |
e4b736f1 MB |
273 | if (wm831x_gpio == NULL) |
274 | return -ENOMEM; | |
275 | ||
276 | wm831x_gpio->wm831x = wm831x; | |
277 | wm831x_gpio->gpio_chip = template_chip; | |
6f2ecaae | 278 | wm831x_gpio->gpio_chip.ngpio = wm831x->num_gpio; |
58383c78 | 279 | wm831x_gpio->gpio_chip.parent = &pdev->dev; |
e4b736f1 MB |
280 | if (pdata && pdata->gpio_base) |
281 | wm831x_gpio->gpio_chip.base = pdata->gpio_base; | |
282 | else | |
283 | wm831x_gpio->gpio_chip.base = -1; | |
284 | ||
61da4848 LD |
285 | ret = devm_gpiochip_add_data(&pdev->dev, &wm831x_gpio->gpio_chip, |
286 | wm831x_gpio); | |
e4b736f1 | 287 | if (ret < 0) { |
718bc6e3 AL |
288 | dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret); |
289 | return ret; | |
e4b736f1 MB |
290 | } |
291 | ||
292 | platform_set_drvdata(pdev, wm831x_gpio); | |
293 | ||
294 | return ret; | |
e4b736f1 MB |
295 | } |
296 | ||
e4b736f1 MB |
297 | static struct platform_driver wm831x_gpio_driver = { |
298 | .driver.name = "wm831x-gpio", | |
e4b736f1 | 299 | .probe = wm831x_gpio_probe, |
e4b736f1 MB |
300 | }; |
301 | ||
302 | static int __init wm831x_gpio_init(void) | |
303 | { | |
304 | return platform_driver_register(&wm831x_gpio_driver); | |
305 | } | |
306 | subsys_initcall(wm831x_gpio_init); | |
307 | ||
308 | static void __exit wm831x_gpio_exit(void) | |
309 | { | |
310 | platform_driver_unregister(&wm831x_gpio_driver); | |
311 | } | |
312 | module_exit(wm831x_gpio_exit); | |
313 | ||
314 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); | |
315 | MODULE_DESCRIPTION("GPIO interface for WM831x PMICs"); | |
316 | MODULE_LICENSE("GPL"); | |
317 | MODULE_ALIAS("platform:wm831x-gpio"); |