From patchwork Tue Sep 6 13:16:33 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnd Bergmann X-Patchwork-Id: 101856 Delivered-To: patch@linaro.org Received: by 10.140.106.11 with SMTP id d11csp543672qgf; Tue, 6 Sep 2016 06:16:39 -0700 (PDT) X-Received: by 10.98.64.193 with SMTP id f62mr72448677pfd.141.1473167799898; Tue, 06 Sep 2016 06:16:39 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c203si35476676pfb.235.2016.09.06.06.16.39; Tue, 06 Sep 2016 06:16:39 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-pm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-pm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934314AbcIFNQh (ORCPT + 14 others); Tue, 6 Sep 2016 09:16:37 -0400 Received: from mout.kundenserver.de ([212.227.126.187]:54766 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932538AbcIFNQg (ORCPT ); Tue, 6 Sep 2016 09:16:36 -0400 Received: from wuerfel.lan. ([78.43.20.153]) by mrelayeu.kundenserver.de (mreue005) with ESMTPA (Nemesis) id 0MW7OX-1bavWo2AUT-00XKv4; Tue, 06 Sep 2016 15:16:15 +0200 From: Arnd Bergmann To: Sebastian Reichel Cc: Phil Reid , Arnd Bergmann , YH Huang , Joshua Clayton , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] power: supply: sbs-battery: simplify DT parsing Date: Tue, 6 Sep 2016 15:16:33 +0200 Message-Id: <20160906131643.1930011-1-arnd@arndb.de> X-Mailer: git-send-email 2.9.0 X-Provags-ID: V03:K0:9UZPYtDhN3JTNHfVNTcuNz5iS5m057kSmu6R6180J/41oR4cRCC SiCH2TfwH4IihVIV/xjiOwFQNXwiZSkWa073+nCU4e7ZEm6DpmfX7X1uxJGsD68i+IsaQe8 UKUqx5qQf9Ld+AalwatYq5SM3VjSR6HTqOnThp7waA1sZMk68tOvqI7YD1z+W9U19iFhHsx wBUGiRgnrZiQqNC6FfeEw== X-UI-Out-Filterresults: notjunk:1; V01:K0:8vSIN8LW7dE=:lpvaGEu0RSb7azRHs+URLr HuTLX9rhBwJgeFhtaErxSx7XdzzxxqjF/Uhx+635LWJNTcabWopPgYLUeFXlTTYx3/Wn0u/n9 lw+AaJ20lqcNkRQsAo/fuZ2+gA1hH3mmZrIRdjIgp7zRHo7I2LX4sftVheTWDt9Ot/Jzwdj5S 0blQgGVWI1qCp77G/Faqa3wCrEsmZzx3yEQzV5kEkbJmBQRIv+lC0lFvU2f2M55jrpcH8pIoR llO4tYGWheIbh16dmdHRUKcNW2SPwLixRaBxGm75HmO4Xnl8aNLjF+V8vDo93sxcW/gghji9F pbzoxjKOCt5FBMv4+TY4hue6j54KKW61LT+YCOQXp4clw/siauqDbG8t71nGW7PyXOTYMzDm0 Uk0pNyljeKC6dYfedlsCyIAOCGOJ0jqRG0Ccy6jz5qbOZMcV9be/q/fxCQbqbdpcDiHHeGIij Id7kPqq9GixGWdFZaXcC0QYu0/IM47bUfBh9dBo5D7Akm1TgwgMhnd3fYzjpi7/TFeEglztIp AuddmBZLoXEtW0A/KwJaY4d98maHGXXr0b0N/cdtDdazvYdV4Ir5jYI5RGlxf0HzYEZl/iJck FeJgv1iHYCbp/ld++JgSI6hJwUFq5KHX/HzRPQ3zGCFLxSVvNBfOHCpm0ncirs3ShTaB6M2s/ uwauoGR88bh+Tz3Q0OMgkUbkhP51Yz81x9qTJBF5hpE6mbpvMDP1d28ESJiENgs/LPfE= Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org After the change to use the gpio descriptor interface, we get a warning if -Wmaybe-uninitialized is added back to the build flags (it is currently disabled: drivers/power/supply/sbs-battery.c: In function 'sbs_probe': drivers/power/supply/sbs-battery.c:760:28: error: 'pdata' may be used uninitialized in this function [-Werror=maybe-uninitialized] The problem is that if neither the DT properties nor a platform_data pointer are provided, the chip->pdata pointer gets set to an uninitialized value. Looking at the code some more, I found that the sbs_of_populate_pdata function is more complex than necessary and has confusing calling conventions of possibly returning a valid pointer, a NULL pointer or an ERR_PTR pointer (in addition to the uninitialized pointer). To fix all of that, this gets rid of the chip->pdata pointer and simply moves the two integers into the sbs_info structure. This makes it much clearer from reading sbs_probe() what the precedence of the three possible values are (pdata, DT, hardcoded defaults) and completely avoids the #ifdef CONFIG_OF guards as of_property_read_u32() gets replaced with a compile-time stub when that is disabled, and returns an error if passed a NULL of_node pointer. Signed-off-by: Arnd Bergmann Fixes: 3b5dd3a49496 ("power: supply: sbs-battery: Use gpio_desc and sleeping calls for battery detect") --- drivers/power/supply/sbs-battery.c | 98 ++++++++++++-------------------------- 1 file changed, 31 insertions(+), 67 deletions(-) -- 2.9.0 -- To unsubscribe from this list: send the line "unsubscribe linux-pm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/power/supply/sbs-battery.c b/drivers/power/supply/sbs-battery.c index 8b4c6a8681b0..248a5dd75e22 100644 --- a/drivers/power/supply/sbs-battery.c +++ b/drivers/power/supply/sbs-battery.c @@ -169,6 +169,8 @@ struct sbs_info { bool enable_detection; int last_state; int poll_time; + int i2c_retry_count; + int poll_retry_count; struct delayed_work work; int ignore_changes; }; @@ -184,7 +186,7 @@ static int sbs_read_word_data(struct i2c_client *client, u8 address) int retries = 1; if (chip->pdata) - retries = max(chip->pdata->i2c_retry_count + 1, 1); + retries = max(chip->i2c_retry_count + 1, 1); while (retries > 0) { ret = i2c_smbus_read_word_data(client, address); @@ -212,8 +214,8 @@ static int sbs_read_string_data(struct i2c_client *client, u8 address, u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1]; if (chip->pdata) { - retries_length = max(chip->pdata->i2c_retry_count + 1, 1); - retries_block = max(chip->pdata->i2c_retry_count + 1, 1); + retries_length = max(chip->i2c_retry_count + 1, 1); + retries_block = max(chip->i2c_retry_count + 1, 1); } /* Adapter needs to support these two functions */ @@ -279,7 +281,7 @@ static int sbs_write_word_data(struct i2c_client *client, u8 address, int retries = 1; if (chip->pdata) - retries = max(chip->pdata->i2c_retry_count + 1, 1); + retries = max(chip->i2c_retry_count + 1, 1); while (retries > 0) { ret = i2c_smbus_write_word_data(client, address, @@ -741,61 +743,6 @@ static void sbs_delayed_work(struct work_struct *work) } } -#if defined(CONFIG_OF) - -#include -#include - -static const struct of_device_id sbs_dt_ids[] = { - { .compatible = "sbs,sbs-battery" }, - { .compatible = "ti,bq20z75" }, - { } -}; -MODULE_DEVICE_TABLE(of, sbs_dt_ids); - -static struct sbs_platform_data *sbs_of_populate_pdata(struct sbs_info *chip) -{ - struct i2c_client *client = chip->client; - struct device_node *of_node = client->dev.of_node; - struct sbs_platform_data *pdata; - int rc; - u32 prop; - - /* verify this driver matches this device */ - if (!of_node) - return NULL; - - /* first make sure at least one property is set, otherwise - * it won't change behavior from running without pdata. - */ - if (!of_get_property(of_node, "sbs,i2c-retry-count", NULL) && - !of_get_property(of_node, "sbs,poll-retry-count", NULL)) - goto of_out; - - pdata = devm_kzalloc(&client->dev, sizeof(struct sbs_platform_data), - GFP_KERNEL); - if (!pdata) - return ERR_PTR(-ENOMEM); - - rc = of_property_read_u32(of_node, "sbs,i2c-retry-count", &prop); - if (!rc) - pdata->i2c_retry_count = prop; - - rc = of_property_read_u32(of_node, "sbs,poll-retry-count", &prop); - if (!rc) - pdata->poll_retry_count = prop; - -of_out: - return pdata; -} -#else -static struct sbs_platform_data *sbs_of_populate_pdata( - struct sbs_info *client) -{ - return ERR_PTR(-ENOSYS); -} -#endif - static const struct power_supply_desc sbs_default_desc = { .type = POWER_SUPPLY_TYPE_BATTERY, .properties = sbs_properties, @@ -838,13 +785,23 @@ static int sbs_probe(struct i2c_client *client, chip->ignore_changes = 1; chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN; - if (!pdata) - pdata = sbs_of_populate_pdata(chip); - - if (IS_ERR(pdata)) - return PTR_ERR(pdata); - - chip->pdata = pdata; + /* use pdata if available, fall back to DT properties, + * or hardcoded defaults if not + */ + rc = of_property_read_u32(client->dev.of_node, "sbs,i2c-retry-count", + &chip->i2c_retry_count); + if (rc) + chip->i2c_retry_count = 1; + + rc = of_property_read_u32(client->dev.of_node, "sbs,poll-retry-count", + &chip->poll_retry_count); + if (rc) + chip->poll_retry_count = 0; + + if (pdata) { + chip->poll_retry_count = pdata->poll_retry_count; + chip->i2c_retry_count = pdata->i2c_retry_count; + } chip->gpio_detect = devm_gpiod_get_optional(&client->dev, "sbs,battery-detect", GPIOD_IN); @@ -953,13 +910,20 @@ static const struct i2c_device_id sbs_id[] = { }; MODULE_DEVICE_TABLE(i2c, sbs_id); +static const struct of_device_id sbs_dt_ids[] = { + { .compatible = "sbs,sbs-battery" }, + { .compatible = "ti,bq20z75" }, + { } +}; +MODULE_DEVICE_TABLE(of, sbs_dt_ids); + static struct i2c_driver sbs_battery_driver = { .probe = sbs_probe, .remove = sbs_remove, .id_table = sbs_id, .driver = { .name = "sbs-battery", - .of_match_table = of_match_ptr(sbs_dt_ids), + .of_match_table = sbs_dt_ids, .pm = SBS_PM_OPS, }, };