10
10
#include <linux/spi/spi.h>
11
11
#include <linux/spi/mcp23s08.h>
12
12
#include <linux/slab.h>
13
#include <asm/byteorder.h>
16
* MCP types supported by driver
18
#define MCP_TYPE_S08 0
19
#define MCP_TYPE_S17 1
15
21
/* Registers are all 8 bits wide.
35
41
#define MCP_GPIO 0x09
36
42
#define MCP_OLAT 0x0a
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);
39
54
struct spi_device *spi;
43
58
/* lock protects the cached values */
46
61
struct gpio_chip chip;
48
63
struct work_struct work;
65
const struct mcp23s08_ops *ops;
51
/* A given spi_device can represent up to four mcp23s08 chips
68
/* A given spi_device can represent up to eight mcp23sxx chips
52
69
* sharing the same chipselect but using different addresses
53
70
* (e.g. chips #0 and #3 might be populated, but not #1 or $2).
54
71
* Driver data holds all the per-chip data.
56
73
struct mcp23s08_driver_data {
58
struct mcp23s08 *mcp[4];
75
struct mcp23s08 *mcp[8];
59
76
struct mcp23s08 chip[];
70
87
return (status < 0) ? status : rx[0];
73
static int mcp23s08_write(struct mcp23s08 *mcp, unsigned reg, u8 val)
90
static int mcp23s08_write(struct mcp23s08 *mcp, unsigned reg, unsigned val)
84
mcp23s08_read_regs(struct mcp23s08 *mcp, unsigned reg, u8 *vals, unsigned n)
101
mcp23s08_read_regs(struct mcp23s08 *mcp, unsigned reg, u16 *vals, unsigned n)
106
if ((n + reg) > sizeof mcp->cache)
108
tx[0] = mcp->addr | 0x01;
112
status = spi_write_then_read(mcp->spi, tx, sizeof tx, tmp, n);
115
vals[n] = tmp[n]; /* expand to 16bit */
120
static int mcp23s17_read(struct mcp23s08 *mcp, unsigned reg)
125
tx[0] = mcp->addr | 0x01;
127
status = spi_write_then_read(mcp->spi, tx, sizeof tx, rx, sizeof rx);
128
return (status < 0) ? status : (rx[0] | (rx[1] << 8));
131
static int mcp23s17_write(struct mcp23s08 *mcp, unsigned reg, unsigned val)
139
return spi_write_then_read(mcp->spi, tx, sizeof tx, NULL, 0);
143
mcp23s17_read_regs(struct mcp23s08 *mcp, unsigned reg, u16 *vals, unsigned n)
88
148
if ((n + reg) > sizeof mcp->cache)
90
150
tx[0] = mcp->addr | 0x01;
92
return spi_write_then_read(mcp->spi, tx, sizeof tx, vals, n);
153
status = spi_write_then_read(mcp->spi, tx, sizeof tx,
157
vals[n] = __le16_to_cpu((__le16)vals[n]);
163
static const struct mcp23s08_ops mcp23s08_ops = {
164
.read = mcp23s08_read,
165
.write = mcp23s08_write,
166
.read_regs = mcp23s08_read_regs,
169
static const struct mcp23s08_ops mcp23s17_ops = {
170
.read = mcp23s17_read,
171
.write = mcp23s17_write,
172
.read_regs = mcp23s17_read_regs,
95
176
/*----------------------------------------------------------------------*/
97
178
static int mcp23s08_direction_input(struct gpio_chip *chip, unsigned offset)
102
183
mutex_lock(&mcp->lock);
103
184
mcp->cache[MCP_IODIR] |= (1 << offset);
104
status = mcp23s08_write(mcp, MCP_IODIR, mcp->cache[MCP_IODIR]);
185
status = mcp->ops->write(mcp, MCP_IODIR, mcp->cache[MCP_IODIR]);
105
186
mutex_unlock(&mcp->lock);
128
209
static int __mcp23s08_set(struct mcp23s08 *mcp, unsigned mask, int value)
130
u8 olat = mcp->cache[MCP_OLAT];
211
unsigned olat = mcp->cache[MCP_OLAT];
136
217
mcp->cache[MCP_OLAT] = olat;
137
return mcp23s08_write(mcp, MCP_OLAT, olat);
218
return mcp->ops->write(mcp, MCP_OLAT, olat);
140
221
static void mcp23s08_set(struct gpio_chip *chip, unsigned offset, int value)
142
223
struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
143
u8 mask = 1 << offset;
224
unsigned mask = 1 << offset;
145
226
mutex_lock(&mcp->lock);
146
227
__mcp23s08_set(mcp, mask, value);
151
232
mcp23s08_direction_output(struct gpio_chip *chip, unsigned offset, int value)
153
234
struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
154
u8 mask = 1 << offset;
235
unsigned mask = 1 << offset;
157
238
mutex_lock(&mcp->lock);
158
239
status = __mcp23s08_set(mcp, mask, value);
159
240
if (status == 0) {
160
241
mcp->cache[MCP_IODIR] &= ~mask;
161
status = mcp23s08_write(mcp, MCP_IODIR, mcp->cache[MCP_IODIR]);
242
status = mcp->ops->write(mcp, MCP_IODIR, mcp->cache[MCP_IODIR]);
163
244
mutex_unlock(&mcp->lock);
184
265
mcp = container_of(chip, struct mcp23s08, chip);
186
267
/* NOTE: we only handle one bank for now ... */
187
bank = '0' + ((mcp->addr >> 1) & 0x3);
268
bank = '0' + ((mcp->addr >> 1) & 0x7);
189
270
mutex_lock(&mcp->lock);
190
t = mcp23s08_read_regs(mcp, 0, mcp->cache, sizeof mcp->cache);
271
t = mcp->ops->read_regs(mcp, 0, mcp->cache, ARRAY_SIZE(mcp->cache));
192
273
seq_printf(s, " I/O ERROR %d\n", t);
196
for (t = 0, mask = 1; t < 8; t++, mask <<= 1) {
277
for (t = 0, mask = 1; t < chip->ngpio; t++, mask <<= 1) {
197
278
const char *label;
199
280
label = gpiochip_is_requested(chip, t);
219
300
/*----------------------------------------------------------------------*/
221
302
static int mcp23s08_probe_one(struct spi_device *spi, unsigned addr,
222
unsigned base, unsigned pullups)
303
unsigned type, unsigned base, unsigned pullups)
224
305
struct mcp23s08_driver_data *data = spi_get_drvdata(spi);
225
306
struct mcp23s08 *mcp = data->mcp[addr];
229
309
mutex_init(&mcp->lock);
232
312
mcp->addr = 0x40 | (addr << 1);
234
mcp->chip.label = "mcp23s08",
236
314
mcp->chip.direction_input = mcp23s08_direction_input;
237
315
mcp->chip.get = mcp23s08_get;
238
316
mcp->chip.direction_output = mcp23s08_direction_output;
239
317
mcp->chip.set = mcp23s08_set;
240
318
mcp->chip.dbg_show = mcp23s08_dbg_show;
320
if (type == MCP_TYPE_S17) {
321
mcp->ops = &mcp23s17_ops;
322
mcp->chip.ngpio = 16;
323
mcp->chip.label = "mcp23s17";
325
mcp->ops = &mcp23s08_ops;
327
mcp->chip.label = "mcp23s08";
242
329
mcp->chip.base = base;
244
330
mcp->chip.can_sleep = 1;
245
331
mcp->chip.dev = &spi->dev;
246
332
mcp->chip.owner = THIS_MODULE;
248
334
/* verify MCP_IOCON.SEQOP = 0, so sequential reads work,
249
335
* and MCP_IOCON.HAEN = 1, so we work with all chips.
251
status = mcp23s08_read(mcp, MCP_IOCON);
337
status = mcp->ops->read(mcp, MCP_IOCON);
254
340
if ((status & IOCON_SEQOP) || !(status & IOCON_HAEN)) {
255
status &= ~IOCON_SEQOP;
256
status |= IOCON_HAEN;
257
status = mcp23s08_write(mcp, MCP_IOCON, (u8) status);
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);
262
349
/* configure ~100K pullups */
263
status = mcp23s08_write(mcp, MCP_GPPU, pullups);
350
status = mcp->ops->write(mcp, MCP_GPPU, pullups);
267
status = mcp23s08_read_regs(mcp, 0, mcp->cache, sizeof mcp->cache);
354
status = mcp->ops->read_regs(mcp, 0, mcp->cache, ARRAY_SIZE(mcp->cache));
271
358
/* disable inverter on input */
272
359
if (mcp->cache[MCP_IPOL] != 0) {
273
360
mcp->cache[MCP_IPOL] = 0;
361
status = mcp->ops->write(mcp, MCP_IPOL, 0);
277
366
/* disable irqs */
278
367
if (mcp->cache[MCP_GPINTEN] != 0) {
279
368
mcp->cache[MCP_GPINTEN] = 0;
288
memcpy(&tx[2], &mcp->cache[MCP_IPOL], sizeof(tx) - 2);
289
status = spi_write_then_read(mcp->spi, tx, sizeof tx, NULL, 0);
369
status = mcp->ops->write(mcp, MCP_GPINTEN, 0);
306
386
unsigned chips = 0;
307
387
struct mcp23s08_driver_data *data;
391
type = spi_get_device_id(spi)->driver_data;
311
393
pdata = spi->dev.platform_data;
312
394
if (!pdata || !gpio_is_valid(pdata->base)) {
313
395
dev_dbg(&spi->dev, "invalid or missing platform data\n");
317
for (addr = 0; addr < 4; addr++) {
399
for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
318
400
if (!pdata->chip[addr].is_present)
403
if ((type == MCP_TYPE_S08) && (addr > 3)) {
405
"mcp23s08 only supports address 0..3\n");
329
416
spi_set_drvdata(spi, data);
331
418
base = pdata->base;
332
for (addr = 0; addr < 4; addr++) {
419
for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
333
420
if (!pdata->chip[addr].is_present)
336
423
data->mcp[addr] = &data->chip[chips];
337
status = mcp23s08_probe_one(spi, addr, base,
338
pdata->chip[addr].pullups);
424
status = mcp23s08_probe_one(spi, addr, type, base,
425
pdata->chip[addr].pullups);
429
base += (type == MCP_TYPE_S17) ? 16 : 8;
343
431
data->ngpio = base - pdata->base;
496
static const struct spi_device_id mcp23s08_ids[] = {
497
{ "mcp23s08", MCP_TYPE_S08 },
498
{ "mcp23s17", MCP_TYPE_S17 },
501
MODULE_DEVICE_TABLE(spi, mcp23s08_ids);
408
503
static struct spi_driver mcp23s08_driver = {
409
504
.probe = mcp23s08_probe,
410
505
.remove = mcp23s08_remove,
506
.id_table = mcp23s08_ids,
412
508
.name = "mcp23s08",
413
509
.owner = THIS_MODULE,