From patchwork Wed Mar 13 16:55:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfram Sang X-Patchwork-Id: 1056157 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-i2c-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=sang-engineering.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 44KHzk3yYSz9s71 for ; Thu, 14 Mar 2019 03:56:18 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726881AbfCMQ4P (ORCPT ); Wed, 13 Mar 2019 12:56:15 -0400 Received: from sauhun.de ([88.99.104.3]:49254 "EHLO pokefinder.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725992AbfCMQ4O (ORCPT ); Wed, 13 Mar 2019 12:56:14 -0400 Received: from localhost (p54B33543.dip0.t-ipconnect.de [84.179.53.67]) by pokefinder.org (Postfix) with ESMTPSA id BA8AE3E4305; Wed, 13 Mar 2019 17:56:12 +0100 (CET) From: Wolfram Sang To: linux-i2c@vger.kernel.org Cc: linux-renesas-soc@vger.kernel.org, Heiner Kallweit , Bartosz Golaszewski , Kieran Bingham , Wolfram Sang Subject: [PATCH v7 1/3] i2c: core: improve return value handling of i2c_new_device and i2c_new_dummy Date: Wed, 13 Mar 2019 17:55:24 +0100 Message-Id: <20190313165526.28588-2-wsa+renesas@sang-engineering.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190313165526.28588-1-wsa+renesas@sang-engineering.com> References: <20190313165526.28588-1-wsa+renesas@sang-engineering.com> Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org From: Heiner Kallweit Currently i2c_new_device and i2c_new_dummy return just NULL in error case although they have more error details internally. Therefore move the functionality into new functions returning detailed errors and add wrappers for compatibility with the current API. This allows to use these functions with detailed error codes within the i2c core or for API extensions. Signed-off-by: Heiner Kallweit [wsa: rename new functions to 'errptr' style and fix minor kdoc issues] Signed-off-by: Wolfram Sang Reviewed-by: Simon Horman Reviewed-by: Kieran Bingham --- drivers/i2c/i2c-core-base.c | 70 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 13 deletions(-) diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index cb6c5cb0df0b..bbca2e8bb019 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -710,7 +710,7 @@ static int i2c_dev_irq_from_resources(const struct resource *resources, } /** - * i2c_new_device - instantiate an i2c device + * i2c_new_device_errptr - instantiate an i2c device * @adap: the adapter managing the device * @info: describes one I2C device; bus_num is ignored * Context: can sleep @@ -723,17 +723,17 @@ static int i2c_dev_irq_from_resources(const struct resource *resources, * before any i2c_adapter could exist. * * This returns the new i2c client, which may be saved for later use with - * i2c_unregister_device(); or NULL to indicate an error. + * i2c_unregister_device(); or an ERR_PTR to describe the error. */ -struct i2c_client * -i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info) +static struct i2c_client * +i2c_new_device_errptr(struct i2c_adapter *adap, struct i2c_board_info const *info) { struct i2c_client *client; int status; client = kzalloc(sizeof *client, GFP_KERNEL); if (!client) - return NULL; + return ERR_PTR(-ENOMEM); client->adapter = adap; @@ -799,7 +799,29 @@ i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info) client->name, client->addr, status); out_err_silent: kfree(client); - return NULL; + return ERR_PTR(status); +} + +/** + * i2c_new_device - instantiate an i2c device + * @adap: the adapter managing the device + * @info: describes one I2C device; bus_num is ignored + * Context: can sleep + * + * This function has the same functionality like i2c_new_device_errptr, it just + * returns NULL instead of an ERR_PTR in case of an error for compatibility + * with current I2C API. + * + * This returns the new i2c client, which may be saved for later use with + * i2c_unregister_device(); or NULL to indicate an error. + */ +struct i2c_client * +i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info) +{ + struct i2c_client *ret; + + ret = i2c_new_device_errptr(adap, info); + return IS_ERR(ret) ? NULL : ret; } EXPORT_SYMBOL_GPL(i2c_new_device); @@ -850,7 +872,7 @@ static struct i2c_driver dummy_driver = { }; /** - * i2c_new_dummy - return a new i2c device bound to a dummy driver + * i2c_new_dummy_errptr - return a new i2c device bound to a dummy driver * @adapter: the adapter managing the device * @address: seven bit address to be used * Context: can sleep @@ -865,15 +887,37 @@ static struct i2c_driver dummy_driver = { * different driver. * * This returns the new i2c client, which should be saved for later use with - * i2c_unregister_device(); or NULL to indicate an error. + * i2c_unregister_device(); or an ERR_PTR to describe the error. */ -struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address) +static struct i2c_client * +i2c_new_dummy_errptr(struct i2c_adapter *adapter, u16 address) { struct i2c_board_info info = { I2C_BOARD_INFO("dummy", address), }; - return i2c_new_device(adapter, &info); + return i2c_new_device_errptr(adapter, &info); +} + +/** + * i2c_new_dummy - return a new i2c device bound to a dummy driver + * @adapter: the adapter managing the device + * @address: seven bit address to be used + * Context: can sleep + * + * This function has the same functionality like i2c_new_dummy_errptr, it just + * returns NULL instead of an ERR_PTR in case of an error for compatibility + * with current I2C API. + * + * This returns the new i2c client, which should be saved for later use with + * i2c_unregister_device(); or NULL to indicate an error. + */ +struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address) +{ + struct i2c_client *ret; + + ret = i2c_new_dummy_errptr(adapter, address); + return IS_ERR(ret) ? NULL : ret; } EXPORT_SYMBOL_GPL(i2c_new_dummy); @@ -996,9 +1040,9 @@ i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr, info.flags |= I2C_CLIENT_SLAVE; } - client = i2c_new_device(adap, &info); - if (!client) - return -EINVAL; + client = i2c_new_device_errptr(adap, &info); + if (IS_ERR(client)) + return PTR_ERR(client); /* Keep track of the added device */ mutex_lock(&adap->userspace_clients_lock); From patchwork Wed Mar 13 16:55:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfram Sang X-Patchwork-Id: 1056159 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-i2c-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=sang-engineering.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 44KHzm0qBHz9s4Y for ; Thu, 14 Mar 2019 03:56:20 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726883AbfCMQ4P (ORCPT ); Wed, 13 Mar 2019 12:56:15 -0400 Received: from sauhun.de ([88.99.104.3]:49268 "EHLO pokefinder.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726842AbfCMQ4P (ORCPT ); Wed, 13 Mar 2019 12:56:15 -0400 Received: from localhost (p54B33543.dip0.t-ipconnect.de [84.179.53.67]) by pokefinder.org (Postfix) with ESMTPSA id 47C523E4306; Wed, 13 Mar 2019 17:56:13 +0100 (CET) From: Wolfram Sang To: linux-i2c@vger.kernel.org Cc: linux-renesas-soc@vger.kernel.org, Heiner Kallweit , Bartosz Golaszewski , Kieran Bingham , Wolfram Sang Subject: [PATCH v7 2/3] i2c: core: add device-managed version of i2c_new_dummy Date: Wed, 13 Mar 2019 17:55:25 +0100 Message-Id: <20190313165526.28588-3-wsa+renesas@sang-engineering.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190313165526.28588-1-wsa+renesas@sang-engineering.com> References: <20190313165526.28588-1-wsa+renesas@sang-engineering.com> Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org From: Heiner Kallweit i2c_new_dummy is typically called from the probe function of the driver for the primary i2c client. It requires calls to i2c_unregister_device in the error path of the probe function and in the remove function. This can be simplified by introducing a device-managed version. Note the changed error case return value type: i2c_new_dummy returns NULL whilst devm_new_i2c_dummy returns an ERR_PTR. Signed-off-by: Heiner Kallweit [wsa: rename new functions to 'errptr' style and fix minor kdoc issues] Signed-off-by: Wolfram Sang Reviewed-by: Simon Horman --- Documentation/driver-model/devres.txt | 3 +++ drivers/i2c/i2c-core-base.c | 44 +++++++++++++++++++++++++++++++++++ include/linux/i2c.h | 3 +++ 3 files changed, 50 insertions(+) diff --git a/Documentation/driver-model/devres.txt b/Documentation/driver-model/devres.txt index b277cafce71e..e36dc8041857 100644 --- a/Documentation/driver-model/devres.txt +++ b/Documentation/driver-model/devres.txt @@ -266,6 +266,9 @@ GPIO devm_gpio_request_one() devm_gpio_free() +I2C + devm_i2c_new_dummy_errptr() + IIO devm_iio_device_alloc() devm_iio_device_free() diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index bbca2e8bb019..7f20c6b8857f 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -921,6 +921,50 @@ struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address) } EXPORT_SYMBOL_GPL(i2c_new_dummy); +struct i2c_dummy_devres { + struct i2c_client *client; +}; + +static void devm_i2c_release_dummy(struct device *dev, void *res) +{ + struct i2c_dummy_devres *this = res; + + i2c_unregister_device(this->client); +} + +/** + * devm_i2c_new_dummy_errptr - return a new i2c device bound to a dummy driver + * @dev: device the managed resource is bound to + * @adapter: the adapter managing the device + * @address: seven bit address to be used + * Context: can sleep + * + * This is the device-managed version of @i2c_new_dummy. It returns the new i2c + * client or an ERR_PTR in case of an error. + */ +struct i2c_client *devm_i2c_new_dummy_errptr(struct device *dev, + struct i2c_adapter *adapter, + u16 address) +{ + struct i2c_dummy_devres *dr; + struct i2c_client *client; + + dr = devres_alloc(devm_i2c_release_dummy, sizeof(*dr), GFP_KERNEL); + if (!dr) + return ERR_PTR(-ENOMEM); + + client = i2c_new_dummy_errptr(adapter, address); + if (IS_ERR(client)) { + devres_free(dr); + } else { + dr->client = client; + devres_add(dev, dr); + } + + return client; +} +EXPORT_SYMBOL_GPL(devm_i2c_new_dummy_errptr); + /** * i2c_new_secondary_device - Helper to get the instantiated secondary address * and create the associated device diff --git a/include/linux/i2c.h b/include/linux/i2c.h index 383510b4f083..93075c2ad9dc 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h @@ -470,6 +470,9 @@ extern struct i2c_client * i2c_new_dummy(struct i2c_adapter *adap, u16 address); extern struct i2c_client * +devm_i2c_new_dummy_errptr(struct device *dev, struct i2c_adapter *adap, u16 address); + +extern struct i2c_client * i2c_new_secondary_device(struct i2c_client *client, const char *name, u16 default_addr); From patchwork Wed Mar 13 16:55:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfram Sang X-Patchwork-Id: 1056158 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-i2c-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=sang-engineering.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 44KHzl2qsXz9s4V for ; Thu, 14 Mar 2019 03:56:19 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726842AbfCMQ4Q (ORCPT ); Wed, 13 Mar 2019 12:56:16 -0400 Received: from sauhun.de ([88.99.104.3]:49278 "EHLO pokefinder.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726876AbfCMQ4P (ORCPT ); Wed, 13 Mar 2019 12:56:15 -0400 Received: from localhost (p54B33543.dip0.t-ipconnect.de [84.179.53.67]) by pokefinder.org (Postfix) with ESMTPSA id C68243E4307; Wed, 13 Mar 2019 17:56:13 +0100 (CET) From: Wolfram Sang To: linux-i2c@vger.kernel.org Cc: linux-renesas-soc@vger.kernel.org, Heiner Kallweit , Bartosz Golaszewski , Kieran Bingham , Wolfram Sang Subject: [PATCH v7 3/3] mfd: da9063: occupy second I2C address, too Date: Wed, 13 Mar 2019 17:55:26 +0100 Message-Id: <20190313165526.28588-4-wsa+renesas@sang-engineering.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190313165526.28588-1-wsa+renesas@sang-engineering.com> References: <20190313165526.28588-1-wsa+renesas@sang-engineering.com> Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org Even though we don't use it yet, we should mark the second I2C address this device is listening to as used. Not yet for upstream until all dependencies are merged! Signed-off-by: Wolfram Sang Reviewed-by: Simon Horman --- drivers/mfd/da9063-i2c.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/mfd/da9063-i2c.c b/drivers/mfd/da9063-i2c.c index 50a24b1921d0..92ae2681d9e4 100644 --- a/drivers/mfd/da9063-i2c.c +++ b/drivers/mfd/da9063-i2c.c @@ -227,6 +227,8 @@ static int da9063_i2c_probe(struct i2c_client *i2c, return ret; } + devm_i2c_new_dummy_errptr(&i2c->dev, i2c->adapter, i2c->addr + 1); + return da9063_device_init(da9063, i2c->irq); }