Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp942504ybi; Wed, 17 Jul 2019 07:16:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqwPcuuGR1HBLYRcOGrzkTUgnp+D8xBJjquQhe0yEC2osnrkV5eqSFL3iO+worKCKn9jMfLN X-Received: by 2002:a63:755e:: with SMTP id f30mr41535151pgn.246.1563372970513; Wed, 17 Jul 2019 07:16:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563372970; cv=none; d=google.com; s=arc-20160816; b=BDWw/eZV6LRSORz4t9BBQtiyBNK9x1OVutDGhE+lFELK3ycvczZF+Vj5xRQr1wNBjG suSIayKUR5mn50GLKgC3Gmg5CA7qNS6dX0EaAG28wqVTfwZGYmumpZ3KX8nm49xYD74F vszkrRVXFCnXVBY3KADAiZSshDCmXwkq5rtqNLr9tbqAcPi1lQV3tVte1mnHR09dM3Te BzL/S44gle6cbm4dcYvRLZ//UXSH5KY799bKtIXt2eYy+BG+J18S+AhS4WNRHmbm86vf EV5RzBK++H8DpILafFyiNh0etu2dHuxtXGDQSMEQxQr5tS2DMK8I6ZDcI/WHbbFkfNKY 5R6Q== 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=zLR5TXDpeQicVz/pIe/H13q6H+7nytKHB0MyNjWGjI4=; b=dMEsD/oNXynXzvP6+H1dcb3bB46R5Qt79taDdomnD2GWbjjsY2QS8t7sE90I0rEawZ 2FoD+SlSBf406aI7hF+xSAgaJ+Af7MZ6hdd3uZVbDct+KmI3r71W1D5Uo4IEUv0We74O AshafVSxvJHNBfxdNTALh0Kp/YlhwklANawq9ntcFFcjTBF5MN+73eDNrmbcSfYomxwn uzlpWlHjX/3s1NjRzR1mYh2OUSe8GlZm27rj++Yn/4Uy2lrOB7UZ6YyV9GLsnHnMdT9H 8bsujNlZ+JboD+6HwaQ2s3KOnMZlFI99fWrUjGI9DoyuCTiuX7hnKFtO8zTLulx6Ilhq uCbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=j26K+EJR; 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 h15si23024528plk.74.2019.07.17.07.15.54; Wed, 17 Jul 2019 07:16:10 -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=j26K+EJR; 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 S1727805AbfGQOPa (ORCPT + 99 others); Wed, 17 Jul 2019 10:15:30 -0400 Received: from lelv0142.ext.ti.com ([198.47.23.249]:33254 "EHLO lelv0142.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727469AbfGQOP3 (ORCPT ); Wed, 17 Jul 2019 10:15:29 -0400 Received: from fllv0034.itg.ti.com ([10.64.40.246]) by lelv0142.ext.ti.com (8.15.2/8.15.2) with ESMTP id x6HEFLoP046062; Wed, 17 Jul 2019 09:15:21 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1563372921; bh=zLR5TXDpeQicVz/pIe/H13q6H+7nytKHB0MyNjWGjI4=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=j26K+EJRL19xzsTqxWX5FFF69E66dyy2T+ERa8XO6u1YlmXtQiwGvQFLb87ZICmgT DQbAG4Qm9sY3pboE+CECdGcA3PQhbdju/43mPwpUjXApxh8DrfIvftbsT2fV3ZMaZT KCF3XqzPUjMMgJ5VpgBfIeniMgz6KlKvZrBANUjg= Received: from DLEE104.ent.ti.com (dlee104.ent.ti.com [157.170.170.34]) by fllv0034.itg.ti.com (8.15.2/8.15.2) with ESMTPS id x6HEFLgl128080 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Wed, 17 Jul 2019 09:15:21 -0500 Received: from DLEE102.ent.ti.com (157.170.170.32) by DLEE104.ent.ti.com (157.170.170.34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Wed, 17 Jul 2019 09:15:21 -0500 Received: from fllv0039.itg.ti.com (10.64.41.19) by DLEE102.ent.ti.com (157.170.170.32) 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; Wed, 17 Jul 2019 09:15:21 -0500 Received: from localhost (ileax41-snat.itg.ti.com [10.172.224.153]) by fllv0039.itg.ti.com (8.15.2/8.15.2) with ESMTP id x6HEFKgD041061; Wed, 17 Jul 2019 09:15:21 -0500 From: Jean-Jacques Hiblot To: , , , , , , CC: , , , , , Jean-Jacques Hiblot Subject: [PATCH v4 2/4] leds: Add managed API to get a LED from a device driver Date: Wed, 17 Jul 2019 16:15:12 +0200 Message-ID: <20190717141514.21171-3-jjhiblot@ti.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190717141514.21171-1-jjhiblot@ti.com> References: <20190717141514.21171-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 9f48798a713d..714b55f1da0f 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