Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

gpio: adp5585: add support for the adp5589 expander

Support the adp5589 I/O expander which supports up to 19 pins. We need
to add a chip_info based struct since accessing register "banks"
and "bits" differs between devices.

Also some register addresses are different.

While at it move ADP558X_GPIO_MAX defines to the main header file and
rename them. That information will be needed by the top level device in
a following change.

Acked-by: Linus Walleij <linus.walleij@linaro.org>
Acked-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Signed-off-by: Nuno Sá <nuno.sa@analog.com>
Link: https://lore.kernel.org/r/20250701-dev-adp5589-fw-v7-9-b1fcfe9e9826@analog.com
Signed-off-by: Lee Jones <lee@kernel.org>

authored by

Nuno Sá and committed by
Lee Jones
9f425bf7 7077fb50

+126 -43
+117 -34
drivers/gpio/gpio-adp5585.c
··· 4 4 * 5 5 * Copyright 2022 NXP 6 6 * Copyright 2024 Ideas on Board Oy 7 + * Copyright 2025 Analog Devices, Inc. 7 8 */ 8 9 9 10 #include <linux/device.h> ··· 15 14 #include <linux/regmap.h> 16 15 #include <linux/types.h> 17 16 18 - #define ADP5585_GPIO_MAX 11 17 + /* 18 + * Bank 0 covers pins "GPIO 1/R0" to "GPIO 6/R5", numbered 0 to 5 by the 19 + * driver, and bank 1 covers pins "GPIO 7/C0" to "GPIO 11/C4", numbered 6 to 20 + * 10. Some variants of the ADP5585 don't support "GPIO 6/R5". As the driver 21 + * uses identical GPIO numbering for all variants to avoid confusion, GPIO 5 is 22 + * marked as reserved in the device tree for variants that don't support it. 23 + */ 24 + #define ADP5585_BANK(n) ((n) >= 6 ? 1 : 0) 25 + #define ADP5585_BIT(n) ((n) >= 6 ? BIT((n) - 6) : BIT(n)) 26 + 27 + /* 28 + * Bank 0 covers pins "GPIO 1/R0" to "GPIO 8/R7", numbered 0 to 7 by the 29 + * driver, bank 1 covers pins "GPIO 9/C0" to "GPIO 16/C7", numbered 8 to 30 + * 15 and bank 3 covers pins "GPIO 17/C8" to "GPIO 19/C10", numbered 16 to 18. 31 + */ 32 + #define ADP5589_BANK(n) ((n) >> 3) 33 + #define ADP5589_BIT(n) BIT((n) & 0x7) 34 + 35 + struct adp5585_gpio_chip { 36 + int (*bank)(unsigned int off); 37 + int (*bit)(unsigned int off); 38 + unsigned int max_gpio; 39 + unsigned int debounce_dis_a; 40 + unsigned int rpull_cfg_a; 41 + unsigned int gpo_data_a; 42 + unsigned int gpo_out_a; 43 + unsigned int gpio_dir_a; 44 + unsigned int gpi_stat_a; 45 + bool has_bias_hole; 46 + }; 19 47 20 48 struct adp5585_gpio_dev { 21 49 struct gpio_chip gpio_chip; 50 + const struct adp5585_gpio_chip *info; 22 51 struct regmap *regmap; 23 52 }; 53 + 54 + static int adp5585_gpio_bank(unsigned int off) 55 + { 56 + return ADP5585_BANK(off); 57 + } 58 + 59 + static int adp5585_gpio_bit(unsigned int off) 60 + { 61 + return ADP5585_BIT(off); 62 + } 63 + 64 + static int adp5589_gpio_bank(unsigned int off) 65 + { 66 + return ADP5589_BANK(off); 67 + } 68 + 69 + static int adp5589_gpio_bit(unsigned int off) 70 + { 71 + return ADP5589_BIT(off); 72 + } 24 73 25 74 static int adp5585_gpio_get_direction(struct gpio_chip *chip, unsigned int off) 26 75 { 27 76 struct adp5585_gpio_dev *adp5585_gpio = gpiochip_get_data(chip); 28 - unsigned int bank = ADP5585_BANK(off); 29 - unsigned int bit = ADP5585_BIT(off); 77 + const struct adp5585_gpio_chip *info = adp5585_gpio->info; 30 78 unsigned int val; 31 79 32 - regmap_read(adp5585_gpio->regmap, ADP5585_GPIO_DIRECTION_A + bank, &val); 80 + regmap_read(adp5585_gpio->regmap, info->gpio_dir_a + info->bank(off), &val); 33 81 34 - return val & bit ? GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN; 82 + return val & info->bit(off) ? GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN; 35 83 } 36 84 37 85 static int adp5585_gpio_direction_input(struct gpio_chip *chip, unsigned int off) 38 86 { 39 87 struct adp5585_gpio_dev *adp5585_gpio = gpiochip_get_data(chip); 40 - unsigned int bank = ADP5585_BANK(off); 41 - unsigned int bit = ADP5585_BIT(off); 88 + const struct adp5585_gpio_chip *info = adp5585_gpio->info; 42 89 43 - return regmap_clear_bits(adp5585_gpio->regmap, 44 - ADP5585_GPIO_DIRECTION_A + bank, bit); 90 + return regmap_clear_bits(adp5585_gpio->regmap, info->gpio_dir_a + info->bank(off), 91 + info->bit(off)); 45 92 } 46 93 47 94 static int adp5585_gpio_direction_output(struct gpio_chip *chip, unsigned int off, int val) 48 95 { 49 96 struct adp5585_gpio_dev *adp5585_gpio = gpiochip_get_data(chip); 50 - unsigned int bank = ADP5585_BANK(off); 51 - unsigned int bit = ADP5585_BIT(off); 97 + const struct adp5585_gpio_chip *info = adp5585_gpio->info; 98 + unsigned int bank = info->bank(off); 99 + unsigned int bit = info->bit(off); 52 100 int ret; 53 101 54 - ret = regmap_update_bits(adp5585_gpio->regmap, 55 - ADP5585_GPO_DATA_OUT_A + bank, bit, 56 - val ? bit : 0); 102 + ret = regmap_update_bits(adp5585_gpio->regmap, info->gpo_data_a + bank, 103 + bit, val ? bit : 0); 57 104 if (ret) 58 105 return ret; 59 106 60 - return regmap_set_bits(adp5585_gpio->regmap, 61 - ADP5585_GPIO_DIRECTION_A + bank, bit); 107 + return regmap_set_bits(adp5585_gpio->regmap, info->gpio_dir_a + bank, 108 + bit); 62 109 } 63 110 64 111 static int adp5585_gpio_get_value(struct gpio_chip *chip, unsigned int off) 65 112 { 66 113 struct adp5585_gpio_dev *adp5585_gpio = gpiochip_get_data(chip); 67 - unsigned int bank = ADP5585_BANK(off); 68 - unsigned int bit = ADP5585_BIT(off); 114 + const struct adp5585_gpio_chip *info = adp5585_gpio->info; 115 + unsigned int bank = info->bank(off); 116 + unsigned int bit = info->bit(off); 69 117 unsigned int reg; 70 118 unsigned int val; 71 119 ··· 129 79 * .direction_input(), .direction_output() or .set() operations racing 130 80 * with this. 131 81 */ 132 - regmap_read(adp5585_gpio->regmap, ADP5585_GPIO_DIRECTION_A + bank, &val); 133 - reg = val & bit ? ADP5585_GPO_DATA_OUT_A : ADP5585_GPI_STATUS_A; 82 + regmap_read(adp5585_gpio->regmap, info->gpio_dir_a + bank, &val); 83 + reg = val & bit ? info->gpo_data_a : info->gpi_stat_a; 134 84 regmap_read(adp5585_gpio->regmap, reg + bank, &val); 135 85 136 86 return !!(val & bit); ··· 140 90 int val) 141 91 { 142 92 struct adp5585_gpio_dev *adp5585_gpio = gpiochip_get_data(chip); 143 - unsigned int bank = ADP5585_BANK(off); 144 - unsigned int bit = ADP5585_BIT(off); 93 + const struct adp5585_gpio_chip *info = adp5585_gpio->info; 94 + unsigned int bit = adp5585_gpio->info->bit(off); 145 95 146 - return regmap_update_bits(adp5585_gpio->regmap, 147 - ADP5585_GPO_DATA_OUT_A + bank, 96 + return regmap_update_bits(adp5585_gpio->regmap, info->gpo_data_a + info->bank(off), 148 97 bit, val ? bit : 0); 149 98 } 150 99 151 100 static int adp5585_gpio_set_bias(struct adp5585_gpio_dev *adp5585_gpio, 152 101 unsigned int off, unsigned int bias) 153 102 { 103 + const struct adp5585_gpio_chip *info = adp5585_gpio->info; 154 104 unsigned int bit, reg, mask, val; 155 105 156 106 /* ··· 158 108 * consecutive registers ADP5585_RPULL_CONFIG_*, with a hole of 4 bits 159 109 * after R5. 160 110 */ 161 - bit = off * 2 + (off > 5 ? 4 : 0); 162 - reg = ADP5585_RPULL_CONFIG_A + bit / 8; 111 + bit = off * 2; 112 + if (info->has_bias_hole) 113 + bit += (off > 5 ? 4 : 0); 114 + reg = info->rpull_cfg_a + bit / 8; 163 115 mask = ADP5585_Rx_PULL_CFG_MASK << (bit % 8); 164 116 val = bias << (bit % 8); 165 117 ··· 171 119 static int adp5585_gpio_set_drive(struct adp5585_gpio_dev *adp5585_gpio, 172 120 unsigned int off, enum pin_config_param drive) 173 121 { 174 - unsigned int bank = ADP5585_BANK(off); 175 - unsigned int bit = ADP5585_BIT(off); 122 + const struct adp5585_gpio_chip *info = adp5585_gpio->info; 123 + unsigned int bit = adp5585_gpio->info->bit(off); 176 124 177 125 return regmap_update_bits(adp5585_gpio->regmap, 178 - ADP5585_GPO_OUT_MODE_A + bank, bit, 126 + info->gpo_out_a + info->bank(off), bit, 179 127 drive == PIN_CONFIG_DRIVE_OPEN_DRAIN ? bit : 0); 180 128 } 181 129 182 130 static int adp5585_gpio_set_debounce(struct adp5585_gpio_dev *adp5585_gpio, 183 131 unsigned int off, unsigned int debounce) 184 132 { 185 - unsigned int bank = ADP5585_BANK(off); 186 - unsigned int bit = ADP5585_BIT(off); 133 + const struct adp5585_gpio_chip *info = adp5585_gpio->info; 134 + unsigned int bit = adp5585_gpio->info->bit(off); 187 135 188 136 return regmap_update_bits(adp5585_gpio->regmap, 189 - ADP5585_DEBOUNCE_DIS_A + bank, bit, 137 + info->debounce_dis_a + info->bank(off), bit, 190 138 debounce ? 0 : bit); 191 139 } 192 140 ··· 227 175 static int adp5585_gpio_probe(struct platform_device *pdev) 228 176 { 229 177 struct adp5585_dev *adp5585 = dev_get_drvdata(pdev->dev.parent); 178 + const struct platform_device_id *id = platform_get_device_id(pdev); 230 179 struct adp5585_gpio_dev *adp5585_gpio; 231 180 struct device *dev = &pdev->dev; 232 181 struct gpio_chip *gc; ··· 238 185 return -ENOMEM; 239 186 240 187 adp5585_gpio->regmap = adp5585->regmap; 188 + 189 + adp5585_gpio->info = (const struct adp5585_gpio_chip *)id->driver_data; 190 + if (!adp5585_gpio->info) 191 + return -ENODEV; 241 192 242 193 device_set_of_node_from_dev(dev, dev->parent); 243 194 ··· 256 199 gc->can_sleep = true; 257 200 258 201 gc->base = -1; 259 - gc->ngpio = ADP5585_GPIO_MAX; 202 + gc->ngpio = adp5585_gpio->info->max_gpio; 260 203 gc->label = pdev->name; 261 204 gc->owner = THIS_MODULE; 262 205 ··· 268 211 return 0; 269 212 } 270 213 214 + static const struct adp5585_gpio_chip adp5585_gpio_chip_info = { 215 + .bank = adp5585_gpio_bank, 216 + .bit = adp5585_gpio_bit, 217 + .debounce_dis_a = ADP5585_DEBOUNCE_DIS_A, 218 + .rpull_cfg_a = ADP5585_RPULL_CONFIG_A, 219 + .gpo_data_a = ADP5585_GPO_DATA_OUT_A, 220 + .gpo_out_a = ADP5585_GPO_OUT_MODE_A, 221 + .gpio_dir_a = ADP5585_GPIO_DIRECTION_A, 222 + .gpi_stat_a = ADP5585_GPI_STATUS_A, 223 + .max_gpio = ADP5585_PIN_MAX, 224 + .has_bias_hole = true, 225 + }; 226 + 227 + static const struct adp5585_gpio_chip adp5589_gpio_chip_info = { 228 + .bank = adp5589_gpio_bank, 229 + .bit = adp5589_gpio_bit, 230 + .debounce_dis_a = ADP5589_DEBOUNCE_DIS_A, 231 + .rpull_cfg_a = ADP5589_RPULL_CONFIG_A, 232 + .gpo_data_a = ADP5589_GPO_DATA_OUT_A, 233 + .gpo_out_a = ADP5589_GPO_OUT_MODE_A, 234 + .gpio_dir_a = ADP5589_GPIO_DIRECTION_A, 235 + .gpi_stat_a = ADP5589_GPI_STATUS_A, 236 + .max_gpio = ADP5589_PIN_MAX, 237 + }; 238 + 271 239 static const struct platform_device_id adp5585_gpio_id_table[] = { 272 - { "adp5585-gpio" }, 240 + { "adp5585-gpio", (kernel_ulong_t)&adp5585_gpio_chip_info }, 241 + { "adp5589-gpio", (kernel_ulong_t)&adp5589_gpio_chip_info }, 273 242 { /* Sentinel */ } 274 243 }; 275 244 MODULE_DEVICE_TABLE(platform, adp5585_gpio_id_table);
+9 -9
include/linux/mfd/adp5585.h
··· 107 107 108 108 #define ADP5585_MAX_REG ADP5585_INT_EN 109 109 110 - /* 111 - * Bank 0 covers pins "GPIO 1/R0" to "GPIO 6/R5", numbered 0 to 5 by the 112 - * driver, and bank 1 covers pins "GPIO 7/C0" to "GPIO 11/C4", numbered 6 to 113 - * 10. Some variants of the ADP5585 don't support "GPIO 6/R5". As the driver 114 - * uses identical GPIO numbering for all variants to avoid confusion, GPIO 5 is 115 - * marked as reserved in the device tree for variants that don't support it. 116 - */ 117 - #define ADP5585_BANK(n) ((n) >= 6 ? 1 : 0) 118 - #define ADP5585_BIT(n) ((n) >= 6 ? BIT((n) - 6) : BIT(n)) 110 + #define ADP5585_PIN_MAX 11 119 111 120 112 /* ADP5589 */ 121 113 #define ADP5589_MAN_ID_VALUE 0x10 114 + #define ADP5589_GPI_STATUS_A 0x16 122 115 #define ADP5589_GPI_STATUS_C 0x18 116 + #define ADP5589_RPULL_CONFIG_A 0x19 117 + #define ADP5589_DEBOUNCE_DIS_A 0x27 118 + #define ADP5589_GPO_DATA_OUT_A 0x2a 119 + #define ADP5589_GPO_OUT_MODE_A 0x2d 120 + #define ADP5589_GPIO_DIRECTION_A 0x30 123 121 #define ADP5589_PIN_CONFIG_D 0x4C 124 122 #define ADP5589_INT_EN 0x4e 125 123 #define ADP5589_MAX_REG ADP5589_INT_EN 124 + 125 + #define ADP5589_PIN_MAX 19 126 126 127 127 struct regmap; 128 128