patches-5.15.92-rt57
[platform/kernel/linux-rpi.git] / drivers / gpio / gpio-pca953x.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  PCA953x 4/8/16/24/40 bit I/O ports
4  *
5  *  Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
6  *  Copyright (C) 2007 Marvell International Ltd.
7  *
8  *  Derived from drivers/i2c/chips/pca9539.c
9  */
10
11 #include <linux/acpi.h>
12 #include <linux/bitmap.h>
13 #include <linux/gpio/driver.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_data/pca953x.h>
21 #include <linux/regmap.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/slab.h>
24
25 #include <asm/unaligned.h>
26
27 #define PCA953X_INPUT           0x00
28 #define PCA953X_OUTPUT          0x01
29 #define PCA953X_INVERT          0x02
30 #define PCA953X_DIRECTION       0x03
31
32 #define REG_ADDR_MASK           GENMASK(5, 0)
33 #define REG_ADDR_EXT            BIT(6)
34 #define REG_ADDR_AI             BIT(7)
35
36 #define PCA957X_IN              0x00
37 #define PCA957X_INVRT           0x01
38 #define PCA957X_BKEN            0x02
39 #define PCA957X_PUPD            0x03
40 #define PCA957X_CFG             0x04
41 #define PCA957X_OUT             0x05
42 #define PCA957X_MSK             0x06
43 #define PCA957X_INTS            0x07
44
45 #define PCAL953X_OUT_STRENGTH   0x20
46 #define PCAL953X_IN_LATCH       0x22
47 #define PCAL953X_PULL_EN        0x23
48 #define PCAL953X_PULL_SEL       0x24
49 #define PCAL953X_INT_MASK       0x25
50 #define PCAL953X_INT_STAT       0x26
51 #define PCAL953X_OUT_CONF       0x27
52
53 #define PCAL6524_INT_EDGE       0x28
54 #define PCAL6524_INT_CLR        0x2a
55 #define PCAL6524_IN_STATUS      0x2b
56 #define PCAL6524_OUT_INDCONF    0x2c
57 #define PCAL6524_DEBOUNCE       0x2d
58
59 #define PCA_GPIO_MASK           GENMASK(7, 0)
60
61 #define PCAL_GPIO_MASK          GENMASK(4, 0)
62 #define PCAL_PINCTRL_MASK       GENMASK(6, 5)
63
64 #define PCA_INT                 BIT(8)
65 #define PCA_PCAL                BIT(9)
66 #define PCA_LATCH_INT           (PCA_PCAL | PCA_INT)
67 #define PCA953X_TYPE            BIT(12)
68 #define PCA957X_TYPE            BIT(13)
69 #define PCA_TYPE_MASK           GENMASK(15, 12)
70
71 #define PCA_CHIP_TYPE(x)        ((x) & PCA_TYPE_MASK)
72
73 static const struct i2c_device_id pca953x_id[] = {
74         { "pca6416", 16 | PCA953X_TYPE | PCA_INT, },
75         { "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
76         { "pca9506", 40 | PCA953X_TYPE | PCA_INT, },
77         { "pca9534", 8  | PCA953X_TYPE | PCA_INT, },
78         { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
79         { "pca9536", 4  | PCA953X_TYPE, },
80         { "pca9537", 4  | PCA953X_TYPE | PCA_INT, },
81         { "pca9538", 8  | PCA953X_TYPE | PCA_INT, },
82         { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
83         { "pca9554", 8  | PCA953X_TYPE | PCA_INT, },
84         { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
85         { "pca9556", 8  | PCA953X_TYPE, },
86         { "pca9557", 8  | PCA953X_TYPE, },
87         { "pca9574", 8  | PCA957X_TYPE | PCA_INT, },
88         { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
89         { "pca9698", 40 | PCA953X_TYPE, },
90
91         { "pcal6416", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
92         { "pcal6524", 24 | PCA953X_TYPE | PCA_LATCH_INT, },
93         { "pcal9535", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
94         { "pcal9554b", 8  | PCA953X_TYPE | PCA_LATCH_INT, },
95         { "pcal9555a", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
96
97         { "max7310", 8  | PCA953X_TYPE, },
98         { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
99         { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
100         { "max7315", 8  | PCA953X_TYPE | PCA_INT, },
101         { "max7318", 16 | PCA953X_TYPE | PCA_INT, },
102         { "pca6107", 8  | PCA953X_TYPE | PCA_INT, },
103         { "tca6408", 8  | PCA953X_TYPE | PCA_INT, },
104         { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
105         { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
106         { "tca9539", 16 | PCA953X_TYPE | PCA_INT, },
107         { "tca9554", 8  | PCA953X_TYPE | PCA_INT, },
108         { "xra1202", 8  | PCA953X_TYPE },
109         { }
110 };
111 MODULE_DEVICE_TABLE(i2c, pca953x_id);
112
113 #ifdef CONFIG_GPIO_PCA953X_IRQ
114
115 #include <linux/dmi.h>
116
117 static const struct acpi_gpio_params pca953x_irq_gpios = { 0, 0, true };
118
119 static const struct acpi_gpio_mapping pca953x_acpi_irq_gpios[] = {
120         { "irq-gpios", &pca953x_irq_gpios, 1, ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER },
121         { }
122 };
123
124 static int pca953x_acpi_get_irq(struct device *dev)
125 {
126         int ret;
127
128         ret = devm_acpi_dev_add_driver_gpios(dev, pca953x_acpi_irq_gpios);
129         if (ret)
130                 dev_warn(dev, "can't add GPIO ACPI mapping\n");
131
132         ret = acpi_dev_gpio_irq_get_by(ACPI_COMPANION(dev), "irq-gpios", 0);
133         if (ret < 0)
134                 return ret;
135
136         dev_info(dev, "ACPI interrupt quirk (IRQ %d)\n", ret);
137         return ret;
138 }
139
140 static const struct dmi_system_id pca953x_dmi_acpi_irq_info[] = {
141         {
142                 /*
143                  * On Intel Galileo Gen 2 board the IRQ pin of one of
144                  * the I²C GPIO expanders, which has GpioInt() resource,
145                  * is provided as an absolute number instead of being
146                  * relative. Since first controller (gpio-sch.c) and
147                  * second (gpio-dwapb.c) are at the fixed bases, we may
148                  * safely refer to the number in the global space to get
149                  * an IRQ out of it.
150                  */
151                 .matches = {
152                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "GalileoGen2"),
153                 },
154         },
155         {}
156 };
157 #endif
158
159 static const struct acpi_device_id pca953x_acpi_ids[] = {
160         { "INT3491", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
161         { }
162 };
163 MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids);
164
165 #define MAX_BANK 5
166 #define BANK_SZ 8
167 #define MAX_LINE        (MAX_BANK * BANK_SZ)
168
169 #define NBANK(chip) DIV_ROUND_UP(chip->gpio_chip.ngpio, BANK_SZ)
170
171 struct pca953x_reg_config {
172         int direction;
173         int output;
174         int input;
175         int invert;
176 };
177
178 static const struct pca953x_reg_config pca953x_regs = {
179         .direction = PCA953X_DIRECTION,
180         .output = PCA953X_OUTPUT,
181         .input = PCA953X_INPUT,
182         .invert = PCA953X_INVERT,
183 };
184
185 static const struct pca953x_reg_config pca957x_regs = {
186         .direction = PCA957X_CFG,
187         .output = PCA957X_OUT,
188         .input = PCA957X_IN,
189         .invert = PCA957X_INVRT,
190 };
191
192 struct pca953x_chip {
193         unsigned gpio_start;
194         struct mutex i2c_lock;
195         struct regmap *regmap;
196
197 #ifdef CONFIG_GPIO_PCA953X_IRQ
198         struct mutex irq_lock;
199         DECLARE_BITMAP(irq_mask, MAX_LINE);
200         DECLARE_BITMAP(irq_stat, MAX_LINE);
201         DECLARE_BITMAP(irq_trig_raise, MAX_LINE);
202         DECLARE_BITMAP(irq_trig_fall, MAX_LINE);
203         struct irq_chip irq_chip;
204 #endif
205         atomic_t wakeup_path;
206
207         struct i2c_client *client;
208         struct gpio_chip gpio_chip;
209         const char *const *names;
210         unsigned long driver_data;
211         struct regulator *regulator;
212
213         const struct pca953x_reg_config *regs;
214 };
215
216 static int pca953x_bank_shift(struct pca953x_chip *chip)
217 {
218         return fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
219 }
220
221 #define PCA953x_BANK_INPUT      BIT(0)
222 #define PCA953x_BANK_OUTPUT     BIT(1)
223 #define PCA953x_BANK_POLARITY   BIT(2)
224 #define PCA953x_BANK_CONFIG     BIT(3)
225
226 #define PCA957x_BANK_INPUT      BIT(0)
227 #define PCA957x_BANK_POLARITY   BIT(1)
228 #define PCA957x_BANK_BUSHOLD    BIT(2)
229 #define PCA957x_BANK_CONFIG     BIT(4)
230 #define PCA957x_BANK_OUTPUT     BIT(5)
231
232 #define PCAL9xxx_BANK_IN_LATCH  BIT(8 + 2)
233 #define PCAL9xxx_BANK_PULL_EN   BIT(8 + 3)
234 #define PCAL9xxx_BANK_PULL_SEL  BIT(8 + 4)
235 #define PCAL9xxx_BANK_IRQ_MASK  BIT(8 + 5)
236 #define PCAL9xxx_BANK_IRQ_STAT  BIT(8 + 6)
237
238 /*
239  * We care about the following registers:
240  * - Standard set, below 0x40, each port can be replicated up to 8 times
241  *   - PCA953x standard
242  *     Input port                       0x00 + 0 * bank_size    R
243  *     Output port                      0x00 + 1 * bank_size    RW
244  *     Polarity Inversion port          0x00 + 2 * bank_size    RW
245  *     Configuration port               0x00 + 3 * bank_size    RW
246  *   - PCA957x with mixed up registers
247  *     Input port                       0x00 + 0 * bank_size    R
248  *     Polarity Inversion port          0x00 + 1 * bank_size    RW
249  *     Bus hold port                    0x00 + 2 * bank_size    RW
250  *     Configuration port               0x00 + 4 * bank_size    RW
251  *     Output port                      0x00 + 5 * bank_size    RW
252  *
253  * - Extended set, above 0x40, often chip specific.
254  *   - PCAL6524/PCAL9555A with custom PCAL IRQ handling:
255  *     Input latch register             0x40 + 2 * bank_size    RW
256  *     Pull-up/pull-down enable reg     0x40 + 3 * bank_size    RW
257  *     Pull-up/pull-down select reg     0x40 + 4 * bank_size    RW
258  *     Interrupt mask register          0x40 + 5 * bank_size    RW
259  *     Interrupt status register        0x40 + 6 * bank_size    R
260  *
261  * - Registers with bit 0x80 set, the AI bit
262  *   The bit is cleared and the registers fall into one of the
263  *   categories above.
264  */
265
266 static bool pca953x_check_register(struct pca953x_chip *chip, unsigned int reg,
267                                    u32 checkbank)
268 {
269         int bank_shift = pca953x_bank_shift(chip);
270         int bank = (reg & REG_ADDR_MASK) >> bank_shift;
271         int offset = reg & (BIT(bank_shift) - 1);
272
273         /* Special PCAL extended register check. */
274         if (reg & REG_ADDR_EXT) {
275                 if (!(chip->driver_data & PCA_PCAL))
276                         return false;
277                 bank += 8;
278         }
279
280         /* Register is not in the matching bank. */
281         if (!(BIT(bank) & checkbank))
282                 return false;
283
284         /* Register is not within allowed range of bank. */
285         if (offset >= NBANK(chip))
286                 return false;
287
288         return true;
289 }
290
291 static bool pca953x_readable_register(struct device *dev, unsigned int reg)
292 {
293         struct pca953x_chip *chip = dev_get_drvdata(dev);
294         u32 bank;
295
296         if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE) {
297                 bank = PCA953x_BANK_INPUT | PCA953x_BANK_OUTPUT |
298                        PCA953x_BANK_POLARITY | PCA953x_BANK_CONFIG;
299         } else {
300                 bank = PCA957x_BANK_INPUT | PCA957x_BANK_OUTPUT |
301                        PCA957x_BANK_POLARITY | PCA957x_BANK_CONFIG |
302                        PCA957x_BANK_BUSHOLD;
303         }
304
305         if (chip->driver_data & PCA_PCAL) {
306                 bank |= PCAL9xxx_BANK_IN_LATCH | PCAL9xxx_BANK_PULL_EN |
307                         PCAL9xxx_BANK_PULL_SEL | PCAL9xxx_BANK_IRQ_MASK |
308                         PCAL9xxx_BANK_IRQ_STAT;
309         }
310
311         return pca953x_check_register(chip, reg, bank);
312 }
313
314 static bool pca953x_writeable_register(struct device *dev, unsigned int reg)
315 {
316         struct pca953x_chip *chip = dev_get_drvdata(dev);
317         u32 bank;
318
319         if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE) {
320                 bank = PCA953x_BANK_OUTPUT | PCA953x_BANK_POLARITY |
321                         PCA953x_BANK_CONFIG;
322         } else {
323                 bank = PCA957x_BANK_OUTPUT | PCA957x_BANK_POLARITY |
324                         PCA957x_BANK_CONFIG | PCA957x_BANK_BUSHOLD;
325         }
326
327         if (chip->driver_data & PCA_PCAL)
328                 bank |= PCAL9xxx_BANK_IN_LATCH | PCAL9xxx_BANK_PULL_EN |
329                         PCAL9xxx_BANK_PULL_SEL | PCAL9xxx_BANK_IRQ_MASK;
330
331         return pca953x_check_register(chip, reg, bank);
332 }
333
334 static bool pca953x_volatile_register(struct device *dev, unsigned int reg)
335 {
336         struct pca953x_chip *chip = dev_get_drvdata(dev);
337         u32 bank;
338
339         if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE)
340                 bank = PCA953x_BANK_INPUT;
341         else
342                 bank = PCA957x_BANK_INPUT;
343
344         if (chip->driver_data & PCA_PCAL)
345                 bank |= PCAL9xxx_BANK_IRQ_STAT;
346
347         return pca953x_check_register(chip, reg, bank);
348 }
349
350 static const struct regmap_config pca953x_i2c_regmap = {
351         .reg_bits = 8,
352         .val_bits = 8,
353
354         .use_single_read = true,
355         .use_single_write = true,
356
357         .readable_reg = pca953x_readable_register,
358         .writeable_reg = pca953x_writeable_register,
359         .volatile_reg = pca953x_volatile_register,
360
361         .disable_locking = true,
362         .cache_type = REGCACHE_RBTREE,
363         .max_register = 0x7f,
364 };
365
366 static const struct regmap_config pca953x_ai_i2c_regmap = {
367         .reg_bits = 8,
368         .val_bits = 8,
369
370         .read_flag_mask = REG_ADDR_AI,
371         .write_flag_mask = REG_ADDR_AI,
372
373         .readable_reg = pca953x_readable_register,
374         .writeable_reg = pca953x_writeable_register,
375         .volatile_reg = pca953x_volatile_register,
376
377         .disable_locking = true,
378         .cache_type = REGCACHE_RBTREE,
379         .max_register = 0x7f,
380 };
381
382 static u8 pca953x_recalc_addr(struct pca953x_chip *chip, int reg, int off)
383 {
384         int bank_shift = pca953x_bank_shift(chip);
385         int addr = (reg & PCAL_GPIO_MASK) << bank_shift;
386         int pinctrl = (reg & PCAL_PINCTRL_MASK) << 1;
387         u8 regaddr = pinctrl | addr | (off / BANK_SZ);
388
389         return regaddr;
390 }
391
392 static int pca953x_write_regs(struct pca953x_chip *chip, int reg, unsigned long *val)
393 {
394         u8 regaddr = pca953x_recalc_addr(chip, reg, 0);
395         u8 value[MAX_BANK];
396         int i, ret;
397
398         for (i = 0; i < NBANK(chip); i++)
399                 value[i] = bitmap_get_value8(val, i * BANK_SZ);
400
401         ret = regmap_bulk_write(chip->regmap, regaddr, value, NBANK(chip));
402         if (ret < 0) {
403                 dev_err(&chip->client->dev, "failed writing register\n");
404                 return ret;
405         }
406
407         return 0;
408 }
409
410 static int pca953x_read_regs(struct pca953x_chip *chip, int reg, unsigned long *val)
411 {
412         u8 regaddr = pca953x_recalc_addr(chip, reg, 0);
413         u8 value[MAX_BANK];
414         int i, ret;
415
416         ret = regmap_bulk_read(chip->regmap, regaddr, value, NBANK(chip));
417         if (ret < 0) {
418                 dev_err(&chip->client->dev, "failed reading register\n");
419                 return ret;
420         }
421
422         for (i = 0; i < NBANK(chip); i++)
423                 bitmap_set_value8(val, value[i], i * BANK_SZ);
424
425         return 0;
426 }
427
428 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
429 {
430         struct pca953x_chip *chip = gpiochip_get_data(gc);
431         u8 dirreg = pca953x_recalc_addr(chip, chip->regs->direction, off);
432         u8 bit = BIT(off % BANK_SZ);
433         int ret;
434
435         mutex_lock(&chip->i2c_lock);
436         ret = regmap_write_bits(chip->regmap, dirreg, bit, bit);
437         mutex_unlock(&chip->i2c_lock);
438         return ret;
439 }
440
441 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
442                 unsigned off, int val)
443 {
444         struct pca953x_chip *chip = gpiochip_get_data(gc);
445         u8 dirreg = pca953x_recalc_addr(chip, chip->regs->direction, off);
446         u8 outreg = pca953x_recalc_addr(chip, chip->regs->output, off);
447         u8 bit = BIT(off % BANK_SZ);
448         int ret;
449
450         mutex_lock(&chip->i2c_lock);
451         /* set output level */
452         ret = regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0);
453         if (ret)
454                 goto exit;
455
456         /* then direction */
457         ret = regmap_write_bits(chip->regmap, dirreg, bit, 0);
458 exit:
459         mutex_unlock(&chip->i2c_lock);
460         return ret;
461 }
462
463 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
464 {
465         struct pca953x_chip *chip = gpiochip_get_data(gc);
466         u8 inreg = pca953x_recalc_addr(chip, chip->regs->input, off);
467         u8 bit = BIT(off % BANK_SZ);
468         u32 reg_val;
469         int ret;
470
471         mutex_lock(&chip->i2c_lock);
472         ret = regmap_read(chip->regmap, inreg, &reg_val);
473         mutex_unlock(&chip->i2c_lock);
474         if (ret < 0)
475                 return ret;
476
477         return !!(reg_val & bit);
478 }
479
480 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
481 {
482         struct pca953x_chip *chip = gpiochip_get_data(gc);
483         u8 outreg = pca953x_recalc_addr(chip, chip->regs->output, off);
484         u8 bit = BIT(off % BANK_SZ);
485
486         mutex_lock(&chip->i2c_lock);
487         regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0);
488         mutex_unlock(&chip->i2c_lock);
489 }
490
491 static int pca953x_gpio_get_direction(struct gpio_chip *gc, unsigned off)
492 {
493         struct pca953x_chip *chip = gpiochip_get_data(gc);
494         u8 dirreg = pca953x_recalc_addr(chip, chip->regs->direction, off);
495         u8 bit = BIT(off % BANK_SZ);
496         u32 reg_val;
497         int ret;
498
499         mutex_lock(&chip->i2c_lock);
500         ret = regmap_read(chip->regmap, dirreg, &reg_val);
501         mutex_unlock(&chip->i2c_lock);
502         if (ret < 0)
503                 return ret;
504
505         if (reg_val & bit)
506                 return GPIO_LINE_DIRECTION_IN;
507
508         return GPIO_LINE_DIRECTION_OUT;
509 }
510
511 static int pca953x_gpio_get_multiple(struct gpio_chip *gc,
512                                      unsigned long *mask, unsigned long *bits)
513 {
514         struct pca953x_chip *chip = gpiochip_get_data(gc);
515         DECLARE_BITMAP(reg_val, MAX_LINE);
516         int ret;
517
518         mutex_lock(&chip->i2c_lock);
519         ret = pca953x_read_regs(chip, chip->regs->input, reg_val);
520         mutex_unlock(&chip->i2c_lock);
521         if (ret)
522                 return ret;
523
524         bitmap_replace(bits, bits, reg_val, mask, gc->ngpio);
525         return 0;
526 }
527
528 static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
529                                       unsigned long *mask, unsigned long *bits)
530 {
531         struct pca953x_chip *chip = gpiochip_get_data(gc);
532         DECLARE_BITMAP(reg_val, MAX_LINE);
533         int ret;
534
535         mutex_lock(&chip->i2c_lock);
536         ret = pca953x_read_regs(chip, chip->regs->output, reg_val);
537         if (ret)
538                 goto exit;
539
540         bitmap_replace(reg_val, reg_val, bits, mask, gc->ngpio);
541
542         pca953x_write_regs(chip, chip->regs->output, reg_val);
543 exit:
544         mutex_unlock(&chip->i2c_lock);
545 }
546
547 static int pca953x_gpio_set_pull_up_down(struct pca953x_chip *chip,
548                                          unsigned int offset,
549                                          unsigned long config)
550 {
551         u8 pull_en_reg = pca953x_recalc_addr(chip, PCAL953X_PULL_EN, offset);
552         u8 pull_sel_reg = pca953x_recalc_addr(chip, PCAL953X_PULL_SEL, offset);
553         u8 bit = BIT(offset % BANK_SZ);
554         int ret;
555
556         /*
557          * pull-up/pull-down configuration requires PCAL extended
558          * registers
559          */
560         if (!(chip->driver_data & PCA_PCAL))
561                 return -ENOTSUPP;
562
563         mutex_lock(&chip->i2c_lock);
564
565         /* Configure pull-up/pull-down */
566         if (config == PIN_CONFIG_BIAS_PULL_UP)
567                 ret = regmap_write_bits(chip->regmap, pull_sel_reg, bit, bit);
568         else if (config == PIN_CONFIG_BIAS_PULL_DOWN)
569                 ret = regmap_write_bits(chip->regmap, pull_sel_reg, bit, 0);
570         else
571                 ret = 0;
572         if (ret)
573                 goto exit;
574
575         /* Disable/Enable pull-up/pull-down */
576         if (config == PIN_CONFIG_BIAS_DISABLE)
577                 ret = regmap_write_bits(chip->regmap, pull_en_reg, bit, 0);
578         else
579                 ret = regmap_write_bits(chip->regmap, pull_en_reg, bit, bit);
580
581 exit:
582         mutex_unlock(&chip->i2c_lock);
583         return ret;
584 }
585
586 static int pca953x_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
587                                    unsigned long config)
588 {
589         struct pca953x_chip *chip = gpiochip_get_data(gc);
590
591         switch (pinconf_to_config_param(config)) {
592         case PIN_CONFIG_BIAS_PULL_UP:
593         case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
594         case PIN_CONFIG_BIAS_PULL_DOWN:
595         case PIN_CONFIG_BIAS_DISABLE:
596                 return pca953x_gpio_set_pull_up_down(chip, offset, config);
597         default:
598                 return -ENOTSUPP;
599         }
600 }
601
602 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
603 {
604         struct gpio_chip *gc;
605
606         gc = &chip->gpio_chip;
607
608         gc->direction_input  = pca953x_gpio_direction_input;
609         gc->direction_output = pca953x_gpio_direction_output;
610         gc->get = pca953x_gpio_get_value;
611         gc->set = pca953x_gpio_set_value;
612         gc->get_direction = pca953x_gpio_get_direction;
613         gc->get_multiple = pca953x_gpio_get_multiple;
614         gc->set_multiple = pca953x_gpio_set_multiple;
615         gc->set_config = pca953x_gpio_set_config;
616         gc->can_sleep = true;
617
618         gc->base = chip->gpio_start;
619         gc->ngpio = gpios;
620         gc->label = dev_name(&chip->client->dev);
621         gc->parent = &chip->client->dev;
622         gc->owner = THIS_MODULE;
623         gc->names = chip->names;
624 }
625
626 #ifdef CONFIG_GPIO_PCA953X_IRQ
627 static void pca953x_irq_mask(struct irq_data *d)
628 {
629         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
630         struct pca953x_chip *chip = gpiochip_get_data(gc);
631         irq_hw_number_t hwirq = irqd_to_hwirq(d);
632
633         clear_bit(hwirq, chip->irq_mask);
634 }
635
636 static void pca953x_irq_unmask(struct irq_data *d)
637 {
638         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
639         struct pca953x_chip *chip = gpiochip_get_data(gc);
640         irq_hw_number_t hwirq = irqd_to_hwirq(d);
641
642         set_bit(hwirq, chip->irq_mask);
643 }
644
645 static int pca953x_irq_set_wake(struct irq_data *d, unsigned int on)
646 {
647         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
648         struct pca953x_chip *chip = gpiochip_get_data(gc);
649
650         if (on)
651                 atomic_inc(&chip->wakeup_path);
652         else
653                 atomic_dec(&chip->wakeup_path);
654
655         return irq_set_irq_wake(chip->client->irq, on);
656 }
657
658 static void pca953x_irq_bus_lock(struct irq_data *d)
659 {
660         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
661         struct pca953x_chip *chip = gpiochip_get_data(gc);
662
663         mutex_lock(&chip->irq_lock);
664 }
665
666 static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
667 {
668         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
669         struct pca953x_chip *chip = gpiochip_get_data(gc);
670         DECLARE_BITMAP(irq_mask, MAX_LINE);
671         DECLARE_BITMAP(reg_direction, MAX_LINE);
672         int level;
673
674         if (chip->driver_data & PCA_PCAL) {
675                 /* Enable latch on interrupt-enabled inputs */
676                 pca953x_write_regs(chip, PCAL953X_IN_LATCH, chip->irq_mask);
677
678                 bitmap_complement(irq_mask, chip->irq_mask, gc->ngpio);
679
680                 /* Unmask enabled interrupts */
681                 pca953x_write_regs(chip, PCAL953X_INT_MASK, irq_mask);
682         }
683
684         /* Switch direction to input if needed */
685         pca953x_read_regs(chip, chip->regs->direction, reg_direction);
686
687         bitmap_or(irq_mask, chip->irq_trig_fall, chip->irq_trig_raise, gc->ngpio);
688         bitmap_complement(reg_direction, reg_direction, gc->ngpio);
689         bitmap_and(irq_mask, irq_mask, reg_direction, gc->ngpio);
690
691         /* Look for any newly setup interrupt */
692         for_each_set_bit(level, irq_mask, gc->ngpio)
693                 pca953x_gpio_direction_input(&chip->gpio_chip, level);
694
695         mutex_unlock(&chip->irq_lock);
696 }
697
698 static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
699 {
700         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
701         struct pca953x_chip *chip = gpiochip_get_data(gc);
702         irq_hw_number_t hwirq = irqd_to_hwirq(d);
703
704         if (!(type & IRQ_TYPE_EDGE_BOTH)) {
705                 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
706                         d->irq, type);
707                 return -EINVAL;
708         }
709
710         assign_bit(hwirq, chip->irq_trig_fall, type & IRQ_TYPE_EDGE_FALLING);
711         assign_bit(hwirq, chip->irq_trig_raise, type & IRQ_TYPE_EDGE_RISING);
712
713         return 0;
714 }
715
716 static void pca953x_irq_shutdown(struct irq_data *d)
717 {
718         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
719         struct pca953x_chip *chip = gpiochip_get_data(gc);
720         irq_hw_number_t hwirq = irqd_to_hwirq(d);
721
722         clear_bit(hwirq, chip->irq_trig_raise);
723         clear_bit(hwirq, chip->irq_trig_fall);
724 }
725
726 static bool pca953x_irq_pending(struct pca953x_chip *chip, unsigned long *pending)
727 {
728         struct gpio_chip *gc = &chip->gpio_chip;
729         DECLARE_BITMAP(reg_direction, MAX_LINE);
730         DECLARE_BITMAP(old_stat, MAX_LINE);
731         DECLARE_BITMAP(cur_stat, MAX_LINE);
732         DECLARE_BITMAP(new_stat, MAX_LINE);
733         DECLARE_BITMAP(trigger, MAX_LINE);
734         int ret;
735
736         if (chip->driver_data & PCA_PCAL) {
737                 /* Read the current interrupt status from the device */
738                 ret = pca953x_read_regs(chip, PCAL953X_INT_STAT, trigger);
739                 if (ret)
740                         return false;
741
742                 /* Check latched inputs and clear interrupt status */
743                 ret = pca953x_read_regs(chip, chip->regs->input, cur_stat);
744                 if (ret)
745                         return false;
746
747                 /* Apply filter for rising/falling edge selection */
748                 bitmap_replace(new_stat, chip->irq_trig_fall, chip->irq_trig_raise, cur_stat, gc->ngpio);
749
750                 bitmap_and(pending, new_stat, trigger, gc->ngpio);
751
752                 return !bitmap_empty(pending, gc->ngpio);
753         }
754
755         ret = pca953x_read_regs(chip, chip->regs->input, cur_stat);
756         if (ret)
757                 return false;
758
759         /* Remove output pins from the equation */
760         pca953x_read_regs(chip, chip->regs->direction, reg_direction);
761
762         bitmap_copy(old_stat, chip->irq_stat, gc->ngpio);
763
764         bitmap_and(new_stat, cur_stat, reg_direction, gc->ngpio);
765         bitmap_xor(cur_stat, new_stat, old_stat, gc->ngpio);
766         bitmap_and(trigger, cur_stat, chip->irq_mask, gc->ngpio);
767
768         bitmap_copy(chip->irq_stat, new_stat, gc->ngpio);
769
770         if (bitmap_empty(trigger, gc->ngpio))
771                 return false;
772
773         bitmap_and(cur_stat, chip->irq_trig_fall, old_stat, gc->ngpio);
774         bitmap_and(old_stat, chip->irq_trig_raise, new_stat, gc->ngpio);
775         bitmap_or(new_stat, old_stat, cur_stat, gc->ngpio);
776         bitmap_and(pending, new_stat, trigger, gc->ngpio);
777
778         return !bitmap_empty(pending, gc->ngpio);
779 }
780
781 static irqreturn_t pca953x_irq_handler(int irq, void *devid)
782 {
783         struct pca953x_chip *chip = devid;
784         struct gpio_chip *gc = &chip->gpio_chip;
785         DECLARE_BITMAP(pending, MAX_LINE);
786         int level;
787         bool ret;
788
789         bitmap_zero(pending, MAX_LINE);
790
791         mutex_lock(&chip->i2c_lock);
792         ret = pca953x_irq_pending(chip, pending);
793         mutex_unlock(&chip->i2c_lock);
794
795         if (ret) {
796                 ret = 0;
797
798                 for_each_set_bit(level, pending, gc->ngpio) {
799                         int nested_irq = irq_find_mapping(gc->irq.domain, level);
800
801                         if (unlikely(nested_irq <= 0)) {
802                                 dev_warn_ratelimited(gc->parent, "unmapped interrupt %d\n", level);
803                                 continue;
804                         }
805
806                         handle_nested_irq(nested_irq);
807                         ret = 1;
808                 }
809         }
810
811         return IRQ_RETVAL(ret);
812 }
813
814 static int pca953x_irq_setup(struct pca953x_chip *chip, int irq_base)
815 {
816         struct i2c_client *client = chip->client;
817         struct irq_chip *irq_chip = &chip->irq_chip;
818         DECLARE_BITMAP(reg_direction, MAX_LINE);
819         DECLARE_BITMAP(irq_stat, MAX_LINE);
820         struct gpio_irq_chip *girq;
821         int ret;
822
823         if (dmi_first_match(pca953x_dmi_acpi_irq_info)) {
824                 ret = pca953x_acpi_get_irq(&client->dev);
825                 if (ret > 0)
826                         client->irq = ret;
827         }
828
829         if (!client->irq)
830                 return 0;
831
832         if (irq_base == -1)
833                 return 0;
834
835         if (!(chip->driver_data & PCA_INT))
836                 return 0;
837
838         ret = pca953x_read_regs(chip, chip->regs->input, irq_stat);
839         if (ret)
840                 return ret;
841
842         /*
843          * There is no way to know which GPIO line generated the
844          * interrupt.  We have to rely on the previous read for
845          * this purpose.
846          */
847         pca953x_read_regs(chip, chip->regs->direction, reg_direction);
848         bitmap_and(chip->irq_stat, irq_stat, reg_direction, chip->gpio_chip.ngpio);
849         mutex_init(&chip->irq_lock);
850
851         irq_chip->name = dev_name(&client->dev);
852         irq_chip->irq_mask = pca953x_irq_mask;
853         irq_chip->irq_unmask = pca953x_irq_unmask;
854         irq_chip->irq_set_wake = pca953x_irq_set_wake;
855         irq_chip->irq_bus_lock = pca953x_irq_bus_lock;
856         irq_chip->irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock;
857         irq_chip->irq_set_type = pca953x_irq_set_type;
858         irq_chip->irq_shutdown = pca953x_irq_shutdown;
859
860         girq = &chip->gpio_chip.irq;
861         girq->chip = irq_chip;
862         /* This will let us handle the parent IRQ in the driver */
863         girq->parent_handler = NULL;
864         girq->num_parents = 0;
865         girq->parents = NULL;
866         girq->default_type = IRQ_TYPE_NONE;
867         girq->handler = handle_simple_irq;
868         girq->threaded = true;
869         girq->first = irq_base; /* FIXME: get rid of this */
870
871         ret = devm_request_threaded_irq(&client->dev, client->irq,
872                                         NULL, pca953x_irq_handler,
873                                         IRQF_ONESHOT | IRQF_SHARED,
874                                         dev_name(&client->dev), chip);
875         if (ret) {
876                 dev_err(&client->dev, "failed to request irq %d\n",
877                         client->irq);
878                 return ret;
879         }
880
881         return 0;
882 }
883
884 #else /* CONFIG_GPIO_PCA953X_IRQ */
885 static int pca953x_irq_setup(struct pca953x_chip *chip,
886                              int irq_base)
887 {
888         struct i2c_client *client = chip->client;
889
890         if (client->irq && irq_base != -1 && (chip->driver_data & PCA_INT))
891                 dev_warn(&client->dev, "interrupt support not compiled in\n");
892
893         return 0;
894 }
895 #endif
896
897 static int device_pca95xx_init(struct pca953x_chip *chip, u32 invert)
898 {
899         DECLARE_BITMAP(val, MAX_LINE);
900         u8 regaddr;
901         int ret;
902
903         regaddr = pca953x_recalc_addr(chip, chip->regs->output, 0);
904         ret = regcache_sync_region(chip->regmap, regaddr,
905                                    regaddr + NBANK(chip) - 1);
906         if (ret)
907                 goto out;
908
909         regaddr = pca953x_recalc_addr(chip, chip->regs->direction, 0);
910         ret = regcache_sync_region(chip->regmap, regaddr,
911                                    regaddr + NBANK(chip) - 1);
912         if (ret)
913                 goto out;
914
915         /* set platform specific polarity inversion */
916         if (invert)
917                 bitmap_fill(val, MAX_LINE);
918         else
919                 bitmap_zero(val, MAX_LINE);
920
921         ret = pca953x_write_regs(chip, chip->regs->invert, val);
922 out:
923         return ret;
924 }
925
926 static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
927 {
928         DECLARE_BITMAP(val, MAX_LINE);
929         unsigned int i;
930         int ret;
931
932         ret = device_pca95xx_init(chip, invert);
933         if (ret)
934                 goto out;
935
936         /* To enable register 6, 7 to control pull up and pull down */
937         for (i = 0; i < NBANK(chip); i++)
938                 bitmap_set_value8(val, 0x02, i * BANK_SZ);
939
940         ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
941         if (ret)
942                 goto out;
943
944         return 0;
945 out:
946         return ret;
947 }
948
949 static int pca953x_probe(struct i2c_client *client,
950                          const struct i2c_device_id *i2c_id)
951 {
952         struct pca953x_platform_data *pdata;
953         struct pca953x_chip *chip;
954         int irq_base = 0;
955         int ret;
956         u32 invert = 0;
957         struct regulator *reg;
958         const struct regmap_config *regmap_config;
959
960         chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
961         if (chip == NULL)
962                 return -ENOMEM;
963
964         pdata = dev_get_platdata(&client->dev);
965         if (pdata) {
966                 irq_base = pdata->irq_base;
967                 chip->gpio_start = pdata->gpio_base;
968                 invert = pdata->invert;
969                 chip->names = pdata->names;
970         } else {
971                 struct gpio_desc *reset_gpio;
972
973                 chip->gpio_start = -1;
974                 irq_base = 0;
975
976                 /*
977                  * See if we need to de-assert a reset pin.
978                  *
979                  * There is no known ACPI-enabled platforms that are
980                  * using "reset" GPIO. Otherwise any of those platform
981                  * must use _DSD method with corresponding property.
982                  */
983                 reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
984                                                      GPIOD_OUT_LOW);
985                 if (IS_ERR(reset_gpio))
986                         return PTR_ERR(reset_gpio);
987         }
988
989         chip->client = client;
990
991         reg = devm_regulator_get(&client->dev, "vcc");
992         if (IS_ERR(reg))
993                 return dev_err_probe(&client->dev, PTR_ERR(reg), "reg get err\n");
994
995         ret = regulator_enable(reg);
996         if (ret) {
997                 dev_err(&client->dev, "reg en err: %d\n", ret);
998                 return ret;
999         }
1000         chip->regulator = reg;
1001
1002         if (i2c_id) {
1003                 chip->driver_data = i2c_id->driver_data;
1004         } else {
1005                 const void *match;
1006
1007                 match = device_get_match_data(&client->dev);
1008                 if (!match) {
1009                         ret = -ENODEV;
1010                         goto err_exit;
1011                 }
1012
1013                 chip->driver_data = (uintptr_t)match;
1014         }
1015
1016         i2c_set_clientdata(client, chip);
1017
1018         pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
1019
1020         if (NBANK(chip) > 2 || PCA_CHIP_TYPE(chip->driver_data) == PCA957X_TYPE) {
1021                 dev_info(&client->dev, "using AI\n");
1022                 regmap_config = &pca953x_ai_i2c_regmap;
1023         } else {
1024                 dev_info(&client->dev, "using no AI\n");
1025                 regmap_config = &pca953x_i2c_regmap;
1026         }
1027
1028         chip->regmap = devm_regmap_init_i2c(client, regmap_config);
1029         if (IS_ERR(chip->regmap)) {
1030                 ret = PTR_ERR(chip->regmap);
1031                 goto err_exit;
1032         }
1033
1034         regcache_mark_dirty(chip->regmap);
1035
1036         mutex_init(&chip->i2c_lock);
1037         /*
1038          * In case we have an i2c-mux controlled by a GPIO provided by an
1039          * expander using the same driver higher on the device tree, read the
1040          * i2c adapter nesting depth and use the retrieved value as lockdep
1041          * subclass for chip->i2c_lock.
1042          *
1043          * REVISIT: This solution is not complete. It protects us from lockdep
1044          * false positives when the expander controlling the i2c-mux is on
1045          * a different level on the device tree, but not when it's on the same
1046          * level on a different branch (in which case the subclass number
1047          * would be the same).
1048          *
1049          * TODO: Once a correct solution is developed, a similar fix should be
1050          * applied to all other i2c-controlled GPIO expanders (and potentially
1051          * regmap-i2c).
1052          */
1053         lockdep_set_subclass(&chip->i2c_lock,
1054                              i2c_adapter_depth(client->adapter));
1055
1056         /* initialize cached registers from their original values.
1057          * we can't share this chip with another i2c master.
1058          */
1059
1060         if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE) {
1061                 chip->regs = &pca953x_regs;
1062                 ret = device_pca95xx_init(chip, invert);
1063         } else {
1064                 chip->regs = &pca957x_regs;
1065                 ret = device_pca957x_init(chip, invert);
1066         }
1067         if (ret)
1068                 goto err_exit;
1069
1070         ret = pca953x_irq_setup(chip, irq_base);
1071         if (ret)
1072                 goto err_exit;
1073
1074         ret = devm_gpiochip_add_data(&client->dev, &chip->gpio_chip, chip);
1075         if (ret)
1076                 goto err_exit;
1077
1078         if (pdata && pdata->setup) {
1079                 ret = pdata->setup(client, chip->gpio_chip.base,
1080                                    chip->gpio_chip.ngpio, pdata->context);
1081                 if (ret < 0)
1082                         dev_warn(&client->dev, "setup failed, %d\n", ret);
1083         }
1084
1085         return 0;
1086
1087 err_exit:
1088         regulator_disable(chip->regulator);
1089         return ret;
1090 }
1091
1092 static int pca953x_remove(struct i2c_client *client)
1093 {
1094         struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
1095         struct pca953x_chip *chip = i2c_get_clientdata(client);
1096         int ret;
1097
1098         if (pdata && pdata->teardown) {
1099                 ret = pdata->teardown(client, chip->gpio_chip.base,
1100                                       chip->gpio_chip.ngpio, pdata->context);
1101                 if (ret < 0)
1102                         dev_err(&client->dev, "teardown failed, %d\n", ret);
1103         } else {
1104                 ret = 0;
1105         }
1106
1107         regulator_disable(chip->regulator);
1108
1109         return ret;
1110 }
1111
1112 #ifdef CONFIG_PM_SLEEP
1113 static int pca953x_regcache_sync(struct device *dev)
1114 {
1115         struct pca953x_chip *chip = dev_get_drvdata(dev);
1116         int ret;
1117         u8 regaddr;
1118
1119         /*
1120          * The ordering between direction and output is important,
1121          * sync these registers first and only then sync the rest.
1122          */
1123         regaddr = pca953x_recalc_addr(chip, chip->regs->direction, 0);
1124         ret = regcache_sync_region(chip->regmap, regaddr, regaddr + NBANK(chip) - 1);
1125         if (ret) {
1126                 dev_err(dev, "Failed to sync GPIO dir registers: %d\n", ret);
1127                 return ret;
1128         }
1129
1130         regaddr = pca953x_recalc_addr(chip, chip->regs->output, 0);
1131         ret = regcache_sync_region(chip->regmap, regaddr, regaddr + NBANK(chip) - 1);
1132         if (ret) {
1133                 dev_err(dev, "Failed to sync GPIO out registers: %d\n", ret);
1134                 return ret;
1135         }
1136
1137 #ifdef CONFIG_GPIO_PCA953X_IRQ
1138         if (chip->driver_data & PCA_PCAL) {
1139                 regaddr = pca953x_recalc_addr(chip, PCAL953X_IN_LATCH, 0);
1140                 ret = regcache_sync_region(chip->regmap, regaddr,
1141                                            regaddr + NBANK(chip) - 1);
1142                 if (ret) {
1143                         dev_err(dev, "Failed to sync INT latch registers: %d\n",
1144                                 ret);
1145                         return ret;
1146                 }
1147
1148                 regaddr = pca953x_recalc_addr(chip, PCAL953X_INT_MASK, 0);
1149                 ret = regcache_sync_region(chip->regmap, regaddr,
1150                                            regaddr + NBANK(chip) - 1);
1151                 if (ret) {
1152                         dev_err(dev, "Failed to sync INT mask registers: %d\n",
1153                                 ret);
1154                         return ret;
1155                 }
1156         }
1157 #endif
1158
1159         return 0;
1160 }
1161
1162 static int pca953x_suspend(struct device *dev)
1163 {
1164         struct pca953x_chip *chip = dev_get_drvdata(dev);
1165
1166         mutex_lock(&chip->i2c_lock);
1167         regcache_cache_only(chip->regmap, true);
1168         mutex_unlock(&chip->i2c_lock);
1169
1170         if (atomic_read(&chip->wakeup_path))
1171                 device_set_wakeup_path(dev);
1172         else
1173                 regulator_disable(chip->regulator);
1174
1175         return 0;
1176 }
1177
1178 static int pca953x_resume(struct device *dev)
1179 {
1180         struct pca953x_chip *chip = dev_get_drvdata(dev);
1181         int ret;
1182
1183         if (!atomic_read(&chip->wakeup_path)) {
1184                 ret = regulator_enable(chip->regulator);
1185                 if (ret) {
1186                         dev_err(dev, "Failed to enable regulator: %d\n", ret);
1187                         return 0;
1188                 }
1189         }
1190
1191         mutex_lock(&chip->i2c_lock);
1192         regcache_cache_only(chip->regmap, false);
1193         regcache_mark_dirty(chip->regmap);
1194         ret = pca953x_regcache_sync(dev);
1195         if (ret) {
1196                 mutex_unlock(&chip->i2c_lock);
1197                 return ret;
1198         }
1199
1200         ret = regcache_sync(chip->regmap);
1201         mutex_unlock(&chip->i2c_lock);
1202         if (ret) {
1203                 dev_err(dev, "Failed to restore register map: %d\n", ret);
1204                 return ret;
1205         }
1206
1207         return 0;
1208 }
1209 #endif
1210
1211 /* convenience to stop overlong match-table lines */
1212 #define OF_953X(__nrgpio, __int) (void *)(__nrgpio | PCA953X_TYPE | __int)
1213 #define OF_957X(__nrgpio, __int) (void *)(__nrgpio | PCA957X_TYPE | __int)
1214
1215 static const struct of_device_id pca953x_dt_ids[] = {
1216         { .compatible = "nxp,pca6416", .data = OF_953X(16, PCA_INT), },
1217         { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
1218         { .compatible = "nxp,pca9506", .data = OF_953X(40, PCA_INT), },
1219         { .compatible = "nxp,pca9534", .data = OF_953X( 8, PCA_INT), },
1220         { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
1221         { .compatible = "nxp,pca9536", .data = OF_953X( 4, 0), },
1222         { .compatible = "nxp,pca9537", .data = OF_953X( 4, PCA_INT), },
1223         { .compatible = "nxp,pca9538", .data = OF_953X( 8, PCA_INT), },
1224         { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
1225         { .compatible = "nxp,pca9554", .data = OF_953X( 8, PCA_INT), },
1226         { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
1227         { .compatible = "nxp,pca9556", .data = OF_953X( 8, 0), },
1228         { .compatible = "nxp,pca9557", .data = OF_953X( 8, 0), },
1229         { .compatible = "nxp,pca9574", .data = OF_957X( 8, PCA_INT), },
1230         { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
1231         { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },
1232
1233         { .compatible = "nxp,pcal6416", .data = OF_953X(16, PCA_LATCH_INT), },
1234         { .compatible = "nxp,pcal6524", .data = OF_953X(24, PCA_LATCH_INT), },
1235         { .compatible = "nxp,pcal9535", .data = OF_953X(16, PCA_LATCH_INT), },
1236         { .compatible = "nxp,pcal9554b", .data = OF_953X( 8, PCA_LATCH_INT), },
1237         { .compatible = "nxp,pcal9555a", .data = OF_953X(16, PCA_LATCH_INT), },
1238
1239         { .compatible = "maxim,max7310", .data = OF_953X( 8, 0), },
1240         { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
1241         { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
1242         { .compatible = "maxim,max7315", .data = OF_953X( 8, PCA_INT), },
1243         { .compatible = "maxim,max7318", .data = OF_953X(16, PCA_INT), },
1244
1245         { .compatible = "ti,pca6107", .data = OF_953X( 8, PCA_INT), },
1246         { .compatible = "ti,pca9536", .data = OF_953X( 4, 0), },
1247         { .compatible = "ti,tca6408", .data = OF_953X( 8, PCA_INT), },
1248         { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
1249         { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
1250         { .compatible = "ti,tca9539", .data = OF_953X(16, PCA_INT), },
1251
1252         { .compatible = "onnn,cat9554", .data = OF_953X( 8, PCA_INT), },
1253         { .compatible = "onnn,pca9654", .data = OF_953X( 8, PCA_INT), },
1254         { .compatible = "onnn,pca9655", .data = OF_953X(16, PCA_INT), },
1255
1256         { .compatible = "exar,xra1202", .data = OF_953X( 8, 0), },
1257         { }
1258 };
1259
1260 MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
1261
1262 static SIMPLE_DEV_PM_OPS(pca953x_pm_ops, pca953x_suspend, pca953x_resume);
1263
1264 static struct i2c_driver pca953x_driver = {
1265         .driver = {
1266                 .name   = "pca953x",
1267                 .pm     = &pca953x_pm_ops,
1268                 .of_match_table = pca953x_dt_ids,
1269                 .acpi_match_table = pca953x_acpi_ids,
1270         },
1271         .probe          = pca953x_probe,
1272         .remove         = pca953x_remove,
1273         .id_table       = pca953x_id,
1274 };
1275
1276 static int __init pca953x_init(void)
1277 {
1278         return i2c_add_driver(&pca953x_driver);
1279 }
1280 /* register after i2c postcore initcall and before
1281  * subsys initcalls that may rely on these GPIOs
1282  */
1283 subsys_initcall(pca953x_init);
1284
1285 static void __exit pca953x_exit(void)
1286 {
1287         i2c_del_driver(&pca953x_driver);
1288 }
1289 module_exit(pca953x_exit);
1290
1291 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
1292 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
1293 MODULE_LICENSE("GPL");