Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp68917ybn; Thu, 3 Oct 2019 01:33:40 -0700 (PDT) X-Google-Smtp-Source: APXvYqxjQeRVQZ3VEENy4rKU1JlH/XzkR8GvnQcFITSsqH0XAxc5eH7+5VCHNCO4cLJEoyN5ey9q X-Received: by 2002:a17:906:27cf:: with SMTP id k15mr6656183ejc.150.1570091620716; Thu, 03 Oct 2019 01:33:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570091620; cv=none; d=google.com; s=arc-20160816; b=hGcaucKFVnVZ7QptY7CeH2p/qwxF4ld7LcUM0NbBEGpBMiMZUnUgga+8NmHjpkxT8R yA/mnbGyUg2SNZqsPEnUBJCYnZhM6L0Lia8CUDF6LU64RMjMvnhZPmz+moV2AFcWph5c 7QsuOq20pJwcloyf/UnvOcJGsmRDGnPGIy5eBn3leW9X9EQTYx8+QFeXJNRgtKzRL4oD +XFXcHfSSm+RPDqhJD8RiHGU6K5TnYtVMv421z820+fow20+qdc+ZdMvCw2KQPb95eBh iEtETXUuQ07z02k2itpxjm/9OC76w65l4X9E/T0c6HM412jcKbisEk+3MgRRtQBM0421 V8Vg== 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=JqcpVY+cllPhKIR7VpbA5/WIIYir2CvRhZ6o6hZeJBc=; b=dw2ERaxC/y84qy/QPrXNLFT7X4ILDOelhyN6RO8LzD20WK9Sq9rmSQpeb9qTul89Sh lfXs6+Wi7waUyaXJoz+Bcbvb7huU8QqR/EC1dSd3lHOT2lK8zowrWHxgIhfp2sQVRZEf 4egwtZJoO4VNBWEzKDcZgfZ1jkyKxRPJXur31If/XWZBl1Kq3O8F5/tEKpsKQh4QW05q eNJD7eZuDD5Hmk9Fr1mHs/bYlFAIl42XVed9mgRhTL/1CkrhaHVAIo/g0t3vKZSFkQS6 52FutbT0cKiUc0qa04Gqg86wb57DrU/Xd49MtxqbRXdqS99118n0tQK7zcRL9nGgaknh K0EQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=WqbYuTBb; 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 g21si963289edm.116.2019.10.03.01.33.16; Thu, 03 Oct 2019 01:33:40 -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=WqbYuTBb; 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 S1728963AbfJCI2q (ORCPT + 99 others); Thu, 3 Oct 2019 04:28:46 -0400 Received: from fllv0015.ext.ti.com ([198.47.19.141]:36036 "EHLO fllv0015.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728794AbfJCI2c (ORCPT ); Thu, 3 Oct 2019 04:28:32 -0400 Received: from lelv0265.itg.ti.com ([10.180.67.224]) by fllv0015.ext.ti.com (8.15.2/8.15.2) with ESMTP id x938SNSY037716; Thu, 3 Oct 2019 03:28:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1570091304; bh=JqcpVY+cllPhKIR7VpbA5/WIIYir2CvRhZ6o6hZeJBc=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=WqbYuTBbYzVOBGPHUqutLCNuRrXp4QekcHPFTwhVFHtZYfSwqyazU0IS+wlvo0gzn 5KzXXigt1Hysy3n3YN1fYkNxuu32Tvmmc7zy4vODfAR8dl/ztsLcBl3DvPUzgiggB9 uPIPS7NoP+Oaf3SRnzN1Und5cbRkzcum1VoSTlTo= Received: from DFLE105.ent.ti.com (dfle105.ent.ti.com [10.64.6.26]) by lelv0265.itg.ti.com (8.15.2/8.15.2) with ESMTPS id x938SN50117594 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Thu, 3 Oct 2019 03:28:23 -0500 Received: from DFLE111.ent.ti.com (10.64.6.32) by DFLE105.ent.ti.com (10.64.6.26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Thu, 3 Oct 2019 03:28:23 -0500 Received: from lelv0327.itg.ti.com (10.180.67.183) by DFLE111.ent.ti.com (10.64.6.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; Thu, 3 Oct 2019 03:28:23 -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 x938SMIP082976; Thu, 3 Oct 2019 03:28:23 -0500 From: Jean-Jacques Hiblot To: , , , , , CC: , , , , , Jean-Jacques Hiblot Subject: [PATCH v8 3/5] leds: Add managed API to get a LED from a device driver Date: Thu, 3 Oct 2019 10:28:10 +0200 Message-ID: <20191003082812.28491-4-jjhiblot@ti.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191003082812.28491-1-jjhiblot@ti.com> References: <20191003082812.28491-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 | 49 ++++++++++++++++++++++++++++++++++++++++ include/linux/leds.h | 2 ++ 2 files changed, 51 insertions(+) diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c index 455545f5d663..80c96dd96afc 100644 --- a/drivers/leds/led-class.c +++ b/drivers/leds/led-class.c @@ -258,6 +258,55 @@ 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_of_led_get - Resource-managed request of a LED device + * @dev: LED consumer + * @index: 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_of_led_get(struct device *dev, + int index) +{ + struct led_classdev *led; + struct led_classdev **dr; + + if (!dev) + return ERR_PTR(-EINVAL); + + /* Not using device tree? */ + if (!IS_ENABLED(CONFIG_OF) || !dev->of_node) + return ERR_PTR(-ENOTSUPP); + + 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_of_led_get); + static int led_classdev_next_name(const char *init_name, char *name, size_t len) { diff --git a/include/linux/leds.h b/include/linux/leds.h index 6f7371bc7757..9b94cf752012 100644 --- a/include/linux/leds.h +++ b/include/linux/leds.h @@ -199,6 +199,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_of_led_get(struct device *dev, + int index); /** * led_blink_set - set blinking with software fallback -- 2.17.1