blob: ec8e6b9fca700e6e1e386a3e052a65b4706bbd6c [file] [log] [blame]
/* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#define pr_fmt(fmt) "%s: " fmt, __func__
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/platform_device.h>
#include <linux/errno.h>
#include <linux/mfd/pm8xxx/pm8921-charger.h>
#include <linux/mfd/pm8xxx/pm8921-bms.h>
#include <linux/mfd/pm8921-adc.h>
#include <linux/mfd/pm8xxx/core.h>
#include <linux/interrupt.h>
#include <linux/power_supply.h>
#include <linux/delay.h>
#include <linux/bitops.h>
#include <linux/workqueue.h>
#include <linux/debugfs.h>
#include <linux/slab.h>
#include <mach/msm_xo.h>
#include <mach/msm_hsusb.h>
#define CHG_BUCK_CLOCK_CTRL 0x14
#define PBL_ACCESS1 0x04
#define PBL_ACCESS2 0x05
#define SYS_CONFIG_1 0x06
#define SYS_CONFIG_2 0x07
#define CHG_CNTRL 0x204
#define CHG_IBAT_MAX 0x205
#define CHG_TEST 0x206
#define CHG_BUCK_CTRL_TEST1 0x207
#define CHG_BUCK_CTRL_TEST2 0x208
#define CHG_BUCK_CTRL_TEST3 0x209
#define COMPARATOR_OVERRIDE 0x20A
#define PSI_TXRX_SAMPLE_DATA_0 0x20B
#define PSI_TXRX_SAMPLE_DATA_1 0x20C
#define PSI_TXRX_SAMPLE_DATA_2 0x20D
#define PSI_TXRX_SAMPLE_DATA_3 0x20E
#define PSI_CONFIG_STATUS 0x20F
#define CHG_IBAT_SAFE 0x210
#define CHG_ITRICKLE 0x211
#define CHG_CNTRL_2 0x212
#define CHG_VBAT_DET 0x213
#define CHG_VTRICKLE 0x214
#define CHG_ITERM 0x215
#define CHG_CNTRL_3 0x216
#define CHG_VIN_MIN 0x217
#define CHG_TWDOG 0x218
#define CHG_TTRKL_MAX 0x219
#define CHG_TEMP_THRESH 0x21A
#define CHG_TCHG_MAX 0x21B
#define USB_OVP_CONTROL 0x21C
#define DC_OVP_CONTROL 0x21D
#define USB_OVP_TEST 0x21E
#define DC_OVP_TEST 0x21F
#define CHG_VDD_MAX 0x220
#define CHG_VDD_SAFE 0x221
#define CHG_VBAT_BOOT_THRESH 0x222
#define USB_OVP_TRIM 0x355
#define BUCK_CONTROL_TRIM1 0x356
#define BUCK_CONTROL_TRIM2 0x357
#define BUCK_CONTROL_TRIM3 0x358
#define BUCK_CONTROL_TRIM4 0x359
#define CHG_DEFAULTS_TRIM 0x35A
#define CHG_ITRIM 0x35B
#define CHG_TTRIM 0x35C
#define CHG_COMP_OVR 0x20A
enum chg_fsm_state {
FSM_STATE_OFF_0 = 0,
FSM_STATE_BATFETDET_START_12 = 12,
FSM_STATE_BATFETDET_END_16 = 16,
FSM_STATE_ON_CHG_HIGHI_1 = 1,
FSM_STATE_ATC_2A = 2,
FSM_STATE_ATC_2B = 18,
FSM_STATE_ON_BAT_3 = 3,
FSM_STATE_ATC_FAIL_4 = 4 ,
FSM_STATE_DELAY_5 = 5,
FSM_STATE_ON_CHG_AND_BAT_6 = 6,
FSM_STATE_FAST_CHG_7 = 7,
FSM_STATE_TRKL_CHG_8 = 8,
FSM_STATE_CHG_FAIL_9 = 9,
FSM_STATE_EOC_10 = 10,
FSM_STATE_ON_CHG_VREGOK_11 = 11,
FSM_STATE_ATC_PAUSE_13 = 13,
FSM_STATE_FAST_CHG_PAUSE_14 = 14,
FSM_STATE_TRKL_CHG_PAUSE_15 = 15,
FSM_STATE_START_BOOT = 20,
FSM_STATE_FLCB_VREGOK = 21,
FSM_STATE_FLCB = 22,
};
enum pmic_chg_interrupts {
USBIN_VALID_IRQ = 0,
USBIN_OV_IRQ,
BATT_INSERTED_IRQ,
VBATDET_LOW_IRQ,
USBIN_UV_IRQ,
VBAT_OV_IRQ,
CHGWDOG_IRQ,
VCP_IRQ,
ATCDONE_IRQ,
ATCFAIL_IRQ,
CHGDONE_IRQ,
CHGFAIL_IRQ,
CHGSTATE_IRQ,
LOOP_CHANGE_IRQ,
FASTCHG_IRQ,
TRKLCHG_IRQ,
BATT_REMOVED_IRQ,
BATTTEMP_HOT_IRQ,
CHGHOT_IRQ,
BATTTEMP_COLD_IRQ,
CHG_GONE_IRQ,
BAT_TEMP_OK_IRQ,
COARSE_DET_LOW_IRQ,
VDD_LOOP_IRQ,
VREG_OV_IRQ,
VBATDET_IRQ,
BATFET_IRQ,
PSI_IRQ,
DCIN_VALID_IRQ,
DCIN_OV_IRQ,
DCIN_UV_IRQ,
PM_CHG_MAX_INTS,
};
struct bms_notify {
int is_charging;
struct work_struct work;
};
/**
* struct pm8921_chg_chip -device information
* @dev: device pointer to access the parent
* @is_usb_path_used: indicates whether USB charging is used at all
* @is_usb_path_used: indicates whether DC charging is used at all
* @usb_present: present status of usb
* @dc_present: present status of dc
* @usb_charger_current: usb current to charge the battery with used when
* the usb path is enabled or charging is resumed
* @safety_time: max time for which charging will happen
* @update_time: how frequently the userland needs to be updated
* @max_voltage: the max volts the batt should be charged up to
* @min_voltage: the min battery voltage before turning the FETon
* @resume_voltage: the voltage at which the battery should resume
* charging
* @term_current: The charging based term current
*
*/
struct pm8921_chg_chip {
struct device *dev;
unsigned int usb_present;
unsigned int dc_present;
unsigned int usb_charger_current;
unsigned int pmic_chg_irq[PM_CHG_MAX_INTS];
unsigned int safety_time;
unsigned int update_time;
unsigned int max_voltage;
unsigned int min_voltage;
unsigned int resume_voltage;
unsigned int term_current;
unsigned int vbat_channel;
struct power_supply usb_psy;
struct power_supply dc_psy;
struct power_supply batt_psy;
struct dentry *dent;
struct bms_notify bms_notify;
DECLARE_BITMAP(enabled_irqs, PM_CHG_MAX_INTS);
};
static int charging_disabled;
static struct pm8921_chg_chip *the_chip;
static int pm_chg_masked_write(struct pm8921_chg_chip *chip, u16 addr,
u8 mask, u8 val)
{
int rc;
u8 reg;
rc = pm8xxx_readb(chip->dev->parent, addr, &reg);
if (rc) {
pr_err("pm8xxx_readb failed: addr=%03X, rc=%d\n", addr, rc);
return rc;
}
reg &= ~mask;
reg |= val & mask;
rc = pm8xxx_writeb(chip->dev->parent, addr, reg);
if (rc) {
pr_err("pm8xxx_writeb failed: addr=%03X, rc=%d\n", addr, rc);
return rc;
}
return 0;
}
#define CAPTURE_FSM_STATE_CMD 0xC2
#define READ_BANK_7 0x70
#define READ_BANK_4 0x40
static int pm_chg_get_fsm_state(struct pm8921_chg_chip *chip)
{
u8 temp;
int err, ret = 0;
temp = CAPTURE_FSM_STATE_CMD;
err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp);
if (err) {
pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
return err;
}
temp = READ_BANK_7;
err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp);
if (err) {
pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
return err;
}
err = pm8xxx_readb(chip->dev->parent, CHG_TEST, &temp);
if (err) {
pr_err("pm8xxx_readb fail: addr=%03X, rc=%d\n", CHG_TEST, err);
return err;
}
/* get the lower 4 bits */
ret = temp & 0xF;
temp = READ_BANK_4;
err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp);
if (err) {
pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
return err;
}
err = pm8xxx_readb(chip->dev->parent, CHG_TEST, &temp);
if (err) {
pr_err("pm8xxx_readb fail: addr=%03X, rc=%d\n", CHG_TEST, err);
return err;
}
/* get the upper 1 bit */
ret |= (temp & 0x1) << 4;
return ret;
}
#define CHG_USB_SUSPEND_BIT BIT(2)
static int pm_chg_usb_suspend_enable(struct pm8921_chg_chip *chip, int enable)
{
return pm_chg_masked_write(chip, CHG_CNTRL_3, CHG_USB_SUSPEND_BIT,
enable ? CHG_USB_SUSPEND_BIT : 0);
}
#define CHG_EN_BIT BIT(7)
static int pm_chg_auto_enable(struct pm8921_chg_chip *chip, int enable)
{
return pm_chg_masked_write(chip, CHG_CNTRL_3, CHG_EN_BIT,
enable ? CHG_EN_BIT : 0);
}
#define CHG_CHARGE_DIS_BIT BIT(1)
static int pm_chg_charge_dis(struct pm8921_chg_chip *chip, int disable)
{
return pm_chg_masked_write(chip, CHG_CNTRL, CHG_CHARGE_DIS_BIT,
disable ? CHG_CHARGE_DIS_BIT : 0);
}
#define PM8921_CHG_V_MIN_MV 3240
#define PM8921_CHG_V_STEP_MV 20
#define PM8921_CHG_VDDMAX_MAX 4500
#define PM8921_CHG_VDDMAX_MIN 3400
#define PM8921_CHG_V_MASK 0x7F
static int pm_chg_vddmax_set(struct pm8921_chg_chip *chip, int voltage)
{
u8 temp;
if (voltage < PM8921_CHG_VDDMAX_MIN
|| voltage > PM8921_CHG_VDDMAX_MAX) {
pr_err("bad mV=%d asked to set\n", voltage);
return -EINVAL;
}
temp = (voltage - PM8921_CHG_V_MIN_MV) / PM8921_CHG_V_STEP_MV;
pr_debug("voltage=%d setting %02x\n", voltage, temp);
return pm_chg_masked_write(chip, CHG_VDD_MAX, PM8921_CHG_V_MASK, temp);
}
#define PM8921_CHG_VDDSAFE_MIN 3400
#define PM8921_CHG_VDDSAFE_MAX 4500
static int pm_chg_vddsafe_set(struct pm8921_chg_chip *chip, int voltage)
{
u8 temp;
if (voltage < PM8921_CHG_VDDSAFE_MIN
|| voltage > PM8921_CHG_VDDSAFE_MAX) {
pr_err("bad mV=%d asked to set\n", voltage);
return -EINVAL;
}
temp = (voltage - PM8921_CHG_V_MIN_MV) / PM8921_CHG_V_STEP_MV;
pr_debug("voltage=%d setting %02x\n", voltage, temp);
return pm_chg_masked_write(chip, CHG_VDD_SAFE, PM8921_CHG_V_MASK, temp);
}
#define PM8921_CHG_VBATDET_MIN 3240
#define PM8921_CHG_VBATDET_MAX 5780
static int pm_chg_vbatdet_set(struct pm8921_chg_chip *chip, int voltage)
{
u8 temp;
if (voltage < PM8921_CHG_VBATDET_MIN
|| voltage > PM8921_CHG_VBATDET_MAX) {
pr_err("bad mV=%d asked to set\n", voltage);
return -EINVAL;
}
temp = (voltage - PM8921_CHG_V_MIN_MV) / PM8921_CHG_V_STEP_MV;
pr_debug("voltage=%d setting %02x\n", voltage, temp);
return pm_chg_masked_write(chip, CHG_VBAT_DET, PM8921_CHG_V_MASK, temp);
}
#define PM8921_CHG_IBATMAX_MIN 325
#define PM8921_CHG_IBATMAX_MAX 2000
#define PM8921_CHG_I_MIN_MA 225
#define PM8921_CHG_I_STEP_MA 50
#define PM8921_CHG_I_MASK 0x3F
static int pm_chg_ibatmax_set(struct pm8921_chg_chip *chip, int chg_current)
{
u8 temp;
if (chg_current < PM8921_CHG_IBATMAX_MIN
|| chg_current > PM8921_CHG_IBATMAX_MAX) {
pr_err("bad mA=%d asked to set\n", chg_current);
return -EINVAL;
}
temp = (chg_current - PM8921_CHG_I_MIN_MA) / PM8921_CHG_I_STEP_MA;
return pm_chg_masked_write(chip, CHG_IBAT_MAX, PM8921_CHG_I_MASK, temp);
}
#define PM8921_CHG_IBATSAFE_MIN 225
#define PM8921_CHG_IBATSAFE_MAX 3375
static int pm_chg_ibatsafe_set(struct pm8921_chg_chip *chip, int chg_current)
{
u8 temp;
if (chg_current < PM8921_CHG_IBATSAFE_MIN
|| chg_current > PM8921_CHG_IBATSAFE_MAX) {
pr_err("bad mA=%d asked to set\n", chg_current);
return -EINVAL;
}
temp = (chg_current - PM8921_CHG_I_MIN_MA) / PM8921_CHG_I_STEP_MA;
return pm_chg_masked_write(chip, CHG_IBAT_SAFE,
PM8921_CHG_I_MASK, temp);
}
#define PM8921_CHG_ITERM_MIN_MA 50
#define PM8921_CHG_ITERM_MAX_MA 200
#define PM8921_CHG_ITERM_STEP_MA 10
#define PM8921_CHG_ITERM_MASK 0xF
static int pm_chg_iterm_set(struct pm8921_chg_chip *chip, int chg_current)
{
u8 temp;
if (chg_current < PM8921_CHG_ITERM_MIN_MA
|| chg_current > PM8921_CHG_ITERM_MAX_MA) {
pr_err("bad mA=%d asked to set\n", chg_current);
return -EINVAL;
}
temp = (chg_current - PM8921_CHG_ITERM_MIN_MA)
/ PM8921_CHG_ITERM_STEP_MA;
return pm_chg_masked_write(chip, CHG_ITERM, PM8921_CHG_ITERM_MASK,
temp);
}
#define PM8921_CHG_IUSB_MASK 0x1C
#define PM8921_CHG_IUSB_MAX 7
#define PM8921_CHG_IUSB_MIN 0
static int pm_chg_iusbmax_set(struct pm8921_chg_chip *chip, int chg_current)
{
u8 temp;
if (chg_current < PM8921_CHG_IUSB_MIN
|| chg_current > PM8921_CHG_IUSB_MAX) {
pr_err("bad mA=%d asked to set\n", chg_current);
return -EINVAL;
}
temp = chg_current << 2;
return pm_chg_masked_write(chip, PBL_ACCESS2, PM8921_CHG_IUSB_MASK,
temp);
}
#define PM8921_CHG_WD_MASK 0x1F
static int pm_chg_disable_wd(struct pm8921_chg_chip *chip)
{
/* writing 0 to the wd timer disables it */
return pm_chg_masked_write(chip, CHG_TWDOG, PM8921_CHG_WD_MASK,
0);
}
static void pm8921_chg_enable_irq(struct pm8921_chg_chip *chip, int interrupt)
{
if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) {
dev_dbg(chip->dev, "%d\n", chip->pmic_chg_irq[interrupt]);
enable_irq(chip->pmic_chg_irq[interrupt]);
}
}
static void pm8921_chg_disable_irq(struct pm8921_chg_chip *chip, int interrupt)
{
if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) {
dev_dbg(chip->dev, "%d\n", chip->pmic_chg_irq[interrupt]);
disable_irq_nosync(chip->pmic_chg_irq[interrupt]);
}
}
static int pm_chg_get_rt_status(struct pm8921_chg_chip *chip, int irq_id)
{
return pm8xxx_read_irq_stat(chip->dev->parent,
chip->pmic_chg_irq[irq_id]);
}
/* Treat OverVoltage/UnderVoltage as source missing */
static int is_usb_chg_plugged_in(struct pm8921_chg_chip *chip)
{
int pres, ov, uv;
pres = pm_chg_get_rt_status(chip, USBIN_VALID_IRQ);
ov = pm_chg_get_rt_status(chip, USBIN_OV_IRQ);
uv = pm_chg_get_rt_status(chip, USBIN_UV_IRQ);
return pres && !ov && !uv;
}
/* Treat OverVoltage/UnderVoltage as source missing */
static int is_dc_chg_plugged_in(struct pm8921_chg_chip *chip)
{
int pres, ov, uv;
pres = pm_chg_get_rt_status(chip, DCIN_VALID_IRQ);
ov = pm_chg_get_rt_status(chip, DCIN_OV_IRQ);
uv = pm_chg_get_rt_status(chip, DCIN_UV_IRQ);
return pres && !ov && !uv;
}
static int is_battery_charging(int fsm_state)
{
switch (fsm_state) {
case FSM_STATE_ATC_2A:
case FSM_STATE_ATC_2B:
case FSM_STATE_ON_CHG_AND_BAT_6:
case FSM_STATE_FAST_CHG_7:
case FSM_STATE_TRKL_CHG_8:
return 1;
}
return 0;
}
static void bms_notify(struct work_struct *work)
{
struct bms_notify *n = container_of(work, struct bms_notify, work);
if (n->is_charging)
pm8921_bms_charging_began();
else
pm8921_bms_charging_end();
}
static enum power_supply_property pm_power_props[] = {
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_ONLINE,
};
static char *pm_power_supplied_to[] = {
"battery",
};
static int pm_power_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct pm8921_chg_chip *chip;
switch (psp) {
case POWER_SUPPLY_PROP_PRESENT:
case POWER_SUPPLY_PROP_ONLINE:
if (psy->type == POWER_SUPPLY_TYPE_MAINS) {
chip = container_of(psy, struct pm8921_chg_chip,
dc_psy);
val->intval = is_dc_chg_plugged_in(chip);
}
if (psy->type == POWER_SUPPLY_TYPE_USB) {
chip = container_of(psy, struct pm8921_chg_chip,
usb_psy);
val->intval = is_usb_chg_plugged_in(chip);
}
break;
default:
return -EINVAL;
}
return 0;
}
static enum power_supply_property msm_batt_power_props[] = {
POWER_SUPPLY_PROP_STATUS,
POWER_SUPPLY_PROP_CHARGE_TYPE,
POWER_SUPPLY_PROP_HEALTH,
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_TECHNOLOGY,
POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
POWER_SUPPLY_PROP_VOLTAGE_NOW,
POWER_SUPPLY_PROP_CAPACITY,
POWER_SUPPLY_PROP_CURRENT_NOW,
};
static int get_prop_battery_mvolts(struct pm8921_chg_chip *chip)
{
int rc;
struct pm8921_adc_chan_result result;
rc = pm8921_adc_read(chip->vbat_channel, &result);
if (rc) {
pr_err("error reading adc channel = %d, rc = %d\n",
chip->vbat_channel, rc);
return rc;
}
pr_debug("mvolts phy = %lld meas = 0x%llx", result.physical,
result.measurement);
return (int)result.physical;
}
static int get_prop_batt_capacity(struct pm8921_chg_chip *chip)
{
int percent_soc = pm8921_bms_get_percent_charge();
if (percent_soc <= 10)
pr_warn("low battery charge = %d%%\n", percent_soc);
return percent_soc;
}
static int get_prop_batt_current(struct pm8921_chg_chip *chip)
{
int result_ma, rc;
rc = pm8921_bms_get_battery_current(&result_ma);
if (rc) {
pr_err("unable to get batt current rc = %d\n", rc);
return rc;
} else {
return result_ma;
}
}
static int get_prop_batt_health(struct pm8921_chg_chip *chip)
{
int temp;
temp = pm_chg_get_rt_status(chip, BATTTEMP_HOT_IRQ);
if (temp)
return POWER_SUPPLY_HEALTH_OVERHEAT;
temp = pm_chg_get_rt_status(chip, BATTTEMP_COLD_IRQ);
if (temp)
return POWER_SUPPLY_HEALTH_COLD;
return POWER_SUPPLY_HEALTH_GOOD;
}
static int get_prop_batt_present(struct pm8921_chg_chip *chip)
{
return pm_chg_get_rt_status(chip, BATT_INSERTED_IRQ);
}
static int get_prop_charge_type(struct pm8921_chg_chip *chip)
{
int temp;
temp = pm_chg_get_rt_status(chip, TRKLCHG_IRQ);
if (temp)
return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
temp = pm_chg_get_rt_status(chip, FASTCHG_IRQ);
if (temp)
return POWER_SUPPLY_CHARGE_TYPE_FAST;
return POWER_SUPPLY_CHARGE_TYPE_NONE;
}
static int get_prop_batt_status(struct pm8921_chg_chip *chip)
{
int temp = 0;
/* TODO reading the FSM state is more reliable */
temp = pm_chg_get_rt_status(chip, TRKLCHG_IRQ);
temp |= pm_chg_get_rt_status(chip, FASTCHG_IRQ);
if (temp)
return POWER_SUPPLY_STATUS_CHARGING;
/*
* The battery is not charging
* check the FET - if on battery is discharging
* - if off battery is isolated(full) and the system
* is being driven from a charger
*/
temp = pm_chg_get_rt_status(chip, BATFET_IRQ);
if (temp)
return POWER_SUPPLY_STATUS_DISCHARGING;
return POWER_SUPPLY_STATUS_FULL;
}
static int pm_batt_power_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct pm8921_chg_chip *chip = container_of(psy, struct pm8921_chg_chip,
batt_psy);
switch (psp) {
case POWER_SUPPLY_PROP_STATUS:
val->intval = get_prop_batt_status(chip);
break;
case POWER_SUPPLY_PROP_CHARGE_TYPE:
val->intval = get_prop_charge_type(chip);
break;
case POWER_SUPPLY_PROP_HEALTH:
val->intval = get_prop_batt_health(chip);
break;
case POWER_SUPPLY_PROP_PRESENT:
val->intval = get_prop_batt_present(chip);
break;
case POWER_SUPPLY_PROP_TECHNOLOGY:
val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
break;
case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
val->intval = chip->max_voltage;
break;
case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
val->intval = chip->min_voltage;
break;
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
val->intval = get_prop_battery_mvolts(chip);
break;
case POWER_SUPPLY_PROP_CAPACITY:
val->intval = get_prop_batt_capacity(chip);
break;
case POWER_SUPPLY_PROP_CURRENT_NOW:
val->intval = get_prop_batt_current(chip);
break;
default:
return -EINVAL;
}
return 0;
}
static void (*notify_vbus_state_func_ptr)(int);
static int usb_chg_current;
static DEFINE_SPINLOCK(vbus_lock);
int pm8921_charger_register_vbus_sn(void (*callback)(int))
{
pr_debug("%p\n", callback);
notify_vbus_state_func_ptr = callback;
return 0;
}
EXPORT_SYMBOL_GPL(pm8921_charger_register_vbus_sn);
/* this is passed to the hsusb via platform_data msm_otg_pdata */
void pm8921_charger_unregister_vbus_sn(void (*callback)(int))
{
pr_debug("%p\n", callback);
notify_vbus_state_func_ptr = NULL;
}
EXPORT_SYMBOL_GPL(pm8921_charger_unregister_vbus_sn);
static void notify_usb_of_the_plugin_event(int plugin)
{
plugin = !!plugin;
if (notify_vbus_state_func_ptr) {
pr_debug("notifying plugin\n");
(*notify_vbus_state_func_ptr) (plugin);
} else {
pr_debug("unable to notify plugin\n");
}
}
struct usb_ma_limit_entry {
int usb_ma;
u8 chg_iusb_value;
};
static struct usb_ma_limit_entry usb_ma_table[] = {
{100, 0},
{500, 1},
{700, 2},
{850, 3},
{900, 4},
{1100, 5},
{1300, 6},
{1500, 7},
};
/* assumes vbus_lock is held */
static void __pm8921_charger_vbus_draw(unsigned int mA)
{
int i, rc;
if (mA > 0 && mA <= 2) {
usb_chg_current = 0;
rc = pm_chg_iusbmax_set(the_chip,
usb_ma_table[0].chg_iusb_value);
if (rc) {
pr_err("unable to set iusb to %d rc = %d\n",
usb_ma_table[0].chg_iusb_value, rc);
}
rc = pm_chg_usb_suspend_enable(the_chip, 1);
if (rc)
pr_err("fail to set suspend bit rc=%d\n", rc);
} else {
rc = pm_chg_usb_suspend_enable(the_chip, 0);
if (rc)
pr_err("fail to reset suspend bit rc=%d\n", rc);
for (i = ARRAY_SIZE(usb_ma_table) - 1; i >= 0; i--) {
if (usb_ma_table[i].usb_ma <= mA)
break;
}
if (i < 0)
i = 0;
rc = pm_chg_iusbmax_set(the_chip,
usb_ma_table[i].chg_iusb_value);
if (rc) {
pr_err("unable to set iusb to %d rc = %d\n",
usb_ma_table[i].chg_iusb_value, rc);
}
}
}
/* USB calls these to tell us how much max usb current the system can draw */
void pm8921_charger_vbus_draw(unsigned int mA)
{
unsigned long flags;
pr_debug("Enter charge=%d\n", mA);
spin_lock_irqsave(&vbus_lock, flags);
if (the_chip) {
__pm8921_charger_vbus_draw(mA);
} else {
/*
* called before pmic initialized,
* save this value and use it at probe
*/
usb_chg_current = mA;
}
spin_unlock_irqrestore(&vbus_lock, flags);
}
EXPORT_SYMBOL_GPL(pm8921_charger_vbus_draw);
int pm8921_charger_enable(bool enable)
{
int rc;
if (!the_chip) {
pr_err("called before init\n");
return -EINVAL;
}
enable = !!enable;
rc = pm_chg_auto_enable(the_chip, enable);
if (rc)
pr_err("Failed rc=%d\n", rc);
return rc;
}
EXPORT_SYMBOL(pm8921_charger_enable);
static void handle_usb_insertion_removal(struct pm8921_chg_chip *chip)
{
int usb_present;
usb_present = is_usb_chg_plugged_in(chip);
if (chip->usb_present ^ usb_present) {
notify_usb_of_the_plugin_event(usb_present);
chip->usb_present = usb_present;
power_supply_changed(&chip->usb_psy);
}
}
static void handle_dc_removal_insertion(struct pm8921_chg_chip *chip)
{
int dc_present;
dc_present = is_dc_chg_plugged_in(chip);
if (chip->dc_present ^ dc_present) {
chip->dc_present = dc_present;
power_supply_changed(&chip->dc_psy);
}
}
static irqreturn_t usbin_valid_irq_handler(int irq, void *data)
{
handle_usb_insertion_removal(data);
return IRQ_HANDLED;
}
static irqreturn_t usbin_ov_irq_handler(int irq, void *data)
{
handle_usb_insertion_removal(data);
return IRQ_HANDLED;
}
static irqreturn_t batt_inserted_irq_handler(int irq, void *data)
{
struct pm8921_chg_chip *chip = data;
int status;
status = pm_chg_get_rt_status(chip,
BATT_INSERTED_IRQ);
pr_debug("battery present=%d", status);
power_supply_changed(&chip->batt_psy);
return IRQ_HANDLED;
}
/* this interrupt used to restart charging a battery */
static irqreturn_t vbatdet_low_irq_handler(int irq, void *data)
{
struct pm8921_chg_chip *chip = data;
pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
power_supply_changed(&chip->batt_psy);
power_supply_changed(&chip->usb_psy);
power_supply_changed(&chip->dc_psy);
return IRQ_HANDLED;
}
static irqreturn_t usbin_uv_irq_handler(int irq, void *data)
{
handle_usb_insertion_removal(data);
return IRQ_HANDLED;
}
static irqreturn_t vbat_ov_irq_handler(int irq, void *data)
{
pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
return IRQ_HANDLED;
}
static irqreturn_t chgwdog_irq_handler(int irq, void *data)
{
pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
return IRQ_HANDLED;
}
static irqreturn_t vcp_irq_handler(int irq, void *data)
{
pr_warning("VCP triggered BATDET forced on\n");
pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data));
return IRQ_HANDLED;
}
static irqreturn_t atcdone_irq_handler(int irq, void *data)
{
pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
return IRQ_HANDLED;
}
static irqreturn_t atcfail_irq_handler(int irq, void *data)
{
pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
return IRQ_HANDLED;
}
static irqreturn_t chgdone_irq_handler(int irq, void *data)
{
struct pm8921_chg_chip *chip = data;
pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data));
power_supply_changed(&chip->batt_psy);
power_supply_changed(&chip->usb_psy);
power_supply_changed(&chip->dc_psy);
return IRQ_HANDLED;
}
static irqreturn_t chgfail_irq_handler(int irq, void *data)
{
struct pm8921_chg_chip *chip = data;
pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data));
power_supply_changed(&chip->batt_psy);
power_supply_changed(&chip->usb_psy);
power_supply_changed(&chip->dc_psy);
return IRQ_HANDLED;
}
static irqreturn_t chgstate_irq_handler(int irq, void *data)
{
struct pm8921_chg_chip *chip = data;
int new_is_charging = 0, fsm_state;
pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data));
power_supply_changed(&chip->batt_psy);
power_supply_changed(&chip->usb_psy);
power_supply_changed(&chip->dc_psy);
fsm_state = pm_chg_get_fsm_state(chip);
new_is_charging = is_battery_charging(fsm_state);
if (chip->bms_notify.is_charging ^ new_is_charging) {
chip->bms_notify.is_charging = new_is_charging;
schedule_work(&(chip->bms_notify.work));
}
return IRQ_HANDLED;
}
static irqreturn_t loop_change_irq_handler(int irq, void *data)
{
pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
return IRQ_HANDLED;
}
static irqreturn_t fastchg_irq_handler(int irq, void *data)
{
struct pm8921_chg_chip *chip = data;
power_supply_changed(&chip->batt_psy);
return IRQ_HANDLED;
}
static irqreturn_t trklchg_irq_handler(int irq, void *data)
{
struct pm8921_chg_chip *chip = data;
power_supply_changed(&chip->batt_psy);
return IRQ_HANDLED;
}
static irqreturn_t batt_removed_irq_handler(int irq, void *data)
{
struct pm8921_chg_chip *chip = data;
int status;
status = pm_chg_get_rt_status(chip, BATT_REMOVED_IRQ);
pr_debug("battery present=%d state=%d", !status,
pm_chg_get_fsm_state(data));
power_supply_changed(&chip->batt_psy);
return IRQ_HANDLED;
}
static irqreturn_t batttemp_hot_irq_handler(int irq, void *data)
{
struct pm8921_chg_chip *chip = data;
power_supply_changed(&chip->batt_psy);
return IRQ_HANDLED;
}
static irqreturn_t chghot_irq_handler(int irq, void *data)
{
struct pm8921_chg_chip *chip = data;
pr_debug("Chg hot fsm_state=%d\n", pm_chg_get_fsm_state(data));
power_supply_changed(&chip->batt_psy);
power_supply_changed(&chip->usb_psy);
power_supply_changed(&chip->dc_psy);
return IRQ_HANDLED;
}
static irqreturn_t batttemp_cold_irq_handler(int irq, void *data)
{
struct pm8921_chg_chip *chip = data;
pr_debug("Batt cold fsm_state=%d\n", pm_chg_get_fsm_state(data));
power_supply_changed(&chip->batt_psy);
power_supply_changed(&chip->usb_psy);
power_supply_changed(&chip->dc_psy);
return IRQ_HANDLED;
}
static irqreturn_t chg_gone_irq_handler(int irq, void *data)
{
struct pm8921_chg_chip *chip = data;
pr_debug("Chg gone fsm_state=%d\n", pm_chg_get_fsm_state(data));
power_supply_changed(&chip->batt_psy);
power_supply_changed(&chip->usb_psy);
power_supply_changed(&chip->dc_psy);
return IRQ_HANDLED;
}
static irqreturn_t bat_temp_ok_irq_handler(int irq, void *data)
{
struct pm8921_chg_chip *chip = data;
pr_debug("batt temp ok fsm_state=%d\n", pm_chg_get_fsm_state(data));
power_supply_changed(&chip->batt_psy);
power_supply_changed(&chip->usb_psy);
power_supply_changed(&chip->dc_psy);
return IRQ_HANDLED;
}
static irqreturn_t coarse_det_low_irq_handler(int irq, void *data)
{
pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
return IRQ_HANDLED;
}
static irqreturn_t vdd_loop_irq_handler(int irq, void *data)
{
pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
return IRQ_HANDLED;
}
static irqreturn_t vreg_ov_irq_handler(int irq, void *data)
{
pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
return IRQ_HANDLED;
}
static irqreturn_t vbatdet_irq_handler(int irq, void *data)
{
pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
return IRQ_HANDLED;
}
static irqreturn_t batfet_irq_handler(int irq, void *data)
{
struct pm8921_chg_chip *chip = data;
pr_debug("vreg ov\n");
power_supply_changed(&chip->batt_psy);
return IRQ_HANDLED;
}
static irqreturn_t dcin_valid_irq_handler(int irq, void *data)
{
handle_dc_removal_insertion(data);
return IRQ_HANDLED;
}
static irqreturn_t dcin_ov_irq_handler(int irq, void *data)
{
handle_dc_removal_insertion(data);
return IRQ_HANDLED;
}
static irqreturn_t dcin_uv_irq_handler(int irq, void *data)
{
handle_dc_removal_insertion(data);
return IRQ_HANDLED;
}
/**
* set_disable_status_param -
*
* Internal function to disable battery charging and also disable drawing
* any current from the source. The device is forced to run on a battery
* after this.
*/
static int set_disable_status_param(const char *val, struct kernel_param *kp)
{
int ret;
struct pm8921_chg_chip *chip = the_chip;
ret = param_set_int(val, kp);
if (ret) {
pr_err("error setting value %d\n", ret);
return ret;
}
pr_info("factory set disable param to %d\n", charging_disabled);
if (chip) {
pm_chg_auto_enable(chip, !charging_disabled);
pm_chg_charge_dis(chip, charging_disabled);
}
return 0;
}
module_param_call(disabled, set_disable_status_param, param_get_uint,
&charging_disabled, 0644);
static void free_irqs(struct pm8921_chg_chip *chip)
{
int i;
for (i = 0; i < PM_CHG_MAX_INTS; i++)
if (chip->pmic_chg_irq[i]) {
free_irq(chip->pmic_chg_irq[i], chip);
chip->pmic_chg_irq[i] = 0;
}
}
/* determines the initial present states and notifies msm_charger */
static void __devinit determine_initial_state(struct pm8921_chg_chip *chip)
{
unsigned long flags;
int fsm_state;
chip->dc_present = !!is_dc_chg_plugged_in(chip);
chip->usb_present = !!is_usb_chg_plugged_in(chip);
notify_usb_of_the_plugin_event(chip->usb_present);
pm8921_chg_enable_irq(chip, DCIN_VALID_IRQ);
pm8921_chg_enable_irq(chip, USBIN_VALID_IRQ);
pm8921_chg_enable_irq(chip, BATT_REMOVED_IRQ);
pm8921_chg_enable_irq(chip, BATT_REMOVED_IRQ);
pm8921_chg_enable_irq(chip, CHGSTATE_IRQ);
spin_lock_irqsave(&vbus_lock, flags);
if (usb_chg_current) {
/* reissue a vbus draw call */
__pm8921_charger_vbus_draw(usb_chg_current);
}
spin_unlock_irqrestore(&vbus_lock, flags);
fsm_state = pm_chg_get_fsm_state(chip);
if (is_battery_charging(fsm_state)) {
chip->bms_notify.is_charging = 1;
pm8921_bms_charging_began();
}
pr_debug("usb = %d, dc = %d batt = %d state=%d\n",
chip->usb_present,
chip->dc_present,
get_prop_batt_present(chip),
fsm_state);
}
struct pm_chg_irq_init_data {
unsigned int irq_id;
char *name;
unsigned long flags;
irqreturn_t (*handler)(int, void *);
};
#define CHG_IRQ(_id, _flags, _handler) \
{ \
.irq_id = _id, \
.name = #_id, \
.flags = _flags, \
.handler = _handler, \
}
struct pm_chg_irq_init_data chg_irq_data[] = {
CHG_IRQ(USBIN_VALID_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
usbin_valid_irq_handler),
CHG_IRQ(USBIN_OV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
usbin_ov_irq_handler),
CHG_IRQ(BATT_INSERTED_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
batt_inserted_irq_handler),
CHG_IRQ(VBATDET_LOW_IRQ, IRQF_TRIGGER_RISING, vbatdet_low_irq_handler),
CHG_IRQ(USBIN_UV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
usbin_uv_irq_handler),
CHG_IRQ(VBAT_OV_IRQ, IRQF_TRIGGER_RISING, vbat_ov_irq_handler),
CHG_IRQ(CHGWDOG_IRQ, IRQF_TRIGGER_RISING, chgwdog_irq_handler),
CHG_IRQ(VCP_IRQ, IRQF_TRIGGER_RISING, vcp_irq_handler),
CHG_IRQ(ATCDONE_IRQ, IRQF_TRIGGER_RISING, atcdone_irq_handler),
CHG_IRQ(ATCFAIL_IRQ, IRQF_TRIGGER_RISING, atcfail_irq_handler),
CHG_IRQ(CHGDONE_IRQ, IRQF_TRIGGER_RISING, chgdone_irq_handler),
CHG_IRQ(CHGFAIL_IRQ, IRQF_TRIGGER_RISING, chgfail_irq_handler),
CHG_IRQ(CHGSTATE_IRQ, IRQF_TRIGGER_RISING, chgstate_irq_handler),
CHG_IRQ(LOOP_CHANGE_IRQ, IRQF_TRIGGER_RISING, loop_change_irq_handler),
CHG_IRQ(FASTCHG_IRQ, IRQF_TRIGGER_RISING, fastchg_irq_handler),
CHG_IRQ(TRKLCHG_IRQ, IRQF_TRIGGER_RISING, trklchg_irq_handler),
CHG_IRQ(BATT_REMOVED_IRQ, IRQF_TRIGGER_RISING,
batt_removed_irq_handler),
CHG_IRQ(BATTTEMP_HOT_IRQ, IRQF_TRIGGER_RISING,
batttemp_hot_irq_handler),
CHG_IRQ(CHGHOT_IRQ, IRQF_TRIGGER_RISING, chghot_irq_handler),
CHG_IRQ(BATTTEMP_COLD_IRQ, IRQF_TRIGGER_RISING,
batttemp_cold_irq_handler),
CHG_IRQ(CHG_GONE_IRQ, IRQF_TRIGGER_RISING, chg_gone_irq_handler),
CHG_IRQ(BAT_TEMP_OK_IRQ, IRQF_TRIGGER_RISING, bat_temp_ok_irq_handler),
CHG_IRQ(COARSE_DET_LOW_IRQ, IRQF_TRIGGER_RISING,
coarse_det_low_irq_handler),
CHG_IRQ(VDD_LOOP_IRQ, IRQF_TRIGGER_RISING, vdd_loop_irq_handler),
CHG_IRQ(VREG_OV_IRQ, IRQF_TRIGGER_RISING, vreg_ov_irq_handler),
CHG_IRQ(VBATDET_IRQ, IRQF_TRIGGER_RISING, vbatdet_irq_handler),
CHG_IRQ(BATFET_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
batfet_irq_handler),
CHG_IRQ(DCIN_VALID_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
dcin_valid_irq_handler),
CHG_IRQ(DCIN_OV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
dcin_ov_irq_handler),
CHG_IRQ(DCIN_UV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
dcin_uv_irq_handler),
};
static int __devinit request_irqs(struct pm8921_chg_chip *chip,
struct platform_device *pdev)
{
struct resource *res;
int ret, i;
ret = 0;
bitmap_fill(chip->enabled_irqs, PM_CHG_MAX_INTS);
for (i = 0; i < ARRAY_SIZE(chg_irq_data); i++) {
res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
chg_irq_data[i].name);
if (res == NULL) {
pr_err("couldn't find %s\n", chg_irq_data[i].name);
goto err_out;
}
ret = request_irq(res->start, chg_irq_data[i].handler,
chg_irq_data[i].flags,
chg_irq_data[i].name, chip);
if (ret < 0) {
pr_err("couldn't request %d (%s) %d\n", res->start,
chg_irq_data[i].name, ret);
goto err_out;
}
chip->pmic_chg_irq[chg_irq_data[i].irq_id] = res->start;
pm8921_chg_disable_irq(chip, chg_irq_data[i].irq_id);
}
return 0;
err_out:
free_irqs(chip);
return -EINVAL;
}
#define ENUM_TIMER_STOP_BIT BIT(1)
#define BOOT_DONE_BIT BIT(6)
#define CHG_BATFET_ON_BIT BIT(3)
#define CHG_VCP_EN BIT(0)
#define CHG_BAT_TEMP_DIS_BIT BIT(2)
#define SAFE_CURRENT_MA 1500
static int __devinit pm8921_chg_hw_init(struct pm8921_chg_chip *chip)
{
int rc;
rc = pm_chg_masked_write(chip, SYS_CONFIG_2,
BOOT_DONE_BIT, BOOT_DONE_BIT);
if (rc) {
pr_err("Failed to set BOOT_DONE_BIT rc=%d\n", rc);
return rc;
}
rc = pm_chg_vddsafe_set(chip, chip->max_voltage);
if (rc) {
pr_err("Failed to set safe voltage to %d rc=%d\n",
chip->max_voltage, rc);
return rc;
}
rc = pm_chg_vbatdet_set(chip, chip->resume_voltage);
if (rc) {
pr_err("Failed to set vbatdet comprator voltage to %d rc=%d\n",
chip->resume_voltage, rc);
return rc;
}
rc = pm_chg_vddmax_set(chip, chip->max_voltage);
if (rc) {
pr_err("Failed to set max voltage to %d rc=%d\n",
chip->max_voltage, rc);
return rc;
}
rc = pm_chg_ibatsafe_set(chip, SAFE_CURRENT_MA);
if (rc) {
pr_err("Failed to set max voltage to %d rc=%d\n",
SAFE_CURRENT_MA, rc);
return rc;
}
/* TODO needs to be changed as per the temeperature of the battery */
rc = pm_chg_ibatmax_set(chip, 400);
if (rc) {
pr_err("Failed to set max current to 400 rc=%d\n", rc);
return rc;
}
rc = pm_chg_iterm_set(chip, chip->term_current);
if (rc) {
pr_err("Failed to set term current to %d rc=%d\n",
chip->term_current, rc);
return rc;
}
/* Disable the ENUM TIMER */
rc = pm_chg_masked_write(chip, PBL_ACCESS2, ENUM_TIMER_STOP_BIT,
ENUM_TIMER_STOP_BIT);
if (rc) {
pr_err("Failed to set enum timer stop rc=%d\n", rc);
return rc;
}
/* init with the lowest USB current */
rc = pm_chg_iusbmax_set(chip, usb_ma_table[0].chg_iusb_value);
if (rc) {
pr_err("Failed to set usb max to %d rc=%d\n",
usb_ma_table[0].chg_iusb_value, rc);
return rc;
}
rc = pm_chg_disable_wd(chip);
if (rc) {
pr_err("Failed to disable wd rc=%d\n", rc);
return rc;
}
rc = pm_chg_masked_write(chip, CHG_CNTRL_2,
CHG_BAT_TEMP_DIS_BIT, 0);
if (rc) {
pr_err("Failed to enable temp control chg rc=%d\n", rc);
return rc;
}
/* switch to a 3.2Mhz for the buck */
rc = pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CLOCK_CTRL, 0x15);
if (rc) {
pr_err("Failed to switch buck clk rc=%d\n", rc);
return rc;
}
/* Workarounds for die 1.1 and 1.0 */
if (pm8xxx_get_revision(chip->dev->parent) < PM8XXX_REVISION_8921_2p0) {
pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST2, 0xF1);
pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0x8C);
pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0xCE);
pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0xD8);
pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0xF1);
}
rc = pm_chg_charge_dis(chip, charging_disabled);
if (rc) {
pr_err("Failed to disable CHG_CHARGE_DIS bit rc=%d\n", rc);
return rc;
}
rc = pm_chg_auto_enable(chip, !charging_disabled);
if (rc) {
pr_err("Failed to enable charging rc=%d\n", rc);
return rc;
}
return 0;
}
static int get_rt_status(void *data, u64 * val)
{
int i = (int)data;
int ret;
/* global irq number is passed in via data */
ret = pm_chg_get_rt_status(the_chip, i);
*val = ret;
return 0;
}
DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n");
static int get_fsm_status(void *data, u64 * val)
{
u8 temp;
temp = pm_chg_get_fsm_state(the_chip);
*val = temp;
return 0;
}
DEFINE_SIMPLE_ATTRIBUTE(fsm_fops, get_fsm_status, NULL, "%llu\n");
static int get_reg(void *data, u64 * val)
{
int addr = (int)data;
int ret;
u8 temp;
ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp);
if (ret) {
pr_err("pm8xxx_readb to %x value =%d errored = %d\n",
addr, temp, ret);
return -EAGAIN;
}
*val = temp;
return 0;
}
static int set_reg(void *data, u64 val)
{
int addr = (int)data;
int ret;
u8 temp;
temp = (u8) val;
ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp);
if (ret) {
pr_err("pm8xxx_writeb to %x value =%d errored = %d\n",
addr, temp, ret);
return -EAGAIN;
}
return 0;
}
DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
static void create_debugfs_entries(struct pm8921_chg_chip *chip)
{
int i;
chip->dent = debugfs_create_dir("pm8921_chg", NULL);
if (IS_ERR(chip->dent)) {
pr_err("pmic charger couldnt create debugfs dir\n");
return;
}
debugfs_create_file("CHG_CNTRL", 0644, chip->dent,
(void *)CHG_CNTRL, &reg_fops);
debugfs_create_file("CHG_CNTRL_2", 0644, chip->dent,
(void *)CHG_CNTRL_2, &reg_fops);
debugfs_create_file("CHG_CNTRL_3", 0644, chip->dent,
(void *)CHG_CNTRL_3, &reg_fops);
debugfs_create_file("PBL_ACCESS1", 0644, chip->dent,
(void *)PBL_ACCESS1, &reg_fops);
debugfs_create_file("PBL_ACCESS2", 0644, chip->dent,
(void *)PBL_ACCESS2, &reg_fops);
debugfs_create_file("SYS_CONFIG_1", 0644, chip->dent,
(void *)SYS_CONFIG_1, &reg_fops);
debugfs_create_file("SYS_CONFIG_2", 0644, chip->dent,
(void *)SYS_CONFIG_2, &reg_fops);
debugfs_create_file("CHG_VDD_MAX", 0644, chip->dent,
(void *)CHG_VDD_MAX, &reg_fops);
debugfs_create_file("CHG_VDD_SAFE", 0644, chip->dent,
(void *)CHG_VDD_SAFE, &reg_fops);
debugfs_create_file("CHG_VBAT_DET", 0644, chip->dent,
(void *)CHG_VBAT_DET, &reg_fops);
debugfs_create_file("CHG_IBAT_MAX", 0644, chip->dent,
(void *)CHG_IBAT_MAX, &reg_fops);
debugfs_create_file("CHG_IBAT_SAFE", 0644, chip->dent,
(void *)CHG_IBAT_SAFE, &reg_fops);
debugfs_create_file("CHG_VIN_MIN", 0644, chip->dent,
(void *)CHG_VIN_MIN, &reg_fops);
debugfs_create_file("CHG_VTRICKLE", 0644, chip->dent,
(void *)CHG_VTRICKLE, &reg_fops);
debugfs_create_file("CHG_ITRICKLE", 0644, chip->dent,
(void *)CHG_ITRICKLE, &reg_fops);
debugfs_create_file("CHG_ITERM", 0644, chip->dent,
(void *)CHG_ITERM, &reg_fops);
debugfs_create_file("CHG_TCHG_MAX", 0644, chip->dent,
(void *)CHG_TCHG_MAX, &reg_fops);
debugfs_create_file("CHG_TWDOG", 0644, chip->dent,
(void *)CHG_TWDOG, &reg_fops);
debugfs_create_file("CHG_TEMP_THRESH", 0644, chip->dent,
(void *)CHG_TEMP_THRESH, &reg_fops);
debugfs_create_file("CHG_COMP_OVR", 0644, chip->dent,
(void *)CHG_COMP_OVR, &reg_fops);
debugfs_create_file("CHG_BUCK_CTRL_TEST1", 0644, chip->dent,
(void *)CHG_BUCK_CTRL_TEST1, &reg_fops);
debugfs_create_file("CHG_BUCK_CTRL_TEST2", 0644, chip->dent,
(void *)CHG_BUCK_CTRL_TEST2, &reg_fops);
debugfs_create_file("CHG_BUCK_CTRL_TEST3", 0644, chip->dent,
(void *)CHG_BUCK_CTRL_TEST3, &reg_fops);
debugfs_create_file("CHG_TEST", 0644, chip->dent,
(void *)CHG_TEST, &reg_fops);
debugfs_create_file("FSM_STATE", 0644, chip->dent, NULL,
&fsm_fops);
for (i = 0; i < ARRAY_SIZE(chg_irq_data); i++) {
if (chip->pmic_chg_irq[chg_irq_data[i].irq_id])
debugfs_create_file(chg_irq_data[i].name, 0444,
chip->dent,
(void *)chg_irq_data[i].irq_id,
&rt_fops);
}
}
static int __devinit pm8921_charger_probe(struct platform_device *pdev)
{
int rc = 0;
struct pm8921_chg_chip *chip;
const struct pm8921_charger_platform_data *pdata
= pdev->dev.platform_data;
if (!pdata) {
pr_err("missing platform data\n");
return -EINVAL;
}
chip = kzalloc(sizeof(struct pm8921_chg_chip),
GFP_KERNEL);
if (!chip) {
pr_err("Cannot allocate pm_chg_chip\n");
return -ENOMEM;
}
chip->dev = &pdev->dev;
chip->safety_time = pdata->safety_time;
chip->update_time = pdata->update_time;
chip->max_voltage = pdata->max_voltage;
chip->min_voltage = pdata->min_voltage;
chip->resume_voltage = pdata->resume_voltage;
chip->term_current = pdata->term_current;
chip->vbat_channel = pdata->charger_cdata.vbat_channel;
rc = pm8921_chg_hw_init(chip);
if (rc) {
pr_err("couldn't init hardware rc=%d\n", rc);
goto free_chip;
}
chip->usb_psy.name = "usb",
chip->usb_psy.type = POWER_SUPPLY_TYPE_USB,
chip->usb_psy.supplied_to = pm_power_supplied_to,
chip->usb_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to),
chip->usb_psy.properties = pm_power_props,
chip->usb_psy.num_properties = ARRAY_SIZE(pm_power_props),
chip->usb_psy.get_property = pm_power_get_property,
chip->dc_psy.name = "ac",
chip->dc_psy.type = POWER_SUPPLY_TYPE_MAINS,
chip->dc_psy.supplied_to = pm_power_supplied_to,
chip->dc_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to),
chip->dc_psy.properties = pm_power_props,
chip->dc_psy.num_properties = ARRAY_SIZE(pm_power_props),
chip->dc_psy.get_property = pm_power_get_property,
chip->batt_psy.name = "battery",
chip->batt_psy.type = POWER_SUPPLY_TYPE_BATTERY,
chip->batt_psy.properties = msm_batt_power_props,
chip->batt_psy.num_properties = ARRAY_SIZE(msm_batt_power_props),
chip->batt_psy.get_property = pm_batt_power_get_property,
rc = power_supply_register(chip->dev, &chip->usb_psy);
if (rc < 0) {
pr_err("power_supply_register usb failed rc = %d\n", rc);
goto free_irq;
}
rc = power_supply_register(chip->dev, &chip->dc_psy);
if (rc < 0) {
pr_err("power_supply_register dc failed rc = %d\n", rc);
goto unregister_usb;
}
rc = power_supply_register(chip->dev, &chip->batt_psy);
if (rc < 0) {
pr_err("power_supply_register batt failed rc = %d\n", rc);
goto unregister_dc;
}
rc = request_irqs(chip, pdev);
if (rc) {
pr_err("couldn't register interrupts rc=%d\n", rc);
goto unregister_batt;
}
platform_set_drvdata(pdev, chip);
the_chip = chip;
create_debugfs_entries(chip);
INIT_WORK(&chip->bms_notify.work, bms_notify);
/* determine what state the charger is in */
determine_initial_state(chip);
return 0;
free_irq:
free_irqs(chip);
unregister_batt:
power_supply_unregister(&chip->batt_psy);
unregister_dc:
power_supply_unregister(&chip->dc_psy);
unregister_usb:
power_supply_unregister(&chip->usb_psy);
free_chip:
kfree(chip);
return rc;
}
static int __devexit pm8921_charger_remove(struct platform_device *pdev)
{
struct pm8921_chg_chip *chip = platform_get_drvdata(pdev);
free_irqs(chip);
platform_set_drvdata(pdev, NULL);
the_chip = NULL;
kfree(chip);
return 0;
}
static struct platform_driver pm8921_charger_driver = {
.probe = pm8921_charger_probe,
.remove = __devexit_p(pm8921_charger_remove),
.driver = {
.name = PM8921_CHARGER_DEV_NAME,
.owner = THIS_MODULE,
},
};
static int __init pm8921_charger_init(void)
{
return platform_driver_register(&pm8921_charger_driver);
}
static void __exit pm8921_charger_exit(void)
{
platform_driver_unregister(&pm8921_charger_driver);
}
late_initcall(pm8921_charger_init);
module_exit(pm8921_charger_exit);
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("PMIC8921 charger/battery driver");
MODULE_VERSION("1.0");
MODULE_ALIAS("platform:" PM8921_CHARGER_DEV_NAME);