1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * GPIO Chip driver for Analog Devices
4 * ADP5588/ADP5587 I/O Expander and QWERTY Keypad Controller
6 * Copyright 2009-2010 Analog Devices Inc.
9 #include <linux/gpio/driver.h>
10 #include <linux/i2c.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/kernel.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
19 #include <linux/platform_data/adp5588.h>
22 * Early pre 4.0 Silicon required to delay readout by at least 25ms,
23 * since the Event Counter Register updated 25ms after the interrupt
26 #define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4)
29 struct i2c_client *client;
30 struct gpio_chip gpio_chip;
31 struct mutex lock; /* protect cached dir, dat_out */
32 /* protect serialized access to the interrupt controller bus */
33 struct mutex irq_lock;
36 uint8_t int_lvl_low[3];
37 uint8_t int_lvl_high[3];
40 uint8_t int_input_en[3];
43 static int adp5588_gpio_read(struct i2c_client *client, u8 reg)
45 int ret = i2c_smbus_read_byte_data(client, reg);
48 dev_err(&client->dev, "Read Error\n");
53 static int adp5588_gpio_write(struct i2c_client *client, u8 reg, u8 val)
55 int ret = i2c_smbus_write_byte_data(client, reg, val);
58 dev_err(&client->dev, "Write Error\n");
63 static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off)
65 struct adp5588_gpio *dev = gpiochip_get_data(chip);
66 unsigned bank = ADP5588_BANK(off);
67 unsigned bit = ADP5588_BIT(off);
70 mutex_lock(&dev->lock);
72 if (dev->dir[bank] & bit)
73 val = dev->dat_out[bank];
75 val = adp5588_gpio_read(dev->client, GPIO_DAT_STAT1 + bank);
77 mutex_unlock(&dev->lock);
82 static void adp5588_gpio_set_value(struct gpio_chip *chip,
83 unsigned off, int val)
86 struct adp5588_gpio *dev = gpiochip_get_data(chip);
88 bank = ADP5588_BANK(off);
89 bit = ADP5588_BIT(off);
91 mutex_lock(&dev->lock);
93 dev->dat_out[bank] |= bit;
95 dev->dat_out[bank] &= ~bit;
97 adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
99 mutex_unlock(&dev->lock);
102 static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
106 struct adp5588_gpio *dev = gpiochip_get_data(chip);
108 bank = ADP5588_BANK(off);
110 mutex_lock(&dev->lock);
111 dev->dir[bank] &= ~ADP5588_BIT(off);
112 ret = adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, dev->dir[bank]);
113 mutex_unlock(&dev->lock);
118 static int adp5588_gpio_direction_output(struct gpio_chip *chip,
119 unsigned off, int val)
123 struct adp5588_gpio *dev = gpiochip_get_data(chip);
125 bank = ADP5588_BANK(off);
126 bit = ADP5588_BIT(off);
128 mutex_lock(&dev->lock);
129 dev->dir[bank] |= bit;
132 dev->dat_out[bank] |= bit;
134 dev->dat_out[bank] &= ~bit;
136 ret = adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
138 ret |= adp5588_gpio_write(dev->client, GPIO_DIR1 + bank,
140 mutex_unlock(&dev->lock);
145 #ifdef CONFIG_GPIO_ADP5588_IRQ
147 static void adp5588_irq_bus_lock(struct irq_data *d)
149 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
150 struct adp5588_gpio *dev = gpiochip_get_data(gc);
152 mutex_lock(&dev->irq_lock);
156 * genirq core code can issue chip->mask/unmask from atomic context.
157 * This doesn't work for slow busses where an access needs to sleep.
158 * bus_sync_unlock() is therefore called outside the atomic context,
159 * syncs the current irq mask state with the slow external controller
160 * and unlocks the bus.
163 static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
165 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
166 struct adp5588_gpio *dev = gpiochip_get_data(gc);
169 for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
170 if (dev->int_input_en[i]) {
171 mutex_lock(&dev->lock);
172 dev->dir[i] &= ~dev->int_input_en[i];
173 dev->int_input_en[i] = 0;
174 adp5588_gpio_write(dev->client, GPIO_DIR1 + i,
176 mutex_unlock(&dev->lock);
179 if (dev->int_en[i] ^ dev->irq_mask[i]) {
180 dev->int_en[i] = dev->irq_mask[i];
181 adp5588_gpio_write(dev->client, GPI_EM1 + i,
186 mutex_unlock(&dev->irq_lock);
189 static void adp5588_irq_mask(struct irq_data *d)
191 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
192 struct adp5588_gpio *dev = gpiochip_get_data(gc);
194 dev->irq_mask[ADP5588_BANK(d->hwirq)] &= ~ADP5588_BIT(d->hwirq);
197 static void adp5588_irq_unmask(struct irq_data *d)
199 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
200 struct adp5588_gpio *dev = gpiochip_get_data(gc);
202 dev->irq_mask[ADP5588_BANK(d->hwirq)] |= ADP5588_BIT(d->hwirq);
205 static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
207 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
208 struct adp5588_gpio *dev = gpiochip_get_data(gc);
209 uint16_t gpio = d->hwirq;
212 bank = ADP5588_BANK(gpio);
213 bit = ADP5588_BIT(gpio);
215 dev->int_lvl_low[bank] &= ~bit;
216 dev->int_lvl_high[bank] &= ~bit;
218 if (type & IRQ_TYPE_EDGE_BOTH || type & IRQ_TYPE_LEVEL_HIGH)
219 dev->int_lvl_high[bank] |= bit;
221 if (type & IRQ_TYPE_EDGE_BOTH || type & IRQ_TYPE_LEVEL_LOW)
222 dev->int_lvl_low[bank] |= bit;
224 dev->int_input_en[bank] |= bit;
229 static struct irq_chip adp5588_irq_chip = {
231 .irq_mask = adp5588_irq_mask,
232 .irq_unmask = adp5588_irq_unmask,
233 .irq_bus_lock = adp5588_irq_bus_lock,
234 .irq_bus_sync_unlock = adp5588_irq_bus_sync_unlock,
235 .irq_set_type = adp5588_irq_set_type,
238 static irqreturn_t adp5588_irq_handler(int irq, void *devid)
240 struct adp5588_gpio *dev = devid;
241 int status = adp5588_gpio_read(dev->client, INT_STAT);
243 if (status & ADP5588_KE_INT) {
244 int ev_cnt = adp5588_gpio_read(dev->client, KEY_LCK_EC_STAT);
249 for (i = 0; i < (ev_cnt & ADP5588_KEC); i++) {
250 int key = adp5588_gpio_read(dev->client,
252 /* GPIN events begin at 97,
253 * bit 7 indicates logic level
255 int gpio = (key & 0x7f) - 97;
256 int lvl = key & (1 << 7);
257 int bank = ADP5588_BANK(gpio);
258 int bit = ADP5588_BIT(gpio);
260 if ((lvl && dev->int_lvl_high[bank] & bit) ||
261 (!lvl && dev->int_lvl_low[bank] & bit))
262 handle_nested_irq(irq_find_mapping(
263 dev->gpio_chip.irq.domain, gpio));
268 adp5588_gpio_write(dev->client, INT_STAT, status); /* Status is W1C */
274 static int adp5588_irq_init_hw(struct gpio_chip *gc)
276 struct adp5588_gpio *dev = gpiochip_get_data(gc);
277 /* Enable IRQs after registering chip */
278 adp5588_gpio_write(dev->client, CFG,
279 ADP5588_AUTO_INC | ADP5588_INT_CFG | ADP5588_KE_IEN);
284 static int adp5588_irq_setup(struct adp5588_gpio *dev)
286 struct i2c_client *client = dev->client;
288 struct adp5588_gpio_platform_data *pdata =
289 dev_get_platdata(&client->dev);
290 struct gpio_irq_chip *girq;
292 adp5588_gpio_write(client, CFG, ADP5588_AUTO_INC);
293 adp5588_gpio_write(client, INT_STAT, -1); /* status is W1C */
295 mutex_init(&dev->irq_lock);
297 ret = devm_request_threaded_irq(&client->dev, client->irq,
298 NULL, adp5588_irq_handler, IRQF_ONESHOT
299 | IRQF_TRIGGER_FALLING | IRQF_SHARED,
300 dev_name(&client->dev), dev);
302 dev_err(&client->dev, "failed to request irq %d\n",
307 /* This will be registered in the call to devm_gpiochip_add_data() */
308 girq = &dev->gpio_chip.irq;
309 girq->chip = &adp5588_irq_chip;
310 /* This will let us handle the parent IRQ in the driver */
311 girq->parent_handler = NULL;
312 girq->num_parents = 0;
313 girq->parents = NULL;
314 girq->first = pdata ? pdata->irq_base : 0;
315 girq->default_type = IRQ_TYPE_NONE;
316 girq->handler = handle_simple_irq;
317 girq->init_hw = adp5588_irq_init_hw;
318 girq->threaded = true;
324 static int adp5588_irq_setup(struct adp5588_gpio *dev)
326 struct i2c_client *client = dev->client;
327 dev_warn(&client->dev, "interrupt support not compiled in\n");
332 #endif /* CONFIG_GPIO_ADP5588_IRQ */
334 static int adp5588_gpio_probe(struct i2c_client *client)
336 struct adp5588_gpio_platform_data *pdata =
337 dev_get_platdata(&client->dev);
338 struct adp5588_gpio *dev;
339 struct gpio_chip *gc;
341 unsigned int pullup_dis_mask = 0;
343 if (!i2c_check_functionality(client->adapter,
344 I2C_FUNC_SMBUS_BYTE_DATA)) {
345 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
349 dev = devm_kzalloc(&client->dev, sizeof(*dev), GFP_KERNEL);
353 dev->client = client;
355 gc = &dev->gpio_chip;
356 gc->direction_input = adp5588_gpio_direction_input;
357 gc->direction_output = adp5588_gpio_direction_output;
358 gc->get = adp5588_gpio_get_value;
359 gc->set = adp5588_gpio_set_value;
360 gc->can_sleep = true;
362 gc->parent = &client->dev;
365 gc->base = pdata->gpio_start;
366 gc->names = pdata->names;
367 pullup_dis_mask = pdata->pullup_dis_mask;
370 gc->ngpio = ADP5588_MAXGPIO;
371 gc->label = client->name;
372 gc->owner = THIS_MODULE;
374 mutex_init(&dev->lock);
376 ret = adp5588_gpio_read(dev->client, DEV_ID);
380 revid = ret & ADP5588_DEVICE_ID_MASK;
382 for (i = 0, ret = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
383 dev->dat_out[i] = adp5588_gpio_read(client, GPIO_DAT_OUT1 + i);
384 dev->dir[i] = adp5588_gpio_read(client, GPIO_DIR1 + i);
385 ret |= adp5588_gpio_write(client, KP_GPIO1 + i, 0);
386 ret |= adp5588_gpio_write(client, GPIO_PULL1 + i,
387 (pullup_dis_mask >> (8 * i)) & 0xFF);
388 ret |= adp5588_gpio_write(client, GPIO_INT_EN1 + i, 0);
394 if (WA_DELAYED_READOUT_REVID(revid)) {
395 dev_warn(&client->dev, "GPIO int not supported\n");
397 ret = adp5588_irq_setup(dev);
403 ret = devm_gpiochip_add_data(&client->dev, &dev->gpio_chip, dev);
407 i2c_set_clientdata(client, dev);
412 static void adp5588_gpio_remove(struct i2c_client *client)
414 struct adp5588_gpio *dev = i2c_get_clientdata(client);
416 if (dev->client->irq)
417 free_irq(dev->client->irq, dev);
420 static const struct i2c_device_id adp5588_gpio_id[] = {
424 MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id);
426 static const struct of_device_id adp5588_gpio_of_id[] = {
427 { .compatible = "adi,adp5588-gpio" },
430 MODULE_DEVICE_TABLE(of, adp5588_gpio_of_id);
432 static struct i2c_driver adp5588_gpio_driver = {
434 .name = "adp5588-gpio",
435 .of_match_table = adp5588_gpio_of_id,
437 .probe_new = adp5588_gpio_probe,
438 .remove = adp5588_gpio_remove,
439 .id_table = adp5588_gpio_id,
442 module_i2c_driver(adp5588_gpio_driver);
444 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
445 MODULE_DESCRIPTION("GPIO ADP5588 Driver");
446 MODULE_LICENSE("GPL");