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