1 // SPDX-License-Identifier: GPL-2.0-only
2 /* MCP23S08 SPI GPIO driver */
4 #include <linux/mod_devicetable.h>
5 #include <linux/module.h>
6 #include <linux/property.h>
7 #include <linux/regmap.h>
8 #include <linux/spi/spi.h>
10 #include "pinctrl-mcp23s08.h"
12 #define MCP_MAX_DEV_PER_CS 8
15 * A given spi_device can represent up to eight mcp23sxx chips
16 * sharing the same chipselect but using different addresses
17 * (e.g. chips #0 and #3 might be populated, but not #1 or #2).
18 * Driver data holds all the per-chip data.
20 struct mcp23s08_driver_data
{
22 struct mcp23s08
*mcp
[8];
23 struct mcp23s08 chip
[];
26 static int mcp23sxx_spi_write(void *context
, const void *data
, size_t count
)
28 struct mcp23s08
*mcp
= context
;
29 struct spi_device
*spi
= to_spi_device(mcp
->dev
);
31 struct spi_transfer t
[2] = { { .tx_buf
= &mcp
->addr
, .len
= 1, },
32 { .tx_buf
= data
, .len
= count
, }, };
35 spi_message_add_tail(&t
[0], &m
);
36 spi_message_add_tail(&t
[1], &m
);
38 return spi_sync(spi
, &m
);
41 static int mcp23sxx_spi_gather_write(void *context
,
42 const void *reg
, size_t reg_size
,
43 const void *val
, size_t val_size
)
45 struct mcp23s08
*mcp
= context
;
46 struct spi_device
*spi
= to_spi_device(mcp
->dev
);
48 struct spi_transfer t
[3] = { { .tx_buf
= &mcp
->addr
, .len
= 1, },
49 { .tx_buf
= reg
, .len
= reg_size
, },
50 { .tx_buf
= val
, .len
= val_size
, }, };
53 spi_message_add_tail(&t
[0], &m
);
54 spi_message_add_tail(&t
[1], &m
);
55 spi_message_add_tail(&t
[2], &m
);
57 return spi_sync(spi
, &m
);
60 static int mcp23sxx_spi_read(void *context
, const void *reg
, size_t reg_size
,
61 void *val
, size_t val_size
)
63 struct mcp23s08
*mcp
= context
;
64 struct spi_device
*spi
= to_spi_device(mcp
->dev
);
70 tx
[0] = mcp
->addr
| 0x01;
71 tx
[1] = *((u8
*) reg
);
73 return spi_write_then_read(spi
, tx
, sizeof(tx
), val
, val_size
);
76 static const struct regmap_bus mcp23sxx_spi_regmap
= {
77 .write
= mcp23sxx_spi_write
,
78 .gather_write
= mcp23sxx_spi_gather_write
,
79 .read
= mcp23sxx_spi_read
,
82 static int mcp23s08_spi_regmap_init(struct mcp23s08
*mcp
, struct device
*dev
,
84 const struct mcp23s08_info
*info
)
86 struct regmap_config
*copy
;
91 mcp
->chip
.label
= devm_kasprintf(dev
, GFP_KERNEL
, "mcp23s08.%d", addr
);
95 name
= devm_kasprintf(dev
, GFP_KERNEL
, "%d", addr
);
102 mcp
->chip
.label
= devm_kasprintf(dev
, GFP_KERNEL
, "mcp23s17.%d", addr
);
103 if (!mcp
->chip
.label
)
106 name
= devm_kasprintf(dev
, GFP_KERNEL
, "%d", addr
);
113 mcp
->chip
.label
= info
->label
;
114 name
= info
->regmap
->name
;
118 dev_err(dev
, "invalid device type (%d)\n", info
->type
);
122 mcp
->reg_shift
= info
->reg_shift
;
123 mcp
->chip
.ngpio
= info
->ngpio
;
124 copy
= devm_kmemdup(dev
, info
->regmap
, sizeof(*info
->regmap
), GFP_KERNEL
);
130 mcp
->regmap
= devm_regmap_init(dev
, &mcp23sxx_spi_regmap
, mcp
, copy
);
131 if (IS_ERR(mcp
->regmap
))
132 dev_err(dev
, "regmap init failed for %s\n", mcp
->chip
.label
);
133 return PTR_ERR_OR_ZERO(mcp
->regmap
);
136 static int mcp23s08_probe(struct spi_device
*spi
)
138 struct mcp23s08_driver_data
*data
;
139 const struct mcp23s08_info
*info
;
140 struct device
*dev
= &spi
->dev
;
141 unsigned long spi_present_mask
;
142 unsigned int ngpio
= 0;
148 info
= spi_get_device_match_data(spi
);
150 ret
= device_property_read_u32(dev
, "microchip,spi-present-mask", &v
);
152 ret
= device_property_read_u32(dev
, "mcp,spi-present-mask", &v
);
154 dev_err(dev
, "missing spi-present-mask");
158 spi_present_mask
= v
;
160 if (!spi_present_mask
|| spi_present_mask
>= BIT(MCP_MAX_DEV_PER_CS
)) {
161 dev_err(dev
, "invalid spi-present-mask");
165 chips
= hweight_long(spi_present_mask
);
167 data
= devm_kzalloc(dev
, struct_size(data
, chip
, chips
), GFP_KERNEL
);
171 spi_set_drvdata(spi
, data
);
173 for_each_set_bit(addr
, &spi_present_mask
, MCP_MAX_DEV_PER_CS
) {
174 data
->mcp
[addr
] = &data
->chip
[--chips
];
175 data
->mcp
[addr
]->irq
= spi
->irq
;
177 ret
= mcp23s08_spi_regmap_init(data
->mcp
[addr
], dev
, addr
, info
);
181 data
->mcp
[addr
]->pinctrl_desc
.name
= devm_kasprintf(dev
, GFP_KERNEL
,
182 "mcp23xxx-pinctrl.%d",
184 if (!data
->mcp
[addr
]->pinctrl_desc
.name
)
187 ret
= mcp23s08_probe_one(data
->mcp
[addr
], dev
, 0x40 | (addr
<< 1),
192 ngpio
+= data
->mcp
[addr
]->chip
.ngpio
;
199 static const struct mcp23s08_info mcp23s08_spi
= {
200 .regmap
= &mcp23x08_regmap
,
201 .type
= MCP_TYPE_S08
,
206 static const struct mcp23s08_info mcp23s17_spi
= {
207 .regmap
= &mcp23x17_regmap
,
208 .type
= MCP_TYPE_S17
,
213 static const struct mcp23s08_info mcp23s18_spi
= {
214 .regmap
= &mcp23x17_regmap
,
216 .type
= MCP_TYPE_S18
,
221 static const struct spi_device_id mcp23s08_ids
[] = {
222 { "mcp23s08", (kernel_ulong_t
)&mcp23s08_spi
},
223 { "mcp23s17", (kernel_ulong_t
)&mcp23s17_spi
},
224 { "mcp23s18", (kernel_ulong_t
)&mcp23s18_spi
},
227 MODULE_DEVICE_TABLE(spi
, mcp23s08_ids
);
229 static const struct of_device_id mcp23s08_spi_of_match
[] = {
230 { .compatible
= "microchip,mcp23s08", .data
= &mcp23s08_spi
},
231 { .compatible
= "microchip,mcp23s17", .data
= &mcp23s17_spi
},
232 { .compatible
= "microchip,mcp23s18", .data
= &mcp23s18_spi
},
233 /* NOTE: The use of the mcp prefix is deprecated and will be removed. */
234 { .compatible
= "mcp,mcp23s08", .data
= &mcp23s08_spi
},
235 { .compatible
= "mcp,mcp23s17", .data
= &mcp23s17_spi
},
238 MODULE_DEVICE_TABLE(of
, mcp23s08_spi_of_match
);
240 static struct spi_driver mcp23s08_driver
= {
241 .probe
= mcp23s08_probe
,
242 .id_table
= mcp23s08_ids
,
245 .of_match_table
= mcp23s08_spi_of_match
,
249 static int __init
mcp23s08_spi_init(void)
251 return spi_register_driver(&mcp23s08_driver
);
255 * Register after SPI postcore initcall and before
256 * subsys initcalls that may rely on these GPIOs.
258 subsys_initcall(mcp23s08_spi_init
);
260 static void mcp23s08_spi_exit(void)
262 spi_unregister_driver(&mcp23s08_driver
);
264 module_exit(mcp23s08_spi_exit
);
266 MODULE_DESCRIPTION("MCP23S08 SPI GPIO driver");
267 MODULE_LICENSE("GPL");