5 #include <linux/kernel.h>
6 #include <linux/device.h>
8 #include <linux/module.h>
10 #include <linux/i2c.h>
13 #include <linux/slab.h>
14 #include <asm/byteorder.h>
19 #define MCP_TYPE_S08 0
20 #define MCP_TYPE_S17 1
21 #define MCP_TYPE_008 2
22 #define MCP_TYPE_017 3
29 #define MCP_IODIR 0x00
31 #define MCP_GPINTEN 0x02
32 #define MCP_DEFVAL 0x03
33 #define MCP_INTCON 0x04
34 #define MCP_IOCON 0x05
35 # define IOCON_SEQOP (1 << 5)
36 # define IOCON_HAEN (1 << 3)
37 # define IOCON_ODR (1 << 2)
38 # define IOCON_INTPOL (1 << 1)
41 #define MCP_INTCAP 0x08
51 u16 *vals,
unsigned n);
80 #if IS_ENABLED(CONFIG_I2C)
96 int ret = mcp23008_read(mcp, reg++);
105 static int mcp23017_read(
struct mcp23s08 *mcp,
unsigned reg)
110 static int mcp23017_write(
struct mcp23s08 *mcp,
unsigned reg,
unsigned val)
116 mcp23017_read_regs(
struct mcp23s08 *mcp,
unsigned reg,
u16 *vals,
unsigned n)
119 int ret = mcp23017_read(mcp, reg++);
129 .
read = mcp23008_read,
130 .write = mcp23008_write,
131 .read_regs = mcp23008_read_regs,
135 .
read = mcp23017_read,
136 .write = mcp23017_write,
137 .read_regs = mcp23017_read_regs,
144 #ifdef CONFIG_SPI_MASTER
146 static int mcp23s08_read(
struct mcp23s08 *mcp,
unsigned reg)
151 tx[0] = mcp->
addr | 0x01;
154 return (status < 0) ? status : rx[0];
157 static int mcp23s08_write(
struct mcp23s08 *mcp,
unsigned reg,
unsigned val)
168 mcp23s08_read_regs(
struct mcp23s08 *mcp,
unsigned reg,
u16 *vals,
unsigned n)
173 if ((n + reg) >
sizeof mcp->
cache)
175 tx[0] = mcp->
addr | 0x01;
187 static int mcp23s17_read(
struct mcp23s08 *mcp,
unsigned reg)
192 tx[0] = mcp->
addr | 0x01;
195 return (status < 0) ? status : (rx[0] | (rx[1] << 8));
198 static int mcp23s17_write(
struct mcp23s08 *mcp,
unsigned reg,
unsigned val)
210 mcp23s17_read_regs(
struct mcp23s08 *mcp,
unsigned reg,
u16 *vals,
unsigned n)
215 if ((n + reg) >
sizeof mcp->
cache)
217 tx[0] = mcp->
addr | 0x01;
231 .
read = mcp23s08_read,
232 .write = mcp23s08_write,
233 .read_regs = mcp23s08_read_regs,
236 static const struct mcp23s08_ops mcp23s17_ops = {
237 .
read = mcp23s17_read,
238 .write = mcp23s17_write,
239 .read_regs = mcp23s17_read_regs,
246 static int mcp23s08_direction_input(
struct gpio_chip *
chip,
unsigned offset)
258 static int mcp23s08_get(
struct gpio_chip *chip,
unsigned offset)
271 status = !!(status & (1 <<
offset));
289 static void mcp23s08_set(
struct gpio_chip *chip,
unsigned offset,
int value)
292 unsigned mask = 1 <<
offset;
295 __mcp23s08_set(mcp, mask, value);
300 mcp23s08_direction_output(
struct gpio_chip *chip,
unsigned offset,
int value)
303 unsigned mask = 1 <<
offset;
307 status = __mcp23s08_set(mcp, mask, value);
318 #ifdef CONFIG_DEBUG_FS
336 bank =
'0' + ((mcp->
addr >> 1) & 0x7);
345 for (t = 0, mask = 1; t < chip->ngpio; t++, mask <<= 1) {
352 seq_printf(s,
" gpio-%-3d P%c.%d (%-12s) %s %s %s",
353 chip->base + t, bank, t, label,
365 #define mcp23s08_dbg_show NULL
372 unsigned type,
unsigned base,
unsigned pullups)
381 mcp->
chip.direction_input = mcp23s08_direction_input;
382 mcp->
chip.get = mcp23s08_get;
383 mcp->
chip.direction_output = mcp23s08_direction_output;
384 mcp->
chip.set = mcp23s08_set;
388 #ifdef CONFIG_SPI_MASTER
390 mcp->
ops = &mcp23s08_ops;
392 mcp->
chip.label =
"mcp23s08";
396 mcp->
ops = &mcp23s17_ops;
397 mcp->
chip.ngpio = 16;
398 mcp->
chip.label =
"mcp23s17";
402 #if IS_ENABLED(CONFIG_I2C)
404 mcp->
ops = &mcp23008_ops;
406 mcp->
chip.label =
"mcp23008";
410 mcp->
ops = &mcp23017_ops;
411 mcp->
chip.ngpio = 16;
412 mcp->
chip.label =
"mcp23017";
417 dev_err(dev,
"invalid device type (%d)\n", type);
421 mcp->
chip.base = base;
422 mcp->
chip.can_sleep = 1;
434 status &= ~(IOCON_SEQOP | (IOCON_SEQOP << 8));
435 status |= IOCON_HAEN | (IOCON_HAEN << 8);
469 dev_dbg(dev,
"can't setup chip %d, --> %d\n",
476 #if IS_ENABLED(CONFIG_I2C)
485 pdata = client->
dev.platform_data;
486 if (!pdata || !gpio_is_valid(pdata->
base)) {
487 dev_dbg(&client->
dev,
"invalid or missing platform data\n");
495 status = mcp23s08_probe_one(mcp, &client->
dev, client, client->
addr,
496 id->driver_data, pdata->
base,
497 pdata->
chip[0].pullups);
501 i2c_set_clientdata(client, mcp);
513 struct mcp23s08 *mcp = i2c_get_clientdata(client);
535 .probe = mcp230xx_probe,
537 .id_table = mcp230xx_id,
540 static int __init mcp23s08_i2c_init(
void)
542 return i2c_add_driver(&mcp230xx_driver);
545 static void mcp23s08_i2c_exit(
void)
552 static int __init mcp23s08_i2c_init(
void) {
return 0; }
553 static void mcp23s08_i2c_exit(
void) { }
559 #ifdef CONFIG_SPI_MASTER
572 pdata = spi->
dev.platform_data;
573 if (!pdata || !gpio_is_valid(pdata->
base)) {
574 dev_dbg(&spi->
dev,
"invalid or missing platform data\n");
579 if (!pdata->
chip[addr].is_present)
584 "mcp23s08 only supports address 0..3\n");
591 data = kzalloc(
sizeof *data + chips *
sizeof(
struct mcp23s08),
595 spi_set_drvdata(spi, data);
599 if (!pdata->
chip[addr].is_present)
603 status = mcp23s08_probe_one(data->
mcp[addr], &spi->
dev, spi,
604 0x40 | (addr << 1), type, base,
605 pdata->
chip[addr].pullups);
624 if (!data->
mcp[addr])
628 dev_err(&spi->
dev,
"%s --> %d\n",
"remove", tmp);
634 static int mcp23s08_remove(
struct spi_device *spi)
643 if (!data->
mcp[addr])
648 dev_err(&spi->
dev,
"%s --> %d\n",
"remove", tmp);
665 .
probe = mcp23s08_probe,
666 .remove = mcp23s08_remove,
667 .id_table = mcp23s08_ids,
674 static int __init mcp23s08_spi_init(
void)
679 static void mcp23s08_spi_exit(
void)
681 spi_unregister_driver(&mcp23s08_driver);
686 static int __init mcp23s08_spi_init(
void) {
return 0; }
687 static void mcp23s08_spi_exit(
void) { }
693 static int __init mcp23s08_init(
void)
697 ret = mcp23s08_spi_init();
701 ret = mcp23s08_i2c_init();
717 static void __exit mcp23s08_exit(
void)