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