blob: c0f718b12317271d90c44ab73438f993c36a343e [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{
Linus Walleijf69255c2015-12-04 15:05:34 +010068 struct adp5588_gpio *dev = gpiochip_get_data(chip);
Jean-Francois Dagenais992196f2014-02-10 12:05:28 -050069 unsigned bank = ADP5588_BANK(off);
70 unsigned bit = ADP5588_BIT(off);
71 int val;
Michael Hennerich80884092010-01-08 14:43:08 -080072
Jean-Francois Dagenais992196f2014-02-10 12:05:28 -050073 mutex_lock(&dev->lock);
74
75 if (dev->dir[bank] & bit)
76 val = dev->dat_out[bank];
77 else
78 val = adp5588_gpio_read(dev->client, GPIO_DAT_STAT1 + bank);
79
80 mutex_unlock(&dev->lock);
81
82 return !!(val & bit);
Michael Hennerich80884092010-01-08 14:43:08 -080083}
84
85static void adp5588_gpio_set_value(struct gpio_chip *chip,
86 unsigned off, int val)
87{
88 unsigned bank, bit;
Linus Walleijf69255c2015-12-04 15:05:34 +010089 struct adp5588_gpio *dev = gpiochip_get_data(chip);
Michael Hennerich80884092010-01-08 14:43:08 -080090
Michael Hennerich459773a2010-10-27 15:33:19 -070091 bank = ADP5588_BANK(off);
92 bit = ADP5588_BIT(off);
Michael Hennerich80884092010-01-08 14:43:08 -080093
94 mutex_lock(&dev->lock);
95 if (val)
96 dev->dat_out[bank] |= bit;
97 else
98 dev->dat_out[bank] &= ~bit;
99
100 adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
101 dev->dat_out[bank]);
102 mutex_unlock(&dev->lock);
103}
104
105static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
106{
107 int ret;
108 unsigned bank;
Linus Walleijf69255c2015-12-04 15:05:34 +0100109 struct adp5588_gpio *dev = gpiochip_get_data(chip);
Michael Hennerich80884092010-01-08 14:43:08 -0800110
Michael Hennerich459773a2010-10-27 15:33:19 -0700111 bank = ADP5588_BANK(off);
Michael Hennerich80884092010-01-08 14:43:08 -0800112
113 mutex_lock(&dev->lock);
Michael Hennerich459773a2010-10-27 15:33:19 -0700114 dev->dir[bank] &= ~ADP5588_BIT(off);
Michael Hennerich80884092010-01-08 14:43:08 -0800115 ret = adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, dev->dir[bank]);
116 mutex_unlock(&dev->lock);
117
118 return ret;
119}
120
121static int adp5588_gpio_direction_output(struct gpio_chip *chip,
122 unsigned off, int val)
123{
124 int ret;
125 unsigned bank, bit;
Linus Walleijf69255c2015-12-04 15:05:34 +0100126 struct adp5588_gpio *dev = gpiochip_get_data(chip);
Michael Hennerich80884092010-01-08 14:43:08 -0800127
Michael Hennerich459773a2010-10-27 15:33:19 -0700128 bank = ADP5588_BANK(off);
129 bit = ADP5588_BIT(off);
Michael Hennerich80884092010-01-08 14:43:08 -0800130
131 mutex_lock(&dev->lock);
132 dev->dir[bank] |= bit;
133
134 if (val)
135 dev->dat_out[bank] |= bit;
136 else
137 dev->dat_out[bank] &= ~bit;
138
139 ret = adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
140 dev->dat_out[bank]);
141 ret |= adp5588_gpio_write(dev->client, GPIO_DIR1 + bank,
142 dev->dir[bank]);
143 mutex_unlock(&dev->lock);
144
145 return ret;
146}
147
Michael Hennerich459773a2010-10-27 15:33:19 -0700148#ifdef CONFIG_GPIO_ADP5588_IRQ
149static int adp5588_gpio_to_irq(struct gpio_chip *chip, unsigned off)
150{
Linus Walleijf69255c2015-12-04 15:05:34 +0100151 struct adp5588_gpio *dev = gpiochip_get_data(chip);
152
Michael Hennerich459773a2010-10-27 15:33:19 -0700153 return dev->irq_base + off;
154}
155
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800156static void adp5588_irq_bus_lock(struct irq_data *d)
Michael Hennerich459773a2010-10-27 15:33:19 -0700157{
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800158 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
159
Michael Hennerich459773a2010-10-27 15:33:19 -0700160 mutex_lock(&dev->irq_lock);
161}
162
163 /*
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.
169 */
170
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800171static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
Michael Hennerich459773a2010-10-27 15:33:19 -0700172{
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800173 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
Michael Hennerich459773a2010-10-27 15:33:19 -0700174 int i;
175
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,
180 dev->int_en[i]);
181 }
182
183 mutex_unlock(&dev->irq_lock);
184}
185
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800186static void adp5588_irq_mask(struct irq_data *d)
Michael Hennerich459773a2010-10-27 15:33:19 -0700187{
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800188 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
189 unsigned gpio = d->irq - dev->irq_base;
Michael Hennerich459773a2010-10-27 15:33:19 -0700190
191 dev->irq_mask[ADP5588_BANK(gpio)] &= ~ADP5588_BIT(gpio);
192}
193
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800194static void adp5588_irq_unmask(struct irq_data *d)
Michael Hennerich459773a2010-10-27 15:33:19 -0700195{
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800196 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
197 unsigned gpio = d->irq - dev->irq_base;
Michael Hennerich459773a2010-10-27 15:33:19 -0700198
199 dev->irq_mask[ADP5588_BANK(gpio)] |= ADP5588_BIT(gpio);
200}
201
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800202static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
Michael Hennerich459773a2010-10-27 15:33:19 -0700203{
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800204 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
205 uint16_t gpio = d->irq - dev->irq_base;
Michael Hennerich459773a2010-10-27 15:33:19 -0700206 unsigned bank, bit;
207
208 if ((type & IRQ_TYPE_EDGE_BOTH)) {
209 dev_err(&dev->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800210 d->irq, type);
Michael Hennerich459773a2010-10-27 15:33:19 -0700211 return -EINVAL;
212 }
213
214 bank = ADP5588_BANK(gpio);
215 bit = ADP5588_BIT(gpio);
216
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;
221 else
222 return -EINVAL;
223
224 adp5588_gpio_direction_input(&dev->gpio_chip, gpio);
225 adp5588_gpio_write(dev->client, GPIO_INT_LVL1 + bank,
226 dev->int_lvl[bank]);
227
228 return 0;
229}
230
231static struct irq_chip adp5588_irq_chip = {
232 .name = "adp5588",
Lennert Buytenhek12401eed2011-01-12 17:00:12 -0800233 .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 Hennerich459773a2010-10-27 15:33:19 -0700238};
239
240static int adp5588_gpio_read_intstat(struct i2c_client *client, u8 *buf)
241{
242 int ret = i2c_smbus_read_i2c_block_data(client, GPIO_INT_STAT1, 3, buf);
243
244 if (ret < 0)
245 dev_err(&client->dev, "Read INT_STAT Error\n");
246
247 return ret;
248}
249
250static irqreturn_t adp5588_irq_handler(int irq, void *devid)
251{
252 struct adp5588_gpio *dev = devid;
253 unsigned status, bank, bit, pending;
254 int ret;
255 status = adp5588_gpio_read(dev->client, INT_STAT);
256
257 if (status & ADP5588_GPI_INT) {
258 ret = adp5588_gpio_read_intstat(dev->client, dev->irq_stat);
259 if (ret < 0)
260 memset(dev->irq_stat, 0, ARRAY_SIZE(dev->irq_stat));
261
Axel Lin078dc652012-04-06 20:37:43 +0800262 for (bank = 0, bit = 0; bank <= ADP5588_BANK(ADP5588_MAXGPIO);
Michael Hennerich459773a2010-10-27 15:33:19 -0700263 bank++, bit = 0) {
264 pending = dev->irq_stat[bank] & dev->irq_mask[bank];
265
266 while (pending) {
267 if (pending & (1 << bit)) {
268 handle_nested_irq(dev->irq_base +
269 (bank << 3) + bit);
270 pending &= ~(1 << bit);
271
272 }
273 bit++;
274 }
275 }
276 }
277
278 adp5588_gpio_write(dev->client, INT_STAT, status); /* Status is W1C */
279
280 return IRQ_HANDLED;
281}
282
283static int adp5588_irq_setup(struct adp5588_gpio *dev)
284{
285 struct i2c_client *client = dev->client;
Jingoo Hane56aee12013-07-30 17:08:05 +0900286 struct adp5588_gpio_platform_data *pdata =
287 dev_get_platdata(&client->dev);
Michael Hennerich459773a2010-10-27 15:33:19 -0700288 unsigned gpio;
289 int ret;
290
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 */
294
295 dev->irq_base = pdata->irq_base;
296 mutex_init(&dev->irq_lock);
297
298 for (gpio = 0; gpio < dev->gpio_chip.ngpio; gpio++) {
299 int irq = gpio + dev->irq_base;
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000300 irq_set_chip_data(irq, dev);
301 irq_set_chip_and_handler(irq, &adp5588_irq_chip,
Michael Hennerich459773a2010-10-27 15:33:19 -0700302 handle_level_irq);
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000303 irq_set_nested_thread(irq, 1);
Rob Herring23393d42015-07-27 15:55:16 -0500304 irq_modify_status(irq, IRQ_NOREQUEST, IRQ_NOPROBE);
Michael Hennerich459773a2010-10-27 15:33:19 -0700305 }
306
307 ret = request_threaded_irq(client->irq,
308 NULL,
309 adp5588_irq_handler,
310 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
311 dev_name(&client->dev), dev);
312 if (ret) {
313 dev_err(&client->dev, "failed to request irq %d\n",
314 client->irq);
315 goto out;
316 }
317
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);
321
322 return 0;
323
324out:
325 dev->irq_base = 0;
326 return ret;
327}
328
329static void adp5588_irq_teardown(struct adp5588_gpio *dev)
330{
331 if (dev->irq_base)
332 free_irq(dev->client->irq, dev);
333}
334
335#else
336static int adp5588_irq_setup(struct adp5588_gpio *dev)
337{
338 struct i2c_client *client = dev->client;
339 dev_warn(&client->dev, "interrupt support not compiled in\n");
340
341 return 0;
342}
343
344static void adp5588_irq_teardown(struct adp5588_gpio *dev)
345{
346}
347#endif /* CONFIG_GPIO_ADP5588_IRQ */
348
Bill Pemberton38363092012-11-19 13:22:34 -0500349static int adp5588_gpio_probe(struct i2c_client *client,
Michael Hennerich80884092010-01-08 14:43:08 -0800350 const struct i2c_device_id *id)
351{
Jingoo Hane56aee12013-07-30 17:08:05 +0900352 struct adp5588_gpio_platform_data *pdata =
353 dev_get_platdata(&client->dev);
Michael Hennerich80884092010-01-08 14:43:08 -0800354 struct adp5588_gpio *dev;
355 struct gpio_chip *gc;
356 int ret, i, revid;
357
Varka Bhadramafeb7b42015-03-31 09:49:11 +0530358 if (!pdata) {
Michael Hennerich80884092010-01-08 14:43:08 -0800359 dev_err(&client->dev, "missing platform data\n");
360 return -ENODEV;
361 }
362
363 if (!i2c_check_functionality(client->adapter,
364 I2C_FUNC_SMBUS_BYTE_DATA)) {
365 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
366 return -EIO;
367 }
368
Varka Bhadram7898b312015-03-31 09:49:08 +0530369 dev = devm_kzalloc(&client->dev, sizeof(*dev), GFP_KERNEL);
Varka Bhadramafeb7b42015-03-31 09:49:11 +0530370 if (!dev)
Michael Hennerich80884092010-01-08 14:43:08 -0800371 return -ENOMEM;
Michael Hennerich80884092010-01-08 14:43:08 -0800372
373 dev->client = client;
374
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;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100380 gc->can_sleep = true;
Michael Hennerich80884092010-01-08 14:43:08 -0800381
382 gc->base = pdata->gpio_start;
Michael Hennerich459773a2010-10-27 15:33:19 -0700383 gc->ngpio = ADP5588_MAXGPIO;
Michael Hennerich80884092010-01-08 14:43:08 -0800384 gc->label = client->name;
385 gc->owner = THIS_MODULE;
Jean-Francois Dagenais11633162014-02-10 12:24:05 -0500386 gc->names = pdata->names;
Michael Hennerich80884092010-01-08 14:43:08 -0800387
388 mutex_init(&dev->lock);
389
Michael Hennerich80884092010-01-08 14:43:08 -0800390 ret = adp5588_gpio_read(dev->client, DEV_ID);
391 if (ret < 0)
392 goto err;
393
394 revid = ret & ADP5588_DEVICE_ID_MASK;
395
Michael Hennerich459773a2010-10-27 15:33:19 -0700396 for (i = 0, ret = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
Michael Hennerich80884092010-01-08 14:43:08 -0800397 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);
Michael Hennerich459773a2010-10-27 15:33:19 -0700402 ret |= adp5588_gpio_write(client, GPIO_INT_EN1 + i, 0);
Michael Hennerich80884092010-01-08 14:43:08 -0800403 if (ret)
404 goto err;
405 }
406
Michael Hennerich459773a2010-10-27 15:33:19 -0700407 if (pdata->irq_base) {
408 if (WA_DELAYED_READOUT_REVID(revid)) {
409 dev_warn(&client->dev, "GPIO int not supported\n");
410 } else {
411 ret = adp5588_irq_setup(dev);
412 if (ret)
413 goto err;
414 }
415 }
416
Laxman Dewangan7c263fe2016-02-22 17:43:28 +0530417 ret = devm_gpiochip_add_data(&client->dev, &dev->gpio_chip, dev);
Michael Hennerich80884092010-01-08 14:43:08 -0800418 if (ret)
Michael Hennerich459773a2010-10-27 15:33:19 -0700419 goto err_irq;
Michael Hennerich80884092010-01-08 14:43:08 -0800420
Grant Likely64842aa2011-11-06 11:36:18 -0700421 dev_info(&client->dev, "IRQ Base: %d Rev.: %d\n",
422 pdata->irq_base, revid);
Michael Hennerich80884092010-01-08 14:43:08 -0800423
424 if (pdata->setup) {
425 ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context);
426 if (ret < 0)
427 dev_warn(&client->dev, "setup failed, %d\n", ret);
428 }
429
430 i2c_set_clientdata(client, dev);
Michael Hennerich459773a2010-10-27 15:33:19 -0700431
Michael Hennerich80884092010-01-08 14:43:08 -0800432 return 0;
433
Michael Hennerich459773a2010-10-27 15:33:19 -0700434err_irq:
435 adp5588_irq_teardown(dev);
Michael Hennerich80884092010-01-08 14:43:08 -0800436err:
Michael Hennerich80884092010-01-08 14:43:08 -0800437 return ret;
438}
439
Bill Pemberton206210c2012-11-19 13:25:50 -0500440static int adp5588_gpio_remove(struct i2c_client *client)
Michael Hennerich80884092010-01-08 14:43:08 -0800441{
Jingoo Hane56aee12013-07-30 17:08:05 +0900442 struct adp5588_gpio_platform_data *pdata =
443 dev_get_platdata(&client->dev);
Michael Hennerich80884092010-01-08 14:43:08 -0800444 struct adp5588_gpio *dev = i2c_get_clientdata(client);
445 int ret;
446
447 if (pdata->teardown) {
448 ret = pdata->teardown(client,
449 dev->gpio_chip.base, dev->gpio_chip.ngpio,
450 pdata->context);
451 if (ret < 0) {
452 dev_err(&client->dev, "teardown failed %d\n", ret);
453 return ret;
454 }
455 }
456
Michael Hennerich459773a2010-10-27 15:33:19 -0700457 if (dev->irq_base)
458 free_irq(dev->client->irq, dev);
459
Michael Hennerich80884092010-01-08 14:43:08 -0800460 return 0;
461}
462
463static const struct i2c_device_id adp5588_gpio_id[] = {
464 {DRV_NAME, 0},
465 {}
466};
467
468MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id);
469
470static struct i2c_driver adp5588_gpio_driver = {
471 .driver = {
472 .name = DRV_NAME,
473 },
474 .probe = adp5588_gpio_probe,
Bill Pemberton8283c4f2012-11-19 13:20:08 -0500475 .remove = adp5588_gpio_remove,
Michael Hennerich80884092010-01-08 14:43:08 -0800476 .id_table = adp5588_gpio_id,
477};
478
Axel Linc8554d32012-09-02 08:08:01 +0800479module_i2c_driver(adp5588_gpio_driver);
Michael Hennerich80884092010-01-08 14:43:08 -0800480
481MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
482MODULE_DESCRIPTION("GPIO ADP5588 Driver");
483MODULE_LICENSE("GPL");