Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp6718271ybe; Wed, 18 Sep 2019 08:00:09 -0700 (PDT) X-Google-Smtp-Source: APXvYqw8QC3ft3+dtWxu8o41Weq/DFBbpJR/UjvkmAI8dsuRAggbdiQ2sElVszQdzdHrd+OSTqxY X-Received: by 2002:a17:906:3298:: with SMTP id 24mr7801094ejw.136.1568818809427; Wed, 18 Sep 2019 08:00:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568818809; cv=none; d=google.com; s=arc-20160816; b=w8yLcJLObeZlPp8gzv3PBTAjxUvh9aTqrExJ/lB0ziPYPylI4OoLSzR6lz+RhGPz7m xP5qHCruZDka9T+cJ/9nFn84U7J9N8huabyfL2C+QYyGJHq5NDyOhfOFE1DZ6b/XEkQh bVzRWXNjph+2EbnCXN+a9YUbwsILPAg0Z0hZFaZ30jtwyarewH16hFuH+2iEAoQYr122 rtDPv40Y6Bykd1Oz4oWaVN1gdaBE5Y2AgaTySinasveyOQrVVO3SWd3SnMNZYO1prhKV M3lc/MOZQw7JVDr8hy3MTLX9PsglM1ZSuW6oHtx4vfpEBq/I0yYu4JL9q1DtL0gl2YpR 0UpQ== 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=OOSxNH9AZ+QI3vXWQ4RnOIoZnDMzpob6pBb1GLj0bJY=; b=CFH+VO/0MB1oTIcZViB0uewJrELPIkurzOsz9o9vaU+BUHRR6j0he1YxL2grP8N6n1 Uw+aldc5VQQs9/N02EHgOK9/C15+lharj+3FQ0F3hrClmAzDrr5tjEXysfxcsjvD1QPe K5CIuaJY+qB5LZCCtNWdj/Hd8k7W23vtmwDXhz9laYokJed9s5KtE/qMtRfDqO56pTuU kuvYO4xK84VC1eFEeHVrXqGutkwmCFiGU9pWImxk7x8kKSH9IewleW1CwQGNKX55W6ot jsUnb6Dv8KBY6Hgdet0UVaTRlWw726eOgV8rlM2utC634qQrXIbhCKoR/aq3YHW5B04V oPVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=w6dnvztF; 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 g12si147850ejw.340.2019.09.18.07.59.46; Wed, 18 Sep 2019 08:00:09 -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=w6dnvztF; 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 S1731552AbfIRO5z (ORCPT + 99 others); Wed, 18 Sep 2019 10:57:55 -0400 Received: from fllv0016.ext.ti.com ([198.47.19.142]:60750 "EHLO fllv0016.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731522AbfIRO5y (ORCPT ); Wed, 18 Sep 2019 10:57:54 -0400 Received: from fllv0035.itg.ti.com ([10.64.41.0]) by fllv0016.ext.ti.com (8.15.2/8.15.2) with ESMTP id x8IEvjjI088040; Wed, 18 Sep 2019 09:57:45 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1568818665; bh=OOSxNH9AZ+QI3vXWQ4RnOIoZnDMzpob6pBb1GLj0bJY=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=w6dnvztFBRCjPz5MnDVBxkMR7XTHoGogXQOfW9WGrqfQS61twEOePfj0Fvwieds/j 1fx/XagszT3IkgQYTz6MgoGkCYOA+y6SV41y4//p/LKtEAR/MrpIOvG6O0V8+WWT26 XL+YzLPM+smfqQ9Il5/DWK6qbmoOIulfgMM60f9E= Received: from DLEE105.ent.ti.com (dlee105.ent.ti.com [157.170.170.35]) by fllv0035.itg.ti.com (8.15.2/8.15.2) with ESMTP id x8IEvjnh034529; Wed, 18 Sep 2019 09:57:45 -0500 Received: from DLEE100.ent.ti.com (157.170.170.30) by DLEE105.ent.ti.com (157.170.170.35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Wed, 18 Sep 2019 09:57:41 -0500 Received: from lelv0327.itg.ti.com (10.180.67.183) by DLEE100.ent.ti.com (157.170.170.30) 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, 18 Sep 2019 09:57:44 -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 x8IEvhgx012863; Wed, 18 Sep 2019 09:57:44 -0500 From: Jean-Jacques Hiblot To: , , , , , CC: , , , , , Subject: [PATCH v7 3/5] leds: Add managed API to get a LED from a device driver Date: Wed, 18 Sep 2019 16:57:28 +0200 Message-ID: <20190918145730.22805-4-jjhiblot@ti.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190918145730.22805-1-jjhiblot@ti.com> References: <20190918145730.22805-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 | 51 ++++++++++++++++++++++++++++++++++++++++ include/linux/leds.h | 2 ++ 2 files changed, 53 insertions(+) diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c index 2b8f20f94128..e11177d77b4c 100644 --- a/drivers/leds/led-class.c +++ b/drivers/leds/led-class.c @@ -264,6 +264,57 @@ 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. + * + * NOTE: At the moment, it supports only device-tree based LED lookup + * + * @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; + + 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_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..88bf2ceaabe6 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_led_get(struct device *dev, + int index); /** * led_blink_set - set blinking with software fallback -- 2.17.1