Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  PCA953x 4/8/16/24/40 bit I/O ports
0004  *
0005  *  Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
0006  *  Copyright (C) 2007 Marvell International Ltd.
0007  *
0008  *  Derived from drivers/i2c/chips/pca9539.c
0009  */
0010 
0011 #include <linux/acpi.h>
0012 #include <linux/bitmap.h>
0013 #include <linux/gpio/driver.h>
0014 #include <linux/gpio/consumer.h>
0015 #include <linux/i2c.h>
0016 #include <linux/init.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/module.h>
0019 #include <linux/of_platform.h>
0020 #include <linux/platform_data/pca953x.h>
0021 #include <linux/regmap.h>
0022 #include <linux/regulator/consumer.h>
0023 #include <linux/slab.h>
0024 
0025 #include <asm/unaligned.h>
0026 
0027 #define PCA953X_INPUT       0x00
0028 #define PCA953X_OUTPUT      0x01
0029 #define PCA953X_INVERT      0x02
0030 #define PCA953X_DIRECTION   0x03
0031 
0032 #define REG_ADDR_MASK       GENMASK(5, 0)
0033 #define REG_ADDR_EXT        BIT(6)
0034 #define REG_ADDR_AI     BIT(7)
0035 
0036 #define PCA957X_IN      0x00
0037 #define PCA957X_INVRT       0x01
0038 #define PCA957X_BKEN        0x02
0039 #define PCA957X_PUPD        0x03
0040 #define PCA957X_CFG     0x04
0041 #define PCA957X_OUT     0x05
0042 #define PCA957X_MSK     0x06
0043 #define PCA957X_INTS        0x07
0044 
0045 #define PCAL953X_OUT_STRENGTH   0x20
0046 #define PCAL953X_IN_LATCH   0x22
0047 #define PCAL953X_PULL_EN    0x23
0048 #define PCAL953X_PULL_SEL   0x24
0049 #define PCAL953X_INT_MASK   0x25
0050 #define PCAL953X_INT_STAT   0x26
0051 #define PCAL953X_OUT_CONF   0x27
0052 
0053 #define PCAL6524_INT_EDGE   0x28
0054 #define PCAL6524_INT_CLR    0x2a
0055 #define PCAL6524_IN_STATUS  0x2b
0056 #define PCAL6524_OUT_INDCONF    0x2c
0057 #define PCAL6524_DEBOUNCE   0x2d
0058 
0059 #define PCA_GPIO_MASK       GENMASK(7, 0)
0060 
0061 #define PCAL_GPIO_MASK      GENMASK(4, 0)
0062 #define PCAL_PINCTRL_MASK   GENMASK(6, 5)
0063 
0064 #define PCA_INT         BIT(8)
0065 #define PCA_PCAL        BIT(9)
0066 #define PCA_LATCH_INT       (PCA_PCAL | PCA_INT)
0067 #define PCA953X_TYPE        BIT(12)
0068 #define PCA957X_TYPE        BIT(13)
0069 #define PCA_TYPE_MASK       GENMASK(15, 12)
0070 
0071 #define PCA_CHIP_TYPE(x)    ((x) & PCA_TYPE_MASK)
0072 
0073 static const struct i2c_device_id pca953x_id[] = {
0074     { "pca6408", 8  | PCA953X_TYPE | PCA_INT, },
0075     { "pca6416", 16 | PCA953X_TYPE | PCA_INT, },
0076     { "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
0077     { "pca9506", 40 | PCA953X_TYPE | PCA_INT, },
0078     { "pca9534", 8  | PCA953X_TYPE | PCA_INT, },
0079     { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
0080     { "pca9536", 4  | PCA953X_TYPE, },
0081     { "pca9537", 4  | PCA953X_TYPE | PCA_INT, },
0082     { "pca9538", 8  | PCA953X_TYPE | PCA_INT, },
0083     { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
0084     { "pca9554", 8  | PCA953X_TYPE | PCA_INT, },
0085     { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
0086     { "pca9556", 8  | PCA953X_TYPE, },
0087     { "pca9557", 8  | PCA953X_TYPE, },
0088     { "pca9574", 8  | PCA957X_TYPE | PCA_INT, },
0089     { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
0090     { "pca9698", 40 | PCA953X_TYPE, },
0091 
0092     { "pcal6416", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
0093     { "pcal6524", 24 | PCA953X_TYPE | PCA_LATCH_INT, },
0094     { "pcal9535", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
0095     { "pcal9554b", 8  | PCA953X_TYPE | PCA_LATCH_INT, },
0096     { "pcal9555a", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
0097 
0098     { "max7310", 8  | PCA953X_TYPE, },
0099     { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
0100     { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
0101     { "max7315", 8  | PCA953X_TYPE | PCA_INT, },
0102     { "max7318", 16 | PCA953X_TYPE | PCA_INT, },
0103     { "pca6107", 8  | PCA953X_TYPE | PCA_INT, },
0104     { "tca6408", 8  | PCA953X_TYPE | PCA_INT, },
0105     { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
0106     { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
0107     { "tca9539", 16 | PCA953X_TYPE | PCA_INT, },
0108     { "tca9554", 8  | PCA953X_TYPE | PCA_INT, },
0109     { "xra1202", 8  | PCA953X_TYPE },
0110     { }
0111 };
0112 MODULE_DEVICE_TABLE(i2c, pca953x_id);
0113 
0114 #ifdef CONFIG_GPIO_PCA953X_IRQ
0115 
0116 #include <linux/dmi.h>
0117 
0118 static const struct acpi_gpio_params pca953x_irq_gpios = { 0, 0, true };
0119 
0120 static const struct acpi_gpio_mapping pca953x_acpi_irq_gpios[] = {
0121     { "irq-gpios", &pca953x_irq_gpios, 1, ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER },
0122     { }
0123 };
0124 
0125 static int pca953x_acpi_get_irq(struct device *dev)
0126 {
0127     int ret;
0128 
0129     ret = devm_acpi_dev_add_driver_gpios(dev, pca953x_acpi_irq_gpios);
0130     if (ret)
0131         dev_warn(dev, "can't add GPIO ACPI mapping\n");
0132 
0133     ret = acpi_dev_gpio_irq_get_by(ACPI_COMPANION(dev), "irq-gpios", 0);
0134     if (ret < 0)
0135         return ret;
0136 
0137     dev_info(dev, "ACPI interrupt quirk (IRQ %d)\n", ret);
0138     return ret;
0139 }
0140 
0141 static const struct dmi_system_id pca953x_dmi_acpi_irq_info[] = {
0142     {
0143         /*
0144          * On Intel Galileo Gen 2 board the IRQ pin of one of
0145          * the I²C GPIO expanders, which has GpioInt() resource,
0146          * is provided as an absolute number instead of being
0147          * relative. Since first controller (gpio-sch.c) and
0148          * second (gpio-dwapb.c) are at the fixed bases, we may
0149          * safely refer to the number in the global space to get
0150          * an IRQ out of it.
0151          */
0152         .matches = {
0153             DMI_EXACT_MATCH(DMI_BOARD_NAME, "GalileoGen2"),
0154         },
0155     },
0156     {}
0157 };
0158 #endif
0159 
0160 static const struct acpi_device_id pca953x_acpi_ids[] = {
0161     { "INT3491", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
0162     { }
0163 };
0164 MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids);
0165 
0166 #define MAX_BANK 5
0167 #define BANK_SZ 8
0168 #define MAX_LINE    (MAX_BANK * BANK_SZ)
0169 
0170 #define NBANK(chip) DIV_ROUND_UP(chip->gpio_chip.ngpio, BANK_SZ)
0171 
0172 struct pca953x_reg_config {
0173     int direction;
0174     int output;
0175     int input;
0176     int invert;
0177 };
0178 
0179 static const struct pca953x_reg_config pca953x_regs = {
0180     .direction = PCA953X_DIRECTION,
0181     .output = PCA953X_OUTPUT,
0182     .input = PCA953X_INPUT,
0183     .invert = PCA953X_INVERT,
0184 };
0185 
0186 static const struct pca953x_reg_config pca957x_regs = {
0187     .direction = PCA957X_CFG,
0188     .output = PCA957X_OUT,
0189     .input = PCA957X_IN,
0190     .invert = PCA957X_INVRT,
0191 };
0192 
0193 struct pca953x_chip {
0194     unsigned gpio_start;
0195     struct mutex i2c_lock;
0196     struct regmap *regmap;
0197 
0198 #ifdef CONFIG_GPIO_PCA953X_IRQ
0199     struct mutex irq_lock;
0200     DECLARE_BITMAP(irq_mask, MAX_LINE);
0201     DECLARE_BITMAP(irq_stat, MAX_LINE);
0202     DECLARE_BITMAP(irq_trig_raise, MAX_LINE);
0203     DECLARE_BITMAP(irq_trig_fall, MAX_LINE);
0204 #endif
0205     atomic_t wakeup_path;
0206 
0207     struct i2c_client *client;
0208     struct gpio_chip gpio_chip;
0209     const char *const *names;
0210     unsigned long driver_data;
0211     struct regulator *regulator;
0212 
0213     const struct pca953x_reg_config *regs;
0214 };
0215 
0216 static int pca953x_bank_shift(struct pca953x_chip *chip)
0217 {
0218     return fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
0219 }
0220 
0221 #define PCA953x_BANK_INPUT  BIT(0)
0222 #define PCA953x_BANK_OUTPUT BIT(1)
0223 #define PCA953x_BANK_POLARITY   BIT(2)
0224 #define PCA953x_BANK_CONFIG BIT(3)
0225 
0226 #define PCA957x_BANK_INPUT  BIT(0)
0227 #define PCA957x_BANK_POLARITY   BIT(1)
0228 #define PCA957x_BANK_BUSHOLD    BIT(2)
0229 #define PCA957x_BANK_CONFIG BIT(4)
0230 #define PCA957x_BANK_OUTPUT BIT(5)
0231 
0232 #define PCAL9xxx_BANK_IN_LATCH  BIT(8 + 2)
0233 #define PCAL9xxx_BANK_PULL_EN   BIT(8 + 3)
0234 #define PCAL9xxx_BANK_PULL_SEL  BIT(8 + 4)
0235 #define PCAL9xxx_BANK_IRQ_MASK  BIT(8 + 5)
0236 #define PCAL9xxx_BANK_IRQ_STAT  BIT(8 + 6)
0237 
0238 /*
0239  * We care about the following registers:
0240  * - Standard set, below 0x40, each port can be replicated up to 8 times
0241  *   - PCA953x standard
0242  *     Input port           0x00 + 0 * bank_size    R
0243  *     Output port          0x00 + 1 * bank_size    RW
0244  *     Polarity Inversion port      0x00 + 2 * bank_size    RW
0245  *     Configuration port       0x00 + 3 * bank_size    RW
0246  *   - PCA957x with mixed up registers
0247  *     Input port           0x00 + 0 * bank_size    R
0248  *     Polarity Inversion port      0x00 + 1 * bank_size    RW
0249  *     Bus hold port            0x00 + 2 * bank_size    RW
0250  *     Configuration port       0x00 + 4 * bank_size    RW
0251  *     Output port          0x00 + 5 * bank_size    RW
0252  *
0253  * - Extended set, above 0x40, often chip specific.
0254  *   - PCAL6524/PCAL9555A with custom PCAL IRQ handling:
0255  *     Input latch register     0x40 + 2 * bank_size    RW
0256  *     Pull-up/pull-down enable reg 0x40 + 3 * bank_size    RW
0257  *     Pull-up/pull-down select reg 0x40 + 4 * bank_size    RW
0258  *     Interrupt mask register      0x40 + 5 * bank_size    RW
0259  *     Interrupt status register    0x40 + 6 * bank_size    R
0260  *
0261  * - Registers with bit 0x80 set, the AI bit
0262  *   The bit is cleared and the registers fall into one of the
0263  *   categories above.
0264  */
0265 
0266 static bool pca953x_check_register(struct pca953x_chip *chip, unsigned int reg,
0267                    u32 checkbank)
0268 {
0269     int bank_shift = pca953x_bank_shift(chip);
0270     int bank = (reg & REG_ADDR_MASK) >> bank_shift;
0271     int offset = reg & (BIT(bank_shift) - 1);
0272 
0273     /* Special PCAL extended register check. */
0274     if (reg & REG_ADDR_EXT) {
0275         if (!(chip->driver_data & PCA_PCAL))
0276             return false;
0277         bank += 8;
0278     }
0279 
0280     /* Register is not in the matching bank. */
0281     if (!(BIT(bank) & checkbank))
0282         return false;
0283 
0284     /* Register is not within allowed range of bank. */
0285     if (offset >= NBANK(chip))
0286         return false;
0287 
0288     return true;
0289 }
0290 
0291 static bool pca953x_readable_register(struct device *dev, unsigned int reg)
0292 {
0293     struct pca953x_chip *chip = dev_get_drvdata(dev);
0294     u32 bank;
0295 
0296     if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE) {
0297         bank = PCA953x_BANK_INPUT | PCA953x_BANK_OUTPUT |
0298                PCA953x_BANK_POLARITY | PCA953x_BANK_CONFIG;
0299     } else {
0300         bank = PCA957x_BANK_INPUT | PCA957x_BANK_OUTPUT |
0301                PCA957x_BANK_POLARITY | PCA957x_BANK_CONFIG |
0302                PCA957x_BANK_BUSHOLD;
0303     }
0304 
0305     if (chip->driver_data & PCA_PCAL) {
0306         bank |= PCAL9xxx_BANK_IN_LATCH | PCAL9xxx_BANK_PULL_EN |
0307             PCAL9xxx_BANK_PULL_SEL | PCAL9xxx_BANK_IRQ_MASK |
0308             PCAL9xxx_BANK_IRQ_STAT;
0309     }
0310 
0311     return pca953x_check_register(chip, reg, bank);
0312 }
0313 
0314 static bool pca953x_writeable_register(struct device *dev, unsigned int reg)
0315 {
0316     struct pca953x_chip *chip = dev_get_drvdata(dev);
0317     u32 bank;
0318 
0319     if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE) {
0320         bank = PCA953x_BANK_OUTPUT | PCA953x_BANK_POLARITY |
0321             PCA953x_BANK_CONFIG;
0322     } else {
0323         bank = PCA957x_BANK_OUTPUT | PCA957x_BANK_POLARITY |
0324             PCA957x_BANK_CONFIG | PCA957x_BANK_BUSHOLD;
0325     }
0326 
0327     if (chip->driver_data & PCA_PCAL)
0328         bank |= PCAL9xxx_BANK_IN_LATCH | PCAL9xxx_BANK_PULL_EN |
0329             PCAL9xxx_BANK_PULL_SEL | PCAL9xxx_BANK_IRQ_MASK;
0330 
0331     return pca953x_check_register(chip, reg, bank);
0332 }
0333 
0334 static bool pca953x_volatile_register(struct device *dev, unsigned int reg)
0335 {
0336     struct pca953x_chip *chip = dev_get_drvdata(dev);
0337     u32 bank;
0338 
0339     if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE)
0340         bank = PCA953x_BANK_INPUT;
0341     else
0342         bank = PCA957x_BANK_INPUT;
0343 
0344     if (chip->driver_data & PCA_PCAL)
0345         bank |= PCAL9xxx_BANK_IRQ_STAT;
0346 
0347     return pca953x_check_register(chip, reg, bank);
0348 }
0349 
0350 static const struct regmap_config pca953x_i2c_regmap = {
0351     .reg_bits = 8,
0352     .val_bits = 8,
0353 
0354     .use_single_read = true,
0355     .use_single_write = true,
0356 
0357     .readable_reg = pca953x_readable_register,
0358     .writeable_reg = pca953x_writeable_register,
0359     .volatile_reg = pca953x_volatile_register,
0360 
0361     .disable_locking = true,
0362     .cache_type = REGCACHE_RBTREE,
0363     .max_register = 0x7f,
0364 };
0365 
0366 static const struct regmap_config pca953x_ai_i2c_regmap = {
0367     .reg_bits = 8,
0368     .val_bits = 8,
0369 
0370     .read_flag_mask = REG_ADDR_AI,
0371     .write_flag_mask = REG_ADDR_AI,
0372 
0373     .readable_reg = pca953x_readable_register,
0374     .writeable_reg = pca953x_writeable_register,
0375     .volatile_reg = pca953x_volatile_register,
0376 
0377     .disable_locking = true,
0378     .cache_type = REGCACHE_RBTREE,
0379     .max_register = 0x7f,
0380 };
0381 
0382 static u8 pca953x_recalc_addr(struct pca953x_chip *chip, int reg, int off)
0383 {
0384     int bank_shift = pca953x_bank_shift(chip);
0385     int addr = (reg & PCAL_GPIO_MASK) << bank_shift;
0386     int pinctrl = (reg & PCAL_PINCTRL_MASK) << 1;
0387     u8 regaddr = pinctrl | addr | (off / BANK_SZ);
0388 
0389     return regaddr;
0390 }
0391 
0392 static int pca953x_write_regs(struct pca953x_chip *chip, int reg, unsigned long *val)
0393 {
0394     u8 regaddr = pca953x_recalc_addr(chip, reg, 0);
0395     u8 value[MAX_BANK];
0396     int i, ret;
0397 
0398     for (i = 0; i < NBANK(chip); i++)
0399         value[i] = bitmap_get_value8(val, i * BANK_SZ);
0400 
0401     ret = regmap_bulk_write(chip->regmap, regaddr, value, NBANK(chip));
0402     if (ret < 0) {
0403         dev_err(&chip->client->dev, "failed writing register\n");
0404         return ret;
0405     }
0406 
0407     return 0;
0408 }
0409 
0410 static int pca953x_read_regs(struct pca953x_chip *chip, int reg, unsigned long *val)
0411 {
0412     u8 regaddr = pca953x_recalc_addr(chip, reg, 0);
0413     u8 value[MAX_BANK];
0414     int i, ret;
0415 
0416     ret = regmap_bulk_read(chip->regmap, regaddr, value, NBANK(chip));
0417     if (ret < 0) {
0418         dev_err(&chip->client->dev, "failed reading register\n");
0419         return ret;
0420     }
0421 
0422     for (i = 0; i < NBANK(chip); i++)
0423         bitmap_set_value8(val, value[i], i * BANK_SZ);
0424 
0425     return 0;
0426 }
0427 
0428 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
0429 {
0430     struct pca953x_chip *chip = gpiochip_get_data(gc);
0431     u8 dirreg = pca953x_recalc_addr(chip, chip->regs->direction, off);
0432     u8 bit = BIT(off % BANK_SZ);
0433     int ret;
0434 
0435     mutex_lock(&chip->i2c_lock);
0436     ret = regmap_write_bits(chip->regmap, dirreg, bit, bit);
0437     mutex_unlock(&chip->i2c_lock);
0438     return ret;
0439 }
0440 
0441 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
0442         unsigned off, int val)
0443 {
0444     struct pca953x_chip *chip = gpiochip_get_data(gc);
0445     u8 dirreg = pca953x_recalc_addr(chip, chip->regs->direction, off);
0446     u8 outreg = pca953x_recalc_addr(chip, chip->regs->output, off);
0447     u8 bit = BIT(off % BANK_SZ);
0448     int ret;
0449 
0450     mutex_lock(&chip->i2c_lock);
0451     /* set output level */
0452     ret = regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0);
0453     if (ret)
0454         goto exit;
0455 
0456     /* then direction */
0457     ret = regmap_write_bits(chip->regmap, dirreg, bit, 0);
0458 exit:
0459     mutex_unlock(&chip->i2c_lock);
0460     return ret;
0461 }
0462 
0463 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
0464 {
0465     struct pca953x_chip *chip = gpiochip_get_data(gc);
0466     u8 inreg = pca953x_recalc_addr(chip, chip->regs->input, off);
0467     u8 bit = BIT(off % BANK_SZ);
0468     u32 reg_val;
0469     int ret;
0470 
0471     mutex_lock(&chip->i2c_lock);
0472     ret = regmap_read(chip->regmap, inreg, &reg_val);
0473     mutex_unlock(&chip->i2c_lock);
0474     if (ret < 0)
0475         return ret;
0476 
0477     return !!(reg_val & bit);
0478 }
0479 
0480 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
0481 {
0482     struct pca953x_chip *chip = gpiochip_get_data(gc);
0483     u8 outreg = pca953x_recalc_addr(chip, chip->regs->output, off);
0484     u8 bit = BIT(off % BANK_SZ);
0485 
0486     mutex_lock(&chip->i2c_lock);
0487     regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0);
0488     mutex_unlock(&chip->i2c_lock);
0489 }
0490 
0491 static int pca953x_gpio_get_direction(struct gpio_chip *gc, unsigned off)
0492 {
0493     struct pca953x_chip *chip = gpiochip_get_data(gc);
0494     u8 dirreg = pca953x_recalc_addr(chip, chip->regs->direction, off);
0495     u8 bit = BIT(off % BANK_SZ);
0496     u32 reg_val;
0497     int ret;
0498 
0499     mutex_lock(&chip->i2c_lock);
0500     ret = regmap_read(chip->regmap, dirreg, &reg_val);
0501     mutex_unlock(&chip->i2c_lock);
0502     if (ret < 0)
0503         return ret;
0504 
0505     if (reg_val & bit)
0506         return GPIO_LINE_DIRECTION_IN;
0507 
0508     return GPIO_LINE_DIRECTION_OUT;
0509 }
0510 
0511 static int pca953x_gpio_get_multiple(struct gpio_chip *gc,
0512                      unsigned long *mask, unsigned long *bits)
0513 {
0514     struct pca953x_chip *chip = gpiochip_get_data(gc);
0515     DECLARE_BITMAP(reg_val, MAX_LINE);
0516     int ret;
0517 
0518     mutex_lock(&chip->i2c_lock);
0519     ret = pca953x_read_regs(chip, chip->regs->input, reg_val);
0520     mutex_unlock(&chip->i2c_lock);
0521     if (ret)
0522         return ret;
0523 
0524     bitmap_replace(bits, bits, reg_val, mask, gc->ngpio);
0525     return 0;
0526 }
0527 
0528 static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
0529                       unsigned long *mask, unsigned long *bits)
0530 {
0531     struct pca953x_chip *chip = gpiochip_get_data(gc);
0532     DECLARE_BITMAP(reg_val, MAX_LINE);
0533     int ret;
0534 
0535     mutex_lock(&chip->i2c_lock);
0536     ret = pca953x_read_regs(chip, chip->regs->output, reg_val);
0537     if (ret)
0538         goto exit;
0539 
0540     bitmap_replace(reg_val, reg_val, bits, mask, gc->ngpio);
0541 
0542     pca953x_write_regs(chip, chip->regs->output, reg_val);
0543 exit:
0544     mutex_unlock(&chip->i2c_lock);
0545 }
0546 
0547 static int pca953x_gpio_set_pull_up_down(struct pca953x_chip *chip,
0548                      unsigned int offset,
0549                      unsigned long config)
0550 {
0551     u8 pull_en_reg = pca953x_recalc_addr(chip, PCAL953X_PULL_EN, offset);
0552     u8 pull_sel_reg = pca953x_recalc_addr(chip, PCAL953X_PULL_SEL, offset);
0553     u8 bit = BIT(offset % BANK_SZ);
0554     int ret;
0555 
0556     /*
0557      * pull-up/pull-down configuration requires PCAL extended
0558      * registers
0559      */
0560     if (!(chip->driver_data & PCA_PCAL))
0561         return -ENOTSUPP;
0562 
0563     mutex_lock(&chip->i2c_lock);
0564 
0565     /* Configure pull-up/pull-down */
0566     if (config == PIN_CONFIG_BIAS_PULL_UP)
0567         ret = regmap_write_bits(chip->regmap, pull_sel_reg, bit, bit);
0568     else if (config == PIN_CONFIG_BIAS_PULL_DOWN)
0569         ret = regmap_write_bits(chip->regmap, pull_sel_reg, bit, 0);
0570     else
0571         ret = 0;
0572     if (ret)
0573         goto exit;
0574 
0575     /* Disable/Enable pull-up/pull-down */
0576     if (config == PIN_CONFIG_BIAS_DISABLE)
0577         ret = regmap_write_bits(chip->regmap, pull_en_reg, bit, 0);
0578     else
0579         ret = regmap_write_bits(chip->regmap, pull_en_reg, bit, bit);
0580 
0581 exit:
0582     mutex_unlock(&chip->i2c_lock);
0583     return ret;
0584 }
0585 
0586 static int pca953x_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
0587                    unsigned long config)
0588 {
0589     struct pca953x_chip *chip = gpiochip_get_data(gc);
0590 
0591     switch (pinconf_to_config_param(config)) {
0592     case PIN_CONFIG_BIAS_PULL_UP:
0593     case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
0594     case PIN_CONFIG_BIAS_PULL_DOWN:
0595     case PIN_CONFIG_BIAS_DISABLE:
0596         return pca953x_gpio_set_pull_up_down(chip, offset, config);
0597     default:
0598         return -ENOTSUPP;
0599     }
0600 }
0601 
0602 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
0603 {
0604     struct gpio_chip *gc;
0605 
0606     gc = &chip->gpio_chip;
0607 
0608     gc->direction_input  = pca953x_gpio_direction_input;
0609     gc->direction_output = pca953x_gpio_direction_output;
0610     gc->get = pca953x_gpio_get_value;
0611     gc->set = pca953x_gpio_set_value;
0612     gc->get_direction = pca953x_gpio_get_direction;
0613     gc->get_multiple = pca953x_gpio_get_multiple;
0614     gc->set_multiple = pca953x_gpio_set_multiple;
0615     gc->set_config = pca953x_gpio_set_config;
0616     gc->can_sleep = true;
0617 
0618     gc->base = chip->gpio_start;
0619     gc->ngpio = gpios;
0620     gc->label = dev_name(&chip->client->dev);
0621     gc->parent = &chip->client->dev;
0622     gc->owner = THIS_MODULE;
0623     gc->names = chip->names;
0624 }
0625 
0626 #ifdef CONFIG_GPIO_PCA953X_IRQ
0627 static void pca953x_irq_mask(struct irq_data *d)
0628 {
0629     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0630     struct pca953x_chip *chip = gpiochip_get_data(gc);
0631     irq_hw_number_t hwirq = irqd_to_hwirq(d);
0632 
0633     clear_bit(hwirq, chip->irq_mask);
0634     gpiochip_disable_irq(gc, hwirq);
0635 }
0636 
0637 static void pca953x_irq_unmask(struct irq_data *d)
0638 {
0639     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0640     struct pca953x_chip *chip = gpiochip_get_data(gc);
0641     irq_hw_number_t hwirq = irqd_to_hwirq(d);
0642 
0643     gpiochip_enable_irq(gc, hwirq);
0644     set_bit(hwirq, chip->irq_mask);
0645 }
0646 
0647 static int pca953x_irq_set_wake(struct irq_data *d, unsigned int on)
0648 {
0649     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0650     struct pca953x_chip *chip = gpiochip_get_data(gc);
0651 
0652     if (on)
0653         atomic_inc(&chip->wakeup_path);
0654     else
0655         atomic_dec(&chip->wakeup_path);
0656 
0657     return irq_set_irq_wake(chip->client->irq, on);
0658 }
0659 
0660 static void pca953x_irq_bus_lock(struct irq_data *d)
0661 {
0662     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0663     struct pca953x_chip *chip = gpiochip_get_data(gc);
0664 
0665     mutex_lock(&chip->irq_lock);
0666 }
0667 
0668 static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
0669 {
0670     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0671     struct pca953x_chip *chip = gpiochip_get_data(gc);
0672     DECLARE_BITMAP(irq_mask, MAX_LINE);
0673     DECLARE_BITMAP(reg_direction, MAX_LINE);
0674     int level;
0675 
0676     if (chip->driver_data & PCA_PCAL) {
0677         /* Enable latch on interrupt-enabled inputs */
0678         pca953x_write_regs(chip, PCAL953X_IN_LATCH, chip->irq_mask);
0679 
0680         bitmap_complement(irq_mask, chip->irq_mask, gc->ngpio);
0681 
0682         /* Unmask enabled interrupts */
0683         pca953x_write_regs(chip, PCAL953X_INT_MASK, irq_mask);
0684     }
0685 
0686     /* Switch direction to input if needed */
0687     pca953x_read_regs(chip, chip->regs->direction, reg_direction);
0688 
0689     bitmap_or(irq_mask, chip->irq_trig_fall, chip->irq_trig_raise, gc->ngpio);
0690     bitmap_complement(reg_direction, reg_direction, gc->ngpio);
0691     bitmap_and(irq_mask, irq_mask, reg_direction, gc->ngpio);
0692 
0693     /* Look for any newly setup interrupt */
0694     for_each_set_bit(level, irq_mask, gc->ngpio)
0695         pca953x_gpio_direction_input(&chip->gpio_chip, level);
0696 
0697     mutex_unlock(&chip->irq_lock);
0698 }
0699 
0700 static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
0701 {
0702     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0703     struct pca953x_chip *chip = gpiochip_get_data(gc);
0704     irq_hw_number_t hwirq = irqd_to_hwirq(d);
0705 
0706     if (!(type & IRQ_TYPE_EDGE_BOTH)) {
0707         dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
0708             d->irq, type);
0709         return -EINVAL;
0710     }
0711 
0712     assign_bit(hwirq, chip->irq_trig_fall, type & IRQ_TYPE_EDGE_FALLING);
0713     assign_bit(hwirq, chip->irq_trig_raise, type & IRQ_TYPE_EDGE_RISING);
0714 
0715     return 0;
0716 }
0717 
0718 static void pca953x_irq_shutdown(struct irq_data *d)
0719 {
0720     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0721     struct pca953x_chip *chip = gpiochip_get_data(gc);
0722     irq_hw_number_t hwirq = irqd_to_hwirq(d);
0723 
0724     clear_bit(hwirq, chip->irq_trig_raise);
0725     clear_bit(hwirq, chip->irq_trig_fall);
0726 }
0727 
0728 static void pca953x_irq_print_chip(struct irq_data *data, struct seq_file *p)
0729 {
0730     struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
0731 
0732     seq_printf(p, dev_name(gc->parent));
0733 }
0734 
0735 static const struct irq_chip pca953x_irq_chip = {
0736     .irq_mask       = pca953x_irq_mask,
0737     .irq_unmask     = pca953x_irq_unmask,
0738     .irq_set_wake       = pca953x_irq_set_wake,
0739     .irq_bus_lock       = pca953x_irq_bus_lock,
0740     .irq_bus_sync_unlock    = pca953x_irq_bus_sync_unlock,
0741     .irq_set_type       = pca953x_irq_set_type,
0742     .irq_shutdown       = pca953x_irq_shutdown,
0743     .irq_print_chip     = pca953x_irq_print_chip,
0744     .flags          = IRQCHIP_IMMUTABLE,
0745     GPIOCHIP_IRQ_RESOURCE_HELPERS,
0746 };
0747 
0748 static bool pca953x_irq_pending(struct pca953x_chip *chip, unsigned long *pending)
0749 {
0750     struct gpio_chip *gc = &chip->gpio_chip;
0751     DECLARE_BITMAP(reg_direction, MAX_LINE);
0752     DECLARE_BITMAP(old_stat, MAX_LINE);
0753     DECLARE_BITMAP(cur_stat, MAX_LINE);
0754     DECLARE_BITMAP(new_stat, MAX_LINE);
0755     DECLARE_BITMAP(trigger, MAX_LINE);
0756     int ret;
0757 
0758     if (chip->driver_data & PCA_PCAL) {
0759         /* Read the current interrupt status from the device */
0760         ret = pca953x_read_regs(chip, PCAL953X_INT_STAT, trigger);
0761         if (ret)
0762             return false;
0763 
0764         /* Check latched inputs and clear interrupt status */
0765         ret = pca953x_read_regs(chip, chip->regs->input, cur_stat);
0766         if (ret)
0767             return false;
0768 
0769         /* Apply filter for rising/falling edge selection */
0770         bitmap_replace(new_stat, chip->irq_trig_fall, chip->irq_trig_raise, cur_stat, gc->ngpio);
0771 
0772         bitmap_and(pending, new_stat, trigger, gc->ngpio);
0773 
0774         return !bitmap_empty(pending, gc->ngpio);
0775     }
0776 
0777     ret = pca953x_read_regs(chip, chip->regs->input, cur_stat);
0778     if (ret)
0779         return false;
0780 
0781     /* Remove output pins from the equation */
0782     pca953x_read_regs(chip, chip->regs->direction, reg_direction);
0783 
0784     bitmap_copy(old_stat, chip->irq_stat, gc->ngpio);
0785 
0786     bitmap_and(new_stat, cur_stat, reg_direction, gc->ngpio);
0787     bitmap_xor(cur_stat, new_stat, old_stat, gc->ngpio);
0788     bitmap_and(trigger, cur_stat, chip->irq_mask, gc->ngpio);
0789 
0790     bitmap_copy(chip->irq_stat, new_stat, gc->ngpio);
0791 
0792     if (bitmap_empty(trigger, gc->ngpio))
0793         return false;
0794 
0795     bitmap_and(cur_stat, chip->irq_trig_fall, old_stat, gc->ngpio);
0796     bitmap_and(old_stat, chip->irq_trig_raise, new_stat, gc->ngpio);
0797     bitmap_or(new_stat, old_stat, cur_stat, gc->ngpio);
0798     bitmap_and(pending, new_stat, trigger, gc->ngpio);
0799 
0800     return !bitmap_empty(pending, gc->ngpio);
0801 }
0802 
0803 static irqreturn_t pca953x_irq_handler(int irq, void *devid)
0804 {
0805     struct pca953x_chip *chip = devid;
0806     struct gpio_chip *gc = &chip->gpio_chip;
0807     DECLARE_BITMAP(pending, MAX_LINE);
0808     int level;
0809     bool ret;
0810 
0811     bitmap_zero(pending, MAX_LINE);
0812 
0813     mutex_lock(&chip->i2c_lock);
0814     ret = pca953x_irq_pending(chip, pending);
0815     mutex_unlock(&chip->i2c_lock);
0816 
0817     if (ret) {
0818         ret = 0;
0819 
0820         for_each_set_bit(level, pending, gc->ngpio) {
0821             int nested_irq = irq_find_mapping(gc->irq.domain, level);
0822 
0823             if (unlikely(nested_irq <= 0)) {
0824                 dev_warn_ratelimited(gc->parent, "unmapped interrupt %d\n", level);
0825                 continue;
0826             }
0827 
0828             handle_nested_irq(nested_irq);
0829             ret = 1;
0830         }
0831     }
0832 
0833     return IRQ_RETVAL(ret);
0834 }
0835 
0836 static int pca953x_irq_setup(struct pca953x_chip *chip, int irq_base)
0837 {
0838     struct i2c_client *client = chip->client;
0839     DECLARE_BITMAP(reg_direction, MAX_LINE);
0840     DECLARE_BITMAP(irq_stat, MAX_LINE);
0841     struct gpio_irq_chip *girq;
0842     int ret;
0843 
0844     if (dmi_first_match(pca953x_dmi_acpi_irq_info)) {
0845         ret = pca953x_acpi_get_irq(&client->dev);
0846         if (ret > 0)
0847             client->irq = ret;
0848     }
0849 
0850     if (!client->irq)
0851         return 0;
0852 
0853     if (irq_base == -1)
0854         return 0;
0855 
0856     if (!(chip->driver_data & PCA_INT))
0857         return 0;
0858 
0859     ret = pca953x_read_regs(chip, chip->regs->input, irq_stat);
0860     if (ret)
0861         return ret;
0862 
0863     /*
0864      * There is no way to know which GPIO line generated the
0865      * interrupt.  We have to rely on the previous read for
0866      * this purpose.
0867      */
0868     pca953x_read_regs(chip, chip->regs->direction, reg_direction);
0869     bitmap_and(chip->irq_stat, irq_stat, reg_direction, chip->gpio_chip.ngpio);
0870     mutex_init(&chip->irq_lock);
0871 
0872     girq = &chip->gpio_chip.irq;
0873     gpio_irq_chip_set_chip(girq, &pca953x_irq_chip);
0874     /* This will let us handle the parent IRQ in the driver */
0875     girq->parent_handler = NULL;
0876     girq->num_parents = 0;
0877     girq->parents = NULL;
0878     girq->default_type = IRQ_TYPE_NONE;
0879     girq->handler = handle_simple_irq;
0880     girq->threaded = true;
0881     girq->first = irq_base; /* FIXME: get rid of this */
0882 
0883     ret = devm_request_threaded_irq(&client->dev, client->irq,
0884                     NULL, pca953x_irq_handler,
0885                     IRQF_ONESHOT | IRQF_SHARED,
0886                     dev_name(&client->dev), chip);
0887     if (ret) {
0888         dev_err(&client->dev, "failed to request irq %d\n",
0889             client->irq);
0890         return ret;
0891     }
0892 
0893     return 0;
0894 }
0895 
0896 #else /* CONFIG_GPIO_PCA953X_IRQ */
0897 static int pca953x_irq_setup(struct pca953x_chip *chip,
0898                  int irq_base)
0899 {
0900     struct i2c_client *client = chip->client;
0901 
0902     if (client->irq && irq_base != -1 && (chip->driver_data & PCA_INT))
0903         dev_warn(&client->dev, "interrupt support not compiled in\n");
0904 
0905     return 0;
0906 }
0907 #endif
0908 
0909 static int device_pca95xx_init(struct pca953x_chip *chip, u32 invert)
0910 {
0911     DECLARE_BITMAP(val, MAX_LINE);
0912     u8 regaddr;
0913     int ret;
0914 
0915     regaddr = pca953x_recalc_addr(chip, chip->regs->output, 0);
0916     ret = regcache_sync_region(chip->regmap, regaddr,
0917                    regaddr + NBANK(chip) - 1);
0918     if (ret)
0919         goto out;
0920 
0921     regaddr = pca953x_recalc_addr(chip, chip->regs->direction, 0);
0922     ret = regcache_sync_region(chip->regmap, regaddr,
0923                    regaddr + NBANK(chip) - 1);
0924     if (ret)
0925         goto out;
0926 
0927     /* set platform specific polarity inversion */
0928     if (invert)
0929         bitmap_fill(val, MAX_LINE);
0930     else
0931         bitmap_zero(val, MAX_LINE);
0932 
0933     ret = pca953x_write_regs(chip, chip->regs->invert, val);
0934 out:
0935     return ret;
0936 }
0937 
0938 static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
0939 {
0940     DECLARE_BITMAP(val, MAX_LINE);
0941     unsigned int i;
0942     int ret;
0943 
0944     ret = device_pca95xx_init(chip, invert);
0945     if (ret)
0946         goto out;
0947 
0948     /* To enable register 6, 7 to control pull up and pull down */
0949     for (i = 0; i < NBANK(chip); i++)
0950         bitmap_set_value8(val, 0x02, i * BANK_SZ);
0951 
0952     ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
0953     if (ret)
0954         goto out;
0955 
0956     return 0;
0957 out:
0958     return ret;
0959 }
0960 
0961 static int pca953x_probe(struct i2c_client *client,
0962              const struct i2c_device_id *i2c_id)
0963 {
0964     struct pca953x_platform_data *pdata;
0965     struct pca953x_chip *chip;
0966     int irq_base = 0;
0967     int ret;
0968     u32 invert = 0;
0969     struct regulator *reg;
0970     const struct regmap_config *regmap_config;
0971 
0972     chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
0973     if (chip == NULL)
0974         return -ENOMEM;
0975 
0976     pdata = dev_get_platdata(&client->dev);
0977     if (pdata) {
0978         irq_base = pdata->irq_base;
0979         chip->gpio_start = pdata->gpio_base;
0980         invert = pdata->invert;
0981         chip->names = pdata->names;
0982     } else {
0983         struct gpio_desc *reset_gpio;
0984 
0985         chip->gpio_start = -1;
0986         irq_base = 0;
0987 
0988         /*
0989          * See if we need to de-assert a reset pin.
0990          *
0991          * There is no known ACPI-enabled platforms that are
0992          * using "reset" GPIO. Otherwise any of those platform
0993          * must use _DSD method with corresponding property.
0994          */
0995         reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
0996                              GPIOD_OUT_LOW);
0997         if (IS_ERR(reset_gpio))
0998             return PTR_ERR(reset_gpio);
0999     }
1000 
1001     chip->client = client;
1002 
1003     reg = devm_regulator_get(&client->dev, "vcc");
1004     if (IS_ERR(reg))
1005         return dev_err_probe(&client->dev, PTR_ERR(reg), "reg get err\n");
1006 
1007     ret = regulator_enable(reg);
1008     if (ret) {
1009         dev_err(&client->dev, "reg en err: %d\n", ret);
1010         return ret;
1011     }
1012     chip->regulator = reg;
1013 
1014     if (i2c_id) {
1015         chip->driver_data = i2c_id->driver_data;
1016     } else {
1017         const void *match;
1018 
1019         match = device_get_match_data(&client->dev);
1020         if (!match) {
1021             ret = -ENODEV;
1022             goto err_exit;
1023         }
1024 
1025         chip->driver_data = (uintptr_t)match;
1026     }
1027 
1028     i2c_set_clientdata(client, chip);
1029 
1030     pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
1031 
1032     if (NBANK(chip) > 2 || PCA_CHIP_TYPE(chip->driver_data) == PCA957X_TYPE) {
1033         dev_info(&client->dev, "using AI\n");
1034         regmap_config = &pca953x_ai_i2c_regmap;
1035     } else {
1036         dev_info(&client->dev, "using no AI\n");
1037         regmap_config = &pca953x_i2c_regmap;
1038     }
1039 
1040     chip->regmap = devm_regmap_init_i2c(client, regmap_config);
1041     if (IS_ERR(chip->regmap)) {
1042         ret = PTR_ERR(chip->regmap);
1043         goto err_exit;
1044     }
1045 
1046     regcache_mark_dirty(chip->regmap);
1047 
1048     mutex_init(&chip->i2c_lock);
1049     /*
1050      * In case we have an i2c-mux controlled by a GPIO provided by an
1051      * expander using the same driver higher on the device tree, read the
1052      * i2c adapter nesting depth and use the retrieved value as lockdep
1053      * subclass for chip->i2c_lock.
1054      *
1055      * REVISIT: This solution is not complete. It protects us from lockdep
1056      * false positives when the expander controlling the i2c-mux is on
1057      * a different level on the device tree, but not when it's on the same
1058      * level on a different branch (in which case the subclass number
1059      * would be the same).
1060      *
1061      * TODO: Once a correct solution is developed, a similar fix should be
1062      * applied to all other i2c-controlled GPIO expanders (and potentially
1063      * regmap-i2c).
1064      */
1065     lockdep_set_subclass(&chip->i2c_lock,
1066                  i2c_adapter_depth(client->adapter));
1067 
1068     /* initialize cached registers from their original values.
1069      * we can't share this chip with another i2c master.
1070      */
1071 
1072     if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE) {
1073         chip->regs = &pca953x_regs;
1074         ret = device_pca95xx_init(chip, invert);
1075     } else {
1076         chip->regs = &pca957x_regs;
1077         ret = device_pca957x_init(chip, invert);
1078     }
1079     if (ret)
1080         goto err_exit;
1081 
1082     ret = pca953x_irq_setup(chip, irq_base);
1083     if (ret)
1084         goto err_exit;
1085 
1086     ret = devm_gpiochip_add_data(&client->dev, &chip->gpio_chip, chip);
1087     if (ret)
1088         goto err_exit;
1089 
1090     if (pdata && pdata->setup) {
1091         ret = pdata->setup(client, chip->gpio_chip.base,
1092                    chip->gpio_chip.ngpio, pdata->context);
1093         if (ret < 0)
1094             dev_warn(&client->dev, "setup failed, %d\n", ret);
1095     }
1096 
1097     return 0;
1098 
1099 err_exit:
1100     regulator_disable(chip->regulator);
1101     return ret;
1102 }
1103 
1104 static int pca953x_remove(struct i2c_client *client)
1105 {
1106     struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
1107     struct pca953x_chip *chip = i2c_get_clientdata(client);
1108     int ret;
1109 
1110     if (pdata && pdata->teardown) {
1111         ret = pdata->teardown(client, chip->gpio_chip.base,
1112                       chip->gpio_chip.ngpio, pdata->context);
1113         if (ret < 0)
1114             dev_err(&client->dev, "teardown failed, %d\n", ret);
1115     } else {
1116         ret = 0;
1117     }
1118 
1119     regulator_disable(chip->regulator);
1120 
1121     return ret;
1122 }
1123 
1124 #ifdef CONFIG_PM_SLEEP
1125 static int pca953x_regcache_sync(struct device *dev)
1126 {
1127     struct pca953x_chip *chip = dev_get_drvdata(dev);
1128     int ret;
1129     u8 regaddr;
1130 
1131     /*
1132      * The ordering between direction and output is important,
1133      * sync these registers first and only then sync the rest.
1134      */
1135     regaddr = pca953x_recalc_addr(chip, chip->regs->direction, 0);
1136     ret = regcache_sync_region(chip->regmap, regaddr, regaddr + NBANK(chip) - 1);
1137     if (ret) {
1138         dev_err(dev, "Failed to sync GPIO dir registers: %d\n", ret);
1139         return ret;
1140     }
1141 
1142     regaddr = pca953x_recalc_addr(chip, chip->regs->output, 0);
1143     ret = regcache_sync_region(chip->regmap, regaddr, regaddr + NBANK(chip) - 1);
1144     if (ret) {
1145         dev_err(dev, "Failed to sync GPIO out registers: %d\n", ret);
1146         return ret;
1147     }
1148 
1149 #ifdef CONFIG_GPIO_PCA953X_IRQ
1150     if (chip->driver_data & PCA_PCAL) {
1151         regaddr = pca953x_recalc_addr(chip, PCAL953X_IN_LATCH, 0);
1152         ret = regcache_sync_region(chip->regmap, regaddr,
1153                        regaddr + NBANK(chip) - 1);
1154         if (ret) {
1155             dev_err(dev, "Failed to sync INT latch registers: %d\n",
1156                 ret);
1157             return ret;
1158         }
1159 
1160         regaddr = pca953x_recalc_addr(chip, PCAL953X_INT_MASK, 0);
1161         ret = regcache_sync_region(chip->regmap, regaddr,
1162                        regaddr + NBANK(chip) - 1);
1163         if (ret) {
1164             dev_err(dev, "Failed to sync INT mask registers: %d\n",
1165                 ret);
1166             return ret;
1167         }
1168     }
1169 #endif
1170 
1171     return 0;
1172 }
1173 
1174 static int pca953x_suspend(struct device *dev)
1175 {
1176     struct pca953x_chip *chip = dev_get_drvdata(dev);
1177 
1178     mutex_lock(&chip->i2c_lock);
1179     regcache_cache_only(chip->regmap, true);
1180     mutex_unlock(&chip->i2c_lock);
1181 
1182     if (atomic_read(&chip->wakeup_path))
1183         device_set_wakeup_path(dev);
1184     else
1185         regulator_disable(chip->regulator);
1186 
1187     return 0;
1188 }
1189 
1190 static int pca953x_resume(struct device *dev)
1191 {
1192     struct pca953x_chip *chip = dev_get_drvdata(dev);
1193     int ret;
1194 
1195     if (!atomic_read(&chip->wakeup_path)) {
1196         ret = regulator_enable(chip->regulator);
1197         if (ret) {
1198             dev_err(dev, "Failed to enable regulator: %d\n", ret);
1199             return 0;
1200         }
1201     }
1202 
1203     mutex_lock(&chip->i2c_lock);
1204     regcache_cache_only(chip->regmap, false);
1205     regcache_mark_dirty(chip->regmap);
1206     ret = pca953x_regcache_sync(dev);
1207     if (ret) {
1208         mutex_unlock(&chip->i2c_lock);
1209         return ret;
1210     }
1211 
1212     ret = regcache_sync(chip->regmap);
1213     mutex_unlock(&chip->i2c_lock);
1214     if (ret) {
1215         dev_err(dev, "Failed to restore register map: %d\n", ret);
1216         return ret;
1217     }
1218 
1219     return 0;
1220 }
1221 #endif
1222 
1223 /* convenience to stop overlong match-table lines */
1224 #define OF_953X(__nrgpio, __int) (void *)(__nrgpio | PCA953X_TYPE | __int)
1225 #define OF_957X(__nrgpio, __int) (void *)(__nrgpio | PCA957X_TYPE | __int)
1226 
1227 static const struct of_device_id pca953x_dt_ids[] = {
1228     { .compatible = "nxp,pca6408", .data = OF_953X(8, PCA_INT), },
1229     { .compatible = "nxp,pca6416", .data = OF_953X(16, PCA_INT), },
1230     { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
1231     { .compatible = "nxp,pca9506", .data = OF_953X(40, PCA_INT), },
1232     { .compatible = "nxp,pca9534", .data = OF_953X( 8, PCA_INT), },
1233     { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
1234     { .compatible = "nxp,pca9536", .data = OF_953X( 4, 0), },
1235     { .compatible = "nxp,pca9537", .data = OF_953X( 4, PCA_INT), },
1236     { .compatible = "nxp,pca9538", .data = OF_953X( 8, PCA_INT), },
1237     { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
1238     { .compatible = "nxp,pca9554", .data = OF_953X( 8, PCA_INT), },
1239     { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
1240     { .compatible = "nxp,pca9556", .data = OF_953X( 8, 0), },
1241     { .compatible = "nxp,pca9557", .data = OF_953X( 8, 0), },
1242     { .compatible = "nxp,pca9574", .data = OF_957X( 8, PCA_INT), },
1243     { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
1244     { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },
1245 
1246     { .compatible = "nxp,pcal6416", .data = OF_953X(16, PCA_LATCH_INT), },
1247     { .compatible = "nxp,pcal6524", .data = OF_953X(24, PCA_LATCH_INT), },
1248     { .compatible = "nxp,pcal9535", .data = OF_953X(16, PCA_LATCH_INT), },
1249     { .compatible = "nxp,pcal9554b", .data = OF_953X( 8, PCA_LATCH_INT), },
1250     { .compatible = "nxp,pcal9555a", .data = OF_953X(16, PCA_LATCH_INT), },
1251 
1252     { .compatible = "maxim,max7310", .data = OF_953X( 8, 0), },
1253     { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
1254     { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
1255     { .compatible = "maxim,max7315", .data = OF_953X( 8, PCA_INT), },
1256     { .compatible = "maxim,max7318", .data = OF_953X(16, PCA_INT), },
1257 
1258     { .compatible = "ti,pca6107", .data = OF_953X( 8, PCA_INT), },
1259     { .compatible = "ti,pca9536", .data = OF_953X( 4, 0), },
1260     { .compatible = "ti,tca6408", .data = OF_953X( 8, PCA_INT), },
1261     { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
1262     { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
1263     { .compatible = "ti,tca9539", .data = OF_953X(16, PCA_INT), },
1264 
1265     { .compatible = "onnn,cat9554", .data = OF_953X( 8, PCA_INT), },
1266     { .compatible = "onnn,pca9654", .data = OF_953X( 8, PCA_INT), },
1267     { .compatible = "onnn,pca9655", .data = OF_953X(16, PCA_INT), },
1268 
1269     { .compatible = "exar,xra1202", .data = OF_953X( 8, 0), },
1270     { }
1271 };
1272 
1273 MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
1274 
1275 static SIMPLE_DEV_PM_OPS(pca953x_pm_ops, pca953x_suspend, pca953x_resume);
1276 
1277 static struct i2c_driver pca953x_driver = {
1278     .driver = {
1279         .name   = "pca953x",
1280         .pm = &pca953x_pm_ops,
1281         .of_match_table = pca953x_dt_ids,
1282         .acpi_match_table = pca953x_acpi_ids,
1283     },
1284     .probe      = pca953x_probe,
1285     .remove     = pca953x_remove,
1286     .id_table   = pca953x_id,
1287 };
1288 
1289 static int __init pca953x_init(void)
1290 {
1291     return i2c_add_driver(&pca953x_driver);
1292 }
1293 /* register after i2c postcore initcall and before
1294  * subsys initcalls that may rely on these GPIOs
1295  */
1296 subsys_initcall(pca953x_init);
1297 
1298 static void __exit pca953x_exit(void)
1299 {
1300     i2c_del_driver(&pca953x_driver);
1301 }
1302 module_exit(pca953x_exit);
1303 
1304 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
1305 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
1306 MODULE_LICENSE("GPL");