1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) ST-Ericsson SA 2012 4 * Copyright (c) 2012 Sony Mobile Communications AB 5 * 6 * Charging algorithm driver for AB8500 7 * 8 * Authors: 9 * Johan Palsson <johan.palsson@stericsson.com> 10 * Karl Komierowski <karl.komierowski@stericsson.com> 11 * Arun R Murthy <arun.murthy@stericsson.com> 12 * Author: Imre Sunyi <imre.sunyi@sonymobile.com> 13 */ 14 15 #include <linux/init.h> 16 #include <linux/module.h> 17 #include <linux/device.h> 18 #include <linux/component.h> 19 #include <linux/hrtimer.h> 20 #include <linux/interrupt.h> 21 #include <linux/delay.h> 22 #include <linux/slab.h> 23 #include <linux/platform_device.h> 24 #include <linux/power_supply.h> 25 #include <linux/completion.h> 26 #include <linux/workqueue.h> 27 #include <linux/kobject.h> 28 #include <linux/of.h> 29 #include <linux/mfd/core.h> 30 #include <linux/mfd/abx500.h> 31 #include <linux/mfd/abx500/ab8500.h> 32 #include <linux/notifier.h> 33 34 #include "ab8500-bm.h" 35 #include "ab8500-chargalg.h" 36 37 /* Watchdog kick interval */ 38 #define CHG_WD_INTERVAL (6 * HZ) 39 40 /* End-of-charge criteria counter */ 41 #define EOC_COND_CNT 10 42 43 /* One hour expressed in seconds */ 44 #define ONE_HOUR_IN_SECONDS 3600 45 46 /* Five minutes expressed in seconds */ 47 #define FIVE_MINUTES_IN_SECONDS 300 48 49 /* 50 * This is the battery capacity limit that will trigger a new 51 * full charging cycle in the case where maintenance charging 52 * has been disabled 53 */ 54 #define AB8500_RECHARGE_CAP 95 55 56 enum ab8500_chargers { 57 NO_CHG, 58 AC_CHG, 59 USB_CHG, 60 }; 61 62 struct ab8500_chargalg_charger_info { 63 enum ab8500_chargers conn_chg; 64 enum ab8500_chargers prev_conn_chg; 65 enum ab8500_chargers online_chg; 66 enum ab8500_chargers prev_online_chg; 67 enum ab8500_chargers charger_type; 68 bool usb_chg_ok; 69 bool ac_chg_ok; 70 int usb_volt_uv; 71 int usb_curr_ua; 72 int ac_volt_uv; 73 int ac_curr_ua; 74 int usb_vset_uv; 75 int usb_iset_ua; 76 int ac_vset_uv; 77 int ac_iset_ua; 78 }; 79 80 struct ab8500_chargalg_battery_data { 81 int temp; 82 int volt_uv; 83 int avg_curr_ua; 84 int inst_curr_ua; 85 int percent; 86 }; 87 88 enum ab8500_chargalg_states { 89 STATE_HANDHELD_INIT, 90 STATE_HANDHELD, 91 STATE_CHG_NOT_OK_INIT, 92 STATE_CHG_NOT_OK, 93 STATE_HW_TEMP_PROTECT_INIT, 94 STATE_HW_TEMP_PROTECT, 95 STATE_NORMAL_INIT, 96 STATE_NORMAL, 97 STATE_WAIT_FOR_RECHARGE_INIT, 98 STATE_WAIT_FOR_RECHARGE, 99 STATE_MAINTENANCE_A_INIT, 100 STATE_MAINTENANCE_A, 101 STATE_MAINTENANCE_B_INIT, 102 STATE_MAINTENANCE_B, 103 STATE_TEMP_UNDEROVER_INIT, 104 STATE_TEMP_UNDEROVER, 105 STATE_TEMP_LOWHIGH_INIT, 106 STATE_TEMP_LOWHIGH, 107 STATE_OVV_PROTECT_INIT, 108 STATE_OVV_PROTECT, 109 STATE_SAFETY_TIMER_EXPIRED_INIT, 110 STATE_SAFETY_TIMER_EXPIRED, 111 STATE_BATT_REMOVED_INIT, 112 STATE_BATT_REMOVED, 113 STATE_WD_EXPIRED_INIT, 114 STATE_WD_EXPIRED, 115 }; 116 117 static const char * const states[] = { 118 "HANDHELD_INIT", 119 "HANDHELD", 120 "CHG_NOT_OK_INIT", 121 "CHG_NOT_OK", 122 "HW_TEMP_PROTECT_INIT", 123 "HW_TEMP_PROTECT", 124 "NORMAL_INIT", 125 "NORMAL", 126 "WAIT_FOR_RECHARGE_INIT", 127 "WAIT_FOR_RECHARGE", 128 "MAINTENANCE_A_INIT", 129 "MAINTENANCE_A", 130 "MAINTENANCE_B_INIT", 131 "MAINTENANCE_B", 132 "TEMP_UNDEROVER_INIT", 133 "TEMP_UNDEROVER", 134 "TEMP_LOWHIGH_INIT", 135 "TEMP_LOWHIGH", 136 "OVV_PROTECT_INIT", 137 "OVV_PROTECT", 138 "SAFETY_TIMER_EXPIRED_INIT", 139 "SAFETY_TIMER_EXPIRED", 140 "BATT_REMOVED_INIT", 141 "BATT_REMOVED", 142 "WD_EXPIRED_INIT", 143 "WD_EXPIRED", 144 }; 145 146 struct ab8500_chargalg_events { 147 bool batt_unknown; 148 bool mainextchnotok; 149 bool batt_ovv; 150 bool batt_rem; 151 bool btemp_underover; 152 bool btemp_low; 153 bool btemp_high; 154 bool main_thermal_prot; 155 bool usb_thermal_prot; 156 bool main_ovv; 157 bool vbus_ovv; 158 bool usbchargernotok; 159 bool safety_timer_expired; 160 bool maintenance_timer_expired; 161 bool ac_wd_expired; 162 bool usb_wd_expired; 163 bool ac_cv_active; 164 bool usb_cv_active; 165 bool vbus_collapsed; 166 }; 167 168 /** 169 * struct ab8500_charge_curr_maximization - Charger maximization parameters 170 * @original_iset_ua: the non optimized/maximised charger current 171 * @current_iset_ua: the charging current used at this moment 172 * @condition_cnt: number of iterations needed before a new charger current 173 is set 174 * @max_current_ua: maximum charger current 175 * @wait_cnt: to avoid too fast current step down in case of charger 176 * voltage collapse, we insert this delay between step 177 * down 178 * @level: tells in how many steps the charging current has been 179 increased 180 */ 181 struct ab8500_charge_curr_maximization { 182 int original_iset_ua; 183 int current_iset_ua; 184 int condition_cnt; 185 int max_current_ua; 186 int wait_cnt; 187 u8 level; 188 }; 189 190 enum maxim_ret { 191 MAXIM_RET_NOACTION, 192 MAXIM_RET_CHANGE, 193 MAXIM_RET_IBAT_TOO_HIGH, 194 }; 195 196 /** 197 * struct ab8500_chargalg - ab8500 Charging algorithm device information 198 * @dev: pointer to the structure device 199 * @charge_status: battery operating status 200 * @eoc_cnt: counter used to determine end-of_charge 201 * @maintenance_chg: indicate if maintenance charge is active 202 * @t_hyst_norm temperature hysteresis when the temperature has been 203 * over or under normal limits 204 * @t_hyst_lowhigh temperature hysteresis when the temperature has been 205 * over or under the high or low limits 206 * @charge_state: current state of the charging algorithm 207 * @ccm charging current maximization parameters 208 * @chg_info: information about connected charger types 209 * @batt_data: data of the battery 210 * @bm: Platform specific battery management information 211 * @parent: pointer to the struct ab8500 212 * @chargalg_psy: structure that holds the battery properties exposed by 213 * the charging algorithm 214 * @events: structure for information about events triggered 215 * @chargalg_wq: work queue for running the charging algorithm 216 * @chargalg_periodic_work: work to run the charging algorithm periodically 217 * @chargalg_wd_work: work to kick the charger watchdog periodically 218 * @chargalg_work: work to run the charging algorithm instantly 219 * @safety_timer: charging safety timer 220 * @maintenance_timer: maintenance charging timer 221 * @chargalg_kobject: structure of type kobject 222 */ 223 struct ab8500_chargalg { 224 struct device *dev; 225 int charge_status; 226 int eoc_cnt; 227 bool maintenance_chg; 228 int t_hyst_norm; 229 int t_hyst_lowhigh; 230 enum ab8500_chargalg_states charge_state; 231 struct ab8500_charge_curr_maximization ccm; 232 struct ab8500_chargalg_charger_info chg_info; 233 struct ab8500_chargalg_battery_data batt_data; 234 struct ab8500 *parent; 235 struct ab8500_bm_data *bm; 236 struct power_supply *chargalg_psy; 237 struct ux500_charger *ac_chg; 238 struct ux500_charger *usb_chg; 239 struct ab8500_chargalg_events events; 240 struct workqueue_struct *chargalg_wq; 241 struct delayed_work chargalg_periodic_work; 242 struct delayed_work chargalg_wd_work; 243 struct work_struct chargalg_work; 244 struct hrtimer safety_timer; 245 struct hrtimer maintenance_timer; 246 struct kobject chargalg_kobject; 247 }; 248 249 /* Main battery properties */ 250 static enum power_supply_property ab8500_chargalg_props[] = { 251 POWER_SUPPLY_PROP_STATUS, 252 POWER_SUPPLY_PROP_HEALTH, 253 }; 254 255 /** 256 * ab8500_chargalg_safety_timer_expired() - Expiration of the safety timer 257 * @timer: pointer to the hrtimer structure 258 * 259 * This function gets called when the safety timer for the charger 260 * expires 261 */ 262 static enum hrtimer_restart 263 ab8500_chargalg_safety_timer_expired(struct hrtimer *timer) 264 { 265 struct ab8500_chargalg *di = container_of(timer, struct ab8500_chargalg, 266 safety_timer); 267 dev_err(di->dev, "Safety timer expired\n"); 268 di->events.safety_timer_expired = true; 269 270 /* Trigger execution of the algorithm instantly */ 271 queue_work(di->chargalg_wq, &di->chargalg_work); 272 273 return HRTIMER_NORESTART; 274 } 275 276 /** 277 * ab8500_chargalg_maintenance_timer_expired() - Expiration of 278 * the maintenance timer 279 * @timer: pointer to the timer structure 280 * 281 * This function gets called when the maintenance timer 282 * expires 283 */ 284 static enum hrtimer_restart 285 ab8500_chargalg_maintenance_timer_expired(struct hrtimer *timer) 286 { 287 288 struct ab8500_chargalg *di = container_of(timer, struct ab8500_chargalg, 289 maintenance_timer); 290 291 dev_dbg(di->dev, "Maintenance timer expired\n"); 292 di->events.maintenance_timer_expired = true; 293 294 /* Trigger execution of the algorithm instantly */ 295 queue_work(di->chargalg_wq, &di->chargalg_work); 296 297 return HRTIMER_NORESTART; 298 } 299 300 /** 301 * ab8500_chargalg_state_to() - Change charge state 302 * @di: pointer to the ab8500_chargalg structure 303 * 304 * This function gets called when a charge state change should occur 305 */ 306 static void ab8500_chargalg_state_to(struct ab8500_chargalg *di, 307 enum ab8500_chargalg_states state) 308 { 309 dev_dbg(di->dev, 310 "State changed: %s (From state: [%d] %s =to=> [%d] %s )\n", 311 di->charge_state == state ? "NO" : "YES", 312 di->charge_state, 313 states[di->charge_state], 314 state, 315 states[state]); 316 317 di->charge_state = state; 318 } 319 320 static int ab8500_chargalg_check_charger_enable(struct ab8500_chargalg *di) 321 { 322 struct power_supply_battery_info *bi = di->bm->bi; 323 324 switch (di->charge_state) { 325 case STATE_NORMAL: 326 case STATE_MAINTENANCE_A: 327 case STATE_MAINTENANCE_B: 328 break; 329 default: 330 return 0; 331 } 332 333 if (di->chg_info.charger_type & USB_CHG) { 334 return di->usb_chg->ops.check_enable(di->usb_chg, 335 bi->constant_charge_voltage_max_uv, 336 bi->constant_charge_current_max_ua); 337 } else if (di->chg_info.charger_type & AC_CHG) { 338 return di->ac_chg->ops.check_enable(di->ac_chg, 339 bi->constant_charge_voltage_max_uv, 340 bi->constant_charge_current_max_ua); 341 } 342 return 0; 343 } 344 345 /** 346 * ab8500_chargalg_check_charger_connection() - Check charger connection change 347 * @di: pointer to the ab8500_chargalg structure 348 * 349 * This function will check if there is a change in the charger connection 350 * and change charge state accordingly. AC has precedence over USB. 351 */ 352 static int ab8500_chargalg_check_charger_connection(struct ab8500_chargalg *di) 353 { 354 if (di->chg_info.conn_chg != di->chg_info.prev_conn_chg) { 355 /* Charger state changed since last update */ 356 if (di->chg_info.conn_chg & AC_CHG) { 357 dev_info(di->dev, "Charging source is AC\n"); 358 if (di->chg_info.charger_type != AC_CHG) { 359 di->chg_info.charger_type = AC_CHG; 360 ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); 361 } 362 } else if (di->chg_info.conn_chg & USB_CHG) { 363 dev_info(di->dev, "Charging source is USB\n"); 364 di->chg_info.charger_type = USB_CHG; 365 ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); 366 } else { 367 dev_dbg(di->dev, "Charging source is OFF\n"); 368 di->chg_info.charger_type = NO_CHG; 369 ab8500_chargalg_state_to(di, STATE_HANDHELD_INIT); 370 } 371 di->chg_info.prev_conn_chg = di->chg_info.conn_chg; 372 } 373 return di->chg_info.conn_chg; 374 } 375 376 /** 377 * ab8500_chargalg_start_safety_timer() - Start charging safety timer 378 * @di: pointer to the ab8500_chargalg structure 379 * 380 * The safety timer is used to avoid overcharging of old or bad batteries. 381 * There are different timers for AC and USB 382 */ 383 static void ab8500_chargalg_start_safety_timer(struct ab8500_chargalg *di) 384 { 385 /* Charger-dependent expiration time in hours*/ 386 int timer_expiration = 0; 387 388 switch (di->chg_info.charger_type) { 389 case AC_CHG: 390 timer_expiration = di->bm->main_safety_tmr_h; 391 break; 392 393 case USB_CHG: 394 timer_expiration = di->bm->usb_safety_tmr_h; 395 break; 396 397 default: 398 dev_err(di->dev, "Unknown charger to charge from\n"); 399 break; 400 } 401 402 di->events.safety_timer_expired = false; 403 hrtimer_set_expires_range(&di->safety_timer, 404 ktime_set(timer_expiration * ONE_HOUR_IN_SECONDS, 0), 405 ktime_set(FIVE_MINUTES_IN_SECONDS, 0)); 406 hrtimer_start_expires(&di->safety_timer, HRTIMER_MODE_REL); 407 } 408 409 /** 410 * ab8500_chargalg_stop_safety_timer() - Stop charging safety timer 411 * @di: pointer to the ab8500_chargalg structure 412 * 413 * The safety timer is stopped whenever the NORMAL state is exited 414 */ 415 static void ab8500_chargalg_stop_safety_timer(struct ab8500_chargalg *di) 416 { 417 if (hrtimer_try_to_cancel(&di->safety_timer) >= 0) 418 di->events.safety_timer_expired = false; 419 } 420 421 /** 422 * ab8500_chargalg_start_maintenance_timer() - Start charging maintenance timer 423 * @di: pointer to the ab8500_chargalg structure 424 * @duration: duration of the maintenance timer in minutes 425 * 426 * The maintenance timer is used to maintain the charge in the battery once 427 * the battery is considered full. These timers are chosen to match the 428 * discharge curve of the battery 429 */ 430 static void ab8500_chargalg_start_maintenance_timer(struct ab8500_chargalg *di, 431 int duration) 432 { 433 /* Set a timer in minutes with a 30 second range */ 434 hrtimer_set_expires_range(&di->maintenance_timer, 435 ktime_set(duration * 60, 0), 436 ktime_set(30, 0)); 437 di->events.maintenance_timer_expired = false; 438 hrtimer_start_expires(&di->maintenance_timer, HRTIMER_MODE_REL); 439 } 440 441 /** 442 * ab8500_chargalg_stop_maintenance_timer() - Stop maintenance timer 443 * @di: pointer to the ab8500_chargalg structure 444 * 445 * The maintenance timer is stopped whenever maintenance ends or when another 446 * state is entered 447 */ 448 static void ab8500_chargalg_stop_maintenance_timer(struct ab8500_chargalg *di) 449 { 450 if (hrtimer_try_to_cancel(&di->maintenance_timer) >= 0) 451 di->events.maintenance_timer_expired = false; 452 } 453 454 /** 455 * ab8500_chargalg_kick_watchdog() - Kick charger watchdog 456 * @di: pointer to the ab8500_chargalg structure 457 * 458 * The charger watchdog have to be kicked periodically whenever the charger is 459 * on, else the ABB will reset the system 460 */ 461 static int ab8500_chargalg_kick_watchdog(struct ab8500_chargalg *di) 462 { 463 /* Check if charger exists and kick watchdog if charging */ 464 if (di->ac_chg && di->ac_chg->ops.kick_wd && 465 di->chg_info.online_chg & AC_CHG) { 466 return di->ac_chg->ops.kick_wd(di->ac_chg); 467 } else if (di->usb_chg && di->usb_chg->ops.kick_wd && 468 di->chg_info.online_chg & USB_CHG) 469 return di->usb_chg->ops.kick_wd(di->usb_chg); 470 471 return -ENXIO; 472 } 473 474 /** 475 * ab8500_chargalg_ac_en() - Turn on/off the AC charger 476 * @di: pointer to the ab8500_chargalg structure 477 * @enable: charger on/off 478 * @vset_uv: requested charger output voltage in microvolt 479 * @iset_ua: requested charger output current in microampere 480 * 481 * The AC charger will be turned on/off with the requested charge voltage and 482 * current 483 */ 484 static int ab8500_chargalg_ac_en(struct ab8500_chargalg *di, int enable, 485 int vset_uv, int iset_ua) 486 { 487 static int ab8500_chargalg_ex_ac_enable_toggle; 488 489 if (!di->ac_chg || !di->ac_chg->ops.enable) 490 return -ENXIO; 491 492 /* Select maximum of what both the charger and the battery supports */ 493 if (di->ac_chg->max_out_volt_uv) 494 vset_uv = min(vset_uv, di->ac_chg->max_out_volt_uv); 495 if (di->ac_chg->max_out_curr_ua) 496 iset_ua = min(iset_ua, di->ac_chg->max_out_curr_ua); 497 498 di->chg_info.ac_iset_ua = iset_ua; 499 di->chg_info.ac_vset_uv = vset_uv; 500 501 return di->ac_chg->ops.enable(di->ac_chg, enable, vset_uv, iset_ua); 502 } 503 504 /** 505 * ab8500_chargalg_usb_en() - Turn on/off the USB charger 506 * @di: pointer to the ab8500_chargalg structure 507 * @enable: charger on/off 508 * @vset_uv: requested charger output voltage in microvolt 509 * @iset_ua: requested charger output current in microampere 510 * 511 * The USB charger will be turned on/off with the requested charge voltage and 512 * current 513 */ 514 static int ab8500_chargalg_usb_en(struct ab8500_chargalg *di, int enable, 515 int vset_uv, int iset_ua) 516 { 517 if (!di->usb_chg || !di->usb_chg->ops.enable) 518 return -ENXIO; 519 520 /* Select maximum of what both the charger and the battery supports */ 521 if (di->usb_chg->max_out_volt_uv) 522 vset_uv = min(vset_uv, di->usb_chg->max_out_volt_uv); 523 if (di->usb_chg->max_out_curr_ua) 524 iset_ua = min(iset_ua, di->usb_chg->max_out_curr_ua); 525 526 di->chg_info.usb_iset_ua = iset_ua; 527 di->chg_info.usb_vset_uv = vset_uv; 528 529 return di->usb_chg->ops.enable(di->usb_chg, enable, vset_uv, iset_ua); 530 } 531 532 /** 533 * ab8500_chargalg_update_chg_curr() - Update charger current 534 * @di: pointer to the ab8500_chargalg structure 535 * @iset_ua: requested charger output current in microampere 536 * 537 * The charger output current will be updated for the charger 538 * that is currently in use 539 */ 540 static int ab8500_chargalg_update_chg_curr(struct ab8500_chargalg *di, 541 int iset_ua) 542 { 543 /* Check if charger exists and update current if charging */ 544 if (di->ac_chg && di->ac_chg->ops.update_curr && 545 di->chg_info.charger_type & AC_CHG) { 546 /* 547 * Select maximum of what both the charger 548 * and the battery supports 549 */ 550 if (di->ac_chg->max_out_curr_ua) 551 iset_ua = min(iset_ua, di->ac_chg->max_out_curr_ua); 552 553 di->chg_info.ac_iset_ua = iset_ua; 554 555 return di->ac_chg->ops.update_curr(di->ac_chg, iset_ua); 556 } else if (di->usb_chg && di->usb_chg->ops.update_curr && 557 di->chg_info.charger_type & USB_CHG) { 558 /* 559 * Select maximum of what both the charger 560 * and the battery supports 561 */ 562 if (di->usb_chg->max_out_curr_ua) 563 iset_ua = min(iset_ua, di->usb_chg->max_out_curr_ua); 564 565 di->chg_info.usb_iset_ua = iset_ua; 566 567 return di->usb_chg->ops.update_curr(di->usb_chg, iset_ua); 568 } 569 570 return -ENXIO; 571 } 572 573 /** 574 * ab8500_chargalg_stop_charging() - Stop charging 575 * @di: pointer to the ab8500_chargalg structure 576 * 577 * This function is called from any state where charging should be stopped. 578 * All charging is disabled and all status parameters and timers are changed 579 * accordingly 580 */ 581 static void ab8500_chargalg_stop_charging(struct ab8500_chargalg *di) 582 { 583 ab8500_chargalg_ac_en(di, false, 0, 0); 584 ab8500_chargalg_usb_en(di, false, 0, 0); 585 ab8500_chargalg_stop_safety_timer(di); 586 ab8500_chargalg_stop_maintenance_timer(di); 587 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING; 588 di->maintenance_chg = false; 589 cancel_delayed_work(&di->chargalg_wd_work); 590 power_supply_changed(di->chargalg_psy); 591 } 592 593 /** 594 * ab8500_chargalg_hold_charging() - Pauses charging 595 * @di: pointer to the ab8500_chargalg structure 596 * 597 * This function is called in the case where maintenance charging has been 598 * disabled and instead a battery voltage mode is entered to check when the 599 * battery voltage has reached a certain recharge voltage 600 */ 601 static void ab8500_chargalg_hold_charging(struct ab8500_chargalg *di) 602 { 603 ab8500_chargalg_ac_en(di, false, 0, 0); 604 ab8500_chargalg_usb_en(di, false, 0, 0); 605 ab8500_chargalg_stop_safety_timer(di); 606 ab8500_chargalg_stop_maintenance_timer(di); 607 di->charge_status = POWER_SUPPLY_STATUS_CHARGING; 608 di->maintenance_chg = false; 609 cancel_delayed_work(&di->chargalg_wd_work); 610 power_supply_changed(di->chargalg_psy); 611 } 612 613 /** 614 * ab8500_chargalg_start_charging() - Start the charger 615 * @di: pointer to the ab8500_chargalg structure 616 * @vset_uv: requested charger output voltage in microvolt 617 * @iset_ua: requested charger output current in microampere 618 * 619 * A charger will be enabled depending on the requested charger type that was 620 * detected previously. 621 */ 622 static void ab8500_chargalg_start_charging(struct ab8500_chargalg *di, 623 int vset_uv, int iset_ua) 624 { 625 switch (di->chg_info.charger_type) { 626 case AC_CHG: 627 dev_dbg(di->dev, 628 "AC parameters: Vset %d, Ich %d\n", vset_uv, iset_ua); 629 ab8500_chargalg_usb_en(di, false, 0, 0); 630 ab8500_chargalg_ac_en(di, true, vset_uv, iset_ua); 631 break; 632 633 case USB_CHG: 634 dev_dbg(di->dev, 635 "USB parameters: Vset %d, Ich %d\n", vset_uv, iset_ua); 636 ab8500_chargalg_ac_en(di, false, 0, 0); 637 ab8500_chargalg_usb_en(di, true, vset_uv, iset_ua); 638 break; 639 640 default: 641 dev_err(di->dev, "Unknown charger to charge from\n"); 642 break; 643 } 644 } 645 646 /** 647 * ab8500_chargalg_check_temp() - Check battery temperature ranges 648 * @di: pointer to the ab8500_chargalg structure 649 * 650 * The battery temperature is checked against the predefined limits and the 651 * charge state is changed accordingly 652 */ 653 static void ab8500_chargalg_check_temp(struct ab8500_chargalg *di) 654 { 655 struct power_supply_battery_info *bi = di->bm->bi; 656 657 if (di->batt_data.temp > (bi->temp_alert_min + di->t_hyst_norm) && 658 di->batt_data.temp < (bi->temp_alert_max - di->t_hyst_norm)) { 659 /* Temp OK! */ 660 di->events.btemp_underover = false; 661 di->events.btemp_low = false; 662 di->events.btemp_high = false; 663 di->t_hyst_norm = 0; 664 di->t_hyst_lowhigh = 0; 665 } else { 666 if ((di->batt_data.temp >= bi->temp_alert_max) && 667 (di->batt_data.temp < (bi->temp_max - di->t_hyst_lowhigh))) { 668 /* Alert zone for high temperature */ 669 di->events.btemp_underover = false; 670 di->events.btemp_high = true; 671 di->t_hyst_norm = di->bm->temp_hysteresis; 672 di->t_hyst_lowhigh = 0; 673 } else if ((di->batt_data.temp > (bi->temp_min + di->t_hyst_lowhigh)) && 674 (di->batt_data.temp <= bi->temp_alert_min)) { 675 /* Alert zone for low temperature */ 676 di->events.btemp_underover = false; 677 di->events.btemp_low = true; 678 di->t_hyst_norm = di->bm->temp_hysteresis; 679 di->t_hyst_lowhigh = 0; 680 } else if (di->batt_data.temp <= bi->temp_min || 681 di->batt_data.temp >= bi->temp_max) { 682 /* TEMP major!!!!! */ 683 di->events.btemp_underover = true; 684 di->events.btemp_low = false; 685 di->events.btemp_high = false; 686 di->t_hyst_norm = 0; 687 di->t_hyst_lowhigh = di->bm->temp_hysteresis; 688 } else { 689 /* Within hysteresis */ 690 dev_dbg(di->dev, "Within hysteresis limit temp: %d " 691 "hyst_lowhigh %d, hyst normal %d\n", 692 di->batt_data.temp, di->t_hyst_lowhigh, 693 di->t_hyst_norm); 694 } 695 } 696 } 697 698 /** 699 * ab8500_chargalg_check_charger_voltage() - Check charger voltage 700 * @di: pointer to the ab8500_chargalg structure 701 * 702 * Charger voltage is checked against maximum limit 703 */ 704 static void ab8500_chargalg_check_charger_voltage(struct ab8500_chargalg *di) 705 { 706 if (di->chg_info.usb_volt_uv > di->bm->chg_params->usb_volt_max_uv) 707 di->chg_info.usb_chg_ok = false; 708 else 709 di->chg_info.usb_chg_ok = true; 710 711 if (di->chg_info.ac_volt_uv > di->bm->chg_params->ac_volt_max_uv) 712 di->chg_info.ac_chg_ok = false; 713 else 714 di->chg_info.ac_chg_ok = true; 715 716 } 717 718 /** 719 * ab8500_chargalg_end_of_charge() - Check if end-of-charge criteria is fulfilled 720 * @di: pointer to the ab8500_chargalg structure 721 * 722 * End-of-charge criteria is fulfilled when the battery voltage is above a 723 * certain limit and the battery current is below a certain limit for a 724 * predefined number of consecutive seconds. If true, the battery is full 725 */ 726 static void ab8500_chargalg_end_of_charge(struct ab8500_chargalg *di) 727 { 728 if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING && 729 di->charge_state == STATE_NORMAL && 730 !di->maintenance_chg && (di->batt_data.volt_uv >= 731 di->bm->bi->voltage_max_design_uv || 732 di->events.usb_cv_active || di->events.ac_cv_active) && 733 di->batt_data.avg_curr_ua < 734 di->bm->bi->charge_term_current_ua && 735 di->batt_data.avg_curr_ua > 0) { 736 if (++di->eoc_cnt >= EOC_COND_CNT) { 737 di->eoc_cnt = 0; 738 di->charge_status = POWER_SUPPLY_STATUS_FULL; 739 di->maintenance_chg = true; 740 dev_dbg(di->dev, "EOC reached!\n"); 741 power_supply_changed(di->chargalg_psy); 742 } else { 743 dev_dbg(di->dev, 744 " EOC limit reached for the %d" 745 " time, out of %d before EOC\n", 746 di->eoc_cnt, 747 EOC_COND_CNT); 748 } 749 } else { 750 di->eoc_cnt = 0; 751 } 752 } 753 754 static void init_maxim_chg_curr(struct ab8500_chargalg *di) 755 { 756 struct power_supply_battery_info *bi = di->bm->bi; 757 758 di->ccm.original_iset_ua = bi->constant_charge_current_max_ua; 759 di->ccm.current_iset_ua = bi->constant_charge_current_max_ua; 760 di->ccm.max_current_ua = di->bm->maxi->chg_curr_ua; 761 di->ccm.condition_cnt = di->bm->maxi->wait_cycles; 762 di->ccm.level = 0; 763 } 764 765 /** 766 * ab8500_chargalg_chg_curr_maxim - increases the charger current to 767 * compensate for the system load 768 * @di pointer to the ab8500_chargalg structure 769 * 770 * This maximization function is used to raise the charger current to get the 771 * battery current as close to the optimal value as possible. The battery 772 * current during charging is affected by the system load 773 */ 774 static enum maxim_ret ab8500_chargalg_chg_curr_maxim(struct ab8500_chargalg *di) 775 { 776 777 if (!di->bm->maxi->ena_maxi) 778 return MAXIM_RET_NOACTION; 779 780 if (di->events.vbus_collapsed) { 781 dev_dbg(di->dev, "Charger voltage has collapsed %d\n", 782 di->ccm.wait_cnt); 783 if (di->ccm.wait_cnt == 0) { 784 dev_dbg(di->dev, "lowering current\n"); 785 di->ccm.wait_cnt++; 786 di->ccm.condition_cnt = di->bm->maxi->wait_cycles; 787 di->ccm.max_current_ua = di->ccm.current_iset_ua; 788 di->ccm.current_iset_ua = di->ccm.max_current_ua; 789 di->ccm.level--; 790 return MAXIM_RET_CHANGE; 791 } else { 792 dev_dbg(di->dev, "waiting\n"); 793 /* Let's go in here twice before lowering curr again */ 794 di->ccm.wait_cnt = (di->ccm.wait_cnt + 1) % 3; 795 return MAXIM_RET_NOACTION; 796 } 797 } 798 799 di->ccm.wait_cnt = 0; 800 801 if (di->batt_data.inst_curr_ua > di->ccm.original_iset_ua) { 802 dev_dbg(di->dev, " Maximization Ibat (%duA) too high" 803 " (limit %duA) (current iset: %duA)!\n", 804 di->batt_data.inst_curr_ua, di->ccm.original_iset_ua, 805 di->ccm.current_iset_ua); 806 807 if (di->ccm.current_iset_ua == di->ccm.original_iset_ua) 808 return MAXIM_RET_NOACTION; 809 810 di->ccm.condition_cnt = di->bm->maxi->wait_cycles; 811 di->ccm.current_iset_ua = di->ccm.original_iset_ua; 812 di->ccm.level = 0; 813 814 return MAXIM_RET_IBAT_TOO_HIGH; 815 } 816 817 di->ccm.condition_cnt = di->bm->maxi->wait_cycles; 818 return MAXIM_RET_NOACTION; 819 } 820 821 static void handle_maxim_chg_curr(struct ab8500_chargalg *di) 822 { 823 struct power_supply_battery_info *bi = di->bm->bi; 824 enum maxim_ret ret; 825 int result; 826 827 ret = ab8500_chargalg_chg_curr_maxim(di); 828 switch (ret) { 829 case MAXIM_RET_CHANGE: 830 result = ab8500_chargalg_update_chg_curr(di, 831 di->ccm.current_iset_ua); 832 if (result) 833 dev_err(di->dev, "failed to set chg curr\n"); 834 break; 835 case MAXIM_RET_IBAT_TOO_HIGH: 836 result = ab8500_chargalg_update_chg_curr(di, 837 bi->constant_charge_current_max_ua); 838 if (result) 839 dev_err(di->dev, "failed to set chg curr\n"); 840 break; 841 842 case MAXIM_RET_NOACTION: 843 default: 844 /* Do nothing..*/ 845 break; 846 } 847 } 848 849 static int ab8500_chargalg_get_ext_psy_data(struct device *dev, void *data) 850 { 851 struct power_supply *psy; 852 struct power_supply *ext = dev_get_drvdata(dev); 853 const char **supplicants = (const char **)ext->supplied_to; 854 struct ab8500_chargalg *di; 855 union power_supply_propval ret; 856 int j; 857 bool capacity_updated = false; 858 859 psy = (struct power_supply *)data; 860 di = power_supply_get_drvdata(psy); 861 /* For all psy where the driver name appears in any supplied_to */ 862 j = match_string(supplicants, ext->num_supplicants, psy->desc->name); 863 if (j < 0) 864 return 0; 865 866 /* 867 * If external is not registering 'POWER_SUPPLY_PROP_CAPACITY' to its 868 * property because of handling that sysfs entry on its own, this is 869 * the place to get the battery capacity. 870 */ 871 if (!power_supply_get_property(ext, POWER_SUPPLY_PROP_CAPACITY, &ret)) { 872 di->batt_data.percent = ret.intval; 873 capacity_updated = true; 874 } 875 876 /* Go through all properties for the psy */ 877 for (j = 0; j < ext->desc->num_properties; j++) { 878 enum power_supply_property prop; 879 prop = ext->desc->properties[j]; 880 881 /* 882 * Initialize chargers if not already done. 883 * The ab8500_charger*/ 884 if (!di->ac_chg && 885 ext->desc->type == POWER_SUPPLY_TYPE_MAINS) 886 di->ac_chg = psy_to_ux500_charger(ext); 887 else if (!di->usb_chg && 888 ext->desc->type == POWER_SUPPLY_TYPE_USB) 889 di->usb_chg = psy_to_ux500_charger(ext); 890 891 if (power_supply_get_property(ext, prop, &ret)) 892 continue; 893 switch (prop) { 894 case POWER_SUPPLY_PROP_PRESENT: 895 switch (ext->desc->type) { 896 case POWER_SUPPLY_TYPE_BATTERY: 897 /* Battery present */ 898 if (ret.intval) 899 di->events.batt_rem = false; 900 /* Battery removed */ 901 else 902 di->events.batt_rem = true; 903 break; 904 case POWER_SUPPLY_TYPE_MAINS: 905 /* AC disconnected */ 906 if (!ret.intval && 907 (di->chg_info.conn_chg & AC_CHG)) { 908 di->chg_info.prev_conn_chg = 909 di->chg_info.conn_chg; 910 di->chg_info.conn_chg &= ~AC_CHG; 911 } 912 /* AC connected */ 913 else if (ret.intval && 914 !(di->chg_info.conn_chg & AC_CHG)) { 915 di->chg_info.prev_conn_chg = 916 di->chg_info.conn_chg; 917 di->chg_info.conn_chg |= AC_CHG; 918 } 919 break; 920 case POWER_SUPPLY_TYPE_USB: 921 /* USB disconnected */ 922 if (!ret.intval && 923 (di->chg_info.conn_chg & USB_CHG)) { 924 di->chg_info.prev_conn_chg = 925 di->chg_info.conn_chg; 926 di->chg_info.conn_chg &= ~USB_CHG; 927 } 928 /* USB connected */ 929 else if (ret.intval && 930 !(di->chg_info.conn_chg & USB_CHG)) { 931 di->chg_info.prev_conn_chg = 932 di->chg_info.conn_chg; 933 di->chg_info.conn_chg |= USB_CHG; 934 } 935 break; 936 default: 937 break; 938 } 939 break; 940 941 case POWER_SUPPLY_PROP_ONLINE: 942 switch (ext->desc->type) { 943 case POWER_SUPPLY_TYPE_BATTERY: 944 break; 945 case POWER_SUPPLY_TYPE_MAINS: 946 /* AC offline */ 947 if (!ret.intval && 948 (di->chg_info.online_chg & AC_CHG)) { 949 di->chg_info.prev_online_chg = 950 di->chg_info.online_chg; 951 di->chg_info.online_chg &= ~AC_CHG; 952 } 953 /* AC online */ 954 else if (ret.intval && 955 !(di->chg_info.online_chg & AC_CHG)) { 956 di->chg_info.prev_online_chg = 957 di->chg_info.online_chg; 958 di->chg_info.online_chg |= AC_CHG; 959 queue_delayed_work(di->chargalg_wq, 960 &di->chargalg_wd_work, 0); 961 } 962 break; 963 case POWER_SUPPLY_TYPE_USB: 964 /* USB offline */ 965 if (!ret.intval && 966 (di->chg_info.online_chg & USB_CHG)) { 967 di->chg_info.prev_online_chg = 968 di->chg_info.online_chg; 969 di->chg_info.online_chg &= ~USB_CHG; 970 } 971 /* USB online */ 972 else if (ret.intval && 973 !(di->chg_info.online_chg & USB_CHG)) { 974 di->chg_info.prev_online_chg = 975 di->chg_info.online_chg; 976 di->chg_info.online_chg |= USB_CHG; 977 queue_delayed_work(di->chargalg_wq, 978 &di->chargalg_wd_work, 0); 979 } 980 break; 981 default: 982 break; 983 } 984 break; 985 986 case POWER_SUPPLY_PROP_HEALTH: 987 switch (ext->desc->type) { 988 case POWER_SUPPLY_TYPE_BATTERY: 989 break; 990 case POWER_SUPPLY_TYPE_MAINS: 991 switch (ret.intval) { 992 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE: 993 di->events.mainextchnotok = true; 994 di->events.main_thermal_prot = false; 995 di->events.main_ovv = false; 996 di->events.ac_wd_expired = false; 997 break; 998 case POWER_SUPPLY_HEALTH_DEAD: 999 di->events.ac_wd_expired = true; 1000 di->events.mainextchnotok = false; 1001 di->events.main_ovv = false; 1002 di->events.main_thermal_prot = false; 1003 break; 1004 case POWER_SUPPLY_HEALTH_COLD: 1005 case POWER_SUPPLY_HEALTH_OVERHEAT: 1006 di->events.main_thermal_prot = true; 1007 di->events.mainextchnotok = false; 1008 di->events.main_ovv = false; 1009 di->events.ac_wd_expired = false; 1010 break; 1011 case POWER_SUPPLY_HEALTH_OVERVOLTAGE: 1012 di->events.main_ovv = true; 1013 di->events.mainextchnotok = false; 1014 di->events.main_thermal_prot = false; 1015 di->events.ac_wd_expired = false; 1016 break; 1017 case POWER_SUPPLY_HEALTH_GOOD: 1018 di->events.main_thermal_prot = false; 1019 di->events.mainextchnotok = false; 1020 di->events.main_ovv = false; 1021 di->events.ac_wd_expired = false; 1022 break; 1023 default: 1024 break; 1025 } 1026 break; 1027 1028 case POWER_SUPPLY_TYPE_USB: 1029 switch (ret.intval) { 1030 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE: 1031 di->events.usbchargernotok = true; 1032 di->events.usb_thermal_prot = false; 1033 di->events.vbus_ovv = false; 1034 di->events.usb_wd_expired = false; 1035 break; 1036 case POWER_SUPPLY_HEALTH_DEAD: 1037 di->events.usb_wd_expired = true; 1038 di->events.usbchargernotok = false; 1039 di->events.usb_thermal_prot = false; 1040 di->events.vbus_ovv = false; 1041 break; 1042 case POWER_SUPPLY_HEALTH_COLD: 1043 case POWER_SUPPLY_HEALTH_OVERHEAT: 1044 di->events.usb_thermal_prot = true; 1045 di->events.usbchargernotok = false; 1046 di->events.vbus_ovv = false; 1047 di->events.usb_wd_expired = false; 1048 break; 1049 case POWER_SUPPLY_HEALTH_OVERVOLTAGE: 1050 di->events.vbus_ovv = true; 1051 di->events.usbchargernotok = false; 1052 di->events.usb_thermal_prot = false; 1053 di->events.usb_wd_expired = false; 1054 break; 1055 case POWER_SUPPLY_HEALTH_GOOD: 1056 di->events.usbchargernotok = false; 1057 di->events.usb_thermal_prot = false; 1058 di->events.vbus_ovv = false; 1059 di->events.usb_wd_expired = false; 1060 break; 1061 default: 1062 break; 1063 } 1064 break; 1065 default: 1066 break; 1067 } 1068 break; 1069 1070 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 1071 switch (ext->desc->type) { 1072 case POWER_SUPPLY_TYPE_BATTERY: 1073 di->batt_data.volt_uv = ret.intval; 1074 break; 1075 case POWER_SUPPLY_TYPE_MAINS: 1076 di->chg_info.ac_volt_uv = ret.intval; 1077 break; 1078 case POWER_SUPPLY_TYPE_USB: 1079 di->chg_info.usb_volt_uv = ret.intval; 1080 break; 1081 default: 1082 break; 1083 } 1084 break; 1085 1086 case POWER_SUPPLY_PROP_VOLTAGE_AVG: 1087 switch (ext->desc->type) { 1088 case POWER_SUPPLY_TYPE_MAINS: 1089 /* AVG is used to indicate when we are 1090 * in CV mode */ 1091 if (ret.intval) 1092 di->events.ac_cv_active = true; 1093 else 1094 di->events.ac_cv_active = false; 1095 1096 break; 1097 case POWER_SUPPLY_TYPE_USB: 1098 /* AVG is used to indicate when we are 1099 * in CV mode */ 1100 if (ret.intval) 1101 di->events.usb_cv_active = true; 1102 else 1103 di->events.usb_cv_active = false; 1104 1105 break; 1106 default: 1107 break; 1108 } 1109 break; 1110 1111 case POWER_SUPPLY_PROP_TECHNOLOGY: 1112 switch (ext->desc->type) { 1113 case POWER_SUPPLY_TYPE_BATTERY: 1114 if (ret.intval) 1115 di->events.batt_unknown = false; 1116 else 1117 di->events.batt_unknown = true; 1118 1119 break; 1120 default: 1121 break; 1122 } 1123 break; 1124 1125 case POWER_SUPPLY_PROP_TEMP: 1126 di->batt_data.temp = ret.intval / 10; 1127 break; 1128 1129 case POWER_SUPPLY_PROP_CURRENT_NOW: 1130 switch (ext->desc->type) { 1131 case POWER_SUPPLY_TYPE_MAINS: 1132 di->chg_info.ac_curr_ua = ret.intval; 1133 break; 1134 case POWER_SUPPLY_TYPE_USB: 1135 di->chg_info.usb_curr_ua = ret.intval; 1136 break; 1137 case POWER_SUPPLY_TYPE_BATTERY: 1138 di->batt_data.inst_curr_ua = ret.intval; 1139 break; 1140 default: 1141 break; 1142 } 1143 break; 1144 1145 case POWER_SUPPLY_PROP_CURRENT_AVG: 1146 switch (ext->desc->type) { 1147 case POWER_SUPPLY_TYPE_BATTERY: 1148 di->batt_data.avg_curr_ua = ret.intval; 1149 break; 1150 case POWER_SUPPLY_TYPE_USB: 1151 if (ret.intval) 1152 di->events.vbus_collapsed = true; 1153 else 1154 di->events.vbus_collapsed = false; 1155 break; 1156 default: 1157 break; 1158 } 1159 break; 1160 case POWER_SUPPLY_PROP_CAPACITY: 1161 if (!capacity_updated) 1162 di->batt_data.percent = ret.intval; 1163 break; 1164 default: 1165 break; 1166 } 1167 } 1168 return 0; 1169 } 1170 1171 /** 1172 * ab8500_chargalg_external_power_changed() - callback for power supply changes 1173 * @psy: pointer to the structure power_supply 1174 * 1175 * This function is the entry point of the pointer external_power_changed 1176 * of the structure power_supply. 1177 * This function gets executed when there is a change in any external power 1178 * supply that this driver needs to be notified of. 1179 */ 1180 static void ab8500_chargalg_external_power_changed(struct power_supply *psy) 1181 { 1182 struct ab8500_chargalg *di = power_supply_get_drvdata(psy); 1183 1184 /* 1185 * Trigger execution of the algorithm instantly and read 1186 * all power_supply properties there instead 1187 */ 1188 if (di->chargalg_wq) 1189 queue_work(di->chargalg_wq, &di->chargalg_work); 1190 } 1191 1192 /** 1193 * ab8500_chargalg_time_to_restart() - time to restart CC/CV charging? 1194 * @di: charging algorithm state 1195 * 1196 * This checks if the voltage or capacity of the battery has fallen so 1197 * low that we need to restart the CC/CV charge cycle. 1198 */ 1199 static bool ab8500_chargalg_time_to_restart(struct ab8500_chargalg *di) 1200 { 1201 struct power_supply_battery_info *bi = di->bm->bi; 1202 1203 /* Sanity check - these need to have some reasonable values */ 1204 if (!di->batt_data.volt_uv || !di->batt_data.percent) 1205 return false; 1206 1207 /* Some batteries tell us at which voltage we should restart charging */ 1208 if (bi->charge_restart_voltage_uv > 0) { 1209 if (di->batt_data.volt_uv <= bi->charge_restart_voltage_uv) 1210 return true; 1211 /* Else we restart as we reach a certain capacity */ 1212 } else { 1213 if (di->batt_data.percent <= AB8500_RECHARGE_CAP) 1214 return true; 1215 } 1216 1217 return false; 1218 } 1219 1220 /** 1221 * ab8500_chargalg_algorithm() - Main function for the algorithm 1222 * @di: pointer to the ab8500_chargalg structure 1223 * 1224 * This is the main control function for the charging algorithm. 1225 * It is called periodically or when something happens that will 1226 * trigger a state change 1227 */ 1228 static void ab8500_chargalg_algorithm(struct ab8500_chargalg *di) 1229 { 1230 struct power_supply_battery_info *bi = di->bm->bi; 1231 struct power_supply_maintenance_charge_table *mt; 1232 int charger_status; 1233 int ret; 1234 1235 /* Collect data from all power_supply class devices */ 1236 class_for_each_device(power_supply_class, NULL, 1237 di->chargalg_psy, ab8500_chargalg_get_ext_psy_data); 1238 1239 ab8500_chargalg_end_of_charge(di); 1240 ab8500_chargalg_check_temp(di); 1241 ab8500_chargalg_check_charger_voltage(di); 1242 1243 charger_status = ab8500_chargalg_check_charger_connection(di); 1244 1245 if (is_ab8500(di->parent)) { 1246 ret = ab8500_chargalg_check_charger_enable(di); 1247 if (ret < 0) 1248 dev_err(di->dev, "Checking charger is enabled error" 1249 ": Returned Value %d\n", ret); 1250 } 1251 1252 /* 1253 * First check if we have a charger connected. 1254 * Also we don't allow charging of unknown batteries if configured 1255 * this way 1256 */ 1257 if (!charger_status || 1258 (di->events.batt_unknown && !di->bm->chg_unknown_bat)) { 1259 if (di->charge_state != STATE_HANDHELD) { 1260 di->events.safety_timer_expired = false; 1261 ab8500_chargalg_state_to(di, STATE_HANDHELD_INIT); 1262 } 1263 } 1264 1265 /* Safety timer expiration */ 1266 else if (di->events.safety_timer_expired) { 1267 if (di->charge_state != STATE_SAFETY_TIMER_EXPIRED) 1268 ab8500_chargalg_state_to(di, 1269 STATE_SAFETY_TIMER_EXPIRED_INIT); 1270 } 1271 /* 1272 * Check if any interrupts has occurred 1273 * that will prevent us from charging 1274 */ 1275 1276 /* Battery removed */ 1277 else if (di->events.batt_rem) { 1278 if (di->charge_state != STATE_BATT_REMOVED) 1279 ab8500_chargalg_state_to(di, STATE_BATT_REMOVED_INIT); 1280 } 1281 /* Main or USB charger not ok. */ 1282 else if (di->events.mainextchnotok || di->events.usbchargernotok) { 1283 /* 1284 * If vbus_collapsed is set, we have to lower the charger 1285 * current, which is done in the normal state below 1286 */ 1287 if (di->charge_state != STATE_CHG_NOT_OK && 1288 !di->events.vbus_collapsed) 1289 ab8500_chargalg_state_to(di, STATE_CHG_NOT_OK_INIT); 1290 } 1291 /* VBUS, Main or VBAT OVV. */ 1292 else if (di->events.vbus_ovv || 1293 di->events.main_ovv || 1294 di->events.batt_ovv || 1295 !di->chg_info.usb_chg_ok || 1296 !di->chg_info.ac_chg_ok) { 1297 if (di->charge_state != STATE_OVV_PROTECT) 1298 ab8500_chargalg_state_to(di, STATE_OVV_PROTECT_INIT); 1299 } 1300 /* USB Thermal, stop charging */ 1301 else if (di->events.main_thermal_prot || 1302 di->events.usb_thermal_prot) { 1303 if (di->charge_state != STATE_HW_TEMP_PROTECT) 1304 ab8500_chargalg_state_to(di, 1305 STATE_HW_TEMP_PROTECT_INIT); 1306 } 1307 /* Battery temp over/under */ 1308 else if (di->events.btemp_underover) { 1309 if (di->charge_state != STATE_TEMP_UNDEROVER) 1310 ab8500_chargalg_state_to(di, 1311 STATE_TEMP_UNDEROVER_INIT); 1312 } 1313 /* Watchdog expired */ 1314 else if (di->events.ac_wd_expired || 1315 di->events.usb_wd_expired) { 1316 if (di->charge_state != STATE_WD_EXPIRED) 1317 ab8500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT); 1318 } 1319 /* Battery temp high/low */ 1320 else if (di->events.btemp_low || di->events.btemp_high) { 1321 if (di->charge_state != STATE_TEMP_LOWHIGH) 1322 ab8500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT); 1323 } 1324 1325 dev_dbg(di->dev, 1326 "[CHARGALG] Vb %d Ib_avg %d Ib_inst %d Tb %d Cap %d Maint %d " 1327 "State %s Active_chg %d Chg_status %d AC %d USB %d " 1328 "AC_online %d USB_online %d AC_CV %d USB_CV %d AC_I %d " 1329 "USB_I %d AC_Vset %d AC_Iset %d USB_Vset %d USB_Iset %d\n", 1330 di->batt_data.volt_uv, 1331 di->batt_data.avg_curr_ua, 1332 di->batt_data.inst_curr_ua, 1333 di->batt_data.temp, 1334 di->batt_data.percent, 1335 di->maintenance_chg, 1336 states[di->charge_state], 1337 di->chg_info.charger_type, 1338 di->charge_status, 1339 di->chg_info.conn_chg & AC_CHG, 1340 di->chg_info.conn_chg & USB_CHG, 1341 di->chg_info.online_chg & AC_CHG, 1342 di->chg_info.online_chg & USB_CHG, 1343 di->events.ac_cv_active, 1344 di->events.usb_cv_active, 1345 di->chg_info.ac_curr_ua, 1346 di->chg_info.usb_curr_ua, 1347 di->chg_info.ac_vset_uv, 1348 di->chg_info.ac_iset_ua, 1349 di->chg_info.usb_vset_uv, 1350 di->chg_info.usb_iset_ua); 1351 1352 switch (di->charge_state) { 1353 case STATE_HANDHELD_INIT: 1354 ab8500_chargalg_stop_charging(di); 1355 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING; 1356 ab8500_chargalg_state_to(di, STATE_HANDHELD); 1357 fallthrough; 1358 1359 case STATE_HANDHELD: 1360 break; 1361 1362 case STATE_BATT_REMOVED_INIT: 1363 ab8500_chargalg_stop_charging(di); 1364 ab8500_chargalg_state_to(di, STATE_BATT_REMOVED); 1365 fallthrough; 1366 1367 case STATE_BATT_REMOVED: 1368 if (!di->events.batt_rem) 1369 ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); 1370 break; 1371 1372 case STATE_HW_TEMP_PROTECT_INIT: 1373 ab8500_chargalg_stop_charging(di); 1374 ab8500_chargalg_state_to(di, STATE_HW_TEMP_PROTECT); 1375 fallthrough; 1376 1377 case STATE_HW_TEMP_PROTECT: 1378 if (!di->events.main_thermal_prot && 1379 !di->events.usb_thermal_prot) 1380 ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); 1381 break; 1382 1383 case STATE_OVV_PROTECT_INIT: 1384 ab8500_chargalg_stop_charging(di); 1385 ab8500_chargalg_state_to(di, STATE_OVV_PROTECT); 1386 fallthrough; 1387 1388 case STATE_OVV_PROTECT: 1389 if (!di->events.vbus_ovv && 1390 !di->events.main_ovv && 1391 !di->events.batt_ovv && 1392 di->chg_info.usb_chg_ok && 1393 di->chg_info.ac_chg_ok) 1394 ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); 1395 break; 1396 1397 case STATE_CHG_NOT_OK_INIT: 1398 ab8500_chargalg_stop_charging(di); 1399 ab8500_chargalg_state_to(di, STATE_CHG_NOT_OK); 1400 fallthrough; 1401 1402 case STATE_CHG_NOT_OK: 1403 if (!di->events.mainextchnotok && 1404 !di->events.usbchargernotok) 1405 ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); 1406 break; 1407 1408 case STATE_SAFETY_TIMER_EXPIRED_INIT: 1409 ab8500_chargalg_stop_charging(di); 1410 ab8500_chargalg_state_to(di, STATE_SAFETY_TIMER_EXPIRED); 1411 fallthrough; 1412 1413 case STATE_SAFETY_TIMER_EXPIRED: 1414 /* We exit this state when charger is removed */ 1415 break; 1416 1417 case STATE_NORMAL_INIT: 1418 if (bi->constant_charge_current_max_ua == 0) 1419 /* "charging" with 0 uA */ 1420 ab8500_chargalg_stop_charging(di); 1421 else { 1422 ab8500_chargalg_start_charging(di, 1423 bi->constant_charge_voltage_max_uv, 1424 bi->constant_charge_current_max_ua); 1425 } 1426 1427 ab8500_chargalg_state_to(di, STATE_NORMAL); 1428 ab8500_chargalg_start_safety_timer(di); 1429 ab8500_chargalg_stop_maintenance_timer(di); 1430 init_maxim_chg_curr(di); 1431 di->charge_status = POWER_SUPPLY_STATUS_CHARGING; 1432 di->eoc_cnt = 0; 1433 di->maintenance_chg = false; 1434 power_supply_changed(di->chargalg_psy); 1435 1436 break; 1437 1438 case STATE_NORMAL: 1439 handle_maxim_chg_curr(di); 1440 if (di->charge_status == POWER_SUPPLY_STATUS_FULL && 1441 di->maintenance_chg) { 1442 /* 1443 * The battery is fully charged, check if we support 1444 * maintenance charging else go back to waiting for 1445 * the recharge voltage limit. 1446 */ 1447 if (!power_supply_supports_maintenance_charging(bi)) 1448 ab8500_chargalg_state_to(di, 1449 STATE_WAIT_FOR_RECHARGE_INIT); 1450 else 1451 ab8500_chargalg_state_to(di, 1452 STATE_MAINTENANCE_A_INIT); 1453 } 1454 break; 1455 1456 /* This state will be used when the maintenance state is disabled */ 1457 case STATE_WAIT_FOR_RECHARGE_INIT: 1458 ab8500_chargalg_hold_charging(di); 1459 ab8500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE); 1460 fallthrough; 1461 1462 case STATE_WAIT_FOR_RECHARGE: 1463 if (ab8500_chargalg_time_to_restart(di)) 1464 ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); 1465 break; 1466 1467 case STATE_MAINTENANCE_A_INIT: 1468 mt = power_supply_get_maintenance_charging_setting(bi, 0); 1469 if (!mt) { 1470 /* No maintenance A state, go back to normal */ 1471 ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); 1472 power_supply_changed(di->chargalg_psy); 1473 break; 1474 } 1475 ab8500_chargalg_stop_safety_timer(di); 1476 ab8500_chargalg_start_maintenance_timer(di, 1477 mt->charge_safety_timer_minutes); 1478 ab8500_chargalg_start_charging(di, 1479 mt->charge_voltage_max_uv, 1480 mt->charge_current_max_ua); 1481 ab8500_chargalg_state_to(di, STATE_MAINTENANCE_A); 1482 power_supply_changed(di->chargalg_psy); 1483 fallthrough; 1484 1485 case STATE_MAINTENANCE_A: 1486 if (di->events.maintenance_timer_expired) { 1487 ab8500_chargalg_stop_maintenance_timer(di); 1488 ab8500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT); 1489 } 1490 /* 1491 * This happens if the voltage drops too quickly during 1492 * maintenance charging, especially in older batteries. 1493 */ 1494 if (ab8500_chargalg_time_to_restart(di)) { 1495 ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); 1496 dev_info(di->dev, "restarted charging from maintenance state A - battery getting old?\n"); 1497 } 1498 break; 1499 1500 case STATE_MAINTENANCE_B_INIT: 1501 mt = power_supply_get_maintenance_charging_setting(bi, 1); 1502 if (!mt) { 1503 /* No maintenance B state, go back to normal */ 1504 ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); 1505 power_supply_changed(di->chargalg_psy); 1506 break; 1507 } 1508 ab8500_chargalg_start_maintenance_timer(di, 1509 mt->charge_safety_timer_minutes); 1510 ab8500_chargalg_start_charging(di, 1511 mt->charge_voltage_max_uv, 1512 mt->charge_current_max_ua); 1513 ab8500_chargalg_state_to(di, STATE_MAINTENANCE_B); 1514 power_supply_changed(di->chargalg_psy); 1515 fallthrough; 1516 1517 case STATE_MAINTENANCE_B: 1518 if (di->events.maintenance_timer_expired) { 1519 ab8500_chargalg_stop_maintenance_timer(di); 1520 ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); 1521 } 1522 /* 1523 * This happens if the voltage drops too quickly during 1524 * maintenance charging, especially in older batteries. 1525 */ 1526 if (ab8500_chargalg_time_to_restart(di)) { 1527 ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); 1528 dev_info(di->dev, "restarted charging from maintenance state B - battery getting old?\n"); 1529 } 1530 break; 1531 1532 case STATE_TEMP_LOWHIGH_INIT: 1533 if (di->events.btemp_low) { 1534 ab8500_chargalg_start_charging(di, 1535 bi->alert_low_temp_charge_voltage_uv, 1536 bi->alert_low_temp_charge_current_ua); 1537 } else if (di->events.btemp_high) { 1538 ab8500_chargalg_start_charging(di, 1539 bi->alert_high_temp_charge_voltage_uv, 1540 bi->alert_high_temp_charge_current_ua); 1541 } else { 1542 dev_err(di->dev, "neither low or high temp event occurred\n"); 1543 ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); 1544 break; 1545 } 1546 ab8500_chargalg_stop_maintenance_timer(di); 1547 di->charge_status = POWER_SUPPLY_STATUS_CHARGING; 1548 ab8500_chargalg_state_to(di, STATE_TEMP_LOWHIGH); 1549 power_supply_changed(di->chargalg_psy); 1550 fallthrough; 1551 1552 case STATE_TEMP_LOWHIGH: 1553 if (!di->events.btemp_low && !di->events.btemp_high) 1554 ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); 1555 break; 1556 1557 case STATE_WD_EXPIRED_INIT: 1558 ab8500_chargalg_stop_charging(di); 1559 ab8500_chargalg_state_to(di, STATE_WD_EXPIRED); 1560 fallthrough; 1561 1562 case STATE_WD_EXPIRED: 1563 if (!di->events.ac_wd_expired && 1564 !di->events.usb_wd_expired) 1565 ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); 1566 break; 1567 1568 case STATE_TEMP_UNDEROVER_INIT: 1569 ab8500_chargalg_stop_charging(di); 1570 ab8500_chargalg_state_to(di, STATE_TEMP_UNDEROVER); 1571 fallthrough; 1572 1573 case STATE_TEMP_UNDEROVER: 1574 if (!di->events.btemp_underover) 1575 ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); 1576 break; 1577 } 1578 1579 /* Start charging directly if the new state is a charge state */ 1580 if (di->charge_state == STATE_NORMAL_INIT || 1581 di->charge_state == STATE_MAINTENANCE_A_INIT || 1582 di->charge_state == STATE_MAINTENANCE_B_INIT) 1583 queue_work(di->chargalg_wq, &di->chargalg_work); 1584 } 1585 1586 /** 1587 * ab8500_chargalg_periodic_work() - Periodic work for the algorithm 1588 * @work: pointer to the work_struct structure 1589 * 1590 * Work queue function for the charging algorithm 1591 */ 1592 static void ab8500_chargalg_periodic_work(struct work_struct *work) 1593 { 1594 struct ab8500_chargalg *di = container_of(work, 1595 struct ab8500_chargalg, chargalg_periodic_work.work); 1596 1597 ab8500_chargalg_algorithm(di); 1598 1599 /* 1600 * If a charger is connected then the battery has to be monitored 1601 * frequently, else the work can be delayed. 1602 */ 1603 if (di->chg_info.conn_chg) 1604 queue_delayed_work(di->chargalg_wq, 1605 &di->chargalg_periodic_work, 1606 di->bm->interval_charging * HZ); 1607 else 1608 queue_delayed_work(di->chargalg_wq, 1609 &di->chargalg_periodic_work, 1610 di->bm->interval_not_charging * HZ); 1611 } 1612 1613 /** 1614 * ab8500_chargalg_wd_work() - periodic work to kick the charger watchdog 1615 * @work: pointer to the work_struct structure 1616 * 1617 * Work queue function for kicking the charger watchdog 1618 */ 1619 static void ab8500_chargalg_wd_work(struct work_struct *work) 1620 { 1621 int ret; 1622 struct ab8500_chargalg *di = container_of(work, 1623 struct ab8500_chargalg, chargalg_wd_work.work); 1624 1625 ret = ab8500_chargalg_kick_watchdog(di); 1626 if (ret < 0) 1627 dev_err(di->dev, "failed to kick watchdog\n"); 1628 1629 queue_delayed_work(di->chargalg_wq, 1630 &di->chargalg_wd_work, CHG_WD_INTERVAL); 1631 } 1632 1633 /** 1634 * ab8500_chargalg_work() - Work to run the charging algorithm instantly 1635 * @work: pointer to the work_struct structure 1636 * 1637 * Work queue function for calling the charging algorithm 1638 */ 1639 static void ab8500_chargalg_work(struct work_struct *work) 1640 { 1641 struct ab8500_chargalg *di = container_of(work, 1642 struct ab8500_chargalg, chargalg_work); 1643 1644 ab8500_chargalg_algorithm(di); 1645 } 1646 1647 /** 1648 * ab8500_chargalg_get_property() - get the chargalg properties 1649 * @psy: pointer to the power_supply structure 1650 * @psp: pointer to the power_supply_property structure 1651 * @val: pointer to the power_supply_propval union 1652 * 1653 * This function gets called when an application tries to get the 1654 * chargalg properties by reading the sysfs files. 1655 * status: charging/discharging/full/unknown 1656 * health: health of the battery 1657 * Returns error code in case of failure else 0 on success 1658 */ 1659 static int ab8500_chargalg_get_property(struct power_supply *psy, 1660 enum power_supply_property psp, 1661 union power_supply_propval *val) 1662 { 1663 struct ab8500_chargalg *di = power_supply_get_drvdata(psy); 1664 1665 switch (psp) { 1666 case POWER_SUPPLY_PROP_STATUS: 1667 val->intval = di->charge_status; 1668 break; 1669 case POWER_SUPPLY_PROP_HEALTH: 1670 if (di->events.batt_ovv) { 1671 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 1672 } else if (di->events.btemp_underover) { 1673 if (di->batt_data.temp <= di->bm->bi->temp_min) 1674 val->intval = POWER_SUPPLY_HEALTH_COLD; 1675 else 1676 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; 1677 } else if (di->charge_state == STATE_SAFETY_TIMER_EXPIRED || 1678 di->charge_state == STATE_SAFETY_TIMER_EXPIRED_INIT) { 1679 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 1680 } else { 1681 val->intval = POWER_SUPPLY_HEALTH_GOOD; 1682 } 1683 break; 1684 default: 1685 return -EINVAL; 1686 } 1687 return 0; 1688 } 1689 1690 static int __maybe_unused ab8500_chargalg_resume(struct device *dev) 1691 { 1692 struct ab8500_chargalg *di = dev_get_drvdata(dev); 1693 1694 /* Kick charger watchdog if charging (any charger online) */ 1695 if (di->chg_info.online_chg) 1696 queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0); 1697 1698 /* 1699 * Run the charging algorithm directly to be sure we don't 1700 * do it too seldom 1701 */ 1702 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0); 1703 1704 return 0; 1705 } 1706 1707 static int __maybe_unused ab8500_chargalg_suspend(struct device *dev) 1708 { 1709 struct ab8500_chargalg *di = dev_get_drvdata(dev); 1710 1711 if (di->chg_info.online_chg) 1712 cancel_delayed_work_sync(&di->chargalg_wd_work); 1713 1714 cancel_delayed_work_sync(&di->chargalg_periodic_work); 1715 1716 return 0; 1717 } 1718 1719 static char *supply_interface[] = { 1720 "ab8500_fg", 1721 }; 1722 1723 static const struct power_supply_desc ab8500_chargalg_desc = { 1724 .name = "ab8500_chargalg", 1725 .type = POWER_SUPPLY_TYPE_BATTERY, 1726 .properties = ab8500_chargalg_props, 1727 .num_properties = ARRAY_SIZE(ab8500_chargalg_props), 1728 .get_property = ab8500_chargalg_get_property, 1729 .external_power_changed = ab8500_chargalg_external_power_changed, 1730 }; 1731 1732 static int ab8500_chargalg_bind(struct device *dev, struct device *master, 1733 void *data) 1734 { 1735 struct ab8500_chargalg *di = dev_get_drvdata(dev); 1736 1737 /* Create a work queue for the chargalg */ 1738 di->chargalg_wq = alloc_ordered_workqueue("ab8500_chargalg_wq", 1739 WQ_MEM_RECLAIM); 1740 if (di->chargalg_wq == NULL) { 1741 dev_err(di->dev, "failed to create work queue\n"); 1742 return -ENOMEM; 1743 } 1744 1745 /* Run the charging algorithm */ 1746 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0); 1747 1748 return 0; 1749 } 1750 1751 static void ab8500_chargalg_unbind(struct device *dev, struct device *master, 1752 void *data) 1753 { 1754 struct ab8500_chargalg *di = dev_get_drvdata(dev); 1755 1756 /* Stop all timers and work */ 1757 hrtimer_cancel(&di->safety_timer); 1758 hrtimer_cancel(&di->maintenance_timer); 1759 1760 cancel_delayed_work_sync(&di->chargalg_periodic_work); 1761 cancel_delayed_work_sync(&di->chargalg_wd_work); 1762 cancel_work_sync(&di->chargalg_work); 1763 1764 /* Delete the work queue */ 1765 destroy_workqueue(di->chargalg_wq); 1766 } 1767 1768 static const struct component_ops ab8500_chargalg_component_ops = { 1769 .bind = ab8500_chargalg_bind, 1770 .unbind = ab8500_chargalg_unbind, 1771 }; 1772 1773 static int ab8500_chargalg_probe(struct platform_device *pdev) 1774 { 1775 struct device *dev = &pdev->dev; 1776 struct power_supply_config psy_cfg = {}; 1777 struct ab8500_chargalg *di; 1778 1779 di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL); 1780 if (!di) 1781 return -ENOMEM; 1782 1783 di->bm = &ab8500_bm_data; 1784 1785 /* get device struct and parent */ 1786 di->dev = dev; 1787 di->parent = dev_get_drvdata(pdev->dev.parent); 1788 1789 psy_cfg.supplied_to = supply_interface; 1790 psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface); 1791 psy_cfg.drv_data = di; 1792 1793 /* Initilialize safety timer */ 1794 hrtimer_init(&di->safety_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 1795 di->safety_timer.function = ab8500_chargalg_safety_timer_expired; 1796 1797 /* Initilialize maintenance timer */ 1798 hrtimer_init(&di->maintenance_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 1799 di->maintenance_timer.function = 1800 ab8500_chargalg_maintenance_timer_expired; 1801 1802 /* Init work for chargalg */ 1803 INIT_DEFERRABLE_WORK(&di->chargalg_periodic_work, 1804 ab8500_chargalg_periodic_work); 1805 INIT_DEFERRABLE_WORK(&di->chargalg_wd_work, 1806 ab8500_chargalg_wd_work); 1807 1808 /* Init work for chargalg */ 1809 INIT_WORK(&di->chargalg_work, ab8500_chargalg_work); 1810 1811 /* To detect charger at startup */ 1812 di->chg_info.prev_conn_chg = -1; 1813 1814 /* Register chargalg power supply class */ 1815 di->chargalg_psy = devm_power_supply_register(di->dev, 1816 &ab8500_chargalg_desc, 1817 &psy_cfg); 1818 if (IS_ERR(di->chargalg_psy)) { 1819 dev_err(di->dev, "failed to register chargalg psy\n"); 1820 return PTR_ERR(di->chargalg_psy); 1821 } 1822 1823 platform_set_drvdata(pdev, di); 1824 1825 dev_info(di->dev, "probe success\n"); 1826 return component_add(dev, &ab8500_chargalg_component_ops); 1827 } 1828 1829 static int ab8500_chargalg_remove(struct platform_device *pdev) 1830 { 1831 component_del(&pdev->dev, &ab8500_chargalg_component_ops); 1832 1833 return 0; 1834 } 1835 1836 static SIMPLE_DEV_PM_OPS(ab8500_chargalg_pm_ops, ab8500_chargalg_suspend, ab8500_chargalg_resume); 1837 1838 static const struct of_device_id ab8500_chargalg_match[] = { 1839 { .compatible = "stericsson,ab8500-chargalg", }, 1840 { }, 1841 }; 1842 1843 struct platform_driver ab8500_chargalg_driver = { 1844 .probe = ab8500_chargalg_probe, 1845 .remove = ab8500_chargalg_remove, 1846 .driver = { 1847 .name = "ab8500_chargalg", 1848 .of_match_table = ab8500_chargalg_match, 1849 .pm = &ab8500_chargalg_pm_ops, 1850 }, 1851 }; 1852 MODULE_LICENSE("GPL v2"); 1853 MODULE_AUTHOR("Johan Palsson, Karl Komierowski"); 1854 MODULE_ALIAS("platform:ab8500-chargalg"); 1855 MODULE_DESCRIPTION("ab8500 battery charging algorithm"); 1856