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/cleanup.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/gpio/driver.h>
16 #include <linux/mfd/core.h>
17 #include <linux/platform_device.h>
18 #include <linux/seq_file.h>
20 #include <linux/mfd/wm831x/core.h>
21 #include <linux/mfd/wm831x/pdata.h>
22 #include <linux/mfd/wm831x/gpio.h>
23 #include <linux/mfd/wm831x/irq.h>
26 struct wm831x *wm831x;
27 struct gpio_chip gpio_chip;
30 static int wm831x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
32 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
33 struct wm831x *wm831x = wm831x_gpio->wm831x;
34 int val = WM831X_GPN_DIR;
36 if (wm831x->has_gpio_ena)
37 val |= WM831X_GPN_TRI;
39 return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
40 WM831X_GPN_DIR | WM831X_GPN_TRI |
41 WM831X_GPN_FN_MASK, val);
44 static int wm831x_gpio_get(struct gpio_chip *chip, unsigned offset)
46 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
47 struct wm831x *wm831x = wm831x_gpio->wm831x;
50 ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL);
54 if (ret & 1 << offset)
60 static void wm831x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
62 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
63 struct wm831x *wm831x = wm831x_gpio->wm831x;
65 wm831x_set_bits(wm831x, WM831X_GPIO_LEVEL, 1 << offset,
69 static int wm831x_gpio_direction_out(struct gpio_chip *chip,
70 unsigned offset, int value)
72 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
73 struct wm831x *wm831x = wm831x_gpio->wm831x;
77 if (wm831x->has_gpio_ena)
78 val |= WM831X_GPN_TRI;
80 ret = wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
81 WM831X_GPN_DIR | WM831X_GPN_TRI |
82 WM831X_GPN_FN_MASK, val);
86 /* Can only set GPIO state once it's in output mode */
87 wm831x_gpio_set(chip, offset, value);
92 static int wm831x_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
94 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
95 struct wm831x *wm831x = wm831x_gpio->wm831x;
97 return irq_create_mapping(wm831x->irq_domain,
98 WM831X_IRQ_GPIO_1 + offset);
101 static int wm831x_gpio_set_debounce(struct wm831x *wm831x, unsigned offset,
104 int reg = WM831X_GPIO1_CONTROL + offset;
107 ret = wm831x_reg_read(wm831x, reg);
111 switch (ret & WM831X_GPN_FN_MASK) {
116 /* Not in GPIO mode */
120 if (debounce >= 32 && debounce <= 64)
122 else if (debounce >= 4000 && debounce <= 8000)
127 return wm831x_set_bits(wm831x, reg, WM831X_GPN_FN_MASK, fn);
130 static int wm831x_set_config(struct gpio_chip *chip, unsigned int offset,
131 unsigned long config)
133 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
134 struct wm831x *wm831x = wm831x_gpio->wm831x;
135 int reg = WM831X_GPIO1_CONTROL + offset;
137 switch (pinconf_to_config_param(config)) {
138 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
139 return wm831x_set_bits(wm831x, reg,
140 WM831X_GPN_OD_MASK, WM831X_GPN_OD);
141 case PIN_CONFIG_DRIVE_PUSH_PULL:
142 return wm831x_set_bits(wm831x, reg,
143 WM831X_GPN_OD_MASK, 0);
144 case PIN_CONFIG_INPUT_DEBOUNCE:
145 return wm831x_gpio_set_debounce(wm831x, offset,
146 pinconf_to_config_argument(config));
154 #ifdef CONFIG_DEBUG_FS
155 static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
157 struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
158 struct wm831x *wm831x = wm831x_gpio->wm831x;
161 for (i = 0; i < chip->ngpio; i++) {
162 int gpio = i + chip->base;
164 const char *pull, *powerdomain;
166 /* We report the GPIO even if it's not requested since
167 * we're also reporting things like alternate
168 * functions which apply even when the GPIO is not in
171 char *label __free(kfree) = gpiochip_dup_line_label(chip, i);
173 dev_err(wm831x->dev, "Failed to duplicate label\n");
177 seq_printf(s, " gpio-%-3d (%-20.20s) ",
178 gpio, label ?: "Unrequested");
180 reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i);
183 "GPIO control %d read failed: %d\n",
189 switch (reg & WM831X_GPN_PULL_MASK) {
190 case WM831X_GPIO_PULL_NONE:
193 case WM831X_GPIO_PULL_DOWN:
196 case WM831X_GPIO_PULL_UP:
200 pull = "INVALID PULL";
207 if (reg & WM831X_GPN_PWR_DOM)
208 powerdomain = "VPMIC";
210 powerdomain = "DBVDD";
215 if (reg & WM831X_GPN_PWR_DOM)
216 powerdomain = "SYSVDD";
218 powerdomain = "DBVDD";
222 powerdomain = "TPVDD";
230 tristated = reg & WM831X_GPN_TRI;
231 if (wm831x->has_gpio_ena)
232 tristated = !tristated;
234 seq_printf(s, " %s %s %s %s%s\n"
236 reg & WM831X_GPN_DIR ? "in" : "out",
237 wm831x_gpio_get(chip, i) ? "high" : "low",
240 reg & WM831X_GPN_POL ? "" : " inverted",
241 reg & WM831X_GPN_OD ? "open-drain" : "push-pull",
242 tristated ? " tristated" : "",
247 #define wm831x_gpio_dbg_show NULL
250 static const struct gpio_chip template_chip = {
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,
257 .to_irq = wm831x_gpio_to_irq,
258 .set_config = wm831x_set_config,
259 .dbg_show = wm831x_gpio_dbg_show,
263 static int wm831x_gpio_probe(struct platform_device *pdev)
265 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
266 struct wm831x_pdata *pdata = &wm831x->pdata;
267 struct wm831x_gpio *wm831x_gpio;
269 device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
271 wm831x_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm831x_gpio),
273 if (wm831x_gpio == NULL)
276 wm831x_gpio->wm831x = wm831x;
277 wm831x_gpio->gpio_chip = template_chip;
278 wm831x_gpio->gpio_chip.ngpio = wm831x->num_gpio;
279 wm831x_gpio->gpio_chip.parent = &pdev->dev;
280 if (pdata && pdata->gpio_base)
281 wm831x_gpio->gpio_chip.base = pdata->gpio_base;
283 wm831x_gpio->gpio_chip.base = -1;
285 return devm_gpiochip_add_data(&pdev->dev, &wm831x_gpio->gpio_chip, wm831x_gpio);
288 static struct platform_driver wm831x_gpio_driver = {
289 .driver.name = "wm831x-gpio",
290 .probe = wm831x_gpio_probe,
293 static int __init wm831x_gpio_init(void)
295 return platform_driver_register(&wm831x_gpio_driver);
297 subsys_initcall(wm831x_gpio_init);
299 static void __exit wm831x_gpio_exit(void)
301 platform_driver_unregister(&wm831x_gpio_driver);
303 module_exit(wm831x_gpio_exit);
305 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
306 MODULE_DESCRIPTION("GPIO interface for WM831x PMICs");
307 MODULE_LICENSE("GPL");
308 MODULE_ALIAS("platform:wm831x-gpio");