blob: c91d0565159661967faa6a73ae0dfc9834fcfada [file] [log] [blame]
Thomas Gleixnerb886d83c2019-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 Shevchenko35d13d92019-12-04 16:53:37 -080012#include <linux/bitmap.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, },
Uwe Kleine-König1421b442021-01-15 17:46:56 +010076 { "pca9506", 40 | PCA953X_TYPE | PCA_INT, },
Haojian Zhuang33226ff2011-04-18 22:12:46 +080077 { "pca9534", 8 | PCA953X_TYPE | PCA_INT, },
78 { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
79 { "pca9536", 4 | PCA953X_TYPE, },
80 { "pca9537", 4 | PCA953X_TYPE | PCA_INT, },
81 { "pca9538", 8 | PCA953X_TYPE | PCA_INT, },
82 { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
83 { "pca9554", 8 | PCA953X_TYPE | PCA_INT, },
84 { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
85 { "pca9556", 8 | PCA953X_TYPE, },
86 { "pca9557", 8 | PCA953X_TYPE, },
87 { "pca9574", 8 | PCA957X_TYPE | PCA_INT, },
88 { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
Aaron Sierraeb32b5a2014-02-13 13:59:23 +010089 { "pca9698", 40 | PCA953X_TYPE, },
David Brownellab5dc372009-01-06 14:42:27 -080090
Andy Shevchenko2870b3c2019-03-05 11:13:46 +020091 { "pcal6416", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
92 { "pcal6524", 24 | PCA953X_TYPE | PCA_LATCH_INT, },
Jan Kiszka3ba3ff52020-06-15 15:22:29 +020093 { "pcal9535", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
Mike Looijmans237d9612020-09-30 11:20:53 +020094 { "pcal9554b", 8 | PCA953X_TYPE | PCA_LATCH_INT, },
Andy Shevchenko2870b3c2019-03-05 11:13:46 +020095 { "pcal9555a", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
Andy Shevchenko747e42a1c2016-06-15 01:57:56 +030096
Haojian Zhuang33226ff2011-04-18 22:12:46 +080097 { "max7310", 8 | PCA953X_TYPE, },
98 { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
99 { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
100 { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
Marek Vasut1208c932016-10-17 18:36:49 +0200101 { "max7318", 16 | PCA953X_TYPE | PCA_INT, },
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800102 { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
103 { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
104 { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
Andreas Schallenbergae79c192012-05-09 09:46:17 +0200105 { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
Thierry Reding2db8aba2015-09-29 12:55:44 +0200106 { "tca9539", 16 | PCA953X_TYPE | PCA_INT, },
Anders Darander1b9a0c22017-04-21 14:46:30 +0200107 { "tca9554", 8 | PCA953X_TYPE | PCA_INT, },
Aaron Sierrae73760a2014-02-07 16:36:21 -0600108 { "xra1202", 8 | PCA953X_TYPE },
Jean Delvare3760f732008-04-29 23:11:40 +0200109 { }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800110};
Jean Delvare3760f732008-04-29 23:11:40 +0200111MODULE_DEVICE_TABLE(i2c, pca953x_id);
eric miao9e60fdc2008-02-04 22:28:26 -0800112
Andy Shevchenkoba8c90c2020-06-05 16:40:34 +0300113#ifdef CONFIG_GPIO_PCA953X_IRQ
114
115#include <linux/dmi.h>
Andy Shevchenkoeb441332021-02-25 18:33:20 +0200116
117static const struct acpi_gpio_params pca953x_irq_gpios = { 0, 0, true };
118
119static const struct acpi_gpio_mapping pca953x_acpi_irq_gpios[] = {
120 { "irq-gpios", &pca953x_irq_gpios, 1, ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER },
121 { }
122};
123
124static int pca953x_acpi_get_irq(struct device *dev)
125{
126 int ret;
127
128 ret = devm_acpi_dev_add_driver_gpios(dev, pca953x_acpi_irq_gpios);
129 if (ret)
130 dev_warn(dev, "can't add GPIO ACPI mapping\n");
131
132 ret = acpi_dev_gpio_irq_get_by(ACPI_COMPANION(dev), "irq-gpios", 0);
133 if (ret < 0)
134 return ret;
135
136 dev_info(dev, "ACPI interrupt quirk (IRQ %d)\n", ret);
137 return ret;
138}
Andy Shevchenkoba8c90c2020-06-05 16:40:34 +0300139
140static const struct dmi_system_id pca953x_dmi_acpi_irq_info[] = {
141 {
142 /*
143 * On Intel Galileo Gen 2 board the IRQ pin of one of
144 * the I²C GPIO expanders, which has GpioInt() resource,
145 * is provided as an absolute number instead of being
146 * relative. Since first controller (gpio-sch.c) and
147 * second (gpio-dwapb.c) are at the fixed bases, we may
148 * safely refer to the number in the global space to get
149 * an IRQ out of it.
150 */
151 .matches = {
152 DMI_EXACT_MATCH(DMI_BOARD_NAME, "GalileoGen2"),
153 },
154 },
155 {}
156};
Andy Shevchenkoba8c90c2020-06-05 16:40:34 +0300157#endif
158
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300159static const struct acpi_device_id pca953x_acpi_ids[] = {
Andy Shevchenko2870b3c2019-03-05 11:13:46 +0200160 { "INT3491", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300161 { }
162};
163MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids);
164
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100165#define MAX_BANK 5
166#define BANK_SZ 8
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800167#define MAX_LINE (MAX_BANK * BANK_SZ)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100168
Vignesh Ra246b812016-06-09 11:02:04 +0530169#define NBANK(chip) DIV_ROUND_UP(chip->gpio_chip.ngpio, BANK_SZ)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100170
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200171struct pca953x_reg_config {
172 int direction;
173 int output;
174 int input;
Marek Vasut7a04aaa2018-12-12 02:39:55 +0100175 int invert;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200176};
177
178static const struct pca953x_reg_config pca953x_regs = {
179 .direction = PCA953X_DIRECTION,
180 .output = PCA953X_OUTPUT,
181 .input = PCA953X_INPUT,
Marek Vasut7a04aaa2018-12-12 02:39:55 +0100182 .invert = PCA953X_INVERT,
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200183};
184
185static const struct pca953x_reg_config pca957x_regs = {
186 .direction = PCA957X_CFG,
187 .output = PCA957X_OUT,
188 .input = PCA957X_IN,
Marek Vasut7a04aaa2018-12-12 02:39:55 +0100189 .invert = PCA957X_INVRT,
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200190};
191
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800192struct pca953x_chip {
eric miao9e60fdc2008-02-04 22:28:26 -0800193 unsigned gpio_start;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800194 struct mutex i2c_lock;
Marek Vasut49427232018-12-12 02:39:58 +0100195 struct regmap *regmap;
eric miao9e60fdc2008-02-04 22:28:26 -0800196
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800197#ifdef CONFIG_GPIO_PCA953X_IRQ
198 struct mutex irq_lock;
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800199 DECLARE_BITMAP(irq_mask, MAX_LINE);
200 DECLARE_BITMAP(irq_stat, MAX_LINE);
201 DECLARE_BITMAP(irq_trig_raise, MAX_LINE);
202 DECLARE_BITMAP(irq_trig_fall, MAX_LINE);
Thomas Petazzoni5c4fee62019-01-16 10:31:58 +0100203 struct irq_chip irq_chip;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800204#endif
Geert Uytterhoevenf70fbc12019-03-20 11:39:27 +0100205 atomic_t wakeup_path;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800206
eric miao9e60fdc2008-02-04 22:28:26 -0800207 struct i2c_client *client;
208 struct gpio_chip gpio_chip;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700209 const char *const *names;
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300210 unsigned long driver_data;
Phil Reide23efa32016-07-29 11:39:55 +0800211 struct regulator *regulator;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200212
213 const struct pca953x_reg_config *regs;
eric miao9e60fdc2008-02-04 22:28:26 -0800214};
215
Marek Vasut873d1e82018-12-12 02:39:49 +0100216static int pca953x_bank_shift(struct pca953x_chip *chip)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100217{
Marek Vasut873d1e82018-12-12 02:39:49 +0100218 return fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
219}
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100220
Marek Vasut49427232018-12-12 02:39:58 +0100221#define PCA953x_BANK_INPUT BIT(0)
222#define PCA953x_BANK_OUTPUT BIT(1)
223#define PCA953x_BANK_POLARITY BIT(2)
224#define PCA953x_BANK_CONFIG BIT(3)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100225
Marek Vasut49427232018-12-12 02:39:58 +0100226#define PCA957x_BANK_INPUT BIT(0)
227#define PCA957x_BANK_POLARITY BIT(1)
228#define PCA957x_BANK_BUSHOLD BIT(2)
229#define PCA957x_BANK_CONFIG BIT(4)
230#define PCA957x_BANK_OUTPUT BIT(5)
231
232#define PCAL9xxx_BANK_IN_LATCH BIT(8 + 2)
Thomas Petazzoni15add06842019-02-07 17:28:59 +0100233#define PCAL9xxx_BANK_PULL_EN BIT(8 + 3)
234#define PCAL9xxx_BANK_PULL_SEL BIT(8 + 4)
Marek Vasut49427232018-12-12 02:39:58 +0100235#define PCAL9xxx_BANK_IRQ_MASK BIT(8 + 5)
236#define PCAL9xxx_BANK_IRQ_STAT BIT(8 + 6)
237
238/*
239 * We care about the following registers:
240 * - Standard set, below 0x40, each port can be replicated up to 8 times
241 * - PCA953x standard
242 * Input port 0x00 + 0 * bank_size R
243 * Output port 0x00 + 1 * bank_size RW
244 * Polarity Inversion port 0x00 + 2 * bank_size RW
245 * Configuration port 0x00 + 3 * bank_size RW
246 * - PCA957x with mixed up registers
247 * Input port 0x00 + 0 * bank_size R
248 * Polarity Inversion port 0x00 + 1 * bank_size RW
249 * Bus hold port 0x00 + 2 * bank_size RW
250 * Configuration port 0x00 + 4 * bank_size RW
251 * Output port 0x00 + 5 * bank_size RW
252 *
253 * - Extended set, above 0x40, often chip specific.
254 * - PCAL6524/PCAL9555A with custom PCAL IRQ handling:
255 * Input latch register 0x40 + 2 * bank_size RW
Thomas Petazzoni15add06842019-02-07 17:28:59 +0100256 * Pull-up/pull-down enable reg 0x40 + 3 * bank_size RW
257 * Pull-up/pull-down select reg 0x40 + 4 * bank_size RW
Marek Vasut49427232018-12-12 02:39:58 +0100258 * Interrupt mask register 0x40 + 5 * bank_size RW
259 * Interrupt status register 0x40 + 6 * bank_size R
260 *
261 * - Registers with bit 0x80 set, the AI bit
262 * The bit is cleared and the registers fall into one of the
263 * categories above.
264 */
265
266static bool pca953x_check_register(struct pca953x_chip *chip, unsigned int reg,
267 u32 checkbank)
268{
269 int bank_shift = pca953x_bank_shift(chip);
270 int bank = (reg & REG_ADDR_MASK) >> bank_shift;
271 int offset = reg & (BIT(bank_shift) - 1);
272
273 /* Special PCAL extended register check. */
274 if (reg & REG_ADDR_EXT) {
275 if (!(chip->driver_data & PCA_PCAL))
276 return false;
277 bank += 8;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100278 }
279
Marek Vasut49427232018-12-12 02:39:58 +0100280 /* Register is not in the matching bank. */
281 if (!(BIT(bank) & checkbank))
282 return false;
283
284 /* Register is not within allowed range of bank. */
285 if (offset >= NBANK(chip))
286 return false;
287
288 return true;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100289}
290
Marek Vasut49427232018-12-12 02:39:58 +0100291static bool pca953x_readable_register(struct device *dev, unsigned int reg)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100292{
Marek Vasut49427232018-12-12 02:39:58 +0100293 struct pca953x_chip *chip = dev_get_drvdata(dev);
294 u32 bank;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100295
Marek Vasut49427232018-12-12 02:39:58 +0100296 if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE) {
297 bank = PCA953x_BANK_INPUT | PCA953x_BANK_OUTPUT |
298 PCA953x_BANK_POLARITY | PCA953x_BANK_CONFIG;
299 } else {
300 bank = PCA957x_BANK_INPUT | PCA957x_BANK_OUTPUT |
301 PCA957x_BANK_POLARITY | PCA957x_BANK_CONFIG |
302 PCA957x_BANK_BUSHOLD;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100303 }
304
Marek Vasut49427232018-12-12 02:39:58 +0100305 if (chip->driver_data & PCA_PCAL) {
Thomas Petazzoni15add06842019-02-07 17:28:59 +0100306 bank |= PCAL9xxx_BANK_IN_LATCH | PCAL9xxx_BANK_PULL_EN |
307 PCAL9xxx_BANK_PULL_SEL | PCAL9xxx_BANK_IRQ_MASK |
Marek Vasut49427232018-12-12 02:39:58 +0100308 PCAL9xxx_BANK_IRQ_STAT;
309 }
310
311 return pca953x_check_register(chip, reg, bank);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100312}
313
Marek Vasut49427232018-12-12 02:39:58 +0100314static bool pca953x_writeable_register(struct device *dev, unsigned int reg)
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200315{
Marek Vasut49427232018-12-12 02:39:58 +0100316 struct pca953x_chip *chip = dev_get_drvdata(dev);
317 u32 bank;
318
319 if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE) {
320 bank = PCA953x_BANK_OUTPUT | PCA953x_BANK_POLARITY |
321 PCA953x_BANK_CONFIG;
322 } else {
323 bank = PCA957x_BANK_OUTPUT | PCA957x_BANK_POLARITY |
324 PCA957x_BANK_CONFIG | PCA957x_BANK_BUSHOLD;
325 }
326
327 if (chip->driver_data & PCA_PCAL)
Thomas Petazzoni15add06842019-02-07 17:28:59 +0100328 bank |= PCAL9xxx_BANK_IN_LATCH | PCAL9xxx_BANK_PULL_EN |
329 PCAL9xxx_BANK_PULL_SEL | PCAL9xxx_BANK_IRQ_MASK;
Marek Vasut49427232018-12-12 02:39:58 +0100330
331 return pca953x_check_register(chip, reg, bank);
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200332}
333
Marek Vasut49427232018-12-12 02:39:58 +0100334static bool pca953x_volatile_register(struct device *dev, unsigned int reg)
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200335{
Marek Vasut49427232018-12-12 02:39:58 +0100336 struct pca953x_chip *chip = dev_get_drvdata(dev);
337 u32 bank;
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200338
Marek Vasut49427232018-12-12 02:39:58 +0100339 if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE)
340 bank = PCA953x_BANK_INPUT;
341 else
342 bank = PCA957x_BANK_INPUT;
343
344 if (chip->driver_data & PCA_PCAL)
345 bank |= PCAL9xxx_BANK_IRQ_STAT;
346
347 return pca953x_check_register(chip, reg, bank);
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200348}
349
Wei Yongjund04e7792019-01-10 14:07:42 +0000350static const struct regmap_config pca953x_i2c_regmap = {
Marek Vasut49427232018-12-12 02:39:58 +0100351 .reg_bits = 8,
352 .val_bits = 8,
353
354 .readable_reg = pca953x_readable_register,
355 .writeable_reg = pca953x_writeable_register,
356 .volatile_reg = pca953x_volatile_register,
357
Bartosz Golaszewskiaa58a212020-04-07 17:42:45 +0200358 .disable_locking = true,
Marek Vasut49427232018-12-12 02:39:58 +0100359 .cache_type = REGCACHE_RBTREE,
Uwe Kleine-Königbcf41dc2020-04-20 20:27:51 +0300360 .max_register = 0x7f,
361};
362
363static const struct regmap_config pca953x_ai_i2c_regmap = {
364 .reg_bits = 8,
365 .val_bits = 8,
366
367 .read_flag_mask = REG_ADDR_AI,
368 .write_flag_mask = REG_ADDR_AI,
369
370 .readable_reg = pca953x_readable_register,
371 .writeable_reg = pca953x_writeable_register,
372 .volatile_reg = pca953x_volatile_register,
373
Andy Shevchenkoec3decd2020-06-05 16:40:36 +0300374 .disable_locking = true,
Uwe Kleine-Königbcf41dc2020-04-20 20:27:51 +0300375 .cache_type = REGCACHE_RBTREE,
376 .max_register = 0x7f,
Marek Vasut49427232018-12-12 02:39:58 +0100377};
378
Uwe Kleine-König6fdeb6c2020-04-20 20:27:52 +0300379static u8 pca953x_recalc_addr(struct pca953x_chip *chip, int reg, int off)
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200380{
Marek Vasutb32cecb2018-12-12 02:39:57 +0100381 int bank_shift = pca953x_bank_shift(chip);
H. Nikolaus Schallerd5dbf9c2018-05-17 06:59:49 +0200382 int addr = (reg & PCAL_GPIO_MASK) << bank_shift;
383 int pinctrl = (reg & PCAL_PINCTRL_MASK) << 1;
Marek Vasutb32cecb2018-12-12 02:39:57 +0100384 u8 regaddr = pinctrl | addr | (off / BANK_SZ);
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200385
Marek Vasutb32cecb2018-12-12 02:39:57 +0100386 return regaddr;
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200387}
388
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800389static int pca953x_write_regs(struct pca953x_chip *chip, int reg, unsigned long *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800390{
Uwe Kleine-König6fdeb6c2020-04-20 20:27:52 +0300391 u8 regaddr = pca953x_recalc_addr(chip, reg, 0);
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800392 u8 value[MAX_BANK];
393 int i, ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800394
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800395 for (i = 0; i < NBANK(chip); i++)
396 value[i] = bitmap_get_value8(val, i * BANK_SZ);
397
398 ret = regmap_bulk_write(chip->regmap, regaddr, value, NBANK(chip));
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800399 if (ret < 0) {
400 dev_err(&chip->client->dev, "failed writing register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800401 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800402 }
403
404 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800405}
406
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800407static int pca953x_read_regs(struct pca953x_chip *chip, int reg, unsigned long *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800408{
Uwe Kleine-König6fdeb6c2020-04-20 20:27:52 +0300409 u8 regaddr = pca953x_recalc_addr(chip, reg, 0);
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800410 u8 value[MAX_BANK];
411 int i, ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800412
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800413 ret = regmap_bulk_read(chip->regmap, regaddr, value, NBANK(chip));
eric miao9e60fdc2008-02-04 22:28:26 -0800414 if (ret < 0) {
415 dev_err(&chip->client->dev, "failed reading register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800416 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800417 }
418
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800419 for (i = 0; i < NBANK(chip); i++)
420 bitmap_set_value8(val, value[i], i * BANK_SZ);
421
eric miao9e60fdc2008-02-04 22:28:26 -0800422 return 0;
423}
424
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800425static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800426{
Linus Walleij468e67f2015-12-07 11:20:54 +0100427 struct pca953x_chip *chip = gpiochip_get_data(gc);
Uwe Kleine-König6fdeb6c2020-04-20 20:27:52 +0300428 u8 dirreg = pca953x_recalc_addr(chip, chip->regs->direction, off);
Marek Vasut0f25fda2018-12-12 02:39:59 +0100429 u8 bit = BIT(off % BANK_SZ);
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200430 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800431
Roland Stigge6e20fb12011-02-10 15:01:23 -0800432 mutex_lock(&chip->i2c_lock);
Marek Vasut0f25fda2018-12-12 02:39:59 +0100433 ret = regmap_write_bits(chip->regmap, dirreg, bit, bit);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800434 mutex_unlock(&chip->i2c_lock);
435 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800436}
437
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800438static int pca953x_gpio_direction_output(struct gpio_chip *gc,
eric miao9e60fdc2008-02-04 22:28:26 -0800439 unsigned off, int val)
440{
Linus Walleij468e67f2015-12-07 11:20:54 +0100441 struct pca953x_chip *chip = gpiochip_get_data(gc);
Uwe Kleine-König6fdeb6c2020-04-20 20:27:52 +0300442 u8 dirreg = pca953x_recalc_addr(chip, chip->regs->direction, off);
443 u8 outreg = pca953x_recalc_addr(chip, chip->regs->output, off);
Marek Vasut0f25fda2018-12-12 02:39:59 +0100444 u8 bit = BIT(off % BANK_SZ);
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200445 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800446
Roland Stigge6e20fb12011-02-10 15:01:23 -0800447 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800448 /* set output level */
Marek Vasutec82d1e2018-12-12 02:40:00 +0100449 ret = regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0);
eric miao9e60fdc2008-02-04 22:28:26 -0800450 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800451 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800452
eric miao9e60fdc2008-02-04 22:28:26 -0800453 /* then direction */
Marek Vasut0f25fda2018-12-12 02:39:59 +0100454 ret = regmap_write_bits(chip->regmap, dirreg, bit, 0);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800455exit:
456 mutex_unlock(&chip->i2c_lock);
457 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800458}
459
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800460static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800461{
Linus Walleij468e67f2015-12-07 11:20:54 +0100462 struct pca953x_chip *chip = gpiochip_get_data(gc);
Uwe Kleine-König6fdeb6c2020-04-20 20:27:52 +0300463 u8 inreg = pca953x_recalc_addr(chip, chip->regs->input, off);
Marek Vasut87813cf2018-12-12 02:40:01 +0100464 u8 bit = BIT(off % BANK_SZ);
Andreas Schallenbergae79c192012-05-09 09:46:17 +0200465 u32 reg_val;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200466 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800467
Roland Stigge6e20fb12011-02-10 15:01:23 -0800468 mutex_lock(&chip->i2c_lock);
Marek Vasut87813cf2018-12-12 02:40:01 +0100469 ret = regmap_read(chip->regmap, inreg, &reg_val);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800470 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800471 if (ret < 0) {
Andy Shevchenkob27d8512019-12-04 16:53:40 -0800472 /*
473 * NOTE:
474 * diagnostic already emitted; that's all we should
eric miao9e60fdc2008-02-04 22:28:26 -0800475 * do unless gpio_*_value_cansleep() calls become different
476 * from their nonsleeping siblings (and report faults).
477 */
478 return 0;
479 }
480
Marek Vasut87813cf2018-12-12 02:40:01 +0100481 return !!(reg_val & bit);
eric miao9e60fdc2008-02-04 22:28:26 -0800482}
483
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800484static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800485{
Linus Walleij468e67f2015-12-07 11:20:54 +0100486 struct pca953x_chip *chip = gpiochip_get_data(gc);
Uwe Kleine-König6fdeb6c2020-04-20 20:27:52 +0300487 u8 outreg = pca953x_recalc_addr(chip, chip->regs->output, off);
Marek Vasutec82d1e2018-12-12 02:40:00 +0100488 u8 bit = BIT(off % BANK_SZ);
eric miao9e60fdc2008-02-04 22:28:26 -0800489
Roland Stigge6e20fb12011-02-10 15:01:23 -0800490 mutex_lock(&chip->i2c_lock);
Marek Vasutec82d1e2018-12-12 02:40:00 +0100491 regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800492 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800493}
494
Andy Shevchenko66e57192017-03-22 16:11:11 +0200495static int pca953x_gpio_get_direction(struct gpio_chip *gc, unsigned off)
496{
497 struct pca953x_chip *chip = gpiochip_get_data(gc);
Uwe Kleine-König6fdeb6c2020-04-20 20:27:52 +0300498 u8 dirreg = pca953x_recalc_addr(chip, chip->regs->direction, off);
Marek Vasut0f25fda2018-12-12 02:39:59 +0100499 u8 bit = BIT(off % BANK_SZ);
Andy Shevchenko66e57192017-03-22 16:11:11 +0200500 u32 reg_val;
501 int ret;
502
503 mutex_lock(&chip->i2c_lock);
Marek Vasut0f25fda2018-12-12 02:39:59 +0100504 ret = regmap_read(chip->regmap, dirreg, &reg_val);
Andy Shevchenko66e57192017-03-22 16:11:11 +0200505 mutex_unlock(&chip->i2c_lock);
506 if (ret < 0)
507 return ret;
508
Matti Vaittinene42615e2019-11-06 10:54:12 +0200509 if (reg_val & bit)
510 return GPIO_LINE_DIRECTION_IN;
511
512 return GPIO_LINE_DIRECTION_OUT;
Andy Shevchenko66e57192017-03-22 16:11:11 +0200513}
514
Paul Thomas96d7c7b32020-04-14 11:28:42 -0400515static int pca953x_gpio_get_multiple(struct gpio_chip *gc,
Andy Shevchenko6f793482020-04-20 20:27:50 +0300516 unsigned long *mask, unsigned long *bits)
Paul Thomas96d7c7b32020-04-14 11:28:42 -0400517{
518 struct pca953x_chip *chip = gpiochip_get_data(gc);
Andy Shevchenko6f793482020-04-20 20:27:50 +0300519 DECLARE_BITMAP(reg_val, MAX_LINE);
520 int ret;
Paul Thomas96d7c7b32020-04-14 11:28:42 -0400521
Andy Shevchenko6f793482020-04-20 20:27:50 +0300522 mutex_lock(&chip->i2c_lock);
523 ret = pca953x_read_regs(chip, chip->regs->input, reg_val);
524 mutex_unlock(&chip->i2c_lock);
525 if (ret)
526 return ret;
527
528 bitmap_replace(bits, bits, reg_val, mask, gc->ngpio);
529 return 0;
Paul Thomas96d7c7b32020-04-14 11:28:42 -0400530}
531
Phil Reidb4818af2015-12-04 15:52:30 +0800532static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
Bartosz Golaszewskiea3d5792016-09-09 11:17:38 +0200533 unsigned long *mask, unsigned long *bits)
Phil Reidb4818af2015-12-04 15:52:30 +0800534{
Linus Walleij468e67f2015-12-07 11:20:54 +0100535 struct pca953x_chip *chip = gpiochip_get_data(gc);
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800536 DECLARE_BITMAP(reg_val, MAX_LINE);
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200537 int ret;
Bartosz Golaszewskiea3d5792016-09-09 11:17:38 +0200538
Phil Reidb4818af2015-12-04 15:52:30 +0800539 mutex_lock(&chip->i2c_lock);
Marek Vasutec82d1e2018-12-12 02:40:00 +0100540 ret = pca953x_read_regs(chip, chip->regs->output, reg_val);
541 if (ret)
542 goto exit;
543
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800544 bitmap_replace(reg_val, reg_val, bits, mask, gc->ngpio);
Bartosz Golaszewskiea3d5792016-09-09 11:17:38 +0200545
Marek Vasutec82d1e2018-12-12 02:40:00 +0100546 pca953x_write_regs(chip, chip->regs->output, reg_val);
Phil Reidb4818af2015-12-04 15:52:30 +0800547exit:
548 mutex_unlock(&chip->i2c_lock);
549}
550
Thomas Petazzoni15add06842019-02-07 17:28:59 +0100551static int pca953x_gpio_set_pull_up_down(struct pca953x_chip *chip,
552 unsigned int offset,
553 unsigned long config)
554{
Uwe Kleine-König6fdeb6c2020-04-20 20:27:52 +0300555 u8 pull_en_reg = pca953x_recalc_addr(chip, PCAL953X_PULL_EN, offset);
556 u8 pull_sel_reg = pca953x_recalc_addr(chip, PCAL953X_PULL_SEL, offset);
Thomas Petazzoni15add06842019-02-07 17:28:59 +0100557 u8 bit = BIT(offset % BANK_SZ);
558 int ret;
559
560 /*
561 * pull-up/pull-down configuration requires PCAL extended
562 * registers
563 */
564 if (!(chip->driver_data & PCA_PCAL))
565 return -ENOTSUPP;
566
567 mutex_lock(&chip->i2c_lock);
568
569 /* Disable pull-up/pull-down */
570 ret = regmap_write_bits(chip->regmap, pull_en_reg, bit, 0);
571 if (ret)
572 goto exit;
573
574 /* Configure pull-up/pull-down */
575 if (config == PIN_CONFIG_BIAS_PULL_UP)
576 ret = regmap_write_bits(chip->regmap, pull_sel_reg, bit, bit);
577 else if (config == PIN_CONFIG_BIAS_PULL_DOWN)
578 ret = regmap_write_bits(chip->regmap, pull_sel_reg, bit, 0);
579 if (ret)
580 goto exit;
581
582 /* Enable pull-up/pull-down */
583 ret = regmap_write_bits(chip->regmap, pull_en_reg, bit, bit);
584
585exit:
586 mutex_unlock(&chip->i2c_lock);
587 return ret;
588}
589
590static int pca953x_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
591 unsigned long config)
592{
593 struct pca953x_chip *chip = gpiochip_get_data(gc);
594
Adam Forddc87f6d2020-04-11 20:33:52 -0500595 switch (pinconf_to_config_param(config)) {
Thomas Petazzoni15add06842019-02-07 17:28:59 +0100596 case PIN_CONFIG_BIAS_PULL_UP:
597 case PIN_CONFIG_BIAS_PULL_DOWN:
598 return pca953x_gpio_set_pull_up_down(chip, offset, config);
599 default:
600 return -ENOTSUPP;
601 }
602}
603
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800604static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800605{
606 struct gpio_chip *gc;
607
608 gc = &chip->gpio_chip;
609
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800610 gc->direction_input = pca953x_gpio_direction_input;
611 gc->direction_output = pca953x_gpio_direction_output;
612 gc->get = pca953x_gpio_get_value;
613 gc->set = pca953x_gpio_set_value;
Andy Shevchenko66e57192017-03-22 16:11:11 +0200614 gc->get_direction = pca953x_gpio_get_direction;
Paul Thomas96d7c7b32020-04-14 11:28:42 -0400615 gc->get_multiple = pca953x_gpio_get_multiple;
Phil Reidb4818af2015-12-04 15:52:30 +0800616 gc->set_multiple = pca953x_gpio_set_multiple;
Thomas Petazzoni15add06842019-02-07 17:28:59 +0100617 gc->set_config = pca953x_gpio_set_config;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100618 gc->can_sleep = true;
eric miao9e60fdc2008-02-04 22:28:26 -0800619
620 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800621 gc->ngpio = gpios;
Linus Walleij5128f8d2018-12-02 09:43:21 +0100622 gc->label = dev_name(&chip->client->dev);
Linus Walleij58383c782015-11-04 09:56:26 +0100623 gc->parent = &chip->client->dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700624 gc->owner = THIS_MODULE;
Daniel Silverstone77906a542009-06-17 16:26:15 -0700625 gc->names = chip->names;
eric miao9e60fdc2008-02-04 22:28:26 -0800626}
627
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800628#ifdef CONFIG_GPIO_PCA953X_IRQ
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800629static void pca953x_irq_mask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800630{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200631 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100632 struct pca953x_chip *chip = gpiochip_get_data(gc);
Andy Shevchenko26883022019-12-09 14:35:44 +0200633 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800634
Andy Shevchenko26883022019-12-09 14:35:44 +0200635 clear_bit(hwirq, chip->irq_mask);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800636}
637
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800638static void pca953x_irq_unmask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800639{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200640 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100641 struct pca953x_chip *chip = gpiochip_get_data(gc);
Andy Shevchenko26883022019-12-09 14:35:44 +0200642 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800643
Andy Shevchenko26883022019-12-09 14:35:44 +0200644 set_bit(hwirq, chip->irq_mask);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800645}
646
Geert Uytterhoeven2a9a2f22019-02-13 14:15:03 +0100647static int pca953x_irq_set_wake(struct irq_data *d, unsigned int on)
648{
649 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
650 struct pca953x_chip *chip = gpiochip_get_data(gc);
651
Geert Uytterhoevenf70fbc12019-03-20 11:39:27 +0100652 if (on)
653 atomic_inc(&chip->wakeup_path);
654 else
655 atomic_dec(&chip->wakeup_path);
656
Geert Uytterhoeven2a9a2f22019-02-13 14:15:03 +0100657 return irq_set_irq_wake(chip->client->irq, on);
658}
659
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800660static void pca953x_irq_bus_lock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800661{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200662 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100663 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800664
665 mutex_lock(&chip->irq_lock);
666}
667
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800668static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800669{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200670 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100671 struct pca953x_chip *chip = gpiochip_get_data(gc);
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800672 DECLARE_BITMAP(irq_mask, MAX_LINE);
673 DECLARE_BITMAP(reg_direction, MAX_LINE);
674 int level;
Marek Vasut0f25fda2018-12-12 02:39:59 +0100675
Yong Li44896be2016-04-07 12:56:32 +0800676 if (chip->driver_data & PCA_PCAL) {
677 /* Enable latch on interrupt-enabled inputs */
678 pca953x_write_regs(chip, PCAL953X_IN_LATCH, chip->irq_mask);
679
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800680 bitmap_complement(irq_mask, chip->irq_mask, gc->ngpio);
Yong Li44896be2016-04-07 12:56:32 +0800681
682 /* Unmask enabled interrupts */
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800683 pca953x_write_regs(chip, PCAL953X_INT_MASK, irq_mask);
Yong Li44896be2016-04-07 12:56:32 +0800684 }
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700685
Andy Shevchenko0b22c252020-06-05 16:40:35 +0300686 /* Switch direction to input if needed */
687 pca953x_read_regs(chip, chip->regs->direction, reg_direction);
688
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800689 bitmap_or(irq_mask, chip->irq_trig_fall, chip->irq_trig_raise, gc->ngpio);
Andy Shevchenko0b22c252020-06-05 16:40:35 +0300690 bitmap_complement(reg_direction, reg_direction, gc->ngpio);
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800691 bitmap_and(irq_mask, irq_mask, reg_direction, gc->ngpio);
692
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700693 /* Look for any newly setup interrupt */
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800694 for_each_set_bit(level, irq_mask, gc->ngpio)
695 pca953x_gpio_direction_input(&chip->gpio_chip, level);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800696
697 mutex_unlock(&chip->irq_lock);
698}
699
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800700static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800701{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200702 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100703 struct pca953x_chip *chip = gpiochip_get_data(gc);
Andy Shevchenko26883022019-12-09 14:35:44 +0200704 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800705
706 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
707 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800708 d->irq, type);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800709 return -EINVAL;
710 }
711
Andy Shevchenko26883022019-12-09 14:35:44 +0200712 assign_bit(hwirq, chip->irq_trig_fall, type & IRQ_TYPE_EDGE_FALLING);
713 assign_bit(hwirq, chip->irq_trig_raise, type & IRQ_TYPE_EDGE_RISING);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800714
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700715 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800716}
717
Grigoryev Denis0a70fe02018-05-04 16:53:18 +0000718static void pca953x_irq_shutdown(struct irq_data *d)
719{
Mark Waltonc378b3a2019-02-28 15:46:36 +0000720 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
721 struct pca953x_chip *chip = gpiochip_get_data(gc);
Andy Shevchenko26883022019-12-09 14:35:44 +0200722 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Grigoryev Denis0a70fe02018-05-04 16:53:18 +0000723
Andy Shevchenko26883022019-12-09 14:35:44 +0200724 clear_bit(hwirq, chip->irq_trig_raise);
725 clear_bit(hwirq, chip->irq_trig_fall);
Grigoryev Denis0a70fe02018-05-04 16:53:18 +0000726}
727
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800728static bool pca953x_irq_pending(struct pca953x_chip *chip, unsigned long *pending)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800729{
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800730 struct gpio_chip *gc = &chip->gpio_chip;
731 DECLARE_BITMAP(reg_direction, MAX_LINE);
732 DECLARE_BITMAP(old_stat, MAX_LINE);
733 DECLARE_BITMAP(cur_stat, MAX_LINE);
734 DECLARE_BITMAP(new_stat, MAX_LINE);
735 DECLARE_BITMAP(trigger, MAX_LINE);
736 int ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800737
Yong Li44896be2016-04-07 12:56:32 +0800738 if (chip->driver_data & PCA_PCAL) {
739 /* Read the current interrupt status from the device */
740 ret = pca953x_read_regs(chip, PCAL953X_INT_STAT, trigger);
741 if (ret)
742 return false;
743
744 /* Check latched inputs and clear interrupt status */
Andy Shevchenko0a0a0212019-12-04 16:53:33 -0800745 ret = pca953x_read_regs(chip, chip->regs->input, cur_stat);
Yong Li44896be2016-04-07 12:56:32 +0800746 if (ret)
747 return false;
748
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800749 /* Apply filter for rising/falling edge selection */
750 bitmap_replace(new_stat, chip->irq_trig_fall, chip->irq_trig_raise, cur_stat, gc->ngpio);
Yong Li44896be2016-04-07 12:56:32 +0800751
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800752 bitmap_and(pending, new_stat, trigger, gc->ngpio);
753
754 return !bitmap_empty(pending, gc->ngpio);
Yong Li44896be2016-04-07 12:56:32 +0800755 }
756
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200757 ret = pca953x_read_regs(chip, chip->regs->input, cur_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800758 if (ret)
Joshua Scottb6ac1282015-05-22 12:35:12 +1200759 return false;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800760
761 /* Remove output pins from the equation */
David Jander438b6c22019-08-27 06:46:29 +0000762 pca953x_read_regs(chip, chip->regs->direction, reg_direction);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800763
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800764 bitmap_copy(old_stat, chip->irq_stat, gc->ngpio);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800765
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800766 bitmap_and(new_stat, cur_stat, reg_direction, gc->ngpio);
767 bitmap_xor(cur_stat, new_stat, old_stat, gc->ngpio);
768 bitmap_and(trigger, cur_stat, chip->irq_mask, gc->ngpio);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100769
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800770 if (bitmap_empty(trigger, gc->ngpio))
Joshua Scottb6ac1282015-05-22 12:35:12 +1200771 return false;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800772
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800773 bitmap_copy(chip->irq_stat, new_stat, gc->ngpio);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800774
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800775 bitmap_and(cur_stat, chip->irq_trig_fall, old_stat, gc->ngpio);
776 bitmap_and(old_stat, chip->irq_trig_raise, new_stat, gc->ngpio);
777 bitmap_or(new_stat, old_stat, cur_stat, gc->ngpio);
778 bitmap_and(pending, new_stat, trigger, gc->ngpio);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800779
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800780 return !bitmap_empty(pending, gc->ngpio);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800781}
782
783static irqreturn_t pca953x_irq_handler(int irq, void *devid)
784{
785 struct pca953x_chip *chip = devid;
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800786 struct gpio_chip *gc = &chip->gpio_chip;
Andy Shevchenkoe09e2002020-09-30 17:20:12 +0300787 DECLARE_BITMAP(pending, MAX_LINE);
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800788 int level;
Andy Shevchenko064c73a2020-06-05 16:40:33 +0300789 bool ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800790
Andy Shevchenkoe09e2002020-09-30 17:20:12 +0300791 bitmap_zero(pending, MAX_LINE);
792
Andy Shevchenko064c73a2020-06-05 16:40:33 +0300793 mutex_lock(&chip->i2c_lock);
794 ret = pca953x_irq_pending(chip, pending);
795 mutex_unlock(&chip->i2c_lock);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800796
Marc Zyngier8b81edd2020-10-05 15:02:17 +0100797 if (ret) {
798 ret = 0;
799
800 for_each_set_bit(level, pending, gc->ngpio) {
801 int nested_irq = irq_find_mapping(gc->irq.domain, level);
802
803 if (unlikely(nested_irq <= 0)) {
804 dev_warn_ratelimited(gc->parent, "unmapped interrupt %d\n", level);
805 continue;
806 }
807
808 handle_nested_irq(nested_irq);
809 ret = 1;
810 }
811 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800812
Andy Shevchenko064c73a2020-06-05 16:40:33 +0300813 return IRQ_RETVAL(ret);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800814}
815
Andy Shevchenkob27d8512019-12-04 16:53:40 -0800816static int pca953x_irq_setup(struct pca953x_chip *chip, int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800817{
818 struct i2c_client *client = chip->client;
Thomas Petazzoni5c4fee62019-01-16 10:31:58 +0100819 struct irq_chip *irq_chip = &chip->irq_chip;
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800820 DECLARE_BITMAP(reg_direction, MAX_LINE);
821 DECLARE_BITMAP(irq_stat, MAX_LINE);
Linus Walleijedee3bc2020-07-17 16:40:40 +0200822 struct gpio_irq_chip *girq;
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800823 int ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800824
Andy Shevchenkoba8c90c2020-06-05 16:40:34 +0300825 if (dmi_first_match(pca953x_dmi_acpi_irq_info)) {
826 ret = pca953x_acpi_get_irq(&client->dev);
827 if (ret > 0)
828 client->irq = ret;
829 }
830
Thomas Petazzoni7341fa72019-01-16 10:31:57 +0100831 if (!client->irq)
832 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800833
Thomas Petazzoni7341fa72019-01-16 10:31:57 +0100834 if (irq_base == -1)
835 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800836
Thomas Petazzoni7341fa72019-01-16 10:31:57 +0100837 if (!(chip->driver_data & PCA_INT))
838 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800839
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800840 ret = pca953x_read_regs(chip, chip->regs->input, irq_stat);
Thomas Petazzoni7341fa72019-01-16 10:31:57 +0100841 if (ret)
842 return ret;
Grygorii Strashkofdd50402015-07-07 17:34:49 +0300843
Thomas Petazzoni7341fa72019-01-16 10:31:57 +0100844 /*
845 * There is no way to know which GPIO line generated the
846 * interrupt. We have to rely on the previous read for
847 * this purpose.
848 */
David Jander438b6c22019-08-27 06:46:29 +0000849 pca953x_read_regs(chip, chip->regs->direction, reg_direction);
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800850 bitmap_and(chip->irq_stat, irq_stat, reg_direction, chip->gpio_chip.ngpio);
Thomas Petazzoni7341fa72019-01-16 10:31:57 +0100851 mutex_init(&chip->irq_lock);
852
Andy Shevchenko2a93a0d2020-07-28 15:55:02 +0300853 irq_chip->name = dev_name(&client->dev);
Thomas Petazzoni5c4fee62019-01-16 10:31:58 +0100854 irq_chip->irq_mask = pca953x_irq_mask;
855 irq_chip->irq_unmask = pca953x_irq_unmask;
Geert Uytterhoeven2a9a2f22019-02-13 14:15:03 +0100856 irq_chip->irq_set_wake = pca953x_irq_set_wake;
Thomas Petazzoni5c4fee62019-01-16 10:31:58 +0100857 irq_chip->irq_bus_lock = pca953x_irq_bus_lock;
858 irq_chip->irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock;
859 irq_chip->irq_set_type = pca953x_irq_set_type;
860 irq_chip->irq_shutdown = pca953x_irq_shutdown;
861
Linus Walleijedee3bc2020-07-17 16:40:40 +0200862 girq = &chip->gpio_chip.irq;
863 girq->chip = irq_chip;
864 /* This will let us handle the parent IRQ in the driver */
865 girq->parent_handler = NULL;
866 girq->num_parents = 0;
867 girq->parents = NULL;
868 girq->default_type = IRQ_TYPE_NONE;
869 girq->handler = handle_simple_irq;
870 girq->threaded = true;
871 girq->first = irq_base; /* FIXME: get rid of this */
Thomas Petazzoni7341fa72019-01-16 10:31:57 +0100872
Andy Shevchenko2a93a0d2020-07-28 15:55:02 +0300873 ret = devm_request_threaded_irq(&client->dev, client->irq,
874 NULL, pca953x_irq_handler,
875 IRQF_ONESHOT | IRQF_SHARED,
876 dev_name(&client->dev), chip);
877 if (ret) {
878 dev_err(&client->dev, "failed to request irq %d\n",
879 client->irq);
880 return ret;
881 }
882
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800883 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800884}
885
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800886#else /* CONFIG_GPIO_PCA953X_IRQ */
887static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200888 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800889{
890 struct i2c_client *client = chip->client;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800891
Baruch Siach72b38ca2018-06-21 16:38:46 +0300892 if (client->irq && irq_base != -1 && (chip->driver_data & PCA_INT))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800893 dev_warn(&client->dev, "interrupt support not compiled in\n");
894
895 return 0;
896}
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800897#endif
898
Marek Vasut7a04aaa2018-12-12 02:39:55 +0100899static int device_pca95xx_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800900{
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800901 DECLARE_BITMAP(val, MAX_LINE);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800902 int ret;
903
Marek Vasutec82d1e2018-12-12 02:40:00 +0100904 ret = regcache_sync_region(chip->regmap, chip->regs->output,
905 chip->regs->output + NBANK(chip));
Andy Shevchenko6dd6a2d2019-08-01 20:39:37 +0300906 if (ret)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800907 goto out;
908
Marek Vasut0f25fda2018-12-12 02:39:59 +0100909 ret = regcache_sync_region(chip->regmap, chip->regs->direction,
910 chip->regs->direction + NBANK(chip));
Andy Shevchenko6dd6a2d2019-08-01 20:39:37 +0300911 if (ret)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800912 goto out;
913
914 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100915 if (invert)
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800916 bitmap_fill(val, MAX_LINE);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100917 else
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800918 bitmap_zero(val, MAX_LINE);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100919
Marek Vasut7a04aaa2018-12-12 02:39:55 +0100920 ret = pca953x_write_regs(chip, chip->regs->invert, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800921out:
922 return ret;
923}
924
Bill Pemberton38363092012-11-19 13:22:34 -0500925static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800926{
Andy Shevchenko35d13d92019-12-04 16:53:37 -0800927 DECLARE_BITMAP(val, MAX_LINE);
Andy Shevchenko8c1f1c342020-09-30 17:20:13 +0300928 unsigned int i;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800929 int ret;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800930
Marek Vasut7a04aaa2018-12-12 02:39:55 +0100931 ret = device_pca95xx_init(chip, invert);
Nicholas Krausec75a3772015-08-26 17:52:19 -0400932 if (ret)
933 goto out;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800934
Colin Cronin20a8a962015-05-18 11:41:43 -0700935 /* To enable register 6, 7 to control pull up and pull down */
Andy Shevchenko8c1f1c342020-09-30 17:20:13 +0300936 for (i = 0; i < NBANK(chip); i++)
937 bitmap_set_value8(val, 0x02, i * BANK_SZ);
938
Nicholas Krausec75a3772015-08-26 17:52:19 -0400939 ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
940 if (ret)
941 goto out;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800942
943 return 0;
944out:
945 return ret;
946}
947
Bill Pemberton38363092012-11-19 13:22:34 -0500948static int pca953x_probe(struct i2c_client *client,
Andy Shevchenkob27d8512019-12-04 16:53:40 -0800949 const struct i2c_device_id *i2c_id)
eric miao9e60fdc2008-02-04 22:28:26 -0800950{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800951 struct pca953x_platform_data *pdata;
952 struct pca953x_chip *chip;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530953 int irq_base = 0;
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200954 int ret;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530955 u32 invert = 0;
Phil Reide23efa32016-07-29 11:39:55 +0800956 struct regulator *reg;
Uwe Kleine-Königbcf41dc2020-04-20 20:27:51 +0300957 const struct regmap_config *regmap_config;
eric miao9e60fdc2008-02-04 22:28:26 -0800958
Andy Shevchenkob27d8512019-12-04 16:53:40 -0800959 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800960 if (chip == NULL)
961 return -ENOMEM;
962
Jingoo Hane56aee12013-07-30 17:08:05 +0900963 pdata = dev_get_platdata(&client->dev);
David Janderc6dcf592011-06-14 11:00:55 +0200964 if (pdata) {
965 irq_base = pdata->irq_base;
966 chip->gpio_start = pdata->gpio_base;
967 invert = pdata->invert;
968 chip->names = pdata->names;
969 } else {
Steve Longerbeam054ccde2017-01-10 11:29:51 -0800970 struct gpio_desc *reset_gpio;
971
Markus Pargmann4bb93342014-07-29 09:24:43 +0200972 chip->gpio_start = -1;
973 irq_base = 0;
Steve Longerbeam054ccde2017-01-10 11:29:51 -0800974
Andy Shevchenko96530b32017-03-22 16:11:13 +0200975 /*
976 * See if we need to de-assert a reset pin.
977 *
978 * There is no known ACPI-enabled platforms that are
979 * using "reset" GPIO. Otherwise any of those platform
980 * must use _DSD method with corresponding property.
981 */
Steve Longerbeam054ccde2017-01-10 11:29:51 -0800982 reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
983 GPIOD_OUT_LOW);
984 if (IS_ERR(reset_gpio))
985 return PTR_ERR(reset_gpio);
Nate Case1965d302009-06-17 16:26:17 -0700986 }
987
eric miao9e60fdc2008-02-04 22:28:26 -0800988 chip->client = client;
989
Phil Reide23efa32016-07-29 11:39:55 +0800990 reg = devm_regulator_get(&client->dev, "vcc");
Krzysztof Kozlowskica6a77e2020-08-27 22:08:25 +0200991 if (IS_ERR(reg))
992 return dev_err_probe(&client->dev, PTR_ERR(reg), "reg get err\n");
993
Phil Reide23efa32016-07-29 11:39:55 +0800994 ret = regulator_enable(reg);
995 if (ret) {
996 dev_err(&client->dev, "reg en err: %d\n", ret);
997 return ret;
998 }
999 chip->regulator = reg;
1000
Wolfram Sang6212e1d2016-09-24 11:06:29 +02001001 if (i2c_id) {
1002 chip->driver_data = i2c_id->driver_data;
Andy Shevchenkof32517b2015-10-01 14:20:28 +03001003 } else {
Andy Shevchenko8eeb467d2019-08-01 20:39:35 +03001004 const void *match;
Andy Shevchenkof32517b2015-10-01 14:20:28 +03001005
Andy Shevchenko8eeb467d2019-08-01 20:39:35 +03001006 match = device_get_match_data(&client->dev);
1007 if (!match) {
1008 ret = -ENODEV;
1009 goto err_exit;
Ben Dooks6f29c9a2015-12-08 10:19:26 +00001010 }
Andy Shevchenko8eeb467d2019-08-01 20:39:35 +03001011
1012 chip->driver_data = (uintptr_t)match;
Andy Shevchenkof32517b2015-10-01 14:20:28 +03001013 }
1014
Marek Vasut49427232018-12-12 02:39:58 +01001015 i2c_set_clientdata(client, chip);
1016
Uwe Kleine-Königbcf41dc2020-04-20 20:27:51 +03001017 pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
1018
1019 if (NBANK(chip) > 2 || PCA_CHIP_TYPE(chip->driver_data) == PCA957X_TYPE) {
1020 dev_info(&client->dev, "using AI\n");
1021 regmap_config = &pca953x_ai_i2c_regmap;
1022 } else {
1023 dev_info(&client->dev, "using no AI\n");
1024 regmap_config = &pca953x_i2c_regmap;
1025 }
1026
1027 chip->regmap = devm_regmap_init_i2c(client, regmap_config);
Marek Vasut49427232018-12-12 02:39:58 +01001028 if (IS_ERR(chip->regmap)) {
1029 ret = PTR_ERR(chip->regmap);
1030 goto err_exit;
1031 }
1032
Marek Vasut0f25fda2018-12-12 02:39:59 +01001033 regcache_mark_dirty(chip->regmap);
1034
Roland Stigge6e20fb12011-02-10 15:01:23 -08001035 mutex_init(&chip->i2c_lock);
Bartosz Golaszewski74f47f02016-09-26 11:54:15 +02001036 /*
1037 * In case we have an i2c-mux controlled by a GPIO provided by an
1038 * expander using the same driver higher on the device tree, read the
1039 * i2c adapter nesting depth and use the retrieved value as lockdep
1040 * subclass for chip->i2c_lock.
1041 *
1042 * REVISIT: This solution is not complete. It protects us from lockdep
1043 * false positives when the expander controlling the i2c-mux is on
1044 * a different level on the device tree, but not when it's on the same
1045 * level on a different branch (in which case the subclass number
1046 * would be the same).
1047 *
1048 * TODO: Once a correct solution is developed, a similar fix should be
1049 * applied to all other i2c-controlled GPIO expanders (and potentially
1050 * regmap-i2c).
1051 */
Bartosz Golaszewski559b4692016-09-16 18:02:45 +02001052 lockdep_set_subclass(&chip->i2c_lock,
1053 i2c_adapter_depth(client->adapter));
Roland Stigge6e20fb12011-02-10 15:01:23 -08001054
eric miao9e60fdc2008-02-04 22:28:26 -08001055 /* initialize cached registers from their original values.
1056 * we can't share this chip with another i2c master.
1057 */
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -08001058
Marek Vasut7a04aaa2018-12-12 02:39:55 +01001059 if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE) {
1060 chip->regs = &pca953x_regs;
1061 ret = device_pca95xx_init(chip, invert);
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +02001062 } else {
Marek Vasut7a04aaa2018-12-12 02:39:55 +01001063 chip->regs = &pca957x_regs;
Wolfram Sang7ea2aa22011-10-14 15:32:00 +02001064 ret = device_pca957x_init(chip, invert);
Marek Vasut7a04aaa2018-12-12 02:39:55 +01001065 }
Wolfram Sang7ea2aa22011-10-14 15:32:00 +02001066 if (ret)
Phil Reide23efa32016-07-29 11:39:55 +08001067 goto err_exit;
eric miao9e60fdc2008-02-04 22:28:26 -08001068
Linus Walleijedee3bc2020-07-17 16:40:40 +02001069 ret = pca953x_irq_setup(chip, irq_base);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -08001070 if (ret)
Phil Reide23efa32016-07-29 11:39:55 +08001071 goto err_exit;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -08001072
Linus Walleijedee3bc2020-07-17 16:40:40 +02001073 ret = devm_gpiochip_add_data(&client->dev, &chip->gpio_chip, chip);
eric miao9e60fdc2008-02-04 22:28:26 -08001074 if (ret)
Phil Reide23efa32016-07-29 11:39:55 +08001075 goto err_exit;
eric miao9e60fdc2008-02-04 22:28:26 -08001076
David Janderc6dcf592011-06-14 11:00:55 +02001077 if (pdata && pdata->setup) {
eric miao9e60fdc2008-02-04 22:28:26 -08001078 ret = pdata->setup(client, chip->gpio_chip.base,
Andy Shevchenkob27d8512019-12-04 16:53:40 -08001079 chip->gpio_chip.ngpio, pdata->context);
eric miao9e60fdc2008-02-04 22:28:26 -08001080 if (ret < 0)
1081 dev_warn(&client->dev, "setup failed, %d\n", ret);
1082 }
1083
eric miao9e60fdc2008-02-04 22:28:26 -08001084 return 0;
Phil Reide23efa32016-07-29 11:39:55 +08001085
1086err_exit:
1087 regulator_disable(chip->regulator);
1088 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -08001089}
1090
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001091static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -08001092{
Jingoo Hane56aee12013-07-30 17:08:05 +09001093 struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001094 struct pca953x_chip *chip = i2c_get_clientdata(client);
Linus Walleijd147d542016-09-13 14:43:23 +02001095 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -08001096
David Janderc6dcf592011-06-14 11:00:55 +02001097 if (pdata && pdata->teardown) {
eric miao9e60fdc2008-02-04 22:28:26 -08001098 ret = pdata->teardown(client, chip->gpio_chip.base,
Andy Shevchenkob27d8512019-12-04 16:53:40 -08001099 chip->gpio_chip.ngpio, pdata->context);
Phil Reide23efa32016-07-29 11:39:55 +08001100 if (ret < 0)
Andy Shevchenkof4160fa2019-08-01 20:39:38 +03001101 dev_err(&client->dev, "teardown failed, %d\n", ret);
Arnd Bergmannbf62efe2016-08-26 17:25:42 +02001102 } else {
1103 ret = 0;
eric miao9e60fdc2008-02-04 22:28:26 -08001104 }
1105
Phil Reide23efa32016-07-29 11:39:55 +08001106 regulator_disable(chip->regulator);
1107
1108 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -08001109}
1110
Marek Vasutb7657432018-12-12 02:40:02 +01001111#ifdef CONFIG_PM_SLEEP
1112static int pca953x_regcache_sync(struct device *dev)
1113{
1114 struct pca953x_chip *chip = dev_get_drvdata(dev);
1115 int ret;
1116
1117 /*
1118 * The ordering between direction and output is important,
1119 * sync these registers first and only then sync the rest.
1120 */
1121 ret = regcache_sync_region(chip->regmap, chip->regs->direction,
1122 chip->regs->direction + NBANK(chip));
Andy Shevchenko6dd6a2d2019-08-01 20:39:37 +03001123 if (ret) {
Marek Vasutb7657432018-12-12 02:40:02 +01001124 dev_err(dev, "Failed to sync GPIO dir registers: %d\n", ret);
1125 return ret;
1126 }
1127
1128 ret = regcache_sync_region(chip->regmap, chip->regs->output,
1129 chip->regs->output + NBANK(chip));
Andy Shevchenko6dd6a2d2019-08-01 20:39:37 +03001130 if (ret) {
Marek Vasutb7657432018-12-12 02:40:02 +01001131 dev_err(dev, "Failed to sync GPIO out registers: %d\n", ret);
1132 return ret;
1133 }
1134
1135#ifdef CONFIG_GPIO_PCA953X_IRQ
1136 if (chip->driver_data & PCA_PCAL) {
1137 ret = regcache_sync_region(chip->regmap, PCAL953X_IN_LATCH,
1138 PCAL953X_IN_LATCH + NBANK(chip));
Andy Shevchenko6dd6a2d2019-08-01 20:39:37 +03001139 if (ret) {
Marek Vasutb7657432018-12-12 02:40:02 +01001140 dev_err(dev, "Failed to sync INT latch registers: %d\n",
1141 ret);
1142 return ret;
1143 }
1144
1145 ret = regcache_sync_region(chip->regmap, PCAL953X_INT_MASK,
1146 PCAL953X_INT_MASK + NBANK(chip));
Andy Shevchenko6dd6a2d2019-08-01 20:39:37 +03001147 if (ret) {
Marek Vasutb7657432018-12-12 02:40:02 +01001148 dev_err(dev, "Failed to sync INT mask registers: %d\n",
1149 ret);
1150 return ret;
1151 }
1152 }
1153#endif
1154
1155 return 0;
1156}
1157
1158static int pca953x_suspend(struct device *dev)
1159{
1160 struct pca953x_chip *chip = dev_get_drvdata(dev);
1161
1162 regcache_cache_only(chip->regmap, true);
1163
Geert Uytterhoevenf70fbc12019-03-20 11:39:27 +01001164 if (atomic_read(&chip->wakeup_path))
1165 device_set_wakeup_path(dev);
1166 else
1167 regulator_disable(chip->regulator);
Marek Vasutb7657432018-12-12 02:40:02 +01001168
1169 return 0;
1170}
1171
1172static int pca953x_resume(struct device *dev)
1173{
1174 struct pca953x_chip *chip = dev_get_drvdata(dev);
1175 int ret;
1176
Geert Uytterhoevenf70fbc12019-03-20 11:39:27 +01001177 if (!atomic_read(&chip->wakeup_path)) {
1178 ret = regulator_enable(chip->regulator);
Andy Shevchenko6dd6a2d2019-08-01 20:39:37 +03001179 if (ret) {
Geert Uytterhoevenf70fbc12019-03-20 11:39:27 +01001180 dev_err(dev, "Failed to enable regulator: %d\n", ret);
1181 return 0;
1182 }
Marek Vasutb7657432018-12-12 02:40:02 +01001183 }
1184
1185 regcache_cache_only(chip->regmap, false);
1186 regcache_mark_dirty(chip->regmap);
1187 ret = pca953x_regcache_sync(dev);
1188 if (ret)
1189 return ret;
1190
1191 ret = regcache_sync(chip->regmap);
Andy Shevchenko6dd6a2d2019-08-01 20:39:37 +03001192 if (ret) {
Marek Vasutb7657432018-12-12 02:40:02 +01001193 dev_err(dev, "Failed to restore register map: %d\n", ret);
1194 return ret;
1195 }
1196
1197 return 0;
1198}
1199#endif
1200
Ben Dooks6f29c9a2015-12-08 10:19:26 +00001201/* convenience to stop overlong match-table lines */
1202#define OF_953X(__nrgpio, __int) (void *)(__nrgpio | PCA953X_TYPE | __int)
1203#define OF_957X(__nrgpio, __int) (void *)(__nrgpio | PCA957X_TYPE | __int)
1204
Maxime Riparded326202012-11-08 18:01:52 +01001205static const struct of_device_id pca953x_dt_ids[] = {
Alexandre Belloni12c7a4f2019-04-09 14:35:46 +02001206 { .compatible = "nxp,pca6416", .data = OF_953X(16, PCA_INT), },
Ben Dooks6f29c9a2015-12-08 10:19:26 +00001207 { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
Uwe Kleine-König1421b442021-01-15 17:46:56 +01001208 { .compatible = "nxp,pca9506", .data = OF_953X(40, PCA_INT), },
Ben Dooks6f29c9a2015-12-08 10:19:26 +00001209 { .compatible = "nxp,pca9534", .data = OF_953X( 8, PCA_INT), },
1210 { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
1211 { .compatible = "nxp,pca9536", .data = OF_953X( 4, 0), },
1212 { .compatible = "nxp,pca9537", .data = OF_953X( 4, PCA_INT), },
1213 { .compatible = "nxp,pca9538", .data = OF_953X( 8, PCA_INT), },
1214 { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
1215 { .compatible = "nxp,pca9554", .data = OF_953X( 8, PCA_INT), },
1216 { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
1217 { .compatible = "nxp,pca9556", .data = OF_953X( 8, 0), },
1218 { .compatible = "nxp,pca9557", .data = OF_953X( 8, 0), },
1219 { .compatible = "nxp,pca9574", .data = OF_957X( 8, PCA_INT), },
1220 { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
1221 { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },
Maxime Riparded326202012-11-08 18:01:52 +01001222
Alexandre Belloni01769c42019-04-09 12:25:17 +02001223 { .compatible = "nxp,pcal6416", .data = OF_953X(16, PCA_LATCH_INT), },
H. Nikolaus Schaller0cdf21b2018-05-17 06:59:47 +02001224 { .compatible = "nxp,pcal6524", .data = OF_953X(24, PCA_LATCH_INT), },
Jan Kiszka3ba3ff52020-06-15 15:22:29 +02001225 { .compatible = "nxp,pcal9535", .data = OF_953X(16, PCA_LATCH_INT), },
Mike Looijmans237d9612020-09-30 11:20:53 +02001226 { .compatible = "nxp,pcal9554b", .data = OF_953X( 8, PCA_LATCH_INT), },
H. Nikolaus Schaller0cdf21b2018-05-17 06:59:47 +02001227 { .compatible = "nxp,pcal9555a", .data = OF_953X(16, PCA_LATCH_INT), },
H. Nikolaus Schaller3a711e02018-03-10 12:00:01 +01001228
Ben Dooks6f29c9a2015-12-08 10:19:26 +00001229 { .compatible = "maxim,max7310", .data = OF_953X( 8, 0), },
1230 { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
1231 { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
1232 { .compatible = "maxim,max7315", .data = OF_953X( 8, PCA_INT), },
Marek Vasut1208c932016-10-17 18:36:49 +02001233 { .compatible = "maxim,max7318", .data = OF_953X(16, PCA_INT), },
Maxime Riparded326202012-11-08 18:01:52 +01001234
Ben Dooks6f29c9a2015-12-08 10:19:26 +00001235 { .compatible = "ti,pca6107", .data = OF_953X( 8, PCA_INT), },
Vignesh R353661d2016-05-19 12:17:29 +05301236 { .compatible = "ti,pca9536", .data = OF_953X( 4, 0), },
Ben Dooks6f29c9a2015-12-08 10:19:26 +00001237 { .compatible = "ti,tca6408", .data = OF_953X( 8, PCA_INT), },
1238 { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
1239 { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
Peter Robinson8b74ae42019-06-01 08:12:18 +01001240 { .compatible = "ti,tca9539", .data = OF_953X(16, PCA_INT), },
Aaron Sierrae73760a2014-02-07 16:36:21 -06001241
Geert Uytterhoeven932002f2019-03-21 10:21:45 +01001242 { .compatible = "onnn,cat9554", .data = OF_953X( 8, PCA_INT), },
Sergei Shtylyov8a64e552017-11-16 23:18:32 +03001243 { .compatible = "onnn,pca9654", .data = OF_953X( 8, PCA_INT), },
Ben Dooks9f49f6d2015-12-08 10:19:25 +00001244
Ben Dooks6f29c9a2015-12-08 10:19:26 +00001245 { .compatible = "exar,xra1202", .data = OF_953X( 8, 0), },
Maxime Riparded326202012-11-08 18:01:52 +01001246 { }
1247};
1248
1249MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
1250
Marek Vasutb7657432018-12-12 02:40:02 +01001251static SIMPLE_DEV_PM_OPS(pca953x_pm_ops, pca953x_suspend, pca953x_resume);
1252
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001253static struct i2c_driver pca953x_driver = {
eric miao9e60fdc2008-02-04 22:28:26 -08001254 .driver = {
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001255 .name = "pca953x",
Marek Vasutb7657432018-12-12 02:40:02 +01001256 .pm = &pca953x_pm_ops,
Maxime Riparded326202012-11-08 18:01:52 +01001257 .of_match_table = pca953x_dt_ids,
Andy Shevchenko74910e12020-05-21 00:19:14 +03001258 .acpi_match_table = pca953x_acpi_ids,
eric miao9e60fdc2008-02-04 22:28:26 -08001259 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001260 .probe = pca953x_probe,
1261 .remove = pca953x_remove,
Jean Delvare3760f732008-04-29 23:11:40 +02001262 .id_table = pca953x_id,
eric miao9e60fdc2008-02-04 22:28:26 -08001263};
1264
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001265static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -08001266{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001267 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -08001268}
David Brownell2f8d1192008-10-15 22:03:13 -07001269/* register after i2c postcore initcall and before
1270 * subsys initcalls that may rely on these GPIOs
1271 */
1272subsys_initcall(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -08001273
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001274static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -08001275{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001276 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -08001277}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001278module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -08001279
1280MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001281MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -08001282MODULE_LICENSE("GPL");