aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHans de Goede <hdegoede@redhat.com>2018-01-20 08:50:24 -0500
committerSebastian Reichel <sebastian.reichel@collabora.co.uk>2018-01-22 11:53:35 -0500
commit6e5ab19d54e8cd7bdc779db0b807403558dbea62 (patch)
tree19ee5d7dd4f309f99d453a70fd866fbff5da8146
parent8be4c3667cf117b8f8a2b0b73a34258d45992867 (diff)
power: supply: max17042_battery: Check battery current for status when supplied
Even though the system is supplied, it may still be discharging if the supply is e.g. only delivering 5V 0.5A. Check the avg battery current if available for more accurate status reporting. Cc: James <kernel@madingley.org> Suggested-by: James <kernel@madingley.org> Signed-off-by: Hans de Goede <hdegoede@redhat.com> Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.co.uk>
-rw-r--r--drivers/power/supply/max17042_battery.c27
1 files changed, 25 insertions, 2 deletions
diff --git a/drivers/power/supply/max17042_battery.c b/drivers/power/supply/max17042_battery.c
index 5b556a13f517..102b3f71e9a4 100644
--- a/drivers/power/supply/max17042_battery.c
+++ b/drivers/power/supply/max17042_battery.c
@@ -123,6 +123,8 @@ static int max17042_get_temperature(struct max17042_chip *chip, int *temp)
123static int max17042_get_status(struct max17042_chip *chip, int *status) 123static int max17042_get_status(struct max17042_chip *chip, int *status)
124{ 124{
125 int ret, charge_full, charge_now; 125 int ret, charge_full, charge_now;
126 int avg_current;
127 u32 data;
126 128
127 ret = power_supply_am_i_supplied(chip->battery); 129 ret = power_supply_am_i_supplied(chip->battery);
128 if (ret < 0) { 130 if (ret < 0) {
@@ -152,10 +154,31 @@ static int max17042_get_status(struct max17042_chip *chip, int *status)
152 if (ret < 0) 154 if (ret < 0)
153 return ret; 155 return ret;
154 156
155 if ((charge_full - charge_now) <= MAX17042_FULL_THRESHOLD) 157 if ((charge_full - charge_now) <= MAX17042_FULL_THRESHOLD) {
156 *status = POWER_SUPPLY_STATUS_FULL; 158 *status = POWER_SUPPLY_STATUS_FULL;
157 else 159 return 0;
160 }
161
162 /*
163 * Even though we are supplied, we may still be discharging if the
164 * supply is e.g. only delivering 5V 0.5A. Check current if available.
165 */
166 if (!chip->pdata->enable_current_sense) {
167 *status = POWER_SUPPLY_STATUS_CHARGING;
168 return 0;
169 }
170
171 ret = regmap_read(chip->regmap, MAX17042_AvgCurrent, &data);
172 if (ret < 0)
173 return ret;
174
175 avg_current = sign_extend32(data, 15);
176 avg_current *= 1562500 / chip->pdata->r_sns;
177
178 if (avg_current > 0)
158 *status = POWER_SUPPLY_STATUS_CHARGING; 179 *status = POWER_SUPPLY_STATUS_CHARGING;
180 else
181 *status = POWER_SUPPLY_STATUS_DISCHARGING;
159 182
160 return 0; 183 return 0;
161} 184}