eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1 | /* |
Aaron Sierra | 1e19169 | 2014-02-07 16:35:48 -0600 | [diff] [blame] | 2 | * PCA953x 4/8/16/24/40 bit I/O ports |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com> |
| 5 | * Copyright (C) 2007 Marvell International Ltd. |
| 6 | * |
| 7 | * Derived from drivers/i2c/chips/pca9539.c |
| 8 | * |
| 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. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/init.h> |
H Hartley Sweeten | d120c17 | 2009-09-22 16:46:37 -0700 | [diff] [blame] | 16 | #include <linux/gpio.h> |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 17 | #include <linux/interrupt.h> |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 18 | #include <linux/i2c.h> |
Vivien Didelot | 5877457 | 2013-08-29 15:24:14 -0400 | [diff] [blame] | 19 | #include <linux/platform_data/pca953x.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 21 | #include <linux/of_platform.h> |
Andy Shevchenko | f32517b | 2015-10-01 14:20:28 +0300 | [diff] [blame] | 22 | #include <linux/acpi.h> |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 23 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 24 | #define PCA953X_INPUT 0 |
| 25 | #define PCA953X_OUTPUT 1 |
| 26 | #define PCA953X_INVERT 2 |
| 27 | #define PCA953X_DIRECTION 3 |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 28 | |
Andreas Schallenberg | ae79c19 | 2012-05-09 09:46:17 +0200 | [diff] [blame] | 29 | #define REG_ADDR_AI 0x80 |
| 30 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 31 | #define PCA957X_IN 0 |
| 32 | #define PCA957X_INVRT 1 |
| 33 | #define PCA957X_BKEN 2 |
| 34 | #define PCA957X_PUPD 3 |
| 35 | #define PCA957X_CFG 4 |
| 36 | #define PCA957X_OUT 5 |
| 37 | #define PCA957X_MSK 6 |
| 38 | #define PCA957X_INTS 7 |
| 39 | |
| 40 | #define PCA_GPIO_MASK 0x00FF |
| 41 | #define PCA_INT 0x0100 |
| 42 | #define PCA953X_TYPE 0x1000 |
| 43 | #define PCA957X_TYPE 0x2000 |
Andy Shevchenko | c666414 | 2015-10-01 14:20:27 +0300 | [diff] [blame] | 44 | #define PCA_TYPE_MASK 0xF000 |
| 45 | |
| 46 | #define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 47 | |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 48 | static const struct i2c_device_id pca953x_id[] = { |
Gregory CLEMENT | 89f5df0 | 2013-01-22 22:10:24 +0100 | [diff] [blame] | 49 | { "pca9505", 40 | PCA953X_TYPE | PCA_INT, }, |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 50 | { "pca9534", 8 | PCA953X_TYPE | PCA_INT, }, |
| 51 | { "pca9535", 16 | PCA953X_TYPE | PCA_INT, }, |
| 52 | { "pca9536", 4 | PCA953X_TYPE, }, |
| 53 | { "pca9537", 4 | PCA953X_TYPE | PCA_INT, }, |
| 54 | { "pca9538", 8 | PCA953X_TYPE | PCA_INT, }, |
| 55 | { "pca9539", 16 | PCA953X_TYPE | PCA_INT, }, |
| 56 | { "pca9554", 8 | PCA953X_TYPE | PCA_INT, }, |
| 57 | { "pca9555", 16 | PCA953X_TYPE | PCA_INT, }, |
| 58 | { "pca9556", 8 | PCA953X_TYPE, }, |
| 59 | { "pca9557", 8 | PCA953X_TYPE, }, |
| 60 | { "pca9574", 8 | PCA957X_TYPE | PCA_INT, }, |
| 61 | { "pca9575", 16 | PCA957X_TYPE | PCA_INT, }, |
Aaron Sierra | eb32b5a | 2014-02-13 13:59:23 +0100 | [diff] [blame] | 62 | { "pca9698", 40 | PCA953X_TYPE, }, |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 63 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 64 | { "max7310", 8 | PCA953X_TYPE, }, |
| 65 | { "max7312", 16 | PCA953X_TYPE | PCA_INT, }, |
| 66 | { "max7313", 16 | PCA953X_TYPE | PCA_INT, }, |
| 67 | { "max7315", 8 | PCA953X_TYPE | PCA_INT, }, |
| 68 | { "pca6107", 8 | PCA953X_TYPE | PCA_INT, }, |
| 69 | { "tca6408", 8 | PCA953X_TYPE | PCA_INT, }, |
| 70 | { "tca6416", 16 | PCA953X_TYPE | PCA_INT, }, |
Andreas Schallenberg | ae79c19 | 2012-05-09 09:46:17 +0200 | [diff] [blame] | 71 | { "tca6424", 24 | PCA953X_TYPE | PCA_INT, }, |
Thierry Reding | 2db8aba | 2015-09-29 12:55:44 +0200 | [diff] [blame] | 72 | { "tca9539", 16 | PCA953X_TYPE | PCA_INT, }, |
Aaron Sierra | e73760a | 2014-02-07 16:36:21 -0600 | [diff] [blame] | 73 | { "xra1202", 8 | PCA953X_TYPE }, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 74 | { } |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 75 | }; |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 76 | MODULE_DEVICE_TABLE(i2c, pca953x_id); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 77 | |
Andy Shevchenko | f32517b | 2015-10-01 14:20:28 +0300 | [diff] [blame] | 78 | static const struct acpi_device_id pca953x_acpi_ids[] = { |
| 79 | { "INT3491", 16 | PCA953X_TYPE | PCA_INT, }, |
| 80 | { } |
| 81 | }; |
| 82 | MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids); |
| 83 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 84 | #define MAX_BANK 5 |
| 85 | #define BANK_SZ 8 |
| 86 | |
| 87 | #define NBANK(chip) (chip->gpio_chip.ngpio / BANK_SZ) |
| 88 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 89 | struct pca953x_chip { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 90 | unsigned gpio_start; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 91 | u8 reg_output[MAX_BANK]; |
| 92 | u8 reg_direction[MAX_BANK]; |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 93 | struct mutex i2c_lock; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 94 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 95 | #ifdef CONFIG_GPIO_PCA953X_IRQ |
| 96 | struct mutex irq_lock; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 97 | u8 irq_mask[MAX_BANK]; |
| 98 | u8 irq_stat[MAX_BANK]; |
| 99 | u8 irq_trig_raise[MAX_BANK]; |
| 100 | u8 irq_trig_fall[MAX_BANK]; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 101 | #endif |
| 102 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 103 | struct i2c_client *client; |
| 104 | struct gpio_chip gpio_chip; |
Uwe Kleine-König | 6215499 | 2010-05-26 14:42:17 -0700 | [diff] [blame] | 105 | const char *const *names; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 106 | int chip_type; |
Andy Shevchenko | c666414 | 2015-10-01 14:20:27 +0300 | [diff] [blame] | 107 | unsigned long driver_data; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 110 | static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val, |
| 111 | int off) |
| 112 | { |
| 113 | int ret; |
| 114 | int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ); |
| 115 | int offset = off / BANK_SZ; |
| 116 | |
| 117 | ret = i2c_smbus_read_byte_data(chip->client, |
| 118 | (reg << bank_shift) + offset); |
| 119 | *val = ret; |
| 120 | |
| 121 | if (ret < 0) { |
| 122 | dev_err(&chip->client->dev, "failed reading register\n"); |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val, |
| 130 | int off) |
| 131 | { |
| 132 | int ret = 0; |
| 133 | int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ); |
| 134 | int offset = off / BANK_SZ; |
| 135 | |
| 136 | ret = i2c_smbus_write_byte_data(chip->client, |
| 137 | (reg << bank_shift) + offset, val); |
| 138 | |
| 139 | if (ret < 0) { |
| 140 | dev_err(&chip->client->dev, "failed writing register\n"); |
| 141 | return ret; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 148 | { |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 149 | int ret = 0; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 150 | |
| 151 | if (chip->gpio_chip.ngpio <= 8) |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 152 | ret = i2c_smbus_write_byte_data(chip->client, reg, *val); |
| 153 | else if (chip->gpio_chip.ngpio >= 24) { |
| 154 | int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ); |
Andreas Schallenberg | 96b7064 | 2012-05-21 09:52:43 +0200 | [diff] [blame] | 155 | ret = i2c_smbus_write_i2c_block_data(chip->client, |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 156 | (reg << bank_shift) | REG_ADDR_AI, |
| 157 | NBANK(chip), val); |
Laurent Navet | 50e4443 | 2013-03-20 13:15:58 +0100 | [diff] [blame] | 158 | } else { |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 159 | switch (chip->chip_type) { |
| 160 | case PCA953X_TYPE: |
| 161 | ret = i2c_smbus_write_word_data(chip->client, |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 162 | reg << 1, (u16) *val); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 163 | break; |
| 164 | case PCA957X_TYPE: |
| 165 | ret = i2c_smbus_write_byte_data(chip->client, reg << 1, |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 166 | val[0]); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 167 | if (ret < 0) |
| 168 | break; |
| 169 | ret = i2c_smbus_write_byte_data(chip->client, |
| 170 | (reg << 1) + 1, |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 171 | val[1]); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 172 | break; |
| 173 | } |
| 174 | } |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 175 | |
| 176 | if (ret < 0) { |
| 177 | dev_err(&chip->client->dev, "failed writing register\n"); |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 178 | return ret; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | return 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 184 | static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 185 | { |
| 186 | int ret; |
| 187 | |
Andreas Schallenberg | 96b7064 | 2012-05-21 09:52:43 +0200 | [diff] [blame] | 188 | if (chip->gpio_chip.ngpio <= 8) { |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 189 | ret = i2c_smbus_read_byte_data(chip->client, reg); |
Andreas Schallenberg | 96b7064 | 2012-05-21 09:52:43 +0200 | [diff] [blame] | 190 | *val = ret; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 191 | } else if (chip->gpio_chip.ngpio >= 24) { |
| 192 | int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ); |
| 193 | |
Andreas Schallenberg | 96b7064 | 2012-05-21 09:52:43 +0200 | [diff] [blame] | 194 | ret = i2c_smbus_read_i2c_block_data(chip->client, |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 195 | (reg << bank_shift) | REG_ADDR_AI, |
| 196 | NBANK(chip), val); |
Andreas Schallenberg | 96b7064 | 2012-05-21 09:52:43 +0200 | [diff] [blame] | 197 | } else { |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 198 | ret = i2c_smbus_read_word_data(chip->client, reg << 1); |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 199 | val[0] = (u16)ret & 0xFF; |
| 200 | val[1] = (u16)ret >> 8; |
Andreas Schallenberg | 96b7064 | 2012-05-21 09:52:43 +0200 | [diff] [blame] | 201 | } |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 202 | if (ret < 0) { |
| 203 | dev_err(&chip->client->dev, "failed reading register\n"); |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 204 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 205 | } |
| 206 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 210 | static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 211 | { |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 212 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 213 | u8 reg_val; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 214 | int ret, offset = 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 215 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 216 | mutex_lock(&chip->i2c_lock); |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 217 | reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ)); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 218 | |
| 219 | switch (chip->chip_type) { |
| 220 | case PCA953X_TYPE: |
| 221 | offset = PCA953X_DIRECTION; |
| 222 | break; |
| 223 | case PCA957X_TYPE: |
| 224 | offset = PCA957X_CFG; |
| 225 | break; |
| 226 | } |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 227 | ret = pca953x_write_single(chip, offset, reg_val, off); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 228 | if (ret) |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 229 | goto exit; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 230 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 231 | chip->reg_direction[off / BANK_SZ] = reg_val; |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 232 | ret = 0; |
| 233 | exit: |
| 234 | mutex_unlock(&chip->i2c_lock); |
| 235 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 238 | static int pca953x_gpio_direction_output(struct gpio_chip *gc, |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 239 | unsigned off, int val) |
| 240 | { |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 241 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 242 | u8 reg_val; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 243 | int ret, offset = 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 244 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 245 | mutex_lock(&chip->i2c_lock); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 246 | /* set output level */ |
| 247 | if (val) |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 248 | reg_val = chip->reg_output[off / BANK_SZ] |
| 249 | | (1u << (off % BANK_SZ)); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 250 | else |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 251 | reg_val = chip->reg_output[off / BANK_SZ] |
| 252 | & ~(1u << (off % BANK_SZ)); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 253 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 254 | switch (chip->chip_type) { |
| 255 | case PCA953X_TYPE: |
| 256 | offset = PCA953X_OUTPUT; |
| 257 | break; |
| 258 | case PCA957X_TYPE: |
| 259 | offset = PCA957X_OUT; |
| 260 | break; |
| 261 | } |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 262 | ret = pca953x_write_single(chip, offset, reg_val, off); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 263 | if (ret) |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 264 | goto exit; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 265 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 266 | chip->reg_output[off / BANK_SZ] = reg_val; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 267 | |
| 268 | /* then direction */ |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 269 | reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ)); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 270 | switch (chip->chip_type) { |
| 271 | case PCA953X_TYPE: |
| 272 | offset = PCA953X_DIRECTION; |
| 273 | break; |
| 274 | case PCA957X_TYPE: |
| 275 | offset = PCA957X_CFG; |
| 276 | break; |
| 277 | } |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 278 | ret = pca953x_write_single(chip, offset, reg_val, off); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 279 | if (ret) |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 280 | goto exit; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 281 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 282 | chip->reg_direction[off / BANK_SZ] = reg_val; |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 283 | ret = 0; |
| 284 | exit: |
| 285 | mutex_unlock(&chip->i2c_lock); |
| 286 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 289 | static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 290 | { |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 291 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Andreas Schallenberg | ae79c19 | 2012-05-09 09:46:17 +0200 | [diff] [blame] | 292 | u32 reg_val; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 293 | int ret, offset = 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 294 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 295 | mutex_lock(&chip->i2c_lock); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 296 | switch (chip->chip_type) { |
| 297 | case PCA953X_TYPE: |
| 298 | offset = PCA953X_INPUT; |
| 299 | break; |
| 300 | case PCA957X_TYPE: |
| 301 | offset = PCA957X_IN; |
| 302 | break; |
| 303 | } |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 304 | ret = pca953x_read_single(chip, offset, ®_val, off); |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 305 | mutex_unlock(&chip->i2c_lock); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 306 | if (ret < 0) { |
| 307 | /* NOTE: diagnostic already emitted; that's all we should |
| 308 | * do unless gpio_*_value_cansleep() calls become different |
| 309 | * from their nonsleeping siblings (and report faults). |
| 310 | */ |
| 311 | return 0; |
| 312 | } |
| 313 | |
Andrew Ruder | 40a625d | 2013-08-07 16:52:40 -0500 | [diff] [blame] | 314 | return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 317 | static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 318 | { |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 319 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 320 | u8 reg_val; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 321 | int ret, offset = 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 322 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 323 | mutex_lock(&chip->i2c_lock); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 324 | if (val) |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 325 | reg_val = chip->reg_output[off / BANK_SZ] |
| 326 | | (1u << (off % BANK_SZ)); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 327 | else |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 328 | reg_val = chip->reg_output[off / BANK_SZ] |
| 329 | & ~(1u << (off % BANK_SZ)); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 330 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 331 | switch (chip->chip_type) { |
| 332 | case PCA953X_TYPE: |
| 333 | offset = PCA953X_OUTPUT; |
| 334 | break; |
| 335 | case PCA957X_TYPE: |
| 336 | offset = PCA957X_OUT; |
| 337 | break; |
| 338 | } |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 339 | ret = pca953x_write_single(chip, offset, reg_val, off); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 340 | if (ret) |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 341 | goto exit; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 342 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 343 | chip->reg_output[off / BANK_SZ] = reg_val; |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 344 | exit: |
| 345 | mutex_unlock(&chip->i2c_lock); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Phil Reid | b4818af | 2015-12-04 15:52:30 +0800 | [diff] [blame] | 348 | |
| 349 | static void pca953x_gpio_set_multiple(struct gpio_chip *gc, |
| 350 | unsigned long *mask, unsigned long *bits) |
| 351 | { |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 352 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Phil Reid | b4818af | 2015-12-04 15:52:30 +0800 | [diff] [blame] | 353 | u8 reg_val[MAX_BANK]; |
| 354 | int ret, offset = 0; |
| 355 | int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ); |
| 356 | int bank; |
| 357 | |
| 358 | switch (chip->chip_type) { |
| 359 | case PCA953X_TYPE: |
| 360 | offset = PCA953X_OUTPUT; |
| 361 | break; |
| 362 | case PCA957X_TYPE: |
| 363 | offset = PCA957X_OUT; |
| 364 | break; |
| 365 | } |
| 366 | |
| 367 | memcpy(reg_val, chip->reg_output, NBANK(chip)); |
| 368 | mutex_lock(&chip->i2c_lock); |
| 369 | for(bank=0; bank<NBANK(chip); bank++) { |
| 370 | unsigned bankmask = mask[bank/4] >> ((bank % 4) * 8); |
| 371 | if(bankmask) { |
| 372 | unsigned bankval = bits[bank/4] >> ((bank % 4) * 8); |
| 373 | reg_val[bank] = (reg_val[bank] & ~bankmask) | bankval; |
| 374 | } |
| 375 | } |
| 376 | ret = i2c_smbus_write_i2c_block_data(chip->client, offset << bank_shift, NBANK(chip), reg_val); |
| 377 | if (ret) |
| 378 | goto exit; |
| 379 | |
| 380 | memcpy(chip->reg_output, reg_val, NBANK(chip)); |
| 381 | exit: |
| 382 | mutex_unlock(&chip->i2c_lock); |
| 383 | } |
| 384 | |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 385 | static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 386 | { |
| 387 | struct gpio_chip *gc; |
| 388 | |
| 389 | gc = &chip->gpio_chip; |
| 390 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 391 | gc->direction_input = pca953x_gpio_direction_input; |
| 392 | gc->direction_output = pca953x_gpio_direction_output; |
| 393 | gc->get = pca953x_gpio_get_value; |
| 394 | gc->set = pca953x_gpio_set_value; |
Phil Reid | b4818af | 2015-12-04 15:52:30 +0800 | [diff] [blame] | 395 | gc->set_multiple = pca953x_gpio_set_multiple; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 396 | gc->can_sleep = true; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 397 | |
| 398 | gc->base = chip->gpio_start; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 399 | gc->ngpio = gpios; |
| 400 | gc->label = chip->client->name; |
Linus Walleij | 58383c7 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 401 | gc->parent = &chip->client->dev; |
Guennadi Liakhovetski | d72cbed | 2008-04-28 02:14:45 -0700 | [diff] [blame] | 402 | gc->owner = THIS_MODULE; |
Daniel Silverstone | 77906a54 | 2009-06-17 16:26:15 -0700 | [diff] [blame] | 403 | gc->names = chip->names; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 406 | #ifdef CONFIG_GPIO_PCA953X_IRQ |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 407 | static void pca953x_irq_mask(struct irq_data *d) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 408 | { |
Linus Walleij | 7bcbce5 | 2014-05-09 13:27:57 +0200 | [diff] [blame] | 409 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 410 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 411 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 412 | chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ)); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 413 | } |
| 414 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 415 | static void pca953x_irq_unmask(struct irq_data *d) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 416 | { |
Linus Walleij | 7bcbce5 | 2014-05-09 13:27:57 +0200 | [diff] [blame] | 417 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 418 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 419 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 420 | chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 423 | static void pca953x_irq_bus_lock(struct irq_data *d) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 424 | { |
Linus Walleij | 7bcbce5 | 2014-05-09 13:27:57 +0200 | [diff] [blame] | 425 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 426 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 427 | |
| 428 | mutex_lock(&chip->irq_lock); |
| 429 | } |
| 430 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 431 | static void pca953x_irq_bus_sync_unlock(struct irq_data *d) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 432 | { |
Linus Walleij | 7bcbce5 | 2014-05-09 13:27:57 +0200 | [diff] [blame] | 433 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 434 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 435 | u8 new_irqs; |
| 436 | int level, i; |
Marc Zyngier | a2cb9ae | 2010-04-27 13:13:07 -0700 | [diff] [blame] | 437 | |
| 438 | /* Look for any newly setup interrupt */ |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 439 | for (i = 0; i < NBANK(chip); i++) { |
| 440 | new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i]; |
| 441 | new_irqs &= ~chip->reg_direction[i]; |
Marc Zyngier | a2cb9ae | 2010-04-27 13:13:07 -0700 | [diff] [blame] | 442 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 443 | while (new_irqs) { |
| 444 | level = __ffs(new_irqs); |
| 445 | pca953x_gpio_direction_input(&chip->gpio_chip, |
| 446 | level + (BANK_SZ * i)); |
| 447 | new_irqs &= ~(1 << level); |
| 448 | } |
Marc Zyngier | a2cb9ae | 2010-04-27 13:13:07 -0700 | [diff] [blame] | 449 | } |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 450 | |
| 451 | mutex_unlock(&chip->irq_lock); |
| 452 | } |
| 453 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 454 | static int pca953x_irq_set_type(struct irq_data *d, unsigned int type) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 455 | { |
Linus Walleij | 7bcbce5 | 2014-05-09 13:27:57 +0200 | [diff] [blame] | 456 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 457 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 458 | int bank_nb = d->hwirq / BANK_SZ; |
| 459 | u8 mask = 1 << (d->hwirq % BANK_SZ); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 460 | |
| 461 | if (!(type & IRQ_TYPE_EDGE_BOTH)) { |
| 462 | dev_err(&chip->client->dev, "irq %d: unsupported type %d\n", |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 463 | d->irq, type); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 464 | return -EINVAL; |
| 465 | } |
| 466 | |
| 467 | if (type & IRQ_TYPE_EDGE_FALLING) |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 468 | chip->irq_trig_fall[bank_nb] |= mask; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 469 | else |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 470 | chip->irq_trig_fall[bank_nb] &= ~mask; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 471 | |
| 472 | if (type & IRQ_TYPE_EDGE_RISING) |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 473 | chip->irq_trig_raise[bank_nb] |= mask; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 474 | else |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 475 | chip->irq_trig_raise[bank_nb] &= ~mask; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 476 | |
Marc Zyngier | a2cb9ae | 2010-04-27 13:13:07 -0700 | [diff] [blame] | 477 | return 0; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | static struct irq_chip pca953x_irq_chip = { |
| 481 | .name = "pca953x", |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 482 | .irq_mask = pca953x_irq_mask, |
| 483 | .irq_unmask = pca953x_irq_unmask, |
| 484 | .irq_bus_lock = pca953x_irq_bus_lock, |
| 485 | .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock, |
| 486 | .irq_set_type = pca953x_irq_set_type, |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 487 | }; |
| 488 | |
Joshua Scott | b6ac128 | 2015-05-22 12:35:12 +1200 | [diff] [blame] | 489 | static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 490 | { |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 491 | u8 cur_stat[MAX_BANK]; |
| 492 | u8 old_stat[MAX_BANK]; |
Joshua Scott | b6ac128 | 2015-05-22 12:35:12 +1200 | [diff] [blame] | 493 | bool pending_seen = false; |
| 494 | bool trigger_seen = false; |
| 495 | u8 trigger[MAX_BANK]; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 496 | int ret, i, offset = 0; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 497 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 498 | switch (chip->chip_type) { |
| 499 | case PCA953X_TYPE: |
| 500 | offset = PCA953X_INPUT; |
| 501 | break; |
| 502 | case PCA957X_TYPE: |
| 503 | offset = PCA957X_IN; |
| 504 | break; |
| 505 | } |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 506 | ret = pca953x_read_regs(chip, offset, cur_stat); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 507 | if (ret) |
Joshua Scott | b6ac128 | 2015-05-22 12:35:12 +1200 | [diff] [blame] | 508 | return false; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 509 | |
| 510 | /* Remove output pins from the equation */ |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 511 | for (i = 0; i < NBANK(chip); i++) |
| 512 | cur_stat[i] &= chip->reg_direction[i]; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 513 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 514 | memcpy(old_stat, chip->irq_stat, NBANK(chip)); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 515 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 516 | for (i = 0; i < NBANK(chip); i++) { |
| 517 | trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i]; |
Joshua Scott | b6ac128 | 2015-05-22 12:35:12 +1200 | [diff] [blame] | 518 | if (trigger[i]) |
| 519 | trigger_seen = true; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 520 | } |
| 521 | |
Joshua Scott | b6ac128 | 2015-05-22 12:35:12 +1200 | [diff] [blame] | 522 | if (!trigger_seen) |
| 523 | return false; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 524 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 525 | memcpy(chip->irq_stat, cur_stat, NBANK(chip)); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 526 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 527 | for (i = 0; i < NBANK(chip); i++) { |
| 528 | pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) | |
| 529 | (cur_stat[i] & chip->irq_trig_raise[i]); |
| 530 | pending[i] &= trigger[i]; |
Joshua Scott | b6ac128 | 2015-05-22 12:35:12 +1200 | [diff] [blame] | 531 | if (pending[i]) |
| 532 | pending_seen = true; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 533 | } |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 534 | |
Joshua Scott | b6ac128 | 2015-05-22 12:35:12 +1200 | [diff] [blame] | 535 | return pending_seen; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | static irqreturn_t pca953x_irq_handler(int irq, void *devid) |
| 539 | { |
| 540 | struct pca953x_chip *chip = devid; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 541 | u8 pending[MAX_BANK]; |
| 542 | u8 level; |
Toby Smith | 3275d07 | 2014-04-30 18:01:40 +1000 | [diff] [blame] | 543 | unsigned nhandled = 0; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 544 | int i; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 545 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 546 | if (!pca953x_irq_pending(chip, pending)) |
Toby Smith | 3275d07 | 2014-04-30 18:01:40 +1000 | [diff] [blame] | 547 | return IRQ_NONE; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 548 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 549 | for (i = 0; i < NBANK(chip); i++) { |
| 550 | while (pending[i]) { |
| 551 | level = __ffs(pending[i]); |
Linus Walleij | 7bcbce5 | 2014-05-09 13:27:57 +0200 | [diff] [blame] | 552 | handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain, |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 553 | level + (BANK_SZ * i))); |
| 554 | pending[i] &= ~(1 << level); |
Toby Smith | 3275d07 | 2014-04-30 18:01:40 +1000 | [diff] [blame] | 555 | nhandled++; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 556 | } |
| 557 | } |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 558 | |
Toby Smith | 3275d07 | 2014-04-30 18:01:40 +1000 | [diff] [blame] | 559 | return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | static int pca953x_irq_setup(struct pca953x_chip *chip, |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 563 | int irq_base) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 564 | { |
| 565 | struct i2c_client *client = chip->client; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 566 | int ret, i, offset = 0; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 567 | |
Markus Pargmann | 4bb9334 | 2014-07-29 09:24:43 +0200 | [diff] [blame] | 568 | if (client->irq && irq_base != -1 |
Andy Shevchenko | c666414 | 2015-10-01 14:20:27 +0300 | [diff] [blame] | 569 | && (chip->driver_data & PCA_INT)) { |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 570 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 571 | switch (chip->chip_type) { |
| 572 | case PCA953X_TYPE: |
| 573 | offset = PCA953X_INPUT; |
| 574 | break; |
| 575 | case PCA957X_TYPE: |
| 576 | offset = PCA957X_IN; |
| 577 | break; |
| 578 | } |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 579 | ret = pca953x_read_regs(chip, offset, chip->irq_stat); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 580 | if (ret) |
Linus Walleij | b42748c | 2013-01-25 17:59:28 +0100 | [diff] [blame] | 581 | return ret; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 582 | |
| 583 | /* |
| 584 | * There is no way to know which GPIO line generated the |
| 585 | * interrupt. We have to rely on the previous read for |
| 586 | * this purpose. |
| 587 | */ |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 588 | for (i = 0; i < NBANK(chip); i++) |
| 589 | chip->irq_stat[i] &= chip->reg_direction[i]; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 590 | mutex_init(&chip->irq_lock); |
| 591 | |
Linus Walleij | b42748c | 2013-01-25 17:59:28 +0100 | [diff] [blame] | 592 | ret = devm_request_threaded_irq(&client->dev, |
| 593 | client->irq, |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 594 | NULL, |
| 595 | pca953x_irq_handler, |
Toby Smith | 9132913 | 2014-04-30 18:01:41 +1000 | [diff] [blame] | 596 | IRQF_TRIGGER_LOW | IRQF_ONESHOT | |
| 597 | IRQF_SHARED, |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 598 | dev_name(&client->dev), chip); |
| 599 | if (ret) { |
| 600 | dev_err(&client->dev, "failed to request irq %d\n", |
| 601 | client->irq); |
Gregory CLEMENT | 0e8f2fd | 2013-01-25 17:59:27 +0100 | [diff] [blame] | 602 | return ret; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 603 | } |
| 604 | |
Linus Walleij | 7bcbce5 | 2014-05-09 13:27:57 +0200 | [diff] [blame] | 605 | ret = gpiochip_irqchip_add(&chip->gpio_chip, |
| 606 | &pca953x_irq_chip, |
| 607 | irq_base, |
| 608 | handle_simple_irq, |
| 609 | IRQ_TYPE_NONE); |
| 610 | if (ret) { |
| 611 | dev_err(&client->dev, |
| 612 | "could not connect irqchip to gpiochip\n"); |
| 613 | return ret; |
| 614 | } |
Grygorii Strashko | fdd5040 | 2015-07-07 17:34:49 +0300 | [diff] [blame] | 615 | |
| 616 | gpiochip_set_chained_irqchip(&chip->gpio_chip, |
| 617 | &pca953x_irq_chip, |
| 618 | client->irq, NULL); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | return 0; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 622 | } |
| 623 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 624 | #else /* CONFIG_GPIO_PCA953X_IRQ */ |
| 625 | static int pca953x_irq_setup(struct pca953x_chip *chip, |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 626 | int irq_base) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 627 | { |
| 628 | struct i2c_client *client = chip->client; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 629 | |
Andy Shevchenko | c666414 | 2015-10-01 14:20:27 +0300 | [diff] [blame] | 630 | if (irq_base != -1 && (chip->driver_data & PCA_INT)) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 631 | dev_warn(&client->dev, "interrupt support not compiled in\n"); |
| 632 | |
| 633 | return 0; |
| 634 | } |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 635 | #endif |
| 636 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 637 | static int device_pca953x_init(struct pca953x_chip *chip, u32 invert) |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 638 | { |
| 639 | int ret; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 640 | u8 val[MAX_BANK]; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 641 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 642 | ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 643 | if (ret) |
| 644 | goto out; |
| 645 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 646 | ret = pca953x_read_regs(chip, PCA953X_DIRECTION, |
| 647 | chip->reg_direction); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 648 | if (ret) |
| 649 | goto out; |
| 650 | |
| 651 | /* set platform specific polarity inversion */ |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 652 | if (invert) |
| 653 | memset(val, 0xFF, NBANK(chip)); |
| 654 | else |
| 655 | memset(val, 0, NBANK(chip)); |
| 656 | |
| 657 | ret = pca953x_write_regs(chip, PCA953X_INVERT, val); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 658 | out: |
| 659 | return ret; |
| 660 | } |
| 661 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 662 | static int device_pca957x_init(struct pca953x_chip *chip, u32 invert) |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 663 | { |
| 664 | int ret; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 665 | u8 val[MAX_BANK]; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 666 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 667 | ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 668 | if (ret) |
| 669 | goto out; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 670 | ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 671 | if (ret) |
| 672 | goto out; |
| 673 | |
| 674 | /* set platform specific polarity inversion */ |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 675 | if (invert) |
| 676 | memset(val, 0xFF, NBANK(chip)); |
| 677 | else |
| 678 | memset(val, 0, NBANK(chip)); |
Nicholas Krause | c75a377 | 2015-08-26 17:52:19 -0400 | [diff] [blame] | 679 | ret = pca953x_write_regs(chip, PCA957X_INVRT, val); |
| 680 | if (ret) |
| 681 | goto out; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 682 | |
Colin Cronin | 20a8a96 | 2015-05-18 11:41:43 -0700 | [diff] [blame] | 683 | /* To enable register 6, 7 to control pull up and pull down */ |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 684 | memset(val, 0x02, NBANK(chip)); |
Nicholas Krause | c75a377 | 2015-08-26 17:52:19 -0400 | [diff] [blame] | 685 | ret = pca953x_write_regs(chip, PCA957X_BKEN, val); |
| 686 | if (ret) |
| 687 | goto out; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 688 | |
| 689 | return 0; |
| 690 | out: |
| 691 | return ret; |
| 692 | } |
| 693 | |
Ben Dooks | 6f29c9a | 2015-12-08 10:19:26 +0000 | [diff] [blame] | 694 | static const struct of_device_id pca953x_dt_ids[]; |
| 695 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 696 | static int pca953x_probe(struct i2c_client *client, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 697 | const struct i2c_device_id *id) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 698 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 699 | struct pca953x_platform_data *pdata; |
| 700 | struct pca953x_chip *chip; |
Chandrabhanu Mahapatra | 6a7b36a | 2012-07-10 19:05:37 +0530 | [diff] [blame] | 701 | int irq_base = 0; |
Wolfram Sang | 7ea2aa2 | 2011-10-14 15:32:00 +0200 | [diff] [blame] | 702 | int ret; |
Chandrabhanu Mahapatra | 6a7b36a | 2012-07-10 19:05:37 +0530 | [diff] [blame] | 703 | u32 invert = 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 704 | |
Linus Walleij | b42748c | 2013-01-25 17:59:28 +0100 | [diff] [blame] | 705 | chip = devm_kzalloc(&client->dev, |
| 706 | sizeof(struct pca953x_chip), GFP_KERNEL); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 707 | if (chip == NULL) |
| 708 | return -ENOMEM; |
| 709 | |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 710 | pdata = dev_get_platdata(&client->dev); |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 711 | if (pdata) { |
| 712 | irq_base = pdata->irq_base; |
| 713 | chip->gpio_start = pdata->gpio_base; |
| 714 | invert = pdata->invert; |
| 715 | chip->names = pdata->names; |
| 716 | } else { |
Markus Pargmann | 4bb9334 | 2014-07-29 09:24:43 +0200 | [diff] [blame] | 717 | chip->gpio_start = -1; |
| 718 | irq_base = 0; |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 719 | } |
| 720 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 721 | chip->client = client; |
| 722 | |
Andy Shevchenko | f32517b | 2015-10-01 14:20:28 +0300 | [diff] [blame] | 723 | if (id) { |
| 724 | chip->driver_data = id->driver_data; |
| 725 | } else { |
| 726 | const struct acpi_device_id *id; |
Ben Dooks | 6f29c9a | 2015-12-08 10:19:26 +0000 | [diff] [blame] | 727 | const struct of_device_id *match; |
Andy Shevchenko | f32517b | 2015-10-01 14:20:28 +0300 | [diff] [blame] | 728 | |
Ben Dooks | 6f29c9a | 2015-12-08 10:19:26 +0000 | [diff] [blame] | 729 | match = of_match_device(pca953x_dt_ids, &client->dev); |
| 730 | if (match) { |
| 731 | chip->driver_data = (int)(uintptr_t)match->data; |
| 732 | } else { |
| 733 | id = acpi_match_device(pca953x_acpi_ids, &client->dev); |
| 734 | if (!id) |
| 735 | return -ENODEV; |
Andy Shevchenko | f32517b | 2015-10-01 14:20:28 +0300 | [diff] [blame] | 736 | |
Ben Dooks | 6f29c9a | 2015-12-08 10:19:26 +0000 | [diff] [blame] | 737 | chip->driver_data = id->driver_data; |
| 738 | } |
Andy Shevchenko | f32517b | 2015-10-01 14:20:28 +0300 | [diff] [blame] | 739 | } |
| 740 | |
Andy Shevchenko | c666414 | 2015-10-01 14:20:27 +0300 | [diff] [blame] | 741 | chip->chip_type = PCA_CHIP_TYPE(chip->driver_data); |
Daniel Silverstone | 77906a54 | 2009-06-17 16:26:15 -0700 | [diff] [blame] | 742 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 743 | mutex_init(&chip->i2c_lock); |
| 744 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 745 | /* initialize cached registers from their original values. |
| 746 | * we can't share this chip with another i2c master. |
| 747 | */ |
Andy Shevchenko | c666414 | 2015-10-01 14:20:27 +0300 | [diff] [blame] | 748 | pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK); |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 749 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 750 | if (chip->chip_type == PCA953X_TYPE) |
Wolfram Sang | 7ea2aa2 | 2011-10-14 15:32:00 +0200 | [diff] [blame] | 751 | ret = device_pca953x_init(chip, invert); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 752 | else |
Wolfram Sang | 7ea2aa2 | 2011-10-14 15:32:00 +0200 | [diff] [blame] | 753 | ret = device_pca957x_init(chip, invert); |
| 754 | if (ret) |
Linus Walleij | b42748c | 2013-01-25 17:59:28 +0100 | [diff] [blame] | 755 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 756 | |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 757 | ret = gpiochip_add_data(&chip->gpio_chip, chip); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 758 | if (ret) |
Linus Walleij | b42748c | 2013-01-25 17:59:28 +0100 | [diff] [blame] | 759 | return ret; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 760 | |
Andy Shevchenko | c666414 | 2015-10-01 14:20:27 +0300 | [diff] [blame] | 761 | ret = pca953x_irq_setup(chip, irq_base); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 762 | if (ret) |
Linus Walleij | b42748c | 2013-01-25 17:59:28 +0100 | [diff] [blame] | 763 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 764 | |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 765 | if (pdata && pdata->setup) { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 766 | ret = pdata->setup(client, chip->gpio_chip.base, |
| 767 | chip->gpio_chip.ngpio, pdata->context); |
| 768 | if (ret < 0) |
| 769 | dev_warn(&client->dev, "setup failed, %d\n", ret); |
| 770 | } |
| 771 | |
| 772 | i2c_set_clientdata(client, chip); |
| 773 | return 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 774 | } |
| 775 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 776 | static int pca953x_remove(struct i2c_client *client) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 777 | { |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 778 | struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev); |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 779 | struct pca953x_chip *chip = i2c_get_clientdata(client); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 780 | int ret = 0; |
| 781 | |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 782 | if (pdata && pdata->teardown) { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 783 | ret = pdata->teardown(client, chip->gpio_chip.base, |
| 784 | chip->gpio_chip.ngpio, pdata->context); |
| 785 | if (ret < 0) { |
| 786 | dev_err(&client->dev, "%s failed, %d\n", |
| 787 | "teardown", ret); |
| 788 | return ret; |
| 789 | } |
| 790 | } |
| 791 | |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 792 | gpiochip_remove(&chip->gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 793 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 794 | return 0; |
| 795 | } |
| 796 | |
Ben Dooks | 6f29c9a | 2015-12-08 10:19:26 +0000 | [diff] [blame] | 797 | /* convenience to stop overlong match-table lines */ |
| 798 | #define OF_953X(__nrgpio, __int) (void *)(__nrgpio | PCA953X_TYPE | __int) |
| 799 | #define OF_957X(__nrgpio, __int) (void *)(__nrgpio | PCA957X_TYPE | __int) |
| 800 | |
Maxime Ripard | ed32620 | 2012-11-08 18:01:52 +0100 | [diff] [blame] | 801 | static const struct of_device_id pca953x_dt_ids[] = { |
Ben Dooks | 6f29c9a | 2015-12-08 10:19:26 +0000 | [diff] [blame] | 802 | { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), }, |
| 803 | { .compatible = "nxp,pca9534", .data = OF_953X( 8, PCA_INT), }, |
| 804 | { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), }, |
| 805 | { .compatible = "nxp,pca9536", .data = OF_953X( 4, 0), }, |
| 806 | { .compatible = "nxp,pca9537", .data = OF_953X( 4, PCA_INT), }, |
| 807 | { .compatible = "nxp,pca9538", .data = OF_953X( 8, PCA_INT), }, |
| 808 | { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), }, |
| 809 | { .compatible = "nxp,pca9554", .data = OF_953X( 8, PCA_INT), }, |
| 810 | { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), }, |
| 811 | { .compatible = "nxp,pca9556", .data = OF_953X( 8, 0), }, |
| 812 | { .compatible = "nxp,pca9557", .data = OF_953X( 8, 0), }, |
| 813 | { .compatible = "nxp,pca9574", .data = OF_957X( 8, PCA_INT), }, |
| 814 | { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), }, |
| 815 | { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), }, |
Maxime Ripard | ed32620 | 2012-11-08 18:01:52 +0100 | [diff] [blame] | 816 | |
Ben Dooks | 6f29c9a | 2015-12-08 10:19:26 +0000 | [diff] [blame] | 817 | { .compatible = "maxim,max7310", .data = OF_953X( 8, 0), }, |
| 818 | { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), }, |
| 819 | { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), }, |
| 820 | { .compatible = "maxim,max7315", .data = OF_953X( 8, PCA_INT), }, |
Maxime Ripard | ed32620 | 2012-11-08 18:01:52 +0100 | [diff] [blame] | 821 | |
Ben Dooks | 6f29c9a | 2015-12-08 10:19:26 +0000 | [diff] [blame] | 822 | { .compatible = "ti,pca6107", .data = OF_953X( 8, PCA_INT), }, |
| 823 | { .compatible = "ti,tca6408", .data = OF_953X( 8, PCA_INT), }, |
| 824 | { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), }, |
| 825 | { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), }, |
Aaron Sierra | e73760a | 2014-02-07 16:36:21 -0600 | [diff] [blame] | 826 | |
Ben Dooks | 6f29c9a | 2015-12-08 10:19:26 +0000 | [diff] [blame] | 827 | { .compatible = "onsemi,pca9654", .data = OF_953X( 8, PCA_INT), }, |
Ben Dooks | 9f49f6d | 2015-12-08 10:19:25 +0000 | [diff] [blame] | 828 | |
Ben Dooks | 6f29c9a | 2015-12-08 10:19:26 +0000 | [diff] [blame] | 829 | { .compatible = "exar,xra1202", .data = OF_953X( 8, 0), }, |
Maxime Ripard | ed32620 | 2012-11-08 18:01:52 +0100 | [diff] [blame] | 830 | { } |
| 831 | }; |
| 832 | |
| 833 | MODULE_DEVICE_TABLE(of, pca953x_dt_ids); |
| 834 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 835 | static struct i2c_driver pca953x_driver = { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 836 | .driver = { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 837 | .name = "pca953x", |
Maxime Ripard | ed32620 | 2012-11-08 18:01:52 +0100 | [diff] [blame] | 838 | .of_match_table = pca953x_dt_ids, |
Andy Shevchenko | f32517b | 2015-10-01 14:20:28 +0300 | [diff] [blame] | 839 | .acpi_match_table = ACPI_PTR(pca953x_acpi_ids), |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 840 | }, |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 841 | .probe = pca953x_probe, |
| 842 | .remove = pca953x_remove, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 843 | .id_table = pca953x_id, |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 844 | }; |
| 845 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 846 | static int __init pca953x_init(void) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 847 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 848 | return i2c_add_driver(&pca953x_driver); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 849 | } |
David Brownell | 2f8d119 | 2008-10-15 22:03:13 -0700 | [diff] [blame] | 850 | /* register after i2c postcore initcall and before |
| 851 | * subsys initcalls that may rely on these GPIOs |
| 852 | */ |
| 853 | subsys_initcall(pca953x_init); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 854 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 855 | static void __exit pca953x_exit(void) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 856 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 857 | i2c_del_driver(&pca953x_driver); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 858 | } |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 859 | module_exit(pca953x_exit); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 860 | |
| 861 | MODULE_AUTHOR("eric miao <eric.miao@marvell.com>"); |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 862 | MODULE_DESCRIPTION("GPIO expander driver for PCA953x"); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 863 | MODULE_LICENSE("GPL"); |