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