Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp6727542ybi; Mon, 8 Jul 2019 07:41:44 -0700 (PDT) X-Google-Smtp-Source: APXvYqxMxWDAAFYHX4nalplSRrU8o1pIA5Fr9Av8VUd3TrxM1ppF27efFd1DT0oCjG22ZPJ3qICK X-Received: by 2002:a17:90a:384d:: with SMTP id l13mr26551053pjf.86.1562596904702; Mon, 08 Jul 2019 07:41:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562596904; cv=none; d=google.com; s=arc-20160816; b=el7Vkg/EAkhIbJZiVlp1CGaTEv1miCW6FI489ih1kueL8HcU5RercdzwQbtSzUP3yG bIsQQc9iVDuSfX/VzeGFd7etCLLLW5CB0NnvQ2ljDmrIVGbW1U7nHzHrwgJGTbRfCry8 OYcjC/bkyg3o2J3KGtKm/vNrnSP+/nWhVfgPqS9AzaP+1n3xqJOY6SVuJ9zr10pea4xZ L4Vzd7Cjue5Ir9IUKtbJOqyNuyls9auTIxXxNOkhkn+uBh1BgPEyfcAsAkQhoXUcGtIb qtcYlrphW3LxUIPmZTVyCqKdu3ICwsG6x/hBVSp4LV7egm8RtWGFQv0fMGZ3tCPmkU9h IvLA== 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=ItirwO8erJEYpB2ZQfeB1f9//VYKLfEYE9vUyXsRw94=; b=O6Wh6lCLTFxz0FNEDEx+iCDBBi/IrWSwYwhvW+3bWFmxPwKtRzH284xmkcsCbsDdPd 3FADGyfbhv7MeuQyC5g5wCVFPziqtDwYq6ZCfhbRnGKXOsZpLuna7WLfXSg3IFohpcrf PGyfoOgr234XZBhN3b3g9WJogpZ7CYMMkEkMV4nzPOSWr3w+7cIATzkVAT+Yv6VgJy1h Z7zqCx7B3zKNohmQgQc6XC4bSFtrZMgEbkXJ7OPgvrfJhqsFP7AG81c3sOUQLfeS2vSk tWJ4/CE5Z0W5BeZHEREqDymvXeRCC7Qbj+4arpBLODXgTyhsqx7qaspUBbMdBARng1qY Naxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=rzCV+8qb; 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 100si18471575pla.158.2019.07.08.07.41.29; Mon, 08 Jul 2019 07:41:44 -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=rzCV+8qb; 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 S1730119AbfGHK1e (ORCPT + 99 others); Mon, 8 Jul 2019 06:27:34 -0400 Received: from fllv0015.ext.ti.com ([198.47.19.141]:57174 "EHLO fllv0015.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729360AbfGHK1S (ORCPT ); Mon, 8 Jul 2019 06:27:18 -0400 Received: from lelv0266.itg.ti.com ([10.180.67.225]) by fllv0015.ext.ti.com (8.15.2/8.15.2) with ESMTP id x68ARA5C041885; Mon, 8 Jul 2019 05:27:10 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1562581630; bh=ItirwO8erJEYpB2ZQfeB1f9//VYKLfEYE9vUyXsRw94=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=rzCV+8qb6tT+XQFnBxYkqKRu5ioTGUuoZo+fLVFgaj3Ur0V2PNb2lsCjKlEpCQf43 a2SKqGFo0uMCkT41mL7ltLHS66jhCKu47cNWhgjfUexCpCYYsymf/kp+ZlME/UPoCe KoZ4Pb+J02tmNRiaXbJGBHOCg7qmyM34Chigmu+c= Received: from DLEE103.ent.ti.com (dlee103.ent.ti.com [157.170.170.33]) by lelv0266.itg.ti.com (8.15.2/8.15.2) with ESMTPS id x68ARAkR112018 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Mon, 8 Jul 2019 05:27:10 -0500 Received: from DLEE103.ent.ti.com (157.170.170.33) 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; Mon, 8 Jul 2019 05:27:09 -0500 Received: from fllv0040.itg.ti.com (10.64.41.20) 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 via Frontend Transport; Mon, 8 Jul 2019 05:27:09 -0500 Received: from localhost (ileax41-snat.itg.ti.com [10.172.224.153]) by fllv0040.itg.ti.com (8.15.2/8.15.2) with ESMTP id x68AR8MN074669; Mon, 8 Jul 2019 05:27:09 -0500 From: Jean-Jacques Hiblot To: , , , , , , CC: , , , , , Jean-Jacques Hiblot Subject: [PATCH v2 2/4] leds: Add managed API to get a LED from a device driver Date: Mon, 8 Jul 2019 12:26:58 +0200 Message-ID: <20190708102700.23138-3-jjhiblot@ti.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190708102700.23138-1-jjhiblot@ti.com> References: <20190708102700.23138-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 release when the device is detach. Signed-off-by: Jean-Jacques Hiblot --- 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 -- 2.17.1