blob: 515d1aa32732c11c5262465b867fdd2094f053fd [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Sebastian Reicheld8f44942017-05-15 11:24:37 +02002/* MCP23S08 SPI/I2C GPIO driver */
David Brownelle58b9e22008-02-04 22:28:25 -08003
4#include <linux/kernel.h>
5#include <linux/device.h>
David Brownelle58b9e22008-02-04 22:28:25 -08006#include <linux/mutex.h>
Paul Gortmakerbb207ef2011-07-03 13:38:09 -04007#include <linux/module.h>
Linus Walleij1c5fb662018-09-13 13:58:21 +02008#include <linux/gpio/driver.h>
Peter Korsgaard752ad5e2011-07-15 10:25:32 +02009#include <linux/i2c.h>
David Brownelle58b9e22008-02-04 22:28:25 -080010#include <linux/spi/spi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Peter Korsgaard0b7bb772011-03-09 17:56:30 +010012#include <asm/byteorder.h>
Lars Poeschel4e47f912014-01-16 11:44:15 +010013#include <linux/interrupt.h>
Lars Poeschel97ddb1c2013-04-04 12:02:02 +020014#include <linux/of_device.h>
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +010015#include <linux/regmap.h>
Sebastian Reichel82039d22017-05-15 11:24:26 +020016#include <linux/pinctrl/pinctrl.h>
17#include <linux/pinctrl/pinconf.h>
18#include <linux/pinctrl/pinconf-generic.h>
David Brownelle58b9e22008-02-04 22:28:25 -080019
Sebastian Reicheld8f44942017-05-15 11:24:37 +020020/*
Peter Korsgaard0b7bb772011-03-09 17:56:30 +010021 * MCP types supported by driver
22 */
23#define MCP_TYPE_S08 0
24#define MCP_TYPE_S17 1
Peter Korsgaard752ad5e2011-07-15 10:25:32 +020025#define MCP_TYPE_008 2
26#define MCP_TYPE_017 3
Phil Reid28c5a412016-03-01 14:25:41 +080027#define MCP_TYPE_S18 4
Phil Reidff0f2ce2017-10-06 13:08:07 +080028#define MCP_TYPE_018 5
David Brownelle58b9e22008-02-04 22:28:25 -080029
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 Brownelle58b9e22008-02-04 22:28:25 -080034 */
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 Poeschel4e47f912014-01-16 11:44:15 +010041# define IOCON_MIRROR (1 << 6)
David Brownelle58b9e22008-02-04 22:28:25 -080042# define IOCON_SEQOP (1 << 5)
43# define IOCON_HAEN (1 << 3)
44# define IOCON_ODR (1 << 2)
45# define IOCON_INTPOL (1 << 1)
Phil Reid35396992016-03-15 15:46:30 +080046# define IOCON_INTCC (1)
David Brownelle58b9e22008-02-04 22:28:25 -080047#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 Korsgaard0b7bb772011-03-09 17:56:30 +010053struct mcp23s08;
54
David Brownelle58b9e22008-02-04 22:28:25 -080055struct mcp23s08 {
David Brownelle58b9e22008-02-04 22:28:25 -080056 u8 addr;
Alexander Steina4e63552014-12-01 08:26:00 +010057 bool irq_active_high;
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +010058 bool reg_shift;
David Brownelle58b9e22008-02-04 22:28:25 -080059
Lars Poeschel4e47f912014-01-16 11:44:15 +010060 u16 irq_rise;
61 u16 irq_fall;
62 int irq;
63 bool irq_controller;
Sebastian Reichel8f389102017-05-15 11:24:28 +020064 int cached_gpio;
65 /* lock protects regmap access with bypass/cache flags */
David Brownelle58b9e22008-02-04 22:28:25 -080066 struct mutex lock;
David Brownelle58b9e22008-02-04 22:28:25 -080067
68 struct gpio_chip chip;
Lars Poeschel19ab5ca2019-01-11 17:25:16 +010069 struct irq_chip irq_chip;
David Brownelle58b9e22008-02-04 22:28:25 -080070
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +010071 struct regmap *regmap;
72 struct device *dev;
Sebastian Reichel82039d22017-05-15 11:24:26 +020073
74 struct pinctrl_dev *pctldev;
75 struct pinctrl_desc pinctrl_desc;
David Brownelle58b9e22008-02-04 22:28:25 -080076};
77
Sebastian Reichel8f389102017-05-15 11:24:28 +020078static 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
89static const struct regmap_range mcp23x08_volatile_range = {
90 .range_min = MCP_INTF,
91 .range_max = MCP_GPIO,
92};
93
94static const struct regmap_access_table mcp23x08_volatile_table = {
95 .yes_ranges = &mcp23x08_volatile_range,
96 .n_yes_ranges = 1,
97};
98
99static const struct regmap_range mcp23x08_precious_range = {
100 .range_min = MCP_GPIO,
101 .range_max = MCP_GPIO,
102};
103
104static const struct regmap_access_table mcp23x08_precious_table = {
105 .yes_ranges = &mcp23x08_precious_range,
106 .n_yes_ranges = 1,
107};
108
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100109static const struct regmap_config mcp23x08_regmap = {
110 .reg_bits = 8,
111 .val_bits = 8,
112
113 .reg_stride = 1,
Sebastian Reichel8f389102017-05-15 11:24:28 +0200114 .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 Reichel3d84fdb2017-01-27 15:47:37 +0100119 .max_register = MCP_OLAT,
120};
121
Sebastian Reichel8f389102017-05-15 11:24:28 +0200122static 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
133static const struct regmap_range mcp23x16_volatile_range = {
134 .range_min = MCP_INTF << 1,
135 .range_max = MCP_GPIO << 1,
136};
137
138static const struct regmap_access_table mcp23x16_volatile_table = {
139 .yes_ranges = &mcp23x16_volatile_range,
140 .n_yes_ranges = 1,
141};
142
143static const struct regmap_range mcp23x16_precious_range = {
144 .range_min = MCP_GPIO << 1,
145 .range_max = MCP_GPIO << 1,
146};
147
148static const struct regmap_access_table mcp23x16_precious_table = {
149 .yes_ranges = &mcp23x16_precious_range,
150 .n_yes_ranges = 1,
151};
152
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100153static 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 Reichel8f389102017-05-15 11:24:28 +0200159 .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 Reichel3d84fdb2017-01-27 15:47:37 +0100164 .val_format_endian = REGMAP_ENDIAN_LITTLE,
165};
166
Sebastian Reichel82039d22017-05-15 11:24:26 +0200167static 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
172static 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 Reichel8f389102017-05-15 11:24:28 +0200177static int mcp_set_mask(struct mcp23s08 *mcp, unsigned int reg,
178 unsigned int mask, bool enabled)
Sebastian Reichel82039d22017-05-15 11:24:26 +0200179{
180 u16 val = enabled ? 0xffff : 0x0000;
Sebastian Reichel82039d22017-05-15 11:24:26 +0200181 return regmap_update_bits(mcp->regmap, reg << mcp->reg_shift,
182 mask, val);
183}
184
Sebastian Reichel8f389102017-05-15 11:24:28 +0200185static int mcp_set_bit(struct mcp23s08 *mcp, unsigned int reg,
186 unsigned int pin, bool enabled)
Sebastian Reichel82039d22017-05-15 11:24:26 +0200187{
Sebastian Reichel8f389102017-05-15 11:24:28 +0200188 u16 mask = BIT(pin);
189 return mcp_set_mask(mcp, reg, mask, enabled);
Sebastian Reichel82039d22017-05-15 11:24:26 +0200190}
191
192static 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
203static 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
222static int mcp_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
223{
224 return 0;
225}
226
227static const char *mcp_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
228 unsigned int group)
229{
230 return NULL;
231}
232
233static 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
241static 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
251static 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 Reichel82039d22017-05-15 11:24:26 +0200267 return -ENOTSUPP;
268 }
269
270 *config = 0;
271
272 return status ? 0 : -EINVAL;
273}
274
275static 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 Reid2a7893c2017-10-06 13:08:11 +0800280 u32 arg;
Sebastian Reichel82039d22017-05-15 11:24:26 +0200281 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 Reichel82039d22017-05-15 11:24:26 +0200290 ret = mcp_set_bit(mcp, MCP_GPPU, pin, arg);
291 break;
292 default:
Jan Kundráte0e31692019-03-07 14:16:51 +0100293 dev_dbg(mcp->dev, "Invalid config param %04x\n", param);
Sebastian Reichel82039d22017-05-15 11:24:26 +0200294 return -ENOTSUPP;
295 }
296 }
297
298 return ret;
299}
300
301static 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 Reichel3d84fdb2017-01-27 15:47:37 +0100307/*----------------------------------------------------------------------*/
308
David Brownelle58b9e22008-02-04 22:28:25 -0800309static int mcp23s08_direction_input(struct gpio_chip *chip, unsigned offset)
310{
Linus Walleij9e03cf02015-12-07 10:09:36 +0100311 struct mcp23s08 *mcp = gpiochip_get_data(chip);
David Brownelle58b9e22008-02-04 22:28:25 -0800312 int status;
313
314 mutex_lock(&mcp->lock);
Sebastian Reichel8f389102017-05-15 11:24:28 +0200315 status = mcp_set_bit(mcp, MCP_IODIR, offset, true);
David Brownelle58b9e22008-02-04 22:28:25 -0800316 mutex_unlock(&mcp->lock);
Sebastian Reichel8f389102017-05-15 11:24:28 +0200317
David Brownelle58b9e22008-02-04 22:28:25 -0800318 return status;
319}
320
321static int mcp23s08_get(struct gpio_chip *chip, unsigned offset)
322{
Linus Walleij9e03cf02015-12-07 10:09:36 +0100323 struct mcp23s08 *mcp = gpiochip_get_data(chip);
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100324 int status, ret;
David Brownelle58b9e22008-02-04 22:28:25 -0800325
326 mutex_lock(&mcp->lock);
327
328 /* REVISIT reading this clears any IRQ ... */
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100329 ret = mcp_read(mcp, MCP_GPIO, &status);
330 if (ret < 0)
David Brownelle58b9e22008-02-04 22:28:25 -0800331 status = 0;
Dmitry Mastykin59861702017-10-18 17:21:02 +0300332 else {
333 mcp->cached_gpio = status;
David Brownelle58b9e22008-02-04 22:28:25 -0800334 status = !!(status & (1 << offset));
Dmitry Mastykin59861702017-10-18 17:21:02 +0300335 }
Sebastian Reichel8f389102017-05-15 11:24:28 +0200336
David Brownelle58b9e22008-02-04 22:28:25 -0800337 mutex_unlock(&mcp->lock);
338 return status;
339}
340
Sebastian Reichel8f389102017-05-15 11:24:28 +0200341static int __mcp23s08_set(struct mcp23s08 *mcp, unsigned mask, bool value)
David Brownelle58b9e22008-02-04 22:28:25 -0800342{
Sebastian Reichel8f389102017-05-15 11:24:28 +0200343 return mcp_set_mask(mcp, MCP_OLAT, mask, value);
David Brownelle58b9e22008-02-04 22:28:25 -0800344}
345
346static void mcp23s08_set(struct gpio_chip *chip, unsigned offset, int value)
347{
Linus Walleij9e03cf02015-12-07 10:09:36 +0100348 struct mcp23s08 *mcp = gpiochip_get_data(chip);
Sebastian Reichel8f389102017-05-15 11:24:28 +0200349 unsigned mask = BIT(offset);
David Brownelle58b9e22008-02-04 22:28:25 -0800350
351 mutex_lock(&mcp->lock);
Sebastian Reichel8f389102017-05-15 11:24:28 +0200352 __mcp23s08_set(mcp, mask, !!value);
David Brownelle58b9e22008-02-04 22:28:25 -0800353 mutex_unlock(&mcp->lock);
354}
355
356static int
357mcp23s08_direction_output(struct gpio_chip *chip, unsigned offset, int value)
358{
Linus Walleij9e03cf02015-12-07 10:09:36 +0100359 struct mcp23s08 *mcp = gpiochip_get_data(chip);
Sebastian Reichel8f389102017-05-15 11:24:28 +0200360 unsigned mask = BIT(offset);
David Brownelle58b9e22008-02-04 22:28:25 -0800361 int status;
362
363 mutex_lock(&mcp->lock);
364 status = __mcp23s08_set(mcp, mask, value);
365 if (status == 0) {
Sebastian Reichel8f389102017-05-15 11:24:28 +0200366 status = mcp_set_mask(mcp, MCP_IODIR, mask, false);
David Brownelle58b9e22008-02-04 22:28:25 -0800367 }
368 mutex_unlock(&mcp->lock);
369 return status;
370}
371
372/*----------------------------------------------------------------------*/
Lars Poeschel4e47f912014-01-16 11:44:15 +0100373static irqreturn_t mcp23s08_irq(int irq, void *data)
374{
375 struct mcp23s08 *mcp = data;
Sebastian Reichel8f389102017-05-15 11:24:28 +0200376 int intcap, intcon, intf, i, gpio, gpio_orig, intcap_mask, defval;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100377 unsigned int child_irq;
Robert Middleton2cd29f22017-03-15 16:56:47 -0400378 bool intf_set, intcap_changed, gpio_bit_changed,
379 defval_changed, gpio_set;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100380
381 mutex_lock(&mcp->lock);
Markus Elfring7f6f50d2017-10-30 16:03:12 +0100382 if (mcp_read(mcp, MCP_INTF, &intf))
383 goto unlock;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100384
Markus Elfring7f6f50d2017-10-30 16:03:12 +0100385 if (mcp_read(mcp, MCP_INTCAP, &intcap))
386 goto unlock;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100387
Markus Elfring7f6f50d2017-10-30 16:03:12 +0100388 if (mcp_read(mcp, MCP_INTCON, &intcon))
389 goto unlock;
Sebastian Reichel8f389102017-05-15 11:24:28 +0200390
Markus Elfring7f6f50d2017-10-30 16:03:12 +0100391 if (mcp_read(mcp, MCP_DEFVAL, &defval))
392 goto unlock;
Robert Middleton2cd29f22017-03-15 16:56:47 -0400393
394 /* This clears the interrupt(configurable on S18) */
Markus Elfring7f6f50d2017-10-30 16:03:12 +0100395 if (mcp_read(mcp, MCP_GPIO, &gpio))
396 goto unlock;
397
Sebastian Reichel8f389102017-05-15 11:24:28 +0200398 gpio_orig = mcp->cached_gpio;
399 mcp->cached_gpio = gpio;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100400 mutex_unlock(&mcp->lock);
401
Sebastian Reichel8f389102017-05-15 11:24:28 +0200402 if (intf == 0) {
Robert Middleton2cd29f22017-03-15 16:56:47 -0400403 /* 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 Poeschel4e47f912014-01-16 11:44:15 +0100410
411 for (i = 0; i < mcp->chip.ngpio; i++) {
Robert Middleton2cd29f22017-03-15 16:56:47 -0400412 /* 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 Reichel8f389102017-05-15 11:24:28 +0200430 intf_set = intf & BIT(i);
Robert Middleton2cd29f22017-03-15 16:56:47 -0400431 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 Reichel8f389102017-05-15 11:24:28 +0200439 (intcap & BIT(i))) !=
Robert Middleton2cd29f22017-03-15 16:56:47 -0400440 (intcap_mask & (BIT(i) & gpio_orig));
Sebastian Reichel8f389102017-05-15 11:24:28 +0200441 gpio_set = BIT(i) & gpio;
Robert Middleton2cd29f22017-03-15 16:56:47 -0400442 gpio_bit_changed = (BIT(i) & gpio_orig) !=
Sebastian Reichel8f389102017-05-15 11:24:28 +0200443 (BIT(i) & gpio);
444 defval_changed = (BIT(i) & intcon) &&
445 ((BIT(i) & gpio) !=
446 (BIT(i) & defval));
Robert Middleton2cd29f22017-03-15 16:56:47 -0400447
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 Redingf0fbe7b2017-11-07 19:15:47 +0100453 child_irq = irq_find_mapping(mcp->chip.irq.domain, i);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100454 handle_nested_irq(child_irq);
455 }
456 }
457
458 return IRQ_HANDLED;
Markus Elfring7f6f50d2017-10-30 16:03:12 +0100459
460unlock:
461 mutex_unlock(&mcp->lock);
462 return IRQ_HANDLED;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100463}
464
Lars Poeschel4e47f912014-01-16 11:44:15 +0100465static void mcp23s08_irq_mask(struct irq_data *data)
466{
Phil Reiddad3d272016-03-18 16:07:06 +0800467 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
468 struct mcp23s08 *mcp = gpiochip_get_data(gc);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100469 unsigned int pos = data->hwirq;
470
Sebastian Reichel8f389102017-05-15 11:24:28 +0200471 mcp_set_bit(mcp, MCP_GPINTEN, pos, false);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100472}
473
474static void mcp23s08_irq_unmask(struct irq_data *data)
475{
Phil Reiddad3d272016-03-18 16:07:06 +0800476 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
477 struct mcp23s08 *mcp = gpiochip_get_data(gc);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100478 unsigned int pos = data->hwirq;
479
Sebastian Reichel8f389102017-05-15 11:24:28 +0200480 mcp_set_bit(mcp, MCP_GPINTEN, pos, true);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100481}
482
483static int mcp23s08_irq_set_type(struct irq_data *data, unsigned int type)
484{
Phil Reiddad3d272016-03-18 16:07:06 +0800485 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
486 struct mcp23s08 *mcp = gpiochip_get_data(gc);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100487 unsigned int pos = data->hwirq;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100488
489 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
Sebastian Reichel8f389102017-05-15 11:24:28 +0200490 mcp_set_bit(mcp, MCP_INTCON, pos, false);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100491 mcp->irq_rise |= BIT(pos);
492 mcp->irq_fall |= BIT(pos);
493 } else if (type & IRQ_TYPE_EDGE_RISING) {
Sebastian Reichel8f389102017-05-15 11:24:28 +0200494 mcp_set_bit(mcp, MCP_INTCON, pos, false);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100495 mcp->irq_rise |= BIT(pos);
496 mcp->irq_fall &= ~BIT(pos);
497 } else if (type & IRQ_TYPE_EDGE_FALLING) {
Sebastian Reichel8f389102017-05-15 11:24:28 +0200498 mcp_set_bit(mcp, MCP_INTCON, pos, false);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100499 mcp->irq_rise &= ~BIT(pos);
500 mcp->irq_fall |= BIT(pos);
Alexander Stein16fe1ad2016-03-23 18:01:27 +0100501 } else if (type & IRQ_TYPE_LEVEL_HIGH) {
Sebastian Reichel8f389102017-05-15 11:24:28 +0200502 mcp_set_bit(mcp, MCP_INTCON, pos, true);
503 mcp_set_bit(mcp, MCP_DEFVAL, pos, false);
Alexander Stein16fe1ad2016-03-23 18:01:27 +0100504 } else if (type & IRQ_TYPE_LEVEL_LOW) {
Sebastian Reichel8f389102017-05-15 11:24:28 +0200505 mcp_set_bit(mcp, MCP_INTCON, pos, true);
506 mcp_set_bit(mcp, MCP_DEFVAL, pos, true);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100507 } else
508 return -EINVAL;
509
Andy Shevchenko88af89b2020-04-07 20:38:46 +0300510 return 0;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100511}
512
513static void mcp23s08_irq_bus_lock(struct irq_data *data)
514{
Phil Reiddad3d272016-03-18 16:07:06 +0800515 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
516 struct mcp23s08 *mcp = gpiochip_get_data(gc);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100517
Sebastian Reichel8f389102017-05-15 11:24:28 +0200518 mutex_lock(&mcp->lock);
519 regcache_cache_only(mcp->regmap, true);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100520}
521
522static void mcp23s08_irq_bus_unlock(struct irq_data *data)
523{
Phil Reiddad3d272016-03-18 16:07:06 +0800524 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
525 struct mcp23s08 *mcp = gpiochip_get_data(gc);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100526
Sebastian Reichel8f389102017-05-15 11:24:28 +0200527 regcache_cache_only(mcp->regmap, false);
528 regcache_sync(mcp->regmap);
529
Lars Poeschel4e47f912014-01-16 11:44:15 +0100530 mutex_unlock(&mcp->lock);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100531}
532
Lars Poeschel4e47f912014-01-16 11:44:15 +0100533static int mcp23s08_irq_setup(struct mcp23s08 *mcp)
534{
535 struct gpio_chip *chip = &mcp->chip;
Phil Reiddad3d272016-03-18 16:07:06 +0800536 int err;
Alexander Steina4e63552014-12-01 08:26:00 +0100537 unsigned long irqflags = IRQF_ONESHOT | IRQF_SHARED;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100538
Alexander Steina4e63552014-12-01 08:26:00 +0100539 if (mcp->irq_active_high)
540 irqflags |= IRQF_TRIGGER_HIGH;
541 else
542 irqflags |= IRQF_TRIGGER_LOW;
543
Linus Walleij58383c782015-11-04 09:56:26 +0100544 err = devm_request_threaded_irq(chip->parent, mcp->irq, NULL,
545 mcp23s08_irq,
546 irqflags, dev_name(chip->parent), mcp);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100547 if (err != 0) {
Linus Walleij58383c782015-11-04 09:56:26 +0100548 dev_err(chip->parent, "unable to request IRQ#%d: %d\n",
Lars Poeschel4e47f912014-01-16 11:44:15 +0100549 mcp->irq, err);
550 return err;
551 }
552
Marco Felschf259f892018-10-02 10:06:46 +0200553 return 0;
554}
555
556static int mcp23s08_irqchip_setup(struct mcp23s08 *mcp)
557{
558 struct gpio_chip *chip = &mcp->chip;
559 int err;
560
Linus Walleijd245b3f2016-11-24 10:57:25 +0100561 err = gpiochip_irqchip_add_nested(chip,
Lars Poeschel19ab5ca2019-01-11 17:25:16 +0100562 &mcp->irq_chip,
Linus Walleijd245b3f2016-11-24 10:57:25 +0100563 0,
564 handle_simple_irq,
565 IRQ_TYPE_NONE);
Phil Reiddad3d272016-03-18 16:07:06 +0800566 if (err) {
567 dev_err(chip->parent,
568 "could not connect irqchip to gpiochip: %d\n", err);
569 return err;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100570 }
Phil Reiddad3d272016-03-18 16:07:06 +0800571
Linus Walleijd245b3f2016-11-24 10:57:25 +0100572 gpiochip_set_nested_irqchip(chip,
Lars Poeschel19ab5ca2019-01-11 17:25:16 +0100573 &mcp->irq_chip,
Linus Walleijd245b3f2016-11-24 10:57:25 +0100574 mcp->irq);
Phil Reiddad3d272016-03-18 16:07:06 +0800575
Lars Poeschel4e47f912014-01-16 11:44:15 +0100576 return 0;
577}
578
Lars Poeschel4e47f912014-01-16 11:44:15 +0100579/*----------------------------------------------------------------------*/
David Brownelle58b9e22008-02-04 22:28:25 -0800580
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200581static int mcp23s08_probe_one(struct mcp23s08 *mcp, struct device *dev,
Andy Shevchenkod3da29b2020-04-07 20:38:43 +0300582 unsigned int addr, unsigned int type,
Andy Shevchenko05217012020-04-07 20:38:44 +0300583 unsigned int base)
David Brownelle58b9e22008-02-04 22:28:25 -0800584{
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100585 int status, ret;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100586 bool mirror = false;
Phil Reidfa2b7fa2018-02-19 17:25:20 +0800587 bool open_drain = false;
David Brownelle58b9e22008-02-04 22:28:25 -0800588
David Brownelle58b9e22008-02-04 22:28:25 -0800589 mutex_init(&mcp->lock);
590
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100591 mcp->dev = dev;
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200592 mcp->addr = addr;
Andy Shevchenko84d02e72020-04-07 20:38:42 +0300593
Alexander Steina4e63552014-12-01 08:26:00 +0100594 mcp->irq_active_high = false;
Andy Shevchenko84d02e72020-04-07 20:38:42 +0300595 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 Brownelle58b9e22008-02-04 22:28:25 -0800601
David Brownelle58b9e22008-02-04 22:28:25 -0800602 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 Walleij60f749f2016-09-07 23:13:20 +0200606#ifdef CONFIG_OF_GPIO
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200607 mcp->chip.of_gpio_n_cells = 2;
608 mcp->chip.of_node = dev->of_node;
609#endif
David Brownelle58b9e22008-02-04 22:28:25 -0800610
Sebastian Reichel5b1a7e82017-05-15 11:24:35 +0200611 mcp->chip.base = base;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100612 mcp->chip.can_sleep = true;
Linus Walleij58383c782015-11-04 09:56:26 +0100613 mcp->chip.parent = dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700614 mcp->chip.owner = THIS_MODULE;
David Brownelle58b9e22008-02-04 22:28:25 -0800615
David Brownell8f1cc3b2008-07-25 01:46:09 -0700616 /* verify MCP_IOCON.SEQOP = 0, so sequential reads work,
617 * and MCP_IOCON.HAEN = 1, so we work with all chips.
618 */
Lars Poeschel4e47f912014-01-16 11:44:15 +0100619
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100620 ret = mcp_read(mcp, MCP_IOCON, &status);
621 if (ret < 0)
David Brownelle58b9e22008-02-04 22:28:25 -0800622 goto fail;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100623
Phil Reid6dbc6e62019-06-13 12:10:23 +0800624 ret = devm_gpiochip_add_data(dev, &mcp->chip, mcp);
625 if (ret < 0)
626 goto fail;
627
Sebastian Reichel5b1a7e82017-05-15 11:24:35 +0200628 mcp->irq_controller =
629 device_property_read_bool(dev, "interrupt-controller");
Alexander Steina4e63552014-12-01 08:26:00 +0100630 if (mcp->irq && mcp->irq_controller) {
Linus Walleij170680a2014-12-12 11:22:11 +0100631 mcp->irq_active_high =
Sebastian Reichel5b1a7e82017-05-15 11:24:35 +0200632 device_property_read_bool(dev,
Linus Walleij170680a2014-12-12 11:22:11 +0100633 "microchip,irq-active-high");
Lars Poeschel4e47f912014-01-16 11:44:15 +0100634
Sebastian Reichel5b1a7e82017-05-15 11:24:35 +0200635 mirror = device_property_read_bool(dev, "microchip,irq-mirror");
Phil Reidfa2b7fa2018-02-19 17:25:20 +0800636 open_drain = device_property_read_bool(dev, "drive-open-drain");
Alexander Steina4e63552014-12-01 08:26:00 +0100637 }
638
639 if ((status & IOCON_SEQOP) || !(status & IOCON_HAEN) || mirror ||
Phil Reidfa2b7fa2018-02-19 17:25:20 +0800640 mcp->irq_active_high || open_drain) {
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100641 /* 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 Steina4e63552014-12-01 08:26:00 +0100644 if (mcp->irq_active_high)
645 status |= IOCON_INTPOL | (IOCON_INTPOL << 8);
646 else
647 status &= ~(IOCON_INTPOL | (IOCON_INTPOL << 8));
648
Lars Poeschel4e47f912014-01-16 11:44:15 +0100649 if (mirror)
650 status |= IOCON_MIRROR | (IOCON_MIRROR << 8);
651
Phil Reidfa2b7fa2018-02-19 17:25:20 +0800652 if (open_drain)
653 status |= IOCON_ODR | (IOCON_ODR << 8);
654
Phil Reidff0f2ce2017-10-06 13:08:07 +0800655 if (type == MCP_TYPE_S18 || type == MCP_TYPE_018)
Phil Reid35396992016-03-15 15:46:30 +0800656 status |= IOCON_INTCC | (IOCON_INTCC << 8);
657
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100658 ret = mcp_write(mcp, MCP_IOCON, status);
659 if (ret < 0)
David Brownelle58b9e22008-02-04 22:28:25 -0800660 goto fail;
661 }
662
Lars Poeschel4e47f912014-01-16 11:44:15 +0100663 if (mcp->irq && mcp->irq_controller) {
Marco Felschf259f892018-10-02 10:06:46 +0200664 ret = mcp23s08_irqchip_setup(mcp);
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100665 if (ret)
Lars Poeschel4e47f912014-01-16 11:44:15 +0100666 goto fail;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100667 }
Sebastian Reichel82039d22017-05-15 11:24:26 +0200668
Sebastian Reichel82039d22017-05-15 11:24:26 +0200669 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 Felschf259f892018-10-02 10:06:46 +0200684 if (mcp->irq)
685 ret = mcp23s08_irq_setup(mcp);
686
David Brownell8f1cc3b2008-07-25 01:46:09 -0700687fail:
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100688 if (ret < 0)
689 dev_dbg(dev, "can't setup chip %d, --> %d\n", addr, ret);
690 return ret;
David Brownell8f1cc3b2008-07-25 01:46:09 -0700691}
692
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200693/*----------------------------------------------------------------------*/
694
Andy Shevchenkod3da29b2020-04-07 20:38:43 +0300695#if IS_ENABLED(CONFIG_I2C)
696
697static 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 Shevchenko05217012020-04-07 20:38:44 +0300743 status = mcp23s08_probe_one(mcp, dev, client->addr, type, -1);
Andy Shevchenkod3da29b2020-04-07 20:38:43 +0300744 if (status)
745 return status;
746
747 i2c_set_clientdata(client, mcp);
748
749 return 0;
750}
751
752static const struct i2c_device_id mcp230xx_id[] = {
753 { "mcp23008", MCP_TYPE_008 },
754 { "mcp23017", MCP_TYPE_017 },
755 { "mcp23018", MCP_TYPE_018 },
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200756 { },
757};
Andy Shevchenkod3da29b2020-04-07 20:38:43 +0300758MODULE_DEVICE_TABLE(i2c, mcp230xx_id);
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200759
Andy Shevchenkod3da29b2020-04-07 20:38:43 +0300760#ifdef CONFIG_OF
Jingoo Hanac791802014-05-07 18:05:17 +0900761static const struct of_device_id mcp23s08_i2c_of_match[] = {
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200762 {
Lars Poeschel45971682013-08-28 10:38:50 +0200763 .compatible = "microchip,mcp23008",
764 .data = (void *) MCP_TYPE_008,
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200765 },
766 {
Lars Poeschel45971682013-08-28 10:38:50 +0200767 .compatible = "microchip,mcp23017",
768 .data = (void *) MCP_TYPE_017,
769 },
Phil Reidff0f2ce2017-10-06 13:08:07 +0800770 {
771 .compatible = "microchip,mcp23018",
772 .data = (void *) MCP_TYPE_018,
773 },
Lars Poeschel45971682013-08-28 10:38:50 +0200774/* 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 Poeschel97ddb1c2013-04-04 12:02:02 +0200782 },
783 { },
784};
785MODULE_DEVICE_TABLE(of, mcp23s08_i2c_of_match);
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200786#endif /* CONFIG_OF */
787
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200788static struct i2c_driver mcp230xx_driver = {
789 .driver = {
790 .name = "mcp230xx",
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200791 .of_match_table = of_match_ptr(mcp23s08_i2c_of_match),
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200792 },
793 .probe = mcp230xx_probe,
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200794 .id_table = mcp230xx_id,
795};
796
797static int __init mcp23s08_i2c_init(void)
798{
799 return i2c_add_driver(&mcp230xx_driver);
800}
801
802static void mcp23s08_i2c_exit(void)
803{
804 i2c_del_driver(&mcp230xx_driver);
805}
806
807#else
808
809static int __init mcp23s08_i2c_init(void) { return 0; }
810static void mcp23s08_i2c_exit(void) { }
811
812#endif /* CONFIG_I2C */
813
814/*----------------------------------------------------------------------*/
815
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200816#ifdef CONFIG_SPI_MASTER
817
Andy Shevchenkod3da29b2020-04-07 20:38:43 +0300818#define MCP_MAX_DEV_PER_CS 8
819
820static 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
835static 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
854static 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
870static 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 */
881struct mcp23s08_driver_data {
882 unsigned ngpio;
883 struct mcp23s08 *mcp[8];
884 struct mcp23s08 chip[];
885};
886
887static int mcp23s08_spi_regmap_init(struct mcp23s08 *mcp, struct device *dev,
888 unsigned int addr, unsigned int type)
889{
Andy Shevchenko08747582020-04-07 20:38:45 +0300890 const struct regmap_config *config;
891 struct regmap_config *copy;
892 const char *name;
Andy Shevchenkod3da29b2020-04-07 20:38:43 +0300893
894 switch (type) {
895 case MCP_TYPE_S08:
Andy Shevchenko08747582020-04-07 20:38:45 +0300896 mcp->reg_shift = 0;
897 mcp->chip.ngpio = 8;
898 mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL, "mcp23s08.%d",
899 addr);
Andy Shevchenkod3da29b2020-04-07 20:38:43 +0300900
Andy Shevchenko08747582020-04-07 20:38:45 +0300901 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 Shevchenkod3da29b2020-04-07 20:38:43 +0300913 break;
914
915 case MCP_TYPE_S18:
Andy Shevchenkod3da29b2020-04-07 20:38:43 +0300916 mcp->reg_shift = 1;
917 mcp->chip.ngpio = 16;
918 mcp->chip.label = "mcp23s18";
Andy Shevchenko08747582020-04-07 20:38:45 +0300919
920 config = &mcp23x17_regmap;
921 name = config->name;
Andy Shevchenkod3da29b2020-04-07 20:38:43 +0300922 break;
923
924 default:
925 dev_err(dev, "invalid device type (%d)\n", type);
926 return -EINVAL;
927 }
928
Andy Shevchenko08747582020-04-07 20:38:45 +0300929 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 Shevchenkod3da29b2020-04-07 20:38:43 +0300936 if (IS_ERR(mcp->regmap))
937 return PTR_ERR(mcp->regmap);
938
939 return 0;
940}
941
David Brownell8f1cc3b2008-07-25 01:46:09 -0700942static int mcp23s08_probe(struct spi_device *spi)
943{
Andy Shevchenkod3da29b2020-04-07 20:38:43 +0300944 struct device *dev = &spi->dev;
David Brownell8f1cc3b2008-07-25 01:46:09 -0700945 unsigned addr;
Linus Walleij596a1c52014-05-28 09:14:06 +0200946 int chips = 0;
David Brownell8f1cc3b2008-07-25 01:46:09 -0700947 struct mcp23s08_driver_data *data;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100948 int status, type;
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800949 unsigned ngpio = 0;
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200950 const struct of_device_id *match;
Andy Shevchenko6aba6ed2020-04-07 20:38:41 +0300951 u32 spi_present_mask;
David Brownell8f1cc3b2008-07-25 01:46:09 -0700952
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200953 match = of_match_device(of_match_ptr(mcp23s08_spi_of_match), &spi->dev);
Sebastian Reichel0d7fcd52017-05-15 11:24:34 +0200954 if (match)
SeongJae Parkde755c32014-01-18 13:53:04 +0900955 type = (int)(uintptr_t)match->data;
Sebastian Reichel0d7fcd52017-05-15 11:24:34 +0200956 else
957 type = spi_get_device_id(spi)->driver_data;
958
Andy Shevchenko6aba6ed2020-04-07 20:38:41 +0300959 status = device_property_read_u32(&spi->dev,
960 "microchip,spi-present-mask", &spi_present_mask);
961 if (status) {
Sebastian Reichel0d7fcd52017-05-15 11:24:34 +0200962 status = device_property_read_u32(&spi->dev,
Andy Shevchenko6aba6ed2020-04-07 20:38:41 +0300963 "mcp,spi-present-mask", &spi_present_mask);
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200964 if (status) {
Andy Shevchenko6aba6ed2020-04-07 20:38:41 +0300965 dev_err(&spi->dev, "missing spi-present-mask");
Andy Shevchenko88af89b2020-04-07 20:38:46 +0300966 return status;
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200967 }
David Brownell8f1cc3b2008-07-25 01:46:09 -0700968 }
David Brownell8f1cc3b2008-07-25 01:46:09 -0700969
Andy Shevchenko6aba6ed2020-04-07 20:38:41 +0300970 if (!spi_present_mask || spi_present_mask > 0xff) {
Sebastian Reichel0d7fcd52017-05-15 11:24:34 +0200971 dev_err(&spi->dev, "invalid spi-present-mask");
972 return -ENODEV;
973 }
974
Sebastian Reichelce9bd0a2017-05-15 11:24:36 +0200975 for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) {
Andy Shevchenko6aba6ed2020-04-07 20:38:41 +0300976 if (spi_present_mask & BIT(addr))
Sebastian Reichel0d7fcd52017-05-15 11:24:34 +0200977 chips++;
978 }
979
Michael Welling99e4b982014-04-16 20:00:24 -0500980 if (!chips)
981 return -ENODEV;
982
Varka Bhadram7898b312015-03-31 09:49:08 +0530983 data = devm_kzalloc(&spi->dev,
Gustavo A. R. Silva16f4372f2019-01-04 11:37:33 -0600984 struct_size(data, chip, chips), GFP_KERNEL);
David Brownell8f1cc3b2008-07-25 01:46:09 -0700985 if (!data)
986 return -ENOMEM;
Varka Bhadram7898b312015-03-31 09:49:08 +0530987
David Brownell8f1cc3b2008-07-25 01:46:09 -0700988 spi_set_drvdata(spi, data);
989
Sebastian Reichelce9bd0a2017-05-15 11:24:36 +0200990 for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) {
Andy Shevchenko6aba6ed2020-04-07 20:38:41 +0300991 if (!(spi_present_mask & BIT(addr)))
David Brownell8f1cc3b2008-07-25 01:46:09 -0700992 continue;
993 chips--;
994 data->mcp[addr] = &data->chip[chips];
Alexander Steina231b88c2014-11-17 09:38:10 +0100995 data->mcp[addr]->irq = spi->irq;
Andy Shevchenkod3da29b2020-04-07 20:38:43 +0300996
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 Shevchenko05217012020-04-07 20:38:44 +03001006 status = mcp23s08_probe_one(data->mcp[addr], dev, 0x40 | (addr << 1), type, -1);
David Brownell8f1cc3b2008-07-25 01:46:09 -07001007 if (status < 0)
Sebastian Reicheld0e49da2017-05-15 11:24:32 +02001008 return status;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +01001009
Phil Reid28c5a412016-03-01 14:25:41 +08001010 ngpio += data->mcp[addr]->chip.ngpio;
David Brownell8f1cc3b2008-07-25 01:46:09 -07001011 }
Lars Poeschel97ddb1c2013-04-04 12:02:02 +02001012 data->ngpio = ngpio;
David Brownelle58b9e22008-02-04 22:28:25 -08001013
David Brownelle58b9e22008-02-04 22:28:25 -08001014 return 0;
David Brownelle58b9e22008-02-04 22:28:25 -08001015}
1016
Peter Korsgaard0b7bb772011-03-09 17:56:30 +01001017static const struct spi_device_id mcp23s08_ids[] = {
1018 { "mcp23s08", MCP_TYPE_S08 },
1019 { "mcp23s17", MCP_TYPE_S17 },
Phil Reid28c5a412016-03-01 14:25:41 +08001020 { "mcp23s18", MCP_TYPE_S18 },
Peter Korsgaard0b7bb772011-03-09 17:56:30 +01001021 { },
1022};
1023MODULE_DEVICE_TABLE(spi, mcp23s08_ids);
1024
Andy Shevchenkod3da29b2020-04-07 20:38:43 +03001025#ifdef CONFIG_OF
1026static 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};
1050MODULE_DEVICE_TABLE(of, mcp23s08_spi_of_match);
1051#endif
1052
David Brownelle58b9e22008-02-04 22:28:25 -08001053static struct spi_driver mcp23s08_driver = {
1054 .probe = mcp23s08_probe,
Peter Korsgaard0b7bb772011-03-09 17:56:30 +01001055 .id_table = mcp23s08_ids,
David Brownelle58b9e22008-02-04 22:28:25 -08001056 .driver = {
1057 .name = "mcp23s08",
Lars Poeschel97ddb1c2013-04-04 12:02:02 +02001058 .of_match_table = of_match_ptr(mcp23s08_spi_of_match),
David Brownelle58b9e22008-02-04 22:28:25 -08001059 },
1060};
1061
Peter Korsgaardd62b98f2011-07-15 10:25:31 +02001062static int __init mcp23s08_spi_init(void)
1063{
1064 return spi_register_driver(&mcp23s08_driver);
1065}
1066
1067static void mcp23s08_spi_exit(void)
1068{
1069 spi_unregister_driver(&mcp23s08_driver);
1070}
1071
1072#else
1073
1074static int __init mcp23s08_spi_init(void) { return 0; }
1075static void mcp23s08_spi_exit(void) { }
1076
1077#endif /* CONFIG_SPI_MASTER */
1078
David Brownelle58b9e22008-02-04 22:28:25 -08001079/*----------------------------------------------------------------------*/
1080
1081static int __init mcp23s08_init(void)
1082{
Peter Korsgaard752ad5e2011-07-15 10:25:32 +02001083 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 Brownelle58b9e22008-02-04 22:28:25 -08001099}
Peter Korsgaard752ad5e2011-07-15 10:25:32 +02001100/* register after spi/i2c postcore initcall and before
David Brownell673c0c02008-10-15 22:02:46 -07001101 * subsys initcalls that may rely on these GPIOs
1102 */
1103subsys_initcall(mcp23s08_init);
David Brownelle58b9e22008-02-04 22:28:25 -08001104
1105static void __exit mcp23s08_exit(void)
1106{
Peter Korsgaardd62b98f2011-07-15 10:25:31 +02001107 mcp23s08_spi_exit();
Peter Korsgaard752ad5e2011-07-15 10:25:32 +02001108 mcp23s08_i2c_exit();
David Brownelle58b9e22008-02-04 22:28:25 -08001109}
1110module_exit(mcp23s08_exit);
1111
1112MODULE_LICENSE("GPL");