blob: d96bf8a76f5cf430795073389147d401dae5601d [file] [log] [blame]
David Brownelle58b9e22008-02-04 22:28:25 -08001/*
Peter Korsgaard752ad5e2011-07-15 10:25:32 +02002 * MCP23S08 SPI/GPIO gpio expander driver
David Brownelle58b9e22008-02-04 22:28:25 -08003 */
4
5#include <linux/kernel.h>
6#include <linux/device.h>
David Brownelle58b9e22008-02-04 22:28:25 -08007#include <linux/mutex.h>
Paul Gortmakerbb207ef2011-07-03 13:38:09 -04008#include <linux/module.h>
H Hartley Sweetend120c172009-09-22 16:46:37 -07009#include <linux/gpio.h>
Peter Korsgaard752ad5e2011-07-15 10:25:32 +020010#include <linux/i2c.h>
David Brownelle58b9e22008-02-04 22:28:25 -080011#include <linux/spi/spi.h>
12#include <linux/spi/mcp23s08.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Peter Korsgaard0b7bb772011-03-09 17:56:30 +010014#include <asm/byteorder.h>
Lars Poeschel97ddb1c2013-04-04 12:02:02 +020015#include <linux/of.h>
16#include <linux/of_device.h>
David Brownelle58b9e22008-02-04 22:28:25 -080017
Peter Korsgaard0b7bb772011-03-09 17:56:30 +010018/**
19 * MCP types supported by driver
20 */
21#define MCP_TYPE_S08 0
22#define MCP_TYPE_S17 1
Peter Korsgaard752ad5e2011-07-15 10:25:32 +020023#define MCP_TYPE_008 2
24#define MCP_TYPE_017 3
David Brownelle58b9e22008-02-04 22:28:25 -080025
26/* Registers are all 8 bits wide.
27 *
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.
David Brownelle58b9e22008-02-04 22:28:25 -080030 */
31#define MCP_IODIR 0x00 /* init/reset: all ones */
32#define MCP_IPOL 0x01
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)
41#define MCP_GPPU 0x06
42#define MCP_INTF 0x07
43#define MCP_INTCAP 0x08
44#define MCP_GPIO 0x09
45#define MCP_OLAT 0x0a
46
Peter Korsgaard0b7bb772011-03-09 17:56:30 +010047struct mcp23s08;
48
49struct mcp23s08_ops {
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);
54};
55
David Brownelle58b9e22008-02-04 22:28:25 -080056struct mcp23s08 {
David Brownelle58b9e22008-02-04 22:28:25 -080057 u8 addr;
58
Peter Korsgaard0b7bb772011-03-09 17:56:30 +010059 u16 cache[11];
David Brownelle58b9e22008-02-04 22:28:25 -080060 /* lock protects the cached values */
61 struct mutex lock;
David Brownelle58b9e22008-02-04 22:28:25 -080062
63 struct gpio_chip chip;
64
Peter Korsgaard0b7bb772011-03-09 17:56:30 +010065 const struct mcp23s08_ops *ops;
Peter Korsgaardd62b98f2011-07-15 10:25:31 +020066 void *data; /* ops specific data */
David Brownelle58b9e22008-02-04 22:28:25 -080067};
68
Peter Korsgaard0b7bb772011-03-09 17:56:30 +010069/* A given spi_device can represent up to eight mcp23sxx chips
David Brownell8f1cc3b2008-07-25 01:46:09 -070070 * 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.
73 */
74struct mcp23s08_driver_data {
75 unsigned ngpio;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +010076 struct mcp23s08 *mcp[8];
David Brownell8f1cc3b2008-07-25 01:46:09 -070077 struct mcp23s08 chip[];
78};
79
Peter Korsgaard752ad5e2011-07-15 10:25:32 +020080/*----------------------------------------------------------------------*/
81
Daniel M. Weekscbf24fa2012-11-06 23:51:05 -050082#if IS_ENABLED(CONFIG_I2C)
Peter Korsgaard752ad5e2011-07-15 10:25:32 +020083
84static int mcp23008_read(struct mcp23s08 *mcp, unsigned reg)
85{
86 return i2c_smbus_read_byte_data(mcp->data, reg);
87}
88
89static int mcp23008_write(struct mcp23s08 *mcp, unsigned reg, unsigned val)
90{
91 return i2c_smbus_write_byte_data(mcp->data, reg, val);
92}
93
94static int
95mcp23008_read_regs(struct mcp23s08 *mcp, unsigned reg, u16 *vals, unsigned n)
96{
97 while (n--) {
98 int ret = mcp23008_read(mcp, reg++);
99 if (ret < 0)
100 return ret;
101 *vals++ = ret;
102 }
103
104 return 0;
105}
106
107static int mcp23017_read(struct mcp23s08 *mcp, unsigned reg)
108{
109 return i2c_smbus_read_word_data(mcp->data, reg << 1);
110}
111
112static int mcp23017_write(struct mcp23s08 *mcp, unsigned reg, unsigned val)
113{
114 return i2c_smbus_write_word_data(mcp->data, reg << 1, val);
115}
116
117static int
118mcp23017_read_regs(struct mcp23s08 *mcp, unsigned reg, u16 *vals, unsigned n)
119{
120 while (n--) {
121 int ret = mcp23017_read(mcp, reg++);
122 if (ret < 0)
123 return ret;
124 *vals++ = ret;
125 }
126
127 return 0;
128}
129
130static const struct mcp23s08_ops mcp23008_ops = {
131 .read = mcp23008_read,
132 .write = mcp23008_write,
133 .read_regs = mcp23008_read_regs,
134};
135
136static const struct mcp23s08_ops mcp23017_ops = {
137 .read = mcp23017_read,
138 .write = mcp23017_write,
139 .read_regs = mcp23017_read_regs,
140};
141
142#endif /* CONFIG_I2C */
143
144/*----------------------------------------------------------------------*/
145
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200146#ifdef CONFIG_SPI_MASTER
147
David Brownelle58b9e22008-02-04 22:28:25 -0800148static int mcp23s08_read(struct mcp23s08 *mcp, unsigned reg)
149{
150 u8 tx[2], rx[1];
151 int status;
152
153 tx[0] = mcp->addr | 0x01;
154 tx[1] = reg;
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200155 status = spi_write_then_read(mcp->data, tx, sizeof tx, rx, sizeof rx);
David Brownelle58b9e22008-02-04 22:28:25 -0800156 return (status < 0) ? status : rx[0];
157}
158
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100159static int mcp23s08_write(struct mcp23s08 *mcp, unsigned reg, unsigned val)
David Brownelle58b9e22008-02-04 22:28:25 -0800160{
161 u8 tx[3];
162
163 tx[0] = mcp->addr;
164 tx[1] = reg;
165 tx[2] = val;
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200166 return spi_write_then_read(mcp->data, tx, sizeof tx, NULL, 0);
David Brownelle58b9e22008-02-04 22:28:25 -0800167}
168
169static int
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100170mcp23s08_read_regs(struct mcp23s08 *mcp, unsigned reg, u16 *vals, unsigned n)
David Brownelle58b9e22008-02-04 22:28:25 -0800171{
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100172 u8 tx[2], *tmp;
173 int status;
David Brownelle58b9e22008-02-04 22:28:25 -0800174
175 if ((n + reg) > sizeof mcp->cache)
176 return -EINVAL;
177 tx[0] = mcp->addr | 0x01;
178 tx[1] = reg;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100179
180 tmp = (u8 *)vals;
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200181 status = spi_write_then_read(mcp->data, tx, sizeof tx, tmp, n);
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100182 if (status >= 0) {
183 while (n--)
184 vals[n] = tmp[n]; /* expand to 16bit */
185 }
186 return status;
David Brownelle58b9e22008-02-04 22:28:25 -0800187}
188
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100189static int mcp23s17_read(struct mcp23s08 *mcp, unsigned reg)
190{
191 u8 tx[2], rx[2];
192 int status;
193
194 tx[0] = mcp->addr | 0x01;
195 tx[1] = reg << 1;
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200196 status = spi_write_then_read(mcp->data, tx, sizeof tx, rx, sizeof rx);
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100197 return (status < 0) ? status : (rx[0] | (rx[1] << 8));
198}
199
200static int mcp23s17_write(struct mcp23s08 *mcp, unsigned reg, unsigned val)
201{
202 u8 tx[4];
203
204 tx[0] = mcp->addr;
205 tx[1] = reg << 1;
206 tx[2] = val;
207 tx[3] = val >> 8;
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200208 return spi_write_then_read(mcp->data, tx, sizeof tx, NULL, 0);
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100209}
210
211static int
212mcp23s17_read_regs(struct mcp23s08 *mcp, unsigned reg, u16 *vals, unsigned n)
213{
214 u8 tx[2];
215 int status;
216
217 if ((n + reg) > sizeof mcp->cache)
218 return -EINVAL;
219 tx[0] = mcp->addr | 0x01;
220 tx[1] = reg << 1;
221
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200222 status = spi_write_then_read(mcp->data, tx, sizeof tx,
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100223 (u8 *)vals, n * 2);
224 if (status >= 0) {
225 while (n--)
226 vals[n] = __le16_to_cpu((__le16)vals[n]);
227 }
228
229 return status;
230}
231
232static const struct mcp23s08_ops mcp23s08_ops = {
233 .read = mcp23s08_read,
234 .write = mcp23s08_write,
235 .read_regs = mcp23s08_read_regs,
236};
237
238static const struct mcp23s08_ops mcp23s17_ops = {
239 .read = mcp23s17_read,
240 .write = mcp23s17_write,
241 .read_regs = mcp23s17_read_regs,
242};
243
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200244#endif /* CONFIG_SPI_MASTER */
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100245
David Brownelle58b9e22008-02-04 22:28:25 -0800246/*----------------------------------------------------------------------*/
247
248static int mcp23s08_direction_input(struct gpio_chip *chip, unsigned offset)
249{
250 struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
251 int status;
252
253 mutex_lock(&mcp->lock);
254 mcp->cache[MCP_IODIR] |= (1 << offset);
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100255 status = mcp->ops->write(mcp, MCP_IODIR, mcp->cache[MCP_IODIR]);
David Brownelle58b9e22008-02-04 22:28:25 -0800256 mutex_unlock(&mcp->lock);
257 return status;
258}
259
260static int mcp23s08_get(struct gpio_chip *chip, unsigned offset)
261{
262 struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
263 int status;
264
265 mutex_lock(&mcp->lock);
266
267 /* REVISIT reading this clears any IRQ ... */
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100268 status = mcp->ops->read(mcp, MCP_GPIO);
David Brownelle58b9e22008-02-04 22:28:25 -0800269 if (status < 0)
270 status = 0;
271 else {
272 mcp->cache[MCP_GPIO] = status;
273 status = !!(status & (1 << offset));
274 }
275 mutex_unlock(&mcp->lock);
276 return status;
277}
278
279static int __mcp23s08_set(struct mcp23s08 *mcp, unsigned mask, int value)
280{
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100281 unsigned olat = mcp->cache[MCP_OLAT];
David Brownelle58b9e22008-02-04 22:28:25 -0800282
283 if (value)
284 olat |= mask;
285 else
286 olat &= ~mask;
287 mcp->cache[MCP_OLAT] = olat;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100288 return mcp->ops->write(mcp, MCP_OLAT, olat);
David Brownelle58b9e22008-02-04 22:28:25 -0800289}
290
291static void mcp23s08_set(struct gpio_chip *chip, unsigned offset, int value)
292{
293 struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100294 unsigned mask = 1 << offset;
David Brownelle58b9e22008-02-04 22:28:25 -0800295
296 mutex_lock(&mcp->lock);
297 __mcp23s08_set(mcp, mask, value);
298 mutex_unlock(&mcp->lock);
299}
300
301static int
302mcp23s08_direction_output(struct gpio_chip *chip, unsigned offset, int value)
303{
304 struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100305 unsigned mask = 1 << offset;
David Brownelle58b9e22008-02-04 22:28:25 -0800306 int status;
307
308 mutex_lock(&mcp->lock);
309 status = __mcp23s08_set(mcp, mask, value);
310 if (status == 0) {
311 mcp->cache[MCP_IODIR] &= ~mask;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100312 status = mcp->ops->write(mcp, MCP_IODIR, mcp->cache[MCP_IODIR]);
David Brownelle58b9e22008-02-04 22:28:25 -0800313 }
314 mutex_unlock(&mcp->lock);
315 return status;
316}
317
318/*----------------------------------------------------------------------*/
319
320#ifdef CONFIG_DEBUG_FS
321
322#include <linux/seq_file.h>
323
324/*
325 * This shows more info than the generic gpio dump code:
326 * pullups, deglitching, open drain drive.
327 */
328static void mcp23s08_dbg_show(struct seq_file *s, struct gpio_chip *chip)
329{
330 struct mcp23s08 *mcp;
331 char bank;
Roel Kluin1d1c1d92008-05-23 13:04:43 -0700332 int t;
David Brownelle58b9e22008-02-04 22:28:25 -0800333 unsigned mask;
334
335 mcp = container_of(chip, struct mcp23s08, chip);
336
337 /* NOTE: we only handle one bank for now ... */
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100338 bank = '0' + ((mcp->addr >> 1) & 0x7);
David Brownelle58b9e22008-02-04 22:28:25 -0800339
340 mutex_lock(&mcp->lock);
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100341 t = mcp->ops->read_regs(mcp, 0, mcp->cache, ARRAY_SIZE(mcp->cache));
David Brownelle58b9e22008-02-04 22:28:25 -0800342 if (t < 0) {
343 seq_printf(s, " I/O ERROR %d\n", t);
344 goto done;
345 }
346
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100347 for (t = 0, mask = 1; t < chip->ngpio; t++, mask <<= 1) {
David Brownelle58b9e22008-02-04 22:28:25 -0800348 const char *label;
349
350 label = gpiochip_is_requested(chip, t);
351 if (!label)
352 continue;
353
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",
Peter Korsgaardeb1567f2012-04-25 11:51:53 +0200358 (mcp->cache[MCP_GPPU] & mask) ? "up" : " ");
David Brownelle58b9e22008-02-04 22:28:25 -0800359 /* NOTE: ignoring the irq-related registers */
360 seq_printf(s, "\n");
361 }
362done:
363 mutex_unlock(&mcp->lock);
364}
365
366#else
367#define mcp23s08_dbg_show NULL
368#endif
369
370/*----------------------------------------------------------------------*/
371
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200372static int mcp23s08_probe_one(struct mcp23s08 *mcp, struct device *dev,
373 void *data, unsigned addr,
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100374 unsigned type, unsigned base, unsigned pullups)
David Brownelle58b9e22008-02-04 22:28:25 -0800375{
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200376 int status;
David Brownelle58b9e22008-02-04 22:28:25 -0800377
David Brownelle58b9e22008-02-04 22:28:25 -0800378 mutex_init(&mcp->lock);
379
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200380 mcp->data = data;
381 mcp->addr = addr;
David Brownelle58b9e22008-02-04 22:28:25 -0800382
David Brownelle58b9e22008-02-04 22:28:25 -0800383 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;
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200388#ifdef CONFIG_OF
389 mcp->chip.of_gpio_n_cells = 2;
390 mcp->chip.of_node = dev->of_node;
391#endif
David Brownelle58b9e22008-02-04 22:28:25 -0800392
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200393 switch (type) {
394#ifdef CONFIG_SPI_MASTER
395 case MCP_TYPE_S08:
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100396 mcp->ops = &mcp23s08_ops;
397 mcp->chip.ngpio = 8;
398 mcp->chip.label = "mcp23s08";
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200399 break;
400
401 case MCP_TYPE_S17:
402 mcp->ops = &mcp23s17_ops;
403 mcp->chip.ngpio = 16;
404 mcp->chip.label = "mcp23s17";
405 break;
406#endif /* CONFIG_SPI_MASTER */
407
Daniel M. Weekscbf24fa2012-11-06 23:51:05 -0500408#if IS_ENABLED(CONFIG_I2C)
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200409 case MCP_TYPE_008:
410 mcp->ops = &mcp23008_ops;
411 mcp->chip.ngpio = 8;
412 mcp->chip.label = "mcp23008";
413 break;
414
415 case MCP_TYPE_017:
416 mcp->ops = &mcp23017_ops;
417 mcp->chip.ngpio = 16;
418 mcp->chip.label = "mcp23017";
419 break;
420#endif /* CONFIG_I2C */
421
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200422 default:
423 dev_err(dev, "invalid device type (%d)\n", type);
424 return -EINVAL;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100425 }
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200426
David Brownell8f1cc3b2008-07-25 01:46:09 -0700427 mcp->chip.base = base;
David Brownelle58b9e22008-02-04 22:28:25 -0800428 mcp->chip.can_sleep = 1;
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200429 mcp->chip.dev = dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700430 mcp->chip.owner = THIS_MODULE;
David Brownelle58b9e22008-02-04 22:28:25 -0800431
David Brownell8f1cc3b2008-07-25 01:46:09 -0700432 /* verify MCP_IOCON.SEQOP = 0, so sequential reads work,
433 * and MCP_IOCON.HAEN = 1, so we work with all chips.
434 */
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100435 status = mcp->ops->read(mcp, MCP_IOCON);
David Brownelle58b9e22008-02-04 22:28:25 -0800436 if (status < 0)
437 goto fail;
David Brownell8f1cc3b2008-07-25 01:46:09 -0700438 if ((status & IOCON_SEQOP) || !(status & IOCON_HAEN)) {
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100439 /* 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);
David Brownelle58b9e22008-02-04 22:28:25 -0800443 if (status < 0)
444 goto fail;
445 }
446
447 /* configure ~100K pullups */
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100448 status = mcp->ops->write(mcp, MCP_GPPU, pullups);
David Brownelle58b9e22008-02-04 22:28:25 -0800449 if (status < 0)
450 goto fail;
451
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100452 status = mcp->ops->read_regs(mcp, 0, mcp->cache, ARRAY_SIZE(mcp->cache));
David Brownelle58b9e22008-02-04 22:28:25 -0800453 if (status < 0)
454 goto fail;
455
456 /* disable inverter on input */
457 if (mcp->cache[MCP_IPOL] != 0) {
458 mcp->cache[MCP_IPOL] = 0;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100459 status = mcp->ops->write(mcp, MCP_IPOL, 0);
460 if (status < 0)
461 goto fail;
David Brownelle58b9e22008-02-04 22:28:25 -0800462 }
463
464 /* disable irqs */
465 if (mcp->cache[MCP_GPINTEN] != 0) {
466 mcp->cache[MCP_GPINTEN] = 0;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100467 status = mcp->ops->write(mcp, MCP_GPINTEN, 0);
David Brownell8f1cc3b2008-07-25 01:46:09 -0700468 if (status < 0)
469 goto fail;
David Brownelle58b9e22008-02-04 22:28:25 -0800470 }
471
472 status = gpiochip_add(&mcp->chip);
David Brownell8f1cc3b2008-07-25 01:46:09 -0700473fail:
474 if (status < 0)
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200475 dev_dbg(dev, "can't setup chip %d, --> %d\n",
476 addr, status);
David Brownell8f1cc3b2008-07-25 01:46:09 -0700477 return status;
478}
479
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200480/*----------------------------------------------------------------------*/
481
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200482#ifdef CONFIG_OF
483#ifdef CONFIG_SPI_MASTER
484static struct of_device_id mcp23s08_spi_of_match[] = {
485 {
486 .compatible = "mcp,mcp23s08", .data = (void *) MCP_TYPE_S08,
487 },
488 {
489 .compatible = "mcp,mcp23s17", .data = (void *) MCP_TYPE_S17,
490 },
491 { },
492};
493MODULE_DEVICE_TABLE(of, mcp23s08_spi_of_match);
494#endif
495
496#if IS_ENABLED(CONFIG_I2C)
497static struct of_device_id mcp23s08_i2c_of_match[] = {
498 {
499 .compatible = "mcp,mcp23008", .data = (void *) MCP_TYPE_008,
500 },
501 {
502 .compatible = "mcp,mcp23017", .data = (void *) MCP_TYPE_017,
503 },
504 { },
505};
506MODULE_DEVICE_TABLE(of, mcp23s08_i2c_of_match);
507#endif
508#endif /* CONFIG_OF */
509
510
Daniel M. Weekscbf24fa2012-11-06 23:51:05 -0500511#if IS_ENABLED(CONFIG_I2C)
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200512
Bill Pemberton38363092012-11-19 13:22:34 -0500513static int mcp230xx_probe(struct i2c_client *client,
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200514 const struct i2c_device_id *id)
515{
516 struct mcp23s08_platform_data *pdata;
517 struct mcp23s08 *mcp;
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200518 int status, base, pullups;
519 const struct of_device_id *match;
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200520
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200521 match = of_match_device(of_match_ptr(mcp23s08_i2c_of_match),
522 &client->dev);
Jingoo Hane56aee12013-07-30 17:08:05 +0900523 pdata = dev_get_platdata(&client->dev);
Daniel M. Weeks8a564062013-07-19 00:19:58 -0400524 if (match || !pdata) {
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200525 base = -1;
526 pullups = 0;
527 } else {
Daniel M. Weeks8a564062013-07-19 00:19:58 -0400528 if (!gpio_is_valid(pdata->base)) {
529 dev_dbg(&client->dev, "invalid platform data\n");
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200530 return -EINVAL;
531 }
532 base = pdata->base;
533 pullups = pdata->chip[0].pullups;
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200534 }
535
536 mcp = kzalloc(sizeof *mcp, GFP_KERNEL);
537 if (!mcp)
538 return -ENOMEM;
539
540 status = mcp23s08_probe_one(mcp, &client->dev, client, client->addr,
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200541 id->driver_data, base, pullups);
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200542 if (status)
543 goto fail;
544
545 i2c_set_clientdata(client, mcp);
546
547 return 0;
548
549fail:
550 kfree(mcp);
551
552 return status;
553}
554
Bill Pemberton206210c2012-11-19 13:25:50 -0500555static int mcp230xx_remove(struct i2c_client *client)
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200556{
557 struct mcp23s08 *mcp = i2c_get_clientdata(client);
558 int status;
559
560 status = gpiochip_remove(&mcp->chip);
561 if (status == 0)
562 kfree(mcp);
563
564 return status;
565}
566
567static const struct i2c_device_id mcp230xx_id[] = {
568 { "mcp23008", MCP_TYPE_008 },
569 { "mcp23017", MCP_TYPE_017 },
570 { },
571};
572MODULE_DEVICE_TABLE(i2c, mcp230xx_id);
573
574static struct i2c_driver mcp230xx_driver = {
575 .driver = {
576 .name = "mcp230xx",
577 .owner = THIS_MODULE,
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200578 .of_match_table = of_match_ptr(mcp23s08_i2c_of_match),
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200579 },
580 .probe = mcp230xx_probe,
Bill Pemberton8283c4f2012-11-19 13:20:08 -0500581 .remove = mcp230xx_remove,
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200582 .id_table = mcp230xx_id,
583};
584
585static int __init mcp23s08_i2c_init(void)
586{
587 return i2c_add_driver(&mcp230xx_driver);
588}
589
590static void mcp23s08_i2c_exit(void)
591{
592 i2c_del_driver(&mcp230xx_driver);
593}
594
595#else
596
597static int __init mcp23s08_i2c_init(void) { return 0; }
598static void mcp23s08_i2c_exit(void) { }
599
600#endif /* CONFIG_I2C */
601
602/*----------------------------------------------------------------------*/
603
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200604#ifdef CONFIG_SPI_MASTER
605
David Brownell8f1cc3b2008-07-25 01:46:09 -0700606static int mcp23s08_probe(struct spi_device *spi)
607{
608 struct mcp23s08_platform_data *pdata;
609 unsigned addr;
610 unsigned chips = 0;
611 struct mcp23s08_driver_data *data;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100612 int status, type;
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200613 unsigned base = -1,
614 ngpio = 0,
615 pullups[ARRAY_SIZE(pdata->chip)];
616 const struct of_device_id *match;
617 u32 spi_present_mask = 0;
David Brownell8f1cc3b2008-07-25 01:46:09 -0700618
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200619 match = of_match_device(of_match_ptr(mcp23s08_spi_of_match), &spi->dev);
620 if (match) {
621 type = (int)match->data;
622 status = of_property_read_u32(spi->dev.of_node,
623 "mcp,spi-present-mask", &spi_present_mask);
624 if (status) {
625 dev_err(&spi->dev, "DT has no spi-present-mask\n");
626 return -ENODEV;
627 }
628 if ((spi_present_mask <= 0) || (spi_present_mask >= 256)) {
629 dev_err(&spi->dev, "invalid spi-present-mask\n");
630 return -ENODEV;
631 }
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100632
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200633 for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++)
634 pullups[addr] = 0;
635 } else {
636 type = spi_get_device_id(spi)->driver_data;
Jingoo Hane56aee12013-07-30 17:08:05 +0900637 pdata = dev_get_platdata(&spi->dev);
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200638 if (!pdata || !gpio_is_valid(pdata->base)) {
639 dev_dbg(&spi->dev,
640 "invalid or missing platform data\n");
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100641 return -EINVAL;
642 }
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200643
644 for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
645 if (!pdata->chip[addr].is_present)
646 continue;
647 chips++;
648 if ((type == MCP_TYPE_S08) && (addr > 3)) {
649 dev_err(&spi->dev,
650 "mcp23s08 only supports address 0..3\n");
651 return -EINVAL;
652 }
653 spi_present_mask |= 1 << addr;
654 pullups[addr] = pdata->chip[addr].pullups;
655 }
656
657 if (!chips)
658 return -ENODEV;
659
660 base = pdata->base;
David Brownell8f1cc3b2008-07-25 01:46:09 -0700661 }
David Brownell8f1cc3b2008-07-25 01:46:09 -0700662
663 data = kzalloc(sizeof *data + chips * sizeof(struct mcp23s08),
664 GFP_KERNEL);
665 if (!data)
666 return -ENOMEM;
667 spi_set_drvdata(spi, data);
668
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100669 for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200670 if (!(spi_present_mask & (1 << addr)))
David Brownell8f1cc3b2008-07-25 01:46:09 -0700671 continue;
672 chips--;
673 data->mcp[addr] = &data->chip[chips];
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200674 status = mcp23s08_probe_one(data->mcp[addr], &spi->dev, spi,
675 0x40 | (addr << 1), type, base,
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200676 pullups[addr]);
David Brownell8f1cc3b2008-07-25 01:46:09 -0700677 if (status < 0)
678 goto fail;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100679
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200680 if (base != -1)
681 base += (type == MCP_TYPE_S17) ? 16 : 8;
682 ngpio += (type == MCP_TYPE_S17) ? 16 : 8;
David Brownell8f1cc3b2008-07-25 01:46:09 -0700683 }
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200684 data->ngpio = ngpio;
David Brownelle58b9e22008-02-04 22:28:25 -0800685
686 /* NOTE: these chips have a relatively sane IRQ framework, with
687 * per-signal masking and level/edge triggering. It's not yet
688 * handled here...
689 */
690
David Brownelle58b9e22008-02-04 22:28:25 -0800691 return 0;
692
693fail:
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100694 for (addr = 0; addr < ARRAY_SIZE(data->mcp); addr++) {
David Brownell8f1cc3b2008-07-25 01:46:09 -0700695 int tmp;
696
697 if (!data->mcp[addr])
698 continue;
699 tmp = gpiochip_remove(&data->mcp[addr]->chip);
700 if (tmp < 0)
701 dev_err(&spi->dev, "%s --> %d\n", "remove", tmp);
702 }
703 kfree(data);
David Brownelle58b9e22008-02-04 22:28:25 -0800704 return status;
705}
706
707static int mcp23s08_remove(struct spi_device *spi)
708{
David Brownell8f1cc3b2008-07-25 01:46:09 -0700709 struct mcp23s08_driver_data *data = spi_get_drvdata(spi);
David Brownell8f1cc3b2008-07-25 01:46:09 -0700710 unsigned addr;
David Brownelle58b9e22008-02-04 22:28:25 -0800711 int status = 0;
712
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100713 for (addr = 0; addr < ARRAY_SIZE(data->mcp); addr++) {
David Brownell8f1cc3b2008-07-25 01:46:09 -0700714 int tmp;
715
716 if (!data->mcp[addr])
717 continue;
718
719 tmp = gpiochip_remove(&data->mcp[addr]->chip);
720 if (tmp < 0) {
721 dev_err(&spi->dev, "%s --> %d\n", "remove", tmp);
722 status = tmp;
723 }
724 }
David Brownelle58b9e22008-02-04 22:28:25 -0800725 if (status == 0)
David Brownell8f1cc3b2008-07-25 01:46:09 -0700726 kfree(data);
David Brownelle58b9e22008-02-04 22:28:25 -0800727 return status;
728}
729
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100730static const struct spi_device_id mcp23s08_ids[] = {
731 { "mcp23s08", MCP_TYPE_S08 },
732 { "mcp23s17", MCP_TYPE_S17 },
733 { },
734};
735MODULE_DEVICE_TABLE(spi, mcp23s08_ids);
736
David Brownelle58b9e22008-02-04 22:28:25 -0800737static struct spi_driver mcp23s08_driver = {
738 .probe = mcp23s08_probe,
739 .remove = mcp23s08_remove,
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100740 .id_table = mcp23s08_ids,
David Brownelle58b9e22008-02-04 22:28:25 -0800741 .driver = {
742 .name = "mcp23s08",
743 .owner = THIS_MODULE,
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200744 .of_match_table = of_match_ptr(mcp23s08_spi_of_match),
David Brownelle58b9e22008-02-04 22:28:25 -0800745 },
746};
747
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200748static int __init mcp23s08_spi_init(void)
749{
750 return spi_register_driver(&mcp23s08_driver);
751}
752
753static void mcp23s08_spi_exit(void)
754{
755 spi_unregister_driver(&mcp23s08_driver);
756}
757
758#else
759
760static int __init mcp23s08_spi_init(void) { return 0; }
761static void mcp23s08_spi_exit(void) { }
762
763#endif /* CONFIG_SPI_MASTER */
764
David Brownelle58b9e22008-02-04 22:28:25 -0800765/*----------------------------------------------------------------------*/
766
767static int __init mcp23s08_init(void)
768{
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200769 int ret;
770
771 ret = mcp23s08_spi_init();
772 if (ret)
773 goto spi_fail;
774
775 ret = mcp23s08_i2c_init();
776 if (ret)
777 goto i2c_fail;
778
779 return 0;
780
781 i2c_fail:
782 mcp23s08_spi_exit();
783 spi_fail:
784 return ret;
David Brownelle58b9e22008-02-04 22:28:25 -0800785}
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200786/* register after spi/i2c postcore initcall and before
David Brownell673c0c02008-10-15 22:02:46 -0700787 * subsys initcalls that may rely on these GPIOs
788 */
789subsys_initcall(mcp23s08_init);
David Brownelle58b9e22008-02-04 22:28:25 -0800790
791static void __exit mcp23s08_exit(void)
792{
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200793 mcp23s08_spi_exit();
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200794 mcp23s08_i2c_exit();
David Brownelle58b9e22008-02-04 22:28:25 -0800795}
796module_exit(mcp23s08_exit);
797
798MODULE_LICENSE("GPL");