From patchwork Wed Jul 10 12:39:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Jacques Hiblot X-Patchwork-Id: 11038433 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 673F1138B for ; Wed, 10 Jul 2019 12:39:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5AA8428802 for ; Wed, 10 Jul 2019 12:39:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4D89528956; Wed, 10 Jul 2019 12:39:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id EA4F62894E for ; Wed, 10 Jul 2019 12:39:50 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 5F96E8982E; Wed, 10 Jul 2019 12:39:47 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from fllv0015.ext.ti.com (fllv0015.ext.ti.com [198.47.19.141]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0690989745 for ; Wed, 10 Jul 2019 12:39:45 +0000 (UTC) Received: from fllv0034.itg.ti.com ([10.64.40.246]) by fllv0015.ext.ti.com (8.15.2/8.15.2) with ESMTP id x6ACdeeO104812; Wed, 10 Jul 2019 07:39:40 -0500 Received: from DLEE103.ent.ti.com (dlee103.ent.ti.com [157.170.170.33]) by fllv0034.itg.ti.com (8.15.2/8.15.2) with ESMTPS id x6ACdeme113528 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Wed, 10 Jul 2019 07:39:40 -0500 Received: from DLEE106.ent.ti.com (157.170.170.36) by DLEE103.ent.ti.com (157.170.170.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Wed, 10 Jul 2019 07:39:40 -0500 Received: from lelv0327.itg.ti.com (10.180.67.183) by DLEE106.ent.ti.com (157.170.170.36) 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, 10 Jul 2019 07:39:40 -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 x6ACdd2g071883; Wed, 10 Jul 2019 07:39:39 -0500 From: Jean-Jacques Hiblot To: , , , , , , Subject: [PATCH v3 2/4] leds: Add managed API to get a LED from a device driver Date: Wed, 10 Jul 2019 14:39:30 +0200 Message-ID: <20190710123932.28244-3-jjhiblot@ti.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190710123932.28244-1-jjhiblot@ti.com> References: <20190710123932.28244-1-jjhiblot@ti.com> MIME-Version: 1.0 X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1562762380; bh=ItirwO8erJEYpB2ZQfeB1f9//VYKLfEYE9vUyXsRw94=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=fiAQzfgmv6EyownznjqE2NAbeOdOnBlqbOktus8k3tf27F6fETNlihMgdsdHarK0a gQLxG0AVtms0wfiulXwC26bUO26ilXTpISul4YJRd+dJxpaYOh/ovK1nH8X8HQx0Qx JC7OAD+x2E6sGrkg1x89rSRrRBMPvdsIkvOg8id0= X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, tomi.valkeinen@ti.com, dmurphy@ti.com, Jean-Jacques Hiblot , linux-leds@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP If the LED is acquired by a consumer device with devm_led_get(), it is automatically release when the device is detach. 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 0f67b13b0f1f..521cb76fbaf7 100644 --- a/drivers/leds/led-class.c +++ b/drivers/leds/led-class.c @@ -264,6 +264,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