Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp406596yba; Wed, 24 Apr 2019 03:25:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqxLiCr6zauYE3q4RmMmGIYLrn5fDB+ofON34ZyvoNLdn2W5NWnltxY6e3MupEs59Qr2zVYm X-Received: by 2002:aa7:90ca:: with SMTP id k10mr31700344pfk.144.1556101534899; Wed, 24 Apr 2019 03:25:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556101534; cv=none; d=google.com; s=arc-20160816; b=vljOquLbS05Yt3usZ+xE/q2P/kF0276YyN3slBmGVSayZyAZ5+wukHDDEh7tnJQTEO vLfTytl0xhM9Lq9RT8j0FgwGaKvqiMjWW5OAzZe+naVOKqTpVQ4fMoxIkB7csdZwjLUR beUIvsw3bHIgMXZvGAPJNDwW7LkuWDH6r5qNdhEmDS2NfRX0LTwuQbXFtwKshcrfTwJC dYAesTf53GO4YHaPSqqnnWIY7SqrvjF2N9/bON11kBpPdnRXXYJWzbELoeEAfO6LpZV5 l/WPpgbbWDXKmskc5KMgTuq1FfAajx/Zer5LW6B3dwnazD9JwdVmDiBhz2gG+ZbXjXyo nYpg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=1qyx5n5yMrkv5gfN2MwhOkJMoRwpnipFKGrbpD1y6Rg=; b=O4B6i8KJlpp60J0HbMhAoNthlhGgxYUZ8TeIUWwOURP2UQLJ6anmrDYlUkSmPjzMpa l/ccisURO4Ts017ow2naib9dvIocd1DTr8VWQ1EdOv6c7/Vb1hv3tpm71Uxzrxx4ESpE dZWcnW7iIl0I8ZYYiM3ymp/BCMEHia9trBi8oYavJphgT1B8GTw5nznrlFgBotyK185o 8RHAsI3vDW6Q47xjIn1bSQI6lqMdLBrWEB6Drtbx7SX2uZjlevf/KL+fN+U55VAUgdUu bDAMgRRLK9Le5GJeAo1/oBcpWyjj5Hlp4ywzGaabjHpbzo6iF1REM4+w6WalW20monQO +QeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@st.com header.s=STMicroelectronics header.b=1j7bAZKq; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v28si5377731pgk.42.2019.04.24.03.25.19; Wed, 24 Apr 2019 03:25:34 -0700 (PDT) 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=@st.com header.s=STMicroelectronics header.b=1j7bAZKq; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728436AbfDXKUF (ORCPT + 99 others); Wed, 24 Apr 2019 06:20:05 -0400 Received: from mx08-00178001.pphosted.com ([91.207.212.93]:54128 "EHLO mx07-00178001.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726443AbfDXKUD (ORCPT ); Wed, 24 Apr 2019 06:20:03 -0400 Received: from pps.filterd (m0046661.ppops.net [127.0.0.1]) by mx08-00178001.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x3OA6f7K003010; Wed, 24 Apr 2019 12:19:51 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=STMicroelectronics; bh=1qyx5n5yMrkv5gfN2MwhOkJMoRwpnipFKGrbpD1y6Rg=; b=1j7bAZKq4hNou+vSEorHQH8RdwSRNQmiMDJ0lwnIy5Gw3qGncRydV5tDwXJMDbYJlGJu cyPsSTodyG/HbIttpP0lfPNSqCxf5C+wWLnD6KI6/fnGWXyi/XPH/+xiLaP6cosZ0Djl FIkUu3sjUgYAbP9WiftplJeJwtLy/SHOXzsWc3W1JMZwYmbCtWy0G3Rh4phRpg9k2t+b sKkpV65/0EXLJk/X842+l7EbH/6vxG2bl1pl8nqjZ4WObLNPKIUfKQob+EqmJlmQ84FM sVTIroyGQ+4486lDSmsdQMwFmoVf7TQ94dwYhIutCcEjoM7xYm2/B7Gh+mvwFPGFjbWT yA== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx08-00178001.pphosted.com with ESMTP id 2rytad5uaw-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Wed, 24 Apr 2019 12:19:51 +0200 Received: from zeta.dmz-eu.st.com (zeta.dmz-eu.st.com [164.129.230.9]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 090CB3D; Wed, 24 Apr 2019 10:19:32 +0000 (GMT) Received: from Webmail-eu.st.com (Safex1hubcas24.st.com [10.75.90.94]) by zeta.dmz-eu.st.com (STMicroelectronics) with ESMTP id A978D1656; Wed, 24 Apr 2019 10:19:32 +0000 (GMT) Received: from SAFEX1HUBCAS22.st.com (10.75.90.93) by Safex1hubcas24.st.com (10.75.90.94) with Microsoft SMTP Server (TLS) id 14.3.439.0; Wed, 24 Apr 2019 12:19:32 +0200 Received: from localhost (10.201.20.122) by Webmail-ga.st.com (10.75.90.48) with Microsoft SMTP Server (TLS) id 14.3.361.1; Wed, 24 Apr 2019 12:19:30 +0200 From: Benjamin Gaignard To: , , , , , , , , , CC: , , , , , Benjamin Gaignard Subject: [PATCH 1/5] of/device: Add of_ functions for device_link_{add,remove} Date: Wed, 24 Apr 2019 12:19:09 +0200 Message-ID: <20190424101913.1534-2-benjamin.gaignard@st.com> X-Mailer: git-send-email 2.15.0 In-Reply-To: <20190424101913.1534-1-benjamin.gaignard@st.com> References: <20190424101913.1534-1-benjamin.gaignard@st.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.201.20.122] X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-04-24_07:,, signatures=0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Allows to create and remove links between consumer and suppliers from device-tree data. Use 'links-add' property from consumer node to setup a link with a list of suppliers. Consumers will be suspend before their suppliers and resume after them. Add devm_of_device_links_add() to automatically remove the links when the device is unbound from the bus. Signed-off-by: Benjamin Gaignard --- drivers/of/device.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/of_device.h | 20 +++++++++ 2 files changed, 123 insertions(+) diff --git a/drivers/of/device.c b/drivers/of/device.c index 3717f2a20d0d..011ba9bf7642 100644 --- a/drivers/of/device.c +++ b/drivers/of/device.c @@ -336,3 +336,106 @@ int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env) return 0; } EXPORT_SYMBOL_GPL(of_device_uevent_modalias); + +/** + * of_device_links_add - Create links between consumer and suppliers from + * device tree data + * + * @consumer: consumer device + * + * Returns 0 on success, < 0 on failure. + */ +int of_device_links_add(struct device *consumer) +{ + struct device_node *np; + struct platform_device *pdev; + int i = 0; + + np = of_parse_phandle(consumer->of_node, "links-add", i++); + while (np) { + pdev = of_find_device_by_node(np); + of_node_put(np); + if (!pdev) + return -EINVAL; + + device_link_add(consumer, &pdev->dev, DL_FLAG_STATELESS); + platform_device_put(pdev); + + np = of_parse_phandle(consumer->of_node, "links-add", i++); + } + + return 0; +} +EXPORT_SYMBOL_GPL(of_device_links_add); + +/** + * of_device_links_remove - Remove links between consumer and suppliers from + * device tree data + * + * @consumer: consumer device + * + * Returns 0 on success, < 0 on failure. + */ +int of_device_links_remove(struct device *consumer) +{ + struct device_node *np; + struct platform_device *pdev; + int i = 0; + + np = of_parse_phandle(consumer->of_node, "links-add", i++); + while (np) { + pdev = of_find_device_by_node(np); + of_node_put(np); + if (!pdev) + return -EINVAL; + + device_link_remove(consumer, &pdev->dev); + platform_device_put(pdev); + + np = of_parse_phandle(consumer->of_node, "links-add", i++); + } + + return 0; +} +EXPORT_SYMBOL_GPL(of_device_links_remove); + +static void devm_of_device_links_remove(struct device *dev, void *res) +{ + of_device_links_remove(*(struct device **)res); +} + +/** + * devm_of_device_links_add - Create links between consumer and suppliers + * from device tree data + * + * @consumer: consumer device + * + * Returns 0 on success, < 0 on failure. + * + * Similar to of_device_links_add(), but will automatically call + * of_device_links_remove() when the device is unbound from the bus. + */ +int devm_of_device_links_add(struct device *consumer) +{ + struct device **ptr; + int ret; + + if (!consumer) + return -EINVAL; + + ptr = devres_alloc(devm_of_device_links_remove, + sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return -ENOMEM; + + ret = of_device_links_add(consumer); + if (ret < 0) { + devres_free(ptr); + } else { + *ptr = consumer; + devres_add(consumer, ptr); + } + + return ret; +} +EXPORT_SYMBOL_GPL(devm_of_device_links_add); diff --git a/include/linux/of_device.h b/include/linux/of_device.h index 8d31e39dd564..ad01db6828e8 100644 --- a/include/linux/of_device.h +++ b/include/linux/of_device.h @@ -41,6 +41,11 @@ extern int of_device_request_module(struct device *dev); extern void of_device_uevent(struct device *dev, struct kobj_uevent_env *env); extern int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env); + +extern int of_device_links_add(struct device *consumer); +extern int of_device_links_remove(struct device *consumer); +extern int devm_of_device_links_add(struct device *consumer); + static inline void of_device_node_put(struct device *dev) { of_node_put(dev->of_node); @@ -91,6 +96,21 @@ static inline int of_device_uevent_modalias(struct device *dev, return -ENODEV; } +static int of_device_links_add(struct device *consumer) +{ + return 0; +} + +static int of_device_links_remove(struct device *consumer) +{ + return 0; +} + +static int devm_of_device_links_add(struct device *consumer) +{ + return 0; +} + static inline void of_device_node_put(struct device *dev) { } static inline const struct of_device_id *__of_match_device( -- 2.15.0