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 MODULE_DEVICE_TABLE(of, max14577_dt_match); 147 148 static bool max14577_muic_volatile_reg(struct device *dev, unsigned int reg) 149 { 150 switch (reg) { 151 case MAX14577_REG_INT1 ... MAX14577_REG_STATUS3: 152 return true; 153 default: 154 break; 155 } 156 return false; 157 } 158 159 static bool max77836_muic_volatile_reg(struct device *dev, unsigned int reg) 160 { 161 /* Any max14577 volatile registers are also max77836 volatile. */ 162 if (max14577_muic_volatile_reg(dev, reg)) 163 return true; 164 165 switch (reg) { 166 case MAX77836_FG_REG_VCELL_MSB ... MAX77836_FG_REG_SOC_LSB: 167 case MAX77836_FG_REG_CRATE_MSB ... MAX77836_FG_REG_CRATE_LSB: 168 case MAX77836_FG_REG_STATUS_H ... MAX77836_FG_REG_STATUS_L: 169 case MAX77836_PMIC_REG_INTSRC: 170 case MAX77836_PMIC_REG_TOPSYS_INT: 171 case MAX77836_PMIC_REG_TOPSYS_STAT: 172 return true; 173 default: 174 break; 175 } 176 return false; 177 } 178 179 static const struct regmap_config max14577_muic_regmap_config = { 180 .reg_bits = 8, 181 .val_bits = 8, 182 .volatile_reg = max14577_muic_volatile_reg, 183 .max_register = MAX14577_REG_END, 184 }; 185 186 static const struct regmap_config max77836_pmic_regmap_config = { 187 .reg_bits = 8, 188 .val_bits = 8, 189 .volatile_reg = max77836_muic_volatile_reg, 190 .max_register = MAX77836_PMIC_REG_END, 191 }; 192 193 static const struct regmap_irq max14577_irqs[] = { 194 /* INT1 interrupts */ 195 { .reg_offset = 0, .mask = MAX14577_INT1_ADC_MASK, }, 196 { .reg_offset = 0, .mask = MAX14577_INT1_ADCLOW_MASK, }, 197 { .reg_offset = 0, .mask = MAX14577_INT1_ADCERR_MASK, }, 198 /* INT2 interrupts */ 199 { .reg_offset = 1, .mask = MAX14577_INT2_CHGTYP_MASK, }, 200 { .reg_offset = 1, .mask = MAX14577_INT2_CHGDETRUN_MASK, }, 201 { .reg_offset = 1, .mask = MAX14577_INT2_DCDTMR_MASK, }, 202 { .reg_offset = 1, .mask = MAX14577_INT2_DBCHG_MASK, }, 203 { .reg_offset = 1, .mask = MAX14577_INT2_VBVOLT_MASK, }, 204 /* INT3 interrupts */ 205 { .reg_offset = 2, .mask = MAX14577_INT3_EOC_MASK, }, 206 { .reg_offset = 2, .mask = MAX14577_INT3_CGMBC_MASK, }, 207 { .reg_offset = 2, .mask = MAX14577_INT3_OVP_MASK, }, 208 { .reg_offset = 2, .mask = MAX14577_INT3_MBCCHGERR_MASK, }, 209 }; 210 211 static const struct regmap_irq_chip max14577_irq_chip = { 212 .name = "max14577", 213 .status_base = MAX14577_REG_INT1, 214 .unmask_base = MAX14577_REG_INTMASK1, 215 .num_regs = 3, 216 .irqs = max14577_irqs, 217 .num_irqs = ARRAY_SIZE(max14577_irqs), 218 }; 219 220 static const struct regmap_irq max77836_muic_irqs[] = { 221 /* INT1 interrupts */ 222 { .reg_offset = 0, .mask = MAX14577_INT1_ADC_MASK, }, 223 { .reg_offset = 0, .mask = MAX14577_INT1_ADCLOW_MASK, }, 224 { .reg_offset = 0, .mask = MAX14577_INT1_ADCERR_MASK, }, 225 { .reg_offset = 0, .mask = MAX77836_INT1_ADC1K_MASK, }, 226 /* INT2 interrupts */ 227 { .reg_offset = 1, .mask = MAX14577_INT2_CHGTYP_MASK, }, 228 { .reg_offset = 1, .mask = MAX14577_INT2_CHGDETRUN_MASK, }, 229 { .reg_offset = 1, .mask = MAX14577_INT2_DCDTMR_MASK, }, 230 { .reg_offset = 1, .mask = MAX14577_INT2_DBCHG_MASK, }, 231 { .reg_offset = 1, .mask = MAX14577_INT2_VBVOLT_MASK, }, 232 { .reg_offset = 1, .mask = MAX77836_INT2_VIDRM_MASK, }, 233 /* INT3 interrupts */ 234 { .reg_offset = 2, .mask = MAX14577_INT3_EOC_MASK, }, 235 { .reg_offset = 2, .mask = MAX14577_INT3_CGMBC_MASK, }, 236 { .reg_offset = 2, .mask = MAX14577_INT3_OVP_MASK, }, 237 { .reg_offset = 2, .mask = MAX14577_INT3_MBCCHGERR_MASK, }, 238 }; 239 240 static const struct regmap_irq_chip max77836_muic_irq_chip = { 241 .name = "max77836-muic", 242 .status_base = MAX14577_REG_INT1, 243 .unmask_base = MAX14577_REG_INTMASK1, 244 .num_regs = 3, 245 .irqs = max77836_muic_irqs, 246 .num_irqs = ARRAY_SIZE(max77836_muic_irqs), 247 }; 248 249 static const struct regmap_irq max77836_pmic_irqs[] = { 250 { .reg_offset = 0, .mask = MAX77836_TOPSYS_INT_T120C_MASK, }, 251 { .reg_offset = 0, .mask = MAX77836_TOPSYS_INT_T140C_MASK, }, 252 }; 253 254 static const struct regmap_irq_chip max77836_pmic_irq_chip = { 255 .name = "max77836-pmic", 256 .status_base = MAX77836_PMIC_REG_TOPSYS_INT, 257 .mask_base = MAX77836_PMIC_REG_TOPSYS_INT_MASK, 258 .num_regs = 1, 259 .irqs = max77836_pmic_irqs, 260 .num_irqs = ARRAY_SIZE(max77836_pmic_irqs), 261 }; 262 263 static void max14577_print_dev_type(struct max14577 *max14577) 264 { 265 u8 reg_data, vendor_id, device_id; 266 int ret; 267 268 ret = max14577_read_reg(max14577->regmap, MAX14577_REG_DEVICEID, 269 ®_data); 270 if (ret) { 271 dev_err(max14577->dev, 272 "Failed to read DEVICEID register: %d\n", ret); 273 return; 274 } 275 276 vendor_id = ((reg_data & DEVID_VENDORID_MASK) >> 277 DEVID_VENDORID_SHIFT); 278 device_id = ((reg_data & DEVID_DEVICEID_MASK) >> 279 DEVID_DEVICEID_SHIFT); 280 281 dev_info(max14577->dev, "Device type: %u (ID: 0x%x, vendor: 0x%x)\n", 282 max14577->dev_type, device_id, vendor_id); 283 } 284 285 /* 286 * Max77836 specific initialization code for driver probe. 287 * Adds new I2C dummy device, regmap and regmap IRQ chip. 288 * Unmasks Interrupt Source register. 289 * 290 * On success returns 0. 291 * On failure returns errno and reverts any changes done so far (e.g. remove 292 * I2C dummy device), except masking the INT SRC register. 293 */ 294 static int max77836_init(struct max14577 *max14577) 295 { 296 int ret; 297 u8 intsrc_mask; 298 299 max14577->i2c_pmic = i2c_new_dummy_device(max14577->i2c->adapter, 300 I2C_ADDR_PMIC); 301 if (IS_ERR(max14577->i2c_pmic)) { 302 dev_err(max14577->dev, "Failed to register PMIC I2C device\n"); 303 return PTR_ERR(max14577->i2c_pmic); 304 } 305 i2c_set_clientdata(max14577->i2c_pmic, max14577); 306 307 max14577->regmap_pmic = devm_regmap_init_i2c(max14577->i2c_pmic, 308 &max77836_pmic_regmap_config); 309 if (IS_ERR(max14577->regmap_pmic)) { 310 ret = PTR_ERR(max14577->regmap_pmic); 311 dev_err(max14577->dev, "Failed to allocate PMIC register map: %d\n", 312 ret); 313 goto err; 314 } 315 316 /* Un-mask MAX77836 Interrupt Source register */ 317 ret = max14577_read_reg(max14577->regmap_pmic, 318 MAX77836_PMIC_REG_INTSRC_MASK, &intsrc_mask); 319 if (ret < 0) { 320 dev_err(max14577->dev, "Failed to read PMIC register\n"); 321 goto err; 322 } 323 324 intsrc_mask &= ~(MAX77836_INTSRC_MASK_TOP_INT_MASK); 325 intsrc_mask &= ~(MAX77836_INTSRC_MASK_MUIC_CHG_INT_MASK); 326 ret = max14577_write_reg(max14577->regmap_pmic, 327 MAX77836_PMIC_REG_INTSRC_MASK, intsrc_mask); 328 if (ret < 0) { 329 dev_err(max14577->dev, "Failed to write PMIC register\n"); 330 goto err; 331 } 332 333 ret = regmap_add_irq_chip(max14577->regmap_pmic, max14577->irq, 334 IRQF_ONESHOT | IRQF_SHARED, 335 0, &max77836_pmic_irq_chip, 336 &max14577->irq_data_pmic); 337 if (ret != 0) { 338 dev_err(max14577->dev, "Failed to request PMIC IRQ %d: %d\n", 339 max14577->irq, ret); 340 goto err; 341 } 342 343 return 0; 344 345 err: 346 i2c_unregister_device(max14577->i2c_pmic); 347 348 return ret; 349 } 350 351 /* 352 * Max77836 specific de-initialization code for driver remove. 353 */ 354 static void max77836_remove(struct max14577 *max14577) 355 { 356 regmap_del_irq_chip(max14577->irq, max14577->irq_data_pmic); 357 i2c_unregister_device(max14577->i2c_pmic); 358 } 359 360 static int max14577_i2c_probe(struct i2c_client *i2c) 361 { 362 struct max14577 *max14577; 363 struct max14577_platform_data *pdata = dev_get_platdata(&i2c->dev); 364 struct device_node *np = i2c->dev.of_node; 365 int ret = 0; 366 const struct regmap_irq_chip *irq_chip; 367 const struct mfd_cell *mfd_devs; 368 unsigned int mfd_devs_size; 369 int irq_flags; 370 371 if (np) { 372 pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL); 373 if (!pdata) 374 return -ENOMEM; 375 i2c->dev.platform_data = pdata; 376 } 377 378 if (!pdata) { 379 dev_err(&i2c->dev, "No platform data found.\n"); 380 return -EINVAL; 381 } 382 383 max14577 = devm_kzalloc(&i2c->dev, sizeof(*max14577), GFP_KERNEL); 384 if (!max14577) 385 return -ENOMEM; 386 387 i2c_set_clientdata(i2c, max14577); 388 max14577->dev = &i2c->dev; 389 max14577->i2c = i2c; 390 max14577->irq = i2c->irq; 391 392 max14577->regmap = devm_regmap_init_i2c(i2c, 393 &max14577_muic_regmap_config); 394 if (IS_ERR(max14577->regmap)) { 395 ret = PTR_ERR(max14577->regmap); 396 dev_err(max14577->dev, "Failed to allocate register map: %d\n", 397 ret); 398 return ret; 399 } 400 401 max14577->dev_type = (kernel_ulong_t)i2c_get_match_data(i2c); 402 403 max14577_print_dev_type(max14577); 404 405 switch (max14577->dev_type) { 406 case MAXIM_DEVICE_TYPE_MAX77836: 407 irq_chip = &max77836_muic_irq_chip; 408 mfd_devs = max77836_devs; 409 mfd_devs_size = ARRAY_SIZE(max77836_devs); 410 irq_flags = IRQF_ONESHOT | IRQF_SHARED; 411 break; 412 case MAXIM_DEVICE_TYPE_MAX14577: 413 default: 414 irq_chip = &max14577_irq_chip; 415 mfd_devs = max14577_devs; 416 mfd_devs_size = ARRAY_SIZE(max14577_devs); 417 irq_flags = IRQF_ONESHOT; 418 break; 419 } 420 421 ret = regmap_add_irq_chip(max14577->regmap, max14577->irq, 422 irq_flags, 0, irq_chip, 423 &max14577->irq_data); 424 if (ret != 0) { 425 dev_err(&i2c->dev, "Failed to request IRQ %d: %d\n", 426 max14577->irq, ret); 427 return ret; 428 } 429 430 /* Max77836 specific initialization code (additional regmap) */ 431 if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836) { 432 ret = max77836_init(max14577); 433 if (ret < 0) 434 goto err_max77836; 435 } 436 437 ret = mfd_add_devices(max14577->dev, -1, mfd_devs, 438 mfd_devs_size, NULL, 0, NULL); 439 if (ret < 0) 440 goto err_mfd; 441 442 device_init_wakeup(max14577->dev, 1); 443 444 return 0; 445 446 err_mfd: 447 if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836) 448 max77836_remove(max14577); 449 err_max77836: 450 regmap_del_irq_chip(max14577->irq, max14577->irq_data); 451 452 return ret; 453 } 454 455 static void max14577_i2c_remove(struct i2c_client *i2c) 456 { 457 struct max14577 *max14577 = i2c_get_clientdata(i2c); 458 459 mfd_remove_devices(max14577->dev); 460 regmap_del_irq_chip(max14577->irq, max14577->irq_data); 461 if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836) 462 max77836_remove(max14577); 463 } 464 465 static const struct i2c_device_id max14577_i2c_id[] = { 466 { "max14577", MAXIM_DEVICE_TYPE_MAX14577, }, 467 { "max77836", MAXIM_DEVICE_TYPE_MAX77836, }, 468 { } 469 }; 470 MODULE_DEVICE_TABLE(i2c, max14577_i2c_id); 471 472 static int max14577_suspend(struct device *dev) 473 { 474 struct i2c_client *i2c = to_i2c_client(dev); 475 struct max14577 *max14577 = i2c_get_clientdata(i2c); 476 477 if (device_may_wakeup(dev)) 478 enable_irq_wake(max14577->irq); 479 /* 480 * MUIC IRQ must be disabled during suspend because if it happens 481 * while suspended it will be handled before resuming I2C. 482 * 483 * When device is woken up from suspend (e.g. by ADC change), 484 * an interrupt occurs before resuming I2C bus controller. 485 * Interrupt handler tries to read registers but this read 486 * will fail because I2C is still suspended. 487 */ 488 disable_irq(max14577->irq); 489 490 return 0; 491 } 492 493 static int max14577_resume(struct device *dev) 494 { 495 struct i2c_client *i2c = to_i2c_client(dev); 496 struct max14577 *max14577 = i2c_get_clientdata(i2c); 497 498 if (device_may_wakeup(dev)) 499 disable_irq_wake(max14577->irq); 500 enable_irq(max14577->irq); 501 502 return 0; 503 } 504 505 static DEFINE_SIMPLE_DEV_PM_OPS(max14577_pm, max14577_suspend, max14577_resume); 506 507 static struct i2c_driver max14577_i2c_driver = { 508 .driver = { 509 .name = "max14577", 510 .pm = pm_sleep_ptr(&max14577_pm), 511 .of_match_table = max14577_dt_match, 512 }, 513 .probe = max14577_i2c_probe, 514 .remove = max14577_i2c_remove, 515 .id_table = max14577_i2c_id, 516 }; 517 518 static int __init max14577_i2c_init(void) 519 { 520 BUILD_BUG_ON(ARRAY_SIZE(max14577_i2c_id) != MAXIM_DEVICE_TYPE_NUM); 521 BUILD_BUG_ON(ARRAY_SIZE(max14577_dt_match) != MAXIM_DEVICE_TYPE_NUM); 522 523 /* Valid charger current values must be provided for each chipset */ 524 BUILD_BUG_ON(ARRAY_SIZE(maxim_charger_currents) != MAXIM_DEVICE_TYPE_NUM); 525 526 /* Check for valid values for charger */ 527 BUILD_BUG_ON(MAX14577_CHARGER_CURRENT_LIMIT_HIGH_START + 528 MAX14577_CHARGER_CURRENT_LIMIT_HIGH_STEP * 0xf != 529 MAX14577_CHARGER_CURRENT_LIMIT_MAX); 530 BUILD_BUG_ON(MAX14577_CHARGER_CURRENT_LIMIT_HIGH_STEP == 0); 531 532 BUILD_BUG_ON(MAX77836_CHARGER_CURRENT_LIMIT_HIGH_START + 533 MAX77836_CHARGER_CURRENT_LIMIT_HIGH_STEP * 0xf != 534 MAX77836_CHARGER_CURRENT_LIMIT_MAX); 535 BUILD_BUG_ON(MAX77836_CHARGER_CURRENT_LIMIT_HIGH_STEP == 0); 536 537 return i2c_add_driver(&max14577_i2c_driver); 538 } 539 module_init(max14577_i2c_init); 540 541 static void __exit max14577_i2c_exit(void) 542 { 543 i2c_del_driver(&max14577_i2c_driver); 544 } 545 module_exit(max14577_i2c_exit); 546 547 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>, Krzysztof Kozlowski <krzk@kernel.org>"); 548 MODULE_DESCRIPTION("Maxim 14577/77836 multi-function core driver"); 549 MODULE_LICENSE("GPL"); 550