blob: bdb692345428ccc99c8f22bd3b460f25b41e3156 [file] [log] [blame]
David Brownelle58b9e22008-02-04 22:28:25 -08001/*
Lars Poeschel4e47f912014-01-16 11:44:15 +01002 * MCP23S08 SPI/I2C GPIO gpio expander driver
3 *
4 * The inputs and outputs of the mcp23s08, mcp23s17, mcp23008 and mcp23017 are
5 * supported.
6 * For the I2C versions of the chips (mcp23008 and mcp23017) generation of
7 * interrupts is also supported.
8 * The hardware of the SPI versions of the chips (mcp23s08 and mcp23s17) is
9 * also capable of generating interrupts, but the linux driver does not
10 * support that yet.
David Brownelle58b9e22008-02-04 22:28:25 -080011 */
12
13#include <linux/kernel.h>
14#include <linux/device.h>
David Brownelle58b9e22008-02-04 22:28:25 -080015#include <linux/mutex.h>
Paul Gortmakerbb207ef2011-07-03 13:38:09 -040016#include <linux/module.h>
H Hartley Sweetend120c172009-09-22 16:46:37 -070017#include <linux/gpio.h>
Peter Korsgaard752ad5e2011-07-15 10:25:32 +020018#include <linux/i2c.h>
David Brownelle58b9e22008-02-04 22:28:25 -080019#include <linux/spi/spi.h>
20#include <linux/spi/mcp23s08.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Peter Korsgaard0b7bb772011-03-09 17:56:30 +010022#include <asm/byteorder.h>
Lars Poeschel4e47f912014-01-16 11:44:15 +010023#include <linux/interrupt.h>
24#include <linux/of_irq.h>
Lars Poeschel97ddb1c2013-04-04 12:02:02 +020025#include <linux/of_device.h>
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +010026#include <linux/regmap.h>
David Brownelle58b9e22008-02-04 22:28:25 -080027
Peter Korsgaard0b7bb772011-03-09 17:56:30 +010028/**
29 * MCP types supported by driver
30 */
31#define MCP_TYPE_S08 0
32#define MCP_TYPE_S17 1
Peter Korsgaard752ad5e2011-07-15 10:25:32 +020033#define MCP_TYPE_008 2
34#define MCP_TYPE_017 3
Phil Reid28c5a412016-03-01 14:25:41 +080035#define MCP_TYPE_S18 4
David Brownelle58b9e22008-02-04 22:28:25 -080036
37/* Registers are all 8 bits wide.
38 *
39 * The mcp23s17 has twice as many bits, and can be configured to work
40 * with either 16 bit registers or with two adjacent 8 bit banks.
David Brownelle58b9e22008-02-04 22:28:25 -080041 */
42#define MCP_IODIR 0x00 /* init/reset: all ones */
43#define MCP_IPOL 0x01
44#define MCP_GPINTEN 0x02
45#define MCP_DEFVAL 0x03
46#define MCP_INTCON 0x04
47#define MCP_IOCON 0x05
Lars Poeschel4e47f912014-01-16 11:44:15 +010048# define IOCON_MIRROR (1 << 6)
David Brownelle58b9e22008-02-04 22:28:25 -080049# define IOCON_SEQOP (1 << 5)
50# define IOCON_HAEN (1 << 3)
51# define IOCON_ODR (1 << 2)
52# define IOCON_INTPOL (1 << 1)
Phil Reid35396992016-03-15 15:46:30 +080053# define IOCON_INTCC (1)
David Brownelle58b9e22008-02-04 22:28:25 -080054#define MCP_GPPU 0x06
55#define MCP_INTF 0x07
56#define MCP_INTCAP 0x08
57#define MCP_GPIO 0x09
58#define MCP_OLAT 0x0a
59
Peter Korsgaard0b7bb772011-03-09 17:56:30 +010060struct mcp23s08;
61
David Brownelle58b9e22008-02-04 22:28:25 -080062struct mcp23s08 {
David Brownelle58b9e22008-02-04 22:28:25 -080063 u8 addr;
Alexander Steina4e63552014-12-01 08:26:00 +010064 bool irq_active_high;
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +010065 bool reg_shift;
David Brownelle58b9e22008-02-04 22:28:25 -080066
Peter Korsgaard0b7bb772011-03-09 17:56:30 +010067 u16 cache[11];
Lars Poeschel4e47f912014-01-16 11:44:15 +010068 u16 irq_rise;
69 u16 irq_fall;
70 int irq;
71 bool irq_controller;
David Brownelle58b9e22008-02-04 22:28:25 -080072 /* lock protects the cached values */
73 struct mutex lock;
Lars Poeschel4e47f912014-01-16 11:44:15 +010074 struct mutex irq_lock;
David Brownelle58b9e22008-02-04 22:28:25 -080075
76 struct gpio_chip chip;
77
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +010078 struct regmap *regmap;
79 struct device *dev;
David Brownelle58b9e22008-02-04 22:28:25 -080080};
81
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +010082static const struct regmap_config mcp23x08_regmap = {
83 .reg_bits = 8,
84 .val_bits = 8,
85
86 .reg_stride = 1,
87 .max_register = MCP_OLAT,
88};
89
90static const struct regmap_config mcp23x17_regmap = {
91 .reg_bits = 8,
92 .val_bits = 16,
93
94 .reg_stride = 2,
95 .max_register = MCP_OLAT << 1,
96 .val_format_endian = REGMAP_ENDIAN_LITTLE,
97};
98
99/*----------------------------------------------------------------------*/
100
101#ifdef CONFIG_SPI_MASTER
102
103static int mcp23sxx_spi_write(void *context, const void *data, size_t count)
104{
105 struct mcp23s08 *mcp = context;
106 struct spi_device *spi = to_spi_device(mcp->dev);
107 struct spi_message m;
108 struct spi_transfer t[2] = { { .tx_buf = &mcp->addr, .len = 1, },
109 { .tx_buf = data, .len = count, }, };
110
111 spi_message_init(&m);
112 spi_message_add_tail(&t[0], &m);
113 spi_message_add_tail(&t[1], &m);
114
115 return spi_sync(spi, &m);
116}
117
118static int mcp23sxx_spi_gather_write(void *context,
119 const void *reg, size_t reg_size,
120 const void *val, size_t val_size)
121{
122 struct mcp23s08 *mcp = context;
123 struct spi_device *spi = to_spi_device(mcp->dev);
124 struct spi_message m;
125 struct spi_transfer t[3] = { { .tx_buf = &mcp->addr, .len = 1, },
126 { .tx_buf = reg, .len = reg_size, },
127 { .tx_buf = val, .len = val_size, }, };
128
129 spi_message_init(&m);
130 spi_message_add_tail(&t[0], &m);
131 spi_message_add_tail(&t[1], &m);
132 spi_message_add_tail(&t[2], &m);
133
134 return spi_sync(spi, &m);
135}
136
137static int mcp23sxx_spi_read(void *context, const void *reg, size_t reg_size,
138 void *val, size_t val_size)
139{
140 struct mcp23s08 *mcp = context;
141 struct spi_device *spi = to_spi_device(mcp->dev);
142 u8 tx[2];
143
144 if (reg_size != 1)
145 return -EINVAL;
146
147 tx[0] = mcp->addr | 0x01;
148 tx[1] = *((u8 *) reg);
149
150 return spi_write_then_read(spi, tx, sizeof(tx), val, val_size);
151}
152
153static const struct regmap_bus mcp23sxx_spi_regmap = {
154 .write = mcp23sxx_spi_write,
155 .gather_write = mcp23sxx_spi_gather_write,
156 .read = mcp23sxx_spi_read,
157};
158
159#endif /* CONFIG_SPI_MASTER */
160
161static int mcp_read(struct mcp23s08 *mcp, unsigned int reg, unsigned int *val)
162{
163 return regmap_read(mcp->regmap, reg << mcp->reg_shift, val);
164}
165
166static int mcp_write(struct mcp23s08 *mcp, unsigned int reg, unsigned int val)
167{
168 return regmap_write(mcp->regmap, reg << mcp->reg_shift, val);
169}
170
171static int mcp_update_cache(struct mcp23s08 *mcp)
172{
173 int ret, reg, i;
174
175 for (i = 0; i < ARRAY_SIZE(mcp->cache); i++) {
176 ret = mcp_read(mcp, i, &reg);
177 if (ret < 0)
178 return ret;
179 mcp->cache[i] = reg;
180 }
181
182 return 0;
183}
184
185/*----------------------------------------------------------------------*/
186
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100187/* A given spi_device can represent up to eight mcp23sxx chips
David Brownell8f1cc3b2008-07-25 01:46:09 -0700188 * sharing the same chipselect but using different addresses
189 * (e.g. chips #0 and #3 might be populated, but not #1 or $2).
190 * Driver data holds all the per-chip data.
191 */
192struct mcp23s08_driver_data {
193 unsigned ngpio;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100194 struct mcp23s08 *mcp[8];
David Brownell8f1cc3b2008-07-25 01:46:09 -0700195 struct mcp23s08 chip[];
196};
197
David Brownelle58b9e22008-02-04 22:28:25 -0800198
199static int mcp23s08_direction_input(struct gpio_chip *chip, unsigned offset)
200{
Linus Walleij9e03cf02015-12-07 10:09:36 +0100201 struct mcp23s08 *mcp = gpiochip_get_data(chip);
David Brownelle58b9e22008-02-04 22:28:25 -0800202 int status;
203
204 mutex_lock(&mcp->lock);
205 mcp->cache[MCP_IODIR] |= (1 << offset);
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100206 status = mcp_write(mcp, MCP_IODIR, mcp->cache[MCP_IODIR]);
David Brownelle58b9e22008-02-04 22:28:25 -0800207 mutex_unlock(&mcp->lock);
208 return status;
209}
210
211static int mcp23s08_get(struct gpio_chip *chip, unsigned offset)
212{
Linus Walleij9e03cf02015-12-07 10:09:36 +0100213 struct mcp23s08 *mcp = gpiochip_get_data(chip);
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100214 int status, ret;
David Brownelle58b9e22008-02-04 22:28:25 -0800215
216 mutex_lock(&mcp->lock);
217
218 /* REVISIT reading this clears any IRQ ... */
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100219 ret = mcp_read(mcp, MCP_GPIO, &status);
220 if (ret < 0)
David Brownelle58b9e22008-02-04 22:28:25 -0800221 status = 0;
222 else {
223 mcp->cache[MCP_GPIO] = status;
224 status = !!(status & (1 << offset));
225 }
226 mutex_unlock(&mcp->lock);
227 return status;
228}
229
230static int __mcp23s08_set(struct mcp23s08 *mcp, unsigned mask, int value)
231{
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100232 unsigned olat = mcp->cache[MCP_OLAT];
David Brownelle58b9e22008-02-04 22:28:25 -0800233
234 if (value)
235 olat |= mask;
236 else
237 olat &= ~mask;
238 mcp->cache[MCP_OLAT] = olat;
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100239 return mcp_write(mcp, MCP_OLAT, olat);
David Brownelle58b9e22008-02-04 22:28:25 -0800240}
241
242static void mcp23s08_set(struct gpio_chip *chip, unsigned offset, int value)
243{
Linus Walleij9e03cf02015-12-07 10:09:36 +0100244 struct mcp23s08 *mcp = gpiochip_get_data(chip);
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100245 unsigned mask = 1 << offset;
David Brownelle58b9e22008-02-04 22:28:25 -0800246
247 mutex_lock(&mcp->lock);
248 __mcp23s08_set(mcp, mask, value);
249 mutex_unlock(&mcp->lock);
250}
251
252static int
253mcp23s08_direction_output(struct gpio_chip *chip, unsigned offset, int value)
254{
Linus Walleij9e03cf02015-12-07 10:09:36 +0100255 struct mcp23s08 *mcp = gpiochip_get_data(chip);
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100256 unsigned mask = 1 << offset;
David Brownelle58b9e22008-02-04 22:28:25 -0800257 int status;
258
259 mutex_lock(&mcp->lock);
260 status = __mcp23s08_set(mcp, mask, value);
261 if (status == 0) {
262 mcp->cache[MCP_IODIR] &= ~mask;
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100263 status = mcp_write(mcp, MCP_IODIR, mcp->cache[MCP_IODIR]);
David Brownelle58b9e22008-02-04 22:28:25 -0800264 }
265 mutex_unlock(&mcp->lock);
266 return status;
267}
268
269/*----------------------------------------------------------------------*/
Lars Poeschel4e47f912014-01-16 11:44:15 +0100270static irqreturn_t mcp23s08_irq(int irq, void *data)
271{
272 struct mcp23s08 *mcp = data;
273 int intcap, intf, i;
274 unsigned int child_irq;
275
276 mutex_lock(&mcp->lock);
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100277 if (mcp_read(mcp, MCP_INTF, &intf) < 0) {
Lars Poeschel4e47f912014-01-16 11:44:15 +0100278 mutex_unlock(&mcp->lock);
279 return IRQ_HANDLED;
280 }
281
282 mcp->cache[MCP_INTF] = intf;
283
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100284 if (mcp_read(mcp, MCP_INTCAP, &intcap) < 0) {
Lars Poeschel4e47f912014-01-16 11:44:15 +0100285 mutex_unlock(&mcp->lock);
286 return IRQ_HANDLED;
287 }
288
289 mcp->cache[MCP_INTCAP] = intcap;
290 mutex_unlock(&mcp->lock);
291
292
293 for (i = 0; i < mcp->chip.ngpio; i++) {
294 if ((BIT(i) & mcp->cache[MCP_INTF]) &&
295 ((BIT(i) & intcap & mcp->irq_rise) ||
Alexander Stein16fe1ad2016-03-23 18:01:27 +0100296 (mcp->irq_fall & ~intcap & BIT(i)) ||
297 (BIT(i) & mcp->cache[MCP_INTCON]))) {
Phil Reiddad3d272016-03-18 16:07:06 +0800298 child_irq = irq_find_mapping(mcp->chip.irqdomain, i);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100299 handle_nested_irq(child_irq);
300 }
301 }
302
303 return IRQ_HANDLED;
304}
305
Lars Poeschel4e47f912014-01-16 11:44:15 +0100306static void mcp23s08_irq_mask(struct irq_data *data)
307{
Phil Reiddad3d272016-03-18 16:07:06 +0800308 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
309 struct mcp23s08 *mcp = gpiochip_get_data(gc);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100310 unsigned int pos = data->hwirq;
311
312 mcp->cache[MCP_GPINTEN] &= ~BIT(pos);
313}
314
315static void mcp23s08_irq_unmask(struct irq_data *data)
316{
Phil Reiddad3d272016-03-18 16:07:06 +0800317 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
318 struct mcp23s08 *mcp = gpiochip_get_data(gc);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100319 unsigned int pos = data->hwirq;
320
321 mcp->cache[MCP_GPINTEN] |= BIT(pos);
322}
323
324static int mcp23s08_irq_set_type(struct irq_data *data, unsigned int type)
325{
Phil Reiddad3d272016-03-18 16:07:06 +0800326 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
327 struct mcp23s08 *mcp = gpiochip_get_data(gc);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100328 unsigned int pos = data->hwirq;
329 int status = 0;
330
331 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
332 mcp->cache[MCP_INTCON] &= ~BIT(pos);
333 mcp->irq_rise |= BIT(pos);
334 mcp->irq_fall |= BIT(pos);
335 } else if (type & IRQ_TYPE_EDGE_RISING) {
336 mcp->cache[MCP_INTCON] &= ~BIT(pos);
337 mcp->irq_rise |= BIT(pos);
338 mcp->irq_fall &= ~BIT(pos);
339 } else if (type & IRQ_TYPE_EDGE_FALLING) {
340 mcp->cache[MCP_INTCON] &= ~BIT(pos);
341 mcp->irq_rise &= ~BIT(pos);
342 mcp->irq_fall |= BIT(pos);
Alexander Stein16fe1ad2016-03-23 18:01:27 +0100343 } else if (type & IRQ_TYPE_LEVEL_HIGH) {
344 mcp->cache[MCP_INTCON] |= BIT(pos);
345 mcp->cache[MCP_DEFVAL] &= ~BIT(pos);
346 } else if (type & IRQ_TYPE_LEVEL_LOW) {
347 mcp->cache[MCP_INTCON] |= BIT(pos);
348 mcp->cache[MCP_DEFVAL] |= BIT(pos);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100349 } else
350 return -EINVAL;
351
352 return status;
353}
354
355static void mcp23s08_irq_bus_lock(struct irq_data *data)
356{
Phil Reiddad3d272016-03-18 16:07:06 +0800357 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
358 struct mcp23s08 *mcp = gpiochip_get_data(gc);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100359
360 mutex_lock(&mcp->irq_lock);
361}
362
363static void mcp23s08_irq_bus_unlock(struct irq_data *data)
364{
Phil Reiddad3d272016-03-18 16:07:06 +0800365 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
366 struct mcp23s08 *mcp = gpiochip_get_data(gc);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100367
368 mutex_lock(&mcp->lock);
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100369 mcp_write(mcp, MCP_GPINTEN, mcp->cache[MCP_GPINTEN]);
370 mcp_write(mcp, MCP_DEFVAL, mcp->cache[MCP_DEFVAL]);
371 mcp_write(mcp, MCP_INTCON, mcp->cache[MCP_INTCON]);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100372 mutex_unlock(&mcp->lock);
373 mutex_unlock(&mcp->irq_lock);
374}
375
Lars Poeschel4e47f912014-01-16 11:44:15 +0100376static struct irq_chip mcp23s08_irq_chip = {
377 .name = "gpio-mcp23xxx",
378 .irq_mask = mcp23s08_irq_mask,
379 .irq_unmask = mcp23s08_irq_unmask,
380 .irq_set_type = mcp23s08_irq_set_type,
381 .irq_bus_lock = mcp23s08_irq_bus_lock,
382 .irq_bus_sync_unlock = mcp23s08_irq_bus_unlock,
Lars Poeschel4e47f912014-01-16 11:44:15 +0100383};
384
385static int mcp23s08_irq_setup(struct mcp23s08 *mcp)
386{
387 struct gpio_chip *chip = &mcp->chip;
Phil Reiddad3d272016-03-18 16:07:06 +0800388 int err;
Alexander Steina4e63552014-12-01 08:26:00 +0100389 unsigned long irqflags = IRQF_ONESHOT | IRQF_SHARED;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100390
391 mutex_init(&mcp->irq_lock);
392
Alexander Steina4e63552014-12-01 08:26:00 +0100393 if (mcp->irq_active_high)
394 irqflags |= IRQF_TRIGGER_HIGH;
395 else
396 irqflags |= IRQF_TRIGGER_LOW;
397
Linus Walleij58383c782015-11-04 09:56:26 +0100398 err = devm_request_threaded_irq(chip->parent, mcp->irq, NULL,
399 mcp23s08_irq,
400 irqflags, dev_name(chip->parent), mcp);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100401 if (err != 0) {
Linus Walleij58383c782015-11-04 09:56:26 +0100402 dev_err(chip->parent, "unable to request IRQ#%d: %d\n",
Lars Poeschel4e47f912014-01-16 11:44:15 +0100403 mcp->irq, err);
404 return err;
405 }
406
Linus Walleijd245b3f2016-11-24 10:57:25 +0100407 err = gpiochip_irqchip_add_nested(chip,
408 &mcp23s08_irq_chip,
409 0,
410 handle_simple_irq,
411 IRQ_TYPE_NONE);
Phil Reiddad3d272016-03-18 16:07:06 +0800412 if (err) {
413 dev_err(chip->parent,
414 "could not connect irqchip to gpiochip: %d\n", err);
415 return err;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100416 }
Phil Reiddad3d272016-03-18 16:07:06 +0800417
Linus Walleijd245b3f2016-11-24 10:57:25 +0100418 gpiochip_set_nested_irqchip(chip,
419 &mcp23s08_irq_chip,
420 mcp->irq);
Phil Reiddad3d272016-03-18 16:07:06 +0800421
Lars Poeschel4e47f912014-01-16 11:44:15 +0100422 return 0;
423}
424
Lars Poeschel4e47f912014-01-16 11:44:15 +0100425/*----------------------------------------------------------------------*/
David Brownelle58b9e22008-02-04 22:28:25 -0800426
427#ifdef CONFIG_DEBUG_FS
428
429#include <linux/seq_file.h>
430
431/*
432 * This shows more info than the generic gpio dump code:
433 * pullups, deglitching, open drain drive.
434 */
435static void mcp23s08_dbg_show(struct seq_file *s, struct gpio_chip *chip)
436{
437 struct mcp23s08 *mcp;
438 char bank;
Roel Kluin1d1c1d92008-05-23 13:04:43 -0700439 int t;
David Brownelle58b9e22008-02-04 22:28:25 -0800440 unsigned mask;
441
Linus Walleij9e03cf02015-12-07 10:09:36 +0100442 mcp = gpiochip_get_data(chip);
David Brownelle58b9e22008-02-04 22:28:25 -0800443
444 /* NOTE: we only handle one bank for now ... */
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100445 bank = '0' + ((mcp->addr >> 1) & 0x7);
David Brownelle58b9e22008-02-04 22:28:25 -0800446
447 mutex_lock(&mcp->lock);
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100448 t = mcp_update_cache(mcp);
David Brownelle58b9e22008-02-04 22:28:25 -0800449 if (t < 0) {
450 seq_printf(s, " I/O ERROR %d\n", t);
451 goto done;
452 }
453
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100454 for (t = 0, mask = 1; t < chip->ngpio; t++, mask <<= 1) {
David Brownelle58b9e22008-02-04 22:28:25 -0800455 const char *label;
456
457 label = gpiochip_is_requested(chip, t);
458 if (!label)
459 continue;
460
461 seq_printf(s, " gpio-%-3d P%c.%d (%-12s) %s %s %s",
462 chip->base + t, bank, t, label,
463 (mcp->cache[MCP_IODIR] & mask) ? "in " : "out",
464 (mcp->cache[MCP_GPIO] & mask) ? "hi" : "lo",
Peter Korsgaardeb1567f2012-04-25 11:51:53 +0200465 (mcp->cache[MCP_GPPU] & mask) ? "up" : " ");
David Brownelle58b9e22008-02-04 22:28:25 -0800466 /* NOTE: ignoring the irq-related registers */
Gary Servin33bc84112014-03-06 20:25:26 -0300467 seq_puts(s, "\n");
David Brownelle58b9e22008-02-04 22:28:25 -0800468 }
469done:
470 mutex_unlock(&mcp->lock);
471}
472
473#else
474#define mcp23s08_dbg_show NULL
475#endif
476
477/*----------------------------------------------------------------------*/
478
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200479static int mcp23s08_probe_one(struct mcp23s08 *mcp, struct device *dev,
Lars Poeschel4e47f912014-01-16 11:44:15 +0100480 void *data, unsigned addr, unsigned type,
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800481 struct mcp23s08_platform_data *pdata, int cs)
David Brownelle58b9e22008-02-04 22:28:25 -0800482{
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100483 int status, ret;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100484 bool mirror = false;
David Brownelle58b9e22008-02-04 22:28:25 -0800485
David Brownelle58b9e22008-02-04 22:28:25 -0800486 mutex_init(&mcp->lock);
487
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100488 mcp->dev = dev;
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200489 mcp->addr = addr;
Alexander Steina4e63552014-12-01 08:26:00 +0100490 mcp->irq_active_high = false;
David Brownelle58b9e22008-02-04 22:28:25 -0800491
David Brownelle58b9e22008-02-04 22:28:25 -0800492 mcp->chip.direction_input = mcp23s08_direction_input;
493 mcp->chip.get = mcp23s08_get;
494 mcp->chip.direction_output = mcp23s08_direction_output;
495 mcp->chip.set = mcp23s08_set;
496 mcp->chip.dbg_show = mcp23s08_dbg_show;
Linus Walleij60f749f2016-09-07 23:13:20 +0200497#ifdef CONFIG_OF_GPIO
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200498 mcp->chip.of_gpio_n_cells = 2;
499 mcp->chip.of_node = dev->of_node;
500#endif
David Brownelle58b9e22008-02-04 22:28:25 -0800501
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200502 switch (type) {
503#ifdef CONFIG_SPI_MASTER
504 case MCP_TYPE_S08:
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100505 mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp,
506 &mcp23x08_regmap);
507 mcp->reg_shift = 0;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100508 mcp->chip.ngpio = 8;
509 mcp->chip.label = "mcp23s08";
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200510 break;
511
512 case MCP_TYPE_S17:
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100513 mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp,
514 &mcp23x17_regmap);
515 mcp->reg_shift = 1;
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200516 mcp->chip.ngpio = 16;
517 mcp->chip.label = "mcp23s17";
518 break;
Phil Reid28c5a412016-03-01 14:25:41 +0800519
520 case MCP_TYPE_S18:
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100521 mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp,
522 &mcp23x17_regmap);
523 mcp->reg_shift = 1;
Phil Reid28c5a412016-03-01 14:25:41 +0800524 mcp->chip.ngpio = 16;
525 mcp->chip.label = "mcp23s18";
526 break;
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200527#endif /* CONFIG_SPI_MASTER */
528
Daniel M. Weekscbf24fa2012-11-06 23:51:05 -0500529#if IS_ENABLED(CONFIG_I2C)
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200530 case MCP_TYPE_008:
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100531 mcp->regmap = devm_regmap_init_i2c(data, &mcp23x08_regmap);
532 mcp->reg_shift = 0;
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200533 mcp->chip.ngpio = 8;
534 mcp->chip.label = "mcp23008";
535 break;
536
537 case MCP_TYPE_017:
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100538 mcp->regmap = devm_regmap_init_i2c(data, &mcp23x17_regmap);
539 mcp->reg_shift = 1;
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200540 mcp->chip.ngpio = 16;
541 mcp->chip.label = "mcp23017";
542 break;
543#endif /* CONFIG_I2C */
544
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200545 default:
546 dev_err(dev, "invalid device type (%d)\n", type);
547 return -EINVAL;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100548 }
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200549
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100550 if (IS_ERR(mcp->regmap))
551 return PTR_ERR(mcp->regmap);
552
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800553 mcp->chip.base = pdata->base;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100554 mcp->chip.can_sleep = true;
Linus Walleij58383c782015-11-04 09:56:26 +0100555 mcp->chip.parent = dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700556 mcp->chip.owner = THIS_MODULE;
David Brownelle58b9e22008-02-04 22:28:25 -0800557
David Brownell8f1cc3b2008-07-25 01:46:09 -0700558 /* verify MCP_IOCON.SEQOP = 0, so sequential reads work,
559 * and MCP_IOCON.HAEN = 1, so we work with all chips.
560 */
Lars Poeschel4e47f912014-01-16 11:44:15 +0100561
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100562 ret = mcp_read(mcp, MCP_IOCON, &status);
563 if (ret < 0)
David Brownelle58b9e22008-02-04 22:28:25 -0800564 goto fail;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100565
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800566 mcp->irq_controller = pdata->irq_controller;
Alexander Steina4e63552014-12-01 08:26:00 +0100567 if (mcp->irq && mcp->irq_controller) {
Linus Walleij170680a2014-12-12 11:22:11 +0100568 mcp->irq_active_high =
Linus Walleij58383c782015-11-04 09:56:26 +0100569 of_property_read_bool(mcp->chip.parent->of_node,
Linus Walleij170680a2014-12-12 11:22:11 +0100570 "microchip,irq-active-high");
Lars Poeschel4e47f912014-01-16 11:44:15 +0100571
Phil Reid28c5a412016-03-01 14:25:41 +0800572 mirror = pdata->mirror;
Alexander Steina4e63552014-12-01 08:26:00 +0100573 }
574
575 if ((status & IOCON_SEQOP) || !(status & IOCON_HAEN) || mirror ||
576 mcp->irq_active_high) {
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100577 /* mcp23s17 has IOCON twice, make sure they are in sync */
578 status &= ~(IOCON_SEQOP | (IOCON_SEQOP << 8));
579 status |= IOCON_HAEN | (IOCON_HAEN << 8);
Alexander Steina4e63552014-12-01 08:26:00 +0100580 if (mcp->irq_active_high)
581 status |= IOCON_INTPOL | (IOCON_INTPOL << 8);
582 else
583 status &= ~(IOCON_INTPOL | (IOCON_INTPOL << 8));
584
Lars Poeschel4e47f912014-01-16 11:44:15 +0100585 if (mirror)
586 status |= IOCON_MIRROR | (IOCON_MIRROR << 8);
587
Phil Reid35396992016-03-15 15:46:30 +0800588 if (type == MCP_TYPE_S18)
589 status |= IOCON_INTCC | (IOCON_INTCC << 8);
590
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100591 ret = mcp_write(mcp, MCP_IOCON, status);
592 if (ret < 0)
David Brownelle58b9e22008-02-04 22:28:25 -0800593 goto fail;
594 }
595
596 /* configure ~100K pullups */
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100597 ret = mcp_write(mcp, MCP_GPPU, pdata->chip[cs].pullups);
598 if (ret < 0)
David Brownelle58b9e22008-02-04 22:28:25 -0800599 goto fail;
600
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100601 ret = mcp_update_cache(mcp);
602 if (ret < 0)
David Brownelle58b9e22008-02-04 22:28:25 -0800603 goto fail;
604
605 /* disable inverter on input */
606 if (mcp->cache[MCP_IPOL] != 0) {
607 mcp->cache[MCP_IPOL] = 0;
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100608 ret = mcp_write(mcp, MCP_IPOL, 0);
609 if (ret < 0)
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100610 goto fail;
David Brownelle58b9e22008-02-04 22:28:25 -0800611 }
612
613 /* disable irqs */
614 if (mcp->cache[MCP_GPINTEN] != 0) {
615 mcp->cache[MCP_GPINTEN] = 0;
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100616 ret = mcp_write(mcp, MCP_GPINTEN, 0);
617 if (ret < 0)
David Brownell8f1cc3b2008-07-25 01:46:09 -0700618 goto fail;
David Brownelle58b9e22008-02-04 22:28:25 -0800619 }
620
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100621 ret = gpiochip_add_data(&mcp->chip, mcp);
622 if (ret < 0)
Lars Poeschel4e47f912014-01-16 11:44:15 +0100623 goto fail;
624
625 if (mcp->irq && mcp->irq_controller) {
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100626 ret = mcp23s08_irq_setup(mcp);
627 if (ret)
Lars Poeschel4e47f912014-01-16 11:44:15 +0100628 goto fail;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100629 }
David Brownell8f1cc3b2008-07-25 01:46:09 -0700630fail:
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100631 if (ret < 0)
632 dev_dbg(dev, "can't setup chip %d, --> %d\n", addr, ret);
633 return ret;
David Brownell8f1cc3b2008-07-25 01:46:09 -0700634}
635
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200636/*----------------------------------------------------------------------*/
637
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200638#ifdef CONFIG_OF
639#ifdef CONFIG_SPI_MASTER
Jingoo Hanac791802014-05-07 18:05:17 +0900640static const struct of_device_id mcp23s08_spi_of_match[] = {
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200641 {
Lars Poeschel45971682013-08-28 10:38:50 +0200642 .compatible = "microchip,mcp23s08",
643 .data = (void *) MCP_TYPE_S08,
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200644 },
645 {
Lars Poeschel45971682013-08-28 10:38:50 +0200646 .compatible = "microchip,mcp23s17",
647 .data = (void *) MCP_TYPE_S17,
648 },
Phil Reid28c5a412016-03-01 14:25:41 +0800649 {
650 .compatible = "microchip,mcp23s18",
651 .data = (void *) MCP_TYPE_S18,
652 },
Lars Poeschel45971682013-08-28 10:38:50 +0200653/* NOTE: The use of the mcp prefix is deprecated and will be removed. */
654 {
655 .compatible = "mcp,mcp23s08",
656 .data = (void *) MCP_TYPE_S08,
657 },
658 {
659 .compatible = "mcp,mcp23s17",
660 .data = (void *) MCP_TYPE_S17,
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200661 },
662 { },
663};
664MODULE_DEVICE_TABLE(of, mcp23s08_spi_of_match);
665#endif
666
667#if IS_ENABLED(CONFIG_I2C)
Jingoo Hanac791802014-05-07 18:05:17 +0900668static const struct of_device_id mcp23s08_i2c_of_match[] = {
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200669 {
Lars Poeschel45971682013-08-28 10:38:50 +0200670 .compatible = "microchip,mcp23008",
671 .data = (void *) MCP_TYPE_008,
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200672 },
673 {
Lars Poeschel45971682013-08-28 10:38:50 +0200674 .compatible = "microchip,mcp23017",
675 .data = (void *) MCP_TYPE_017,
676 },
677/* NOTE: The use of the mcp prefix is deprecated and will be removed. */
678 {
679 .compatible = "mcp,mcp23008",
680 .data = (void *) MCP_TYPE_008,
681 },
682 {
683 .compatible = "mcp,mcp23017",
684 .data = (void *) MCP_TYPE_017,
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200685 },
686 { },
687};
688MODULE_DEVICE_TABLE(of, mcp23s08_i2c_of_match);
689#endif
690#endif /* CONFIG_OF */
691
692
Daniel M. Weekscbf24fa2012-11-06 23:51:05 -0500693#if IS_ENABLED(CONFIG_I2C)
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200694
Bill Pemberton38363092012-11-19 13:22:34 -0500695static int mcp230xx_probe(struct i2c_client *client,
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200696 const struct i2c_device_id *id)
697{
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800698 struct mcp23s08_platform_data *pdata, local_pdata;
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200699 struct mcp23s08 *mcp;
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800700 int status;
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200701 const struct of_device_id *match;
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200702
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200703 match = of_match_device(of_match_ptr(mcp23s08_i2c_of_match),
704 &client->dev);
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800705 if (match) {
706 pdata = &local_pdata;
707 pdata->base = -1;
708 pdata->chip[0].pullups = 0;
709 pdata->irq_controller = of_property_read_bool(
710 client->dev.of_node,
711 "interrupt-controller");
712 pdata->mirror = of_property_read_bool(client->dev.of_node,
713 "microchip,irq-mirror");
Lars Poeschel4e47f912014-01-16 11:44:15 +0100714 client->irq = irq_of_parse_and_map(client->dev.of_node, 0);
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200715 } else {
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800716 pdata = dev_get_platdata(&client->dev);
Sonic Zhangb184c382015-01-20 17:00:08 +0800717 if (!pdata) {
718 pdata = devm_kzalloc(&client->dev,
719 sizeof(struct mcp23s08_platform_data),
720 GFP_KERNEL);
Insu Yunaaf2b3a2016-02-15 21:19:57 -0500721 if (!pdata)
722 return -ENOMEM;
Sonic Zhangb184c382015-01-20 17:00:08 +0800723 pdata->base = -1;
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200724 }
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200725 }
726
Gary Servin33bc84112014-03-06 20:25:26 -0300727 mcp = kzalloc(sizeof(*mcp), GFP_KERNEL);
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200728 if (!mcp)
729 return -ENOMEM;
730
Lars Poeschel4e47f912014-01-16 11:44:15 +0100731 mcp->irq = client->irq;
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200732 status = mcp23s08_probe_one(mcp, &client->dev, client, client->addr,
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800733 id->driver_data, pdata, 0);
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200734 if (status)
735 goto fail;
736
737 i2c_set_clientdata(client, mcp);
738
739 return 0;
740
741fail:
742 kfree(mcp);
743
744 return status;
745}
746
Bill Pemberton206210c2012-11-19 13:25:50 -0500747static int mcp230xx_remove(struct i2c_client *client)
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200748{
749 struct mcp23s08 *mcp = i2c_get_clientdata(client);
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200750
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200751 gpiochip_remove(&mcp->chip);
752 kfree(mcp);
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200753
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200754 return 0;
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200755}
756
757static const struct i2c_device_id mcp230xx_id[] = {
758 { "mcp23008", MCP_TYPE_008 },
759 { "mcp23017", MCP_TYPE_017 },
760 { },
761};
762MODULE_DEVICE_TABLE(i2c, mcp230xx_id);
763
764static struct i2c_driver mcp230xx_driver = {
765 .driver = {
766 .name = "mcp230xx",
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200767 .of_match_table = of_match_ptr(mcp23s08_i2c_of_match),
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200768 },
769 .probe = mcp230xx_probe,
Bill Pemberton8283c4f2012-11-19 13:20:08 -0500770 .remove = mcp230xx_remove,
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200771 .id_table = mcp230xx_id,
772};
773
774static int __init mcp23s08_i2c_init(void)
775{
776 return i2c_add_driver(&mcp230xx_driver);
777}
778
779static void mcp23s08_i2c_exit(void)
780{
781 i2c_del_driver(&mcp230xx_driver);
782}
783
784#else
785
786static int __init mcp23s08_i2c_init(void) { return 0; }
787static void mcp23s08_i2c_exit(void) { }
788
789#endif /* CONFIG_I2C */
790
791/*----------------------------------------------------------------------*/
792
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200793#ifdef CONFIG_SPI_MASTER
794
David Brownell8f1cc3b2008-07-25 01:46:09 -0700795static int mcp23s08_probe(struct spi_device *spi)
796{
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800797 struct mcp23s08_platform_data *pdata, local_pdata;
David Brownell8f1cc3b2008-07-25 01:46:09 -0700798 unsigned addr;
Linus Walleij596a1c52014-05-28 09:14:06 +0200799 int chips = 0;
David Brownell8f1cc3b2008-07-25 01:46:09 -0700800 struct mcp23s08_driver_data *data;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100801 int status, type;
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800802 unsigned ngpio = 0;
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200803 const struct of_device_id *match;
804 u32 spi_present_mask = 0;
David Brownell8f1cc3b2008-07-25 01:46:09 -0700805
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200806 match = of_match_device(of_match_ptr(mcp23s08_spi_of_match), &spi->dev);
807 if (match) {
SeongJae Parkde755c32014-01-18 13:53:04 +0900808 type = (int)(uintptr_t)match->data;
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200809 status = of_property_read_u32(spi->dev.of_node,
Lars Poeschel45971682013-08-28 10:38:50 +0200810 "microchip,spi-present-mask", &spi_present_mask);
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200811 if (status) {
Lars Poeschel45971682013-08-28 10:38:50 +0200812 status = of_property_read_u32(spi->dev.of_node,
813 "mcp,spi-present-mask", &spi_present_mask);
814 if (status) {
815 dev_err(&spi->dev,
816 "DT has no spi-present-mask\n");
817 return -ENODEV;
818 }
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200819 }
820 if ((spi_present_mask <= 0) || (spi_present_mask >= 256)) {
821 dev_err(&spi->dev, "invalid spi-present-mask\n");
822 return -ENODEV;
823 }
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100824
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800825 pdata = &local_pdata;
826 pdata->base = -1;
Michael Welling99e4b982014-04-16 20:00:24 -0500827 for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800828 pdata->chip[addr].pullups = 0;
Michael Stickel3e3bed92014-05-26 10:03:16 +0200829 if (spi_present_mask & (1 << addr))
830 chips++;
Michael Welling99e4b982014-04-16 20:00:24 -0500831 }
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800832 pdata->irq_controller = of_property_read_bool(
833 spi->dev.of_node,
834 "interrupt-controller");
835 pdata->mirror = of_property_read_bool(spi->dev.of_node,
836 "microchip,irq-mirror");
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200837 } else {
838 type = spi_get_device_id(spi)->driver_data;
Jingoo Hane56aee12013-07-30 17:08:05 +0900839 pdata = dev_get_platdata(&spi->dev);
Sonic Zhangb184c382015-01-20 17:00:08 +0800840 if (!pdata) {
841 pdata = devm_kzalloc(&spi->dev,
842 sizeof(struct mcp23s08_platform_data),
843 GFP_KERNEL);
844 pdata->base = -1;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100845 }
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200846
847 for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
848 if (!pdata->chip[addr].is_present)
849 continue;
850 chips++;
851 if ((type == MCP_TYPE_S08) && (addr > 3)) {
852 dev_err(&spi->dev,
853 "mcp23s08 only supports address 0..3\n");
854 return -EINVAL;
855 }
856 spi_present_mask |= 1 << addr;
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200857 }
David Brownell8f1cc3b2008-07-25 01:46:09 -0700858 }
David Brownell8f1cc3b2008-07-25 01:46:09 -0700859
Michael Welling99e4b982014-04-16 20:00:24 -0500860 if (!chips)
861 return -ENODEV;
862
Varka Bhadram7898b312015-03-31 09:49:08 +0530863 data = devm_kzalloc(&spi->dev,
864 sizeof(*data) + chips * sizeof(struct mcp23s08),
865 GFP_KERNEL);
David Brownell8f1cc3b2008-07-25 01:46:09 -0700866 if (!data)
867 return -ENOMEM;
Varka Bhadram7898b312015-03-31 09:49:08 +0530868
David Brownell8f1cc3b2008-07-25 01:46:09 -0700869 spi_set_drvdata(spi, data);
870
Alexander Steina231b88c2014-11-17 09:38:10 +0100871 spi->irq = irq_of_parse_and_map(spi->dev.of_node, 0);
872
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100873 for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200874 if (!(spi_present_mask & (1 << addr)))
David Brownell8f1cc3b2008-07-25 01:46:09 -0700875 continue;
876 chips--;
877 data->mcp[addr] = &data->chip[chips];
Alexander Steina231b88c2014-11-17 09:38:10 +0100878 data->mcp[addr]->irq = spi->irq;
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200879 status = mcp23s08_probe_one(data->mcp[addr], &spi->dev, spi,
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800880 0x40 | (addr << 1), type, pdata,
881 addr);
David Brownell8f1cc3b2008-07-25 01:46:09 -0700882 if (status < 0)
883 goto fail;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100884
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800885 if (pdata->base != -1)
Phil Reid28c5a412016-03-01 14:25:41 +0800886 pdata->base += data->mcp[addr]->chip.ngpio;
887 ngpio += data->mcp[addr]->chip.ngpio;
David Brownell8f1cc3b2008-07-25 01:46:09 -0700888 }
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200889 data->ngpio = ngpio;
David Brownelle58b9e22008-02-04 22:28:25 -0800890
891 /* NOTE: these chips have a relatively sane IRQ framework, with
892 * per-signal masking and level/edge triggering. It's not yet
893 * handled here...
894 */
895
David Brownelle58b9e22008-02-04 22:28:25 -0800896 return 0;
897
898fail:
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100899 for (addr = 0; addr < ARRAY_SIZE(data->mcp); addr++) {
David Brownell8f1cc3b2008-07-25 01:46:09 -0700900
901 if (!data->mcp[addr])
902 continue;
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200903 gpiochip_remove(&data->mcp[addr]->chip);
David Brownell8f1cc3b2008-07-25 01:46:09 -0700904 }
David Brownelle58b9e22008-02-04 22:28:25 -0800905 return status;
906}
907
908static int mcp23s08_remove(struct spi_device *spi)
909{
David Brownell8f1cc3b2008-07-25 01:46:09 -0700910 struct mcp23s08_driver_data *data = spi_get_drvdata(spi);
David Brownell8f1cc3b2008-07-25 01:46:09 -0700911 unsigned addr;
David Brownelle58b9e22008-02-04 22:28:25 -0800912
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100913 for (addr = 0; addr < ARRAY_SIZE(data->mcp); addr++) {
David Brownell8f1cc3b2008-07-25 01:46:09 -0700914
915 if (!data->mcp[addr])
916 continue;
917
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200918 gpiochip_remove(&data->mcp[addr]->chip);
David Brownell8f1cc3b2008-07-25 01:46:09 -0700919 }
Varka Bhadramc4941e02015-04-07 21:34:40 +0530920
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200921 return 0;
David Brownelle58b9e22008-02-04 22:28:25 -0800922}
923
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100924static const struct spi_device_id mcp23s08_ids[] = {
925 { "mcp23s08", MCP_TYPE_S08 },
926 { "mcp23s17", MCP_TYPE_S17 },
Phil Reid28c5a412016-03-01 14:25:41 +0800927 { "mcp23s18", MCP_TYPE_S18 },
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100928 { },
929};
930MODULE_DEVICE_TABLE(spi, mcp23s08_ids);
931
David Brownelle58b9e22008-02-04 22:28:25 -0800932static struct spi_driver mcp23s08_driver = {
933 .probe = mcp23s08_probe,
934 .remove = mcp23s08_remove,
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100935 .id_table = mcp23s08_ids,
David Brownelle58b9e22008-02-04 22:28:25 -0800936 .driver = {
937 .name = "mcp23s08",
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200938 .of_match_table = of_match_ptr(mcp23s08_spi_of_match),
David Brownelle58b9e22008-02-04 22:28:25 -0800939 },
940};
941
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200942static int __init mcp23s08_spi_init(void)
943{
944 return spi_register_driver(&mcp23s08_driver);
945}
946
947static void mcp23s08_spi_exit(void)
948{
949 spi_unregister_driver(&mcp23s08_driver);
950}
951
952#else
953
954static int __init mcp23s08_spi_init(void) { return 0; }
955static void mcp23s08_spi_exit(void) { }
956
957#endif /* CONFIG_SPI_MASTER */
958
David Brownelle58b9e22008-02-04 22:28:25 -0800959/*----------------------------------------------------------------------*/
960
961static int __init mcp23s08_init(void)
962{
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200963 int ret;
964
965 ret = mcp23s08_spi_init();
966 if (ret)
967 goto spi_fail;
968
969 ret = mcp23s08_i2c_init();
970 if (ret)
971 goto i2c_fail;
972
973 return 0;
974
975 i2c_fail:
976 mcp23s08_spi_exit();
977 spi_fail:
978 return ret;
David Brownelle58b9e22008-02-04 22:28:25 -0800979}
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200980/* register after spi/i2c postcore initcall and before
David Brownell673c0c02008-10-15 22:02:46 -0700981 * subsys initcalls that may rely on these GPIOs
982 */
983subsys_initcall(mcp23s08_init);
David Brownelle58b9e22008-02-04 22:28:25 -0800984
985static void __exit mcp23s08_exit(void)
986{
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200987 mcp23s08_spi_exit();
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200988 mcp23s08_i2c_exit();
David Brownelle58b9e22008-02-04 22:28:25 -0800989}
990module_exit(mcp23s08_exit);
991
992MODULE_LICENSE("GPL");