Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 2 | /* |
Aaron Sierra | 1e19169 | 2014-02-07 16:35:48 -0600 | [diff] [blame] | 3 | * PCA953x 4/8/16/24/40 bit I/O ports |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com> |
| 6 | * Copyright (C) 2007 Marvell International Ltd. |
| 7 | * |
| 8 | * Derived from drivers/i2c/chips/pca9539.c |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 9 | */ |
| 10 | |
Andy Shevchenko | b413d7a | 2017-03-22 16:11:12 +0200 | [diff] [blame] | 11 | #include <linux/acpi.h> |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 12 | #include <linux/bitmap.h> |
Linus Walleij | 644f3da | 2018-05-24 14:26:20 +0200 | [diff] [blame] | 13 | #include <linux/gpio/driver.h> |
Steve Longerbeam | 054ccde | 2017-01-10 11:29:51 -0800 | [diff] [blame] | 14 | #include <linux/gpio/consumer.h> |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 15 | #include <linux/i2c.h> |
Andy Shevchenko | b413d7a | 2017-03-22 16:11:12 +0200 | [diff] [blame] | 16 | #include <linux/init.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/module.h> |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 19 | #include <linux/of_platform.h> |
Andy Shevchenko | b413d7a | 2017-03-22 16:11:12 +0200 | [diff] [blame] | 20 | #include <linux/platform_data/pca953x.h> |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 21 | #include <linux/regmap.h> |
Phil Reid | e23efa3 | 2016-07-29 11:39:55 +0800 | [diff] [blame] | 22 | #include <linux/regulator/consumer.h> |
Andy Shevchenko | b413d7a | 2017-03-22 16:11:12 +0200 | [diff] [blame] | 23 | #include <linux/slab.h> |
| 24 | |
| 25 | #include <asm/unaligned.h> |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 26 | |
H. Nikolaus Schaller | 0950c19 | 2018-04-28 18:31:31 +0200 | [diff] [blame] | 27 | #define PCA953X_INPUT 0x00 |
| 28 | #define PCA953X_OUTPUT 0x01 |
| 29 | #define PCA953X_INVERT 0x02 |
| 30 | #define PCA953X_DIRECTION 0x03 |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 31 | |
Andy Shevchenko | 63b484c | 2019-08-01 20:39:36 +0300 | [diff] [blame] | 32 | #define REG_ADDR_MASK GENMASK(5, 0) |
| 33 | #define REG_ADDR_EXT BIT(6) |
| 34 | #define REG_ADDR_AI BIT(7) |
Andreas Schallenberg | ae79c19 | 2012-05-09 09:46:17 +0200 | [diff] [blame] | 35 | |
H. Nikolaus Schaller | 0950c19 | 2018-04-28 18:31:31 +0200 | [diff] [blame] | 36 | #define PCA957X_IN 0x00 |
| 37 | #define PCA957X_INVRT 0x01 |
| 38 | #define PCA957X_BKEN 0x02 |
| 39 | #define PCA957X_PUPD 0x03 |
| 40 | #define PCA957X_CFG 0x04 |
| 41 | #define PCA957X_OUT 0x05 |
| 42 | #define PCA957X_MSK 0x06 |
| 43 | #define PCA957X_INTS 0x07 |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 44 | |
H. Nikolaus Schaller | 6315d23 | 2018-04-28 18:31:32 +0200 | [diff] [blame] | 45 | #define PCAL953X_OUT_STRENGTH 0x20 |
H. Nikolaus Schaller | 0950c19 | 2018-04-28 18:31:31 +0200 | [diff] [blame] | 46 | #define PCAL953X_IN_LATCH 0x22 |
H. Nikolaus Schaller | 6315d23 | 2018-04-28 18:31:32 +0200 | [diff] [blame] | 47 | #define PCAL953X_PULL_EN 0x23 |
| 48 | #define PCAL953X_PULL_SEL 0x24 |
H. Nikolaus Schaller | 0950c19 | 2018-04-28 18:31:31 +0200 | [diff] [blame] | 49 | #define PCAL953X_INT_MASK 0x25 |
| 50 | #define PCAL953X_INT_STAT 0x26 |
H. Nikolaus Schaller | 6315d23 | 2018-04-28 18:31:32 +0200 | [diff] [blame] | 51 | #define PCAL953X_OUT_CONF 0x27 |
Yong Li | 44896be | 2016-04-07 12:56:32 +0800 | [diff] [blame] | 52 | |
H. Nikolaus Schaller | a0ecbcc | 2018-04-28 18:31:33 +0200 | [diff] [blame] | 53 | #define PCAL6524_INT_EDGE 0x28 |
| 54 | #define PCAL6524_INT_CLR 0x2a |
| 55 | #define PCAL6524_IN_STATUS 0x2b |
| 56 | #define PCAL6524_OUT_INDCONF 0x2c |
| 57 | #define PCAL6524_DEBOUNCE 0x2d |
| 58 | |
Andy Shevchenko | 63b484c | 2019-08-01 20:39:36 +0300 | [diff] [blame] | 59 | #define PCA_GPIO_MASK GENMASK(7, 0) |
H. Nikolaus Schaller | 394aeef | 2018-05-17 06:59:48 +0200 | [diff] [blame] | 60 | |
Andy Shevchenko | 63b484c | 2019-08-01 20:39:36 +0300 | [diff] [blame] | 61 | #define PCAL_GPIO_MASK GENMASK(4, 0) |
| 62 | #define PCAL_PINCTRL_MASK GENMASK(6, 5) |
H. Nikolaus Schaller | 394aeef | 2018-05-17 06:59:48 +0200 | [diff] [blame] | 63 | |
Andy Shevchenko | 63b484c | 2019-08-01 20:39:36 +0300 | [diff] [blame] | 64 | #define PCA_INT BIT(8) |
| 65 | #define PCA_PCAL BIT(9) |
Andy Shevchenko | 2870b3c | 2019-03-05 11:13:46 +0200 | [diff] [blame] | 66 | #define PCA_LATCH_INT (PCA_PCAL | PCA_INT) |
Andy Shevchenko | 63b484c | 2019-08-01 20:39:36 +0300 | [diff] [blame] | 67 | #define PCA953X_TYPE BIT(12) |
| 68 | #define PCA957X_TYPE BIT(13) |
| 69 | #define PCA_TYPE_MASK GENMASK(15, 12) |
Andy Shevchenko | c666414 | 2015-10-01 14:20:27 +0300 | [diff] [blame] | 70 | |
| 71 | #define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 72 | |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 73 | static const struct i2c_device_id pca953x_id[] = { |
Alexandre Belloni | 12c7a4f | 2019-04-09 14:35:46 +0200 | [diff] [blame] | 74 | { "pca6416", 16 | PCA953X_TYPE | PCA_INT, }, |
Gregory CLEMENT | 89f5df0 | 2013-01-22 22:10:24 +0100 | [diff] [blame] | 75 | { "pca9505", 40 | PCA953X_TYPE | PCA_INT, }, |
Uwe Kleine-König | 1421b44 | 2021-01-15 17:46:56 +0100 | [diff] [blame] | 76 | { "pca9506", 40 | PCA953X_TYPE | PCA_INT, }, |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 77 | { "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 Sierra | eb32b5a | 2014-02-13 13:59:23 +0100 | [diff] [blame] | 89 | { "pca9698", 40 | PCA953X_TYPE, }, |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 90 | |
Andy Shevchenko | 2870b3c | 2019-03-05 11:13:46 +0200 | [diff] [blame] | 91 | { "pcal6416", 16 | PCA953X_TYPE | PCA_LATCH_INT, }, |
| 92 | { "pcal6524", 24 | PCA953X_TYPE | PCA_LATCH_INT, }, |
Jan Kiszka | 3ba3ff5 | 2020-06-15 15:22:29 +0200 | [diff] [blame] | 93 | { "pcal9535", 16 | PCA953X_TYPE | PCA_LATCH_INT, }, |
Mike Looijmans | 237d961 | 2020-09-30 11:20:53 +0200 | [diff] [blame] | 94 | { "pcal9554b", 8 | PCA953X_TYPE | PCA_LATCH_INT, }, |
Andy Shevchenko | 2870b3c | 2019-03-05 11:13:46 +0200 | [diff] [blame] | 95 | { "pcal9555a", 16 | PCA953X_TYPE | PCA_LATCH_INT, }, |
Andy Shevchenko | 747e42a1c | 2016-06-15 01:57:56 +0300 | [diff] [blame] | 96 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 97 | { "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 Vasut | 1208c93 | 2016-10-17 18:36:49 +0200 | [diff] [blame] | 101 | { "max7318", 16 | PCA953X_TYPE | PCA_INT, }, |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 102 | { "pca6107", 8 | PCA953X_TYPE | PCA_INT, }, |
| 103 | { "tca6408", 8 | PCA953X_TYPE | PCA_INT, }, |
| 104 | { "tca6416", 16 | PCA953X_TYPE | PCA_INT, }, |
Andreas Schallenberg | ae79c19 | 2012-05-09 09:46:17 +0200 | [diff] [blame] | 105 | { "tca6424", 24 | PCA953X_TYPE | PCA_INT, }, |
Thierry Reding | 2db8aba | 2015-09-29 12:55:44 +0200 | [diff] [blame] | 106 | { "tca9539", 16 | PCA953X_TYPE | PCA_INT, }, |
Anders Darander | 1b9a0c2 | 2017-04-21 14:46:30 +0200 | [diff] [blame] | 107 | { "tca9554", 8 | PCA953X_TYPE | PCA_INT, }, |
Aaron Sierra | e73760a | 2014-02-07 16:36:21 -0600 | [diff] [blame] | 108 | { "xra1202", 8 | PCA953X_TYPE }, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 109 | { } |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 110 | }; |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 111 | MODULE_DEVICE_TABLE(i2c, pca953x_id); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 112 | |
Andy Shevchenko | ba8c90c | 2020-06-05 16:40:34 +0300 | [diff] [blame] | 113 | #ifdef CONFIG_GPIO_PCA953X_IRQ |
| 114 | |
| 115 | #include <linux/dmi.h> |
Andy Shevchenko | eb44133 | 2021-02-25 18:33:20 +0200 | [diff] [blame] | 116 | |
| 117 | static const struct acpi_gpio_params pca953x_irq_gpios = { 0, 0, true }; |
| 118 | |
| 119 | static const struct acpi_gpio_mapping pca953x_acpi_irq_gpios[] = { |
| 120 | { "irq-gpios", &pca953x_irq_gpios, 1, ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER }, |
| 121 | { } |
| 122 | }; |
| 123 | |
| 124 | static 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 Shevchenko | ba8c90c | 2020-06-05 16:40:34 +0300 | [diff] [blame] | 139 | |
| 140 | static 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 Shevchenko | ba8c90c | 2020-06-05 16:40:34 +0300 | [diff] [blame] | 157 | #endif |
| 158 | |
Andy Shevchenko | f32517b | 2015-10-01 14:20:28 +0300 | [diff] [blame] | 159 | static const struct acpi_device_id pca953x_acpi_ids[] = { |
Andy Shevchenko | 2870b3c | 2019-03-05 11:13:46 +0200 | [diff] [blame] | 160 | { "INT3491", 16 | PCA953X_TYPE | PCA_LATCH_INT, }, |
Andy Shevchenko | f32517b | 2015-10-01 14:20:28 +0300 | [diff] [blame] | 161 | { } |
| 162 | }; |
| 163 | MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids); |
| 164 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 165 | #define MAX_BANK 5 |
| 166 | #define BANK_SZ 8 |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 167 | #define MAX_LINE (MAX_BANK * BANK_SZ) |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 168 | |
Vignesh R | a246b81 | 2016-06-09 11:02:04 +0530 | [diff] [blame] | 169 | #define NBANK(chip) DIV_ROUND_UP(chip->gpio_chip.ngpio, BANK_SZ) |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 170 | |
Bartosz Golaszewski | 53661f3b | 2016-09-09 11:17:34 +0200 | [diff] [blame] | 171 | struct pca953x_reg_config { |
| 172 | int direction; |
| 173 | int output; |
| 174 | int input; |
Marek Vasut | 7a04aaa | 2018-12-12 02:39:55 +0100 | [diff] [blame] | 175 | int invert; |
Bartosz Golaszewski | 53661f3b | 2016-09-09 11:17:34 +0200 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | static const struct pca953x_reg_config pca953x_regs = { |
| 179 | .direction = PCA953X_DIRECTION, |
| 180 | .output = PCA953X_OUTPUT, |
| 181 | .input = PCA953X_INPUT, |
Marek Vasut | 7a04aaa | 2018-12-12 02:39:55 +0100 | [diff] [blame] | 182 | .invert = PCA953X_INVERT, |
Bartosz Golaszewski | 53661f3b | 2016-09-09 11:17:34 +0200 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | static const struct pca953x_reg_config pca957x_regs = { |
| 186 | .direction = PCA957X_CFG, |
| 187 | .output = PCA957X_OUT, |
| 188 | .input = PCA957X_IN, |
Marek Vasut | 7a04aaa | 2018-12-12 02:39:55 +0100 | [diff] [blame] | 189 | .invert = PCA957X_INVRT, |
Bartosz Golaszewski | 53661f3b | 2016-09-09 11:17:34 +0200 | [diff] [blame] | 190 | }; |
| 191 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 192 | struct pca953x_chip { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 193 | unsigned gpio_start; |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 194 | struct mutex i2c_lock; |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 195 | struct regmap *regmap; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 196 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 197 | #ifdef CONFIG_GPIO_PCA953X_IRQ |
| 198 | struct mutex irq_lock; |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 199 | 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 Petazzoni | 5c4fee6 | 2019-01-16 10:31:58 +0100 | [diff] [blame] | 203 | struct irq_chip irq_chip; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 204 | #endif |
Geert Uytterhoeven | f70fbc1 | 2019-03-20 11:39:27 +0100 | [diff] [blame] | 205 | atomic_t wakeup_path; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 206 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 207 | struct i2c_client *client; |
| 208 | struct gpio_chip gpio_chip; |
Uwe Kleine-König | 6215499 | 2010-05-26 14:42:17 -0700 | [diff] [blame] | 209 | const char *const *names; |
Andy Shevchenko | c666414 | 2015-10-01 14:20:27 +0300 | [diff] [blame] | 210 | unsigned long driver_data; |
Phil Reid | e23efa3 | 2016-07-29 11:39:55 +0800 | [diff] [blame] | 211 | struct regulator *regulator; |
Bartosz Golaszewski | 53661f3b | 2016-09-09 11:17:34 +0200 | [diff] [blame] | 212 | |
| 213 | const struct pca953x_reg_config *regs; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 214 | }; |
| 215 | |
Marek Vasut | 873d1e8 | 2018-12-12 02:39:49 +0100 | [diff] [blame] | 216 | static int pca953x_bank_shift(struct pca953x_chip *chip) |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 217 | { |
Marek Vasut | 873d1e8 | 2018-12-12 02:39:49 +0100 | [diff] [blame] | 218 | return fls((chip->gpio_chip.ngpio - 1) / BANK_SZ); |
| 219 | } |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 220 | |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 221 | #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 CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 225 | |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 226 | #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 Petazzoni | 15add0684 | 2019-02-07 17:28:59 +0100 | [diff] [blame] | 233 | #define PCAL9xxx_BANK_PULL_EN BIT(8 + 3) |
| 234 | #define PCAL9xxx_BANK_PULL_SEL BIT(8 + 4) |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 235 | #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 Petazzoni | 15add0684 | 2019-02-07 17:28:59 +0100 | [diff] [blame] | 256 | * Pull-up/pull-down enable reg 0x40 + 3 * bank_size RW |
| 257 | * Pull-up/pull-down select reg 0x40 + 4 * bank_size RW |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 258 | * Interrupt mask register 0x40 + 5 * bank_size RW |
| 259 | * Interrupt status register 0x40 + 6 * bank_size R |
| 260 | * |
| 261 | * - Registers with bit 0x80 set, the AI bit |
| 262 | * The bit is cleared and the registers fall into one of the |
| 263 | * categories above. |
| 264 | */ |
| 265 | |
| 266 | static bool pca953x_check_register(struct pca953x_chip *chip, unsigned int reg, |
| 267 | u32 checkbank) |
| 268 | { |
| 269 | int bank_shift = pca953x_bank_shift(chip); |
| 270 | int bank = (reg & REG_ADDR_MASK) >> bank_shift; |
| 271 | int offset = reg & (BIT(bank_shift) - 1); |
| 272 | |
| 273 | /* Special PCAL extended register check. */ |
| 274 | if (reg & REG_ADDR_EXT) { |
| 275 | if (!(chip->driver_data & PCA_PCAL)) |
| 276 | return false; |
| 277 | bank += 8; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 280 | /* Register is not in the matching bank. */ |
| 281 | if (!(BIT(bank) & checkbank)) |
| 282 | return false; |
| 283 | |
| 284 | /* Register is not within allowed range of bank. */ |
| 285 | if (offset >= NBANK(chip)) |
| 286 | return false; |
| 287 | |
| 288 | return true; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 289 | } |
| 290 | |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 291 | static bool pca953x_readable_register(struct device *dev, unsigned int reg) |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 292 | { |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 293 | struct pca953x_chip *chip = dev_get_drvdata(dev); |
| 294 | u32 bank; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 295 | |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 296 | if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE) { |
| 297 | bank = PCA953x_BANK_INPUT | PCA953x_BANK_OUTPUT | |
| 298 | PCA953x_BANK_POLARITY | PCA953x_BANK_CONFIG; |
| 299 | } else { |
| 300 | bank = PCA957x_BANK_INPUT | PCA957x_BANK_OUTPUT | |
| 301 | PCA957x_BANK_POLARITY | PCA957x_BANK_CONFIG | |
| 302 | PCA957x_BANK_BUSHOLD; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 303 | } |
| 304 | |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 305 | if (chip->driver_data & PCA_PCAL) { |
Thomas Petazzoni | 15add0684 | 2019-02-07 17:28:59 +0100 | [diff] [blame] | 306 | bank |= PCAL9xxx_BANK_IN_LATCH | PCAL9xxx_BANK_PULL_EN | |
| 307 | PCAL9xxx_BANK_PULL_SEL | PCAL9xxx_BANK_IRQ_MASK | |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 308 | PCAL9xxx_BANK_IRQ_STAT; |
| 309 | } |
| 310 | |
| 311 | return pca953x_check_register(chip, reg, bank); |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 312 | } |
| 313 | |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 314 | static bool pca953x_writeable_register(struct device *dev, unsigned int reg) |
Bartosz Golaszewski | 7acc66e | 2016-09-09 11:17:35 +0200 | [diff] [blame] | 315 | { |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 316 | struct pca953x_chip *chip = dev_get_drvdata(dev); |
| 317 | u32 bank; |
| 318 | |
| 319 | if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE) { |
| 320 | bank = PCA953x_BANK_OUTPUT | PCA953x_BANK_POLARITY | |
| 321 | PCA953x_BANK_CONFIG; |
| 322 | } else { |
| 323 | bank = PCA957x_BANK_OUTPUT | PCA957x_BANK_POLARITY | |
| 324 | PCA957x_BANK_CONFIG | PCA957x_BANK_BUSHOLD; |
| 325 | } |
| 326 | |
| 327 | if (chip->driver_data & PCA_PCAL) |
Thomas Petazzoni | 15add0684 | 2019-02-07 17:28:59 +0100 | [diff] [blame] | 328 | bank |= PCAL9xxx_BANK_IN_LATCH | PCAL9xxx_BANK_PULL_EN | |
| 329 | PCAL9xxx_BANK_PULL_SEL | PCAL9xxx_BANK_IRQ_MASK; |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 330 | |
| 331 | return pca953x_check_register(chip, reg, bank); |
Bartosz Golaszewski | 7acc66e | 2016-09-09 11:17:35 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 334 | static bool pca953x_volatile_register(struct device *dev, unsigned int reg) |
Bartosz Golaszewski | 7acc66e | 2016-09-09 11:17:35 +0200 | [diff] [blame] | 335 | { |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 336 | struct pca953x_chip *chip = dev_get_drvdata(dev); |
| 337 | u32 bank; |
Bartosz Golaszewski | 7acc66e | 2016-09-09 11:17:35 +0200 | [diff] [blame] | 338 | |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 339 | 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 Golaszewski | 7acc66e | 2016-09-09 11:17:35 +0200 | [diff] [blame] | 348 | } |
| 349 | |
Wei Yongjun | d04e779 | 2019-01-10 14:07:42 +0000 | [diff] [blame] | 350 | static const struct regmap_config pca953x_i2c_regmap = { |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 351 | .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 Golaszewski | aa58a21 | 2020-04-07 17:42:45 +0200 | [diff] [blame] | 358 | .disable_locking = true, |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 359 | .cache_type = REGCACHE_RBTREE, |
Uwe Kleine-König | bcf41dc | 2020-04-20 20:27:51 +0300 | [diff] [blame] | 360 | .max_register = 0x7f, |
| 361 | }; |
| 362 | |
| 363 | static 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 Shevchenko | ec3decd | 2020-06-05 16:40:36 +0300 | [diff] [blame] | 374 | .disable_locking = true, |
Uwe Kleine-König | bcf41dc | 2020-04-20 20:27:51 +0300 | [diff] [blame] | 375 | .cache_type = REGCACHE_RBTREE, |
| 376 | .max_register = 0x7f, |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 377 | }; |
| 378 | |
Uwe Kleine-König | 6fdeb6c | 2020-04-20 20:27:52 +0300 | [diff] [blame] | 379 | static u8 pca953x_recalc_addr(struct pca953x_chip *chip, int reg, int off) |
Bartosz Golaszewski | 7acc66e | 2016-09-09 11:17:35 +0200 | [diff] [blame] | 380 | { |
Marek Vasut | b32cecb | 2018-12-12 02:39:57 +0100 | [diff] [blame] | 381 | int bank_shift = pca953x_bank_shift(chip); |
H. Nikolaus Schaller | d5dbf9c | 2018-05-17 06:59:49 +0200 | [diff] [blame] | 382 | int addr = (reg & PCAL_GPIO_MASK) << bank_shift; |
| 383 | int pinctrl = (reg & PCAL_PINCTRL_MASK) << 1; |
Marek Vasut | b32cecb | 2018-12-12 02:39:57 +0100 | [diff] [blame] | 384 | u8 regaddr = pinctrl | addr | (off / BANK_SZ); |
Bartosz Golaszewski | 7acc66e | 2016-09-09 11:17:35 +0200 | [diff] [blame] | 385 | |
Marek Vasut | b32cecb | 2018-12-12 02:39:57 +0100 | [diff] [blame] | 386 | return regaddr; |
Bartosz Golaszewski | 7acc66e | 2016-09-09 11:17:35 +0200 | [diff] [blame] | 387 | } |
| 388 | |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 389 | static int pca953x_write_regs(struct pca953x_chip *chip, int reg, unsigned long *val) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 390 | { |
Uwe Kleine-König | 6fdeb6c | 2020-04-20 20:27:52 +0300 | [diff] [blame] | 391 | u8 regaddr = pca953x_recalc_addr(chip, reg, 0); |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 392 | u8 value[MAX_BANK]; |
| 393 | int i, ret; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 394 | |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 395 | 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 Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 399 | if (ret < 0) { |
| 400 | dev_err(&chip->client->dev, "failed writing register\n"); |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 401 | return ret; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | return 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 407 | static int pca953x_read_regs(struct pca953x_chip *chip, int reg, unsigned long *val) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 408 | { |
Uwe Kleine-König | 6fdeb6c | 2020-04-20 20:27:52 +0300 | [diff] [blame] | 409 | u8 regaddr = pca953x_recalc_addr(chip, reg, 0); |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 410 | u8 value[MAX_BANK]; |
| 411 | int i, ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 412 | |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 413 | ret = regmap_bulk_read(chip->regmap, regaddr, value, NBANK(chip)); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 414 | if (ret < 0) { |
| 415 | dev_err(&chip->client->dev, "failed reading register\n"); |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 416 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 419 | for (i = 0; i < NBANK(chip); i++) |
| 420 | bitmap_set_value8(val, value[i], i * BANK_SZ); |
| 421 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 425 | static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 426 | { |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 427 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Uwe Kleine-König | 6fdeb6c | 2020-04-20 20:27:52 +0300 | [diff] [blame] | 428 | u8 dirreg = pca953x_recalc_addr(chip, chip->regs->direction, off); |
Marek Vasut | 0f25fda | 2018-12-12 02:39:59 +0100 | [diff] [blame] | 429 | u8 bit = BIT(off % BANK_SZ); |
Bartosz Golaszewski | 53661f3b | 2016-09-09 11:17:34 +0200 | [diff] [blame] | 430 | int ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 431 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 432 | mutex_lock(&chip->i2c_lock); |
Marek Vasut | 0f25fda | 2018-12-12 02:39:59 +0100 | [diff] [blame] | 433 | ret = regmap_write_bits(chip->regmap, dirreg, bit, bit); |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 434 | mutex_unlock(&chip->i2c_lock); |
| 435 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 436 | } |
| 437 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 438 | static int pca953x_gpio_direction_output(struct gpio_chip *gc, |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 439 | unsigned off, int val) |
| 440 | { |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 441 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Uwe Kleine-König | 6fdeb6c | 2020-04-20 20:27:52 +0300 | [diff] [blame] | 442 | u8 dirreg = pca953x_recalc_addr(chip, chip->regs->direction, off); |
| 443 | u8 outreg = pca953x_recalc_addr(chip, chip->regs->output, off); |
Marek Vasut | 0f25fda | 2018-12-12 02:39:59 +0100 | [diff] [blame] | 444 | u8 bit = BIT(off % BANK_SZ); |
Bartosz Golaszewski | 53661f3b | 2016-09-09 11:17:34 +0200 | [diff] [blame] | 445 | int ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 446 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 447 | mutex_lock(&chip->i2c_lock); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 448 | /* set output level */ |
Marek Vasut | ec82d1e | 2018-12-12 02:40:00 +0100 | [diff] [blame] | 449 | ret = regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 450 | if (ret) |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 451 | goto exit; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 452 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 453 | /* then direction */ |
Marek Vasut | 0f25fda | 2018-12-12 02:39:59 +0100 | [diff] [blame] | 454 | ret = regmap_write_bits(chip->regmap, dirreg, bit, 0); |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 455 | exit: |
| 456 | mutex_unlock(&chip->i2c_lock); |
| 457 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 460 | static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 461 | { |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 462 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Uwe Kleine-König | 6fdeb6c | 2020-04-20 20:27:52 +0300 | [diff] [blame] | 463 | u8 inreg = pca953x_recalc_addr(chip, chip->regs->input, off); |
Marek Vasut | 87813cf | 2018-12-12 02:40:01 +0100 | [diff] [blame] | 464 | u8 bit = BIT(off % BANK_SZ); |
Andreas Schallenberg | ae79c19 | 2012-05-09 09:46:17 +0200 | [diff] [blame] | 465 | u32 reg_val; |
Bartosz Golaszewski | 53661f3b | 2016-09-09 11:17:34 +0200 | [diff] [blame] | 466 | int ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 467 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 468 | mutex_lock(&chip->i2c_lock); |
Marek Vasut | 87813cf | 2018-12-12 02:40:01 +0100 | [diff] [blame] | 469 | ret = regmap_read(chip->regmap, inreg, ®_val); |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 470 | mutex_unlock(&chip->i2c_lock); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 471 | if (ret < 0) { |
Andy Shevchenko | b27d851 | 2019-12-04 16:53:40 -0800 | [diff] [blame] | 472 | /* |
| 473 | * NOTE: |
| 474 | * diagnostic already emitted; that's all we should |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 475 | * do unless gpio_*_value_cansleep() calls become different |
| 476 | * from their nonsleeping siblings (and report faults). |
| 477 | */ |
| 478 | return 0; |
| 479 | } |
| 480 | |
Marek Vasut | 87813cf | 2018-12-12 02:40:01 +0100 | [diff] [blame] | 481 | return !!(reg_val & bit); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 482 | } |
| 483 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 484 | static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 485 | { |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 486 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Uwe Kleine-König | 6fdeb6c | 2020-04-20 20:27:52 +0300 | [diff] [blame] | 487 | u8 outreg = pca953x_recalc_addr(chip, chip->regs->output, off); |
Marek Vasut | ec82d1e | 2018-12-12 02:40:00 +0100 | [diff] [blame] | 488 | u8 bit = BIT(off % BANK_SZ); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 489 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 490 | mutex_lock(&chip->i2c_lock); |
Marek Vasut | ec82d1e | 2018-12-12 02:40:00 +0100 | [diff] [blame] | 491 | regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0); |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 492 | mutex_unlock(&chip->i2c_lock); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 493 | } |
| 494 | |
Andy Shevchenko | 66e5719 | 2017-03-22 16:11:11 +0200 | [diff] [blame] | 495 | static int pca953x_gpio_get_direction(struct gpio_chip *gc, unsigned off) |
| 496 | { |
| 497 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Uwe Kleine-König | 6fdeb6c | 2020-04-20 20:27:52 +0300 | [diff] [blame] | 498 | u8 dirreg = pca953x_recalc_addr(chip, chip->regs->direction, off); |
Marek Vasut | 0f25fda | 2018-12-12 02:39:59 +0100 | [diff] [blame] | 499 | u8 bit = BIT(off % BANK_SZ); |
Andy Shevchenko | 66e5719 | 2017-03-22 16:11:11 +0200 | [diff] [blame] | 500 | u32 reg_val; |
| 501 | int ret; |
| 502 | |
| 503 | mutex_lock(&chip->i2c_lock); |
Marek Vasut | 0f25fda | 2018-12-12 02:39:59 +0100 | [diff] [blame] | 504 | ret = regmap_read(chip->regmap, dirreg, ®_val); |
Andy Shevchenko | 66e5719 | 2017-03-22 16:11:11 +0200 | [diff] [blame] | 505 | mutex_unlock(&chip->i2c_lock); |
| 506 | if (ret < 0) |
| 507 | return ret; |
| 508 | |
Matti Vaittinen | e42615e | 2019-11-06 10:54:12 +0200 | [diff] [blame] | 509 | if (reg_val & bit) |
| 510 | return GPIO_LINE_DIRECTION_IN; |
| 511 | |
| 512 | return GPIO_LINE_DIRECTION_OUT; |
Andy Shevchenko | 66e5719 | 2017-03-22 16:11:11 +0200 | [diff] [blame] | 513 | } |
| 514 | |
Paul Thomas | 96d7c7b3 | 2020-04-14 11:28:42 -0400 | [diff] [blame] | 515 | static int pca953x_gpio_get_multiple(struct gpio_chip *gc, |
Andy Shevchenko | 6f79348 | 2020-04-20 20:27:50 +0300 | [diff] [blame] | 516 | unsigned long *mask, unsigned long *bits) |
Paul Thomas | 96d7c7b3 | 2020-04-14 11:28:42 -0400 | [diff] [blame] | 517 | { |
| 518 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Andy Shevchenko | 6f79348 | 2020-04-20 20:27:50 +0300 | [diff] [blame] | 519 | DECLARE_BITMAP(reg_val, MAX_LINE); |
| 520 | int ret; |
Paul Thomas | 96d7c7b3 | 2020-04-14 11:28:42 -0400 | [diff] [blame] | 521 | |
Andy Shevchenko | 6f79348 | 2020-04-20 20:27:50 +0300 | [diff] [blame] | 522 | 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 Thomas | 96d7c7b3 | 2020-04-14 11:28:42 -0400 | [diff] [blame] | 530 | } |
| 531 | |
Phil Reid | b4818af | 2015-12-04 15:52:30 +0800 | [diff] [blame] | 532 | static void pca953x_gpio_set_multiple(struct gpio_chip *gc, |
Bartosz Golaszewski | ea3d579 | 2016-09-09 11:17:38 +0200 | [diff] [blame] | 533 | unsigned long *mask, unsigned long *bits) |
Phil Reid | b4818af | 2015-12-04 15:52:30 +0800 | [diff] [blame] | 534 | { |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 535 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 536 | DECLARE_BITMAP(reg_val, MAX_LINE); |
Bartosz Golaszewski | 53661f3b | 2016-09-09 11:17:34 +0200 | [diff] [blame] | 537 | int ret; |
Bartosz Golaszewski | ea3d579 | 2016-09-09 11:17:38 +0200 | [diff] [blame] | 538 | |
Phil Reid | b4818af | 2015-12-04 15:52:30 +0800 | [diff] [blame] | 539 | mutex_lock(&chip->i2c_lock); |
Marek Vasut | ec82d1e | 2018-12-12 02:40:00 +0100 | [diff] [blame] | 540 | ret = pca953x_read_regs(chip, chip->regs->output, reg_val); |
| 541 | if (ret) |
| 542 | goto exit; |
| 543 | |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 544 | bitmap_replace(reg_val, reg_val, bits, mask, gc->ngpio); |
Bartosz Golaszewski | ea3d579 | 2016-09-09 11:17:38 +0200 | [diff] [blame] | 545 | |
Marek Vasut | ec82d1e | 2018-12-12 02:40:00 +0100 | [diff] [blame] | 546 | pca953x_write_regs(chip, chip->regs->output, reg_val); |
Phil Reid | b4818af | 2015-12-04 15:52:30 +0800 | [diff] [blame] | 547 | exit: |
| 548 | mutex_unlock(&chip->i2c_lock); |
| 549 | } |
| 550 | |
Thomas Petazzoni | 15add0684 | 2019-02-07 17:28:59 +0100 | [diff] [blame] | 551 | static int pca953x_gpio_set_pull_up_down(struct pca953x_chip *chip, |
| 552 | unsigned int offset, |
| 553 | unsigned long config) |
| 554 | { |
Uwe Kleine-König | 6fdeb6c | 2020-04-20 20:27:52 +0300 | [diff] [blame] | 555 | 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 Petazzoni | 15add0684 | 2019-02-07 17:28:59 +0100 | [diff] [blame] | 557 | 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 | |
| 585 | exit: |
| 586 | mutex_unlock(&chip->i2c_lock); |
| 587 | return ret; |
| 588 | } |
| 589 | |
| 590 | static 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 Ford | dc87f6d | 2020-04-11 20:33:52 -0500 | [diff] [blame] | 595 | switch (pinconf_to_config_param(config)) { |
Thomas Petazzoni | 15add0684 | 2019-02-07 17:28:59 +0100 | [diff] [blame] | 596 | 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 Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 604 | static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 605 | { |
| 606 | struct gpio_chip *gc; |
| 607 | |
| 608 | gc = &chip->gpio_chip; |
| 609 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 610 | 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 Shevchenko | 66e5719 | 2017-03-22 16:11:11 +0200 | [diff] [blame] | 614 | gc->get_direction = pca953x_gpio_get_direction; |
Paul Thomas | 96d7c7b3 | 2020-04-14 11:28:42 -0400 | [diff] [blame] | 615 | gc->get_multiple = pca953x_gpio_get_multiple; |
Phil Reid | b4818af | 2015-12-04 15:52:30 +0800 | [diff] [blame] | 616 | gc->set_multiple = pca953x_gpio_set_multiple; |
Thomas Petazzoni | 15add0684 | 2019-02-07 17:28:59 +0100 | [diff] [blame] | 617 | gc->set_config = pca953x_gpio_set_config; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 618 | gc->can_sleep = true; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 619 | |
| 620 | gc->base = chip->gpio_start; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 621 | gc->ngpio = gpios; |
Linus Walleij | 5128f8d | 2018-12-02 09:43:21 +0100 | [diff] [blame] | 622 | gc->label = dev_name(&chip->client->dev); |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 623 | gc->parent = &chip->client->dev; |
Guennadi Liakhovetski | d72cbed | 2008-04-28 02:14:45 -0700 | [diff] [blame] | 624 | gc->owner = THIS_MODULE; |
Daniel Silverstone | 77906a54 | 2009-06-17 16:26:15 -0700 | [diff] [blame] | 625 | gc->names = chip->names; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 626 | } |
| 627 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 628 | #ifdef CONFIG_GPIO_PCA953X_IRQ |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 629 | static void pca953x_irq_mask(struct irq_data *d) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 630 | { |
Linus Walleij | 7bcbce5 | 2014-05-09 13:27:57 +0200 | [diff] [blame] | 631 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 632 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Andy Shevchenko | 2688302 | 2019-12-09 14:35:44 +0200 | [diff] [blame] | 633 | irq_hw_number_t hwirq = irqd_to_hwirq(d); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 634 | |
Andy Shevchenko | 2688302 | 2019-12-09 14:35:44 +0200 | [diff] [blame] | 635 | clear_bit(hwirq, chip->irq_mask); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 638 | static void pca953x_irq_unmask(struct irq_data *d) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 639 | { |
Linus Walleij | 7bcbce5 | 2014-05-09 13:27:57 +0200 | [diff] [blame] | 640 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 641 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Andy Shevchenko | 2688302 | 2019-12-09 14:35:44 +0200 | [diff] [blame] | 642 | irq_hw_number_t hwirq = irqd_to_hwirq(d); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 643 | |
Andy Shevchenko | 2688302 | 2019-12-09 14:35:44 +0200 | [diff] [blame] | 644 | set_bit(hwirq, chip->irq_mask); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 645 | } |
| 646 | |
Geert Uytterhoeven | 2a9a2f2 | 2019-02-13 14:15:03 +0100 | [diff] [blame] | 647 | static 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 Uytterhoeven | f70fbc1 | 2019-03-20 11:39:27 +0100 | [diff] [blame] | 652 | if (on) |
| 653 | atomic_inc(&chip->wakeup_path); |
| 654 | else |
| 655 | atomic_dec(&chip->wakeup_path); |
| 656 | |
Geert Uytterhoeven | 2a9a2f2 | 2019-02-13 14:15:03 +0100 | [diff] [blame] | 657 | return irq_set_irq_wake(chip->client->irq, on); |
| 658 | } |
| 659 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 660 | static void pca953x_irq_bus_lock(struct irq_data *d) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 661 | { |
Linus Walleij | 7bcbce5 | 2014-05-09 13:27:57 +0200 | [diff] [blame] | 662 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 663 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 664 | |
| 665 | mutex_lock(&chip->irq_lock); |
| 666 | } |
| 667 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 668 | static void pca953x_irq_bus_sync_unlock(struct irq_data *d) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 669 | { |
Linus Walleij | 7bcbce5 | 2014-05-09 13:27:57 +0200 | [diff] [blame] | 670 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 671 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 672 | DECLARE_BITMAP(irq_mask, MAX_LINE); |
| 673 | DECLARE_BITMAP(reg_direction, MAX_LINE); |
| 674 | int level; |
Marek Vasut | 0f25fda | 2018-12-12 02:39:59 +0100 | [diff] [blame] | 675 | |
Yong Li | 44896be | 2016-04-07 12:56:32 +0800 | [diff] [blame] | 676 | 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 Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 680 | bitmap_complement(irq_mask, chip->irq_mask, gc->ngpio); |
Yong Li | 44896be | 2016-04-07 12:56:32 +0800 | [diff] [blame] | 681 | |
| 682 | /* Unmask enabled interrupts */ |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 683 | pca953x_write_regs(chip, PCAL953X_INT_MASK, irq_mask); |
Yong Li | 44896be | 2016-04-07 12:56:32 +0800 | [diff] [blame] | 684 | } |
Marc Zyngier | a2cb9ae | 2010-04-27 13:13:07 -0700 | [diff] [blame] | 685 | |
Andy Shevchenko | 0b22c25 | 2020-06-05 16:40:35 +0300 | [diff] [blame] | 686 | /* Switch direction to input if needed */ |
| 687 | pca953x_read_regs(chip, chip->regs->direction, reg_direction); |
| 688 | |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 689 | bitmap_or(irq_mask, chip->irq_trig_fall, chip->irq_trig_raise, gc->ngpio); |
Andy Shevchenko | 0b22c25 | 2020-06-05 16:40:35 +0300 | [diff] [blame] | 690 | bitmap_complement(reg_direction, reg_direction, gc->ngpio); |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 691 | bitmap_and(irq_mask, irq_mask, reg_direction, gc->ngpio); |
| 692 | |
Marc Zyngier | a2cb9ae | 2010-04-27 13:13:07 -0700 | [diff] [blame] | 693 | /* Look for any newly setup interrupt */ |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 694 | for_each_set_bit(level, irq_mask, gc->ngpio) |
| 695 | pca953x_gpio_direction_input(&chip->gpio_chip, level); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 696 | |
| 697 | mutex_unlock(&chip->irq_lock); |
| 698 | } |
| 699 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 700 | static int pca953x_irq_set_type(struct irq_data *d, unsigned int type) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 701 | { |
Linus Walleij | 7bcbce5 | 2014-05-09 13:27:57 +0200 | [diff] [blame] | 702 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 468e67f | 2015-12-07 11:20:54 +0100 | [diff] [blame] | 703 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Andy Shevchenko | 2688302 | 2019-12-09 14:35:44 +0200 | [diff] [blame] | 704 | irq_hw_number_t hwirq = irqd_to_hwirq(d); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 705 | |
| 706 | if (!(type & IRQ_TYPE_EDGE_BOTH)) { |
| 707 | dev_err(&chip->client->dev, "irq %d: unsupported type %d\n", |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 708 | d->irq, type); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 709 | return -EINVAL; |
| 710 | } |
| 711 | |
Andy Shevchenko | 2688302 | 2019-12-09 14:35:44 +0200 | [diff] [blame] | 712 | 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 Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 714 | |
Marc Zyngier | a2cb9ae | 2010-04-27 13:13:07 -0700 | [diff] [blame] | 715 | return 0; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 716 | } |
| 717 | |
Grigoryev Denis | 0a70fe0 | 2018-05-04 16:53:18 +0000 | [diff] [blame] | 718 | static void pca953x_irq_shutdown(struct irq_data *d) |
| 719 | { |
Mark Walton | c378b3a | 2019-02-28 15:46:36 +0000 | [diff] [blame] | 720 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
| 721 | struct pca953x_chip *chip = gpiochip_get_data(gc); |
Andy Shevchenko | 2688302 | 2019-12-09 14:35:44 +0200 | [diff] [blame] | 722 | irq_hw_number_t hwirq = irqd_to_hwirq(d); |
Grigoryev Denis | 0a70fe0 | 2018-05-04 16:53:18 +0000 | [diff] [blame] | 723 | |
Andy Shevchenko | 2688302 | 2019-12-09 14:35:44 +0200 | [diff] [blame] | 724 | clear_bit(hwirq, chip->irq_trig_raise); |
| 725 | clear_bit(hwirq, chip->irq_trig_fall); |
Grigoryev Denis | 0a70fe0 | 2018-05-04 16:53:18 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 728 | static bool pca953x_irq_pending(struct pca953x_chip *chip, unsigned long *pending) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 729 | { |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 730 | 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 Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 737 | |
Yong Li | 44896be | 2016-04-07 12:56:32 +0800 | [diff] [blame] | 738 | 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 Shevchenko | 0a0a021 | 2019-12-04 16:53:33 -0800 | [diff] [blame] | 745 | ret = pca953x_read_regs(chip, chip->regs->input, cur_stat); |
Yong Li | 44896be | 2016-04-07 12:56:32 +0800 | [diff] [blame] | 746 | if (ret) |
| 747 | return false; |
| 748 | |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 749 | /* 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 Li | 44896be | 2016-04-07 12:56:32 +0800 | [diff] [blame] | 751 | |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 752 | bitmap_and(pending, new_stat, trigger, gc->ngpio); |
| 753 | |
| 754 | return !bitmap_empty(pending, gc->ngpio); |
Yong Li | 44896be | 2016-04-07 12:56:32 +0800 | [diff] [blame] | 755 | } |
| 756 | |
Bartosz Golaszewski | 53661f3b | 2016-09-09 11:17:34 +0200 | [diff] [blame] | 757 | ret = pca953x_read_regs(chip, chip->regs->input, cur_stat); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 758 | if (ret) |
Joshua Scott | b6ac128 | 2015-05-22 12:35:12 +1200 | [diff] [blame] | 759 | return false; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 760 | |
| 761 | /* Remove output pins from the equation */ |
David Jander | 438b6c2 | 2019-08-27 06:46:29 +0000 | [diff] [blame] | 762 | pca953x_read_regs(chip, chip->regs->direction, reg_direction); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 763 | |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 764 | bitmap_copy(old_stat, chip->irq_stat, gc->ngpio); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 765 | |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 766 | 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 CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 769 | |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 770 | if (bitmap_empty(trigger, gc->ngpio)) |
Joshua Scott | b6ac128 | 2015-05-22 12:35:12 +1200 | [diff] [blame] | 771 | return false; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 772 | |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 773 | bitmap_copy(chip->irq_stat, new_stat, gc->ngpio); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 774 | |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 775 | 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 Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 779 | |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 780 | return !bitmap_empty(pending, gc->ngpio); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | static irqreturn_t pca953x_irq_handler(int irq, void *devid) |
| 784 | { |
| 785 | struct pca953x_chip *chip = devid; |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 786 | struct gpio_chip *gc = &chip->gpio_chip; |
Andy Shevchenko | e09e200 | 2020-09-30 17:20:12 +0300 | [diff] [blame] | 787 | DECLARE_BITMAP(pending, MAX_LINE); |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 788 | int level; |
Andy Shevchenko | 064c73a | 2020-06-05 16:40:33 +0300 | [diff] [blame] | 789 | bool ret; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 790 | |
Andy Shevchenko | e09e200 | 2020-09-30 17:20:12 +0300 | [diff] [blame] | 791 | bitmap_zero(pending, MAX_LINE); |
| 792 | |
Andy Shevchenko | 064c73a | 2020-06-05 16:40:33 +0300 | [diff] [blame] | 793 | mutex_lock(&chip->i2c_lock); |
| 794 | ret = pca953x_irq_pending(chip, pending); |
| 795 | mutex_unlock(&chip->i2c_lock); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 796 | |
Marc Zyngier | 8b81edd | 2020-10-05 15:02:17 +0100 | [diff] [blame] | 797 | 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 Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 812 | |
Andy Shevchenko | 064c73a | 2020-06-05 16:40:33 +0300 | [diff] [blame] | 813 | return IRQ_RETVAL(ret); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 814 | } |
| 815 | |
Andy Shevchenko | b27d851 | 2019-12-04 16:53:40 -0800 | [diff] [blame] | 816 | static int pca953x_irq_setup(struct pca953x_chip *chip, int irq_base) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 817 | { |
| 818 | struct i2c_client *client = chip->client; |
Thomas Petazzoni | 5c4fee6 | 2019-01-16 10:31:58 +0100 | [diff] [blame] | 819 | struct irq_chip *irq_chip = &chip->irq_chip; |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 820 | DECLARE_BITMAP(reg_direction, MAX_LINE); |
| 821 | DECLARE_BITMAP(irq_stat, MAX_LINE); |
Linus Walleij | edee3bc | 2020-07-17 16:40:40 +0200 | [diff] [blame] | 822 | struct gpio_irq_chip *girq; |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 823 | int ret; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 824 | |
Andy Shevchenko | ba8c90c | 2020-06-05 16:40:34 +0300 | [diff] [blame] | 825 | 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 Petazzoni | 7341fa7 | 2019-01-16 10:31:57 +0100 | [diff] [blame] | 831 | if (!client->irq) |
| 832 | return 0; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 833 | |
Thomas Petazzoni | 7341fa7 | 2019-01-16 10:31:57 +0100 | [diff] [blame] | 834 | if (irq_base == -1) |
| 835 | return 0; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 836 | |
Thomas Petazzoni | 7341fa7 | 2019-01-16 10:31:57 +0100 | [diff] [blame] | 837 | if (!(chip->driver_data & PCA_INT)) |
| 838 | return 0; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 839 | |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 840 | ret = pca953x_read_regs(chip, chip->regs->input, irq_stat); |
Thomas Petazzoni | 7341fa7 | 2019-01-16 10:31:57 +0100 | [diff] [blame] | 841 | if (ret) |
| 842 | return ret; |
Grygorii Strashko | fdd5040 | 2015-07-07 17:34:49 +0300 | [diff] [blame] | 843 | |
Thomas Petazzoni | 7341fa7 | 2019-01-16 10:31:57 +0100 | [diff] [blame] | 844 | /* |
| 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 Jander | 438b6c2 | 2019-08-27 06:46:29 +0000 | [diff] [blame] | 849 | pca953x_read_regs(chip, chip->regs->direction, reg_direction); |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 850 | bitmap_and(chip->irq_stat, irq_stat, reg_direction, chip->gpio_chip.ngpio); |
Thomas Petazzoni | 7341fa7 | 2019-01-16 10:31:57 +0100 | [diff] [blame] | 851 | mutex_init(&chip->irq_lock); |
| 852 | |
Andy Shevchenko | 2a93a0d | 2020-07-28 15:55:02 +0300 | [diff] [blame] | 853 | irq_chip->name = dev_name(&client->dev); |
Thomas Petazzoni | 5c4fee6 | 2019-01-16 10:31:58 +0100 | [diff] [blame] | 854 | irq_chip->irq_mask = pca953x_irq_mask; |
| 855 | irq_chip->irq_unmask = pca953x_irq_unmask; |
Geert Uytterhoeven | 2a9a2f2 | 2019-02-13 14:15:03 +0100 | [diff] [blame] | 856 | irq_chip->irq_set_wake = pca953x_irq_set_wake; |
Thomas Petazzoni | 5c4fee6 | 2019-01-16 10:31:58 +0100 | [diff] [blame] | 857 | 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 Walleij | edee3bc | 2020-07-17 16:40:40 +0200 | [diff] [blame] | 862 | 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 Petazzoni | 7341fa7 | 2019-01-16 10:31:57 +0100 | [diff] [blame] | 872 | |
Andy Shevchenko | 2a93a0d | 2020-07-28 15:55:02 +0300 | [diff] [blame] | 873 | 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 Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 883 | return 0; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 884 | } |
| 885 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 886 | #else /* CONFIG_GPIO_PCA953X_IRQ */ |
| 887 | static int pca953x_irq_setup(struct pca953x_chip *chip, |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 888 | int irq_base) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 889 | { |
| 890 | struct i2c_client *client = chip->client; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 891 | |
Baruch Siach | 72b38ca | 2018-06-21 16:38:46 +0300 | [diff] [blame] | 892 | if (client->irq && irq_base != -1 && (chip->driver_data & PCA_INT)) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 893 | dev_warn(&client->dev, "interrupt support not compiled in\n"); |
| 894 | |
| 895 | return 0; |
| 896 | } |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 897 | #endif |
| 898 | |
Marek Vasut | 7a04aaa | 2018-12-12 02:39:55 +0100 | [diff] [blame] | 899 | static int device_pca95xx_init(struct pca953x_chip *chip, u32 invert) |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 900 | { |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 901 | DECLARE_BITMAP(val, MAX_LINE); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 902 | int ret; |
| 903 | |
Marek Vasut | ec82d1e | 2018-12-12 02:40:00 +0100 | [diff] [blame] | 904 | ret = regcache_sync_region(chip->regmap, chip->regs->output, |
| 905 | chip->regs->output + NBANK(chip)); |
Andy Shevchenko | 6dd6a2d | 2019-08-01 20:39:37 +0300 | [diff] [blame] | 906 | if (ret) |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 907 | goto out; |
| 908 | |
Marek Vasut | 0f25fda | 2018-12-12 02:39:59 +0100 | [diff] [blame] | 909 | ret = regcache_sync_region(chip->regmap, chip->regs->direction, |
| 910 | chip->regs->direction + NBANK(chip)); |
Andy Shevchenko | 6dd6a2d | 2019-08-01 20:39:37 +0300 | [diff] [blame] | 911 | if (ret) |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 912 | goto out; |
| 913 | |
| 914 | /* set platform specific polarity inversion */ |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 915 | if (invert) |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 916 | bitmap_fill(val, MAX_LINE); |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 917 | else |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 918 | bitmap_zero(val, MAX_LINE); |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 919 | |
Marek Vasut | 7a04aaa | 2018-12-12 02:39:55 +0100 | [diff] [blame] | 920 | ret = pca953x_write_regs(chip, chip->regs->invert, val); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 921 | out: |
| 922 | return ret; |
| 923 | } |
| 924 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 925 | static int device_pca957x_init(struct pca953x_chip *chip, u32 invert) |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 926 | { |
Andy Shevchenko | 35d13d9 | 2019-12-04 16:53:37 -0800 | [diff] [blame] | 927 | DECLARE_BITMAP(val, MAX_LINE); |
Andy Shevchenko | 8c1f1c34 | 2020-09-30 17:20:13 +0300 | [diff] [blame] | 928 | unsigned int i; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 929 | int ret; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 930 | |
Marek Vasut | 7a04aaa | 2018-12-12 02:39:55 +0100 | [diff] [blame] | 931 | ret = device_pca95xx_init(chip, invert); |
Nicholas Krause | c75a377 | 2015-08-26 17:52:19 -0400 | [diff] [blame] | 932 | if (ret) |
| 933 | goto out; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 934 | |
Colin Cronin | 20a8a96 | 2015-05-18 11:41:43 -0700 | [diff] [blame] | 935 | /* To enable register 6, 7 to control pull up and pull down */ |
Andy Shevchenko | 8c1f1c34 | 2020-09-30 17:20:13 +0300 | [diff] [blame] | 936 | for (i = 0; i < NBANK(chip); i++) |
| 937 | bitmap_set_value8(val, 0x02, i * BANK_SZ); |
| 938 | |
Nicholas Krause | c75a377 | 2015-08-26 17:52:19 -0400 | [diff] [blame] | 939 | ret = pca953x_write_regs(chip, PCA957X_BKEN, val); |
| 940 | if (ret) |
| 941 | goto out; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 942 | |
| 943 | return 0; |
| 944 | out: |
| 945 | return ret; |
| 946 | } |
| 947 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 948 | static int pca953x_probe(struct i2c_client *client, |
Andy Shevchenko | b27d851 | 2019-12-04 16:53:40 -0800 | [diff] [blame] | 949 | const struct i2c_device_id *i2c_id) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 950 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 951 | struct pca953x_platform_data *pdata; |
| 952 | struct pca953x_chip *chip; |
Chandrabhanu Mahapatra | 6a7b36a | 2012-07-10 19:05:37 +0530 | [diff] [blame] | 953 | int irq_base = 0; |
Wolfram Sang | 7ea2aa2 | 2011-10-14 15:32:00 +0200 | [diff] [blame] | 954 | int ret; |
Chandrabhanu Mahapatra | 6a7b36a | 2012-07-10 19:05:37 +0530 | [diff] [blame] | 955 | u32 invert = 0; |
Phil Reid | e23efa3 | 2016-07-29 11:39:55 +0800 | [diff] [blame] | 956 | struct regulator *reg; |
Uwe Kleine-König | bcf41dc | 2020-04-20 20:27:51 +0300 | [diff] [blame] | 957 | const struct regmap_config *regmap_config; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 958 | |
Andy Shevchenko | b27d851 | 2019-12-04 16:53:40 -0800 | [diff] [blame] | 959 | chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 960 | if (chip == NULL) |
| 961 | return -ENOMEM; |
| 962 | |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 963 | pdata = dev_get_platdata(&client->dev); |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 964 | 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 Longerbeam | 054ccde | 2017-01-10 11:29:51 -0800 | [diff] [blame] | 970 | struct gpio_desc *reset_gpio; |
| 971 | |
Markus Pargmann | 4bb9334 | 2014-07-29 09:24:43 +0200 | [diff] [blame] | 972 | chip->gpio_start = -1; |
| 973 | irq_base = 0; |
Steve Longerbeam | 054ccde | 2017-01-10 11:29:51 -0800 | [diff] [blame] | 974 | |
Andy Shevchenko | 96530b3 | 2017-03-22 16:11:13 +0200 | [diff] [blame] | 975 | /* |
| 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 Longerbeam | 054ccde | 2017-01-10 11:29:51 -0800 | [diff] [blame] | 982 | 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 Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 986 | } |
| 987 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 988 | chip->client = client; |
| 989 | |
Phil Reid | e23efa3 | 2016-07-29 11:39:55 +0800 | [diff] [blame] | 990 | reg = devm_regulator_get(&client->dev, "vcc"); |
Krzysztof Kozlowski | ca6a77e | 2020-08-27 22:08:25 +0200 | [diff] [blame] | 991 | if (IS_ERR(reg)) |
| 992 | return dev_err_probe(&client->dev, PTR_ERR(reg), "reg get err\n"); |
| 993 | |
Phil Reid | e23efa3 | 2016-07-29 11:39:55 +0800 | [diff] [blame] | 994 | 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 Sang | 6212e1d | 2016-09-24 11:06:29 +0200 | [diff] [blame] | 1001 | if (i2c_id) { |
| 1002 | chip->driver_data = i2c_id->driver_data; |
Andy Shevchenko | f32517b | 2015-10-01 14:20:28 +0300 | [diff] [blame] | 1003 | } else { |
Andy Shevchenko | 8eeb467d | 2019-08-01 20:39:35 +0300 | [diff] [blame] | 1004 | const void *match; |
Andy Shevchenko | f32517b | 2015-10-01 14:20:28 +0300 | [diff] [blame] | 1005 | |
Andy Shevchenko | 8eeb467d | 2019-08-01 20:39:35 +0300 | [diff] [blame] | 1006 | match = device_get_match_data(&client->dev); |
| 1007 | if (!match) { |
| 1008 | ret = -ENODEV; |
| 1009 | goto err_exit; |
Ben Dooks | 6f29c9a | 2015-12-08 10:19:26 +0000 | [diff] [blame] | 1010 | } |
Andy Shevchenko | 8eeb467d | 2019-08-01 20:39:35 +0300 | [diff] [blame] | 1011 | |
| 1012 | chip->driver_data = (uintptr_t)match; |
Andy Shevchenko | f32517b | 2015-10-01 14:20:28 +0300 | [diff] [blame] | 1013 | } |
| 1014 | |
Marek Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 1015 | i2c_set_clientdata(client, chip); |
| 1016 | |
Uwe Kleine-König | bcf41dc | 2020-04-20 20:27:51 +0300 | [diff] [blame] | 1017 | 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 Vasut | 4942723 | 2018-12-12 02:39:58 +0100 | [diff] [blame] | 1028 | if (IS_ERR(chip->regmap)) { |
| 1029 | ret = PTR_ERR(chip->regmap); |
| 1030 | goto err_exit; |
| 1031 | } |
| 1032 | |
Marek Vasut | 0f25fda | 2018-12-12 02:39:59 +0100 | [diff] [blame] | 1033 | regcache_mark_dirty(chip->regmap); |
| 1034 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 1035 | mutex_init(&chip->i2c_lock); |
Bartosz Golaszewski | 74f47f0 | 2016-09-26 11:54:15 +0200 | [diff] [blame] | 1036 | /* |
| 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 Golaszewski | 559b469 | 2016-09-16 18:02:45 +0200 | [diff] [blame] | 1052 | lockdep_set_subclass(&chip->i2c_lock, |
| 1053 | i2c_adapter_depth(client->adapter)); |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 1054 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1055 | /* initialize cached registers from their original values. |
| 1056 | * we can't share this chip with another i2c master. |
| 1057 | */ |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 1058 | |
Marek Vasut | 7a04aaa | 2018-12-12 02:39:55 +0100 | [diff] [blame] | 1059 | if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE) { |
| 1060 | chip->regs = &pca953x_regs; |
| 1061 | ret = device_pca95xx_init(chip, invert); |
Bartosz Golaszewski | 7acc66e | 2016-09-09 11:17:35 +0200 | [diff] [blame] | 1062 | } else { |
Marek Vasut | 7a04aaa | 2018-12-12 02:39:55 +0100 | [diff] [blame] | 1063 | chip->regs = &pca957x_regs; |
Wolfram Sang | 7ea2aa2 | 2011-10-14 15:32:00 +0200 | [diff] [blame] | 1064 | ret = device_pca957x_init(chip, invert); |
Marek Vasut | 7a04aaa | 2018-12-12 02:39:55 +0100 | [diff] [blame] | 1065 | } |
Wolfram Sang | 7ea2aa2 | 2011-10-14 15:32:00 +0200 | [diff] [blame] | 1066 | if (ret) |
Phil Reid | e23efa3 | 2016-07-29 11:39:55 +0800 | [diff] [blame] | 1067 | goto err_exit; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1068 | |
Linus Walleij | edee3bc | 2020-07-17 16:40:40 +0200 | [diff] [blame] | 1069 | ret = pca953x_irq_setup(chip, irq_base); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 1070 | if (ret) |
Phil Reid | e23efa3 | 2016-07-29 11:39:55 +0800 | [diff] [blame] | 1071 | goto err_exit; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 1072 | |
Linus Walleij | edee3bc | 2020-07-17 16:40:40 +0200 | [diff] [blame] | 1073 | ret = devm_gpiochip_add_data(&client->dev, &chip->gpio_chip, chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1074 | if (ret) |
Phil Reid | e23efa3 | 2016-07-29 11:39:55 +0800 | [diff] [blame] | 1075 | goto err_exit; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1076 | |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 1077 | if (pdata && pdata->setup) { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1078 | ret = pdata->setup(client, chip->gpio_chip.base, |
Andy Shevchenko | b27d851 | 2019-12-04 16:53:40 -0800 | [diff] [blame] | 1079 | chip->gpio_chip.ngpio, pdata->context); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1080 | if (ret < 0) |
| 1081 | dev_warn(&client->dev, "setup failed, %d\n", ret); |
| 1082 | } |
| 1083 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1084 | return 0; |
Phil Reid | e23efa3 | 2016-07-29 11:39:55 +0800 | [diff] [blame] | 1085 | |
| 1086 | err_exit: |
| 1087 | regulator_disable(chip->regulator); |
| 1088 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1089 | } |
| 1090 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 1091 | static int pca953x_remove(struct i2c_client *client) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1092 | { |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 1093 | struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev); |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 1094 | struct pca953x_chip *chip = i2c_get_clientdata(client); |
Linus Walleij | d147d54 | 2016-09-13 14:43:23 +0200 | [diff] [blame] | 1095 | int ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1096 | |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 1097 | if (pdata && pdata->teardown) { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1098 | ret = pdata->teardown(client, chip->gpio_chip.base, |
Andy Shevchenko | b27d851 | 2019-12-04 16:53:40 -0800 | [diff] [blame] | 1099 | chip->gpio_chip.ngpio, pdata->context); |
Phil Reid | e23efa3 | 2016-07-29 11:39:55 +0800 | [diff] [blame] | 1100 | if (ret < 0) |
Andy Shevchenko | f4160fa | 2019-08-01 20:39:38 +0300 | [diff] [blame] | 1101 | dev_err(&client->dev, "teardown failed, %d\n", ret); |
Arnd Bergmann | bf62efe | 2016-08-26 17:25:42 +0200 | [diff] [blame] | 1102 | } else { |
| 1103 | ret = 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1104 | } |
| 1105 | |
Phil Reid | e23efa3 | 2016-07-29 11:39:55 +0800 | [diff] [blame] | 1106 | regulator_disable(chip->regulator); |
| 1107 | |
| 1108 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1109 | } |
| 1110 | |
Marek Vasut | b765743 | 2018-12-12 02:40:02 +0100 | [diff] [blame] | 1111 | #ifdef CONFIG_PM_SLEEP |
| 1112 | static 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 Shevchenko | 6dd6a2d | 2019-08-01 20:39:37 +0300 | [diff] [blame] | 1123 | if (ret) { |
Marek Vasut | b765743 | 2018-12-12 02:40:02 +0100 | [diff] [blame] | 1124 | 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 Shevchenko | 6dd6a2d | 2019-08-01 20:39:37 +0300 | [diff] [blame] | 1130 | if (ret) { |
Marek Vasut | b765743 | 2018-12-12 02:40:02 +0100 | [diff] [blame] | 1131 | 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 Shevchenko | 6dd6a2d | 2019-08-01 20:39:37 +0300 | [diff] [blame] | 1139 | if (ret) { |
Marek Vasut | b765743 | 2018-12-12 02:40:02 +0100 | [diff] [blame] | 1140 | 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 Shevchenko | 6dd6a2d | 2019-08-01 20:39:37 +0300 | [diff] [blame] | 1147 | if (ret) { |
Marek Vasut | b765743 | 2018-12-12 02:40:02 +0100 | [diff] [blame] | 1148 | 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 | |
| 1158 | static 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 Uytterhoeven | f70fbc1 | 2019-03-20 11:39:27 +0100 | [diff] [blame] | 1164 | if (atomic_read(&chip->wakeup_path)) |
| 1165 | device_set_wakeup_path(dev); |
| 1166 | else |
| 1167 | regulator_disable(chip->regulator); |
Marek Vasut | b765743 | 2018-12-12 02:40:02 +0100 | [diff] [blame] | 1168 | |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | static int pca953x_resume(struct device *dev) |
| 1173 | { |
| 1174 | struct pca953x_chip *chip = dev_get_drvdata(dev); |
| 1175 | int ret; |
| 1176 | |
Geert Uytterhoeven | f70fbc1 | 2019-03-20 11:39:27 +0100 | [diff] [blame] | 1177 | if (!atomic_read(&chip->wakeup_path)) { |
| 1178 | ret = regulator_enable(chip->regulator); |
Andy Shevchenko | 6dd6a2d | 2019-08-01 20:39:37 +0300 | [diff] [blame] | 1179 | if (ret) { |
Geert Uytterhoeven | f70fbc1 | 2019-03-20 11:39:27 +0100 | [diff] [blame] | 1180 | dev_err(dev, "Failed to enable regulator: %d\n", ret); |
| 1181 | return 0; |
| 1182 | } |
Marek Vasut | b765743 | 2018-12-12 02:40:02 +0100 | [diff] [blame] | 1183 | } |
| 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 Shevchenko | 6dd6a2d | 2019-08-01 20:39:37 +0300 | [diff] [blame] | 1192 | if (ret) { |
Marek Vasut | b765743 | 2018-12-12 02:40:02 +0100 | [diff] [blame] | 1193 | dev_err(dev, "Failed to restore register map: %d\n", ret); |
| 1194 | return ret; |
| 1195 | } |
| 1196 | |
| 1197 | return 0; |
| 1198 | } |
| 1199 | #endif |
| 1200 | |
Ben Dooks | 6f29c9a | 2015-12-08 10:19:26 +0000 | [diff] [blame] | 1201 | /* 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 Ripard | ed32620 | 2012-11-08 18:01:52 +0100 | [diff] [blame] | 1205 | static const struct of_device_id pca953x_dt_ids[] = { |
Alexandre Belloni | 12c7a4f | 2019-04-09 14:35:46 +0200 | [diff] [blame] | 1206 | { .compatible = "nxp,pca6416", .data = OF_953X(16, PCA_INT), }, |
Ben Dooks | 6f29c9a | 2015-12-08 10:19:26 +0000 | [diff] [blame] | 1207 | { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), }, |
Uwe Kleine-König | 1421b44 | 2021-01-15 17:46:56 +0100 | [diff] [blame] | 1208 | { .compatible = "nxp,pca9506", .data = OF_953X(40, PCA_INT), }, |
Ben Dooks | 6f29c9a | 2015-12-08 10:19:26 +0000 | [diff] [blame] | 1209 | { .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 Ripard | ed32620 | 2012-11-08 18:01:52 +0100 | [diff] [blame] | 1222 | |
Alexandre Belloni | 01769c4 | 2019-04-09 12:25:17 +0200 | [diff] [blame] | 1223 | { .compatible = "nxp,pcal6416", .data = OF_953X(16, PCA_LATCH_INT), }, |
H. Nikolaus Schaller | 0cdf21b | 2018-05-17 06:59:47 +0200 | [diff] [blame] | 1224 | { .compatible = "nxp,pcal6524", .data = OF_953X(24, PCA_LATCH_INT), }, |
Jan Kiszka | 3ba3ff5 | 2020-06-15 15:22:29 +0200 | [diff] [blame] | 1225 | { .compatible = "nxp,pcal9535", .data = OF_953X(16, PCA_LATCH_INT), }, |
Mike Looijmans | 237d961 | 2020-09-30 11:20:53 +0200 | [diff] [blame] | 1226 | { .compatible = "nxp,pcal9554b", .data = OF_953X( 8, PCA_LATCH_INT), }, |
H. Nikolaus Schaller | 0cdf21b | 2018-05-17 06:59:47 +0200 | [diff] [blame] | 1227 | { .compatible = "nxp,pcal9555a", .data = OF_953X(16, PCA_LATCH_INT), }, |
H. Nikolaus Schaller | 3a711e0 | 2018-03-10 12:00:01 +0100 | [diff] [blame] | 1228 | |
Ben Dooks | 6f29c9a | 2015-12-08 10:19:26 +0000 | [diff] [blame] | 1229 | { .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 Vasut | 1208c93 | 2016-10-17 18:36:49 +0200 | [diff] [blame] | 1233 | { .compatible = "maxim,max7318", .data = OF_953X(16, PCA_INT), }, |
Maxime Ripard | ed32620 | 2012-11-08 18:01:52 +0100 | [diff] [blame] | 1234 | |
Ben Dooks | 6f29c9a | 2015-12-08 10:19:26 +0000 | [diff] [blame] | 1235 | { .compatible = "ti,pca6107", .data = OF_953X( 8, PCA_INT), }, |
Vignesh R | 353661d | 2016-05-19 12:17:29 +0530 | [diff] [blame] | 1236 | { .compatible = "ti,pca9536", .data = OF_953X( 4, 0), }, |
Ben Dooks | 6f29c9a | 2015-12-08 10:19:26 +0000 | [diff] [blame] | 1237 | { .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 Robinson | 8b74ae4 | 2019-06-01 08:12:18 +0100 | [diff] [blame] | 1240 | { .compatible = "ti,tca9539", .data = OF_953X(16, PCA_INT), }, |
Aaron Sierra | e73760a | 2014-02-07 16:36:21 -0600 | [diff] [blame] | 1241 | |
Geert Uytterhoeven | 932002f | 2019-03-21 10:21:45 +0100 | [diff] [blame] | 1242 | { .compatible = "onnn,cat9554", .data = OF_953X( 8, PCA_INT), }, |
Sergei Shtylyov | 8a64e55 | 2017-11-16 23:18:32 +0300 | [diff] [blame] | 1243 | { .compatible = "onnn,pca9654", .data = OF_953X( 8, PCA_INT), }, |
Ben Dooks | 9f49f6d | 2015-12-08 10:19:25 +0000 | [diff] [blame] | 1244 | |
Ben Dooks | 6f29c9a | 2015-12-08 10:19:26 +0000 | [diff] [blame] | 1245 | { .compatible = "exar,xra1202", .data = OF_953X( 8, 0), }, |
Maxime Ripard | ed32620 | 2012-11-08 18:01:52 +0100 | [diff] [blame] | 1246 | { } |
| 1247 | }; |
| 1248 | |
| 1249 | MODULE_DEVICE_TABLE(of, pca953x_dt_ids); |
| 1250 | |
Marek Vasut | b765743 | 2018-12-12 02:40:02 +0100 | [diff] [blame] | 1251 | static SIMPLE_DEV_PM_OPS(pca953x_pm_ops, pca953x_suspend, pca953x_resume); |
| 1252 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 1253 | static struct i2c_driver pca953x_driver = { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1254 | .driver = { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 1255 | .name = "pca953x", |
Marek Vasut | b765743 | 2018-12-12 02:40:02 +0100 | [diff] [blame] | 1256 | .pm = &pca953x_pm_ops, |
Maxime Ripard | ed32620 | 2012-11-08 18:01:52 +0100 | [diff] [blame] | 1257 | .of_match_table = pca953x_dt_ids, |
Andy Shevchenko | 74910e1 | 2020-05-21 00:19:14 +0300 | [diff] [blame] | 1258 | .acpi_match_table = pca953x_acpi_ids, |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1259 | }, |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 1260 | .probe = pca953x_probe, |
| 1261 | .remove = pca953x_remove, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 1262 | .id_table = pca953x_id, |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1263 | }; |
| 1264 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 1265 | static int __init pca953x_init(void) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1266 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 1267 | return i2c_add_driver(&pca953x_driver); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1268 | } |
David Brownell | 2f8d119 | 2008-10-15 22:03:13 -0700 | [diff] [blame] | 1269 | /* register after i2c postcore initcall and before |
| 1270 | * subsys initcalls that may rely on these GPIOs |
| 1271 | */ |
| 1272 | subsys_initcall(pca953x_init); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1273 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 1274 | static void __exit pca953x_exit(void) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1275 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 1276 | i2c_del_driver(&pca953x_driver); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1277 | } |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 1278 | module_exit(pca953x_exit); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1279 | |
| 1280 | MODULE_AUTHOR("eric miao <eric.miao@marvell.com>"); |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 1281 | MODULE_DESCRIPTION("GPIO expander driver for PCA953x"); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1282 | MODULE_LICENSE("GPL"); |