Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Sebastian Reichel | d8f4494 | 2017-05-15 11:24:37 +0200 | [diff] [blame] | 2 | /* MCP23S08 SPI/I2C GPIO driver */ |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 3 | |
| 4 | #include <linux/kernel.h> |
| 5 | #include <linux/device.h> |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 6 | #include <linux/mutex.h> |
Paul Gortmaker | bb207ef | 2011-07-03 13:38:09 -0400 | [diff] [blame] | 7 | #include <linux/module.h> |
Linus Walleij | 1c5fb66 | 2018-09-13 13:58:21 +0200 | [diff] [blame] | 8 | #include <linux/gpio/driver.h> |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 9 | #include <linux/i2c.h> |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 10 | #include <linux/spi/spi.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 |
Phil Reid | ff0f2ce | 2017-10-06 13:08:07 +0800 | [diff] [blame] | 28 | #define MCP_TYPE_018 5 |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 29 | |
| 30 | /* Registers are all 8 bits wide. |
| 31 | * |
| 32 | * The mcp23s17 has twice as many bits, and can be configured to work |
| 33 | * with either 16 bit registers or with two adjacent 8 bit banks. |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 34 | */ |
| 35 | #define MCP_IODIR 0x00 /* init/reset: all ones */ |
| 36 | #define MCP_IPOL 0x01 |
| 37 | #define MCP_GPINTEN 0x02 |
| 38 | #define MCP_DEFVAL 0x03 |
| 39 | #define MCP_INTCON 0x04 |
| 40 | #define MCP_IOCON 0x05 |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 41 | # define IOCON_MIRROR (1 << 6) |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 42 | # define IOCON_SEQOP (1 << 5) |
| 43 | # define IOCON_HAEN (1 << 3) |
| 44 | # define IOCON_ODR (1 << 2) |
| 45 | # define IOCON_INTPOL (1 << 1) |
Phil Reid | 3539699 | 2016-03-15 15:46:30 +0800 | [diff] [blame] | 46 | # define IOCON_INTCC (1) |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 47 | #define MCP_GPPU 0x06 |
| 48 | #define MCP_INTF 0x07 |
| 49 | #define MCP_INTCAP 0x08 |
| 50 | #define MCP_GPIO 0x09 |
| 51 | #define MCP_OLAT 0x0a |
| 52 | |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 53 | struct mcp23s08; |
| 54 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 55 | struct mcp23s08 { |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 56 | u8 addr; |
Alexander Stein | a4e6355 | 2014-12-01 08:26:00 +0100 | [diff] [blame] | 57 | bool irq_active_high; |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 58 | bool reg_shift; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 59 | |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 60 | u16 irq_rise; |
| 61 | u16 irq_fall; |
| 62 | int irq; |
| 63 | bool irq_controller; |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 64 | int cached_gpio; |
| 65 | /* lock protects regmap access with bypass/cache flags */ |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 66 | struct mutex lock; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 67 | |
| 68 | struct gpio_chip chip; |
Lars Poeschel | 19ab5ca | 2019-01-11 17:25:16 +0100 | [diff] [blame] | 69 | struct irq_chip irq_chip; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 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: |
Sebastian Reichel | 82039d2 | 2017-05-15 11:24:26 +0200 | [diff] [blame] | 267 | return -ENOTSUPP; |
| 268 | } |
| 269 | |
| 270 | *config = 0; |
| 271 | |
| 272 | return status ? 0 : -EINVAL; |
| 273 | } |
| 274 | |
| 275 | static int mcp_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin, |
| 276 | unsigned long *configs, unsigned int num_configs) |
| 277 | { |
| 278 | struct mcp23s08 *mcp = pinctrl_dev_get_drvdata(pctldev); |
| 279 | enum pin_config_param param; |
Phil Reid | 2a7893c | 2017-10-06 13:08:11 +0800 | [diff] [blame] | 280 | u32 arg; |
Sebastian Reichel | 82039d2 | 2017-05-15 11:24:26 +0200 | [diff] [blame] | 281 | int ret = 0; |
| 282 | int i; |
| 283 | |
| 284 | for (i = 0; i < num_configs; i++) { |
| 285 | param = pinconf_to_config_param(configs[i]); |
| 286 | arg = pinconf_to_config_argument(configs[i]); |
| 287 | |
| 288 | switch (param) { |
| 289 | case PIN_CONFIG_BIAS_PULL_UP: |
Sebastian Reichel | 82039d2 | 2017-05-15 11:24:26 +0200 | [diff] [blame] | 290 | ret = mcp_set_bit(mcp, MCP_GPPU, pin, arg); |
| 291 | break; |
| 292 | default: |
Jan Kundrát | e0e3169 | 2019-03-07 14:16:51 +0100 | [diff] [blame] | 293 | dev_dbg(mcp->dev, "Invalid config param %04x\n", param); |
Sebastian Reichel | 82039d2 | 2017-05-15 11:24:26 +0200 | [diff] [blame] | 294 | return -ENOTSUPP; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | return ret; |
| 299 | } |
| 300 | |
| 301 | static const struct pinconf_ops mcp_pinconf_ops = { |
| 302 | .pin_config_get = mcp_pinconf_get, |
| 303 | .pin_config_set = mcp_pinconf_set, |
| 304 | .is_generic = true, |
| 305 | }; |
| 306 | |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 307 | /*----------------------------------------------------------------------*/ |
| 308 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 309 | static int mcp23s08_direction_input(struct gpio_chip *chip, unsigned offset) |
| 310 | { |
Linus Walleij | 9e03cf0 | 2015-12-07 10:09:36 +0100 | [diff] [blame] | 311 | struct mcp23s08 *mcp = gpiochip_get_data(chip); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 312 | int status; |
| 313 | |
| 314 | mutex_lock(&mcp->lock); |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 315 | status = mcp_set_bit(mcp, MCP_IODIR, offset, true); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 316 | mutex_unlock(&mcp->lock); |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 317 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 318 | return status; |
| 319 | } |
| 320 | |
| 321 | static int mcp23s08_get(struct gpio_chip *chip, unsigned offset) |
| 322 | { |
Linus Walleij | 9e03cf0 | 2015-12-07 10:09:36 +0100 | [diff] [blame] | 323 | struct mcp23s08 *mcp = gpiochip_get_data(chip); |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 324 | int status, ret; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 325 | |
| 326 | mutex_lock(&mcp->lock); |
| 327 | |
| 328 | /* REVISIT reading this clears any IRQ ... */ |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 329 | ret = mcp_read(mcp, MCP_GPIO, &status); |
| 330 | if (ret < 0) |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 331 | status = 0; |
Dmitry Mastykin | 5986170 | 2017-10-18 17:21:02 +0300 | [diff] [blame] | 332 | else { |
| 333 | mcp->cached_gpio = status; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 334 | status = !!(status & (1 << offset)); |
Dmitry Mastykin | 5986170 | 2017-10-18 17:21:02 +0300 | [diff] [blame] | 335 | } |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 336 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 337 | mutex_unlock(&mcp->lock); |
| 338 | return status; |
| 339 | } |
| 340 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 341 | static int __mcp23s08_set(struct mcp23s08 *mcp, unsigned mask, bool value) |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 342 | { |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 343 | return mcp_set_mask(mcp, MCP_OLAT, mask, value); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static void mcp23s08_set(struct gpio_chip *chip, unsigned offset, int value) |
| 347 | { |
Linus Walleij | 9e03cf0 | 2015-12-07 10:09:36 +0100 | [diff] [blame] | 348 | struct mcp23s08 *mcp = gpiochip_get_data(chip); |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 349 | unsigned mask = BIT(offset); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 350 | |
| 351 | mutex_lock(&mcp->lock); |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 352 | __mcp23s08_set(mcp, mask, !!value); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 353 | mutex_unlock(&mcp->lock); |
| 354 | } |
| 355 | |
| 356 | static int |
| 357 | mcp23s08_direction_output(struct gpio_chip *chip, unsigned offset, int value) |
| 358 | { |
Linus Walleij | 9e03cf0 | 2015-12-07 10:09:36 +0100 | [diff] [blame] | 359 | struct mcp23s08 *mcp = gpiochip_get_data(chip); |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 360 | unsigned mask = BIT(offset); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 361 | int status; |
| 362 | |
| 363 | mutex_lock(&mcp->lock); |
| 364 | status = __mcp23s08_set(mcp, mask, value); |
| 365 | if (status == 0) { |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 366 | status = mcp_set_mask(mcp, MCP_IODIR, mask, false); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 367 | } |
| 368 | mutex_unlock(&mcp->lock); |
| 369 | return status; |
| 370 | } |
| 371 | |
| 372 | /*----------------------------------------------------------------------*/ |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 373 | static irqreturn_t mcp23s08_irq(int irq, void *data) |
| 374 | { |
| 375 | struct mcp23s08 *mcp = data; |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 376 | int intcap, intcon, intf, i, gpio, gpio_orig, intcap_mask, defval; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 377 | unsigned int child_irq; |
Robert Middleton | 2cd29f2 | 2017-03-15 16:56:47 -0400 | [diff] [blame] | 378 | bool intf_set, intcap_changed, gpio_bit_changed, |
| 379 | defval_changed, gpio_set; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 380 | |
| 381 | mutex_lock(&mcp->lock); |
Markus Elfring | 7f6f50d | 2017-10-30 16:03:12 +0100 | [diff] [blame] | 382 | if (mcp_read(mcp, MCP_INTF, &intf)) |
| 383 | goto unlock; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 384 | |
Markus Elfring | 7f6f50d | 2017-10-30 16:03:12 +0100 | [diff] [blame] | 385 | if (mcp_read(mcp, MCP_INTCAP, &intcap)) |
| 386 | goto unlock; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 387 | |
Markus Elfring | 7f6f50d | 2017-10-30 16:03:12 +0100 | [diff] [blame] | 388 | if (mcp_read(mcp, MCP_INTCON, &intcon)) |
| 389 | goto unlock; |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 390 | |
Markus Elfring | 7f6f50d | 2017-10-30 16:03:12 +0100 | [diff] [blame] | 391 | if (mcp_read(mcp, MCP_DEFVAL, &defval)) |
| 392 | goto unlock; |
Robert Middleton | 2cd29f2 | 2017-03-15 16:56:47 -0400 | [diff] [blame] | 393 | |
| 394 | /* This clears the interrupt(configurable on S18) */ |
Markus Elfring | 7f6f50d | 2017-10-30 16:03:12 +0100 | [diff] [blame] | 395 | if (mcp_read(mcp, MCP_GPIO, &gpio)) |
| 396 | goto unlock; |
| 397 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 398 | gpio_orig = mcp->cached_gpio; |
| 399 | mcp->cached_gpio = gpio; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 400 | mutex_unlock(&mcp->lock); |
| 401 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 402 | if (intf == 0) { |
Robert Middleton | 2cd29f2 | 2017-03-15 16:56:47 -0400 | [diff] [blame] | 403 | /* There is no interrupt pending */ |
| 404 | return IRQ_HANDLED; |
| 405 | } |
| 406 | |
| 407 | dev_dbg(mcp->chip.parent, |
| 408 | "intcap 0x%04X intf 0x%04X gpio_orig 0x%04X gpio 0x%04X\n", |
| 409 | intcap, intf, gpio_orig, gpio); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 410 | |
| 411 | for (i = 0; i < mcp->chip.ngpio; i++) { |
Robert Middleton | 2cd29f2 | 2017-03-15 16:56:47 -0400 | [diff] [blame] | 412 | /* We must check all of the inputs on the chip, |
| 413 | * otherwise we may not notice a change on >=2 pins. |
| 414 | * |
| 415 | * On at least the mcp23s17, INTCAP is only updated |
| 416 | * one byte at a time(INTCAPA and INTCAPB are |
| 417 | * not written to at the same time - only on a per-bank |
| 418 | * basis). |
| 419 | * |
| 420 | * INTF only contains the single bit that caused the |
| 421 | * interrupt per-bank. On the mcp23s17, there is |
| 422 | * INTFA and INTFB. If two pins are changed on the A |
| 423 | * side at the same time, INTF will only have one bit |
| 424 | * set. If one pin on the A side and one pin on the B |
| 425 | * side are changed at the same time, INTF will have |
| 426 | * two bits set. Thus, INTF can't be the only check |
| 427 | * to see if the input has changed. |
| 428 | */ |
| 429 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 430 | intf_set = intf & BIT(i); |
Robert Middleton | 2cd29f2 | 2017-03-15 16:56:47 -0400 | [diff] [blame] | 431 | if (i < 8 && intf_set) |
| 432 | intcap_mask = 0x00FF; |
| 433 | else if (i >= 8 && intf_set) |
| 434 | intcap_mask = 0xFF00; |
| 435 | else |
| 436 | intcap_mask = 0x00; |
| 437 | |
| 438 | intcap_changed = (intcap_mask & |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 439 | (intcap & BIT(i))) != |
Robert Middleton | 2cd29f2 | 2017-03-15 16:56:47 -0400 | [diff] [blame] | 440 | (intcap_mask & (BIT(i) & gpio_orig)); |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 441 | gpio_set = BIT(i) & gpio; |
Robert Middleton | 2cd29f2 | 2017-03-15 16:56:47 -0400 | [diff] [blame] | 442 | gpio_bit_changed = (BIT(i) & gpio_orig) != |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 443 | (BIT(i) & gpio); |
| 444 | defval_changed = (BIT(i) & intcon) && |
| 445 | ((BIT(i) & gpio) != |
| 446 | (BIT(i) & defval)); |
Robert Middleton | 2cd29f2 | 2017-03-15 16:56:47 -0400 | [diff] [blame] | 447 | |
| 448 | if (((gpio_bit_changed || intcap_changed) && |
| 449 | (BIT(i) & mcp->irq_rise) && gpio_set) || |
| 450 | ((gpio_bit_changed || intcap_changed) && |
| 451 | (BIT(i) & mcp->irq_fall) && !gpio_set) || |
| 452 | defval_changed) { |
Thierry Reding | f0fbe7b | 2017-11-07 19:15:47 +0100 | [diff] [blame] | 453 | child_irq = irq_find_mapping(mcp->chip.irq.domain, i); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 454 | handle_nested_irq(child_irq); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | return IRQ_HANDLED; |
Markus Elfring | 7f6f50d | 2017-10-30 16:03:12 +0100 | [diff] [blame] | 459 | |
| 460 | unlock: |
| 461 | mutex_unlock(&mcp->lock); |
| 462 | return IRQ_HANDLED; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 463 | } |
| 464 | |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 465 | static void mcp23s08_irq_mask(struct irq_data *data) |
| 466 | { |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 467 | struct gpio_chip *gc = irq_data_get_irq_chip_data(data); |
| 468 | struct mcp23s08 *mcp = gpiochip_get_data(gc); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 469 | unsigned int pos = data->hwirq; |
| 470 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 471 | mcp_set_bit(mcp, MCP_GPINTEN, pos, false); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | static void mcp23s08_irq_unmask(struct irq_data *data) |
| 475 | { |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 476 | struct gpio_chip *gc = irq_data_get_irq_chip_data(data); |
| 477 | struct mcp23s08 *mcp = gpiochip_get_data(gc); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 478 | unsigned int pos = data->hwirq; |
| 479 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 480 | mcp_set_bit(mcp, MCP_GPINTEN, pos, true); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static int mcp23s08_irq_set_type(struct irq_data *data, unsigned int type) |
| 484 | { |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 485 | struct gpio_chip *gc = irq_data_get_irq_chip_data(data); |
| 486 | struct mcp23s08 *mcp = gpiochip_get_data(gc); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 487 | unsigned int pos = data->hwirq; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 488 | |
| 489 | if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) { |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 490 | mcp_set_bit(mcp, MCP_INTCON, pos, false); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 491 | mcp->irq_rise |= BIT(pos); |
| 492 | mcp->irq_fall |= BIT(pos); |
| 493 | } else if (type & IRQ_TYPE_EDGE_RISING) { |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 494 | mcp_set_bit(mcp, MCP_INTCON, pos, false); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 495 | mcp->irq_rise |= BIT(pos); |
| 496 | mcp->irq_fall &= ~BIT(pos); |
| 497 | } else if (type & IRQ_TYPE_EDGE_FALLING) { |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 498 | mcp_set_bit(mcp, MCP_INTCON, pos, false); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 499 | mcp->irq_rise &= ~BIT(pos); |
| 500 | mcp->irq_fall |= BIT(pos); |
Alexander Stein | 16fe1ad | 2016-03-23 18:01:27 +0100 | [diff] [blame] | 501 | } else if (type & IRQ_TYPE_LEVEL_HIGH) { |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 502 | mcp_set_bit(mcp, MCP_INTCON, pos, true); |
| 503 | mcp_set_bit(mcp, MCP_DEFVAL, pos, false); |
Alexander Stein | 16fe1ad | 2016-03-23 18:01:27 +0100 | [diff] [blame] | 504 | } else if (type & IRQ_TYPE_LEVEL_LOW) { |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 505 | mcp_set_bit(mcp, MCP_INTCON, pos, true); |
| 506 | mcp_set_bit(mcp, MCP_DEFVAL, pos, true); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 507 | } else |
| 508 | return -EINVAL; |
| 509 | |
Andy Shevchenko | 88af89b | 2020-04-07 20:38:46 +0300 | [diff] [blame^] | 510 | return 0; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static void mcp23s08_irq_bus_lock(struct irq_data *data) |
| 514 | { |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 515 | struct gpio_chip *gc = irq_data_get_irq_chip_data(data); |
| 516 | struct mcp23s08 *mcp = gpiochip_get_data(gc); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 517 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 518 | mutex_lock(&mcp->lock); |
| 519 | regcache_cache_only(mcp->regmap, true); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | static void mcp23s08_irq_bus_unlock(struct irq_data *data) |
| 523 | { |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 524 | struct gpio_chip *gc = irq_data_get_irq_chip_data(data); |
| 525 | struct mcp23s08 *mcp = gpiochip_get_data(gc); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 526 | |
Sebastian Reichel | 8f38910 | 2017-05-15 11:24:28 +0200 | [diff] [blame] | 527 | regcache_cache_only(mcp->regmap, false); |
| 528 | regcache_sync(mcp->regmap); |
| 529 | |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 530 | mutex_unlock(&mcp->lock); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 531 | } |
| 532 | |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 533 | static int mcp23s08_irq_setup(struct mcp23s08 *mcp) |
| 534 | { |
| 535 | struct gpio_chip *chip = &mcp->chip; |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 536 | int err; |
Alexander Stein | a4e6355 | 2014-12-01 08:26:00 +0100 | [diff] [blame] | 537 | unsigned long irqflags = IRQF_ONESHOT | IRQF_SHARED; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 538 | |
Alexander Stein | a4e6355 | 2014-12-01 08:26:00 +0100 | [diff] [blame] | 539 | if (mcp->irq_active_high) |
| 540 | irqflags |= IRQF_TRIGGER_HIGH; |
| 541 | else |
| 542 | irqflags |= IRQF_TRIGGER_LOW; |
| 543 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 544 | err = devm_request_threaded_irq(chip->parent, mcp->irq, NULL, |
| 545 | mcp23s08_irq, |
| 546 | irqflags, dev_name(chip->parent), mcp); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 547 | if (err != 0) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 548 | dev_err(chip->parent, "unable to request IRQ#%d: %d\n", |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 549 | mcp->irq, err); |
| 550 | return err; |
| 551 | } |
| 552 | |
Marco Felsch | f259f89 | 2018-10-02 10:06:46 +0200 | [diff] [blame] | 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | static int mcp23s08_irqchip_setup(struct mcp23s08 *mcp) |
| 557 | { |
| 558 | struct gpio_chip *chip = &mcp->chip; |
| 559 | int err; |
| 560 | |
Linus Walleij | d245b3f | 2016-11-24 10:57:25 +0100 | [diff] [blame] | 561 | err = gpiochip_irqchip_add_nested(chip, |
Lars Poeschel | 19ab5ca | 2019-01-11 17:25:16 +0100 | [diff] [blame] | 562 | &mcp->irq_chip, |
Linus Walleij | d245b3f | 2016-11-24 10:57:25 +0100 | [diff] [blame] | 563 | 0, |
| 564 | handle_simple_irq, |
| 565 | IRQ_TYPE_NONE); |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 566 | if (err) { |
| 567 | dev_err(chip->parent, |
| 568 | "could not connect irqchip to gpiochip: %d\n", err); |
| 569 | return err; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 570 | } |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 571 | |
Linus Walleij | d245b3f | 2016-11-24 10:57:25 +0100 | [diff] [blame] | 572 | gpiochip_set_nested_irqchip(chip, |
Lars Poeschel | 19ab5ca | 2019-01-11 17:25:16 +0100 | [diff] [blame] | 573 | &mcp->irq_chip, |
Linus Walleij | d245b3f | 2016-11-24 10:57:25 +0100 | [diff] [blame] | 574 | mcp->irq); |
Phil Reid | dad3d27 | 2016-03-18 16:07:06 +0800 | [diff] [blame] | 575 | |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 576 | return 0; |
| 577 | } |
| 578 | |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 579 | /*----------------------------------------------------------------------*/ |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 580 | |
Peter Korsgaard | d62b98f | 2011-07-15 10:25:31 +0200 | [diff] [blame] | 581 | static int mcp23s08_probe_one(struct mcp23s08 *mcp, struct device *dev, |
Andy Shevchenko | d3da29b | 2020-04-07 20:38:43 +0300 | [diff] [blame] | 582 | unsigned int addr, unsigned int type, |
Andy Shevchenko | 0521701 | 2020-04-07 20:38:44 +0300 | [diff] [blame] | 583 | unsigned int base) |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 584 | { |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 585 | int status, ret; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 586 | bool mirror = false; |
Phil Reid | fa2b7fa | 2018-02-19 17:25:20 +0800 | [diff] [blame] | 587 | bool open_drain = false; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 588 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 589 | mutex_init(&mcp->lock); |
| 590 | |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 591 | mcp->dev = dev; |
Peter Korsgaard | d62b98f | 2011-07-15 10:25:31 +0200 | [diff] [blame] | 592 | mcp->addr = addr; |
Andy Shevchenko | 84d02e7 | 2020-04-07 20:38:42 +0300 | [diff] [blame] | 593 | |
Alexander Stein | a4e6355 | 2014-12-01 08:26:00 +0100 | [diff] [blame] | 594 | mcp->irq_active_high = false; |
Andy Shevchenko | 84d02e7 | 2020-04-07 20:38:42 +0300 | [diff] [blame] | 595 | mcp->irq_chip.name = dev_name(dev); |
| 596 | mcp->irq_chip.irq_mask = mcp23s08_irq_mask; |
| 597 | mcp->irq_chip.irq_unmask = mcp23s08_irq_unmask; |
| 598 | mcp->irq_chip.irq_set_type = mcp23s08_irq_set_type; |
| 599 | mcp->irq_chip.irq_bus_lock = mcp23s08_irq_bus_lock; |
| 600 | mcp->irq_chip.irq_bus_sync_unlock = mcp23s08_irq_bus_unlock; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 601 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 602 | mcp->chip.direction_input = mcp23s08_direction_input; |
| 603 | mcp->chip.get = mcp23s08_get; |
| 604 | mcp->chip.direction_output = mcp23s08_direction_output; |
| 605 | mcp->chip.set = mcp23s08_set; |
Linus Walleij | 60f749f | 2016-09-07 23:13:20 +0200 | [diff] [blame] | 606 | #ifdef CONFIG_OF_GPIO |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 607 | mcp->chip.of_gpio_n_cells = 2; |
| 608 | mcp->chip.of_node = dev->of_node; |
| 609 | #endif |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 610 | |
Sebastian Reichel | 5b1a7e8 | 2017-05-15 11:24:35 +0200 | [diff] [blame] | 611 | mcp->chip.base = base; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 612 | mcp->chip.can_sleep = true; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 613 | mcp->chip.parent = dev; |
Guennadi Liakhovetski | d72cbed | 2008-04-28 02:14:45 -0700 | [diff] [blame] | 614 | mcp->chip.owner = THIS_MODULE; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 615 | |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 616 | /* verify MCP_IOCON.SEQOP = 0, so sequential reads work, |
| 617 | * and MCP_IOCON.HAEN = 1, so we work with all chips. |
| 618 | */ |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 619 | |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 620 | ret = mcp_read(mcp, MCP_IOCON, &status); |
| 621 | if (ret < 0) |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 622 | goto fail; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 623 | |
Phil Reid | 6dbc6e6 | 2019-06-13 12:10:23 +0800 | [diff] [blame] | 624 | ret = devm_gpiochip_add_data(dev, &mcp->chip, mcp); |
| 625 | if (ret < 0) |
| 626 | goto fail; |
| 627 | |
Sebastian Reichel | 5b1a7e8 | 2017-05-15 11:24:35 +0200 | [diff] [blame] | 628 | mcp->irq_controller = |
| 629 | device_property_read_bool(dev, "interrupt-controller"); |
Alexander Stein | a4e6355 | 2014-12-01 08:26:00 +0100 | [diff] [blame] | 630 | if (mcp->irq && mcp->irq_controller) { |
Linus Walleij | 170680a | 2014-12-12 11:22:11 +0100 | [diff] [blame] | 631 | mcp->irq_active_high = |
Sebastian Reichel | 5b1a7e8 | 2017-05-15 11:24:35 +0200 | [diff] [blame] | 632 | device_property_read_bool(dev, |
Linus Walleij | 170680a | 2014-12-12 11:22:11 +0100 | [diff] [blame] | 633 | "microchip,irq-active-high"); |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 634 | |
Sebastian Reichel | 5b1a7e8 | 2017-05-15 11:24:35 +0200 | [diff] [blame] | 635 | mirror = device_property_read_bool(dev, "microchip,irq-mirror"); |
Phil Reid | fa2b7fa | 2018-02-19 17:25:20 +0800 | [diff] [blame] | 636 | open_drain = device_property_read_bool(dev, "drive-open-drain"); |
Alexander Stein | a4e6355 | 2014-12-01 08:26:00 +0100 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | if ((status & IOCON_SEQOP) || !(status & IOCON_HAEN) || mirror || |
Phil Reid | fa2b7fa | 2018-02-19 17:25:20 +0800 | [diff] [blame] | 640 | mcp->irq_active_high || open_drain) { |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 641 | /* mcp23s17 has IOCON twice, make sure they are in sync */ |
| 642 | status &= ~(IOCON_SEQOP | (IOCON_SEQOP << 8)); |
| 643 | status |= IOCON_HAEN | (IOCON_HAEN << 8); |
Alexander Stein | a4e6355 | 2014-12-01 08:26:00 +0100 | [diff] [blame] | 644 | if (mcp->irq_active_high) |
| 645 | status |= IOCON_INTPOL | (IOCON_INTPOL << 8); |
| 646 | else |
| 647 | status &= ~(IOCON_INTPOL | (IOCON_INTPOL << 8)); |
| 648 | |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 649 | if (mirror) |
| 650 | status |= IOCON_MIRROR | (IOCON_MIRROR << 8); |
| 651 | |
Phil Reid | fa2b7fa | 2018-02-19 17:25:20 +0800 | [diff] [blame] | 652 | if (open_drain) |
| 653 | status |= IOCON_ODR | (IOCON_ODR << 8); |
| 654 | |
Phil Reid | ff0f2ce | 2017-10-06 13:08:07 +0800 | [diff] [blame] | 655 | if (type == MCP_TYPE_S18 || type == MCP_TYPE_018) |
Phil Reid | 3539699 | 2016-03-15 15:46:30 +0800 | [diff] [blame] | 656 | status |= IOCON_INTCC | (IOCON_INTCC << 8); |
| 657 | |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 658 | ret = mcp_write(mcp, MCP_IOCON, status); |
| 659 | if (ret < 0) |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 660 | goto fail; |
| 661 | } |
| 662 | |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 663 | if (mcp->irq && mcp->irq_controller) { |
Marco Felsch | f259f89 | 2018-10-02 10:06:46 +0200 | [diff] [blame] | 664 | ret = mcp23s08_irqchip_setup(mcp); |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 665 | if (ret) |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 666 | goto fail; |
Lars Poeschel | 4e47f91 | 2014-01-16 11:44:15 +0100 | [diff] [blame] | 667 | } |
Sebastian Reichel | 82039d2 | 2017-05-15 11:24:26 +0200 | [diff] [blame] | 668 | |
Sebastian Reichel | 82039d2 | 2017-05-15 11:24:26 +0200 | [diff] [blame] | 669 | mcp->pinctrl_desc.pctlops = &mcp_pinctrl_ops; |
| 670 | mcp->pinctrl_desc.confops = &mcp_pinconf_ops; |
| 671 | mcp->pinctrl_desc.npins = mcp->chip.ngpio; |
| 672 | if (mcp->pinctrl_desc.npins == 8) |
| 673 | mcp->pinctrl_desc.pins = mcp23x08_pins; |
| 674 | else if (mcp->pinctrl_desc.npins == 16) |
| 675 | mcp->pinctrl_desc.pins = mcp23x17_pins; |
| 676 | mcp->pinctrl_desc.owner = THIS_MODULE; |
| 677 | |
| 678 | mcp->pctldev = devm_pinctrl_register(dev, &mcp->pinctrl_desc, mcp); |
| 679 | if (IS_ERR(mcp->pctldev)) { |
| 680 | ret = PTR_ERR(mcp->pctldev); |
| 681 | goto fail; |
| 682 | } |
| 683 | |
Marco Felsch | f259f89 | 2018-10-02 10:06:46 +0200 | [diff] [blame] | 684 | if (mcp->irq) |
| 685 | ret = mcp23s08_irq_setup(mcp); |
| 686 | |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 687 | fail: |
Sebastian Reichel | 3d84fdb | 2017-01-27 15:47:37 +0100 | [diff] [blame] | 688 | if (ret < 0) |
| 689 | dev_dbg(dev, "can't setup chip %d, --> %d\n", addr, ret); |
| 690 | return ret; |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 693 | /*----------------------------------------------------------------------*/ |
| 694 | |
Andy Shevchenko | d3da29b | 2020-04-07 20:38:43 +0300 | [diff] [blame] | 695 | #if IS_ENABLED(CONFIG_I2C) |
| 696 | |
| 697 | static int mcp230xx_probe(struct i2c_client *client, |
| 698 | const struct i2c_device_id *id) |
| 699 | { |
| 700 | struct device *dev = &client->dev; |
| 701 | unsigned int type = id->driver_data; |
| 702 | struct mcp23s08 *mcp; |
| 703 | int status; |
| 704 | |
| 705 | mcp = devm_kzalloc(&client->dev, sizeof(*mcp), GFP_KERNEL); |
| 706 | if (!mcp) |
| 707 | return -ENOMEM; |
| 708 | |
| 709 | mcp->irq = client->irq; |
| 710 | |
| 711 | switch (type) { |
| 712 | case MCP_TYPE_008: |
| 713 | mcp->regmap = devm_regmap_init_i2c(client, &mcp23x08_regmap); |
| 714 | mcp->reg_shift = 0; |
| 715 | mcp->chip.ngpio = 8; |
| 716 | mcp->chip.label = "mcp23008"; |
| 717 | break; |
| 718 | |
| 719 | case MCP_TYPE_017: |
| 720 | mcp->regmap = devm_regmap_init_i2c(client, &mcp23x17_regmap); |
| 721 | mcp->reg_shift = 1; |
| 722 | mcp->chip.ngpio = 16; |
| 723 | mcp->chip.label = "mcp23017"; |
| 724 | break; |
| 725 | |
| 726 | case MCP_TYPE_018: |
| 727 | mcp->regmap = devm_regmap_init_i2c(client, &mcp23x17_regmap); |
| 728 | mcp->reg_shift = 1; |
| 729 | mcp->chip.ngpio = 16; |
| 730 | mcp->chip.label = "mcp23018"; |
| 731 | break; |
| 732 | |
| 733 | default: |
| 734 | dev_err(dev, "invalid device type (%d)\n", type); |
| 735 | return -EINVAL; |
| 736 | } |
| 737 | |
| 738 | if (IS_ERR(mcp->regmap)) |
| 739 | return PTR_ERR(mcp->regmap); |
| 740 | |
| 741 | mcp->pinctrl_desc.name = "mcp23xxx-pinctrl"; |
| 742 | |
Andy Shevchenko | 0521701 | 2020-04-07 20:38:44 +0300 | [diff] [blame] | 743 | status = mcp23s08_probe_one(mcp, dev, client->addr, type, -1); |
Andy Shevchenko | d3da29b | 2020-04-07 20:38:43 +0300 | [diff] [blame] | 744 | if (status) |
| 745 | return status; |
| 746 | |
| 747 | i2c_set_clientdata(client, mcp); |
| 748 | |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | static const struct i2c_device_id mcp230xx_id[] = { |
| 753 | { "mcp23008", MCP_TYPE_008 }, |
| 754 | { "mcp23017", MCP_TYPE_017 }, |
| 755 | { "mcp23018", MCP_TYPE_018 }, |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 756 | { }, |
| 757 | }; |
Andy Shevchenko | d3da29b | 2020-04-07 20:38:43 +0300 | [diff] [blame] | 758 | MODULE_DEVICE_TABLE(i2c, mcp230xx_id); |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 759 | |
Andy Shevchenko | d3da29b | 2020-04-07 20:38:43 +0300 | [diff] [blame] | 760 | #ifdef CONFIG_OF |
Jingoo Han | ac79180 | 2014-05-07 18:05:17 +0900 | [diff] [blame] | 761 | static const struct of_device_id mcp23s08_i2c_of_match[] = { |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 762 | { |
Lars Poeschel | 4597168 | 2013-08-28 10:38:50 +0200 | [diff] [blame] | 763 | .compatible = "microchip,mcp23008", |
| 764 | .data = (void *) MCP_TYPE_008, |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 765 | }, |
| 766 | { |
Lars Poeschel | 4597168 | 2013-08-28 10:38:50 +0200 | [diff] [blame] | 767 | .compatible = "microchip,mcp23017", |
| 768 | .data = (void *) MCP_TYPE_017, |
| 769 | }, |
Phil Reid | ff0f2ce | 2017-10-06 13:08:07 +0800 | [diff] [blame] | 770 | { |
| 771 | .compatible = "microchip,mcp23018", |
| 772 | .data = (void *) MCP_TYPE_018, |
| 773 | }, |
Lars Poeschel | 4597168 | 2013-08-28 10:38:50 +0200 | [diff] [blame] | 774 | /* NOTE: The use of the mcp prefix is deprecated and will be removed. */ |
| 775 | { |
| 776 | .compatible = "mcp,mcp23008", |
| 777 | .data = (void *) MCP_TYPE_008, |
| 778 | }, |
| 779 | { |
| 780 | .compatible = "mcp,mcp23017", |
| 781 | .data = (void *) MCP_TYPE_017, |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 782 | }, |
| 783 | { }, |
| 784 | }; |
| 785 | MODULE_DEVICE_TABLE(of, mcp23s08_i2c_of_match); |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 786 | #endif /* CONFIG_OF */ |
| 787 | |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 788 | static struct i2c_driver mcp230xx_driver = { |
| 789 | .driver = { |
| 790 | .name = "mcp230xx", |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 791 | .of_match_table = of_match_ptr(mcp23s08_i2c_of_match), |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 792 | }, |
| 793 | .probe = mcp230xx_probe, |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 794 | .id_table = mcp230xx_id, |
| 795 | }; |
| 796 | |
| 797 | static int __init mcp23s08_i2c_init(void) |
| 798 | { |
| 799 | return i2c_add_driver(&mcp230xx_driver); |
| 800 | } |
| 801 | |
| 802 | static void mcp23s08_i2c_exit(void) |
| 803 | { |
| 804 | i2c_del_driver(&mcp230xx_driver); |
| 805 | } |
| 806 | |
| 807 | #else |
| 808 | |
| 809 | static int __init mcp23s08_i2c_init(void) { return 0; } |
| 810 | static void mcp23s08_i2c_exit(void) { } |
| 811 | |
| 812 | #endif /* CONFIG_I2C */ |
| 813 | |
| 814 | /*----------------------------------------------------------------------*/ |
| 815 | |
Peter Korsgaard | d62b98f | 2011-07-15 10:25:31 +0200 | [diff] [blame] | 816 | #ifdef CONFIG_SPI_MASTER |
| 817 | |
Andy Shevchenko | d3da29b | 2020-04-07 20:38:43 +0300 | [diff] [blame] | 818 | #define MCP_MAX_DEV_PER_CS 8 |
| 819 | |
| 820 | static int mcp23sxx_spi_write(void *context, const void *data, size_t count) |
| 821 | { |
| 822 | struct mcp23s08 *mcp = context; |
| 823 | struct spi_device *spi = to_spi_device(mcp->dev); |
| 824 | struct spi_message m; |
| 825 | struct spi_transfer t[2] = { { .tx_buf = &mcp->addr, .len = 1, }, |
| 826 | { .tx_buf = data, .len = count, }, }; |
| 827 | |
| 828 | spi_message_init(&m); |
| 829 | spi_message_add_tail(&t[0], &m); |
| 830 | spi_message_add_tail(&t[1], &m); |
| 831 | |
| 832 | return spi_sync(spi, &m); |
| 833 | } |
| 834 | |
| 835 | static int mcp23sxx_spi_gather_write(void *context, |
| 836 | const void *reg, size_t reg_size, |
| 837 | const void *val, size_t val_size) |
| 838 | { |
| 839 | struct mcp23s08 *mcp = context; |
| 840 | struct spi_device *spi = to_spi_device(mcp->dev); |
| 841 | struct spi_message m; |
| 842 | struct spi_transfer t[3] = { { .tx_buf = &mcp->addr, .len = 1, }, |
| 843 | { .tx_buf = reg, .len = reg_size, }, |
| 844 | { .tx_buf = val, .len = val_size, }, }; |
| 845 | |
| 846 | spi_message_init(&m); |
| 847 | spi_message_add_tail(&t[0], &m); |
| 848 | spi_message_add_tail(&t[1], &m); |
| 849 | spi_message_add_tail(&t[2], &m); |
| 850 | |
| 851 | return spi_sync(spi, &m); |
| 852 | } |
| 853 | |
| 854 | static int mcp23sxx_spi_read(void *context, const void *reg, size_t reg_size, |
| 855 | void *val, size_t val_size) |
| 856 | { |
| 857 | struct mcp23s08 *mcp = context; |
| 858 | struct spi_device *spi = to_spi_device(mcp->dev); |
| 859 | u8 tx[2]; |
| 860 | |
| 861 | if (reg_size != 1) |
| 862 | return -EINVAL; |
| 863 | |
| 864 | tx[0] = mcp->addr | 0x01; |
| 865 | tx[1] = *((u8 *) reg); |
| 866 | |
| 867 | return spi_write_then_read(spi, tx, sizeof(tx), val, val_size); |
| 868 | } |
| 869 | |
| 870 | static const struct regmap_bus mcp23sxx_spi_regmap = { |
| 871 | .write = mcp23sxx_spi_write, |
| 872 | .gather_write = mcp23sxx_spi_gather_write, |
| 873 | .read = mcp23sxx_spi_read, |
| 874 | }; |
| 875 | |
| 876 | /* A given spi_device can represent up to eight mcp23sxx chips |
| 877 | * sharing the same chipselect but using different addresses |
| 878 | * (e.g. chips #0 and #3 might be populated, but not #1 or $2). |
| 879 | * Driver data holds all the per-chip data. |
| 880 | */ |
| 881 | struct mcp23s08_driver_data { |
| 882 | unsigned ngpio; |
| 883 | struct mcp23s08 *mcp[8]; |
| 884 | struct mcp23s08 chip[]; |
| 885 | }; |
| 886 | |
| 887 | static int mcp23s08_spi_regmap_init(struct mcp23s08 *mcp, struct device *dev, |
| 888 | unsigned int addr, unsigned int type) |
| 889 | { |
Andy Shevchenko | 0874758 | 2020-04-07 20:38:45 +0300 | [diff] [blame] | 890 | const struct regmap_config *config; |
| 891 | struct regmap_config *copy; |
| 892 | const char *name; |
Andy Shevchenko | d3da29b | 2020-04-07 20:38:43 +0300 | [diff] [blame] | 893 | |
| 894 | switch (type) { |
| 895 | case MCP_TYPE_S08: |
Andy Shevchenko | 0874758 | 2020-04-07 20:38:45 +0300 | [diff] [blame] | 896 | mcp->reg_shift = 0; |
| 897 | mcp->chip.ngpio = 8; |
| 898 | mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL, "mcp23s08.%d", |
| 899 | addr); |
Andy Shevchenko | d3da29b | 2020-04-07 20:38:43 +0300 | [diff] [blame] | 900 | |
Andy Shevchenko | 0874758 | 2020-04-07 20:38:45 +0300 | [diff] [blame] | 901 | config = &mcp23x08_regmap; |
| 902 | name = devm_kasprintf(dev, GFP_KERNEL, "%d", addr); |
| 903 | break; |
| 904 | |
| 905 | case MCP_TYPE_S17: |
| 906 | mcp->reg_shift = 1; |
| 907 | mcp->chip.ngpio = 16; |
| 908 | mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL, "mcp23s17.%d", |
| 909 | addr); |
| 910 | |
| 911 | config = &mcp23x17_regmap; |
| 912 | name = devm_kasprintf(dev, GFP_KERNEL, "%d", addr); |
Andy Shevchenko | d3da29b | 2020-04-07 20:38:43 +0300 | [diff] [blame] | 913 | break; |
| 914 | |
| 915 | case MCP_TYPE_S18: |
Andy Shevchenko | d3da29b | 2020-04-07 20:38:43 +0300 | [diff] [blame] | 916 | mcp->reg_shift = 1; |
| 917 | mcp->chip.ngpio = 16; |
| 918 | mcp->chip.label = "mcp23s18"; |
Andy Shevchenko | 0874758 | 2020-04-07 20:38:45 +0300 | [diff] [blame] | 919 | |
| 920 | config = &mcp23x17_regmap; |
| 921 | name = config->name; |
Andy Shevchenko | d3da29b | 2020-04-07 20:38:43 +0300 | [diff] [blame] | 922 | break; |
| 923 | |
| 924 | default: |
| 925 | dev_err(dev, "invalid device type (%d)\n", type); |
| 926 | return -EINVAL; |
| 927 | } |
| 928 | |
Andy Shevchenko | 0874758 | 2020-04-07 20:38:45 +0300 | [diff] [blame] | 929 | copy = devm_kmemdup(dev, &config, sizeof(config), GFP_KERNEL); |
| 930 | if (!copy) |
| 931 | return -ENOMEM; |
| 932 | |
| 933 | copy->name = name; |
| 934 | |
| 935 | mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp, copy); |
Andy Shevchenko | d3da29b | 2020-04-07 20:38:43 +0300 | [diff] [blame] | 936 | if (IS_ERR(mcp->regmap)) |
| 937 | return PTR_ERR(mcp->regmap); |
| 938 | |
| 939 | return 0; |
| 940 | } |
| 941 | |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 942 | static int mcp23s08_probe(struct spi_device *spi) |
| 943 | { |
Andy Shevchenko | d3da29b | 2020-04-07 20:38:43 +0300 | [diff] [blame] | 944 | struct device *dev = &spi->dev; |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 945 | unsigned addr; |
Linus Walleij | 596a1c5 | 2014-05-28 09:14:06 +0200 | [diff] [blame] | 946 | int chips = 0; |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 947 | struct mcp23s08_driver_data *data; |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 948 | int status, type; |
Sonic Zhang | 3af0dbd | 2014-09-01 11:19:52 +0800 | [diff] [blame] | 949 | unsigned ngpio = 0; |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 950 | const struct of_device_id *match; |
Andy Shevchenko | 6aba6ed | 2020-04-07 20:38:41 +0300 | [diff] [blame] | 951 | u32 spi_present_mask; |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 952 | |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 953 | 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] | 954 | if (match) |
SeongJae Park | de755c3 | 2014-01-18 13:53:04 +0900 | [diff] [blame] | 955 | type = (int)(uintptr_t)match->data; |
Sebastian Reichel | 0d7fcd5 | 2017-05-15 11:24:34 +0200 | [diff] [blame] | 956 | else |
| 957 | type = spi_get_device_id(spi)->driver_data; |
| 958 | |
Andy Shevchenko | 6aba6ed | 2020-04-07 20:38:41 +0300 | [diff] [blame] | 959 | status = device_property_read_u32(&spi->dev, |
| 960 | "microchip,spi-present-mask", &spi_present_mask); |
| 961 | if (status) { |
Sebastian Reichel | 0d7fcd5 | 2017-05-15 11:24:34 +0200 | [diff] [blame] | 962 | status = device_property_read_u32(&spi->dev, |
Andy Shevchenko | 6aba6ed | 2020-04-07 20:38:41 +0300 | [diff] [blame] | 963 | "mcp,spi-present-mask", &spi_present_mask); |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 964 | if (status) { |
Andy Shevchenko | 6aba6ed | 2020-04-07 20:38:41 +0300 | [diff] [blame] | 965 | dev_err(&spi->dev, "missing spi-present-mask"); |
Andy Shevchenko | 88af89b | 2020-04-07 20:38:46 +0300 | [diff] [blame^] | 966 | return status; |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 967 | } |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 968 | } |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 969 | |
Andy Shevchenko | 6aba6ed | 2020-04-07 20:38:41 +0300 | [diff] [blame] | 970 | if (!spi_present_mask || spi_present_mask > 0xff) { |
Sebastian Reichel | 0d7fcd5 | 2017-05-15 11:24:34 +0200 | [diff] [blame] | 971 | dev_err(&spi->dev, "invalid spi-present-mask"); |
| 972 | return -ENODEV; |
| 973 | } |
| 974 | |
Sebastian Reichel | ce9bd0a | 2017-05-15 11:24:36 +0200 | [diff] [blame] | 975 | for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) { |
Andy Shevchenko | 6aba6ed | 2020-04-07 20:38:41 +0300 | [diff] [blame] | 976 | if (spi_present_mask & BIT(addr)) |
Sebastian Reichel | 0d7fcd5 | 2017-05-15 11:24:34 +0200 | [diff] [blame] | 977 | chips++; |
| 978 | } |
| 979 | |
Michael Welling | 99e4b98 | 2014-04-16 20:00:24 -0500 | [diff] [blame] | 980 | if (!chips) |
| 981 | return -ENODEV; |
| 982 | |
Varka Bhadram | 7898b31 | 2015-03-31 09:49:08 +0530 | [diff] [blame] | 983 | data = devm_kzalloc(&spi->dev, |
Gustavo A. R. Silva | 16f4372f | 2019-01-04 11:37:33 -0600 | [diff] [blame] | 984 | struct_size(data, chip, chips), GFP_KERNEL); |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 985 | if (!data) |
| 986 | return -ENOMEM; |
Varka Bhadram | 7898b31 | 2015-03-31 09:49:08 +0530 | [diff] [blame] | 987 | |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 988 | spi_set_drvdata(spi, data); |
| 989 | |
Sebastian Reichel | ce9bd0a | 2017-05-15 11:24:36 +0200 | [diff] [blame] | 990 | for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) { |
Andy Shevchenko | 6aba6ed | 2020-04-07 20:38:41 +0300 | [diff] [blame] | 991 | if (!(spi_present_mask & BIT(addr))) |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 992 | continue; |
| 993 | chips--; |
| 994 | data->mcp[addr] = &data->chip[chips]; |
Alexander Stein | a231b88c | 2014-11-17 09:38:10 +0100 | [diff] [blame] | 995 | data->mcp[addr]->irq = spi->irq; |
Andy Shevchenko | d3da29b | 2020-04-07 20:38:43 +0300 | [diff] [blame] | 996 | |
| 997 | status = mcp23s08_spi_regmap_init(data->mcp[addr], dev, addr, type); |
| 998 | if (status) |
| 999 | return status; |
| 1000 | |
| 1001 | data->mcp[addr]->pinctrl_desc.name = devm_kasprintf(dev, GFP_KERNEL, |
| 1002 | "mcp23xxx-pinctrl.%d", addr); |
| 1003 | if (!data->mcp[addr]->pinctrl_desc.name) |
| 1004 | return -ENOMEM; |
| 1005 | |
Andy Shevchenko | 0521701 | 2020-04-07 20:38:44 +0300 | [diff] [blame] | 1006 | status = mcp23s08_probe_one(data->mcp[addr], dev, 0x40 | (addr << 1), type, -1); |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 1007 | if (status < 0) |
Sebastian Reichel | d0e49da | 2017-05-15 11:24:32 +0200 | [diff] [blame] | 1008 | return status; |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 1009 | |
Phil Reid | 28c5a41 | 2016-03-01 14:25:41 +0800 | [diff] [blame] | 1010 | ngpio += data->mcp[addr]->chip.ngpio; |
David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 1011 | } |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 1012 | data->ngpio = ngpio; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1013 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1014 | return 0; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1015 | } |
| 1016 | |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 1017 | static const struct spi_device_id mcp23s08_ids[] = { |
| 1018 | { "mcp23s08", MCP_TYPE_S08 }, |
| 1019 | { "mcp23s17", MCP_TYPE_S17 }, |
Phil Reid | 28c5a41 | 2016-03-01 14:25:41 +0800 | [diff] [blame] | 1020 | { "mcp23s18", MCP_TYPE_S18 }, |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 1021 | { }, |
| 1022 | }; |
| 1023 | MODULE_DEVICE_TABLE(spi, mcp23s08_ids); |
| 1024 | |
Andy Shevchenko | d3da29b | 2020-04-07 20:38:43 +0300 | [diff] [blame] | 1025 | #ifdef CONFIG_OF |
| 1026 | static const struct of_device_id mcp23s08_spi_of_match[] = { |
| 1027 | { |
| 1028 | .compatible = "microchip,mcp23s08", |
| 1029 | .data = (void *) MCP_TYPE_S08, |
| 1030 | }, |
| 1031 | { |
| 1032 | .compatible = "microchip,mcp23s17", |
| 1033 | .data = (void *) MCP_TYPE_S17, |
| 1034 | }, |
| 1035 | { |
| 1036 | .compatible = "microchip,mcp23s18", |
| 1037 | .data = (void *) MCP_TYPE_S18, |
| 1038 | }, |
| 1039 | /* NOTE: The use of the mcp prefix is deprecated and will be removed. */ |
| 1040 | { |
| 1041 | .compatible = "mcp,mcp23s08", |
| 1042 | .data = (void *) MCP_TYPE_S08, |
| 1043 | }, |
| 1044 | { |
| 1045 | .compatible = "mcp,mcp23s17", |
| 1046 | .data = (void *) MCP_TYPE_S17, |
| 1047 | }, |
| 1048 | { }, |
| 1049 | }; |
| 1050 | MODULE_DEVICE_TABLE(of, mcp23s08_spi_of_match); |
| 1051 | #endif |
| 1052 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1053 | static struct spi_driver mcp23s08_driver = { |
| 1054 | .probe = mcp23s08_probe, |
Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 1055 | .id_table = mcp23s08_ids, |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1056 | .driver = { |
| 1057 | .name = "mcp23s08", |
Lars Poeschel | 97ddb1c | 2013-04-04 12:02:02 +0200 | [diff] [blame] | 1058 | .of_match_table = of_match_ptr(mcp23s08_spi_of_match), |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1059 | }, |
| 1060 | }; |
| 1061 | |
Peter Korsgaard | d62b98f | 2011-07-15 10:25:31 +0200 | [diff] [blame] | 1062 | static int __init mcp23s08_spi_init(void) |
| 1063 | { |
| 1064 | return spi_register_driver(&mcp23s08_driver); |
| 1065 | } |
| 1066 | |
| 1067 | static void mcp23s08_spi_exit(void) |
| 1068 | { |
| 1069 | spi_unregister_driver(&mcp23s08_driver); |
| 1070 | } |
| 1071 | |
| 1072 | #else |
| 1073 | |
| 1074 | static int __init mcp23s08_spi_init(void) { return 0; } |
| 1075 | static void mcp23s08_spi_exit(void) { } |
| 1076 | |
| 1077 | #endif /* CONFIG_SPI_MASTER */ |
| 1078 | |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1079 | /*----------------------------------------------------------------------*/ |
| 1080 | |
| 1081 | static int __init mcp23s08_init(void) |
| 1082 | { |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 1083 | int ret; |
| 1084 | |
| 1085 | ret = mcp23s08_spi_init(); |
| 1086 | if (ret) |
| 1087 | goto spi_fail; |
| 1088 | |
| 1089 | ret = mcp23s08_i2c_init(); |
| 1090 | if (ret) |
| 1091 | goto i2c_fail; |
| 1092 | |
| 1093 | return 0; |
| 1094 | |
| 1095 | i2c_fail: |
| 1096 | mcp23s08_spi_exit(); |
| 1097 | spi_fail: |
| 1098 | return ret; |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1099 | } |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 1100 | /* register after spi/i2c postcore initcall and before |
David Brownell | 673c0c0 | 2008-10-15 22:02:46 -0700 | [diff] [blame] | 1101 | * subsys initcalls that may rely on these GPIOs |
| 1102 | */ |
| 1103 | subsys_initcall(mcp23s08_init); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1104 | |
| 1105 | static void __exit mcp23s08_exit(void) |
| 1106 | { |
Peter Korsgaard | d62b98f | 2011-07-15 10:25:31 +0200 | [diff] [blame] | 1107 | mcp23s08_spi_exit(); |
Peter Korsgaard | 752ad5e | 2011-07-15 10:25:32 +0200 | [diff] [blame] | 1108 | mcp23s08_i2c_exit(); |
David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1109 | } |
| 1110 | module_exit(mcp23s08_exit); |
| 1111 | |
| 1112 | MODULE_LICENSE("GPL"); |