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.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
19 #include <linux/i2c/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
=
69 container_of(chip
, struct adp5588_gpio
, gpio_chip
);
71 return !!(adp5588_gpio_read(dev
->client
,
72 GPIO_DAT_STAT1
+ ADP5588_BANK(off
)) & ADP5588_BIT(off
));
75 static void adp5588_gpio_set_value(struct gpio_chip
*chip
,
76 unsigned off
, int val
)
79 struct adp5588_gpio
*dev
=
80 container_of(chip
, struct adp5588_gpio
, gpio_chip
);
82 bank
= ADP5588_BANK(off
);
83 bit
= ADP5588_BIT(off
);
85 mutex_lock(&dev
->lock
);
87 dev
->dat_out
[bank
] |= bit
;
89 dev
->dat_out
[bank
] &= ~bit
;
91 adp5588_gpio_write(dev
->client
, GPIO_DAT_OUT1
+ bank
,
93 mutex_unlock(&dev
->lock
);
96 static int adp5588_gpio_direction_input(struct gpio_chip
*chip
, unsigned off
)
100 struct adp5588_gpio
*dev
=
101 container_of(chip
, struct adp5588_gpio
, gpio_chip
);
103 bank
= ADP5588_BANK(off
);
105 mutex_lock(&dev
->lock
);
106 dev
->dir
[bank
] &= ~ADP5588_BIT(off
);
107 ret
= adp5588_gpio_write(dev
->client
, GPIO_DIR1
+ bank
, dev
->dir
[bank
]);
108 mutex_unlock(&dev
->lock
);
113 static int adp5588_gpio_direction_output(struct gpio_chip
*chip
,
114 unsigned off
, int val
)
118 struct adp5588_gpio
*dev
=
119 container_of(chip
, struct adp5588_gpio
, gpio_chip
);
121 bank
= ADP5588_BANK(off
);
122 bit
= ADP5588_BIT(off
);
124 mutex_lock(&dev
->lock
);
125 dev
->dir
[bank
] |= bit
;
128 dev
->dat_out
[bank
] |= bit
;
130 dev
->dat_out
[bank
] &= ~bit
;
132 ret
= adp5588_gpio_write(dev
->client
, GPIO_DAT_OUT1
+ bank
,
134 ret
|= adp5588_gpio_write(dev
->client
, GPIO_DIR1
+ bank
,
136 mutex_unlock(&dev
->lock
);
141 #ifdef CONFIG_GPIO_ADP5588_IRQ
142 static int adp5588_gpio_to_irq(struct gpio_chip
*chip
, unsigned off
)
144 struct adp5588_gpio
*dev
=
145 container_of(chip
, struct adp5588_gpio
, gpio_chip
);
146 return dev
->irq_base
+ off
;
149 static void adp5588_irq_bus_lock(unsigned int irq
)
151 struct adp5588_gpio
*dev
= get_irq_chip_data(irq
);
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(unsigned int irq
)
165 struct adp5588_gpio
*dev
= get_irq_chip_data(irq
);
168 for (i
= 0; i
<= ADP5588_BANK(ADP5588_MAXGPIO
); i
++)
169 if (dev
->int_en
[i
] ^ dev
->irq_mask
[i
]) {
170 dev
->int_en
[i
] = dev
->irq_mask
[i
];
171 adp5588_gpio_write(dev
->client
, GPIO_INT_EN1
+ i
,
175 mutex_unlock(&dev
->irq_lock
);
178 static void adp5588_irq_mask(unsigned int irq
)
180 struct adp5588_gpio
*dev
= get_irq_chip_data(irq
);
181 unsigned gpio
= irq
- dev
->irq_base
;
183 dev
->irq_mask
[ADP5588_BANK(gpio
)] &= ~ADP5588_BIT(gpio
);
186 static void adp5588_irq_unmask(unsigned int irq
)
188 struct adp5588_gpio
*dev
= get_irq_chip_data(irq
);
189 unsigned gpio
= irq
- dev
->irq_base
;
191 dev
->irq_mask
[ADP5588_BANK(gpio
)] |= ADP5588_BIT(gpio
);
194 static int adp5588_irq_set_type(unsigned int irq
, unsigned int type
)
196 struct adp5588_gpio
*dev
= get_irq_chip_data(irq
);
197 uint16_t gpio
= irq
- dev
->irq_base
;
200 if ((type
& IRQ_TYPE_EDGE_BOTH
)) {
201 dev_err(&dev
->client
->dev
, "irq %d: unsupported type %d\n",
206 bank
= ADP5588_BANK(gpio
);
207 bit
= ADP5588_BIT(gpio
);
209 if (type
& IRQ_TYPE_LEVEL_HIGH
)
210 dev
->int_lvl
[bank
] |= bit
;
211 else if (type
& IRQ_TYPE_LEVEL_LOW
)
212 dev
->int_lvl
[bank
] &= ~bit
;
216 adp5588_gpio_direction_input(&dev
->gpio_chip
, gpio
);
217 adp5588_gpio_write(dev
->client
, GPIO_INT_LVL1
+ bank
,
223 static struct irq_chip adp5588_irq_chip
= {
225 .mask
= adp5588_irq_mask
,
226 .unmask
= adp5588_irq_unmask
,
227 .bus_lock
= adp5588_irq_bus_lock
,
228 .bus_sync_unlock
= adp5588_irq_bus_sync_unlock
,
229 .set_type
= adp5588_irq_set_type
,
232 static int adp5588_gpio_read_intstat(struct i2c_client
*client
, u8
*buf
)
234 int ret
= i2c_smbus_read_i2c_block_data(client
, GPIO_INT_STAT1
, 3, buf
);
237 dev_err(&client
->dev
, "Read INT_STAT Error\n");
242 static irqreturn_t
adp5588_irq_handler(int irq
, void *devid
)
244 struct adp5588_gpio
*dev
= devid
;
245 unsigned status
, bank
, bit
, pending
;
247 status
= adp5588_gpio_read(dev
->client
, INT_STAT
);
249 if (status
& ADP5588_GPI_INT
) {
250 ret
= adp5588_gpio_read_intstat(dev
->client
, dev
->irq_stat
);
252 memset(dev
->irq_stat
, 0, ARRAY_SIZE(dev
->irq_stat
));
254 for (bank
= 0; bank
<= ADP5588_BANK(ADP5588_MAXGPIO
);
256 pending
= dev
->irq_stat
[bank
] & dev
->irq_mask
[bank
];
259 if (pending
& (1 << bit
)) {
260 handle_nested_irq(dev
->irq_base
+
262 pending
&= ~(1 << bit
);
270 adp5588_gpio_write(dev
->client
, INT_STAT
, status
); /* Status is W1C */
275 static int adp5588_irq_setup(struct adp5588_gpio
*dev
)
277 struct i2c_client
*client
= dev
->client
;
278 struct adp5588_gpio_platform_data
*pdata
= client
->dev
.platform_data
;
282 adp5588_gpio_write(client
, CFG
, ADP5588_AUTO_INC
);
283 adp5588_gpio_write(client
, INT_STAT
, -1); /* status is W1C */
284 adp5588_gpio_read_intstat(client
, dev
->irq_stat
); /* read to clear */
286 dev
->irq_base
= pdata
->irq_base
;
287 mutex_init(&dev
->irq_lock
);
289 for (gpio
= 0; gpio
< dev
->gpio_chip
.ngpio
; gpio
++) {
290 int irq
= gpio
+ dev
->irq_base
;
291 set_irq_chip_data(irq
, dev
);
292 set_irq_chip_and_handler(irq
, &adp5588_irq_chip
,
294 set_irq_nested_thread(irq
, 1);
297 * ARM needs us to explicitly flag the IRQ as VALID,
298 * once we do so, it will also set the noprobe.
300 set_irq_flags(irq
, IRQF_VALID
);
302 set_irq_noprobe(irq
);
306 ret
= request_threaded_irq(client
->irq
,
309 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
310 dev_name(&client
->dev
), dev
);
312 dev_err(&client
->dev
, "failed to request irq %d\n",
317 dev
->gpio_chip
.to_irq
= adp5588_gpio_to_irq
;
318 adp5588_gpio_write(client
, CFG
,
319 ADP5588_AUTO_INC
| ADP5588_INT_CFG
| ADP5588_GPI_INT
);
328 static void adp5588_irq_teardown(struct adp5588_gpio
*dev
)
331 free_irq(dev
->client
->irq
, dev
);
335 static int adp5588_irq_setup(struct adp5588_gpio
*dev
)
337 struct i2c_client
*client
= dev
->client
;
338 dev_warn(&client
->dev
, "interrupt support not compiled in\n");
343 static void adp5588_irq_teardown(struct adp5588_gpio
*dev
)
346 #endif /* CONFIG_GPIO_ADP5588_IRQ */
348 static int __devinit
adp5588_gpio_probe(struct i2c_client
*client
,
349 const struct i2c_device_id
*id
)
351 struct adp5588_gpio_platform_data
*pdata
= client
->dev
.platform_data
;
352 struct adp5588_gpio
*dev
;
353 struct gpio_chip
*gc
;
357 dev_err(&client
->dev
, "missing platform data\n");
361 if (!i2c_check_functionality(client
->adapter
,
362 I2C_FUNC_SMBUS_BYTE_DATA
)) {
363 dev_err(&client
->dev
, "SMBUS Byte Data not Supported\n");
367 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
369 dev_err(&client
->dev
, "failed to alloc memory\n");
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
;
382 gc
->base
= pdata
->gpio_start
;
383 gc
->ngpio
= ADP5588_MAXGPIO
;
384 gc
->label
= client
->name
;
385 gc
->owner
= THIS_MODULE
;
387 mutex_init(&dev
->lock
);
389 ret
= adp5588_gpio_read(dev
->client
, DEV_ID
);
393 revid
= ret
& ADP5588_DEVICE_ID_MASK
;
395 for (i
= 0, ret
= 0; i
<= ADP5588_BANK(ADP5588_MAXGPIO
); i
++) {
396 dev
->dat_out
[i
] = adp5588_gpio_read(client
, GPIO_DAT_OUT1
+ i
);
397 dev
->dir
[i
] = adp5588_gpio_read(client
, GPIO_DIR1
+ i
);
398 ret
|= adp5588_gpio_write(client
, KP_GPIO1
+ i
, 0);
399 ret
|= adp5588_gpio_write(client
, GPIO_PULL1
+ i
,
400 (pdata
->pullup_dis_mask
>> (8 * i
)) & 0xFF);
401 ret
|= adp5588_gpio_write(client
, GPIO_INT_EN1
+ i
, 0);
406 if (pdata
->irq_base
) {
407 if (WA_DELAYED_READOUT_REVID(revid
)) {
408 dev_warn(&client
->dev
, "GPIO int not supported\n");
410 ret
= adp5588_irq_setup(dev
);
416 ret
= gpiochip_add(&dev
->gpio_chip
);
420 dev_info(&client
->dev
, "gpios %d..%d (IRQ Base %d) on a %s Rev. %d\n",
421 gc
->base
, gc
->base
+ gc
->ngpio
- 1,
422 pdata
->irq_base
, client
->name
, 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
);
441 static int __devexit
adp5588_gpio_remove(struct i2c_client
*client
)
443 struct adp5588_gpio_platform_data
*pdata
= client
->dev
.platform_data
;
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
);
460 ret
= gpiochip_remove(&dev
->gpio_chip
);
462 dev_err(&client
->dev
, "gpiochip_remove failed %d\n", ret
);
470 static const struct i2c_device_id adp5588_gpio_id
[] = {
475 MODULE_DEVICE_TABLE(i2c
, adp5588_gpio_id
);
477 static struct i2c_driver adp5588_gpio_driver
= {
481 .probe
= adp5588_gpio_probe
,
482 .remove
= __devexit_p(adp5588_gpio_remove
),
483 .id_table
= adp5588_gpio_id
,
486 static int __init
adp5588_gpio_init(void)
488 return i2c_add_driver(&adp5588_gpio_driver
);
491 module_init(adp5588_gpio_init
);
493 static void __exit
adp5588_gpio_exit(void)
495 i2c_del_driver(&adp5588_gpio_driver
);
498 module_exit(adp5588_gpio_exit
);
500 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
501 MODULE_DESCRIPTION("GPIO ADP5588 Driver");
502 MODULE_LICENSE("GPL");