1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // max14577.c - mfd core driver for the Maxim 14577/77836 4 // 5 // Copyright (C) 2014 Samsung Electronics 6 // Chanwoo Choi <cw00.choi@samsung.com> 7 // Krzysztof Kozlowski <krzk@kernel.org> 8 // 9 // This driver is based on max8997.c 10 11 #include <linux/err.h> 12 #include <linux/i2c.h> 13 #include <linux/mod_devicetable.h> 14 #include <linux/module.h> 15 #include <linux/interrupt.h> 16 #include <linux/mfd/core.h> 17 #include <linux/mfd/max14577.h> 18 #include <linux/mfd/max14577-private.h> 19 20 /* 21 * Table of valid charger currents for different Maxim chipsets. 22 * It is placed here because it is used by both charger and regulator driver. 23 */ 24 const struct maxim_charger_current maxim_charger_currents[] = { 25 [MAXIM_DEVICE_TYPE_UNKNOWN] = { 0, 0, 0, 0 }, 26 [MAXIM_DEVICE_TYPE_MAX14577] = { 27 .min = MAX14577_CHARGER_CURRENT_LIMIT_MIN, 28 .high_start = MAX14577_CHARGER_CURRENT_LIMIT_HIGH_START, 29 .high_step = MAX14577_CHARGER_CURRENT_LIMIT_HIGH_STEP, 30 .max = MAX14577_CHARGER_CURRENT_LIMIT_MAX, 31 }, 32 [MAXIM_DEVICE_TYPE_MAX77836] = { 33 .min = MAX77836_CHARGER_CURRENT_LIMIT_MIN, 34 .high_start = MAX77836_CHARGER_CURRENT_LIMIT_HIGH_START, 35 .high_step = MAX77836_CHARGER_CURRENT_LIMIT_HIGH_STEP, 36 .max = MAX77836_CHARGER_CURRENT_LIMIT_MAX, 37 }, 38 }; 39 EXPORT_SYMBOL_GPL(maxim_charger_currents); 40 41 /* 42 * maxim_charger_calc_reg_current - Calculate register value for current 43 * @limits: constraints for charger, matching the MBCICHWRC register 44 * @min_ua: minimal requested current, micro Amps 45 * @max_ua: maximum requested current, micro Amps 46 * @dst: destination to store calculated register value 47 * 48 * Calculates the value of MBCICHWRC (Fast Battery Charge Current) register 49 * for given current and stores it under pointed 'dst'. The stored value 50 * combines low bit (MBCICHWRCL) and high bits (MBCICHWRCH). It is also 51 * properly shifted. 52 * 53 * The calculated register value matches the current which: 54 * - is always between <limits.min, limits.max>; 55 * - is always less or equal to max_ua; 56 * - is the highest possible value; 57 * - may be lower than min_ua. 58 * 59 * On success returns 0. On error returns -EINVAL (requested min/max current 60 * is outside of given charger limits) and 'dst' is not set. 61 */ 62 int maxim_charger_calc_reg_current(const struct maxim_charger_current *limits, 63 unsigned int min_ua, unsigned int max_ua, u8 *dst) 64 { 65 unsigned int current_bits; 66 67 if (min_ua > max_ua) 68 return -EINVAL; 69 70 if (min_ua > limits->max || max_ua < limits->min) 71 return -EINVAL; 72 73 if (max_ua < limits->high_start) { 74 /* 75 * Less than high_start, so set the minimal current 76 * (turn Low Bit off, 0 as high bits). 77 */ 78 *dst = 0x0; 79 return 0; 80 } 81 82 /* max_ua is in range: <high_start, infinite>, cut it to limits.max */ 83 max_ua = min(limits->max, max_ua); 84 max_ua -= limits->high_start; 85 /* 86 * There is no risk of overflow 'max_ua' here because: 87 * - max_ua >= limits.high_start 88 * - BUILD_BUG checks that 'limits' are: max >= high_start + high_step 89 */ 90 current_bits = max_ua / limits->high_step; 91 92 /* Turn Low Bit on (use range <limits.high_start, limits.max>) ... */ 93 *dst = 0x1 << CHGCTRL4_MBCICHWRCL_SHIFT; 94 /* and set proper High Bits */ 95 *dst |= current_bits << CHGCTRL4_MBCICHWRCH_SHIFT; 96 97 return 0; 98 } 99 EXPORT_SYMBOL_GPL(maxim_charger_calc_reg_current); 100 101 static const struct mfd_cell max14577_devs[] = { 102 { 103 .name = "max14577-muic", 104 .of_compatible = "maxim,max14577-muic", 105 }, 106 { 107 .name = "max14577-regulator", 108 .of_compatible = "maxim,max14577-regulator", 109 }, 110 { 111 .name = "max14577-charger", 112 .of_compatible = "maxim,max14577-charger", 113 }, 114 }; 115 116 static const struct mfd_cell max77836_devs[] = { 117 { 118 .name = "max77836-muic", 119 .of_compatible = "maxim,max77836-muic", 120 }, 121 { 122 .name = "max77836-regulator", 123 .of_compatible = "maxim,max77836-regulator", 124 }, 125 { 126 .name = "max77836-charger", 127 .of_compatible = "maxim,max77836-charger", 128 }, 129 { 130 .name = "max77836-battery", 131 .of_compatible = "maxim,max77836-battery", 132 }, 133 }; 134 135 static const struct of_device_id max14577_dt_match[] = { 136 { 137 .compatible = "maxim,max14577", 138 .data = (void *)MAXIM_DEVICE_TYPE_MAX14577, 139 }, 140 { 141 .compatible = "maxim,max77836", 142 .data = (void *)MAXIM_DEVICE_TYPE_MAX77836, 143 }, 144 {}, 145 }; 146 147 static bool max14577_muic_volatile_reg(struct device *dev, unsigned int reg) 148 { 149 switch (reg) { 150 case MAX14577_REG_INT1 ... MAX14577_REG_STATUS3: 151 return true; 152 default: 153 break; 154 } 155 return false; 156 } 157 158 static bool max77836_muic_volatile_reg(struct device *dev, unsigned int reg) 159 { 160 /* Any max14577 volatile registers are also max77836 volatile. */ 161 if (max14577_muic_volatile_reg(dev, reg)) 162 return true; 163 164 switch (reg) { 165 case MAX77836_FG_REG_VCELL_MSB ... MAX77836_FG_REG_SOC_LSB: 166 case MAX77836_FG_REG_CRATE_MSB ... MAX77836_FG_REG_CRATE_LSB: 167 case MAX77836_FG_REG_STATUS_H ... MAX77836_FG_REG_STATUS_L: 168 case MAX77836_PMIC_REG_INTSRC: 169 case MAX77836_PMIC_REG_TOPSYS_INT: 170 case MAX77836_PMIC_REG_TOPSYS_STAT: 171 return true; 172 default: 173 break; 174 } 175 return false; 176 } 177 178 static const struct regmap_config max14577_muic_regmap_config = { 179 .reg_bits = 8, 180 .val_bits = 8, 181 .volatile_reg = max14577_muic_volatile_reg, 182 .max_register = MAX14577_REG_END, 183 }; 184 185 static const struct regmap_config max77836_pmic_regmap_config = { 186 .reg_bits = 8, 187 .val_bits = 8, 188 .volatile_reg = max77836_muic_volatile_reg, 189 .max_register = MAX77836_PMIC_REG_END, 190 }; 191 192 static const struct regmap_irq max14577_irqs[] = { 193 /* INT1 interrupts */ 194 { .reg_offset = 0, .mask = MAX14577_INT1_ADC_MASK, }, 195 { .reg_offset = 0, .mask = MAX14577_INT1_ADCLOW_MASK, }, 196 { .reg_offset = 0, .mask = MAX14577_INT1_ADCERR_MASK, }, 197 /* INT2 interrupts */ 198 { .reg_offset = 1, .mask = MAX14577_INT2_CHGTYP_MASK, }, 199 { .reg_offset = 1, .mask = MAX14577_INT2_CHGDETRUN_MASK, }, 200 { .reg_offset = 1, .mask = MAX14577_INT2_DCDTMR_MASK, }, 201 { .reg_offset = 1, .mask = MAX14577_INT2_DBCHG_MASK, }, 202 { .reg_offset = 1, .mask = MAX14577_INT2_VBVOLT_MASK, }, 203 /* INT3 interrupts */ 204 { .reg_offset = 2, .mask = MAX14577_INT3_EOC_MASK, }, 205 { .reg_offset = 2, .mask = MAX14577_INT3_CGMBC_MASK, }, 206 { .reg_offset = 2, .mask = MAX14577_INT3_OVP_MASK, }, 207 { .reg_offset = 2, .mask = MAX14577_INT3_MBCCHGERR_MASK, }, 208 }; 209 210 static const struct regmap_irq_chip max14577_irq_chip = { 211 .name = "max14577", 212 .status_base = MAX14577_REG_INT1, 213 .unmask_base = MAX14577_REG_INTMASK1, 214 .num_regs = 3, 215 .irqs = max14577_irqs, 216 .num_irqs = ARRAY_SIZE(max14577_irqs), 217 }; 218 219 static const struct regmap_irq max77836_muic_irqs[] = { 220 /* INT1 interrupts */ 221 { .reg_offset = 0, .mask = MAX14577_INT1_ADC_MASK, }, 222 { .reg_offset = 0, .mask = MAX14577_INT1_ADCLOW_MASK, }, 223 { .reg_offset = 0, .mask = MAX14577_INT1_ADCERR_MASK, }, 224 { .reg_offset = 0, .mask = MAX77836_INT1_ADC1K_MASK, }, 225 /* INT2 interrupts */ 226 { .reg_offset = 1, .mask = MAX14577_INT2_CHGTYP_MASK, }, 227 { .reg_offset = 1, .mask = MAX14577_INT2_CHGDETRUN_MASK, }, 228 { .reg_offset = 1, .mask = MAX14577_INT2_DCDTMR_MASK, }, 229 { .reg_offset = 1, .mask = MAX14577_INT2_DBCHG_MASK, }, 230 { .reg_offset = 1, .mask = MAX14577_INT2_VBVOLT_MASK, }, 231 { .reg_offset = 1, .mask = MAX77836_INT2_VIDRM_MASK, }, 232 /* INT3 interrupts */ 233 { .reg_offset = 2, .mask = MAX14577_INT3_EOC_MASK, }, 234 { .reg_offset = 2, .mask = MAX14577_INT3_CGMBC_MASK, }, 235 { .reg_offset = 2, .mask = MAX14577_INT3_OVP_MASK, }, 236 { .reg_offset = 2, .mask = MAX14577_INT3_MBCCHGERR_MASK, }, 237 }; 238 239 static const struct regmap_irq_chip max77836_muic_irq_chip = { 240 .name = "max77836-muic", 241 .status_base = MAX14577_REG_INT1, 242 .unmask_base = MAX14577_REG_INTMASK1, 243 .num_regs = 3, 244 .irqs = max77836_muic_irqs, 245 .num_irqs = ARRAY_SIZE(max77836_muic_irqs), 246 }; 247 248 static const struct regmap_irq max77836_pmic_irqs[] = { 249 { .reg_offset = 0, .mask = MAX77836_TOPSYS_INT_T120C_MASK, }, 250 { .reg_offset = 0, .mask = MAX77836_TOPSYS_INT_T140C_MASK, }, 251 }; 252 253 static const struct regmap_irq_chip max77836_pmic_irq_chip = { 254 .name = "max77836-pmic", 255 .status_base = MAX77836_PMIC_REG_TOPSYS_INT, 256 .mask_base = MAX77836_PMIC_REG_TOPSYS_INT_MASK, 257 .num_regs = 1, 258 .irqs = max77836_pmic_irqs, 259 .num_irqs = ARRAY_SIZE(max77836_pmic_irqs), 260 }; 261 262 static void max14577_print_dev_type(struct max14577 *max14577) 263 { 264 u8 reg_data, vendor_id, device_id; 265 int ret; 266 267 ret = max14577_read_reg(max14577->regmap, MAX14577_REG_DEVICEID, 268 ®_data); 269 if (ret) { 270 dev_err(max14577->dev, 271 "Failed to read DEVICEID register: %d\n", ret); 272 return; 273 } 274 275 vendor_id = ((reg_data & DEVID_VENDORID_MASK) >> 276 DEVID_VENDORID_SHIFT); 277 device_id = ((reg_data & DEVID_DEVICEID_MASK) >> 278 DEVID_DEVICEID_SHIFT); 279 280 dev_info(max14577->dev, "Device type: %u (ID: 0x%x, vendor: 0x%x)\n", 281 max14577->dev_type, device_id, vendor_id); 282 } 283 284 /* 285 * Max77836 specific initialization code for driver probe. 286 * Adds new I2C dummy device, regmap and regmap IRQ chip. 287 * Unmasks Interrupt Source register. 288 * 289 * On success returns 0. 290 * On failure returns errno and reverts any changes done so far (e.g. remove 291 * I2C dummy device), except masking the INT SRC register. 292 */ 293 static int max77836_init(struct max14577 *max14577) 294 { 295 int ret; 296 u8 intsrc_mask; 297 298 max14577->i2c_pmic = i2c_new_dummy_device(max14577->i2c->adapter, 299 I2C_ADDR_PMIC); 300 if (IS_ERR(max14577->i2c_pmic)) { 301 dev_err(max14577->dev, "Failed to register PMIC I2C device\n"); 302 return PTR_ERR(max14577->i2c_pmic); 303 } 304 i2c_set_clientdata(max14577->i2c_pmic, max14577); 305 306 max14577->regmap_pmic = devm_regmap_init_i2c(max14577->i2c_pmic, 307 &max77836_pmic_regmap_config); 308 if (IS_ERR(max14577->regmap_pmic)) { 309 ret = PTR_ERR(max14577->regmap_pmic); 310 dev_err(max14577->dev, "Failed to allocate PMIC register map: %d\n", 311 ret); 312 goto err; 313 } 314 315 /* Un-mask MAX77836 Interrupt Source register */ 316 ret = max14577_read_reg(max14577->regmap_pmic, 317 MAX77836_PMIC_REG_INTSRC_MASK, &intsrc_mask); 318 if (ret < 0) { 319 dev_err(max14577->dev, "Failed to read PMIC register\n"); 320 goto err; 321 } 322 323 intsrc_mask &= ~(MAX77836_INTSRC_MASK_TOP_INT_MASK); 324 intsrc_mask &= ~(MAX77836_INTSRC_MASK_MUIC_CHG_INT_MASK); 325 ret = max14577_write_reg(max14577->regmap_pmic, 326 MAX77836_PMIC_REG_INTSRC_MASK, intsrc_mask); 327 if (ret < 0) { 328 dev_err(max14577->dev, "Failed to write PMIC register\n"); 329 goto err; 330 } 331 332 ret = regmap_add_irq_chip(max14577->regmap_pmic, max14577->irq, 333 IRQF_ONESHOT | IRQF_SHARED, 334 0, &max77836_pmic_irq_chip, 335 &max14577->irq_data_pmic); 336 if (ret != 0) { 337 dev_err(max14577->dev, "Failed to request PMIC IRQ %d: %d\n", 338 max14577->irq, ret); 339 goto err; 340 } 341 342 return 0; 343 344 err: 345 i2c_unregister_device(max14577->i2c_pmic); 346 347 return ret; 348 } 349 350 /* 351 * Max77836 specific de-initialization code for driver remove. 352 */ 353 static void max77836_remove(struct max14577 *max14577) 354 { 355 regmap_del_irq_chip(max14577->irq, max14577->irq_data_pmic); 356 i2c_unregister_device(max14577->i2c_pmic); 357 } 358 359 static int max14577_i2c_probe(struct i2c_client *i2c) 360 { 361 struct max14577 *max14577; 362 struct max14577_platform_data *pdata = dev_get_platdata(&i2c->dev); 363 struct device_node *np = i2c->dev.of_node; 364 int ret = 0; 365 const struct regmap_irq_chip *irq_chip; 366 const struct mfd_cell *mfd_devs; 367 unsigned int mfd_devs_size; 368 int irq_flags; 369 370 if (np) { 371 pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL); 372 if (!pdata) 373 return -ENOMEM; 374 i2c->dev.platform_data = pdata; 375 } 376 377 if (!pdata) { 378 dev_err(&i2c->dev, "No platform data found.\n"); 379 return -EINVAL; 380 } 381 382 max14577 = devm_kzalloc(&i2c->dev, sizeof(*max14577), GFP_KERNEL); 383 if (!max14577) 384 return -ENOMEM; 385 386 i2c_set_clientdata(i2c, max14577); 387 max14577->dev = &i2c->dev; 388 max14577->i2c = i2c; 389 max14577->irq = i2c->irq; 390 391 max14577->regmap = devm_regmap_init_i2c(i2c, 392 &max14577_muic_regmap_config); 393 if (IS_ERR(max14577->regmap)) { 394 ret = PTR_ERR(max14577->regmap); 395 dev_err(max14577->dev, "Failed to allocate register map: %d\n", 396 ret); 397 return ret; 398 } 399 400 max14577->dev_type = (enum maxim_device_type)i2c_get_match_data(i2c); 401 402 max14577_print_dev_type(max14577); 403 404 switch (max14577->dev_type) { 405 case MAXIM_DEVICE_TYPE_MAX77836: 406 irq_chip = &max77836_muic_irq_chip; 407 mfd_devs = max77836_devs; 408 mfd_devs_size = ARRAY_SIZE(max77836_devs); 409 irq_flags = IRQF_ONESHOT | IRQF_SHARED; 410 break; 411 case MAXIM_DEVICE_TYPE_MAX14577: 412 default: 413 irq_chip = &max14577_irq_chip; 414 mfd_devs = max14577_devs; 415 mfd_devs_size = ARRAY_SIZE(max14577_devs); 416 irq_flags = IRQF_ONESHOT; 417 break; 418 } 419 420 ret = regmap_add_irq_chip(max14577->regmap, max14577->irq, 421 irq_flags, 0, irq_chip, 422 &max14577->irq_data); 423 if (ret != 0) { 424 dev_err(&i2c->dev, "Failed to request IRQ %d: %d\n", 425 max14577->irq, ret); 426 return ret; 427 } 428 429 /* Max77836 specific initialization code (additional regmap) */ 430 if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836) { 431 ret = max77836_init(max14577); 432 if (ret < 0) 433 goto err_max77836; 434 } 435 436 ret = mfd_add_devices(max14577->dev, -1, mfd_devs, 437 mfd_devs_size, NULL, 0, NULL); 438 if (ret < 0) 439 goto err_mfd; 440 441 device_init_wakeup(max14577->dev, 1); 442 443 return 0; 444 445 err_mfd: 446 if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836) 447 max77836_remove(max14577); 448 err_max77836: 449 regmap_del_irq_chip(max14577->irq, max14577->irq_data); 450 451 return ret; 452 } 453 454 static void max14577_i2c_remove(struct i2c_client *i2c) 455 { 456 struct max14577 *max14577 = i2c_get_clientdata(i2c); 457 458 mfd_remove_devices(max14577->dev); 459 regmap_del_irq_chip(max14577->irq, max14577->irq_data); 460 if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836) 461 max77836_remove(max14577); 462 } 463 464 static const struct i2c_device_id max14577_i2c_id[] = { 465 { "max14577", MAXIM_DEVICE_TYPE_MAX14577, }, 466 { "max77836", MAXIM_DEVICE_TYPE_MAX77836, }, 467 { } 468 }; 469 MODULE_DEVICE_TABLE(i2c, max14577_i2c_id); 470 471 static int max14577_suspend(struct device *dev) 472 { 473 struct i2c_client *i2c = to_i2c_client(dev); 474 struct max14577 *max14577 = i2c_get_clientdata(i2c); 475 476 if (device_may_wakeup(dev)) 477 enable_irq_wake(max14577->irq); 478 /* 479 * MUIC IRQ must be disabled during suspend because if it happens 480 * while suspended it will be handled before resuming I2C. 481 * 482 * When device is woken up from suspend (e.g. by ADC change), 483 * an interrupt occurs before resuming I2C bus controller. 484 * Interrupt handler tries to read registers but this read 485 * will fail because I2C is still suspended. 486 */ 487 disable_irq(max14577->irq); 488 489 return 0; 490 } 491 492 static int max14577_resume(struct device *dev) 493 { 494 struct i2c_client *i2c = to_i2c_client(dev); 495 struct max14577 *max14577 = i2c_get_clientdata(i2c); 496 497 if (device_may_wakeup(dev)) 498 disable_irq_wake(max14577->irq); 499 enable_irq(max14577->irq); 500 501 return 0; 502 } 503 504 static DEFINE_SIMPLE_DEV_PM_OPS(max14577_pm, max14577_suspend, max14577_resume); 505 506 static struct i2c_driver max14577_i2c_driver = { 507 .driver = { 508 .name = "max14577", 509 .pm = pm_sleep_ptr(&max14577_pm), 510 .of_match_table = max14577_dt_match, 511 }, 512 .probe = max14577_i2c_probe, 513 .remove = max14577_i2c_remove, 514 .id_table = max14577_i2c_id, 515 }; 516 517 static int __init max14577_i2c_init(void) 518 { 519 BUILD_BUG_ON(ARRAY_SIZE(max14577_i2c_id) != MAXIM_DEVICE_TYPE_NUM); 520 BUILD_BUG_ON(ARRAY_SIZE(max14577_dt_match) != MAXIM_DEVICE_TYPE_NUM); 521 522 /* Valid charger current values must be provided for each chipset */ 523 BUILD_BUG_ON(ARRAY_SIZE(maxim_charger_currents) != MAXIM_DEVICE_TYPE_NUM); 524 525 /* Check for valid values for charger */ 526 BUILD_BUG_ON(MAX14577_CHARGER_CURRENT_LIMIT_HIGH_START + 527 MAX14577_CHARGER_CURRENT_LIMIT_HIGH_STEP * 0xf != 528 MAX14577_CHARGER_CURRENT_LIMIT_MAX); 529 BUILD_BUG_ON(MAX14577_CHARGER_CURRENT_LIMIT_HIGH_STEP == 0); 530 531 BUILD_BUG_ON(MAX77836_CHARGER_CURRENT_LIMIT_HIGH_START + 532 MAX77836_CHARGER_CURRENT_LIMIT_HIGH_STEP * 0xf != 533 MAX77836_CHARGER_CURRENT_LIMIT_MAX); 534 BUILD_BUG_ON(MAX77836_CHARGER_CURRENT_LIMIT_HIGH_STEP == 0); 535 536 return i2c_add_driver(&max14577_i2c_driver); 537 } 538 module_init(max14577_i2c_init); 539 540 static void __exit max14577_i2c_exit(void) 541 { 542 i2c_del_driver(&max14577_i2c_driver); 543 } 544 module_exit(max14577_i2c_exit); 545 546 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>, Krzysztof Kozlowski <krzk@kernel.org>"); 547 MODULE_DESCRIPTION("Maxim 14577/77836 multi-function core driver"); 548 MODULE_LICENSE("GPL"); 549