Thomas Gleixner | 80503b2 | 2019-05-24 12:04:09 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 2 | /* |
| 3 | * GPIO Chip driver for Analog Devices |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 4 | * ADP5588/ADP5587 I/O Expander and QWERTY Keypad Controller |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 5 | * |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 6 | * Copyright 2009-2010 Analog Devices Inc. |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 12 | #include <linux/init.h> |
| 13 | #include <linux/i2c.h> |
Linus Walleij | d543668 | 2018-01-13 22:14:56 +0100 | [diff] [blame] | 14 | #include <linux/gpio/driver.h> |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/irq.h> |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 17 | #include <linux/of_device.h> |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 18 | |
Wolfram Sang | c1a4634 | 2017-05-21 23:57:27 +0200 | [diff] [blame] | 19 | #include <linux/platform_data/adp5588.h> |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 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 | uint8_t dat_out[3]; |
| 37 | uint8_t dir[3]; |
Nikolaus Voss | 5d643ed | 2019-02-05 14:31:11 +0100 | [diff] [blame] | 38 | uint8_t int_lvl_low[3]; |
| 39 | uint8_t int_lvl_high[3]; |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 40 | uint8_t int_en[3]; |
| 41 | uint8_t irq_mask[3]; |
Michael Hennerich | 6537886 | 2018-08-13 15:57:44 +0200 | [diff] [blame] | 42 | uint8_t int_input_en[3]; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | static int adp5588_gpio_read(struct i2c_client *client, u8 reg) |
| 46 | { |
| 47 | int ret = i2c_smbus_read_byte_data(client, reg); |
| 48 | |
| 49 | if (ret < 0) |
| 50 | dev_err(&client->dev, "Read Error\n"); |
| 51 | |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | static int adp5588_gpio_write(struct i2c_client *client, u8 reg, u8 val) |
| 56 | { |
| 57 | int ret = i2c_smbus_write_byte_data(client, reg, val); |
| 58 | |
| 59 | if (ret < 0) |
| 60 | dev_err(&client->dev, "Write Error\n"); |
| 61 | |
| 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off) |
| 66 | { |
Linus Walleij | f69255c | 2015-12-04 15:05:34 +0100 | [diff] [blame] | 67 | struct adp5588_gpio *dev = gpiochip_get_data(chip); |
Jean-Francois Dagenais | 992196f | 2014-02-10 12:05:28 -0500 | [diff] [blame] | 68 | unsigned bank = ADP5588_BANK(off); |
| 69 | unsigned bit = ADP5588_BIT(off); |
| 70 | int val; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 71 | |
Jean-Francois Dagenais | 992196f | 2014-02-10 12:05:28 -0500 | [diff] [blame] | 72 | mutex_lock(&dev->lock); |
| 73 | |
| 74 | if (dev->dir[bank] & bit) |
| 75 | val = dev->dat_out[bank]; |
| 76 | else |
| 77 | val = adp5588_gpio_read(dev->client, GPIO_DAT_STAT1 + bank); |
| 78 | |
| 79 | mutex_unlock(&dev->lock); |
| 80 | |
| 81 | return !!(val & bit); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static void adp5588_gpio_set_value(struct gpio_chip *chip, |
| 85 | unsigned off, int val) |
| 86 | { |
| 87 | unsigned bank, bit; |
Linus Walleij | f69255c | 2015-12-04 15:05:34 +0100 | [diff] [blame] | 88 | struct adp5588_gpio *dev = gpiochip_get_data(chip); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 89 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 90 | bank = ADP5588_BANK(off); |
| 91 | bit = ADP5588_BIT(off); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 92 | |
| 93 | mutex_lock(&dev->lock); |
| 94 | if (val) |
| 95 | dev->dat_out[bank] |= bit; |
| 96 | else |
| 97 | dev->dat_out[bank] &= ~bit; |
| 98 | |
| 99 | adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank, |
| 100 | dev->dat_out[bank]); |
| 101 | mutex_unlock(&dev->lock); |
| 102 | } |
| 103 | |
| 104 | static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off) |
| 105 | { |
| 106 | int ret; |
| 107 | unsigned bank; |
Linus Walleij | f69255c | 2015-12-04 15:05:34 +0100 | [diff] [blame] | 108 | struct adp5588_gpio *dev = gpiochip_get_data(chip); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 109 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 110 | bank = ADP5588_BANK(off); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 111 | |
| 112 | mutex_lock(&dev->lock); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 113 | dev->dir[bank] &= ~ADP5588_BIT(off); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 114 | ret = adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, dev->dir[bank]); |
| 115 | mutex_unlock(&dev->lock); |
| 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static int adp5588_gpio_direction_output(struct gpio_chip *chip, |
| 121 | unsigned off, int val) |
| 122 | { |
| 123 | int ret; |
| 124 | unsigned bank, bit; |
Linus Walleij | f69255c | 2015-12-04 15:05:34 +0100 | [diff] [blame] | 125 | struct adp5588_gpio *dev = gpiochip_get_data(chip); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 126 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 127 | bank = ADP5588_BANK(off); |
| 128 | bit = ADP5588_BIT(off); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 129 | |
| 130 | mutex_lock(&dev->lock); |
| 131 | dev->dir[bank] |= bit; |
| 132 | |
| 133 | if (val) |
| 134 | dev->dat_out[bank] |= bit; |
| 135 | else |
| 136 | dev->dat_out[bank] &= ~bit; |
| 137 | |
| 138 | ret = adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank, |
| 139 | dev->dat_out[bank]); |
| 140 | ret |= adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, |
| 141 | dev->dir[bank]); |
| 142 | mutex_unlock(&dev->lock); |
| 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 147 | #ifdef CONFIG_GPIO_ADP5588_IRQ |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 148 | |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 149 | static void adp5588_irq_bus_lock(struct irq_data *d) |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 150 | { |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 151 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
| 152 | struct adp5588_gpio *dev = gpiochip_get_data(gc); |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 153 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 154 | mutex_lock(&dev->irq_lock); |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * genirq core code can issue chip->mask/unmask from atomic context. |
| 159 | * This doesn't work for slow busses where an access needs to sleep. |
| 160 | * bus_sync_unlock() is therefore called outside the atomic context, |
| 161 | * syncs the current irq mask state with the slow external controller |
| 162 | * and unlocks the bus. |
| 163 | */ |
| 164 | |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 165 | static void adp5588_irq_bus_sync_unlock(struct irq_data *d) |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 166 | { |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 167 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
| 168 | struct adp5588_gpio *dev = gpiochip_get_data(gc); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 169 | int i; |
| 170 | |
Michael Hennerich | 6537886 | 2018-08-13 15:57:44 +0200 | [diff] [blame] | 171 | for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) { |
| 172 | if (dev->int_input_en[i]) { |
| 173 | mutex_lock(&dev->lock); |
| 174 | dev->dir[i] &= ~dev->int_input_en[i]; |
| 175 | dev->int_input_en[i] = 0; |
| 176 | adp5588_gpio_write(dev->client, GPIO_DIR1 + i, |
| 177 | dev->dir[i]); |
| 178 | mutex_unlock(&dev->lock); |
| 179 | } |
| 180 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 181 | if (dev->int_en[i] ^ dev->irq_mask[i]) { |
| 182 | dev->int_en[i] = dev->irq_mask[i]; |
Nikolaus Voss | 5d643ed | 2019-02-05 14:31:11 +0100 | [diff] [blame] | 183 | adp5588_gpio_write(dev->client, GPI_EM1 + i, |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 184 | dev->int_en[i]); |
| 185 | } |
Michael Hennerich | 6537886 | 2018-08-13 15:57:44 +0200 | [diff] [blame] | 186 | } |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 187 | |
| 188 | mutex_unlock(&dev->irq_lock); |
| 189 | } |
| 190 | |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 191 | static void adp5588_irq_mask(struct irq_data *d) |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 192 | { |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 193 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
| 194 | struct adp5588_gpio *dev = gpiochip_get_data(gc); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 195 | |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 196 | dev->irq_mask[ADP5588_BANK(d->hwirq)] &= ~ADP5588_BIT(d->hwirq); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 199 | static void adp5588_irq_unmask(struct irq_data *d) |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 200 | { |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 201 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
| 202 | struct adp5588_gpio *dev = gpiochip_get_data(gc); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 203 | |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 204 | dev->irq_mask[ADP5588_BANK(d->hwirq)] |= ADP5588_BIT(d->hwirq); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 207 | 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] | 208 | { |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 209 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
| 210 | struct adp5588_gpio *dev = gpiochip_get_data(gc); |
| 211 | uint16_t gpio = d->hwirq; |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 212 | unsigned bank, bit; |
| 213 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 214 | bank = ADP5588_BANK(gpio); |
| 215 | bit = ADP5588_BIT(gpio); |
| 216 | |
Nikolaus Voss | 5d643ed | 2019-02-05 14:31:11 +0100 | [diff] [blame] | 217 | dev->int_lvl_low[bank] &= ~bit; |
| 218 | dev->int_lvl_high[bank] &= ~bit; |
| 219 | |
| 220 | if (type & IRQ_TYPE_EDGE_BOTH || type & IRQ_TYPE_LEVEL_HIGH) |
| 221 | dev->int_lvl_high[bank] |= bit; |
| 222 | |
| 223 | if (type & IRQ_TYPE_EDGE_BOTH || type & IRQ_TYPE_LEVEL_LOW) |
| 224 | dev->int_lvl_low[bank] |= bit; |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 225 | |
Michael Hennerich | 6537886 | 2018-08-13 15:57:44 +0200 | [diff] [blame] | 226 | dev->int_input_en[bank] |= bit; |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static struct irq_chip adp5588_irq_chip = { |
| 232 | .name = "adp5588", |
Lennert Buytenhek | 12401eed | 2011-01-12 17:00:12 -0800 | [diff] [blame] | 233 | .irq_mask = adp5588_irq_mask, |
| 234 | .irq_unmask = adp5588_irq_unmask, |
| 235 | .irq_bus_lock = adp5588_irq_bus_lock, |
| 236 | .irq_bus_sync_unlock = adp5588_irq_bus_sync_unlock, |
| 237 | .irq_set_type = adp5588_irq_set_type, |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 238 | }; |
| 239 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 240 | static irqreturn_t adp5588_irq_handler(int irq, void *devid) |
| 241 | { |
| 242 | struct adp5588_gpio *dev = devid; |
Nikolaus Voss | 5d643ed | 2019-02-05 14:31:11 +0100 | [diff] [blame] | 243 | int status = adp5588_gpio_read(dev->client, INT_STAT); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 244 | |
Nikolaus Voss | 5d643ed | 2019-02-05 14:31:11 +0100 | [diff] [blame] | 245 | if (status & ADP5588_KE_INT) { |
| 246 | int ev_cnt = adp5588_gpio_read(dev->client, KEY_LCK_EC_STAT); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 247 | |
Nikolaus Voss | 5d643ed | 2019-02-05 14:31:11 +0100 | [diff] [blame] | 248 | if (ev_cnt > 0) { |
| 249 | int i; |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 250 | |
Nikolaus Voss | 5d643ed | 2019-02-05 14:31:11 +0100 | [diff] [blame] | 251 | for (i = 0; i < (ev_cnt & ADP5588_KEC); i++) { |
| 252 | int key = adp5588_gpio_read(dev->client, |
| 253 | Key_EVENTA + i); |
| 254 | /* GPIN events begin at 97, |
| 255 | * bit 7 indicates logic level |
| 256 | */ |
| 257 | int gpio = (key & 0x7f) - 97; |
| 258 | int lvl = key & (1 << 7); |
| 259 | int bank = ADP5588_BANK(gpio); |
| 260 | int bit = ADP5588_BIT(gpio); |
| 261 | |
| 262 | if ((lvl && dev->int_lvl_high[bank] & bit) || |
| 263 | (!lvl && dev->int_lvl_low[bank] & bit)) |
| 264 | handle_nested_irq(irq_find_mapping( |
| 265 | dev->gpio_chip.irq.domain, gpio)); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | adp5588_gpio_write(dev->client, INT_STAT, status); /* Status is W1C */ |
| 271 | |
| 272 | return IRQ_HANDLED; |
| 273 | } |
| 274 | |
| 275 | static int adp5588_irq_setup(struct adp5588_gpio *dev) |
| 276 | { |
| 277 | struct i2c_client *client = dev->client; |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 278 | int ret; |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 279 | struct adp5588_gpio_platform_data *pdata = |
| 280 | dev_get_platdata(&client->dev); |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 281 | int irq_base = pdata ? pdata->irq_base : 0; |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 282 | |
| 283 | adp5588_gpio_write(client, CFG, ADP5588_AUTO_INC); |
| 284 | adp5588_gpio_write(client, INT_STAT, -1); /* status is W1C */ |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 285 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 286 | mutex_init(&dev->irq_lock); |
| 287 | |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 288 | ret = devm_request_threaded_irq(&client->dev, client->irq, |
| 289 | NULL, adp5588_irq_handler, IRQF_ONESHOT |
| 290 | | IRQF_TRIGGER_FALLING | IRQF_SHARED, |
| 291 | dev_name(&client->dev), dev); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 292 | if (ret) { |
| 293 | dev_err(&client->dev, "failed to request irq %d\n", |
| 294 | client->irq); |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 295 | return ret; |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 296 | } |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 297 | ret = gpiochip_irqchip_add_nested(&dev->gpio_chip, |
| 298 | &adp5588_irq_chip, irq_base, |
| 299 | handle_simple_irq, |
| 300 | IRQ_TYPE_NONE); |
| 301 | if (ret) { |
| 302 | dev_err(&client->dev, |
| 303 | "could not connect irqchip to gpiochip\n"); |
| 304 | return ret; |
| 305 | } |
| 306 | gpiochip_set_nested_irqchip(&dev->gpio_chip, |
| 307 | &adp5588_irq_chip, |
| 308 | client->irq); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 309 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 310 | adp5588_gpio_write(client, CFG, |
Nikolaus Voss | 5d643ed | 2019-02-05 14:31:11 +0100 | [diff] [blame] | 311 | ADP5588_AUTO_INC | ADP5588_INT_CFG | ADP5588_KE_IEN); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 312 | |
| 313 | return 0; |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | #else |
| 317 | static int adp5588_irq_setup(struct adp5588_gpio *dev) |
| 318 | { |
| 319 | struct i2c_client *client = dev->client; |
| 320 | dev_warn(&client->dev, "interrupt support not compiled in\n"); |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 325 | #endif /* CONFIG_GPIO_ADP5588_IRQ */ |
| 326 | |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 327 | static int adp5588_gpio_probe(struct i2c_client *client) |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 328 | { |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 329 | struct adp5588_gpio_platform_data *pdata = |
| 330 | dev_get_platdata(&client->dev); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 331 | struct adp5588_gpio *dev; |
| 332 | struct gpio_chip *gc; |
| 333 | int ret, i, revid; |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 334 | unsigned int pullup_dis_mask = 0; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 335 | |
| 336 | if (!i2c_check_functionality(client->adapter, |
| 337 | I2C_FUNC_SMBUS_BYTE_DATA)) { |
| 338 | dev_err(&client->dev, "SMBUS Byte Data not Supported\n"); |
| 339 | return -EIO; |
| 340 | } |
| 341 | |
Varka Bhadram | 7898b31 | 2015-03-31 09:49:08 +0530 | [diff] [blame] | 342 | dev = devm_kzalloc(&client->dev, sizeof(*dev), GFP_KERNEL); |
Varka Bhadram | afeb7b4 | 2015-03-31 09:49:11 +0530 | [diff] [blame] | 343 | if (!dev) |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 344 | return -ENOMEM; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 345 | |
| 346 | dev->client = client; |
| 347 | |
| 348 | gc = &dev->gpio_chip; |
| 349 | gc->direction_input = adp5588_gpio_direction_input; |
| 350 | gc->direction_output = adp5588_gpio_direction_output; |
| 351 | gc->get = adp5588_gpio_get_value; |
| 352 | gc->set = adp5588_gpio_set_value; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 353 | gc->can_sleep = true; |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 354 | gc->base = -1; |
| 355 | gc->parent = &client->dev; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 356 | |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 357 | if (pdata) { |
| 358 | gc->base = pdata->gpio_start; |
| 359 | gc->names = pdata->names; |
| 360 | pullup_dis_mask = pdata->pullup_dis_mask; |
| 361 | } |
| 362 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 363 | gc->ngpio = ADP5588_MAXGPIO; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 364 | gc->label = client->name; |
| 365 | gc->owner = THIS_MODULE; |
| 366 | |
| 367 | mutex_init(&dev->lock); |
| 368 | |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 369 | ret = adp5588_gpio_read(dev->client, DEV_ID); |
| 370 | if (ret < 0) |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 371 | return ret; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 372 | |
| 373 | revid = ret & ADP5588_DEVICE_ID_MASK; |
| 374 | |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 375 | for (i = 0, ret = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) { |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 376 | dev->dat_out[i] = adp5588_gpio_read(client, GPIO_DAT_OUT1 + i); |
| 377 | dev->dir[i] = adp5588_gpio_read(client, GPIO_DIR1 + i); |
| 378 | ret |= adp5588_gpio_write(client, KP_GPIO1 + i, 0); |
| 379 | ret |= adp5588_gpio_write(client, GPIO_PULL1 + i, |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 380 | (pullup_dis_mask >> (8 * i)) & 0xFF); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 381 | ret |= adp5588_gpio_write(client, GPIO_INT_EN1 + i, 0); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 382 | if (ret) |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 383 | return ret; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 386 | if (client->irq) { |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 387 | if (WA_DELAYED_READOUT_REVID(revid)) { |
| 388 | dev_warn(&client->dev, "GPIO int not supported\n"); |
| 389 | } else { |
| 390 | ret = adp5588_irq_setup(dev); |
| 391 | if (ret) |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 392 | return ret; |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Laxman Dewangan | 7c263fe | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 396 | ret = devm_gpiochip_add_data(&client->dev, &dev->gpio_chip, dev); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 397 | if (ret) |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 398 | return ret; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 399 | |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 400 | if (pdata && pdata->setup) { |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 401 | ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context); |
| 402 | if (ret < 0) |
| 403 | dev_warn(&client->dev, "setup failed, %d\n", ret); |
| 404 | } |
| 405 | |
| 406 | i2c_set_clientdata(client, dev); |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 407 | |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 408 | return 0; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 409 | } |
| 410 | |
Bill Pemberton | 206210c | 2012-11-19 13:25:50 -0500 | [diff] [blame] | 411 | static int adp5588_gpio_remove(struct i2c_client *client) |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 412 | { |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 413 | struct adp5588_gpio_platform_data *pdata = |
| 414 | dev_get_platdata(&client->dev); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 415 | struct adp5588_gpio *dev = i2c_get_clientdata(client); |
| 416 | int ret; |
| 417 | |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 418 | if (pdata && pdata->teardown) { |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 419 | ret = pdata->teardown(client, |
| 420 | dev->gpio_chip.base, dev->gpio_chip.ngpio, |
| 421 | pdata->context); |
| 422 | if (ret < 0) { |
| 423 | dev_err(&client->dev, "teardown failed %d\n", ret); |
| 424 | return ret; |
| 425 | } |
| 426 | } |
| 427 | |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 428 | if (dev->client->irq) |
Michael Hennerich | 459773a | 2010-10-27 15:33:19 -0700 | [diff] [blame] | 429 | free_irq(dev->client->irq, dev); |
| 430 | |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | static const struct i2c_device_id adp5588_gpio_id[] = { |
| 435 | {DRV_NAME, 0}, |
| 436 | {} |
| 437 | }; |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 438 | MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id); |
| 439 | |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 440 | #ifdef CONFIG_OF |
| 441 | static const struct of_device_id adp5588_gpio_of_id[] = { |
| 442 | { .compatible = "adi," DRV_NAME, }, |
| 443 | {}, |
| 444 | }; |
| 445 | MODULE_DEVICE_TABLE(of, adp5588_gpio_of_id); |
| 446 | #endif |
| 447 | |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 448 | static struct i2c_driver adp5588_gpio_driver = { |
| 449 | .driver = { |
Nikolaus Voss | 9f22af1 | 2018-07-25 10:43:57 +0200 | [diff] [blame] | 450 | .name = DRV_NAME, |
| 451 | .of_match_table = of_match_ptr(adp5588_gpio_of_id), |
| 452 | }, |
| 453 | .probe_new = adp5588_gpio_probe, |
Bill Pemberton | 8283c4f | 2012-11-19 13:20:08 -0500 | [diff] [blame] | 454 | .remove = adp5588_gpio_remove, |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 455 | .id_table = adp5588_gpio_id, |
| 456 | }; |
| 457 | |
Axel Lin | c8554d3 | 2012-09-02 08:08:01 +0800 | [diff] [blame] | 458 | module_i2c_driver(adp5588_gpio_driver); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 459 | |
Michael Hennerich | be88784 | 2018-08-14 13:29:18 +0200 | [diff] [blame] | 460 | MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); |
Michael Hennerich | 8088409 | 2010-01-08 14:43:08 -0800 | [diff] [blame] | 461 | MODULE_DESCRIPTION("GPIO ADP5588 Driver"); |
| 462 | MODULE_LICENSE("GPL"); |