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