From patchwork Thu Nov 21 15:27:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Enrico Weigelt, metux IT consult" X-Patchwork-Id: 1198997 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) 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=metux.net Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47Jk2c3qkNz9sPK for ; Fri, 22 Nov 2019 02:27:36 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726563AbfKUP1f (ORCPT ); Thu, 21 Nov 2019 10:27:35 -0500 Received: from mout.kundenserver.de ([212.227.17.10]:41955 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726546AbfKUP1f (ORCPT ); Thu, 21 Nov 2019 10:27:35 -0500 Received: from orion.localdomain ([95.115.120.75]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MJW18-1iITLJ3m6i-00JrJT; Thu, 21 Nov 2019 16:27:34 +0100 From: "Enrico Weigelt, metux IT consult" To: linux-kernel@vger.kernel.org Cc: wsa@the-dreams.de, linux-i2c@vger.kernel.org Subject: [PATCH] drivers: i2c: introduce devm_i2c_add_adapter() Date: Thu, 21 Nov 2019 16:27:16 +0100 Message-Id: <20191121152716.23940-1-info@metux.net> X-Mailer: git-send-email 2.11.0 X-Provags-ID: V03:K1:ET/rQWaaj+bqgKvzzHm7j0cGhbypk2slAUvAcx1PA1h6UOx75CS 83ugTIkteaXVtIZ8MkFAQqji9tgVzfKMzE3jeJei1t5nUyPoS9evulJSKjo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3fok2Ntg== Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org Introduce devm'ed version of i2c_add_adapter() which triggers i2c_del_adapter() to be called when parent device is removed. Signed-off-by: Enrico Weigelt, metux IT consult --- drivers/i2c/i2c-core-base.c | 38 ++++++++++++++++++++++++++++++++++++++ include/linux/i2c.h | 2 ++ 2 files changed, 40 insertions(+) diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index 5f6a4985f2bc..bd713e451633 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -1452,6 +1452,44 @@ int i2c_add_adapter(struct i2c_adapter *adapter) } EXPORT_SYMBOL(i2c_add_adapter); +static void devm_i2c_adapter_release(struct device *dev, void *res) +{ + i2c_del_adapter(*(struct i2c_adapter**)res); +} + +/** + * devm_i2c_add_adapter - devm version of i2c_add_adapter() + * @adapter: the adapter to add + * @data: private data to attach (if non-NULL) + * Context: can sleep + */ +int devm_i2c_add_adapter(struct device *parent, + struct i2c_adapter *adap, + void *data) +{ + struct i2c_adapter **dr; + int rc; + + dr = devres_alloc(devm_i2c_adapter_release, sizeof(*dr), GFP_KERNEL); + if (!dr) + return -ENOMEM; + + rc = i2c_add_adapter(adap); + if (rc) { + devres_free(dr); + return rc; + } + + if (data) + i2c_set_adapdata(adap, data); + + *dr = adap; + devres_add(parent, dr); + + return 0; +} +EXPORT_SYMBOL_GPL(devm_i2c_add_adapter); + /** * i2c_add_numbered_adapter - declare i2c adapter, use static bus number * @adap: the adapter to register (with adap->nr initialized) diff --git a/include/linux/i2c.h b/include/linux/i2c.h index 1361637c369d..8dc07d03a502 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h @@ -834,6 +834,8 @@ static inline void i2c_mark_adapter_resumed(struct i2c_adapter *adap) */ #if IS_ENABLED(CONFIG_I2C) extern int i2c_add_adapter(struct i2c_adapter *adap); +extern int devm_i2c_add_adapter(struct device *parent, + struct i2c_adapter *adap, void *data); extern void i2c_del_adapter(struct i2c_adapter *adap); extern int i2c_add_numbered_adapter(struct i2c_adapter *adap);