Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp842127imm; Fri, 1 Jun 2018 10:25:47 -0700 (PDT) X-Google-Smtp-Source: ADUXVKL/swkH7VDTOtCrkd7g5LGiDn45DyLNcupg3s+NFXfOUU8IypRxP9LYNrAsItwSw5PAnpJJ X-Received: by 2002:a63:744a:: with SMTP id e10-v6mr9615095pgn.275.1527873947316; Fri, 01 Jun 2018 10:25:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527873947; cv=none; d=google.com; s=arc-20160816; b=bz3Yx0sj6da6UHllNwbne62MTaq7uPiCj2Hccgeb3sfLlMqTyDWBLS2FXlgICiBttX fU+VNTfdha1iGtSInVLiIJGJpKC3lNPe73T10TYwsTAAfqvK8JYAHp75kcTRktJLTKbw BkXEkPd2aUOIIHXM606/u+NhdeoUYCwhmZVi0SALGt86HDkT5CDy5jy49Q6x6uLnnz9/ umEnzbip32150lZbXp1iMXpBp+RdJqLNzozuLt7BtlpyGKj40DYyC9vQyToatN88D2/T nNoEWNs6GSrJmqqE8ElHuQO24PdXdZxD9CaQRnYre7RD9KFPqzgJuDl6mqSF2ZSdTRbg F1ng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=12uX0eaAZvBo4RMM+ShNuoNHc89jV4REk8CSjvpIocY=; b=cQC89JLcxmgE5BrvtSba09Uht/zjIpRC+J/4c/Cuwmsat7Z4zo8zhyTVroNm0sy9p+ 4oZxgM5ON9RcgRo6HerbryGpOw6MYrNy6NvK0g802jzYL3YF4BDpNd3iH3uYge/UZ9fa fpQZXHUj3TlQVcKk9M48YcMSdoKbG8bZ5/PdA5BuC6ZrKE2G1Nko645ICdVT14kN2elP +Qow7FG5o/9ZGgsCvN0iIWsOKCldSSPxsEnx1GOctZkjU3AWwWpnZ/JzPpFrnIydeZWF lGTxwwBsgyGnCBpYghd6b1wJeQy1IfNLgxZT2FS0pJkcSf1SD97y63PRa1a+qwsCaoVR cj2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=KjcIR2Pq; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s13-v6si39299458plp.350.2018.06.01.10.25.33; Fri, 01 Jun 2018 10:25:47 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=KjcIR2Pq; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752741AbeFARYR (ORCPT + 99 others); Fri, 1 Jun 2018 13:24:17 -0400 Received: from mail-pg0-f68.google.com ([74.125.83.68]:32823 "EHLO mail-pg0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752778AbeFARYL (ORCPT ); Fri, 1 Jun 2018 13:24:11 -0400 Received: by mail-pg0-f68.google.com with SMTP id e11-v6so2361549pgq.0 for ; Fri, 01 Jun 2018 10:24:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id; bh=12uX0eaAZvBo4RMM+ShNuoNHc89jV4REk8CSjvpIocY=; b=KjcIR2PqByf132lXERlz3tT9NxnwXnrrIxgbK8D00omD/Jnn7U5Xub7pHqRl9Y2Aux 9fPtMIoi1cjs0Pwy+1g5ue9OkggUFAX9uyjHfv6EjtnBMT9ndGTiuF/jK/GMaW+VAxFS TWOxTQ1BL4iYq2I30fiX3gwhjG/TyJJ8hJ2Bc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=12uX0eaAZvBo4RMM+ShNuoNHc89jV4REk8CSjvpIocY=; b=fcI/f8GuZH61iCc3H8Uvi++IHwb3U/o9gDW6CtwIYGlKBtCwhbo7yaxqoyUgfhUIEc ymVxeV0fxwvHfoNAHOxa1x/A8PzwLzv5FDDG8iBsngaQ8HUkewVsTenAiyaOE9tojgQF kmOCWrgL1Xwx5UO65/ionl6Ez7xDnZatKzzAjlcvpOylM2IvOQaWSeTEy2H5R/mHb8uD T+F0WNKPJRXvXFHO78/+b6pPtT822SuwiKVMfHCqi97Ch6rR0OE8Haqxm4muChzZpHTv fxSsI2fMiIRZyOazqH5L0+u8yLzg8Gp/Z5SfoL6NqpwE3ow9APThgraeaZ3FfSsaNbrt mpiQ== X-Gm-Message-State: ALKqPwc108cdqRn81uMl59P1WAoHr2CoY2B4wDViU/p6H7cKFTsVWvne uB9+uYMXpPzjL0y7ex3JdHuCpRti88o= X-Received: by 2002:a65:44c8:: with SMTP id g8-v6mr1955601pgs.356.1527873850684; Fri, 01 Jun 2018 10:24:10 -0700 (PDT) Received: from ban.mtv.corp.google.com ([2620:0:1000:1501:bc2f:3082:9938:5d41]) by smtp.gmail.com with ESMTPSA id t17-v6sm12265627pfj.75.2018.06.01.10.24.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 01 Jun 2018 10:24:09 -0700 (PDT) From: Brian Norris To: Sebastian Reichel Cc: , Rob Herring , linux-pm@vger.kernel.org, devicetree@vger.kernel.org, Rhyland Klein , Alexandru Stan , Guenter Roeck , Doug Anderson , Brian Norris Subject: [PATCH v2 1/2] power: supply: sbs-battery: don't assume MANUFACTURER_DATA formats Date: Fri, 1 Jun 2018 10:23:59 -0700 Message-Id: <20180601172400.50737-1-briannorris@chromium.org> X-Mailer: git-send-email 2.17.0.921.gf22659ad46-goog Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This driver was originally submitted for the TI BQ20Z75 battery IC (commit a7640bfa10c5 ("power_supply: Add driver for TI BQ20Z75 gas gauge IC")) and later renamed to express generic SBS support. While it's mostly true that this driver implemented a standard SBS command set, it takes liberties with the REG_MANUFACTURER_DATA register. This register is specified in the SBS spec, but it doesn't make any mention of what its actual contents are. We've sort of noticed this optionality previously, with commit 17c6d3979e5b ("sbs-battery: make writes to ManufacturerAccess optional"), where we found that some batteries NAK writes to this register. What this really means is that so far, we've just been lucky that most batteries have either been compatible with the TI chip, or else at least haven't reported highly-unexpected values. For instance, one battery I have here seems to report either 0x0000 or 0x0100 to the MANUFACTURER_ACCESS_STATUS command -- while this seems to match either Wake Up (bits[11:8] = 0000b) or Normal Discharge (bits[11:8] = 0001b) status for the TI part [1], they don't seem to actually correspond to real states (for instance, I never see 0101b = Charge, even when charging). On other batteries, I'm getting apparently random data in return, which means that occasionally, we interpret this as "battery not present" or "battery is not healthy". All in all, it seems to be a really bad idea to make assumptions about REG_MANUFACTURER_DATA, unless we already know what battery we're using. Therefore, this patch reimplements the "present" and "health" checks to the following on most SBS batteries: 1. HEALTH: report "unknown" -- I couldn't find a standard SBS command that gives us much useful here 2. PRESENT: just send a REG_STATUS command; if it succeeds, then the battery is present Also, we stop sending MANUFACTURER_ACCESS_SLEEP to non-TI parts. I have no proof that this is useful and supported. If someone explicitly provided a 'ti,bq20z75' compatible property, then we retain the existing TI command behaviors. [1] http://www.ti.com/lit/er/sluu265a/sluu265a.pdf Signed-off-by: Brian Norris --- v2: * don't stub out POWER_SUPPLY_PROP_PRESENT from sbs_data[] * use if/else instead of switch/case --- drivers/power/supply/sbs-battery.c | 54 +++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/drivers/power/supply/sbs-battery.c b/drivers/power/supply/sbs-battery.c index 83d7b4115857..8dea63464a3f 100644 --- a/drivers/power/supply/sbs-battery.c +++ b/drivers/power/supply/sbs-battery.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -156,6 +157,9 @@ static enum power_supply_property sbs_properties[] = { POWER_SUPPLY_PROP_MODEL_NAME }; +/* Supports special manufacturer commands from TI BQ20Z75 IC. */ +#define SBS_FLAGS_TI_BQ20Z75 BIT(0) + struct sbs_info { struct i2c_client *client; struct power_supply *power_supply; @@ -168,6 +172,7 @@ struct sbs_info { u32 poll_retry_count; struct delayed_work work; struct mutex mode_lock; + u32 flags; }; static char model_name[I2C_SMBUS_BLOCK_MAX + 1]; @@ -315,6 +320,27 @@ static int sbs_status_correct(struct i2c_client *client, int *intval) static int sbs_get_battery_presence_and_health( struct i2c_client *client, enum power_supply_property psp, union power_supply_propval *val) +{ + int ret; + + if (psp == POWER_SUPPLY_PROP_PRESENT) { + /* Dummy command; if it succeeds, battery is present. */ + ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr); + if (ret < 0) + val->intval = 0; /* battery disconnected */ + else + val->intval = 1; /* battery present */ + return 0; + } else { /* POWER_SUPPLY_PROP_HEALTH */ + /* SBS spec doesn't have a general health command. */ + val->intval = POWER_SUPPLY_HEALTH_UNKNOWN; + return 0; + } +} + +static int sbs_get_ti_battery_presence_and_health( + struct i2c_client *client, enum power_supply_property psp, + union power_supply_propval *val) { s32 ret; @@ -600,7 +626,12 @@ static int sbs_get_property(struct power_supply *psy, switch (psp) { case POWER_SUPPLY_PROP_PRESENT: case POWER_SUPPLY_PROP_HEALTH: - ret = sbs_get_battery_presence_and_health(client, psp, val); + if (client->flags & SBS_FLAGS_TI_BQ20Z75) + ret = sbs_get_ti_battery_presence_and_health(client, + psp, val); + else + ret = sbs_get_battery_presence_and_health(client, psp, + val); if (psp == POWER_SUPPLY_PROP_PRESENT) return 0; break; @@ -806,6 +837,7 @@ static int sbs_probe(struct i2c_client *client, if (!chip) return -ENOMEM; + chip->flags = (u32)(uintptr_t)of_device_get_match_data(&client->dev); chip->client = client; chip->enable_detection = false; psy_cfg.of_node = client->dev.of_node; @@ -915,12 +947,15 @@ static int sbs_suspend(struct device *dev) if (chip->poll_time > 0) cancel_delayed_work_sync(&chip->work); - /* - * Write to manufacturer access with sleep command. - * Support is manufacturer dependend, so ignore errors. - */ - sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr, - MANUFACTURER_ACCESS_SLEEP); + if (chip->flags & SBS_FLAGS_TI_BQ20Z75) { + /* + * Write to manufacturer access with sleep command. + * Support is manufacturer dependent, so ignore errors. + */ + sbs_write_word_data(client, + sbs_data[REG_MANUFACTURER_DATA].addr, + MANUFACTURER_ACCESS_SLEEP); + } return 0; } @@ -941,7 +976,10 @@ MODULE_DEVICE_TABLE(i2c, sbs_id); static const struct of_device_id sbs_dt_ids[] = { { .compatible = "sbs,sbs-battery" }, - { .compatible = "ti,bq20z75" }, + { + .compatible = "ti,bq20z75", + .data = (void *)SBS_FLAGS_TI_BQ20Z75, + }, { } }; MODULE_DEVICE_TABLE(of, sbs_dt_ids); -- 2.17.0.921.gf22659ad46-goog