From patchwork Fri Feb 24 16:14:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 94490 Delivered-To: patch@linaro.org Received: by 10.140.20.99 with SMTP id 90csp758587qgi; Fri, 24 Feb 2017 08:15:15 -0800 (PST) X-Received: by 10.99.167.74 with SMTP id w10mr4526709pgo.2.1487952914878; Fri, 24 Feb 2017 08:15:14 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b77si7347053pfj.36.2017.02.24.08.15.14; Fri, 24 Feb 2017 08:15:14 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751629AbdBXQPN (ORCPT + 25 others); Fri, 24 Feb 2017 11:15:13 -0500 Received: from mail-wm0-f43.google.com ([74.125.82.43]:35252 "EHLO mail-wm0-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751486AbdBXQOs (ORCPT ); Fri, 24 Feb 2017 11:14:48 -0500 Received: by mail-wm0-f43.google.com with SMTP id v186so18293039wmd.0 for ; Fri, 24 Feb 2017 08:14:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=uVdOJeWN3Ms3SfAAF6ZcxAxkIHdaNts+Eq8V9aA2ETs=; b=DQB8qKKfuA0wF1sifsyjKc9QsJMN2vn7hv1MPH6InToGX7ODVzXobP42oCeJk9DfSS v51Kqup/mIiUGrNR6kgTmWwu/g1he3P9q9QgkRLFP8IA8cFv/ZbWhTc42Fb+cw8va5ph PCoTGTviDeNJnpMBTxIkp4wzQ/FCsrjSV32ys= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=uVdOJeWN3Ms3SfAAF6ZcxAxkIHdaNts+Eq8V9aA2ETs=; b=pchYbuW7a0BEBuN0dWES2+VWl/GtEaiEWNtbzlejeZuUkDEHSnuU53nTW3e3Xng10u h5tsnpmsUd0MxcySjw5JAh1oDDfOMq7+gke6OH7ybBXzG3i1Zmy5RNDxVD+87tX77wup cF4CkGAAAx+Kbk/r0sIaiZgslb7o+qOFLjPKeuC8aReo8+MScse9KYEpPFvc/J/MMFiH 3eWwIpxOua012tN4UoZvwH7w86pG3L7madfOH/bimt4tB7JgxeI0HlcgeiHml63LStSU hFCikZ7A3VdZIhQD0DcsxxwsyuvgJ69agKlqd8H4vC+hJqqRULubPLOKvgPOPH0ggyzR q3dQ== X-Gm-Message-State: AMke39mqOW5xUpxsN3vh5mhCFXYeCQHjnxS7LDQILNUDUQvSvD4/WSfE/jnqfgQA53EiT2nj X-Received: by 10.28.197.142 with SMTP id v136mr3431787wmf.62.1487952886555; Fri, 24 Feb 2017 08:14:46 -0800 (PST) Received: from lmenx321.st.com. ([80.215.231.24]) by smtp.gmail.com with ESMTPSA id 10sm2871338wmk.26.2017.02.24.08.14.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 24 Feb 2017 08:14:46 -0800 (PST) From: Benjamin Gaignard To: robh+dt@kernel.org, frowand.list@gmail.com, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, vincent.abriou@st.com, dri-devel@lists.freedesktop.org Cc: linaro-kernel@lists.linaro.org, Benjamin Gaignard Subject: [PATCH v2 1/2] of: add devm_ functions for populate and depopulate Date: Fri, 24 Feb 2017 17:14:33 +0100 Message-Id: <1487952874-23635-2-git-send-email-benjamin.gaignard@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1487952874-23635-1-git-send-email-benjamin.gaignard@linaro.org> References: <1487952874-23635-1-git-send-email-benjamin.gaignard@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Lots of calls to of_platform_populate() are not unbalanced by a call to of_platform_depopulate(). This create issues while drivers are bind/unbind. In way to solve those issues is to add devm_of_platform_populate() which will call of_platform_depopulate() when the device is unbound from the bus. Signed-off-by: Benjamin Gaignard --- version 2: - simplify function prototype to only keep device as parameter drivers/of/platform.c | 71 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/of_platform.h | 11 +++++++ 2 files changed, 82 insertions(+) -- 1.9.1 diff --git a/drivers/of/platform.c b/drivers/of/platform.c index b8064bc..f5bbb50 100644 --- a/drivers/of/platform.c +++ b/drivers/of/platform.c @@ -571,6 +571,77 @@ void of_platform_depopulate(struct device *parent) } EXPORT_SYMBOL_GPL(of_platform_depopulate); +static void devm_of_platform_populate_release(struct device *dev, void *res) +{ + of_platform_depopulate(*(struct device **)res); +} + +/** + * devm_of_platform_populate() - Populate platform_devices from device tree data + * @dev: device that requested to populate from device tree data + * + * Similar to of_platform_populate(), but will automatically call + * of_platform_depopulate() when the device is unbound from the bus. + * + * Returns 0 on success, < 0 on failure. + */ +int devm_of_platform_populate(struct device *dev) +{ + struct device **ptr; + int ret; + + if (!dev) + return -EINVAL; + + ptr = devres_alloc(devm_of_platform_populate_release, + sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return -ENOMEM; + + ret = of_platform_populate(dev->of_node, NULL, NULL, dev); + if (ret) { + devres_free(ptr); + } else { + *ptr = dev; + devres_add(dev, ptr); + } + + return ret; +} +EXPORT_SYMBOL_GPL(devm_of_platform_populate); + +static int devm_of_platform_match(struct device *dev, void *res, void *data) +{ + struct device **ptr = res; + + if (!ptr) { + WARN_ON(!ptr); + return 0; + } + + return *ptr == data; +} + +/** + * devm_of_platform_depopulate() - Remove devices populated from device tree + * @dev: device that requested to depopulate from device tree data + * + * Complementary to devm_of_platform_populate(), this function removes children + * of the given device (and, recurrently, their children) that have been + * created from their respective device tree nodes (and only those, + * leaving others - eg. manually created - unharmed). + */ +void devm_of_platform_depopulate(struct device *dev) +{ + int ret; + + ret = devres_release(dev, devm_of_platform_populate_release, + devm_of_platform_match, dev); + + WARN_ON(ret); +} +EXPORT_SYMBOL_GPL(devm_of_platform_depopulate); + #ifdef CONFIG_OF_DYNAMIC static int of_platform_notify(struct notifier_block *nb, unsigned long action, void *arg) diff --git a/include/linux/of_platform.h b/include/linux/of_platform.h index 956a100..dc8224a 100644 --- a/include/linux/of_platform.h +++ b/include/linux/of_platform.h @@ -76,6 +76,10 @@ extern int of_platform_default_populate(struct device_node *root, const struct of_dev_auxdata *lookup, struct device *parent); extern void of_platform_depopulate(struct device *parent); + +extern int devm_of_platform_populate(struct device *dev); + +extern void devm_of_platform_depopulate(struct device *dev); #else static inline int of_platform_populate(struct device_node *root, const struct of_device_id *matches, @@ -91,6 +95,13 @@ static inline int of_platform_default_populate(struct device_node *root, return -ENODEV; } static inline void of_platform_depopulate(struct device *parent) { } + +static inline int devm_of_platform_populate(struct device *dev) +{ + return -ENODEV; +} + +static inline void devm_of_platform_depopulate(struct device *dev) { } #endif #if defined(CONFIG_OF_DYNAMIC) && defined(CONFIG_OF_ADDRESS)