aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHans de Goede <hdegoede@redhat.com>2017-04-14 14:32:51 -0400
committerSebastian Reichel <sre@kernel.org>2017-05-01 06:37:29 -0400
commita9df22c00d7c2c9c2944c62f1b819de6c214660f (patch)
tree8c81794bb217d09821e704bbb3566b09d228d291
parent917362135b8a5c0680acf08807e9fc6179eb6c79 (diff)
power: supply: max17042_battery: Add support for the STATUS property
Userspace prefers the driver having a status property over having to guess itself. Specifically this will properly make the GNOME3 UI (and likely others) properly show discharging / charging / full status, instead of always showing discharging as status. Note that in the case there is no charger driver supplying the max17042, then a status of unknown will get returned. At least upower treats this the same as not having a status attribute, so in this case nothing changes from a userspace pov. Signed-off-by: Hans de Goede <hdegoede@redhat.com> Reviewed-by: Krzysztof Kozlowski <krzk@kernel.org> Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.co.uk>
-rw-r--r--drivers/power/supply/max17042_battery.c46
-rw-r--r--include/linux/power/max17042_battery.h3
2 files changed, 49 insertions, 0 deletions
diff --git a/drivers/power/supply/max17042_battery.c b/drivers/power/supply/max17042_battery.c
index f0ff6e880ff2..62efe7eeb3f8 100644
--- a/drivers/power/supply/max17042_battery.c
+++ b/drivers/power/supply/max17042_battery.c
@@ -76,6 +76,7 @@ struct max17042_chip {
76}; 76};
77 77
78static enum power_supply_property max17042_battery_props[] = { 78static enum power_supply_property max17042_battery_props[] = {
79 POWER_SUPPLY_PROP_STATUS,
79 POWER_SUPPLY_PROP_PRESENT, 80 POWER_SUPPLY_PROP_PRESENT,
80 POWER_SUPPLY_PROP_CYCLE_COUNT, 81 POWER_SUPPLY_PROP_CYCLE_COUNT,
81 POWER_SUPPLY_PROP_VOLTAGE_MAX, 82 POWER_SUPPLY_PROP_VOLTAGE_MAX,
@@ -113,6 +114,46 @@ static int max17042_get_temperature(struct max17042_chip *chip, int *temp)
113 return 0; 114 return 0;
114} 115}
115 116
117static int max17042_get_status(struct max17042_chip *chip, int *status)
118{
119 int ret, charge_full, charge_now;
120
121 ret = power_supply_am_i_supplied(chip->battery);
122 if (ret < 0) {
123 *status = POWER_SUPPLY_STATUS_UNKNOWN;
124 return 0;
125 }
126 if (ret == 0) {
127 *status = POWER_SUPPLY_STATUS_DISCHARGING;
128 return 0;
129 }
130
131 /*
132 * The MAX170xx has builtin end-of-charge detection and will update
133 * FullCAP to match RepCap when it detects end of charging.
134 *
135 * When this cycle the battery gets charged to a higher (calculated)
136 * capacity then the previous cycle then FullCAP will get updated
137 * contineously once end-of-charge detection kicks in, so allow the
138 * 2 to differ a bit.
139 */
140
141 ret = regmap_read(chip->regmap, MAX17042_FullCAP, &charge_full);
142 if (ret < 0)
143 return ret;
144
145 ret = regmap_read(chip->regmap, MAX17042_RepCap, &charge_now);
146 if (ret < 0)
147 return ret;
148
149 if ((charge_full - charge_now) <= MAX17042_FULL_THRESHOLD)
150 *status = POWER_SUPPLY_STATUS_FULL;
151 else
152 *status = POWER_SUPPLY_STATUS_CHARGING;
153
154 return 0;
155}
156
116static int max17042_get_battery_health(struct max17042_chip *chip, int *health) 157static int max17042_get_battery_health(struct max17042_chip *chip, int *health)
117{ 158{
118 int temp, vavg, vbatt, ret; 159 int temp, vavg, vbatt, ret;
@@ -182,6 +223,11 @@ static int max17042_get_property(struct power_supply *psy,
182 return -EAGAIN; 223 return -EAGAIN;
183 224
184 switch (psp) { 225 switch (psp) {
226 case POWER_SUPPLY_PROP_STATUS:
227 ret = max17042_get_status(chip, &val->intval);
228 if (ret < 0)
229 return ret;
230 break;
185 case POWER_SUPPLY_PROP_PRESENT: 231 case POWER_SUPPLY_PROP_PRESENT:
186 ret = regmap_read(map, MAX17042_STATUS, &data); 232 ret = regmap_read(map, MAX17042_STATUS, &data);
187 if (ret < 0) 233 if (ret < 0)
diff --git a/include/linux/power/max17042_battery.h b/include/linux/power/max17042_battery.h
index 3489fb0f9099..a7ed29baf44a 100644
--- a/include/linux/power/max17042_battery.h
+++ b/include/linux/power/max17042_battery.h
@@ -31,6 +31,9 @@
31#define MAX17042_DEFAULT_TEMP_MIN (0) /* For sys without temp sensor */ 31#define MAX17042_DEFAULT_TEMP_MIN (0) /* For sys without temp sensor */
32#define MAX17042_DEFAULT_TEMP_MAX (700) /* 70 degrees Celcius */ 32#define MAX17042_DEFAULT_TEMP_MAX (700) /* 70 degrees Celcius */
33 33
34/* Consider RepCap which is less then 10 units below FullCAP full */
35#define MAX17042_FULL_THRESHOLD 10
36
34#define MAX17042_CHARACTERIZATION_DATA_SIZE 48 37#define MAX17042_CHARACTERIZATION_DATA_SIZE 48
35 38
36enum max17042_register { 39enum max17042_register {