2 * MCP23S08 SPI/I2C GPIO gpio expander driver
4 * The inputs and outputs of the mcp23s08, mcp23s17, mcp23008 and mcp23017 are
6 * For the I2C versions of the chips (mcp23008 and mcp23017) generation of
7 * interrupts is also supported.
8 * The hardware of the SPI versions of the chips (mcp23s08 and mcp23s17) is
9 * also capable of generating interrupts, but the linux driver does not
13 #include <linux/kernel.h>
14 #include <linux/device.h>
15 #include <linux/mutex.h>
16 #include <linux/module.h>
17 #include <linux/gpio.h>
18 #include <linux/i2c.h>
19 #include <linux/spi/spi.h>
20 #include <linux/spi/mcp23s08.h>
21 #include <linux/slab.h>
22 #include <asm/byteorder.h>
23 #include <linux/interrupt.h>
24 #include <linux/of_irq.h>
25 #include <linux/of_device.h>
26 #include <linux/regmap.h>
29 * MCP types supported by driver
31 #define MCP_TYPE_S08 0
32 #define MCP_TYPE_S17 1
33 #define MCP_TYPE_008 2
34 #define MCP_TYPE_017 3
35 #define MCP_TYPE_S18 4
37 /* Registers are all 8 bits wide.
39 * The mcp23s17 has twice as many bits, and can be configured to work
40 * with either 16 bit registers or with two adjacent 8 bit banks.
42 #define MCP_IODIR 0x00 /* init/reset: all ones */
44 #define MCP_GPINTEN 0x02
45 #define MCP_DEFVAL 0x03
46 #define MCP_INTCON 0x04
47 #define MCP_IOCON 0x05
48 # define IOCON_MIRROR (1 << 6)
49 # define IOCON_SEQOP (1 << 5)
50 # define IOCON_HAEN (1 << 3)
51 # define IOCON_ODR (1 << 2)
52 # define IOCON_INTPOL (1 << 1)
53 # define IOCON_INTCC (1)
56 #define MCP_INTCAP 0x08
72 /* lock protects the cached values */
74 struct mutex irq_lock;
76 struct gpio_chip chip;
78 struct regmap *regmap;
82 static const struct regmap_config mcp23x08_regmap = {
87 .max_register = MCP_OLAT,
90 static const struct regmap_config mcp23x17_regmap = {
95 .max_register = MCP_OLAT << 1,
96 .val_format_endian = REGMAP_ENDIAN_LITTLE,
99 /*----------------------------------------------------------------------*/
101 #ifdef CONFIG_SPI_MASTER
103 static int mcp23sxx_spi_write(void *context, const void *data, size_t count)
105 struct mcp23s08 *mcp = context;
106 struct spi_device *spi = to_spi_device(mcp->dev);
107 struct spi_message m;
108 struct spi_transfer t[2] = { { .tx_buf = &mcp->addr, .len = 1, },
109 { .tx_buf = data, .len = count, }, };
111 spi_message_init(&m);
112 spi_message_add_tail(&t[0], &m);
113 spi_message_add_tail(&t[1], &m);
115 return spi_sync(spi, &m);
118 static int mcp23sxx_spi_gather_write(void *context,
119 const void *reg, size_t reg_size,
120 const void *val, size_t val_size)
122 struct mcp23s08 *mcp = context;
123 struct spi_device *spi = to_spi_device(mcp->dev);
124 struct spi_message m;
125 struct spi_transfer t[3] = { { .tx_buf = &mcp->addr, .len = 1, },
126 { .tx_buf = reg, .len = reg_size, },
127 { .tx_buf = val, .len = val_size, }, };
129 spi_message_init(&m);
130 spi_message_add_tail(&t[0], &m);
131 spi_message_add_tail(&t[1], &m);
132 spi_message_add_tail(&t[2], &m);
134 return spi_sync(spi, &m);
137 static int mcp23sxx_spi_read(void *context, const void *reg, size_t reg_size,
138 void *val, size_t val_size)
140 struct mcp23s08 *mcp = context;
141 struct spi_device *spi = to_spi_device(mcp->dev);
147 tx[0] = mcp->addr | 0x01;
148 tx[1] = *((u8 *) reg);
150 return spi_write_then_read(spi, tx, sizeof(tx), val, val_size);
153 static const struct regmap_bus mcp23sxx_spi_regmap = {
154 .write = mcp23sxx_spi_write,
155 .gather_write = mcp23sxx_spi_gather_write,
156 .read = mcp23sxx_spi_read,
159 #endif /* CONFIG_SPI_MASTER */
161 static int mcp_read(struct mcp23s08 *mcp, unsigned int reg, unsigned int *val)
163 return regmap_read(mcp->regmap, reg << mcp->reg_shift, val);
166 static int mcp_write(struct mcp23s08 *mcp, unsigned int reg, unsigned int val)
168 return regmap_write(mcp->regmap, reg << mcp->reg_shift, val);
171 static int mcp_update_cache(struct mcp23s08 *mcp)
175 for (i = 0; i < ARRAY_SIZE(mcp->cache); i++) {
176 ret = mcp_read(mcp, i, ®);
185 /*----------------------------------------------------------------------*/
187 /* A given spi_device can represent up to eight mcp23sxx chips
188 * sharing the same chipselect but using different addresses
189 * (e.g. chips #0 and #3 might be populated, but not #1 or $2).
190 * Driver data holds all the per-chip data.
192 struct mcp23s08_driver_data {
194 struct mcp23s08 *mcp[8];
195 struct mcp23s08 chip[];
199 static int mcp23s08_direction_input(struct gpio_chip *chip, unsigned offset)
201 struct mcp23s08 *mcp = gpiochip_get_data(chip);
204 mutex_lock(&mcp->lock);
205 mcp->cache[MCP_IODIR] |= (1 << offset);
206 status = mcp_write(mcp, MCP_IODIR, mcp->cache[MCP_IODIR]);
207 mutex_unlock(&mcp->lock);
211 static int mcp23s08_get(struct gpio_chip *chip, unsigned offset)
213 struct mcp23s08 *mcp = gpiochip_get_data(chip);
216 mutex_lock(&mcp->lock);
218 /* REVISIT reading this clears any IRQ ... */
219 ret = mcp_read(mcp, MCP_GPIO, &status);
223 mcp->cache[MCP_GPIO] = status;
224 status = !!(status & (1 << offset));
226 mutex_unlock(&mcp->lock);
230 static int __mcp23s08_set(struct mcp23s08 *mcp, unsigned mask, int value)
232 unsigned olat = mcp->cache[MCP_OLAT];
238 mcp->cache[MCP_OLAT] = olat;
239 return mcp_write(mcp, MCP_OLAT, olat);
242 static void mcp23s08_set(struct gpio_chip *chip, unsigned offset, int value)
244 struct mcp23s08 *mcp = gpiochip_get_data(chip);
245 unsigned mask = 1 << offset;
247 mutex_lock(&mcp->lock);
248 __mcp23s08_set(mcp, mask, value);
249 mutex_unlock(&mcp->lock);
253 mcp23s08_direction_output(struct gpio_chip *chip, unsigned offset, int value)
255 struct mcp23s08 *mcp = gpiochip_get_data(chip);
256 unsigned mask = 1 << offset;
259 mutex_lock(&mcp->lock);
260 status = __mcp23s08_set(mcp, mask, value);
262 mcp->cache[MCP_IODIR] &= ~mask;
263 status = mcp_write(mcp, MCP_IODIR, mcp->cache[MCP_IODIR]);
265 mutex_unlock(&mcp->lock);
269 /*----------------------------------------------------------------------*/
270 static irqreturn_t mcp23s08_irq(int irq, void *data)
272 struct mcp23s08 *mcp = data;
274 unsigned int child_irq;
276 mutex_lock(&mcp->lock);
277 if (mcp_read(mcp, MCP_INTF, &intf) < 0) {
278 mutex_unlock(&mcp->lock);
282 mcp->cache[MCP_INTF] = intf;
284 if (mcp_read(mcp, MCP_INTCAP, &intcap) < 0) {
285 mutex_unlock(&mcp->lock);
289 mcp->cache[MCP_INTCAP] = intcap;
290 mutex_unlock(&mcp->lock);
293 for (i = 0; i < mcp->chip.ngpio; i++) {
294 if ((BIT(i) & mcp->cache[MCP_INTF]) &&
295 ((BIT(i) & intcap & mcp->irq_rise) ||
296 (mcp->irq_fall & ~intcap & BIT(i)) ||
297 (BIT(i) & mcp->cache[MCP_INTCON]))) {
298 child_irq = irq_find_mapping(mcp->chip.irqdomain, i);
299 handle_nested_irq(child_irq);
306 static void mcp23s08_irq_mask(struct irq_data *data)
308 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
309 struct mcp23s08 *mcp = gpiochip_get_data(gc);
310 unsigned int pos = data->hwirq;
312 mcp->cache[MCP_GPINTEN] &= ~BIT(pos);
315 static void mcp23s08_irq_unmask(struct irq_data *data)
317 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
318 struct mcp23s08 *mcp = gpiochip_get_data(gc);
319 unsigned int pos = data->hwirq;
321 mcp->cache[MCP_GPINTEN] |= BIT(pos);
324 static int mcp23s08_irq_set_type(struct irq_data *data, unsigned int type)
326 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
327 struct mcp23s08 *mcp = gpiochip_get_data(gc);
328 unsigned int pos = data->hwirq;
331 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
332 mcp->cache[MCP_INTCON] &= ~BIT(pos);
333 mcp->irq_rise |= BIT(pos);
334 mcp->irq_fall |= BIT(pos);
335 } else if (type & IRQ_TYPE_EDGE_RISING) {
336 mcp->cache[MCP_INTCON] &= ~BIT(pos);
337 mcp->irq_rise |= BIT(pos);
338 mcp->irq_fall &= ~BIT(pos);
339 } else if (type & IRQ_TYPE_EDGE_FALLING) {
340 mcp->cache[MCP_INTCON] &= ~BIT(pos);
341 mcp->irq_rise &= ~BIT(pos);
342 mcp->irq_fall |= BIT(pos);
343 } else if (type & IRQ_TYPE_LEVEL_HIGH) {
344 mcp->cache[MCP_INTCON] |= BIT(pos);
345 mcp->cache[MCP_DEFVAL] &= ~BIT(pos);
346 } else if (type & IRQ_TYPE_LEVEL_LOW) {
347 mcp->cache[MCP_INTCON] |= BIT(pos);
348 mcp->cache[MCP_DEFVAL] |= BIT(pos);
355 static void mcp23s08_irq_bus_lock(struct irq_data *data)
357 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
358 struct mcp23s08 *mcp = gpiochip_get_data(gc);
360 mutex_lock(&mcp->irq_lock);
363 static void mcp23s08_irq_bus_unlock(struct irq_data *data)
365 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
366 struct mcp23s08 *mcp = gpiochip_get_data(gc);
368 mutex_lock(&mcp->lock);
369 mcp_write(mcp, MCP_GPINTEN, mcp->cache[MCP_GPINTEN]);
370 mcp_write(mcp, MCP_DEFVAL, mcp->cache[MCP_DEFVAL]);
371 mcp_write(mcp, MCP_INTCON, mcp->cache[MCP_INTCON]);
372 mutex_unlock(&mcp->lock);
373 mutex_unlock(&mcp->irq_lock);
376 static struct irq_chip mcp23s08_irq_chip = {
377 .name = "gpio-mcp23xxx",
378 .irq_mask = mcp23s08_irq_mask,
379 .irq_unmask = mcp23s08_irq_unmask,
380 .irq_set_type = mcp23s08_irq_set_type,
381 .irq_bus_lock = mcp23s08_irq_bus_lock,
382 .irq_bus_sync_unlock = mcp23s08_irq_bus_unlock,
385 static int mcp23s08_irq_setup(struct mcp23s08 *mcp)
387 struct gpio_chip *chip = &mcp->chip;
389 unsigned long irqflags = IRQF_ONESHOT | IRQF_SHARED;
391 mutex_init(&mcp->irq_lock);
393 if (mcp->irq_active_high)
394 irqflags |= IRQF_TRIGGER_HIGH;
396 irqflags |= IRQF_TRIGGER_LOW;
398 err = devm_request_threaded_irq(chip->parent, mcp->irq, NULL,
400 irqflags, dev_name(chip->parent), mcp);
402 dev_err(chip->parent, "unable to request IRQ#%d: %d\n",
407 err = gpiochip_irqchip_add_nested(chip,
413 dev_err(chip->parent,
414 "could not connect irqchip to gpiochip: %d\n", err);
418 gpiochip_set_nested_irqchip(chip,
425 /*----------------------------------------------------------------------*/
427 #ifdef CONFIG_DEBUG_FS
429 #include <linux/seq_file.h>
432 * This shows more info than the generic gpio dump code:
433 * pullups, deglitching, open drain drive.
435 static void mcp23s08_dbg_show(struct seq_file *s, struct gpio_chip *chip)
437 struct mcp23s08 *mcp;
442 mcp = gpiochip_get_data(chip);
444 /* NOTE: we only handle one bank for now ... */
445 bank = '0' + ((mcp->addr >> 1) & 0x7);
447 mutex_lock(&mcp->lock);
448 t = mcp_update_cache(mcp);
450 seq_printf(s, " I/O ERROR %d\n", t);
454 for (t = 0, mask = 1; t < chip->ngpio; t++, mask <<= 1) {
457 label = gpiochip_is_requested(chip, t);
461 seq_printf(s, " gpio-%-3d P%c.%d (%-12s) %s %s %s",
462 chip->base + t, bank, t, label,
463 (mcp->cache[MCP_IODIR] & mask) ? "in " : "out",
464 (mcp->cache[MCP_GPIO] & mask) ? "hi" : "lo",
465 (mcp->cache[MCP_GPPU] & mask) ? "up" : " ");
466 /* NOTE: ignoring the irq-related registers */
470 mutex_unlock(&mcp->lock);
474 #define mcp23s08_dbg_show NULL
477 /*----------------------------------------------------------------------*/
479 static int mcp23s08_probe_one(struct mcp23s08 *mcp, struct device *dev,
480 void *data, unsigned addr, unsigned type,
481 struct mcp23s08_platform_data *pdata, int cs)
486 mutex_init(&mcp->lock);
490 mcp->irq_active_high = false;
492 mcp->chip.direction_input = mcp23s08_direction_input;
493 mcp->chip.get = mcp23s08_get;
494 mcp->chip.direction_output = mcp23s08_direction_output;
495 mcp->chip.set = mcp23s08_set;
496 mcp->chip.dbg_show = mcp23s08_dbg_show;
497 #ifdef CONFIG_OF_GPIO
498 mcp->chip.of_gpio_n_cells = 2;
499 mcp->chip.of_node = dev->of_node;
503 #ifdef CONFIG_SPI_MASTER
505 mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp,
509 mcp->chip.label = "mcp23s08";
513 mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp,
516 mcp->chip.ngpio = 16;
517 mcp->chip.label = "mcp23s17";
521 mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp,
524 mcp->chip.ngpio = 16;
525 mcp->chip.label = "mcp23s18";
527 #endif /* CONFIG_SPI_MASTER */
529 #if IS_ENABLED(CONFIG_I2C)
531 mcp->regmap = devm_regmap_init_i2c(data, &mcp23x08_regmap);
534 mcp->chip.label = "mcp23008";
538 mcp->regmap = devm_regmap_init_i2c(data, &mcp23x17_regmap);
540 mcp->chip.ngpio = 16;
541 mcp->chip.label = "mcp23017";
543 #endif /* CONFIG_I2C */
546 dev_err(dev, "invalid device type (%d)\n", type);
550 if (IS_ERR(mcp->regmap))
551 return PTR_ERR(mcp->regmap);
553 mcp->chip.base = pdata->base;
554 mcp->chip.can_sleep = true;
555 mcp->chip.parent = dev;
556 mcp->chip.owner = THIS_MODULE;
558 /* verify MCP_IOCON.SEQOP = 0, so sequential reads work,
559 * and MCP_IOCON.HAEN = 1, so we work with all chips.
562 ret = mcp_read(mcp, MCP_IOCON, &status);
566 mcp->irq_controller = pdata->irq_controller;
567 if (mcp->irq && mcp->irq_controller) {
568 mcp->irq_active_high =
569 of_property_read_bool(mcp->chip.parent->of_node,
570 "microchip,irq-active-high");
572 mirror = pdata->mirror;
575 if ((status & IOCON_SEQOP) || !(status & IOCON_HAEN) || mirror ||
576 mcp->irq_active_high) {
577 /* mcp23s17 has IOCON twice, make sure they are in sync */
578 status &= ~(IOCON_SEQOP | (IOCON_SEQOP << 8));
579 status |= IOCON_HAEN | (IOCON_HAEN << 8);
580 if (mcp->irq_active_high)
581 status |= IOCON_INTPOL | (IOCON_INTPOL << 8);
583 status &= ~(IOCON_INTPOL | (IOCON_INTPOL << 8));
586 status |= IOCON_MIRROR | (IOCON_MIRROR << 8);
588 if (type == MCP_TYPE_S18)
589 status |= IOCON_INTCC | (IOCON_INTCC << 8);
591 ret = mcp_write(mcp, MCP_IOCON, status);
596 /* configure ~100K pullups */
597 ret = mcp_write(mcp, MCP_GPPU, pdata->chip[cs].pullups);
601 ret = mcp_update_cache(mcp);
605 /* disable inverter on input */
606 if (mcp->cache[MCP_IPOL] != 0) {
607 mcp->cache[MCP_IPOL] = 0;
608 ret = mcp_write(mcp, MCP_IPOL, 0);
614 if (mcp->cache[MCP_GPINTEN] != 0) {
615 mcp->cache[MCP_GPINTEN] = 0;
616 ret = mcp_write(mcp, MCP_GPINTEN, 0);
621 ret = gpiochip_add_data(&mcp->chip, mcp);
625 if (mcp->irq && mcp->irq_controller) {
626 ret = mcp23s08_irq_setup(mcp);
632 dev_dbg(dev, "can't setup chip %d, --> %d\n", addr, ret);
636 /*----------------------------------------------------------------------*/
639 #ifdef CONFIG_SPI_MASTER
640 static const struct of_device_id mcp23s08_spi_of_match[] = {
642 .compatible = "microchip,mcp23s08",
643 .data = (void *) MCP_TYPE_S08,
646 .compatible = "microchip,mcp23s17",
647 .data = (void *) MCP_TYPE_S17,
650 .compatible = "microchip,mcp23s18",
651 .data = (void *) MCP_TYPE_S18,
653 /* NOTE: The use of the mcp prefix is deprecated and will be removed. */
655 .compatible = "mcp,mcp23s08",
656 .data = (void *) MCP_TYPE_S08,
659 .compatible = "mcp,mcp23s17",
660 .data = (void *) MCP_TYPE_S17,
664 MODULE_DEVICE_TABLE(of, mcp23s08_spi_of_match);
667 #if IS_ENABLED(CONFIG_I2C)
668 static const struct of_device_id mcp23s08_i2c_of_match[] = {
670 .compatible = "microchip,mcp23008",
671 .data = (void *) MCP_TYPE_008,
674 .compatible = "microchip,mcp23017",
675 .data = (void *) MCP_TYPE_017,
677 /* NOTE: The use of the mcp prefix is deprecated and will be removed. */
679 .compatible = "mcp,mcp23008",
680 .data = (void *) MCP_TYPE_008,
683 .compatible = "mcp,mcp23017",
684 .data = (void *) MCP_TYPE_017,
688 MODULE_DEVICE_TABLE(of, mcp23s08_i2c_of_match);
690 #endif /* CONFIG_OF */
693 #if IS_ENABLED(CONFIG_I2C)
695 static int mcp230xx_probe(struct i2c_client *client,
696 const struct i2c_device_id *id)
698 struct mcp23s08_platform_data *pdata, local_pdata;
699 struct mcp23s08 *mcp;
701 const struct of_device_id *match;
703 match = of_match_device(of_match_ptr(mcp23s08_i2c_of_match),
706 pdata = &local_pdata;
708 pdata->chip[0].pullups = 0;
709 pdata->irq_controller = of_property_read_bool(
711 "interrupt-controller");
712 pdata->mirror = of_property_read_bool(client->dev.of_node,
713 "microchip,irq-mirror");
714 client->irq = irq_of_parse_and_map(client->dev.of_node, 0);
716 pdata = dev_get_platdata(&client->dev);
718 pdata = devm_kzalloc(&client->dev,
719 sizeof(struct mcp23s08_platform_data),
727 mcp = kzalloc(sizeof(*mcp), GFP_KERNEL);
731 mcp->irq = client->irq;
732 status = mcp23s08_probe_one(mcp, &client->dev, client, client->addr,
733 id->driver_data, pdata, 0);
737 i2c_set_clientdata(client, mcp);
747 static int mcp230xx_remove(struct i2c_client *client)
749 struct mcp23s08 *mcp = i2c_get_clientdata(client);
751 gpiochip_remove(&mcp->chip);
757 static const struct i2c_device_id mcp230xx_id[] = {
758 { "mcp23008", MCP_TYPE_008 },
759 { "mcp23017", MCP_TYPE_017 },
762 MODULE_DEVICE_TABLE(i2c, mcp230xx_id);
764 static struct i2c_driver mcp230xx_driver = {
767 .of_match_table = of_match_ptr(mcp23s08_i2c_of_match),
769 .probe = mcp230xx_probe,
770 .remove = mcp230xx_remove,
771 .id_table = mcp230xx_id,
774 static int __init mcp23s08_i2c_init(void)
776 return i2c_add_driver(&mcp230xx_driver);
779 static void mcp23s08_i2c_exit(void)
781 i2c_del_driver(&mcp230xx_driver);
786 static int __init mcp23s08_i2c_init(void) { return 0; }
787 static void mcp23s08_i2c_exit(void) { }
789 #endif /* CONFIG_I2C */
791 /*----------------------------------------------------------------------*/
793 #ifdef CONFIG_SPI_MASTER
795 static int mcp23s08_probe(struct spi_device *spi)
797 struct mcp23s08_platform_data *pdata, local_pdata;
800 struct mcp23s08_driver_data *data;
803 const struct of_device_id *match;
804 u32 spi_present_mask = 0;
806 match = of_match_device(of_match_ptr(mcp23s08_spi_of_match), &spi->dev);
808 type = (int)(uintptr_t)match->data;
809 status = of_property_read_u32(spi->dev.of_node,
810 "microchip,spi-present-mask", &spi_present_mask);
812 status = of_property_read_u32(spi->dev.of_node,
813 "mcp,spi-present-mask", &spi_present_mask);
816 "DT has no spi-present-mask\n");
820 if ((spi_present_mask <= 0) || (spi_present_mask >= 256)) {
821 dev_err(&spi->dev, "invalid spi-present-mask\n");
825 pdata = &local_pdata;
827 for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
828 pdata->chip[addr].pullups = 0;
829 if (spi_present_mask & (1 << addr))
832 pdata->irq_controller = of_property_read_bool(
834 "interrupt-controller");
835 pdata->mirror = of_property_read_bool(spi->dev.of_node,
836 "microchip,irq-mirror");
838 type = spi_get_device_id(spi)->driver_data;
839 pdata = dev_get_platdata(&spi->dev);
841 pdata = devm_kzalloc(&spi->dev,
842 sizeof(struct mcp23s08_platform_data),
847 for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
848 if (!pdata->chip[addr].is_present)
851 if ((type == MCP_TYPE_S08) && (addr > 3)) {
853 "mcp23s08 only supports address 0..3\n");
856 spi_present_mask |= 1 << addr;
863 data = devm_kzalloc(&spi->dev,
864 sizeof(*data) + chips * sizeof(struct mcp23s08),
869 spi_set_drvdata(spi, data);
871 spi->irq = irq_of_parse_and_map(spi->dev.of_node, 0);
873 for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
874 if (!(spi_present_mask & (1 << addr)))
877 data->mcp[addr] = &data->chip[chips];
878 data->mcp[addr]->irq = spi->irq;
879 status = mcp23s08_probe_one(data->mcp[addr], &spi->dev, spi,
880 0x40 | (addr << 1), type, pdata,
885 if (pdata->base != -1)
886 pdata->base += data->mcp[addr]->chip.ngpio;
887 ngpio += data->mcp[addr]->chip.ngpio;
891 /* NOTE: these chips have a relatively sane IRQ framework, with
892 * per-signal masking and level/edge triggering. It's not yet
899 for (addr = 0; addr < ARRAY_SIZE(data->mcp); addr++) {
901 if (!data->mcp[addr])
903 gpiochip_remove(&data->mcp[addr]->chip);
908 static int mcp23s08_remove(struct spi_device *spi)
910 struct mcp23s08_driver_data *data = spi_get_drvdata(spi);
913 for (addr = 0; addr < ARRAY_SIZE(data->mcp); addr++) {
915 if (!data->mcp[addr])
918 gpiochip_remove(&data->mcp[addr]->chip);
924 static const struct spi_device_id mcp23s08_ids[] = {
925 { "mcp23s08", MCP_TYPE_S08 },
926 { "mcp23s17", MCP_TYPE_S17 },
927 { "mcp23s18", MCP_TYPE_S18 },
930 MODULE_DEVICE_TABLE(spi, mcp23s08_ids);
932 static struct spi_driver mcp23s08_driver = {
933 .probe = mcp23s08_probe,
934 .remove = mcp23s08_remove,
935 .id_table = mcp23s08_ids,
938 .of_match_table = of_match_ptr(mcp23s08_spi_of_match),
942 static int __init mcp23s08_spi_init(void)
944 return spi_register_driver(&mcp23s08_driver);
947 static void mcp23s08_spi_exit(void)
949 spi_unregister_driver(&mcp23s08_driver);
954 static int __init mcp23s08_spi_init(void) { return 0; }
955 static void mcp23s08_spi_exit(void) { }
957 #endif /* CONFIG_SPI_MASTER */
959 /*----------------------------------------------------------------------*/
961 static int __init mcp23s08_init(void)
965 ret = mcp23s08_spi_init();
969 ret = mcp23s08_i2c_init();
980 /* register after spi/i2c postcore initcall and before
981 * subsys initcalls that may rely on these GPIOs
983 subsys_initcall(mcp23s08_init);
985 static void __exit mcp23s08_exit(void)
990 module_exit(mcp23s08_exit);
992 MODULE_LICENSE("GPL");