From patchwork Wed Dec 13 19:11:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heiner Kallweit X-Patchwork-Id: 848187 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; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="NJZpYKd8"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3yxmXg31Kpz9s4q for ; Thu, 14 Dec 2017 06:12:19 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753724AbdLMTLv (ORCPT ); Wed, 13 Dec 2017 14:11:51 -0500 Received: from mail-wm0-f66.google.com ([74.125.82.66]:34786 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753144AbdLMTLt (ORCPT ); Wed, 13 Dec 2017 14:11:49 -0500 Received: by mail-wm0-f66.google.com with SMTP id y82so23099959wmg.1; Wed, 13 Dec 2017 11:11:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:subject:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=e33iXdyS7ix5AENh0zTTznXdi1nQnuB1jkiaUu2YCy0=; b=NJZpYKd89J5SPVqMg4ltqdtOodkezIS439ziDmGTh3uxMFdWVGjixFZX9sOA22jE7d xya127UR2u7VbGWly20SKyeFZg0+QiZij9+xPUS19z3O+7SvFGtrJ6TyGzXC5iu5p84o k9qLg91OEuxI5TgaZPu5M9SLRwKQshXcxQHFrP18063KABDXq6Gp49ljuDHSPtc5rasx AQya14TJawlQPHC4SRNNAKD7qoW8xAgs4bV9yBliA1jyojSINcw7xkRIr2m38NtKpiln EGQbVkXlozFHRX0zOBDJZdr9qWXy3d0Hb6MgL17cA7PlZVXDmxwA4YgqHoojjoNyPpVy OIMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=e33iXdyS7ix5AENh0zTTznXdi1nQnuB1jkiaUu2YCy0=; b=O5Ae5CECdZ089UnWBI03Hjx/tDeY9pALpWQtpmU+pfAf40bHU09j78uTozte4lCvR2 uDqZy0Pvr2Q2JvuZvZPoaxneaJoAoNBM3jUPkx+I5g9dnwwtkDPn4VAICV2FKSp9cx0M FBpulFNAGCHRRLDG6qci5JJcTIWDSocXsFZxQ9LuYHHOf1MJvwcq5DVrJCGhmsm1noLX 0ZUTLNe8EGfEwsVp1He5GXNsOhOIdWMXKj6SMfv0dgeU94qYMOLKB24Pyht2HgnN+B5m 0LmLljQf5Y5fT4c/UEwdvRt2Tdk3IsG5foIxCk4r8khGy5pfxmm+S51qBhvserd6yIN+ a92w== X-Gm-Message-State: AKGB3mJz63t/iLxDxe+Z5vtjTThK6ZWe7kBLp3EosheAViRMN67sQYR7 I9pPyFq2tDtJSeFEP+RwtHbUeA== X-Google-Smtp-Source: ACJfBoulCRT8rNtb2gT1HHDhGjVrMnYuKVI7mjfpoGQqk7ma27uBqsw9vwJqEuguSp9NNLWATK8ICA== X-Received: by 10.80.184.83 with SMTP id k19mr9283840ede.190.1513192307636; Wed, 13 Dec 2017 11:11:47 -0800 (PST) Received: from ?IPv6:2003:ea:8bc0:e400:d163:20bf:7500:cc98? (p200300EA8BC0E400D16320BF7500CC98.dip0.t-ipconnect.de. [2003:ea:8bc0:e400:d163:20bf:7500:cc98]) by smtp.googlemail.com with ESMTPSA id j39sm2033596ede.38.2017.12.13.11.11.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Dec 2017 11:11:47 -0800 (PST) From: Heiner Kallweit Subject: [PATCH v3 2/3] i2c: core: add device-managed version of i2c_new_dummy To: Bartosz Golaszewski , Wolfram Sang , Peter Rosin Cc: "linux-i2c@vger.kernel.org" , Linux Kernel Mailing List References: <69b03782-cb1a-2b31-c8aa-1827caa66dd5@gmail.com> Message-ID: <9fa02862-2908-b445-ef7f-a587fd6ac666@gmail.com> Date: Wed, 13 Dec 2017 20:11:32 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: <69b03782-cb1a-2b31-c8aa-1827caa66dd5@gmail.com> Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org 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 --- v2: - use new function _i2c_new_dummy with detailed error codes v3: - no changes --- Documentation/driver-model/devres.txt | 3 +++ drivers/i2c/i2c-core-base.c | 38 +++++++++++++++++++++++++++++++++++ include/linux/i2c.h | 3 +++ 3 files changed, 44 insertions(+) diff --git a/Documentation/driver-model/devres.txt b/Documentation/driver-model/devres.txt index c180045eb..6e2bccf85 100644 --- a/Documentation/driver-model/devres.txt +++ b/Documentation/driver-model/devres.txt @@ -259,6 +259,9 @@ GPIO devm_gpio_request_one() devm_gpio_free() +I2C + devm_i2c_new_dummy + 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 3faa9b1d3..7b3cd95be 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -840,6 +840,44 @@ struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address) } EXPORT_SYMBOL_GPL(i2c_new_dummy); +static void devm_i2c_release_dummy(struct device *dev, void *res) +{ + i2c_unregister_device(*(struct i2c_client **)res); +} + +/** + * devm_i2c_new_dummy - 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. + * Note the changed return value type: It returns the new i2c client + * or an ERR_PTR in case of an error. + */ +struct i2c_client *devm_i2c_new_dummy(struct device *dev, + struct i2c_adapter *adapter, + u16 address) +{ + struct i2c_client *ret, **dr; + + dr = devres_alloc(devm_i2c_release_dummy, sizeof(*dr), GFP_KERNEL); + if (!dr) + return ERR_PTR(-ENOMEM); + + ret = _i2c_new_dummy(adapter, address); + if (IS_ERR(ret)) { + devres_free(dr); + } else { + *dr = ret; + devres_add(dev, dr); + } + + return ret; +} +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 5d7f3c185..aca6ebbb8 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h @@ -441,6 +441,9 @@ extern int i2c_probe_func_quick_read(struct i2c_adapter *, unsigned short addr); extern struct i2c_client * i2c_new_dummy(struct i2c_adapter *adap, u16 address); +extern struct i2c_client * +devm_i2c_new_dummy(struct device *dev, struct i2c_adapter *adap, u16 address); + extern struct i2c_client * i2c_new_secondary_device(struct i2c_client *client, const char *name,