Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * wm831x-gpio.c -- gpiolib support for Wolfson WM831x PMICs |
| 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 | |
| 33 | static inline struct wm831x_gpio *to_wm831x_gpio(struct gpio_chip *chip) |
| 34 | { |
| 35 | return container_of(chip, struct wm831x_gpio, gpio_chip); |
| 36 | } |
| 37 | |
| 38 | static int wm831x_gpio_direction_in(struct gpio_chip *chip, unsigned offset) |
| 39 | { |
| 40 | struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip); |
| 41 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
Mark Brown | f92e8f8 | 2010-02-17 18:45:25 +0000 | [diff] [blame] | 42 | int val = WM831X_GPN_DIR; |
| 43 | |
| 44 | if (wm831x->has_gpio_ena) |
| 45 | val |= WM831X_GPN_TRI; |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 46 | |
| 47 | return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset, |
Mark Brown | 1bca748 | 2010-02-17 18:45:26 +0000 | [diff] [blame] | 48 | WM831X_GPN_DIR | WM831X_GPN_TRI | |
| 49 | WM831X_GPN_FN_MASK, val); |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static int wm831x_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 53 | { |
| 54 | struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip); |
| 55 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
| 56 | int ret; |
| 57 | |
| 58 | ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL); |
| 59 | if (ret < 0) |
| 60 | return ret; |
| 61 | |
| 62 | if (ret & 1 << offset) |
| 63 | return 1; |
| 64 | else |
| 65 | return 0; |
| 66 | } |
| 67 | |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 68 | static void wm831x_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 69 | { |
| 70 | struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip); |
| 71 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
| 72 | |
| 73 | wm831x_set_bits(wm831x, WM831X_GPIO_LEVEL, 1 << offset, |
| 74 | value << offset); |
| 75 | } |
| 76 | |
Mark Brown | 3383d23 | 2010-02-17 18:04:35 +0000 | [diff] [blame] | 77 | static int wm831x_gpio_direction_out(struct gpio_chip *chip, |
| 78 | unsigned offset, int value) |
| 79 | { |
| 80 | struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip); |
| 81 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
Mark Brown | f92e8f8 | 2010-02-17 18:45:25 +0000 | [diff] [blame] | 82 | int val = 0; |
Mark Brown | 3383d23 | 2010-02-17 18:04:35 +0000 | [diff] [blame] | 83 | int ret; |
| 84 | |
Mark Brown | f92e8f8 | 2010-02-17 18:45:25 +0000 | [diff] [blame] | 85 | if (wm831x->has_gpio_ena) |
| 86 | val |= WM831X_GPN_TRI; |
| 87 | |
Mark Brown | 3383d23 | 2010-02-17 18:04:35 +0000 | [diff] [blame] | 88 | ret = wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset, |
Mark Brown | 1bca748 | 2010-02-17 18:45:26 +0000 | [diff] [blame] | 89 | WM831X_GPN_DIR | WM831X_GPN_TRI | |
| 90 | WM831X_GPN_FN_MASK, val); |
Mark Brown | 3383d23 | 2010-02-17 18:04:35 +0000 | [diff] [blame] | 91 | if (ret < 0) |
| 92 | return ret; |
| 93 | |
| 94 | /* Can only set GPIO state once it's in output mode */ |
| 95 | wm831x_gpio_set(chip, offset, value); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
Mark Brown | dc0fb25 | 2009-11-16 17:21:56 +0000 | [diff] [blame] | 100 | static int wm831x_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 101 | { |
| 102 | struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip); |
| 103 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
| 104 | |
| 105 | if (!wm831x->irq_base) |
| 106 | return -EINVAL; |
| 107 | |
| 108 | return wm831x->irq_base + WM831X_IRQ_GPIO_1 + offset; |
| 109 | } |
| 110 | |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 111 | #ifdef CONFIG_DEBUG_FS |
| 112 | static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 113 | { |
| 114 | struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip); |
| 115 | struct wm831x *wm831x = wm831x_gpio->wm831x; |
Mark Brown | f92e8f8 | 2010-02-17 18:45:25 +0000 | [diff] [blame] | 116 | int i, tristated; |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 117 | |
| 118 | for (i = 0; i < chip->ngpio; i++) { |
| 119 | int gpio = i + chip->base; |
| 120 | int reg; |
| 121 | const char *label, *pull, *powerdomain; |
| 122 | |
| 123 | /* We report the GPIO even if it's not requested since |
| 124 | * we're also reporting things like alternate |
| 125 | * functions which apply even when the GPIO is not in |
| 126 | * use as a GPIO. |
| 127 | */ |
| 128 | label = gpiochip_is_requested(chip, i); |
| 129 | if (!label) |
| 130 | label = "Unrequested"; |
| 131 | |
| 132 | seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label); |
| 133 | |
| 134 | reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i); |
| 135 | if (reg < 0) { |
| 136 | dev_err(wm831x->dev, |
| 137 | "GPIO control %d read failed: %d\n", |
| 138 | gpio, reg); |
| 139 | seq_printf(s, "\n"); |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | switch (reg & WM831X_GPN_PULL_MASK) { |
| 144 | case WM831X_GPIO_PULL_NONE: |
| 145 | pull = "nopull"; |
| 146 | break; |
| 147 | case WM831X_GPIO_PULL_DOWN: |
| 148 | pull = "pulldown"; |
| 149 | break; |
| 150 | case WM831X_GPIO_PULL_UP: |
| 151 | pull = "pullup"; |
| 152 | default: |
| 153 | pull = "INVALID PULL"; |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | switch (i + 1) { |
| 158 | case 1 ... 3: |
| 159 | case 7 ... 9: |
| 160 | if (reg & WM831X_GPN_PWR_DOM) |
| 161 | powerdomain = "VPMIC"; |
| 162 | else |
| 163 | powerdomain = "DBVDD"; |
| 164 | break; |
| 165 | |
| 166 | case 4 ... 6: |
| 167 | case 10 ... 12: |
| 168 | if (reg & WM831X_GPN_PWR_DOM) |
| 169 | powerdomain = "SYSVDD"; |
| 170 | else |
| 171 | powerdomain = "DBVDD"; |
| 172 | break; |
| 173 | |
| 174 | case 13 ... 16: |
| 175 | powerdomain = "TPVDD"; |
| 176 | break; |
| 177 | |
| 178 | default: |
| 179 | BUG(); |
| 180 | break; |
| 181 | } |
| 182 | |
Mark Brown | f92e8f8 | 2010-02-17 18:45:25 +0000 | [diff] [blame] | 183 | tristated = reg & WM831X_GPN_TRI; |
| 184 | if (wm831x->has_gpio_ena) |
| 185 | tristated = !tristated; |
| 186 | |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 187 | seq_printf(s, " %s %s %s %s%s\n" |
| 188 | " %s%s (0x%4x)\n", |
| 189 | reg & WM831X_GPN_DIR ? "in" : "out", |
| 190 | wm831x_gpio_get(chip, i) ? "high" : "low", |
| 191 | pull, |
| 192 | powerdomain, |
Mark Brown | 6b8274f | 2010-02-17 18:45:24 +0000 | [diff] [blame] | 193 | reg & WM831X_GPN_POL ? "" : " inverted", |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 194 | reg & WM831X_GPN_OD ? "open-drain" : "CMOS", |
Mark Brown | f92e8f8 | 2010-02-17 18:45:25 +0000 | [diff] [blame] | 195 | tristated ? " tristated" : "", |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 196 | reg); |
| 197 | } |
| 198 | } |
| 199 | #else |
| 200 | #define wm831x_gpio_dbg_show NULL |
| 201 | #endif |
| 202 | |
| 203 | static struct gpio_chip template_chip = { |
| 204 | .label = "wm831x", |
| 205 | .owner = THIS_MODULE, |
| 206 | .direction_input = wm831x_gpio_direction_in, |
| 207 | .get = wm831x_gpio_get, |
| 208 | .direction_output = wm831x_gpio_direction_out, |
| 209 | .set = wm831x_gpio_set, |
Mark Brown | dc0fb25 | 2009-11-16 17:21:56 +0000 | [diff] [blame] | 210 | .to_irq = wm831x_gpio_to_irq, |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 211 | .dbg_show = wm831x_gpio_dbg_show, |
| 212 | .can_sleep = 1, |
| 213 | }; |
| 214 | |
| 215 | static int __devinit wm831x_gpio_probe(struct platform_device *pdev) |
| 216 | { |
| 217 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); |
| 218 | struct wm831x_pdata *pdata = wm831x->dev->platform_data; |
| 219 | struct wm831x_gpio *wm831x_gpio; |
| 220 | int ret; |
| 221 | |
| 222 | wm831x_gpio = kzalloc(sizeof(*wm831x_gpio), GFP_KERNEL); |
| 223 | if (wm831x_gpio == NULL) |
| 224 | return -ENOMEM; |
| 225 | |
| 226 | wm831x_gpio->wm831x = wm831x; |
| 227 | wm831x_gpio->gpio_chip = template_chip; |
Mark Brown | 6f2ecaa | 2009-10-01 15:41:05 +0100 | [diff] [blame] | 228 | wm831x_gpio->gpio_chip.ngpio = wm831x->num_gpio; |
Mark Brown | e4b736f | 2009-07-27 14:46:00 +0100 | [diff] [blame] | 229 | wm831x_gpio->gpio_chip.dev = &pdev->dev; |
| 230 | if (pdata && pdata->gpio_base) |
| 231 | wm831x_gpio->gpio_chip.base = pdata->gpio_base; |
| 232 | else |
| 233 | wm831x_gpio->gpio_chip.base = -1; |
| 234 | |
| 235 | ret = gpiochip_add(&wm831x_gpio->gpio_chip); |
| 236 | if (ret < 0) { |
| 237 | dev_err(&pdev->dev, "Could not register gpiochip, %d\n", |
| 238 | ret); |
| 239 | goto err; |
| 240 | } |
| 241 | |
| 242 | platform_set_drvdata(pdev, wm831x_gpio); |
| 243 | |
| 244 | return ret; |
| 245 | |
| 246 | err: |
| 247 | kfree(wm831x_gpio); |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | static int __devexit wm831x_gpio_remove(struct platform_device *pdev) |
| 252 | { |
| 253 | struct wm831x_gpio *wm831x_gpio = platform_get_drvdata(pdev); |
| 254 | int ret; |
| 255 | |
| 256 | ret = gpiochip_remove(&wm831x_gpio->gpio_chip); |
| 257 | if (ret == 0) |
| 258 | kfree(wm831x_gpio); |
| 259 | |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | static struct platform_driver wm831x_gpio_driver = { |
| 264 | .driver.name = "wm831x-gpio", |
| 265 | .driver.owner = THIS_MODULE, |
| 266 | .probe = wm831x_gpio_probe, |
| 267 | .remove = __devexit_p(wm831x_gpio_remove), |
| 268 | }; |
| 269 | |
| 270 | static int __init wm831x_gpio_init(void) |
| 271 | { |
| 272 | return platform_driver_register(&wm831x_gpio_driver); |
| 273 | } |
| 274 | subsys_initcall(wm831x_gpio_init); |
| 275 | |
| 276 | static void __exit wm831x_gpio_exit(void) |
| 277 | { |
| 278 | platform_driver_unregister(&wm831x_gpio_driver); |
| 279 | } |
| 280 | module_exit(wm831x_gpio_exit); |
| 281 | |
| 282 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 283 | MODULE_DESCRIPTION("GPIO interface for WM831x PMICs"); |
| 284 | MODULE_LICENSE("GPL"); |
| 285 | MODULE_ALIAS("platform:wm831x-gpio"); |