Linus Walleij | 66dbe75 | 2018-09-13 13:28:13 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 2 | /* |
Grant Likely | c103de2 | 2011-06-04 18:38:28 -0600 | [diff] [blame] | 3 | * gpiolib support for Wolfson WM831x PMICs |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 4 | * |
| 5 | * Copyright 2009 Wolfson Microelectronics PLC. |
| 6 | * |
| 7 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 8 | * |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 13 | #include <linux/module.h> |
Linus Walleij | 10833c4 | 2018-09-13 13:25:58 +0200 | [diff] [blame] | 14 | #include <linux/gpio/driver.h> |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 15 | #include <linux/mfd/core.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/seq_file.h> |
| 18 | |
| 19 | #include <linux/mfd/wm831x/core.h> |
| 20 | #include <linux/mfd/wm831x/pdata.h> |
| 21 | #include <linux/mfd/wm831x/gpio.h> |
Mark Brown | dc0fb25 | 2009-11-16 17:21:56 +0000 | [diff] [blame] | 22 | #include <linux/mfd/wm831x/irq.h> |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 23 | |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 24 | struct wm831x_gpio { |
| 25 | struct wm831x *wm831x; |
| 26 | struct gpio_chip gpio_chip; |
| 27 | }; |
| 28 | |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 29 | static int wm831x_gpio_direction_in(struct gpio_chip *chip, unsigned offset) |
| 30 | { |
Linus Walleij | 9b3c817 | 2015-12-07 15:11:34 +0100 | [diff] [blame] | 31 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 32 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
Mark Brown | f92e8f8 | 2010-02-17 18:45:25 +0000 | [diff] [blame] | 33 | int val = WM831X_GPN_DIR; |
| 34 | |
| 35 | if (wm831x->has_gpio_ena) |
| 36 | val |= WM831X_GPN_TRI; |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 37 | |
| 38 | return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset, |
Mark Brown | 1bca748 | 2010-02-17 18:45:26 +0000 | [diff] [blame] | 39 | WM831X_GPN_DIR | WM831X_GPN_TRI | |
| 40 | WM831X_GPN_FN_MASK, val); |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static int wm831x_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 44 | { |
Linus Walleij | 9b3c817 | 2015-12-07 15:11:34 +0100 | [diff] [blame] | 45 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 46 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
| 47 | int ret; |
| 48 | |
| 49 | ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL); |
| 50 | if (ret < 0) |
| 51 | return ret; |
| 52 | |
| 53 | if (ret & 1 << offset) |
| 54 | return 1; |
| 55 | else |
| 56 | return 0; |
| 57 | } |
| 58 | |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 59 | static void wm831x_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 60 | { |
Linus Walleij | 9b3c817 | 2015-12-07 15:11:34 +0100 | [diff] [blame] | 61 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 62 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
| 63 | |
| 64 | wm831x_set_bits(wm831x, WM831X_GPIO_LEVEL, 1 << offset, |
| 65 | value << offset); |
| 66 | } |
| 67 | |
Mark Brown | 3383d23 | 2010-02-17 18:04:35 +0000 | [diff] [blame] | 68 | static int wm831x_gpio_direction_out(struct gpio_chip *chip, |
| 69 | unsigned offset, int value) |
| 70 | { |
Linus Walleij | 9b3c817 | 2015-12-07 15:11:34 +0100 | [diff] [blame] | 71 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
Mark Brown | 3383d23 | 2010-02-17 18:04:35 +0000 | [diff] [blame] | 72 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
Mark Brown | f92e8f8 | 2010-02-17 18:45:25 +0000 | [diff] [blame] | 73 | int val = 0; |
Mark Brown | 3383d23 | 2010-02-17 18:04:35 +0000 | [diff] [blame] | 74 | int ret; |
| 75 | |
Mark Brown | f92e8f8 | 2010-02-17 18:45:25 +0000 | [diff] [blame] | 76 | if (wm831x->has_gpio_ena) |
| 77 | val |= WM831X_GPN_TRI; |
| 78 | |
Mark Brown | 3383d23 | 2010-02-17 18:04:35 +0000 | [diff] [blame] | 79 | ret = wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset, |
Mark Brown | 1bca748 | 2010-02-17 18:45:26 +0000 | [diff] [blame] | 80 | WM831X_GPN_DIR | WM831X_GPN_TRI | |
| 81 | WM831X_GPN_FN_MASK, val); |
Mark Brown | 3383d23 | 2010-02-17 18:04:35 +0000 | [diff] [blame] | 82 | if (ret < 0) |
| 83 | return ret; |
| 84 | |
| 85 | /* Can only set GPIO state once it's in output mode */ |
| 86 | wm831x_gpio_set(chip, offset, value); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
Mark Brown | dc0fb25 | 2009-11-16 17:21:56 +0000 | [diff] [blame] | 91 | static int wm831x_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 92 | { |
Linus Walleij | 9b3c817 | 2015-12-07 15:11:34 +0100 | [diff] [blame] | 93 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
Mark Brown | dc0fb25 | 2009-11-16 17:21:56 +0000 | [diff] [blame] | 94 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
| 95 | |
Mark Brown | cd99758 | 2012-05-14 23:14:24 +0200 | [diff] [blame] | 96 | return irq_create_mapping(wm831x->irq_domain, |
| 97 | WM831X_IRQ_GPIO_1 + offset); |
Mark Brown | dc0fb25 | 2009-11-16 17:21:56 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 100 | static int wm831x_gpio_set_debounce(struct wm831x *wm831x, unsigned offset, |
Mark Brown | b12c35e | 2010-07-03 15:40:56 +0200 | [diff] [blame] | 101 | unsigned debounce) |
| 102 | { |
Mark Brown | b12c35e | 2010-07-03 15:40:56 +0200 | [diff] [blame] | 103 | int reg = WM831X_GPIO1_CONTROL + offset; |
| 104 | int ret, fn; |
| 105 | |
| 106 | ret = wm831x_reg_read(wm831x, reg); |
| 107 | if (ret < 0) |
| 108 | return ret; |
| 109 | |
| 110 | switch (ret & WM831X_GPN_FN_MASK) { |
| 111 | case 0: |
| 112 | case 1: |
| 113 | break; |
| 114 | default: |
| 115 | /* Not in GPIO mode */ |
| 116 | return -EBUSY; |
| 117 | } |
| 118 | |
| 119 | if (debounce >= 32 && debounce <= 64) |
| 120 | fn = 0; |
| 121 | else if (debounce >= 4000 && debounce <= 8000) |
| 122 | fn = 1; |
| 123 | else |
| 124 | return -EINVAL; |
| 125 | |
| 126 | return wm831x_set_bits(wm831x, reg, WM831X_GPN_FN_MASK, fn); |
| 127 | } |
| 128 | |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 129 | static int wm831x_set_config(struct gpio_chip *chip, unsigned int offset, |
| 130 | unsigned long config) |
Linus Walleij | 51c27da | 2016-04-10 14:52:33 +0200 | [diff] [blame] | 131 | { |
| 132 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
| 133 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
| 134 | int reg = WM831X_GPIO1_CONTROL + offset; |
| 135 | |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 136 | switch (pinconf_to_config_param(config)) { |
| 137 | case PIN_CONFIG_DRIVE_OPEN_DRAIN: |
Linus Walleij | 51c27da | 2016-04-10 14:52:33 +0200 | [diff] [blame] | 138 | return wm831x_set_bits(wm831x, reg, |
| 139 | WM831X_GPN_OD_MASK, WM831X_GPN_OD); |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 140 | case PIN_CONFIG_DRIVE_PUSH_PULL: |
Linus Walleij | 51c27da | 2016-04-10 14:52:33 +0200 | [diff] [blame] | 141 | return wm831x_set_bits(wm831x, reg, |
| 142 | WM831X_GPN_OD_MASK, 0); |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 143 | case PIN_CONFIG_INPUT_DEBOUNCE: |
| 144 | return wm831x_gpio_set_debounce(wm831x, offset, |
| 145 | pinconf_to_config_argument(config)); |
Linus Walleij | 51c27da | 2016-04-10 14:52:33 +0200 | [diff] [blame] | 146 | default: |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | return -ENOTSUPP; |
| 151 | } |
| 152 | |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 153 | #ifdef CONFIG_DEBUG_FS |
| 154 | static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 155 | { |
Linus Walleij | 9b3c817 | 2015-12-07 15:11:34 +0100 | [diff] [blame] | 156 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 157 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
Mark Brown | f92e8f8 | 2010-02-17 18:45:25 +0000 | [diff] [blame] | 158 | int i, tristated; |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 159 | |
| 160 | for (i = 0; i < chip->ngpio; i++) { |
| 161 | int gpio = i + chip->base; |
| 162 | int reg; |
| 163 | const char *label, *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 | label = gpiochip_is_requested(chip, i); |
| 171 | if (!label) |
| 172 | label = "Unrequested"; |
| 173 | |
| 174 | seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label); |
| 175 | |
| 176 | reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i); |
| 177 | if (reg < 0) { |
| 178 | dev_err(wm831x->dev, |
| 179 | "GPIO control %d read failed: %d\n", |
| 180 | gpio, reg); |
Markus Elfring | b5cd780 | 2018-01-12 21:30:09 +0100 | [diff] [blame] | 181 | seq_putc(s, '\n'); |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 182 | continue; |
| 183 | } |
| 184 | |
| 185 | switch (reg & WM831X_GPN_PULL_MASK) { |
| 186 | case WM831X_GPIO_PULL_NONE: |
| 187 | pull = "nopull"; |
| 188 | break; |
| 189 | case WM831X_GPIO_PULL_DOWN: |
| 190 | pull = "pulldown"; |
| 191 | break; |
| 192 | case WM831X_GPIO_PULL_UP: |
| 193 | pull = "pullup"; |
Axel Lin | 164d5c3 | 2011-07-10 15:45:07 +0800 | [diff] [blame] | 194 | break; |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 195 | default: |
| 196 | pull = "INVALID PULL"; |
| 197 | break; |
| 198 | } |
| 199 | |
| 200 | switch (i + 1) { |
| 201 | case 1 ... 3: |
| 202 | case 7 ... 9: |
| 203 | if (reg & WM831X_GPN_PWR_DOM) |
| 204 | powerdomain = "VPMIC"; |
| 205 | else |
| 206 | powerdomain = "DBVDD"; |
| 207 | break; |
| 208 | |
| 209 | case 4 ... 6: |
| 210 | case 10 ... 12: |
| 211 | if (reg & WM831X_GPN_PWR_DOM) |
| 212 | powerdomain = "SYSVDD"; |
| 213 | else |
| 214 | powerdomain = "DBVDD"; |
| 215 | break; |
| 216 | |
| 217 | case 13 ... 16: |
| 218 | powerdomain = "TPVDD"; |
| 219 | break; |
| 220 | |
| 221 | default: |
| 222 | BUG(); |
| 223 | break; |
| 224 | } |
| 225 | |
Mark Brown | f92e8f8 | 2010-02-17 18:45:25 +0000 | [diff] [blame] | 226 | tristated = reg & WM831X_GPN_TRI; |
| 227 | if (wm831x->has_gpio_ena) |
| 228 | tristated = !tristated; |
| 229 | |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 230 | seq_printf(s, " %s %s %s %s%s\n" |
| 231 | " %s%s (0x%4x)\n", |
| 232 | reg & WM831X_GPN_DIR ? "in" : "out", |
| 233 | wm831x_gpio_get(chip, i) ? "high" : "low", |
| 234 | pull, |
| 235 | powerdomain, |
Mark Brown | 6b8274f | 2010-02-17 18:45:24 +0000 | [diff] [blame] | 236 | reg & WM831X_GPN_POL ? "" : " inverted", |
Linus Walleij | 51c27da | 2016-04-10 14:52:33 +0200 | [diff] [blame] | 237 | reg & WM831X_GPN_OD ? "open-drain" : "push-pull", |
Mark Brown | f92e8f8 | 2010-02-17 18:45:25 +0000 | [diff] [blame] | 238 | tristated ? " tristated" : "", |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 239 | reg); |
| 240 | } |
| 241 | } |
| 242 | #else |
| 243 | #define wm831x_gpio_dbg_show NULL |
| 244 | #endif |
| 245 | |
Julia Lawall | e35b5ab | 2016-09-11 14:14:37 +0200 | [diff] [blame] | 246 | static const struct gpio_chip template_chip = { |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 247 | .label = "wm831x", |
| 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, |
Mark Brown | dc0fb25 | 2009-11-16 17:21:56 +0000 | [diff] [blame] | 253 | .to_irq = wm831x_gpio_to_irq, |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 254 | .set_config = wm831x_set_config, |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 255 | .dbg_show = wm831x_gpio_dbg_show, |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 256 | .can_sleep = true, |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 257 | }; |
| 258 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 259 | static int wm831x_gpio_probe(struct platform_device *pdev) |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 260 | { |
| 261 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); |
Charles Keepax | 9674a8d | 2017-03-17 10:05:19 +0000 | [diff] [blame] | 262 | struct wm831x_pdata *pdata = &wm831x->pdata; |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 263 | struct wm831x_gpio *wm831x_gpio; |
| 264 | int ret; |
| 265 | |
Axel Lin | 718bc6e | 2012-09-02 11:58:07 +0800 | [diff] [blame] | 266 | wm831x_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm831x_gpio), |
| 267 | GFP_KERNEL); |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 268 | if (wm831x_gpio == NULL) |
| 269 | return -ENOMEM; |
| 270 | |
| 271 | wm831x_gpio->wm831x = wm831x; |
| 272 | wm831x_gpio->gpio_chip = template_chip; |
Mark Brown | 6f2ecaa | 2009-10-01 15:41:05 +0100 | [diff] [blame] | 273 | wm831x_gpio->gpio_chip.ngpio = wm831x->num_gpio; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 274 | wm831x_gpio->gpio_chip.parent = &pdev->dev; |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 275 | if (pdata && pdata->gpio_base) |
| 276 | wm831x_gpio->gpio_chip.base = pdata->gpio_base; |
| 277 | else |
| 278 | wm831x_gpio->gpio_chip.base = -1; |
Charles Keepax | 9674a8d | 2017-03-17 10:05:19 +0000 | [diff] [blame] | 279 | #ifdef CONFIG_OF_GPIO |
| 280 | wm831x_gpio->gpio_chip.of_node = wm831x->dev->of_node; |
| 281 | #endif |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 282 | |
Laxman Dewangan | 61da484 | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 283 | ret = devm_gpiochip_add_data(&pdev->dev, &wm831x_gpio->gpio_chip, |
| 284 | wm831x_gpio); |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 285 | if (ret < 0) { |
Axel Lin | 718bc6e | 2012-09-02 11:58:07 +0800 | [diff] [blame] | 286 | dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret); |
| 287 | return ret; |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | platform_set_drvdata(pdev, wm831x_gpio); |
| 291 | |
| 292 | return ret; |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 293 | } |
| 294 | |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 295 | static struct platform_driver wm831x_gpio_driver = { |
| 296 | .driver.name = "wm831x-gpio", |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 297 | .probe = wm831x_gpio_probe, |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | static int __init wm831x_gpio_init(void) |
| 301 | { |
| 302 | return platform_driver_register(&wm831x_gpio_driver); |
| 303 | } |
| 304 | subsys_initcall(wm831x_gpio_init); |
| 305 | |
| 306 | static void __exit wm831x_gpio_exit(void) |
| 307 | { |
| 308 | platform_driver_unregister(&wm831x_gpio_driver); |
| 309 | } |
| 310 | module_exit(wm831x_gpio_exit); |
| 311 | |
| 312 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 313 | MODULE_DESCRIPTION("GPIO interface for WM831x PMICs"); |
| 314 | MODULE_LICENSE("GPL"); |
| 315 | MODULE_ALIAS("platform:wm831x-gpio"); |