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