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/bitfield.h> 12 #include <linux/device.h> 13 #include <linux/err.h> 14 #include <linux/i2c.h> 15 #include <linux/gpio/consumer.h> 16 #include <linux/mfd/adp5585.h> 17 #include <linux/mfd/core.h> 18 #include <linux/module.h> 19 #include <linux/regmap.h> 20 #include <linux/regulator/consumer.h> 21 #include <linux/types.h> 22 23 enum { 24 ADP5585_DEV_GPIO, 25 ADP5585_DEV_PWM, 26 ADP5585_DEV_INPUT, 27 ADP5585_DEV_MAX 28 }; 29 30 static const struct mfd_cell adp5585_devs[ADP5585_DEV_MAX] = { 31 MFD_CELL_NAME("adp5585-gpio"), 32 MFD_CELL_NAME("adp5585-pwm"), 33 MFD_CELL_NAME("adp5585-keys"), 34 }; 35 36 static const struct mfd_cell adp5589_devs[] = { 37 MFD_CELL_NAME("adp5589-gpio"), 38 MFD_CELL_NAME("adp5589-pwm"), 39 MFD_CELL_NAME("adp5589-keys"), 40 }; 41 42 static const struct regmap_range adp5585_volatile_ranges[] = { 43 regmap_reg_range(ADP5585_ID, ADP5585_GPI_STATUS_B), 44 }; 45 46 static const struct regmap_access_table adp5585_volatile_regs = { 47 .yes_ranges = adp5585_volatile_ranges, 48 .n_yes_ranges = ARRAY_SIZE(adp5585_volatile_ranges), 49 }; 50 51 static const struct regmap_range adp5589_volatile_ranges[] = { 52 regmap_reg_range(ADP5585_ID, ADP5589_GPI_STATUS_C), 53 }; 54 55 static const struct regmap_access_table adp5589_volatile_regs = { 56 .yes_ranges = adp5589_volatile_ranges, 57 .n_yes_ranges = ARRAY_SIZE(adp5589_volatile_ranges), 58 }; 59 60 /* 61 * Chip variants differ in the default configuration of pull-up and pull-down 62 * resistors, and therefore have different default register values: 63 * 64 * - The -00, -01 and -03 variants (collectively referred to as 65 * ADP5585_REGMAP_00) have pull-up on all GPIO pins by default. 66 * - The -02 variant has no default pull-up or pull-down resistors. 67 * - The -04 variant has default pull-down resistors on all GPIO pins. 68 */ 69 70 static const u8 adp5585_regmap_defaults_00[ADP5585_MAX_REG + 1] = { 71 /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 72 /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 73 /* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 74 /* 0x18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 75 /* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 76 /* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 77 /* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 78 /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00, 79 }; 80 81 static const u8 adp5585_regmap_defaults_02[ADP5585_MAX_REG + 1] = { 82 /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 83 /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 84 /* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc3, 85 /* 0x18 */ 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 86 /* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 87 /* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 88 /* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 89 /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00, 90 }; 91 92 static const u8 adp5585_regmap_defaults_04[ADP5585_MAX_REG + 1] = { 93 /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 94 /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 95 /* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 96 /* 0x18 */ 0x05, 0x55, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 97 /* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 98 /* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 99 /* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 100 /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00, 101 }; 102 103 static const u8 adp5589_regmap_defaults_00[ADP5589_MAX_REG + 1] = { 104 /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 105 /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 106 /* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 107 /* 0x18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 108 /* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 109 /* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 110 /* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 111 /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 112 /* 0x40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 113 /* 0x48 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 114 }; 115 116 static const u8 adp5589_regmap_defaults_01[ADP5589_MAX_REG + 1] = { 117 /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 118 /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 119 /* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 120 /* 0x18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 121 /* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 122 /* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 123 /* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 124 /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 125 /* 0x40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 126 /* 0x48 */ 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 127 }; 128 129 static const u8 adp5589_regmap_defaults_02[ADP5589_MAX_REG + 1] = { 130 /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 131 /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 132 /* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 133 /* 0x18 */ 0x00, 0x41, 0x01, 0x00, 0x11, 0x04, 0x00, 0x00, 134 /* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 135 /* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 136 /* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 137 /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 138 /* 0x40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 139 /* 0x48 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 140 }; 141 142 static const u8 *adp5585_regmap_defaults[ADP5585_MAX] = { 143 [ADP5585_00] = adp5585_regmap_defaults_00, 144 [ADP5585_01] = adp5585_regmap_defaults_00, 145 [ADP5585_02] = adp5585_regmap_defaults_02, 146 [ADP5585_03] = adp5585_regmap_defaults_00, 147 [ADP5585_04] = adp5585_regmap_defaults_04, 148 [ADP5589_00] = adp5589_regmap_defaults_00, 149 [ADP5589_01] = adp5589_regmap_defaults_01, 150 [ADP5589_02] = adp5589_regmap_defaults_02, 151 }; 152 153 static const struct regmap_config adp5585_regmap_config_template = { 154 .reg_bits = 8, 155 .val_bits = 8, 156 .max_register = ADP5585_MAX_REG, 157 .volatile_table = &adp5585_volatile_regs, 158 .cache_type = REGCACHE_MAPLE, 159 .num_reg_defaults_raw = ADP5585_MAX_REG + 1, 160 }; 161 162 static const struct regmap_config adp5589_regmap_config_template = { 163 .reg_bits = 8, 164 .val_bits = 8, 165 .max_register = ADP5589_MAX_REG, 166 .volatile_table = &adp5589_volatile_regs, 167 .cache_type = REGCACHE_MAPLE, 168 .num_reg_defaults_raw = ADP5589_MAX_REG + 1, 169 }; 170 171 static const struct adp5585_regs adp5585_regs = { 172 .ext_cfg = ADP5585_PIN_CONFIG_C, 173 .int_en = ADP5585_INT_EN, 174 .gen_cfg = ADP5585_GENERAL_CFG, 175 .poll_ptime_cfg = ADP5585_POLL_PTIME_CFG, 176 .reset_cfg = ADP5585_RESET_CFG, 177 .reset1_event_a = ADP5585_RESET1_EVENT_A, 178 .reset2_event_a = ADP5585_RESET2_EVENT_A, 179 .pin_cfg_a = ADP5585_PIN_CONFIG_A, 180 }; 181 182 static const struct adp5585_regs adp5589_regs = { 183 .ext_cfg = ADP5589_PIN_CONFIG_D, 184 .int_en = ADP5589_INT_EN, 185 .gen_cfg = ADP5589_GENERAL_CFG, 186 .poll_ptime_cfg = ADP5589_POLL_PTIME_CFG, 187 .reset_cfg = ADP5589_RESET_CFG, 188 .reset1_event_a = ADP5589_RESET1_EVENT_A, 189 .reset2_event_a = ADP5589_RESET2_EVENT_A, 190 .pin_cfg_a = ADP5589_PIN_CONFIG_A, 191 }; 192 193 static int adp5585_validate_event(const struct adp5585_dev *adp5585, unsigned int ev) 194 { 195 if (adp5585->has_pin6) { 196 if (ev >= ADP5585_ROW5_KEY_EVENT_START && ev <= ADP5585_ROW5_KEY_EVENT_END) 197 return 0; 198 if (ev >= ADP5585_GPI_EVENT_START && ev <= ADP5585_GPI_EVENT_END) 199 return 0; 200 201 return dev_err_probe(adp5585->dev, -EINVAL, 202 "Invalid unlock/reset event(%u) for this device\n", ev); 203 } 204 205 if (ev >= ADP5585_KEY_EVENT_START && ev <= ADP5585_KEY_EVENT_END) 206 return 0; 207 if (ev >= ADP5585_GPI_EVENT_START && ev <= ADP5585_GPI_EVENT_END) { 208 /* 209 * Some variants of the adp5585 do not have the Row 5 210 * (meaning pin 6 or GPIO 6) available. Instead that pin serves 211 * as a reset pin. So, we need to make sure no event is 212 * configured for it. 213 */ 214 if (ev == (ADP5585_GPI_EVENT_START + 5)) 215 return dev_err_probe(adp5585->dev, -EINVAL, 216 "Invalid unlock/reset event(%u). R5 not available\n", 217 ev); 218 return 0; 219 } 220 221 return dev_err_probe(adp5585->dev, -EINVAL, 222 "Invalid unlock/reset event(%u) for this device\n", ev); 223 } 224 225 static int adp5589_validate_event(const struct adp5585_dev *adp5585, unsigned int ev) 226 { 227 if (ev >= ADP5589_KEY_EVENT_START && ev <= ADP5589_KEY_EVENT_END) 228 return 0; 229 if (ev >= ADP5589_GPI_EVENT_START && ev <= ADP5589_GPI_EVENT_END) 230 return 0; 231 232 return dev_err_probe(adp5585->dev, -EINVAL, 233 "Invalid unlock/reset event(%u) for this device\n", ev); 234 } 235 236 static struct regmap_config *adp5585_fill_variant_config(struct adp5585_dev *adp5585) 237 { 238 struct regmap_config *regmap_config; 239 240 switch (adp5585->variant) { 241 case ADP5585_00: 242 case ADP5585_01: 243 case ADP5585_02: 244 case ADP5585_03: 245 case ADP5585_04: 246 adp5585->id = ADP5585_MAN_ID_VALUE; 247 adp5585->regs = &adp5585_regs; 248 adp5585->n_pins = ADP5585_PIN_MAX; 249 adp5585->reset2_out = ADP5585_RESET2_OUT; 250 if (adp5585->variant == ADP5585_01) 251 adp5585->has_pin6 = true; 252 regmap_config = devm_kmemdup(adp5585->dev, &adp5585_regmap_config_template, 253 sizeof(*regmap_config), GFP_KERNEL); 254 break; 255 case ADP5589_00: 256 case ADP5589_01: 257 case ADP5589_02: 258 adp5585->id = ADP5589_MAN_ID_VALUE; 259 adp5585->regs = &adp5589_regs; 260 adp5585->has_unlock = true; 261 adp5585->has_pin6 = true; 262 adp5585->n_pins = ADP5589_PIN_MAX; 263 adp5585->reset2_out = ADP5589_RESET2_OUT; 264 regmap_config = devm_kmemdup(adp5585->dev, &adp5589_regmap_config_template, 265 sizeof(*regmap_config), GFP_KERNEL); 266 break; 267 default: 268 return ERR_PTR(-ENODEV); 269 } 270 271 if (!regmap_config) 272 return ERR_PTR(-ENOMEM); 273 274 regmap_config->reg_defaults_raw = adp5585_regmap_defaults[adp5585->variant]; 275 276 return regmap_config; 277 } 278 279 static int adp5585_parse_ev_array(const struct adp5585_dev *adp5585, const char *prop, u32 *events, 280 u32 *n_events, u32 max_evs, bool reset_ev) 281 { 282 struct device *dev = adp5585->dev; 283 unsigned int ev; 284 int ret; 285 286 /* 287 * The device has the capability of handling special events through GPIs or a Keypad: 288 * unlock events: Unlock the keymap until one of the configured events is detected. 289 * reset events: Generate a reset pulse when one of the configured events is detected. 290 */ 291 ret = device_property_count_u32(dev, prop); 292 if (ret < 0) 293 return 0; 294 295 *n_events = ret; 296 297 if (!adp5585->has_unlock && !reset_ev) 298 return dev_err_probe(dev, -EOPNOTSUPP, "Unlock keys not supported\n"); 299 300 if (*n_events > max_evs) 301 return dev_err_probe(dev, -EINVAL, 302 "Invalid number of keys(%u > %u) for %s\n", 303 *n_events, max_evs, prop); 304 305 ret = device_property_read_u32_array(dev, prop, events, *n_events); 306 if (ret) 307 return ret; 308 309 for (ev = 0; ev < *n_events; ev++) { 310 if (!reset_ev && events[ev] == ADP5589_UNLOCK_WILDCARD) 311 continue; 312 313 if (adp5585->id == ADP5585_MAN_ID_VALUE) 314 ret = adp5585_validate_event(adp5585, events[ev]); 315 else 316 ret = adp5589_validate_event(adp5585, events[ev]); 317 if (ret) 318 return ret; 319 } 320 321 return 0; 322 } 323 324 static int adp5585_unlock_ev_parse(struct adp5585_dev *adp5585) 325 { 326 struct device *dev = adp5585->dev; 327 int ret; 328 329 ret = adp5585_parse_ev_array(adp5585, "adi,unlock-events", adp5585->unlock_keys, 330 &adp5585->nkeys_unlock, ARRAY_SIZE(adp5585->unlock_keys), 331 false); 332 if (ret) 333 return ret; 334 if (!adp5585->nkeys_unlock) 335 return 0; 336 337 ret = device_property_read_u32(dev, "adi,unlock-trigger-sec", &adp5585->unlock_time); 338 if (!ret) { 339 if (adp5585->unlock_time > ADP5585_MAX_UNLOCK_TIME_SEC) 340 return dev_err_probe(dev, -EINVAL, 341 "Invalid unlock time(%u > %d)\n", 342 adp5585->unlock_time, 343 ADP5585_MAX_UNLOCK_TIME_SEC); 344 } 345 346 return 0; 347 } 348 349 static int adp5585_reset_ev_parse(struct adp5585_dev *adp5585) 350 { 351 struct device *dev = adp5585->dev; 352 u32 prop_val; 353 int ret; 354 355 ret = adp5585_parse_ev_array(adp5585, "adi,reset1-events", adp5585->reset1_keys, 356 &adp5585->nkeys_reset1, 357 ARRAY_SIZE(adp5585->reset1_keys), true); 358 if (ret) 359 return ret; 360 361 ret = adp5585_parse_ev_array(adp5585, "adi,reset2-events", 362 adp5585->reset2_keys, 363 &adp5585->nkeys_reset2, 364 ARRAY_SIZE(adp5585->reset2_keys), true); 365 if (ret) 366 return ret; 367 368 if (!adp5585->nkeys_reset1 && !adp5585->nkeys_reset2) 369 return 0; 370 371 if (adp5585->nkeys_reset1 && device_property_read_bool(dev, "adi,reset1-active-high")) 372 adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET1_POL, 1); 373 374 if (adp5585->nkeys_reset2 && device_property_read_bool(dev, "adi,reset2-active-high")) 375 adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET2_POL, 1); 376 377 if (device_property_read_bool(dev, "adi,rst-passthrough-enable")) 378 adp5585->reset_cfg |= FIELD_PREP(ADP5585_RST_PASSTHRU_EN, 1); 379 380 ret = device_property_read_u32(dev, "adi,reset-trigger-ms", &prop_val); 381 if (!ret) { 382 switch (prop_val) { 383 case 0: 384 adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET_TRIG_TIME, 0); 385 break; 386 case 1000: 387 adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET_TRIG_TIME, 1); 388 break; 389 case 1500: 390 adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET_TRIG_TIME, 2); 391 break; 392 case 2000: 393 adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET_TRIG_TIME, 3); 394 break; 395 case 2500: 396 adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET_TRIG_TIME, 4); 397 break; 398 case 3000: 399 adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET_TRIG_TIME, 5); 400 break; 401 case 3500: 402 adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET_TRIG_TIME, 6); 403 break; 404 case 4000: 405 adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET_TRIG_TIME, 7); 406 break; 407 default: 408 return dev_err_probe(dev, -EINVAL, 409 "Invalid value(%u) for adi,reset-trigger-ms\n", 410 prop_val); 411 } 412 } 413 414 ret = device_property_read_u32(dev, "adi,reset-pulse-width-us", &prop_val); 415 if (!ret) { 416 switch (prop_val) { 417 case 500: 418 adp5585->reset_cfg |= FIELD_PREP(ADP5585_PULSE_WIDTH, 0); 419 break; 420 case 1000: 421 adp5585->reset_cfg |= FIELD_PREP(ADP5585_PULSE_WIDTH, 1); 422 break; 423 case 2000: 424 adp5585->reset_cfg |= FIELD_PREP(ADP5585_PULSE_WIDTH, 2); 425 break; 426 case 10000: 427 adp5585->reset_cfg |= FIELD_PREP(ADP5585_PULSE_WIDTH, 3); 428 break; 429 default: 430 return dev_err_probe(dev, -EINVAL, 431 "Invalid value(%u) for adi,reset-pulse-width-us\n", 432 prop_val); 433 } 434 } 435 436 return 0; 437 } 438 439 static int adp5585_add_devices(const struct adp5585_dev *adp5585) 440 { 441 struct device *dev = adp5585->dev; 442 const struct mfd_cell *cells; 443 int ret; 444 445 if (adp5585->id == ADP5585_MAN_ID_VALUE) 446 cells = adp5585_devs; 447 else 448 cells = adp5589_devs; 449 450 if (device_property_present(dev, "#pwm-cells")) { 451 /* Make sure the PWM output pin is not used by the GPIO or INPUT devices */ 452 __set_bit(ADP5585_PWM_OUT, adp5585->pin_usage); 453 ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO, 454 &cells[ADP5585_DEV_PWM], 1, NULL, 0, NULL); 455 if (ret) 456 return dev_err_probe(dev, ret, "Failed to add PWM device\n"); 457 } 458 459 if (device_property_present(dev, "#gpio-cells")) { 460 ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO, 461 &cells[ADP5585_DEV_GPIO], 1, NULL, 0, NULL); 462 if (ret) 463 return dev_err_probe(dev, ret, "Failed to add GPIO device\n"); 464 } 465 466 if (device_property_present(adp5585->dev, "adi,keypad-pins")) { 467 ret = devm_mfd_add_devices(adp5585->dev, PLATFORM_DEVID_AUTO, 468 &cells[ADP5585_DEV_INPUT], 1, NULL, 0, NULL); 469 if (ret) 470 return dev_err_probe(dev, ret, "Failed to add input device\n"); 471 } 472 473 return 0; 474 } 475 476 static void adp5585_osc_disable(void *data) 477 { 478 const struct adp5585_dev *adp5585 = data; 479 480 regmap_write(adp5585->regmap, ADP5585_GENERAL_CFG, 0); 481 } 482 483 static void adp5585_report_events(struct adp5585_dev *adp5585, int ev_cnt) 484 { 485 unsigned int i; 486 487 for (i = 0; i < ev_cnt; i++) { 488 unsigned long key_val, key_press; 489 unsigned int key; 490 int ret; 491 492 ret = regmap_read(adp5585->regmap, ADP5585_FIFO_1 + i, &key); 493 if (ret) 494 return; 495 496 key_val = FIELD_GET(ADP5585_KEY_EVENT_MASK, key); 497 key_press = FIELD_GET(ADP5585_KEV_EV_PRESS_MASK, key); 498 499 blocking_notifier_call_chain(&adp5585->event_notifier, key_val, (void *)key_press); 500 } 501 } 502 503 static irqreturn_t adp5585_irq(int irq, void *data) 504 { 505 struct adp5585_dev *adp5585 = data; 506 unsigned int status, ev_cnt; 507 int ret; 508 509 ret = regmap_read(adp5585->regmap, ADP5585_INT_STATUS, &status); 510 if (ret) 511 return IRQ_HANDLED; 512 513 if (status & ADP5585_OVRFLOW_INT) 514 dev_err_ratelimited(adp5585->dev, "Event overflow error\n"); 515 516 if (!(status & ADP5585_EVENT_INT)) 517 goto out_irq; 518 519 ret = regmap_read(adp5585->regmap, ADP5585_STATUS, &ev_cnt); 520 if (ret) 521 goto out_irq; 522 523 ev_cnt = FIELD_GET(ADP5585_EC_MASK, ev_cnt); 524 if (!ev_cnt) 525 goto out_irq; 526 527 adp5585_report_events(adp5585, ev_cnt); 528 out_irq: 529 regmap_write(adp5585->regmap, ADP5585_INT_STATUS, status); 530 return IRQ_HANDLED; 531 } 532 533 static int adp5585_setup(struct adp5585_dev *adp5585) 534 { 535 const struct adp5585_regs *regs = adp5585->regs; 536 unsigned int reg_val = 0, i; 537 int ret; 538 539 /* If pin_6 (ROW5/GPI6) is not available, make sure to mark it as "busy" */ 540 if (!adp5585->has_pin6) 541 __set_bit(ADP5585_ROW5, adp5585->pin_usage); 542 543 /* Configure the device with reset and unlock events */ 544 for (i = 0; i < adp5585->nkeys_unlock; i++) { 545 ret = regmap_write(adp5585->regmap, ADP5589_UNLOCK1 + i, 546 adp5585->unlock_keys[i] | ADP5589_UNLOCK_EV_PRESS); 547 if (ret) 548 return ret; 549 } 550 551 if (adp5585->nkeys_unlock) { 552 ret = regmap_update_bits(adp5585->regmap, ADP5589_UNLOCK_TIMERS, 553 ADP5589_UNLOCK_TIMER, adp5585->unlock_time); 554 if (ret) 555 return ret; 556 557 ret = regmap_set_bits(adp5585->regmap, ADP5589_LOCK_CFG, ADP5589_LOCK_EN); 558 if (ret) 559 return ret; 560 } 561 562 for (i = 0; i < adp5585->nkeys_reset1; i++) { 563 ret = regmap_write(adp5585->regmap, regs->reset1_event_a + i, 564 adp5585->reset1_keys[i] | ADP5585_RESET_EV_PRESS); 565 if (ret) 566 return ret; 567 568 /* Mark that pin as not usable for the INPUT and GPIO devices. */ 569 __set_bit(ADP5585_RESET1_OUT, adp5585->pin_usage); 570 } 571 572 for (i = 0; i < adp5585->nkeys_reset2; i++) { 573 ret = regmap_write(adp5585->regmap, regs->reset2_event_a + i, 574 adp5585->reset2_keys[i] | ADP5585_RESET_EV_PRESS); 575 if (ret) 576 return ret; 577 578 __set_bit(adp5585->reset2_out, adp5585->pin_usage); 579 } 580 581 if (adp5585->nkeys_reset1 || adp5585->nkeys_reset2) { 582 ret = regmap_write(adp5585->regmap, regs->reset_cfg, adp5585->reset_cfg); 583 if (ret) 584 return ret; 585 586 /* If there's a reset1 event, then R4 is used as an output for the reset signal */ 587 if (adp5585->nkeys_reset1) 588 reg_val = ADP5585_R4_EXTEND_CFG_RESET1; 589 /* If there's a reset2 event, then C4 is used as an output for the reset signal */ 590 if (adp5585->nkeys_reset2) 591 reg_val |= ADP5585_C4_EXTEND_CFG_RESET2; 592 593 ret = regmap_update_bits(adp5585->regmap, regs->ext_cfg, 594 ADP5585_C4_EXTEND_CFG_MASK | ADP5585_R4_EXTEND_CFG_MASK, 595 reg_val); 596 if (ret) 597 return ret; 598 } 599 600 /* Clear any possible event by reading all the FIFO entries */ 601 for (i = 0; i < ADP5585_EV_MAX; i++) { 602 ret = regmap_read(adp5585->regmap, ADP5585_FIFO_1 + i, ®_val); 603 if (ret) 604 return ret; 605 } 606 607 ret = regmap_write(adp5585->regmap, regs->poll_ptime_cfg, adp5585->ev_poll_time); 608 if (ret) 609 return ret; 610 611 /* 612 * Enable the internal oscillator, as it's shared between multiple 613 * functions. 614 */ 615 ret = regmap_write(adp5585->regmap, regs->gen_cfg, 616 ADP5585_OSC_FREQ_500KHZ | ADP5585_INT_CFG | ADP5585_OSC_EN); 617 if (ret) 618 return ret; 619 620 return devm_add_action_or_reset(adp5585->dev, adp5585_osc_disable, adp5585); 621 } 622 623 static int adp5585_parse_fw(struct adp5585_dev *adp5585) 624 { 625 unsigned int prop_val; 626 int ret; 627 628 ret = device_property_read_u32(adp5585->dev, "poll-interval", &prop_val); 629 if (!ret) { 630 adp5585->ev_poll_time = prop_val / 10 - 1; 631 /* 632 * ev_poll_time is the raw value to be written on the register and 0 to 3 are the 633 * valid values. 634 */ 635 if (adp5585->ev_poll_time > 3) 636 return dev_err_probe(adp5585->dev, -EINVAL, 637 "Invalid value(%u) for poll-interval\n", prop_val); 638 } 639 640 ret = adp5585_unlock_ev_parse(adp5585); 641 if (ret) 642 return ret; 643 644 return adp5585_reset_ev_parse(adp5585); 645 } 646 647 static void adp5585_irq_disable(void *data) 648 { 649 struct adp5585_dev *adp5585 = data; 650 651 regmap_write(adp5585->regmap, adp5585->regs->int_en, 0); 652 } 653 654 static int adp5585_irq_enable(struct i2c_client *i2c, 655 struct adp5585_dev *adp5585) 656 { 657 const struct adp5585_regs *regs = adp5585->regs; 658 unsigned int stat; 659 int ret; 660 661 if (i2c->irq <= 0) 662 return 0; 663 664 ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL, adp5585_irq, 665 IRQF_ONESHOT, i2c->name, adp5585); 666 if (ret) 667 return ret; 668 669 /* 670 * Clear any possible outstanding interrupt before enabling them. We do that by reading 671 * the status register and writing back the same value. 672 */ 673 ret = regmap_read(adp5585->regmap, ADP5585_INT_STATUS, &stat); 674 if (ret) 675 return ret; 676 677 ret = regmap_write(adp5585->regmap, ADP5585_INT_STATUS, stat); 678 if (ret) 679 return ret; 680 681 ret = regmap_write(adp5585->regmap, regs->int_en, ADP5585_OVRFLOW_IEN | ADP5585_EVENT_IEN); 682 if (ret) 683 return ret; 684 685 return devm_add_action_or_reset(&i2c->dev, adp5585_irq_disable, adp5585); 686 } 687 688 static int adp5585_i2c_probe(struct i2c_client *i2c) 689 { 690 struct regmap_config *regmap_config; 691 struct adp5585_dev *adp5585; 692 struct gpio_desc *gpio; 693 unsigned int id; 694 int ret; 695 696 adp5585 = devm_kzalloc(&i2c->dev, sizeof(*adp5585), GFP_KERNEL); 697 if (!adp5585) 698 return -ENOMEM; 699 700 i2c_set_clientdata(i2c, adp5585); 701 adp5585->dev = &i2c->dev; 702 adp5585->irq = i2c->irq; 703 BLOCKING_INIT_NOTIFIER_HEAD(&adp5585->event_notifier); 704 705 adp5585->variant = (enum adp5585_variant)(uintptr_t)i2c_get_match_data(i2c); 706 if (!adp5585->variant) 707 return -ENODEV; 708 709 regmap_config = adp5585_fill_variant_config(adp5585); 710 if (IS_ERR(regmap_config)) 711 return PTR_ERR(regmap_config); 712 713 ret = devm_regulator_get_enable(&i2c->dev, "vdd"); 714 if (ret) 715 return ret; 716 717 gpio = devm_gpiod_get_optional(&i2c->dev, "reset", GPIOD_OUT_HIGH); 718 if (IS_ERR(gpio)) 719 return PTR_ERR(gpio); 720 721 /* 722 * Note the timings are not documented anywhere in the datasheet. They are just 723 * reasonable values that work. 724 */ 725 if (gpio) { 726 fsleep(30); 727 gpiod_set_value_cansleep(gpio, 0); 728 fsleep(60); 729 } 730 731 adp5585->regmap = devm_regmap_init_i2c(i2c, regmap_config); 732 if (IS_ERR(adp5585->regmap)) 733 return dev_err_probe(&i2c->dev, PTR_ERR(adp5585->regmap), 734 "Failed to initialize register map\n"); 735 736 ret = regmap_read(adp5585->regmap, ADP5585_ID, &id); 737 if (ret) 738 return dev_err_probe(&i2c->dev, ret, 739 "Failed to read device ID\n"); 740 741 id &= ADP5585_MAN_ID_MASK; 742 if (id != adp5585->id) 743 return dev_err_probe(&i2c->dev, -ENODEV, 744 "Invalid device ID 0x%02x\n", id); 745 746 adp5585->pin_usage = devm_bitmap_zalloc(&i2c->dev, adp5585->n_pins, GFP_KERNEL); 747 if (!adp5585->pin_usage) 748 return -ENOMEM; 749 750 ret = adp5585_parse_fw(adp5585); 751 if (ret) 752 return ret; 753 754 ret = adp5585_setup(adp5585); 755 if (ret) 756 return ret; 757 758 ret = adp5585_add_devices(adp5585); 759 if (ret) 760 return ret; 761 762 return adp5585_irq_enable(i2c, adp5585); 763 } 764 765 static int adp5585_suspend(struct device *dev) 766 { 767 struct adp5585_dev *adp5585 = dev_get_drvdata(dev); 768 769 if (adp5585->irq) 770 disable_irq(adp5585->irq); 771 772 regcache_cache_only(adp5585->regmap, true); 773 774 return 0; 775 } 776 777 static int adp5585_resume(struct device *dev) 778 { 779 struct adp5585_dev *adp5585 = dev_get_drvdata(dev); 780 int ret; 781 782 regcache_cache_only(adp5585->regmap, false); 783 regcache_mark_dirty(adp5585->regmap); 784 785 ret = regcache_sync(adp5585->regmap); 786 if (ret) 787 return ret; 788 789 if (adp5585->irq) 790 enable_irq(adp5585->irq); 791 792 return 0; 793 } 794 795 static DEFINE_SIMPLE_DEV_PM_OPS(adp5585_pm, adp5585_suspend, adp5585_resume); 796 797 static const struct of_device_id adp5585_of_match[] = { 798 { 799 .compatible = "adi,adp5585-00", 800 .data = (void *)ADP5585_00, 801 }, { 802 .compatible = "adi,adp5585-01", 803 .data = (void *)ADP5585_01, 804 }, { 805 .compatible = "adi,adp5585-02", 806 .data = (void *)ADP5585_02, 807 }, { 808 .compatible = "adi,adp5585-03", 809 .data = (void *)ADP5585_03, 810 }, { 811 .compatible = "adi,adp5585-04", 812 .data = (void *)ADP5585_04, 813 }, { 814 .compatible = "adi,adp5589-00", 815 .data = (void *)ADP5589_00, 816 }, { 817 .compatible = "adi,adp5589-01", 818 .data = (void *)ADP5589_01, 819 }, { 820 .compatible = "adi,adp5589-02", 821 .data = (void *)ADP5589_02, 822 }, { 823 .compatible = "adi,adp5589", 824 .data = (void *)ADP5589_00, 825 }, 826 { /* sentinel */ } 827 }; 828 MODULE_DEVICE_TABLE(of, adp5585_of_match); 829 830 static struct i2c_driver adp5585_i2c_driver = { 831 .driver = { 832 .name = "adp5585", 833 .of_match_table = adp5585_of_match, 834 .pm = pm_sleep_ptr(&adp5585_pm), 835 }, 836 .probe = adp5585_i2c_probe, 837 }; 838 module_i2c_driver(adp5585_i2c_driver); 839 840 MODULE_DESCRIPTION("ADP5585 core driver"); 841 MODULE_AUTHOR("Haibo Chen <haibo.chen@nxp.com>"); 842 MODULE_LICENSE("GPL"); 843