| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * mcp23s08.c - SPI gpio expander driver | 
 | 3 |  */ | 
 | 4 |  | 
 | 5 | #include <linux/kernel.h> | 
 | 6 | #include <linux/device.h> | 
 | 7 | #include <linux/workqueue.h> | 
 | 8 | #include <linux/mutex.h> | 
| H Hartley Sweeten | d120c17 | 2009-09-22 16:46:37 -0700 | [diff] [blame] | 9 | #include <linux/gpio.h> | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 10 | #include <linux/spi/spi.h> | 
 | 11 | #include <linux/spi/mcp23s08.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 13 | #include <asm/byteorder.h> | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 14 |  | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 15 | /** | 
 | 16 |  * MCP types supported by driver | 
 | 17 |  */ | 
 | 18 | #define MCP_TYPE_S08	0 | 
 | 19 | #define MCP_TYPE_S17	1 | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 20 |  | 
 | 21 | /* Registers are all 8 bits wide. | 
 | 22 |  * | 
 | 23 |  * The mcp23s17 has twice as many bits, and can be configured to work | 
 | 24 |  * with either 16 bit registers or with two adjacent 8 bit banks. | 
 | 25 |  * | 
 | 26 |  * Also, there are I2C versions of both chips. | 
 | 27 |  */ | 
 | 28 | #define MCP_IODIR	0x00		/* init/reset:  all ones */ | 
 | 29 | #define MCP_IPOL	0x01 | 
 | 30 | #define MCP_GPINTEN	0x02 | 
 | 31 | #define MCP_DEFVAL	0x03 | 
 | 32 | #define MCP_INTCON	0x04 | 
 | 33 | #define MCP_IOCON	0x05 | 
 | 34 | #	define IOCON_SEQOP	(1 << 5) | 
 | 35 | #	define IOCON_HAEN	(1 << 3) | 
 | 36 | #	define IOCON_ODR	(1 << 2) | 
 | 37 | #	define IOCON_INTPOL	(1 << 1) | 
 | 38 | #define MCP_GPPU	0x06 | 
 | 39 | #define MCP_INTF	0x07 | 
 | 40 | #define MCP_INTCAP	0x08 | 
 | 41 | #define MCP_GPIO	0x09 | 
 | 42 | #define MCP_OLAT	0x0a | 
 | 43 |  | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 44 | struct mcp23s08; | 
 | 45 |  | 
 | 46 | struct mcp23s08_ops { | 
 | 47 | 	int	(*read)(struct mcp23s08 *mcp, unsigned reg); | 
 | 48 | 	int	(*write)(struct mcp23s08 *mcp, unsigned reg, unsigned val); | 
 | 49 | 	int	(*read_regs)(struct mcp23s08 *mcp, unsigned reg, | 
 | 50 | 			     u16 *vals, unsigned n); | 
 | 51 | }; | 
 | 52 |  | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 53 | struct mcp23s08 { | 
 | 54 | 	struct spi_device	*spi; | 
 | 55 | 	u8			addr; | 
 | 56 |  | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 57 | 	u16			cache[11]; | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 58 | 	/* lock protects the cached values */ | 
 | 59 | 	struct mutex		lock; | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 60 |  | 
 | 61 | 	struct gpio_chip	chip; | 
 | 62 |  | 
 | 63 | 	struct work_struct	work; | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 64 |  | 
 | 65 | 	const struct mcp23s08_ops	*ops; | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 66 | }; | 
 | 67 |  | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 68 | /* A given spi_device can represent up to eight mcp23sxx chips | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 69 |  * sharing the same chipselect but using different addresses | 
 | 70 |  * (e.g. chips #0 and #3 might be populated, but not #1 or $2). | 
 | 71 |  * Driver data holds all the per-chip data. | 
 | 72 |  */ | 
 | 73 | struct mcp23s08_driver_data { | 
 | 74 | 	unsigned		ngpio; | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 75 | 	struct mcp23s08		*mcp[8]; | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 76 | 	struct mcp23s08		chip[]; | 
 | 77 | }; | 
 | 78 |  | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 79 | static int mcp23s08_read(struct mcp23s08 *mcp, unsigned reg) | 
 | 80 | { | 
 | 81 | 	u8	tx[2], rx[1]; | 
 | 82 | 	int	status; | 
 | 83 |  | 
 | 84 | 	tx[0] = mcp->addr | 0x01; | 
 | 85 | 	tx[1] = reg; | 
 | 86 | 	status = spi_write_then_read(mcp->spi, tx, sizeof tx, rx, sizeof rx); | 
 | 87 | 	return (status < 0) ? status : rx[0]; | 
 | 88 | } | 
 | 89 |  | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 90 | static int mcp23s08_write(struct mcp23s08 *mcp, unsigned reg, unsigned val) | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 91 | { | 
 | 92 | 	u8	tx[3]; | 
 | 93 |  | 
 | 94 | 	tx[0] = mcp->addr; | 
 | 95 | 	tx[1] = reg; | 
 | 96 | 	tx[2] = val; | 
 | 97 | 	return spi_write_then_read(mcp->spi, tx, sizeof tx, NULL, 0); | 
 | 98 | } | 
 | 99 |  | 
 | 100 | static int | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 101 | mcp23s08_read_regs(struct mcp23s08 *mcp, unsigned reg, u16 *vals, unsigned n) | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 102 | { | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 103 | 	u8	tx[2], *tmp; | 
 | 104 | 	int	status; | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 105 |  | 
 | 106 | 	if ((n + reg) > sizeof mcp->cache) | 
 | 107 | 		return -EINVAL; | 
 | 108 | 	tx[0] = mcp->addr | 0x01; | 
 | 109 | 	tx[1] = reg; | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 110 |  | 
 | 111 | 	tmp = (u8 *)vals; | 
 | 112 | 	status = spi_write_then_read(mcp->spi, tx, sizeof tx, tmp, n); | 
 | 113 | 	if (status >= 0) { | 
 | 114 | 		while (n--) | 
 | 115 | 			vals[n] = tmp[n]; /* expand to 16bit */ | 
 | 116 | 	} | 
 | 117 | 	return status; | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 118 | } | 
 | 119 |  | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 120 | static int mcp23s17_read(struct mcp23s08 *mcp, unsigned reg) | 
 | 121 | { | 
 | 122 | 	u8	tx[2], rx[2]; | 
 | 123 | 	int	status; | 
 | 124 |  | 
 | 125 | 	tx[0] = mcp->addr | 0x01; | 
 | 126 | 	tx[1] = reg << 1; | 
 | 127 | 	status = spi_write_then_read(mcp->spi, tx, sizeof tx, rx, sizeof rx); | 
 | 128 | 	return (status < 0) ? status : (rx[0] | (rx[1] << 8)); | 
 | 129 | } | 
 | 130 |  | 
 | 131 | static int mcp23s17_write(struct mcp23s08 *mcp, unsigned reg, unsigned val) | 
 | 132 | { | 
 | 133 | 	u8	tx[4]; | 
 | 134 |  | 
 | 135 | 	tx[0] = mcp->addr; | 
 | 136 | 	tx[1] = reg << 1; | 
 | 137 | 	tx[2] = val; | 
 | 138 | 	tx[3] = val >> 8; | 
 | 139 | 	return spi_write_then_read(mcp->spi, tx, sizeof tx, NULL, 0); | 
 | 140 | } | 
 | 141 |  | 
 | 142 | static int | 
 | 143 | mcp23s17_read_regs(struct mcp23s08 *mcp, unsigned reg, u16 *vals, unsigned n) | 
 | 144 | { | 
 | 145 | 	u8	tx[2]; | 
 | 146 | 	int	status; | 
 | 147 |  | 
 | 148 | 	if ((n + reg) > sizeof mcp->cache) | 
 | 149 | 		return -EINVAL; | 
 | 150 | 	tx[0] = mcp->addr | 0x01; | 
 | 151 | 	tx[1] = reg << 1; | 
 | 152 |  | 
 | 153 | 	status = spi_write_then_read(mcp->spi, tx, sizeof tx, | 
 | 154 | 				     (u8 *)vals, n * 2); | 
 | 155 | 	if (status >= 0) { | 
 | 156 | 		while (n--) | 
 | 157 | 			vals[n] = __le16_to_cpu((__le16)vals[n]); | 
 | 158 | 	} | 
 | 159 |  | 
 | 160 | 	return status; | 
 | 161 | } | 
 | 162 |  | 
 | 163 | static const struct mcp23s08_ops mcp23s08_ops = { | 
 | 164 | 	.read		= mcp23s08_read, | 
 | 165 | 	.write		= mcp23s08_write, | 
 | 166 | 	.read_regs	= mcp23s08_read_regs, | 
 | 167 | }; | 
 | 168 |  | 
 | 169 | static const struct mcp23s08_ops mcp23s17_ops = { | 
 | 170 | 	.read		= mcp23s17_read, | 
 | 171 | 	.write		= mcp23s17_write, | 
 | 172 | 	.read_regs	= mcp23s17_read_regs, | 
 | 173 | }; | 
 | 174 |  | 
 | 175 |  | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 176 | /*----------------------------------------------------------------------*/ | 
 | 177 |  | 
 | 178 | static int mcp23s08_direction_input(struct gpio_chip *chip, unsigned offset) | 
 | 179 | { | 
 | 180 | 	struct mcp23s08	*mcp = container_of(chip, struct mcp23s08, chip); | 
 | 181 | 	int status; | 
 | 182 |  | 
 | 183 | 	mutex_lock(&mcp->lock); | 
 | 184 | 	mcp->cache[MCP_IODIR] |= (1 << offset); | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 185 | 	status = mcp->ops->write(mcp, MCP_IODIR, mcp->cache[MCP_IODIR]); | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 186 | 	mutex_unlock(&mcp->lock); | 
 | 187 | 	return status; | 
 | 188 | } | 
 | 189 |  | 
 | 190 | static int mcp23s08_get(struct gpio_chip *chip, unsigned offset) | 
 | 191 | { | 
 | 192 | 	struct mcp23s08	*mcp = container_of(chip, struct mcp23s08, chip); | 
 | 193 | 	int status; | 
 | 194 |  | 
 | 195 | 	mutex_lock(&mcp->lock); | 
 | 196 |  | 
 | 197 | 	/* REVISIT reading this clears any IRQ ... */ | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 198 | 	status = mcp->ops->read(mcp, MCP_GPIO); | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 199 | 	if (status < 0) | 
 | 200 | 		status = 0; | 
 | 201 | 	else { | 
 | 202 | 		mcp->cache[MCP_GPIO] = status; | 
 | 203 | 		status = !!(status & (1 << offset)); | 
 | 204 | 	} | 
 | 205 | 	mutex_unlock(&mcp->lock); | 
 | 206 | 	return status; | 
 | 207 | } | 
 | 208 |  | 
 | 209 | static int __mcp23s08_set(struct mcp23s08 *mcp, unsigned mask, int value) | 
 | 210 | { | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 211 | 	unsigned olat = mcp->cache[MCP_OLAT]; | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 212 |  | 
 | 213 | 	if (value) | 
 | 214 | 		olat |= mask; | 
 | 215 | 	else | 
 | 216 | 		olat &= ~mask; | 
 | 217 | 	mcp->cache[MCP_OLAT] = olat; | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 218 | 	return mcp->ops->write(mcp, MCP_OLAT, olat); | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 219 | } | 
 | 220 |  | 
 | 221 | static void mcp23s08_set(struct gpio_chip *chip, unsigned offset, int value) | 
 | 222 | { | 
 | 223 | 	struct mcp23s08	*mcp = container_of(chip, struct mcp23s08, chip); | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 224 | 	unsigned mask = 1 << offset; | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 225 |  | 
 | 226 | 	mutex_lock(&mcp->lock); | 
 | 227 | 	__mcp23s08_set(mcp, mask, value); | 
 | 228 | 	mutex_unlock(&mcp->lock); | 
 | 229 | } | 
 | 230 |  | 
 | 231 | static int | 
 | 232 | mcp23s08_direction_output(struct gpio_chip *chip, unsigned offset, int value) | 
 | 233 | { | 
 | 234 | 	struct mcp23s08	*mcp = container_of(chip, struct mcp23s08, chip); | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 235 | 	unsigned mask = 1 << offset; | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 236 | 	int status; | 
 | 237 |  | 
 | 238 | 	mutex_lock(&mcp->lock); | 
 | 239 | 	status = __mcp23s08_set(mcp, mask, value); | 
 | 240 | 	if (status == 0) { | 
 | 241 | 		mcp->cache[MCP_IODIR] &= ~mask; | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 242 | 		status = mcp->ops->write(mcp, MCP_IODIR, mcp->cache[MCP_IODIR]); | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 243 | 	} | 
 | 244 | 	mutex_unlock(&mcp->lock); | 
 | 245 | 	return status; | 
 | 246 | } | 
 | 247 |  | 
 | 248 | /*----------------------------------------------------------------------*/ | 
 | 249 |  | 
 | 250 | #ifdef CONFIG_DEBUG_FS | 
 | 251 |  | 
 | 252 | #include <linux/seq_file.h> | 
 | 253 |  | 
 | 254 | /* | 
 | 255 |  * This shows more info than the generic gpio dump code: | 
 | 256 |  * pullups, deglitching, open drain drive. | 
 | 257 |  */ | 
 | 258 | static void mcp23s08_dbg_show(struct seq_file *s, struct gpio_chip *chip) | 
 | 259 | { | 
 | 260 | 	struct mcp23s08	*mcp; | 
 | 261 | 	char		bank; | 
| Roel Kluin | 1d1c1d9 | 2008-05-23 13:04:43 -0700 | [diff] [blame] | 262 | 	int		t; | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 263 | 	unsigned	mask; | 
 | 264 |  | 
 | 265 | 	mcp = container_of(chip, struct mcp23s08, chip); | 
 | 266 |  | 
 | 267 | 	/* NOTE: we only handle one bank for now ... */ | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 268 | 	bank = '0' + ((mcp->addr >> 1) & 0x7); | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 269 |  | 
 | 270 | 	mutex_lock(&mcp->lock); | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 271 | 	t = mcp->ops->read_regs(mcp, 0, mcp->cache, ARRAY_SIZE(mcp->cache)); | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 272 | 	if (t < 0) { | 
 | 273 | 		seq_printf(s, " I/O ERROR %d\n", t); | 
 | 274 | 		goto done; | 
 | 275 | 	} | 
 | 276 |  | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 277 | 	for (t = 0, mask = 1; t < chip->ngpio; t++, mask <<= 1) { | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 278 | 		const char	*label; | 
 | 279 |  | 
 | 280 | 		label = gpiochip_is_requested(chip, t); | 
 | 281 | 		if (!label) | 
 | 282 | 			continue; | 
 | 283 |  | 
 | 284 | 		seq_printf(s, " gpio-%-3d P%c.%d (%-12s) %s %s %s", | 
 | 285 | 			chip->base + t, bank, t, label, | 
 | 286 | 			(mcp->cache[MCP_IODIR] & mask) ? "in " : "out", | 
 | 287 | 			(mcp->cache[MCP_GPIO] & mask) ? "hi" : "lo", | 
 | 288 | 			(mcp->cache[MCP_GPPU] & mask) ? "  " : "up"); | 
 | 289 | 		/* NOTE:  ignoring the irq-related registers */ | 
 | 290 | 		seq_printf(s, "\n"); | 
 | 291 | 	} | 
 | 292 | done: | 
 | 293 | 	mutex_unlock(&mcp->lock); | 
 | 294 | } | 
 | 295 |  | 
 | 296 | #else | 
 | 297 | #define mcp23s08_dbg_show	NULL | 
 | 298 | #endif | 
 | 299 |  | 
 | 300 | /*----------------------------------------------------------------------*/ | 
 | 301 |  | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 302 | static int mcp23s08_probe_one(struct spi_device *spi, unsigned addr, | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 303 | 			      unsigned type, unsigned base, unsigned pullups) | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 304 | { | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 305 | 	struct mcp23s08_driver_data	*data = spi_get_drvdata(spi); | 
 | 306 | 	struct mcp23s08			*mcp = data->mcp[addr]; | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 307 | 	int				status; | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 308 |  | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 309 | 	mutex_init(&mcp->lock); | 
 | 310 |  | 
 | 311 | 	mcp->spi = spi; | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 312 | 	mcp->addr = 0x40 | (addr << 1); | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 313 |  | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 314 | 	mcp->chip.direction_input = mcp23s08_direction_input; | 
 | 315 | 	mcp->chip.get = mcp23s08_get; | 
 | 316 | 	mcp->chip.direction_output = mcp23s08_direction_output; | 
 | 317 | 	mcp->chip.set = mcp23s08_set; | 
 | 318 | 	mcp->chip.dbg_show = mcp23s08_dbg_show; | 
 | 319 |  | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 320 | 	if (type == MCP_TYPE_S17) { | 
 | 321 | 		mcp->ops = &mcp23s17_ops; | 
 | 322 | 		mcp->chip.ngpio = 16; | 
 | 323 | 		mcp->chip.label = "mcp23s17"; | 
 | 324 | 	} else { | 
 | 325 | 		mcp->ops = &mcp23s08_ops; | 
 | 326 | 		mcp->chip.ngpio = 8; | 
 | 327 | 		mcp->chip.label = "mcp23s08"; | 
 | 328 | 	} | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 329 | 	mcp->chip.base = base; | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 330 | 	mcp->chip.can_sleep = 1; | 
| David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 331 | 	mcp->chip.dev = &spi->dev; | 
| Guennadi Liakhovetski | d72cbed | 2008-04-28 02:14:45 -0700 | [diff] [blame] | 332 | 	mcp->chip.owner = THIS_MODULE; | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 333 |  | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 334 | 	/* verify MCP_IOCON.SEQOP = 0, so sequential reads work, | 
 | 335 | 	 * and MCP_IOCON.HAEN = 1, so we work with all chips. | 
 | 336 | 	 */ | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 337 | 	status = mcp->ops->read(mcp, MCP_IOCON); | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 338 | 	if (status < 0) | 
 | 339 | 		goto fail; | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 340 | 	if ((status & IOCON_SEQOP) || !(status & IOCON_HAEN)) { | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 341 | 		/* mcp23s17 has IOCON twice, make sure they are in sync */ | 
 | 342 | 		status &= ~(IOCON_SEQOP | (IOCON_SEQOP << 8)); | 
 | 343 | 		status |= IOCON_HAEN | (IOCON_HAEN << 8); | 
 | 344 | 		status = mcp->ops->write(mcp, MCP_IOCON, status); | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 345 | 		if (status < 0) | 
 | 346 | 			goto fail; | 
 | 347 | 	} | 
 | 348 |  | 
 | 349 | 	/* configure ~100K pullups */ | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 350 | 	status = mcp->ops->write(mcp, MCP_GPPU, pullups); | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 351 | 	if (status < 0) | 
 | 352 | 		goto fail; | 
 | 353 |  | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 354 | 	status = mcp->ops->read_regs(mcp, 0, mcp->cache, ARRAY_SIZE(mcp->cache)); | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 355 | 	if (status < 0) | 
 | 356 | 		goto fail; | 
 | 357 |  | 
 | 358 | 	/* disable inverter on input */ | 
 | 359 | 	if (mcp->cache[MCP_IPOL] != 0) { | 
 | 360 | 		mcp->cache[MCP_IPOL] = 0; | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 361 | 		status = mcp->ops->write(mcp, MCP_IPOL, 0); | 
 | 362 | 		if (status < 0) | 
 | 363 | 			goto fail; | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 364 | 	} | 
 | 365 |  | 
 | 366 | 	/* disable irqs */ | 
 | 367 | 	if (mcp->cache[MCP_GPINTEN] != 0) { | 
 | 368 | 		mcp->cache[MCP_GPINTEN] = 0; | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 369 | 		status = mcp->ops->write(mcp, MCP_GPINTEN, 0); | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 370 | 		if (status < 0) | 
 | 371 | 			goto fail; | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 372 | 	} | 
 | 373 |  | 
 | 374 | 	status = gpiochip_add(&mcp->chip); | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 375 | fail: | 
 | 376 | 	if (status < 0) | 
 | 377 | 		dev_dbg(&spi->dev, "can't setup chip %d, --> %d\n", | 
 | 378 | 				addr, status); | 
 | 379 | 	return status; | 
 | 380 | } | 
 | 381 |  | 
 | 382 | static int mcp23s08_probe(struct spi_device *spi) | 
 | 383 | { | 
 | 384 | 	struct mcp23s08_platform_data	*pdata; | 
 | 385 | 	unsigned			addr; | 
 | 386 | 	unsigned			chips = 0; | 
 | 387 | 	struct mcp23s08_driver_data	*data; | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 388 | 	int				status, type; | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 389 | 	unsigned			base; | 
 | 390 |  | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 391 | 	type = spi_get_device_id(spi)->driver_data; | 
 | 392 |  | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 393 | 	pdata = spi->dev.platform_data; | 
| Ben Dooks | a342d21 | 2009-01-15 13:50:45 -0800 | [diff] [blame] | 394 | 	if (!pdata || !gpio_is_valid(pdata->base)) { | 
 | 395 | 		dev_dbg(&spi->dev, "invalid or missing platform data\n"); | 
 | 396 | 		return -EINVAL; | 
 | 397 | 	} | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 398 |  | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 399 | 	for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) { | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 400 | 		if (!pdata->chip[addr].is_present) | 
 | 401 | 			continue; | 
 | 402 | 		chips++; | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 403 | 		if ((type == MCP_TYPE_S08) && (addr > 3)) { | 
 | 404 | 			dev_err(&spi->dev, | 
 | 405 | 				"mcp23s08 only supports address 0..3\n"); | 
 | 406 | 			return -EINVAL; | 
 | 407 | 		} | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 408 | 	} | 
 | 409 | 	if (!chips) | 
 | 410 | 		return -ENODEV; | 
 | 411 |  | 
 | 412 | 	data = kzalloc(sizeof *data + chips * sizeof(struct mcp23s08), | 
 | 413 | 			GFP_KERNEL); | 
 | 414 | 	if (!data) | 
 | 415 | 		return -ENOMEM; | 
 | 416 | 	spi_set_drvdata(spi, data); | 
 | 417 |  | 
 | 418 | 	base = pdata->base; | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 419 | 	for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) { | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 420 | 		if (!pdata->chip[addr].is_present) | 
 | 421 | 			continue; | 
 | 422 | 		chips--; | 
 | 423 | 		data->mcp[addr] = &data->chip[chips]; | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 424 | 		status = mcp23s08_probe_one(spi, addr, type, base, | 
 | 425 | 					    pdata->chip[addr].pullups); | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 426 | 		if (status < 0) | 
 | 427 | 			goto fail; | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 428 |  | 
 | 429 | 		base += (type == MCP_TYPE_S17) ? 16 : 8; | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 430 | 	} | 
 | 431 | 	data->ngpio = base - pdata->base; | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 432 |  | 
 | 433 | 	/* NOTE:  these chips have a relatively sane IRQ framework, with | 
 | 434 | 	 * per-signal masking and level/edge triggering.  It's not yet | 
 | 435 | 	 * handled here... | 
 | 436 | 	 */ | 
 | 437 |  | 
 | 438 | 	if (pdata->setup) { | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 439 | 		status = pdata->setup(spi, | 
 | 440 | 				pdata->base, data->ngpio, | 
 | 441 | 				pdata->context); | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 442 | 		if (status < 0) | 
 | 443 | 			dev_dbg(&spi->dev, "setup --> %d\n", status); | 
 | 444 | 	} | 
 | 445 |  | 
 | 446 | 	return 0; | 
 | 447 |  | 
 | 448 | fail: | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 449 | 	for (addr = 0; addr < ARRAY_SIZE(data->mcp); addr++) { | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 450 | 		int tmp; | 
 | 451 |  | 
 | 452 | 		if (!data->mcp[addr]) | 
 | 453 | 			continue; | 
 | 454 | 		tmp = gpiochip_remove(&data->mcp[addr]->chip); | 
 | 455 | 		if (tmp < 0) | 
 | 456 | 			dev_err(&spi->dev, "%s --> %d\n", "remove", tmp); | 
 | 457 | 	} | 
 | 458 | 	kfree(data); | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 459 | 	return status; | 
 | 460 | } | 
 | 461 |  | 
 | 462 | static int mcp23s08_remove(struct spi_device *spi) | 
 | 463 | { | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 464 | 	struct mcp23s08_driver_data	*data = spi_get_drvdata(spi); | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 465 | 	struct mcp23s08_platform_data	*pdata = spi->dev.platform_data; | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 466 | 	unsigned			addr; | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 467 | 	int				status = 0; | 
 | 468 |  | 
 | 469 | 	if (pdata->teardown) { | 
 | 470 | 		status = pdata->teardown(spi, | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 471 | 				pdata->base, data->ngpio, | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 472 | 				pdata->context); | 
 | 473 | 		if (status < 0) { | 
 | 474 | 			dev_err(&spi->dev, "%s --> %d\n", "teardown", status); | 
 | 475 | 			return status; | 
 | 476 | 		} | 
 | 477 | 	} | 
 | 478 |  | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 479 | 	for (addr = 0; addr < ARRAY_SIZE(data->mcp); addr++) { | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 480 | 		int tmp; | 
 | 481 |  | 
 | 482 | 		if (!data->mcp[addr]) | 
 | 483 | 			continue; | 
 | 484 |  | 
 | 485 | 		tmp = gpiochip_remove(&data->mcp[addr]->chip); | 
 | 486 | 		if (tmp < 0) { | 
 | 487 | 			dev_err(&spi->dev, "%s --> %d\n", "remove", tmp); | 
 | 488 | 			status = tmp; | 
 | 489 | 		} | 
 | 490 | 	} | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 491 | 	if (status == 0) | 
| David Brownell | 8f1cc3b | 2008-07-25 01:46:09 -0700 | [diff] [blame] | 492 | 		kfree(data); | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 493 | 	return status; | 
 | 494 | } | 
 | 495 |  | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 496 | static const struct spi_device_id mcp23s08_ids[] = { | 
 | 497 | 	{ "mcp23s08", MCP_TYPE_S08 }, | 
 | 498 | 	{ "mcp23s17", MCP_TYPE_S17 }, | 
 | 499 | 	{ }, | 
 | 500 | }; | 
 | 501 | MODULE_DEVICE_TABLE(spi, mcp23s08_ids); | 
 | 502 |  | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 503 | static struct spi_driver mcp23s08_driver = { | 
 | 504 | 	.probe		= mcp23s08_probe, | 
 | 505 | 	.remove		= mcp23s08_remove, | 
| Peter Korsgaard | 0b7bb77 | 2011-03-09 17:56:30 +0100 | [diff] [blame] | 506 | 	.id_table	= mcp23s08_ids, | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 507 | 	.driver = { | 
 | 508 | 		.name	= "mcp23s08", | 
 | 509 | 		.owner	= THIS_MODULE, | 
 | 510 | 	}, | 
 | 511 | }; | 
 | 512 |  | 
 | 513 | /*----------------------------------------------------------------------*/ | 
 | 514 |  | 
 | 515 | static int __init mcp23s08_init(void) | 
 | 516 | { | 
 | 517 | 	return spi_register_driver(&mcp23s08_driver); | 
 | 518 | } | 
| David Brownell | 673c0c0 | 2008-10-15 22:02:46 -0700 | [diff] [blame] | 519 | /* register after spi postcore initcall and before | 
 | 520 |  * subsys initcalls that may rely on these GPIOs | 
 | 521 |  */ | 
 | 522 | subsys_initcall(mcp23s08_init); | 
| David Brownell | e58b9e2 | 2008-02-04 22:28:25 -0800 | [diff] [blame] | 523 |  | 
 | 524 | static void __exit mcp23s08_exit(void) | 
 | 525 | { | 
 | 526 | 	spi_unregister_driver(&mcp23s08_driver); | 
 | 527 | } | 
 | 528 | module_exit(mcp23s08_exit); | 
 | 529 |  | 
 | 530 | MODULE_LICENSE("GPL"); |