14 #include <linux/module.h>
19 #include <linux/i2c.h>
21 #include <linux/slab.h>
26 #define PCA953X_INPUT 0
27 #define PCA953X_OUTPUT 1
28 #define PCA953X_INVERT 2
29 #define PCA953X_DIRECTION 3
31 #define REG_ADDR_AI 0x80
34 #define PCA957X_INVRT 1
35 #define PCA957X_BKEN 2
36 #define PCA957X_PUPD 3
40 #define PCA957X_INTS 7
42 #define PCA_GPIO_MASK 0x00FF
43 #define PCA_INT 0x0100
44 #define PCA953X_TYPE 0x1000
45 #define PCA957X_TYPE 0x2000
79 #ifdef CONFIG_GPIO_PCA953X_IRQ
120 (val & 0xff00) >> 8);
133 static int pca953x_read_reg(
struct pca953x_chip *chip,
int reg,
u32 *val)
161 static int pca953x_gpio_direction_input(
struct gpio_chip *
gc,
unsigned off)
180 ret = pca953x_write_reg(chip, offset, reg_val);
191 static int pca953x_gpio_direction_output(
struct gpio_chip *gc,
192 unsigned off,
int val)
215 ret = pca953x_write_reg(chip, offset, reg_val);
231 ret = pca953x_write_reg(chip, offset, reg_val);
242 static int pca953x_gpio_get_value(
struct gpio_chip *gc,
unsigned off)
259 ret = pca953x_read_reg(chip, offset, ®_val);
269 return (reg_val & (1
u << off)) ? 1 : 0;
272 static void pca953x_gpio_set_value(
struct gpio_chip *gc,
unsigned off,
int val)
294 ret = pca953x_write_reg(chip, offset, reg_val);
303 static void pca953x_setup_gpio(
struct pca953x_chip *chip,
int gpios)
305 struct gpio_chip *gc;
309 gc->direction_input = pca953x_gpio_direction_input;
310 gc->direction_output = pca953x_gpio_direction_output;
311 gc->get = pca953x_gpio_get_value;
312 gc->set = pca953x_gpio_set_value;
317 gc->label = chip->
client->name;
318 gc->dev = &chip->
client->dev;
320 gc->names = chip->
names;
323 #ifdef CONFIG_GPIO_PCA953X_IRQ
324 static int pca953x_gpio_to_irq(
struct gpio_chip *gc,
unsigned off)
329 return chip->irq_base + off;
332 static void pca953x_irq_mask(
struct irq_data *
d)
334 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
336 chip->irq_mask &= ~(1 << (d->
irq - chip->irq_base));
339 static void pca953x_irq_unmask(
struct irq_data *
d)
341 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
343 chip->irq_mask |= 1 << (d->
irq - chip->irq_base);
346 static void pca953x_irq_bus_lock(
struct irq_data *
d)
348 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
353 static void pca953x_irq_bus_sync_unlock(
struct irq_data *
d)
355 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
360 new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
364 level =
__ffs(new_irqs);
365 pca953x_gpio_direction_input(&chip->
gpio_chip, level);
366 new_irqs &= ~(1 <<
level);
372 static int pca953x_irq_set_type(
struct irq_data *d,
unsigned int type)
374 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
375 u32 level = d->
irq - chip->irq_base;
385 chip->irq_trig_fall |=
mask;
387 chip->irq_trig_fall &= ~mask;
390 chip->irq_trig_raise |=
mask;
392 chip->irq_trig_raise &= ~mask;
397 static struct irq_chip pca953x_irq_chip = {
399 .irq_mask = pca953x_irq_mask,
400 .irq_unmask = pca953x_irq_unmask,
401 .irq_bus_lock = pca953x_irq_bus_lock,
402 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
403 .irq_set_type = pca953x_irq_set_type,
422 ret = pca953x_read_reg(chip, offset, &cur_stat);
429 old_stat = chip->irq_stat;
430 trigger = (cur_stat ^ old_stat) & chip->irq_mask;
435 chip->irq_stat = cur_stat;
437 pending = (old_stat & chip->irq_trig_fall) |
438 (cur_stat & chip->irq_trig_raise);
450 pending = pca953x_irq_pending(chip);
456 level =
__ffs(pending);
459 pending &= ~(1 <<
level);
474 && (id->driver_data &
PCA_INT)) {
485 ret = pca953x_read_reg(chip, offset, &temporary);
486 chip->irq_stat = temporary;
498 chip->irq_base = irq_alloc_descs(-1, irq_base, chip->
gpio_chip.ngpio, -1);
499 if (chip->irq_base < 0)
502 for (lvl = 0; lvl < chip->
gpio_chip.ngpio; lvl++) {
503 int irq = lvl + chip->irq_base;
508 irq_set_nested_thread(irq,
true);
512 irq_set_noprobe(irq);
520 dev_name(&client->
dev), chip);
522 dev_err(&client->
dev,
"failed to request irq %d\n",
527 chip->
gpio_chip.to_irq = pca953x_gpio_to_irq;
537 static void pca953x_irq_teardown(
struct pca953x_chip *chip)
539 if (chip->irq_base != -1) {
551 if (irq_base != -1 && (id->driver_data &
PCA_INT))
552 dev_warn(&client->
dev,
"interrupt support not compiled in\n");
557 static void pca953x_irq_teardown(
struct pca953x_chip *chip)
565 #ifdef CONFIG_OF_GPIO
577 node = client->
dev.of_node;
583 WARN(val,
"%s: device-tree property 'linux,gpio-base' is deprecated!", __func__);
585 if (size !=
sizeof(*val))
586 dev_warn(&client->
dev,
"%s: wrong linux,gpio-base\n",
593 WARN(val,
"%s: device-tree property 'polarity' is deprecated!", __func__);
599 pca953x_get_alt_pdata(
struct i2c_client *client,
int *gpio_base,
u32 *invert)
634 ret = pca953x_read_reg(chip,
PCA957X_IN, &val);
668 pdata = client->
dev.platform_data;
675 pca953x_get_alt_pdata(client, &chip->
gpio_start, &invert);
676 #ifdef CONFIG_OF_GPIO
695 ret = device_pca953x_init(chip, invert);
697 ret = device_pca957x_init(chip, invert);
701 ret = pca953x_irq_setup(chip,
id, irq_base);
709 if (pdata && pdata->
setup) {
716 i2c_set_clientdata(client, chip);
720 pca953x_irq_teardown(chip);
726 static int pca953x_remove(
struct i2c_client *client)
745 "gpiochip_remove()", ret);
749 pca953x_irq_teardown(chip);
758 .probe = pca953x_probe,
759 .remove = pca953x_remove,
760 .id_table = pca953x_id,
763 static int __init pca953x_init(
void)
765 return i2c_add_driver(&pca953x_driver);
772 static void __exit pca953x_exit(
void)