blob: 90fc4c99c024ee39605381c247236644e4c927b4 [file] [log] [blame]
Michael Hennerich80884092010-01-08 14:43:08 -08001/*
2 * GPIO Chip driver for Analog Devices
Michael Hennerich459773a2010-10-27 15:33:19 -07003 * ADP5588/ADP5587 I/O Expander and QWERTY Keypad Controller
Michael Hennerich80884092010-01-08 14:43:08 -08004 *
Michael Hennerich459773a2010-10-27 15:33:19 -07005 * Copyright 2009-2010 Analog Devices Inc.
Michael Hennerich80884092010-01-08 14:43:08 -08006 *
7 * Licensed under the GPL-2 or later.
8 */
9
10#include <linux/module.h>
11#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Michael Hennerich80884092010-01-08 14:43:08 -080013#include <linux/init.h>
14#include <linux/i2c.h>
15#include <linux/gpio.h>
Michael Hennerich459773a2010-10-27 15:33:19 -070016#include <linux/interrupt.h>
17#include <linux/irq.h>
Michael Hennerich80884092010-01-08 14:43:08 -080018
19#include <linux/i2c/adp5588.h>
20
Michael Hennerich459773a2010-10-27 15:33:19 -070021#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 Hennerich80884092010-01-08 14:43:08 -080029
30struct adp5588_gpio {
31 struct i2c_client *client;
32 struct gpio_chip gpio_chip;
33 struct mutex lock; /* protect cached dir, dat_out */
Michael Hennerich459773a2010-10-27 15:33:19 -070034 /* protect serialized access to the interrupt controller bus */
35 struct mutex irq_lock;
Michael Hennerich80884092010-01-08 14:43:08 -080036 unsigned gpio_start;
Michael Hennerich459773a2010-10-27 15:33:19 -070037 unsigned irq_base;
Michael Hennerich80884092010-01-08 14:43:08 -080038 uint8_t dat_out[3];
39 uint8_t dir[3];
Michael Hennerich459773a2010-10-27 15:33:19 -070040 uint8_t int_lvl[3];
41 uint8_t int_en[3];
42 uint8_t irq_mask[3];
43 uint8_t irq_stat[3];
Michael Hennerich80884092010-01-08 14:43:08 -080044};
45
46static int adp5588_gpio_read(struct i2c_client *client, u8 reg)
47{
48 int ret = i2c_smbus_read_byte_data(client, reg);
49
50 if (ret < 0)
51 dev_err(&client->dev, "Read Error\n");
52
53 return ret;
54}
55
56static int adp5588_gpio_write(struct i2c_client *client, u8 reg, u8 val)
57{
58 int ret = i2c_smbus_write_byte_data(client, reg, val);
59
60 if (ret < 0)
61 dev_err(&client->dev, "Write Error\n");
62
63 return ret;
64}
65
66static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off)
67{
68 struct adp5588_gpio *dev =
69 container_of(chip, struct adp5588_gpio, gpio_chip);
70
Michael Hennerich459773a2010-10-27 15:33:19 -070071 return !!(adp5588_gpio_read(dev->client,
72 GPIO_DAT_STAT1 + ADP5588_BANK(off)) & ADP5588_BIT(off));
Michael Hennerich80884092010-01-08 14:43:08 -080073}
74
75static void adp5588_gpio_set_value(struct gpio_chip *chip,
76 unsigned off, int val)
77{
78 unsigned bank, bit;
79 struct adp5588_gpio *dev =
80 container_of(chip, struct adp5588_gpio, gpio_chip);
81
Michael Hennerich459773a2010-10-27 15:33:19 -070082 bank = ADP5588_BANK(off);
83 bit = ADP5588_BIT(off);
Michael Hennerich80884092010-01-08 14:43:08 -080084
85 mutex_lock(&dev->lock);
86 if (val)
87 dev->dat_out[bank] |= bit;
88 else
89 dev->dat_out[bank] &= ~bit;
90
91 adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
92 dev->dat_out[bank]);
93 mutex_unlock(&dev->lock);
94}
95
96static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
97{
98 int ret;
99 unsigned bank;
100 struct adp5588_gpio *dev =
101 container_of(chip, struct adp5588_gpio, gpio_chip);
102
Michael Hennerich459773a2010-10-27 15:33:19 -0700103 bank = ADP5588_BANK(off);
Michael Hennerich80884092010-01-08 14:43:08 -0800104
105 mutex_lock(&dev->lock);
Michael Hennerich459773a2010-10-27 15:33:19 -0700106 dev->dir[bank] &= ~ADP5588_BIT(off);
Michael Hennerich80884092010-01-08 14:43:08 -0800107 ret = adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, dev->dir[bank]);
108 mutex_unlock(&dev->lock);
109
110 return ret;
111}
112
113static int adp5588_gpio_direction_output(struct gpio_chip *chip,
114 unsigned off, int val)
115{
116 int ret;
117 unsigned bank, bit;
118 struct adp5588_gpio *dev =
119 container_of(chip, struct adp5588_gpio, gpio_chip);
120
Michael Hennerich459773a2010-10-27 15:33:19 -0700121 bank = ADP5588_BANK(off);
122 bit = ADP5588_BIT(off);
Michael Hennerich80884092010-01-08 14:43:08 -0800123
124 mutex_lock(&dev->lock);
125 dev->dir[bank] |= bit;
126
127 if (val)
128 dev->dat_out[bank] |= bit;
129 else
130 dev->dat_out[bank] &= ~bit;
131
132 ret = adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
133 dev->dat_out[bank]);
134 ret |= adp5588_gpio_write(dev->client, GPIO_DIR1 + bank,
135 dev->dir[bank]);
136 mutex_unlock(&dev->lock);
137
138 return ret;
139}
140
Michael Hennerich459773a2010-10-27 15:33:19 -0700141#ifdef CONFIG_GPIO_ADP5588_IRQ
142static int adp5588_gpio_to_irq(struct gpio_chip *chip, unsigned off)
143{
144 struct adp5588_gpio *dev =
145 container_of(chip, struct adp5588_gpio, gpio_chip);
146 return dev->irq_base + off;
147}
148
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800149static void adp5588_irq_bus_lock(struct irq_data *d)
Michael Hennerich459773a2010-10-27 15:33:19 -0700150{
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800151 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
152
Michael Hennerich459773a2010-10-27 15:33:19 -0700153 mutex_lock(&dev->irq_lock);
154}
155
156 /*
157 * genirq core code can issue chip->mask/unmask from atomic context.
158 * This doesn't work for slow busses where an access needs to sleep.
159 * bus_sync_unlock() is therefore called outside the atomic context,
160 * syncs the current irq mask state with the slow external controller
161 * and unlocks the bus.
162 */
163
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800164static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
Michael Hennerich459773a2010-10-27 15:33:19 -0700165{
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800166 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
Michael Hennerich459773a2010-10-27 15:33:19 -0700167 int i;
168
169 for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++)
170 if (dev->int_en[i] ^ dev->irq_mask[i]) {
171 dev->int_en[i] = dev->irq_mask[i];
172 adp5588_gpio_write(dev->client, GPIO_INT_EN1 + i,
173 dev->int_en[i]);
174 }
175
176 mutex_unlock(&dev->irq_lock);
177}
178
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800179static void adp5588_irq_mask(struct irq_data *d)
Michael Hennerich459773a2010-10-27 15:33:19 -0700180{
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800181 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
182 unsigned gpio = d->irq - dev->irq_base;
Michael Hennerich459773a2010-10-27 15:33:19 -0700183
184 dev->irq_mask[ADP5588_BANK(gpio)] &= ~ADP5588_BIT(gpio);
185}
186
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800187static void adp5588_irq_unmask(struct irq_data *d)
Michael Hennerich459773a2010-10-27 15:33:19 -0700188{
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800189 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
190 unsigned gpio = d->irq - dev->irq_base;
Michael Hennerich459773a2010-10-27 15:33:19 -0700191
192 dev->irq_mask[ADP5588_BANK(gpio)] |= ADP5588_BIT(gpio);
193}
194
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800195static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
Michael Hennerich459773a2010-10-27 15:33:19 -0700196{
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800197 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
198 uint16_t gpio = d->irq - dev->irq_base;
Michael Hennerich459773a2010-10-27 15:33:19 -0700199 unsigned bank, bit;
200
201 if ((type & IRQ_TYPE_EDGE_BOTH)) {
202 dev_err(&dev->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800203 d->irq, type);
Michael Hennerich459773a2010-10-27 15:33:19 -0700204 return -EINVAL;
205 }
206
207 bank = ADP5588_BANK(gpio);
208 bit = ADP5588_BIT(gpio);
209
210 if (type & IRQ_TYPE_LEVEL_HIGH)
211 dev->int_lvl[bank] |= bit;
212 else if (type & IRQ_TYPE_LEVEL_LOW)
213 dev->int_lvl[bank] &= ~bit;
214 else
215 return -EINVAL;
216
217 adp5588_gpio_direction_input(&dev->gpio_chip, gpio);
218 adp5588_gpio_write(dev->client, GPIO_INT_LVL1 + bank,
219 dev->int_lvl[bank]);
220
221 return 0;
222}
223
224static struct irq_chip adp5588_irq_chip = {
225 .name = "adp5588",
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800226 .irq_mask = adp5588_irq_mask,
227 .irq_unmask = adp5588_irq_unmask,
228 .irq_bus_lock = adp5588_irq_bus_lock,
229 .irq_bus_sync_unlock = adp5588_irq_bus_sync_unlock,
230 .irq_set_type = adp5588_irq_set_type,
Michael Hennerich459773a2010-10-27 15:33:19 -0700231};
232
233static int adp5588_gpio_read_intstat(struct i2c_client *client, u8 *buf)
234{
235 int ret = i2c_smbus_read_i2c_block_data(client, GPIO_INT_STAT1, 3, buf);
236
237 if (ret < 0)
238 dev_err(&client->dev, "Read INT_STAT Error\n");
239
240 return ret;
241}
242
243static irqreturn_t adp5588_irq_handler(int irq, void *devid)
244{
245 struct adp5588_gpio *dev = devid;
246 unsigned status, bank, bit, pending;
247 int ret;
248 status = adp5588_gpio_read(dev->client, INT_STAT);
249
250 if (status & ADP5588_GPI_INT) {
251 ret = adp5588_gpio_read_intstat(dev->client, dev->irq_stat);
252 if (ret < 0)
253 memset(dev->irq_stat, 0, ARRAY_SIZE(dev->irq_stat));
254
Axel Lin078dc652012-04-06 20:37:43 +0800255 for (bank = 0, bit = 0; bank <= ADP5588_BANK(ADP5588_MAXGPIO);
Michael Hennerich459773a2010-10-27 15:33:19 -0700256 bank++, bit = 0) {
257 pending = dev->irq_stat[bank] & dev->irq_mask[bank];
258
259 while (pending) {
260 if (pending & (1 << bit)) {
261 handle_nested_irq(dev->irq_base +
262 (bank << 3) + bit);
263 pending &= ~(1 << bit);
264
265 }
266 bit++;
267 }
268 }
269 }
270
271 adp5588_gpio_write(dev->client, INT_STAT, status); /* Status is W1C */
272
273 return IRQ_HANDLED;
274}
275
276static int adp5588_irq_setup(struct adp5588_gpio *dev)
277{
278 struct i2c_client *client = dev->client;
Jingoo Hane56aee12013-07-30 17:08:05 +0900279 struct adp5588_gpio_platform_data *pdata =
280 dev_get_platdata(&client->dev);
Michael Hennerich459773a2010-10-27 15:33:19 -0700281 unsigned gpio;
282 int ret;
283
284 adp5588_gpio_write(client, CFG, ADP5588_AUTO_INC);
285 adp5588_gpio_write(client, INT_STAT, -1); /* status is W1C */
286 adp5588_gpio_read_intstat(client, dev->irq_stat); /* read to clear */
287
288 dev->irq_base = pdata->irq_base;
289 mutex_init(&dev->irq_lock);
290
291 for (gpio = 0; gpio < dev->gpio_chip.ngpio; gpio++) {
292 int irq = gpio + dev->irq_base;
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000293 irq_set_chip_data(irq, dev);
294 irq_set_chip_and_handler(irq, &adp5588_irq_chip,
Michael Hennerich459773a2010-10-27 15:33:19 -0700295 handle_level_irq);
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000296 irq_set_nested_thread(irq, 1);
Michael Hennerich459773a2010-10-27 15:33:19 -0700297#ifdef CONFIG_ARM
298 /*
299 * ARM needs us to explicitly flag the IRQ as VALID,
300 * once we do so, it will also set the noprobe.
301 */
302 set_irq_flags(irq, IRQF_VALID);
303#else
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000304 irq_set_noprobe(irq);
Michael Hennerich459773a2010-10-27 15:33:19 -0700305#endif
306 }
307
308 ret = request_threaded_irq(client->irq,
309 NULL,
310 adp5588_irq_handler,
311 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
312 dev_name(&client->dev), dev);
313 if (ret) {
314 dev_err(&client->dev, "failed to request irq %d\n",
315 client->irq);
316 goto out;
317 }
318
319 dev->gpio_chip.to_irq = adp5588_gpio_to_irq;
320 adp5588_gpio_write(client, CFG,
321 ADP5588_AUTO_INC | ADP5588_INT_CFG | ADP5588_GPI_INT);
322
323 return 0;
324
325out:
326 dev->irq_base = 0;
327 return ret;
328}
329
330static void adp5588_irq_teardown(struct adp5588_gpio *dev)
331{
332 if (dev->irq_base)
333 free_irq(dev->client->irq, dev);
334}
335
336#else
337static int adp5588_irq_setup(struct adp5588_gpio *dev)
338{
339 struct i2c_client *client = dev->client;
340 dev_warn(&client->dev, "interrupt support not compiled in\n");
341
342 return 0;
343}
344
345static void adp5588_irq_teardown(struct adp5588_gpio *dev)
346{
347}
348#endif /* CONFIG_GPIO_ADP5588_IRQ */
349
Bill Pemberton38363092012-11-19 13:22:34 -0500350static int adp5588_gpio_probe(struct i2c_client *client,
Michael Hennerich80884092010-01-08 14:43:08 -0800351 const struct i2c_device_id *id)
352{
Jingoo Hane56aee12013-07-30 17:08:05 +0900353 struct adp5588_gpio_platform_data *pdata =
354 dev_get_platdata(&client->dev);
Michael Hennerich80884092010-01-08 14:43:08 -0800355 struct adp5588_gpio *dev;
356 struct gpio_chip *gc;
357 int ret, i, revid;
358
359 if (pdata == NULL) {
360 dev_err(&client->dev, "missing platform data\n");
361 return -ENODEV;
362 }
363
364 if (!i2c_check_functionality(client->adapter,
365 I2C_FUNC_SMBUS_BYTE_DATA)) {
366 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
367 return -EIO;
368 }
369
370 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
371 if (dev == NULL) {
372 dev_err(&client->dev, "failed to alloc memory\n");
373 return -ENOMEM;
374 }
375
376 dev->client = client;
377
378 gc = &dev->gpio_chip;
379 gc->direction_input = adp5588_gpio_direction_input;
380 gc->direction_output = adp5588_gpio_direction_output;
381 gc->get = adp5588_gpio_get_value;
382 gc->set = adp5588_gpio_set_value;
383 gc->can_sleep = 1;
384
385 gc->base = pdata->gpio_start;
Michael Hennerich459773a2010-10-27 15:33:19 -0700386 gc->ngpio = ADP5588_MAXGPIO;
Michael Hennerich80884092010-01-08 14:43:08 -0800387 gc->label = client->name;
388 gc->owner = THIS_MODULE;
389
390 mutex_init(&dev->lock);
391
Michael Hennerich80884092010-01-08 14:43:08 -0800392 ret = adp5588_gpio_read(dev->client, DEV_ID);
393 if (ret < 0)
394 goto err;
395
396 revid = ret & ADP5588_DEVICE_ID_MASK;
397
Michael Hennerich459773a2010-10-27 15:33:19 -0700398 for (i = 0, ret = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
Michael Hennerich80884092010-01-08 14:43:08 -0800399 dev->dat_out[i] = adp5588_gpio_read(client, GPIO_DAT_OUT1 + i);
400 dev->dir[i] = adp5588_gpio_read(client, GPIO_DIR1 + i);
401 ret |= adp5588_gpio_write(client, KP_GPIO1 + i, 0);
402 ret |= adp5588_gpio_write(client, GPIO_PULL1 + i,
403 (pdata->pullup_dis_mask >> (8 * i)) & 0xFF);
Michael Hennerich459773a2010-10-27 15:33:19 -0700404 ret |= adp5588_gpio_write(client, GPIO_INT_EN1 + i, 0);
Michael Hennerich80884092010-01-08 14:43:08 -0800405 if (ret)
406 goto err;
407 }
408
Michael Hennerich459773a2010-10-27 15:33:19 -0700409 if (pdata->irq_base) {
410 if (WA_DELAYED_READOUT_REVID(revid)) {
411 dev_warn(&client->dev, "GPIO int not supported\n");
412 } else {
413 ret = adp5588_irq_setup(dev);
414 if (ret)
415 goto err;
416 }
417 }
418
Michael Hennerich80884092010-01-08 14:43:08 -0800419 ret = gpiochip_add(&dev->gpio_chip);
420 if (ret)
Michael Hennerich459773a2010-10-27 15:33:19 -0700421 goto err_irq;
Michael Hennerich80884092010-01-08 14:43:08 -0800422
Grant Likely64842aa2011-11-06 11:36:18 -0700423 dev_info(&client->dev, "IRQ Base: %d Rev.: %d\n",
424 pdata->irq_base, revid);
Michael Hennerich80884092010-01-08 14:43:08 -0800425
426 if (pdata->setup) {
427 ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context);
428 if (ret < 0)
429 dev_warn(&client->dev, "setup failed, %d\n", ret);
430 }
431
432 i2c_set_clientdata(client, dev);
Michael Hennerich459773a2010-10-27 15:33:19 -0700433
Michael Hennerich80884092010-01-08 14:43:08 -0800434 return 0;
435
Michael Hennerich459773a2010-10-27 15:33:19 -0700436err_irq:
437 adp5588_irq_teardown(dev);
Michael Hennerich80884092010-01-08 14:43:08 -0800438err:
439 kfree(dev);
440 return ret;
441}
442
Bill Pemberton206210c2012-11-19 13:25:50 -0500443static int adp5588_gpio_remove(struct i2c_client *client)
Michael Hennerich80884092010-01-08 14:43:08 -0800444{
Jingoo Hane56aee12013-07-30 17:08:05 +0900445 struct adp5588_gpio_platform_data *pdata =
446 dev_get_platdata(&client->dev);
Michael Hennerich80884092010-01-08 14:43:08 -0800447 struct adp5588_gpio *dev = i2c_get_clientdata(client);
448 int ret;
449
450 if (pdata->teardown) {
451 ret = pdata->teardown(client,
452 dev->gpio_chip.base, dev->gpio_chip.ngpio,
453 pdata->context);
454 if (ret < 0) {
455 dev_err(&client->dev, "teardown failed %d\n", ret);
456 return ret;
457 }
458 }
459
Michael Hennerich459773a2010-10-27 15:33:19 -0700460 if (dev->irq_base)
461 free_irq(dev->client->irq, dev);
462
Michael Hennerich80884092010-01-08 14:43:08 -0800463 ret = gpiochip_remove(&dev->gpio_chip);
464 if (ret) {
465 dev_err(&client->dev, "gpiochip_remove failed %d\n", ret);
466 return ret;
467 }
468
469 kfree(dev);
470 return 0;
471}
472
473static const struct i2c_device_id adp5588_gpio_id[] = {
474 {DRV_NAME, 0},
475 {}
476};
477
478MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id);
479
480static struct i2c_driver adp5588_gpio_driver = {
481 .driver = {
482 .name = DRV_NAME,
483 },
484 .probe = adp5588_gpio_probe,
Bill Pemberton8283c4f2012-11-19 13:20:08 -0500485 .remove = adp5588_gpio_remove,
Michael Hennerich80884092010-01-08 14:43:08 -0800486 .id_table = adp5588_gpio_id,
487};
488
Axel Linc8554d32012-09-02 08:08:01 +0800489module_i2c_driver(adp5588_gpio_driver);
Michael Hennerich80884092010-01-08 14:43:08 -0800490
491MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
492MODULE_DESCRIPTION("GPIO ADP5588 Driver");
493MODULE_LICENSE("GPL");