Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp6005045ybe; Tue, 10 Sep 2019 12:02:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqxdxi7hn11TEBE89pzomK2pKR5d/X47H2JwFlAwmdnCETW5XKPhSg+emYopPuX9aGAA+HKF X-Received: by 2002:a17:906:b5a:: with SMTP id v26mr26319103ejg.235.1568142178385; Tue, 10 Sep 2019 12:02:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568142178; cv=none; d=google.com; s=arc-20160816; b=EoJ2WPoV3srVkoXsHx/Q2R/U1+oHziW+nwtNJfRrnaJu6dUgIFwzjg8mmgbAw/hq/c BCoMT/LlMBBgTng81NOLX2Rmcl0Hzyl9++kUqg4ny4JtzUpDNO/D2VYZAJk4kgX0ahOD hpRL1FLLqiAK/N/VcU5PoeIjccHguwenw7ddQE1P7TZIzolcWa3xV588H8Jn9f59XgXk 4+BnMcVupjTanKOhLYcaHLP/z7RN902azIfuo1plYwUm4+sqzPscxF2ToHl7vnKeXHMp NbgKN9fQCNk9xYDSSrAElnIx3tknv7LgBrVlkMDn1wD9Pl8HMsn9b3C2CdnzH9x9WVb2 qQow== 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=d5veEZ1PcXAieWongpOg9OtCDdccu5gYAuMOT2iHat8=; b=K0bGXrcSHd+nqh1cjXjPO5shjENdlg8XltT6r8DdGvMPXEob9tgIDinYE5k0cMv7w1 XfLiU/4sK47sveTVBSTBVpYmNNc27KRKGMlcFGCTVTVSYzpQaxZrqMU27ODP1JmfjI9X FjgZX5TATyz1sTRwNzlAQHsxFOGQr7jmPw2PgnRB/BVvmQ2UFV38N/p3W8xn+2bqNIM3 sXWVDB07oYd1iGH0RqOOK2fPaTkXHYVbsF7q5rrZWds2T+zF9tZN7LZBirTAQXEqGFf8 PqnfoGJvEQbesoYNGh3YUs3xsjB6S4Svm/OY3tGUl2or0s/V3JIGowK839bIQ24CxMEO WaDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=sSXYxdiK; 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=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b27si9588224eje.123.2019.09.10.12.02.33; Tue, 10 Sep 2019 12:02:58 -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=@ti.com header.s=ti-com-17Q1 header.b=sSXYxdiK; 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=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732389AbfIJN1r (ORCPT + 99 others); Tue, 10 Sep 2019 09:27:47 -0400 Received: from lelv0143.ext.ti.com ([198.47.23.248]:53234 "EHLO lelv0143.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728759AbfIJN1r (ORCPT ); Tue, 10 Sep 2019 09:27:47 -0400 Received: from fllv0034.itg.ti.com ([10.64.40.246]) by lelv0143.ext.ti.com (8.15.2/8.15.2) with ESMTP id x8ADRdkm025009; Tue, 10 Sep 2019 08:27:39 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1568122059; bh=d5veEZ1PcXAieWongpOg9OtCDdccu5gYAuMOT2iHat8=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=sSXYxdiKY/JMCbuhm/iUxpx+DxRYN5lYkuEZBUvUalIXXfLYMQGVj5xMNOutvmwFn KL81w1wRhTrKnru18FNfiewYAv9dB5O3Jsf86SQd174PWQHz/5/RESmqoTXq4buxWU +92GuZOmeAeREQO5uR6BTb5pJSanW46oY3f08tWE= Received: from DLEE115.ent.ti.com (dlee115.ent.ti.com [157.170.170.26]) by fllv0034.itg.ti.com (8.15.2/8.15.2) with ESMTPS id x8ADRd6G086640 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 10 Sep 2019 08:27:39 -0500 Received: from DLEE111.ent.ti.com (157.170.170.22) by DLEE115.ent.ti.com (157.170.170.26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Tue, 10 Sep 2019 08:27:39 -0500 Received: from lelv0327.itg.ti.com (10.180.67.183) by DLEE111.ent.ti.com (157.170.170.22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5 via Frontend Transport; Tue, 10 Sep 2019 08:27:39 -0500 Received: from localhost (ileax41-snat.itg.ti.com [10.172.224.153]) by lelv0327.itg.ti.com (8.15.2/8.15.2) with ESMTP id x8ADRcKu048670; Tue, 10 Sep 2019 08:27:38 -0500 From: Jean-Jacques Hiblot To: , , , , , CC: , , , , , Jean-Jacques Hiblot Subject: [PATCH v6 2/4] leds: Add managed API to get a LED from a device driver Date: Tue, 10 Sep 2019 15:27:32 +0200 Message-ID: <20190910132734.9544-3-jjhiblot@ti.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190910132734.9544-1-jjhiblot@ti.com> References: <20190910132734.9544-1-jjhiblot@ti.com> MIME-Version: 1.0 Content-Type: text/plain X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If the LED is acquired by a consumer device with devm_led_get(), it is automatically released when the device is detached. Signed-off-by: Jean-Jacques Hiblot Acked-by: Pavel Machek --- drivers/leds/led-class.c | 42 ++++++++++++++++++++++++++++++++++++++++ include/linux/leds.h | 2 ++ 2 files changed, 44 insertions(+) diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c index d4d22f4b5052..664c81e2513c 100644 --- a/drivers/leds/led-class.c +++ b/drivers/leds/led-class.c @@ -263,6 +263,48 @@ void led_put(struct led_classdev *led_cdev) } EXPORT_SYMBOL_GPL(led_put); +static void devm_led_release(struct device *dev, void *res) +{ + struct led_classdev **p = res; + + led_put(*p); +} + +/** + * devm_led_get - Resource-managed request of a LED device + * @dev: LED consumer + * @idx: index of the LED to obtain in the consumer + * + * The device node of the device is parse to find the request LED device. + * The LED device returned from this function is automatically released + * on driver detach. + * + * @return a pointer to a LED device or ERR_PTR(errno) on failure. + */ +struct led_classdev *__must_check devm_led_get(struct device *dev, + int index) +{ + struct led_classdev *led; + struct led_classdev **dr; + + led = of_led_get(dev->of_node, index); + if (IS_ERR(led)) + return led; + + dr = devres_alloc(devm_led_release, sizeof(struct led_classdev *), + GFP_KERNEL); + if (!dr) { + led_put(led); + return ERR_PTR(-ENOMEM); + } + + *dr = led; + devres_add(dev, dr); + + return led; +} +EXPORT_SYMBOL_GPL(devm_led_get); + static int match_name(struct device *dev, const void *data) { if (!dev_name(dev)) diff --git a/include/linux/leds.h b/include/linux/leds.h index 0a71c7cdd191..7fcec566d774 100644 --- a/include/linux/leds.h +++ b/include/linux/leds.h @@ -148,6 +148,8 @@ extern void led_classdev_resume(struct led_classdev *led_cdev); extern struct led_classdev *of_led_get(struct device_node *np, int index); extern void led_put(struct led_classdev *led_cdev); +struct led_classdev *__must_check devm_led_get(struct device *dev, + int index); /** * led_blink_set - set blinking with software fallback -- 2.17.1