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
);
70 unsigned bank
= ADP5588_BANK(off
);
71 unsigned bit
= ADP5588_BIT(off
);
74 mutex_lock(&dev
->lock
);
76 if (dev
->dir
[bank
] & bit
)
77 val
= dev
->dat_out
[bank
];
79 val
= adp5588_gpio_read(dev
->client
, GPIO_DAT_STAT1
+ bank
);
81 mutex_unlock(&dev
->lock
);
86 static void adp5588_gpio_set_value(struct gpio_chip
*chip
,
87 unsigned off
, int val
)
90 struct adp5588_gpio
*dev
=
91 container_of(chip
, struct adp5588_gpio
, gpio_chip
);
93 bank
= ADP5588_BANK(off
);
94 bit
= ADP5588_BIT(off
);
96 mutex_lock(&dev
->lock
);
98 dev
->dat_out
[bank
] |= bit
;
100 dev
->dat_out
[bank
] &= ~bit
;
102 adp5588_gpio_write(dev
->client
, GPIO_DAT_OUT1
+ bank
,
104 mutex_unlock(&dev
->lock
);
107 static int adp5588_gpio_direction_input(struct gpio_chip
*chip
, unsigned off
)
111 struct adp5588_gpio
*dev
=
112 container_of(chip
, struct adp5588_gpio
, gpio_chip
);
114 bank
= ADP5588_BANK(off
);
116 mutex_lock(&dev
->lock
);
117 dev
->dir
[bank
] &= ~ADP5588_BIT(off
);
118 ret
= adp5588_gpio_write(dev
->client
, GPIO_DIR1
+ bank
, dev
->dir
[bank
]);
119 mutex_unlock(&dev
->lock
);
124 static int adp5588_gpio_direction_output(struct gpio_chip
*chip
,
125 unsigned off
, int val
)
129 struct adp5588_gpio
*dev
=
130 container_of(chip
, struct adp5588_gpio
, gpio_chip
);
132 bank
= ADP5588_BANK(off
);
133 bit
= ADP5588_BIT(off
);
135 mutex_lock(&dev
->lock
);
136 dev
->dir
[bank
] |= bit
;
139 dev
->dat_out
[bank
] |= bit
;
141 dev
->dat_out
[bank
] &= ~bit
;
143 ret
= adp5588_gpio_write(dev
->client
, GPIO_DAT_OUT1
+ bank
,
145 ret
|= adp5588_gpio_write(dev
->client
, GPIO_DIR1
+ bank
,
147 mutex_unlock(&dev
->lock
);
152 #ifdef CONFIG_GPIO_ADP5588_IRQ
153 static int adp5588_gpio_to_irq(struct gpio_chip
*chip
, unsigned off
)
155 struct adp5588_gpio
*dev
=
156 container_of(chip
, struct adp5588_gpio
, gpio_chip
);
157 return dev
->irq_base
+ off
;
160 static void adp5588_irq_bus_lock(struct irq_data
*d
)
162 struct adp5588_gpio
*dev
= irq_data_get_irq_chip_data(d
);
164 mutex_lock(&dev
->irq_lock
);
168 * genirq core code can issue chip->mask/unmask from atomic context.
169 * This doesn't work for slow busses where an access needs to sleep.
170 * bus_sync_unlock() is therefore called outside the atomic context,
171 * syncs the current irq mask state with the slow external controller
172 * and unlocks the bus.
175 static void adp5588_irq_bus_sync_unlock(struct irq_data
*d
)
177 struct adp5588_gpio
*dev
= irq_data_get_irq_chip_data(d
);
180 for (i
= 0; i
<= ADP5588_BANK(ADP5588_MAXGPIO
); i
++)
181 if (dev
->int_en
[i
] ^ dev
->irq_mask
[i
]) {
182 dev
->int_en
[i
] = dev
->irq_mask
[i
];
183 adp5588_gpio_write(dev
->client
, GPIO_INT_EN1
+ i
,
187 mutex_unlock(&dev
->irq_lock
);
190 static void adp5588_irq_mask(struct irq_data
*d
)
192 struct adp5588_gpio
*dev
= irq_data_get_irq_chip_data(d
);
193 unsigned gpio
= d
->irq
- dev
->irq_base
;
195 dev
->irq_mask
[ADP5588_BANK(gpio
)] &= ~ADP5588_BIT(gpio
);
198 static void adp5588_irq_unmask(struct irq_data
*d
)
200 struct adp5588_gpio
*dev
= irq_data_get_irq_chip_data(d
);
201 unsigned gpio
= d
->irq
- dev
->irq_base
;
203 dev
->irq_mask
[ADP5588_BANK(gpio
)] |= ADP5588_BIT(gpio
);
206 static int adp5588_irq_set_type(struct irq_data
*d
, unsigned int type
)
208 struct adp5588_gpio
*dev
= irq_data_get_irq_chip_data(d
);
209 uint16_t gpio
= d
->irq
- dev
->irq_base
;
212 if ((type
& IRQ_TYPE_EDGE_BOTH
)) {
213 dev_err(&dev
->client
->dev
, "irq %d: unsupported type %d\n",
218 bank
= ADP5588_BANK(gpio
);
219 bit
= ADP5588_BIT(gpio
);
221 if (type
& IRQ_TYPE_LEVEL_HIGH
)
222 dev
->int_lvl
[bank
] |= bit
;
223 else if (type
& IRQ_TYPE_LEVEL_LOW
)
224 dev
->int_lvl
[bank
] &= ~bit
;
228 adp5588_gpio_direction_input(&dev
->gpio_chip
, gpio
);
229 adp5588_gpio_write(dev
->client
, GPIO_INT_LVL1
+ bank
,
235 static struct irq_chip adp5588_irq_chip
= {
237 .irq_mask
= adp5588_irq_mask
,
238 .irq_unmask
= adp5588_irq_unmask
,
239 .irq_bus_lock
= adp5588_irq_bus_lock
,
240 .irq_bus_sync_unlock
= adp5588_irq_bus_sync_unlock
,
241 .irq_set_type
= adp5588_irq_set_type
,
244 static int adp5588_gpio_read_intstat(struct i2c_client
*client
, u8
*buf
)
246 int ret
= i2c_smbus_read_i2c_block_data(client
, GPIO_INT_STAT1
, 3, buf
);
249 dev_err(&client
->dev
, "Read INT_STAT Error\n");
254 static irqreturn_t
adp5588_irq_handler(int irq
, void *devid
)
256 struct adp5588_gpio
*dev
= devid
;
257 unsigned status
, bank
, bit
, pending
;
259 status
= adp5588_gpio_read(dev
->client
, INT_STAT
);
261 if (status
& ADP5588_GPI_INT
) {
262 ret
= adp5588_gpio_read_intstat(dev
->client
, dev
->irq_stat
);
264 memset(dev
->irq_stat
, 0, ARRAY_SIZE(dev
->irq_stat
));
266 for (bank
= 0, bit
= 0; bank
<= ADP5588_BANK(ADP5588_MAXGPIO
);
268 pending
= dev
->irq_stat
[bank
] & dev
->irq_mask
[bank
];
271 if (pending
& (1 << bit
)) {
272 handle_nested_irq(dev
->irq_base
+
274 pending
&= ~(1 << bit
);
282 adp5588_gpio_write(dev
->client
, INT_STAT
, status
); /* Status is W1C */
287 static int adp5588_irq_setup(struct adp5588_gpio
*dev
)
289 struct i2c_client
*client
= dev
->client
;
290 struct adp5588_gpio_platform_data
*pdata
=
291 dev_get_platdata(&client
->dev
);
295 adp5588_gpio_write(client
, CFG
, ADP5588_AUTO_INC
);
296 adp5588_gpio_write(client
, INT_STAT
, -1); /* status is W1C */
297 adp5588_gpio_read_intstat(client
, dev
->irq_stat
); /* read to clear */
299 dev
->irq_base
= pdata
->irq_base
;
300 mutex_init(&dev
->irq_lock
);
302 for (gpio
= 0; gpio
< dev
->gpio_chip
.ngpio
; gpio
++) {
303 int irq
= gpio
+ dev
->irq_base
;
304 irq_set_chip_data(irq
, dev
);
305 irq_set_chip_and_handler(irq
, &adp5588_irq_chip
,
307 irq_set_nested_thread(irq
, 1);
310 * ARM needs us to explicitly flag the IRQ as VALID,
311 * once we do so, it will also set the noprobe.
313 set_irq_flags(irq
, IRQF_VALID
);
315 irq_set_noprobe(irq
);
319 ret
= request_threaded_irq(client
->irq
,
322 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
323 dev_name(&client
->dev
), dev
);
325 dev_err(&client
->dev
, "failed to request irq %d\n",
330 dev
->gpio_chip
.to_irq
= adp5588_gpio_to_irq
;
331 adp5588_gpio_write(client
, CFG
,
332 ADP5588_AUTO_INC
| ADP5588_INT_CFG
| ADP5588_GPI_INT
);
341 static void adp5588_irq_teardown(struct adp5588_gpio
*dev
)
344 free_irq(dev
->client
->irq
, dev
);
348 static int adp5588_irq_setup(struct adp5588_gpio
*dev
)
350 struct i2c_client
*client
= dev
->client
;
351 dev_warn(&client
->dev
, "interrupt support not compiled in\n");
356 static void adp5588_irq_teardown(struct adp5588_gpio
*dev
)
359 #endif /* CONFIG_GPIO_ADP5588_IRQ */
361 static int adp5588_gpio_probe(struct i2c_client
*client
,
362 const struct i2c_device_id
*id
)
364 struct adp5588_gpio_platform_data
*pdata
=
365 dev_get_platdata(&client
->dev
);
366 struct adp5588_gpio
*dev
;
367 struct gpio_chip
*gc
;
371 dev_err(&client
->dev
, "missing platform data\n");
375 if (!i2c_check_functionality(client
->adapter
,
376 I2C_FUNC_SMBUS_BYTE_DATA
)) {
377 dev_err(&client
->dev
, "SMBUS Byte Data not Supported\n");
381 dev
= devm_kzalloc(&client
->dev
, sizeof(*dev
), GFP_KERNEL
);
385 dev
->client
= client
;
387 gc
= &dev
->gpio_chip
;
388 gc
->direction_input
= adp5588_gpio_direction_input
;
389 gc
->direction_output
= adp5588_gpio_direction_output
;
390 gc
->get
= adp5588_gpio_get_value
;
391 gc
->set
= adp5588_gpio_set_value
;
392 gc
->can_sleep
= true;
394 gc
->base
= pdata
->gpio_start
;
395 gc
->ngpio
= ADP5588_MAXGPIO
;
396 gc
->label
= client
->name
;
397 gc
->owner
= THIS_MODULE
;
398 gc
->names
= pdata
->names
;
400 mutex_init(&dev
->lock
);
402 ret
= adp5588_gpio_read(dev
->client
, DEV_ID
);
406 revid
= ret
& ADP5588_DEVICE_ID_MASK
;
408 for (i
= 0, ret
= 0; i
<= ADP5588_BANK(ADP5588_MAXGPIO
); i
++) {
409 dev
->dat_out
[i
] = adp5588_gpio_read(client
, GPIO_DAT_OUT1
+ i
);
410 dev
->dir
[i
] = adp5588_gpio_read(client
, GPIO_DIR1
+ i
);
411 ret
|= adp5588_gpio_write(client
, KP_GPIO1
+ i
, 0);
412 ret
|= adp5588_gpio_write(client
, GPIO_PULL1
+ i
,
413 (pdata
->pullup_dis_mask
>> (8 * i
)) & 0xFF);
414 ret
|= adp5588_gpio_write(client
, GPIO_INT_EN1
+ i
, 0);
419 if (pdata
->irq_base
) {
420 if (WA_DELAYED_READOUT_REVID(revid
)) {
421 dev_warn(&client
->dev
, "GPIO int not supported\n");
423 ret
= adp5588_irq_setup(dev
);
429 ret
= gpiochip_add(&dev
->gpio_chip
);
433 dev_info(&client
->dev
, "IRQ Base: %d Rev.: %d\n",
434 pdata
->irq_base
, revid
);
437 ret
= pdata
->setup(client
, gc
->base
, gc
->ngpio
, pdata
->context
);
439 dev_warn(&client
->dev
, "setup failed, %d\n", ret
);
442 i2c_set_clientdata(client
, dev
);
447 adp5588_irq_teardown(dev
);
452 static int adp5588_gpio_remove(struct i2c_client
*client
)
454 struct adp5588_gpio_platform_data
*pdata
=
455 dev_get_platdata(&client
->dev
);
456 struct adp5588_gpio
*dev
= i2c_get_clientdata(client
);
459 if (pdata
->teardown
) {
460 ret
= pdata
->teardown(client
,
461 dev
->gpio_chip
.base
, dev
->gpio_chip
.ngpio
,
464 dev_err(&client
->dev
, "teardown failed %d\n", ret
);
470 free_irq(dev
->client
->irq
, dev
);
472 gpiochip_remove(&dev
->gpio_chip
);
477 static const struct i2c_device_id adp5588_gpio_id
[] = {
482 MODULE_DEVICE_TABLE(i2c
, adp5588_gpio_id
);
484 static struct i2c_driver adp5588_gpio_driver
= {
488 .probe
= adp5588_gpio_probe
,
489 .remove
= adp5588_gpio_remove
,
490 .id_table
= adp5588_gpio_id
,
493 module_i2c_driver(adp5588_gpio_driver
);
495 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
496 MODULE_DESCRIPTION("GPIO ADP5588 Driver");
497 MODULE_LICENSE("GPL");