blob: 82122c3c688ae45c475954afb3b3b68726c45b0e [file] [log] [blame]
Thomas Gleixnerb886d832019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
eric miao9e60fdc2008-02-04 22:28:26 -08002/*
Aaron Sierra1e191692014-02-07 16:35:48 -06003 * PCA953x 4/8/16/24/40 bit I/O ports
eric miao9e60fdc2008-02-04 22:28:26 -08004 *
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
eric miao9e60fdc2008-02-04 22:28:26 -08009 */
10
Andy Shevchenkob413d7a2017-03-22 16:11:12 +020011#include <linux/acpi.h>
Andy Shevchenko63b484c2019-08-01 20:39:36 +030012#include <linux/bits.h>
Linus Walleij644f3da2018-05-24 14:26:20 +020013#include <linux/gpio/driver.h>
Steve Longerbeam054ccde2017-01-10 11:29:51 -080014#include <linux/gpio/consumer.h>
eric miao9e60fdc2008-02-04 22:28:26 -080015#include <linux/i2c.h>
Andy Shevchenkob413d7a2017-03-22 16:11:12 +020016#include <linux/init.h>
17#include <linux/interrupt.h>
18#include <linux/module.h>
Nate Case1965d302009-06-17 16:26:17 -070019#include <linux/of_platform.h>
Andy Shevchenkob413d7a2017-03-22 16:11:12 +020020#include <linux/platform_data/pca953x.h>
Marek Vasut49427232018-12-12 02:39:58 +010021#include <linux/regmap.h>
Phil Reide23efa32016-07-29 11:39:55 +080022#include <linux/regulator/consumer.h>
Andy Shevchenkob413d7a2017-03-22 16:11:12 +020023#include <linux/slab.h>
24
25#include <asm/unaligned.h>
eric miao9e60fdc2008-02-04 22:28:26 -080026
H. Nikolaus Schaller0950c192018-04-28 18:31:31 +020027#define PCA953X_INPUT 0x00
28#define PCA953X_OUTPUT 0x01
29#define PCA953X_INVERT 0x02
30#define PCA953X_DIRECTION 0x03
eric miao9e60fdc2008-02-04 22:28:26 -080031
Andy Shevchenko63b484c2019-08-01 20:39:36 +030032#define REG_ADDR_MASK GENMASK(5, 0)
33#define REG_ADDR_EXT BIT(6)
34#define REG_ADDR_AI BIT(7)
Andreas Schallenbergae79c192012-05-09 09:46:17 +020035
H. Nikolaus Schaller0950c192018-04-28 18:31:31 +020036#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
Haojian Zhuang33226ff2011-04-18 22:12:46 +080044
H. Nikolaus Schaller6315d232018-04-28 18:31:32 +020045#define PCAL953X_OUT_STRENGTH 0x20
H. Nikolaus Schaller0950c192018-04-28 18:31:31 +020046#define PCAL953X_IN_LATCH 0x22
H. Nikolaus Schaller6315d232018-04-28 18:31:32 +020047#define PCAL953X_PULL_EN 0x23
48#define PCAL953X_PULL_SEL 0x24
H. Nikolaus Schaller0950c192018-04-28 18:31:31 +020049#define PCAL953X_INT_MASK 0x25
50#define PCAL953X_INT_STAT 0x26
H. Nikolaus Schaller6315d232018-04-28 18:31:32 +020051#define PCAL953X_OUT_CONF 0x27
Yong Li44896be2016-04-07 12:56:32 +080052
H. Nikolaus Schallera0ecbcc2018-04-28 18:31:33 +020053#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
Andy Shevchenko63b484c2019-08-01 20:39:36 +030059#define PCA_GPIO_MASK GENMASK(7, 0)
H. Nikolaus Schaller394aeef2018-05-17 06:59:48 +020060
Andy Shevchenko63b484c2019-08-01 20:39:36 +030061#define PCAL_GPIO_MASK GENMASK(4, 0)
62#define PCAL_PINCTRL_MASK GENMASK(6, 5)
H. Nikolaus Schaller394aeef2018-05-17 06:59:48 +020063
Andy Shevchenko63b484c2019-08-01 20:39:36 +030064#define PCA_INT BIT(8)
65#define PCA_PCAL BIT(9)
Andy Shevchenko2870b3c2019-03-05 11:13:46 +020066#define PCA_LATCH_INT (PCA_PCAL | PCA_INT)
Andy Shevchenko63b484c2019-08-01 20:39:36 +030067#define PCA953X_TYPE BIT(12)
68#define PCA957X_TYPE BIT(13)
69#define PCA_TYPE_MASK GENMASK(15, 12)
Andy Shevchenkoc6664142015-10-01 14:20:27 +030070
71#define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080072
Jean Delvare3760f732008-04-29 23:11:40 +020073static const struct i2c_device_id pca953x_id[] = {
Alexandre Belloni12c7a4f2019-04-09 14:35:46 +020074 { "pca6416", 16 | PCA953X_TYPE | PCA_INT, },
Gregory CLEMENT89f5df02013-01-22 22:10:24 +010075 { "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
Haojian Zhuang33226ff2011-04-18 22:12:46 +080076 { "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, },
Aaron Sierraeb32b5a2014-02-13 13:59:23 +010088 { "pca9698", 40 | PCA953X_TYPE, },
David Brownellab5dc372009-01-06 14:42:27 -080089
Andy Shevchenko2870b3c2019-03-05 11:13:46 +020090 { "pcal6416", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
91 { "pcal6524", 24 | PCA953X_TYPE | PCA_LATCH_INT, },
92 { "pcal9555a", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
Andy Shevchenko747e42a1c2016-06-15 01:57:56 +030093
Haojian Zhuang33226ff2011-04-18 22:12:46 +080094 { "max7310", 8 | PCA953X_TYPE, },
95 { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
96 { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
97 { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
Marek Vasut1208c932016-10-17 18:36:49 +020098 { "max7318", 16 | PCA953X_TYPE | PCA_INT, },
Haojian Zhuang33226ff2011-04-18 22:12:46 +080099 { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
100 { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
101 { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
Andreas Schallenbergae79c192012-05-09 09:46:17 +0200102 { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
Thierry Reding2db8aba2015-09-29 12:55:44 +0200103 { "tca9539", 16 | PCA953X_TYPE | PCA_INT, },
Anders Darander1b9a0c22017-04-21 14:46:30 +0200104 { "tca9554", 8 | PCA953X_TYPE | PCA_INT, },
Aaron Sierrae73760a2014-02-07 16:36:21 -0600105 { "xra1202", 8 | PCA953X_TYPE },
Jean Delvare3760f732008-04-29 23:11:40 +0200106 { }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800107};
Jean Delvare3760f732008-04-29 23:11:40 +0200108MODULE_DEVICE_TABLE(i2c, pca953x_id);
eric miao9e60fdc2008-02-04 22:28:26 -0800109
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300110static const struct acpi_device_id pca953x_acpi_ids[] = {
Andy Shevchenko2870b3c2019-03-05 11:13:46 +0200111 { "INT3491", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300112 { }
113};
114MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids);
115
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100116#define MAX_BANK 5
117#define BANK_SZ 8
118
Vignesh Ra246b812016-06-09 11:02:04 +0530119#define NBANK(chip) DIV_ROUND_UP(chip->gpio_chip.ngpio, BANK_SZ)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100120
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200121struct pca953x_reg_config {
122 int direction;
123 int output;
124 int input;
Marek Vasut7a04aaa2018-12-12 02:39:55 +0100125 int invert;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200126};
127
128static const struct pca953x_reg_config pca953x_regs = {
129 .direction = PCA953X_DIRECTION,
130 .output = PCA953X_OUTPUT,
131 .input = PCA953X_INPUT,
Marek Vasut7a04aaa2018-12-12 02:39:55 +0100132 .invert = PCA953X_INVERT,
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200133};
134
135static const struct pca953x_reg_config pca957x_regs = {
136 .direction = PCA957X_CFG,
137 .output = PCA957X_OUT,
138 .input = PCA957X_IN,
Marek Vasut7a04aaa2018-12-12 02:39:55 +0100139 .invert = PCA957X_INVRT,
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200140};
141
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800142struct pca953x_chip {
eric miao9e60fdc2008-02-04 22:28:26 -0800143 unsigned gpio_start;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800144 struct mutex i2c_lock;
Marek Vasut49427232018-12-12 02:39:58 +0100145 struct regmap *regmap;
eric miao9e60fdc2008-02-04 22:28:26 -0800146
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800147#ifdef CONFIG_GPIO_PCA953X_IRQ
148 struct mutex irq_lock;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100149 u8 irq_mask[MAX_BANK];
150 u8 irq_stat[MAX_BANK];
151 u8 irq_trig_raise[MAX_BANK];
152 u8 irq_trig_fall[MAX_BANK];
Thomas Petazzoni5c4fee62019-01-16 10:31:58 +0100153 struct irq_chip irq_chip;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800154#endif
Geert Uytterhoevenf70fbc12019-03-20 11:39:27 +0100155 atomic_t wakeup_path;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800156
eric miao9e60fdc2008-02-04 22:28:26 -0800157 struct i2c_client *client;
158 struct gpio_chip gpio_chip;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700159 const char *const *names;
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300160 unsigned long driver_data;
Phil Reide23efa32016-07-29 11:39:55 +0800161 struct regulator *regulator;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200162
163 const struct pca953x_reg_config *regs;
eric miao9e60fdc2008-02-04 22:28:26 -0800164};
165
Marek Vasut873d1e82018-12-12 02:39:49 +0100166static int pca953x_bank_shift(struct pca953x_chip *chip)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100167{
Marek Vasut873d1e82018-12-12 02:39:49 +0100168 return fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
169}
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100170
Marek Vasut49427232018-12-12 02:39:58 +0100171#define PCA953x_BANK_INPUT BIT(0)
172#define PCA953x_BANK_OUTPUT BIT(1)
173#define PCA953x_BANK_POLARITY BIT(2)
174#define PCA953x_BANK_CONFIG BIT(3)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100175
Marek Vasut49427232018-12-12 02:39:58 +0100176#define PCA957x_BANK_INPUT BIT(0)
177#define PCA957x_BANK_POLARITY BIT(1)
178#define PCA957x_BANK_BUSHOLD BIT(2)
179#define PCA957x_BANK_CONFIG BIT(4)
180#define PCA957x_BANK_OUTPUT BIT(5)
181
182#define PCAL9xxx_BANK_IN_LATCH BIT(8 + 2)
Thomas Petazzoni15add06842019-02-07 17:28:59 +0100183#define PCAL9xxx_BANK_PULL_EN BIT(8 + 3)
184#define PCAL9xxx_BANK_PULL_SEL BIT(8 + 4)
Marek Vasut49427232018-12-12 02:39:58 +0100185#define PCAL9xxx_BANK_IRQ_MASK BIT(8 + 5)
186#define PCAL9xxx_BANK_IRQ_STAT BIT(8 + 6)
187
188/*
189 * We care about the following registers:
190 * - Standard set, below 0x40, each port can be replicated up to 8 times
191 * - PCA953x standard
192 * Input port 0x00 + 0 * bank_size R
193 * Output port 0x00 + 1 * bank_size RW
194 * Polarity Inversion port 0x00 + 2 * bank_size RW
195 * Configuration port 0x00 + 3 * bank_size RW
196 * - PCA957x with mixed up registers
197 * Input port 0x00 + 0 * bank_size R
198 * Polarity Inversion port 0x00 + 1 * bank_size RW
199 * Bus hold port 0x00 + 2 * bank_size RW
200 * Configuration port 0x00 + 4 * bank_size RW
201 * Output port 0x00 + 5 * bank_size RW
202 *
203 * - Extended set, above 0x40, often chip specific.
204 * - PCAL6524/PCAL9555A with custom PCAL IRQ handling:
205 * Input latch register 0x40 + 2 * bank_size RW
Thomas Petazzoni15add06842019-02-07 17:28:59 +0100206 * Pull-up/pull-down enable reg 0x40 + 3 * bank_size RW
207 * Pull-up/pull-down select reg 0x40 + 4 * bank_size RW
Marek Vasut49427232018-12-12 02:39:58 +0100208 * Interrupt mask register 0x40 + 5 * bank_size RW
209 * Interrupt status register 0x40 + 6 * bank_size R
210 *
211 * - Registers with bit 0x80 set, the AI bit
212 * The bit is cleared and the registers fall into one of the
213 * categories above.
214 */
215
216static bool pca953x_check_register(struct pca953x_chip *chip, unsigned int reg,
217 u32 checkbank)
218{
219 int bank_shift = pca953x_bank_shift(chip);
220 int bank = (reg & REG_ADDR_MASK) >> bank_shift;
221 int offset = reg & (BIT(bank_shift) - 1);
222
223 /* Special PCAL extended register check. */
224 if (reg & REG_ADDR_EXT) {
225 if (!(chip->driver_data & PCA_PCAL))
226 return false;
227 bank += 8;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100228 }
229
Marek Vasut49427232018-12-12 02:39:58 +0100230 /* Register is not in the matching bank. */
231 if (!(BIT(bank) & checkbank))
232 return false;
233
234 /* Register is not within allowed range of bank. */
235 if (offset >= NBANK(chip))
236 return false;
237
238 return true;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100239}
240
Marek Vasut49427232018-12-12 02:39:58 +0100241static bool pca953x_readable_register(struct device *dev, unsigned int reg)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100242{
Marek Vasut49427232018-12-12 02:39:58 +0100243 struct pca953x_chip *chip = dev_get_drvdata(dev);
244 u32 bank;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100245
Marek Vasut49427232018-12-12 02:39:58 +0100246 if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE) {
247 bank = PCA953x_BANK_INPUT | PCA953x_BANK_OUTPUT |
248 PCA953x_BANK_POLARITY | PCA953x_BANK_CONFIG;
249 } else {
250 bank = PCA957x_BANK_INPUT | PCA957x_BANK_OUTPUT |
251 PCA957x_BANK_POLARITY | PCA957x_BANK_CONFIG |
252 PCA957x_BANK_BUSHOLD;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100253 }
254
Marek Vasut49427232018-12-12 02:39:58 +0100255 if (chip->driver_data & PCA_PCAL) {
Thomas Petazzoni15add06842019-02-07 17:28:59 +0100256 bank |= PCAL9xxx_BANK_IN_LATCH | PCAL9xxx_BANK_PULL_EN |
257 PCAL9xxx_BANK_PULL_SEL | PCAL9xxx_BANK_IRQ_MASK |
Marek Vasut49427232018-12-12 02:39:58 +0100258 PCAL9xxx_BANK_IRQ_STAT;
259 }
260
261 return pca953x_check_register(chip, reg, bank);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100262}
263
Marek Vasut49427232018-12-12 02:39:58 +0100264static bool pca953x_writeable_register(struct device *dev, unsigned int reg)
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200265{
Marek Vasut49427232018-12-12 02:39:58 +0100266 struct pca953x_chip *chip = dev_get_drvdata(dev);
267 u32 bank;
268
269 if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE) {
270 bank = PCA953x_BANK_OUTPUT | PCA953x_BANK_POLARITY |
271 PCA953x_BANK_CONFIG;
272 } else {
273 bank = PCA957x_BANK_OUTPUT | PCA957x_BANK_POLARITY |
274 PCA957x_BANK_CONFIG | PCA957x_BANK_BUSHOLD;
275 }
276
277 if (chip->driver_data & PCA_PCAL)
Thomas Petazzoni15add06842019-02-07 17:28:59 +0100278 bank |= PCAL9xxx_BANK_IN_LATCH | PCAL9xxx_BANK_PULL_EN |
279 PCAL9xxx_BANK_PULL_SEL | PCAL9xxx_BANK_IRQ_MASK;
Marek Vasut49427232018-12-12 02:39:58 +0100280
281 return pca953x_check_register(chip, reg, bank);
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200282}
283
Marek Vasut49427232018-12-12 02:39:58 +0100284static bool pca953x_volatile_register(struct device *dev, unsigned int reg)
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200285{
Marek Vasut49427232018-12-12 02:39:58 +0100286 struct pca953x_chip *chip = dev_get_drvdata(dev);
287 u32 bank;
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200288
Marek Vasut49427232018-12-12 02:39:58 +0100289 if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE)
290 bank = PCA953x_BANK_INPUT;
291 else
292 bank = PCA957x_BANK_INPUT;
293
294 if (chip->driver_data & PCA_PCAL)
295 bank |= PCAL9xxx_BANK_IRQ_STAT;
296
297 return pca953x_check_register(chip, reg, bank);
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200298}
299
Wei Yongjund04e7792019-01-10 14:07:42 +0000300static const struct regmap_config pca953x_i2c_regmap = {
Marek Vasut49427232018-12-12 02:39:58 +0100301 .reg_bits = 8,
302 .val_bits = 8,
303
304 .readable_reg = pca953x_readable_register,
305 .writeable_reg = pca953x_writeable_register,
306 .volatile_reg = pca953x_volatile_register,
307
308 .cache_type = REGCACHE_RBTREE,
H. Nikolaus Schaller3b006912019-06-04 14:35:58 +0200309 /* REVISIT: should be 0x7f but some 24 bit chips use REG_ADDR_AI */
310 .max_register = 0xff,
Marek Vasut49427232018-12-12 02:39:58 +0100311};
312
Marek Vasutb32cecb2018-12-12 02:39:57 +0100313static u8 pca953x_recalc_addr(struct pca953x_chip *chip, int reg, int off,
314 bool write, bool addrinc)
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200315{
Marek Vasutb32cecb2018-12-12 02:39:57 +0100316 int bank_shift = pca953x_bank_shift(chip);
H. Nikolaus Schallerd5dbf9c2018-05-17 06:59:49 +0200317 int addr = (reg & PCAL_GPIO_MASK) << bank_shift;
318 int pinctrl = (reg & PCAL_PINCTRL_MASK) << 1;
Marek Vasutb32cecb2018-12-12 02:39:57 +0100319 u8 regaddr = pinctrl | addr | (off / BANK_SZ);
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200320
Marek Vasutb32cecb2018-12-12 02:39:57 +0100321 /* Single byte read doesn't need AI bit set. */
322 if (!addrinc)
323 return regaddr;
324
325 /* Chips with 24 and more GPIOs always support Auto Increment */
326 if (write && NBANK(chip) > 2)
327 regaddr |= REG_ADDR_AI;
328
329 /* PCA9575 needs address-increment on multi-byte writes */
330 if (PCA_CHIP_TYPE(chip->driver_data) == PCA957X_TYPE)
331 regaddr |= REG_ADDR_AI;
332
333 return regaddr;
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200334}
335
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100336static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800337{
Marek Vasutb32cecb2018-12-12 02:39:57 +0100338 u8 regaddr = pca953x_recalc_addr(chip, reg, 0, true, true);
Marek Vasut90adb092018-12-12 02:39:54 +0100339 int ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800340
Marek Vasut49427232018-12-12 02:39:58 +0100341 ret = regmap_bulk_write(chip->regmap, regaddr, val, NBANK(chip));
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800342 if (ret < 0) {
343 dev_err(&chip->client->dev, "failed writing register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800344 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800345 }
346
347 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800348}
349
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100350static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800351{
Marek Vasutb32cecb2018-12-12 02:39:57 +0100352 u8 regaddr = pca953x_recalc_addr(chip, reg, 0, false, true);
eric miao9e60fdc2008-02-04 22:28:26 -0800353 int ret;
354
Marek Vasut49427232018-12-12 02:39:58 +0100355 ret = regmap_bulk_read(chip->regmap, regaddr, val, NBANK(chip));
eric miao9e60fdc2008-02-04 22:28:26 -0800356 if (ret < 0) {
357 dev_err(&chip->client->dev, "failed reading register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800358 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800359 }
360
eric miao9e60fdc2008-02-04 22:28:26 -0800361 return 0;
362}
363
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800364static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800365{
Linus Walleij468e67f2015-12-07 11:20:54 +0100366 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marek Vasut0f25fda2018-12-12 02:39:59 +0100367 u8 dirreg = pca953x_recalc_addr(chip, chip->regs->direction, off,
368 true, false);
369 u8 bit = BIT(off % BANK_SZ);
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200370 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800371
Roland Stigge6e20fb12011-02-10 15:01:23 -0800372 mutex_lock(&chip->i2c_lock);
Marek Vasut0f25fda2018-12-12 02:39:59 +0100373 ret = regmap_write_bits(chip->regmap, dirreg, bit, bit);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800374 mutex_unlock(&chip->i2c_lock);
375 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800376}
377
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800378static int pca953x_gpio_direction_output(struct gpio_chip *gc,
eric miao9e60fdc2008-02-04 22:28:26 -0800379 unsigned off, int val)
380{
Linus Walleij468e67f2015-12-07 11:20:54 +0100381 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marek Vasut0f25fda2018-12-12 02:39:59 +0100382 u8 dirreg = pca953x_recalc_addr(chip, chip->regs->direction, off,
383 true, false);
Marek Vasutec82d1e2018-12-12 02:40:00 +0100384 u8 outreg = pca953x_recalc_addr(chip, chip->regs->output, off,
385 true, false);
Marek Vasut0f25fda2018-12-12 02:39:59 +0100386 u8 bit = BIT(off % BANK_SZ);
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200387 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800388
Roland Stigge6e20fb12011-02-10 15:01:23 -0800389 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800390 /* set output level */
Marek Vasutec82d1e2018-12-12 02:40:00 +0100391 ret = regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0);
eric miao9e60fdc2008-02-04 22:28:26 -0800392 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800393 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800394
eric miao9e60fdc2008-02-04 22:28:26 -0800395 /* then direction */
Marek Vasut0f25fda2018-12-12 02:39:59 +0100396 ret = regmap_write_bits(chip->regmap, dirreg, bit, 0);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800397exit:
398 mutex_unlock(&chip->i2c_lock);
399 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800400}
401
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800402static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800403{
Linus Walleij468e67f2015-12-07 11:20:54 +0100404 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marek Vasut87813cf2018-12-12 02:40:01 +0100405 u8 inreg = pca953x_recalc_addr(chip, chip->regs->input, off,
406 true, false);
407 u8 bit = BIT(off % BANK_SZ);
Andreas Schallenbergae79c192012-05-09 09:46:17 +0200408 u32 reg_val;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200409 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800410
Roland Stigge6e20fb12011-02-10 15:01:23 -0800411 mutex_lock(&chip->i2c_lock);
Marek Vasut87813cf2018-12-12 02:40:01 +0100412 ret = regmap_read(chip->regmap, inreg, &reg_val);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800413 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800414 if (ret < 0) {
415 /* NOTE: diagnostic already emitted; that's all we should
416 * do unless gpio_*_value_cansleep() calls become different
417 * from their nonsleeping siblings (and report faults).
418 */
419 return 0;
420 }
421
Marek Vasut87813cf2018-12-12 02:40:01 +0100422 return !!(reg_val & bit);
eric miao9e60fdc2008-02-04 22:28:26 -0800423}
424
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800425static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800426{
Linus Walleij468e67f2015-12-07 11:20:54 +0100427 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marek Vasutec82d1e2018-12-12 02:40:00 +0100428 u8 outreg = pca953x_recalc_addr(chip, chip->regs->output, off,
429 true, false);
430 u8 bit = BIT(off % BANK_SZ);
eric miao9e60fdc2008-02-04 22:28:26 -0800431
Roland Stigge6e20fb12011-02-10 15:01:23 -0800432 mutex_lock(&chip->i2c_lock);
Marek Vasutec82d1e2018-12-12 02:40:00 +0100433 regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800434 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800435}
436
Andy Shevchenko66e57192017-03-22 16:11:11 +0200437static int pca953x_gpio_get_direction(struct gpio_chip *gc, unsigned off)
438{
439 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marek Vasut0f25fda2018-12-12 02:39:59 +0100440 u8 dirreg = pca953x_recalc_addr(chip, chip->regs->direction, off,
441 true, false);
442 u8 bit = BIT(off % BANK_SZ);
Andy Shevchenko66e57192017-03-22 16:11:11 +0200443 u32 reg_val;
444 int ret;
445
446 mutex_lock(&chip->i2c_lock);
Marek Vasut0f25fda2018-12-12 02:39:59 +0100447 ret = regmap_read(chip->regmap, dirreg, &reg_val);
Andy Shevchenko66e57192017-03-22 16:11:11 +0200448 mutex_unlock(&chip->i2c_lock);
449 if (ret < 0)
450 return ret;
451
Matti Vaittinene42615e2019-11-06 10:54:12 +0200452 if (reg_val & bit)
453 return GPIO_LINE_DIRECTION_IN;
454
455 return GPIO_LINE_DIRECTION_OUT;
Andy Shevchenko66e57192017-03-22 16:11:11 +0200456}
457
Phil Reidb4818af2015-12-04 15:52:30 +0800458static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
Bartosz Golaszewskiea3d5792016-09-09 11:17:38 +0200459 unsigned long *mask, unsigned long *bits)
Phil Reidb4818af2015-12-04 15:52:30 +0800460{
Linus Walleij468e67f2015-12-07 11:20:54 +0100461 struct pca953x_chip *chip = gpiochip_get_data(gc);
Bartosz Golaszewskiea3d5792016-09-09 11:17:38 +0200462 unsigned int bank_mask, bank_val;
Marek Vasut873d1e82018-12-12 02:39:49 +0100463 int bank;
Phil Reidb4818af2015-12-04 15:52:30 +0800464 u8 reg_val[MAX_BANK];
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200465 int ret;
Bartosz Golaszewskiea3d5792016-09-09 11:17:38 +0200466
Phil Reidb4818af2015-12-04 15:52:30 +0800467 mutex_lock(&chip->i2c_lock);
Marek Vasutec82d1e2018-12-12 02:40:00 +0100468 ret = pca953x_read_regs(chip, chip->regs->output, reg_val);
469 if (ret)
470 goto exit;
471
Bartosz Golaszewskiea3d5792016-09-09 11:17:38 +0200472 for (bank = 0; bank < NBANK(chip); bank++) {
473 bank_mask = mask[bank / sizeof(*mask)] >>
474 ((bank % sizeof(*mask)) * 8);
475 if (bank_mask) {
476 bank_val = bits[bank / sizeof(*bits)] >>
477 ((bank % sizeof(*bits)) * 8);
Phil Reid53f8d322016-11-08 14:00:45 +0800478 bank_val &= bank_mask;
Bartosz Golaszewskiea3d5792016-09-09 11:17:38 +0200479 reg_val[bank] = (reg_val[bank] & ~bank_mask) | bank_val;
Phil Reidb4818af2015-12-04 15:52:30 +0800480 }
481 }
Bartosz Golaszewskiea3d5792016-09-09 11:17:38 +0200482
Marek Vasutec82d1e2018-12-12 02:40:00 +0100483 pca953x_write_regs(chip, chip->regs->output, reg_val);
Phil Reidb4818af2015-12-04 15:52:30 +0800484exit:
485 mutex_unlock(&chip->i2c_lock);
486}
487
Thomas Petazzoni15add06842019-02-07 17:28:59 +0100488static int pca953x_gpio_set_pull_up_down(struct pca953x_chip *chip,
489 unsigned int offset,
490 unsigned long config)
491{
492 u8 pull_en_reg = pca953x_recalc_addr(chip, PCAL953X_PULL_EN, offset,
493 true, false);
494 u8 pull_sel_reg = pca953x_recalc_addr(chip, PCAL953X_PULL_SEL, offset,
495 true, false);
496 u8 bit = BIT(offset % BANK_SZ);
497 int ret;
498
499 /*
500 * pull-up/pull-down configuration requires PCAL extended
501 * registers
502 */
503 if (!(chip->driver_data & PCA_PCAL))
504 return -ENOTSUPP;
505
506 mutex_lock(&chip->i2c_lock);
507
508 /* Disable pull-up/pull-down */
509 ret = regmap_write_bits(chip->regmap, pull_en_reg, bit, 0);
510 if (ret)
511 goto exit;
512
513 /* Configure pull-up/pull-down */
514 if (config == PIN_CONFIG_BIAS_PULL_UP)
515 ret = regmap_write_bits(chip->regmap, pull_sel_reg, bit, bit);
516 else if (config == PIN_CONFIG_BIAS_PULL_DOWN)
517 ret = regmap_write_bits(chip->regmap, pull_sel_reg, bit, 0);
518 if (ret)
519 goto exit;
520
521 /* Enable pull-up/pull-down */
522 ret = regmap_write_bits(chip->regmap, pull_en_reg, bit, bit);
523
524exit:
525 mutex_unlock(&chip->i2c_lock);
526 return ret;
527}
528
529static int pca953x_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
530 unsigned long config)
531{
532 struct pca953x_chip *chip = gpiochip_get_data(gc);
533
534 switch (config) {
535 case PIN_CONFIG_BIAS_PULL_UP:
536 case PIN_CONFIG_BIAS_PULL_DOWN:
537 return pca953x_gpio_set_pull_up_down(chip, offset, config);
538 default:
539 return -ENOTSUPP;
540 }
541}
542
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800543static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800544{
545 struct gpio_chip *gc;
546
547 gc = &chip->gpio_chip;
548
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800549 gc->direction_input = pca953x_gpio_direction_input;
550 gc->direction_output = pca953x_gpio_direction_output;
551 gc->get = pca953x_gpio_get_value;
552 gc->set = pca953x_gpio_set_value;
Andy Shevchenko66e57192017-03-22 16:11:11 +0200553 gc->get_direction = pca953x_gpio_get_direction;
Phil Reidb4818af2015-12-04 15:52:30 +0800554 gc->set_multiple = pca953x_gpio_set_multiple;
Thomas Petazzoni15add06842019-02-07 17:28:59 +0100555 gc->set_config = pca953x_gpio_set_config;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100556 gc->can_sleep = true;
eric miao9e60fdc2008-02-04 22:28:26 -0800557
558 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800559 gc->ngpio = gpios;
Linus Walleij5128f8d2018-12-02 09:43:21 +0100560 gc->label = dev_name(&chip->client->dev);
Linus Walleij58383c782015-11-04 09:56:26 +0100561 gc->parent = &chip->client->dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700562 gc->owner = THIS_MODULE;
Daniel Silverstone77906a542009-06-17 16:26:15 -0700563 gc->names = chip->names;
eric miao9e60fdc2008-02-04 22:28:26 -0800564}
565
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800566#ifdef CONFIG_GPIO_PCA953X_IRQ
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800567static void pca953x_irq_mask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800568{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200569 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100570 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800571
Andy Shevchenko63b484c2019-08-01 20:39:36 +0300572 chip->irq_mask[d->hwirq / BANK_SZ] &= ~BIT(d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800573}
574
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800575static void pca953x_irq_unmask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800576{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200577 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100578 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800579
Andy Shevchenko63b484c2019-08-01 20:39:36 +0300580 chip->irq_mask[d->hwirq / BANK_SZ] |= BIT(d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800581}
582
Geert Uytterhoeven2a9a2f22019-02-13 14:15:03 +0100583static int pca953x_irq_set_wake(struct irq_data *d, unsigned int on)
584{
585 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
586 struct pca953x_chip *chip = gpiochip_get_data(gc);
587
Geert Uytterhoevenf70fbc12019-03-20 11:39:27 +0100588 if (on)
589 atomic_inc(&chip->wakeup_path);
590 else
591 atomic_dec(&chip->wakeup_path);
592
Geert Uytterhoeven2a9a2f22019-02-13 14:15:03 +0100593 return irq_set_irq_wake(chip->client->irq, on);
594}
595
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800596static void pca953x_irq_bus_lock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800597{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200598 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100599 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800600
601 mutex_lock(&chip->irq_lock);
602}
603
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800604static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800605{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200606 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100607 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100608 u8 new_irqs;
609 int level, i;
Yong Li44896be2016-04-07 12:56:32 +0800610 u8 invert_irq_mask[MAX_BANK];
David Janderbc624a02019-08-27 06:46:28 +0000611 u8 reg_direction[MAX_BANK];
Marek Vasut0f25fda2018-12-12 02:39:59 +0100612
David Jander438b6c22019-08-27 06:46:29 +0000613 pca953x_read_regs(chip, chip->regs->direction, reg_direction);
Yong Li44896be2016-04-07 12:56:32 +0800614
615 if (chip->driver_data & PCA_PCAL) {
616 /* Enable latch on interrupt-enabled inputs */
617 pca953x_write_regs(chip, PCAL953X_IN_LATCH, chip->irq_mask);
618
619 for (i = 0; i < NBANK(chip); i++)
620 invert_irq_mask[i] = ~chip->irq_mask[i];
621
622 /* Unmask enabled interrupts */
623 pca953x_write_regs(chip, PCAL953X_INT_MASK, invert_irq_mask);
624 }
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700625
626 /* Look for any newly setup interrupt */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100627 for (i = 0; i < NBANK(chip); i++) {
628 new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
Marek Vasut0f25fda2018-12-12 02:39:59 +0100629 new_irqs &= reg_direction[i];
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700630
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100631 while (new_irqs) {
632 level = __ffs(new_irqs);
633 pca953x_gpio_direction_input(&chip->gpio_chip,
634 level + (BANK_SZ * i));
635 new_irqs &= ~(1 << level);
636 }
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700637 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800638
639 mutex_unlock(&chip->irq_lock);
640}
641
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800642static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800643{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200644 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100645 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100646 int bank_nb = d->hwirq / BANK_SZ;
Andy Shevchenko63b484c2019-08-01 20:39:36 +0300647 u8 mask = BIT(d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800648
649 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
650 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800651 d->irq, type);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800652 return -EINVAL;
653 }
654
655 if (type & IRQ_TYPE_EDGE_FALLING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100656 chip->irq_trig_fall[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800657 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100658 chip->irq_trig_fall[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800659
660 if (type & IRQ_TYPE_EDGE_RISING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100661 chip->irq_trig_raise[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800662 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100663 chip->irq_trig_raise[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800664
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700665 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800666}
667
Grigoryev Denis0a70fe02018-05-04 16:53:18 +0000668static void pca953x_irq_shutdown(struct irq_data *d)
669{
Mark Waltonc378b3a2019-02-28 15:46:36 +0000670 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
671 struct pca953x_chip *chip = gpiochip_get_data(gc);
Andy Shevchenko63b484c2019-08-01 20:39:36 +0300672 u8 mask = BIT(d->hwirq % BANK_SZ);
Grigoryev Denis0a70fe02018-05-04 16:53:18 +0000673
674 chip->irq_trig_raise[d->hwirq / BANK_SZ] &= ~mask;
675 chip->irq_trig_fall[d->hwirq / BANK_SZ] &= ~mask;
676}
677
Joshua Scottb6ac1282015-05-22 12:35:12 +1200678static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800679{
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100680 u8 cur_stat[MAX_BANK];
681 u8 old_stat[MAX_BANK];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200682 bool pending_seen = false;
683 bool trigger_seen = false;
684 u8 trigger[MAX_BANK];
David Janderbc624a02019-08-27 06:46:28 +0000685 u8 reg_direction[MAX_BANK];
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200686 int ret, i;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800687
Yong Li44896be2016-04-07 12:56:32 +0800688 if (chip->driver_data & PCA_PCAL) {
689 /* Read the current interrupt status from the device */
690 ret = pca953x_read_regs(chip, PCAL953X_INT_STAT, trigger);
691 if (ret)
692 return false;
693
694 /* Check latched inputs and clear interrupt status */
695 ret = pca953x_read_regs(chip, PCA953X_INPUT, cur_stat);
696 if (ret)
697 return false;
698
699 for (i = 0; i < NBANK(chip); i++) {
700 /* Apply filter for rising/falling edge selection */
701 pending[i] = (~cur_stat[i] & chip->irq_trig_fall[i]) |
702 (cur_stat[i] & chip->irq_trig_raise[i]);
703 pending[i] &= trigger[i];
704 if (pending[i])
705 pending_seen = true;
706 }
707
708 return pending_seen;
709 }
710
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200711 ret = pca953x_read_regs(chip, chip->regs->input, cur_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800712 if (ret)
Joshua Scottb6ac1282015-05-22 12:35:12 +1200713 return false;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800714
715 /* Remove output pins from the equation */
David Jander438b6c22019-08-27 06:46:29 +0000716 pca953x_read_regs(chip, chip->regs->direction, reg_direction);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100717 for (i = 0; i < NBANK(chip); i++)
Marek Vasut0f25fda2018-12-12 02:39:59 +0100718 cur_stat[i] &= reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800719
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100720 memcpy(old_stat, chip->irq_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800721
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100722 for (i = 0; i < NBANK(chip); i++) {
723 trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200724 if (trigger[i])
725 trigger_seen = true;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100726 }
727
Joshua Scottb6ac1282015-05-22 12:35:12 +1200728 if (!trigger_seen)
729 return false;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800730
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100731 memcpy(chip->irq_stat, cur_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800732
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100733 for (i = 0; i < NBANK(chip); i++) {
734 pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) |
735 (cur_stat[i] & chip->irq_trig_raise[i]);
736 pending[i] &= trigger[i];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200737 if (pending[i])
738 pending_seen = true;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100739 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800740
Joshua Scottb6ac1282015-05-22 12:35:12 +1200741 return pending_seen;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800742}
743
744static irqreturn_t pca953x_irq_handler(int irq, void *devid)
745{
746 struct pca953x_chip *chip = devid;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100747 u8 pending[MAX_BANK];
748 u8 level;
Toby Smith3275d072014-04-30 18:01:40 +1000749 unsigned nhandled = 0;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100750 int i;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800751
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100752 if (!pca953x_irq_pending(chip, pending))
Toby Smith3275d072014-04-30 18:01:40 +1000753 return IRQ_NONE;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800754
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100755 for (i = 0; i < NBANK(chip); i++) {
756 while (pending[i]) {
757 level = __ffs(pending[i]);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100758 handle_nested_irq(irq_find_mapping(chip->gpio_chip.irq.domain,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100759 level + (BANK_SZ * i)));
760 pending[i] &= ~(1 << level);
Toby Smith3275d072014-04-30 18:01:40 +1000761 nhandled++;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100762 }
763 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800764
Toby Smith3275d072014-04-30 18:01:40 +1000765 return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800766}
767
768static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200769 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800770{
771 struct i2c_client *client = chip->client;
Thomas Petazzoni5c4fee62019-01-16 10:31:58 +0100772 struct irq_chip *irq_chip = &chip->irq_chip;
David Janderbc624a02019-08-27 06:46:28 +0000773 u8 reg_direction[MAX_BANK];
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200774 int ret, i;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800775
Thomas Petazzoni7341fa72019-01-16 10:31:57 +0100776 if (!client->irq)
777 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800778
Thomas Petazzoni7341fa72019-01-16 10:31:57 +0100779 if (irq_base == -1)
780 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800781
Thomas Petazzoni7341fa72019-01-16 10:31:57 +0100782 if (!(chip->driver_data & PCA_INT))
783 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800784
Thomas Petazzoni7341fa72019-01-16 10:31:57 +0100785 ret = pca953x_read_regs(chip, chip->regs->input, chip->irq_stat);
786 if (ret)
787 return ret;
Grygorii Strashkofdd50402015-07-07 17:34:49 +0300788
Thomas Petazzoni7341fa72019-01-16 10:31:57 +0100789 /*
790 * There is no way to know which GPIO line generated the
791 * interrupt. We have to rely on the previous read for
792 * this purpose.
793 */
David Jander438b6c22019-08-27 06:46:29 +0000794 pca953x_read_regs(chip, chip->regs->direction, reg_direction);
Thomas Petazzoni7341fa72019-01-16 10:31:57 +0100795 for (i = 0; i < NBANK(chip); i++)
796 chip->irq_stat[i] &= reg_direction[i];
797 mutex_init(&chip->irq_lock);
798
799 ret = devm_request_threaded_irq(&client->dev, client->irq,
800 NULL, pca953x_irq_handler,
801 IRQF_TRIGGER_LOW | IRQF_ONESHOT |
802 IRQF_SHARED,
803 dev_name(&client->dev), chip);
804 if (ret) {
805 dev_err(&client->dev, "failed to request irq %d\n",
806 client->irq);
807 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800808 }
809
Thomas Petazzoni5c4fee62019-01-16 10:31:58 +0100810 irq_chip->name = dev_name(&chip->client->dev);
811 irq_chip->irq_mask = pca953x_irq_mask;
812 irq_chip->irq_unmask = pca953x_irq_unmask;
Geert Uytterhoeven2a9a2f22019-02-13 14:15:03 +0100813 irq_chip->irq_set_wake = pca953x_irq_set_wake;
Thomas Petazzoni5c4fee62019-01-16 10:31:58 +0100814 irq_chip->irq_bus_lock = pca953x_irq_bus_lock;
815 irq_chip->irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock;
816 irq_chip->irq_set_type = pca953x_irq_set_type;
817 irq_chip->irq_shutdown = pca953x_irq_shutdown;
818
819 ret = gpiochip_irqchip_add_nested(&chip->gpio_chip, irq_chip,
Thomas Petazzoni7341fa72019-01-16 10:31:57 +0100820 irq_base, handle_simple_irq,
821 IRQ_TYPE_NONE);
822 if (ret) {
823 dev_err(&client->dev,
824 "could not connect irqchip to gpiochip\n");
825 return ret;
826 }
827
Thomas Petazzoni5c4fee62019-01-16 10:31:58 +0100828 gpiochip_set_nested_irqchip(&chip->gpio_chip, irq_chip, client->irq);
Thomas Petazzoni7341fa72019-01-16 10:31:57 +0100829
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800830 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800831}
832
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800833#else /* CONFIG_GPIO_PCA953X_IRQ */
834static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200835 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800836{
837 struct i2c_client *client = chip->client;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800838
Baruch Siach72b38ca2018-06-21 16:38:46 +0300839 if (client->irq && irq_base != -1 && (chip->driver_data & PCA_INT))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800840 dev_warn(&client->dev, "interrupt support not compiled in\n");
841
842 return 0;
843}
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800844#endif
845
Marek Vasut7a04aaa2018-12-12 02:39:55 +0100846static int device_pca95xx_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800847{
848 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100849 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800850
Marek Vasutec82d1e2018-12-12 02:40:00 +0100851 ret = regcache_sync_region(chip->regmap, chip->regs->output,
852 chip->regs->output + NBANK(chip));
Andy Shevchenko6dd6a2d2019-08-01 20:39:37 +0300853 if (ret)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800854 goto out;
855
Marek Vasut0f25fda2018-12-12 02:39:59 +0100856 ret = regcache_sync_region(chip->regmap, chip->regs->direction,
857 chip->regs->direction + NBANK(chip));
Andy Shevchenko6dd6a2d2019-08-01 20:39:37 +0300858 if (ret)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800859 goto out;
860
861 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100862 if (invert)
863 memset(val, 0xFF, NBANK(chip));
864 else
865 memset(val, 0, NBANK(chip));
866
Marek Vasut7a04aaa2018-12-12 02:39:55 +0100867 ret = pca953x_write_regs(chip, chip->regs->invert, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800868out:
869 return ret;
870}
871
Bill Pemberton38363092012-11-19 13:22:34 -0500872static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800873{
874 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100875 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800876
Marek Vasut7a04aaa2018-12-12 02:39:55 +0100877 ret = device_pca95xx_init(chip, invert);
Nicholas Krausec75a3772015-08-26 17:52:19 -0400878 if (ret)
879 goto out;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800880
Colin Cronin20a8a962015-05-18 11:41:43 -0700881 /* To enable register 6, 7 to control pull up and pull down */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100882 memset(val, 0x02, NBANK(chip));
Nicholas Krausec75a3772015-08-26 17:52:19 -0400883 ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
884 if (ret)
885 goto out;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800886
887 return 0;
888out:
889 return ret;
890}
891
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000892static const struct of_device_id pca953x_dt_ids[];
893
Bill Pemberton38363092012-11-19 13:22:34 -0500894static int pca953x_probe(struct i2c_client *client,
Wolfram Sang6212e1d2016-09-24 11:06:29 +0200895 const struct i2c_device_id *i2c_id)
eric miao9e60fdc2008-02-04 22:28:26 -0800896{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800897 struct pca953x_platform_data *pdata;
898 struct pca953x_chip *chip;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530899 int irq_base = 0;
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200900 int ret;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530901 u32 invert = 0;
Phil Reide23efa32016-07-29 11:39:55 +0800902 struct regulator *reg;
eric miao9e60fdc2008-02-04 22:28:26 -0800903
Linus Walleijb42748c2013-01-25 17:59:28 +0100904 chip = devm_kzalloc(&client->dev,
905 sizeof(struct pca953x_chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800906 if (chip == NULL)
907 return -ENOMEM;
908
Jingoo Hane56aee12013-07-30 17:08:05 +0900909 pdata = dev_get_platdata(&client->dev);
David Janderc6dcf592011-06-14 11:00:55 +0200910 if (pdata) {
911 irq_base = pdata->irq_base;
912 chip->gpio_start = pdata->gpio_base;
913 invert = pdata->invert;
914 chip->names = pdata->names;
915 } else {
Steve Longerbeam054ccde2017-01-10 11:29:51 -0800916 struct gpio_desc *reset_gpio;
917
Markus Pargmann4bb93342014-07-29 09:24:43 +0200918 chip->gpio_start = -1;
919 irq_base = 0;
Steve Longerbeam054ccde2017-01-10 11:29:51 -0800920
Andy Shevchenko96530b32017-03-22 16:11:13 +0200921 /*
922 * See if we need to de-assert a reset pin.
923 *
924 * There is no known ACPI-enabled platforms that are
925 * using "reset" GPIO. Otherwise any of those platform
926 * must use _DSD method with corresponding property.
927 */
Steve Longerbeam054ccde2017-01-10 11:29:51 -0800928 reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
929 GPIOD_OUT_LOW);
930 if (IS_ERR(reset_gpio))
931 return PTR_ERR(reset_gpio);
Nate Case1965d302009-06-17 16:26:17 -0700932 }
933
eric miao9e60fdc2008-02-04 22:28:26 -0800934 chip->client = client;
935
Phil Reide23efa32016-07-29 11:39:55 +0800936 reg = devm_regulator_get(&client->dev, "vcc");
937 if (IS_ERR(reg)) {
938 ret = PTR_ERR(reg);
939 if (ret != -EPROBE_DEFER)
940 dev_err(&client->dev, "reg get err: %d\n", ret);
941 return ret;
942 }
943 ret = regulator_enable(reg);
944 if (ret) {
945 dev_err(&client->dev, "reg en err: %d\n", ret);
946 return ret;
947 }
948 chip->regulator = reg;
949
Wolfram Sang6212e1d2016-09-24 11:06:29 +0200950 if (i2c_id) {
951 chip->driver_data = i2c_id->driver_data;
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300952 } else {
Andy Shevchenko8eeb467d2019-08-01 20:39:35 +0300953 const void *match;
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300954
Andy Shevchenko8eeb467d2019-08-01 20:39:35 +0300955 match = device_get_match_data(&client->dev);
956 if (!match) {
957 ret = -ENODEV;
958 goto err_exit;
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000959 }
Andy Shevchenko8eeb467d2019-08-01 20:39:35 +0300960
961 chip->driver_data = (uintptr_t)match;
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300962 }
963
Marek Vasut49427232018-12-12 02:39:58 +0100964 i2c_set_clientdata(client, chip);
965
966 chip->regmap = devm_regmap_init_i2c(client, &pca953x_i2c_regmap);
967 if (IS_ERR(chip->regmap)) {
968 ret = PTR_ERR(chip->regmap);
969 goto err_exit;
970 }
971
Marek Vasut0f25fda2018-12-12 02:39:59 +0100972 regcache_mark_dirty(chip->regmap);
973
Roland Stigge6e20fb12011-02-10 15:01:23 -0800974 mutex_init(&chip->i2c_lock);
Bartosz Golaszewski74f47f02016-09-26 11:54:15 +0200975 /*
976 * In case we have an i2c-mux controlled by a GPIO provided by an
977 * expander using the same driver higher on the device tree, read the
978 * i2c adapter nesting depth and use the retrieved value as lockdep
979 * subclass for chip->i2c_lock.
980 *
981 * REVISIT: This solution is not complete. It protects us from lockdep
982 * false positives when the expander controlling the i2c-mux is on
983 * a different level on the device tree, but not when it's on the same
984 * level on a different branch (in which case the subclass number
985 * would be the same).
986 *
987 * TODO: Once a correct solution is developed, a similar fix should be
988 * applied to all other i2c-controlled GPIO expanders (and potentially
989 * regmap-i2c).
990 */
Bartosz Golaszewski559b4692016-09-16 18:02:45 +0200991 lockdep_set_subclass(&chip->i2c_lock,
992 i2c_adapter_depth(client->adapter));
Roland Stigge6e20fb12011-02-10 15:01:23 -0800993
eric miao9e60fdc2008-02-04 22:28:26 -0800994 /* initialize cached registers from their original values.
995 * we can't share this chip with another i2c master.
996 */
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300997 pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800998
Marek Vasut7a04aaa2018-12-12 02:39:55 +0100999 if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE) {
1000 chip->regs = &pca953x_regs;
1001 ret = device_pca95xx_init(chip, invert);
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +02001002 } else {
Marek Vasut7a04aaa2018-12-12 02:39:55 +01001003 chip->regs = &pca957x_regs;
Wolfram Sang7ea2aa22011-10-14 15:32:00 +02001004 ret = device_pca957x_init(chip, invert);
Marek Vasut7a04aaa2018-12-12 02:39:55 +01001005 }
Wolfram Sang7ea2aa22011-10-14 15:32:00 +02001006 if (ret)
Phil Reide23efa32016-07-29 11:39:55 +08001007 goto err_exit;
eric miao9e60fdc2008-02-04 22:28:26 -08001008
Laxman Dewangan0ece84f2016-02-22 17:43:28 +05301009 ret = devm_gpiochip_add_data(&client->dev, &chip->gpio_chip, chip);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -08001010 if (ret)
Phil Reide23efa32016-07-29 11:39:55 +08001011 goto err_exit;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -08001012
Andy Shevchenkoc6664142015-10-01 14:20:27 +03001013 ret = pca953x_irq_setup(chip, irq_base);
eric miao9e60fdc2008-02-04 22:28:26 -08001014 if (ret)
Phil Reide23efa32016-07-29 11:39:55 +08001015 goto err_exit;
eric miao9e60fdc2008-02-04 22:28:26 -08001016
David Janderc6dcf592011-06-14 11:00:55 +02001017 if (pdata && pdata->setup) {
eric miao9e60fdc2008-02-04 22:28:26 -08001018 ret = pdata->setup(client, chip->gpio_chip.base,
1019 chip->gpio_chip.ngpio, pdata->context);
1020 if (ret < 0)
1021 dev_warn(&client->dev, "setup failed, %d\n", ret);
1022 }
1023
eric miao9e60fdc2008-02-04 22:28:26 -08001024 return 0;
Phil Reide23efa32016-07-29 11:39:55 +08001025
1026err_exit:
1027 regulator_disable(chip->regulator);
1028 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -08001029}
1030
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001031static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -08001032{
Jingoo Hane56aee12013-07-30 17:08:05 +09001033 struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001034 struct pca953x_chip *chip = i2c_get_clientdata(client);
Linus Walleijd147d542016-09-13 14:43:23 +02001035 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -08001036
David Janderc6dcf592011-06-14 11:00:55 +02001037 if (pdata && pdata->teardown) {
eric miao9e60fdc2008-02-04 22:28:26 -08001038 ret = pdata->teardown(client, chip->gpio_chip.base,
1039 chip->gpio_chip.ngpio, pdata->context);
Phil Reide23efa32016-07-29 11:39:55 +08001040 if (ret < 0)
Andy Shevchenkof4160fa2019-08-01 20:39:38 +03001041 dev_err(&client->dev, "teardown failed, %d\n", ret);
Arnd Bergmannbf62efe2016-08-26 17:25:42 +02001042 } else {
1043 ret = 0;
eric miao9e60fdc2008-02-04 22:28:26 -08001044 }
1045
Phil Reide23efa32016-07-29 11:39:55 +08001046 regulator_disable(chip->regulator);
1047
1048 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -08001049}
1050
Marek Vasutb7657432018-12-12 02:40:02 +01001051#ifdef CONFIG_PM_SLEEP
1052static int pca953x_regcache_sync(struct device *dev)
1053{
1054 struct pca953x_chip *chip = dev_get_drvdata(dev);
1055 int ret;
1056
1057 /*
1058 * The ordering between direction and output is important,
1059 * sync these registers first and only then sync the rest.
1060 */
1061 ret = regcache_sync_region(chip->regmap, chip->regs->direction,
1062 chip->regs->direction + NBANK(chip));
Andy Shevchenko6dd6a2d2019-08-01 20:39:37 +03001063 if (ret) {
Marek Vasutb7657432018-12-12 02:40:02 +01001064 dev_err(dev, "Failed to sync GPIO dir registers: %d\n", ret);
1065 return ret;
1066 }
1067
1068 ret = regcache_sync_region(chip->regmap, chip->regs->output,
1069 chip->regs->output + NBANK(chip));
Andy Shevchenko6dd6a2d2019-08-01 20:39:37 +03001070 if (ret) {
Marek Vasutb7657432018-12-12 02:40:02 +01001071 dev_err(dev, "Failed to sync GPIO out registers: %d\n", ret);
1072 return ret;
1073 }
1074
1075#ifdef CONFIG_GPIO_PCA953X_IRQ
1076 if (chip->driver_data & PCA_PCAL) {
1077 ret = regcache_sync_region(chip->regmap, PCAL953X_IN_LATCH,
1078 PCAL953X_IN_LATCH + NBANK(chip));
Andy Shevchenko6dd6a2d2019-08-01 20:39:37 +03001079 if (ret) {
Marek Vasutb7657432018-12-12 02:40:02 +01001080 dev_err(dev, "Failed to sync INT latch registers: %d\n",
1081 ret);
1082 return ret;
1083 }
1084
1085 ret = regcache_sync_region(chip->regmap, PCAL953X_INT_MASK,
1086 PCAL953X_INT_MASK + NBANK(chip));
Andy Shevchenko6dd6a2d2019-08-01 20:39:37 +03001087 if (ret) {
Marek Vasutb7657432018-12-12 02:40:02 +01001088 dev_err(dev, "Failed to sync INT mask registers: %d\n",
1089 ret);
1090 return ret;
1091 }
1092 }
1093#endif
1094
1095 return 0;
1096}
1097
1098static int pca953x_suspend(struct device *dev)
1099{
1100 struct pca953x_chip *chip = dev_get_drvdata(dev);
1101
1102 regcache_cache_only(chip->regmap, true);
1103
Geert Uytterhoevenf70fbc12019-03-20 11:39:27 +01001104 if (atomic_read(&chip->wakeup_path))
1105 device_set_wakeup_path(dev);
1106 else
1107 regulator_disable(chip->regulator);
Marek Vasutb7657432018-12-12 02:40:02 +01001108
1109 return 0;
1110}
1111
1112static int pca953x_resume(struct device *dev)
1113{
1114 struct pca953x_chip *chip = dev_get_drvdata(dev);
1115 int ret;
1116
Geert Uytterhoevenf70fbc12019-03-20 11:39:27 +01001117 if (!atomic_read(&chip->wakeup_path)) {
1118 ret = regulator_enable(chip->regulator);
Andy Shevchenko6dd6a2d2019-08-01 20:39:37 +03001119 if (ret) {
Geert Uytterhoevenf70fbc12019-03-20 11:39:27 +01001120 dev_err(dev, "Failed to enable regulator: %d\n", ret);
1121 return 0;
1122 }
Marek Vasutb7657432018-12-12 02:40:02 +01001123 }
1124
1125 regcache_cache_only(chip->regmap, false);
1126 regcache_mark_dirty(chip->regmap);
1127 ret = pca953x_regcache_sync(dev);
1128 if (ret)
1129 return ret;
1130
1131 ret = regcache_sync(chip->regmap);
Andy Shevchenko6dd6a2d2019-08-01 20:39:37 +03001132 if (ret) {
Marek Vasutb7657432018-12-12 02:40:02 +01001133 dev_err(dev, "Failed to restore register map: %d\n", ret);
1134 return ret;
1135 }
1136
1137 return 0;
1138}
1139#endif
1140
Ben Dooks6f29c9a2015-12-08 10:19:26 +00001141/* convenience to stop overlong match-table lines */
1142#define OF_953X(__nrgpio, __int) (void *)(__nrgpio | PCA953X_TYPE | __int)
1143#define OF_957X(__nrgpio, __int) (void *)(__nrgpio | PCA957X_TYPE | __int)
1144
Maxime Riparded326202012-11-08 18:01:52 +01001145static const struct of_device_id pca953x_dt_ids[] = {
Alexandre Belloni12c7a4f2019-04-09 14:35:46 +02001146 { .compatible = "nxp,pca6416", .data = OF_953X(16, PCA_INT), },
Ben Dooks6f29c9a2015-12-08 10:19:26 +00001147 { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
1148 { .compatible = "nxp,pca9534", .data = OF_953X( 8, PCA_INT), },
1149 { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
1150 { .compatible = "nxp,pca9536", .data = OF_953X( 4, 0), },
1151 { .compatible = "nxp,pca9537", .data = OF_953X( 4, PCA_INT), },
1152 { .compatible = "nxp,pca9538", .data = OF_953X( 8, PCA_INT), },
1153 { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
1154 { .compatible = "nxp,pca9554", .data = OF_953X( 8, PCA_INT), },
1155 { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
1156 { .compatible = "nxp,pca9556", .data = OF_953X( 8, 0), },
1157 { .compatible = "nxp,pca9557", .data = OF_953X( 8, 0), },
1158 { .compatible = "nxp,pca9574", .data = OF_957X( 8, PCA_INT), },
1159 { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
1160 { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },
Maxime Riparded326202012-11-08 18:01:52 +01001161
Alexandre Belloni01769c42019-04-09 12:25:17 +02001162 { .compatible = "nxp,pcal6416", .data = OF_953X(16, PCA_LATCH_INT), },
H. Nikolaus Schaller0cdf21b2018-05-17 06:59:47 +02001163 { .compatible = "nxp,pcal6524", .data = OF_953X(24, PCA_LATCH_INT), },
1164 { .compatible = "nxp,pcal9555a", .data = OF_953X(16, PCA_LATCH_INT), },
H. Nikolaus Schaller3a711e02018-03-10 12:00:01 +01001165
Ben Dooks6f29c9a2015-12-08 10:19:26 +00001166 { .compatible = "maxim,max7310", .data = OF_953X( 8, 0), },
1167 { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
1168 { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
1169 { .compatible = "maxim,max7315", .data = OF_953X( 8, PCA_INT), },
Marek Vasut1208c932016-10-17 18:36:49 +02001170 { .compatible = "maxim,max7318", .data = OF_953X(16, PCA_INT), },
Maxime Riparded326202012-11-08 18:01:52 +01001171
Ben Dooks6f29c9a2015-12-08 10:19:26 +00001172 { .compatible = "ti,pca6107", .data = OF_953X( 8, PCA_INT), },
Vignesh R353661d2016-05-19 12:17:29 +05301173 { .compatible = "ti,pca9536", .data = OF_953X( 4, 0), },
Ben Dooks6f29c9a2015-12-08 10:19:26 +00001174 { .compatible = "ti,tca6408", .data = OF_953X( 8, PCA_INT), },
1175 { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
1176 { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
Peter Robinson8b74ae42019-06-01 08:12:18 +01001177 { .compatible = "ti,tca9539", .data = OF_953X(16, PCA_INT), },
Aaron Sierrae73760a2014-02-07 16:36:21 -06001178
Geert Uytterhoeven932002f2019-03-21 10:21:45 +01001179 { .compatible = "onnn,cat9554", .data = OF_953X( 8, PCA_INT), },
Sergei Shtylyov8a64e552017-11-16 23:18:32 +03001180 { .compatible = "onnn,pca9654", .data = OF_953X( 8, PCA_INT), },
Ben Dooks9f49f6d2015-12-08 10:19:25 +00001181
Ben Dooks6f29c9a2015-12-08 10:19:26 +00001182 { .compatible = "exar,xra1202", .data = OF_953X( 8, 0), },
Maxime Riparded326202012-11-08 18:01:52 +01001183 { }
1184};
1185
1186MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
1187
Marek Vasutb7657432018-12-12 02:40:02 +01001188static SIMPLE_DEV_PM_OPS(pca953x_pm_ops, pca953x_suspend, pca953x_resume);
1189
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001190static struct i2c_driver pca953x_driver = {
eric miao9e60fdc2008-02-04 22:28:26 -08001191 .driver = {
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001192 .name = "pca953x",
Marek Vasutb7657432018-12-12 02:40:02 +01001193 .pm = &pca953x_pm_ops,
Maxime Riparded326202012-11-08 18:01:52 +01001194 .of_match_table = pca953x_dt_ids,
Andy Shevchenkof32517b2015-10-01 14:20:28 +03001195 .acpi_match_table = ACPI_PTR(pca953x_acpi_ids),
eric miao9e60fdc2008-02-04 22:28:26 -08001196 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001197 .probe = pca953x_probe,
1198 .remove = pca953x_remove,
Jean Delvare3760f732008-04-29 23:11:40 +02001199 .id_table = pca953x_id,
eric miao9e60fdc2008-02-04 22:28:26 -08001200};
1201
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001202static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -08001203{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001204 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -08001205}
David Brownell2f8d1192008-10-15 22:03:13 -07001206/* register after i2c postcore initcall and before
1207 * subsys initcalls that may rely on these GPIOs
1208 */
1209subsys_initcall(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -08001210
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001211static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -08001212{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001213 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -08001214}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001215module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -08001216
1217MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001218MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -08001219MODULE_LICENSE("GPL");