Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp814261ybk; Wed, 13 May 2020 13:55:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzJ+m2CwaTxFC3b1mcwdBMw+HSgAI/+l8pSsaa3VJT7gMA8RtU2B39t4ShMjXqyNOcYA4b/ X-Received: by 2002:a17:906:7750:: with SMTP id o16mr900824ejn.12.1589403318261; Wed, 13 May 2020 13:55:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589403318; cv=none; d=google.com; s=arc-20160816; b=riImBi8IBafV/LlbLSc3IvoWM5AhMqO/dSzwgTHx3Z3MkJYSwULitGx6WgepAZtAOO 3ryw6xuNLimnlktD3G1zNhcySgz95Kh80qVxbNMZ0G4kXvyOnOzKMKJE+kCphkGN4WJP QA0nGfMJRxh+Fe4oXxXMxcM1HOaCsCDoOXfQU+rSUAkOd/rNuvcEw84/Cpib59Z9lnQ+ W8re+bScDipdr57cDg1F8iDw+35H/g/aFuE8neXIfvY7xywOVTKfzi6tiXtuu0cfwjMm IdtJ8nqyPWipUr/RXTJAFnnEYOiSiE75fIK0L462BMKlUU1b3+Z6K1KlFCE5xWDwTnBO qIog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=SyJnI9TkS4iqPk4BBXglelCm0hn+kHLo2E4BFbilHNo=; b=MPKhTDYA9Ovcx3nnGOrktzDAp3o5Ts28BVs6WeAYV61wg2fM2i+/1uqrkw78SgOfMQ 2wFZ/BY+/IudfBOUXmkXapHlvGK3E7TGm5B81WtFe0Rh3IIhgz0vxOYY02I5r2My7hxM AtwmylTOQlp2qgiigwRsQiYZ0PLzm0T3vq9OQa36nCyHzR5eJbHsZpT9gaMSULGnUngT gYzG9lGH2BssO82MDxGHPCC+pUgjdkY7EvzE/q0LI3HH3fQCpa+dXi7MhHPWW/SX94Fi ZW+qHWS3KsZ8x8cz6MsWOIwpaikuE5gkYd9Yb88d9+1ChVEn+1flASRnYFcLsgoV0LXA ZPYw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q25si56246edw.313.2020.05.13.13.54.55; Wed, 13 May 2020 13:55:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390551AbgEMS5d (ORCPT + 99 others); Wed, 13 May 2020 14:57:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S2390394AbgEMS4g (ORCPT ); Wed, 13 May 2020 14:56:36 -0400 Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CEE64C05BD09; Wed, 13 May 2020 11:56:35 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: sre) with ESMTPSA id 4D5722A2521 Received: by jupiter.universe (Postfix, from userid 1000) id 777C3480105; Wed, 13 May 2020 20:56:29 +0200 (CEST) From: Sebastian Reichel To: Sebastian Reichel , Rob Herring , Greg Kroah-Hartman , "Rafael J . Wysocki" Cc: linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, kernel@collabora.com, Sebastian Reichel Subject: [PATCHv1 07/19] power: supply: sbs-battery: simplify read_read_string_data Date: Wed, 13 May 2020 20:56:03 +0200 Message-Id: <20200513185615.508236-8-sebastian.reichel@collabora.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200513185615.508236-1-sebastian.reichel@collabora.com> References: <20200513185615.508236-1-sebastian.reichel@collabora.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The SBS battery implements SMBus block reads. Currently the driver "emulates" this by doing an I2C byte read for the length followed by an I2C block read. The I2C subsystem actually provides a proper API for doing SMBus block reads, which can and should be used instead. The current implementation does not properly handle packet error checking (PEC). This change requires, that I2C bus drivers support I2C_M_RECV_LEN or directly provide the SMBus API to access device manufacturer and model name. Signed-off-by: Sebastian Reichel --- drivers/power/supply/sbs-battery.c | 65 ++++++------------------------ 1 file changed, 12 insertions(+), 53 deletions(-) diff --git a/drivers/power/supply/sbs-battery.c b/drivers/power/supply/sbs-battery.c index 4356fdf25d4a..a9a1d28dabbe 100644 --- a/drivers/power/supply/sbs-battery.c +++ b/drivers/power/supply/sbs-battery.c @@ -202,66 +202,32 @@ static int sbs_read_string_data(struct i2c_client *client, u8 address, char *values) { struct sbs_info *chip = i2c_get_clientdata(client); - s32 ret = 0, block_length = 0; - int retries_length, retries_block; - u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1]; - - retries_length = chip->i2c_retry_count; - retries_block = chip->i2c_retry_count; + int retries = chip->i2c_retry_count; + s32 ret = 0; - /* Adapter needs to support these two functions */ if (!i2c_check_functionality(client->adapter, - I2C_FUNC_SMBUS_BYTE_DATA | - I2C_FUNC_SMBUS_I2C_BLOCK)){ + I2C_FUNC_SMBUS_READ_BLOCK_DATA)) { return -ENODEV; } - /* Get the length of block data */ - while (retries_length > 0) { - ret = i2c_smbus_read_byte_data(client, address); - if (ret >= 0) - break; - retries_length--; - } - - if (ret < 0) { - dev_dbg(&client->dev, - "%s: i2c read at address 0x%x failed\n", - __func__, address); - return ret; - } - - /* block_length does not include NULL terminator */ - block_length = ret; - if (block_length > I2C_SMBUS_BLOCK_MAX) { - dev_err(&client->dev, - "%s: Returned block_length is longer than 0x%x\n", - __func__, I2C_SMBUS_BLOCK_MAX); - return -EINVAL; - } - /* Get the block data */ - while (retries_block > 0) { - ret = i2c_smbus_read_i2c_block_data( - client, address, - block_length + 1, block_buffer); + while (retries > 0) { + ret = i2c_smbus_read_block_data(client, address, values); if (ret >= 0) break; - retries_block--; + retries--; } if (ret < 0) { - dev_dbg(&client->dev, - "%s: i2c read at address 0x%x failed\n", - __func__, address); + dev_dbg(&client->dev, "%s: failed to read block 0x%x: %d\n", + __func__, address, ret); return ret; } - /* block_buffer[0] == block_length */ - memcpy(values, block_buffer + 1, block_length); - values[block_length] = '\0'; + /* add string termination */ + values[ret] = '\0'; - return ret; + return 0; } static int sbs_write_word_data(struct i2c_client *client, u8 address, @@ -465,14 +431,7 @@ static int sbs_get_battery_property(struct i2c_client *client, static int sbs_get_battery_string_property(struct i2c_client *client, int reg_offset, enum power_supply_property psp, char *val) { - s32 ret; - - ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val); - - if (ret < 0) - return ret; - - return 0; + return sbs_read_string_data(client, sbs_data[reg_offset].addr, val); } static void sbs_unit_adjustment(struct i2c_client *client, -- 2.26.2