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