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