1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Battery charger driver for RT5033 4 * 5 * Copyright (C) 2014 Samsung Electronics, Co., Ltd. 6 * Author: Beomho Seo <beomho.seo@samsung.com> 7 */ 8 9 #include <linux/mod_devicetable.h> 10 #include <linux/module.h> 11 #include <linux/platform_device.h> 12 #include <linux/power_supply.h> 13 #include <linux/regmap.h> 14 #include <linux/mfd/rt5033-private.h> 15 16 struct rt5033_charger_data { 17 unsigned int pre_uamp; 18 unsigned int pre_uvolt; 19 unsigned int const_uvolt; 20 unsigned int eoc_uamp; 21 unsigned int fast_uamp; 22 }; 23 24 struct rt5033_charger { 25 struct device *dev; 26 struct regmap *regmap; 27 struct power_supply *psy; 28 struct rt5033_charger_data *chg; 29 }; 30 31 static int rt5033_get_charger_state(struct rt5033_charger *charger) 32 { 33 struct regmap *regmap = charger->regmap; 34 unsigned int reg_data; 35 int state; 36 37 if (!regmap) 38 return POWER_SUPPLY_STATUS_UNKNOWN; 39 40 regmap_read(regmap, RT5033_REG_CHG_STAT, ®_data); 41 42 switch (reg_data & RT5033_CHG_STAT_MASK) { 43 case RT5033_CHG_STAT_DISCHARGING: 44 state = POWER_SUPPLY_STATUS_DISCHARGING; 45 break; 46 case RT5033_CHG_STAT_CHARGING: 47 state = POWER_SUPPLY_STATUS_CHARGING; 48 break; 49 case RT5033_CHG_STAT_FULL: 50 state = POWER_SUPPLY_STATUS_FULL; 51 break; 52 case RT5033_CHG_STAT_NOT_CHARGING: 53 state = POWER_SUPPLY_STATUS_NOT_CHARGING; 54 break; 55 default: 56 state = POWER_SUPPLY_STATUS_UNKNOWN; 57 } 58 59 return state; 60 } 61 62 static int rt5033_get_charger_type(struct rt5033_charger *charger) 63 { 64 struct regmap *regmap = charger->regmap; 65 unsigned int reg_data; 66 int state; 67 68 regmap_read(regmap, RT5033_REG_CHG_STAT, ®_data); 69 70 switch (reg_data & RT5033_CHG_STAT_TYPE_MASK) { 71 case RT5033_CHG_STAT_TYPE_FAST: 72 state = POWER_SUPPLY_CHARGE_TYPE_FAST; 73 break; 74 case RT5033_CHG_STAT_TYPE_PRE: 75 state = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; 76 break; 77 default: 78 state = POWER_SUPPLY_CHARGE_TYPE_NONE; 79 } 80 81 return state; 82 } 83 84 static int rt5033_get_charger_current_limit(struct rt5033_charger *charger) 85 { 86 struct regmap *regmap = charger->regmap; 87 unsigned int state, reg_data, data; 88 89 regmap_read(regmap, RT5033_REG_CHG_CTRL5, ®_data); 90 91 state = (reg_data & RT5033_CHGCTRL5_ICHG_MASK) 92 >> RT5033_CHGCTRL5_ICHG_SHIFT; 93 94 data = RT5033_CHARGER_FAST_CURRENT_MIN + 95 RT5033_CHARGER_FAST_CURRENT_STEP_NUM * state; 96 97 return data; 98 } 99 100 static int rt5033_get_charger_const_voltage(struct rt5033_charger *charger) 101 { 102 struct regmap *regmap = charger->regmap; 103 unsigned int state, reg_data, data; 104 105 regmap_read(regmap, RT5033_REG_CHG_CTRL2, ®_data); 106 107 state = (reg_data & RT5033_CHGCTRL2_CV_MASK) 108 >> RT5033_CHGCTRL2_CV_SHIFT; 109 110 data = RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MIN + 111 RT5033_CHARGER_CONST_VOLTAGE_STEP_NUM * state; 112 113 return data; 114 } 115 116 static inline int rt5033_init_const_charge(struct rt5033_charger *charger) 117 { 118 struct rt5033_charger_data *chg = charger->chg; 119 int ret; 120 unsigned int val; 121 u8 reg_data; 122 123 /* Set constant voltage mode */ 124 if (chg->const_uvolt < RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MIN || 125 chg->const_uvolt > RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MAX) { 126 dev_err(charger->dev, 127 "Value 'constant-charge-voltage-max-microvolt' out of range\n"); 128 return -EINVAL; 129 } 130 131 if (chg->const_uvolt == RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MIN) 132 reg_data = 0x00; 133 else if (chg->const_uvolt == RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MAX) 134 reg_data = RT5033_CV_MAX_VOLTAGE; 135 else { 136 val = chg->const_uvolt; 137 val -= RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MIN; 138 val /= RT5033_CHARGER_CONST_VOLTAGE_STEP_NUM; 139 reg_data = val; 140 } 141 142 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL2, 143 RT5033_CHGCTRL2_CV_MASK, 144 reg_data << RT5033_CHGCTRL2_CV_SHIFT); 145 if (ret) { 146 dev_err(charger->dev, "Failed regmap update\n"); 147 return -EINVAL; 148 } 149 150 /* Set end of charge current */ 151 if (chg->eoc_uamp < RT5033_CHARGER_EOC_MIN || 152 chg->eoc_uamp > RT5033_CHARGER_EOC_MAX) { 153 dev_err(charger->dev, 154 "Value 'charge-term-current-microamp' out of range\n"); 155 return -EINVAL; 156 } 157 158 if (chg->eoc_uamp == RT5033_CHARGER_EOC_MIN) 159 reg_data = 0x01; 160 else if (chg->eoc_uamp == RT5033_CHARGER_EOC_MAX) 161 reg_data = 0x07; 162 else { 163 val = chg->eoc_uamp; 164 if (val < RT5033_CHARGER_EOC_REF) { 165 val -= RT5033_CHARGER_EOC_MIN; 166 val /= RT5033_CHARGER_EOC_STEP_NUM1; 167 reg_data = 0x01 + val; 168 } else if (val > RT5033_CHARGER_EOC_REF) { 169 val -= RT5033_CHARGER_EOC_REF; 170 val /= RT5033_CHARGER_EOC_STEP_NUM2; 171 reg_data = 0x04 + val; 172 } else { 173 reg_data = 0x04; 174 } 175 } 176 177 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL4, 178 RT5033_CHGCTRL4_EOC_MASK, reg_data); 179 if (ret) { 180 dev_err(charger->dev, "Failed regmap update\n"); 181 return -EINVAL; 182 } 183 184 return 0; 185 } 186 187 static inline int rt5033_init_fast_charge(struct rt5033_charger *charger) 188 { 189 struct rt5033_charger_data *chg = charger->chg; 190 int ret; 191 unsigned int val; 192 u8 reg_data; 193 194 /* Set limit input current */ 195 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL1, 196 RT5033_CHGCTRL1_IAICR_MASK, RT5033_AICR_2000_MODE); 197 if (ret) { 198 dev_err(charger->dev, "Failed regmap update\n"); 199 return -EINVAL; 200 } 201 202 /* Set fast-charge mode charging current */ 203 if (chg->fast_uamp < RT5033_CHARGER_FAST_CURRENT_MIN || 204 chg->fast_uamp > RT5033_CHARGER_FAST_CURRENT_MAX) { 205 dev_err(charger->dev, 206 "Value 'constant-charge-current-max-microamp' out of range\n"); 207 return -EINVAL; 208 } 209 210 if (chg->fast_uamp == RT5033_CHARGER_FAST_CURRENT_MIN) 211 reg_data = 0x00; 212 else if (chg->fast_uamp == RT5033_CHARGER_FAST_CURRENT_MAX) 213 reg_data = RT5033_CHG_MAX_CURRENT; 214 else { 215 val = chg->fast_uamp; 216 val -= RT5033_CHARGER_FAST_CURRENT_MIN; 217 val /= RT5033_CHARGER_FAST_CURRENT_STEP_NUM; 218 reg_data = val; 219 } 220 221 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL5, 222 RT5033_CHGCTRL5_ICHG_MASK, 223 reg_data << RT5033_CHGCTRL5_ICHG_SHIFT); 224 if (ret) { 225 dev_err(charger->dev, "Failed regmap update\n"); 226 return -EINVAL; 227 } 228 229 return 0; 230 } 231 232 static inline int rt5033_init_pre_charge(struct rt5033_charger *charger) 233 { 234 struct rt5033_charger_data *chg = charger->chg; 235 int ret; 236 unsigned int val; 237 u8 reg_data; 238 239 /* Set pre-charge threshold voltage */ 240 if (chg->pre_uvolt < RT5033_CHARGER_PRE_THRESHOLD_LIMIT_MIN || 241 chg->pre_uvolt > RT5033_CHARGER_PRE_THRESHOLD_LIMIT_MAX) { 242 dev_err(charger->dev, 243 "Value 'precharge-upper-limit-microvolt' out of range\n"); 244 return -EINVAL; 245 } 246 247 if (chg->pre_uvolt == RT5033_CHARGER_PRE_THRESHOLD_LIMIT_MIN) 248 reg_data = 0x00; 249 else if (chg->pre_uvolt == RT5033_CHARGER_PRE_THRESHOLD_LIMIT_MAX) 250 reg_data = 0x0f; 251 else { 252 val = chg->pre_uvolt; 253 val -= RT5033_CHARGER_PRE_THRESHOLD_LIMIT_MIN; 254 val /= RT5033_CHARGER_PRE_THRESHOLD_STEP_NUM; 255 reg_data = val; 256 } 257 258 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL5, 259 RT5033_CHGCTRL5_VPREC_MASK, reg_data); 260 if (ret) { 261 dev_err(charger->dev, "Failed regmap update\n"); 262 return -EINVAL; 263 } 264 265 /* Set pre-charge mode charging current */ 266 if (chg->pre_uamp < RT5033_CHARGER_PRE_CURRENT_LIMIT_MIN || 267 chg->pre_uamp > RT5033_CHARGER_PRE_CURRENT_LIMIT_MAX) { 268 dev_err(charger->dev, 269 "Value 'precharge-current-microamp' out of range\n"); 270 return -EINVAL; 271 } 272 273 if (chg->pre_uamp == RT5033_CHARGER_PRE_CURRENT_LIMIT_MIN) 274 reg_data = 0x00; 275 else if (chg->pre_uamp == RT5033_CHARGER_PRE_CURRENT_LIMIT_MAX) 276 reg_data = RT5033_CHG_MAX_PRE_CURRENT; 277 else { 278 val = chg->pre_uamp; 279 val -= RT5033_CHARGER_PRE_CURRENT_LIMIT_MIN; 280 val /= RT5033_CHARGER_PRE_CURRENT_STEP_NUM; 281 reg_data = val; 282 } 283 284 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL4, 285 RT5033_CHGCTRL4_IPREC_MASK, 286 reg_data << RT5033_CHGCTRL4_IPREC_SHIFT); 287 if (ret) { 288 dev_err(charger->dev, "Failed regmap update\n"); 289 return -EINVAL; 290 } 291 292 return 0; 293 } 294 295 static int rt5033_charger_reg_init(struct rt5033_charger *charger) 296 { 297 int ret = 0; 298 299 /* Enable charging termination */ 300 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL1, 301 RT5033_CHGCTRL1_TE_EN_MASK, RT5033_TE_ENABLE); 302 if (ret) { 303 dev_err(charger->dev, "Failed to enable charging termination.\n"); 304 return -EINVAL; 305 } 306 307 /* 308 * Disable minimum input voltage regulation (MIVR), this improves 309 * the charging performance. 310 */ 311 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL4, 312 RT5033_CHGCTRL4_MIVR_MASK, RT5033_CHARGER_MIVR_DISABLE); 313 if (ret) { 314 dev_err(charger->dev, "Failed to disable MIVR.\n"); 315 return -EINVAL; 316 } 317 318 ret = rt5033_init_pre_charge(charger); 319 if (ret) 320 return ret; 321 322 ret = rt5033_init_fast_charge(charger); 323 if (ret) 324 return ret; 325 326 ret = rt5033_init_const_charge(charger); 327 if (ret) 328 return ret; 329 330 return 0; 331 } 332 333 static enum power_supply_property rt5033_charger_props[] = { 334 POWER_SUPPLY_PROP_STATUS, 335 POWER_SUPPLY_PROP_CHARGE_TYPE, 336 POWER_SUPPLY_PROP_CURRENT_MAX, 337 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, 338 POWER_SUPPLY_PROP_MODEL_NAME, 339 POWER_SUPPLY_PROP_MANUFACTURER, 340 POWER_SUPPLY_PROP_ONLINE, 341 }; 342 343 static int rt5033_charger_get_property(struct power_supply *psy, 344 enum power_supply_property psp, 345 union power_supply_propval *val) 346 { 347 struct rt5033_charger *charger = power_supply_get_drvdata(psy); 348 349 switch (psp) { 350 case POWER_SUPPLY_PROP_STATUS: 351 val->intval = rt5033_get_charger_state(charger); 352 break; 353 case POWER_SUPPLY_PROP_CHARGE_TYPE: 354 val->intval = rt5033_get_charger_type(charger); 355 break; 356 case POWER_SUPPLY_PROP_CURRENT_MAX: 357 val->intval = rt5033_get_charger_current_limit(charger); 358 break; 359 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 360 val->intval = rt5033_get_charger_const_voltage(charger); 361 break; 362 case POWER_SUPPLY_PROP_MODEL_NAME: 363 val->strval = RT5033_CHARGER_MODEL; 364 break; 365 case POWER_SUPPLY_PROP_MANUFACTURER: 366 val->strval = RT5033_MANUFACTURER; 367 break; 368 case POWER_SUPPLY_PROP_ONLINE: 369 val->intval = (rt5033_get_charger_state(charger) == 370 POWER_SUPPLY_STATUS_CHARGING); 371 break; 372 default: 373 return -EINVAL; 374 } 375 376 return 0; 377 } 378 379 static struct rt5033_charger_data *rt5033_charger_dt_init( 380 struct rt5033_charger *charger) 381 { 382 struct rt5033_charger_data *chg; 383 struct power_supply_battery_info *info; 384 int ret; 385 386 chg = devm_kzalloc(charger->dev, sizeof(*chg), GFP_KERNEL); 387 if (!chg) 388 return ERR_PTR(-ENOMEM); 389 390 ret = power_supply_get_battery_info(charger->psy, &info); 391 if (ret) 392 return ERR_PTR(dev_err_probe(charger->dev, -EINVAL, 393 "missing battery info\n")); 394 395 /* Assign data. Validity will be checked in the init functions. */ 396 chg->pre_uamp = info->precharge_current_ua; 397 chg->fast_uamp = info->constant_charge_current_max_ua; 398 chg->eoc_uamp = info->charge_term_current_ua; 399 chg->pre_uvolt = info->precharge_voltage_max_uv; 400 chg->const_uvolt = info->constant_charge_voltage_max_uv; 401 402 return chg; 403 } 404 405 static const struct power_supply_desc rt5033_charger_desc = { 406 .name = "rt5033-charger", 407 .type = POWER_SUPPLY_TYPE_USB, 408 .properties = rt5033_charger_props, 409 .num_properties = ARRAY_SIZE(rt5033_charger_props), 410 .get_property = rt5033_charger_get_property, 411 }; 412 413 static int rt5033_charger_probe(struct platform_device *pdev) 414 { 415 struct rt5033_charger *charger; 416 struct power_supply_config psy_cfg = {}; 417 int ret; 418 419 charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL); 420 if (!charger) 421 return -ENOMEM; 422 423 platform_set_drvdata(pdev, charger); 424 charger->dev = &pdev->dev; 425 charger->regmap = dev_get_regmap(pdev->dev.parent, NULL); 426 427 psy_cfg.of_node = pdev->dev.of_node; 428 psy_cfg.drv_data = charger; 429 430 charger->psy = devm_power_supply_register(&pdev->dev, 431 &rt5033_charger_desc, 432 &psy_cfg); 433 if (IS_ERR(charger->psy)) 434 return dev_err_probe(&pdev->dev, PTR_ERR(charger->psy), 435 "Failed to register power supply\n"); 436 437 charger->chg = rt5033_charger_dt_init(charger); 438 if (IS_ERR_OR_NULL(charger->chg)) 439 return PTR_ERR(charger->chg); 440 441 ret = rt5033_charger_reg_init(charger); 442 if (ret) 443 return ret; 444 445 return 0; 446 } 447 448 static const struct platform_device_id rt5033_charger_id[] = { 449 { "rt5033-charger", }, 450 { } 451 }; 452 MODULE_DEVICE_TABLE(platform, rt5033_charger_id); 453 454 static const struct of_device_id rt5033_charger_of_match[] = { 455 { .compatible = "richtek,rt5033-charger", }, 456 { } 457 }; 458 MODULE_DEVICE_TABLE(of, rt5033_charger_of_match); 459 460 static struct platform_driver rt5033_charger_driver = { 461 .driver = { 462 .name = "rt5033-charger", 463 .of_match_table = rt5033_charger_of_match, 464 }, 465 .probe = rt5033_charger_probe, 466 .id_table = rt5033_charger_id, 467 }; 468 module_platform_driver(rt5033_charger_driver); 469 470 MODULE_DESCRIPTION("Richtek RT5033 charger driver"); 471 MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>"); 472 MODULE_LICENSE("GPL v2"); 473