2 * MCP23S08 SPI/GPIO gpio expander driver
5 #include <linux/kernel.h>
6 #include <linux/device.h>
7 #include <linux/mutex.h>
8 #include <linux/module.h>
9 #include <linux/gpio.h>
10 #include <linux/i2c.h>
11 #include <linux/spi/spi.h>
12 #include <linux/spi/mcp23s08.h>
13 #include <linux/slab.h>
14 #include <asm/byteorder.h>
16 #include <linux/of_device.h>
19 * MCP types supported by driver
21 #define MCP_TYPE_S08 0
22 #define MCP_TYPE_S17 1
23 #define MCP_TYPE_008 2
24 #define MCP_TYPE_017 3
26 /* Registers are all 8 bits wide.
28 * The mcp23s17 has twice as many bits, and can be configured to work
29 * with either 16 bit registers or with two adjacent 8 bit banks.
31 #define MCP_IODIR 0x00 /* init/reset: all ones */
33 #define MCP_GPINTEN 0x02
34 #define MCP_DEFVAL 0x03
35 #define MCP_INTCON 0x04
36 #define MCP_IOCON 0x05
37 # define IOCON_SEQOP (1 << 5)
38 # define IOCON_HAEN (1 << 3)
39 # define IOCON_ODR (1 << 2)
40 # define IOCON_INTPOL (1 << 1)
43 #define MCP_INTCAP 0x08
50 int (*read
)(struct mcp23s08
*mcp
, unsigned reg
);
51 int (*write
)(struct mcp23s08
*mcp
, unsigned reg
, unsigned val
);
52 int (*read_regs
)(struct mcp23s08
*mcp
, unsigned reg
,
53 u16
*vals
, unsigned n
);
60 /* lock protects the cached values */
63 struct gpio_chip chip
;
65 const struct mcp23s08_ops
*ops
;
66 void *data
; /* ops specific data */
69 /* A given spi_device can represent up to eight mcp23sxx chips
70 * sharing the same chipselect but using different addresses
71 * (e.g. chips #0 and #3 might be populated, but not #1 or $2).
72 * Driver data holds all the per-chip data.
74 struct mcp23s08_driver_data
{
76 struct mcp23s08
*mcp
[8];
77 struct mcp23s08 chip
[];
80 /*----------------------------------------------------------------------*/
82 #if IS_ENABLED(CONFIG_I2C)
84 static int mcp23008_read(struct mcp23s08
*mcp
, unsigned reg
)
86 return i2c_smbus_read_byte_data(mcp
->data
, reg
);
89 static int mcp23008_write(struct mcp23s08
*mcp
, unsigned reg
, unsigned val
)
91 return i2c_smbus_write_byte_data(mcp
->data
, reg
, val
);
95 mcp23008_read_regs(struct mcp23s08
*mcp
, unsigned reg
, u16
*vals
, unsigned n
)
98 int ret
= mcp23008_read(mcp
, reg
++);
107 static int mcp23017_read(struct mcp23s08
*mcp
, unsigned reg
)
109 return i2c_smbus_read_word_data(mcp
->data
, reg
<< 1);
112 static int mcp23017_write(struct mcp23s08
*mcp
, unsigned reg
, unsigned val
)
114 return i2c_smbus_write_word_data(mcp
->data
, reg
<< 1, val
);
118 mcp23017_read_regs(struct mcp23s08
*mcp
, unsigned reg
, u16
*vals
, unsigned n
)
121 int ret
= mcp23017_read(mcp
, reg
++);
130 static const struct mcp23s08_ops mcp23008_ops
= {
131 .read
= mcp23008_read
,
132 .write
= mcp23008_write
,
133 .read_regs
= mcp23008_read_regs
,
136 static const struct mcp23s08_ops mcp23017_ops
= {
137 .read
= mcp23017_read
,
138 .write
= mcp23017_write
,
139 .read_regs
= mcp23017_read_regs
,
142 #endif /* CONFIG_I2C */
144 /*----------------------------------------------------------------------*/
146 #ifdef CONFIG_SPI_MASTER
148 static int mcp23s08_read(struct mcp23s08
*mcp
, unsigned reg
)
153 tx
[0] = mcp
->addr
| 0x01;
155 status
= spi_write_then_read(mcp
->data
, tx
, sizeof tx
, rx
, sizeof rx
);
156 return (status
< 0) ? status
: rx
[0];
159 static int mcp23s08_write(struct mcp23s08
*mcp
, unsigned reg
, unsigned val
)
166 return spi_write_then_read(mcp
->data
, tx
, sizeof tx
, NULL
, 0);
170 mcp23s08_read_regs(struct mcp23s08
*mcp
, unsigned reg
, u16
*vals
, unsigned n
)
175 if ((n
+ reg
) > sizeof mcp
->cache
)
177 tx
[0] = mcp
->addr
| 0x01;
181 status
= spi_write_then_read(mcp
->data
, tx
, sizeof tx
, tmp
, n
);
184 vals
[n
] = tmp
[n
]; /* expand to 16bit */
189 static int mcp23s17_read(struct mcp23s08
*mcp
, unsigned reg
)
194 tx
[0] = mcp
->addr
| 0x01;
196 status
= spi_write_then_read(mcp
->data
, tx
, sizeof tx
, rx
, sizeof rx
);
197 return (status
< 0) ? status
: (rx
[0] | (rx
[1] << 8));
200 static int mcp23s17_write(struct mcp23s08
*mcp
, unsigned reg
, unsigned val
)
208 return spi_write_then_read(mcp
->data
, tx
, sizeof tx
, NULL
, 0);
212 mcp23s17_read_regs(struct mcp23s08
*mcp
, unsigned reg
, u16
*vals
, unsigned n
)
217 if ((n
+ reg
) > sizeof mcp
->cache
)
219 tx
[0] = mcp
->addr
| 0x01;
222 status
= spi_write_then_read(mcp
->data
, tx
, sizeof tx
,
226 vals
[n
] = __le16_to_cpu((__le16
)vals
[n
]);
232 static const struct mcp23s08_ops mcp23s08_ops
= {
233 .read
= mcp23s08_read
,
234 .write
= mcp23s08_write
,
235 .read_regs
= mcp23s08_read_regs
,
238 static const struct mcp23s08_ops mcp23s17_ops
= {
239 .read
= mcp23s17_read
,
240 .write
= mcp23s17_write
,
241 .read_regs
= mcp23s17_read_regs
,
244 #endif /* CONFIG_SPI_MASTER */
246 /*----------------------------------------------------------------------*/
248 static int mcp23s08_direction_input(struct gpio_chip
*chip
, unsigned offset
)
250 struct mcp23s08
*mcp
= container_of(chip
, struct mcp23s08
, chip
);
253 mutex_lock(&mcp
->lock
);
254 mcp
->cache
[MCP_IODIR
] |= (1 << offset
);
255 status
= mcp
->ops
->write(mcp
, MCP_IODIR
, mcp
->cache
[MCP_IODIR
]);
256 mutex_unlock(&mcp
->lock
);
260 static int mcp23s08_get(struct gpio_chip
*chip
, unsigned offset
)
262 struct mcp23s08
*mcp
= container_of(chip
, struct mcp23s08
, chip
);
265 mutex_lock(&mcp
->lock
);
267 /* REVISIT reading this clears any IRQ ... */
268 status
= mcp
->ops
->read(mcp
, MCP_GPIO
);
272 mcp
->cache
[MCP_GPIO
] = status
;
273 status
= !!(status
& (1 << offset
));
275 mutex_unlock(&mcp
->lock
);
279 static int __mcp23s08_set(struct mcp23s08
*mcp
, unsigned mask
, int value
)
281 unsigned olat
= mcp
->cache
[MCP_OLAT
];
287 mcp
->cache
[MCP_OLAT
] = olat
;
288 return mcp
->ops
->write(mcp
, MCP_OLAT
, olat
);
291 static void mcp23s08_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
293 struct mcp23s08
*mcp
= container_of(chip
, struct mcp23s08
, chip
);
294 unsigned mask
= 1 << offset
;
296 mutex_lock(&mcp
->lock
);
297 __mcp23s08_set(mcp
, mask
, value
);
298 mutex_unlock(&mcp
->lock
);
302 mcp23s08_direction_output(struct gpio_chip
*chip
, unsigned offset
, int value
)
304 struct mcp23s08
*mcp
= container_of(chip
, struct mcp23s08
, chip
);
305 unsigned mask
= 1 << offset
;
308 mutex_lock(&mcp
->lock
);
309 status
= __mcp23s08_set(mcp
, mask
, value
);
311 mcp
->cache
[MCP_IODIR
] &= ~mask
;
312 status
= mcp
->ops
->write(mcp
, MCP_IODIR
, mcp
->cache
[MCP_IODIR
]);
314 mutex_unlock(&mcp
->lock
);
318 /*----------------------------------------------------------------------*/
320 #ifdef CONFIG_DEBUG_FS
322 #include <linux/seq_file.h>
325 * This shows more info than the generic gpio dump code:
326 * pullups, deglitching, open drain drive.
328 static void mcp23s08_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
330 struct mcp23s08
*mcp
;
335 mcp
= container_of(chip
, struct mcp23s08
, chip
);
337 /* NOTE: we only handle one bank for now ... */
338 bank
= '0' + ((mcp
->addr
>> 1) & 0x7);
340 mutex_lock(&mcp
->lock
);
341 t
= mcp
->ops
->read_regs(mcp
, 0, mcp
->cache
, ARRAY_SIZE(mcp
->cache
));
343 seq_printf(s
, " I/O ERROR %d\n", t
);
347 for (t
= 0, mask
= 1; t
< chip
->ngpio
; t
++, mask
<<= 1) {
350 label
= gpiochip_is_requested(chip
, t
);
354 seq_printf(s
, " gpio-%-3d P%c.%d (%-12s) %s %s %s",
355 chip
->base
+ t
, bank
, t
, label
,
356 (mcp
->cache
[MCP_IODIR
] & mask
) ? "in " : "out",
357 (mcp
->cache
[MCP_GPIO
] & mask
) ? "hi" : "lo",
358 (mcp
->cache
[MCP_GPPU
] & mask
) ? "up" : " ");
359 /* NOTE: ignoring the irq-related registers */
363 mutex_unlock(&mcp
->lock
);
367 #define mcp23s08_dbg_show NULL
370 /*----------------------------------------------------------------------*/
372 static int mcp23s08_probe_one(struct mcp23s08
*mcp
, struct device
*dev
,
373 void *data
, unsigned addr
,
374 unsigned type
, unsigned base
, unsigned pullups
)
378 mutex_init(&mcp
->lock
);
383 mcp
->chip
.direction_input
= mcp23s08_direction_input
;
384 mcp
->chip
.get
= mcp23s08_get
;
385 mcp
->chip
.direction_output
= mcp23s08_direction_output
;
386 mcp
->chip
.set
= mcp23s08_set
;
387 mcp
->chip
.dbg_show
= mcp23s08_dbg_show
;
389 mcp
->chip
.of_gpio_n_cells
= 2;
390 mcp
->chip
.of_node
= dev
->of_node
;
394 #ifdef CONFIG_SPI_MASTER
396 mcp
->ops
= &mcp23s08_ops
;
398 mcp
->chip
.label
= "mcp23s08";
402 mcp
->ops
= &mcp23s17_ops
;
403 mcp
->chip
.ngpio
= 16;
404 mcp
->chip
.label
= "mcp23s17";
406 #endif /* CONFIG_SPI_MASTER */
408 #if IS_ENABLED(CONFIG_I2C)
410 mcp
->ops
= &mcp23008_ops
;
412 mcp
->chip
.label
= "mcp23008";
416 mcp
->ops
= &mcp23017_ops
;
417 mcp
->chip
.ngpio
= 16;
418 mcp
->chip
.label
= "mcp23017";
420 #endif /* CONFIG_I2C */
423 dev_err(dev
, "invalid device type (%d)\n", type
);
427 mcp
->chip
.base
= base
;
428 mcp
->chip
.can_sleep
= 1;
430 mcp
->chip
.owner
= THIS_MODULE
;
432 /* verify MCP_IOCON.SEQOP = 0, so sequential reads work,
433 * and MCP_IOCON.HAEN = 1, so we work with all chips.
435 status
= mcp
->ops
->read(mcp
, MCP_IOCON
);
438 if ((status
& IOCON_SEQOP
) || !(status
& IOCON_HAEN
)) {
439 /* mcp23s17 has IOCON twice, make sure they are in sync */
440 status
&= ~(IOCON_SEQOP
| (IOCON_SEQOP
<< 8));
441 status
|= IOCON_HAEN
| (IOCON_HAEN
<< 8);
442 status
= mcp
->ops
->write(mcp
, MCP_IOCON
, status
);
447 /* configure ~100K pullups */
448 status
= mcp
->ops
->write(mcp
, MCP_GPPU
, pullups
);
452 status
= mcp
->ops
->read_regs(mcp
, 0, mcp
->cache
, ARRAY_SIZE(mcp
->cache
));
456 /* disable inverter on input */
457 if (mcp
->cache
[MCP_IPOL
] != 0) {
458 mcp
->cache
[MCP_IPOL
] = 0;
459 status
= mcp
->ops
->write(mcp
, MCP_IPOL
, 0);
465 if (mcp
->cache
[MCP_GPINTEN
] != 0) {
466 mcp
->cache
[MCP_GPINTEN
] = 0;
467 status
= mcp
->ops
->write(mcp
, MCP_GPINTEN
, 0);
472 status
= gpiochip_add(&mcp
->chip
);
475 dev_dbg(dev
, "can't setup chip %d, --> %d\n",
480 /*----------------------------------------------------------------------*/
483 #ifdef CONFIG_SPI_MASTER
484 static struct of_device_id mcp23s08_spi_of_match
[] = {
486 .compatible
= "microchip,mcp23s08",
487 .data
= (void *) MCP_TYPE_S08
,
490 .compatible
= "microchip,mcp23s17",
491 .data
= (void *) MCP_TYPE_S17
,
493 /* NOTE: The use of the mcp prefix is deprecated and will be removed. */
495 .compatible
= "mcp,mcp23s08",
496 .data
= (void *) MCP_TYPE_S08
,
499 .compatible
= "mcp,mcp23s17",
500 .data
= (void *) MCP_TYPE_S17
,
504 MODULE_DEVICE_TABLE(of
, mcp23s08_spi_of_match
);
507 #if IS_ENABLED(CONFIG_I2C)
508 static struct of_device_id mcp23s08_i2c_of_match
[] = {
510 .compatible
= "microchip,mcp23008",
511 .data
= (void *) MCP_TYPE_008
,
514 .compatible
= "microchip,mcp23017",
515 .data
= (void *) MCP_TYPE_017
,
517 /* NOTE: The use of the mcp prefix is deprecated and will be removed. */
519 .compatible
= "mcp,mcp23008",
520 .data
= (void *) MCP_TYPE_008
,
523 .compatible
= "mcp,mcp23017",
524 .data
= (void *) MCP_TYPE_017
,
528 MODULE_DEVICE_TABLE(of
, mcp23s08_i2c_of_match
);
530 #endif /* CONFIG_OF */
533 #if IS_ENABLED(CONFIG_I2C)
535 static int mcp230xx_probe(struct i2c_client
*client
,
536 const struct i2c_device_id
*id
)
538 struct mcp23s08_platform_data
*pdata
;
539 struct mcp23s08
*mcp
;
540 int status
, base
, pullups
;
541 const struct of_device_id
*match
;
543 match
= of_match_device(of_match_ptr(mcp23s08_i2c_of_match
),
545 pdata
= dev_get_platdata(&client
->dev
);
546 if (match
|| !pdata
) {
550 if (!gpio_is_valid(pdata
->base
)) {
551 dev_dbg(&client
->dev
, "invalid platform data\n");
555 pullups
= pdata
->chip
[0].pullups
;
558 mcp
= kzalloc(sizeof *mcp
, GFP_KERNEL
);
562 status
= mcp23s08_probe_one(mcp
, &client
->dev
, client
, client
->addr
,
563 id
->driver_data
, base
, pullups
);
567 i2c_set_clientdata(client
, mcp
);
577 static int mcp230xx_remove(struct i2c_client
*client
)
579 struct mcp23s08
*mcp
= i2c_get_clientdata(client
);
582 status
= gpiochip_remove(&mcp
->chip
);
589 static const struct i2c_device_id mcp230xx_id
[] = {
590 { "mcp23008", MCP_TYPE_008
},
591 { "mcp23017", MCP_TYPE_017
},
594 MODULE_DEVICE_TABLE(i2c
, mcp230xx_id
);
596 static struct i2c_driver mcp230xx_driver
= {
599 .owner
= THIS_MODULE
,
600 .of_match_table
= of_match_ptr(mcp23s08_i2c_of_match
),
602 .probe
= mcp230xx_probe
,
603 .remove
= mcp230xx_remove
,
604 .id_table
= mcp230xx_id
,
607 static int __init
mcp23s08_i2c_init(void)
609 return i2c_add_driver(&mcp230xx_driver
);
612 static void mcp23s08_i2c_exit(void)
614 i2c_del_driver(&mcp230xx_driver
);
619 static int __init
mcp23s08_i2c_init(void) { return 0; }
620 static void mcp23s08_i2c_exit(void) { }
622 #endif /* CONFIG_I2C */
624 /*----------------------------------------------------------------------*/
626 #ifdef CONFIG_SPI_MASTER
628 static int mcp23s08_probe(struct spi_device
*spi
)
630 struct mcp23s08_platform_data
*pdata
;
633 struct mcp23s08_driver_data
*data
;
637 pullups
[ARRAY_SIZE(pdata
->chip
)];
638 const struct of_device_id
*match
;
639 u32 spi_present_mask
= 0;
641 match
= of_match_device(of_match_ptr(mcp23s08_spi_of_match
), &spi
->dev
);
643 type
= (int)match
->data
;
644 status
= of_property_read_u32(spi
->dev
.of_node
,
645 "microchip,spi-present-mask", &spi_present_mask
);
647 status
= of_property_read_u32(spi
->dev
.of_node
,
648 "mcp,spi-present-mask", &spi_present_mask
);
651 "DT has no spi-present-mask\n");
655 if ((spi_present_mask
<= 0) || (spi_present_mask
>= 256)) {
656 dev_err(&spi
->dev
, "invalid spi-present-mask\n");
660 for (addr
= 0; addr
< ARRAY_SIZE(pdata
->chip
); addr
++) {
662 if (spi_present_mask
& (1 << addr
))
666 type
= spi_get_device_id(spi
)->driver_data
;
667 pdata
= dev_get_platdata(&spi
->dev
);
668 if (!pdata
|| !gpio_is_valid(pdata
->base
)) {
670 "invalid or missing platform data\n");
674 for (addr
= 0; addr
< ARRAY_SIZE(pdata
->chip
); addr
++) {
675 if (!pdata
->chip
[addr
].is_present
)
678 if ((type
== MCP_TYPE_S08
) && (addr
> 3)) {
680 "mcp23s08 only supports address 0..3\n");
683 spi_present_mask
|= 1 << addr
;
684 pullups
[addr
] = pdata
->chip
[addr
].pullups
;
693 data
= kzalloc(sizeof *data
+ chips
* sizeof(struct mcp23s08
),
697 spi_set_drvdata(spi
, data
);
699 for (addr
= 0; addr
< ARRAY_SIZE(pdata
->chip
); addr
++) {
700 if (!(spi_present_mask
& (1 << addr
)))
703 data
->mcp
[addr
] = &data
->chip
[chips
];
704 status
= mcp23s08_probe_one(data
->mcp
[addr
], &spi
->dev
, spi
,
705 0x40 | (addr
<< 1), type
, base
,
711 base
+= (type
== MCP_TYPE_S17
) ? 16 : 8;
712 ngpio
+= (type
== MCP_TYPE_S17
) ? 16 : 8;
716 /* NOTE: these chips have a relatively sane IRQ framework, with
717 * per-signal masking and level/edge triggering. It's not yet
724 for (addr
= 0; addr
< ARRAY_SIZE(data
->mcp
); addr
++) {
727 if (!data
->mcp
[addr
])
729 tmp
= gpiochip_remove(&data
->mcp
[addr
]->chip
);
731 dev_err(&spi
->dev
, "%s --> %d\n", "remove", tmp
);
737 static int mcp23s08_remove(struct spi_device
*spi
)
739 struct mcp23s08_driver_data
*data
= spi_get_drvdata(spi
);
743 for (addr
= 0; addr
< ARRAY_SIZE(data
->mcp
); addr
++) {
746 if (!data
->mcp
[addr
])
749 tmp
= gpiochip_remove(&data
->mcp
[addr
]->chip
);
751 dev_err(&spi
->dev
, "%s --> %d\n", "remove", tmp
);
760 static const struct spi_device_id mcp23s08_ids
[] = {
761 { "mcp23s08", MCP_TYPE_S08
},
762 { "mcp23s17", MCP_TYPE_S17
},
765 MODULE_DEVICE_TABLE(spi
, mcp23s08_ids
);
767 static struct spi_driver mcp23s08_driver
= {
768 .probe
= mcp23s08_probe
,
769 .remove
= mcp23s08_remove
,
770 .id_table
= mcp23s08_ids
,
773 .owner
= THIS_MODULE
,
774 .of_match_table
= of_match_ptr(mcp23s08_spi_of_match
),
778 static int __init
mcp23s08_spi_init(void)
780 return spi_register_driver(&mcp23s08_driver
);
783 static void mcp23s08_spi_exit(void)
785 spi_unregister_driver(&mcp23s08_driver
);
790 static int __init
mcp23s08_spi_init(void) { return 0; }
791 static void mcp23s08_spi_exit(void) { }
793 #endif /* CONFIG_SPI_MASTER */
795 /*----------------------------------------------------------------------*/
797 static int __init
mcp23s08_init(void)
801 ret
= mcp23s08_spi_init();
805 ret
= mcp23s08_i2c_init();
816 /* register after spi/i2c postcore initcall and before
817 * subsys initcalls that may rely on these GPIOs
819 subsys_initcall(mcp23s08_init
);
821 static void __exit
mcp23s08_exit(void)
826 module_exit(mcp23s08_exit
);
828 MODULE_LICENSE("GPL");