linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Hans de Goede <hdegoede@redhat.com>
To: Sebastian Reichel <sre@kernel.org>,
	Krzysztof Kozlowski <krzk@kernel.org>,
	Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>
Cc: Hans de Goede <hdegoede@redhat.com>, linux-pm@vger.kernel.org
Subject: [PATCH v2 06/14] power: max17042_battery: Add support for the STATUS property
Date: Fri, 14 Apr 2017 20:32:51 +0200	[thread overview]
Message-ID: <20170414183259.24382-6-hdegoede@redhat.com> (raw)
In-Reply-To: <20170414183259.24382-1-hdegoede@redhat.com>

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>
---
Changes in v2:
-Move MAX17042_FULL_THRESHOLD define to max17042.h
---
 drivers/power/supply/max17042_battery.c | 46 +++++++++++++++++++++++++++++++++
 include/linux/power/max17042_battery.h  |  3 +++
 2 files changed, 49 insertions(+)

diff --git a/drivers/power/supply/max17042_battery.c b/drivers/power/supply/max17042_battery.c
index 3249eb0..13909eb 100644
--- a/drivers/power/supply/max17042_battery.c
+++ b/drivers/power/supply/max17042_battery.c
@@ -76,6 +76,7 @@ struct max17042_chip {
 };
 
 static enum power_supply_property max17042_battery_props[] = {
+	POWER_SUPPLY_PROP_STATUS,
 	POWER_SUPPLY_PROP_PRESENT,
 	POWER_SUPPLY_PROP_CYCLE_COUNT,
 	POWER_SUPPLY_PROP_VOLTAGE_MAX,
@@ -113,6 +114,46 @@ static int max17042_get_temperature(struct max17042_chip *chip, int *temp)
 	return 0;
 }
 
+static int max17042_get_status(struct max17042_chip *chip, int *status)
+{
+	int ret, charge_full, charge_now;
+
+	ret = power_supply_am_i_supplied(chip->battery);
+	if (ret < 0) {
+		*status = POWER_SUPPLY_STATUS_UNKNOWN;
+		return 0;
+	}
+	if (ret == 0) {
+		*status = POWER_SUPPLY_STATUS_DISCHARGING;
+		return 0;
+	}
+
+	/*
+	 * The MAX170xx has builtin end-of-charge detection and will update
+	 * FullCAP to match RepCap when it detects end of charging.
+	 *
+	 * When this cycle the battery gets charged to a higher (calculated)
+	 * capacity then the previous cycle then FullCAP will get updated
+	 * contineously once end-of-charge detection kicks in, so allow the
+	 * 2 to differ a bit.
+	 */
+
+	ret = regmap_read(chip->regmap, MAX17042_FullCAP, &charge_full);
+	if (ret < 0)
+		return ret;
+
+	ret = regmap_read(chip->regmap, MAX17042_RepCap, &charge_now);
+	if (ret < 0)
+		return ret;
+
+	if ((charge_full - charge_now) <= MAX17042_FULL_THRESHOLD)
+		*status = POWER_SUPPLY_STATUS_FULL;
+	else
+		*status = POWER_SUPPLY_STATUS_CHARGING;
+
+	return 0;
+}
+
 static int max17042_get_battery_health(struct max17042_chip *chip, int *health)
 {
 	int temp, vavg, vbatt, ret;
@@ -182,6 +223,11 @@ static int max17042_get_property(struct power_supply *psy,
 		return -EAGAIN;
 
 	switch (psp) {
+	case POWER_SUPPLY_PROP_STATUS:
+		ret = max17042_get_status(chip, &val->intval);
+		if (ret < 0)
+			return ret;
+		break;
 	case POWER_SUPPLY_PROP_PRESENT:
 		ret = regmap_read(map, MAX17042_STATUS, &data);
 		if (ret < 0)
diff --git a/include/linux/power/max17042_battery.h b/include/linux/power/max17042_battery.h
index 3489fb0..a7ed29b 100644
--- a/include/linux/power/max17042_battery.h
+++ b/include/linux/power/max17042_battery.h
@@ -31,6 +31,9 @@
 #define MAX17042_DEFAULT_TEMP_MIN	(0)    /* For sys without temp sensor */
 #define MAX17042_DEFAULT_TEMP_MAX	(700)  /* 70 degrees Celcius */
 
+/* Consider RepCap which is less then 10 units below FullCAP full */
+#define MAX17042_FULL_THRESHOLD		10
+
 #define MAX17042_CHARACTERIZATION_DATA_SIZE 48
 
 enum max17042_register {
-- 
2.9.3

  parent reply	other threads:[~2017-04-14 18:33 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-14 18:32 [PATCH v2 01/14] power: Make power_supply_am_i_supplied return -ENODEV if there are no suppliers Hans de Goede
2017-04-14 18:32 ` [PATCH v2 02/14] power: max17042_battery: Use sign_extend32 instead of DIY code Hans de Goede
2017-05-01 11:36   ` Sebastian Reichel
2017-04-14 18:32 ` [PATCH v2 03/14] power: max17047_battery: The temp alert values are 8-bit 2's complement Hans de Goede
2017-05-01 11:37   ` Sebastian Reichel
2017-04-14 18:32 ` [PATCH v2 04/14] power: max17042_battery: Add default platform_data fallback data Hans de Goede
2017-04-15 10:31   ` Krzysztof Kozlowski
2017-05-01 11:37   ` Sebastian Reichel
2017-04-14 18:32 ` [PATCH v2 05/14] power: max17042_battery: Change name in power_supply_desc to "main-battery" Hans de Goede
2017-05-01 12:22   ` Sebastian Reichel
2017-05-01 15:11     ` Hans de Goede
2017-05-01 15:50       ` Sebastian Reichel
2017-04-14 18:32 ` Hans de Goede [this message]
2017-04-15 10:32   ` [PATCH v2 06/14] power: max17042_battery: Add support for the STATUS property Krzysztof Kozlowski
2017-05-01 11:38   ` Sebastian Reichel
2017-04-14 18:32 ` [PATCH v2 07/14] power: max17042_battery: Add external_power_changed callback Hans de Goede
2017-05-01 11:38   ` Sebastian Reichel
2017-04-14 18:32 ` [PATCH v2 08/14] power: max17042_battery: Add support for the TECHNOLOGY attribute Hans de Goede
2017-04-15 10:33   ` Krzysztof Kozlowski
2017-05-01 11:39   ` Sebastian Reichel
2017-04-14 18:32 ` [PATCH v2 09/14] power: max17042_battery: Add support for the VOLT_MIN property Hans de Goede
2017-05-01 11:39   ` Sebastian Reichel
2017-04-14 18:32 ` [PATCH v2 10/14] power: max17042_battery: mAh readings depend on r_sns value Hans de Goede
2017-04-15 10:38   ` Krzysztof Kozlowski
2017-05-01 11:39   ` Sebastian Reichel
2017-04-14 18:32 ` [PATCH v2 11/14] power: max17042_battery: Add support for the CHARGE_FULL_DESIGN property Hans de Goede
2017-04-15 10:38   ` Krzysztof Kozlowski
2017-05-01 11:40   ` Sebastian Reichel
2017-04-14 18:32 ` [PATCH v2 12/14] power: max17042_battery: Add support for the CHARGE_NOW property Hans de Goede
2017-04-15 10:39   ` Krzysztof Kozlowski
2017-05-01 11:40   ` Sebastian Reichel
2017-04-14 18:32 ` [PATCH v2 13/14] power: max17042_battery: Add support for the SCOPE property Hans de Goede
2017-05-01 11:41   ` Sebastian Reichel
2017-04-14 18:32 ` [PATCH v2 14/14] power: max77693_charger: Add supplied_to info to power_supply_config Hans de Goede
2017-05-01 12:19   ` Sebastian Reichel
2017-04-15 10:30 ` [PATCH v2 01/14] power: Make power_supply_am_i_supplied return -ENODEV if there are no suppliers Krzysztof Kozlowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170414183259.24382-6-hdegoede@redhat.com \
    --to=hdegoede@redhat.com \
    --cc=b.zolnierkie@samsung.com \
    --cc=krzk@kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=sre@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).