1 // SPDX-License-Identifier: GPL-2.0-only
2 /* MCP23S08 SPI GPIO driver */
3 
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>
9 
10 #include "pinctrl-mcp23s08.h"
11 
12 #define MCP_MAX_DEV_PER_CS	8
13 
14 /*
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.
19  */
20 struct mcp23s08_driver_data {
21 	unsigned		ngpio;
22 	struct mcp23s08		*mcp[8];
23 	struct mcp23s08		chip[];
24 };
25 
mcp23sxx_spi_write(void * context,const void * data,size_t count)26 static int mcp23sxx_spi_write(void *context, const void *data, size_t count)
27 {
28 	struct mcp23s08 *mcp = context;
29 	struct spi_device *spi = to_spi_device(mcp->dev);
30 	struct spi_message m;
31 	struct spi_transfer t[2] = { { .tx_buf = &mcp->addr, .len = 1, },
32 				     { .tx_buf = data, .len = count, }, };
33 
34 	spi_message_init(&m);
35 	spi_message_add_tail(&t[0], &m);
36 	spi_message_add_tail(&t[1], &m);
37 
38 	return spi_sync(spi, &m);
39 }
40 
mcp23sxx_spi_gather_write(void * context,const void * reg,size_t reg_size,const void * val,size_t val_size)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)
44 {
45 	struct mcp23s08 *mcp = context;
46 	struct spi_device *spi = to_spi_device(mcp->dev);
47 	struct spi_message m;
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, }, };
51 
52 	spi_message_init(&m);
53 	spi_message_add_tail(&t[0], &m);
54 	spi_message_add_tail(&t[1], &m);
55 	spi_message_add_tail(&t[2], &m);
56 
57 	return spi_sync(spi, &m);
58 }
59 
mcp23sxx_spi_read(void * context,const void * reg,size_t reg_size,void * val,size_t val_size)60 static int mcp23sxx_spi_read(void *context, const void *reg, size_t reg_size,
61 				void *val, size_t val_size)
62 {
63 	struct mcp23s08 *mcp = context;
64 	struct spi_device *spi = to_spi_device(mcp->dev);
65 	u8 tx[2];
66 
67 	if (reg_size != 1)
68 		return -EINVAL;
69 
70 	tx[0] = mcp->addr | 0x01;
71 	tx[1] = *((u8 *) reg);
72 
73 	return spi_write_then_read(spi, tx, sizeof(tx), val, val_size);
74 }
75 
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,
80 };
81 
mcp23s08_spi_regmap_init(struct mcp23s08 * mcp,struct device * dev,unsigned int addr,unsigned int type)82 static int mcp23s08_spi_regmap_init(struct mcp23s08 *mcp, struct device *dev,
83 				    unsigned int addr, unsigned int type)
84 {
85 	const struct regmap_config *config;
86 	struct regmap_config *copy;
87 	const char *name;
88 
89 	switch (type) {
90 	case MCP_TYPE_S08:
91 		mcp->reg_shift = 0;
92 		mcp->chip.ngpio = 8;
93 		mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL, "mcp23s08.%d", addr);
94 		if (!mcp->chip.label)
95 			return -ENOMEM;
96 
97 		config = &mcp23x08_regmap;
98 		name = devm_kasprintf(dev, GFP_KERNEL, "%d", addr);
99 		if (!name)
100 			return -ENOMEM;
101 
102 		break;
103 
104 	case MCP_TYPE_S17:
105 		mcp->reg_shift = 1;
106 		mcp->chip.ngpio = 16;
107 		mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL, "mcp23s17.%d", addr);
108 		if (!mcp->chip.label)
109 			return -ENOMEM;
110 
111 		config = &mcp23x17_regmap;
112 		name = devm_kasprintf(dev, GFP_KERNEL, "%d", addr);
113 		if (!name)
114 			return -ENOMEM;
115 
116 		break;
117 
118 	case MCP_TYPE_S18:
119 		mcp->reg_shift = 1;
120 		mcp->chip.ngpio = 16;
121 		mcp->chip.label = "mcp23s18";
122 
123 		config = &mcp23x17_regmap;
124 		name = config->name;
125 		break;
126 
127 	default:
128 		dev_err(dev, "invalid device type (%d)\n", type);
129 		return -EINVAL;
130 	}
131 
132 	copy = devm_kmemdup(dev, config, sizeof(*config), GFP_KERNEL);
133 	if (!copy)
134 		return -ENOMEM;
135 
136 	copy->name = name;
137 
138 	mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp, copy);
139 	if (IS_ERR(mcp->regmap))
140 		dev_err(dev, "regmap init failed for %s\n", mcp->chip.label);
141 	return PTR_ERR_OR_ZERO(mcp->regmap);
142 }
143 
mcp23s08_probe(struct spi_device * spi)144 static int mcp23s08_probe(struct spi_device *spi)
145 {
146 	struct device *dev = &spi->dev;
147 	struct mcp23s08_driver_data *data;
148 	unsigned long spi_present_mask;
149 	const void *match;
150 	unsigned int addr;
151 	unsigned int ngpio = 0;
152 	int chips;
153 	int type;
154 	int ret;
155 	u32 v;
156 
157 	match = device_get_match_data(dev);
158 	if (match)
159 		type = (int)(uintptr_t)match;
160 	else
161 		type = spi_get_device_id(spi)->driver_data;
162 
163 	ret = device_property_read_u32(dev, "microchip,spi-present-mask", &v);
164 	if (ret) {
165 		ret = device_property_read_u32(dev, "mcp,spi-present-mask", &v);
166 		if (ret) {
167 			dev_err(dev, "missing spi-present-mask");
168 			return ret;
169 		}
170 	}
171 	spi_present_mask = v;
172 
173 	if (!spi_present_mask || spi_present_mask >= BIT(MCP_MAX_DEV_PER_CS)) {
174 		dev_err(dev, "invalid spi-present-mask");
175 		return -ENODEV;
176 	}
177 
178 	chips = hweight_long(spi_present_mask);
179 
180 	data = devm_kzalloc(dev, struct_size(data, chip, chips), GFP_KERNEL);
181 	if (!data)
182 		return -ENOMEM;
183 
184 	spi_set_drvdata(spi, data);
185 
186 	for_each_set_bit(addr, &spi_present_mask, MCP_MAX_DEV_PER_CS) {
187 		data->mcp[addr] = &data->chip[--chips];
188 		data->mcp[addr]->irq = spi->irq;
189 
190 		ret = mcp23s08_spi_regmap_init(data->mcp[addr], dev, addr, type);
191 		if (ret)
192 			return ret;
193 
194 		data->mcp[addr]->pinctrl_desc.name = devm_kasprintf(dev, GFP_KERNEL,
195 								    "mcp23xxx-pinctrl.%d",
196 								    addr);
197 		if (!data->mcp[addr]->pinctrl_desc.name)
198 			return -ENOMEM;
199 
200 		ret = mcp23s08_probe_one(data->mcp[addr], dev, 0x40 | (addr << 1), type, -1);
201 		if (ret < 0)
202 			return ret;
203 
204 		ngpio += data->mcp[addr]->chip.ngpio;
205 	}
206 	data->ngpio = ngpio;
207 
208 	return 0;
209 }
210 
211 static const struct spi_device_id mcp23s08_ids[] = {
212 	{ "mcp23s08", MCP_TYPE_S08 },
213 	{ "mcp23s17", MCP_TYPE_S17 },
214 	{ "mcp23s18", MCP_TYPE_S18 },
215 	{ }
216 };
217 MODULE_DEVICE_TABLE(spi, mcp23s08_ids);
218 
219 static const struct of_device_id mcp23s08_spi_of_match[] = {
220 	{
221 		.compatible = "microchip,mcp23s08",
222 		.data = (void *) MCP_TYPE_S08,
223 	},
224 	{
225 		.compatible = "microchip,mcp23s17",
226 		.data = (void *) MCP_TYPE_S17,
227 	},
228 	{
229 		.compatible = "microchip,mcp23s18",
230 		.data = (void *) MCP_TYPE_S18,
231 	},
232 /* NOTE: The use of the mcp prefix is deprecated and will be removed. */
233 	{
234 		.compatible = "mcp,mcp23s08",
235 		.data = (void *) MCP_TYPE_S08,
236 	},
237 	{
238 		.compatible = "mcp,mcp23s17",
239 		.data = (void *) MCP_TYPE_S17,
240 	},
241 	{ }
242 };
243 MODULE_DEVICE_TABLE(of, mcp23s08_spi_of_match);
244 
245 static struct spi_driver mcp23s08_driver = {
246 	.probe		= mcp23s08_probe,
247 	.id_table	= mcp23s08_ids,
248 	.driver = {
249 		.name	= "mcp23s08",
250 		.of_match_table = mcp23s08_spi_of_match,
251 	},
252 };
253 
mcp23s08_spi_init(void)254 static int __init mcp23s08_spi_init(void)
255 {
256 	return spi_register_driver(&mcp23s08_driver);
257 }
258 
259 /*
260  * Register after SPI postcore initcall and before
261  * subsys initcalls that may rely on these GPIOs.
262  */
263 subsys_initcall(mcp23s08_spi_init);
264 
mcp23s08_spi_exit(void)265 static void mcp23s08_spi_exit(void)
266 {
267 	spi_unregister_driver(&mcp23s08_driver);
268 }
269 module_exit(mcp23s08_spi_exit);
270 
271 MODULE_LICENSE("GPL");
272