Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * GPIO Chip driver for Analog Devices |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 3 | * ADP5588/ADP5587 I/O Expander and QWERTY Keypad Controller |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 4 | * |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 5 | * Copyright 2009-2010 Analog Devices Inc. |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 6 | * |
| 7 | * Licensed under the GPL-2 or later. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 13 | #include <linux/init.h> |
| 14 | #include <linux/i2c.h> |
| 15 | #include <linux/gpio.h> |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/irq.h> |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 18 | |
| 19 | #include <linux/i2c/adp5588.h> |
| 20 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 21 | #define DRV_NAME "adp5588-gpio" |
| 22 | |
| 23 | /* |
| 24 | * Early pre 4.0 Silicon required to delay readout by at least 25ms, |
| 25 | * since the Event Counter Register updated 25ms after the interrupt |
| 26 | * asserted. |
| 27 | */ |
| 28 | #define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4) |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 29 | |
| 30 | struct adp5588_gpio { |
| 31 | struct i2c_client *client; |
| 32 | struct gpio_chip gpio_chip; |
| 33 | struct mutex lock; /* protect cached dir, dat_out */ |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 34 | /* protect serialized access to the interrupt controller bus */ |
| 35 | struct mutex irq_lock; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 36 | unsigned gpio_start; |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 37 | unsigned irq_base; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 38 | uint8_t dat_out[3]; |
| 39 | uint8_t dir[3]; |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 40 | uint8_t int_lvl[3]; |
| 41 | uint8_t int_en[3]; |
| 42 | uint8_t irq_mask[3]; |
| 43 | uint8_t irq_stat[3]; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static int adp5588_gpio_read(struct i2c_client *client, u8 reg) |
| 47 | { |
| 48 | int ret = i2c_smbus_read_byte_data(client, reg); |
| 49 | |
| 50 | if (ret < 0) |
| 51 | dev_err(&client->dev, "Read Error\n"); |
| 52 | |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | static int adp5588_gpio_write(struct i2c_client *client, u8 reg, u8 val) |
| 57 | { |
| 58 | int ret = i2c_smbus_write_byte_data(client, reg, val); |
| 59 | |
| 60 | if (ret < 0) |
| 61 | dev_err(&client->dev, "Write Error\n"); |
| 62 | |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off) |
| 67 | { |
| 68 | struct adp5588_gpio *dev = |
| 69 | container_of(chip, struct adp5588_gpio, gpio_chip); |
Jean-Francois Dagenais | 992196f | 2014-02-10 12:05:28 -0500 | [diff] [blame] | 70 | unsigned bank = ADP5588_BANK(off); |
| 71 | unsigned bit = ADP5588_BIT(off); |
| 72 | int val; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 73 | |
Jean-Francois Dagenais | 992196f | 2014-02-10 12:05:28 -0500 | [diff] [blame] | 74 | mutex_lock(&dev->lock); |
| 75 | |
| 76 | if (dev->dir[bank] & bit) |
| 77 | val = dev->dat_out[bank]; |
| 78 | else |
| 79 | val = adp5588_gpio_read(dev->client, GPIO_DAT_STAT1 + bank); |
| 80 | |
| 81 | mutex_unlock(&dev->lock); |
| 82 | |
| 83 | return !!(val & bit); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static void adp5588_gpio_set_value(struct gpio_chip *chip, |
| 87 | unsigned off, int val) |
| 88 | { |
| 89 | unsigned bank, bit; |
| 90 | struct adp5588_gpio *dev = |
| 91 | container_of(chip, struct adp5588_gpio, gpio_chip); |
| 92 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 93 | bank = ADP5588_BANK(off); |
| 94 | bit = ADP5588_BIT(off); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 95 | |
| 96 | mutex_lock(&dev->lock); |
| 97 | if (val) |
| 98 | dev->dat_out[bank] |= bit; |
| 99 | else |
| 100 | dev->dat_out[bank] &= ~bit; |
| 101 | |
| 102 | adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank, |
| 103 | dev->dat_out[bank]); |
| 104 | mutex_unlock(&dev->lock); |
| 105 | } |
| 106 | |
| 107 | static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off) |
| 108 | { |
| 109 | int ret; |
| 110 | unsigned bank; |
| 111 | struct adp5588_gpio *dev = |
| 112 | container_of(chip, struct adp5588_gpio, gpio_chip); |
| 113 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 114 | bank = ADP5588_BANK(off); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 115 | |
| 116 | mutex_lock(&dev->lock); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 117 | dev->dir[bank] &= ~ADP5588_BIT(off); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 118 | ret = adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, dev->dir[bank]); |
| 119 | mutex_unlock(&dev->lock); |
| 120 | |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | static int adp5588_gpio_direction_output(struct gpio_chip *chip, |
| 125 | unsigned off, int val) |
| 126 | { |
| 127 | int ret; |
| 128 | unsigned bank, bit; |
| 129 | struct adp5588_gpio *dev = |
| 130 | container_of(chip, struct adp5588_gpio, gpio_chip); |
| 131 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 132 | bank = ADP5588_BANK(off); |
| 133 | bit = ADP5588_BIT(off); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 134 | |
| 135 | mutex_lock(&dev->lock); |
| 136 | dev->dir[bank] |= bit; |
| 137 | |
| 138 | if (val) |
| 139 | dev->dat_out[bank] |= bit; |
| 140 | else |
| 141 | dev->dat_out[bank] &= ~bit; |
| 142 | |
| 143 | ret = adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank, |
| 144 | dev->dat_out[bank]); |
| 145 | ret |= adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, |
| 146 | dev->dir[bank]); |
| 147 | mutex_unlock(&dev->lock); |
| 148 | |
| 149 | return ret; |
| 150 | } |
| 151 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 152 | #ifdef CONFIG_GPIO_ADP5588_IRQ |
| 153 | static int adp5588_gpio_to_irq(struct gpio_chip *chip, unsigned off) |
| 154 | { |
| 155 | struct adp5588_gpio *dev = |
| 156 | container_of(chip, struct adp5588_gpio, gpio_chip); |
| 157 | return dev->irq_base + off; |
| 158 | } |
| 159 | |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 160 | static void adp5588_irq_bus_lock(struct irq_data *d) |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 161 | { |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 162 | struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d); |
| 163 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 164 | mutex_lock(&dev->irq_lock); |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * genirq core code can issue chip->mask/unmask from atomic context. |
| 169 | * This doesn't work for slow busses where an access needs to sleep. |
| 170 | * bus_sync_unlock() is therefore called outside the atomic context, |
| 171 | * syncs the current irq mask state with the slow external controller |
| 172 | * and unlocks the bus. |
| 173 | */ |
| 174 | |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 175 | static void adp5588_irq_bus_sync_unlock(struct irq_data *d) |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 176 | { |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 177 | struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 178 | int i; |
| 179 | |
| 180 | for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) |
| 181 | if (dev->int_en[i] ^ dev->irq_mask[i]) { |
| 182 | dev->int_en[i] = dev->irq_mask[i]; |
| 183 | adp5588_gpio_write(dev->client, GPIO_INT_EN1 + i, |
| 184 | dev->int_en[i]); |
| 185 | } |
| 186 | |
| 187 | mutex_unlock(&dev->irq_lock); |
| 188 | } |
| 189 | |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 190 | static void adp5588_irq_mask(struct irq_data *d) |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 191 | { |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 192 | struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d); |
| 193 | unsigned gpio = d->irq - dev->irq_base; |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 194 | |
| 195 | dev->irq_mask[ADP5588_BANK(gpio)] &= ~ADP5588_BIT(gpio); |
| 196 | } |
| 197 | |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 198 | static void adp5588_irq_unmask(struct irq_data *d) |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 199 | { |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 200 | struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d); |
| 201 | unsigned gpio = d->irq - dev->irq_base; |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 202 | |
| 203 | dev->irq_mask[ADP5588_BANK(gpio)] |= ADP5588_BIT(gpio); |
| 204 | } |
| 205 | |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 206 | static int adp5588_irq_set_type(struct irq_data *d, unsigned int type) |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 207 | { |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 208 | struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d); |
| 209 | uint16_t gpio = d->irq - dev->irq_base; |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 210 | unsigned bank, bit; |
| 211 | |
| 212 | if ((type & IRQ_TYPE_EDGE_BOTH)) { |
| 213 | dev_err(&dev->client->dev, "irq %d: unsupported type %d\n", |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 214 | d->irq, type); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 215 | return -EINVAL; |
| 216 | } |
| 217 | |
| 218 | bank = ADP5588_BANK(gpio); |
| 219 | bit = ADP5588_BIT(gpio); |
| 220 | |
| 221 | if (type & IRQ_TYPE_LEVEL_HIGH) |
| 222 | dev->int_lvl[bank] |= bit; |
| 223 | else if (type & IRQ_TYPE_LEVEL_LOW) |
| 224 | dev->int_lvl[bank] &= ~bit; |
| 225 | else |
| 226 | return -EINVAL; |
| 227 | |
| 228 | adp5588_gpio_direction_input(&dev->gpio_chip, gpio); |
| 229 | adp5588_gpio_write(dev->client, GPIO_INT_LVL1 + bank, |
| 230 | dev->int_lvl[bank]); |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static struct irq_chip adp5588_irq_chip = { |
| 236 | .name = "adp5588", |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 237 | .irq_mask = adp5588_irq_mask, |
| 238 | .irq_unmask = adp5588_irq_unmask, |
| 239 | .irq_bus_lock = adp5588_irq_bus_lock, |
| 240 | .irq_bus_sync_unlock = adp5588_irq_bus_sync_unlock, |
| 241 | .irq_set_type = adp5588_irq_set_type, |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | static int adp5588_gpio_read_intstat(struct i2c_client *client, u8 *buf) |
| 245 | { |
| 246 | int ret = i2c_smbus_read_i2c_block_data(client, GPIO_INT_STAT1, 3, buf); |
| 247 | |
| 248 | if (ret < 0) |
| 249 | dev_err(&client->dev, "Read INT_STAT Error\n"); |
| 250 | |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | static irqreturn_t adp5588_irq_handler(int irq, void *devid) |
| 255 | { |
| 256 | struct adp5588_gpio *dev = devid; |
| 257 | unsigned status, bank, bit, pending; |
| 258 | int ret; |
| 259 | status = adp5588_gpio_read(dev->client, INT_STAT); |
| 260 | |
| 261 | if (status & ADP5588_GPI_INT) { |
| 262 | ret = adp5588_gpio_read_intstat(dev->client, dev->irq_stat); |
| 263 | if (ret < 0) |
| 264 | memset(dev->irq_stat, 0, ARRAY_SIZE(dev->irq_stat)); |
| 265 | |
Axel Lin | 078dc65 | 2012-04-06 20:37:43 +0800 | [diff] [blame] | 266 | for (bank = 0, bit = 0; bank <= ADP5588_BANK(ADP5588_MAXGPIO); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 267 | bank++, bit = 0) { |
| 268 | pending = dev->irq_stat[bank] & dev->irq_mask[bank]; |
| 269 | |
| 270 | while (pending) { |
| 271 | if (pending & (1 << bit)) { |
| 272 | handle_nested_irq(dev->irq_base + |
| 273 | (bank << 3) + bit); |
| 274 | pending &= ~(1 << bit); |
| 275 | |
| 276 | } |
| 277 | bit++; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | adp5588_gpio_write(dev->client, INT_STAT, status); /* Status is W1C */ |
| 283 | |
| 284 | return IRQ_HANDLED; |
| 285 | } |
| 286 | |
| 287 | static int adp5588_irq_setup(struct adp5588_gpio *dev) |
| 288 | { |
| 289 | struct i2c_client *client = dev->client; |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 290 | struct adp5588_gpio_platform_data *pdata = |
| 291 | dev_get_platdata(&client->dev); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 292 | unsigned gpio; |
| 293 | int ret; |
| 294 | |
| 295 | adp5588_gpio_write(client, CFG, ADP5588_AUTO_INC); |
| 296 | adp5588_gpio_write(client, INT_STAT, -1); /* status is W1C */ |
| 297 | adp5588_gpio_read_intstat(client, dev->irq_stat); /* read to clear */ |
| 298 | |
| 299 | dev->irq_base = pdata->irq_base; |
| 300 | mutex_init(&dev->irq_lock); |
| 301 | |
| 302 | for (gpio = 0; gpio < dev->gpio_chip.ngpio; gpio++) { |
| 303 | int irq = gpio + dev->irq_base; |
Thomas Gleixner | b51804b | 2011-03-24 21:27:36 +0000 | [diff] [blame] | 304 | irq_set_chip_data(irq, dev); |
| 305 | irq_set_chip_and_handler(irq, &adp5588_irq_chip, |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 306 | handle_level_irq); |
Thomas Gleixner | b51804b | 2011-03-24 21:27:36 +0000 | [diff] [blame] | 307 | irq_set_nested_thread(irq, 1); |
Rob Herring | 23393d4 | 2015-07-27 15:55:16 -0500 | [diff] [blame] | 308 | irq_modify_status(irq, IRQ_NOREQUEST, IRQ_NOPROBE); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | ret = request_threaded_irq(client->irq, |
| 312 | NULL, |
| 313 | adp5588_irq_handler, |
| 314 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 315 | dev_name(&client->dev), dev); |
| 316 | if (ret) { |
| 317 | dev_err(&client->dev, "failed to request irq %d\n", |
| 318 | client->irq); |
| 319 | goto out; |
| 320 | } |
| 321 | |
| 322 | dev->gpio_chip.to_irq = adp5588_gpio_to_irq; |
| 323 | adp5588_gpio_write(client, CFG, |
| 324 | ADP5588_AUTO_INC | ADP5588_INT_CFG | ADP5588_GPI_INT); |
| 325 | |
| 326 | return 0; |
| 327 | |
| 328 | out: |
| 329 | dev->irq_base = 0; |
| 330 | return ret; |
| 331 | } |
| 332 | |
| 333 | static void adp5588_irq_teardown(struct adp5588_gpio *dev) |
| 334 | { |
| 335 | if (dev->irq_base) |
| 336 | free_irq(dev->client->irq, dev); |
| 337 | } |
| 338 | |
| 339 | #else |
| 340 | static int adp5588_irq_setup(struct adp5588_gpio *dev) |
| 341 | { |
| 342 | struct i2c_client *client = dev->client; |
| 343 | dev_warn(&client->dev, "interrupt support not compiled in\n"); |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | static void adp5588_irq_teardown(struct adp5588_gpio *dev) |
| 349 | { |
| 350 | } |
| 351 | #endif /* CONFIG_GPIO_ADP5588_IRQ */ |
| 352 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 353 | static int adp5588_gpio_probe(struct i2c_client *client, |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 354 | const struct i2c_device_id *id) |
| 355 | { |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 356 | struct adp5588_gpio_platform_data *pdata = |
| 357 | dev_get_platdata(&client->dev); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 358 | struct adp5588_gpio *dev; |
| 359 | struct gpio_chip *gc; |
| 360 | int ret, i, revid; |
| 361 | |
Varka Bhadram | afeb7b4 | 2015-03-31 09:49:11 +0530 | [diff] [blame] | 362 | if (!pdata) { |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 363 | dev_err(&client->dev, "missing platform data\n"); |
| 364 | return -ENODEV; |
| 365 | } |
| 366 | |
| 367 | if (!i2c_check_functionality(client->adapter, |
| 368 | I2C_FUNC_SMBUS_BYTE_DATA)) { |
| 369 | dev_err(&client->dev, "SMBUS Byte Data not Supported\n"); |
| 370 | return -EIO; |
| 371 | } |
| 372 | |
Varka Bhadram | 7898b31 | 2015-03-31 09:49:08 +0530 | [diff] [blame] | 373 | dev = devm_kzalloc(&client->dev, sizeof(*dev), GFP_KERNEL); |
Varka Bhadram | afeb7b4 | 2015-03-31 09:49:11 +0530 | [diff] [blame] | 374 | if (!dev) |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 375 | return -ENOMEM; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 376 | |
| 377 | dev->client = client; |
| 378 | |
| 379 | gc = &dev->gpio_chip; |
| 380 | gc->direction_input = adp5588_gpio_direction_input; |
| 381 | gc->direction_output = adp5588_gpio_direction_output; |
| 382 | gc->get = adp5588_gpio_get_value; |
| 383 | gc->set = adp5588_gpio_set_value; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 384 | gc->can_sleep = true; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 385 | |
| 386 | gc->base = pdata->gpio_start; |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 387 | gc->ngpio = ADP5588_MAXGPIO; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 388 | gc->label = client->name; |
| 389 | gc->owner = THIS_MODULE; |
Jean-Francois Dagenais | 1163316 | 2014-02-10 12:24:05 -0500 | [diff] [blame] | 390 | gc->names = pdata->names; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 391 | |
| 392 | mutex_init(&dev->lock); |
| 393 | |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 394 | ret = adp5588_gpio_read(dev->client, DEV_ID); |
| 395 | if (ret < 0) |
| 396 | goto err; |
| 397 | |
| 398 | revid = ret & ADP5588_DEVICE_ID_MASK; |
| 399 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 400 | for (i = 0, ret = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) { |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 401 | dev->dat_out[i] = adp5588_gpio_read(client, GPIO_DAT_OUT1 + i); |
| 402 | dev->dir[i] = adp5588_gpio_read(client, GPIO_DIR1 + i); |
| 403 | ret |= adp5588_gpio_write(client, KP_GPIO1 + i, 0); |
| 404 | ret |= adp5588_gpio_write(client, GPIO_PULL1 + i, |
| 405 | (pdata->pullup_dis_mask >> (8 * i)) & 0xFF); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 406 | ret |= adp5588_gpio_write(client, GPIO_INT_EN1 + i, 0); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 407 | if (ret) |
| 408 | goto err; |
| 409 | } |
| 410 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 411 | if (pdata->irq_base) { |
| 412 | if (WA_DELAYED_READOUT_REVID(revid)) { |
| 413 | dev_warn(&client->dev, "GPIO int not supported\n"); |
| 414 | } else { |
| 415 | ret = adp5588_irq_setup(dev); |
| 416 | if (ret) |
| 417 | goto err; |
| 418 | } |
| 419 | } |
| 420 | |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 421 | ret = gpiochip_add(&dev->gpio_chip); |
| 422 | if (ret) |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 423 | goto err_irq; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 424 | |
Grant Likely | 64842aa | 2011-11-06 11:36:18 -0700 | [diff] [blame] | 425 | dev_info(&client->dev, "IRQ Base: %d Rev.: %d\n", |
| 426 | pdata->irq_base, revid); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 427 | |
| 428 | if (pdata->setup) { |
| 429 | ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context); |
| 430 | if (ret < 0) |
| 431 | dev_warn(&client->dev, "setup failed, %d\n", ret); |
| 432 | } |
| 433 | |
| 434 | i2c_set_clientdata(client, dev); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 435 | |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 436 | return 0; |
| 437 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 438 | err_irq: |
| 439 | adp5588_irq_teardown(dev); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 440 | err: |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 441 | return ret; |
| 442 | } |
| 443 | |
Bill Pemberton | 206210c | 2012-11-19 13:25:50 -0500 | [diff] [blame] | 444 | static int adp5588_gpio_remove(struct i2c_client *client) |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 445 | { |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 446 | struct adp5588_gpio_platform_data *pdata = |
| 447 | dev_get_platdata(&client->dev); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 448 | struct adp5588_gpio *dev = i2c_get_clientdata(client); |
| 449 | int ret; |
| 450 | |
| 451 | if (pdata->teardown) { |
| 452 | ret = pdata->teardown(client, |
| 453 | dev->gpio_chip.base, dev->gpio_chip.ngpio, |
| 454 | pdata->context); |
| 455 | if (ret < 0) { |
| 456 | dev_err(&client->dev, "teardown failed %d\n", ret); |
| 457 | return ret; |
| 458 | } |
| 459 | } |
| 460 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 461 | if (dev->irq_base) |
| 462 | free_irq(dev->client->irq, dev); |
| 463 | |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 464 | gpiochip_remove(&dev->gpio_chip); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 465 | |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static const struct i2c_device_id adp5588_gpio_id[] = { |
| 470 | {DRV_NAME, 0}, |
| 471 | {} |
| 472 | }; |
| 473 | |
| 474 | MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id); |
| 475 | |
| 476 | static struct i2c_driver adp5588_gpio_driver = { |
| 477 | .driver = { |
| 478 | .name = DRV_NAME, |
| 479 | }, |
| 480 | .probe = adp5588_gpio_probe, |
Bill Pemberton | 8283c4f | 2012-11-19 13:20:08 -0500 | [diff] [blame] | 481 | .remove = adp5588_gpio_remove, |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 482 | .id_table = adp5588_gpio_id, |
| 483 | }; |
| 484 | |
Axel Lin | c8554d3 | 2012-09-02 08:08:01 +0800 | [diff] [blame] | 485 | module_i2c_driver(adp5588_gpio_driver); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 486 | |
| 487 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
| 488 | MODULE_DESCRIPTION("GPIO ADP5588 Driver"); |
| 489 | MODULE_LICENSE("GPL"); |