Sebastian Reichel | d8f4494 | 2017-05-15 11:24:37 +0200 | [diff] [blame] | 1 | /* MCP23S08 SPI/I2C GPIO driver */ |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 2 | |
| 3 | #include <linux/kernel.h> |
| 4 | #include <linux/device.h> |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 5 | #include <linux/mutex.h> |
Paul Gortmaker | bb207ef | 2011-07-03 13:38:09 -0400 | [diff] [blame] | 6 | #include <linux/module.h> |
H Hartley Sweeten | d120c17 | 2009-09-22 16:46:37 -0700 | [diff] [blame] | 7 | #include <linux/gpio.h> |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 8 | #include <linux/i2c.h> |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 9 | #include <linux/spi/spi.h> |
| 10 | #include <linux/spi/mcp23s08.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 12 | #include <asm/byteorder.h> |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 13 | #include <linux/interrupt.h> |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 14 | #include <linux/of_device.h> |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 15 | #include <linux/regmap.h> |
Sebastian Reichel | 82039d2 | 2017-05-15 11:24:26 +0200 | [diff] [blame] | 16 | #include <linux/pinctrl/pinctrl.h> |
| 17 | #include <linux/pinctrl/pinconf.h> |
| 18 | #include <linux/pinctrl/pinconf-generic.h> |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 19 | |
Sebastian Reichel | d8f4494 | 2017-05-15 11:24:37 +0200 | [diff] [blame] | 20 | /* |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 21 | * MCP types supported by driver |
| 22 | */ |
| 23 | #define MCP_TYPE_S08 0 |
| 24 | #define MCP_TYPE_S17 1 |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 25 | #define MCP_TYPE_008 2 |
| 26 | #define MCP_TYPE_017 3 |
Phil Reid | 28c5a41 | 2016-03-01 14:25:41 +0800 | [diff] [blame] | 27 | #define MCP_TYPE_S18 4 |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 28 | |
Sebastian Reichel | ce9bd0a | 2017-05-15 11:24:36 +0200 | [diff] [blame] | 29 | #define MCP_MAX_DEV_PER_CS 8 |
| 30 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 31 | /* Registers are all 8 bits wide. |
| 32 | * |
| 33 | * The mcp23s17 has twice as many bits, and can be configured to work |
| 34 | * with either 16 bit registers or with two adjacent 8 bit banks. |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 35 | */ |
| 36 | #define MCP_IODIR 0x00 /* init/reset: all ones */ |
| 37 | #define MCP_IPOL 0x01 |
| 38 | #define MCP_GPINTEN 0x02 |
| 39 | #define MCP_DEFVAL 0x03 |
| 40 | #define MCP_INTCON 0x04 |
| 41 | #define MCP_IOCON 0x05 |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 42 | # define IOCON_MIRROR (1 << 6) |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 43 | # define IOCON_SEQOP (1 << 5) |
| 44 | # define IOCON_HAEN (1 << 3) |
| 45 | # define IOCON_ODR (1 << 2) |
| 46 | # define IOCON_INTPOL (1 << 1) |
Phil Reid | 3539699 | 2016-03-15 15:46:30 +0800 | [diff] [blame] | 47 | # define IOCON_INTCC (1) |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 48 | #define MCP_GPPU 0x06 |
| 49 | #define MCP_INTF 0x07 |
| 50 | #define MCP_INTCAP 0x08 |
| 51 | #define MCP_GPIO 0x09 |
| 52 | #define MCP_OLAT 0x0a |
| 53 | |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 54 | struct mcp23s08; |
| 55 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 56 | struct mcp23s08 { |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 57 | u8 addr; |
Alexander Stein | a4e6355 | 2014-12-01 08:26:00 +0100 | [diff] [blame] | 58 | bool irq_active_high; |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 59 | bool reg_shift; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 60 | |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 61 | u16 irq_rise; |
| 62 | u16 irq_fall; |
| 63 | int irq; |
| 64 | bool irq_controller; |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 65 | int cached_gpio; |
| 66 | /* lock protects regmap access with bypass/cache flags */ |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 67 | struct mutex lock; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 68 | |
| 69 | struct gpio_chip chip; |
| 70 | |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 71 | struct regmap *regmap; |
| 72 | struct device *dev; |
Sebastian Reichel | 82039d2 | 2017-05-15 11:24:26 +0200 | [diff] [blame] | 73 | |
| 74 | struct pinctrl_dev *pctldev; |
| 75 | struct pinctrl_desc pinctrl_desc; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 78 | static const struct reg_default mcp23x08_defaults[] = { |
| 79 | {.reg = MCP_IODIR, .def = 0xff}, |
| 80 | {.reg = MCP_IPOL, .def = 0x00}, |
| 81 | {.reg = MCP_GPINTEN, .def = 0x00}, |
| 82 | {.reg = MCP_DEFVAL, .def = 0x00}, |
| 83 | {.reg = MCP_INTCON, .def = 0x00}, |
| 84 | {.reg = MCP_IOCON, .def = 0x00}, |
| 85 | {.reg = MCP_GPPU, .def = 0x00}, |
| 86 | {.reg = MCP_OLAT, .def = 0x00}, |
| 87 | }; |
| 88 | |
| 89 | static const struct regmap_range mcp23x08_volatile_range = { |
| 90 | .range_min = MCP_INTF, |
| 91 | .range_max = MCP_GPIO, |
| 92 | }; |
| 93 | |
| 94 | static const struct regmap_access_table mcp23x08_volatile_table = { |
| 95 | .yes_ranges = &mcp23x08_volatile_range, |
| 96 | .n_yes_ranges = 1, |
| 97 | }; |
| 98 | |
| 99 | static const struct regmap_range mcp23x08_precious_range = { |
| 100 | .range_min = MCP_GPIO, |
| 101 | .range_max = MCP_GPIO, |
| 102 | }; |
| 103 | |
| 104 | static const struct regmap_access_table mcp23x08_precious_table = { |
| 105 | .yes_ranges = &mcp23x08_precious_range, |
| 106 | .n_yes_ranges = 1, |
| 107 | }; |
| 108 | |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 109 | static const struct regmap_config mcp23x08_regmap = { |
| 110 | .reg_bits = 8, |
| 111 | .val_bits = 8, |
| 112 | |
| 113 | .reg_stride = 1, |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 114 | .volatile_table = &mcp23x08_volatile_table, |
| 115 | .precious_table = &mcp23x08_precious_table, |
| 116 | .reg_defaults = mcp23x08_defaults, |
| 117 | .num_reg_defaults = ARRAY_SIZE(mcp23x08_defaults), |
| 118 | .cache_type = REGCACHE_FLAT, |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 119 | .max_register = MCP_OLAT, |
| 120 | }; |
| 121 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 122 | static const struct reg_default mcp23x16_defaults[] = { |
| 123 | {.reg = MCP_IODIR << 1, .def = 0xffff}, |
| 124 | {.reg = MCP_IPOL << 1, .def = 0x0000}, |
| 125 | {.reg = MCP_GPINTEN << 1, .def = 0x0000}, |
| 126 | {.reg = MCP_DEFVAL << 1, .def = 0x0000}, |
| 127 | {.reg = MCP_INTCON << 1, .def = 0x0000}, |
| 128 | {.reg = MCP_IOCON << 1, .def = 0x0000}, |
| 129 | {.reg = MCP_GPPU << 1, .def = 0x0000}, |
| 130 | {.reg = MCP_OLAT << 1, .def = 0x0000}, |
| 131 | }; |
| 132 | |
| 133 | static const struct regmap_range mcp23x16_volatile_range = { |
| 134 | .range_min = MCP_INTF << 1, |
| 135 | .range_max = MCP_GPIO << 1, |
| 136 | }; |
| 137 | |
| 138 | static const struct regmap_access_table mcp23x16_volatile_table = { |
| 139 | .yes_ranges = &mcp23x16_volatile_range, |
| 140 | .n_yes_ranges = 1, |
| 141 | }; |
| 142 | |
| 143 | static const struct regmap_range mcp23x16_precious_range = { |
| 144 | .range_min = MCP_GPIO << 1, |
| 145 | .range_max = MCP_GPIO << 1, |
| 146 | }; |
| 147 | |
| 148 | static const struct regmap_access_table mcp23x16_precious_table = { |
| 149 | .yes_ranges = &mcp23x16_precious_range, |
| 150 | .n_yes_ranges = 1, |
| 151 | }; |
| 152 | |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 153 | static const struct regmap_config mcp23x17_regmap = { |
| 154 | .reg_bits = 8, |
| 155 | .val_bits = 16, |
| 156 | |
| 157 | .reg_stride = 2, |
| 158 | .max_register = MCP_OLAT << 1, |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 159 | .volatile_table = &mcp23x16_volatile_table, |
| 160 | .precious_table = &mcp23x16_precious_table, |
| 161 | .reg_defaults = mcp23x16_defaults, |
| 162 | .num_reg_defaults = ARRAY_SIZE(mcp23x16_defaults), |
| 163 | .cache_type = REGCACHE_FLAT, |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 164 | .val_format_endian = REGMAP_ENDIAN_LITTLE, |
| 165 | }; |
| 166 | |
Sebastian Reichel | 82039d2 | 2017-05-15 11:24:26 +0200 | [diff] [blame] | 167 | static int mcp_read(struct mcp23s08 *mcp, unsigned int reg, unsigned int *val) |
| 168 | { |
| 169 | return regmap_read(mcp->regmap, reg << mcp->reg_shift, val); |
| 170 | } |
| 171 | |
| 172 | static int mcp_write(struct mcp23s08 *mcp, unsigned int reg, unsigned int val) |
| 173 | { |
| 174 | return regmap_write(mcp->regmap, reg << mcp->reg_shift, val); |
| 175 | } |
| 176 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 177 | static int mcp_set_mask(struct mcp23s08 *mcp, unsigned int reg, |
| 178 | unsigned int mask, bool enabled) |
Sebastian Reichel | 82039d2 | 2017-05-15 11:24:26 +0200 | [diff] [blame] | 179 | { |
| 180 | u16 val = enabled ? 0xffff : 0x0000; |
Sebastian Reichel | 82039d2 | 2017-05-15 11:24:26 +0200 | [diff] [blame] | 181 | return regmap_update_bits(mcp->regmap, reg << mcp->reg_shift, |
| 182 | mask, val); |
| 183 | } |
| 184 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 185 | static int mcp_set_bit(struct mcp23s08 *mcp, unsigned int reg, |
| 186 | unsigned int pin, bool enabled) |
Sebastian Reichel | 82039d2 | 2017-05-15 11:24:26 +0200 | [diff] [blame] | 187 | { |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 188 | u16 mask = BIT(pin); |
| 189 | return mcp_set_mask(mcp, reg, mask, enabled); |
Sebastian Reichel | 82039d2 | 2017-05-15 11:24:26 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static const struct pinctrl_pin_desc mcp23x08_pins[] = { |
| 193 | PINCTRL_PIN(0, "gpio0"), |
| 194 | PINCTRL_PIN(1, "gpio1"), |
| 195 | PINCTRL_PIN(2, "gpio2"), |
| 196 | PINCTRL_PIN(3, "gpio3"), |
| 197 | PINCTRL_PIN(4, "gpio4"), |
| 198 | PINCTRL_PIN(5, "gpio5"), |
| 199 | PINCTRL_PIN(6, "gpio6"), |
| 200 | PINCTRL_PIN(7, "gpio7"), |
| 201 | }; |
| 202 | |
| 203 | static const struct pinctrl_pin_desc mcp23x17_pins[] = { |
| 204 | PINCTRL_PIN(0, "gpio0"), |
| 205 | PINCTRL_PIN(1, "gpio1"), |
| 206 | PINCTRL_PIN(2, "gpio2"), |
| 207 | PINCTRL_PIN(3, "gpio3"), |
| 208 | PINCTRL_PIN(4, "gpio4"), |
| 209 | PINCTRL_PIN(5, "gpio5"), |
| 210 | PINCTRL_PIN(6, "gpio6"), |
| 211 | PINCTRL_PIN(7, "gpio7"), |
| 212 | PINCTRL_PIN(8, "gpio8"), |
| 213 | PINCTRL_PIN(9, "gpio9"), |
| 214 | PINCTRL_PIN(10, "gpio10"), |
| 215 | PINCTRL_PIN(11, "gpio11"), |
| 216 | PINCTRL_PIN(12, "gpio12"), |
| 217 | PINCTRL_PIN(13, "gpio13"), |
| 218 | PINCTRL_PIN(14, "gpio14"), |
| 219 | PINCTRL_PIN(15, "gpio15"), |
| 220 | }; |
| 221 | |
| 222 | static int mcp_pinctrl_get_groups_count(struct pinctrl_dev *pctldev) |
| 223 | { |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static const char *mcp_pinctrl_get_group_name(struct pinctrl_dev *pctldev, |
| 228 | unsigned int group) |
| 229 | { |
| 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | static int mcp_pinctrl_get_group_pins(struct pinctrl_dev *pctldev, |
| 234 | unsigned int group, |
| 235 | const unsigned int **pins, |
| 236 | unsigned int *num_pins) |
| 237 | { |
| 238 | return -ENOTSUPP; |
| 239 | } |
| 240 | |
| 241 | static const struct pinctrl_ops mcp_pinctrl_ops = { |
| 242 | .get_groups_count = mcp_pinctrl_get_groups_count, |
| 243 | .get_group_name = mcp_pinctrl_get_group_name, |
| 244 | .get_group_pins = mcp_pinctrl_get_group_pins, |
| 245 | #ifdef CONFIG_OF |
| 246 | .dt_node_to_map = pinconf_generic_dt_node_to_map_pin, |
| 247 | .dt_free_map = pinconf_generic_dt_free_map, |
| 248 | #endif |
| 249 | }; |
| 250 | |
| 251 | static int mcp_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin, |
| 252 | unsigned long *config) |
| 253 | { |
| 254 | struct mcp23s08 *mcp = pinctrl_dev_get_drvdata(pctldev); |
| 255 | enum pin_config_param param = pinconf_to_config_param(*config); |
| 256 | unsigned int data, status; |
| 257 | int ret; |
| 258 | |
| 259 | switch (param) { |
| 260 | case PIN_CONFIG_BIAS_PULL_UP: |
| 261 | ret = mcp_read(mcp, MCP_GPPU, &data); |
| 262 | if (ret < 0) |
| 263 | return ret; |
| 264 | status = (data & BIT(pin)) ? 1 : 0; |
| 265 | break; |
| 266 | default: |
| 267 | dev_err(mcp->dev, "Invalid config param %04x\n", param); |
| 268 | return -ENOTSUPP; |
| 269 | } |
| 270 | |
| 271 | *config = 0; |
| 272 | |
| 273 | return status ? 0 : -EINVAL; |
| 274 | } |
| 275 | |
| 276 | static int mcp_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin, |
| 277 | unsigned long *configs, unsigned int num_configs) |
| 278 | { |
| 279 | struct mcp23s08 *mcp = pinctrl_dev_get_drvdata(pctldev); |
| 280 | enum pin_config_param param; |
| 281 | u32 arg, mask; |
| 282 | u16 val; |
| 283 | int ret = 0; |
| 284 | int i; |
| 285 | |
| 286 | for (i = 0; i < num_configs; i++) { |
| 287 | param = pinconf_to_config_param(configs[i]); |
| 288 | arg = pinconf_to_config_argument(configs[i]); |
| 289 | |
| 290 | switch (param) { |
| 291 | case PIN_CONFIG_BIAS_PULL_UP: |
| 292 | val = arg ? 0xFFFF : 0x0000; |
| 293 | mask = BIT(pin); |
| 294 | ret = mcp_set_bit(mcp, MCP_GPPU, pin, arg); |
| 295 | break; |
| 296 | default: |
| 297 | dev_err(mcp->dev, "Invalid config param %04x\n", param); |
| 298 | return -ENOTSUPP; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | static const struct pinconf_ops mcp_pinconf_ops = { |
| 306 | .pin_config_get = mcp_pinconf_get, |
| 307 | .pin_config_set = mcp_pinconf_set, |
| 308 | .is_generic = true, |
| 309 | }; |
| 310 | |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 311 | /*----------------------------------------------------------------------*/ |
| 312 | |
| 313 | #ifdef CONFIG_SPI_MASTER |
| 314 | |
| 315 | static int mcp23sxx_spi_write(void *context, const void *data, size_t count) |
| 316 | { |
| 317 | struct mcp23s08 *mcp = context; |
| 318 | struct spi_device *spi = to_spi_device(mcp->dev); |
| 319 | struct spi_message m; |
| 320 | struct spi_transfer t[2] = { { .tx_buf = &mcp->addr, .len = 1, }, |
| 321 | { .tx_buf = data, .len = count, }, }; |
| 322 | |
| 323 | spi_message_init(&m); |
| 324 | spi_message_add_tail(&t[0], &m); |
| 325 | spi_message_add_tail(&t[1], &m); |
| 326 | |
| 327 | return spi_sync(spi, &m); |
| 328 | } |
| 329 | |
| 330 | static int mcp23sxx_spi_gather_write(void *context, |
| 331 | const void *reg, size_t reg_size, |
| 332 | const void *val, size_t val_size) |
| 333 | { |
| 334 | struct mcp23s08 *mcp = context; |
| 335 | struct spi_device *spi = to_spi_device(mcp->dev); |
| 336 | struct spi_message m; |
| 337 | struct spi_transfer t[3] = { { .tx_buf = &mcp->addr, .len = 1, }, |
| 338 | { .tx_buf = reg, .len = reg_size, }, |
| 339 | { .tx_buf = val, .len = val_size, }, }; |
| 340 | |
| 341 | spi_message_init(&m); |
| 342 | spi_message_add_tail(&t[0], &m); |
| 343 | spi_message_add_tail(&t[1], &m); |
| 344 | spi_message_add_tail(&t[2], &m); |
| 345 | |
| 346 | return spi_sync(spi, &m); |
| 347 | } |
| 348 | |
| 349 | static int mcp23sxx_spi_read(void *context, const void *reg, size_t reg_size, |
| 350 | void *val, size_t val_size) |
| 351 | { |
| 352 | struct mcp23s08 *mcp = context; |
| 353 | struct spi_device *spi = to_spi_device(mcp->dev); |
| 354 | u8 tx[2]; |
| 355 | |
| 356 | if (reg_size != 1) |
| 357 | return -EINVAL; |
| 358 | |
| 359 | tx[0] = mcp->addr | 0x01; |
| 360 | tx[1] = *((u8 *) reg); |
| 361 | |
| 362 | return spi_write_then_read(spi, tx, sizeof(tx), val, val_size); |
| 363 | } |
| 364 | |
| 365 | static const struct regmap_bus mcp23sxx_spi_regmap = { |
| 366 | .write = mcp23sxx_spi_write, |
| 367 | .gather_write = mcp23sxx_spi_gather_write, |
| 368 | .read = mcp23sxx_spi_read, |
| 369 | }; |
| 370 | |
| 371 | #endif /* CONFIG_SPI_MASTER */ |
| 372 | |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 373 | /*----------------------------------------------------------------------*/ |
| 374 | |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 375 | /* A given spi_device can represent up to eight mcp23sxx chips |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 376 | * sharing the same chipselect but using different addresses |
| 377 | * (e.g. chips #0 and #3 might be populated, but not #1 or $2). |
| 378 | * Driver data holds all the per-chip data. |
| 379 | */ |
| 380 | struct mcp23s08_driver_data { |
| 381 | unsigned ngpio; |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 382 | struct mcp23s08 *mcp[8]; |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 383 | struct mcp23s08 chip[]; |
| 384 | }; |
| 385 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 386 | |
| 387 | static int mcp23s08_direction_input(struct gpio_chip *chip, unsigned offset) |
| 388 | { |
Linus Walleij | 9e03cf0 | 2015-12-07 10:09:36 +0100 | [diff] [blame] | 389 | struct mcp23s08 *mcp = gpiochip_get_data(chip); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 390 | int status; |
| 391 | |
| 392 | mutex_lock(&mcp->lock); |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 393 | status = mcp_set_bit(mcp, MCP_IODIR, offset, true); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 394 | mutex_unlock(&mcp->lock); |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 395 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 396 | return status; |
| 397 | } |
| 398 | |
| 399 | static int mcp23s08_get(struct gpio_chip *chip, unsigned offset) |
| 400 | { |
Linus Walleij | 9e03cf0 | 2015-12-07 10:09:36 +0100 | [diff] [blame] | 401 | struct mcp23s08 *mcp = gpiochip_get_data(chip); |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 402 | int status, ret; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 403 | |
| 404 | mutex_lock(&mcp->lock); |
| 405 | |
| 406 | /* REVISIT reading this clears any IRQ ... */ |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 407 | ret = mcp_read(mcp, MCP_GPIO, &status); |
| 408 | if (ret < 0) |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 409 | status = 0; |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 410 | else |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 411 | status = !!(status & (1 << offset)); |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 412 | |
| 413 | mcp->cached_gpio = status; |
| 414 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 415 | mutex_unlock(&mcp->lock); |
| 416 | return status; |
| 417 | } |
| 418 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 419 | static int __mcp23s08_set(struct mcp23s08 *mcp, unsigned mask, bool value) |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 420 | { |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 421 | return mcp_set_mask(mcp, MCP_OLAT, mask, value); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static void mcp23s08_set(struct gpio_chip *chip, unsigned offset, int value) |
| 425 | { |
Linus Walleij | 9e03cf0 | 2015-12-07 10:09:36 +0100 | [diff] [blame] | 426 | struct mcp23s08 *mcp = gpiochip_get_data(chip); |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 427 | unsigned mask = BIT(offset); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 428 | |
| 429 | mutex_lock(&mcp->lock); |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 430 | __mcp23s08_set(mcp, mask, !!value); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 431 | mutex_unlock(&mcp->lock); |
| 432 | } |
| 433 | |
| 434 | static int |
| 435 | mcp23s08_direction_output(struct gpio_chip *chip, unsigned offset, int value) |
| 436 | { |
Linus Walleij | 9e03cf0 | 2015-12-07 10:09:36 +0100 | [diff] [blame] | 437 | struct mcp23s08 *mcp = gpiochip_get_data(chip); |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 438 | unsigned mask = BIT(offset); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 439 | int status; |
| 440 | |
| 441 | mutex_lock(&mcp->lock); |
| 442 | status = __mcp23s08_set(mcp, mask, value); |
| 443 | if (status == 0) { |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 444 | status = mcp_set_mask(mcp, MCP_IODIR, mask, false); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 445 | } |
| 446 | mutex_unlock(&mcp->lock); |
| 447 | return status; |
| 448 | } |
| 449 | |
| 450 | /*----------------------------------------------------------------------*/ |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 451 | static irqreturn_t mcp23s08_irq(int irq, void *data) |
| 452 | { |
| 453 | struct mcp23s08 *mcp = data; |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 454 | int intcap, intcon, intf, i, gpio, gpio_orig, intcap_mask, defval; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 455 | unsigned int child_irq; |
Robert Middleton | 2cd29f2 | 2017-03-15 16:56:47 -0400 | [diff] [blame] | 456 | bool intf_set, intcap_changed, gpio_bit_changed, |
| 457 | defval_changed, gpio_set; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 458 | |
| 459 | mutex_lock(&mcp->lock); |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 460 | if (mcp_read(mcp, MCP_INTF, &intf) < 0) { |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 461 | mutex_unlock(&mcp->lock); |
| 462 | return IRQ_HANDLED; |
| 463 | } |
| 464 | |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 465 | if (mcp_read(mcp, MCP_INTCAP, &intcap) < 0) { |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 466 | mutex_unlock(&mcp->lock); |
| 467 | return IRQ_HANDLED; |
| 468 | } |
| 469 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 470 | if (mcp_read(mcp, MCP_INTCON, &intcon) < 0) { |
| 471 | mutex_unlock(&mcp->lock); |
| 472 | return IRQ_HANDLED; |
| 473 | } |
| 474 | |
| 475 | if (mcp_read(mcp, MCP_DEFVAL, &defval) < 0) { |
| 476 | mutex_unlock(&mcp->lock); |
| 477 | return IRQ_HANDLED; |
| 478 | } |
Robert Middleton | 2cd29f2 | 2017-03-15 16:56:47 -0400 | [diff] [blame] | 479 | |
| 480 | /* This clears the interrupt(configurable on S18) */ |
| 481 | if (mcp_read(mcp, MCP_GPIO, &gpio) < 0) { |
| 482 | mutex_unlock(&mcp->lock); |
| 483 | return IRQ_HANDLED; |
| 484 | } |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 485 | gpio_orig = mcp->cached_gpio; |
| 486 | mcp->cached_gpio = gpio; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 487 | mutex_unlock(&mcp->lock); |
| 488 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 489 | if (intf == 0) { |
Robert Middleton | 2cd29f2 | 2017-03-15 16:56:47 -0400 | [diff] [blame] | 490 | /* There is no interrupt pending */ |
| 491 | return IRQ_HANDLED; |
| 492 | } |
| 493 | |
| 494 | dev_dbg(mcp->chip.parent, |
| 495 | "intcap 0x%04X intf 0x%04X gpio_orig 0x%04X gpio 0x%04X\n", |
| 496 | intcap, intf, gpio_orig, gpio); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 497 | |
| 498 | for (i = 0; i < mcp->chip.ngpio; i++) { |
Robert Middleton | 2cd29f2 | 2017-03-15 16:56:47 -0400 | [diff] [blame] | 499 | /* We must check all of the inputs on the chip, |
| 500 | * otherwise we may not notice a change on >=2 pins. |
| 501 | * |
| 502 | * On at least the mcp23s17, INTCAP is only updated |
| 503 | * one byte at a time(INTCAPA and INTCAPB are |
| 504 | * not written to at the same time - only on a per-bank |
| 505 | * basis). |
| 506 | * |
| 507 | * INTF only contains the single bit that caused the |
| 508 | * interrupt per-bank. On the mcp23s17, there is |
| 509 | * INTFA and INTFB. If two pins are changed on the A |
| 510 | * side at the same time, INTF will only have one bit |
| 511 | * set. If one pin on the A side and one pin on the B |
| 512 | * side are changed at the same time, INTF will have |
| 513 | * two bits set. Thus, INTF can't be the only check |
| 514 | * to see if the input has changed. |
| 515 | */ |
| 516 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 517 | intf_set = intf & BIT(i); |
Robert Middleton | 2cd29f2 | 2017-03-15 16:56:47 -0400 | [diff] [blame] | 518 | if (i < 8 && intf_set) |
| 519 | intcap_mask = 0x00FF; |
| 520 | else if (i >= 8 && intf_set) |
| 521 | intcap_mask = 0xFF00; |
| 522 | else |
| 523 | intcap_mask = 0x00; |
| 524 | |
| 525 | intcap_changed = (intcap_mask & |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 526 | (intcap & BIT(i))) != |
Robert Middleton | 2cd29f2 | 2017-03-15 16:56:47 -0400 | [diff] [blame] | 527 | (intcap_mask & (BIT(i) & gpio_orig)); |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 528 | gpio_set = BIT(i) & gpio; |
Robert Middleton | 2cd29f2 | 2017-03-15 16:56:47 -0400 | [diff] [blame] | 529 | gpio_bit_changed = (BIT(i) & gpio_orig) != |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 530 | (BIT(i) & gpio); |
| 531 | defval_changed = (BIT(i) & intcon) && |
| 532 | ((BIT(i) & gpio) != |
| 533 | (BIT(i) & defval)); |
Robert Middleton | 2cd29f2 | 2017-03-15 16:56:47 -0400 | [diff] [blame] | 534 | |
| 535 | if (((gpio_bit_changed || intcap_changed) && |
| 536 | (BIT(i) & mcp->irq_rise) && gpio_set) || |
| 537 | ((gpio_bit_changed || intcap_changed) && |
| 538 | (BIT(i) & mcp->irq_fall) && !gpio_set) || |
| 539 | defval_changed) { |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 540 | child_irq = irq_find_mapping(mcp->chip.irqdomain, i); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 541 | handle_nested_irq(child_irq); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | return IRQ_HANDLED; |
| 546 | } |
| 547 | |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 548 | static void mcp23s08_irq_mask(struct irq_data *data) |
| 549 | { |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 550 | struct gpio_chip *gc = irq_data_get_irq_chip_data(data); |
| 551 | struct mcp23s08 *mcp = gpiochip_get_data(gc); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 552 | unsigned int pos = data->hwirq; |
| 553 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 554 | mcp_set_bit(mcp, MCP_GPINTEN, pos, false); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | static void mcp23s08_irq_unmask(struct irq_data *data) |
| 558 | { |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 559 | struct gpio_chip *gc = irq_data_get_irq_chip_data(data); |
| 560 | struct mcp23s08 *mcp = gpiochip_get_data(gc); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 561 | unsigned int pos = data->hwirq; |
| 562 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 563 | mcp_set_bit(mcp, MCP_GPINTEN, pos, true); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | static int mcp23s08_irq_set_type(struct irq_data *data, unsigned int type) |
| 567 | { |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 568 | struct gpio_chip *gc = irq_data_get_irq_chip_data(data); |
| 569 | struct mcp23s08 *mcp = gpiochip_get_data(gc); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 570 | unsigned int pos = data->hwirq; |
| 571 | int status = 0; |
| 572 | |
| 573 | if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) { |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 574 | mcp_set_bit(mcp, MCP_INTCON, pos, false); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 575 | mcp->irq_rise |= BIT(pos); |
| 576 | mcp->irq_fall |= BIT(pos); |
| 577 | } else if (type & IRQ_TYPE_EDGE_RISING) { |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 578 | mcp_set_bit(mcp, MCP_INTCON, pos, false); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 579 | mcp->irq_rise |= BIT(pos); |
| 580 | mcp->irq_fall &= ~BIT(pos); |
| 581 | } else if (type & IRQ_TYPE_EDGE_FALLING) { |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 582 | mcp_set_bit(mcp, MCP_INTCON, pos, false); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 583 | mcp->irq_rise &= ~BIT(pos); |
| 584 | mcp->irq_fall |= BIT(pos); |
Alexander Stein | 16fe1ad | 2016-03-23 18:01:27 +0100 | [diff] [blame] | 585 | } else if (type & IRQ_TYPE_LEVEL_HIGH) { |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 586 | mcp_set_bit(mcp, MCP_INTCON, pos, true); |
| 587 | mcp_set_bit(mcp, MCP_DEFVAL, pos, false); |
Alexander Stein | 16fe1ad | 2016-03-23 18:01:27 +0100 | [diff] [blame] | 588 | } else if (type & IRQ_TYPE_LEVEL_LOW) { |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 589 | mcp_set_bit(mcp, MCP_INTCON, pos, true); |
| 590 | mcp_set_bit(mcp, MCP_DEFVAL, pos, true); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 591 | } else |
| 592 | return -EINVAL; |
| 593 | |
| 594 | return status; |
| 595 | } |
| 596 | |
| 597 | static void mcp23s08_irq_bus_lock(struct irq_data *data) |
| 598 | { |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 599 | struct gpio_chip *gc = irq_data_get_irq_chip_data(data); |
| 600 | struct mcp23s08 *mcp = gpiochip_get_data(gc); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 601 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 602 | mutex_lock(&mcp->lock); |
| 603 | regcache_cache_only(mcp->regmap, true); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | static void mcp23s08_irq_bus_unlock(struct irq_data *data) |
| 607 | { |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 608 | struct gpio_chip *gc = irq_data_get_irq_chip_data(data); |
| 609 | struct mcp23s08 *mcp = gpiochip_get_data(gc); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 610 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 611 | regcache_cache_only(mcp->regmap, false); |
| 612 | regcache_sync(mcp->regmap); |
| 613 | |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 614 | mutex_unlock(&mcp->lock); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 615 | } |
| 616 | |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 617 | static struct irq_chip mcp23s08_irq_chip = { |
| 618 | .name = "gpio-mcp23xxx", |
| 619 | .irq_mask = mcp23s08_irq_mask, |
| 620 | .irq_unmask = mcp23s08_irq_unmask, |
| 621 | .irq_set_type = mcp23s08_irq_set_type, |
| 622 | .irq_bus_lock = mcp23s08_irq_bus_lock, |
| 623 | .irq_bus_sync_unlock = mcp23s08_irq_bus_unlock, |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 624 | }; |
| 625 | |
| 626 | static int mcp23s08_irq_setup(struct mcp23s08 *mcp) |
| 627 | { |
| 628 | struct gpio_chip *chip = &mcp->chip; |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 629 | int err; |
Alexander Stein | a4e6355 | 2014-12-01 08:26:00 +0100 | [diff] [blame] | 630 | unsigned long irqflags = IRQF_ONESHOT | IRQF_SHARED; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 631 | |
Alexander Stein | a4e6355 | 2014-12-01 08:26:00 +0100 | [diff] [blame] | 632 | if (mcp->irq_active_high) |
| 633 | irqflags |= IRQF_TRIGGER_HIGH; |
| 634 | else |
| 635 | irqflags |= IRQF_TRIGGER_LOW; |
| 636 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 637 | err = devm_request_threaded_irq(chip->parent, mcp->irq, NULL, |
| 638 | mcp23s08_irq, |
| 639 | irqflags, dev_name(chip->parent), mcp); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 640 | if (err != 0) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 641 | dev_err(chip->parent, "unable to request IRQ#%d: %d\n", |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 642 | mcp->irq, err); |
| 643 | return err; |
| 644 | } |
| 645 | |
Linus Walleij | d245b3f | 2016-11-24 10:57:25 +0100 | [diff] [blame] | 646 | err = gpiochip_irqchip_add_nested(chip, |
| 647 | &mcp23s08_irq_chip, |
| 648 | 0, |
| 649 | handle_simple_irq, |
| 650 | IRQ_TYPE_NONE); |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 651 | if (err) { |
| 652 | dev_err(chip->parent, |
| 653 | "could not connect irqchip to gpiochip: %d\n", err); |
| 654 | return err; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 655 | } |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 656 | |
Linus Walleij | d245b3f | 2016-11-24 10:57:25 +0100 | [diff] [blame] | 657 | gpiochip_set_nested_irqchip(chip, |
| 658 | &mcp23s08_irq_chip, |
| 659 | mcp->irq); |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 660 | |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 661 | return 0; |
| 662 | } |
| 663 | |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 664 | /*----------------------------------------------------------------------*/ |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 665 | |
| 666 | #ifdef CONFIG_DEBUG_FS |
| 667 | |
| 668 | #include <linux/seq_file.h> |
| 669 | |
| 670 | /* |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 671 | * This compares the chip's registers with the register |
| 672 | * cache and corrects any incorrectly set register. This |
| 673 | * can be used to fix state for MCP23xxx, that temporary |
| 674 | * lost its power supply. |
| 675 | */ |
| 676 | #define MCP23S08_CONFIG_REGS 8 |
| 677 | static int __check_mcp23s08_reg_cache(struct mcp23s08 *mcp) |
| 678 | { |
| 679 | int cached[MCP23S08_CONFIG_REGS]; |
| 680 | int err = 0, i; |
| 681 | |
| 682 | /* read cached config registers */ |
| 683 | for (i = 0; i < MCP23S08_CONFIG_REGS; i++) { |
| 684 | err = mcp_read(mcp, i, &cached[i]); |
| 685 | if (err) |
| 686 | goto out; |
| 687 | } |
| 688 | |
| 689 | regcache_cache_bypass(mcp->regmap, true); |
| 690 | |
| 691 | for (i = 0; i < MCP23S08_CONFIG_REGS; i++) { |
| 692 | int uncached; |
| 693 | err = mcp_read(mcp, i, &uncached); |
| 694 | if (err) |
| 695 | goto out; |
| 696 | |
| 697 | if (uncached != cached[i]) { |
| 698 | dev_err(mcp->dev, "restoring reg 0x%02x from 0x%04x to 0x%04x (power-loss?)\n", |
| 699 | i, uncached, cached[i]); |
| 700 | mcp_write(mcp, i, cached[i]); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | out: |
| 705 | if (err) |
| 706 | dev_err(mcp->dev, "read error: reg=%02x, err=%d", i, err); |
| 707 | regcache_cache_bypass(mcp->regmap, false); |
| 708 | return err; |
| 709 | } |
| 710 | |
| 711 | /* |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 712 | * This shows more info than the generic gpio dump code: |
| 713 | * pullups, deglitching, open drain drive. |
| 714 | */ |
| 715 | static void mcp23s08_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 716 | { |
| 717 | struct mcp23s08 *mcp; |
| 718 | char bank; |
Roel Kluin | 1d1c1d9 | 2008-05-23 13:04:43 -0700 | [diff] [blame] | 719 | int t; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 720 | unsigned mask; |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 721 | int iodir, gpio, gppu; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 722 | |
Linus Walleij | 9e03cf0 | 2015-12-07 10:09:36 +0100 | [diff] [blame] | 723 | mcp = gpiochip_get_data(chip); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 724 | |
| 725 | /* NOTE: we only handle one bank for now ... */ |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 726 | bank = '0' + ((mcp->addr >> 1) & 0x7); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 727 | |
| 728 | mutex_lock(&mcp->lock); |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 729 | |
| 730 | t = __check_mcp23s08_reg_cache(mcp); |
| 731 | if (t) { |
| 732 | seq_printf(s, " I/O Error\n"); |
| 733 | goto done; |
| 734 | } |
| 735 | t = mcp_read(mcp, MCP_IODIR, &iodir); |
| 736 | if (t) { |
| 737 | seq_printf(s, " I/O Error\n"); |
| 738 | goto done; |
| 739 | } |
| 740 | t = mcp_read(mcp, MCP_GPIO, &gpio); |
| 741 | if (t) { |
| 742 | seq_printf(s, " I/O Error\n"); |
| 743 | goto done; |
| 744 | } |
| 745 | t = mcp_read(mcp, MCP_GPPU, &gppu); |
| 746 | if (t) { |
| 747 | seq_printf(s, " I/O Error\n"); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 748 | goto done; |
| 749 | } |
| 750 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 751 | for (t = 0, mask = BIT(0); t < chip->ngpio; t++, mask <<= 1) { |
| 752 | const char *label; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 753 | |
| 754 | label = gpiochip_is_requested(chip, t); |
| 755 | if (!label) |
| 756 | continue; |
| 757 | |
| 758 | seq_printf(s, " gpio-%-3d P%c.%d (%-12s) %s %s %s", |
| 759 | chip->base + t, bank, t, label, |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 760 | (iodir & mask) ? "in " : "out", |
| 761 | (gpio & mask) ? "hi" : "lo", |
| 762 | (gppu & mask) ? "up" : " "); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 763 | /* NOTE: ignoring the irq-related registers */ |
Gary Servin | 33bc8411 | 2014-03-06 20:25:26 -0300 | [diff] [blame] | 764 | seq_puts(s, "\n"); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 765 | } |
| 766 | done: |
| 767 | mutex_unlock(&mcp->lock); |
| 768 | } |
| 769 | |
| 770 | #else |
| 771 | #define mcp23s08_dbg_show NULL |
| 772 | #endif |
| 773 | |
| 774 | /*----------------------------------------------------------------------*/ |
| 775 | |
Peter Korsgaard | d62b98f | 2011-07-15 10:25:31 +0200 | [diff] [blame] | 776 | static int mcp23s08_probe_one(struct mcp23s08 *mcp, struct device *dev, |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 777 | void *data, unsigned addr, unsigned type, |
Sebastian Reichel | 5b1a7e8 | 2017-05-15 11:24:35 +0200 | [diff] [blame] | 778 | unsigned int base, int cs) |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 779 | { |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 780 | int status, ret; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 781 | bool mirror = false; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 782 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 783 | mutex_init(&mcp->lock); |
| 784 | |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 785 | mcp->dev = dev; |
Peter Korsgaard | d62b98f | 2011-07-15 10:25:31 +0200 | [diff] [blame] | 786 | mcp->addr = addr; |
Alexander Stein | a4e6355 | 2014-12-01 08:26:00 +0100 | [diff] [blame] | 787 | mcp->irq_active_high = false; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 788 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 789 | mcp->chip.direction_input = mcp23s08_direction_input; |
| 790 | mcp->chip.get = mcp23s08_get; |
| 791 | mcp->chip.direction_output = mcp23s08_direction_output; |
| 792 | mcp->chip.set = mcp23s08_set; |
| 793 | mcp->chip.dbg_show = mcp23s08_dbg_show; |
Linus Walleij | 60f749f | 2016-09-07 23:13:20 +0200 | [diff] [blame] | 794 | #ifdef CONFIG_OF_GPIO |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 795 | mcp->chip.of_gpio_n_cells = 2; |
| 796 | mcp->chip.of_node = dev->of_node; |
| 797 | #endif |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 798 | |
Peter Korsgaard | d62b98f | 2011-07-15 10:25:31 +0200 | [diff] [blame] | 799 | switch (type) { |
| 800 | #ifdef CONFIG_SPI_MASTER |
| 801 | case MCP_TYPE_S08: |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 802 | mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp, |
| 803 | &mcp23x08_regmap); |
| 804 | mcp->reg_shift = 0; |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 805 | mcp->chip.ngpio = 8; |
| 806 | mcp->chip.label = "mcp23s08"; |
Peter Korsgaard | d62b98f | 2011-07-15 10:25:31 +0200 | [diff] [blame] | 807 | break; |
| 808 | |
| 809 | case MCP_TYPE_S17: |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 810 | mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp, |
| 811 | &mcp23x17_regmap); |
| 812 | mcp->reg_shift = 1; |
Peter Korsgaard | d62b98f | 2011-07-15 10:25:31 +0200 | [diff] [blame] | 813 | mcp->chip.ngpio = 16; |
| 814 | mcp->chip.label = "mcp23s17"; |
| 815 | break; |
Phil Reid | 28c5a41 | 2016-03-01 14:25:41 +0800 | [diff] [blame] | 816 | |
| 817 | case MCP_TYPE_S18: |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 818 | mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp, |
| 819 | &mcp23x17_regmap); |
| 820 | mcp->reg_shift = 1; |
Phil Reid | 28c5a41 | 2016-03-01 14:25:41 +0800 | [diff] [blame] | 821 | mcp->chip.ngpio = 16; |
| 822 | mcp->chip.label = "mcp23s18"; |
| 823 | break; |
Peter Korsgaard | d62b98f | 2011-07-15 10:25:31 +0200 | [diff] [blame] | 824 | #endif /* CONFIG_SPI_MASTER */ |
| 825 | |
Daniel M. Weeks | cbf24fa | 2012-11-06 23:51:05 -0500 | [diff] [blame] | 826 | #if IS_ENABLED(CONFIG_I2C) |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 827 | case MCP_TYPE_008: |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 828 | mcp->regmap = devm_regmap_init_i2c(data, &mcp23x08_regmap); |
| 829 | mcp->reg_shift = 0; |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 830 | mcp->chip.ngpio = 8; |
| 831 | mcp->chip.label = "mcp23008"; |
| 832 | break; |
| 833 | |
| 834 | case MCP_TYPE_017: |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 835 | mcp->regmap = devm_regmap_init_i2c(data, &mcp23x17_regmap); |
| 836 | mcp->reg_shift = 1; |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 837 | mcp->chip.ngpio = 16; |
| 838 | mcp->chip.label = "mcp23017"; |
| 839 | break; |
| 840 | #endif /* CONFIG_I2C */ |
| 841 | |
Peter Korsgaard | d62b98f | 2011-07-15 10:25:31 +0200 | [diff] [blame] | 842 | default: |
| 843 | dev_err(dev, "invalid device type (%d)\n", type); |
| 844 | return -EINVAL; |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 845 | } |
Peter Korsgaard | d62b98f | 2011-07-15 10:25:31 +0200 | [diff] [blame] | 846 | |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 847 | if (IS_ERR(mcp->regmap)) |
| 848 | return PTR_ERR(mcp->regmap); |
| 849 | |
Sebastian Reichel | 5b1a7e8 | 2017-05-15 11:24:35 +0200 | [diff] [blame] | 850 | mcp->chip.base = base; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 851 | mcp->chip.can_sleep = true; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 852 | mcp->chip.parent = dev; |
Guennadi Liakhovetski | d72cbed | 2008-04-28 02:14:45 -0700 | [diff] [blame] | 853 | mcp->chip.owner = THIS_MODULE; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 854 | |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 855 | /* verify MCP_IOCON.SEQOP = 0, so sequential reads work, |
| 856 | * and MCP_IOCON.HAEN = 1, so we work with all chips. |
| 857 | */ |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 858 | |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 859 | ret = mcp_read(mcp, MCP_IOCON, &status); |
| 860 | if (ret < 0) |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 861 | goto fail; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 862 | |
Sebastian Reichel | 5b1a7e8 | 2017-05-15 11:24:35 +0200 | [diff] [blame] | 863 | mcp->irq_controller = |
| 864 | device_property_read_bool(dev, "interrupt-controller"); |
Alexander Stein | a4e6355 | 2014-12-01 08:26:00 +0100 | [diff] [blame] | 865 | if (mcp->irq && mcp->irq_controller) { |
Linus Walleij | 170680a | 2014-12-12 11:22:11 +0100 | [diff] [blame] | 866 | mcp->irq_active_high = |
Sebastian Reichel | 5b1a7e8 | 2017-05-15 11:24:35 +0200 | [diff] [blame] | 867 | device_property_read_bool(dev, |
Linus Walleij | 170680a | 2014-12-12 11:22:11 +0100 | [diff] [blame] | 868 | "microchip,irq-active-high"); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 869 | |
Sebastian Reichel | 5b1a7e8 | 2017-05-15 11:24:35 +0200 | [diff] [blame] | 870 | mirror = device_property_read_bool(dev, "microchip,irq-mirror"); |
Alexander Stein | a4e6355 | 2014-12-01 08:26:00 +0100 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | if ((status & IOCON_SEQOP) || !(status & IOCON_HAEN) || mirror || |
| 874 | mcp->irq_active_high) { |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 875 | /* mcp23s17 has IOCON twice, make sure they are in sync */ |
| 876 | status &= ~(IOCON_SEQOP | (IOCON_SEQOP << 8)); |
| 877 | status |= IOCON_HAEN | (IOCON_HAEN << 8); |
Alexander Stein | a4e6355 | 2014-12-01 08:26:00 +0100 | [diff] [blame] | 878 | if (mcp->irq_active_high) |
| 879 | status |= IOCON_INTPOL | (IOCON_INTPOL << 8); |
| 880 | else |
| 881 | status &= ~(IOCON_INTPOL | (IOCON_INTPOL << 8)); |
| 882 | |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 883 | if (mirror) |
| 884 | status |= IOCON_MIRROR | (IOCON_MIRROR << 8); |
| 885 | |
Phil Reid | 3539699 | 2016-03-15 15:46:30 +0800 | [diff] [blame] | 886 | if (type == MCP_TYPE_S18) |
| 887 | status |= IOCON_INTCC | (IOCON_INTCC << 8); |
| 888 | |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 889 | ret = mcp_write(mcp, MCP_IOCON, status); |
| 890 | if (ret < 0) |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 891 | goto fail; |
| 892 | } |
| 893 | |
Sebastian Reichel | d0e49da | 2017-05-15 11:24:32 +0200 | [diff] [blame] | 894 | ret = devm_gpiochip_add_data(dev, &mcp->chip, mcp); |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 895 | if (ret < 0) |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 896 | goto fail; |
| 897 | |
| 898 | if (mcp->irq && mcp->irq_controller) { |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 899 | ret = mcp23s08_irq_setup(mcp); |
| 900 | if (ret) |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 901 | goto fail; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 902 | } |
Sebastian Reichel | 82039d2 | 2017-05-15 11:24:26 +0200 | [diff] [blame] | 903 | |
| 904 | mcp->pinctrl_desc.name = "mcp23xxx-pinctrl"; |
| 905 | mcp->pinctrl_desc.pctlops = &mcp_pinctrl_ops; |
| 906 | mcp->pinctrl_desc.confops = &mcp_pinconf_ops; |
| 907 | mcp->pinctrl_desc.npins = mcp->chip.ngpio; |
| 908 | if (mcp->pinctrl_desc.npins == 8) |
| 909 | mcp->pinctrl_desc.pins = mcp23x08_pins; |
| 910 | else if (mcp->pinctrl_desc.npins == 16) |
| 911 | mcp->pinctrl_desc.pins = mcp23x17_pins; |
| 912 | mcp->pinctrl_desc.owner = THIS_MODULE; |
| 913 | |
| 914 | mcp->pctldev = devm_pinctrl_register(dev, &mcp->pinctrl_desc, mcp); |
| 915 | if (IS_ERR(mcp->pctldev)) { |
| 916 | ret = PTR_ERR(mcp->pctldev); |
| 917 | goto fail; |
| 918 | } |
| 919 | |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 920 | fail: |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 921 | if (ret < 0) |
| 922 | dev_dbg(dev, "can't setup chip %d, --> %d\n", addr, ret); |
| 923 | return ret; |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 926 | /*----------------------------------------------------------------------*/ |
| 927 | |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 928 | #ifdef CONFIG_OF |
| 929 | #ifdef CONFIG_SPI_MASTER |
Jingoo Han | ac79180 | 2014-05-07 18:05:17 +0900 | [diff] [blame] | 930 | static const struct of_device_id mcp23s08_spi_of_match[] = { |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 931 | { |
Lars Poeschel | 4597168 | 2013-08-28 10:38:50 +0200 | [diff] [blame] | 932 | .compatible = "microchip,mcp23s08", |
| 933 | .data = (void *) MCP_TYPE_S08, |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 934 | }, |
| 935 | { |
Lars Poeschel | 4597168 | 2013-08-28 10:38:50 +0200 | [diff] [blame] | 936 | .compatible = "microchip,mcp23s17", |
| 937 | .data = (void *) MCP_TYPE_S17, |
| 938 | }, |
Phil Reid | 28c5a41 | 2016-03-01 14:25:41 +0800 | [diff] [blame] | 939 | { |
| 940 | .compatible = "microchip,mcp23s18", |
| 941 | .data = (void *) MCP_TYPE_S18, |
| 942 | }, |
Lars Poeschel | 4597168 | 2013-08-28 10:38:50 +0200 | [diff] [blame] | 943 | /* NOTE: The use of the mcp prefix is deprecated and will be removed. */ |
| 944 | { |
| 945 | .compatible = "mcp,mcp23s08", |
| 946 | .data = (void *) MCP_TYPE_S08, |
| 947 | }, |
| 948 | { |
| 949 | .compatible = "mcp,mcp23s17", |
| 950 | .data = (void *) MCP_TYPE_S17, |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 951 | }, |
| 952 | { }, |
| 953 | }; |
| 954 | MODULE_DEVICE_TABLE(of, mcp23s08_spi_of_match); |
| 955 | #endif |
| 956 | |
| 957 | #if IS_ENABLED(CONFIG_I2C) |
Jingoo Han | ac79180 | 2014-05-07 18:05:17 +0900 | [diff] [blame] | 958 | static const struct of_device_id mcp23s08_i2c_of_match[] = { |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 959 | { |
Lars Poeschel | 4597168 | 2013-08-28 10:38:50 +0200 | [diff] [blame] | 960 | .compatible = "microchip,mcp23008", |
| 961 | .data = (void *) MCP_TYPE_008, |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 962 | }, |
| 963 | { |
Lars Poeschel | 4597168 | 2013-08-28 10:38:50 +0200 | [diff] [blame] | 964 | .compatible = "microchip,mcp23017", |
| 965 | .data = (void *) MCP_TYPE_017, |
| 966 | }, |
| 967 | /* NOTE: The use of the mcp prefix is deprecated and will be removed. */ |
| 968 | { |
| 969 | .compatible = "mcp,mcp23008", |
| 970 | .data = (void *) MCP_TYPE_008, |
| 971 | }, |
| 972 | { |
| 973 | .compatible = "mcp,mcp23017", |
| 974 | .data = (void *) MCP_TYPE_017, |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 975 | }, |
| 976 | { }, |
| 977 | }; |
| 978 | MODULE_DEVICE_TABLE(of, mcp23s08_i2c_of_match); |
| 979 | #endif |
| 980 | #endif /* CONFIG_OF */ |
| 981 | |
| 982 | |
Daniel M. Weeks | cbf24fa | 2012-11-06 23:51:05 -0500 | [diff] [blame] | 983 | #if IS_ENABLED(CONFIG_I2C) |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 984 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 985 | static int mcp230xx_probe(struct i2c_client *client, |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 986 | const struct i2c_device_id *id) |
| 987 | { |
Sonic Zhang | 3af0dbd | 2014-09-01 11:19:52 +0800 | [diff] [blame] | 988 | struct mcp23s08_platform_data *pdata, local_pdata; |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 989 | struct mcp23s08 *mcp; |
Sonic Zhang | 3af0dbd | 2014-09-01 11:19:52 +0800 | [diff] [blame] | 990 | int status; |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 991 | |
Sebastian Reichel | 5f853ac | 2017-05-15 11:24:33 +0200 | [diff] [blame] | 992 | pdata = dev_get_platdata(&client->dev); |
| 993 | if (!pdata) { |
Sonic Zhang | 3af0dbd | 2014-09-01 11:19:52 +0800 | [diff] [blame] | 994 | pdata = &local_pdata; |
| 995 | pdata->base = -1; |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 996 | } |
| 997 | |
Sebastian Reichel | 2f98e78 | 2017-05-15 11:24:31 +0200 | [diff] [blame] | 998 | mcp = devm_kzalloc(&client->dev, sizeof(*mcp), GFP_KERNEL); |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 999 | if (!mcp) |
| 1000 | return -ENOMEM; |
| 1001 | |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 1002 | mcp->irq = client->irq; |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 1003 | status = mcp23s08_probe_one(mcp, &client->dev, client, client->addr, |
Sebastian Reichel | 5b1a7e8 | 2017-05-15 11:24:35 +0200 | [diff] [blame] | 1004 | id->driver_data, pdata->base, 0); |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 1005 | if (status) |
Sebastian Reichel | 2f98e78 | 2017-05-15 11:24:31 +0200 | [diff] [blame] | 1006 | return status; |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 1007 | |
| 1008 | i2c_set_clientdata(client, mcp); |
| 1009 | |
| 1010 | return 0; |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 1011 | } |
| 1012 | |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 1013 | static const struct i2c_device_id mcp230xx_id[] = { |
| 1014 | { "mcp23008", MCP_TYPE_008 }, |
| 1015 | { "mcp23017", MCP_TYPE_017 }, |
| 1016 | { }, |
| 1017 | }; |
| 1018 | MODULE_DEVICE_TABLE(i2c, mcp230xx_id); |
| 1019 | |
| 1020 | static struct i2c_driver mcp230xx_driver = { |
| 1021 | .driver = { |
| 1022 | .name = "mcp230xx", |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 1023 | .of_match_table = of_match_ptr(mcp23s08_i2c_of_match), |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 1024 | }, |
| 1025 | .probe = mcp230xx_probe, |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 1026 | .id_table = mcp230xx_id, |
| 1027 | }; |
| 1028 | |
| 1029 | static int __init mcp23s08_i2c_init(void) |
| 1030 | { |
| 1031 | return i2c_add_driver(&mcp230xx_driver); |
| 1032 | } |
| 1033 | |
| 1034 | static void mcp23s08_i2c_exit(void) |
| 1035 | { |
| 1036 | i2c_del_driver(&mcp230xx_driver); |
| 1037 | } |
| 1038 | |
| 1039 | #else |
| 1040 | |
| 1041 | static int __init mcp23s08_i2c_init(void) { return 0; } |
| 1042 | static void mcp23s08_i2c_exit(void) { } |
| 1043 | |
| 1044 | #endif /* CONFIG_I2C */ |
| 1045 | |
| 1046 | /*----------------------------------------------------------------------*/ |
| 1047 | |
Peter Korsgaard | d62b98f | 2011-07-15 10:25:31 +0200 | [diff] [blame] | 1048 | #ifdef CONFIG_SPI_MASTER |
| 1049 | |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 1050 | static int mcp23s08_probe(struct spi_device *spi) |
| 1051 | { |
Sonic Zhang | 3af0dbd | 2014-09-01 11:19:52 +0800 | [diff] [blame] | 1052 | struct mcp23s08_platform_data *pdata, local_pdata; |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 1053 | unsigned addr; |
Linus Walleij | 596a1c5 | 2014-05-28 09:14:06 +0200 | [diff] [blame] | 1054 | int chips = 0; |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 1055 | struct mcp23s08_driver_data *data; |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 1056 | int status, type; |
Sonic Zhang | 3af0dbd | 2014-09-01 11:19:52 +0800 | [diff] [blame] | 1057 | unsigned ngpio = 0; |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 1058 | const struct of_device_id *match; |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 1059 | |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 1060 | match = of_match_device(of_match_ptr(mcp23s08_spi_of_match), &spi->dev); |
Sebastian Reichel | 0d7fcd5 | 2017-05-15 11:24:34 +0200 | [diff] [blame] | 1061 | if (match) |
SeongJae Park | de755c3 | 2014-01-18 13:53:04 +0900 | [diff] [blame] | 1062 | type = (int)(uintptr_t)match->data; |
Sebastian Reichel | 0d7fcd5 | 2017-05-15 11:24:34 +0200 | [diff] [blame] | 1063 | else |
| 1064 | type = spi_get_device_id(spi)->driver_data; |
| 1065 | |
| 1066 | pdata = dev_get_platdata(&spi->dev); |
| 1067 | if (!pdata) { |
| 1068 | pdata = &local_pdata; |
| 1069 | pdata->base = -1; |
| 1070 | |
Sebastian Reichel | 0d7fcd5 | 2017-05-15 11:24:34 +0200 | [diff] [blame] | 1071 | status = device_property_read_u32(&spi->dev, |
Sebastian Reichel | ce9bd0a | 2017-05-15 11:24:36 +0200 | [diff] [blame] | 1072 | "microchip,spi-present-mask", &pdata->spi_present_mask); |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 1073 | if (status) { |
Sebastian Reichel | 0d7fcd5 | 2017-05-15 11:24:34 +0200 | [diff] [blame] | 1074 | status = device_property_read_u32(&spi->dev, |
Sebastian Reichel | ce9bd0a | 2017-05-15 11:24:36 +0200 | [diff] [blame] | 1075 | "mcp,spi-present-mask", |
| 1076 | &pdata->spi_present_mask); |
Sebastian Reichel | 0d7fcd5 | 2017-05-15 11:24:34 +0200 | [diff] [blame] | 1077 | |
Lars Poeschel | 4597168 | 2013-08-28 10:38:50 +0200 | [diff] [blame] | 1078 | if (status) { |
Sebastian Reichel | 0d7fcd5 | 2017-05-15 11:24:34 +0200 | [diff] [blame] | 1079 | dev_err(&spi->dev, "missing spi-present-mask"); |
Lars Poeschel | 4597168 | 2013-08-28 10:38:50 +0200 | [diff] [blame] | 1080 | return -ENODEV; |
| 1081 | } |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 1082 | } |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 1083 | } |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 1084 | |
Sebastian Reichel | ce9bd0a | 2017-05-15 11:24:36 +0200 | [diff] [blame] | 1085 | if (!pdata->spi_present_mask || pdata->spi_present_mask > 0xff) { |
Sebastian Reichel | 0d7fcd5 | 2017-05-15 11:24:34 +0200 | [diff] [blame] | 1086 | dev_err(&spi->dev, "invalid spi-present-mask"); |
| 1087 | return -ENODEV; |
| 1088 | } |
| 1089 | |
Sebastian Reichel | ce9bd0a | 2017-05-15 11:24:36 +0200 | [diff] [blame] | 1090 | for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) { |
| 1091 | if (pdata->spi_present_mask & BIT(addr)) |
Sebastian Reichel | 0d7fcd5 | 2017-05-15 11:24:34 +0200 | [diff] [blame] | 1092 | chips++; |
| 1093 | } |
| 1094 | |
Michael Welling | 99e4b98 | 2014-04-16 20:00:24 -0500 | [diff] [blame] | 1095 | if (!chips) |
| 1096 | return -ENODEV; |
| 1097 | |
Varka Bhadram | 7898b31 | 2015-03-31 09:49:08 +0530 | [diff] [blame] | 1098 | data = devm_kzalloc(&spi->dev, |
| 1099 | sizeof(*data) + chips * sizeof(struct mcp23s08), |
| 1100 | GFP_KERNEL); |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 1101 | if (!data) |
| 1102 | return -ENOMEM; |
Varka Bhadram | 7898b31 | 2015-03-31 09:49:08 +0530 | [diff] [blame] | 1103 | |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 1104 | spi_set_drvdata(spi, data); |
| 1105 | |
Sebastian Reichel | ce9bd0a | 2017-05-15 11:24:36 +0200 | [diff] [blame] | 1106 | for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) { |
| 1107 | if (!(pdata->spi_present_mask & BIT(addr))) |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 1108 | continue; |
| 1109 | chips--; |
| 1110 | data->mcp[addr] = &data->chip[chips]; |
Alexander Stein | a231b88 | 2014-11-17 09:38:10 +0100 | [diff] [blame] | 1111 | data->mcp[addr]->irq = spi->irq; |
Peter Korsgaard | d62b98f | 2011-07-15 10:25:31 +0200 | [diff] [blame] | 1112 | status = mcp23s08_probe_one(data->mcp[addr], &spi->dev, spi, |
Sebastian Reichel | 5b1a7e8 | 2017-05-15 11:24:35 +0200 | [diff] [blame] | 1113 | 0x40 | (addr << 1), type, |
| 1114 | pdata->base, addr); |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 1115 | if (status < 0) |
Sebastian Reichel | d0e49da | 2017-05-15 11:24:32 +0200 | [diff] [blame] | 1116 | return status; |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 1117 | |
Sonic Zhang | 3af0dbd | 2014-09-01 11:19:52 +0800 | [diff] [blame] | 1118 | if (pdata->base != -1) |
Phil Reid | 28c5a41 | 2016-03-01 14:25:41 +0800 | [diff] [blame] | 1119 | pdata->base += data->mcp[addr]->chip.ngpio; |
| 1120 | ngpio += data->mcp[addr]->chip.ngpio; |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 1121 | } |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 1122 | data->ngpio = ngpio; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1123 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1124 | return 0; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1125 | } |
| 1126 | |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 1127 | static const struct spi_device_id mcp23s08_ids[] = { |
| 1128 | { "mcp23s08", MCP_TYPE_S08 }, |
| 1129 | { "mcp23s17", MCP_TYPE_S17 }, |
Phil Reid | 28c5a41 | 2016-03-01 14:25:41 +0800 | [diff] [blame] | 1130 | { "mcp23s18", MCP_TYPE_S18 }, |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 1131 | { }, |
| 1132 | }; |
| 1133 | MODULE_DEVICE_TABLE(spi, mcp23s08_ids); |
| 1134 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1135 | static struct spi_driver mcp23s08_driver = { |
| 1136 | .probe = mcp23s08_probe, |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 1137 | .id_table = mcp23s08_ids, |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1138 | .driver = { |
| 1139 | .name = "mcp23s08", |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 1140 | .of_match_table = of_match_ptr(mcp23s08_spi_of_match), |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1141 | }, |
| 1142 | }; |
| 1143 | |
Peter Korsgaard | d62b98f | 2011-07-15 10:25:31 +0200 | [diff] [blame] | 1144 | static int __init mcp23s08_spi_init(void) |
| 1145 | { |
| 1146 | return spi_register_driver(&mcp23s08_driver); |
| 1147 | } |
| 1148 | |
| 1149 | static void mcp23s08_spi_exit(void) |
| 1150 | { |
| 1151 | spi_unregister_driver(&mcp23s08_driver); |
| 1152 | } |
| 1153 | |
| 1154 | #else |
| 1155 | |
| 1156 | static int __init mcp23s08_spi_init(void) { return 0; } |
| 1157 | static void mcp23s08_spi_exit(void) { } |
| 1158 | |
| 1159 | #endif /* CONFIG_SPI_MASTER */ |
| 1160 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1161 | /*----------------------------------------------------------------------*/ |
| 1162 | |
| 1163 | static int __init mcp23s08_init(void) |
| 1164 | { |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 1165 | int ret; |
| 1166 | |
| 1167 | ret = mcp23s08_spi_init(); |
| 1168 | if (ret) |
| 1169 | goto spi_fail; |
| 1170 | |
| 1171 | ret = mcp23s08_i2c_init(); |
| 1172 | if (ret) |
| 1173 | goto i2c_fail; |
| 1174 | |
| 1175 | return 0; |
| 1176 | |
| 1177 | i2c_fail: |
| 1178 | mcp23s08_spi_exit(); |
| 1179 | spi_fail: |
| 1180 | return ret; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1181 | } |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 1182 | /* register after spi/i2c postcore initcall and before |
David Brownell | 673c0c0 | 2008-10-15 22:02:46 -0700 | [diff] [blame] | 1183 | * subsys initcalls that may rely on these GPIOs |
| 1184 | */ |
| 1185 | subsys_initcall(mcp23s08_init); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1186 | |
| 1187 | static void __exit mcp23s08_exit(void) |
| 1188 | { |
Peter Korsgaard | d62b98f | 2011-07-15 10:25:31 +0200 | [diff] [blame] | 1189 | mcp23s08_spi_exit(); |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 1190 | mcp23s08_i2c_exit(); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1191 | } |
| 1192 | module_exit(mcp23s08_exit); |
| 1193 | |
| 1194 | MODULE_LICENSE("GPL"); |