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