1 /* MCP23S08 SPI/I2C GPIO driver */
3 #include <linux/kernel.h>
4 #include <linux/device.h>
5 #include <linux/mutex.h>
6 #include <linux/module.h>
7 #include <linux/gpio.h>
9 #include <linux/spi/spi.h>
10 #include <linux/spi/mcp23s08.h>
11 #include <linux/slab.h>
12 #include <asm/byteorder.h>
13 #include <linux/interrupt.h>
14 #include <linux/of_device.h>
15 #include <linux/regmap.h>
16 #include <linux/pinctrl/pinctrl.h>
17 #include <linux/pinctrl/pinconf.h>
18 #include <linux/pinctrl/pinconf-generic.h>
21 * MCP types supported by driver
23 #define MCP_TYPE_S08 0
24 #define MCP_TYPE_S17 1
25 #define MCP_TYPE_008 2
26 #define MCP_TYPE_017 3
27 #define MCP_TYPE_S18 4
28 #define MCP_TYPE_018 5
30 #define MCP_MAX_DEV_PER_CS 8
32 /* Registers are all 8 bits wide.
34 * The mcp23s17 has twice as many bits, and can be configured to work
35 * with either 16 bit registers or with two adjacent 8 bit banks.
37 #define MCP_IODIR 0x00 /* init/reset: all ones */
39 #define MCP_GPINTEN 0x02
40 #define MCP_DEFVAL 0x03
41 #define MCP_INTCON 0x04
42 #define MCP_IOCON 0x05
43 # define IOCON_MIRROR (1 << 6)
44 # define IOCON_SEQOP (1 << 5)
45 # define IOCON_HAEN (1 << 3)
46 # define IOCON_ODR (1 << 2)
47 # define IOCON_INTPOL (1 << 1)
48 # define IOCON_INTCC (1)
51 #define MCP_INTCAP 0x08
67 /* lock protects regmap access with bypass/cache flags */
70 struct gpio_chip chip
;
72 struct regmap
*regmap
;
75 struct pinctrl_dev
*pctldev
;
76 struct pinctrl_desc pinctrl_desc
;
79 static const struct reg_default mcp23x08_defaults
[] = {
80 {.reg
= MCP_IODIR
, .def
= 0xff},
81 {.reg
= MCP_IPOL
, .def
= 0x00},
82 {.reg
= MCP_GPINTEN
, .def
= 0x00},
83 {.reg
= MCP_DEFVAL
, .def
= 0x00},
84 {.reg
= MCP_INTCON
, .def
= 0x00},
85 {.reg
= MCP_IOCON
, .def
= 0x00},
86 {.reg
= MCP_GPPU
, .def
= 0x00},
87 {.reg
= MCP_OLAT
, .def
= 0x00},
90 static const struct regmap_range mcp23x08_volatile_range
= {
91 .range_min
= MCP_INTF
,
92 .range_max
= MCP_GPIO
,
95 static const struct regmap_access_table mcp23x08_volatile_table
= {
96 .yes_ranges
= &mcp23x08_volatile_range
,
100 static const struct regmap_range mcp23x08_precious_range
= {
101 .range_min
= MCP_GPIO
,
102 .range_max
= MCP_GPIO
,
105 static const struct regmap_access_table mcp23x08_precious_table
= {
106 .yes_ranges
= &mcp23x08_precious_range
,
110 static const struct regmap_config mcp23x08_regmap
= {
115 .volatile_table
= &mcp23x08_volatile_table
,
116 .precious_table
= &mcp23x08_precious_table
,
117 .reg_defaults
= mcp23x08_defaults
,
118 .num_reg_defaults
= ARRAY_SIZE(mcp23x08_defaults
),
119 .cache_type
= REGCACHE_FLAT
,
120 .max_register
= MCP_OLAT
,
123 static const struct reg_default mcp23x16_defaults
[] = {
124 {.reg
= MCP_IODIR
<< 1, .def
= 0xffff},
125 {.reg
= MCP_IPOL
<< 1, .def
= 0x0000},
126 {.reg
= MCP_GPINTEN
<< 1, .def
= 0x0000},
127 {.reg
= MCP_DEFVAL
<< 1, .def
= 0x0000},
128 {.reg
= MCP_INTCON
<< 1, .def
= 0x0000},
129 {.reg
= MCP_IOCON
<< 1, .def
= 0x0000},
130 {.reg
= MCP_GPPU
<< 1, .def
= 0x0000},
131 {.reg
= MCP_OLAT
<< 1, .def
= 0x0000},
134 static const struct regmap_range mcp23x16_volatile_range
= {
135 .range_min
= MCP_INTF
<< 1,
136 .range_max
= MCP_GPIO
<< 1,
139 static const struct regmap_access_table mcp23x16_volatile_table
= {
140 .yes_ranges
= &mcp23x16_volatile_range
,
144 static const struct regmap_range mcp23x16_precious_range
= {
145 .range_min
= MCP_GPIO
<< 1,
146 .range_max
= MCP_GPIO
<< 1,
149 static const struct regmap_access_table mcp23x16_precious_table
= {
150 .yes_ranges
= &mcp23x16_precious_range
,
154 static const struct regmap_config mcp23x17_regmap
= {
159 .max_register
= MCP_OLAT
<< 1,
160 .volatile_table
= &mcp23x16_volatile_table
,
161 .precious_table
= &mcp23x16_precious_table
,
162 .reg_defaults
= mcp23x16_defaults
,
163 .num_reg_defaults
= ARRAY_SIZE(mcp23x16_defaults
),
164 .cache_type
= REGCACHE_FLAT
,
165 .val_format_endian
= REGMAP_ENDIAN_LITTLE
,
168 static int mcp_read(struct mcp23s08
*mcp
, unsigned int reg
, unsigned int *val
)
170 return regmap_read(mcp
->regmap
, reg
<< mcp
->reg_shift
, val
);
173 static int mcp_write(struct mcp23s08
*mcp
, unsigned int reg
, unsigned int val
)
175 return regmap_write(mcp
->regmap
, reg
<< mcp
->reg_shift
, val
);
178 static int mcp_set_mask(struct mcp23s08
*mcp
, unsigned int reg
,
179 unsigned int mask
, bool enabled
)
181 u16 val
= enabled
? 0xffff : 0x0000;
182 return regmap_update_bits(mcp
->regmap
, reg
<< mcp
->reg_shift
,
186 static int mcp_set_bit(struct mcp23s08
*mcp
, unsigned int reg
,
187 unsigned int pin
, bool enabled
)
190 return mcp_set_mask(mcp
, reg
, mask
, enabled
);
193 static const struct pinctrl_pin_desc mcp23x08_pins
[] = {
194 PINCTRL_PIN(0, "gpio0"),
195 PINCTRL_PIN(1, "gpio1"),
196 PINCTRL_PIN(2, "gpio2"),
197 PINCTRL_PIN(3, "gpio3"),
198 PINCTRL_PIN(4, "gpio4"),
199 PINCTRL_PIN(5, "gpio5"),
200 PINCTRL_PIN(6, "gpio6"),
201 PINCTRL_PIN(7, "gpio7"),
204 static const struct pinctrl_pin_desc mcp23x17_pins
[] = {
205 PINCTRL_PIN(0, "gpio0"),
206 PINCTRL_PIN(1, "gpio1"),
207 PINCTRL_PIN(2, "gpio2"),
208 PINCTRL_PIN(3, "gpio3"),
209 PINCTRL_PIN(4, "gpio4"),
210 PINCTRL_PIN(5, "gpio5"),
211 PINCTRL_PIN(6, "gpio6"),
212 PINCTRL_PIN(7, "gpio7"),
213 PINCTRL_PIN(8, "gpio8"),
214 PINCTRL_PIN(9, "gpio9"),
215 PINCTRL_PIN(10, "gpio10"),
216 PINCTRL_PIN(11, "gpio11"),
217 PINCTRL_PIN(12, "gpio12"),
218 PINCTRL_PIN(13, "gpio13"),
219 PINCTRL_PIN(14, "gpio14"),
220 PINCTRL_PIN(15, "gpio15"),
223 static int mcp_pinctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
228 static const char *mcp_pinctrl_get_group_name(struct pinctrl_dev
*pctldev
,
234 static int mcp_pinctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
236 const unsigned int **pins
,
237 unsigned int *num_pins
)
242 static const struct pinctrl_ops mcp_pinctrl_ops
= {
243 .get_groups_count
= mcp_pinctrl_get_groups_count
,
244 .get_group_name
= mcp_pinctrl_get_group_name
,
245 .get_group_pins
= mcp_pinctrl_get_group_pins
,
247 .dt_node_to_map
= pinconf_generic_dt_node_to_map_pin
,
248 .dt_free_map
= pinconf_generic_dt_free_map
,
252 static int mcp_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned int pin
,
253 unsigned long *config
)
255 struct mcp23s08
*mcp
= pinctrl_dev_get_drvdata(pctldev
);
256 enum pin_config_param param
= pinconf_to_config_param(*config
);
257 unsigned int data
, status
;
261 case PIN_CONFIG_BIAS_PULL_UP
:
262 ret
= mcp_read(mcp
, MCP_GPPU
, &data
);
265 status
= (data
& BIT(pin
)) ? 1 : 0;
268 dev_err(mcp
->dev
, "Invalid config param %04x\n", param
);
274 return status
? 0 : -EINVAL
;
277 static int mcp_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
278 unsigned long *configs
, unsigned int num_configs
)
280 struct mcp23s08
*mcp
= pinctrl_dev_get_drvdata(pctldev
);
281 enum pin_config_param param
;
286 for (i
= 0; i
< num_configs
; i
++) {
287 param
= pinconf_to_config_param(configs
[i
]);
288 arg
= pinconf_to_config_argument(configs
[i
]);
291 case PIN_CONFIG_BIAS_PULL_UP
:
292 ret
= mcp_set_bit(mcp
, MCP_GPPU
, pin
, arg
);
295 dev_err(mcp
->dev
, "Invalid config param %04x\n", param
);
303 static const struct pinconf_ops mcp_pinconf_ops
= {
304 .pin_config_get
= mcp_pinconf_get
,
305 .pin_config_set
= mcp_pinconf_set
,
309 /*----------------------------------------------------------------------*/
311 #ifdef CONFIG_SPI_MASTER
313 static int mcp23sxx_spi_write(void *context
, const void *data
, size_t count
)
315 struct mcp23s08
*mcp
= context
;
316 struct spi_device
*spi
= to_spi_device(mcp
->dev
);
317 struct spi_message m
;
318 struct spi_transfer t
[2] = { { .tx_buf
= &mcp
->addr
, .len
= 1, },
319 { .tx_buf
= data
, .len
= count
, }, };
321 spi_message_init(&m
);
322 spi_message_add_tail(&t
[0], &m
);
323 spi_message_add_tail(&t
[1], &m
);
325 return spi_sync(spi
, &m
);
328 static int mcp23sxx_spi_gather_write(void *context
,
329 const void *reg
, size_t reg_size
,
330 const void *val
, size_t val_size
)
332 struct mcp23s08
*mcp
= context
;
333 struct spi_device
*spi
= to_spi_device(mcp
->dev
);
334 struct spi_message m
;
335 struct spi_transfer t
[3] = { { .tx_buf
= &mcp
->addr
, .len
= 1, },
336 { .tx_buf
= reg
, .len
= reg_size
, },
337 { .tx_buf
= val
, .len
= val_size
, }, };
339 spi_message_init(&m
);
340 spi_message_add_tail(&t
[0], &m
);
341 spi_message_add_tail(&t
[1], &m
);
342 spi_message_add_tail(&t
[2], &m
);
344 return spi_sync(spi
, &m
);
347 static int mcp23sxx_spi_read(void *context
, const void *reg
, size_t reg_size
,
348 void *val
, size_t val_size
)
350 struct mcp23s08
*mcp
= context
;
351 struct spi_device
*spi
= to_spi_device(mcp
->dev
);
357 tx
[0] = mcp
->addr
| 0x01;
358 tx
[1] = *((u8
*) reg
);
360 return spi_write_then_read(spi
, tx
, sizeof(tx
), val
, val_size
);
363 static const struct regmap_bus mcp23sxx_spi_regmap
= {
364 .write
= mcp23sxx_spi_write
,
365 .gather_write
= mcp23sxx_spi_gather_write
,
366 .read
= mcp23sxx_spi_read
,
369 #endif /* CONFIG_SPI_MASTER */
371 /*----------------------------------------------------------------------*/
373 /* A given spi_device can represent up to eight mcp23sxx chips
374 * sharing the same chipselect but using different addresses
375 * (e.g. chips #0 and #3 might be populated, but not #1 or $2).
376 * Driver data holds all the per-chip data.
378 struct mcp23s08_driver_data
{
380 struct mcp23s08
*mcp
[8];
381 struct mcp23s08 chip
[];
385 static int mcp23s08_direction_input(struct gpio_chip
*chip
, unsigned offset
)
387 struct mcp23s08
*mcp
= gpiochip_get_data(chip
);
390 mutex_lock(&mcp
->lock
);
391 status
= mcp_set_bit(mcp
, MCP_IODIR
, offset
, true);
392 mutex_unlock(&mcp
->lock
);
397 static int mcp23s08_get(struct gpio_chip
*chip
, unsigned offset
)
399 struct mcp23s08
*mcp
= gpiochip_get_data(chip
);
402 mutex_lock(&mcp
->lock
);
404 /* REVISIT reading this clears any IRQ ... */
405 ret
= mcp_read(mcp
, MCP_GPIO
, &status
);
409 mcp
->cached_gpio
= status
;
410 status
= !!(status
& (1 << offset
));
413 mutex_unlock(&mcp
->lock
);
417 static int __mcp23s08_set(struct mcp23s08
*mcp
, unsigned mask
, bool value
)
419 return mcp_set_mask(mcp
, MCP_OLAT
, mask
, value
);
422 static void mcp23s08_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
424 struct mcp23s08
*mcp
= gpiochip_get_data(chip
);
425 unsigned mask
= BIT(offset
);
427 mutex_lock(&mcp
->lock
);
428 __mcp23s08_set(mcp
, mask
, !!value
);
429 mutex_unlock(&mcp
->lock
);
433 mcp23s08_direction_output(struct gpio_chip
*chip
, unsigned offset
, int value
)
435 struct mcp23s08
*mcp
= gpiochip_get_data(chip
);
436 unsigned mask
= BIT(offset
);
439 mutex_lock(&mcp
->lock
);
440 status
= __mcp23s08_set(mcp
, mask
, value
);
442 status
= mcp_set_mask(mcp
, MCP_IODIR
, mask
, false);
444 mutex_unlock(&mcp
->lock
);
448 /*----------------------------------------------------------------------*/
449 static irqreturn_t
mcp23s08_irq(int irq
, void *data
)
451 struct mcp23s08
*mcp
= data
;
452 int intcap
, intcon
, intf
, i
, gpio
, gpio_orig
, intcap_mask
, defval
;
453 unsigned int child_irq
;
454 bool intf_set
, intcap_changed
, gpio_bit_changed
,
455 defval_changed
, gpio_set
;
457 mutex_lock(&mcp
->lock
);
458 if (mcp_read(mcp
, MCP_INTF
, &intf
))
461 if (mcp_read(mcp
, MCP_INTCAP
, &intcap
))
464 if (mcp_read(mcp
, MCP_INTCON
, &intcon
))
467 if (mcp_read(mcp
, MCP_DEFVAL
, &defval
))
470 /* This clears the interrupt(configurable on S18) */
471 if (mcp_read(mcp
, MCP_GPIO
, &gpio
))
474 gpio_orig
= mcp
->cached_gpio
;
475 mcp
->cached_gpio
= gpio
;
476 mutex_unlock(&mcp
->lock
);
479 /* There is no interrupt pending */
483 dev_dbg(mcp
->chip
.parent
,
484 "intcap 0x%04X intf 0x%04X gpio_orig 0x%04X gpio 0x%04X\n",
485 intcap
, intf
, gpio_orig
, gpio
);
487 for (i
= 0; i
< mcp
->chip
.ngpio
; i
++) {
488 /* We must check all of the inputs on the chip,
489 * otherwise we may not notice a change on >=2 pins.
491 * On at least the mcp23s17, INTCAP is only updated
492 * one byte at a time(INTCAPA and INTCAPB are
493 * not written to at the same time - only on a per-bank
496 * INTF only contains the single bit that caused the
497 * interrupt per-bank. On the mcp23s17, there is
498 * INTFA and INTFB. If two pins are changed on the A
499 * side at the same time, INTF will only have one bit
500 * set. If one pin on the A side and one pin on the B
501 * side are changed at the same time, INTF will have
502 * two bits set. Thus, INTF can't be the only check
503 * to see if the input has changed.
506 intf_set
= intf
& BIT(i
);
507 if (i
< 8 && intf_set
)
508 intcap_mask
= 0x00FF;
509 else if (i
>= 8 && intf_set
)
510 intcap_mask
= 0xFF00;
514 intcap_changed
= (intcap_mask
&
515 (intcap
& BIT(i
))) !=
516 (intcap_mask
& (BIT(i
) & gpio_orig
));
517 gpio_set
= BIT(i
) & gpio
;
518 gpio_bit_changed
= (BIT(i
) & gpio_orig
) !=
520 defval_changed
= (BIT(i
) & intcon
) &&
524 if (((gpio_bit_changed
|| intcap_changed
) &&
525 (BIT(i
) & mcp
->irq_rise
) && gpio_set
) ||
526 ((gpio_bit_changed
|| intcap_changed
) &&
527 (BIT(i
) & mcp
->irq_fall
) && !gpio_set
) ||
529 child_irq
= irq_find_mapping(mcp
->chip
.irq
.domain
, i
);
530 handle_nested_irq(child_irq
);
537 mutex_unlock(&mcp
->lock
);
541 static void mcp23s08_irq_mask(struct irq_data
*data
)
543 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(data
);
544 struct mcp23s08
*mcp
= gpiochip_get_data(gc
);
545 unsigned int pos
= data
->hwirq
;
547 mcp_set_bit(mcp
, MCP_GPINTEN
, pos
, false);
550 static void mcp23s08_irq_unmask(struct irq_data
*data
)
552 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(data
);
553 struct mcp23s08
*mcp
= gpiochip_get_data(gc
);
554 unsigned int pos
= data
->hwirq
;
556 mcp_set_bit(mcp
, MCP_GPINTEN
, pos
, true);
559 static int mcp23s08_irq_set_type(struct irq_data
*data
, unsigned int type
)
561 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(data
);
562 struct mcp23s08
*mcp
= gpiochip_get_data(gc
);
563 unsigned int pos
= data
->hwirq
;
566 if ((type
& IRQ_TYPE_EDGE_BOTH
) == IRQ_TYPE_EDGE_BOTH
) {
567 mcp_set_bit(mcp
, MCP_INTCON
, pos
, false);
568 mcp
->irq_rise
|= BIT(pos
);
569 mcp
->irq_fall
|= BIT(pos
);
570 } else if (type
& IRQ_TYPE_EDGE_RISING
) {
571 mcp_set_bit(mcp
, MCP_INTCON
, pos
, false);
572 mcp
->irq_rise
|= BIT(pos
);
573 mcp
->irq_fall
&= ~BIT(pos
);
574 } else if (type
& IRQ_TYPE_EDGE_FALLING
) {
575 mcp_set_bit(mcp
, MCP_INTCON
, pos
, false);
576 mcp
->irq_rise
&= ~BIT(pos
);
577 mcp
->irq_fall
|= BIT(pos
);
578 } else if (type
& IRQ_TYPE_LEVEL_HIGH
) {
579 mcp_set_bit(mcp
, MCP_INTCON
, pos
, true);
580 mcp_set_bit(mcp
, MCP_DEFVAL
, pos
, false);
581 } else if (type
& IRQ_TYPE_LEVEL_LOW
) {
582 mcp_set_bit(mcp
, MCP_INTCON
, pos
, true);
583 mcp_set_bit(mcp
, MCP_DEFVAL
, pos
, true);
590 static void mcp23s08_irq_bus_lock(struct irq_data
*data
)
592 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(data
);
593 struct mcp23s08
*mcp
= gpiochip_get_data(gc
);
595 mutex_lock(&mcp
->lock
);
596 regcache_cache_only(mcp
->regmap
, true);
599 static void mcp23s08_irq_bus_unlock(struct irq_data
*data
)
601 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(data
);
602 struct mcp23s08
*mcp
= gpiochip_get_data(gc
);
604 regcache_cache_only(mcp
->regmap
, false);
605 regcache_sync(mcp
->regmap
);
607 mutex_unlock(&mcp
->lock
);
610 static struct irq_chip mcp23s08_irq_chip
= {
611 .name
= "gpio-mcp23xxx",
612 .irq_mask
= mcp23s08_irq_mask
,
613 .irq_unmask
= mcp23s08_irq_unmask
,
614 .irq_set_type
= mcp23s08_irq_set_type
,
615 .irq_bus_lock
= mcp23s08_irq_bus_lock
,
616 .irq_bus_sync_unlock
= mcp23s08_irq_bus_unlock
,
619 static int mcp23s08_irq_setup(struct mcp23s08
*mcp
)
621 struct gpio_chip
*chip
= &mcp
->chip
;
623 unsigned long irqflags
= IRQF_ONESHOT
| IRQF_SHARED
;
625 if (mcp
->irq_active_high
)
626 irqflags
|= IRQF_TRIGGER_HIGH
;
628 irqflags
|= IRQF_TRIGGER_LOW
;
630 err
= devm_request_threaded_irq(chip
->parent
, mcp
->irq
, NULL
,
632 irqflags
, dev_name(chip
->parent
), mcp
);
634 dev_err(chip
->parent
, "unable to request IRQ#%d: %d\n",
639 err
= gpiochip_irqchip_add_nested(chip
,
645 dev_err(chip
->parent
,
646 "could not connect irqchip to gpiochip: %d\n", err
);
650 gpiochip_set_nested_irqchip(chip
,
657 /*----------------------------------------------------------------------*/
659 #ifdef CONFIG_DEBUG_FS
661 #include <linux/seq_file.h>
664 * This compares the chip's registers with the register
665 * cache and corrects any incorrectly set register. This
666 * can be used to fix state for MCP23xxx, that temporary
667 * lost its power supply.
669 #define MCP23S08_CONFIG_REGS 8
670 static int __check_mcp23s08_reg_cache(struct mcp23s08
*mcp
)
672 int cached
[MCP23S08_CONFIG_REGS
];
675 /* read cached config registers */
676 for (i
= 0; i
< MCP23S08_CONFIG_REGS
; i
++) {
677 err
= mcp_read(mcp
, i
, &cached
[i
]);
682 regcache_cache_bypass(mcp
->regmap
, true);
684 for (i
= 0; i
< MCP23S08_CONFIG_REGS
; i
++) {
686 err
= mcp_read(mcp
, i
, &uncached
);
690 if (uncached
!= cached
[i
]) {
691 dev_err(mcp
->dev
, "restoring reg 0x%02x from 0x%04x to 0x%04x (power-loss?)\n",
692 i
, uncached
, cached
[i
]);
693 mcp_write(mcp
, i
, cached
[i
]);
699 dev_err(mcp
->dev
, "read error: reg=%02x, err=%d", i
, err
);
700 regcache_cache_bypass(mcp
->regmap
, false);
705 * This shows more info than the generic gpio dump code:
706 * pullups, deglitching, open drain drive.
708 static void mcp23s08_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
710 struct mcp23s08
*mcp
;
714 int iodir
, gpio
, gppu
;
716 mcp
= gpiochip_get_data(chip
);
718 /* NOTE: we only handle one bank for now ... */
719 bank
= '0' + ((mcp
->addr
>> 1) & 0x7);
721 mutex_lock(&mcp
->lock
);
723 t
= __check_mcp23s08_reg_cache(mcp
);
725 seq_printf(s
, " I/O Error\n");
728 t
= mcp_read(mcp
, MCP_IODIR
, &iodir
);
730 seq_printf(s
, " I/O Error\n");
733 t
= mcp_read(mcp
, MCP_GPIO
, &gpio
);
735 seq_printf(s
, " I/O Error\n");
738 t
= mcp_read(mcp
, MCP_GPPU
, &gppu
);
740 seq_printf(s
, " I/O Error\n");
744 for (t
= 0, mask
= BIT(0); t
< chip
->ngpio
; t
++, mask
<<= 1) {
747 label
= gpiochip_is_requested(chip
, t
);
751 seq_printf(s
, " gpio-%-3d P%c.%d (%-12s) %s %s %s\n",
752 chip
->base
+ t
, bank
, t
, label
,
753 (iodir
& mask
) ? "in " : "out",
754 (gpio
& mask
) ? "hi" : "lo",
755 (gppu
& mask
) ? "up" : " ");
756 /* NOTE: ignoring the irq-related registers */
759 mutex_unlock(&mcp
->lock
);
763 #define mcp23s08_dbg_show NULL
766 /*----------------------------------------------------------------------*/
768 static int mcp23s08_probe_one(struct mcp23s08
*mcp
, struct device
*dev
,
769 void *data
, unsigned addr
, unsigned type
,
770 unsigned int base
, int cs
)
775 mutex_init(&mcp
->lock
);
779 mcp
->irq_active_high
= false;
781 mcp
->chip
.direction_input
= mcp23s08_direction_input
;
782 mcp
->chip
.get
= mcp23s08_get
;
783 mcp
->chip
.direction_output
= mcp23s08_direction_output
;
784 mcp
->chip
.set
= mcp23s08_set
;
785 mcp
->chip
.dbg_show
= mcp23s08_dbg_show
;
786 #ifdef CONFIG_OF_GPIO
787 mcp
->chip
.of_gpio_n_cells
= 2;
788 mcp
->chip
.of_node
= dev
->of_node
;
792 #ifdef CONFIG_SPI_MASTER
794 mcp
->regmap
= devm_regmap_init(dev
, &mcp23sxx_spi_regmap
, mcp
,
798 mcp
->chip
.label
= "mcp23s08";
802 mcp
->regmap
= devm_regmap_init(dev
, &mcp23sxx_spi_regmap
, mcp
,
805 mcp
->chip
.ngpio
= 16;
806 mcp
->chip
.label
= "mcp23s17";
810 mcp
->regmap
= devm_regmap_init(dev
, &mcp23sxx_spi_regmap
, mcp
,
813 mcp
->chip
.ngpio
= 16;
814 mcp
->chip
.label
= "mcp23s18";
816 #endif /* CONFIG_SPI_MASTER */
818 #if IS_ENABLED(CONFIG_I2C)
820 mcp
->regmap
= devm_regmap_init_i2c(data
, &mcp23x08_regmap
);
823 mcp
->chip
.label
= "mcp23008";
827 mcp
->regmap
= devm_regmap_init_i2c(data
, &mcp23x17_regmap
);
829 mcp
->chip
.ngpio
= 16;
830 mcp
->chip
.label
= "mcp23017";
834 mcp
->regmap
= devm_regmap_init_i2c(data
, &mcp23x17_regmap
);
836 mcp
->chip
.ngpio
= 16;
837 mcp
->chip
.label
= "mcp23018";
839 #endif /* CONFIG_I2C */
842 dev_err(dev
, "invalid device type (%d)\n", type
);
846 if (IS_ERR(mcp
->regmap
))
847 return PTR_ERR(mcp
->regmap
);
849 mcp
->chip
.base
= base
;
850 mcp
->chip
.can_sleep
= true;
851 mcp
->chip
.parent
= dev
;
852 mcp
->chip
.owner
= THIS_MODULE
;
854 /* verify MCP_IOCON.SEQOP = 0, so sequential reads work,
855 * and MCP_IOCON.HAEN = 1, so we work with all chips.
858 ret
= mcp_read(mcp
, MCP_IOCON
, &status
);
862 mcp
->irq_controller
=
863 device_property_read_bool(dev
, "interrupt-controller");
864 if (mcp
->irq
&& mcp
->irq_controller
) {
865 mcp
->irq_active_high
=
866 device_property_read_bool(dev
,
867 "microchip,irq-active-high");
869 mirror
= device_property_read_bool(dev
, "microchip,irq-mirror");
872 if ((status
& IOCON_SEQOP
) || !(status
& IOCON_HAEN
) || mirror
||
873 mcp
->irq_active_high
) {
874 /* mcp23s17 has IOCON twice, make sure they are in sync */
875 status
&= ~(IOCON_SEQOP
| (IOCON_SEQOP
<< 8));
876 status
|= IOCON_HAEN
| (IOCON_HAEN
<< 8);
877 if (mcp
->irq_active_high
)
878 status
|= IOCON_INTPOL
| (IOCON_INTPOL
<< 8);
880 status
&= ~(IOCON_INTPOL
| (IOCON_INTPOL
<< 8));
883 status
|= IOCON_MIRROR
| (IOCON_MIRROR
<< 8);
885 if (type
== MCP_TYPE_S18
|| type
== MCP_TYPE_018
)
886 status
|= IOCON_INTCC
| (IOCON_INTCC
<< 8);
888 ret
= mcp_write(mcp
, MCP_IOCON
, status
);
893 if (mcp
->irq
&& mcp
->irq_controller
) {
894 ret
= mcp23s08_irq_setup(mcp
);
899 ret
= devm_gpiochip_add_data(dev
, &mcp
->chip
, mcp
);
903 mcp
->pinctrl_desc
.name
= "mcp23xxx-pinctrl";
904 mcp
->pinctrl_desc
.pctlops
= &mcp_pinctrl_ops
;
905 mcp
->pinctrl_desc
.confops
= &mcp_pinconf_ops
;
906 mcp
->pinctrl_desc
.npins
= mcp
->chip
.ngpio
;
907 if (mcp
->pinctrl_desc
.npins
== 8)
908 mcp
->pinctrl_desc
.pins
= mcp23x08_pins
;
909 else if (mcp
->pinctrl_desc
.npins
== 16)
910 mcp
->pinctrl_desc
.pins
= mcp23x17_pins
;
911 mcp
->pinctrl_desc
.owner
= THIS_MODULE
;
913 mcp
->pctldev
= devm_pinctrl_register(dev
, &mcp
->pinctrl_desc
, mcp
);
914 if (IS_ERR(mcp
->pctldev
)) {
915 ret
= PTR_ERR(mcp
->pctldev
);
921 dev_dbg(dev
, "can't setup chip %d, --> %d\n", addr
, ret
);
925 /*----------------------------------------------------------------------*/
928 #ifdef CONFIG_SPI_MASTER
929 static const struct of_device_id mcp23s08_spi_of_match
[] = {
931 .compatible
= "microchip,mcp23s08",
932 .data
= (void *) MCP_TYPE_S08
,
935 .compatible
= "microchip,mcp23s17",
936 .data
= (void *) MCP_TYPE_S17
,
939 .compatible
= "microchip,mcp23s18",
940 .data
= (void *) MCP_TYPE_S18
,
942 /* NOTE: The use of the mcp prefix is deprecated and will be removed. */
944 .compatible
= "mcp,mcp23s08",
945 .data
= (void *) MCP_TYPE_S08
,
948 .compatible
= "mcp,mcp23s17",
949 .data
= (void *) MCP_TYPE_S17
,
953 MODULE_DEVICE_TABLE(of
, mcp23s08_spi_of_match
);
956 #if IS_ENABLED(CONFIG_I2C)
957 static const struct of_device_id mcp23s08_i2c_of_match
[] = {
959 .compatible
= "microchip,mcp23008",
960 .data
= (void *) MCP_TYPE_008
,
963 .compatible
= "microchip,mcp23017",
964 .data
= (void *) MCP_TYPE_017
,
967 .compatible
= "microchip,mcp23018",
968 .data
= (void *) MCP_TYPE_018
,
970 /* NOTE: The use of the mcp prefix is deprecated and will be removed. */
972 .compatible
= "mcp,mcp23008",
973 .data
= (void *) MCP_TYPE_008
,
976 .compatible
= "mcp,mcp23017",
977 .data
= (void *) MCP_TYPE_017
,
981 MODULE_DEVICE_TABLE(of
, mcp23s08_i2c_of_match
);
983 #endif /* CONFIG_OF */
986 #if IS_ENABLED(CONFIG_I2C)
988 static int mcp230xx_probe(struct i2c_client
*client
,
989 const struct i2c_device_id
*id
)
991 struct mcp23s08_platform_data
*pdata
, local_pdata
;
992 struct mcp23s08
*mcp
;
995 pdata
= dev_get_platdata(&client
->dev
);
997 pdata
= &local_pdata
;
1001 mcp
= devm_kzalloc(&client
->dev
, sizeof(*mcp
), GFP_KERNEL
);
1005 mcp
->irq
= client
->irq
;
1006 status
= mcp23s08_probe_one(mcp
, &client
->dev
, client
, client
->addr
,
1007 id
->driver_data
, pdata
->base
, 0);
1011 i2c_set_clientdata(client
, mcp
);
1016 static const struct i2c_device_id mcp230xx_id
[] = {
1017 { "mcp23008", MCP_TYPE_008
},
1018 { "mcp23017", MCP_TYPE_017
},
1019 { "mcp23018", MCP_TYPE_018
},
1022 MODULE_DEVICE_TABLE(i2c
, mcp230xx_id
);
1024 static struct i2c_driver mcp230xx_driver
= {
1027 .of_match_table
= of_match_ptr(mcp23s08_i2c_of_match
),
1029 .probe
= mcp230xx_probe
,
1030 .id_table
= mcp230xx_id
,
1033 static int __init
mcp23s08_i2c_init(void)
1035 return i2c_add_driver(&mcp230xx_driver
);
1038 static void mcp23s08_i2c_exit(void)
1040 i2c_del_driver(&mcp230xx_driver
);
1045 static int __init
mcp23s08_i2c_init(void) { return 0; }
1046 static void mcp23s08_i2c_exit(void) { }
1048 #endif /* CONFIG_I2C */
1050 /*----------------------------------------------------------------------*/
1052 #ifdef CONFIG_SPI_MASTER
1054 static int mcp23s08_probe(struct spi_device
*spi
)
1056 struct mcp23s08_platform_data
*pdata
, local_pdata
;
1059 struct mcp23s08_driver_data
*data
;
1062 const struct of_device_id
*match
;
1064 match
= of_match_device(of_match_ptr(mcp23s08_spi_of_match
), &spi
->dev
);
1066 type
= (int)(uintptr_t)match
->data
;
1068 type
= spi_get_device_id(spi
)->driver_data
;
1070 pdata
= dev_get_platdata(&spi
->dev
);
1072 pdata
= &local_pdata
;
1075 status
= device_property_read_u32(&spi
->dev
,
1076 "microchip,spi-present-mask", &pdata
->spi_present_mask
);
1078 status
= device_property_read_u32(&spi
->dev
,
1079 "mcp,spi-present-mask",
1080 &pdata
->spi_present_mask
);
1083 dev_err(&spi
->dev
, "missing spi-present-mask");
1089 if (!pdata
->spi_present_mask
|| pdata
->spi_present_mask
> 0xff) {
1090 dev_err(&spi
->dev
, "invalid spi-present-mask");
1094 for (addr
= 0; addr
< MCP_MAX_DEV_PER_CS
; addr
++) {
1095 if (pdata
->spi_present_mask
& BIT(addr
))
1102 data
= devm_kzalloc(&spi
->dev
,
1103 sizeof(*data
) + chips
* sizeof(struct mcp23s08
),
1108 spi_set_drvdata(spi
, data
);
1110 for (addr
= 0; addr
< MCP_MAX_DEV_PER_CS
; addr
++) {
1111 if (!(pdata
->spi_present_mask
& BIT(addr
)))
1114 data
->mcp
[addr
] = &data
->chip
[chips
];
1115 data
->mcp
[addr
]->irq
= spi
->irq
;
1116 status
= mcp23s08_probe_one(data
->mcp
[addr
], &spi
->dev
, spi
,
1117 0x40 | (addr
<< 1), type
,
1122 if (pdata
->base
!= -1)
1123 pdata
->base
+= data
->mcp
[addr
]->chip
.ngpio
;
1124 ngpio
+= data
->mcp
[addr
]->chip
.ngpio
;
1126 data
->ngpio
= ngpio
;
1131 static const struct spi_device_id mcp23s08_ids
[] = {
1132 { "mcp23s08", MCP_TYPE_S08
},
1133 { "mcp23s17", MCP_TYPE_S17
},
1134 { "mcp23s18", MCP_TYPE_S18
},
1137 MODULE_DEVICE_TABLE(spi
, mcp23s08_ids
);
1139 static struct spi_driver mcp23s08_driver
= {
1140 .probe
= mcp23s08_probe
,
1141 .id_table
= mcp23s08_ids
,
1144 .of_match_table
= of_match_ptr(mcp23s08_spi_of_match
),
1148 static int __init
mcp23s08_spi_init(void)
1150 return spi_register_driver(&mcp23s08_driver
);
1153 static void mcp23s08_spi_exit(void)
1155 spi_unregister_driver(&mcp23s08_driver
);
1160 static int __init
mcp23s08_spi_init(void) { return 0; }
1161 static void mcp23s08_spi_exit(void) { }
1163 #endif /* CONFIG_SPI_MASTER */
1165 /*----------------------------------------------------------------------*/
1167 static int __init
mcp23s08_init(void)
1171 ret
= mcp23s08_spi_init();
1175 ret
= mcp23s08_i2c_init();
1182 mcp23s08_spi_exit();
1186 /* register after spi/i2c postcore initcall and before
1187 * subsys initcalls that may rely on these GPIOs
1189 subsys_initcall(mcp23s08_init
);
1191 static void __exit
mcp23s08_exit(void)
1193 mcp23s08_spi_exit();
1194 mcp23s08_i2c_exit();
1196 module_exit(mcp23s08_exit
);
1198 MODULE_LICENSE("GPL");