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
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
, },
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
, },
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, }, */
57 MODULE_DEVICE_TABLE(i2c
, pca953x_id
);
62 uint16_t reg_direction
;
63 struct mutex i2c_lock
;
65 #ifdef CONFIG_GPIO_PCA953X_IRQ
66 struct mutex irq_lock
;
69 uint16_t irq_trig_raise
;
70 uint16_t irq_trig_fall
;
74 struct i2c_client
*client
;
75 struct pca953x_platform_data
*dyn_pdata
;
76 struct gpio_chip gpio_chip
;
77 const char *const *names
;
80 static int pca953x_write_reg(struct pca953x_chip
*chip
, int reg
, uint16_t val
)
84 if (chip
->gpio_chip
.ngpio
<= 8)
85 ret
= i2c_smbus_write_byte_data(chip
->client
, reg
, val
);
87 ret
= i2c_smbus_write_word_data(chip
->client
, reg
<< 1, val
);
90 dev_err(&chip
->client
->dev
, "failed writing register\n");
97 static int pca953x_read_reg(struct pca953x_chip
*chip
, int reg
, uint16_t *val
)
101 if (chip
->gpio_chip
.ngpio
<= 8)
102 ret
= i2c_smbus_read_byte_data(chip
->client
, reg
);
104 ret
= i2c_smbus_read_word_data(chip
->client
, reg
<< 1);
107 dev_err(&chip
->client
->dev
, "failed reading register\n");
111 *val
= (uint16_t)ret
;
115 static int pca953x_gpio_direction_input(struct gpio_chip
*gc
, unsigned off
)
117 struct pca953x_chip
*chip
;
121 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
123 mutex_lock(&chip
->i2c_lock
);
124 reg_val
= chip
->reg_direction
| (1u << off
);
125 ret
= pca953x_write_reg(chip
, PCA953X_DIRECTION
, reg_val
);
129 chip
->reg_direction
= reg_val
;
132 mutex_unlock(&chip
->i2c_lock
);
136 static int pca953x_gpio_direction_output(struct gpio_chip
*gc
,
137 unsigned off
, int val
)
139 struct pca953x_chip
*chip
;
143 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
145 mutex_lock(&chip
->i2c_lock
);
146 /* set output level */
148 reg_val
= chip
->reg_output
| (1u << off
);
150 reg_val
= chip
->reg_output
& ~(1u << off
);
152 ret
= pca953x_write_reg(chip
, PCA953X_OUTPUT
, reg_val
);
156 chip
->reg_output
= reg_val
;
159 reg_val
= chip
->reg_direction
& ~(1u << off
);
160 ret
= pca953x_write_reg(chip
, PCA953X_DIRECTION
, reg_val
);
164 chip
->reg_direction
= reg_val
;
167 mutex_unlock(&chip
->i2c_lock
);
171 static int pca953x_gpio_get_value(struct gpio_chip
*gc
, unsigned off
)
173 struct pca953x_chip
*chip
;
177 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
179 mutex_lock(&chip
->i2c_lock
);
180 ret
= pca953x_read_reg(chip
, PCA953X_INPUT
, ®_val
);
181 mutex_unlock(&chip
->i2c_lock
);
183 /* NOTE: diagnostic already emitted; that's all we should
184 * do unless gpio_*_value_cansleep() calls become different
185 * from their nonsleeping siblings (and report faults).
190 return (reg_val
& (1u << off
)) ? 1 : 0;
193 static void pca953x_gpio_set_value(struct gpio_chip
*gc
, unsigned off
, int val
)
195 struct pca953x_chip
*chip
;
199 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
201 mutex_lock(&chip
->i2c_lock
);
203 reg_val
= chip
->reg_output
| (1u << off
);
205 reg_val
= chip
->reg_output
& ~(1u << off
);
207 ret
= pca953x_write_reg(chip
, PCA953X_OUTPUT
, reg_val
);
211 chip
->reg_output
= reg_val
;
213 mutex_unlock(&chip
->i2c_lock
);
216 static void pca953x_setup_gpio(struct pca953x_chip
*chip
, int gpios
)
218 struct gpio_chip
*gc
;
220 gc
= &chip
->gpio_chip
;
222 gc
->direction_input
= pca953x_gpio_direction_input
;
223 gc
->direction_output
= pca953x_gpio_direction_output
;
224 gc
->get
= pca953x_gpio_get_value
;
225 gc
->set
= pca953x_gpio_set_value
;
228 gc
->base
= chip
->gpio_start
;
230 gc
->label
= chip
->client
->name
;
231 gc
->dev
= &chip
->client
->dev
;
232 gc
->owner
= THIS_MODULE
;
233 gc
->names
= chip
->names
;
236 #ifdef CONFIG_GPIO_PCA953X_IRQ
237 static int pca953x_gpio_to_irq(struct gpio_chip
*gc
, unsigned off
)
239 struct pca953x_chip
*chip
;
241 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
242 return chip
->irq_base
+ off
;
245 static void pca953x_irq_mask(struct irq_data
*d
)
247 struct pca953x_chip
*chip
= irq_data_get_irq_chip_data(d
);
249 chip
->irq_mask
&= ~(1 << (d
->irq
- chip
->irq_base
));
252 static void pca953x_irq_unmask(struct irq_data
*d
)
254 struct pca953x_chip
*chip
= irq_data_get_irq_chip_data(d
);
256 chip
->irq_mask
|= 1 << (d
->irq
- chip
->irq_base
);
259 static void pca953x_irq_bus_lock(struct irq_data
*d
)
261 struct pca953x_chip
*chip
= irq_data_get_irq_chip_data(d
);
263 mutex_lock(&chip
->irq_lock
);
266 static void pca953x_irq_bus_sync_unlock(struct irq_data
*d
)
268 struct pca953x_chip
*chip
= irq_data_get_irq_chip_data(d
);
272 /* Look for any newly setup interrupt */
273 new_irqs
= chip
->irq_trig_fall
| chip
->irq_trig_raise
;
274 new_irqs
&= ~chip
->reg_direction
;
277 level
= __ffs(new_irqs
);
278 pca953x_gpio_direction_input(&chip
->gpio_chip
, level
);
279 new_irqs
&= ~(1 << level
);
282 mutex_unlock(&chip
->irq_lock
);
285 static int pca953x_irq_set_type(struct irq_data
*d
, unsigned int type
)
287 struct pca953x_chip
*chip
= irq_data_get_irq_chip_data(d
);
288 uint16_t level
= d
->irq
- chip
->irq_base
;
289 uint16_t mask
= 1 << level
;
291 if (!(type
& IRQ_TYPE_EDGE_BOTH
)) {
292 dev_err(&chip
->client
->dev
, "irq %d: unsupported type %d\n",
297 if (type
& IRQ_TYPE_EDGE_FALLING
)
298 chip
->irq_trig_fall
|= mask
;
300 chip
->irq_trig_fall
&= ~mask
;
302 if (type
& IRQ_TYPE_EDGE_RISING
)
303 chip
->irq_trig_raise
|= mask
;
305 chip
->irq_trig_raise
&= ~mask
;
310 static struct irq_chip pca953x_irq_chip
= {
312 .irq_mask
= pca953x_irq_mask
,
313 .irq_unmask
= pca953x_irq_unmask
,
314 .irq_bus_lock
= pca953x_irq_bus_lock
,
315 .irq_bus_sync_unlock
= pca953x_irq_bus_sync_unlock
,
316 .irq_set_type
= pca953x_irq_set_type
,
319 static uint16_t pca953x_irq_pending(struct pca953x_chip
*chip
)
327 ret
= pca953x_read_reg(chip
, PCA953X_INPUT
, &cur_stat
);
331 /* Remove output pins from the equation */
332 cur_stat
&= chip
->reg_direction
;
334 old_stat
= chip
->irq_stat
;
335 trigger
= (cur_stat
^ old_stat
) & chip
->irq_mask
;
340 chip
->irq_stat
= cur_stat
;
342 pending
= (old_stat
& chip
->irq_trig_fall
) |
343 (cur_stat
& chip
->irq_trig_raise
);
349 static irqreturn_t
pca953x_irq_handler(int irq
, void *devid
)
351 struct pca953x_chip
*chip
= devid
;
355 pending
= pca953x_irq_pending(chip
);
361 level
= __ffs(pending
);
362 generic_handle_irq(level
+ chip
->irq_base
);
364 pending
&= ~(1 << level
);
370 static int pca953x_irq_setup(struct pca953x_chip
*chip
,
371 const struct i2c_device_id
*id
)
373 struct i2c_client
*client
= chip
->client
;
374 struct pca953x_platform_data
*pdata
= client
->dev
.platform_data
;
377 if (pdata
->irq_base
!= -1
378 && (id
->driver_data
& PCA953X_INT
)) {
381 ret
= pca953x_read_reg(chip
, PCA953X_INPUT
,
387 * There is no way to know which GPIO line generated the
388 * interrupt. We have to rely on the previous read for
391 chip
->irq_stat
&= chip
->reg_direction
;
392 chip
->irq_base
= pdata
->irq_base
;
393 mutex_init(&chip
->irq_lock
);
395 for (lvl
= 0; lvl
< chip
->gpio_chip
.ngpio
; lvl
++) {
396 int irq
= lvl
+ chip
->irq_base
;
398 irq_set_chip_data(irq
, chip
);
399 irq_set_chip_and_handler(irq
, &pca953x_irq_chip
,
402 set_irq_flags(irq
, IRQF_VALID
);
404 irq_set_noprobe(irq
);
408 ret
= request_threaded_irq(client
->irq
,
411 IRQF_TRIGGER_RISING
|
412 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
413 dev_name(&client
->dev
), chip
);
415 dev_err(&client
->dev
, "failed to request irq %d\n",
420 chip
->gpio_chip
.to_irq
= pca953x_gpio_to_irq
;
430 static void pca953x_irq_teardown(struct pca953x_chip
*chip
)
432 if (chip
->irq_base
!= -1)
433 free_irq(chip
->client
->irq
, chip
);
435 #else /* CONFIG_GPIO_PCA953X_IRQ */
436 static int pca953x_irq_setup(struct pca953x_chip
*chip
,
437 const struct i2c_device_id
*id
)
439 struct i2c_client
*client
= chip
->client
;
440 struct pca953x_platform_data
*pdata
= client
->dev
.platform_data
;
442 if (pdata
->irq_base
!= -1 && (id
->driver_data
& PCA953X_INT
))
443 dev_warn(&client
->dev
, "interrupt support not compiled in\n");
448 static void pca953x_irq_teardown(struct pca953x_chip
*chip
)
454 * Handlers for alternative sources of platform_data
456 #ifdef CONFIG_OF_GPIO
458 * Translate OpenFirmware node properties into platform_data
460 static struct pca953x_platform_data
*
461 pca953x_get_alt_pdata(struct i2c_client
*client
)
463 struct pca953x_platform_data
*pdata
;
464 struct device_node
*node
;
468 node
= client
->dev
.of_node
;
472 pdata
= kzalloc(sizeof(struct pca953x_platform_data
), GFP_KERNEL
);
474 dev_err(&client
->dev
, "Unable to allocate platform_data\n");
478 pdata
->gpio_base
= -1;
479 val
= of_get_property(node
, "linux,gpio-base", &size
);
481 if (size
!= sizeof(*val
))
482 dev_warn(&client
->dev
, "%s: wrong linux,gpio-base\n",
485 pdata
->gpio_base
= be32_to_cpup(val
);
488 val
= of_get_property(node
, "polarity", NULL
);
490 pdata
->invert
= *val
;
495 static struct pca953x_platform_data
*
496 pca953x_get_alt_pdata(struct i2c_client
*client
)
502 static int __devinit
pca953x_probe(struct i2c_client
*client
,
503 const struct i2c_device_id
*id
)
505 struct pca953x_platform_data
*pdata
;
506 struct pca953x_chip
*chip
;
509 chip
= kzalloc(sizeof(struct pca953x_chip
), GFP_KERNEL
);
513 pdata
= client
->dev
.platform_data
;
515 pdata
= pca953x_get_alt_pdata(client
);
517 * Unlike normal platform_data, this is allocated
518 * dynamically and must be freed in the driver
520 chip
->dyn_pdata
= pdata
;
524 dev_dbg(&client
->dev
, "no platform data\n");
529 chip
->client
= client
;
531 chip
->gpio_start
= pdata
->gpio_base
;
533 chip
->names
= pdata
->names
;
535 mutex_init(&chip
->i2c_lock
);
537 /* initialize cached registers from their original values.
538 * we can't share this chip with another i2c master.
540 pca953x_setup_gpio(chip
, id
->driver_data
& PCA953X_GPIOS
);
542 ret
= pca953x_read_reg(chip
, PCA953X_OUTPUT
, &chip
->reg_output
);
546 ret
= pca953x_read_reg(chip
, PCA953X_DIRECTION
, &chip
->reg_direction
);
550 /* set platform specific polarity inversion */
551 ret
= pca953x_write_reg(chip
, PCA953X_INVERT
, pdata
->invert
);
555 ret
= pca953x_irq_setup(chip
, id
);
559 ret
= gpiochip_add(&chip
->gpio_chip
);
564 ret
= pdata
->setup(client
, chip
->gpio_chip
.base
,
565 chip
->gpio_chip
.ngpio
, pdata
->context
);
567 dev_warn(&client
->dev
, "setup failed, %d\n", ret
);
570 i2c_set_clientdata(client
, chip
);
574 pca953x_irq_teardown(chip
);
575 kfree(chip
->dyn_pdata
);
580 static int pca953x_remove(struct i2c_client
*client
)
582 struct pca953x_platform_data
*pdata
= client
->dev
.platform_data
;
583 struct pca953x_chip
*chip
= i2c_get_clientdata(client
);
586 if (pdata
->teardown
) {
587 ret
= pdata
->teardown(client
, chip
->gpio_chip
.base
,
588 chip
->gpio_chip
.ngpio
, pdata
->context
);
590 dev_err(&client
->dev
, "%s failed, %d\n",
596 ret
= gpiochip_remove(&chip
->gpio_chip
);
598 dev_err(&client
->dev
, "%s failed, %d\n",
599 "gpiochip_remove()", ret
);
603 pca953x_irq_teardown(chip
);
604 kfree(chip
->dyn_pdata
);
609 static struct i2c_driver pca953x_driver
= {
613 .probe
= pca953x_probe
,
614 .remove
= pca953x_remove
,
615 .id_table
= pca953x_id
,
618 static int __init
pca953x_init(void)
620 return i2c_add_driver(&pca953x_driver
);
622 /* register after i2c postcore initcall and before
623 * subsys initcalls that may rely on these GPIOs
625 subsys_initcall(pca953x_init
);
627 static void __exit
pca953x_exit(void)
629 i2c_del_driver(&pca953x_driver
);
631 module_exit(pca953x_exit
);
633 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
634 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
635 MODULE_LICENSE("GPL");