From b1b56872be3b36af19313bf0953b1361c36b4a98 Mon Sep 17 00:00:00 2001 From: Ramakrishna Pallala Date: Thu, 23 Aug 2012 06:50:21 +0530 Subject: power_supply: Add new power supply AUTHENTIC property It is possible that users can use non-standard chargers or use invalid batteries especially with mobile devices. This patch adds a new power supply property called 'AUTHENTIC' to indicate this to the user(user space). Signed-off-by: Ramakrishna Pallala Signed-off-by: Anton Vorontsov --- include/linux/power_supply.h | 1 + 1 file changed, 1 insertion(+) (limited to 'include') diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index 0bafbb15f29c..204d43dc6ddc 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -88,6 +88,7 @@ enum power_supply_property { POWER_SUPPLY_PROP_HEALTH, POWER_SUPPLY_PROP_PRESENT, POWER_SUPPLY_PROP_ONLINE, + POWER_SUPPLY_PROP_AUTHENTIC, POWER_SUPPLY_PROP_TECHNOLOGY, POWER_SUPPLY_PROP_CYCLE_COUNT, POWER_SUPPLY_PROP_VOLTAGE_MAX, -- cgit v1.2.3-58-ga151 From fd65ee5f1c21af9ff9f113842d513ca50749ad68 Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Fri, 27 Jul 2012 14:01:37 +0900 Subject: charger-manager: Use replacement variable to check state of battery This patch remove unnecessary variable(cm->fullbatt_vchk_uV) by using 'desc->fullbatt_uV' field directly in fullbatt_handler() function to check the state of battery. Signed-off-by: Chanwoo Choi Signed-off-by: Myungjoo Ham Signed-off-by: Kyungmin Park Signed-off-by: Anton Vorontsov --- drivers/power/charger-manager.c | 2 +- include/linux/power/charger-manager.h | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) (limited to 'include') diff --git a/drivers/power/charger-manager.c b/drivers/power/charger-manager.c index 240de49934f1..cdf29d2eb7a5 100644 --- a/drivers/power/charger-manager.c +++ b/drivers/power/charger-manager.c @@ -415,7 +415,7 @@ static void fullbatt_vchk(struct work_struct *work) return; } - diff = cm->fullbatt_vchk_uV; + diff = desc->fullbatt_uV; diff -= batt_uV; dev_dbg(cm->dev, "VBATT dropped %duV after full-batt.\n", diff); diff --git a/include/linux/power/charger-manager.h b/include/linux/power/charger-manager.h index cd22029e32aa..7d7b90fb7b4b 100644 --- a/include/linux/power/charger-manager.h +++ b/include/linux/power/charger-manager.h @@ -194,8 +194,6 @@ struct charger_desc { * @charger_enabled: the state of charger * @fullbatt_vchk_jiffies_at: * jiffies at the time full battery check will occur. - * @fullbatt_vchk_uV: voltage in microvolt - * criteria for full battery * @fullbatt_vchk_work: work queue for full battery check * @emergency_stop: * When setting true, stop charging @@ -218,7 +216,6 @@ struct charger_manager { bool charger_enabled; unsigned long fullbatt_vchk_jiffies_at; - unsigned int fullbatt_vchk_uV; struct delayed_work fullbatt_vchk_work; int emergency_stop; -- cgit v1.2.3-58-ga151 From c6b2744c5dd098e8bca2515e91219cf0206290c5 Mon Sep 17 00:00:00 2001 From: Anton Vorontsov Date: Wed, 22 Aug 2012 21:36:05 -0700 Subject: charger-manager: Fix struct charger_desc's misleading comment The comment says that charger_regulators is an array of regulator_bulk_data, which is not true, since it's actually a pointer to 'struct charger_regulator'. Signed-off-by: Anton Vorontsov --- include/linux/power/charger-manager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/linux/power/charger-manager.h b/include/linux/power/charger-manager.h index 7d7b90fb7b4b..b542270affc1 100644 --- a/include/linux/power/charger-manager.h +++ b/include/linux/power/charger-manager.h @@ -148,7 +148,7 @@ struct charger_regulator { * Specify where information for existance of battery can be obtained * @psy_charger_stat: the names of power-supply for chargers * @num_charger_regulator: the number of entries in charger_regulators - * @charger_regulators: array of regulator_bulk_data for chargers + * @charger_regulators: array of charger regulators * @psy_fuel_gauge: the name of power-supply for fuel gauge * @temperature_out_of_range: * Determine whether the status is overheat or cold or normal. -- cgit v1.2.3-58-ga151 From 2815b786c3bb86fff97f1f6e2f0874903ff2339b Mon Sep 17 00:00:00 2001 From: Ramakrishna Pallala Date: Mon, 30 Jul 2012 12:49:21 +0530 Subject: power_supply: Add new power supply properties CHARGE_CURRENT/VOLTAGE_MAX There are different types of chargers avalibale like AC, Solar, USB, etc.. Even in USB we have different types SDP/DCP/CDP/ACA and all these chargers have different o/p ratings. For example SDP supports only 500mA of charge current whereas AC charger can support upto 8A or more. Similarly batteries also come with charge current and voltage ratings and these ratings vary depending on its capacity and the technology used. This patch adds two new power supply properties CONSTANT_CHARGE_CURRENT_MAX and CONSTANT_CHARGE_CURRENT_MAX. Signed-off-by: Ramakrishna Pallala Signed-off-by: Anton Vorontsov --- Documentation/power/power_supply_class.txt | 4 ++++ drivers/power/power_supply_sysfs.c | 2 ++ include/linux/power_supply.h | 4 ++++ 3 files changed, 10 insertions(+) (limited to 'include') diff --git a/Documentation/power/power_supply_class.txt b/Documentation/power/power_supply_class.txt index 48badec1cec3..9c647bd7c5a9 100644 --- a/Documentation/power/power_supply_class.txt +++ b/Documentation/power/power_supply_class.txt @@ -116,8 +116,12 @@ be negative; there is no empty or full value. It is only useful for relative, time-based measurements. CONSTANT_CHARGE_CURRENT - constant charge current programmed by charger. +CONSTANT_CHARGE_CURRENT_MAX - maximum charge current supported by the +power supply object. CONSTANT_CHARGE_VOLTAGE - constant charge voltage programmed by charger. +CONSTANT_CHARGE_VOLTAGE_MAX - maximum charge voltage supported by the +power supply object. ENERGY_FULL, ENERGY_EMPTY - same as above but for energy. diff --git a/drivers/power/power_supply_sysfs.c b/drivers/power/power_supply_sysfs.c index be34a6d4664d..395c2cfa16c0 100644 --- a/drivers/power/power_supply_sysfs.c +++ b/drivers/power/power_supply_sysfs.c @@ -161,7 +161,9 @@ static struct device_attribute power_supply_attrs[] = { POWER_SUPPLY_ATTR(charge_avg), POWER_SUPPLY_ATTR(charge_counter), POWER_SUPPLY_ATTR(constant_charge_current), + POWER_SUPPLY_ATTR(constant_charge_current_max), POWER_SUPPLY_ATTR(constant_charge_voltage), + POWER_SUPPLY_ATTR(constant_charge_voltage_max), POWER_SUPPLY_ATTR(energy_full_design), POWER_SUPPLY_ATTR(energy_empty_design), POWER_SUPPLY_ATTR(energy_full), diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index 204d43dc6ddc..e5ef45834c3c 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -111,7 +111,9 @@ enum power_supply_property { POWER_SUPPLY_PROP_CHARGE_AVG, POWER_SUPPLY_PROP_CHARGE_COUNTER, POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, + POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, + POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN, POWER_SUPPLY_PROP_ENERGY_FULL, @@ -249,6 +251,7 @@ static inline bool power_supply_is_amp_property(enum power_supply_property psp) case POWER_SUPPLY_PROP_CHARGE_AVG: case POWER_SUPPLY_PROP_CHARGE_COUNTER: case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: case POWER_SUPPLY_PROP_CURRENT_MAX: case POWER_SUPPLY_PROP_CURRENT_NOW: case POWER_SUPPLY_PROP_CURRENT_AVG: @@ -277,6 +280,7 @@ static inline bool power_supply_is_watt_property(enum power_supply_property psp) case POWER_SUPPLY_PROP_VOLTAGE_AVG: case POWER_SUPPLY_PROP_VOLTAGE_OCV: case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: case POWER_SUPPLY_PROP_POWER_NOW: return 1; default: -- cgit v1.2.3-58-ga151 From a830d28b48bf92944e57058e87d17cee5a7cd2a1 Mon Sep 17 00:00:00 2001 From: "Jett.Zhou" Date: Fri, 27 Jul 2012 16:27:16 +0800 Subject: power_supply: Enable battery-charger for 88pm860x There are charger and battery measurement feature for 88pm860x PMIC. For charger, it can support pre-charge with small current when battery is nearly exausted and then changed into fast-charge with CC&CV mode. For battery monitor, it can support battery measurement such as vbat,vsys,vchg and ibat etc,it can aslo accumulating the Coulomb value charged or discharged from battery based on Conlomb Counter, we use it to estimate battery capacity. Signed-off-by: Jett.Zhou Signed-off-by: Anton Vorontsov --- drivers/mfd/88pm860x-core.c | 22 +- drivers/power/88pm860x_battery.c | 1041 ++++++++++++++++++++++++++++++++++++++ drivers/power/88pm860x_charger.c | 746 +++++++++++++++++++++++++++ drivers/power/Kconfig | 12 + drivers/power/Makefile | 2 + include/linux/mfd/88pm860x.h | 84 ++- 6 files changed, 1905 insertions(+), 2 deletions(-) create mode 100644 drivers/power/88pm860x_battery.c create mode 100644 drivers/power/88pm860x_charger.c (limited to 'include') diff --git a/drivers/mfd/88pm860x-core.c b/drivers/mfd/88pm860x-core.c index d09918cf1b15..229cb2920089 100644 --- a/drivers/mfd/88pm860x-core.c +++ b/drivers/mfd/88pm860x-core.c @@ -18,6 +18,7 @@ #include #include #include +#include #define INT_STATUS_NUM 3 @@ -84,7 +85,8 @@ static struct resource battery_resources[] __devinitdata = { static struct resource charger_resources[] __devinitdata = { {PM8607_IRQ_CHG, PM8607_IRQ_CHG, "charger detect", IORESOURCE_IRQ,}, {PM8607_IRQ_CHG_DONE, PM8607_IRQ_CHG_DONE, "charging done", IORESOURCE_IRQ,}, - {PM8607_IRQ_CHG_FAULT, PM8607_IRQ_CHG_FAULT, "charging timeout", IORESOURCE_IRQ,}, + {PM8607_IRQ_CHG_FAIL, PM8607_IRQ_CHG_FAIL, "charging timeout", IORESOURCE_IRQ,}, + {PM8607_IRQ_CHG_FAULT, PM8607_IRQ_CHG_FAULT, "charging fault", IORESOURCE_IRQ,}, {PM8607_IRQ_GPADC1, PM8607_IRQ_GPADC1, "battery temperature", IORESOURCE_IRQ,}, {PM8607_IRQ_VBAT, PM8607_IRQ_VBAT, "battery voltage", IORESOURCE_IRQ,}, {PM8607_IRQ_VCHG, PM8607_IRQ_VCHG, "vchg voltage", IORESOURCE_IRQ,}, @@ -155,10 +157,15 @@ static struct regulator_init_data preg_init_data = { .consumer_supplies = &preg_supply[0], }; +static struct regulator_bulk_data chg_desc_regulator_data[] = { + { .supply = "preg", }, +}; + static struct mfd_cell power_devs[] = { {"88pm860x-battery", -1,}, {"88pm860x-charger", -1,}, {"88pm860x-preg", -1,}, + {"charger-manager", -1,}, }; static struct mfd_cell rtc_devs[] = { @@ -791,6 +798,19 @@ static void __devinit device_power_init(struct pm860x_chip *chip, &preg_resources[0], chip->irq_base); if (ret < 0) dev_err(chip->dev, "Failed to add preg subdev\n"); + + if (pdata->chg_desc) { + pdata->chg_desc->charger_regulators = + &chg_desc_regulator_data[0]; + pdata->chg_desc->num_charger_regulators = + ARRAY_SIZE(chg_desc_regulator_data), + power_devs[3].platform_data = pdata->chg_desc; + power_devs[3].pdata_size = sizeof(*pdata->chg_desc); + ret = mfd_add_devices(chip->dev, 0, &power_devs[3], 1, + NULL, chip->irq_base); + if (ret < 0) + dev_err(chip->dev, "Failed to add chg-manager subdev\n"); + } } static void __devinit device_onkey_init(struct pm860x_chip *chip, diff --git a/drivers/power/88pm860x_battery.c b/drivers/power/88pm860x_battery.c new file mode 100644 index 000000000000..5e905f3db4bf --- /dev/null +++ b/drivers/power/88pm860x_battery.c @@ -0,0 +1,1041 @@ +/* + * Battery driver for Marvell 88PM860x PMIC + * + * Copyright (c) 2012 Marvell International Ltd. + * Author: Jett Zhou + * Haojian Zhuang + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* bit definitions of Status Query Interface 2 */ +#define STATUS2_CHG (1 << 2) +#define STATUS2_BAT (1 << 3) +#define STATUS2_VBUS (1 << 4) + +/* bit definitions of Measurement Enable 1 Register */ +#define MEAS1_TINT (1 << 3) +#define MEAS1_GP1 (1 << 5) + +/* bit definitions of Measurement Enable 3 Register */ +#define MEAS3_IBAT (1 << 0) +#define MEAS3_BAT_DET (1 << 1) +#define MEAS3_CC (1 << 2) + +/* bit definitions of Measurement Off Time Register */ +#define MEAS_OFF_SLEEP_EN (1 << 1) + +/* bit definitions of GPADC Bias Current 2 Register */ +#define GPBIAS2_GPADC1_SET (2 << 4) +/* GPADC1 Bias Current value in uA unit */ +#define GPBIAS2_GPADC1_UA ((GPBIAS2_GPADC1_SET >> 4) * 5 + 1) + +/* bit definitions of GPADC Misc 1 Register */ +#define GPMISC1_GPADC_EN (1 << 0) + +/* bit definitions of Charger Control 6 Register */ +#define CC6_BAT_DET_GPADC1 1 + +/* bit definitions of Coulomb Counter Reading Register */ +#define CCNT_AVG_SEL (4 << 3) + +/* bit definitions of RTC miscellaneous Register1 */ +#define RTC_SOC_5LSB (0x1F << 3) + +/* bit definitions of RTC Register1 */ +#define RTC_SOC_3MSB (0x7) + +/* bit definitions of Power up Log register */ +#define BAT_WU_LOG (1<<6) + +/* coulomb counter index */ +#define CCNT_POS1 0 +#define CCNT_POS2 1 +#define CCNT_NEG1 2 +#define CCNT_NEG2 3 +#define CCNT_SPOS 4 +#define CCNT_SNEG 5 + +/* OCV -- Open Circuit Voltage */ +#define OCV_MODE_ACTIVE 0 +#define OCV_MODE_SLEEP 1 + +/* Vbat range of CC for measuring Rbat */ +#define LOW_BAT_THRESHOLD 3600 +#define VBATT_RESISTOR_MIN 3800 +#define VBATT_RESISTOR_MAX 4100 + +/* TBAT for batt, TINT for chip itself */ +#define PM860X_TEMP_TINT (0) +#define PM860X_TEMP_TBAT (1) + +/* + * Battery temperature based on NTC resistor, defined + * corresponding resistor value -- Ohm / C degeree. + */ +#define TBAT_NEG_25D 127773 /* -25 */ +#define TBAT_NEG_10D 54564 /* -10 */ +#define TBAT_0D 32330 /* 0 */ +#define TBAT_10D 19785 /* 10 */ +#define TBAT_20D 12468 /* 20 */ +#define TBAT_30D 8072 /* 30 */ +#define TBAT_40D 5356 /* 40 */ + +struct pm860x_battery_info { + struct pm860x_chip *chip; + struct i2c_client *i2c; + struct device *dev; + + struct power_supply battery; + struct mutex lock; + int status; + int irq_cc; + int irq_batt; + int max_capacity; + int resistor; /* Battery Internal Resistor */ + int last_capacity; + int start_soc; + unsigned present:1; + unsigned temp_type:1; /* TINT or TBAT */ +}; + +struct ccnt { + unsigned long long int pos; + unsigned long long int neg; + unsigned int spos; + unsigned int sneg; + + int total_chg; /* mAh(3.6C) */ + int total_dischg; /* mAh(3.6C) */ +}; + +/* + * State of Charge. + * The first number is mAh(=3.6C), and the second number is percent point. + */ +int array_soc[][2] = { + {4170, 100}, {4154, 99}, {4136, 98}, {4122, 97}, {4107, 96}, + {4102, 95}, {4088, 94}, {4081, 93}, {4070, 92}, {4060, 91}, + {4053, 90}, {4044, 89}, {4035, 88}, {4028, 87}, {4019, 86}, + {4013, 85}, {4006, 84}, {3995, 83}, {3987, 82}, {3982, 81}, + {3976, 80}, {3968, 79}, {3962, 78}, {3954, 77}, {3946, 76}, + {3941, 75}, {3934, 74}, {3929, 73}, {3922, 72}, {3916, 71}, + {3910, 70}, {3904, 69}, {3898, 68}, {3892, 67}, {3887, 66}, + {3880, 65}, {3874, 64}, {3868, 63}, {3862, 62}, {3854, 61}, + {3849, 60}, {3843, 59}, {3840, 58}, {3833, 57}, {3829, 56}, + {3824, 55}, {3818, 54}, {3815, 53}, {3810, 52}, {3808, 51}, + {3804, 50}, {3801, 49}, {3798, 48}, {3796, 47}, {3792, 46}, + {3789, 45}, {3785, 44}, {3784, 43}, {3782, 42}, {3780, 41}, + {3777, 40}, {3776, 39}, {3774, 38}, {3772, 37}, {3771, 36}, + {3769, 35}, {3768, 34}, {3764, 33}, {3763, 32}, {3760, 31}, + {3760, 30}, {3754, 29}, {3750, 28}, {3749, 27}, {3744, 26}, + {3740, 25}, {3734, 24}, {3732, 23}, {3728, 22}, {3726, 21}, + {3720, 20}, {3716, 19}, {3709, 18}, {3703, 17}, {3698, 16}, + {3692, 15}, {3683, 14}, {3675, 13}, {3670, 12}, {3665, 11}, + {3661, 10}, {3649, 9}, {3637, 8}, {3622, 7}, {3609, 6}, + {3580, 5}, {3558, 4}, {3540, 3}, {3510, 2}, {3429, 1}, +}; + +static struct ccnt ccnt_data; + +/* + * register 1 bit[7:0] -- bit[11:4] of measured value of voltage + * register 0 bit[3:0] -- bit[3:0] of measured value of voltage + */ +static int measure_12bit_voltage(struct pm860x_battery_info *info, + int offset, int *data) +{ + unsigned char buf[2]; + int ret; + + ret = pm860x_bulk_read(info->i2c, offset, 2, buf); + if (ret < 0) + return ret; + + *data = ((buf[0] & 0xff) << 4) | (buf[1] & 0x0f); + /* V_MEAS(mV) = data * 1.8 * 1000 / (2^12) */ + *data = ((*data & 0xfff) * 9 * 25) >> 9; + return 0; +} + +static int measure_vbatt(struct pm860x_battery_info *info, int state, + int *data) +{ + unsigned char buf[5]; + int ret; + + switch (state) { + case OCV_MODE_ACTIVE: + ret = measure_12bit_voltage(info, PM8607_VBAT_MEAS1, data); + if (ret) + return ret; + /* V_BATT_MEAS(mV) = value * 3 * 1.8 * 1000 / (2^12) */ + *data *= 3; + break; + case OCV_MODE_SLEEP: + /* + * voltage value of VBATT in sleep mode is saved in different + * registers. + * bit[11:10] -- bit[7:6] of LDO9(0x18) + * bit[9:8] -- bit[7:6] of LDO8(0x17) + * bit[7:6] -- bit[7:6] of LDO7(0x16) + * bit[5:4] -- bit[7:6] of LDO6(0x15) + * bit[3:0] -- bit[7:4] of LDO5(0x14) + */ + ret = pm860x_bulk_read(info->i2c, PM8607_LDO5, 5, buf); + if (ret < 0) + return ret; + ret = ((buf[4] >> 6) << 10) | ((buf[3] >> 6) << 8) + | ((buf[2] >> 6) << 6) | ((buf[1] >> 6) << 4) + | (buf[0] >> 4); + /* V_BATT_MEAS(mV) = data * 3 * 1.8 * 1000 / (2^12) */ + *data = ((*data & 0xff) * 27 * 25) >> 9; + break; + default: + return -EINVAL; + } + return 0; +} + +/* + * Return value is signed data. + * Negative value means discharging, and positive value means charging. + */ +static int measure_current(struct pm860x_battery_info *info, int *data) +{ + unsigned char buf[2]; + short s; + int ret; + + ret = pm860x_bulk_read(info->i2c, PM8607_IBAT_MEAS1, 2, buf); + if (ret < 0) + return ret; + + s = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff); + /* current(mA) = value * 0.125 */ + *data = s >> 3; + return 0; +} + +static int set_charger_current(struct pm860x_battery_info *info, int data, + int *old) +{ + int ret; + + if (data < 50 || data > 1600 || !old) + return -EINVAL; + + data = ((data - 50) / 50) & 0x1f; + *old = pm860x_reg_read(info->i2c, PM8607_CHG_CTRL2); + *old = (*old & 0x1f) * 50 + 50; + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL2, 0x1f, data); + if (ret < 0) + return ret; + return 0; +} + +static int read_ccnt(struct pm860x_battery_info *info, int offset, + int *ccnt) +{ + unsigned char buf[2]; + int ret; + + ret = pm860x_set_bits(info->i2c, PM8607_CCNT, 7, offset & 7); + if (ret < 0) + goto out; + ret = pm860x_bulk_read(info->i2c, PM8607_CCNT_MEAS1, 2, buf); + if (ret < 0) + goto out; + *ccnt = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff); + return 0; +out: + return ret; +} + +static int calc_ccnt(struct pm860x_battery_info *info, struct ccnt *ccnt) +{ + unsigned int sum; + int ret; + int data; + + ret = read_ccnt(info, CCNT_POS1, &data); + if (ret) + goto out; + sum = data & 0xffff; + ret = read_ccnt(info, CCNT_POS2, &data); + if (ret) + goto out; + sum |= (data & 0xffff) << 16; + ccnt->pos += sum; + + ret = read_ccnt(info, CCNT_NEG1, &data); + if (ret) + goto out; + sum = data & 0xffff; + ret = read_ccnt(info, CCNT_NEG2, &data); + if (ret) + goto out; + sum |= (data & 0xffff) << 16; + sum = ~sum + 1; /* since it's negative */ + ccnt->neg += sum; + + ret = read_ccnt(info, CCNT_SPOS, &data); + if (ret) + goto out; + ccnt->spos += data; + ret = read_ccnt(info, CCNT_SNEG, &data); + if (ret) + goto out; + + /* + * charge(mAh) = count * 1.6984 * 1e(-8) + * = count * 16984 * 1.024 * 1.024 * 1.024 / (2 ^ 40) + * = count * 18236 / (2 ^ 40) + */ + ccnt->total_chg = (int) ((ccnt->pos * 18236) >> 40); + ccnt->total_dischg = (int) ((ccnt->neg * 18236) >> 40); + return 0; +out: + return ret; +} + +static int clear_ccnt(struct pm860x_battery_info *info, struct ccnt *ccnt) +{ + int data; + + memset(ccnt, 0, sizeof(*ccnt)); + /* read to clear ccnt */ + read_ccnt(info, CCNT_POS1, &data); + read_ccnt(info, CCNT_POS2, &data); + read_ccnt(info, CCNT_NEG1, &data); + read_ccnt(info, CCNT_NEG2, &data); + read_ccnt(info, CCNT_SPOS, &data); + read_ccnt(info, CCNT_SNEG, &data); + return 0; +} + +/* Calculate Open Circuit Voltage */ +static int calc_ocv(struct pm860x_battery_info *info, int *ocv) +{ + int ret; + int i; + int data; + int vbatt_avg; + int vbatt_sum; + int ibatt_avg; + int ibatt_sum; + + if (!ocv) + return -EINVAL; + + for (i = 0, ibatt_sum = 0, vbatt_sum = 0; i < 10; i++) { + ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data); + if (ret) + goto out; + vbatt_sum += data; + ret = measure_current(info, &data); + if (ret) + goto out; + ibatt_sum += data; + } + vbatt_avg = vbatt_sum / 10; + ibatt_avg = ibatt_sum / 10; + + mutex_lock(&info->lock); + if (info->present) + *ocv = vbatt_avg - ibatt_avg * info->resistor / 1000; + else + *ocv = vbatt_avg; + mutex_unlock(&info->lock); + dev_dbg(info->dev, "VBAT average:%d, OCV:%d\n", vbatt_avg, *ocv); + return 0; +out: + return ret; +} + +/* Calculate State of Charge (percent points) */ +static int calc_soc(struct pm860x_battery_info *info, int state, int *soc) +{ + int i; + int ocv; + int count; + int ret = -EINVAL; + + if (!soc) + return -EINVAL; + + switch (state) { + case OCV_MODE_ACTIVE: + ret = calc_ocv(info, &ocv); + break; + case OCV_MODE_SLEEP: + ret = measure_vbatt(info, OCV_MODE_SLEEP, &ocv); + break; + } + if (ret) + return ret; + + count = ARRAY_SIZE(array_soc); + if (ocv < array_soc[count - 1][0]) { + *soc = 0; + return 0; + } + + for (i = 0; i < count; i++) { + if (ocv >= array_soc[i][0]) { + *soc = array_soc[i][1]; + break; + } + } + return 0; +} + +static irqreturn_t pm860x_coulomb_handler(int irq, void *data) +{ + struct pm860x_battery_info *info = data; + + calc_ccnt(info, &ccnt_data); + return IRQ_HANDLED; +} + +static irqreturn_t pm860x_batt_handler(int irq, void *data) +{ + struct pm860x_battery_info *info = data; + int ret; + + mutex_lock(&info->lock); + ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2); + if (ret & STATUS2_BAT) { + info->present = 1; + info->temp_type = PM860X_TEMP_TBAT; + } else { + info->present = 0; + info->temp_type = PM860X_TEMP_TINT; + } + mutex_unlock(&info->lock); + /* clear ccnt since battery is attached or dettached */ + clear_ccnt(info, &ccnt_data); + return IRQ_HANDLED; +} + +static void pm860x_init_battery(struct pm860x_battery_info *info) +{ + unsigned char buf[2]; + int ret; + int data; + int bat_remove; + int soc; + + /* measure enable on GPADC1 */ + data = MEAS1_GP1; + if (info->temp_type == PM860X_TEMP_TINT) + data |= MEAS1_TINT; + ret = pm860x_set_bits(info->i2c, PM8607_MEAS_EN1, data, data); + if (ret) + goto out; + + /* measure enable on IBAT, BAT_DET, CC. IBAT is depend on CC. */ + data = MEAS3_IBAT | MEAS3_BAT_DET | MEAS3_CC; + ret = pm860x_set_bits(info->i2c, PM8607_MEAS_EN3, data, data); + if (ret) + goto out; + + /* measure disable CC in sleep time */ + ret = pm860x_reg_write(info->i2c, PM8607_MEAS_OFF_TIME1, 0x82); + if (ret) + goto out; + ret = pm860x_reg_write(info->i2c, PM8607_MEAS_OFF_TIME2, 0x6c); + if (ret) + goto out; + + /* enable GPADC */ + ret = pm860x_set_bits(info->i2c, PM8607_GPADC_MISC1, + GPMISC1_GPADC_EN, GPMISC1_GPADC_EN); + if (ret < 0) + goto out; + + /* detect battery via GPADC1 */ + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL6, + CC6_BAT_DET_GPADC1, CC6_BAT_DET_GPADC1); + if (ret < 0) + goto out; + + ret = pm860x_set_bits(info->i2c, PM8607_CCNT, 7 << 3, + CCNT_AVG_SEL); + if (ret < 0) + goto out; + + /* set GPADC1 bias */ + ret = pm860x_set_bits(info->i2c, PM8607_GP_BIAS2, 0xF << 4, + GPBIAS2_GPADC1_SET); + if (ret < 0) + goto out; + + /* check whether battery present) */ + mutex_lock(&info->lock); + ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2); + if (ret < 0) { + mutex_unlock(&info->lock); + goto out; + } + if (ret & STATUS2_BAT) { + info->present = 1; + info->temp_type = PM860X_TEMP_TBAT; + } else { + info->present = 0; + info->temp_type = PM860X_TEMP_TINT; + } + mutex_unlock(&info->lock); + + calc_soc(info, OCV_MODE_ACTIVE, &soc); + + data = pm860x_reg_read(info->i2c, PM8607_POWER_UP_LOG); + bat_remove = data & BAT_WU_LOG; + + dev_dbg(info->dev, "battery wake up? %s\n", + bat_remove != 0 ? "yes" : "no"); + + /* restore SOC from RTC domain register */ + if (bat_remove == 0) { + buf[0] = pm860x_reg_read(info->i2c, PM8607_RTC_MISC2); + buf[1] = pm860x_reg_read(info->i2c, PM8607_RTC1); + data = ((buf[1] & 0x3) << 5) | ((buf[0] >> 3) & 0x1F); + if (data > soc + 15) + info->start_soc = soc; + else if (data < soc - 15) + info->start_soc = soc; + else + info->start_soc = data; + dev_dbg(info->dev, "soc_rtc %d, soc_ocv :%d\n", data, soc); + } else { + pm860x_set_bits(info->i2c, PM8607_POWER_UP_LOG, + BAT_WU_LOG, BAT_WU_LOG); + info->start_soc = soc; + } + info->last_capacity = info->start_soc; + dev_dbg(info->dev, "init soc : %d\n", info->last_capacity); +out: + return; +} + +static void set_temp_threshold(struct pm860x_battery_info *info, + int min, int max) +{ + int data; + + /* (tmp << 8) / 1800 */ + if (min <= 0) + data = 0; + else + data = (min << 8) / 1800; + pm860x_reg_write(info->i2c, PM8607_GPADC1_HIGHTH, data); + dev_dbg(info->dev, "TEMP_HIGHTH : min: %d, 0x%x\n", min, data); + + if (max <= 0) + data = 0xff; + else + data = (max << 8) / 1800; + pm860x_reg_write(info->i2c, PM8607_GPADC1_LOWTH, data); + dev_dbg(info->dev, "TEMP_LOWTH:max : %d, 0x%x\n", max, data); +} + +static int measure_temp(struct pm860x_battery_info *info, int *data) +{ + int ret; + int temp; + int min; + int max; + + if (info->temp_type == PM860X_TEMP_TINT) { + ret = measure_12bit_voltage(info, PM8607_TINT_MEAS1, data); + if (ret) + return ret; + *data = (*data - 884) * 1000 / 3611; + } else { + ret = measure_12bit_voltage(info, PM8607_GPADC1_MEAS1, data); + if (ret) + return ret; + /* meausered Vtbat(mV) / Ibias_current(11uA)*/ + *data = (*data * 1000) / GPBIAS2_GPADC1_UA; + + if (*data > TBAT_NEG_25D) { + temp = -30; /* over cold , suppose -30 roughly */ + max = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000; + set_temp_threshold(info, 0, max); + } else if (*data > TBAT_NEG_10D) { + temp = -15; /* -15 degree, code */ + max = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000; + set_temp_threshold(info, 0, max); + } else if (*data > TBAT_0D) { + temp = -5; /* -5 degree */ + min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000; + max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000; + set_temp_threshold(info, min, max); + } else if (*data > TBAT_10D) { + temp = 5; /* in range of (0, 10) */ + min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000; + max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000; + set_temp_threshold(info, min, max); + } else if (*data > TBAT_20D) { + temp = 15; /* in range of (10, 20) */ + min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000; + max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000; + set_temp_threshold(info, min, max); + } else if (*data > TBAT_30D) { + temp = 25; /* in range of (20, 30) */ + min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000; + max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000; + set_temp_threshold(info, min, max); + } else if (*data > TBAT_40D) { + temp = 35; /* in range of (30, 40) */ + min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000; + max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000; + set_temp_threshold(info, min, max); + } else { + min = TBAT_40D * GPBIAS2_GPADC1_UA / 1000; + set_temp_threshold(info, min, 0); + temp = 45; /* over heat ,suppose 45 roughly */ + } + + dev_dbg(info->dev, "temp_C:%d C,temp_mv:%d mv\n", temp, *data); + *data = temp; + } + return 0; +} + +static int calc_resistor(struct pm860x_battery_info *info) +{ + int vbatt_sum1; + int vbatt_sum2; + int chg_current; + int ibatt_sum1; + int ibatt_sum2; + int data; + int ret; + int i; + + ret = measure_current(info, &data); + /* make sure that charging is launched by data > 0 */ + if (ret || data < 0) + goto out; + + ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data); + if (ret) + goto out; + /* calculate resistor only in CC charge mode */ + if (data < VBATT_RESISTOR_MIN || data > VBATT_RESISTOR_MAX) + goto out; + + /* current is saved */ + if (set_charger_current(info, 500, &chg_current)) + goto out; + + /* + * set charge current as 500mA, wait about 500ms till charging + * process is launched and stable with the newer charging current. + */ + msleep(500); + + for (i = 0, vbatt_sum1 = 0, ibatt_sum1 = 0; i < 10; i++) { + ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data); + if (ret) + goto out_meas; + vbatt_sum1 += data; + ret = measure_current(info, &data); + if (ret) + goto out_meas; + + if (data < 0) + ibatt_sum1 = ibatt_sum1 - data; /* discharging */ + else + ibatt_sum1 = ibatt_sum1 + data; /* charging */ + } + + if (set_charger_current(info, 100, &ret)) + goto out_meas; + /* + * set charge current as 100mA, wait about 500ms till charging + * process is launched and stable with the newer charging current. + */ + msleep(500); + + for (i = 0, vbatt_sum2 = 0, ibatt_sum2 = 0; i < 10; i++) { + ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data); + if (ret) + goto out_meas; + vbatt_sum2 += data; + ret = measure_current(info, &data); + if (ret) + goto out_meas; + + if (data < 0) + ibatt_sum2 = ibatt_sum2 - data; /* discharging */ + else + ibatt_sum2 = ibatt_sum2 + data; /* charging */ + } + + /* restore current setting */ + if (set_charger_current(info, chg_current, &ret)) + goto out_meas; + + if ((vbatt_sum1 > vbatt_sum2) && (ibatt_sum1 > ibatt_sum2) && + (ibatt_sum2 > 0)) { + /* calculate resistor in discharging case */ + data = 1000 * (vbatt_sum1 - vbatt_sum2) + / (ibatt_sum1 - ibatt_sum2); + if ((data - info->resistor > 0) && + (data - info->resistor < info->resistor)) + info->resistor = data; + if ((info->resistor - data > 0) && + (info->resistor - data < data)) + info->resistor = data; + } + return 0; + +out_meas: + set_charger_current(info, chg_current, &ret); +out: + return -EINVAL; +} + +static int calc_capacity(struct pm860x_battery_info *info, int *cap) +{ + int ret; + int data; + int ibat; + int cap_ocv = 0; + int cap_cc = 0; + + ret = calc_ccnt(info, &ccnt_data); + if (ret) + goto out; +soc: + data = info->max_capacity * info->start_soc / 100; + if (ccnt_data.total_dischg - ccnt_data.total_chg <= data) { + cap_cc = + data + ccnt_data.total_chg - ccnt_data.total_dischg; + } else { + clear_ccnt(info, &ccnt_data); + calc_soc(info, OCV_MODE_ACTIVE, &info->start_soc); + dev_dbg(info->dev, "restart soc = %d !\n", + info->start_soc); + goto soc; + } + + cap_cc = cap_cc * 100 / info->max_capacity; + if (cap_cc < 0) + cap_cc = 0; + else if (cap_cc > 100) + cap_cc = 100; + + dev_dbg(info->dev, "%s, last cap : %d", __func__, + info->last_capacity); + + ret = measure_current(info, &ibat); + if (ret) + goto out; + /* Calculate the capacity when discharging(ibat < 0) */ + if (ibat < 0) { + ret = calc_soc(info, OCV_MODE_ACTIVE, &cap_ocv); + if (ret) + cap_ocv = info->last_capacity; + ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data); + if (ret) + goto out; + if (data <= LOW_BAT_THRESHOLD) { + /* choose the lower capacity value to report + * between vbat and CC when vbat < 3.6v; + * than 3.6v; + */ + *cap = min(cap_ocv, cap_cc); + } else { + /* when detect vbat > 3.6v, but cap_cc < 15,and + * cap_ocv is 10% larger than cap_cc, we can think + * CC have some accumulation error, switch to OCV + * to estimate capacity; + * */ + if (cap_cc < 15 && cap_ocv - cap_cc > 10) + *cap = cap_ocv; + else + *cap = cap_cc; + } + /* when discharging, make sure current capacity + * is lower than last*/ + if (*cap > info->last_capacity) + *cap = info->last_capacity; + } else { + *cap = cap_cc; + } + info->last_capacity = *cap; + + dev_dbg(info->dev, "%s, cap_ocv:%d cap_cc:%d, cap:%d\n", + (ibat < 0) ? "discharging" : "charging", + cap_ocv, cap_cc, *cap); + /* + * store the current capacity to RTC domain register, + * after next power up , it will be restored. + */ + pm860x_set_bits(info->i2c, PM8607_RTC_MISC2, RTC_SOC_5LSB, + (*cap & 0x1F) << 3); + pm860x_set_bits(info->i2c, PM8607_RTC1, RTC_SOC_3MSB, + ((*cap >> 5) & 0x3)); + return 0; +out: + return ret; +} + +static void pm860x_external_power_changed(struct power_supply *psy) +{ + struct pm860x_battery_info *info; + + info = container_of(psy, struct pm860x_battery_info, battery); + calc_resistor(info); +} + +static int pm860x_batt_get_prop(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct pm860x_battery_info *info = dev_get_drvdata(psy->dev->parent); + int data; + int ret; + + switch (psp) { + case POWER_SUPPLY_PROP_PRESENT: + val->intval = info->present; + break; + case POWER_SUPPLY_PROP_CAPACITY: + ret = calc_capacity(info, &data); + if (ret) + return ret; + if (data < 0) + data = 0; + else if (data > 100) + data = 100; + /* return 100 if battery is not attached */ + if (!info->present) + data = 100; + val->intval = data; + break; + case POWER_SUPPLY_PROP_TECHNOLOGY: + val->intval = POWER_SUPPLY_TECHNOLOGY_LION; + break; + case POWER_SUPPLY_PROP_VOLTAGE_NOW: + /* return real vbatt Voltage */ + ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data); + if (ret) + return ret; + val->intval = data * 1000; + break; + case POWER_SUPPLY_PROP_VOLTAGE_AVG: + /* return Open Circuit Voltage (not measured voltage) */ + ret = calc_ocv(info, &data); + if (ret) + return ret; + val->intval = data * 1000; + break; + case POWER_SUPPLY_PROP_CURRENT_NOW: + ret = measure_current(info, &data); + if (ret) + return ret; + val->intval = data; + break; + case POWER_SUPPLY_PROP_TEMP: + if (info->present) { + ret = measure_temp(info, &data); + if (ret) + return ret; + data *= 10; + } else { + /* Fake Temp 25C Without Battery */ + data = 250; + } + val->intval = data; + break; + default: + return -ENODEV; + } + return 0; +} + +static int pm860x_batt_set_prop(struct power_supply *psy, + enum power_supply_property psp, + const union power_supply_propval *val) +{ + struct pm860x_battery_info *info = dev_get_drvdata(psy->dev->parent); + + switch (psp) { + case POWER_SUPPLY_PROP_CHARGE_FULL: + clear_ccnt(info, &ccnt_data); + info->start_soc = 100; + dev_dbg(info->dev, "chg done, update soc = %d\n", + info->start_soc); + break; + default: + return -EPERM; + } + + return 0; +} + + +static enum power_supply_property pm860x_batt_props[] = { + POWER_SUPPLY_PROP_PRESENT, + POWER_SUPPLY_PROP_CAPACITY, + POWER_SUPPLY_PROP_TECHNOLOGY, + POWER_SUPPLY_PROP_VOLTAGE_NOW, + POWER_SUPPLY_PROP_VOLTAGE_AVG, + POWER_SUPPLY_PROP_CURRENT_NOW, + POWER_SUPPLY_PROP_TEMP, +}; + +static __devinit int pm860x_battery_probe(struct platform_device *pdev) +{ + struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent); + struct pm860x_battery_info *info; + struct pm860x_power_pdata *pdata; + int ret; + + info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); + if (!info) + return -ENOMEM; + + info->irq_cc = platform_get_irq(pdev, 0); + if (info->irq_cc <= 0) { + dev_err(&pdev->dev, "No IRQ resource!\n"); + ret = -EINVAL; + goto out; + } + + info->irq_batt = platform_get_irq(pdev, 1); + if (info->irq_batt <= 0) { + dev_err(&pdev->dev, "No IRQ resource!\n"); + ret = -EINVAL; + goto out; + } + + info->chip = chip; + info->i2c = + (chip->id == CHIP_PM8607) ? chip->client : chip->companion; + info->dev = &pdev->dev; + info->status = POWER_SUPPLY_STATUS_UNKNOWN; + pdata = pdev->dev.platform_data; + + mutex_init(&info->lock); + platform_set_drvdata(pdev, info); + + pm860x_init_battery(info); + + info->battery.name = "battery-monitor"; + info->battery.type = POWER_SUPPLY_TYPE_BATTERY; + info->battery.properties = pm860x_batt_props; + info->battery.num_properties = ARRAY_SIZE(pm860x_batt_props); + info->battery.get_property = pm860x_batt_get_prop; + info->battery.set_property = pm860x_batt_set_prop; + info->battery.external_power_changed = pm860x_external_power_changed; + + if (pdata && pdata->max_capacity) + info->max_capacity = pdata->max_capacity; + else + info->max_capacity = 1500; /* set default capacity */ + if (pdata && pdata->resistor) + info->resistor = pdata->resistor; + else + info->resistor = 300; /* set default internal resistor */ + + ret = power_supply_register(&pdev->dev, &info->battery); + if (ret) + goto out; + info->battery.dev->parent = &pdev->dev; + + ret = request_threaded_irq(info->irq_cc, NULL, + pm860x_coulomb_handler, IRQF_ONESHOT, + "coulomb", info); + if (ret < 0) { + dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n", + info->irq_cc, ret); + goto out_reg; + } + + ret = request_threaded_irq(info->irq_batt, NULL, pm860x_batt_handler, + IRQF_ONESHOT, "battery", info); + if (ret < 0) { + dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n", + info->irq_batt, ret); + goto out_coulomb; + } + + + return 0; + +out_coulomb: + free_irq(info->irq_cc, info); +out_reg: + power_supply_unregister(&info->battery); +out: + kfree(info); + return ret; +} + +static int __devexit pm860x_battery_remove(struct platform_device *pdev) +{ + struct pm860x_battery_info *info = platform_get_drvdata(pdev); + + power_supply_unregister(&info->battery); + free_irq(info->irq_batt, info); + free_irq(info->irq_cc, info); + kfree(info); + platform_set_drvdata(pdev, NULL); + return 0; +} + +#ifdef CONFIG_PM_SLEEP +static int pm860x_battery_suspend(struct device *dev) +{ + struct platform_device *pdev = to_platform_device(dev); + struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent); + + if (device_may_wakeup(dev)) + chip->wakeup_flag |= 1 << PM8607_IRQ_CC; + return 0; +} + +static int pm860x_battery_resume(struct device *dev) +{ + struct platform_device *pdev = to_platform_device(dev); + struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent); + + if (device_may_wakeup(dev)) + chip->wakeup_flag &= ~(1 << PM8607_IRQ_CC); + return 0; +} +#endif + +static SIMPLE_DEV_PM_OPS(pm860x_battery_pm_ops, + pm860x_battery_suspend, pm860x_battery_resume); + +static struct platform_driver pm860x_battery_driver = { + .driver = { + .name = "88pm860x-battery", + .owner = THIS_MODULE, + .pm = &pm860x_battery_pm_ops, + }, + .probe = pm860x_battery_probe, + .remove = __devexit_p(pm860x_battery_remove), +}; +module_platform_driver(pm860x_battery_driver); + +MODULE_DESCRIPTION("Marvell 88PM860x Battery driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/power/88pm860x_charger.c b/drivers/power/88pm860x_charger.c new file mode 100644 index 000000000000..4fd7614ee839 --- /dev/null +++ b/drivers/power/88pm860x_charger.c @@ -0,0 +1,746 @@ +/* + * Battery driver for Marvell 88PM860x PMIC + * + * Copyright (c) 2012 Marvell International Ltd. + * Author: Jett Zhou + * Haojian Zhuang + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* bit definitions of Status Query Interface 2 */ +#define STATUS2_CHG (1 << 2) + +/* bit definitions of Reset Out Register */ +#define RESET_SW_PD (1 << 7) + +/* bit definitions of PreReg 1 */ +#define PREREG1_90MA (0x0) +#define PREREG1_180MA (0x1) +#define PREREG1_450MA (0x4) +#define PREREG1_540MA (0x5) +#define PREREG1_1350MA (0xE) +#define PREREG1_VSYS_4_5V (3 << 4) + +/* bit definitions of Charger Control 1 Register */ +#define CC1_MODE_OFF (0) +#define CC1_MODE_PRECHARGE (1) +#define CC1_MODE_FASTCHARGE (2) +#define CC1_MODE_PULSECHARGE (3) +#define CC1_ITERM_20MA (0 << 2) +#define CC1_ITERM_60MA (2 << 2) +#define CC1_VFCHG_4_2V (9 << 4) + +/* bit definitions of Charger Control 2 Register */ +#define CC2_ICHG_100MA (0x1) +#define CC2_ICHG_500MA (0x9) +#define CC2_ICHG_1000MA (0x13) + +/* bit definitions of Charger Control 3 Register */ +#define CC3_180MIN_TIMEOUT (0x6 << 4) +#define CC3_270MIN_TIMEOUT (0x7 << 4) +#define CC3_360MIN_TIMEOUT (0xA << 4) +#define CC3_DISABLE_TIMEOUT (0xF << 4) + +/* bit definitions of Charger Control 4 Register */ +#define CC4_IPRE_40MA (7) +#define CC4_VPCHG_3_2V (3 << 4) +#define CC4_IFCHG_MON_EN (1 << 6) +#define CC4_BTEMP_MON_EN (1 << 7) + +/* bit definitions of Charger Control 6 Register */ +#define CC6_BAT_OV_EN (1 << 2) +#define CC6_BAT_UV_EN (1 << 3) +#define CC6_UV_VBAT_SET (0x3 << 6) /* 2.8v */ + +/* bit definitions of Charger Control 7 Register */ +#define CC7_BAT_REM_EN (1 << 3) +#define CC7_IFSM_EN (1 << 7) + +/* bit definitions of Measurement Enable 1 Register */ +#define MEAS1_VBAT (1 << 0) + +/* bit definitions of Measurement Enable 3 Register */ +#define MEAS3_IBAT_EN (1 << 0) +#define MEAS3_CC_EN (1 << 2) + +#define FSM_INIT 0 +#define FSM_DISCHARGE 1 +#define FSM_PRECHARGE 2 +#define FSM_FASTCHARGE 3 + +#define PRECHARGE_THRESHOLD 3100 +#define POWEROFF_THRESHOLD 3400 +#define CHARGE_THRESHOLD 4000 +#define DISCHARGE_THRESHOLD 4180 + +/* over-temperature on PM8606 setting */ +#define OVER_TEMP_FLAG (1 << 6) +#define OVTEMP_AUTORECOVER (1 << 3) + +/* over-voltage protect on vchg setting mv */ +#define VCHG_NORMAL_LOW 4200 +#define VCHG_NORMAL_CHECK 5800 +#define VCHG_NORMAL_HIGH 6000 +#define VCHG_OVP_LOW 5500 + +struct pm860x_charger_info { + struct pm860x_chip *chip; + struct i2c_client *i2c; + struct i2c_client *i2c_8606; + struct device *dev; + + struct power_supply usb; + struct mutex lock; + int irq_nums; + int irq[7]; + unsigned state:3; /* fsm state */ + unsigned online:1; /* usb charger */ + unsigned present:1; /* battery present */ + unsigned allowed:1; +}; + +static char *pm860x_supplied_to[] = { + "battery-monitor", +}; + +static int measure_vchg(struct pm860x_charger_info *info, int *data) +{ + unsigned char buf[2]; + int ret = 0; + + ret = pm860x_bulk_read(info->i2c, PM8607_VCHG_MEAS1, 2, buf); + if (ret < 0) + return ret; + + *data = ((buf[0] & 0xff) << 4) | (buf[1] & 0x0f); + /* V_BATT_MEAS(mV) = value * 5 * 1.8 * 1000 / (2^12) */ + *data = ((*data & 0xfff) * 9 * 125) >> 9; + + dev_dbg(info->dev, "%s, vchg: %d mv\n", __func__, *data); + + return ret; +} + +static void set_vchg_threshold(struct pm860x_charger_info *info, + int min, int max) +{ + int data; + + /* (tmp << 8) * / 5 / 1800 */ + if (min <= 0) + data = 0; + else + data = (min << 5) / 1125; + pm860x_reg_write(info->i2c, PM8607_VCHG_LOWTH, data); + dev_dbg(info->dev, "VCHG_LOWTH:%dmv, 0x%x\n", min, data); + + if (max <= 0) + data = 0xff; + else + data = (max << 5) / 1125; + pm860x_reg_write(info->i2c, PM8607_VCHG_HIGHTH, data); + dev_dbg(info->dev, "VCHG_HIGHTH:%dmv, 0x%x\n", max, data); + +} + +static void set_vbatt_threshold(struct pm860x_charger_info *info, + int min, int max) +{ + int data; + + /* (tmp << 8) * 3 / 1800 */ + if (min <= 0) + data = 0; + else + data = (min << 5) / 675; + pm860x_reg_write(info->i2c, PM8607_VBAT_LOWTH, data); + dev_dbg(info->dev, "VBAT Min:%dmv, LOWTH:0x%x\n", min, data); + + if (max <= 0) + data = 0xff; + else + data = (max << 5) / 675; + pm860x_reg_write(info->i2c, PM8607_VBAT_HIGHTH, data); + dev_dbg(info->dev, "VBAT Max:%dmv, HIGHTH:0x%x\n", max, data); + + return; +} + +static int start_precharge(struct pm860x_charger_info *info) +{ + int ret; + + dev_dbg(info->dev, "Start Pre-charging!\n"); + set_vbatt_threshold(info, 0, 0); + + ret = pm860x_reg_write(info->i2c_8606, PM8606_PREREGULATORA, + PREREG1_1350MA | PREREG1_VSYS_4_5V); + if (ret < 0) + goto out; + /* stop charging */ + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3, + CC1_MODE_OFF); + if (ret < 0) + goto out; + /* set 270 minutes timeout */ + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL3, (0xf << 4), + CC3_270MIN_TIMEOUT); + if (ret < 0) + goto out; + /* set precharge current, termination voltage, IBAT & TBAT monitor */ + ret = pm860x_reg_write(info->i2c, PM8607_CHG_CTRL4, + CC4_IPRE_40MA | CC4_VPCHG_3_2V | + CC4_IFCHG_MON_EN | CC4_BTEMP_MON_EN); + if (ret < 0) + goto out; + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL7, + CC7_BAT_REM_EN | CC7_IFSM_EN, + CC7_BAT_REM_EN | CC7_IFSM_EN); + if (ret < 0) + goto out; + /* trigger precharge */ + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3, + CC1_MODE_PRECHARGE); +out: + return ret; +} + +static int start_fastcharge(struct pm860x_charger_info *info) +{ + int ret; + + dev_dbg(info->dev, "Start Fast-charging!\n"); + + /* set fastcharge termination current & voltage, disable charging */ + ret = pm860x_reg_write(info->i2c, PM8607_CHG_CTRL1, + CC1_MODE_OFF | CC1_ITERM_60MA | + CC1_VFCHG_4_2V); + if (ret < 0) + goto out; + ret = pm860x_reg_write(info->i2c_8606, PM8606_PREREGULATORA, + PREREG1_540MA | PREREG1_VSYS_4_5V); + if (ret < 0) + goto out; + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL2, 0x1f, + CC2_ICHG_500MA); + if (ret < 0) + goto out; + /* set 270 minutes timeout */ + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL3, (0xf << 4), + CC3_270MIN_TIMEOUT); + if (ret < 0) + goto out; + /* set IBAT & TBAT monitor */ + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL4, + CC4_IFCHG_MON_EN | CC4_BTEMP_MON_EN, + CC4_IFCHG_MON_EN | CC4_BTEMP_MON_EN); + if (ret < 0) + goto out; + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL6, + CC6_BAT_OV_EN | CC6_BAT_UV_EN | + CC6_UV_VBAT_SET, + CC6_BAT_OV_EN | CC6_BAT_UV_EN | + CC6_UV_VBAT_SET); + if (ret < 0) + goto out; + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL7, + CC7_BAT_REM_EN | CC7_IFSM_EN, + CC7_BAT_REM_EN | CC7_IFSM_EN); + if (ret < 0) + goto out; + /* launch fast-charge */ + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3, + CC1_MODE_FASTCHARGE); + /* vchg threshold setting */ + set_vchg_threshold(info, VCHG_NORMAL_LOW, VCHG_NORMAL_HIGH); +out: + return ret; +} + +static void stop_charge(struct pm860x_charger_info *info, int vbatt) +{ + dev_dbg(info->dev, "Stop charging!\n"); + pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3, CC1_MODE_OFF); + if (vbatt > CHARGE_THRESHOLD && info->online) + set_vbatt_threshold(info, CHARGE_THRESHOLD, 0); +} + +static void power_off_notification(struct pm860x_charger_info *info) +{ + dev_dbg(info->dev, "Power-off notification!\n"); +} + +static int set_charging_fsm(struct pm860x_charger_info *info) +{ + struct power_supply *psy; + union power_supply_propval data; + unsigned char fsm_state[][16] = { "init", "discharge", "precharge", + "fastcharge", + }; + int ret; + int vbatt; + + psy = power_supply_get_by_name(pm860x_supplied_to[0]); + if (!psy) + return -EINVAL; + ret = psy->get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW, &data); + if (ret) + return ret; + vbatt = data.intval / 1000; + + ret = psy->get_property(psy, POWER_SUPPLY_PROP_PRESENT, &data); + if (ret) + return ret; + + mutex_lock(&info->lock); + info->present = data.intval; + + dev_dbg(info->dev, "Entering FSM:%s, Charger:%s, Battery:%s, " + "Allowed:%d\n", + &fsm_state[info->state][0], + (info->online) ? "online" : "N/A", + (info->present) ? "present" : "N/A", info->allowed); + dev_dbg(info->dev, "set_charging_fsm:vbatt:%d(mV)\n", vbatt); + + switch (info->state) { + case FSM_INIT: + if (info->online && info->present && info->allowed) { + if (vbatt < PRECHARGE_THRESHOLD) { + info->state = FSM_PRECHARGE; + start_precharge(info); + } else if (vbatt > DISCHARGE_THRESHOLD) { + info->state = FSM_DISCHARGE; + stop_charge(info, vbatt); + } else if (vbatt < DISCHARGE_THRESHOLD) { + info->state = FSM_FASTCHARGE; + start_fastcharge(info); + } + } else { + if (vbatt < POWEROFF_THRESHOLD) { + power_off_notification(info); + } else { + info->state = FSM_DISCHARGE; + stop_charge(info, vbatt); + } + } + break; + case FSM_PRECHARGE: + if (info->online && info->present && info->allowed) { + if (vbatt > PRECHARGE_THRESHOLD) { + info->state = FSM_FASTCHARGE; + start_fastcharge(info); + } + } else { + info->state = FSM_DISCHARGE; + stop_charge(info, vbatt); + } + break; + case FSM_FASTCHARGE: + if (info->online && info->present && info->allowed) { + if (vbatt < PRECHARGE_THRESHOLD) { + info->state = FSM_PRECHARGE; + start_precharge(info); + } + } else { + info->state = FSM_DISCHARGE; + stop_charge(info, vbatt); + } + break; + case FSM_DISCHARGE: + if (info->online && info->present && info->allowed) { + if (vbatt < PRECHARGE_THRESHOLD) { + info->state = FSM_PRECHARGE; + start_precharge(info); + } else if (vbatt < DISCHARGE_THRESHOLD) { + info->state = FSM_FASTCHARGE; + start_fastcharge(info); + } + } else { + if (vbatt < POWEROFF_THRESHOLD) + power_off_notification(info); + else if (vbatt > CHARGE_THRESHOLD && info->online) + set_vbatt_threshold(info, CHARGE_THRESHOLD, 0); + } + break; + default: + dev_warn(info->dev, "FSM meets wrong state:%d\n", + info->state); + break; + } + dev_dbg(info->dev, + "Out FSM:%s, Charger:%s, Battery:%s, Allowed:%d\n", + &fsm_state[info->state][0], + (info->online) ? "online" : "N/A", + (info->present) ? "present" : "N/A", info->allowed); + mutex_unlock(&info->lock); + + return 0; +} + +static irqreturn_t pm860x_charger_handler(int irq, void *data) +{ + struct pm860x_charger_info *info = data; + int ret; + + mutex_lock(&info->lock); + ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2); + if (ret < 0) { + mutex_unlock(&info->lock); + goto out; + } + if (ret & STATUS2_CHG) { + info->online = 1; + info->allowed = 1; + } else { + info->online = 0; + info->allowed = 0; + } + mutex_unlock(&info->lock); + dev_dbg(info->dev, "%s, Charger:%s, Allowed:%d\n", __func__, + (info->online) ? "online" : "N/A", info->allowed); + + set_charging_fsm(info); + + power_supply_changed(&info->usb); +out: + return IRQ_HANDLED; +} + +static irqreturn_t pm860x_temp_handler(int irq, void *data) +{ + struct power_supply *psy; + struct pm860x_charger_info *info = data; + union power_supply_propval temp; + int value; + int ret; + + psy = power_supply_get_by_name(pm860x_supplied_to[0]); + if (!psy) + goto out; + ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &temp); + if (ret) + goto out; + value = temp.intval / 10; + + mutex_lock(&info->lock); + /* Temperature < -10 C or >40 C, Will not allow charge */ + if (value < -10 || value > 40) + info->allowed = 0; + else + info->allowed = 1; + dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed); + mutex_unlock(&info->lock); + + set_charging_fsm(info); +out: + return IRQ_HANDLED; +} + +static irqreturn_t pm860x_exception_handler(int irq, void *data) +{ + struct pm860x_charger_info *info = data; + + mutex_lock(&info->lock); + info->allowed = 0; + mutex_unlock(&info->lock); + dev_dbg(info->dev, "%s, irq: %d\n", __func__, irq); + + set_charging_fsm(info); + return IRQ_HANDLED; +} + +static irqreturn_t pm860x_done_handler(int irq, void *data) +{ + struct pm860x_charger_info *info = data; + struct power_supply *psy; + union power_supply_propval val; + int ret; + int vbatt; + + mutex_lock(&info->lock); + /* pre-charge done, will transimit to fast-charge stage */ + if (info->state == FSM_PRECHARGE) { + info->allowed = 1; + goto out; + } + /* + * Fast charge done, delay to read + * the correct status of CHG_DET. + */ + mdelay(5); + info->allowed = 0; + psy = power_supply_get_by_name(pm860x_supplied_to[0]); + if (!psy) + goto out; + ret = psy->get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW, &val); + if (ret) + goto out; + vbatt = val.intval / 1000; + /* + * CHG_DONE interrupt is faster than CHG_DET interrupt when + * plug in/out usb, So we can not rely on info->online, we + * need check pm8607 status register to check usb is online + * or not, then we can decide it is real charge done + * automatically or it is triggered by usb plug out; + */ + ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2); + if (ret < 0) + goto out; + if (vbatt > CHARGE_THRESHOLD && ret & STATUS2_CHG) + psy->set_property(psy, POWER_SUPPLY_PROP_CHARGE_FULL, &val); + +out: + mutex_unlock(&info->lock); + dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed); + set_charging_fsm(info); + + return IRQ_HANDLED; +} + +static irqreturn_t pm860x_vbattery_handler(int irq, void *data) +{ + struct pm860x_charger_info *info = data; + + mutex_lock(&info->lock); + + set_vbatt_threshold(info, 0, 0); + + if (info->present && info->online) + info->allowed = 1; + else + info->allowed = 0; + mutex_unlock(&info->lock); + dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed); + + set_charging_fsm(info); + + return IRQ_HANDLED; +} + +static irqreturn_t pm860x_vchg_handler(int irq, void *data) +{ + struct pm860x_charger_info *info = data; + int vchg = 0; + + if (info->present) + goto out; + + measure_vchg(info, &vchg); + + mutex_lock(&info->lock); + if (!info->online) { + int status; + /* check if over-temp on pm8606 or not */ + status = pm860x_reg_read(info->i2c_8606, PM8606_FLAGS); + if (status & OVER_TEMP_FLAG) { + /* clear over temp flag and set auto recover */ + pm860x_set_bits(info->i2c_8606, PM8606_FLAGS, + OVER_TEMP_FLAG, OVER_TEMP_FLAG); + pm860x_set_bits(info->i2c_8606, + PM8606_VSYS, + OVTEMP_AUTORECOVER, + OVTEMP_AUTORECOVER); + dev_dbg(info->dev, + "%s, pm8606 over-temp occure\n", __func__); + } + } + + if (vchg > VCHG_NORMAL_CHECK) { + set_vchg_threshold(info, VCHG_OVP_LOW, 0); + info->allowed = 0; + dev_dbg(info->dev, + "%s,pm8607 over-vchg occure,vchg = %dmv\n", + __func__, vchg); + } else if (vchg < VCHG_OVP_LOW) { + set_vchg_threshold(info, VCHG_NORMAL_LOW, + VCHG_NORMAL_HIGH); + info->allowed = 1; + dev_dbg(info->dev, + "%s,pm8607 over-vchg recover,vchg = %dmv\n", + __func__, vchg); + } + mutex_unlock(&info->lock); + + dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed); + set_charging_fsm(info); +out: + return IRQ_HANDLED; +} + +static int pm860x_usb_get_prop(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct pm860x_charger_info *info = + dev_get_drvdata(psy->dev->parent); + + switch (psp) { + case POWER_SUPPLY_PROP_STATUS: + if (info->state == FSM_FASTCHARGE || + info->state == FSM_PRECHARGE) + val->intval = POWER_SUPPLY_STATUS_CHARGING; + else + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; + break; + case POWER_SUPPLY_PROP_ONLINE: + val->intval = info->online; + break; + default: + return -ENODEV; + } + return 0; +} + +static enum power_supply_property pm860x_usb_props[] = { + POWER_SUPPLY_PROP_STATUS, + POWER_SUPPLY_PROP_ONLINE, +}; + +static int pm860x_init_charger(struct pm860x_charger_info *info) +{ + int ret; + + ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2); + if (ret < 0) + return ret; + + mutex_lock(&info->lock); + info->state = FSM_INIT; + if (ret & STATUS2_CHG) { + info->online = 1; + info->allowed = 1; + } else { + info->online = 0; + info->allowed = 0; + } + mutex_unlock(&info->lock); + + set_charging_fsm(info); + return 0; +} + +struct pm860x_irq_desc { + const char *name; + irqreturn_t (*handler)(int irq, void *data); +} pm860x_irq_descs[] = { + { "usb supply detect", pm860x_charger_handler }, + { "charge done", pm860x_done_handler }, + { "charge timeout", pm860x_exception_handler }, + { "charge fault", pm860x_exception_handler }, + { "temperature", pm860x_temp_handler }, + { "vbatt", pm860x_vbattery_handler }, + { "vchg", pm860x_vchg_handler }, +}; + +static __devinit int pm860x_charger_probe(struct platform_device *pdev) +{ + struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent); + struct pm860x_charger_info *info; + int ret; + int count; + int i; + int j; + + info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); + if (!info) + return -ENOMEM; + + count = pdev->num_resources; + for (i = 0, j = 0; i < count; i++) { + info->irq[j] = platform_get_irq(pdev, i); + if (info->irq[j] < 0) + continue; + j++; + } + info->irq_nums = j; + + info->chip = chip; + info->i2c = + (chip->id == CHIP_PM8607) ? chip->client : chip->companion; + info->i2c_8606 = + (chip->id == CHIP_PM8607) ? chip->companion : chip->client; + if (!info->i2c_8606) { + dev_err(&pdev->dev, "Missed I2C address of 88PM8606!\n"); + ret = -EINVAL; + goto out; + } + info->dev = &pdev->dev; + + /* set init value for the case we are not using battery */ + set_vchg_threshold(info, VCHG_NORMAL_LOW, VCHG_OVP_LOW); + + mutex_init(&info->lock); + platform_set_drvdata(pdev, info); + + info->usb.name = "usb"; + info->usb.type = POWER_SUPPLY_TYPE_USB; + info->usb.supplied_to = pm860x_supplied_to; + info->usb.num_supplicants = ARRAY_SIZE(pm860x_supplied_to); + info->usb.properties = pm860x_usb_props; + info->usb.num_properties = ARRAY_SIZE(pm860x_usb_props); + info->usb.get_property = pm860x_usb_get_prop; + ret = power_supply_register(&pdev->dev, &info->usb); + if (ret) + goto out; + + pm860x_init_charger(info); + + for (i = 0; i < ARRAY_SIZE(info->irq); i++) { + ret = request_threaded_irq(info->irq[i], NULL, + pm860x_irq_descs[i].handler, + IRQF_ONESHOT, pm860x_irq_descs[i].name, info); + if (ret < 0) { + dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n", + info->irq[i], ret); + goto out_irq; + } + } + return 0; + +out_irq: + while (--i >= 0) + free_irq(info->irq[i], info); +out: + kfree(info); + return ret; +} + +static int __devexit pm860x_charger_remove(struct platform_device *pdev) +{ + struct pm860x_charger_info *info = platform_get_drvdata(pdev); + int i; + + platform_set_drvdata(pdev, NULL); + power_supply_unregister(&info->usb); + free_irq(info->irq[0], info); + for (i = 0; i < info->irq_nums; i++) + free_irq(info->irq[i], info); + kfree(info); + return 0; +} + +static struct platform_driver pm860x_charger_driver = { + .driver = { + .name = "88pm860x-charger", + .owner = THIS_MODULE, + }, + .probe = pm860x_charger_probe, + .remove = __devexit_p(pm860x_charger_remove), +}; +module_platform_driver(pm860x_charger_driver); + +MODULE_DESCRIPTION("Marvell 88PM860x Charger driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig index 4ec2ff0ee4a2..9d8047735841 100644 --- a/drivers/power/Kconfig +++ b/drivers/power/Kconfig @@ -62,6 +62,12 @@ config TEST_POWER help This driver is used for testing. It's safe to say M here. +config BATTERY_88PM860X + tristate "Marvell 88PM860x battery driver" + depends on MFD_88PM860X + help + Say Y here to enable battery monitor for Marvell 88PM860x chip. + config BATTERY_DS2760 tristate "DS2760 battery driver (HP iPAQ & others)" depends on W1 && W1_SLAVE_DS2760 @@ -202,6 +208,12 @@ config BATTERY_S3C_ADC help Say Y here to enable support for iPAQ h1930/h1940/rx1950 battery +config CHARGER_88PM860X + tristate "Marvell 88PM860x Charger driver" + depends on MFD_88PM860X && BATTERY_88PM860X + help + Say Y here to enable charger for Marvell 88PM860x chip. + config CHARGER_PCF50633 tristate "NXP PCF50633 MBC" depends on MFD_PCF50633 diff --git a/drivers/power/Makefile b/drivers/power/Makefile index 587c5f174626..059c0a931526 100644 --- a/drivers/power/Makefile +++ b/drivers/power/Makefile @@ -14,6 +14,7 @@ obj-$(CONFIG_WM831X_POWER) += wm831x_power.o obj-$(CONFIG_WM8350_POWER) += wm8350_power.o obj-$(CONFIG_TEST_POWER) += test_power.o +obj-$(CONFIG_BATTERY_88PM860X) += 88pm860x_battery.o obj-$(CONFIG_BATTERY_DS2760) += ds2760_battery.o obj-$(CONFIG_BATTERY_DS2780) += ds2780_battery.o obj-$(CONFIG_BATTERY_DS2781) += ds2781_battery.o @@ -31,6 +32,7 @@ obj-$(CONFIG_BATTERY_MAX17040) += max17040_battery.o obj-$(CONFIG_BATTERY_MAX17042) += max17042_battery.o obj-$(CONFIG_BATTERY_Z2) += z2_battery.o obj-$(CONFIG_BATTERY_S3C_ADC) += s3c_adc_battery.o +obj-$(CONFIG_CHARGER_88PM860X) += 88pm860x_charger.o obj-$(CONFIG_CHARGER_PCF50633) += pcf50633-charger.o obj-$(CONFIG_BATTERY_JZ4740) += jz4740-battery.o obj-$(CONFIG_BATTERY_INTEL_MID) += intel_mid_battery.o diff --git a/include/linux/mfd/88pm860x.h b/include/linux/mfd/88pm860x.h index 7b24943779fa..b7c5a3c27c98 100644 --- a/include/linux/mfd/88pm860x.h +++ b/include/linux/mfd/88pm860x.h @@ -55,6 +55,21 @@ enum { #define PM8606_DCM_BOOST (0x00) #define PM8606_PWM (0x01) +#define PM8607_MISC2 (0x42) + +/* Power Up Log Register */ +#define PM8607_POWER_UP_LOG (0x3F) + +/* Charger Control Registers */ +#define PM8607_CCNT (0x47) +#define PM8607_CHG_CTRL1 (0x48) +#define PM8607_CHG_CTRL2 (0x49) +#define PM8607_CHG_CTRL3 (0x4A) +#define PM8607_CHG_CTRL4 (0x4B) +#define PM8607_CHG_CTRL5 (0x4C) +#define PM8607_CHG_CTRL6 (0x4D) +#define PM8607_CHG_CTRL7 (0x4E) + /* Backlight Registers */ #define PM8606_WLED1A (0x02) #define PM8606_WLED1B (0x03) @@ -205,6 +220,71 @@ enum { #define PM8607_PD_PREBIAS (0x56) /* prebias time */ #define PM8607_GPADC_MISC1 (0x57) +/* bit definitions of MEAS_EN1*/ +#define PM8607_MEAS_EN1_VBAT (1 << 0) +#define PM8607_MEAS_EN1_VCHG (1 << 1) +#define PM8607_MEAS_EN1_VSYS (1 << 2) +#define PM8607_MEAS_EN1_TINT (1 << 3) +#define PM8607_MEAS_EN1_RFTMP (1 << 4) +#define PM8607_MEAS_EN1_TBAT (1 << 5) +#define PM8607_MEAS_EN1_GPADC2 (1 << 6) +#define PM8607_MEAS_EN1_GPADC3 (1 << 7) + +/* Battery Monitor Registers */ +#define PM8607_GP_BIAS2 (0x5A) +#define PM8607_VBAT_LOWTH (0x5B) +#define PM8607_VCHG_LOWTH (0x5C) +#define PM8607_VSYS_LOWTH (0x5D) +#define PM8607_TINT_LOWTH (0x5E) +#define PM8607_GPADC0_LOWTH (0x5F) +#define PM8607_GPADC1_LOWTH (0x60) +#define PM8607_GPADC2_LOWTH (0x61) +#define PM8607_GPADC3_LOWTH (0x62) +#define PM8607_VBAT_HIGHTH (0x63) +#define PM8607_VCHG_HIGHTH (0x64) +#define PM8607_VSYS_HIGHTH (0x65) +#define PM8607_TINT_HIGHTH (0x66) +#define PM8607_GPADC0_HIGHTH (0x67) +#define PM8607_GPADC1_HIGHTH (0x68) +#define PM8607_GPADC2_HIGHTH (0x69) +#define PM8607_GPADC3_HIGHTH (0x6A) +#define PM8607_IBAT_MEAS1 (0x6B) +#define PM8607_IBAT_MEAS2 (0x6C) +#define PM8607_VBAT_MEAS1 (0x6D) +#define PM8607_VBAT_MEAS2 (0x6E) +#define PM8607_VCHG_MEAS1 (0x6F) +#define PM8607_VCHG_MEAS2 (0x70) +#define PM8607_VSYS_MEAS1 (0x71) +#define PM8607_VSYS_MEAS2 (0x72) +#define PM8607_TINT_MEAS1 (0x73) +#define PM8607_TINT_MEAS2 (0x74) +#define PM8607_GPADC0_MEAS1 (0x75) +#define PM8607_GPADC0_MEAS2 (0x76) +#define PM8607_GPADC1_MEAS1 (0x77) +#define PM8607_GPADC1_MEAS2 (0x78) +#define PM8607_GPADC2_MEAS1 (0x79) +#define PM8607_GPADC2_MEAS2 (0x7A) +#define PM8607_GPADC3_MEAS1 (0x7B) +#define PM8607_GPADC3_MEAS2 (0x7C) +#define PM8607_CCNT_MEAS1 (0x95) +#define PM8607_CCNT_MEAS2 (0x96) +#define PM8607_VBAT_AVG (0x97) +#define PM8607_VCHG_AVG (0x98) +#define PM8607_VSYS_AVG (0x99) +#define PM8607_VBAT_MIN (0x9A) +#define PM8607_VCHG_MIN (0x9B) +#define PM8607_VSYS_MIN (0x9C) +#define PM8607_VBAT_MAX (0x9D) +#define PM8607_VCHG_MAX (0x9E) +#define PM8607_VSYS_MAX (0x9F) + +#define PM8607_GPADC_MISC2 (0x59) +#define PM8607_GPADC0_GP_BIAS_A0 (1 << 0) +#define PM8607_GPADC1_GP_BIAS_A1 (1 << 1) +#define PM8607_GPADC2_GP_BIAS_A2 (1 << 2) +#define PM8607_GPADC3_GP_BIAS_A3 (1 << 3) +#define PM8607_GPADC2_GP_BIAS_OUT2 (1 << 6) + /* RTC Control Registers */ #define PM8607_RTC1 (0xA0) #define PM8607_RTC_COUNTER1 (0xA1) @@ -370,7 +450,8 @@ struct pm860x_touch_pdata { }; struct pm860x_power_pdata { - unsigned fast_charge; /* charge current */ + int max_capacity; + int resistor; }; struct pm860x_platform_data { @@ -379,6 +460,7 @@ struct pm860x_platform_data { struct pm860x_rtc_pdata *rtc; struct pm860x_touch_pdata *touch; struct pm860x_power_pdata *power; + struct charger_desc *chg_desc; struct regulator_init_data *regulator; unsigned short companion_addr; /* I2C address of companion chip */ -- cgit v1.2.3-58-ga151 From 318cb389d020e3107979d7d794ab992e4bcd6665 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:23:12 +0000 Subject: lp8727_charger: Fix buggy code of NULL pdata LP8727 platform data is optional, so the driver should work even the platform data is NULL. To check the platform data, charging parameter data should be changed to the pointer type. Fix NULL point access problem when getting the battery properties. When the data is NULL, just return as invalid value. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 28 +++++++++++++++++++++------- include/linux/platform_data/lp8727.h | 7 ++++--- 2 files changed, 25 insertions(+), 10 deletions(-) (limited to 'include') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index b2df11458d98..7c19c0927f3a 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -170,20 +170,21 @@ static void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw) static void lp8727_id_detection(struct lp8727_chg *pchg, u8 id, int vbusin) { + struct lp8727_platform_data *pdata = pchg->pdata; u8 devid = ID_NONE; u8 swctrl = SW_DM1_HiZ | SW_DP2_HiZ; switch (id) { case 0x5: devid = ID_TA; - pchg->chg_parm = &pchg->pdata->ac; + pchg->chg_parm = pdata ? pdata->ac : NULL; break; case 0xB: if (lp8727_is_dedicated_charger(pchg)) { - pchg->chg_parm = &pchg->pdata->ac; + pchg->chg_parm = pdata ? pdata->ac : NULL; devid = ID_DEDICATED_CHG; } else if (lp8727_is_usb_charger(pchg)) { - pchg->chg_parm = &pchg->pdata->usb; + pchg->chg_parm = pdata ? pdata->usb : NULL; devid = ID_USB_CHG; swctrl = SW_DM1_DM | SW_DP2_DP; } else if (vbusin) { @@ -295,6 +296,7 @@ static int lp8727_battery_get_property(struct power_supply *psy, union power_supply_propval *val) { struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent); + struct lp8727_platform_data *pdata = pchg->pdata; u8 read; switch (psp) { @@ -318,19 +320,31 @@ static int lp8727_battery_get_property(struct power_supply *psy, val->intval = POWER_SUPPLY_HEALTH_GOOD; break; case POWER_SUPPLY_PROP_PRESENT: - if (pchg->pdata->get_batt_present) + if (!pdata) + return -EINVAL; + + if (pdata->get_batt_present) val->intval = pchg->pdata->get_batt_present(); break; case POWER_SUPPLY_PROP_VOLTAGE_NOW: - if (pchg->pdata->get_batt_level) + if (!pdata) + return -EINVAL; + + if (pdata->get_batt_level) val->intval = pchg->pdata->get_batt_level(); break; case POWER_SUPPLY_PROP_CAPACITY: - if (pchg->pdata->get_batt_capacity) + if (!pdata) + return -EINVAL; + + if (pdata->get_batt_capacity) val->intval = pchg->pdata->get_batt_capacity(); break; case POWER_SUPPLY_PROP_TEMP: - if (pchg->pdata->get_batt_temp) + if (!pdata) + return -EINVAL; + + if (pdata->get_batt_temp) val->intval = pchg->pdata->get_batt_temp(); break; default: diff --git a/include/linux/platform_data/lp8727.h b/include/linux/platform_data/lp8727.h index ea98c6133d32..f4bcdd5f20a4 100644 --- a/include/linux/platform_data/lp8727.h +++ b/include/linux/platform_data/lp8727.h @@ -51,15 +51,16 @@ struct lp8727_chg_param { * @get_batt_level : get battery voltage (mV) * @get_batt_capacity : get battery capacity (%) * @get_batt_temp : get battery temperature - * @ac, @usb : charging parameters each charger type + * @ac : charging parameters for AC type charger + * @usb : charging parameters for USB type charger */ struct lp8727_platform_data { u8 (*get_batt_present)(void); u16 (*get_batt_level)(void); u8 (*get_batt_capacity)(void); u8 (*get_batt_temp)(void); - struct lp8727_chg_param ac; - struct lp8727_chg_param usb; + struct lp8727_chg_param *ac; + struct lp8727_chg_param *usb; }; #endif -- cgit v1.2.3-58-ga151 From 60fd57e06ed7ea13bc0bdf4cb5324d47039105ab Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:23:25 +0000 Subject: lp8727_charger: Add configurable debouce timer Debounce time is configurable in the platform side. If it is not defined, the default value is 270ms. Platform data is msec unit, and this time is converted to jiffies internally. The workqueue uses this jiffies time in the interrupt handling. So debounce_jiffies is added in the private data. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 12 +++++++++--- include/linux/platform_data/lp8727.h | 2 ++ 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 7c19c0927f3a..1907b1f7953d 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -17,7 +17,7 @@ #include #include -#define DEBOUNCE_MSEC 270 +#define DEFAULT_DEBOUNCE_MSEC 270 /* Registers */ #define CTRL1 0x1 @@ -90,6 +90,7 @@ struct lp8727_chg { struct lp8727_psy *psy; struct lp8727_chg_param *chg_parm; enum lp8727_dev_id devid; + unsigned long debounce_jiffies; }; static int lp8727_read_bytes(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len) @@ -236,15 +237,18 @@ static void lp8727_delayed_func(struct work_struct *_work) static irqreturn_t lp8727_isr_func(int irq, void *ptr) { struct lp8727_chg *pchg = ptr; - unsigned long delay = msecs_to_jiffies(DEBOUNCE_MSEC); - queue_delayed_work(pchg->irqthread, &pchg->work, delay); + queue_delayed_work(pchg->irqthread, &pchg->work, + pchg->debounce_jiffies); return IRQ_HANDLED; } static int lp8727_intr_config(struct lp8727_chg *pchg) { + unsigned delay_msec = pchg->pdata ? pchg->pdata->debounce_msec : + DEFAULT_DEBOUNCE_MSEC; + INIT_DELAYED_WORK(&pchg->work, lp8727_delayed_func); pchg->irqthread = create_singlethread_workqueue("lp8727-irqthd"); @@ -253,6 +257,8 @@ static int lp8727_intr_config(struct lp8727_chg *pchg) return -ENOMEM; } + pchg->debounce_jiffies = msecs_to_jiffies(delay_msec); + return request_threaded_irq(pchg->client->irq, NULL, lp8727_isr_func, diff --git a/include/linux/platform_data/lp8727.h b/include/linux/platform_data/lp8727.h index f4bcdd5f20a4..54b77880ed67 100644 --- a/include/linux/platform_data/lp8727.h +++ b/include/linux/platform_data/lp8727.h @@ -53,6 +53,7 @@ struct lp8727_chg_param { * @get_batt_temp : get battery temperature * @ac : charging parameters for AC type charger * @usb : charging parameters for USB type charger + * @debounce_msec : interrupt debounce time */ struct lp8727_platform_data { u8 (*get_batt_present)(void); @@ -61,6 +62,7 @@ struct lp8727_platform_data { u8 (*get_batt_temp)(void); struct lp8727_chg_param *ac; struct lp8727_chg_param *usb; + unsigned int debounce_msec; }; #endif -- cgit v1.2.3-58-ga151 From 6029719f08d2d21624504e326f50c68f89731353 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:24:51 +0000 Subject: lp8727_charger: Clean up lp8727 definitions All definitions should be unique, since they're in the gloabl namespace. So the prefix LP8727_ are added. Additionally, use BIT() macro for bit masks. Remove unnecessary definitions such as SW_DM1_U1 and SW_DP2_U2. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 130 +++++++++++++++++------------------ include/linux/platform_data/lp8727.h | 34 ++++----- 2 files changed, 81 insertions(+), 83 deletions(-) (limited to 'include') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 32bf157f4815..65c9840a37e1 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -21,53 +21,51 @@ #define DEFAULT_DEBOUNCE_MSEC 270 /* Registers */ -#define CTRL1 0x1 -#define CTRL2 0x2 -#define SWCTRL 0x3 -#define INT1 0x4 -#define INT2 0x5 -#define STATUS1 0x6 -#define STATUS2 0x7 -#define CHGCTRL2 0x9 +#define LP8727_CTRL1 0x1 +#define LP8727_CTRL2 0x2 +#define LP8727_SWCTRL 0x3 +#define LP8727_INT1 0x4 +#define LP8727_INT2 0x5 +#define LP8727_STATUS1 0x6 +#define LP8727_STATUS2 0x7 +#define LP8727_CHGCTRL2 0x9 /* CTRL1 register */ -#define CP_EN (1 << 0) -#define ADC_EN (1 << 1) -#define ID200_EN (1 << 4) +#define LP8727_CP_EN BIT(0) +#define LP8727_ADC_EN BIT(1) +#define LP8727_ID200_EN BIT(4) /* CTRL2 register */ -#define CHGDET_EN (1 << 1) -#define INT_EN (1 << 6) +#define LP8727_CHGDET_EN BIT(1) +#define LP8727_INT_EN BIT(6) /* SWCTRL register */ -#define SW_DM1_DM (0x0 << 0) -#define SW_DM1_U1 (0x1 << 0) -#define SW_DM1_HiZ (0x7 << 0) -#define SW_DP2_DP (0x0 << 3) -#define SW_DP2_U2 (0x1 << 3) -#define SW_DP2_HiZ (0x7 << 3) +#define LP8727_SW_DM1_DM (0x0 << 0) +#define LP8727_SW_DM1_HiZ (0x7 << 0) +#define LP8727_SW_DP2_DP (0x0 << 3) +#define LP8727_SW_DP2_HiZ (0x7 << 3) /* INT1 register */ -#define IDNO (0xF << 0) -#define VBUS (1 << 4) +#define LP8727_IDNO (0xF << 0) +#define LP8727_VBUS BIT(4) /* STATUS1 register */ -#define CHGSTAT (3 << 4) -#define CHPORT (1 << 6) -#define DCPORT (1 << 7) -#define LP8727_STAT_EOC 0x30 +#define LP8727_CHGSTAT (3 << 4) +#define LP8727_CHPORT BIT(6) +#define LP8727_DCPORT BIT(7) +#define LP8727_STAT_EOC 0x30 /* STATUS2 register */ -#define TEMP_STAT (3 << 5) -#define TEMP_SHIFT 5 +#define LP8727_TEMP_STAT (3 << 5) +#define LP8727_TEMP_SHIFT 5 enum lp8727_dev_id { - ID_NONE, - ID_TA, - ID_DEDICATED_CHG, - ID_USB_CHG, - ID_USB_DS, - ID_MAX, + LP8727_ID_NONE, + LP8727_ID_TA, + LP8727_ID_DEDICATED_CHG, + LP8727_ID_USB_CHG, + LP8727_ID_USB_DS, + LP8727_ID_MAX, }; enum lp8727_die_temp { @@ -127,12 +125,12 @@ static int lp8727_is_charger_attached(const char *name, int id) { if (name) { if (!strcmp(name, "ac")) - return (id == ID_TA || id == ID_DEDICATED_CHG) ? 1 : 0; + return (id == LP8727_ID_TA || id == LP8727_ID_DEDICATED_CHG) ? 1 : 0; else if (!strcmp(name, "usb")) - return (id == ID_USB_CHG) ? 1 : 0; + return (id == LP8727_ID_USB_CHG) ? 1 : 0; } - return (id >= ID_TA && id <= ID_USB_CHG) ? 1 : 0; + return (id >= LP8727_ID_TA && id <= LP8727_ID_USB_CHG) ? 1 : 0; } static int lp8727_init_device(struct lp8727_chg *pchg) @@ -142,18 +140,18 @@ static int lp8727_init_device(struct lp8727_chg *pchg) u8 intstat[LP8788_NUM_INTREGS]; /* clear interrupts */ - ret = lp8727_read_bytes(pchg, INT1, intstat, LP8788_NUM_INTREGS); + ret = lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS); if (ret) return ret; - val = ID200_EN | ADC_EN | CP_EN; - ret = lp8727_write_byte(pchg, CTRL1, val); + val = LP8727_ID200_EN | LP8727_ADC_EN | LP8727_CP_EN; + ret = lp8727_write_byte(pchg, LP8727_CTRL1, val); if (ret) return ret; - val = INT_EN | CHGDET_EN; - ret = lp8727_write_byte(pchg, CTRL2, val); + val = LP8727_INT_EN | LP8727_CHGDET_EN; + ret = lp8727_write_byte(pchg, LP8727_CTRL2, val); if (ret) return ret; @@ -163,48 +161,48 @@ static int lp8727_init_device(struct lp8727_chg *pchg) static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg) { u8 val; - lp8727_read_byte(pchg, STATUS1, &val); - return val & DCPORT; + lp8727_read_byte(pchg, LP8727_STATUS1, &val); + return val & LP8727_DCPORT; } static int lp8727_is_usb_charger(struct lp8727_chg *pchg) { u8 val; - lp8727_read_byte(pchg, STATUS1, &val); - return val & CHPORT; + lp8727_read_byte(pchg, LP8727_STATUS1, &val); + return val & LP8727_CHPORT; } static void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw) { - lp8727_write_byte(pchg, SWCTRL, sw); + lp8727_write_byte(pchg, LP8727_SWCTRL, sw); } static void lp8727_id_detection(struct lp8727_chg *pchg, u8 id, int vbusin) { struct lp8727_platform_data *pdata = pchg->pdata; - u8 devid = ID_NONE; - u8 swctrl = SW_DM1_HiZ | SW_DP2_HiZ; + u8 devid = LP8727_ID_NONE; + u8 swctrl = LP8727_SW_DM1_HiZ | LP8727_SW_DP2_HiZ; switch (id) { case 0x5: - devid = ID_TA; + devid = LP8727_ID_TA; pchg->chg_parm = pdata ? pdata->ac : NULL; break; case 0xB: if (lp8727_is_dedicated_charger(pchg)) { pchg->chg_parm = pdata ? pdata->ac : NULL; - devid = ID_DEDICATED_CHG; + devid = LP8727_ID_DEDICATED_CHG; } else if (lp8727_is_usb_charger(pchg)) { pchg->chg_parm = pdata ? pdata->usb : NULL; - devid = ID_USB_CHG; - swctrl = SW_DM1_DM | SW_DP2_DP; + devid = LP8727_ID_USB_CHG; + swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP; } else if (vbusin) { - devid = ID_USB_DS; - swctrl = SW_DM1_DM | SW_DP2_DP; + devid = LP8727_ID_USB_DS; + swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP; } break; default: - devid = ID_NONE; + devid = LP8727_ID_NONE; pchg->chg_parm = NULL; break; } @@ -217,9 +215,9 @@ static void lp8727_enable_chgdet(struct lp8727_chg *pchg) { u8 val; - lp8727_read_byte(pchg, CTRL2, &val); - val |= CHGDET_EN; - lp8727_write_byte(pchg, CTRL2, val); + lp8727_read_byte(pchg, LP8727_CTRL2, &val); + val |= LP8727_CHGDET_EN; + lp8727_write_byte(pchg, LP8727_CTRL2, val); } static void lp8727_delayed_func(struct work_struct *_work) @@ -228,13 +226,13 @@ static void lp8727_delayed_func(struct work_struct *_work) struct lp8727_chg *pchg = container_of(_work, struct lp8727_chg, work.work); - if (lp8727_read_bytes(pchg, INT1, intstat, 2)) { + if (lp8727_read_bytes(pchg, LP8727_INT1, intstat, 2)) { dev_err(pchg->dev, "can not read INT registers\n"); return; } - idno = intstat[0] & IDNO; - vbus = intstat[0] & VBUS; + idno = intstat[0] & LP8727_IDNO; + vbus = intstat[0] & LP8727_VBUS; lp8727_id_detection(pchg, idno, vbus); lp8727_enable_chgdet(pchg); @@ -341,9 +339,9 @@ static int lp8727_battery_get_property(struct power_supply *psy, switch (psp) { case POWER_SUPPLY_PROP_STATUS: if (lp8727_is_charger_attached(psy->name, pchg->devid)) { - lp8727_read_byte(pchg, STATUS1, &read); + lp8727_read_byte(pchg, LP8727_STATUS1, &read); - val->intval = (read & CHGSTAT) == LP8727_STAT_EOC ? + val->intval = (read & LP8727_CHGSTAT) == LP8727_STAT_EOC ? POWER_SUPPLY_STATUS_FULL : POWER_SUPPLY_STATUS_CHARGING; } else { @@ -351,8 +349,8 @@ static int lp8727_battery_get_property(struct power_supply *psy, } break; case POWER_SUPPLY_PROP_HEALTH: - lp8727_read_byte(pchg, STATUS2, &read); - temp = (read & TEMP_STAT) >> TEMP_SHIFT; + lp8727_read_byte(pchg, LP8727_STATUS2, &read); + temp = (read & LP8727_TEMP_STAT) >> LP8727_TEMP_SHIFT; val->intval = lp8727_is_high_temperature(temp) ? POWER_SUPPLY_HEALTH_OVERHEAT : @@ -404,7 +402,7 @@ static void lp8727_charger_changed(struct power_supply *psy) eoc_level = pchg->chg_parm->eoc_level; ichg = pchg->chg_parm->ichg; val = (ichg << 4) | eoc_level; - lp8727_write_byte(pchg, CHGCTRL2, val); + lp8727_write_byte(pchg, LP8727_CHGCTRL2, val); } } } diff --git a/include/linux/platform_data/lp8727.h b/include/linux/platform_data/lp8727.h index 54b77880ed67..a8b8dbc418cc 100644 --- a/include/linux/platform_data/lp8727.h +++ b/include/linux/platform_data/lp8727.h @@ -13,26 +13,26 @@ #define _LP8727_H enum lp8727_eoc_level { - EOC_5P, - EOC_10P, - EOC_16P, - EOC_20P, - EOC_25P, - EOC_33P, - EOC_50P, + LP8727_EOC_5P, + LP8727_EOC_10P, + LP8727_EOC_16P, + LP8727_EOC_20P, + LP8727_EOC_25P, + LP8727_EOC_33P, + LP8727_EOC_50P, }; enum lp8727_ichg { - ICHG_90mA, - ICHG_100mA, - ICHG_400mA, - ICHG_450mA, - ICHG_500mA, - ICHG_600mA, - ICHG_700mA, - ICHG_800mA, - ICHG_900mA, - ICHG_1000mA, + LP8727_ICHG_90mA, + LP8727_ICHG_100mA, + LP8727_ICHG_400mA, + LP8727_ICHG_450mA, + LP8727_ICHG_500mA, + LP8727_ICHG_600mA, + LP8727_ICHG_700mA, + LP8727_ICHG_800mA, + LP8727_ICHG_900mA, + LP8727_ICHG_1000mA, }; /** -- cgit v1.2.3-58-ga151 From b9633ef1a9cdf4317d8c4a8db977485e2a8e1cb8 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:27:22 +0000 Subject: lp8727_charger: More pure cosmetic improvements This is really minor, but it improves the readability. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 3 ++- include/linux/platform_data/lp8727.h | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 717a97690dd4..c628224b7f58 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -149,7 +149,6 @@ static int lp8727_init_device(struct lp8727_chg *pchg) if (ret) return ret; - val = LP8727_ID200_EN | LP8727_ADC_EN | LP8727_CP_EN; ret = lp8727_write_byte(pchg, LP8727_CTRL1, val); if (ret) @@ -162,6 +161,7 @@ static int lp8727_init_device(struct lp8727_chg *pchg) static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg) { u8 val; + lp8727_read_byte(pchg, LP8727_STATUS1, &val); return val & LP8727_DCPORT; } @@ -169,6 +169,7 @@ static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg) static int lp8727_is_usb_charger(struct lp8727_chg *pchg) { u8 val; + lp8727_read_byte(pchg, LP8727_STATUS1, &val); return val & LP8727_CHPORT; } diff --git a/include/linux/platform_data/lp8727.h b/include/linux/platform_data/lp8727.h index a8b8dbc418cc..47128a50e04e 100644 --- a/include/linux/platform_data/lp8727.h +++ b/include/linux/platform_data/lp8727.h @@ -38,7 +38,7 @@ enum lp8727_ichg { /** * struct lp8727_chg_param * @eoc_level : end of charge level setting - * @ichg : charging current + * @ichg : charging current */ struct lp8727_chg_param { enum lp8727_eoc_level eoc_level; @@ -47,10 +47,10 @@ struct lp8727_chg_param { /** * struct lp8727_platform_data - * @get_batt_present : check battery status - exists or not - * @get_batt_level : get battery voltage (mV) + * @get_batt_present : check battery status - exists or not + * @get_batt_level : get battery voltage (mV) * @get_batt_capacity : get battery capacity (%) - * @get_batt_temp : get battery temperature + * @get_batt_temp : get battery temperature * @ac : charging parameters for AC type charger * @usb : charging parameters for USB type charger * @debounce_msec : interrupt debounce time -- cgit v1.2.3-58-ga151 From 2ed9e9b6530951b5b96185e6761119361a166d7a Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Tue, 21 Aug 2012 17:06:52 +0900 Subject: charger-manager: Check fully charged state of battery periodically This patch check periodically fully charged state of battery to protect overcharge and overheat. If battery is fully charged, stop charging and check droped voltage with 'fullbatt_vchkdrop_ms' period. When voltage of battery is more droped than 'fullbatt_vchkdrop_uV' voltage, charger-manager will restart charging for battery. Signed-off-by: Chanwoo Choi Signed-off-by: Myungjoo Ham Signed-off-by: Kyungmin Park Signed-off-by: Anton Vorontsov --- drivers/power/charger-manager.c | 161 ++++++++++++++++++++++++---------- include/linux/power/charger-manager.h | 8 +- 2 files changed, 123 insertions(+), 46 deletions(-) (limited to 'include') diff --git a/drivers/power/charger-manager.c b/drivers/power/charger-manager.c index cdf29d2eb7a5..c701f5e6f327 100644 --- a/drivers/power/charger-manager.c +++ b/drivers/power/charger-manager.c @@ -226,6 +226,58 @@ static bool is_charging(struct charger_manager *cm) return charging; } +/** + * is_full_charged - Returns true if the battery is fully charged. + * @cm: the Charger Manager representing the battery. + */ +static bool is_full_charged(struct charger_manager *cm) +{ + struct charger_desc *desc = cm->desc; + union power_supply_propval val; + int ret = 0; + int uV; + + /* If there is no battery, it cannot be charged */ + if (!is_batt_present(cm)) { + val.intval = 0; + goto out; + } + + if (cm->fuel_gauge && desc->fullbatt_full_capacity > 0) { + /* Not full if capacity of fuel gauge isn't full */ + ret = cm->fuel_gauge->get_property(cm->fuel_gauge, + POWER_SUPPLY_PROP_CHARGE_FULL, &val); + if (!ret && val.intval > desc->fullbatt_full_capacity) { + val.intval = 1; + goto out; + } + } + + /* Full, if it's over the fullbatt voltage */ + if (desc->fullbatt_uV > 0) { + ret = get_batt_uV(cm, &uV); + if (!ret && uV >= desc->fullbatt_uV) { + val.intval = 1; + goto out; + } + } + + /* Full, if the capacity is more than fullbatt_soc */ + if (cm->fuel_gauge && desc->fullbatt_soc > 0) { + ret = cm->fuel_gauge->get_property(cm->fuel_gauge, + POWER_SUPPLY_PROP_CAPACITY, &val); + if (!ret && val.intval >= desc->fullbatt_soc) { + val.intval = 1; + goto out; + } + } + + val.intval = 0; + +out: + return val.intval ? true : false; +} + /** * is_polling_required - Return true if need to continue polling for this CM. * @cm: the Charger Manager representing the battery. @@ -418,7 +470,7 @@ static void fullbatt_vchk(struct work_struct *work) diff = desc->fullbatt_uV; diff -= batt_uV; - dev_dbg(cm->dev, "VBATT dropped %duV after full-batt.\n", diff); + dev_info(cm->dev, "VBATT dropped %duV after full-batt.\n", diff); if (diff > desc->fullbatt_vchkdrop_uV) { try_charger_restart(cm); @@ -441,10 +493,14 @@ static bool _cm_monitor(struct charger_manager *cm) dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n", cm->last_temp_mC / 1000, cm->last_temp_mC % 1000); - /* It has been stopped or charging already */ - if (!!temp == !!cm->emergency_stop) + /* It has been stopped already */ + if (temp && cm->emergency_stop) return false; + /* + * Check temperature whether overheat or cold. + * If temperature is out of range normal state, stop charging. + */ if (temp) { cm->emergency_stop = temp; if (!try_charger_enable(cm, false)) { @@ -453,10 +509,34 @@ static bool _cm_monitor(struct charger_manager *cm) else uevent_notify(cm, "COLD"); } + + /* + * Check dropped voltage of battery. If battery voltage is more + * dropped than fullbatt_vchkdrop_uV after fully charged state, + * charger-manager have to recharge battery. + */ + } else if (!cm->emergency_stop && is_ext_pwr_online(cm) && + !cm->charger_enabled) { + fullbatt_vchk(&cm->fullbatt_vchk_work.work); + + /* + * Check whether fully charged state to protect overcharge + * if charger-manager is charging for battery. + */ + } else if (!cm->emergency_stop && is_full_charged(cm) && + cm->charger_enabled) { + dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n"); + uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]); + + try_charger_enable(cm, false); + + fullbatt_vchk(&cm->fullbatt_vchk_work.work); } else { cm->emergency_stop = 0; - if (!try_charger_enable(cm, true)) - uevent_notify(cm, "CHARGING"); + if (is_ext_pwr_online(cm)) { + if (!try_charger_enable(cm, true)) + uevent_notify(cm, "CHARGING"); + } } return true; @@ -719,47 +799,10 @@ static int charger_get_property(struct power_supply *psy, val->intval = 0; break; case POWER_SUPPLY_PROP_CHARGE_FULL: - if (cm->fuel_gauge) { - if (cm->fuel_gauge->get_property(cm->fuel_gauge, - POWER_SUPPLY_PROP_CHARGE_FULL, val) == 0) - break; - } - - if (is_ext_pwr_online(cm)) { - /* Not full if it's charging. */ - if (is_charging(cm)) { - val->intval = 0; - break; - } - /* - * Full if it's powered but not charging andi - * not forced stop by emergency - */ - if (!cm->emergency_stop) { - val->intval = 1; - break; - } - } - - /* Full if it's over the fullbatt voltage */ - ret = get_batt_uV(cm, &uV); - if (!ret && desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV && - !is_charging(cm)) { + if (is_full_charged(cm)) val->intval = 1; - break; - } - - /* Full if the cap is 100 */ - if (cm->fuel_gauge) { - ret = cm->fuel_gauge->get_property(cm->fuel_gauge, - POWER_SUPPLY_PROP_CAPACITY, val); - if (!ret && val->intval >= 100 && !is_charging(cm)) { - val->intval = 1; - break; - } - } - - val->intval = 0; + else + val->intval = 0; ret = 0; break; case POWER_SUPPLY_PROP_CHARGE_NOW: @@ -1049,7 +1092,26 @@ static int charger_extcon_notifier(struct notifier_block *self, struct charger_cable *cable = container_of(self, struct charger_cable, nb); + /* + * The newly state of charger cable. + * If cable is attached, cable->attached is true. + */ cable->attached = event; + + /* + * Setup monitoring to check battery state + * when charger cable is attached. + */ + if (cable->attached && is_polling_required(cable->cm)) { + if (work_pending(&setup_polling)) + cancel_work_sync(&setup_polling); + schedule_work(&setup_polling); + } + + /* + * Setup work for controlling charger(regulator) + * according to charger cable. + */ schedule_work(&cable->wq); return NOTIFY_DONE; @@ -1143,6 +1205,15 @@ static int charger_manager_probe(struct platform_device *pdev) desc->fullbatt_vchkdrop_ms = 0; desc->fullbatt_vchkdrop_uV = 0; } + if (desc->fullbatt_soc == 0) { + dev_info(&pdev->dev, "Ignoring full-battery soc(state of" + " charge) threshold as it is not" + " supplied."); + } + if (desc->fullbatt_full_capacity == 0) { + dev_info(&pdev->dev, "Ignoring full-battery full capacity" + " threshold as it is not supplied."); + } if (!desc->charger_regulators || desc->num_charger_regulators < 1) { ret = -EINVAL; diff --git a/include/linux/power/charger-manager.h b/include/linux/power/charger-manager.h index b542270affc1..76b37ef3c071 100644 --- a/include/linux/power/charger-manager.h +++ b/include/linux/power/charger-manager.h @@ -140,7 +140,11 @@ struct charger_regulator { * If it has dropped more than fullbatt_vchkdrop_uV after * fullbatt_vchkdrop_ms, CM will restart charging. * @fullbatt_uV: voltage in microvolt - * If it is not being charged and VBATT >= fullbatt_uV, + * If VBATT >= fullbatt_uV, it is assumed to be full. + * @fullbatt_soc: state of Charge in % + * If state of Charge >= fullbatt_soc, it is assumed to be full. + * @fullbatt_full_capacity: full capacity measure + * If full capacity of battery >= fullbatt_full_capacity, * it is assumed to be full. * @polling_interval_ms: interval in millisecond at which * charger manager will monitor battery health @@ -168,6 +172,8 @@ struct charger_desc { unsigned int fullbatt_vchkdrop_ms; unsigned int fullbatt_vchkdrop_uV; unsigned int fullbatt_uV; + unsigned int fullbatt_soc; + unsigned int fullbatt_full_capacity; enum data_source battery_present; -- cgit v1.2.3-58-ga151 From 8fcfe088e21aa0db9d62eaf565757def673efba6 Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Thu, 20 Sep 2012 21:20:05 -0700 Subject: charger-manager: Support limit of maximum possible This patch check maximum possible duration of charging/discharging. If whole charging duration exceed 'desc->charging_max_duration_ms', cm stop charging to prevent overcharge/overheat. And if discharging duration exceed, charger cable is attached, after full-batt, cm start charging to maintain fully charged state for battery. Signed-off-by: Chanwoo Choi Signed-off-by: Myungjoo Ham Signed-off-by: Kyungmin Park Signed-off-by: Anton Vorontsov --- drivers/power/charger-manager.c | 80 ++++++++++++++++++++++++++++++++++- include/linux/power/charger-manager.h | 15 +++++++ 2 files changed, 94 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/drivers/power/charger-manager.c b/drivers/power/charger-manager.c index c701f5e6f327..e92ec55ced91 100644 --- a/drivers/power/charger-manager.c +++ b/drivers/power/charger-manager.c @@ -323,6 +323,14 @@ static int try_charger_enable(struct charger_manager *cm, bool enable) if (enable) { if (cm->emergency_stop) return -EAGAIN; + + /* + * Save start time of charging to limit + * maximum possible charging time. + */ + cm->charging_start_time = ktime_to_ms(ktime_get()); + cm->charging_end_time = 0; + for (i = 0 ; i < desc->num_charger_regulators ; i++) { err = regulator_enable(desc->charger_regulators[i].consumer); if (err < 0) { @@ -332,6 +340,13 @@ static int try_charger_enable(struct charger_manager *cm, bool enable) } } } else { + /* + * Save end time of charging to maintain fully charged state + * of battery after full-batt. + */ + cm->charging_start_time = 0; + cm->charging_end_time = ktime_to_ms(ktime_get()); + for (i = 0 ; i < desc->num_charger_regulators ; i++) { err = regulator_disable(desc->charger_regulators[i].consumer); if (err < 0) { @@ -474,8 +489,55 @@ static void fullbatt_vchk(struct work_struct *work) if (diff > desc->fullbatt_vchkdrop_uV) { try_charger_restart(cm); - uevent_notify(cm, "Recharge"); + uevent_notify(cm, "Recharging"); + } +} + +/** + * check_charging_duration - Monitor charging/discharging duration + * @cm: the Charger Manager representing the battery. + * + * If whole charging duration exceed 'charging_max_duration_ms', + * cm stop charging to prevent overcharge/overheat. If discharging + * duration exceed 'discharging _max_duration_ms', charger cable is + * attached, after full-batt, cm start charging to maintain fully + * charged state for battery. + */ +static int check_charging_duration(struct charger_manager *cm) +{ + struct charger_desc *desc = cm->desc; + u64 curr = ktime_to_ms(ktime_get()); + u64 duration; + int ret = false; + + if (!desc->charging_max_duration_ms && + !desc->discharging_max_duration_ms) + return ret; + + if (cm->charger_enabled) { + duration = curr - cm->charging_start_time; + + if (duration > desc->charging_max_duration_ms) { + dev_info(cm->dev, "Charging duration exceed %lldms", + desc->charging_max_duration_ms); + uevent_notify(cm, "Discharging"); + try_charger_enable(cm, false); + ret = true; + } + } else if (is_ext_pwr_online(cm) && !cm->charger_enabled) { + duration = curr - cm->charging_end_time; + + if (duration > desc->charging_max_duration_ms && + is_ext_pwr_online(cm)) { + dev_info(cm->dev, "DisCharging duration exceed %lldms", + desc->discharging_max_duration_ms); + uevent_notify(cm, "Recharing"); + try_charger_enable(cm, true); + ret = true; + } } + + return ret; } /** @@ -510,6 +572,13 @@ static bool _cm_monitor(struct charger_manager *cm) uevent_notify(cm, "COLD"); } + /* + * Check whole charging duration and discharing duration + * after full-batt. + */ + } else if (!cm->emergency_stop && check_charging_duration(cm)) { + dev_dbg(cm->dev, + "Charging/Discharging duration is out of range"); /* * Check dropped voltage of battery. If battery voltage is more * dropped than fullbatt_vchkdrop_uV after fully charged state, @@ -1271,6 +1340,15 @@ static int charger_manager_probe(struct platform_device *pdev) goto err_chg_stat; } + if (!desc->charging_max_duration_ms || + !desc->discharging_max_duration_ms) { + dev_info(&pdev->dev, "Cannot limit charging duration " + "checking mechanism to prevent overcharge/overheat " + "and control discharging duration"); + desc->charging_max_duration_ms = 0; + desc->discharging_max_duration_ms = 0; + } + platform_set_drvdata(pdev, cm); memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default)); diff --git a/include/linux/power/charger-manager.h b/include/linux/power/charger-manager.h index 76b37ef3c071..a7b388ea1588 100644 --- a/include/linux/power/charger-manager.h +++ b/include/linux/power/charger-manager.h @@ -162,6 +162,13 @@ struct charger_regulator { * @measure_battery_temp: * true: measure battery temperature * false: measure ambient temperature + * @charging_max_duration_ms: Maximum possible duration for charging + * If whole charging duration exceed 'charging_max_duration_ms', + * cm stop charging. + * @discharging_max_duration_ms: + * Maximum possible duration for discharging with charger cable + * after full-batt. If discharging duration exceed 'discharging + * max_duration_ms', cm start charging. */ struct charger_desc { char *psy_name; @@ -186,6 +193,9 @@ struct charger_desc { int (*temperature_out_of_range)(int *mC); bool measure_battery_temp; + + u64 charging_max_duration_ms; + u64 discharging_max_duration_ms; }; #define PSY_NAME_MAX 30 @@ -210,6 +220,8 @@ struct charger_desc { * saved status of external power before entering suspend-to-RAM * @status_save_batt: * saved status of battery before entering suspend-to-RAM + * @charging_start_time: saved start time of enabling charging + * @charging_end_time: saved end time of disabling charging */ struct charger_manager { struct list_head entry; @@ -232,6 +244,9 @@ struct charger_manager { bool status_save_ext_pwr_inserted; bool status_save_batt; + + u64 charging_start_time; + u64 charging_end_time; }; #ifdef CONFIG_CHARGER_MANAGER -- cgit v1.2.3-58-ga151 From 3950c7865cd7c963982a2c94457182b96732f4c9 Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Fri, 21 Sep 2012 18:49:37 +0900 Subject: charger-manager: Add support sysfs entry for charger This patch add support sysfs entry for each charger(regulator). Charger-manager use one or more chargers for charging battery but some charger isn't necessary on specific scenario. So, if some charger isn't needed, can disable specific charger through 'externally_control' entry while system is on state and confirm the information(name, state) of charger. The list of added sysfs entry - /sys/class/power_supply/battery/chargers/charger.[index]/name show name of charger(regulator) - /sys/class/power_supply/battery/chargers/charger.[index]/state show either enabled or disabled state of charger - /sys/class/power_supply/battery/chargers/charger.[index]/externally_control If 'externally_control' of specific charger is 1, Charger-manager cannot enable regulator for charging when charger cable is attached and charger must be maintained with disabled state. If 'externally_control' is zero, Charger-manager usually can control to enable/disable regulator. Signed-off-by: Chanwoo Choi Signed-off-by: Myungjoo Ham Signed-off-by: Kyungmin Park Signed-off-by: Anton Vorontsov --- drivers/power/charger-manager.c | 172 ++++++++++++++++++++++++++++++++++ include/linux/power/charger-manager.h | 19 ++++ 2 files changed, 191 insertions(+) (limited to 'include') diff --git a/drivers/power/charger-manager.c b/drivers/power/charger-manager.c index e92ec55ced91..92dfa5c64876 100644 --- a/drivers/power/charger-manager.c +++ b/drivers/power/charger-manager.c @@ -22,6 +22,7 @@ #include #include #include +#include static const char * const default_event_names[] = { [CM_EVENT_UNKNOWN] = "Unknown", @@ -332,6 +333,9 @@ static int try_charger_enable(struct charger_manager *cm, bool enable) cm->charging_end_time = 0; for (i = 0 ; i < desc->num_charger_regulators ; i++) { + if (desc->charger_regulators[i].externally_control) + continue; + err = regulator_enable(desc->charger_regulators[i].consumer); if (err < 0) { dev_warn(cm->dev, @@ -348,6 +352,9 @@ static int try_charger_enable(struct charger_manager *cm, bool enable) cm->charging_end_time = ktime_to_ms(ktime_get()); for (i = 0 ; i < desc->num_charger_regulators ; i++) { + if (desc->charger_regulators[i].externally_control) + continue; + err = regulator_disable(desc->charger_regulators[i].consumer); if (err < 0) { dev_warn(cm->dev, @@ -1217,12 +1224,101 @@ static int charger_extcon_init(struct charger_manager *cm, return ret; } +/* help function of sysfs node to control charger(regulator) */ +static ssize_t charger_name_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct charger_regulator *charger + = container_of(attr, struct charger_regulator, attr_name); + + return sprintf(buf, "%s\n", charger->regulator_name); +} + +static ssize_t charger_state_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct charger_regulator *charger + = container_of(attr, struct charger_regulator, attr_state); + int state = 0; + + if (!charger->externally_control) + state = regulator_is_enabled(charger->consumer); + + return sprintf(buf, "%s\n", state ? "enabled" : "disabled"); +} + +static ssize_t charger_externally_control_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct charger_regulator *charger = container_of(attr, + struct charger_regulator, attr_externally_control); + + return sprintf(buf, "%d\n", charger->externally_control); +} + +static ssize_t charger_externally_control_store(struct device *dev, + struct device_attribute *attr, const char *buf, + size_t count) +{ + struct charger_regulator *charger + = container_of(attr, struct charger_regulator, + attr_externally_control); + struct charger_manager *cm = charger->cm; + struct charger_desc *desc = cm->desc; + int i; + int ret; + int externally_control; + int chargers_externally_control = 1; + + ret = sscanf(buf, "%d", &externally_control); + if (ret == 0) { + ret = -EINVAL; + return ret; + } + + if (!externally_control) { + charger->externally_control = 0; + return count; + } + + for (i = 0; i < desc->num_charger_regulators; i++) { + if (&desc->charger_regulators[i] != charger && + !desc->charger_regulators[i].externally_control) { + /* + * At least, one charger is controlled by + * charger-manager + */ + chargers_externally_control = 0; + break; + } + } + + if (!chargers_externally_control) { + if (cm->charger_enabled) { + try_charger_enable(charger->cm, false); + charger->externally_control = externally_control; + try_charger_enable(charger->cm, true); + } else { + charger->externally_control = externally_control; + } + } else { + dev_warn(cm->dev, + "'%s' regulator should be controlled " + "in charger-manager because charger-manager " + "must need at least one charger for charging\n", + charger->regulator_name); + } + + return count; +} + static int charger_manager_probe(struct platform_device *pdev) { struct charger_desc *desc = dev_get_platdata(&pdev->dev); struct charger_manager *cm; int ret = 0, i = 0; int j = 0; + int chargers_externally_control = 1; union power_supply_propval val; if (g_desc && !rtc_dev && g_desc->rtc_name) { @@ -1412,6 +1508,8 @@ static int charger_manager_probe(struct platform_device *pdev) for (i = 0 ; i < desc->num_charger_regulators ; i++) { struct charger_regulator *charger = &desc->charger_regulators[i]; + char buf[11]; + char *str; charger->consumer = regulator_get(&pdev->dev, charger->regulator_name); @@ -1421,6 +1519,7 @@ static int charger_manager_probe(struct platform_device *pdev) ret = -EINVAL; goto err_chg_get; } + charger->cm = cm; for (j = 0 ; j < charger->num_cables ; j++) { struct charger_cable *cable = &charger->cables[j]; @@ -1434,6 +1533,71 @@ static int charger_manager_probe(struct platform_device *pdev) cable->charger = charger; cable->cm = cm; } + + /* Create sysfs entry to control charger(regulator) */ + snprintf(buf, 10, "charger.%d", i); + str = kzalloc(sizeof(char) * (strlen(buf) + 1), GFP_KERNEL); + if (!str) { + for (i--; i >= 0; i--) { + charger = &desc->charger_regulators[i]; + kfree(charger->attr_g.name); + } + ret = -ENOMEM; + + goto err_extcon; + } + strcpy(str, buf); + + charger->attrs[0] = &charger->attr_name.attr; + charger->attrs[1] = &charger->attr_state.attr; + charger->attrs[2] = &charger->attr_externally_control.attr; + charger->attrs[3] = NULL; + charger->attr_g.name = str; + charger->attr_g.attrs = charger->attrs; + + sysfs_attr_init(&charger->attr_name.attr); + charger->attr_name.attr.name = "name"; + charger->attr_name.attr.mode = 0444; + charger->attr_name.show = charger_name_show; + + sysfs_attr_init(&charger->attr_state.attr); + charger->attr_state.attr.name = "state"; + charger->attr_state.attr.mode = 0444; + charger->attr_state.show = charger_state_show; + + sysfs_attr_init(&charger->attr_externally_control.attr); + charger->attr_externally_control.attr.name + = "externally_control"; + charger->attr_externally_control.attr.mode = 0644; + charger->attr_externally_control.show + = charger_externally_control_show; + charger->attr_externally_control.store + = charger_externally_control_store; + + if (!desc->charger_regulators[i].externally_control || + !chargers_externally_control) { + chargers_externally_control = 0; + } + dev_info(&pdev->dev, "'%s' regulator's externally_control" + "is %d\n", charger->regulator_name, + charger->externally_control); + + ret = sysfs_create_group(&cm->charger_psy.dev->kobj, + &charger->attr_g); + if (ret < 0) { + dev_info(&pdev->dev, "Cannot create sysfs entry" + "of %s regulator\n", + charger->regulator_name); + } + } + + if (chargers_externally_control) { + dev_err(&pdev->dev, "Cannot register regulator because " + "charger-manager must need at least " + "one charger for charging battery\n"); + + ret = -EINVAL; + goto err_chg_enable; } ret = try_charger_enable(cm, true); @@ -1459,6 +1623,14 @@ static int charger_manager_probe(struct platform_device *pdev) return 0; err_chg_enable: + for (i = 0; i < desc->num_charger_regulators; i++) { + struct charger_regulator *charger; + + charger = &desc->charger_regulators[i]; + sysfs_remove_group(&cm->charger_psy.dev->kobj, + &charger->attr_g); + kfree(charger->attr_g.name); + } err_extcon: for (i = 0 ; i < desc->num_charger_regulators ; i++) { struct charger_regulator *charger diff --git a/include/linux/power/charger-manager.h b/include/linux/power/charger-manager.h index a7b388ea1588..0e86840eb603 100644 --- a/include/linux/power/charger-manager.h +++ b/include/linux/power/charger-manager.h @@ -109,24 +109,43 @@ struct charger_cable { * struct charger_regulator * @regulator_name: the name of regulator for using charger. * @consumer: the regulator consumer for the charger. + * @externally_control: + * Set if the charger-manager cannot control charger, + * the charger will be maintained with disabled state. * @cables: * the array of charger cables to enable/disable charger * and set current limit according to constratint data of * struct charger_cable if only charger cable included * in the array of charger cables is attached/detached. * @num_cables: the number of charger cables. + * @attr_g: Attribute group for the charger(regulator) + * @attr_name: "name" sysfs entry + * @attr_state: "state" sysfs entry + * @attr_externally_control: "externally_control" sysfs entry + * @attrs: Arrays pointing to attr_name/state/externally_control for attr_g */ struct charger_regulator { /* The name of regulator for charging */ const char *regulator_name; struct regulator *consumer; + /* charger never on when system is on */ + int externally_control; + /* * Store constraint information related to current limit, * each cable have different condition for charging. */ struct charger_cable *cables; int num_cables; + + struct attribute_group attr_g; + struct device_attribute attr_name; + struct device_attribute attr_state; + struct device_attribute attr_externally_control; + struct attribute *attrs[4]; + + struct charger_manager *cm; }; /** -- cgit v1.2.3-58-ga151