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