1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Battery charger driver for Dialog Semiconductor DA9030 4 * 5 * Copyright (C) 2008 Compulab, Ltd. 6 * Mike Rapoport <mike@compulab.co.il> 7 */ 8 9 #include <linux/kernel.h> 10 #include <linux/slab.h> 11 #include <linux/init.h> 12 #include <linux/types.h> 13 #include <linux/device.h> 14 #include <linux/workqueue.h> 15 #include <linux/module.h> 16 #include <linux/platform_device.h> 17 #include <linux/power_supply.h> 18 #include <linux/mfd/da903x.h> 19 20 #include <linux/debugfs.h> 21 #include <linux/seq_file.h> 22 #include <linux/notifier.h> 23 24 #define DA9030_FAULT_LOG 0x0a 25 #define DA9030_FAULT_LOG_OVER_TEMP (1 << 7) 26 #define DA9030_FAULT_LOG_VBAT_OVER (1 << 4) 27 28 #define DA9030_CHARGE_CONTROL 0x28 29 #define DA9030_CHRG_CHARGER_ENABLE (1 << 7) 30 31 #define DA9030_ADC_MAN_CONTROL 0x30 32 #define DA9030_ADC_TBATREF_ENABLE (1 << 5) 33 #define DA9030_ADC_LDO_INT_ENABLE (1 << 4) 34 35 #define DA9030_ADC_AUTO_CONTROL 0x31 36 #define DA9030_ADC_TBAT_ENABLE (1 << 5) 37 #define DA9030_ADC_VBAT_IN_TXON (1 << 4) 38 #define DA9030_ADC_VCH_ENABLE (1 << 3) 39 #define DA9030_ADC_ICH_ENABLE (1 << 2) 40 #define DA9030_ADC_VBAT_ENABLE (1 << 1) 41 #define DA9030_ADC_AUTO_SLEEP_ENABLE (1 << 0) 42 43 #define DA9030_VBATMON 0x32 44 #define DA9030_VBATMONTXON 0x33 45 #define DA9030_TBATHIGHP 0x34 46 #define DA9030_TBATHIGHN 0x35 47 #define DA9030_TBATLOW 0x36 48 49 #define DA9030_VBAT_RES 0x41 50 #define DA9030_VBATMIN_RES 0x42 51 #define DA9030_VBATMINTXON_RES 0x43 52 #define DA9030_ICHMAX_RES 0x44 53 #define DA9030_ICHMIN_RES 0x45 54 #define DA9030_ICHAVERAGE_RES 0x46 55 #define DA9030_VCHMAX_RES 0x47 56 #define DA9030_VCHMIN_RES 0x48 57 #define DA9030_TBAT_RES 0x49 58 59 struct da9030_adc_res { 60 uint8_t vbat_res; 61 uint8_t vbatmin_res; 62 uint8_t vbatmintxon; 63 uint8_t ichmax_res; 64 uint8_t ichmin_res; 65 uint8_t ichaverage_res; 66 uint8_t vchmax_res; 67 uint8_t vchmin_res; 68 uint8_t tbat_res; 69 uint8_t adc_in4_res; 70 uint8_t adc_in5_res; 71 }; 72 73 struct da9030_battery_thresholds { 74 int tbat_low; 75 int tbat_high; 76 int tbat_restart; 77 78 int vbat_low; 79 int vbat_crit; 80 int vbat_charge_start; 81 int vbat_charge_stop; 82 int vbat_charge_restart; 83 84 int vcharge_min; 85 int vcharge_max; 86 }; 87 88 struct da9030_charger { 89 struct power_supply *psy; 90 struct power_supply_desc psy_desc; 91 92 struct device *master; 93 94 struct da9030_adc_res adc; 95 struct delayed_work work; 96 unsigned int interval; 97 98 struct power_supply_info *battery_info; 99 100 struct da9030_battery_thresholds thresholds; 101 102 unsigned int charge_milliamp; 103 unsigned int charge_millivolt; 104 105 /* charger status */ 106 bool chdet; 107 uint8_t fault; 108 int mA; 109 int mV; 110 bool is_on; 111 112 struct notifier_block nb; 113 114 /* platform callbacks for battery low and critical events */ 115 void (*battery_low)(void); 116 void (*battery_critical)(void); 117 118 struct dentry *debug_file; 119 }; 120 121 static inline int da9030_reg_to_mV(int reg) 122 { 123 return ((reg * 2650) >> 8) + 2650; 124 } 125 126 static inline int da9030_millivolt_to_reg(int mV) 127 { 128 return ((mV - 2650) << 8) / 2650; 129 } 130 131 static inline int da9030_reg_to_mA(int reg) 132 { 133 return ((reg * 24000) >> 8) / 15; 134 } 135 136 #ifdef CONFIG_DEBUG_FS 137 static int bat_debug_show(struct seq_file *s, void *data) 138 { 139 struct da9030_charger *charger = s->private; 140 141 seq_printf(s, "charger is %s\n", charger->is_on ? "on" : "off"); 142 if (charger->chdet) { 143 seq_printf(s, "iset = %dmA, vset = %dmV\n", 144 charger->mA, charger->mV); 145 } 146 147 seq_printf(s, "vbat_res = %d (%dmV)\n", 148 charger->adc.vbat_res, 149 da9030_reg_to_mV(charger->adc.vbat_res)); 150 seq_printf(s, "vbatmin_res = %d (%dmV)\n", 151 charger->adc.vbatmin_res, 152 da9030_reg_to_mV(charger->adc.vbatmin_res)); 153 seq_printf(s, "vbatmintxon = %d (%dmV)\n", 154 charger->adc.vbatmintxon, 155 da9030_reg_to_mV(charger->adc.vbatmintxon)); 156 seq_printf(s, "ichmax_res = %d (%dmA)\n", 157 charger->adc.ichmax_res, 158 da9030_reg_to_mV(charger->adc.ichmax_res)); 159 seq_printf(s, "ichmin_res = %d (%dmA)\n", 160 charger->adc.ichmin_res, 161 da9030_reg_to_mA(charger->adc.ichmin_res)); 162 seq_printf(s, "ichaverage_res = %d (%dmA)\n", 163 charger->adc.ichaverage_res, 164 da9030_reg_to_mA(charger->adc.ichaverage_res)); 165 seq_printf(s, "vchmax_res = %d (%dmV)\n", 166 charger->adc.vchmax_res, 167 da9030_reg_to_mA(charger->adc.vchmax_res)); 168 seq_printf(s, "vchmin_res = %d (%dmV)\n", 169 charger->adc.vchmin_res, 170 da9030_reg_to_mV(charger->adc.vchmin_res)); 171 172 return 0; 173 } 174 175 static int debug_open(struct inode *inode, struct file *file) 176 { 177 return single_open(file, bat_debug_show, inode->i_private); 178 } 179 180 static const struct file_operations bat_debug_fops = { 181 .open = debug_open, 182 .read = seq_read, 183 .llseek = seq_lseek, 184 .release = single_release, 185 }; 186 187 static struct dentry *da9030_bat_create_debugfs(struct da9030_charger *charger) 188 { 189 charger->debug_file = debugfs_create_file("charger", 0666, NULL, 190 charger, &bat_debug_fops); 191 return charger->debug_file; 192 } 193 194 static void da9030_bat_remove_debugfs(struct da9030_charger *charger) 195 { 196 debugfs_remove(charger->debug_file); 197 } 198 #else 199 static inline struct dentry *da9030_bat_create_debugfs(struct da9030_charger *charger) 200 { 201 return NULL; 202 } 203 static inline void da9030_bat_remove_debugfs(struct da9030_charger *charger) 204 { 205 } 206 #endif 207 208 static inline void da9030_read_adc(struct da9030_charger *charger, 209 struct da9030_adc_res *adc) 210 { 211 da903x_reads(charger->master, DA9030_VBAT_RES, 212 sizeof(*adc), (uint8_t *)adc); 213 } 214 215 static void da9030_charger_update_state(struct da9030_charger *charger) 216 { 217 uint8_t val; 218 219 da903x_read(charger->master, DA9030_CHARGE_CONTROL, &val); 220 charger->is_on = (val & DA9030_CHRG_CHARGER_ENABLE) ? 1 : 0; 221 charger->mA = ((val >> 3) & 0xf) * 100; 222 charger->mV = (val & 0x7) * 50 + 4000; 223 224 da9030_read_adc(charger, &charger->adc); 225 da903x_read(charger->master, DA9030_FAULT_LOG, &charger->fault); 226 charger->chdet = da903x_query_status(charger->master, 227 DA9030_STATUS_CHDET); 228 } 229 230 static void da9030_set_charge(struct da9030_charger *charger, int on) 231 { 232 uint8_t val; 233 234 if (on) { 235 val = DA9030_CHRG_CHARGER_ENABLE; 236 val |= (charger->charge_milliamp / 100) << 3; 237 val |= (charger->charge_millivolt - 4000) / 50; 238 charger->is_on = 1; 239 } else { 240 val = 0; 241 charger->is_on = 0; 242 } 243 244 da903x_write(charger->master, DA9030_CHARGE_CONTROL, val); 245 246 power_supply_changed(charger->psy); 247 } 248 249 static void da9030_charger_check_state(struct da9030_charger *charger) 250 { 251 da9030_charger_update_state(charger); 252 253 /* we wake or boot with external power on */ 254 if (!charger->is_on) { 255 if ((charger->chdet) && 256 (charger->adc.vbat_res < 257 charger->thresholds.vbat_charge_start)) { 258 da9030_set_charge(charger, 1); 259 } 260 } else { 261 /* Charger has been pulled out */ 262 if (!charger->chdet) { 263 da9030_set_charge(charger, 0); 264 return; 265 } 266 267 if (charger->adc.vbat_res >= 268 charger->thresholds.vbat_charge_stop) { 269 da9030_set_charge(charger, 0); 270 da903x_write(charger->master, DA9030_VBATMON, 271 charger->thresholds.vbat_charge_restart); 272 } else if (charger->adc.vbat_res > 273 charger->thresholds.vbat_low) { 274 /* we are charging and passed LOW_THRESH, 275 so upate DA9030 VBAT threshold 276 */ 277 da903x_write(charger->master, DA9030_VBATMON, 278 charger->thresholds.vbat_low); 279 } 280 if (charger->adc.vchmax_res > charger->thresholds.vcharge_max || 281 charger->adc.vchmin_res < charger->thresholds.vcharge_min || 282 /* Tempreture readings are negative */ 283 charger->adc.tbat_res < charger->thresholds.tbat_high || 284 charger->adc.tbat_res > charger->thresholds.tbat_low) { 285 /* disable charger */ 286 da9030_set_charge(charger, 0); 287 } 288 } 289 } 290 291 static void da9030_charging_monitor(struct work_struct *work) 292 { 293 struct da9030_charger *charger; 294 295 charger = container_of(work, struct da9030_charger, work.work); 296 297 da9030_charger_check_state(charger); 298 299 /* reschedule for the next time */ 300 schedule_delayed_work(&charger->work, charger->interval); 301 } 302 303 static enum power_supply_property da9030_battery_props[] = { 304 POWER_SUPPLY_PROP_MODEL_NAME, 305 POWER_SUPPLY_PROP_STATUS, 306 POWER_SUPPLY_PROP_HEALTH, 307 POWER_SUPPLY_PROP_TECHNOLOGY, 308 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 309 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 310 POWER_SUPPLY_PROP_VOLTAGE_NOW, 311 POWER_SUPPLY_PROP_CURRENT_AVG, 312 }; 313 314 static void da9030_battery_check_status(struct da9030_charger *charger, 315 union power_supply_propval *val) 316 { 317 if (charger->chdet) { 318 if (charger->is_on) 319 val->intval = POWER_SUPPLY_STATUS_CHARGING; 320 else 321 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; 322 } else { 323 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 324 } 325 } 326 327 static void da9030_battery_check_health(struct da9030_charger *charger, 328 union power_supply_propval *val) 329 { 330 if (charger->fault & DA9030_FAULT_LOG_OVER_TEMP) 331 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; 332 else if (charger->fault & DA9030_FAULT_LOG_VBAT_OVER) 333 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 334 else 335 val->intval = POWER_SUPPLY_HEALTH_GOOD; 336 } 337 338 static int da9030_battery_get_property(struct power_supply *psy, 339 enum power_supply_property psp, 340 union power_supply_propval *val) 341 { 342 struct da9030_charger *charger = power_supply_get_drvdata(psy); 343 344 switch (psp) { 345 case POWER_SUPPLY_PROP_STATUS: 346 da9030_battery_check_status(charger, val); 347 break; 348 case POWER_SUPPLY_PROP_HEALTH: 349 da9030_battery_check_health(charger, val); 350 break; 351 case POWER_SUPPLY_PROP_TECHNOLOGY: 352 val->intval = charger->battery_info->technology; 353 break; 354 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: 355 val->intval = charger->battery_info->voltage_max_design; 356 break; 357 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 358 val->intval = charger->battery_info->voltage_min_design; 359 break; 360 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 361 val->intval = da9030_reg_to_mV(charger->adc.vbat_res) * 1000; 362 break; 363 case POWER_SUPPLY_PROP_CURRENT_AVG: 364 val->intval = 365 da9030_reg_to_mA(charger->adc.ichaverage_res) * 1000; 366 break; 367 case POWER_SUPPLY_PROP_MODEL_NAME: 368 val->strval = charger->battery_info->name; 369 break; 370 default: 371 break; 372 } 373 374 return 0; 375 } 376 377 static void da9030_battery_vbat_event(struct da9030_charger *charger) 378 { 379 da9030_read_adc(charger, &charger->adc); 380 381 if (charger->is_on) 382 return; 383 384 if (charger->adc.vbat_res < charger->thresholds.vbat_low) { 385 /* set VBAT threshold for critical */ 386 da903x_write(charger->master, DA9030_VBATMON, 387 charger->thresholds.vbat_crit); 388 if (charger->battery_low) 389 charger->battery_low(); 390 } else if (charger->adc.vbat_res < 391 charger->thresholds.vbat_crit) { 392 /* notify the system of battery critical */ 393 if (charger->battery_critical) 394 charger->battery_critical(); 395 } 396 } 397 398 static int da9030_battery_event(struct notifier_block *nb, unsigned long event, 399 void *data) 400 { 401 struct da9030_charger *charger = 402 container_of(nb, struct da9030_charger, nb); 403 404 switch (event) { 405 case DA9030_EVENT_CHDET: 406 cancel_delayed_work_sync(&charger->work); 407 schedule_work(&charger->work.work); 408 break; 409 case DA9030_EVENT_VBATMON: 410 da9030_battery_vbat_event(charger); 411 break; 412 case DA9030_EVENT_CHIOVER: 413 case DA9030_EVENT_TBAT: 414 da9030_set_charge(charger, 0); 415 break; 416 } 417 418 return 0; 419 } 420 421 static void da9030_battery_convert_thresholds(struct da9030_charger *charger, 422 struct da9030_battery_info *pdata) 423 { 424 charger->thresholds.tbat_low = pdata->tbat_low; 425 charger->thresholds.tbat_high = pdata->tbat_high; 426 charger->thresholds.tbat_restart = pdata->tbat_restart; 427 428 charger->thresholds.vbat_low = 429 da9030_millivolt_to_reg(pdata->vbat_low); 430 charger->thresholds.vbat_crit = 431 da9030_millivolt_to_reg(pdata->vbat_crit); 432 charger->thresholds.vbat_charge_start = 433 da9030_millivolt_to_reg(pdata->vbat_charge_start); 434 charger->thresholds.vbat_charge_stop = 435 da9030_millivolt_to_reg(pdata->vbat_charge_stop); 436 charger->thresholds.vbat_charge_restart = 437 da9030_millivolt_to_reg(pdata->vbat_charge_restart); 438 439 charger->thresholds.vcharge_min = 440 da9030_millivolt_to_reg(pdata->vcharge_min); 441 charger->thresholds.vcharge_max = 442 da9030_millivolt_to_reg(pdata->vcharge_max); 443 } 444 445 static void da9030_battery_setup_psy(struct da9030_charger *charger) 446 { 447 struct power_supply_desc *psy_desc = &charger->psy_desc; 448 struct power_supply_info *info = charger->battery_info; 449 450 psy_desc->name = info->name; 451 psy_desc->use_for_apm = info->use_for_apm; 452 psy_desc->type = POWER_SUPPLY_TYPE_BATTERY; 453 psy_desc->get_property = da9030_battery_get_property; 454 455 psy_desc->properties = da9030_battery_props; 456 psy_desc->num_properties = ARRAY_SIZE(da9030_battery_props); 457 }; 458 459 static int da9030_battery_charger_init(struct da9030_charger *charger) 460 { 461 char v[5]; 462 int ret; 463 464 v[0] = v[1] = charger->thresholds.vbat_low; 465 v[2] = charger->thresholds.tbat_high; 466 v[3] = charger->thresholds.tbat_restart; 467 v[4] = charger->thresholds.tbat_low; 468 469 ret = da903x_writes(charger->master, DA9030_VBATMON, 5, v); 470 if (ret) 471 return ret; 472 473 /* 474 * Enable reference voltage supply for ADC from the LDO_INTERNAL 475 * regulator. Must be set before ADC measurements can be made. 476 */ 477 ret = da903x_write(charger->master, DA9030_ADC_MAN_CONTROL, 478 DA9030_ADC_LDO_INT_ENABLE | 479 DA9030_ADC_TBATREF_ENABLE); 480 if (ret) 481 return ret; 482 483 /* enable auto ADC measuremnts */ 484 return da903x_write(charger->master, DA9030_ADC_AUTO_CONTROL, 485 DA9030_ADC_TBAT_ENABLE | DA9030_ADC_VBAT_IN_TXON | 486 DA9030_ADC_VCH_ENABLE | DA9030_ADC_ICH_ENABLE | 487 DA9030_ADC_VBAT_ENABLE | 488 DA9030_ADC_AUTO_SLEEP_ENABLE); 489 } 490 491 static int da9030_battery_probe(struct platform_device *pdev) 492 { 493 struct da9030_charger *charger; 494 struct power_supply_config psy_cfg = {}; 495 struct da9030_battery_info *pdata = pdev->dev.platform_data; 496 int ret; 497 498 if (pdata == NULL) 499 return -EINVAL; 500 501 if (pdata->charge_milliamp >= 1500 || 502 pdata->charge_millivolt < 4000 || 503 pdata->charge_millivolt > 4350) 504 return -EINVAL; 505 506 charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL); 507 if (charger == NULL) 508 return -ENOMEM; 509 510 charger->master = pdev->dev.parent; 511 512 /* 10 seconds between monitor runs unless platform defines other 513 interval */ 514 charger->interval = msecs_to_jiffies( 515 (pdata->batmon_interval ? : 10) * 1000); 516 517 charger->charge_milliamp = pdata->charge_milliamp; 518 charger->charge_millivolt = pdata->charge_millivolt; 519 charger->battery_info = pdata->battery_info; 520 charger->battery_low = pdata->battery_low; 521 charger->battery_critical = pdata->battery_critical; 522 523 da9030_battery_convert_thresholds(charger, pdata); 524 525 ret = da9030_battery_charger_init(charger); 526 if (ret) 527 goto err_charger_init; 528 529 INIT_DELAYED_WORK(&charger->work, da9030_charging_monitor); 530 schedule_delayed_work(&charger->work, charger->interval); 531 532 charger->nb.notifier_call = da9030_battery_event; 533 ret = da903x_register_notifier(charger->master, &charger->nb, 534 DA9030_EVENT_CHDET | 535 DA9030_EVENT_VBATMON | 536 DA9030_EVENT_CHIOVER | 537 DA9030_EVENT_TBAT); 538 if (ret) 539 goto err_notifier; 540 541 da9030_battery_setup_psy(charger); 542 psy_cfg.drv_data = charger; 543 charger->psy = power_supply_register(&pdev->dev, &charger->psy_desc, 544 &psy_cfg); 545 if (IS_ERR(charger->psy)) { 546 ret = PTR_ERR(charger->psy); 547 goto err_ps_register; 548 } 549 550 charger->debug_file = da9030_bat_create_debugfs(charger); 551 platform_set_drvdata(pdev, charger); 552 return 0; 553 554 err_ps_register: 555 da903x_unregister_notifier(charger->master, &charger->nb, 556 DA9030_EVENT_CHDET | DA9030_EVENT_VBATMON | 557 DA9030_EVENT_CHIOVER | DA9030_EVENT_TBAT); 558 err_notifier: 559 cancel_delayed_work(&charger->work); 560 561 err_charger_init: 562 return ret; 563 } 564 565 static int da9030_battery_remove(struct platform_device *dev) 566 { 567 struct da9030_charger *charger = platform_get_drvdata(dev); 568 569 da9030_bat_remove_debugfs(charger); 570 571 da903x_unregister_notifier(charger->master, &charger->nb, 572 DA9030_EVENT_CHDET | DA9030_EVENT_VBATMON | 573 DA9030_EVENT_CHIOVER | DA9030_EVENT_TBAT); 574 cancel_delayed_work_sync(&charger->work); 575 da9030_set_charge(charger, 0); 576 power_supply_unregister(charger->psy); 577 578 return 0; 579 } 580 581 static struct platform_driver da903x_battery_driver = { 582 .driver = { 583 .name = "da903x-battery", 584 }, 585 .probe = da9030_battery_probe, 586 .remove = da9030_battery_remove, 587 }; 588 589 module_platform_driver(da903x_battery_driver); 590 591 MODULE_DESCRIPTION("DA9030 battery charger driver"); 592 MODULE_AUTHOR("Mike Rapoport, CompuLab"); 593 MODULE_LICENSE("GPL"); 594