[SCSI] mvsas: update comments
[linux-2.6/linux-mips.git] / drivers / gpio / gpio-adp5588.c
blob3525ad91877162aefb6cacc5bcd0963f3156aee7
1 /*
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.
8 */
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
26 * asserted.
28 #define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4)
30 struct adp5588_gpio {
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;
36 unsigned gpio_start;
37 unsigned irq_base;
38 uint8_t dat_out[3];
39 uint8_t dir[3];
40 uint8_t int_lvl[3];
41 uint8_t int_en[3];
42 uint8_t irq_mask[3];
43 uint8_t irq_stat[3];
46 static int adp5588_gpio_read(struct i2c_client *client, u8 reg)
48 int ret = i2c_smbus_read_byte_data(client, reg);
50 if (ret < 0)
51 dev_err(&client->dev, "Read Error\n");
53 return ret;
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);
60 if (ret < 0)
61 dev_err(&client->dev, "Write Error\n");
63 return ret;
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)
78 unsigned bank, bit;
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);
86 if (val)
87 dev->dat_out[bank] |= bit;
88 else
89 dev->dat_out[bank] &= ~bit;
91 adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
92 dev->dat_out[bank]);
93 mutex_unlock(&dev->lock);
96 static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
98 int ret;
99 unsigned bank;
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);
110 return ret;
113 static int adp5588_gpio_direction_output(struct gpio_chip *chip,
114 unsigned off, int val)
116 int ret;
117 unsigned bank, bit;
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;
127 if (val)
128 dev->dat_out[bank] |= bit;
129 else
130 dev->dat_out[bank] &= ~bit;
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);
138 return ret;
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(struct irq_data *d)
151 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
153 mutex_lock(&dev->irq_lock);
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.
164 static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
166 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
167 int i;
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]);
176 mutex_unlock(&dev->irq_lock);
179 static void adp5588_irq_mask(struct irq_data *d)
181 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
182 unsigned gpio = d->irq - dev->irq_base;
184 dev->irq_mask[ADP5588_BANK(gpio)] &= ~ADP5588_BIT(gpio);
187 static void adp5588_irq_unmask(struct irq_data *d)
189 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
190 unsigned gpio = d->irq - dev->irq_base;
192 dev->irq_mask[ADP5588_BANK(gpio)] |= ADP5588_BIT(gpio);
195 static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
197 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
198 uint16_t gpio = d->irq - dev->irq_base;
199 unsigned bank, bit;
201 if ((type & IRQ_TYPE_EDGE_BOTH)) {
202 dev_err(&dev->client->dev, "irq %d: unsupported type %d\n",
203 d->irq, type);
204 return -EINVAL;
207 bank = ADP5588_BANK(gpio);
208 bit = ADP5588_BIT(gpio);
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;
217 adp5588_gpio_direction_input(&dev->gpio_chip, gpio);
218 adp5588_gpio_write(dev->client, GPIO_INT_LVL1 + bank,
219 dev->int_lvl[bank]);
221 return 0;
224 static struct irq_chip adp5588_irq_chip = {
225 .name = "adp5588",
226 .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,
233 static int adp5588_gpio_read_intstat(struct i2c_client *client, u8 *buf)
235 int ret = i2c_smbus_read_i2c_block_data(client, GPIO_INT_STAT1, 3, buf);
237 if (ret < 0)
238 dev_err(&client->dev, "Read INT_STAT Error\n");
240 return ret;
243 static irqreturn_t adp5588_irq_handler(int irq, void *devid)
245 struct adp5588_gpio *dev = devid;
246 unsigned status, bank, bit, pending;
247 int ret;
248 status = adp5588_gpio_read(dev->client, INT_STAT);
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));
255 for (bank = 0; bank <= ADP5588_BANK(ADP5588_MAXGPIO);
256 bank++, bit = 0) {
257 pending = dev->irq_stat[bank] & dev->irq_mask[bank];
259 while (pending) {
260 if (pending & (1 << bit)) {
261 handle_nested_irq(dev->irq_base +
262 (bank << 3) + bit);
263 pending &= ~(1 << bit);
266 bit++;
271 adp5588_gpio_write(dev->client, INT_STAT, status); /* Status is W1C */
273 return IRQ_HANDLED;
276 static int adp5588_irq_setup(struct adp5588_gpio *dev)
278 struct i2c_client *client = dev->client;
279 struct adp5588_gpio_platform_data *pdata = client->dev.platform_data;
280 unsigned gpio;
281 int ret;
283 adp5588_gpio_write(client, CFG, ADP5588_AUTO_INC);
284 adp5588_gpio_write(client, INT_STAT, -1); /* status is W1C */
285 adp5588_gpio_read_intstat(client, dev->irq_stat); /* read to clear */
287 dev->irq_base = pdata->irq_base;
288 mutex_init(&dev->irq_lock);
290 for (gpio = 0; gpio < dev->gpio_chip.ngpio; gpio++) {
291 int irq = gpio + dev->irq_base;
292 irq_set_chip_data(irq, dev);
293 irq_set_chip_and_handler(irq, &adp5588_irq_chip,
294 handle_level_irq);
295 irq_set_nested_thread(irq, 1);
296 #ifdef CONFIG_ARM
298 * ARM needs us to explicitly flag the IRQ as VALID,
299 * once we do so, it will also set the noprobe.
301 set_irq_flags(irq, IRQF_VALID);
302 #else
303 irq_set_noprobe(irq);
304 #endif
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;
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);
322 return 0;
324 out:
325 dev->irq_base = 0;
326 return ret;
329 static void adp5588_irq_teardown(struct adp5588_gpio *dev)
331 if (dev->irq_base)
332 free_irq(dev->client->irq, dev);
335 #else
336 static int adp5588_irq_setup(struct adp5588_gpio *dev)
338 struct i2c_client *client = dev->client;
339 dev_warn(&client->dev, "interrupt support not compiled in\n");
341 return 0;
344 static void adp5588_irq_teardown(struct adp5588_gpio *dev)
347 #endif /* CONFIG_GPIO_ADP5588_IRQ */
349 static int __devinit adp5588_gpio_probe(struct i2c_client *client,
350 const struct i2c_device_id *id)
352 struct adp5588_gpio_platform_data *pdata = client->dev.platform_data;
353 struct adp5588_gpio *dev;
354 struct gpio_chip *gc;
355 int ret, i, revid;
357 if (pdata == NULL) {
358 dev_err(&client->dev, "missing platform data\n");
359 return -ENODEV;
362 if (!i2c_check_functionality(client->adapter,
363 I2C_FUNC_SMBUS_BYTE_DATA)) {
364 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
365 return -EIO;
368 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
369 if (dev == NULL) {
370 dev_err(&client->dev, "failed to alloc memory\n");
371 return -ENOMEM;
374 dev->client = client;
376 gc = &dev->gpio_chip;
377 gc->direction_input = adp5588_gpio_direction_input;
378 gc->direction_output = adp5588_gpio_direction_output;
379 gc->get = adp5588_gpio_get_value;
380 gc->set = adp5588_gpio_set_value;
381 gc->can_sleep = 1;
383 gc->base = pdata->gpio_start;
384 gc->ngpio = ADP5588_MAXGPIO;
385 gc->label = client->name;
386 gc->owner = THIS_MODULE;
388 mutex_init(&dev->lock);
390 ret = adp5588_gpio_read(dev->client, DEV_ID);
391 if (ret < 0)
392 goto err;
394 revid = ret & ADP5588_DEVICE_ID_MASK;
396 for (i = 0, ret = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
397 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);
402 ret |= adp5588_gpio_write(client, GPIO_INT_EN1 + i, 0);
403 if (ret)
404 goto err;
407 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;
417 ret = gpiochip_add(&dev->gpio_chip);
418 if (ret)
419 goto err_irq;
421 dev_info(&client->dev, "gpios %d..%d (IRQ Base %d) on a %s Rev. %d\n",
422 gc->base, gc->base + gc->ngpio - 1,
423 pdata->irq_base, client->name, revid);
425 if (pdata->setup) {
426 ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context);
427 if (ret < 0)
428 dev_warn(&client->dev, "setup failed, %d\n", ret);
431 i2c_set_clientdata(client, dev);
433 return 0;
435 err_irq:
436 adp5588_irq_teardown(dev);
437 err:
438 kfree(dev);
439 return ret;
442 static int __devexit adp5588_gpio_remove(struct i2c_client *client)
444 struct adp5588_gpio_platform_data *pdata = client->dev.platform_data;
445 struct adp5588_gpio *dev = i2c_get_clientdata(client);
446 int ret;
448 if (pdata->teardown) {
449 ret = pdata->teardown(client,
450 dev->gpio_chip.base, dev->gpio_chip.ngpio,
451 pdata->context);
452 if (ret < 0) {
453 dev_err(&client->dev, "teardown failed %d\n", ret);
454 return ret;
458 if (dev->irq_base)
459 free_irq(dev->client->irq, dev);
461 ret = gpiochip_remove(&dev->gpio_chip);
462 if (ret) {
463 dev_err(&client->dev, "gpiochip_remove failed %d\n", ret);
464 return ret;
467 kfree(dev);
468 return 0;
471 static const struct i2c_device_id adp5588_gpio_id[] = {
472 {DRV_NAME, 0},
476 MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id);
478 static struct i2c_driver adp5588_gpio_driver = {
479 .driver = {
480 .name = DRV_NAME,
482 .probe = adp5588_gpio_probe,
483 .remove = __devexit_p(adp5588_gpio_remove),
484 .id_table = adp5588_gpio_id,
487 static int __init adp5588_gpio_init(void)
489 return i2c_add_driver(&adp5588_gpio_driver);
492 module_init(adp5588_gpio_init);
494 static void __exit adp5588_gpio_exit(void)
496 i2c_del_driver(&adp5588_gpio_driver);
499 module_exit(adp5588_gpio_exit);
501 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
502 MODULE_DESCRIPTION("GPIO ADP5588 Driver");
503 MODULE_LICENSE("GPL");