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