Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 1 | /* |
Grant Likely | c103de2 | 2011-06-04 18:38:28 -0600 | [diff] [blame] | 2 | * gpiolib support for Wolfson WM831x PMICs |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2009 Wolfson Microelectronics PLC. |
| 5 | * |
| 6 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/gpio.h> |
| 19 | #include <linux/mfd/core.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/seq_file.h> |
| 22 | |
| 23 | #include <linux/mfd/wm831x/core.h> |
| 24 | #include <linux/mfd/wm831x/pdata.h> |
| 25 | #include <linux/mfd/wm831x/gpio.h> |
Mark Brown | dc0fb25 | 2009-11-16 17:21:56 +0000 | [diff] [blame] | 26 | #include <linux/mfd/wm831x/irq.h> |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 27 | |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 28 | struct wm831x_gpio { |
| 29 | struct wm831x *wm831x; |
| 30 | struct gpio_chip gpio_chip; |
| 31 | }; |
| 32 | |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 33 | static int wm831x_gpio_direction_in(struct gpio_chip *chip, unsigned offset) |
| 34 | { |
Linus Walleij | 9b3c817 | 2015-12-07 15:11:34 +0100 | [diff] [blame] | 35 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 36 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
Mark Brown | f92e8f8 | 2010-02-17 18:45:25 +0000 | [diff] [blame] | 37 | int val = WM831X_GPN_DIR; |
| 38 | |
| 39 | if (wm831x->has_gpio_ena) |
| 40 | val |= WM831X_GPN_TRI; |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 41 | |
| 42 | return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset, |
Mark Brown | 1bca748 | 2010-02-17 18:45:26 +0000 | [diff] [blame] | 43 | WM831X_GPN_DIR | WM831X_GPN_TRI | |
| 44 | WM831X_GPN_FN_MASK, val); |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static int wm831x_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 48 | { |
Linus Walleij | 9b3c817 | 2015-12-07 15:11:34 +0100 | [diff] [blame] | 49 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 50 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
| 51 | int ret; |
| 52 | |
| 53 | ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL); |
| 54 | if (ret < 0) |
| 55 | return ret; |
| 56 | |
| 57 | if (ret & 1 << offset) |
| 58 | return 1; |
| 59 | else |
| 60 | return 0; |
| 61 | } |
| 62 | |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 63 | static void wm831x_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 64 | { |
Linus Walleij | 9b3c817 | 2015-12-07 15:11:34 +0100 | [diff] [blame] | 65 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 66 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
| 67 | |
| 68 | wm831x_set_bits(wm831x, WM831X_GPIO_LEVEL, 1 << offset, |
| 69 | value << offset); |
| 70 | } |
| 71 | |
Mark Brown | 3383d23 | 2010-02-17 18:04:35 +0000 | [diff] [blame] | 72 | static int wm831x_gpio_direction_out(struct gpio_chip *chip, |
| 73 | unsigned offset, int value) |
| 74 | { |
Linus Walleij | 9b3c817 | 2015-12-07 15:11:34 +0100 | [diff] [blame] | 75 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
Mark Brown | 3383d23 | 2010-02-17 18:04:35 +0000 | [diff] [blame] | 76 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
Mark Brown | f92e8f8 | 2010-02-17 18:45:25 +0000 | [diff] [blame] | 77 | int val = 0; |
Mark Brown | 3383d23 | 2010-02-17 18:04:35 +0000 | [diff] [blame] | 78 | int ret; |
| 79 | |
Mark Brown | f92e8f8 | 2010-02-17 18:45:25 +0000 | [diff] [blame] | 80 | if (wm831x->has_gpio_ena) |
| 81 | val |= WM831X_GPN_TRI; |
| 82 | |
Mark Brown | 3383d23 | 2010-02-17 18:04:35 +0000 | [diff] [blame] | 83 | ret = wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset, |
Mark Brown | 1bca748 | 2010-02-17 18:45:26 +0000 | [diff] [blame] | 84 | WM831X_GPN_DIR | WM831X_GPN_TRI | |
| 85 | WM831X_GPN_FN_MASK, val); |
Mark Brown | 3383d23 | 2010-02-17 18:04:35 +0000 | [diff] [blame] | 86 | if (ret < 0) |
| 87 | return ret; |
| 88 | |
| 89 | /* Can only set GPIO state once it's in output mode */ |
| 90 | wm831x_gpio_set(chip, offset, value); |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
Mark Brown | dc0fb25 | 2009-11-16 17:21:56 +0000 | [diff] [blame] | 95 | static int wm831x_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 96 | { |
Linus Walleij | 9b3c817 | 2015-12-07 15:11:34 +0100 | [diff] [blame] | 97 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
Mark Brown | dc0fb25 | 2009-11-16 17:21:56 +0000 | [diff] [blame] | 98 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
| 99 | |
Mark Brown | cd99758 | 2012-05-14 23:14:24 +0200 | [diff] [blame] | 100 | return irq_create_mapping(wm831x->irq_domain, |
| 101 | WM831X_IRQ_GPIO_1 + offset); |
Mark Brown | dc0fb25 | 2009-11-16 17:21:56 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Mark Brown | b12c35e | 2010-07-03 15:40:56 +0200 | [diff] [blame] | 104 | static int wm831x_gpio_set_debounce(struct gpio_chip *chip, unsigned offset, |
| 105 | unsigned debounce) |
| 106 | { |
Linus Walleij | 9b3c817 | 2015-12-07 15:11:34 +0100 | [diff] [blame] | 107 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
Mark Brown | b12c35e | 2010-07-03 15:40:56 +0200 | [diff] [blame] | 108 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
| 109 | int reg = WM831X_GPIO1_CONTROL + offset; |
| 110 | int ret, fn; |
| 111 | |
| 112 | ret = wm831x_reg_read(wm831x, reg); |
| 113 | if (ret < 0) |
| 114 | return ret; |
| 115 | |
| 116 | switch (ret & WM831X_GPN_FN_MASK) { |
| 117 | case 0: |
| 118 | case 1: |
| 119 | break; |
| 120 | default: |
| 121 | /* Not in GPIO mode */ |
| 122 | return -EBUSY; |
| 123 | } |
| 124 | |
| 125 | if (debounce >= 32 && debounce <= 64) |
| 126 | fn = 0; |
| 127 | else if (debounce >= 4000 && debounce <= 8000) |
| 128 | fn = 1; |
| 129 | else |
| 130 | return -EINVAL; |
| 131 | |
| 132 | return wm831x_set_bits(wm831x, reg, WM831X_GPN_FN_MASK, fn); |
| 133 | } |
| 134 | |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 135 | #ifdef CONFIG_DEBUG_FS |
| 136 | static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 137 | { |
Linus Walleij | 9b3c817 | 2015-12-07 15:11:34 +0100 | [diff] [blame] | 138 | struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip); |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 139 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
Mark Brown | f92e8f8 | 2010-02-17 18:45:25 +0000 | [diff] [blame] | 140 | int i, tristated; |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 141 | |
| 142 | for (i = 0; i < chip->ngpio; i++) { |
| 143 | int gpio = i + chip->base; |
| 144 | int reg; |
| 145 | const char *label, *pull, *powerdomain; |
| 146 | |
| 147 | /* We report the GPIO even if it's not requested since |
| 148 | * we're also reporting things like alternate |
| 149 | * functions which apply even when the GPIO is not in |
| 150 | * use as a GPIO. |
| 151 | */ |
| 152 | label = gpiochip_is_requested(chip, i); |
| 153 | if (!label) |
| 154 | label = "Unrequested"; |
| 155 | |
| 156 | seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label); |
| 157 | |
| 158 | reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i); |
| 159 | if (reg < 0) { |
| 160 | dev_err(wm831x->dev, |
| 161 | "GPIO control %d read failed: %d\n", |
| 162 | gpio, reg); |
| 163 | seq_printf(s, "\n"); |
| 164 | continue; |
| 165 | } |
| 166 | |
| 167 | switch (reg & WM831X_GPN_PULL_MASK) { |
| 168 | case WM831X_GPIO_PULL_NONE: |
| 169 | pull = "nopull"; |
| 170 | break; |
| 171 | case WM831X_GPIO_PULL_DOWN: |
| 172 | pull = "pulldown"; |
| 173 | break; |
| 174 | case WM831X_GPIO_PULL_UP: |
| 175 | pull = "pullup"; |
Axel Lin | 164d5c3 | 2011-07-10 15:45:07 +0800 | [diff] [blame] | 176 | break; |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 177 | default: |
| 178 | pull = "INVALID PULL"; |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | switch (i + 1) { |
| 183 | case 1 ... 3: |
| 184 | case 7 ... 9: |
| 185 | if (reg & WM831X_GPN_PWR_DOM) |
| 186 | powerdomain = "VPMIC"; |
| 187 | else |
| 188 | powerdomain = "DBVDD"; |
| 189 | break; |
| 190 | |
| 191 | case 4 ... 6: |
| 192 | case 10 ... 12: |
| 193 | if (reg & WM831X_GPN_PWR_DOM) |
| 194 | powerdomain = "SYSVDD"; |
| 195 | else |
| 196 | powerdomain = "DBVDD"; |
| 197 | break; |
| 198 | |
| 199 | case 13 ... 16: |
| 200 | powerdomain = "TPVDD"; |
| 201 | break; |
| 202 | |
| 203 | default: |
| 204 | BUG(); |
| 205 | break; |
| 206 | } |
| 207 | |
Mark Brown | f92e8f8 | 2010-02-17 18:45:25 +0000 | [diff] [blame] | 208 | tristated = reg & WM831X_GPN_TRI; |
| 209 | if (wm831x->has_gpio_ena) |
| 210 | tristated = !tristated; |
| 211 | |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 212 | seq_printf(s, " %s %s %s %s%s\n" |
| 213 | " %s%s (0x%4x)\n", |
| 214 | reg & WM831X_GPN_DIR ? "in" : "out", |
| 215 | wm831x_gpio_get(chip, i) ? "high" : "low", |
| 216 | pull, |
| 217 | powerdomain, |
Mark Brown | 6b8274f | 2010-02-17 18:45:24 +0000 | [diff] [blame] | 218 | reg & WM831X_GPN_POL ? "" : " inverted", |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 219 | reg & WM831X_GPN_OD ? "open-drain" : "CMOS", |
Mark Brown | f92e8f8 | 2010-02-17 18:45:25 +0000 | [diff] [blame] | 220 | tristated ? " tristated" : "", |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 221 | reg); |
| 222 | } |
| 223 | } |
| 224 | #else |
| 225 | #define wm831x_gpio_dbg_show NULL |
| 226 | #endif |
| 227 | |
| 228 | static struct gpio_chip template_chip = { |
| 229 | .label = "wm831x", |
| 230 | .owner = THIS_MODULE, |
| 231 | .direction_input = wm831x_gpio_direction_in, |
| 232 | .get = wm831x_gpio_get, |
| 233 | .direction_output = wm831x_gpio_direction_out, |
| 234 | .set = wm831x_gpio_set, |
Mark Brown | dc0fb25 | 2009-11-16 17:21:56 +0000 | [diff] [blame] | 235 | .to_irq = wm831x_gpio_to_irq, |
Mark Brown | b12c35e | 2010-07-03 15:40:56 +0200 | [diff] [blame] | 236 | .set_debounce = wm831x_gpio_set_debounce, |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 237 | .dbg_show = wm831x_gpio_dbg_show, |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 238 | .can_sleep = true, |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 239 | }; |
| 240 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 241 | static int wm831x_gpio_probe(struct platform_device *pdev) |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 242 | { |
| 243 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 244 | struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev); |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 245 | struct wm831x_gpio *wm831x_gpio; |
| 246 | int ret; |
| 247 | |
Axel Lin | 718bc6e | 2012-09-02 11:58:07 +0800 | [diff] [blame] | 248 | wm831x_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm831x_gpio), |
| 249 | GFP_KERNEL); |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 250 | if (wm831x_gpio == NULL) |
| 251 | return -ENOMEM; |
| 252 | |
| 253 | wm831x_gpio->wm831x = wm831x; |
| 254 | wm831x_gpio->gpio_chip = template_chip; |
Mark Brown | 6f2ecaa | 2009-10-01 15:41:05 +0100 | [diff] [blame] | 255 | wm831x_gpio->gpio_chip.ngpio = wm831x->num_gpio; |
Linus Walleij | 58383c7 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 256 | wm831x_gpio->gpio_chip.parent = &pdev->dev; |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 257 | if (pdata && pdata->gpio_base) |
| 258 | wm831x_gpio->gpio_chip.base = pdata->gpio_base; |
| 259 | else |
| 260 | wm831x_gpio->gpio_chip.base = -1; |
| 261 | |
Linus Walleij | 9b3c817 | 2015-12-07 15:11:34 +0100 | [diff] [blame] | 262 | ret = gpiochip_add_data(&wm831x_gpio->gpio_chip, wm831x_gpio); |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 263 | if (ret < 0) { |
Axel Lin | 718bc6e | 2012-09-02 11:58:07 +0800 | [diff] [blame] | 264 | dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret); |
| 265 | return ret; |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | platform_set_drvdata(pdev, wm831x_gpio); |
| 269 | |
| 270 | return ret; |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 271 | } |
| 272 | |
Bill Pemberton | 206210c | 2012-11-19 13:25:50 -0500 | [diff] [blame] | 273 | static int wm831x_gpio_remove(struct platform_device *pdev) |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 274 | { |
| 275 | struct wm831x_gpio *wm831x_gpio = platform_get_drvdata(pdev); |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 276 | |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 277 | gpiochip_remove(&wm831x_gpio->gpio_chip); |
| 278 | return 0; |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static struct platform_driver wm831x_gpio_driver = { |
| 282 | .driver.name = "wm831x-gpio", |
| 283 | .driver.owner = THIS_MODULE, |
| 284 | .probe = wm831x_gpio_probe, |
Bill Pemberton | 8283c4f | 2012-11-19 13:20:08 -0500 | [diff] [blame] | 285 | .remove = wm831x_gpio_remove, |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | static int __init wm831x_gpio_init(void) |
| 289 | { |
| 290 | return platform_driver_register(&wm831x_gpio_driver); |
| 291 | } |
| 292 | subsys_initcall(wm831x_gpio_init); |
| 293 | |
| 294 | static void __exit wm831x_gpio_exit(void) |
| 295 | { |
| 296 | platform_driver_unregister(&wm831x_gpio_driver); |
| 297 | } |
| 298 | module_exit(wm831x_gpio_exit); |
| 299 | |
| 300 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 301 | MODULE_DESCRIPTION("GPIO interface for WM831x PMICs"); |
| 302 | MODULE_LICENSE("GPL"); |
| 303 | MODULE_ALIAS("platform:wm831x-gpio"); |