2 * GPIO Chip driver for Analog Devices
3 * ADP5588/ADP5587 I/O Expander and QWERTY Keypad Controller
5 * Copyright 2009-2010 Analog Devices Inc.
7 * Licensed under the GPL-2 or later.
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/init.h>
14 #include <linux/i2c.h>
15 #include <linux/gpio/driver.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
19 #include <linux/platform_data/adp5588.h>
21 #define DRV_NAME "adp5588-gpio"
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
28 #define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4)
31 struct i2c_client *client;
32 struct gpio_chip gpio_chip;
33 struct mutex lock; /* protect cached dir, dat_out */
34 /* protect serialized access to the interrupt controller bus */
35 struct mutex irq_lock;
46 static int adp5588_gpio_read(struct i2c_client *client, u8 reg)
48 int ret = i2c_smbus_read_byte_data(client, reg);
51 dev_err(&client->dev, "Read Error\n");
56 static int adp5588_gpio_write(struct i2c_client *client, u8 reg, u8 val)
58 int ret = i2c_smbus_write_byte_data(client, reg, val);
61 dev_err(&client->dev, "Write Error\n");
66 static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off)
68 struct adp5588_gpio *dev = gpiochip_get_data(chip);
69 unsigned bank = ADP5588_BANK(off);
70 unsigned bit = ADP5588_BIT(off);
73 mutex_lock(&dev->lock);
75 if (dev->dir[bank] & bit)
76 val = dev->dat_out[bank];
78 val = adp5588_gpio_read(dev->client, GPIO_DAT_STAT1 + bank);
80 mutex_unlock(&dev->lock);
85 static void adp5588_gpio_set_value(struct gpio_chip *chip,
86 unsigned off, int val)
89 struct adp5588_gpio *dev = gpiochip_get_data(chip);
91 bank = ADP5588_BANK(off);
92 bit = ADP5588_BIT(off);
94 mutex_lock(&dev->lock);
96 dev->dat_out[bank] |= bit;
98 dev->dat_out[bank] &= ~bit;
100 adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
102 mutex_unlock(&dev->lock);
105 static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
109 struct adp5588_gpio *dev = gpiochip_get_data(chip);
111 bank = ADP5588_BANK(off);
113 mutex_lock(&dev->lock);
114 dev->dir[bank] &= ~ADP5588_BIT(off);
115 ret = adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, dev->dir[bank]);
116 mutex_unlock(&dev->lock);
121 static int adp5588_gpio_direction_output(struct gpio_chip *chip,
122 unsigned off, int val)
126 struct adp5588_gpio *dev = gpiochip_get_data(chip);
128 bank = ADP5588_BANK(off);
129 bit = ADP5588_BIT(off);
131 mutex_lock(&dev->lock);
132 dev->dir[bank] |= bit;
135 dev->dat_out[bank] |= bit;
137 dev->dat_out[bank] &= ~bit;
139 ret = adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
141 ret |= adp5588_gpio_write(dev->client, GPIO_DIR1 + bank,
143 mutex_unlock(&dev->lock);
148 #ifdef CONFIG_GPIO_ADP5588_IRQ
149 static int adp5588_gpio_to_irq(struct gpio_chip *chip, unsigned off)
151 struct adp5588_gpio *dev = gpiochip_get_data(chip);
153 return dev->irq_base + off;
156 static void adp5588_irq_bus_lock(struct irq_data *d)
158 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
160 mutex_lock(&dev->irq_lock);
164 * genirq core code can issue chip->mask/unmask from atomic context.
165 * This doesn't work for slow busses where an access needs to sleep.
166 * bus_sync_unlock() is therefore called outside the atomic context,
167 * syncs the current irq mask state with the slow external controller
168 * and unlocks the bus.
171 static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
173 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
176 for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++)
177 if (dev->int_en[i] ^ dev->irq_mask[i]) {
178 dev->int_en[i] = dev->irq_mask[i];
179 adp5588_gpio_write(dev->client, GPIO_INT_EN1 + i,
183 mutex_unlock(&dev->irq_lock);
186 static void adp5588_irq_mask(struct irq_data *d)
188 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
189 unsigned gpio = d->irq - dev->irq_base;
191 dev->irq_mask[ADP5588_BANK(gpio)] &= ~ADP5588_BIT(gpio);
194 static void adp5588_irq_unmask(struct irq_data *d)
196 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
197 unsigned gpio = d->irq - dev->irq_base;
199 dev->irq_mask[ADP5588_BANK(gpio)] |= ADP5588_BIT(gpio);
202 static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
204 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
205 uint16_t gpio = d->irq - dev->irq_base;
208 if ((type & IRQ_TYPE_EDGE_BOTH)) {
209 dev_err(&dev->client->dev, "irq %d: unsupported type %d\n",
214 bank = ADP5588_BANK(gpio);
215 bit = ADP5588_BIT(gpio);
217 if (type & IRQ_TYPE_LEVEL_HIGH)
218 dev->int_lvl[bank] |= bit;
219 else if (type & IRQ_TYPE_LEVEL_LOW)
220 dev->int_lvl[bank] &= ~bit;
224 adp5588_gpio_direction_input(&dev->gpio_chip, gpio);
225 adp5588_gpio_write(dev->client, GPIO_INT_LVL1 + bank,
231 static struct irq_chip adp5588_irq_chip = {
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,
240 static int adp5588_gpio_read_intstat(struct i2c_client *client, u8 *buf)
242 int ret = i2c_smbus_read_i2c_block_data(client, GPIO_INT_STAT1, 3, buf);
245 dev_err(&client->dev, "Read INT_STAT Error\n");
250 static irqreturn_t adp5588_irq_handler(int irq, void *devid)
252 struct adp5588_gpio *dev = devid;
253 unsigned status, bank, bit, pending;
255 status = adp5588_gpio_read(dev->client, INT_STAT);
257 if (status & ADP5588_GPI_INT) {
258 ret = adp5588_gpio_read_intstat(dev->client, dev->irq_stat);
260 memset(dev->irq_stat, 0, ARRAY_SIZE(dev->irq_stat));
262 for (bank = 0, bit = 0; bank <= ADP5588_BANK(ADP5588_MAXGPIO);
264 pending = dev->irq_stat[bank] & dev->irq_mask[bank];
267 if (pending & (1 << bit)) {
268 handle_nested_irq(dev->irq_base +
270 pending &= ~(1 << bit);
278 adp5588_gpio_write(dev->client, INT_STAT, status); /* Status is W1C */
283 static int adp5588_irq_setup(struct adp5588_gpio *dev)
285 struct i2c_client *client = dev->client;
286 struct adp5588_gpio_platform_data *pdata =
287 dev_get_platdata(&client->dev);
291 adp5588_gpio_write(client, CFG, ADP5588_AUTO_INC);
292 adp5588_gpio_write(client, INT_STAT, -1); /* status is W1C */
293 adp5588_gpio_read_intstat(client, dev->irq_stat); /* read to clear */
295 dev->irq_base = pdata->irq_base;
296 mutex_init(&dev->irq_lock);
298 for (gpio = 0; gpio < dev->gpio_chip.ngpio; gpio++) {
299 int irq = gpio + dev->irq_base;
300 irq_set_chip_data(irq, dev);
301 irq_set_chip_and_handler(irq, &adp5588_irq_chip,
303 irq_set_nested_thread(irq, 1);
304 irq_modify_status(irq, IRQ_NOREQUEST, IRQ_NOPROBE);
307 ret = request_threaded_irq(client->irq,
310 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
311 dev_name(&client->dev), dev);
313 dev_err(&client->dev, "failed to request irq %d\n",
318 dev->gpio_chip.to_irq = adp5588_gpio_to_irq;
319 adp5588_gpio_write(client, CFG,
320 ADP5588_AUTO_INC | ADP5588_INT_CFG | ADP5588_GPI_INT);
329 static void adp5588_irq_teardown(struct adp5588_gpio *dev)
332 free_irq(dev->client->irq, dev);
336 static int adp5588_irq_setup(struct adp5588_gpio *dev)
338 struct i2c_client *client = dev->client;
339 dev_warn(&client->dev, "interrupt support not compiled in\n");
344 static void adp5588_irq_teardown(struct adp5588_gpio *dev)
347 #endif /* CONFIG_GPIO_ADP5588_IRQ */
349 static int adp5588_gpio_probe(struct i2c_client *client,
350 const struct i2c_device_id *id)
352 struct adp5588_gpio_platform_data *pdata =
353 dev_get_platdata(&client->dev);
354 struct adp5588_gpio *dev;
355 struct gpio_chip *gc;
359 dev_err(&client->dev, "missing platform data\n");
363 if (!i2c_check_functionality(client->adapter,
364 I2C_FUNC_SMBUS_BYTE_DATA)) {
365 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
369 dev = devm_kzalloc(&client->dev, sizeof(*dev), GFP_KERNEL);
373 dev->client = client;
375 gc = &dev->gpio_chip;
376 gc->direction_input = adp5588_gpio_direction_input;
377 gc->direction_output = adp5588_gpio_direction_output;
378 gc->get = adp5588_gpio_get_value;
379 gc->set = adp5588_gpio_set_value;
380 gc->can_sleep = true;
382 gc->base = pdata->gpio_start;
383 gc->ngpio = ADP5588_MAXGPIO;
384 gc->label = client->name;
385 gc->owner = THIS_MODULE;
386 gc->names = pdata->names;
388 mutex_init(&dev->lock);
390 ret = adp5588_gpio_read(dev->client, DEV_ID);
394 revid = ret & ADP5588_DEVICE_ID_MASK;
396 for (i = 0, ret = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
397 dev->dat_out[i] = adp5588_gpio_read(client, GPIO_DAT_OUT1 + i);
398 dev->dir[i] = adp5588_gpio_read(client, GPIO_DIR1 + i);
399 ret |= adp5588_gpio_write(client, KP_GPIO1 + i, 0);
400 ret |= adp5588_gpio_write(client, GPIO_PULL1 + i,
401 (pdata->pullup_dis_mask >> (8 * i)) & 0xFF);
402 ret |= adp5588_gpio_write(client, GPIO_INT_EN1 + i, 0);
407 if (pdata->irq_base) {
408 if (WA_DELAYED_READOUT_REVID(revid)) {
409 dev_warn(&client->dev, "GPIO int not supported\n");
411 ret = adp5588_irq_setup(dev);
417 ret = devm_gpiochip_add_data(&client->dev, &dev->gpio_chip, dev);
421 dev_info(&client->dev, "IRQ Base: %d Rev.: %d\n",
422 pdata->irq_base, revid);
425 ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context);
427 dev_warn(&client->dev, "setup failed, %d\n", ret);
430 i2c_set_clientdata(client, dev);
435 adp5588_irq_teardown(dev);
440 static int adp5588_gpio_remove(struct i2c_client *client)
442 struct adp5588_gpio_platform_data *pdata =
443 dev_get_platdata(&client->dev);
444 struct adp5588_gpio *dev = i2c_get_clientdata(client);
447 if (pdata->teardown) {
448 ret = pdata->teardown(client,
449 dev->gpio_chip.base, dev->gpio_chip.ngpio,
452 dev_err(&client->dev, "teardown failed %d\n", ret);
458 free_irq(dev->client->irq, dev);
463 static const struct i2c_device_id adp5588_gpio_id[] = {
468 MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id);
470 static struct i2c_driver adp5588_gpio_driver = {
474 .probe = adp5588_gpio_probe,
475 .remove = adp5588_gpio_remove,
476 .id_table = adp5588_gpio_id,
479 module_i2c_driver(adp5588_gpio_driver);
481 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
482 MODULE_DESCRIPTION("GPIO ADP5588 Driver");
483 MODULE_LICENSE("GPL");