ux500: remove ambiguous irq macros
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpio / pca953x.c
blob501866662e055131bc3dad3f4a5fb0b104913060
1 /*
2 * pca953x.c - 4/8/16 bit I/O ports
4 * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
5 * Copyright (C) 2007 Marvell International Ltd.
7 * Derived from drivers/i2c/chips/pca9539.c
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/gpio.h>
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/i2c.h>
20 #include <linux/i2c/pca953x.h>
21 #include <linux/slab.h>
22 #ifdef CONFIG_OF_GPIO
23 #include <linux/of_platform.h>
24 #include <linux/of_gpio.h>
25 #endif
27 #define PCA953X_INPUT 0
28 #define PCA953X_OUTPUT 1
29 #define PCA953X_INVERT 2
30 #define PCA953X_DIRECTION 3
32 #define PCA953X_GPIOS 0x00FF
33 #define PCA953X_INT 0x0100
35 static const struct i2c_device_id pca953x_id[] = {
36 { "pca9534", 8 | PCA953X_INT, },
37 { "pca9535", 16 | PCA953X_INT, },
38 { "pca9536", 4, },
39 { "pca9537", 4 | PCA953X_INT, },
40 { "pca9538", 8 | PCA953X_INT, },
41 { "pca9539", 16 | PCA953X_INT, },
42 { "pca9554", 8 | PCA953X_INT, },
43 { "pca9555", 16 | PCA953X_INT, },
44 { "pca9556", 8, },
45 { "pca9557", 8, },
47 { "max7310", 8, },
48 { "max7312", 16 | PCA953X_INT, },
49 { "max7313", 16 | PCA953X_INT, },
50 { "max7315", 8 | PCA953X_INT, },
51 { "pca6107", 8 | PCA953X_INT, },
52 { "tca6408", 8 | PCA953X_INT, },
53 { "tca6416", 16 | PCA953X_INT, },
54 /* NYET: { "tca6424", 24, }, */
55 { }
57 MODULE_DEVICE_TABLE(i2c, pca953x_id);
59 struct pca953x_chip {
60 unsigned gpio_start;
61 uint16_t reg_output;
62 uint16_t reg_direction;
64 #ifdef CONFIG_GPIO_PCA953X_IRQ
65 struct mutex irq_lock;
66 uint16_t irq_mask;
67 uint16_t irq_stat;
68 uint16_t irq_trig_raise;
69 uint16_t irq_trig_fall;
70 int irq_base;
71 #endif
73 struct i2c_client *client;
74 struct pca953x_platform_data *dyn_pdata;
75 struct gpio_chip gpio_chip;
76 const char *const *names;
79 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
81 int ret;
83 if (chip->gpio_chip.ngpio <= 8)
84 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
85 else
86 ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
88 if (ret < 0) {
89 dev_err(&chip->client->dev, "failed writing register\n");
90 return ret;
93 return 0;
96 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
98 int ret;
100 if (chip->gpio_chip.ngpio <= 8)
101 ret = i2c_smbus_read_byte_data(chip->client, reg);
102 else
103 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
105 if (ret < 0) {
106 dev_err(&chip->client->dev, "failed reading register\n");
107 return ret;
110 *val = (uint16_t)ret;
111 return 0;
114 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
116 struct pca953x_chip *chip;
117 uint16_t reg_val;
118 int ret;
120 chip = container_of(gc, struct pca953x_chip, gpio_chip);
122 reg_val = chip->reg_direction | (1u << off);
123 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
124 if (ret)
125 return ret;
127 chip->reg_direction = reg_val;
128 return 0;
131 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
132 unsigned off, int val)
134 struct pca953x_chip *chip;
135 uint16_t reg_val;
136 int ret;
138 chip = container_of(gc, struct pca953x_chip, gpio_chip);
140 /* set output level */
141 if (val)
142 reg_val = chip->reg_output | (1u << off);
143 else
144 reg_val = chip->reg_output & ~(1u << off);
146 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
147 if (ret)
148 return ret;
150 chip->reg_output = reg_val;
152 /* then direction */
153 reg_val = chip->reg_direction & ~(1u << off);
154 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
155 if (ret)
156 return ret;
158 chip->reg_direction = reg_val;
159 return 0;
162 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
164 struct pca953x_chip *chip;
165 uint16_t reg_val;
166 int ret;
168 chip = container_of(gc, struct pca953x_chip, gpio_chip);
170 ret = pca953x_read_reg(chip, PCA953X_INPUT, &reg_val);
171 if (ret < 0) {
172 /* NOTE: diagnostic already emitted; that's all we should
173 * do unless gpio_*_value_cansleep() calls become different
174 * from their nonsleeping siblings (and report faults).
176 return 0;
179 return (reg_val & (1u << off)) ? 1 : 0;
182 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
184 struct pca953x_chip *chip;
185 uint16_t reg_val;
186 int ret;
188 chip = container_of(gc, struct pca953x_chip, gpio_chip);
190 if (val)
191 reg_val = chip->reg_output | (1u << off);
192 else
193 reg_val = chip->reg_output & ~(1u << off);
195 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
196 if (ret)
197 return;
199 chip->reg_output = reg_val;
202 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
204 struct gpio_chip *gc;
206 gc = &chip->gpio_chip;
208 gc->direction_input = pca953x_gpio_direction_input;
209 gc->direction_output = pca953x_gpio_direction_output;
210 gc->get = pca953x_gpio_get_value;
211 gc->set = pca953x_gpio_set_value;
212 gc->can_sleep = 1;
214 gc->base = chip->gpio_start;
215 gc->ngpio = gpios;
216 gc->label = chip->client->name;
217 gc->dev = &chip->client->dev;
218 gc->owner = THIS_MODULE;
219 gc->names = chip->names;
222 #ifdef CONFIG_GPIO_PCA953X_IRQ
223 static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
225 struct pca953x_chip *chip;
227 chip = container_of(gc, struct pca953x_chip, gpio_chip);
228 return chip->irq_base + off;
231 static void pca953x_irq_mask(unsigned int irq)
233 struct pca953x_chip *chip = get_irq_chip_data(irq);
235 chip->irq_mask &= ~(1 << (irq - chip->irq_base));
238 static void pca953x_irq_unmask(unsigned int irq)
240 struct pca953x_chip *chip = get_irq_chip_data(irq);
242 chip->irq_mask |= 1 << (irq - chip->irq_base);
245 static void pca953x_irq_bus_lock(unsigned int irq)
247 struct pca953x_chip *chip = get_irq_chip_data(irq);
249 mutex_lock(&chip->irq_lock);
252 static void pca953x_irq_bus_sync_unlock(unsigned int irq)
254 struct pca953x_chip *chip = get_irq_chip_data(irq);
255 uint16_t new_irqs;
256 uint16_t level;
258 /* Look for any newly setup interrupt */
259 new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
260 new_irqs &= ~chip->reg_direction;
262 while (new_irqs) {
263 level = __ffs(new_irqs);
264 pca953x_gpio_direction_input(&chip->gpio_chip, level);
265 new_irqs &= ~(1 << level);
268 mutex_unlock(&chip->irq_lock);
271 static int pca953x_irq_set_type(unsigned int irq, unsigned int type)
273 struct pca953x_chip *chip = get_irq_chip_data(irq);
274 uint16_t level = irq - chip->irq_base;
275 uint16_t mask = 1 << level;
277 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
278 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
279 irq, type);
280 return -EINVAL;
283 if (type & IRQ_TYPE_EDGE_FALLING)
284 chip->irq_trig_fall |= mask;
285 else
286 chip->irq_trig_fall &= ~mask;
288 if (type & IRQ_TYPE_EDGE_RISING)
289 chip->irq_trig_raise |= mask;
290 else
291 chip->irq_trig_raise &= ~mask;
293 return 0;
296 static struct irq_chip pca953x_irq_chip = {
297 .name = "pca953x",
298 .mask = pca953x_irq_mask,
299 .unmask = pca953x_irq_unmask,
300 .bus_lock = pca953x_irq_bus_lock,
301 .bus_sync_unlock = pca953x_irq_bus_sync_unlock,
302 .set_type = pca953x_irq_set_type,
305 static uint16_t pca953x_irq_pending(struct pca953x_chip *chip)
307 uint16_t cur_stat;
308 uint16_t old_stat;
309 uint16_t pending;
310 uint16_t trigger;
311 int ret;
313 ret = pca953x_read_reg(chip, PCA953X_INPUT, &cur_stat);
314 if (ret)
315 return 0;
317 /* Remove output pins from the equation */
318 cur_stat &= chip->reg_direction;
320 old_stat = chip->irq_stat;
321 trigger = (cur_stat ^ old_stat) & chip->irq_mask;
323 if (!trigger)
324 return 0;
326 chip->irq_stat = cur_stat;
328 pending = (old_stat & chip->irq_trig_fall) |
329 (cur_stat & chip->irq_trig_raise);
330 pending &= trigger;
332 return pending;
335 static irqreturn_t pca953x_irq_handler(int irq, void *devid)
337 struct pca953x_chip *chip = devid;
338 uint16_t pending;
339 uint16_t level;
341 pending = pca953x_irq_pending(chip);
343 if (!pending)
344 return IRQ_HANDLED;
346 do {
347 level = __ffs(pending);
348 generic_handle_irq(level + chip->irq_base);
350 pending &= ~(1 << level);
351 } while (pending);
353 return IRQ_HANDLED;
356 static int pca953x_irq_setup(struct pca953x_chip *chip,
357 const struct i2c_device_id *id)
359 struct i2c_client *client = chip->client;
360 struct pca953x_platform_data *pdata = client->dev.platform_data;
361 int ret;
363 if (pdata->irq_base != -1
364 && (id->driver_data & PCA953X_INT)) {
365 int lvl;
367 ret = pca953x_read_reg(chip, PCA953X_INPUT,
368 &chip->irq_stat);
369 if (ret)
370 goto out_failed;
373 * There is no way to know which GPIO line generated the
374 * interrupt. We have to rely on the previous read for
375 * this purpose.
377 chip->irq_stat &= chip->reg_direction;
378 chip->irq_base = pdata->irq_base;
379 mutex_init(&chip->irq_lock);
381 for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
382 int irq = lvl + chip->irq_base;
384 set_irq_chip_data(irq, chip);
385 set_irq_chip_and_handler(irq, &pca953x_irq_chip,
386 handle_edge_irq);
387 #ifdef CONFIG_ARM
388 set_irq_flags(irq, IRQF_VALID);
389 #else
390 set_irq_noprobe(irq);
391 #endif
394 ret = request_threaded_irq(client->irq,
395 NULL,
396 pca953x_irq_handler,
397 IRQF_TRIGGER_RISING |
398 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
399 dev_name(&client->dev), chip);
400 if (ret) {
401 dev_err(&client->dev, "failed to request irq %d\n",
402 client->irq);
403 goto out_failed;
406 chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
409 return 0;
411 out_failed:
412 chip->irq_base = -1;
413 return ret;
416 static void pca953x_irq_teardown(struct pca953x_chip *chip)
418 if (chip->irq_base != -1)
419 free_irq(chip->client->irq, chip);
421 #else /* CONFIG_GPIO_PCA953X_IRQ */
422 static int pca953x_irq_setup(struct pca953x_chip *chip,
423 const struct i2c_device_id *id)
425 struct i2c_client *client = chip->client;
426 struct pca953x_platform_data *pdata = client->dev.platform_data;
428 if (pdata->irq_base != -1 && (id->driver_data & PCA953X_INT))
429 dev_warn(&client->dev, "interrupt support not compiled in\n");
431 return 0;
434 static void pca953x_irq_teardown(struct pca953x_chip *chip)
437 #endif
440 * Handlers for alternative sources of platform_data
442 #ifdef CONFIG_OF_GPIO
444 * Translate OpenFirmware node properties into platform_data
446 static struct pca953x_platform_data *
447 pca953x_get_alt_pdata(struct i2c_client *client)
449 struct pca953x_platform_data *pdata;
450 struct device_node *node;
451 const uint16_t *val;
453 node = client->dev.of_node;
454 if (node == NULL)
455 return NULL;
457 pdata = kzalloc(sizeof(struct pca953x_platform_data), GFP_KERNEL);
458 if (pdata == NULL) {
459 dev_err(&client->dev, "Unable to allocate platform_data\n");
460 return NULL;
463 pdata->gpio_base = -1;
464 val = of_get_property(node, "linux,gpio-base", NULL);
465 if (val) {
466 if (*val < 0)
467 dev_warn(&client->dev,
468 "invalid gpio-base in device tree\n");
469 else
470 pdata->gpio_base = *val;
473 val = of_get_property(node, "polarity", NULL);
474 if (val)
475 pdata->invert = *val;
477 return pdata;
479 #else
480 static struct pca953x_platform_data *
481 pca953x_get_alt_pdata(struct i2c_client *client)
483 return NULL;
485 #endif
487 static int __devinit pca953x_probe(struct i2c_client *client,
488 const struct i2c_device_id *id)
490 struct pca953x_platform_data *pdata;
491 struct pca953x_chip *chip;
492 int ret;
494 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
495 if (chip == NULL)
496 return -ENOMEM;
498 pdata = client->dev.platform_data;
499 if (pdata == NULL) {
500 pdata = pca953x_get_alt_pdata(client);
502 * Unlike normal platform_data, this is allocated
503 * dynamically and must be freed in the driver
505 chip->dyn_pdata = pdata;
508 if (pdata == NULL) {
509 dev_dbg(&client->dev, "no platform data\n");
510 ret = -EINVAL;
511 goto out_failed;
514 chip->client = client;
516 chip->gpio_start = pdata->gpio_base;
518 chip->names = pdata->names;
520 /* initialize cached registers from their original values.
521 * we can't share this chip with another i2c master.
523 pca953x_setup_gpio(chip, id->driver_data & PCA953X_GPIOS);
525 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
526 if (ret)
527 goto out_failed;
529 ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
530 if (ret)
531 goto out_failed;
533 /* set platform specific polarity inversion */
534 ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
535 if (ret)
536 goto out_failed;
538 ret = pca953x_irq_setup(chip, id);
539 if (ret)
540 goto out_failed;
542 ret = gpiochip_add(&chip->gpio_chip);
543 if (ret)
544 goto out_failed;
546 if (pdata->setup) {
547 ret = pdata->setup(client, chip->gpio_chip.base,
548 chip->gpio_chip.ngpio, pdata->context);
549 if (ret < 0)
550 dev_warn(&client->dev, "setup failed, %d\n", ret);
553 i2c_set_clientdata(client, chip);
554 return 0;
556 out_failed:
557 pca953x_irq_teardown(chip);
558 kfree(chip->dyn_pdata);
559 kfree(chip);
560 return ret;
563 static int pca953x_remove(struct i2c_client *client)
565 struct pca953x_platform_data *pdata = client->dev.platform_data;
566 struct pca953x_chip *chip = i2c_get_clientdata(client);
567 int ret = 0;
569 if (pdata->teardown) {
570 ret = pdata->teardown(client, chip->gpio_chip.base,
571 chip->gpio_chip.ngpio, pdata->context);
572 if (ret < 0) {
573 dev_err(&client->dev, "%s failed, %d\n",
574 "teardown", ret);
575 return ret;
579 ret = gpiochip_remove(&chip->gpio_chip);
580 if (ret) {
581 dev_err(&client->dev, "%s failed, %d\n",
582 "gpiochip_remove()", ret);
583 return ret;
586 pca953x_irq_teardown(chip);
587 kfree(chip->dyn_pdata);
588 kfree(chip);
589 return 0;
592 static struct i2c_driver pca953x_driver = {
593 .driver = {
594 .name = "pca953x",
596 .probe = pca953x_probe,
597 .remove = pca953x_remove,
598 .id_table = pca953x_id,
601 static int __init pca953x_init(void)
603 return i2c_add_driver(&pca953x_driver);
605 /* register after i2c postcore initcall and before
606 * subsys initcalls that may rely on these GPIOs
608 subsys_initcall(pca953x_init);
610 static void __exit pca953x_exit(void)
612 i2c_del_driver(&pca953x_driver);
614 module_exit(pca953x_exit);
616 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
617 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
618 MODULE_LICENSE("GPL");