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