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>
18 #include <linux/of_device.h>
20 #include <linux/platform_data/adp5588.h>
22 #define DRV_NAME "adp5588-gpio"
25 * Early pre 4.0 Silicon required to delay readout by at least 25ms,
26 * since the Event Counter Register updated 25ms after the interrupt
29 #define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4)
32 struct i2c_client *client;
33 struct gpio_chip gpio_chip;
34 struct mutex lock; /* protect cached dir, dat_out */
35 /* protect serialized access to the interrupt controller bus */
36 struct mutex irq_lock;
39 uint8_t int_lvl_low[3];
40 uint8_t int_lvl_high[3];
43 uint8_t int_input_en[3];
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
150 static void adp5588_irq_bus_lock(struct irq_data *d)
152 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
153 struct adp5588_gpio *dev = gpiochip_get_data(gc);
155 mutex_lock(&dev->irq_lock);
159 * genirq core code can issue chip->mask/unmask from atomic context.
160 * This doesn't work for slow busses where an access needs to sleep.
161 * bus_sync_unlock() is therefore called outside the atomic context,
162 * syncs the current irq mask state with the slow external controller
163 * and unlocks the bus.
166 static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
168 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
169 struct adp5588_gpio *dev = gpiochip_get_data(gc);
172 for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
173 if (dev->int_input_en[i]) {
174 mutex_lock(&dev->lock);
175 dev->dir[i] &= ~dev->int_input_en[i];
176 dev->int_input_en[i] = 0;
177 adp5588_gpio_write(dev->client, GPIO_DIR1 + i,
179 mutex_unlock(&dev->lock);
182 if (dev->int_en[i] ^ dev->irq_mask[i]) {
183 dev->int_en[i] = dev->irq_mask[i];
184 adp5588_gpio_write(dev->client, GPI_EM1 + i,
189 mutex_unlock(&dev->irq_lock);
192 static void adp5588_irq_mask(struct irq_data *d)
194 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
195 struct adp5588_gpio *dev = gpiochip_get_data(gc);
197 dev->irq_mask[ADP5588_BANK(d->hwirq)] &= ~ADP5588_BIT(d->hwirq);
200 static void adp5588_irq_unmask(struct irq_data *d)
202 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
203 struct adp5588_gpio *dev = gpiochip_get_data(gc);
205 dev->irq_mask[ADP5588_BANK(d->hwirq)] |= ADP5588_BIT(d->hwirq);
208 static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
210 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
211 struct adp5588_gpio *dev = gpiochip_get_data(gc);
212 uint16_t gpio = d->hwirq;
215 bank = ADP5588_BANK(gpio);
216 bit = ADP5588_BIT(gpio);
218 dev->int_lvl_low[bank] &= ~bit;
219 dev->int_lvl_high[bank] &= ~bit;
221 if (type & IRQ_TYPE_EDGE_BOTH || type & IRQ_TYPE_LEVEL_HIGH)
222 dev->int_lvl_high[bank] |= bit;
224 if (type & IRQ_TYPE_EDGE_BOTH || type & IRQ_TYPE_LEVEL_LOW)
225 dev->int_lvl_low[bank] |= bit;
227 dev->int_input_en[bank] |= bit;
232 static struct irq_chip adp5588_irq_chip = {
234 .irq_mask = adp5588_irq_mask,
235 .irq_unmask = adp5588_irq_unmask,
236 .irq_bus_lock = adp5588_irq_bus_lock,
237 .irq_bus_sync_unlock = adp5588_irq_bus_sync_unlock,
238 .irq_set_type = adp5588_irq_set_type,
241 static irqreturn_t adp5588_irq_handler(int irq, void *devid)
243 struct adp5588_gpio *dev = devid;
244 int status = adp5588_gpio_read(dev->client, INT_STAT);
246 if (status & ADP5588_KE_INT) {
247 int ev_cnt = adp5588_gpio_read(dev->client, KEY_LCK_EC_STAT);
252 for (i = 0; i < (ev_cnt & ADP5588_KEC); i++) {
253 int key = adp5588_gpio_read(dev->client,
255 /* GPIN events begin at 97,
256 * bit 7 indicates logic level
258 int gpio = (key & 0x7f) - 97;
259 int lvl = key & (1 << 7);
260 int bank = ADP5588_BANK(gpio);
261 int bit = ADP5588_BIT(gpio);
263 if ((lvl && dev->int_lvl_high[bank] & bit) ||
264 (!lvl && dev->int_lvl_low[bank] & bit))
265 handle_nested_irq(irq_find_mapping(
266 dev->gpio_chip.irq.domain, gpio));
271 adp5588_gpio_write(dev->client, INT_STAT, status); /* Status is W1C */
276 static int adp5588_irq_setup(struct adp5588_gpio *dev)
278 struct i2c_client *client = dev->client;
280 struct adp5588_gpio_platform_data *pdata =
281 dev_get_platdata(&client->dev);
282 int irq_base = pdata ? pdata->irq_base : 0;
284 adp5588_gpio_write(client, CFG, ADP5588_AUTO_INC);
285 adp5588_gpio_write(client, INT_STAT, -1); /* status is W1C */
287 mutex_init(&dev->irq_lock);
289 ret = devm_request_threaded_irq(&client->dev, client->irq,
290 NULL, adp5588_irq_handler, IRQF_ONESHOT
291 | IRQF_TRIGGER_FALLING | IRQF_SHARED,
292 dev_name(&client->dev), dev);
294 dev_err(&client->dev, "failed to request irq %d\n",
298 ret = gpiochip_irqchip_add_nested(&dev->gpio_chip,
299 &adp5588_irq_chip, irq_base,
303 dev_err(&client->dev,
304 "could not connect irqchip to gpiochip\n");
307 gpiochip_set_nested_irqchip(&dev->gpio_chip,
311 adp5588_gpio_write(client, CFG,
312 ADP5588_AUTO_INC | ADP5588_INT_CFG | ADP5588_KE_IEN);
318 static int adp5588_irq_setup(struct adp5588_gpio *dev)
320 struct i2c_client *client = dev->client;
321 dev_warn(&client->dev, "interrupt support not compiled in\n");
326 #endif /* CONFIG_GPIO_ADP5588_IRQ */
328 static int adp5588_gpio_probe(struct i2c_client *client)
330 struct adp5588_gpio_platform_data *pdata =
331 dev_get_platdata(&client->dev);
332 struct adp5588_gpio *dev;
333 struct gpio_chip *gc;
335 unsigned int pullup_dis_mask = 0;
337 if (!i2c_check_functionality(client->adapter,
338 I2C_FUNC_SMBUS_BYTE_DATA)) {
339 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
343 dev = devm_kzalloc(&client->dev, sizeof(*dev), GFP_KERNEL);
347 dev->client = client;
349 gc = &dev->gpio_chip;
350 gc->direction_input = adp5588_gpio_direction_input;
351 gc->direction_output = adp5588_gpio_direction_output;
352 gc->get = adp5588_gpio_get_value;
353 gc->set = adp5588_gpio_set_value;
354 gc->can_sleep = true;
356 gc->parent = &client->dev;
359 gc->base = pdata->gpio_start;
360 gc->names = pdata->names;
361 pullup_dis_mask = pdata->pullup_dis_mask;
364 gc->ngpio = ADP5588_MAXGPIO;
365 gc->label = client->name;
366 gc->owner = THIS_MODULE;
368 mutex_init(&dev->lock);
370 ret = adp5588_gpio_read(dev->client, DEV_ID);
374 revid = ret & ADP5588_DEVICE_ID_MASK;
376 for (i = 0, ret = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
377 dev->dat_out[i] = adp5588_gpio_read(client, GPIO_DAT_OUT1 + i);
378 dev->dir[i] = adp5588_gpio_read(client, GPIO_DIR1 + i);
379 ret |= adp5588_gpio_write(client, KP_GPIO1 + i, 0);
380 ret |= adp5588_gpio_write(client, GPIO_PULL1 + i,
381 (pullup_dis_mask >> (8 * i)) & 0xFF);
382 ret |= adp5588_gpio_write(client, GPIO_INT_EN1 + i, 0);
388 if (WA_DELAYED_READOUT_REVID(revid)) {
389 dev_warn(&client->dev, "GPIO int not supported\n");
391 ret = adp5588_irq_setup(dev);
397 ret = devm_gpiochip_add_data(&client->dev, &dev->gpio_chip, dev);
401 if (pdata && pdata->setup) {
402 ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context);
404 dev_warn(&client->dev, "setup failed, %d\n", ret);
407 i2c_set_clientdata(client, dev);
412 static int adp5588_gpio_remove(struct i2c_client *client)
414 struct adp5588_gpio_platform_data *pdata =
415 dev_get_platdata(&client->dev);
416 struct adp5588_gpio *dev = i2c_get_clientdata(client);
419 if (pdata && pdata->teardown) {
420 ret = pdata->teardown(client,
421 dev->gpio_chip.base, dev->gpio_chip.ngpio,
424 dev_err(&client->dev, "teardown failed %d\n", ret);
429 if (dev->client->irq)
430 free_irq(dev->client->irq, dev);
435 static const struct i2c_device_id adp5588_gpio_id[] = {
439 MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id);
442 static const struct of_device_id adp5588_gpio_of_id[] = {
443 { .compatible = "adi," DRV_NAME, },
446 MODULE_DEVICE_TABLE(of, adp5588_gpio_of_id);
449 static struct i2c_driver adp5588_gpio_driver = {
452 .of_match_table = of_match_ptr(adp5588_gpio_of_id),
454 .probe_new = adp5588_gpio_probe,
455 .remove = adp5588_gpio_remove,
456 .id_table = adp5588_gpio_id,
459 module_i2c_driver(adp5588_gpio_driver);
461 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
462 MODULE_DESCRIPTION("GPIO ADP5588 Driver");
463 MODULE_LICENSE("GPL");