linux/drivers/power/ab8500_charger.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) ST-Ericsson SA 2012
   3 *
   4 * Charger driver for AB8500
   5 *
   6 * License Terms: GNU General Public License v2
   7 * Author:
   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/regulator/consumer.h>
  23#include <linux/err.h>
  24#include <linux/workqueue.h>
  25#include <linux/kobject.h>
  26#include <linux/of.h>
  27#include <linux/mfd/core.h>
  28#include <linux/mfd/abx500/ab8500.h>
  29#include <linux/mfd/abx500.h>
  30#include <linux/mfd/abx500/ab8500-bm.h>
  31#include <linux/mfd/abx500/ab8500-gpadc.h>
  32#include <linux/mfd/abx500/ux500_chargalg.h>
  33#include <linux/usb/otg.h>
  34#include <linux/mutex.h>
  35
  36/* Charger constants */
  37#define NO_PW_CONN                      0
  38#define AC_PW_CONN                      1
  39#define USB_PW_CONN                     2
  40
  41#define MAIN_WDOG_ENA                   0x01
  42#define MAIN_WDOG_KICK                  0x02
  43#define MAIN_WDOG_DIS                   0x00
  44#define CHARG_WD_KICK                   0x01
  45#define MAIN_CH_ENA                     0x01
  46#define MAIN_CH_NO_OVERSHOOT_ENA_N      0x02
  47#define USB_CH_ENA                      0x01
  48#define USB_CHG_NO_OVERSHOOT_ENA_N      0x02
  49#define MAIN_CH_DET                     0x01
  50#define MAIN_CH_CV_ON                   0x04
  51#define USB_CH_CV_ON                    0x08
  52#define VBUS_DET_DBNC100                0x02
  53#define VBUS_DET_DBNC1                  0x01
  54#define OTP_ENABLE_WD                   0x01
  55
  56#define MAIN_CH_INPUT_CURR_SHIFT        4
  57#define VBUS_IN_CURR_LIM_SHIFT          4
  58#define AUTO_VBUS_IN_CURR_LIM_SHIFT     4
  59
  60#define LED_INDICATOR_PWM_ENA           0x01
  61#define LED_INDICATOR_PWM_DIS           0x00
  62#define LED_IND_CUR_5MA                 0x04
  63#define LED_INDICATOR_PWM_DUTY_252_256  0xBF
  64
  65/* HW failure constants */
  66#define MAIN_CH_TH_PROT                 0x02
  67#define VBUS_CH_NOK                     0x08
  68#define USB_CH_TH_PROT                  0x02
  69#define VBUS_OVV_TH                     0x01
  70#define MAIN_CH_NOK                     0x01
  71#define VBUS_DET                        0x80
  72
  73#define MAIN_CH_STATUS2_MAINCHGDROP             0x80
  74#define MAIN_CH_STATUS2_MAINCHARGERDETDBNC      0x40
  75#define USB_CH_VBUSDROP                         0x40
  76#define USB_CH_VBUSDETDBNC                      0x01
  77
  78/* UsbLineStatus register bit masks */
  79#define AB8500_USB_LINK_STATUS          0x78
  80#define AB8500_STD_HOST_SUSP            0x18
  81
  82/* Watchdog timeout constant */
  83#define WD_TIMER                        0x30 /* 4min */
  84#define WD_KICK_INTERVAL                (60 * HZ)
  85
  86/* Lowest charger voltage is 3.39V -> 0x4E */
  87#define LOW_VOLT_REG                    0x4E
  88
  89/* Step up/down delay in us */
  90#define STEP_UDELAY                     1000
  91
  92#define CHARGER_STATUS_POLL 10 /* in ms */
  93
  94#define CHG_WD_INTERVAL                 (60 * HZ)
  95
  96#define AB8500_SW_CONTROL_FALLBACK      0x03
  97/* Wait for enumeration before charing in us */
  98#define WAIT_ACA_RID_ENUMERATION        (5 * 1000)
  99
 100/* UsbLineStatus register - usb types */
 101enum ab8500_charger_link_status {
 102        USB_STAT_NOT_CONFIGURED,
 103        USB_STAT_STD_HOST_NC,
 104        USB_STAT_STD_HOST_C_NS,
 105        USB_STAT_STD_HOST_C_S,
 106        USB_STAT_HOST_CHG_NM,
 107        USB_STAT_HOST_CHG_HS,
 108        USB_STAT_HOST_CHG_HS_CHIRP,
 109        USB_STAT_DEDICATED_CHG,
 110        USB_STAT_ACA_RID_A,
 111        USB_STAT_ACA_RID_B,
 112        USB_STAT_ACA_RID_C_NM,
 113        USB_STAT_ACA_RID_C_HS,
 114        USB_STAT_ACA_RID_C_HS_CHIRP,
 115        USB_STAT_HM_IDGND,
 116        USB_STAT_RESERVED,
 117        USB_STAT_NOT_VALID_LINK,
 118        USB_STAT_PHY_EN,
 119        USB_STAT_SUP_NO_IDGND_VBUS,
 120        USB_STAT_SUP_IDGND_VBUS,
 121        USB_STAT_CHARGER_LINE_1,
 122        USB_STAT_CARKIT_1,
 123        USB_STAT_CARKIT_2,
 124        USB_STAT_ACA_DOCK_CHARGER,
 125};
 126
 127enum ab8500_usb_state {
 128        AB8500_BM_USB_STATE_RESET_HS,   /* HighSpeed Reset */
 129        AB8500_BM_USB_STATE_RESET_FS,   /* FullSpeed/LowSpeed Reset */
 130        AB8500_BM_USB_STATE_CONFIGURED,
 131        AB8500_BM_USB_STATE_SUSPEND,
 132        AB8500_BM_USB_STATE_RESUME,
 133        AB8500_BM_USB_STATE_MAX,
 134};
 135
 136/* VBUS input current limits supported in AB8500 in mA */
 137#define USB_CH_IP_CUR_LVL_0P05          50
 138#define USB_CH_IP_CUR_LVL_0P09          98
 139#define USB_CH_IP_CUR_LVL_0P19          193
 140#define USB_CH_IP_CUR_LVL_0P29          290
 141#define USB_CH_IP_CUR_LVL_0P38          380
 142#define USB_CH_IP_CUR_LVL_0P45          450
 143#define USB_CH_IP_CUR_LVL_0P5           500
 144#define USB_CH_IP_CUR_LVL_0P6           600
 145#define USB_CH_IP_CUR_LVL_0P7           700
 146#define USB_CH_IP_CUR_LVL_0P8           800
 147#define USB_CH_IP_CUR_LVL_0P9           900
 148#define USB_CH_IP_CUR_LVL_1P0           1000
 149#define USB_CH_IP_CUR_LVL_1P1           1100
 150#define USB_CH_IP_CUR_LVL_1P3           1300
 151#define USB_CH_IP_CUR_LVL_1P4           1400
 152#define USB_CH_IP_CUR_LVL_1P5           1500
 153
 154#define VBAT_TRESH_IP_CUR_RED           3800
 155
 156#define to_ab8500_charger_usb_device_info(x) container_of((x), \
 157        struct ab8500_charger, usb_chg)
 158#define to_ab8500_charger_ac_device_info(x) container_of((x), \
 159        struct ab8500_charger, ac_chg)
 160
 161/**
 162 * struct ab8500_charger_interrupts - ab8500 interupts
 163 * @name:       name of the interrupt
 164 * @isr         function pointer to the isr
 165 */
 166struct ab8500_charger_interrupts {
 167        char *name;
 168        irqreturn_t (*isr)(int irq, void *data);
 169};
 170
 171struct ab8500_charger_info {
 172        int charger_connected;
 173        int charger_online;
 174        int charger_voltage;
 175        int cv_active;
 176        bool wd_expired;
 177        int charger_current;
 178};
 179
 180struct ab8500_charger_event_flags {
 181        bool mainextchnotok;
 182        bool main_thermal_prot;
 183        bool usb_thermal_prot;
 184        bool vbus_ovv;
 185        bool usbchargernotok;
 186        bool chgwdexp;
 187        bool vbus_collapse;
 188        bool vbus_drop_end;
 189};
 190
 191struct ab8500_charger_usb_state {
 192        int usb_current;
 193        int usb_current_tmp;
 194        enum ab8500_usb_state state;
 195        enum ab8500_usb_state state_tmp;
 196        spinlock_t usb_lock;
 197};
 198
 199/**
 200 * struct ab8500_charger - ab8500 Charger device information
 201 * @dev:                Pointer to the structure device
 202 * @max_usb_in_curr:    Max USB charger input current
 203 * @vbus_detected:      VBUS detected
 204 * @vbus_detected_start:
 205 *                      VBUS detected during startup
 206 * @ac_conn:            This will be true when the AC charger has been plugged
 207 * @vddadc_en_ac:       Indicate if VDD ADC supply is enabled because AC
 208 *                      charger is enabled
 209 * @vddadc_en_usb:      Indicate if VDD ADC supply is enabled because USB
 210 *                      charger is enabled
 211 * @vbat                Battery voltage
 212 * @old_vbat            Previously measured battery voltage
 213 * @usb_device_is_unrecognised  USB device is unrecognised by the hardware
 214 * @autopower           Indicate if we should have automatic pwron after pwrloss
 215 * @autopower_cfg       platform specific power config support for "pwron after pwrloss"
 216 * @invalid_charger_detect_state State when forcing AB to use invalid charger
 217 * @is_usb_host:        Indicate if last detected USB type is host
 218 * @is_aca_rid:         Incicate if accessory is ACA type
 219 * @current_stepping_sessions:
 220 *                      Counter for current stepping sessions
 221 * @parent:             Pointer to the struct ab8500
 222 * @gpadc:              Pointer to the struct gpadc
 223 * @bm:                 Platform specific battery management information
 224 * @flags:              Structure for information about events triggered
 225 * @usb_state:          Structure for usb stack information
 226 * @ac_chg:             AC charger power supply
 227 * @usb_chg:            USB charger power supply
 228 * @ac:                 Structure that holds the AC charger properties
 229 * @usb:                Structure that holds the USB charger properties
 230 * @regu:               Pointer to the struct regulator
 231 * @charger_wq:         Work queue for the IRQs and checking HW state
 232 * @usb_ipt_crnt_lock:  Lock to protect VBUS input current setting from mutuals
 233 * @pm_lock:            Lock to prevent system to suspend
 234 * @check_vbat_work     Work for checking vbat threshold to adjust vbus current
 235 * @check_hw_failure_work:      Work for checking HW state
 236 * @check_usbchgnotok_work:     Work for checking USB charger not ok status
 237 * @kick_wd_work:               Work for kicking the charger watchdog in case
 238 *                              of ABB rev 1.* due to the watchog logic bug
 239 * @ac_charger_attached_work:   Work for checking if AC charger is still
 240 *                              connected
 241 * @usb_charger_attached_work:  Work for checking if USB charger is still
 242 *                              connected
 243 * @ac_work:                    Work for checking AC charger connection
 244 * @detect_usb_type_work:       Work for detecting the USB type connected
 245 * @usb_link_status_work:       Work for checking the new USB link status
 246 * @usb_state_changed_work:     Work for checking USB state
 247 * @attach_work:                Work for detecting USB type
 248 * @vbus_drop_end_work:         Work for detecting VBUS drop end
 249 * @check_main_thermal_prot_work:
 250 *                              Work for checking Main thermal status
 251 * @check_usb_thermal_prot_work:
 252 *                              Work for checking USB thermal status
 253 * @charger_attached_mutex:     For controlling the wakelock
 254 */
 255struct ab8500_charger {
 256        struct device *dev;
 257        int max_usb_in_curr;
 258        bool vbus_detected;
 259        bool vbus_detected_start;
 260        bool ac_conn;
 261        bool vddadc_en_ac;
 262        bool vddadc_en_usb;
 263        int vbat;
 264        int old_vbat;
 265        bool usb_device_is_unrecognised;
 266        bool autopower;
 267        bool autopower_cfg;
 268        int invalid_charger_detect_state;
 269        bool is_usb_host;
 270        int is_aca_rid;
 271        atomic_t current_stepping_sessions;
 272        struct ab8500 *parent;
 273        struct ab8500_gpadc *gpadc;
 274        struct abx500_bm_data *bm;
 275        struct ab8500_charger_event_flags flags;
 276        struct ab8500_charger_usb_state usb_state;
 277        struct ux500_charger ac_chg;
 278        struct ux500_charger usb_chg;
 279        struct ab8500_charger_info ac;
 280        struct ab8500_charger_info usb;
 281        struct regulator *regu;
 282        struct workqueue_struct *charger_wq;
 283        struct mutex usb_ipt_crnt_lock;
 284        struct delayed_work check_vbat_work;
 285        struct delayed_work check_hw_failure_work;
 286        struct delayed_work check_usbchgnotok_work;
 287        struct delayed_work kick_wd_work;
 288        struct delayed_work usb_state_changed_work;
 289        struct delayed_work attach_work;
 290        struct delayed_work ac_charger_attached_work;
 291        struct delayed_work usb_charger_attached_work;
 292        struct delayed_work vbus_drop_end_work;
 293        struct work_struct ac_work;
 294        struct work_struct detect_usb_type_work;
 295        struct work_struct usb_link_status_work;
 296        struct work_struct check_main_thermal_prot_work;
 297        struct work_struct check_usb_thermal_prot_work;
 298        struct usb_phy *usb_phy;
 299        struct notifier_block nb;
 300        struct mutex charger_attached_mutex;
 301};
 302
 303/* AC properties */
 304static enum power_supply_property ab8500_charger_ac_props[] = {
 305        POWER_SUPPLY_PROP_HEALTH,
 306        POWER_SUPPLY_PROP_PRESENT,
 307        POWER_SUPPLY_PROP_ONLINE,
 308        POWER_SUPPLY_PROP_VOLTAGE_NOW,
 309        POWER_SUPPLY_PROP_VOLTAGE_AVG,
 310        POWER_SUPPLY_PROP_CURRENT_NOW,
 311};
 312
 313/* USB properties */
 314static enum power_supply_property ab8500_charger_usb_props[] = {
 315        POWER_SUPPLY_PROP_HEALTH,
 316        POWER_SUPPLY_PROP_CURRENT_AVG,
 317        POWER_SUPPLY_PROP_PRESENT,
 318        POWER_SUPPLY_PROP_ONLINE,
 319        POWER_SUPPLY_PROP_VOLTAGE_NOW,
 320        POWER_SUPPLY_PROP_VOLTAGE_AVG,
 321        POWER_SUPPLY_PROP_CURRENT_NOW,
 322};
 323
 324/*
 325 * Function for enabling and disabling sw fallback mode
 326 * should always be disabled when no charger is connected.
 327 */
 328static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
 329                bool fallback)
 330{
 331        u8 val;
 332        u8 reg;
 333        u8 bank;
 334        u8 bit;
 335        int ret;
 336
 337        dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
 338
 339        if (is_ab8500(di->parent)) {
 340                bank = 0x15;
 341                reg = 0x0;
 342                bit = 3;
 343        } else {
 344                bank = AB8500_SYS_CTRL1_BLOCK;
 345                reg = AB8500_SW_CONTROL_FALLBACK;
 346                bit = 0;
 347        }
 348
 349        /* read the register containing fallback bit */
 350        ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
 351        if (ret < 0) {
 352                dev_err(di->dev, "%d read failed\n", __LINE__);
 353                return;
 354        }
 355
 356        if (is_ab8500(di->parent)) {
 357                /* enable the OPT emulation registers */
 358                ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
 359                if (ret) {
 360                        dev_err(di->dev, "%d write failed\n", __LINE__);
 361                        goto disable_otp;
 362                }
 363        }
 364
 365        if (fallback)
 366                val |= (1 << bit);
 367        else
 368                val &= ~(1 << bit);
 369
 370        /* write back the changed fallback bit value to register */
 371        ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
 372        if (ret) {
 373                dev_err(di->dev, "%d write failed\n", __LINE__);
 374        }
 375
 376disable_otp:
 377        if (is_ab8500(di->parent)) {
 378                /* disable the set OTP registers again */
 379                ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
 380                if (ret) {
 381                        dev_err(di->dev, "%d write failed\n", __LINE__);
 382                }
 383        }
 384}
 385
 386/**
 387 * ab8500_power_supply_changed - a wrapper with local extentions for
 388 * power_supply_changed
 389 * @di:   pointer to the ab8500_charger structure
 390 * @psy:  pointer to power_supply_that have changed.
 391 *
 392 */
 393static void ab8500_power_supply_changed(struct ab8500_charger *di,
 394                                        struct power_supply *psy)
 395{
 396        if (di->autopower_cfg) {
 397                if (!di->usb.charger_connected &&
 398                    !di->ac.charger_connected &&
 399                    di->autopower) {
 400                        di->autopower = false;
 401                        ab8500_enable_disable_sw_fallback(di, false);
 402                } else if (!di->autopower &&
 403                           (di->ac.charger_connected ||
 404                            di->usb.charger_connected)) {
 405                        di->autopower = true;
 406                        ab8500_enable_disable_sw_fallback(di, true);
 407                }
 408        }
 409        power_supply_changed(psy);
 410}
 411
 412static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
 413        bool connected)
 414{
 415        if (connected != di->usb.charger_connected) {
 416                dev_dbg(di->dev, "USB connected:%i\n", connected);
 417                di->usb.charger_connected = connected;
 418                sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
 419
 420                if (connected) {
 421                        mutex_lock(&di->charger_attached_mutex);
 422                        mutex_unlock(&di->charger_attached_mutex);
 423
 424                        queue_delayed_work(di->charger_wq,
 425                                           &di->usb_charger_attached_work,
 426                                           HZ);
 427                } else {
 428                        cancel_delayed_work_sync(&di->usb_charger_attached_work);
 429                        mutex_lock(&di->charger_attached_mutex);
 430                        mutex_unlock(&di->charger_attached_mutex);
 431                }
 432        }
 433}
 434
 435/**
 436 * ab8500_charger_get_ac_voltage() - get ac charger voltage
 437 * @di:         pointer to the ab8500_charger structure
 438 *
 439 * Returns ac charger voltage (on success)
 440 */
 441static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
 442{
 443        int vch;
 444
 445        /* Only measure voltage if the charger is connected */
 446        if (di->ac.charger_connected) {
 447                vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
 448                if (vch < 0)
 449                        dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
 450        } else {
 451                vch = 0;
 452        }
 453        return vch;
 454}
 455
 456/**
 457 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
 458 * @di:         pointer to the ab8500_charger structure
 459 *
 460 * Returns ac charger CV mode (on success) else error code
 461 */
 462static int ab8500_charger_ac_cv(struct ab8500_charger *di)
 463{
 464        u8 val;
 465        int ret = 0;
 466
 467        /* Only check CV mode if the charger is online */
 468        if (di->ac.charger_online) {
 469                ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
 470                        AB8500_CH_STATUS1_REG, &val);
 471                if (ret < 0) {
 472                        dev_err(di->dev, "%s ab8500 read failed\n", __func__);
 473                        return 0;
 474                }
 475
 476                if (val & MAIN_CH_CV_ON)
 477                        ret = 1;
 478                else
 479                        ret = 0;
 480        }
 481
 482        return ret;
 483}
 484
 485/**
 486 * ab8500_charger_get_vbus_voltage() - get vbus voltage
 487 * @di:         pointer to the ab8500_charger structure
 488 *
 489 * This function returns the vbus voltage.
 490 * Returns vbus voltage (on success)
 491 */
 492static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
 493{
 494        int vch;
 495
 496        /* Only measure voltage if the charger is connected */
 497        if (di->usb.charger_connected) {
 498                vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
 499                if (vch < 0)
 500                        dev_err(di->dev, "%s gpadc conv failed\n", __func__);
 501        } else {
 502                vch = 0;
 503        }
 504        return vch;
 505}
 506
 507/**
 508 * ab8500_charger_get_usb_current() - get usb charger current
 509 * @di:         pointer to the ab8500_charger structure
 510 *
 511 * This function returns the usb charger current.
 512 * Returns usb current (on success) and error code on failure
 513 */
 514static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
 515{
 516        int ich;
 517
 518        /* Only measure current if the charger is online */
 519        if (di->usb.charger_online) {
 520                ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
 521                if (ich < 0)
 522                        dev_err(di->dev, "%s gpadc conv failed\n", __func__);
 523        } else {
 524                ich = 0;
 525        }
 526        return ich;
 527}
 528
 529/**
 530 * ab8500_charger_get_ac_current() - get ac charger current
 531 * @di:         pointer to the ab8500_charger structure
 532 *
 533 * This function returns the ac charger current.
 534 * Returns ac current (on success) and error code on failure.
 535 */
 536static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
 537{
 538        int ich;
 539
 540        /* Only measure current if the charger is online */
 541        if (di->ac.charger_online) {
 542                ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
 543                if (ich < 0)
 544                        dev_err(di->dev, "%s gpadc conv failed\n", __func__);
 545        } else {
 546                ich = 0;
 547        }
 548        return ich;
 549}
 550
 551/**
 552 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
 553 * @di:         pointer to the ab8500_charger structure
 554 *
 555 * Returns ac charger CV mode (on success) else error code
 556 */
 557static int ab8500_charger_usb_cv(struct ab8500_charger *di)
 558{
 559        int ret;
 560        u8 val;
 561
 562        /* Only check CV mode if the charger is online */
 563        if (di->usb.charger_online) {
 564                ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
 565                        AB8500_CH_USBCH_STAT1_REG, &val);
 566                if (ret < 0) {
 567                        dev_err(di->dev, "%s ab8500 read failed\n", __func__);
 568                        return 0;
 569                }
 570
 571                if (val & USB_CH_CV_ON)
 572                        ret = 1;
 573                else
 574                        ret = 0;
 575        } else {
 576                ret = 0;
 577        }
 578
 579        return ret;
 580}
 581
 582/**
 583 * ab8500_charger_detect_chargers() - Detect the connected chargers
 584 * @di:         pointer to the ab8500_charger structure
 585 * @probe:      if probe, don't delay and wait for HW
 586 *
 587 * Returns the type of charger connected.
 588 * For USB it will not mean we can actually charge from it
 589 * but that there is a USB cable connected that we have to
 590 * identify. This is used during startup when we don't get
 591 * interrupts of the charger detection
 592 *
 593 * Returns an integer value, that means,
 594 * NO_PW_CONN  no power supply is connected
 595 * AC_PW_CONN  if the AC power supply is connected
 596 * USB_PW_CONN  if the USB power supply is connected
 597 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
 598 */
 599static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
 600{
 601        int result = NO_PW_CONN;
 602        int ret;
 603        u8 val;
 604
 605        /* Check for AC charger */
 606        ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
 607                AB8500_CH_STATUS1_REG, &val);
 608        if (ret < 0) {
 609                dev_err(di->dev, "%s ab8500 read failed\n", __func__);
 610                return ret;
 611        }
 612
 613        if (val & MAIN_CH_DET)
 614                result = AC_PW_CONN;
 615
 616        /* Check for USB charger */
 617
 618        if (!probe) {
 619                /*
 620                 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
 621                 * when disconnecting ACA even though no
 622                 * charger was connected. Try waiting a little
 623                 * longer than the 100 ms of VBUS_DET_DBNC100...
 624                 */
 625                msleep(110);
 626        }
 627        ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
 628                AB8500_CH_USBCH_STAT1_REG, &val);
 629        if (ret < 0) {
 630                dev_err(di->dev, "%s ab8500 read failed\n", __func__);
 631                return ret;
 632        }
 633        dev_dbg(di->dev,
 634                "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
 635                val);
 636        if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
 637                result |= USB_PW_CONN;
 638
 639        return result;
 640}
 641
 642/**
 643 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
 644 * @di:                 pointer to the ab8500_charger structure
 645 * @link_status:        the identified USB type
 646 *
 647 * Get the maximum current that is allowed to be drawn from the host
 648 * based on the USB type.
 649 * Returns error code in case of failure else 0 on success
 650 */
 651static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
 652                enum ab8500_charger_link_status link_status)
 653{
 654        int ret = 0;
 655
 656        di->usb_device_is_unrecognised = false;
 657
 658        /*
 659         * Platform only supports USB 2.0.
 660         * This means that charging current from USB source
 661         * is maximum 500 mA. Every occurence of USB_STAT_*_HOST_*
 662         * should set USB_CH_IP_CUR_LVL_0P5.
 663         */
 664
 665        switch (link_status) {
 666        case USB_STAT_STD_HOST_NC:
 667        case USB_STAT_STD_HOST_C_NS:
 668        case USB_STAT_STD_HOST_C_S:
 669                dev_dbg(di->dev, "USB Type - Standard host is "
 670                        "detected through USB driver\n");
 671                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
 672                di->is_usb_host = true;
 673                di->is_aca_rid = 0;
 674                break;
 675        case USB_STAT_HOST_CHG_HS_CHIRP:
 676                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
 677                di->is_usb_host = true;
 678                di->is_aca_rid = 0;
 679                break;
 680        case USB_STAT_HOST_CHG_HS:
 681                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
 682                di->is_usb_host = true;
 683                di->is_aca_rid = 0;
 684                break;
 685        case USB_STAT_ACA_RID_C_HS:
 686                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9;
 687                di->is_usb_host = false;
 688                di->is_aca_rid = 0;
 689                break;
 690        case USB_STAT_ACA_RID_A:
 691                /*
 692                 * Dedicated charger level minus maximum current accessory
 693                 * can consume (900mA). Closest level is 500mA
 694                 */
 695                dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
 696                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
 697                di->is_usb_host = false;
 698                di->is_aca_rid = 1;
 699                break;
 700        case USB_STAT_ACA_RID_B:
 701                /*
 702                 * Dedicated charger level minus 120mA (20mA for ACA and
 703                 * 100mA for potential accessory). Closest level is 1300mA
 704                 */
 705                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3;
 706                dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
 707                                di->max_usb_in_curr);
 708                di->is_usb_host = false;
 709                di->is_aca_rid = 1;
 710                break;
 711        case USB_STAT_HOST_CHG_NM:
 712                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
 713                di->is_usb_host = true;
 714                di->is_aca_rid = 0;
 715                break;
 716        case USB_STAT_DEDICATED_CHG:
 717                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
 718                di->is_usb_host = false;
 719                di->is_aca_rid = 0;
 720                break;
 721        case USB_STAT_ACA_RID_C_HS_CHIRP:
 722        case USB_STAT_ACA_RID_C_NM:
 723                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
 724                di->is_usb_host = false;
 725                di->is_aca_rid = 1;
 726                break;
 727        case USB_STAT_NOT_CONFIGURED:
 728                if (di->vbus_detected) {
 729                        di->usb_device_is_unrecognised = true;
 730                        dev_dbg(di->dev, "USB Type - Legacy charger.\n");
 731                        di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
 732                        break;
 733                }
 734        case USB_STAT_HM_IDGND:
 735                dev_err(di->dev, "USB Type - Charging not allowed\n");
 736                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
 737                ret = -ENXIO;
 738                break;
 739        case USB_STAT_RESERVED:
 740                if (is_ab8500(di->parent)) {
 741                        di->flags.vbus_collapse = true;
 742                        dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
 743                                                "VBUS has collapsed\n");
 744                        ret = -ENXIO;
 745                        break;
 746                }
 747                if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
 748                        dev_dbg(di->dev, "USB Type - Charging not allowed\n");
 749                        di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
 750                        dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
 751                                        link_status, di->max_usb_in_curr);
 752                        ret = -ENXIO;
 753                        break;
 754                }
 755                break;
 756        case USB_STAT_CARKIT_1:
 757        case USB_STAT_CARKIT_2:
 758        case USB_STAT_ACA_DOCK_CHARGER:
 759        case USB_STAT_CHARGER_LINE_1:
 760                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
 761                dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
 762                                di->max_usb_in_curr);
 763        case USB_STAT_NOT_VALID_LINK:
 764                dev_err(di->dev, "USB Type invalid - try charging anyway\n");
 765                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
 766                break;
 767
 768        default:
 769                dev_err(di->dev, "USB Type - Unknown\n");
 770                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
 771                ret = -ENXIO;
 772                break;
 773        };
 774
 775        dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
 776                link_status, di->max_usb_in_curr);
 777
 778        return ret;
 779}
 780
 781/**
 782 * ab8500_charger_read_usb_type() - read the type of usb connected
 783 * @di:         pointer to the ab8500_charger structure
 784 *
 785 * Detect the type of the plugged USB
 786 * Returns error code in case of failure else 0 on success
 787 */
 788static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
 789{
 790        int ret;
 791        u8 val;
 792
 793        ret = abx500_get_register_interruptible(di->dev,
 794                AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
 795        if (ret < 0) {
 796                dev_err(di->dev, "%s ab8500 read failed\n", __func__);
 797                return ret;
 798        }
 799        if (is_ab8500(di->parent)) {
 800                ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
 801                                AB8500_USB_LINE_STAT_REG, &val);
 802        } else {
 803                if (is_ab9540(di->parent) || is_ab8505(di->parent))
 804                        ret = abx500_get_register_interruptible(di->dev,
 805                                AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
 806        }
 807        if (ret < 0) {
 808                dev_err(di->dev, "%s ab8500 read failed\n", __func__);
 809                return ret;
 810        }
 811
 812        /* get the USB type */
 813        val = (val & AB8500_USB_LINK_STATUS) >> 3;
 814        ret = ab8500_charger_max_usb_curr(di,
 815                (enum ab8500_charger_link_status) val);
 816
 817        return ret;
 818}
 819
 820/**
 821 * ab8500_charger_detect_usb_type() - get the type of usb connected
 822 * @di:         pointer to the ab8500_charger structure
 823 *
 824 * Detect the type of the plugged USB
 825 * Returns error code in case of failure else 0 on success
 826 */
 827static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
 828{
 829        int i, ret;
 830        u8 val;
 831
 832        /*
 833         * On getting the VBUS rising edge detect interrupt there
 834         * is a 250ms delay after which the register UsbLineStatus
 835         * is filled with valid data.
 836         */
 837        for (i = 0; i < 10; i++) {
 838                msleep(250);
 839                ret = abx500_get_register_interruptible(di->dev,
 840                        AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
 841                        &val);
 842                dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
 843                        __func__, val);
 844                if (ret < 0) {
 845                        dev_err(di->dev, "%s ab8500 read failed\n", __func__);
 846                        return ret;
 847                }
 848
 849                if (is_ab8500(di->parent))
 850                        ret = abx500_get_register_interruptible(di->dev,
 851                                AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
 852                else
 853                        ret = abx500_get_register_interruptible(di->dev,
 854                                AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
 855                if (ret < 0) {
 856                        dev_err(di->dev, "%s ab8500 read failed\n", __func__);
 857                        return ret;
 858                }
 859                dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
 860                        val);
 861                /*
 862                 * Until the IT source register is read the UsbLineStatus
 863                 * register is not updated, hence doing the same
 864                 * Revisit this:
 865                 */
 866
 867                /* get the USB type */
 868                val = (val & AB8500_USB_LINK_STATUS) >> 3;
 869                if (val)
 870                        break;
 871        }
 872        ret = ab8500_charger_max_usb_curr(di,
 873                (enum ab8500_charger_link_status) val);
 874
 875        return ret;
 876}
 877
 878/*
 879 * This array maps the raw hex value to charger voltage used by the AB8500
 880 * Values taken from the UM0836
 881 */
 882static int ab8500_charger_voltage_map[] = {
 883        3500 ,
 884        3525 ,
 885        3550 ,
 886        3575 ,
 887        3600 ,
 888        3625 ,
 889        3650 ,
 890        3675 ,
 891        3700 ,
 892        3725 ,
 893        3750 ,
 894        3775 ,
 895        3800 ,
 896        3825 ,
 897        3850 ,
 898        3875 ,
 899        3900 ,
 900        3925 ,
 901        3950 ,
 902        3975 ,
 903        4000 ,
 904        4025 ,
 905        4050 ,
 906        4060 ,
 907        4070 ,
 908        4080 ,
 909        4090 ,
 910        4100 ,
 911        4110 ,
 912        4120 ,
 913        4130 ,
 914        4140 ,
 915        4150 ,
 916        4160 ,
 917        4170 ,
 918        4180 ,
 919        4190 ,
 920        4200 ,
 921        4210 ,
 922        4220 ,
 923        4230 ,
 924        4240 ,
 925        4250 ,
 926        4260 ,
 927        4270 ,
 928        4280 ,
 929        4290 ,
 930        4300 ,
 931        4310 ,
 932        4320 ,
 933        4330 ,
 934        4340 ,
 935        4350 ,
 936        4360 ,
 937        4370 ,
 938        4380 ,
 939        4390 ,
 940        4400 ,
 941        4410 ,
 942        4420 ,
 943        4430 ,
 944        4440 ,
 945        4450 ,
 946        4460 ,
 947        4470 ,
 948        4480 ,
 949        4490 ,
 950        4500 ,
 951        4510 ,
 952        4520 ,
 953        4530 ,
 954        4540 ,
 955        4550 ,
 956        4560 ,
 957        4570 ,
 958        4580 ,
 959        4590 ,
 960        4600 ,
 961};
 962
 963/*
 964 * This array maps the raw hex value to charger current used by the AB8500
 965 * Values taken from the UM0836
 966 */
 967static int ab8500_charger_current_map[] = {
 968        100 ,
 969        200 ,
 970        300 ,
 971        400 ,
 972        500 ,
 973        600 ,
 974        700 ,
 975        800 ,
 976        900 ,
 977        1000 ,
 978        1100 ,
 979        1200 ,
 980        1300 ,
 981        1400 ,
 982        1500 ,
 983};
 984
 985/*
 986 * This array maps the raw hex value to VBUS input current used by the AB8500
 987 * Values taken from the UM0836
 988 */
 989static int ab8500_charger_vbus_in_curr_map[] = {
 990        USB_CH_IP_CUR_LVL_0P05,
 991        USB_CH_IP_CUR_LVL_0P09,
 992        USB_CH_IP_CUR_LVL_0P19,
 993        USB_CH_IP_CUR_LVL_0P29,
 994        USB_CH_IP_CUR_LVL_0P38,
 995        USB_CH_IP_CUR_LVL_0P45,
 996        USB_CH_IP_CUR_LVL_0P5,
 997        USB_CH_IP_CUR_LVL_0P6,
 998        USB_CH_IP_CUR_LVL_0P7,
 999        USB_CH_IP_CUR_LVL_0P8,
1000        USB_CH_IP_CUR_LVL_0P9,
1001        USB_CH_IP_CUR_LVL_1P0,
1002        USB_CH_IP_CUR_LVL_1P1,
1003        USB_CH_IP_CUR_LVL_1P3,
1004        USB_CH_IP_CUR_LVL_1P4,
1005        USB_CH_IP_CUR_LVL_1P5,
1006};
1007
1008static int ab8500_voltage_to_regval(int voltage)
1009{
1010        int i;
1011
1012        /* Special case for voltage below 3.5V */
1013        if (voltage < ab8500_charger_voltage_map[0])
1014                return LOW_VOLT_REG;
1015
1016        for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
1017                if (voltage < ab8500_charger_voltage_map[i])
1018                        return i - 1;
1019        }
1020
1021        /* If not last element, return error */
1022        i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
1023        if (voltage == ab8500_charger_voltage_map[i])
1024                return i;
1025        else
1026                return -1;
1027}
1028
1029static int ab8500_current_to_regval(int curr)
1030{
1031        int i;
1032
1033        if (curr < ab8500_charger_current_map[0])
1034                return 0;
1035
1036        for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) {
1037                if (curr < ab8500_charger_current_map[i])
1038                        return i - 1;
1039        }
1040
1041        /* If not last element, return error */
1042        i = ARRAY_SIZE(ab8500_charger_current_map) - 1;
1043        if (curr == ab8500_charger_current_map[i])
1044                return i;
1045        else
1046                return -1;
1047}
1048
1049static int ab8500_vbus_in_curr_to_regval(int curr)
1050{
1051        int i;
1052
1053        if (curr < ab8500_charger_vbus_in_curr_map[0])
1054                return 0;
1055
1056        for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) {
1057                if (curr < ab8500_charger_vbus_in_curr_map[i])
1058                        return i - 1;
1059        }
1060
1061        /* If not last element, return error */
1062        i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1;
1063        if (curr == ab8500_charger_vbus_in_curr_map[i])
1064                return i;
1065        else
1066                return -1;
1067}
1068
1069/**
1070 * ab8500_charger_get_usb_cur() - get usb current
1071 * @di:         pointer to the ab8500_charger structre
1072 *
1073 * The usb stack provides the maximum current that can be drawn from
1074 * the standard usb host. This will be in mA.
1075 * This function converts current in mA to a value that can be written
1076 * to the register. Returns -1 if charging is not allowed
1077 */
1078static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1079{
1080        switch (di->usb_state.usb_current) {
1081        case 100:
1082                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
1083                break;
1084        case 200:
1085                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19;
1086                break;
1087        case 300:
1088                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29;
1089                break;
1090        case 400:
1091                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38;
1092                break;
1093        case 500:
1094                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
1095                break;
1096        default:
1097                di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
1098                return -1;
1099                break;
1100        };
1101        return 0;
1102}
1103
1104/**
1105 * ab8500_charger_set_current() - set charger current
1106 * @di:         pointer to the ab8500_charger structure
1107 * @ich:        charger current, in mA
1108 * @reg:        select what charger register to set
1109 *
1110 * Set charger current.
1111 * There is no state machine in the AB to step up/down the charger
1112 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1113 * charging is started. Instead we need to implement
1114 * this charger current step-up/down here.
1115 * Returns error code in case of failure else 0(on success)
1116 */
1117static int ab8500_charger_set_current(struct ab8500_charger *di,
1118        int ich, int reg)
1119{
1120        int ret = 0;
1121        int auto_curr_index, curr_index, prev_curr_index, shift_value, i;
1122        u8 reg_value;
1123        u32 step_udelay;
1124        bool no_stepping = false;
1125
1126        atomic_inc(&di->current_stepping_sessions);
1127
1128        ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1129                reg, &reg_value);
1130        if (ret < 0) {
1131                dev_err(di->dev, "%s read failed\n", __func__);
1132                goto exit_set_current;
1133        }
1134
1135        switch (reg) {
1136        case AB8500_MCH_IPT_CURLVL_REG:
1137                shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1138                prev_curr_index = (reg_value >> shift_value);
1139                curr_index = ab8500_current_to_regval(ich);
1140                step_udelay = STEP_UDELAY;
1141                if (!di->ac.charger_connected)
1142                        no_stepping = true;
1143                break;
1144        case AB8500_USBCH_IPT_CRNTLVL_REG:
1145                shift_value = VBUS_IN_CURR_LIM_SHIFT;
1146                prev_curr_index = (reg_value >> shift_value);
1147                curr_index = ab8500_vbus_in_curr_to_regval(ich);
1148                step_udelay = STEP_UDELAY * 100;
1149
1150                ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1151                                        AB8500_CH_USBCH_STAT2_REG, &reg_value);
1152                if (ret < 0) {
1153                        dev_err(di->dev, "%s read failed\n", __func__);
1154                        goto exit_set_current;
1155                }
1156                auto_curr_index =
1157                        reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT;
1158
1159                dev_dbg(di->dev, "%s Auto VBUS curr is %d mA\n",
1160                        __func__,
1161                        ab8500_charger_vbus_in_curr_map[auto_curr_index]);
1162
1163                prev_curr_index = min(prev_curr_index, auto_curr_index);
1164
1165                if (!di->usb.charger_connected)
1166                        no_stepping = true;
1167                break;
1168        case AB8500_CH_OPT_CRNTLVL_REG:
1169                shift_value = 0;
1170                prev_curr_index = (reg_value >> shift_value);
1171                curr_index = ab8500_current_to_regval(ich);
1172                step_udelay = STEP_UDELAY;
1173                if (curr_index && (curr_index - prev_curr_index) > 1)
1174                        step_udelay *= 100;
1175
1176                if (!di->usb.charger_connected && !di->ac.charger_connected)
1177                        no_stepping = true;
1178
1179                break;
1180        default:
1181                dev_err(di->dev, "%s current register not valid\n", __func__);
1182                ret = -ENXIO;
1183                goto exit_set_current;
1184        }
1185
1186        if (curr_index < 0) {
1187                dev_err(di->dev, "requested current limit out-of-range\n");
1188                ret = -ENXIO;
1189                goto exit_set_current;
1190        }
1191
1192        /* only update current if it's been changed */
1193        if (prev_curr_index == curr_index) {
1194                dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1195                        __func__, reg);
1196                ret = 0;
1197                goto exit_set_current;
1198        }
1199
1200        dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1201                __func__, ich, reg);
1202
1203        if (no_stepping) {
1204                ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1205                                        reg, (u8)curr_index << shift_value);
1206                if (ret)
1207                        dev_err(di->dev, "%s write failed\n", __func__);
1208        } else if (prev_curr_index > curr_index) {
1209                for (i = prev_curr_index - 1; i >= curr_index; i--) {
1210                        dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1211                                (u8) i << shift_value, reg);
1212                        ret = abx500_set_register_interruptible(di->dev,
1213                                AB8500_CHARGER, reg, (u8)i << shift_value);
1214                        if (ret) {
1215                                dev_err(di->dev, "%s write failed\n", __func__);
1216                                goto exit_set_current;
1217                        }
1218                        if (i != curr_index)
1219                                usleep_range(step_udelay, step_udelay * 2);
1220                }
1221        } else {
1222                for (i = prev_curr_index + 1; i <= curr_index; i++) {
1223                        dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1224                                (u8)i << shift_value, reg);
1225                        ret = abx500_set_register_interruptible(di->dev,
1226                                AB8500_CHARGER, reg, (u8)i << shift_value);
1227                        if (ret) {
1228                                dev_err(di->dev, "%s write failed\n", __func__);
1229                                goto exit_set_current;
1230                        }
1231                        if (i != curr_index)
1232                                usleep_range(step_udelay, step_udelay * 2);
1233                }
1234        }
1235
1236exit_set_current:
1237        atomic_dec(&di->current_stepping_sessions);
1238
1239        return ret;
1240}
1241
1242/**
1243 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1244 * @di:         pointer to the ab8500_charger structure
1245 * @ich_in:     charger input current limit
1246 *
1247 * Sets the current that can be drawn from the USB host
1248 * Returns error code in case of failure else 0(on success)
1249 */
1250static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1251                int ich_in)
1252{
1253        int min_value;
1254        int ret;
1255
1256        /* We should always use to lowest current limit */
1257        min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
1258
1259        switch (min_value) {
1260        case 100:
1261                if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1262                        min_value = USB_CH_IP_CUR_LVL_0P05;
1263                break;
1264        case 500:
1265                if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1266                        min_value = USB_CH_IP_CUR_LVL_0P45;
1267                break;
1268        default:
1269                break;
1270        }
1271
1272        dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
1273
1274        mutex_lock(&di->usb_ipt_crnt_lock);
1275        ret = ab8500_charger_set_current(di, min_value,
1276                AB8500_USBCH_IPT_CRNTLVL_REG);
1277        mutex_unlock(&di->usb_ipt_crnt_lock);
1278
1279        return ret;
1280}
1281
1282/**
1283 * ab8500_charger_set_main_in_curr() - set main charger input current
1284 * @di:         pointer to the ab8500_charger structure
1285 * @ich_in:     input charger current, in mA
1286 *
1287 * Set main charger input current.
1288 * Returns error code in case of failure else 0(on success)
1289 */
1290static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1291        int ich_in)
1292{
1293        return ab8500_charger_set_current(di, ich_in,
1294                AB8500_MCH_IPT_CURLVL_REG);
1295}
1296
1297/**
1298 * ab8500_charger_set_output_curr() - set charger output current
1299 * @di:         pointer to the ab8500_charger structure
1300 * @ich_out:    output charger current, in mA
1301 *
1302 * Set charger output current.
1303 * Returns error code in case of failure else 0(on success)
1304 */
1305static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1306        int ich_out)
1307{
1308        return ab8500_charger_set_current(di, ich_out,
1309                AB8500_CH_OPT_CRNTLVL_REG);
1310}
1311
1312/**
1313 * ab8500_charger_led_en() - turn on/off chargign led
1314 * @di:         pointer to the ab8500_charger structure
1315 * @on:         flag to turn on/off the chargign led
1316 *
1317 * Power ON/OFF charging LED indication
1318 * Returns error code in case of failure else 0(on success)
1319 */
1320static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1321{
1322        int ret;
1323
1324        if (on) {
1325                /* Power ON charging LED indicator, set LED current to 5mA */
1326                ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1327                        AB8500_LED_INDICATOR_PWM_CTRL,
1328                        (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1329                if (ret) {
1330                        dev_err(di->dev, "Power ON LED failed\n");
1331                        return ret;
1332                }
1333                /* LED indicator PWM duty cycle 252/256 */
1334                ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1335                        AB8500_LED_INDICATOR_PWM_DUTY,
1336                        LED_INDICATOR_PWM_DUTY_252_256);
1337                if (ret) {
1338                        dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1339                        return ret;
1340                }
1341        } else {
1342                /* Power off charging LED indicator */
1343                ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1344                        AB8500_LED_INDICATOR_PWM_CTRL,
1345                        LED_INDICATOR_PWM_DIS);
1346                if (ret) {
1347                        dev_err(di->dev, "Power-off LED failed\n");
1348                        return ret;
1349                }
1350        }
1351
1352        return ret;
1353}
1354
1355/**
1356 * ab8500_charger_ac_en() - enable or disable ac charging
1357 * @di:         pointer to the ab8500_charger structure
1358 * @enable:     enable/disable flag
1359 * @vset:       charging voltage
1360 * @iset:       charging current
1361 *
1362 * Enable/Disable AC/Mains charging and turns on/off the charging led
1363 * respectively.
1364 **/
1365static int ab8500_charger_ac_en(struct ux500_charger *charger,
1366        int enable, int vset, int iset)
1367{
1368        int ret;
1369        int volt_index;
1370        int curr_index;
1371        int input_curr_index;
1372        u8 overshoot = 0;
1373
1374        struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1375
1376        if (enable) {
1377                /* Check if AC is connected */
1378                if (!di->ac.charger_connected) {
1379                        dev_err(di->dev, "AC charger not connected\n");
1380                        return -ENXIO;
1381                }
1382
1383                /* Enable AC charging */
1384                dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1385
1386                /*
1387                 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1388                 * will be triggered everytime we enable the VDD ADC supply.
1389                 * This will turn off charging for a short while.
1390                 * It can be avoided by having the supply on when
1391                 * there is a charger enabled. Normally the VDD ADC supply
1392                 * is enabled everytime a GPADC conversion is triggered. We will
1393                 * force it to be enabled from this driver to have
1394                 * the GPADC module independant of the AB8500 chargers
1395                 */
1396                if (!di->vddadc_en_ac) {
1397                        regulator_enable(di->regu);
1398                        di->vddadc_en_ac = true;
1399                }
1400
1401                /* Check if the requested voltage or current is valid */
1402                volt_index = ab8500_voltage_to_regval(vset);
1403                curr_index = ab8500_current_to_regval(iset);
1404                input_curr_index = ab8500_current_to_regval(
1405                        di->bm->chg_params->ac_curr_max);
1406                if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1407                        dev_err(di->dev,
1408                                "Charger voltage or current too high, "
1409                                "charging not started\n");
1410                        return -ENXIO;
1411                }
1412
1413                /* ChVoltLevel: maximum battery charging voltage */
1414                ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1415                        AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1416                if (ret) {
1417                        dev_err(di->dev, "%s write failed\n", __func__);
1418                        return ret;
1419                }
1420                /* MainChInputCurr: current that can be drawn from the charger*/
1421                ret = ab8500_charger_set_main_in_curr(di,
1422                        di->bm->chg_params->ac_curr_max);
1423                if (ret) {
1424                        dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1425                                __func__);
1426                        return ret;
1427                }
1428                /* ChOutputCurentLevel: protected output current */
1429                ret = ab8500_charger_set_output_curr(di, iset);
1430                if (ret) {
1431                        dev_err(di->dev, "%s "
1432                                "Failed to set ChOutputCurentLevel\n",
1433                                __func__);
1434                        return ret;
1435                }
1436
1437                /* Check if VBAT overshoot control should be enabled */
1438                if (!di->bm->enable_overshoot)
1439                        overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1440
1441                /* Enable Main Charger */
1442                ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1443                        AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1444                if (ret) {
1445                        dev_err(di->dev, "%s write failed\n", __func__);
1446                        return ret;
1447                }
1448
1449                /* Power on charging LED indication */
1450                ret = ab8500_charger_led_en(di, true);
1451                if (ret < 0)
1452                        dev_err(di->dev, "failed to enable LED\n");
1453
1454                di->ac.charger_online = 1;
1455        } else {
1456                /* Disable AC charging */
1457                if (is_ab8500_1p1_or_earlier(di->parent)) {
1458                        /*
1459                         * For ABB revision 1.0 and 1.1 there is a bug in the
1460                         * watchdog logic. That means we have to continously
1461                         * kick the charger watchdog even when no charger is
1462                         * connected. This is only valid once the AC charger
1463                         * has been enabled. This is a bug that is not handled
1464                         * by the algorithm and the watchdog have to be kicked
1465                         * by the charger driver when the AC charger
1466                         * is disabled
1467                         */
1468                        if (di->ac_conn) {
1469                                queue_delayed_work(di->charger_wq,
1470                                        &di->kick_wd_work,
1471                                        round_jiffies(WD_KICK_INTERVAL));
1472                        }
1473
1474                        /*
1475                         * We can't turn off charging completely
1476                         * due to a bug in AB8500 cut1.
1477                         * If we do, charging will not start again.
1478                         * That is why we set the lowest voltage
1479                         * and current possible
1480                         */
1481                        ret = abx500_set_register_interruptible(di->dev,
1482                                AB8500_CHARGER,
1483                                AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1484                        if (ret) {
1485                                dev_err(di->dev,
1486                                        "%s write failed\n", __func__);
1487                                return ret;
1488                        }
1489
1490                        ret = ab8500_charger_set_output_curr(di, 0);
1491                        if (ret) {
1492                                dev_err(di->dev, "%s "
1493                                        "Failed to set ChOutputCurentLevel\n",
1494                                        __func__);
1495                                return ret;
1496                        }
1497                } else {
1498                        ret = abx500_set_register_interruptible(di->dev,
1499                                AB8500_CHARGER,
1500                                AB8500_MCH_CTRL1, 0);
1501                        if (ret) {
1502                                dev_err(di->dev,
1503                                        "%s write failed\n", __func__);
1504                                return ret;
1505                        }
1506                }
1507
1508                ret = ab8500_charger_led_en(di, false);
1509                if (ret < 0)
1510                        dev_err(di->dev, "failed to disable LED\n");
1511
1512                di->ac.charger_online = 0;
1513                di->ac.wd_expired = false;
1514
1515                /* Disable regulator if enabled */
1516                if (di->vddadc_en_ac) {
1517                        regulator_disable(di->regu);
1518                        di->vddadc_en_ac = false;
1519                }
1520
1521                dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1522        }
1523        ab8500_power_supply_changed(di, &di->ac_chg.psy);
1524
1525        return ret;
1526}
1527
1528/**
1529 * ab8500_charger_usb_en() - enable usb charging
1530 * @di:         pointer to the ab8500_charger structure
1531 * @enable:     enable/disable flag
1532 * @vset:       charging voltage
1533 * @ich_out:    charger output current
1534 *
1535 * Enable/Disable USB charging and turns on/off the charging led respectively.
1536 * Returns error code in case of failure else 0(on success)
1537 */
1538static int ab8500_charger_usb_en(struct ux500_charger *charger,
1539        int enable, int vset, int ich_out)
1540{
1541        int ret;
1542        int volt_index;
1543        int curr_index;
1544        u8 overshoot = 0;
1545
1546        struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1547
1548        if (enable) {
1549                /* Check if USB is connected */
1550                if (!di->usb.charger_connected) {
1551                        dev_err(di->dev, "USB charger not connected\n");
1552                        return -ENXIO;
1553                }
1554
1555                /*
1556                 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1557                 * will be triggered everytime we enable the VDD ADC supply.
1558                 * This will turn off charging for a short while.
1559                 * It can be avoided by having the supply on when
1560                 * there is a charger enabled. Normally the VDD ADC supply
1561                 * is enabled everytime a GPADC conversion is triggered. We will
1562                 * force it to be enabled from this driver to have
1563                 * the GPADC module independant of the AB8500 chargers
1564                 */
1565                if (!di->vddadc_en_usb) {
1566                        regulator_enable(di->regu);
1567                        di->vddadc_en_usb = true;
1568                }
1569
1570                /* Enable USB charging */
1571                dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1572
1573                /* Check if the requested voltage or current is valid */
1574                volt_index = ab8500_voltage_to_regval(vset);
1575                curr_index = ab8500_current_to_regval(ich_out);
1576                if (volt_index < 0 || curr_index < 0) {
1577                        dev_err(di->dev,
1578                                "Charger voltage or current too high, "
1579                                "charging not started\n");
1580                        return -ENXIO;
1581                }
1582
1583                /* ChVoltLevel: max voltage upto which battery can be charged */
1584                ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1585                        AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1586                if (ret) {
1587                        dev_err(di->dev, "%s write failed\n", __func__);
1588                        return ret;
1589                }
1590                /* Check if VBAT overshoot control should be enabled */
1591                if (!di->bm->enable_overshoot)
1592                        overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1593
1594                /* Enable USB Charger */
1595                dev_dbg(di->dev,
1596                        "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
1597                ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1598                        AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1599                if (ret) {
1600                        dev_err(di->dev, "%s write failed\n", __func__);
1601                        return ret;
1602                }
1603
1604                /* If success power on charging LED indication */
1605                ret = ab8500_charger_led_en(di, true);
1606                if (ret < 0)
1607                        dev_err(di->dev, "failed to enable LED\n");
1608
1609                di->usb.charger_online = 1;
1610
1611                /* USBChInputCurr: current that can be drawn from the usb */
1612                ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1613                if (ret) {
1614                        dev_err(di->dev, "setting USBChInputCurr failed\n");
1615                        return ret;
1616                }
1617
1618                /* ChOutputCurentLevel: protected output current */
1619                ret = ab8500_charger_set_output_curr(di, ich_out);
1620                if (ret) {
1621                        dev_err(di->dev, "%s "
1622                                "Failed to set ChOutputCurentLevel\n",
1623                                __func__);
1624                        return ret;
1625                }
1626
1627                queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1628
1629        } else {
1630                /* Disable USB charging */
1631                dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1632                ret = abx500_set_register_interruptible(di->dev,
1633                        AB8500_CHARGER,
1634                        AB8500_USBCH_CTRL1_REG, 0);
1635                if (ret) {
1636                        dev_err(di->dev,
1637                                "%s write failed\n", __func__);
1638                        return ret;
1639                }
1640
1641                ret = ab8500_charger_led_en(di, false);
1642                if (ret < 0)
1643                        dev_err(di->dev, "failed to disable LED\n");
1644                /* USBChInputCurr: current that can be drawn from the usb */
1645                ret = ab8500_charger_set_vbus_in_curr(di, 0);
1646                if (ret) {
1647                        dev_err(di->dev, "setting USBChInputCurr failed\n");
1648                        return ret;
1649                }
1650
1651                /* ChOutputCurentLevel: protected output current */
1652                ret = ab8500_charger_set_output_curr(di, 0);
1653                if (ret) {
1654                        dev_err(di->dev, "%s "
1655                                "Failed to reset ChOutputCurentLevel\n",
1656                                __func__);
1657                        return ret;
1658                }
1659                di->usb.charger_online = 0;
1660                di->usb.wd_expired = false;
1661
1662                /* Disable regulator if enabled */
1663                if (di->vddadc_en_usb) {
1664                        regulator_disable(di->regu);
1665                        di->vddadc_en_usb = false;
1666                }
1667
1668                dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1669
1670                /* Cancel any pending Vbat check work */
1671                if (delayed_work_pending(&di->check_vbat_work))
1672                        cancel_delayed_work(&di->check_vbat_work);
1673
1674        }
1675        ab8500_power_supply_changed(di, &di->usb_chg.psy);
1676
1677        return ret;
1678}
1679
1680/**
1681 * ab8500_charger_watchdog_kick() - kick charger watchdog
1682 * @di:         pointer to the ab8500_charger structure
1683 *
1684 * Kick charger watchdog
1685 * Returns error code in case of failure else 0(on success)
1686 */
1687static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1688{
1689        int ret;
1690        struct ab8500_charger *di;
1691
1692        if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1693                di = to_ab8500_charger_ac_device_info(charger);
1694        else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1695                di = to_ab8500_charger_usb_device_info(charger);
1696        else
1697                return -ENXIO;
1698
1699        ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1700                AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1701        if (ret)
1702                dev_err(di->dev, "Failed to kick WD!\n");
1703
1704        return ret;
1705}
1706
1707/**
1708 * ab8500_charger_update_charger_current() - update charger current
1709 * @di:         pointer to the ab8500_charger structure
1710 *
1711 * Update the charger output current for the specified charger
1712 * Returns error code in case of failure else 0(on success)
1713 */
1714static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1715                int ich_out)
1716{
1717        int ret;
1718        struct ab8500_charger *di;
1719
1720        if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1721                di = to_ab8500_charger_ac_device_info(charger);
1722        else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1723                di = to_ab8500_charger_usb_device_info(charger);
1724        else
1725                return -ENXIO;
1726
1727        ret = ab8500_charger_set_output_curr(di, ich_out);
1728        if (ret) {
1729                dev_err(di->dev, "%s "
1730                        "Failed to set ChOutputCurentLevel\n",
1731                        __func__);
1732                return ret;
1733        }
1734
1735        /* Reset the main and usb drop input current measurement counter */
1736        ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1737                                AB8500_CHARGER_CTRL,
1738                                0x1);
1739        if (ret) {
1740                dev_err(di->dev, "%s write failed\n", __func__);
1741                return ret;
1742        }
1743
1744        return ret;
1745}
1746
1747static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1748{
1749        struct power_supply *psy;
1750        struct power_supply *ext;
1751        struct ab8500_charger *di;
1752        union power_supply_propval ret;
1753        int i, j;
1754        bool psy_found = false;
1755        struct ux500_charger *usb_chg;
1756
1757        usb_chg = (struct ux500_charger *)data;
1758        psy = &usb_chg->psy;
1759
1760        di = to_ab8500_charger_usb_device_info(usb_chg);
1761
1762        ext = dev_get_drvdata(dev);
1763
1764        /* For all psy where the driver name appears in any supplied_to */
1765        for (i = 0; i < ext->num_supplicants; i++) {
1766                if (!strcmp(ext->supplied_to[i], psy->name))
1767                        psy_found = true;
1768        }
1769
1770        if (!psy_found)
1771                return 0;
1772
1773        /* Go through all properties for the psy */
1774        for (j = 0; j < ext->num_properties; j++) {
1775                enum power_supply_property prop;
1776                prop = ext->properties[j];
1777
1778                if (ext->get_property(ext, prop, &ret))
1779                        continue;
1780
1781                switch (prop) {
1782                case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1783                        switch (ext->type) {
1784                        case POWER_SUPPLY_TYPE_BATTERY:
1785                                di->vbat = ret.intval / 1000;
1786                                break;
1787                        default:
1788                                break;
1789                        }
1790                        break;
1791                default:
1792                        break;
1793                }
1794        }
1795        return 0;
1796}
1797
1798/**
1799 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1800 * @work        pointer to the work_struct structure
1801 *
1802 * Due to a asic bug it is necessary to lower the input current to the vbus
1803 * charger when charging with at some specific levels. This issue is only valid
1804 * for below a certain battery voltage. This function makes sure that the
1805 * the allowed current limit isn't exceeded.
1806 */
1807static void ab8500_charger_check_vbat_work(struct work_struct *work)
1808{
1809        int t = 10;
1810        struct ab8500_charger *di = container_of(work,
1811                struct ab8500_charger, check_vbat_work.work);
1812
1813        class_for_each_device(power_supply_class, NULL,
1814                &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1815
1816        /* First run old_vbat is 0. */
1817        if (di->old_vbat == 0)
1818                di->old_vbat = di->vbat;
1819
1820        if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1821                di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1822                (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1823                di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1824
1825                dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1826                        " old: %d\n", di->max_usb_in_curr, di->vbat,
1827                        di->old_vbat);
1828                ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1829                power_supply_changed(&di->usb_chg.psy);
1830        }
1831
1832        di->old_vbat = di->vbat;
1833
1834        /*
1835         * No need to check the battery voltage every second when not close to
1836         * the threshold.
1837         */
1838        if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1839                (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1840                        t = 1;
1841
1842        queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1843}
1844
1845/**
1846 * ab8500_charger_check_hw_failure_work() - check main charger failure
1847 * @work:       pointer to the work_struct structure
1848 *
1849 * Work queue function for checking the main charger status
1850 */
1851static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1852{
1853        int ret;
1854        u8 reg_value;
1855
1856        struct ab8500_charger *di = container_of(work,
1857                struct ab8500_charger, check_hw_failure_work.work);
1858
1859        /* Check if the status bits for HW failure is still active */
1860        if (di->flags.mainextchnotok) {
1861                ret = abx500_get_register_interruptible(di->dev,
1862                        AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
1863                if (ret < 0) {
1864                        dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1865                        return;
1866                }
1867                if (!(reg_value & MAIN_CH_NOK)) {
1868                        di->flags.mainextchnotok = false;
1869                        ab8500_power_supply_changed(di, &di->ac_chg.psy);
1870                }
1871        }
1872        if (di->flags.vbus_ovv) {
1873                ret = abx500_get_register_interruptible(di->dev,
1874                        AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
1875                        &reg_value);
1876                if (ret < 0) {
1877                        dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1878                        return;
1879                }
1880                if (!(reg_value & VBUS_OVV_TH)) {
1881                        di->flags.vbus_ovv = false;
1882                        ab8500_power_supply_changed(di, &di->usb_chg.psy);
1883                }
1884        }
1885        /* If we still have a failure, schedule a new check */
1886        if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
1887                queue_delayed_work(di->charger_wq,
1888                        &di->check_hw_failure_work, round_jiffies(HZ));
1889        }
1890}
1891
1892/**
1893 * ab8500_charger_kick_watchdog_work() - kick the watchdog
1894 * @work:       pointer to the work_struct structure
1895 *
1896 * Work queue function for kicking the charger watchdog.
1897 *
1898 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
1899 * logic. That means we have to continously kick the charger
1900 * watchdog even when no charger is connected. This is only
1901 * valid once the AC charger has been enabled. This is
1902 * a bug that is not handled by the algorithm and the
1903 * watchdog have to be kicked by the charger driver
1904 * when the AC charger is disabled
1905 */
1906static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
1907{
1908        int ret;
1909
1910        struct ab8500_charger *di = container_of(work,
1911                struct ab8500_charger, kick_wd_work.work);
1912
1913        ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1914                AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1915        if (ret)
1916                dev_err(di->dev, "Failed to kick WD!\n");
1917
1918        /* Schedule a new watchdog kick */
1919        queue_delayed_work(di->charger_wq,
1920                &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
1921}
1922
1923/**
1924 * ab8500_charger_ac_work() - work to get and set main charger status
1925 * @work:       pointer to the work_struct structure
1926 *
1927 * Work queue function for checking the main charger status
1928 */
1929static void ab8500_charger_ac_work(struct work_struct *work)
1930{
1931        int ret;
1932
1933        struct ab8500_charger *di = container_of(work,
1934                struct ab8500_charger, ac_work);
1935
1936        /*
1937         * Since we can't be sure that the events are received
1938         * synchronously, we have the check if the main charger is
1939         * connected by reading the status register
1940         */
1941        ret = ab8500_charger_detect_chargers(di, false);
1942        if (ret < 0)
1943                return;
1944
1945        if (ret & AC_PW_CONN) {
1946                di->ac.charger_connected = 1;
1947                di->ac_conn = true;
1948        } else {
1949                di->ac.charger_connected = 0;
1950        }
1951
1952        ab8500_power_supply_changed(di, &di->ac_chg.psy);
1953        sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
1954}
1955
1956static void ab8500_charger_usb_attached_work(struct work_struct *work)
1957{
1958        struct ab8500_charger *di = container_of(work,
1959                                                 struct ab8500_charger,
1960                                                 usb_charger_attached_work.work);
1961        int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
1962        int ret, i;
1963        u8 statval;
1964
1965        for (i = 0; i < 10; i++) {
1966                ret = abx500_get_register_interruptible(di->dev,
1967                                                        AB8500_CHARGER,
1968                                                        AB8500_CH_USBCH_STAT1_REG,
1969                                                        &statval);
1970                if (ret < 0) {
1971                        dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1972                        goto reschedule;
1973                }
1974                if ((statval & usbch) != usbch)
1975                        goto reschedule;
1976
1977                msleep(CHARGER_STATUS_POLL);
1978        }
1979
1980        ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
1981
1982        mutex_lock(&di->charger_attached_mutex);
1983        mutex_unlock(&di->charger_attached_mutex);
1984
1985        return;
1986
1987reschedule:
1988        queue_delayed_work(di->charger_wq,
1989                           &di->usb_charger_attached_work,
1990                           HZ);
1991}
1992
1993static void ab8500_charger_ac_attached_work(struct work_struct *work)
1994{
1995
1996        struct ab8500_charger *di = container_of(work,
1997                                                 struct ab8500_charger,
1998                                                 ac_charger_attached_work.work);
1999        int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2000                      MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2001        int ret, i;
2002        u8 statval;
2003
2004        for (i = 0; i < 10; i++) {
2005                ret = abx500_get_register_interruptible(di->dev,
2006                                                        AB8500_CHARGER,
2007                                                        AB8500_CH_STATUS2_REG,
2008                                                        &statval);
2009                if (ret < 0) {
2010                        dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2011                        goto reschedule;
2012                }
2013
2014                if ((statval & mainch) != mainch)
2015                        goto reschedule;
2016
2017                msleep(CHARGER_STATUS_POLL);
2018        }
2019
2020        ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2021        queue_work(di->charger_wq, &di->ac_work);
2022
2023        mutex_lock(&di->charger_attached_mutex);
2024        mutex_unlock(&di->charger_attached_mutex);
2025
2026        return;
2027
2028reschedule:
2029        queue_delayed_work(di->charger_wq,
2030                           &di->ac_charger_attached_work,
2031                           HZ);
2032}
2033
2034/**
2035 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2036 * @work:       Pointer to the work_struct structure
2037 *
2038 * Detect the type of USB plugged
2039 */
2040static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
2041{
2042        int ret;
2043
2044        struct ab8500_charger *di = container_of(work,
2045                struct ab8500_charger, detect_usb_type_work);
2046
2047        /*
2048         * Since we can't be sure that the events are received
2049         * synchronously, we have the check if is
2050         * connected by reading the status register
2051         */
2052        ret = ab8500_charger_detect_chargers(di, false);
2053        if (ret < 0)
2054                return;
2055
2056        if (!(ret & USB_PW_CONN)) {
2057                dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2058                di->vbus_detected = false;
2059                ab8500_charger_set_usb_connected(di, false);
2060                ab8500_power_supply_changed(di, &di->usb_chg.psy);
2061        } else {
2062                dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2063                di->vbus_detected = true;
2064
2065                if (is_ab8500_1p1_or_earlier(di->parent)) {
2066                        ret = ab8500_charger_detect_usb_type(di);
2067                        if (!ret) {
2068                                ab8500_charger_set_usb_connected(di, true);
2069                                ab8500_power_supply_changed(di,
2070                                                            &di->usb_chg.psy);
2071                        }
2072                } else {
2073                        /*
2074                         * For ABB cut2.0 and onwards we have an IRQ,
2075                         * USB_LINK_STATUS that will be triggered when the USB
2076                         * link status changes. The exception is USB connected
2077                         * during startup. Then we don't get a
2078                         * USB_LINK_STATUS IRQ
2079                         */
2080                        if (di->vbus_detected_start) {
2081                                di->vbus_detected_start = false;
2082                                ret = ab8500_charger_detect_usb_type(di);
2083                                if (!ret) {
2084                                        ab8500_charger_set_usb_connected(di,
2085                                                true);
2086                                        ab8500_power_supply_changed(di,
2087                                                &di->usb_chg.psy);
2088                                }
2089                        }
2090                }
2091        }
2092}
2093
2094/**
2095 * ab8500_charger_usb_link_attach_work() - work to detect USB type
2096 * @work:       pointer to the work_struct structure
2097 *
2098 * Detect the type of USB plugged
2099 */
2100static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2101{
2102        struct ab8500_charger *di =
2103                container_of(work, struct ab8500_charger, attach_work.work);
2104        int ret;
2105
2106        /* Update maximum input current if USB enumeration is not detected */
2107        if (!di->usb.charger_online) {
2108                ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
2109                if (ret)
2110                        return;
2111        }
2112
2113        ab8500_charger_set_usb_connected(di, true);
2114        ab8500_power_supply_changed(di, &di->usb_chg.psy);
2115}
2116
2117/**
2118 * ab8500_charger_usb_link_status_work() - work to detect USB type
2119 * @work:       pointer to the work_struct structure
2120 *
2121 * Detect the type of USB plugged
2122 */
2123static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2124{
2125        int detected_chargers;
2126        int ret;
2127        u8 val;
2128
2129        struct ab8500_charger *di = container_of(work,
2130                struct ab8500_charger, usb_link_status_work);
2131
2132        /*
2133         * Since we can't be sure that the events are received
2134         * synchronously, we have the check if  is
2135         * connected by reading the status register
2136         */
2137        detected_chargers = ab8500_charger_detect_chargers(di, false);
2138        if (detected_chargers < 0)
2139                return;
2140
2141        /*
2142         * Some chargers that breaks the USB spec is
2143         * identified as invalid by AB8500 and it refuse
2144         * to start the charging process. but by jumping
2145         * thru a few hoops it can be forced to start.
2146         */
2147        ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2148                        AB8500_USB_LINE_STAT_REG, &val);
2149        if (ret >= 0)
2150                dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2151        else
2152                dev_dbg(di->dev, "Error reading USB link status\n");
2153
2154        if (detected_chargers & USB_PW_CONN) {
2155                if (((val & AB8500_USB_LINK_STATUS) >> 3) == USB_STAT_NOT_VALID_LINK &&
2156                                di->invalid_charger_detect_state == 0) {
2157                        dev_dbg(di->dev, "Invalid charger detected, state= 0\n");
2158                        /*Enable charger*/
2159                        abx500_mask_and_set_register_interruptible(di->dev,
2160                                        AB8500_CHARGER, AB8500_USBCH_CTRL1_REG, 0x01, 0x01);
2161                        /*Enable charger detection*/
2162                        abx500_mask_and_set_register_interruptible(di->dev, AB8500_USB,
2163                                        AB8500_MCH_IPT_CURLVL_REG, 0x01, 0x01);
2164                        di->invalid_charger_detect_state = 1;
2165                        /*exit and wait for new link status interrupt.*/
2166                        return;
2167
2168                }
2169                if (di->invalid_charger_detect_state == 1) {
2170                        dev_dbg(di->dev, "Invalid charger detected, state= 1\n");
2171                        /*Stop charger detection*/
2172                        abx500_mask_and_set_register_interruptible(di->dev, AB8500_USB,
2173                                        AB8500_MCH_IPT_CURLVL_REG, 0x01, 0x00);
2174                        /*Check link status*/
2175                        ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2176                                        AB8500_USB_LINE_STAT_REG, &val);
2177                        dev_dbg(di->dev, "USB link status= 0x%02x\n",
2178                                        (val & AB8500_USB_LINK_STATUS) >> 3);
2179                        di->invalid_charger_detect_state = 2;
2180                }
2181        } else {
2182                di->invalid_charger_detect_state = 0;
2183        }
2184
2185        if (!(detected_chargers & USB_PW_CONN)) {
2186                di->vbus_detected = false;
2187                ab8500_charger_set_usb_connected(di, false);
2188                ab8500_power_supply_changed(di, &di->usb_chg.psy);
2189                return;
2190        }
2191
2192        dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2193        di->vbus_detected = true;
2194        ret = ab8500_charger_read_usb_type(di);
2195        if (ret) {
2196                if (ret == -ENXIO) {
2197                        /* No valid charger type detected */
2198                        ab8500_charger_set_usb_connected(di, false);
2199                        ab8500_power_supply_changed(di, &di->usb_chg.psy);
2200                }
2201                return;
2202        }
2203
2204        if (di->usb_device_is_unrecognised) {
2205                dev_dbg(di->dev,
2206                        "Potential Legacy Charger device. "
2207                        "Delay work for %d msec for USB enum "
2208                        "to finish",
2209                        WAIT_ACA_RID_ENUMERATION);
2210                queue_delayed_work(di->charger_wq,
2211                                   &di->attach_work,
2212                                   msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2213        } else if (di->is_aca_rid == 1) {
2214                /* Only wait once */
2215                di->is_aca_rid++;
2216                dev_dbg(di->dev,
2217                        "%s Wait %d msec for USB enum to finish",
2218                        __func__, WAIT_ACA_RID_ENUMERATION);
2219                queue_delayed_work(di->charger_wq,
2220                                   &di->attach_work,
2221                                   msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2222        } else {
2223                queue_delayed_work(di->charger_wq,
2224                                   &di->attach_work,
2225                                   0);
2226        }
2227}
2228
2229static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2230{
2231        int ret;
2232        unsigned long flags;
2233
2234        struct ab8500_charger *di = container_of(work,
2235                struct ab8500_charger, usb_state_changed_work.work);
2236
2237        if (!di->vbus_detected) {
2238                dev_dbg(di->dev,
2239                        "%s !di->vbus_detected\n",
2240                        __func__);
2241                return;
2242        }
2243
2244        spin_lock_irqsave(&di->usb_state.usb_lock, flags);
2245        di->usb_state.state = di->usb_state.state_tmp;
2246        di->usb_state.usb_current = di->usb_state.usb_current_tmp;
2247        spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2248
2249        dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2250                __func__, di->usb_state.state, di->usb_state.usb_current);
2251
2252        switch (di->usb_state.state) {
2253        case AB8500_BM_USB_STATE_RESET_HS:
2254        case AB8500_BM_USB_STATE_RESET_FS:
2255        case AB8500_BM_USB_STATE_SUSPEND:
2256        case AB8500_BM_USB_STATE_MAX:
2257                ab8500_charger_set_usb_connected(di, false);
2258                ab8500_power_supply_changed(di, &di->usb_chg.psy);
2259                break;
2260
2261        case AB8500_BM_USB_STATE_RESUME:
2262                /*
2263                 * when suspend->resume there should be delay
2264                 * of 1sec for enabling charging
2265                 */
2266                msleep(1000);
2267                /* Intentional fall through */
2268        case AB8500_BM_USB_STATE_CONFIGURED:
2269                /*
2270                 * USB is configured, enable charging with the charging
2271                 * input current obtained from USB driver
2272                 */
2273                if (!ab8500_charger_get_usb_cur(di)) {
2274                        /* Update maximum input current */
2275                        ret = ab8500_charger_set_vbus_in_curr(di,
2276                                        di->max_usb_in_curr);
2277                        if (ret)
2278                                return;
2279
2280                        ab8500_charger_set_usb_connected(di, true);
2281                        ab8500_power_supply_changed(di, &di->usb_chg.psy);
2282                }
2283                break;
2284
2285        default:
2286                break;
2287        };
2288}
2289
2290/**
2291 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2292 * @work:       pointer to the work_struct structure
2293 *
2294 * Work queue function for checking the USB charger Not OK status
2295 */
2296static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2297{
2298        int ret;
2299        u8 reg_value;
2300        bool prev_status;
2301
2302        struct ab8500_charger *di = container_of(work,
2303                struct ab8500_charger, check_usbchgnotok_work.work);
2304
2305        /* Check if the status bit for usbchargernotok is still active */
2306        ret = abx500_get_register_interruptible(di->dev,
2307                AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2308        if (ret < 0) {
2309                dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2310                return;
2311        }
2312        prev_status = di->flags.usbchargernotok;
2313
2314        if (reg_value & VBUS_CH_NOK) {
2315                di->flags.usbchargernotok = true;
2316                /* Check again in 1sec */
2317                queue_delayed_work(di->charger_wq,
2318                        &di->check_usbchgnotok_work, HZ);
2319        } else {
2320                di->flags.usbchargernotok = false;
2321                di->flags.vbus_collapse = false;
2322        }
2323
2324        if (prev_status != di->flags.usbchargernotok)
2325                ab8500_power_supply_changed(di, &di->usb_chg.psy);
2326}
2327
2328/**
2329 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2330 * @work:       pointer to the work_struct structure
2331 *
2332 * Work queue function for checking the Main thermal prot status
2333 */
2334static void ab8500_charger_check_main_thermal_prot_work(
2335        struct work_struct *work)
2336{
2337        int ret;
2338        u8 reg_value;
2339
2340        struct ab8500_charger *di = container_of(work,
2341                struct ab8500_charger, check_main_thermal_prot_work);
2342
2343        /* Check if the status bit for main_thermal_prot is still active */
2344        ret = abx500_get_register_interruptible(di->dev,
2345                AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
2346        if (ret < 0) {
2347                dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2348                return;
2349        }
2350        if (reg_value & MAIN_CH_TH_PROT)
2351                di->flags.main_thermal_prot = true;
2352        else
2353                di->flags.main_thermal_prot = false;
2354
2355        ab8500_power_supply_changed(di, &di->ac_chg.psy);
2356}
2357
2358/**
2359 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2360 * @work:       pointer to the work_struct structure
2361 *
2362 * Work queue function for checking the USB thermal prot status
2363 */
2364static void ab8500_charger_check_usb_thermal_prot_work(
2365        struct work_struct *work)
2366{
2367        int ret;
2368        u8 reg_value;
2369
2370        struct ab8500_charger *di = container_of(work,
2371                struct ab8500_charger, check_usb_thermal_prot_work);
2372
2373        /* Check if the status bit for usb_thermal_prot is still active */
2374        ret = abx500_get_register_interruptible(di->dev,
2375                AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2376        if (ret < 0) {
2377                dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2378                return;
2379        }
2380        if (reg_value & USB_CH_TH_PROT)
2381                di->flags.usb_thermal_prot = true;
2382        else
2383                di->flags.usb_thermal_prot = false;
2384
2385        ab8500_power_supply_changed(di, &di->usb_chg.psy);
2386}
2387
2388/**
2389 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2390 * @irq:       interrupt number
2391 * @_di:       pointer to the ab8500_charger structure
2392 *
2393 * Returns IRQ status(IRQ_HANDLED)
2394 */
2395static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2396{
2397        struct ab8500_charger *di = _di;
2398
2399        dev_dbg(di->dev, "Main charger unplugged\n");
2400        queue_work(di->charger_wq, &di->ac_work);
2401
2402        cancel_delayed_work_sync(&di->ac_charger_attached_work);
2403        mutex_lock(&di->charger_attached_mutex);
2404        mutex_unlock(&di->charger_attached_mutex);
2405
2406        return IRQ_HANDLED;
2407}
2408
2409/**
2410 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2411 * @irq:       interrupt number
2412 * @_di:       pointer to the ab8500_charger structure
2413 *
2414 * Returns IRQ status(IRQ_HANDLED)
2415 */
2416static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2417{
2418        struct ab8500_charger *di = _di;
2419
2420        dev_dbg(di->dev, "Main charger plugged\n");
2421        queue_work(di->charger_wq, &di->ac_work);
2422
2423        mutex_lock(&di->charger_attached_mutex);
2424        mutex_unlock(&di->charger_attached_mutex);
2425        queue_delayed_work(di->charger_wq,
2426                           &di->ac_charger_attached_work,
2427                           HZ);
2428        return IRQ_HANDLED;
2429}
2430
2431/**
2432 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2433 * @irq:       interrupt number
2434 * @_di:       pointer to the ab8500_charger structure
2435 *
2436 * Returns IRQ status(IRQ_HANDLED)
2437 */
2438static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2439{
2440        struct ab8500_charger *di = _di;
2441
2442        dev_dbg(di->dev, "Main charger not ok\n");
2443        di->flags.mainextchnotok = true;
2444        ab8500_power_supply_changed(di, &di->ac_chg.psy);
2445
2446        /* Schedule a new HW failure check */
2447        queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2448
2449        return IRQ_HANDLED;
2450}
2451
2452/**
2453 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2454 * thermal protection threshold
2455 * @irq:       interrupt number
2456 * @_di:       pointer to the ab8500_charger structure
2457 *
2458 * Returns IRQ status(IRQ_HANDLED)
2459 */
2460static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2461{
2462        struct ab8500_charger *di = _di;
2463
2464        dev_dbg(di->dev,
2465                "Die temp above Main charger thermal protection threshold\n");
2466        queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2467
2468        return IRQ_HANDLED;
2469}
2470
2471/**
2472 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2473 * thermal protection threshold
2474 * @irq:       interrupt number
2475 * @_di:       pointer to the ab8500_charger structure
2476 *
2477 * Returns IRQ status(IRQ_HANDLED)
2478 */
2479static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2480{
2481        struct ab8500_charger *di = _di;
2482
2483        dev_dbg(di->dev,
2484                "Die temp ok for Main charger thermal protection threshold\n");
2485        queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2486
2487        return IRQ_HANDLED;
2488}
2489
2490static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2491{
2492        struct ab8500_charger *di = container_of(work,
2493                struct ab8500_charger, vbus_drop_end_work.work);
2494
2495        di->flags.vbus_drop_end = false;
2496
2497        /* Reset the drop counter */
2498        abx500_set_register_interruptible(di->dev,
2499                                  AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
2500
2501        if (di->usb.charger_connected)
2502                ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
2503}
2504
2505/**
2506 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2507 * @irq:       interrupt number
2508 * @_di:       pointer to the ab8500_charger structure
2509 *
2510 * Returns IRQ status(IRQ_HANDLED)
2511 */
2512static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2513{
2514        struct ab8500_charger *di = _di;
2515
2516        di->vbus_detected = false;
2517        dev_dbg(di->dev, "VBUS falling detected\n");
2518        queue_work(di->charger_wq, &di->detect_usb_type_work);
2519
2520        return IRQ_HANDLED;
2521}
2522
2523/**
2524 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2525 * @irq:       interrupt number
2526 * @_di:       pointer to the ab8500_charger structure
2527 *
2528 * Returns IRQ status(IRQ_HANDLED)
2529 */
2530static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2531{
2532        struct ab8500_charger *di = _di;
2533
2534        di->vbus_detected = true;
2535        dev_dbg(di->dev, "VBUS rising detected\n");
2536
2537        queue_work(di->charger_wq, &di->detect_usb_type_work);
2538
2539        return IRQ_HANDLED;
2540}
2541
2542/**
2543 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2544 * @irq:       interrupt number
2545 * @_di:       pointer to the ab8500_charger structure
2546 *
2547 * Returns IRQ status(IRQ_HANDLED)
2548 */
2549static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2550{
2551        struct ab8500_charger *di = _di;
2552
2553        dev_dbg(di->dev, "USB link status changed\n");
2554
2555        queue_work(di->charger_wq, &di->usb_link_status_work);
2556
2557        return IRQ_HANDLED;
2558}
2559
2560/**
2561 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2562 * thermal protection threshold
2563 * @irq:       interrupt number
2564 * @_di:       pointer to the ab8500_charger structure
2565 *
2566 * Returns IRQ status(IRQ_HANDLED)
2567 */
2568static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2569{
2570        struct ab8500_charger *di = _di;
2571
2572        dev_dbg(di->dev,
2573                "Die temp above USB charger thermal protection threshold\n");
2574        queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2575
2576        return IRQ_HANDLED;
2577}
2578
2579/**
2580 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2581 * thermal protection threshold
2582 * @irq:       interrupt number
2583 * @_di:       pointer to the ab8500_charger structure
2584 *
2585 * Returns IRQ status(IRQ_HANDLED)
2586 */
2587static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2588{
2589        struct ab8500_charger *di = _di;
2590
2591        dev_dbg(di->dev,
2592                "Die temp ok for USB charger thermal protection threshold\n");
2593        queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2594
2595        return IRQ_HANDLED;
2596}
2597
2598/**
2599 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2600 * @irq:       interrupt number
2601 * @_di:       pointer to the ab8500_charger structure
2602 *
2603 * Returns IRQ status(IRQ_HANDLED)
2604 */
2605static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2606{
2607        struct ab8500_charger *di = _di;
2608
2609        dev_dbg(di->dev, "Not allowed USB charger detected\n");
2610        queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2611
2612        return IRQ_HANDLED;
2613}
2614
2615/**
2616 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2617 * @irq:       interrupt number
2618 * @_di:       pointer to the ab8500_charger structure
2619 *
2620 * Returns IRQ status(IRQ_HANDLED)
2621 */
2622static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2623{
2624        struct ab8500_charger *di = _di;
2625
2626        dev_dbg(di->dev, "Charger watchdog expired\n");
2627
2628        /*
2629         * The charger that was online when the watchdog expired
2630         * needs to be restarted for charging to start again
2631         */
2632        if (di->ac.charger_online) {
2633                di->ac.wd_expired = true;
2634                ab8500_power_supply_changed(di, &di->ac_chg.psy);
2635        }
2636        if (di->usb.charger_online) {
2637                di->usb.wd_expired = true;
2638                ab8500_power_supply_changed(di, &di->usb_chg.psy);
2639        }
2640
2641        return IRQ_HANDLED;
2642}
2643
2644/**
2645 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2646 * @irq:       interrupt number
2647 * @_di:       pointer to the ab8500_charger structure
2648 *
2649 * Returns IRQ status(IRQ_HANDLED)
2650 */
2651static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2652{
2653        struct ab8500_charger *di = _di;
2654
2655        dev_dbg(di->dev, "VBUS charger drop ended\n");
2656        di->flags.vbus_drop_end = true;
2657        queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
2658                           round_jiffies(30 * HZ));
2659
2660        return IRQ_HANDLED;
2661}
2662
2663/**
2664 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2665 * @irq:       interrupt number
2666 * @_di:       pointer to the ab8500_charger structure
2667 *
2668 * Returns IRQ status(IRQ_HANDLED)
2669 */
2670static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2671{
2672        struct ab8500_charger *di = _di;
2673
2674        dev_dbg(di->dev, "VBUS overvoltage detected\n");
2675        di->flags.vbus_ovv = true;
2676        ab8500_power_supply_changed(di, &di->usb_chg.psy);
2677
2678        /* Schedule a new HW failure check */
2679        queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2680
2681        return IRQ_HANDLED;
2682}
2683
2684/**
2685 * ab8500_charger_ac_get_property() - get the ac/mains properties
2686 * @psy:       pointer to the power_supply structure
2687 * @psp:       pointer to the power_supply_property structure
2688 * @val:       pointer to the power_supply_propval union
2689 *
2690 * This function gets called when an application tries to get the ac/mains
2691 * properties by reading the sysfs files.
2692 * AC/Mains properties are online, present and voltage.
2693 * online:     ac/mains charging is in progress or not
2694 * present:    presence of the ac/mains
2695 * voltage:    AC/Mains voltage
2696 * Returns error code in case of failure else 0(on success)
2697 */
2698static int ab8500_charger_ac_get_property(struct power_supply *psy,
2699        enum power_supply_property psp,
2700        union power_supply_propval *val)
2701{
2702        struct ab8500_charger *di;
2703        int ret;
2704
2705        di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2706
2707        switch (psp) {
2708        case POWER_SUPPLY_PROP_HEALTH:
2709                if (di->flags.mainextchnotok)
2710                        val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2711                else if (di->ac.wd_expired || di->usb.wd_expired)
2712                        val->intval = POWER_SUPPLY_HEALTH_DEAD;
2713                else if (di->flags.main_thermal_prot)
2714                        val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2715                else
2716                        val->intval = POWER_SUPPLY_HEALTH_GOOD;
2717                break;
2718        case POWER_SUPPLY_PROP_ONLINE:
2719                val->intval = di->ac.charger_online;
2720                break;
2721        case POWER_SUPPLY_PROP_PRESENT:
2722                val->intval = di->ac.charger_connected;
2723                break;
2724        case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2725                ret = ab8500_charger_get_ac_voltage(di);
2726                if (ret >= 0)
2727                        di->ac.charger_voltage = ret;
2728                /* On error, use previous value */
2729                val->intval = di->ac.charger_voltage * 1000;
2730                break;
2731        case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2732                /*
2733                 * This property is used to indicate when CV mode is entered
2734                 * for the AC charger
2735                 */
2736                di->ac.cv_active = ab8500_charger_ac_cv(di);
2737                val->intval = di->ac.cv_active;
2738                break;
2739        case POWER_SUPPLY_PROP_CURRENT_NOW:
2740                ret = ab8500_charger_get_ac_current(di);
2741                if (ret >= 0)
2742                        di->ac.charger_current = ret;
2743                val->intval = di->ac.charger_current * 1000;
2744                break;
2745        default:
2746                return -EINVAL;
2747        }
2748        return 0;
2749}
2750
2751/**
2752 * ab8500_charger_usb_get_property() - get the usb properties
2753 * @psy:        pointer to the power_supply structure
2754 * @psp:        pointer to the power_supply_property structure
2755 * @val:        pointer to the power_supply_propval union
2756 *
2757 * This function gets called when an application tries to get the usb
2758 * properties by reading the sysfs files.
2759 * USB properties are online, present and voltage.
2760 * online:     usb charging is in progress or not
2761 * present:    presence of the usb
2762 * voltage:    vbus voltage
2763 * Returns error code in case of failure else 0(on success)
2764 */
2765static int ab8500_charger_usb_get_property(struct power_supply *psy,
2766        enum power_supply_property psp,
2767        union power_supply_propval *val)
2768{
2769        struct ab8500_charger *di;
2770        int ret;
2771
2772        di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2773
2774        switch (psp) {
2775        case POWER_SUPPLY_PROP_HEALTH:
2776                if (di->flags.usbchargernotok)
2777                        val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2778                else if (di->ac.wd_expired || di->usb.wd_expired)
2779                        val->intval = POWER_SUPPLY_HEALTH_DEAD;
2780                else if (di->flags.usb_thermal_prot)
2781                        val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2782                else if (di->flags.vbus_ovv)
2783                        val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2784                else
2785                        val->intval = POWER_SUPPLY_HEALTH_GOOD;
2786                break;
2787        case POWER_SUPPLY_PROP_ONLINE:
2788                val->intval = di->usb.charger_online;
2789                break;
2790        case POWER_SUPPLY_PROP_PRESENT:
2791                val->intval = di->usb.charger_connected;
2792                break;
2793        case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2794                ret = ab8500_charger_get_vbus_voltage(di);
2795                if (ret >= 0)
2796                        di->usb.charger_voltage = ret;
2797                val->intval = di->usb.charger_voltage * 1000;
2798                break;
2799        case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2800                /*
2801                 * This property is used to indicate when CV mode is entered
2802                 * for the USB charger
2803                 */
2804                di->usb.cv_active = ab8500_charger_usb_cv(di);
2805                val->intval = di->usb.cv_active;
2806                break;
2807        case POWER_SUPPLY_PROP_CURRENT_NOW:
2808                ret = ab8500_charger_get_usb_current(di);
2809                if (ret >= 0)
2810                        di->usb.charger_current = ret;
2811                val->intval = di->usb.charger_current * 1000;
2812                break;
2813        case POWER_SUPPLY_PROP_CURRENT_AVG:
2814                /*
2815                 * This property is used to indicate when VBUS has collapsed
2816                 * due to too high output current from the USB charger
2817                 */
2818                if (di->flags.vbus_collapse)
2819                        val->intval = 1;
2820                else
2821                        val->intval = 0;
2822                break;
2823        default:
2824                return -EINVAL;
2825        }
2826        return 0;
2827}
2828
2829/**
2830 * ab8500_charger_init_hw_registers() - Set up charger related registers
2831 * @di:         pointer to the ab8500_charger structure
2832 *
2833 * Set up charger OVV, watchdog and maximum voltage registers as well as
2834 * charging of the backup battery
2835 */
2836static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
2837{
2838        int ret = 0;
2839
2840        /* Setup maximum charger current and voltage for ABB cut2.0 */
2841        if (!is_ab8500_1p1_or_earlier(di->parent)) {
2842                ret = abx500_set_register_interruptible(di->dev,
2843                        AB8500_CHARGER,
2844                        AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
2845                if (ret) {
2846                        dev_err(di->dev,
2847                                "failed to set CH_VOLT_LVL_MAX_REG\n");
2848                        goto out;
2849                }
2850
2851                ret = abx500_set_register_interruptible(di->dev,
2852                        AB8500_CHARGER,
2853                        AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6);
2854                if (ret) {
2855                        dev_err(di->dev,
2856                                "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
2857                        goto out;
2858                }
2859        }
2860
2861        if (is_ab9540_2p0(di->parent) || is_ab8505_2p0(di->parent))
2862                ret = abx500_mask_and_set_register_interruptible(di->dev,
2863                        AB8500_CHARGER,
2864                        AB8500_USBCH_CTRL2_REG,
2865                        VBUS_AUTO_IN_CURR_LIM_ENA,
2866                        VBUS_AUTO_IN_CURR_LIM_ENA);
2867        else
2868                /*
2869                 * VBUS OVV set to 6.3V and enable automatic current limitation
2870                 */
2871                ret = abx500_set_register_interruptible(di->dev,
2872                        AB8500_CHARGER,
2873                        AB8500_USBCH_CTRL2_REG,
2874                        VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
2875        if (ret) {
2876                dev_err(di->dev,
2877                        "failed to set automatic current limitation\n");
2878                goto out;
2879        }
2880
2881        /* Enable main watchdog in OTP */
2882        ret = abx500_set_register_interruptible(di->dev,
2883                AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
2884        if (ret) {
2885                dev_err(di->dev, "failed to enable main WD in OTP\n");
2886                goto out;
2887        }
2888
2889        /* Enable main watchdog */
2890        ret = abx500_set_register_interruptible(di->dev,
2891                AB8500_SYS_CTRL2_BLOCK,
2892                AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
2893        if (ret) {
2894                dev_err(di->dev, "faile to enable main watchdog\n");
2895                goto out;
2896        }
2897
2898        /*
2899         * Due to internal synchronisation, Enable and Kick watchdog bits
2900         * cannot be enabled in a single write.
2901         * A minimum delay of 2*32 kHz period (62.5┬Ás) must be inserted
2902         * between writing Enable then Kick bits.
2903         */
2904        udelay(63);
2905
2906        /* Kick main watchdog */
2907        ret = abx500_set_register_interruptible(di->dev,
2908                AB8500_SYS_CTRL2_BLOCK,
2909                AB8500_MAIN_WDOG_CTRL_REG,
2910                (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
2911        if (ret) {
2912                dev_err(di->dev, "failed to kick main watchdog\n");
2913                goto out;
2914        }
2915
2916        /* Disable main watchdog */
2917        ret = abx500_set_register_interruptible(di->dev,
2918                AB8500_SYS_CTRL2_BLOCK,
2919                AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
2920        if (ret) {
2921                dev_err(di->dev, "failed to disable main watchdog\n");
2922                goto out;
2923        }
2924
2925        /* Set watchdog timeout */
2926        ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2927                AB8500_CH_WD_TIMER_REG, WD_TIMER);
2928        if (ret) {
2929                dev_err(di->dev, "failed to set charger watchdog timeout\n");
2930                goto out;
2931        }
2932
2933        /* Set charger watchdog timeout */
2934        ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2935                AB8500_CH_WD_TIMER_REG, WD_TIMER);
2936        if (ret) {
2937                dev_err(di->dev, "failed to set charger watchdog timeout\n");
2938                goto out;
2939        }
2940
2941        ret = ab8500_charger_led_en(di, false);
2942        if (ret < 0) {
2943                dev_err(di->dev, "failed to disable LED\n");
2944                goto out;
2945        }
2946
2947        /* Backup battery voltage and current */
2948        ret = abx500_set_register_interruptible(di->dev,
2949                AB8500_RTC,
2950                AB8500_RTC_BACKUP_CHG_REG,
2951                di->bm->bkup_bat_v |
2952                di->bm->bkup_bat_i);
2953        if (ret) {
2954                dev_err(di->dev, "failed to setup backup battery charging\n");
2955                goto out;
2956        }
2957
2958        /* Enable backup battery charging */
2959        abx500_mask_and_set_register_interruptible(di->dev,
2960                AB8500_RTC, AB8500_RTC_CTRL_REG,
2961                RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
2962        if (ret < 0)
2963                dev_err(di->dev, "%s mask and set failed\n", __func__);
2964
2965out:
2966        return ret;
2967}
2968
2969/*
2970 * ab8500 charger driver interrupts and their respective isr
2971 */
2972static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
2973        {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
2974        {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
2975        {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
2976        {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
2977        {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
2978        {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
2979        {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
2980        {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
2981        {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
2982        {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
2983        {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
2984        {"VBUS_OVV", ab8500_charger_vbusovv_handler},
2985        {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
2986        {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
2987};
2988
2989static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
2990                unsigned long event, void *power)
2991{
2992        struct ab8500_charger *di =
2993                container_of(nb, struct ab8500_charger, nb);
2994        enum ab8500_usb_state bm_usb_state;
2995        unsigned mA = *((unsigned *)power);
2996
2997        if (!di)
2998                return NOTIFY_DONE;
2999
3000        if (event != USB_EVENT_VBUS) {
3001                dev_dbg(di->dev, "not a standard host, returning\n");
3002                return NOTIFY_DONE;
3003        }
3004
3005        /* TODO: State is fabricate  here. See if charger really needs USB
3006         * state or if mA is enough
3007         */
3008        if ((di->usb_state.usb_current == 2) && (mA > 2))
3009                bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3010        else if (mA == 0)
3011                bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3012        else if (mA == 2)
3013                bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3014        else if (mA >= 8) /* 8, 100, 500 */
3015                bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3016        else /* Should never occur */
3017                bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3018
3019        dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3020                __func__, bm_usb_state, mA);
3021
3022        spin_lock(&di->usb_state.usb_lock);
3023        di->usb_state.state_tmp = bm_usb_state;
3024        di->usb_state.usb_current_tmp = mA;
3025        spin_unlock(&di->usb_state.usb_lock);
3026
3027        /*
3028         * wait for some time until you get updates from the usb stack
3029         * and negotiations are completed
3030         */
3031        queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
3032
3033        return NOTIFY_OK;
3034}
3035
3036#if defined(CONFIG_PM)
3037static int ab8500_charger_resume(struct platform_device *pdev)
3038{
3039        int ret;
3040        struct ab8500_charger *di = platform_get_drvdata(pdev);
3041
3042        /*
3043         * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3044         * logic. That means we have to continously kick the charger
3045         * watchdog even when no charger is connected. This is only
3046         * valid once the AC charger has been enabled. This is
3047         * a bug that is not handled by the algorithm and the
3048         * watchdog have to be kicked by the charger driver
3049         * when the AC charger is disabled
3050         */
3051        if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3052                ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3053                        AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3054                if (ret)
3055                        dev_err(di->dev, "Failed to kick WD!\n");
3056
3057                /* If not already pending start a new timer */
3058                if (!delayed_work_pending(
3059                        &di->kick_wd_work)) {
3060                        queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3061                                round_jiffies(WD_KICK_INTERVAL));
3062                }
3063        }
3064
3065        /* If we still have a HW failure, schedule a new check */
3066        if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
3067                queue_delayed_work(di->charger_wq,
3068                        &di->check_hw_failure_work, 0);
3069        }
3070
3071        if (di->flags.vbus_drop_end)
3072                queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
3073
3074        return 0;
3075}
3076
3077static int ab8500_charger_suspend(struct platform_device *pdev,
3078        pm_message_t state)
3079{
3080        struct ab8500_charger *di = platform_get_drvdata(pdev);
3081
3082        /* Cancel any pending HW failure check */
3083        if (delayed_work_pending(&di->check_hw_failure_work))
3084                cancel_delayed_work(&di->check_hw_failure_work);
3085
3086        if (delayed_work_pending(&di->vbus_drop_end_work))
3087                cancel_delayed_work(&di->vbus_drop_end_work);
3088
3089        flush_delayed_work(&di->attach_work);
3090        flush_delayed_work(&di->usb_charger_attached_work);
3091        flush_delayed_work(&di->ac_charger_attached_work);
3092        flush_delayed_work(&di->check_usbchgnotok_work);
3093        flush_delayed_work(&di->check_vbat_work);
3094        flush_delayed_work(&di->kick_wd_work);
3095
3096        flush_work(&di->usb_link_status_work);
3097        flush_work(&di->ac_work);
3098        flush_work(&di->detect_usb_type_work);
3099
3100        if (atomic_read(&di->current_stepping_sessions))
3101                return -EAGAIN;
3102
3103        return 0;
3104}
3105#else
3106#define ab8500_charger_suspend      NULL
3107#define ab8500_charger_resume       NULL
3108#endif
3109
3110static int ab8500_charger_remove(struct platform_device *pdev)
3111{
3112        struct ab8500_charger *di = platform_get_drvdata(pdev);
3113        int i, irq, ret;
3114
3115        /* Disable AC charging */
3116        ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3117
3118        /* Disable USB charging */
3119        ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3120
3121        /* Disable interrupts */
3122        for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3123                irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3124                free_irq(irq, di);
3125        }
3126
3127        /* Backup battery voltage and current disable */
3128        ret = abx500_mask_and_set_register_interruptible(di->dev,
3129                AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3130        if (ret < 0)
3131                dev_err(di->dev, "%s mask and set failed\n", __func__);
3132
3133        usb_unregister_notifier(di->usb_phy, &di->nb);
3134        usb_put_phy(di->usb_phy);
3135
3136        /* Delete the work queue */
3137        destroy_workqueue(di->charger_wq);
3138
3139        flush_scheduled_work();
3140        if(di->usb_chg.enabled)
3141                power_supply_unregister(&di->usb_chg.psy);
3142#if !defined(CONFIG_CHARGER_PM2301)
3143        if(di->ac_chg.enabled)
3144                power_supply_unregister(&di->ac_chg.psy);
3145#endif
3146        platform_set_drvdata(pdev, NULL);
3147
3148        return 0;
3149}
3150
3151static char *supply_interface[] = {
3152        "ab8500_chargalg",
3153        "ab8500_fg",
3154        "ab8500_btemp",
3155};
3156
3157static int ab8500_charger_probe(struct platform_device *pdev)
3158{
3159        struct device_node *np = pdev->dev.of_node;
3160        struct abx500_bm_data *plat = pdev->dev.platform_data;
3161        struct ab8500_charger *di;
3162        int irq, i, charger_status, ret = 0, ch_stat;
3163
3164        di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3165        if (!di) {
3166                dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
3167                return -ENOMEM;
3168        }
3169
3170        if (!plat) {
3171                dev_err(&pdev->dev, "no battery management data supplied\n");
3172                return -EINVAL;
3173        }
3174        di->bm = plat;
3175
3176        if (np) {
3177                ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
3178                if (ret) {
3179                        dev_err(&pdev->dev, "failed to get battery information\n");
3180                        return ret;
3181                }
3182                di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3183        } else
3184                di->autopower_cfg = false;
3185
3186        /* get parent data */
3187        di->dev = &pdev->dev;
3188        di->parent = dev_get_drvdata(pdev->dev.parent);
3189        di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
3190
3191        /* initialize lock */
3192        spin_lock_init(&di->usb_state.usb_lock);
3193        mutex_init(&di->usb_ipt_crnt_lock);
3194
3195        di->autopower = false;
3196        di->invalid_charger_detect_state = 0;
3197
3198        /* AC supply */
3199        /* power_supply base class */
3200        di->ac_chg.psy.name = "ab8500_ac";
3201        di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
3202        di->ac_chg.psy.properties = ab8500_charger_ac_props;
3203        di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
3204        di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
3205        di->ac_chg.psy.supplied_to = supply_interface;
3206        di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
3207        /* ux500_charger sub-class */
3208        di->ac_chg.ops.enable = &ab8500_charger_ac_en;
3209        di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3210        di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3211        di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
3212                ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3213        di->ac_chg.max_out_curr = ab8500_charger_current_map[
3214                ARRAY_SIZE(ab8500_charger_current_map) - 1];
3215        di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
3216        di->ac_chg.enabled = di->bm->ac_enabled;
3217        di->ac_chg.external = false;
3218
3219        /* USB supply */
3220        /* power_supply base class */
3221        di->usb_chg.psy.name = "ab8500_usb";
3222        di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
3223        di->usb_chg.psy.properties = ab8500_charger_usb_props;
3224        di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
3225        di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
3226        di->usb_chg.psy.supplied_to = supply_interface;
3227        di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
3228        /* ux500_charger sub-class */
3229        di->usb_chg.ops.enable = &ab8500_charger_usb_en;
3230        di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3231        di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3232        di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
3233                ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3234        di->usb_chg.max_out_curr = ab8500_charger_current_map[
3235                ARRAY_SIZE(ab8500_charger_current_map) - 1];
3236        di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
3237        di->usb_chg.enabled = di->bm->usb_enabled;
3238        di->usb_chg.external = false;
3239
3240        /* Create a work queue for the charger */
3241        di->charger_wq =
3242                create_singlethread_workqueue("ab8500_charger_wq");
3243        if (di->charger_wq == NULL) {
3244                dev_err(di->dev, "failed to create work queue\n");
3245                return -ENOMEM;
3246        }
3247
3248        mutex_init(&di->charger_attached_mutex);
3249
3250        /* Init work for HW failure check */
3251        INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
3252                ab8500_charger_check_hw_failure_work);
3253        INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
3254                ab8500_charger_check_usbchargernotok_work);
3255
3256        INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3257                          ab8500_charger_ac_attached_work);
3258        INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3259                          ab8500_charger_usb_attached_work);
3260
3261        /*
3262         * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3263         * logic. That means we have to continously kick the charger
3264         * watchdog even when no charger is connected. This is only
3265         * valid once the AC charger has been enabled. This is
3266         * a bug that is not handled by the algorithm and the
3267         * watchdog have to be kicked by the charger driver
3268         * when the AC charger is disabled
3269         */
3270        INIT_DEFERRABLE_WORK(&di->kick_wd_work,
3271                ab8500_charger_kick_watchdog_work);
3272
3273        INIT_DEFERRABLE_WORK(&di->check_vbat_work,
3274                ab8500_charger_check_vbat_work);
3275
3276        INIT_DELAYED_WORK(&di->attach_work,
3277                ab8500_charger_usb_link_attach_work);
3278
3279        INIT_DELAYED_WORK(&di->usb_state_changed_work,
3280                ab8500_charger_usb_state_changed_work);
3281
3282        INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3283                ab8500_charger_vbus_drop_end_work);
3284
3285        /* Init work for charger detection */
3286        INIT_WORK(&di->usb_link_status_work,
3287                ab8500_charger_usb_link_status_work);
3288        INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3289        INIT_WORK(&di->detect_usb_type_work,
3290                ab8500_charger_detect_usb_type_work);
3291
3292        /* Init work for checking HW status */
3293        INIT_WORK(&di->check_main_thermal_prot_work,
3294                ab8500_charger_check_main_thermal_prot_work);
3295        INIT_WORK(&di->check_usb_thermal_prot_work,
3296                ab8500_charger_check_usb_thermal_prot_work);
3297
3298        /*
3299         * VDD ADC supply needs to be enabled from this driver when there
3300         * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3301         * interrupts during charging
3302         */
3303        di->regu = devm_regulator_get(di->dev, "vddadc");
3304        if (IS_ERR(di->regu)) {
3305                ret = PTR_ERR(di->regu);
3306                dev_err(di->dev, "failed to get vddadc regulator\n");
3307                goto free_charger_wq;
3308        }
3309
3310
3311        /* Initialize OVV, and other registers */
3312        ret = ab8500_charger_init_hw_registers(di);
3313        if (ret) {
3314                dev_err(di->dev, "failed to initialize ABB registers\n");
3315                goto free_charger_wq;
3316        }
3317
3318        /* Register AC charger class */
3319        if(di->ac_chg.enabled) {
3320                ret = power_supply_register(di->dev, &di->ac_chg.psy);
3321                if (ret) {
3322                        dev_err(di->dev, "failed to register AC charger\n");
3323                        goto free_charger_wq;
3324                }
3325        }
3326
3327        /* Register USB charger class */
3328        if(di->usb_chg.enabled) {
3329                ret = power_supply_register(di->dev, &di->usb_chg.psy);
3330                if (ret) {
3331                        dev_err(di->dev, "failed to register USB charger\n");
3332                        goto free_ac;
3333                }
3334        }
3335
3336        di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
3337        if (IS_ERR_OR_NULL(di->usb_phy)) {
3338                dev_err(di->dev, "failed to get usb transceiver\n");
3339                ret = -EINVAL;
3340                goto free_usb;
3341        }
3342        di->nb.notifier_call = ab8500_charger_usb_notifier_call;
3343        ret = usb_register_notifier(di->usb_phy, &di->nb);
3344        if (ret) {
3345                dev_err(di->dev, "failed to register usb notifier\n");
3346                goto put_usb_phy;
3347        }
3348
3349        /* Identify the connected charger types during startup */
3350        charger_status = ab8500_charger_detect_chargers(di, true);
3351        if (charger_status & AC_PW_CONN) {
3352                di->ac.charger_connected = 1;
3353                di->ac_conn = true;
3354                ab8500_power_supply_changed(di, &di->ac_chg.psy);
3355                sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
3356        }
3357
3358        if (charger_status & USB_PW_CONN) {
3359                di->vbus_detected = true;
3360                di->vbus_detected_start = true;
3361                queue_work(di->charger_wq,
3362                        &di->detect_usb_type_work);
3363        }
3364
3365        /* Register interrupts */
3366        for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3367                irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3368                ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3369                        IRQF_SHARED | IRQF_NO_SUSPEND,
3370                        ab8500_charger_irq[i].name, di);
3371
3372                if (ret != 0) {
3373                        dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3374                                , ab8500_charger_irq[i].name, irq, ret);
3375                        goto free_irq;
3376                }
3377                dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3378                        ab8500_charger_irq[i].name, irq, ret);
3379        }
3380
3381        platform_set_drvdata(pdev, di);
3382
3383        mutex_lock(&di->charger_attached_mutex);
3384
3385        ch_stat = ab8500_charger_detect_chargers(di, false);
3386
3387        if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3388                queue_delayed_work(di->charger_wq,
3389                                   &di->ac_charger_attached_work,
3390                                   HZ);
3391        }
3392        if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3393                queue_delayed_work(di->charger_wq,
3394                                   &di->usb_charger_attached_work,
3395                                   HZ);
3396        }
3397
3398        mutex_unlock(&di->charger_attached_mutex);
3399
3400        return ret;
3401
3402free_irq:
3403        usb_unregister_notifier(di->usb_phy, &di->nb);
3404
3405        /* We also have to free all successfully registered irqs */
3406        for (i = i - 1; i >= 0; i--) {
3407                irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3408                free_irq(irq, di);
3409        }
3410put_usb_phy:
3411        usb_put_phy(di->usb_phy);
3412free_usb:
3413        if(di->usb_chg.enabled)
3414                power_supply_unregister(&di->usb_chg.psy);
3415free_ac:
3416        if(di->ac_chg.enabled)
3417                power_supply_unregister(&di->ac_chg.psy);
3418free_charger_wq:
3419        destroy_workqueue(di->charger_wq);
3420        return ret;
3421}
3422
3423static const struct of_device_id ab8500_charger_match[] = {
3424        { .compatible = "stericsson,ab8500-charger", },
3425        { },
3426};
3427
3428static struct platform_driver ab8500_charger_driver = {
3429        .probe = ab8500_charger_probe,
3430        .remove = ab8500_charger_remove,
3431        .suspend = ab8500_charger_suspend,
3432        .resume = ab8500_charger_resume,
3433        .driver = {
3434                .name = "ab8500-charger",
3435                .owner = THIS_MODULE,
3436                .of_match_table = ab8500_charger_match,
3437        },
3438};
3439
3440static int __init ab8500_charger_init(void)
3441{
3442        return platform_driver_register(&ab8500_charger_driver);
3443}
3444
3445static void __exit ab8500_charger_exit(void)
3446{
3447        platform_driver_unregister(&ab8500_charger_driver);
3448}
3449
3450subsys_initcall_sync(ab8500_charger_init);
3451module_exit(ab8500_charger_exit);
3452
3453MODULE_LICENSE("GPL v2");
3454MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3455MODULE_ALIAS("platform:ab8500-charger");
3456MODULE_DESCRIPTION("AB8500 charger management driver");
3457
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.