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>
23 #include <linux/of_platform.h>
24 #include <linux/of_gpio.h>
27 #define PCA953X_INPUT 0
28 #define PCA953X_OUTPUT 1
29 #define PCA953X_INVERT 2
30 #define PCA953X_DIRECTION 3
33 #define PCA957X_INVRT 1
34 #define PCA957X_BKEN 2
35 #define PCA957X_PUPD 3
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, }, */
70 MODULE_DEVICE_TABLE(i2c
, pca953x_id
);
75 uint16_t reg_direction
;
76 struct mutex i2c_lock
;
78 #ifdef CONFIG_GPIO_PCA953X_IRQ
79 struct mutex irq_lock
;
82 uint16_t irq_trig_raise
;
83 uint16_t irq_trig_fall
;
87 struct i2c_client
*client
;
88 struct pca953x_platform_data
*dyn_pdata
;
89 struct gpio_chip gpio_chip
;
90 const char *const *names
;
94 static int pca953x_write_reg(struct pca953x_chip
*chip
, int reg
, uint16_t val
)
98 if (chip
->gpio_chip
.ngpio
<= 8)
99 ret
= i2c_smbus_write_byte_data(chip
->client
, reg
, val
);
101 switch (chip
->chip_type
) {
103 ret
= i2c_smbus_write_word_data(chip
->client
,
107 ret
= i2c_smbus_write_byte_data(chip
->client
, reg
<< 1,
111 ret
= i2c_smbus_write_byte_data(chip
->client
,
113 (val
& 0xff00) >> 8);
119 dev_err(&chip
->client
->dev
, "failed writing register\n");
126 static int pca953x_read_reg(struct pca953x_chip
*chip
, int reg
, uint16_t *val
)
130 if (chip
->gpio_chip
.ngpio
<= 8)
131 ret
= i2c_smbus_read_byte_data(chip
->client
, reg
);
133 ret
= i2c_smbus_read_word_data(chip
->client
, reg
<< 1);
136 dev_err(&chip
->client
->dev
, "failed reading register\n");
140 *val
= (uint16_t)ret
;
144 static int pca953x_gpio_direction_input(struct gpio_chip
*gc
, unsigned off
)
146 struct pca953x_chip
*chip
;
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
) {
157 offset
= PCA953X_DIRECTION
;
160 offset
= PCA957X_CFG
;
163 ret
= pca953x_write_reg(chip
, offset
, reg_val
);
167 chip
->reg_direction
= reg_val
;
170 mutex_unlock(&chip
->i2c_lock
);
174 static int pca953x_gpio_direction_output(struct gpio_chip
*gc
,
175 unsigned off
, int val
)
177 struct pca953x_chip
*chip
;
181 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
183 mutex_lock(&chip
->i2c_lock
);
184 /* set output level */
186 reg_val
= chip
->reg_output
| (1u << off
);
188 reg_val
= chip
->reg_output
& ~(1u << off
);
190 switch (chip
->chip_type
) {
192 offset
= PCA953X_OUTPUT
;
195 offset
= PCA957X_OUT
;
198 ret
= pca953x_write_reg(chip
, offset
, reg_val
);
202 chip
->reg_output
= reg_val
;
205 reg_val
= chip
->reg_direction
& ~(1u << off
);
206 switch (chip
->chip_type
) {
208 offset
= PCA953X_DIRECTION
;
211 offset
= PCA957X_CFG
;
214 ret
= pca953x_write_reg(chip
, offset
, reg_val
);
218 chip
->reg_direction
= reg_val
;
221 mutex_unlock(&chip
->i2c_lock
);
225 static int pca953x_gpio_get_value(struct gpio_chip
*gc
, unsigned off
)
227 struct pca953x_chip
*chip
;
231 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
233 mutex_lock(&chip
->i2c_lock
);
234 switch (chip
->chip_type
) {
236 offset
= PCA953X_INPUT
;
242 ret
= pca953x_read_reg(chip
, offset
, ®_val
);
243 mutex_unlock(&chip
->i2c_lock
);
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).
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
;
261 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
263 mutex_lock(&chip
->i2c_lock
);
265 reg_val
= chip
->reg_output
| (1u << off
);
267 reg_val
= chip
->reg_output
& ~(1u << off
);
269 switch (chip
->chip_type
) {
271 offset
= PCA953X_OUTPUT
;
274 offset
= PCA957X_OUT
;
277 ret
= pca953x_write_reg(chip
, offset
, reg_val
);
281 chip
->reg_output
= reg_val
;
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
;
298 gc
->base
= chip
->gpio_start
;
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
);
342 /* Look for any newly setup interrupt */
343 new_irqs
= chip
->irq_trig_fall
| chip
->irq_trig_raise
;
344 new_irqs
&= ~chip
->reg_direction
;
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",
367 if (type
& IRQ_TYPE_EDGE_FALLING
)
368 chip
->irq_trig_fall
|= mask
;
370 chip
->irq_trig_fall
&= ~mask
;
372 if (type
& IRQ_TYPE_EDGE_RISING
)
373 chip
->irq_trig_raise
|= mask
;
375 chip
->irq_trig_raise
&= ~mask
;
380 static struct irq_chip pca953x_irq_chip
= {
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
)
397 switch (chip
->chip_type
) {
399 offset
= PCA953X_INPUT
;
405 ret
= pca953x_read_reg(chip
, offset
, &cur_stat
);
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
;
418 chip
->irq_stat
= cur_stat
;
420 pending
= (old_stat
& chip
->irq_trig_fall
) |
421 (cur_stat
& chip
->irq_trig_raise
);
427 static irqreturn_t
pca953x_irq_handler(int irq
, void *devid
)
429 struct pca953x_chip
*chip
= devid
;
433 pending
= pca953x_irq_pending(chip
);
439 level
= __ffs(pending
);
440 handle_nested_irq(level
+ chip
->irq_base
);
442 pending
&= ~(1 << level
);
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
;
455 if (pdata
->irq_base
!= -1
456 && (id
->driver_data
& PCA_INT
)) {
459 switch (chip
->chip_type
) {
461 offset
= PCA953X_INPUT
;
467 ret
= pca953x_read_reg(chip
, offset
, &chip
->irq_stat
);
472 * There is no way to know which GPIO line generated the
473 * interrupt. We have to rely on the previous read for
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(irq
, &pca953x_irq_chip
);
485 irq_set_nested_thread(irq
, true);
487 set_irq_flags(irq
, IRQF_VALID
);
489 irq_set_noprobe(irq
);
493 ret
= request_threaded_irq(client
->irq
,
496 IRQF_TRIGGER_RISING
|
497 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
498 dev_name(&client
->dev
), chip
);
500 dev_err(&client
->dev
, "failed to request irq %d\n",
505 chip
->gpio_chip
.to_irq
= pca953x_gpio_to_irq
;
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");
533 static void pca953x_irq_teardown(struct pca953x_chip
*chip
)
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
;
553 node
= client
->dev
.of_node
;
557 pdata
= kzalloc(sizeof(struct pca953x_platform_data
), GFP_KERNEL
);
559 dev_err(&client
->dev
, "Unable to allocate platform_data\n");
563 pdata
->gpio_base
= -1;
564 val
= of_get_property(node
, "linux,gpio-base", &size
);
566 if (size
!= sizeof(*val
))
567 dev_warn(&client
->dev
, "%s: wrong linux,gpio-base\n",
570 pdata
->gpio_base
= be32_to_cpup(val
);
573 val
= of_get_property(node
, "polarity", NULL
);
575 pdata
->invert
= *val
;
580 static struct pca953x_platform_data
*
581 pca953x_get_alt_pdata(struct i2c_client
*client
)
587 static int __devinit
device_pca953x_init(struct pca953x_chip
*chip
, int invert
)
591 ret
= pca953x_read_reg(chip
, PCA953X_OUTPUT
, &chip
->reg_output
);
595 ret
= pca953x_read_reg(chip
, PCA953X_DIRECTION
,
596 &chip
->reg_direction
);
600 /* set platform specific polarity inversion */
601 ret
= pca953x_write_reg(chip
, PCA953X_INVERT
, invert
);
609 static int __devinit
device_pca957x_init(struct pca953x_chip
*chip
, int invert
)
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
);
622 ret
= pca953x_read_reg(chip
, PCA957X_OUT
, &chip
->reg_output
);
625 ret
= pca953x_read_reg(chip
, PCA957X_CFG
, &chip
->reg_direction
);
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);
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
;
647 chip
= kzalloc(sizeof(struct pca953x_chip
), GFP_KERNEL
);
651 pdata
= client
->dev
.platform_data
;
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
;
662 dev_dbg(&client
->dev
, "no platform data\n");
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
);
688 ret
= pca953x_irq_setup(chip
, id
);
692 ret
= gpiochip_add(&chip
->gpio_chip
);
697 ret
= pdata
->setup(client
, chip
->gpio_chip
.base
,
698 chip
->gpio_chip
.ngpio
, pdata
->context
);
700 dev_warn(&client
->dev
, "setup failed, %d\n", ret
);
703 i2c_set_clientdata(client
, chip
);
707 pca953x_irq_teardown(chip
);
709 kfree(chip
->dyn_pdata
);
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
);
720 if (pdata
->teardown
) {
721 ret
= pdata
->teardown(client
, chip
->gpio_chip
.base
,
722 chip
->gpio_chip
.ngpio
, pdata
->context
);
724 dev_err(&client
->dev
, "%s failed, %d\n",
730 ret
= gpiochip_remove(&chip
->gpio_chip
);
732 dev_err(&client
->dev
, "%s failed, %d\n",
733 "gpiochip_remove()", ret
);
737 pca953x_irq_teardown(chip
);
738 kfree(chip
->dyn_pdata
);
743 static struct i2c_driver pca953x_driver
= {
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");