xref: /linux/drivers/gpio/gpio-wm831x.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * gpiolib support for Wolfson WM831x PMICs
4  *
5  * Copyright 2009 Wolfson Microelectronics PLC.
6  *
7  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8  *
9  */
10 
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>
19 #include <linux/string_choices.h>
20 
21 #include <linux/mfd/wm831x/core.h>
22 #include <linux/mfd/wm831x/pdata.h>
23 #include <linux/mfd/wm831x/gpio.h>
24 #include <linux/mfd/wm831x/irq.h>
25 
26 struct wm831x_gpio {
27 	struct wm831x *wm831x;
28 	struct gpio_chip gpio_chip;
29 };
30 
31 static int wm831x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
32 {
33 	struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
34 	struct wm831x *wm831x = wm831x_gpio->wm831x;
35 	int val = WM831X_GPN_DIR;
36 
37 	if (wm831x->has_gpio_ena)
38 		val |= WM831X_GPN_TRI;
39 
40 	return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
41 			       WM831X_GPN_DIR | WM831X_GPN_TRI |
42 			       WM831X_GPN_FN_MASK, val);
43 }
44 
45 static int wm831x_gpio_get(struct gpio_chip *chip, unsigned offset)
46 {
47 	struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
48 	struct wm831x *wm831x = wm831x_gpio->wm831x;
49 	int ret;
50 
51 	ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL);
52 	if (ret < 0)
53 		return ret;
54 
55 	if (ret & 1 << offset)
56 		return 1;
57 	else
58 		return 0;
59 }
60 
61 static int wm831x_gpio_set(struct gpio_chip *chip, unsigned int offset,
62 			   int value)
63 {
64 	struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
65 	struct wm831x *wm831x = wm831x_gpio->wm831x;
66 
67 	return wm831x_set_bits(wm831x, WM831X_GPIO_LEVEL, 1 << offset,
68 			       value << offset);
69 }
70 
71 static int wm831x_gpio_direction_out(struct gpio_chip *chip,
72 				     unsigned offset, int value)
73 {
74 	struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
75 	struct wm831x *wm831x = wm831x_gpio->wm831x;
76 	int val = 0;
77 	int ret;
78 
79 	if (wm831x->has_gpio_ena)
80 		val |= WM831X_GPN_TRI;
81 
82 	ret = wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
83 			      WM831X_GPN_DIR | WM831X_GPN_TRI |
84 			      WM831X_GPN_FN_MASK, val);
85 	if (ret < 0)
86 		return ret;
87 
88 	/* Can only set GPIO state once it's in output mode */
89 	return wm831x_gpio_set(chip, offset, value);
90 }
91 
92 static int wm831x_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
93 {
94 	struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
95 	struct wm831x *wm831x = wm831x_gpio->wm831x;
96 
97 	return irq_create_mapping(wm831x->irq_domain,
98 				  WM831X_IRQ_GPIO_1 + offset);
99 }
100 
101 static int wm831x_gpio_set_debounce(struct wm831x *wm831x, unsigned offset,
102 				    unsigned debounce)
103 {
104 	int reg = WM831X_GPIO1_CONTROL + offset;
105 	int ret, fn;
106 
107 	ret = wm831x_reg_read(wm831x, reg);
108 	if (ret < 0)
109 		return ret;
110 
111 	switch (ret & WM831X_GPN_FN_MASK) {
112 	case 0:
113 	case 1:
114 		break;
115 	default:
116 		/* Not in GPIO mode */
117 		return -EBUSY;
118 	}
119 
120 	if (debounce >= 32 && debounce <= 64)
121 		fn = 0;
122 	else if (debounce >= 4000 && debounce <= 8000)
123 		fn = 1;
124 	else
125 		return -EINVAL;
126 
127 	return wm831x_set_bits(wm831x, reg, WM831X_GPN_FN_MASK, fn);
128 }
129 
130 static int wm831x_set_config(struct gpio_chip *chip, unsigned int offset,
131 			     unsigned long config)
132 {
133 	struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
134 	struct wm831x *wm831x = wm831x_gpio->wm831x;
135 	int reg = WM831X_GPIO1_CONTROL + offset;
136 
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));
147 	default:
148 		break;
149 	}
150 
151 	return -ENOTSUPP;
152 }
153 
154 #ifdef CONFIG_DEBUG_FS
155 static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
156 {
157 	struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
158 	struct wm831x *wm831x = wm831x_gpio->wm831x;
159 	int i, tristated;
160 
161 	for (i = 0; i < chip->ngpio; i++) {
162 		int reg;
163 		const char *pull, *powerdomain;
164 
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
168 		 * use as a GPIO.
169 		 */
170 		char *label __free(kfree) = gpiochip_dup_line_label(chip, i);
171 		if (IS_ERR(label)) {
172 			dev_err(wm831x->dev, "Failed to duplicate label\n");
173 			continue;
174 		}
175 
176 		seq_printf(s, " gpio-%-3d (%-20.20s) ",
177 			   i, label ?: "Unrequested");
178 
179 		reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i);
180 		if (reg < 0) {
181 			dev_err(wm831x->dev,
182 				"GPIO control %d read failed: %d\n",
183 				i, reg);
184 			seq_putc(s, '\n');
185 			continue;
186 		}
187 
188 		switch (reg & WM831X_GPN_PULL_MASK) {
189 		case WM831X_GPIO_PULL_NONE:
190 			pull = "nopull";
191 			break;
192 		case WM831X_GPIO_PULL_DOWN:
193 			pull = "pulldown";
194 			break;
195 		case WM831X_GPIO_PULL_UP:
196 			pull = "pullup";
197 			break;
198 		default:
199 			pull = "INVALID PULL";
200 			break;
201 		}
202 
203 		switch (i + 1) {
204 		case 1 ... 3:
205 		case 7 ... 9:
206 			if (reg & WM831X_GPN_PWR_DOM)
207 				powerdomain = "VPMIC";
208 			else
209 				powerdomain = "DBVDD";
210 			break;
211 
212 		case 4 ... 6:
213 		case 10 ... 12:
214 			if (reg & WM831X_GPN_PWR_DOM)
215 				powerdomain = "SYSVDD";
216 			else
217 				powerdomain = "DBVDD";
218 			break;
219 
220 		case 13 ... 16:
221 			powerdomain = "TPVDD";
222 			break;
223 
224 		default:
225 			BUG();
226 			break;
227 		}
228 
229 		tristated = reg & WM831X_GPN_TRI;
230 		if (wm831x->has_gpio_ena)
231 			tristated = !tristated;
232 
233 		seq_printf(s, " %s %s %s %s%s\n"
234 			   "                                  %s%s (0x%4x)\n",
235 			   reg & WM831X_GPN_DIR ? "in" : "out",
236 			   str_high_low(wm831x_gpio_get(chip, i)),
237 			   pull,
238 			   powerdomain,
239 			   reg & WM831X_GPN_POL ? "" : " inverted",
240 			   reg & WM831X_GPN_OD ? "open-drain" : "push-pull",
241 			   tristated ? " tristated" : "",
242 			   reg);
243 	}
244 }
245 #else
246 #define wm831x_gpio_dbg_show NULL
247 #endif
248 
249 static const struct gpio_chip template_chip = {
250 	.label			= "wm831x",
251 	.owner			= THIS_MODULE,
252 	.direction_input	= wm831x_gpio_direction_in,
253 	.get			= wm831x_gpio_get,
254 	.direction_output	= wm831x_gpio_direction_out,
255 	.set			= wm831x_gpio_set,
256 	.to_irq			= wm831x_gpio_to_irq,
257 	.set_config		= wm831x_set_config,
258 	.dbg_show		= wm831x_gpio_dbg_show,
259 	.can_sleep		= true,
260 };
261 
262 static int wm831x_gpio_probe(struct platform_device *pdev)
263 {
264 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
265 	struct wm831x_pdata *pdata = &wm831x->pdata;
266 	struct wm831x_gpio *wm831x_gpio;
267 
268 	device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
269 
270 	wm831x_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm831x_gpio),
271 				   GFP_KERNEL);
272 	if (wm831x_gpio == NULL)
273 		return -ENOMEM;
274 
275 	wm831x_gpio->wm831x = wm831x;
276 	wm831x_gpio->gpio_chip = template_chip;
277 	wm831x_gpio->gpio_chip.ngpio = wm831x->num_gpio;
278 	wm831x_gpio->gpio_chip.parent = &pdev->dev;
279 	if (pdata && pdata->gpio_base)
280 		wm831x_gpio->gpio_chip.base = pdata->gpio_base;
281 	else
282 		wm831x_gpio->gpio_chip.base = -1;
283 
284 	return devm_gpiochip_add_data(&pdev->dev, &wm831x_gpio->gpio_chip, wm831x_gpio);
285 }
286 
287 static struct platform_driver wm831x_gpio_driver = {
288 	.driver.name	= "wm831x-gpio",
289 	.probe		= wm831x_gpio_probe,
290 };
291 
292 static int __init wm831x_gpio_init(void)
293 {
294 	return platform_driver_register(&wm831x_gpio_driver);
295 }
296 subsys_initcall(wm831x_gpio_init);
297 
298 static void __exit wm831x_gpio_exit(void)
299 {
300 	platform_driver_unregister(&wm831x_gpio_driver);
301 }
302 module_exit(wm831x_gpio_exit);
303 
304 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
305 MODULE_DESCRIPTION("GPIO interface for WM831x PMICs");
306 MODULE_LICENSE("GPL");
307 MODULE_ALIAS("platform:wm831x-gpio");
308