2021-05-26 07:50:32

by Ikjoon Jang

[permalink] [raw]
Subject: [PATCH v3] power: supply: sbs-battery: cache constant string properties

Currently sbs-battery supports three string properties -
manufacturer, model_name, and chemistry. Buffers for those
properties are currently defined as global variables.

This patch moves those global variables into struct sbs_info
and cache/reuse them as they are all constant values.

Signed-off-by: Ikjoon Jang <[email protected]>
Tested-by: Hsin-Yi Wang <[email protected]>

---

Changes in v3:
- Invalidate cached properties upon update_presence(!present)
- Fix a bug in reading chemistry

Changes in v2:
- change function name of sbs_get_battery_string_property()
to sbs_get_constant_string()
- use cached string properties
- use cached technology value in sbs_get_chemistry()

drivers/power/supply/sbs-battery.c | 153 ++++++++++++++++++-----------
1 file changed, 95 insertions(+), 58 deletions(-)

diff --git a/drivers/power/supply/sbs-battery.c b/drivers/power/supply/sbs-battery.c
index b6a538ebb378..56eaef8c32fc 100644
--- a/drivers/power/supply/sbs-battery.c
+++ b/drivers/power/supply/sbs-battery.c
@@ -188,6 +188,14 @@ static const enum power_supply_property sbs_properties[] = {
/* Supports special manufacturer commands from TI BQ20Z65 and BQ20Z75 IC. */
#define SBS_FLAGS_TI_BQ20ZX5 BIT(0)

+static const enum power_supply_property string_properties[] = {
+ POWER_SUPPLY_PROP_TECHNOLOGY,
+ POWER_SUPPLY_PROP_MANUFACTURER,
+ POWER_SUPPLY_PROP_MODEL_NAME,
+};
+
+#define NR_STRING_BUFFERS ARRAY_SIZE(string_properties)
+
struct sbs_info {
struct i2c_client *client;
struct power_supply *power_supply;
@@ -201,11 +209,32 @@ struct sbs_info {
struct delayed_work work;
struct mutex mode_lock;
u32 flags;
+ int technology;
+ char strings[NR_STRING_BUFFERS][I2C_SMBUS_BLOCK_MAX + 1];
};

-static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
-static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
-static char chemistry[I2C_SMBUS_BLOCK_MAX + 1];
+static char *sbs_get_string_buf(struct sbs_info *chip,
+ enum power_supply_property psp)
+{
+ int i = 0;
+
+ for (i = 0; i < NR_STRING_BUFFERS; i++)
+ if (string_properties[i] == psp)
+ return chip->strings[i];
+
+ return ERR_PTR(-EINVAL);
+}
+
+static void sbs_invalidate_cached_props(struct sbs_info *chip)
+{
+ int i = 0;
+
+ chip->technology = -1;
+
+ for (i = 0; i < NR_STRING_BUFFERS; i++)
+ if (chip->strings[i][0] = 0;
+}
+
static bool force_load;

static int sbs_read_word_data(struct i2c_client *client, u8 address);
@@ -243,6 +272,7 @@ static int sbs_update_presence(struct sbs_info *chip, bool is_present)
chip->is_present = false;
/* Disable PEC when no device is present */
client->flags &= ~I2C_CLIENT_PEC;
+ sbs_invalidate_cached_props(chip);
return 0;
}

@@ -639,17 +669,45 @@ static int sbs_get_battery_property(struct i2c_client *client,
return 0;
}

-static int sbs_get_battery_string_property(struct i2c_client *client,
- int reg_offset, enum power_supply_property psp, char *val)
+static int sbs_get_property_index(struct i2c_client *client,
+ enum power_supply_property psp)
{
- s32 ret;
+ int count;

- ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
+ for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
+ if (psp == sbs_data[count].psp)
+ return count;

- if (ret < 0)
- return ret;
+ dev_warn(&client->dev,
+ "%s: Invalid Property - %d\n", __func__, psp);

- return 0;
+ return -EINVAL;
+}
+
+static const char *sbs_get_constant_string(struct sbs_info *chip,
+ enum power_supply_property psp)
+{
+ int ret;
+ char *buf;
+ u8 addr;
+
+ buf = sbs_get_string_buf(chip, psp);
+ if (IS_ERR(buf))
+ return buf;
+
+ if (!buf[0]) {
+ ret = sbs_get_property_index(chip->client, psp);
+ if (ret < 0)
+ return ERR_PTR(ret);
+
+ addr = sbs_data[ret].addr;
+
+ ret = sbs_read_string_data(chip->client, addr, buf);
+ if (ret < 0)
+ return ERR_PTR(ret);
+ }
+
+ return buf;
}

static void sbs_unit_adjustment(struct i2c_client *client,
@@ -772,48 +830,36 @@ static int sbs_get_battery_serial_number(struct i2c_client *client,
return 0;
}

-static int sbs_get_property_index(struct i2c_client *client,
- enum power_supply_property psp)
-{
- int count;
- for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
- if (psp == sbs_data[count].psp)
- return count;
-
- dev_warn(&client->dev,
- "%s: Invalid Property - %d\n", __func__, psp);
-
- return -EINVAL;
-}
-
-static int sbs_get_chemistry(struct i2c_client *client,
+static int sbs_get_chemistry(struct sbs_info *chip,
union power_supply_propval *val)
{
- enum power_supply_property psp = POWER_SUPPLY_PROP_TECHNOLOGY;
- int ret;
+ const char *chemistry;

- ret = sbs_get_property_index(client, psp);
- if (ret < 0)
- return ret;
+ if (chip->technology != -1) {
+ val->intval = chip->technology;
+ return 0;
+ }

- ret = sbs_get_battery_string_property(client, ret, psp,
- chemistry);
- if (ret < 0)
- return ret;
+ chemistry = sbs_get_constant_string(chip, POWER_SUPPLY_PROP_TECHNOLOGY);
+
+ if (IS_ERR(chemistry))
+ return PTR_ERR(chemistry);

if (!strncasecmp(chemistry, "LION", 4))
- val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
+ chip->technology = POWER_SUPPLY_TECHNOLOGY_LION;
else if (!strncasecmp(chemistry, "LiP", 3))
- val->intval = POWER_SUPPLY_TECHNOLOGY_LIPO;
+ chip->technology = POWER_SUPPLY_TECHNOLOGY_LIPO;
else if (!strncasecmp(chemistry, "NiCd", 4))
- val->intval = POWER_SUPPLY_TECHNOLOGY_NiCd;
+ chip->technology = POWER_SUPPLY_TECHNOLOGY_NiCd;
else if (!strncasecmp(chemistry, "NiMH", 4))
- val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
+ chip->technology = POWER_SUPPLY_TECHNOLOGY_NiMH;
else
- val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
+ chip->technology = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
+
+ if (chip->technology == POWER_SUPPLY_TECHNOLOGY_UNKNOWN)
+ dev_warn(&chip->client->dev, "Unknown chemistry: %s\n", chemistry);

- if (val->intval == POWER_SUPPLY_TECHNOLOGY_UNKNOWN)
- dev_warn(&client->dev, "Unknown chemistry: %s\n", chemistry);
+ val->intval = chip->technology;

return 0;
}
@@ -857,6 +903,7 @@ static int sbs_get_property(struct power_supply *psy,
int ret = 0;
struct sbs_info *chip = power_supply_get_drvdata(psy);
struct i2c_client *client = chip->client;
+ const char *str;

if (chip->gpio_detect) {
ret = gpiod_get_value_cansleep(chip->gpio_detect);
@@ -882,7 +929,7 @@ static int sbs_get_property(struct power_supply *psy,
break;

case POWER_SUPPLY_PROP_TECHNOLOGY:
- ret = sbs_get_chemistry(client, val);
+ ret = sbs_get_chemistry(chip, val);
if (ret < 0)
break;

@@ -934,23 +981,12 @@ static int sbs_get_property(struct power_supply *psy,
break;

case POWER_SUPPLY_PROP_MODEL_NAME:
- ret = sbs_get_property_index(client, psp);
- if (ret < 0)
- break;
-
- ret = sbs_get_battery_string_property(client, ret, psp,
- model_name);
- val->strval = model_name;
- break;
-
case POWER_SUPPLY_PROP_MANUFACTURER:
- ret = sbs_get_property_index(client, psp);
- if (ret < 0)
- break;
-
- ret = sbs_get_battery_string_property(client, ret, psp,
- manufacturer);
- val->strval = manufacturer;
+ str = sbs_get_constant_string(chip, psp);
+ if (IS_ERR(str))
+ ret = PTR_ERR(str);
+ else
+ val->strval = str;
break;

case POWER_SUPPLY_PROP_MANUFACTURE_YEAR:
@@ -1097,6 +1133,7 @@ static int sbs_probe(struct i2c_client *client)
psy_cfg.of_node = client->dev.of_node;
psy_cfg.drv_data = chip;
chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
+ sbs_invalidate_cached_props(chip);
mutex_init(&chip->mode_lock);

/* use pdata if available, fall back to DT properties,
--
2.31.1.818.g46aad6cb9e-goog


2021-05-26 08:40:35

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v3] power: supply: sbs-battery: cache constant string properties

Hi Ikjoon,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on power-supply/for-next]
[also build test WARNING on v5.13-rc3 next-20210525]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url: https://github.com/0day-ci/linux/commits/Ikjoon-Jang/power-supply-sbs-battery-cache-constant-string-properties/20210526-132701
base: https://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-power-supply.git for-next
config: i386-randconfig-a015-20210526 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build):
# https://github.com/0day-ci/linux/commit/584859e4c0fe02c3c7b9b4e8c4a2c99d5212fe21
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Ikjoon-Jang/power-supply-sbs-battery-cache-constant-string-properties/20210526-132701
git checkout 584859e4c0fe02c3c7b9b4e8c4a2c99d5212fe21
# save the attached .config to linux build tree
make W=1 ARCH=i386

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>

All warnings (new ones prefixed by >>):

drivers/power/supply/sbs-battery.c: In function 'sbs_invalidate_cached_props':
drivers/power/supply/sbs-battery.c:1288: error: unterminated argument list invoking macro "if"
1288 | "Attempt to load the driver even if no battery is connected");
|
drivers/power/supply/sbs-battery.c:1288: error: expected '(' at end of input
1288 | "Attempt to load the driver even if no battery is connected");
|
|(
>> drivers/power/supply/sbs-battery.c:236:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
236 | if (chip->strings[i][0] = 0;
| ^~
drivers/power/supply/sbs-battery.c:1288: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
1288 | "Attempt to load the driver even if no battery is connected");
|
drivers/power/supply/sbs-battery.c:1288: note: '-Wmisleading-indentation' is disabled from this point onwards, since column-tracking was disabled due to the size of the code/headers
drivers/power/supply/sbs-battery.c:236:3: error: expected declaration or statement at end of input
236 | if (chip->strings[i][0] = 0;
| ^~
At top level:
drivers/power/supply/sbs-battery.c:229:13: warning: 'sbs_invalidate_cached_props' defined but not used [-Wunused-function]
229 | static void sbs_invalidate_cached_props(struct sbs_info *chip)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/power/supply/sbs-battery.c:217:14: warning: 'sbs_get_string_buf' defined but not used [-Wunused-function]
217 | static char *sbs_get_string_buf(struct sbs_info *chip,
| ^~~~~~~~~~~~~~~~~~
drivers/power/supply/sbs-battery.c:155:41: warning: 'sbs_properties' defined but not used [-Wunused-const-variable=]
155 | static const enum power_supply_property sbs_properties[] = {
| ^~~~~~~~~~~~~~
drivers/power/supply/sbs-battery.c:97:3: warning: 'sbs_data' defined but not used [-Wunused-const-variable=]
97 | } sbs_data[] = {
| ^~~~~~~~


vim +/if +236 drivers/power/supply/sbs-battery.c

228
229 static void sbs_invalidate_cached_props(struct sbs_info *chip)
230 {
231 int i = 0;
232
233 chip->technology = -1;
234
235 for (i = 0; i < NR_STRING_BUFFERS; i++)
> 236 if (chip->strings[i][0] = 0;
237 }
238

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]


Attachments:
(No filename) (3.83 kB)
.config.gz (32.81 kB)
Download all attachments

2021-05-26 09:02:47

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v3] power: supply: sbs-battery: cache constant string properties

Hi Ikjoon,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on power-supply/for-next]
[also build test ERROR on v5.13-rc3 next-20210525]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url: https://github.com/0day-ci/linux/commits/Ikjoon-Jang/power-supply-sbs-battery-cache-constant-string-properties/20210526-132701
base: https://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-power-supply.git for-next
config: arm-randconfig-r036-20210526 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 99155e913e9bad5f7f8a247f8bb3a3ff3da74af1)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install arm cross compiling tool for clang build
# apt-get install binutils-arm-linux-gnueabi
# https://github.com/0day-ci/linux/commit/584859e4c0fe02c3c7b9b4e8c4a2c99d5212fe21
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Ikjoon-Jang/power-supply-sbs-battery-cache-constant-string-properties/20210526-132701
git checkout 584859e4c0fe02c3c7b9b4e8c4a2c99d5212fe21
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>

All error/warnings (new ones prefixed by >>):

>> drivers/power/supply/sbs-battery.c:236:27: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
if (chip->strings[i][0] = 0;
~~~~~~~~~~~~~~~~~~~~^~~
drivers/power/supply/sbs-battery.c:236:27: note: place parentheses around the assignment to silence this warning
if (chip->strings[i][0] = 0;
^
( )
drivers/power/supply/sbs-battery.c:236:27: note: use '==' to turn this assignment into an equality comparison
if (chip->strings[i][0] = 0;
^
==
>> drivers/power/supply/sbs-battery.c:236:30: error: expected ')'
if (chip->strings[i][0] = 0;
^
drivers/power/supply/sbs-battery.c:236:6: note: to match this '('
if (chip->strings[i][0] = 0;
^
>> drivers/power/supply/sbs-battery.c:236:30: warning: if statement has empty body [-Wempty-body]
if (chip->strings[i][0] = 0;
^
drivers/power/supply/sbs-battery.c:236:30: note: put the semicolon on a separate line to silence this warning
2 warnings and 1 error generated.


vim +236 drivers/power/supply/sbs-battery.c

228
229 static void sbs_invalidate_cached_props(struct sbs_info *chip)
230 {
231 int i = 0;
232
233 chip->technology = -1;
234
235 for (i = 0; i < NR_STRING_BUFFERS; i++)
> 236 if (chip->strings[i][0] = 0;
237 }
238

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]


Attachments:
(No filename) (3.47 kB)
.config.gz (33.81 kB)
Download all attachments