[2/2] power: supply: sbs_battery: Support Manufacture Date field

Message ID 20200609192237.32571-2-twawrzynczak@chromium.org
State New
Headers show
Series
  • Untitled series #38160
Related show

Commit Message

Tim Wawrzynczak June 9, 2020, 7:22 p.m.
This patch adds support for the sbs-battery driver to export a battery's
manufacture date as a field in sysfs. It is exported as a simple Y/M/D
format for ease of consumption.

Signed-off-by: Tim Wawrzynczak <twawrzynczak@chromium.org>
Change-Id: Ia952624f33700587f4d71ad09ad68c0940c7f508
---
 drivers/power/supply/sbs-battery.c | 31 +++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

Patch

diff --git a/drivers/power/supply/sbs-battery.c b/drivers/power/supply/sbs-battery.c
index f8d74e9f79317..989625be80743 100644
--- a/drivers/power/supply/sbs-battery.c
+++ b/drivers/power/supply/sbs-battery.c
@@ -42,6 +42,7 @@  enum {
 	REG_DESIGN_VOLTAGE_MAX,
 	REG_MANUFACTURER,
 	REG_MODEL_NAME,
+	REG_MANUFACTURE_DATE,
 };
 
 /* Battery Mode defines */
@@ -112,6 +113,10 @@  static const struct chip_data {
 		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
 	[REG_DESIGN_VOLTAGE_MAX] =
 		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
+	/* Manufacture Date is encoded as a 16-bit register, but is exported
+	 * from here as a string */
+	[REG_MANUFACTURE_DATE] =
+		SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURE_DATE, 0x1B, 0, 65535),
 	[REG_SERIAL_NUMBER] =
 		SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
 	/* Properties of type `const char *' */
@@ -145,7 +150,8 @@  static enum power_supply_property sbs_properties[] = {
 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
 	/* Properties of type `const char *' */
 	POWER_SUPPLY_PROP_MANUFACTURER,
-	POWER_SUPPLY_PROP_MODEL_NAME
+	POWER_SUPPLY_PROP_MODEL_NAME,
+	POWER_SUPPLY_PROP_MANUFACTURE_DATE,
 };
 
 /* Supports special manufacturer commands from TI BQ20Z75 IC. */
@@ -170,6 +176,10 @@  static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
 static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
 static bool force_load;
 
+/* manufacture_date is returned as a 16-bit word in the smart battery itself.
+ * It is decoded here and displayed as "%4d/%2d/%2d", Y/m/d format. */
+static char manufacture_date[11];
+
 static int sbs_read_word_data(struct i2c_client *client, u8 address)
 {
 	struct sbs_info *chip = i2c_get_clientdata(client);
@@ -682,6 +692,25 @@  static int sbs_get_property(struct power_supply *psy,
 		ret = sbs_get_battery_property(client, ret, psp, val);
 		break;
 
+	case POWER_SUPPLY_PROP_MANUFACTURE_DATE:
+		ret = sbs_get_property_index(client, psp);
+		if (ret < 0)
+			break;
+
+		ret = sbs_get_battery_property(client, ret, psp, val);
+
+		/* Convert encoded data (from the SBS 1.1 spec) into a string:
+		 * bits 9-15 are the year, encoded as years since 1980
+		 * bits 5-8 are the month, encoded as 1-12
+		 * bits 0-4 are the day of the month, encoded as 1-31 */
+		snprintf(manufacture_date, sizeof(manufacture_date),
+			 "%4d/%2d/%2d",
+			 ((val->intval >> 9) & 0x7f) + 1980,
+			 (val->intval >> 5) & 0xf,
+			 val->intval & 0x1f);
+		val->strval = manufacture_date;
+		break;
+
 	case POWER_SUPPLY_PROP_MODEL_NAME:
 		ret = sbs_get_property_index(client, psp);
 		if (ret < 0)