blob: 938bbe3f831ca0b6511ba913267ae62dbc903301 [file] [log] [blame]
Mark Browne4b736f2009-07-27 14:46:00 +01001/*
Grant Likelyc103de22011-06-04 18:38:28 -06002 * gpiolib support for Wolfson WM831x PMICs
Mark Browne4b736f2009-07-27 14:46:00 +01003 *
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 Browne4b736f2009-07-27 14:46:00 +010017#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>
Mark Browndc0fb252009-11-16 17:21:56 +000026#include <linux/mfd/wm831x/irq.h>
Mark Browne4b736f2009-07-27 14:46:00 +010027
Mark Browne4b736f2009-07-27 14:46:00 +010028struct wm831x_gpio {
29 struct wm831x *wm831x;
30 struct gpio_chip gpio_chip;
31};
32
Mark Browne4b736f2009-07-27 14:46:00 +010033static int wm831x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
34{
Linus Walleij9b3c8172015-12-07 15:11:34 +010035 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
Mark Browne4b736f2009-07-27 14:46:00 +010036 struct wm831x *wm831x = wm831x_gpio->wm831x;
Mark Brownf92e8f82010-02-17 18:45:25 +000037 int val = WM831X_GPN_DIR;
38
39 if (wm831x->has_gpio_ena)
40 val |= WM831X_GPN_TRI;
Mark Browne4b736f2009-07-27 14:46:00 +010041
42 return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
Mark Brown1bca7482010-02-17 18:45:26 +000043 WM831X_GPN_DIR | WM831X_GPN_TRI |
44 WM831X_GPN_FN_MASK, val);
Mark Browne4b736f2009-07-27 14:46:00 +010045}
46
47static int wm831x_gpio_get(struct gpio_chip *chip, unsigned offset)
48{
Linus Walleij9b3c8172015-12-07 15:11:34 +010049 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
Mark Browne4b736f2009-07-27 14:46:00 +010050 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
Mark Browne4b736f2009-07-27 14:46:00 +010063static void wm831x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
64{
Linus Walleij9b3c8172015-12-07 15:11:34 +010065 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
Mark Browne4b736f2009-07-27 14:46:00 +010066 struct wm831x *wm831x = wm831x_gpio->wm831x;
67
68 wm831x_set_bits(wm831x, WM831X_GPIO_LEVEL, 1 << offset,
69 value << offset);
70}
71
Mark Brown3383d232010-02-17 18:04:35 +000072static int wm831x_gpio_direction_out(struct gpio_chip *chip,
73 unsigned offset, int value)
74{
Linus Walleij9b3c8172015-12-07 15:11:34 +010075 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
Mark Brown3383d232010-02-17 18:04:35 +000076 struct wm831x *wm831x = wm831x_gpio->wm831x;
Mark Brownf92e8f82010-02-17 18:45:25 +000077 int val = 0;
Mark Brown3383d232010-02-17 18:04:35 +000078 int ret;
79
Mark Brownf92e8f82010-02-17 18:45:25 +000080 if (wm831x->has_gpio_ena)
81 val |= WM831X_GPN_TRI;
82
Mark Brown3383d232010-02-17 18:04:35 +000083 ret = wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
Mark Brown1bca7482010-02-17 18:45:26 +000084 WM831X_GPN_DIR | WM831X_GPN_TRI |
85 WM831X_GPN_FN_MASK, val);
Mark Brown3383d232010-02-17 18:04:35 +000086 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;
93}
94
Mark Browndc0fb252009-11-16 17:21:56 +000095static int wm831x_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
96{
Linus Walleij9b3c8172015-12-07 15:11:34 +010097 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
Mark Browndc0fb252009-11-16 17:21:56 +000098 struct wm831x *wm831x = wm831x_gpio->wm831x;
99
Mark Browncd997582012-05-14 23:14:24 +0200100 return irq_create_mapping(wm831x->irq_domain,
101 WM831X_IRQ_GPIO_1 + offset);
Mark Browndc0fb252009-11-16 17:21:56 +0000102}
103
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300104static int wm831x_gpio_set_debounce(struct wm831x *wm831x, unsigned offset,
Mark Brownb12c35e2010-07-03 15:40:56 +0200105 unsigned debounce)
106{
Mark Brownb12c35e2010-07-03 15:40:56 +0200107 int reg = WM831X_GPIO1_CONTROL + offset;
108 int ret, fn;
109
110 ret = wm831x_reg_read(wm831x, reg);
111 if (ret < 0)
112 return ret;
113
114 switch (ret & WM831X_GPN_FN_MASK) {
115 case 0:
116 case 1:
117 break;
118 default:
119 /* Not in GPIO mode */
120 return -EBUSY;
121 }
122
123 if (debounce >= 32 && debounce <= 64)
124 fn = 0;
125 else if (debounce >= 4000 && debounce <= 8000)
126 fn = 1;
127 else
128 return -EINVAL;
129
130 return wm831x_set_bits(wm831x, reg, WM831X_GPN_FN_MASK, fn);
131}
132
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300133static int wm831x_set_config(struct gpio_chip *chip, unsigned int offset,
134 unsigned long config)
Linus Walleij51c27da2016-04-10 14:52:33 +0200135{
136 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
137 struct wm831x *wm831x = wm831x_gpio->wm831x;
138 int reg = WM831X_GPIO1_CONTROL + offset;
139
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300140 switch (pinconf_to_config_param(config)) {
141 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
Linus Walleij51c27da2016-04-10 14:52:33 +0200142 return wm831x_set_bits(wm831x, reg,
143 WM831X_GPN_OD_MASK, WM831X_GPN_OD);
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300144 case PIN_CONFIG_DRIVE_PUSH_PULL:
Linus Walleij51c27da2016-04-10 14:52:33 +0200145 return wm831x_set_bits(wm831x, reg,
146 WM831X_GPN_OD_MASK, 0);
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300147 case PIN_CONFIG_INPUT_DEBOUNCE:
148 return wm831x_gpio_set_debounce(wm831x, offset,
149 pinconf_to_config_argument(config));
Linus Walleij51c27da2016-04-10 14:52:33 +0200150 default:
151 break;
152 }
153
154 return -ENOTSUPP;
155}
156
Mark Browne4b736f2009-07-27 14:46:00 +0100157#ifdef CONFIG_DEBUG_FS
158static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
159{
Linus Walleij9b3c8172015-12-07 15:11:34 +0100160 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
Mark Browne4b736f2009-07-27 14:46:00 +0100161 struct wm831x *wm831x = wm831x_gpio->wm831x;
Mark Brownf92e8f82010-02-17 18:45:25 +0000162 int i, tristated;
Mark Browne4b736f2009-07-27 14:46:00 +0100163
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";
Axel Lin164d5c32011-07-10 15:45:07 +0800198 break;
Mark Browne4b736f2009-07-27 14:46:00 +0100199 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
Mark Brownf92e8f82010-02-17 18:45:25 +0000230 tristated = reg & WM831X_GPN_TRI;
231 if (wm831x->has_gpio_ena)
232 tristated = !tristated;
233
Mark Browne4b736f2009-07-27 14:46:00 +0100234 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,
Mark Brown6b8274f2010-02-17 18:45:24 +0000240 reg & WM831X_GPN_POL ? "" : " inverted",
Linus Walleij51c27da2016-04-10 14:52:33 +0200241 reg & WM831X_GPN_OD ? "open-drain" : "push-pull",
Mark Brownf92e8f82010-02-17 18:45:25 +0000242 tristated ? " tristated" : "",
Mark Browne4b736f2009-07-27 14:46:00 +0100243 reg);
244 }
245}
246#else
247#define wm831x_gpio_dbg_show NULL
248#endif
249
Julia Lawalle35b5ab2016-09-11 14:14:37 +0200250static const struct gpio_chip template_chip = {
Mark Browne4b736f2009-07-27 14:46:00 +0100251 .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,
Mark Browndc0fb252009-11-16 17:21:56 +0000257 .to_irq = wm831x_gpio_to_irq,
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300258 .set_config = wm831x_set_config,
Mark Browne4b736f2009-07-27 14:46:00 +0100259 .dbg_show = wm831x_gpio_dbg_show,
Linus Walleij9fb1f392013-12-04 14:42:46 +0100260 .can_sleep = true,
Mark Browne4b736f2009-07-27 14:46:00 +0100261};
262
Bill Pemberton38363092012-11-19 13:22:34 -0500263static int wm831x_gpio_probe(struct platform_device *pdev)
Mark Browne4b736f2009-07-27 14:46:00 +0100264{
265 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
Charles Keepax9674a8d2017-03-17 10:05:19 +0000266 struct wm831x_pdata *pdata = &wm831x->pdata;
Mark Browne4b736f2009-07-27 14:46:00 +0100267 struct wm831x_gpio *wm831x_gpio;
268 int ret;
269
Axel Lin718bc6e2012-09-02 11:58:07 +0800270 wm831x_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm831x_gpio),
271 GFP_KERNEL);
Mark Browne4b736f2009-07-27 14:46:00 +0100272 if (wm831x_gpio == NULL)
273 return -ENOMEM;
274
275 wm831x_gpio->wm831x = wm831x;
276 wm831x_gpio->gpio_chip = template_chip;
Mark Brown6f2ecaa2009-10-01 15:41:05 +0100277 wm831x_gpio->gpio_chip.ngpio = wm831x->num_gpio;
Linus Walleij58383c782015-11-04 09:56:26 +0100278 wm831x_gpio->gpio_chip.parent = &pdev->dev;
Mark Browne4b736f2009-07-27 14:46:00 +0100279 if (pdata && pdata->gpio_base)
280 wm831x_gpio->gpio_chip.base = pdata->gpio_base;
281 else
282 wm831x_gpio->gpio_chip.base = -1;
Charles Keepax9674a8d2017-03-17 10:05:19 +0000283#ifdef CONFIG_OF_GPIO
284 wm831x_gpio->gpio_chip.of_node = wm831x->dev->of_node;
285#endif
Mark Browne4b736f2009-07-27 14:46:00 +0100286
Laxman Dewangan61da4842016-02-22 17:43:28 +0530287 ret = devm_gpiochip_add_data(&pdev->dev, &wm831x_gpio->gpio_chip,
288 wm831x_gpio);
Mark Browne4b736f2009-07-27 14:46:00 +0100289 if (ret < 0) {
Axel Lin718bc6e2012-09-02 11:58:07 +0800290 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
291 return ret;
Mark Browne4b736f2009-07-27 14:46:00 +0100292 }
293
294 platform_set_drvdata(pdev, wm831x_gpio);
295
296 return ret;
Mark Browne4b736f2009-07-27 14:46:00 +0100297}
298
Mark Browne4b736f2009-07-27 14:46:00 +0100299static struct platform_driver wm831x_gpio_driver = {
300 .driver.name = "wm831x-gpio",
Mark Browne4b736f2009-07-27 14:46:00 +0100301 .probe = wm831x_gpio_probe,
Mark Browne4b736f2009-07-27 14:46:00 +0100302};
303
304static int __init wm831x_gpio_init(void)
305{
306 return platform_driver_register(&wm831x_gpio_driver);
307}
308subsys_initcall(wm831x_gpio_init);
309
310static void __exit wm831x_gpio_exit(void)
311{
312 platform_driver_unregister(&wm831x_gpio_driver);
313}
314module_exit(wm831x_gpio_exit);
315
316MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
317MODULE_DESCRIPTION("GPIO interface for WM831x PMICs");
318MODULE_LICENSE("GPL");
319MODULE_ALIAS("platform:wm831x-gpio");