ppp: fix pptp double release_sock in pptp_bind()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpio / pca953x.c
blob0451d7ac94ac1d51b062d0da4ce19e4e12b6bdf4
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 PCA957X_IN 0
33 #define PCA957X_INVRT 1
34 #define PCA957X_BKEN 2
35 #define PCA957X_PUPD 3
36 #define PCA957X_CFG 4
37 #define PCA957X_OUT 5
38 #define PCA957X_MSK 6
39 #define PCA957X_INTS 7
41 #define PCA_GPIO_MASK 0x00FF
42 #define PCA_INT 0x0100
43 #define PCA953X_TYPE 0x1000
44 #define PCA957X_TYPE 0x2000
46 static const struct i2c_device_id pca953x_id[] = {
47 { "pca9534", 8 | PCA953X_TYPE | PCA_INT, },
48 { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
49 { "pca9536", 4 | PCA953X_TYPE, },
50 { "pca9537", 4 | PCA953X_TYPE | PCA_INT, },
51 { "pca9538", 8 | PCA953X_TYPE | PCA_INT, },
52 { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
53 { "pca9554", 8 | PCA953X_TYPE | PCA_INT, },
54 { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
55 { "pca9556", 8 | PCA953X_TYPE, },
56 { "pca9557", 8 | PCA953X_TYPE, },
57 { "pca9574", 8 | PCA957X_TYPE | PCA_INT, },
58 { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
60 { "max7310", 8 | PCA953X_TYPE, },
61 { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
62 { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
63 { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
64 { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
65 { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
66 { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
67 /* NYET: { "tca6424", 24, }, */
68 { }
70 MODULE_DEVICE_TABLE(i2c, pca953x_id);
72 struct pca953x_chip {
73 unsigned gpio_start;
74 uint16_t reg_output;
75 uint16_t reg_direction;
76 struct mutex i2c_lock;
78 #ifdef CONFIG_GPIO_PCA953X_IRQ
79 struct mutex irq_lock;
80 uint16_t irq_mask;
81 uint16_t irq_stat;
82 uint16_t irq_trig_raise;
83 uint16_t irq_trig_fall;
84 int irq_base;
85 #endif
87 struct i2c_client *client;
88 struct pca953x_platform_data *dyn_pdata;
89 struct gpio_chip gpio_chip;
90 const char *const *names;
91 int chip_type;
94 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
96 int ret = 0;
98 if (chip->gpio_chip.ngpio <= 8)
99 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
100 else {
101 switch (chip->chip_type) {
102 case PCA953X_TYPE:
103 ret = i2c_smbus_write_word_data(chip->client,
104 reg << 1, val);
105 break;
106 case PCA957X_TYPE:
107 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
108 val & 0xff);
109 if (ret < 0)
110 break;
111 ret = i2c_smbus_write_byte_data(chip->client,
112 (reg << 1) + 1,
113 (val & 0xff00) >> 8);
114 break;
118 if (ret < 0) {
119 dev_err(&chip->client->dev, "failed writing register\n");
120 return ret;
123 return 0;
126 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
128 int ret;
130 if (chip->gpio_chip.ngpio <= 8)
131 ret = i2c_smbus_read_byte_data(chip->client, reg);
132 else
133 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
135 if (ret < 0) {
136 dev_err(&chip->client->dev, "failed reading register\n");
137 return ret;
140 *val = (uint16_t)ret;
141 return 0;
144 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
146 struct pca953x_chip *chip;
147 uint16_t reg_val;
148 int ret, offset = 0;
150 chip = container_of(gc, struct pca953x_chip, gpio_chip);
152 mutex_lock(&chip->i2c_lock);
153 reg_val = chip->reg_direction | (1u << off);
155 switch (chip->chip_type) {
156 case PCA953X_TYPE:
157 offset = PCA953X_DIRECTION;
158 break;
159 case PCA957X_TYPE:
160 offset = PCA957X_CFG;
161 break;
163 ret = pca953x_write_reg(chip, offset, reg_val);
164 if (ret)
165 goto exit;
167 chip->reg_direction = reg_val;
168 ret = 0;
169 exit:
170 mutex_unlock(&chip->i2c_lock);
171 return ret;
174 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
175 unsigned off, int val)
177 struct pca953x_chip *chip;
178 uint16_t reg_val;
179 int ret, offset = 0;
181 chip = container_of(gc, struct pca953x_chip, gpio_chip);
183 mutex_lock(&chip->i2c_lock);
184 /* set output level */
185 if (val)
186 reg_val = chip->reg_output | (1u << off);
187 else
188 reg_val = chip->reg_output & ~(1u << off);
190 switch (chip->chip_type) {
191 case PCA953X_TYPE:
192 offset = PCA953X_OUTPUT;
193 break;
194 case PCA957X_TYPE:
195 offset = PCA957X_OUT;
196 break;
198 ret = pca953x_write_reg(chip, offset, reg_val);
199 if (ret)
200 goto exit;
202 chip->reg_output = reg_val;
204 /* then direction */
205 reg_val = chip->reg_direction & ~(1u << off);
206 switch (chip->chip_type) {
207 case PCA953X_TYPE:
208 offset = PCA953X_DIRECTION;
209 break;
210 case PCA957X_TYPE:
211 offset = PCA957X_CFG;
212 break;
214 ret = pca953x_write_reg(chip, offset, reg_val);
215 if (ret)
216 goto exit;
218 chip->reg_direction = reg_val;
219 ret = 0;
220 exit:
221 mutex_unlock(&chip->i2c_lock);
222 return ret;
225 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
227 struct pca953x_chip *chip;
228 uint16_t reg_val;
229 int ret, offset = 0;
231 chip = container_of(gc, struct pca953x_chip, gpio_chip);
233 mutex_lock(&chip->i2c_lock);
234 switch (chip->chip_type) {
235 case PCA953X_TYPE:
236 offset = PCA953X_INPUT;
237 break;
238 case PCA957X_TYPE:
239 offset = PCA957X_IN;
240 break;
242 ret = pca953x_read_reg(chip, offset, &reg_val);
243 mutex_unlock(&chip->i2c_lock);
244 if (ret < 0) {
245 /* NOTE: diagnostic already emitted; that's all we should
246 * do unless gpio_*_value_cansleep() calls become different
247 * from their nonsleeping siblings (and report faults).
249 return 0;
252 return (reg_val & (1u << off)) ? 1 : 0;
255 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
257 struct pca953x_chip *chip;
258 uint16_t reg_val;
259 int ret, offset = 0;
261 chip = container_of(gc, struct pca953x_chip, gpio_chip);
263 mutex_lock(&chip->i2c_lock);
264 if (val)
265 reg_val = chip->reg_output | (1u << off);
266 else
267 reg_val = chip->reg_output & ~(1u << off);
269 switch (chip->chip_type) {
270 case PCA953X_TYPE:
271 offset = PCA953X_OUTPUT;
272 break;
273 case PCA957X_TYPE:
274 offset = PCA957X_OUT;
275 break;
277 ret = pca953x_write_reg(chip, offset, reg_val);
278 if (ret)
279 goto exit;
281 chip->reg_output = reg_val;
282 exit:
283 mutex_unlock(&chip->i2c_lock);
286 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
288 struct gpio_chip *gc;
290 gc = &chip->gpio_chip;
292 gc->direction_input = pca953x_gpio_direction_input;
293 gc->direction_output = pca953x_gpio_direction_output;
294 gc->get = pca953x_gpio_get_value;
295 gc->set = pca953x_gpio_set_value;
296 gc->can_sleep = 1;
298 gc->base = chip->gpio_start;
299 gc->ngpio = gpios;
300 gc->label = chip->client->name;
301 gc->dev = &chip->client->dev;
302 gc->owner = THIS_MODULE;
303 gc->names = chip->names;
306 #ifdef CONFIG_GPIO_PCA953X_IRQ
307 static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
309 struct pca953x_chip *chip;
311 chip = container_of(gc, struct pca953x_chip, gpio_chip);
312 return chip->irq_base + off;
315 static void pca953x_irq_mask(struct irq_data *d)
317 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
319 chip->irq_mask &= ~(1 << (d->irq - chip->irq_base));
322 static void pca953x_irq_unmask(struct irq_data *d)
324 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
326 chip->irq_mask |= 1 << (d->irq - chip->irq_base);
329 static void pca953x_irq_bus_lock(struct irq_data *d)
331 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
333 mutex_lock(&chip->irq_lock);
336 static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
338 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
339 uint16_t new_irqs;
340 uint16_t level;
342 /* Look for any newly setup interrupt */
343 new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
344 new_irqs &= ~chip->reg_direction;
346 while (new_irqs) {
347 level = __ffs(new_irqs);
348 pca953x_gpio_direction_input(&chip->gpio_chip, level);
349 new_irqs &= ~(1 << level);
352 mutex_unlock(&chip->irq_lock);
355 static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
357 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
358 uint16_t level = d->irq - chip->irq_base;
359 uint16_t mask = 1 << level;
361 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
362 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
363 d->irq, type);
364 return -EINVAL;
367 if (type & IRQ_TYPE_EDGE_FALLING)
368 chip->irq_trig_fall |= mask;
369 else
370 chip->irq_trig_fall &= ~mask;
372 if (type & IRQ_TYPE_EDGE_RISING)
373 chip->irq_trig_raise |= mask;
374 else
375 chip->irq_trig_raise &= ~mask;
377 return 0;
380 static struct irq_chip pca953x_irq_chip = {
381 .name = "pca953x",
382 .irq_mask = pca953x_irq_mask,
383 .irq_unmask = pca953x_irq_unmask,
384 .irq_bus_lock = pca953x_irq_bus_lock,
385 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
386 .irq_set_type = pca953x_irq_set_type,
389 static uint16_t pca953x_irq_pending(struct pca953x_chip *chip)
391 uint16_t cur_stat;
392 uint16_t old_stat;
393 uint16_t pending;
394 uint16_t trigger;
395 int ret, offset = 0;
397 switch (chip->chip_type) {
398 case PCA953X_TYPE:
399 offset = PCA953X_INPUT;
400 break;
401 case PCA957X_TYPE:
402 offset = PCA957X_IN;
403 break;
405 ret = pca953x_read_reg(chip, offset, &cur_stat);
406 if (ret)
407 return 0;
409 /* Remove output pins from the equation */
410 cur_stat &= chip->reg_direction;
412 old_stat = chip->irq_stat;
413 trigger = (cur_stat ^ old_stat) & chip->irq_mask;
415 if (!trigger)
416 return 0;
418 chip->irq_stat = cur_stat;
420 pending = (old_stat & chip->irq_trig_fall) |
421 (cur_stat & chip->irq_trig_raise);
422 pending &= trigger;
424 return pending;
427 static irqreturn_t pca953x_irq_handler(int irq, void *devid)
429 struct pca953x_chip *chip = devid;
430 uint16_t pending;
431 uint16_t level;
433 pending = pca953x_irq_pending(chip);
435 if (!pending)
436 return IRQ_HANDLED;
438 do {
439 level = __ffs(pending);
440 generic_handle_irq(level + chip->irq_base);
442 pending &= ~(1 << level);
443 } while (pending);
445 return IRQ_HANDLED;
448 static int pca953x_irq_setup(struct pca953x_chip *chip,
449 const struct i2c_device_id *id)
451 struct i2c_client *client = chip->client;
452 struct pca953x_platform_data *pdata = client->dev.platform_data;
453 int ret, offset = 0;
455 if (pdata->irq_base != -1
456 && (id->driver_data & PCA_INT)) {
457 int lvl;
459 switch (chip->chip_type) {
460 case PCA953X_TYPE:
461 offset = PCA953X_INPUT;
462 break;
463 case PCA957X_TYPE:
464 offset = PCA957X_IN;
465 break;
467 ret = pca953x_read_reg(chip, offset, &chip->irq_stat);
468 if (ret)
469 goto out_failed;
472 * There is no way to know which GPIO line generated the
473 * interrupt. We have to rely on the previous read for
474 * this purpose.
476 chip->irq_stat &= chip->reg_direction;
477 chip->irq_base = pdata->irq_base;
478 mutex_init(&chip->irq_lock);
480 for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
481 int irq = lvl + chip->irq_base;
483 irq_set_chip_data(irq, chip);
484 irq_set_chip_and_handler(irq, &pca953x_irq_chip,
485 handle_simple_irq);
486 #ifdef CONFIG_ARM
487 set_irq_flags(irq, IRQF_VALID);
488 #else
489 irq_set_noprobe(irq);
490 #endif
493 ret = request_threaded_irq(client->irq,
494 NULL,
495 pca953x_irq_handler,
496 IRQF_TRIGGER_RISING |
497 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
498 dev_name(&client->dev), chip);
499 if (ret) {
500 dev_err(&client->dev, "failed to request irq %d\n",
501 client->irq);
502 goto out_failed;
505 chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
508 return 0;
510 out_failed:
511 chip->irq_base = -1;
512 return ret;
515 static void pca953x_irq_teardown(struct pca953x_chip *chip)
517 if (chip->irq_base != -1)
518 free_irq(chip->client->irq, chip);
520 #else /* CONFIG_GPIO_PCA953X_IRQ */
521 static int pca953x_irq_setup(struct pca953x_chip *chip,
522 const struct i2c_device_id *id)
524 struct i2c_client *client = chip->client;
525 struct pca953x_platform_data *pdata = client->dev.platform_data;
527 if (pdata->irq_base != -1 && (id->driver_data & PCA_INT))
528 dev_warn(&client->dev, "interrupt support not compiled in\n");
530 return 0;
533 static void pca953x_irq_teardown(struct pca953x_chip *chip)
536 #endif
539 * Handlers for alternative sources of platform_data
541 #ifdef CONFIG_OF_GPIO
543 * Translate OpenFirmware node properties into platform_data
545 static struct pca953x_platform_data *
546 pca953x_get_alt_pdata(struct i2c_client *client)
548 struct pca953x_platform_data *pdata;
549 struct device_node *node;
550 const __be32 *val;
551 int size;
553 node = client->dev.of_node;
554 if (node == NULL)
555 return NULL;
557 pdata = kzalloc(sizeof(struct pca953x_platform_data), GFP_KERNEL);
558 if (pdata == NULL) {
559 dev_err(&client->dev, "Unable to allocate platform_data\n");
560 return NULL;
563 pdata->gpio_base = -1;
564 val = of_get_property(node, "linux,gpio-base", &size);
565 if (val) {
566 if (size != sizeof(*val))
567 dev_warn(&client->dev, "%s: wrong linux,gpio-base\n",
568 node->full_name);
569 else
570 pdata->gpio_base = be32_to_cpup(val);
573 val = of_get_property(node, "polarity", NULL);
574 if (val)
575 pdata->invert = *val;
577 return pdata;
579 #else
580 static struct pca953x_platform_data *
581 pca953x_get_alt_pdata(struct i2c_client *client)
583 return NULL;
585 #endif
587 static int __devinit device_pca953x_init(struct pca953x_chip *chip, int invert)
589 int ret;
591 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
592 if (ret)
593 goto out;
595 ret = pca953x_read_reg(chip, PCA953X_DIRECTION,
596 &chip->reg_direction);
597 if (ret)
598 goto out;
600 /* set platform specific polarity inversion */
601 ret = pca953x_write_reg(chip, PCA953X_INVERT, invert);
602 if (ret)
603 goto out;
604 return 0;
605 out:
606 return ret;
609 static int __devinit device_pca957x_init(struct pca953x_chip *chip, int invert)
611 int ret;
612 uint16_t val = 0;
614 /* Let every port in proper state, that could save power */
615 pca953x_write_reg(chip, PCA957X_PUPD, 0x0);
616 pca953x_write_reg(chip, PCA957X_CFG, 0xffff);
617 pca953x_write_reg(chip, PCA957X_OUT, 0x0);
619 ret = pca953x_read_reg(chip, PCA957X_IN, &val);
620 if (ret)
621 goto out;
622 ret = pca953x_read_reg(chip, PCA957X_OUT, &chip->reg_output);
623 if (ret)
624 goto out;
625 ret = pca953x_read_reg(chip, PCA957X_CFG, &chip->reg_direction);
626 if (ret)
627 goto out;
629 /* set platform specific polarity inversion */
630 pca953x_write_reg(chip, PCA957X_INVRT, invert);
632 /* To enable register 6, 7 to controll pull up and pull down */
633 pca953x_write_reg(chip, PCA957X_BKEN, 0x202);
635 return 0;
636 out:
637 return ret;
640 static int __devinit pca953x_probe(struct i2c_client *client,
641 const struct i2c_device_id *id)
643 struct pca953x_platform_data *pdata;
644 struct pca953x_chip *chip;
645 int ret = 0;
647 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
648 if (chip == NULL)
649 return -ENOMEM;
651 pdata = client->dev.platform_data;
652 if (pdata == NULL) {
653 pdata = pca953x_get_alt_pdata(client);
655 * Unlike normal platform_data, this is allocated
656 * dynamically and must be freed in the driver
658 chip->dyn_pdata = pdata;
661 if (pdata == NULL) {
662 dev_dbg(&client->dev, "no platform data\n");
663 ret = -EINVAL;
664 goto out_failed;
667 chip->client = client;
669 chip->gpio_start = pdata->gpio_base;
671 chip->names = pdata->names;
672 chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE);
674 mutex_init(&chip->i2c_lock);
676 /* initialize cached registers from their original values.
677 * we can't share this chip with another i2c master.
679 pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK);
681 if (chip->chip_type == PCA953X_TYPE)
682 device_pca953x_init(chip, pdata->invert);
683 else if (chip->chip_type == PCA957X_TYPE)
684 device_pca957x_init(chip, pdata->invert);
685 else
686 goto out_failed;
688 ret = pca953x_irq_setup(chip, id);
689 if (ret)
690 goto out_failed;
692 ret = gpiochip_add(&chip->gpio_chip);
693 if (ret)
694 goto out_failed_irq;
696 if (pdata->setup) {
697 ret = pdata->setup(client, chip->gpio_chip.base,
698 chip->gpio_chip.ngpio, pdata->context);
699 if (ret < 0)
700 dev_warn(&client->dev, "setup failed, %d\n", ret);
703 i2c_set_clientdata(client, chip);
704 return 0;
706 out_failed_irq:
707 pca953x_irq_teardown(chip);
708 out_failed:
709 kfree(chip->dyn_pdata);
710 kfree(chip);
711 return ret;
714 static int pca953x_remove(struct i2c_client *client)
716 struct pca953x_platform_data *pdata = client->dev.platform_data;
717 struct pca953x_chip *chip = i2c_get_clientdata(client);
718 int ret = 0;
720 if (pdata->teardown) {
721 ret = pdata->teardown(client, chip->gpio_chip.base,
722 chip->gpio_chip.ngpio, pdata->context);
723 if (ret < 0) {
724 dev_err(&client->dev, "%s failed, %d\n",
725 "teardown", ret);
726 return ret;
730 ret = gpiochip_remove(&chip->gpio_chip);
731 if (ret) {
732 dev_err(&client->dev, "%s failed, %d\n",
733 "gpiochip_remove()", ret);
734 return ret;
737 pca953x_irq_teardown(chip);
738 kfree(chip->dyn_pdata);
739 kfree(chip);
740 return 0;
743 static struct i2c_driver pca953x_driver = {
744 .driver = {
745 .name = "pca953x",
747 .probe = pca953x_probe,
748 .remove = pca953x_remove,
749 .id_table = pca953x_id,
752 static int __init pca953x_init(void)
754 return i2c_add_driver(&pca953x_driver);
756 /* register after i2c postcore initcall and before
757 * subsys initcalls that may rely on these GPIOs
759 subsys_initcall(pca953x_init);
761 static void __exit pca953x_exit(void)
763 i2c_del_driver(&pca953x_driver);
765 module_exit(pca953x_exit);
767 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
768 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
769 MODULE_LICENSE("GPL");