1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Analog Devices ADP5585 I/O expander, PWM controller and keypad controller 4 * 5 * Copyright 2022 NXP 6 * Copyright 2024 Ideas on Board Oy 7 * Copyright 2025 Analog Devices Inc. 8 */ 9 10 #include <linux/array_size.h> 11 #include <linux/device.h> 12 #include <linux/err.h> 13 #include <linux/i2c.h> 14 #include <linux/mfd/adp5585.h> 15 #include <linux/mfd/core.h> 16 #include <linux/mod_devicetable.h> 17 #include <linux/module.h> 18 #include <linux/regmap.h> 19 #include <linux/types.h> 20 21 enum { 22 ADP5585_DEV_GPIO, 23 ADP5585_DEV_PWM, 24 ADP5585_DEV_MAX 25 }; 26 27 static const struct mfd_cell adp5585_devs[ADP5585_DEV_MAX] = { 28 MFD_CELL_NAME("adp5585-gpio"), 29 MFD_CELL_NAME("adp5585-pwm"), 30 }; 31 32 static const struct mfd_cell adp5589_devs[] = { 33 MFD_CELL_NAME("adp5589-gpio"), 34 MFD_CELL_NAME("adp5589-pwm"), 35 }; 36 37 static const struct regmap_range adp5585_volatile_ranges[] = { 38 regmap_reg_range(ADP5585_ID, ADP5585_GPI_STATUS_B), 39 }; 40 41 static const struct regmap_access_table adp5585_volatile_regs = { 42 .yes_ranges = adp5585_volatile_ranges, 43 .n_yes_ranges = ARRAY_SIZE(adp5585_volatile_ranges), 44 }; 45 46 static const struct regmap_range adp5589_volatile_ranges[] = { 47 regmap_reg_range(ADP5585_ID, ADP5589_GPI_STATUS_C), 48 }; 49 50 static const struct regmap_access_table adp5589_volatile_regs = { 51 .yes_ranges = adp5589_volatile_ranges, 52 .n_yes_ranges = ARRAY_SIZE(adp5589_volatile_ranges), 53 }; 54 55 /* 56 * Chip variants differ in the default configuration of pull-up and pull-down 57 * resistors, and therefore have different default register values: 58 * 59 * - The -00, -01 and -03 variants (collectively referred to as 60 * ADP5585_REGMAP_00) have pull-up on all GPIO pins by default. 61 * - The -02 variant has no default pull-up or pull-down resistors. 62 * - The -04 variant has default pull-down resistors on all GPIO pins. 63 */ 64 65 static const u8 adp5585_regmap_defaults_00[ADP5585_MAX_REG + 1] = { 66 /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 67 /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 68 /* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 69 /* 0x18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 70 /* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 71 /* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 72 /* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 73 /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00, 74 }; 75 76 static const u8 adp5585_regmap_defaults_02[ADP5585_MAX_REG + 1] = { 77 /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 78 /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 79 /* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc3, 80 /* 0x18 */ 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 81 /* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 82 /* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 83 /* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 84 /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00, 85 }; 86 87 static const u8 adp5585_regmap_defaults_04[ADP5585_MAX_REG + 1] = { 88 /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 89 /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 90 /* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 91 /* 0x18 */ 0x05, 0x55, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 92 /* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 93 /* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 94 /* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 95 /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00, 96 }; 97 98 static const u8 adp5589_regmap_defaults_00[ADP5589_MAX_REG + 1] = { 99 /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 100 /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 101 /* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 102 /* 0x18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 103 /* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 104 /* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 105 /* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 106 /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 107 /* 0x40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 108 /* 0x48 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 109 }; 110 111 static const u8 adp5589_regmap_defaults_01[ADP5589_MAX_REG + 1] = { 112 /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 113 /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 114 /* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 115 /* 0x18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 116 /* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 117 /* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 118 /* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 119 /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 120 /* 0x40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 121 /* 0x48 */ 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 122 }; 123 124 static const u8 adp5589_regmap_defaults_02[ADP5589_MAX_REG + 1] = { 125 /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 126 /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 127 /* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 128 /* 0x18 */ 0x00, 0x41, 0x01, 0x00, 0x11, 0x04, 0x00, 0x00, 129 /* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 130 /* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 131 /* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 132 /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 133 /* 0x40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 134 /* 0x48 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 135 }; 136 137 static const u8 *adp5585_regmap_defaults[ADP5585_MAX] = { 138 [ADP5585_00] = adp5585_regmap_defaults_00, 139 [ADP5585_01] = adp5585_regmap_defaults_00, 140 [ADP5585_02] = adp5585_regmap_defaults_02, 141 [ADP5585_03] = adp5585_regmap_defaults_00, 142 [ADP5585_04] = adp5585_regmap_defaults_04, 143 [ADP5589_00] = adp5589_regmap_defaults_00, 144 [ADP5589_01] = adp5589_regmap_defaults_01, 145 [ADP5589_02] = adp5589_regmap_defaults_02, 146 }; 147 148 static const struct regmap_config adp5585_regmap_config_template = { 149 .reg_bits = 8, 150 .val_bits = 8, 151 .max_register = ADP5585_MAX_REG, 152 .volatile_table = &adp5585_volatile_regs, 153 .cache_type = REGCACHE_MAPLE, 154 .num_reg_defaults_raw = ADP5585_MAX_REG + 1, 155 }; 156 157 static const struct regmap_config adp5589_regmap_config_template = { 158 .reg_bits = 8, 159 .val_bits = 8, 160 .max_register = ADP5589_MAX_REG, 161 .volatile_table = &adp5589_volatile_regs, 162 .cache_type = REGCACHE_MAPLE, 163 .num_reg_defaults_raw = ADP5589_MAX_REG + 1, 164 }; 165 166 static const struct adp5585_regs adp5585_regs = { 167 .ext_cfg = ADP5585_PIN_CONFIG_C, 168 }; 169 170 static const struct adp5585_regs adp5589_regs = { 171 .ext_cfg = ADP5589_PIN_CONFIG_D, 172 }; 173 174 static struct regmap_config *adp5585_fill_variant_config(struct adp5585_dev *adp5585) 175 { 176 struct regmap_config *regmap_config; 177 178 switch (adp5585->variant) { 179 case ADP5585_00: 180 case ADP5585_01: 181 case ADP5585_02: 182 case ADP5585_03: 183 case ADP5585_04: 184 adp5585->id = ADP5585_MAN_ID_VALUE; 185 adp5585->regs = &adp5585_regs; 186 regmap_config = devm_kmemdup(adp5585->dev, &adp5585_regmap_config_template, 187 sizeof(*regmap_config), GFP_KERNEL); 188 break; 189 case ADP5589_00: 190 case ADP5589_01: 191 case ADP5589_02: 192 adp5585->id = ADP5589_MAN_ID_VALUE; 193 adp5585->regs = &adp5589_regs; 194 regmap_config = devm_kmemdup(adp5585->dev, &adp5589_regmap_config_template, 195 sizeof(*regmap_config), GFP_KERNEL); 196 break; 197 default: 198 return ERR_PTR(-ENODEV); 199 } 200 201 if (!regmap_config) 202 return ERR_PTR(-ENOMEM); 203 204 regmap_config->reg_defaults_raw = adp5585_regmap_defaults[adp5585->variant]; 205 206 return regmap_config; 207 } 208 209 static int adp5585_add_devices(const struct adp5585_dev *adp5585) 210 { 211 struct device *dev = adp5585->dev; 212 const struct mfd_cell *cells; 213 int ret; 214 215 if (adp5585->id == ADP5585_MAN_ID_VALUE) 216 cells = adp5585_devs; 217 else 218 cells = adp5589_devs; 219 220 if (device_property_present(dev, "#pwm-cells")) { 221 ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO, 222 &cells[ADP5585_DEV_PWM], 1, NULL, 0, NULL); 223 if (ret) 224 return dev_err_probe(dev, ret, "Failed to add PWM device\n"); 225 } 226 227 if (device_property_present(dev, "#gpio-cells")) { 228 ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO, 229 &cells[ADP5585_DEV_GPIO], 1, NULL, 0, NULL); 230 if (ret) 231 return dev_err_probe(dev, ret, "Failed to add GPIO device\n"); 232 } 233 234 return 0; 235 } 236 237 static void adp5585_osc_disable(void *data) 238 { 239 const struct adp5585_dev *adp5585 = data; 240 241 regmap_write(adp5585->regmap, ADP5585_GENERAL_CFG, 0); 242 } 243 244 static int adp5585_i2c_probe(struct i2c_client *i2c) 245 { 246 struct regmap_config *regmap_config; 247 struct adp5585_dev *adp5585; 248 unsigned int id; 249 int ret; 250 251 adp5585 = devm_kzalloc(&i2c->dev, sizeof(*adp5585), GFP_KERNEL); 252 if (!adp5585) 253 return -ENOMEM; 254 255 i2c_set_clientdata(i2c, adp5585); 256 adp5585->dev = &i2c->dev; 257 258 adp5585->variant = (enum adp5585_variant)(uintptr_t)i2c_get_match_data(i2c); 259 if (!adp5585->variant) 260 return -ENODEV; 261 262 regmap_config = adp5585_fill_variant_config(adp5585); 263 if (IS_ERR(regmap_config)) 264 return PTR_ERR(regmap_config); 265 266 adp5585->regmap = devm_regmap_init_i2c(i2c, regmap_config); 267 if (IS_ERR(adp5585->regmap)) 268 return dev_err_probe(&i2c->dev, PTR_ERR(adp5585->regmap), 269 "Failed to initialize register map\n"); 270 271 ret = regmap_read(adp5585->regmap, ADP5585_ID, &id); 272 if (ret) 273 return dev_err_probe(&i2c->dev, ret, 274 "Failed to read device ID\n"); 275 276 id &= ADP5585_MAN_ID_MASK; 277 if (id != adp5585->id) 278 return dev_err_probe(&i2c->dev, -ENODEV, 279 "Invalid device ID 0x%02x\n", id); 280 281 /* 282 * Enable the internal oscillator, as it's shared between multiple 283 * functions. 284 */ 285 ret = regmap_set_bits(adp5585->regmap, ADP5585_GENERAL_CFG, ADP5585_OSC_EN); 286 if (ret) 287 return ret; 288 289 ret = devm_add_action_or_reset(&i2c->dev, adp5585_osc_disable, adp5585); 290 if (ret) 291 return ret; 292 293 return adp5585_add_devices(adp5585); 294 } 295 296 static int adp5585_suspend(struct device *dev) 297 { 298 struct adp5585_dev *adp5585 = dev_get_drvdata(dev); 299 300 regcache_cache_only(adp5585->regmap, true); 301 302 return 0; 303 } 304 305 static int adp5585_resume(struct device *dev) 306 { 307 struct adp5585_dev *adp5585 = dev_get_drvdata(dev); 308 309 regcache_cache_only(adp5585->regmap, false); 310 regcache_mark_dirty(adp5585->regmap); 311 312 return regcache_sync(adp5585->regmap); 313 } 314 315 static DEFINE_SIMPLE_DEV_PM_OPS(adp5585_pm, adp5585_suspend, adp5585_resume); 316 317 static const struct of_device_id adp5585_of_match[] = { 318 { 319 .compatible = "adi,adp5585-00", 320 .data = (void *)ADP5585_00, 321 }, { 322 .compatible = "adi,adp5585-01", 323 .data = (void *)ADP5585_01, 324 }, { 325 .compatible = "adi,adp5585-02", 326 .data = (void *)ADP5585_02, 327 }, { 328 .compatible = "adi,adp5585-03", 329 .data = (void *)ADP5585_03, 330 }, { 331 .compatible = "adi,adp5585-04", 332 .data = (void *)ADP5585_04, 333 }, { 334 .compatible = "adi,adp5589-00", 335 .data = (void *)ADP5589_00, 336 }, { 337 .compatible = "adi,adp5589-01", 338 .data = (void *)ADP5589_01, 339 }, { 340 .compatible = "adi,adp5589-02", 341 .data = (void *)ADP5589_02, 342 }, { 343 .compatible = "adi,adp5589", 344 .data = (void *)ADP5589_00, 345 }, 346 { /* sentinel */ } 347 }; 348 MODULE_DEVICE_TABLE(of, adp5585_of_match); 349 350 static struct i2c_driver adp5585_i2c_driver = { 351 .driver = { 352 .name = "adp5585", 353 .of_match_table = adp5585_of_match, 354 .pm = pm_sleep_ptr(&adp5585_pm), 355 }, 356 .probe = adp5585_i2c_probe, 357 }; 358 module_i2c_driver(adp5585_i2c_driver); 359 360 MODULE_DESCRIPTION("ADP5585 core driver"); 361 MODULE_AUTHOR("Haibo Chen <haibo.chen@nxp.com>"); 362 MODULE_LICENSE("GPL"); 363