1 // SPDX-License-Identifier: GPL-2.0+
3 * gpiolib support for Wolfson WM831x PMICs
5 * Copyright 2009 Wolfson Microelectronics PLC.
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/gpio/driver.h>
15 #include <linux/mfd/core.h>
16 #include <linux/platform_device.h>
17 #include <linux/seq_file.h>
19 #include <linux/mfd/wm831x/core.h>
20 #include <linux/mfd/wm831x/pdata.h>
21 #include <linux/mfd/wm831x/gpio.h>
22 #include <linux/mfd/wm831x/irq.h>
25 struct wm831x *wm831x;
26 struct gpio_chip gpio_chip;
29 static int wm831x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
31 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
32 struct wm831x *wm831x = wm831x_gpio->wm831x;
33 int val = WM831X_GPN_DIR;
35 if (wm831x->has_gpio_ena)
36 val |= WM831X_GPN_TRI;
38 return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
39 WM831X_GPN_DIR | WM831X_GPN_TRI |
40 WM831X_GPN_FN_MASK, val);
43 static int wm831x_gpio_get(struct gpio_chip *chip, unsigned offset)
45 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
46 struct wm831x *wm831x = wm831x_gpio->wm831x;
49 ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL);
53 if (ret & 1 << offset)
59 static void wm831x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
61 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
62 struct wm831x *wm831x = wm831x_gpio->wm831x;
64 wm831x_set_bits(wm831x, WM831X_GPIO_LEVEL, 1 << offset,
68 static int wm831x_gpio_direction_out(struct gpio_chip *chip,
69 unsigned offset, int value)
71 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
72 struct wm831x *wm831x = wm831x_gpio->wm831x;
76 if (wm831x->has_gpio_ena)
77 val |= WM831X_GPN_TRI;
79 ret = wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
80 WM831X_GPN_DIR | WM831X_GPN_TRI |
81 WM831X_GPN_FN_MASK, val);
85 /* Can only set GPIO state once it's in output mode */
86 wm831x_gpio_set(chip, offset, value);
91 static int wm831x_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
93 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
94 struct wm831x *wm831x = wm831x_gpio->wm831x;
96 return irq_create_mapping(wm831x->irq_domain,
97 WM831X_IRQ_GPIO_1 + offset);
100 static int wm831x_gpio_set_debounce(struct wm831x *wm831x, unsigned offset,
103 int reg = WM831X_GPIO1_CONTROL + offset;
106 ret = wm831x_reg_read(wm831x, reg);
110 switch (ret & WM831X_GPN_FN_MASK) {
115 /* Not in GPIO mode */
119 if (debounce >= 32 && debounce <= 64)
121 else if (debounce >= 4000 && debounce <= 8000)
126 return wm831x_set_bits(wm831x, reg, WM831X_GPN_FN_MASK, fn);
129 static int wm831x_set_config(struct gpio_chip *chip, unsigned int offset,
130 unsigned long config)
132 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
133 struct wm831x *wm831x = wm831x_gpio->wm831x;
134 int reg = WM831X_GPIO1_CONTROL + offset;
136 switch (pinconf_to_config_param(config)) {
137 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
138 return wm831x_set_bits(wm831x, reg,
139 WM831X_GPN_OD_MASK, WM831X_GPN_OD);
140 case PIN_CONFIG_DRIVE_PUSH_PULL:
141 return wm831x_set_bits(wm831x, reg,
142 WM831X_GPN_OD_MASK, 0);
143 case PIN_CONFIG_INPUT_DEBOUNCE:
144 return wm831x_gpio_set_debounce(wm831x, offset,
145 pinconf_to_config_argument(config));
153 #ifdef CONFIG_DEBUG_FS
154 static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
156 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
157 struct wm831x *wm831x = wm831x_gpio->wm831x;
160 for (i = 0; i < chip->ngpio; i++) {
161 int gpio = i + chip->base;
163 const char *label, *pull, *powerdomain;
165 /* We report the GPIO even if it's not requested since
166 * we're also reporting things like alternate
167 * functions which apply even when the GPIO is not in
170 label = gpiochip_is_requested(chip, i);
172 label = "Unrequested";
174 seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
176 reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i);
179 "GPIO control %d read failed: %d\n",
185 switch (reg & WM831X_GPN_PULL_MASK) {
186 case WM831X_GPIO_PULL_NONE:
189 case WM831X_GPIO_PULL_DOWN:
192 case WM831X_GPIO_PULL_UP:
196 pull = "INVALID PULL";
203 if (reg & WM831X_GPN_PWR_DOM)
204 powerdomain = "VPMIC";
206 powerdomain = "DBVDD";
211 if (reg & WM831X_GPN_PWR_DOM)
212 powerdomain = "SYSVDD";
214 powerdomain = "DBVDD";
218 powerdomain = "TPVDD";
226 tristated = reg & WM831X_GPN_TRI;
227 if (wm831x->has_gpio_ena)
228 tristated = !tristated;
230 seq_printf(s, " %s %s %s %s%s\n"
232 reg & WM831X_GPN_DIR ? "in" : "out",
233 wm831x_gpio_get(chip, i) ? "high" : "low",
236 reg & WM831X_GPN_POL ? "" : " inverted",
237 reg & WM831X_GPN_OD ? "open-drain" : "push-pull",
238 tristated ? " tristated" : "",
243 #define wm831x_gpio_dbg_show NULL
246 static const struct gpio_chip template_chip = {
248 .owner = THIS_MODULE,
249 .direction_input = wm831x_gpio_direction_in,
250 .get = wm831x_gpio_get,
251 .direction_output = wm831x_gpio_direction_out,
252 .set = wm831x_gpio_set,
253 .to_irq = wm831x_gpio_to_irq,
254 .set_config = wm831x_set_config,
255 .dbg_show = wm831x_gpio_dbg_show,
259 static int wm831x_gpio_probe(struct platform_device *pdev)
261 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
262 struct wm831x_pdata *pdata = &wm831x->pdata;
263 struct wm831x_gpio *wm831x_gpio;
265 device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
267 wm831x_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm831x_gpio),
269 if (wm831x_gpio == NULL)
272 wm831x_gpio->wm831x = wm831x;
273 wm831x_gpio->gpio_chip = template_chip;
274 wm831x_gpio->gpio_chip.ngpio = wm831x->num_gpio;
275 wm831x_gpio->gpio_chip.parent = &pdev->dev;
276 if (pdata && pdata->gpio_base)
277 wm831x_gpio->gpio_chip.base = pdata->gpio_base;
279 wm831x_gpio->gpio_chip.base = -1;
281 return devm_gpiochip_add_data(&pdev->dev, &wm831x_gpio->gpio_chip, wm831x_gpio);
284 static struct platform_driver wm831x_gpio_driver = {
285 .driver.name = "wm831x-gpio",
286 .probe = wm831x_gpio_probe,
289 static int __init wm831x_gpio_init(void)
291 return platform_driver_register(&wm831x_gpio_driver);
293 subsys_initcall(wm831x_gpio_init);
295 static void __exit wm831x_gpio_exit(void)
297 platform_driver_unregister(&wm831x_gpio_driver);
299 module_exit(wm831x_gpio_exit);
301 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
302 MODULE_DESCRIPTION("GPIO interface for WM831x PMICs");
303 MODULE_LICENSE("GPL");
304 MODULE_ALIAS("platform:wm831x-gpio");