From patchwork Fri Jun 17 13:42:21 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laxman Dewangan X-Patchwork-Id: 637038 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3rWMGW4PSBz9t1r for ; Fri, 17 Jun 2016 23:55:47 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755194AbcFQNzq (ORCPT ); Fri, 17 Jun 2016 09:55:46 -0400 Received: from nat-hk.nvidia.com ([203.18.50.4]:33560 "EHLO hkmmgate101.nvidia.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751309AbcFQNzp (ORCPT ); Fri, 17 Jun 2016 09:55:45 -0400 Received: from hkpgpgate101.nvidia.com (Not Verified[10.18.92.9]) by hkmmgate101.nvidia.com id ; Fri, 17 Jun 2016 21:55:45 +0800 Received: from HKMAIL102.nvidia.com ([10.18.67.137]) by hkpgpgate101.nvidia.com (PGP Universal service); Fri, 17 Jun 2016 06:55:42 -0700 X-PGP-Universal: processed; by hkpgpgate101.nvidia.com on Fri, 17 Jun 2016 06:55:42 -0700 Received: from DRBGMAIL102.nvidia.com (10.18.16.21) by HKMAIL102.nvidia.com (10.18.16.11) with Microsoft SMTP Server (TLS) id 15.0.1130.7; Fri, 17 Jun 2016 13:55:39 +0000 Received: from HQMAIL105.nvidia.com (172.20.187.12) by DRBGMAIL102.nvidia.com (10.18.16.21) with Microsoft SMTP Server (TLS) id 15.0.1130.7; Fri, 17 Jun 2016 13:55:37 +0000 Received: from ldewanganubuntu-System-Product-Name.nvidia.com (172.20.13.39) by HQMAIL105.nvidia.com (172.20.187.12) with Microsoft SMTP Server id 15.0.1130.7 via Frontend Transport; Fri, 17 Jun 2016 13:55:35 +0000 From: Laxman Dewangan To: , CC: , , , Laxman Dewangan Subject: [PATCH V3 1/2] i2c: core: add devm apis for i2c_new_dummy() Date: Fri, 17 Jun 2016 19:12:21 +0530 Message-ID: <1466170942-21214-1-git-send-email-ldewangan@nvidia.com> X-Mailer: git-send-email 2.1.4 MIME-Version: 1.0 Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org Add resource managed version of i2c_new_dummy(). This helps in: 1. reducing the code size in error path to explicitly unregister the new dummy client. 2. Sometime, need of the remove callback on client driver to just unregister the new i2c dummy client. Signed-off-by: Laxman Dewangan --- Changes from V1: - Got review comment on similar change for pinctrl and taken care of the same on this patch as V1 has similar issue. - Add the new devm_ APIs in the Documentation/driver-model/devres.txt - Will push the change for using this new APIs later once this is applied as most of consumer for these APIs are in other subsystem like MFD/RTC. Changes from V2: - Drop devm_i2c_unregister_device() as it is not used in general. --- drivers/i2c/i2c-core.c | 41 +++++++++++++++++++++++++++++++++++++++++ include/linux/i2c.h | 4 ++++ 2 files changed, 45 insertions(+) diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c index 952d2f0..dc30b9b 100644 --- a/drivers/i2c/i2c-core.c +++ b/drivers/i2c/i2c-core.c @@ -1145,6 +1145,47 @@ struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address) } EXPORT_SYMBOL_GPL(i2c_new_dummy); +static void devm_i2c_dummy_release(struct device *dev, void *res) +{ + i2c_unregister_device(*(struct i2c_client **)res); +} + +/** + * devm_i2c_new_dummy - Resource managed version of i2c_new_dummy(). + * @dev: Device handle for which this resouce belongs to. + * @adapter: the adapter managing the device + * @address: seven bit address to be used + * Context: can sleep + + * Please refer the details of i2c_new_dummy() for more information. + * + * This returns the new i2c client on success; or NULL to indicate an error. + * The new i2c_client will be automatically released when the device is unbound. + */ +struct i2c_client *devm_i2c_new_dummy(struct device *dev, + struct i2c_adapter *adapter, + u16 address) +{ + struct i2c_client **ptr, *i2c_dummy; + + ptr = devres_alloc(devm_i2c_dummy_release, sizeof(*ptr), + GFP_KERNEL); + if (!ptr) + return NULL; + + i2c_dummy = i2c_new_dummy(adapter, address); + if (!i2c_dummy) { + devres_free(ptr); + return NULL; + } + + *ptr = i2c_dummy; + devres_add(dev, ptr); + + return i2c_dummy; +} +EXPORT_SYMBOL_GPL(devm_i2c_new_dummy); + /** * 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 6df7bad..257b239 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h @@ -355,6 +355,10 @@ i2c_new_secondary_device(struct i2c_client *client, u16 default_addr); extern void i2c_unregister_device(struct i2c_client *); + +extern struct i2c_client * +devm_i2c_new_dummy(struct device *dev, struct i2c_adapter *adap, u16 address); + #endif /* I2C */ /* Mainboard arch_initcall() code should register all its I2C devices.