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