From patchwork Tue Feb 20 13:39:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herve Codina X-Patchwork-Id: 203604 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp404950dyc; Tue, 20 Feb 2024 05:40:28 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU3pD/gh6ArSyGVJf1S1hmHBYL44wa+fekWcxeD11XkPr+eiGe2hqCGA1mALy9IwpCJCKm+uCGqC4RtWfHJy6lY5RLUvg== X-Google-Smtp-Source: AGHT+IHcznfF2ukZ53U4XxAd+t6m7oLp7niv4ORsHZRk9GPgi6V0p50XhTeMcbu7F9K/lK1Jjlq4 X-Received: by 2002:a05:6358:6f13:b0:17b:4b5b:db59 with SMTP id r19-20020a0563586f1300b0017b4b5bdb59mr3315752rwn.23.1708436428499; Tue, 20 Feb 2024 05:40:28 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708436428; cv=pass; d=google.com; s=arc-20160816; b=RMDZ1SkJ/pn15A5CfaaWk2l4UV2pIVPAJviprmSL4LjNpoqz3fZyavUc5QI/69Zq14 8ejbIKooOuHHWzc5q35IxN+bbJZkHqyESO16S+hyeNYgp/YdrL8dtwG0HgiFQCcTpbqW jMvVjG9vS6mVWSnK7y8/LyUIyiqr4HY3oQCXdT+f+9w+HV7So8P7TXs+ds3MdBGC+jTJ gdpgCJ7GdW4quMwKq3XINfx/8mTGdV3RDTM+sQ81oOl3NXuwK12ofdAxa6GVs3ZxuDEK mUh4O0HcputsfRf2940THEesTvZJq68bGL4/536p73OISJbkwcePf/FU335XxJXgg5k3 xtGw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Oc4ohg5LOK/NMkvzmh0zZUOGGe+/Ajs5RSnJ2qJvG0E=; fh=QafpXXBPN+evVxhvNLO0vOTmFz/JzXhK29Onx49STnY=; b=zwL7bufMR4Q43etAW0jOjOrQBCMizsVRUkH1Yw6kFCzFd8QN7yoKi2PN15LPep83lo 4n8oYRGlZjFtcyotLgfC7GoTy6gUf/Pl3op6YYkYs9OYdxzC8Kg/KCo2gteB2k9J/8l2 5ZoQ3GOxRi+b9ZT+x5bk8hNPJLmAo7vG3TzHOsFhf9PNxiabcy5dPcxy/iBn41bWRRjz upsYGSnhUMg16ypesmq+VaS83J1MT4NJYbXcY5w3NR/rUO3ZWeq7+HvL6N6OfW9sh0R7 +Q4r7guAOHQJZlrteDoXy/8/z3FU2D56fc8D0XNavaihW94z7yJLMBe9SxfyY4OIafxF 2gbw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=dummO1kT; arc=pass (i=1 spf=pass spfdomain=bootlin.com dkim=pass dkdomain=bootlin.com dmarc=pass fromdomain=bootlin.com); spf=pass (google.com: domain of linux-kernel+bounces-73084-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73084-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id n128-20020a632786000000b005dc4eb22ed9si6070060pgn.166.2024.02.20.05.40.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 05:40:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-73084-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=dummO1kT; arc=pass (i=1 spf=pass spfdomain=bootlin.com dkim=pass dkdomain=bootlin.com dmarc=pass fromdomain=bootlin.com); spf=pass (google.com: domain of linux-kernel+bounces-73084-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73084-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id B0E96285FDE for ; Tue, 20 Feb 2024 13:40:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CB5B76A8D2; Tue, 20 Feb 2024 13:40:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="dummO1kT" Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CDD4267E63; Tue, 20 Feb 2024 13:39:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708436401; cv=none; b=k8qx1Syhn/oRulatSEpmmHyYRTcYsXJ/S7K52uhZc4AvY5yxISP4MH3Tv7Jqo4pZF556uNVlz6l1apIXydYfxU2nIM4U0AtXSOXaOkK2U1siyLm7BnRFsLDbURiDBw6+dhod5WYsJT/3IZqWSWZq+p4olmjoo29Y0KKeNwsc9jM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708436401; c=relaxed/simple; bh=ulweI1Ce0cUyHQQ4Wm/kRBRiyj0VTxmuz/Nzdi2q6FM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eiBCBixih3x1i/mPp6x2iNjK6dfrFFWVifLDBhkW4k/+cO+Xfa6SUxDjhrDzV17voNq2Nwl91BFbw6YqmmYelbjIb9KDtKSfWPOQX76izXZdbFLAGbVXSwFCzS80p9hY52Cvq1FoCG7OIyPJ/20VuDErDA9WLJZlUp8dIis3zzk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=dummO1kT; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Received: by mail.gandi.net (Postfix) with ESMTPA id AC9E62000B; Tue, 20 Feb 2024 13:39:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1708436397; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Oc4ohg5LOK/NMkvzmh0zZUOGGe+/Ajs5RSnJ2qJvG0E=; b=dummO1kTW7Rvsqc9TVwoXA/SOVTnR/bjg8Q2Hwyhqd890trD1tkru/OyYC/ytGqXsnjKzR w9SVt52EE+Wpj8a1Yx/QHnnxQYbIywjBNhnFZuqC431/VhWJRfn0CsEQZWS/WRsU2I4gh1 x1jSd8qdY3PjrDgaVpSREjnyhcXgw6I44nPysypqWQHKPNTPleOVFXFiHrvfvw5Dgl9yMv vPxIsyIbxrGVZFLYKurn8ocQYvZIHsmHuYexKFWba6x5prRXDhczbf6xvnzlJdbfbyFawF dyD1SIBFzUXszpAmM53CHcNF0sJBNW1iw7pdVE6P+WFHY3wKT7kqZ8IslPlg4g== From: Herve Codina To: Linus Walleij , Bartosz Golaszewski , Pavel Machek , Lee Jones Cc: Saravana Kannan , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-leds@vger.kernel.org, Luca Ceresoli , Thomas Petazzoni , Herve Codina Subject: [PATCH RESEND 1/2] gpiolib: Introduce gpiod_device_add_link() Date: Tue, 20 Feb 2024 14:39:48 +0100 Message-ID: <20240220133950.138452-2-herve.codina@bootlin.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240220133950.138452-1-herve.codina@bootlin.com> References: <20240220133950.138452-1-herve.codina@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-GND-Sasl: herve.codina@bootlin.com X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791425436479397971 X-GMAIL-MSGID: 1791425436479397971 With device-tree, some devlink related to gpios are automatically added when a consumer device is added and the attached node has phandles related to GPIOs. In some cases, the real device used to get the gpio during a probe() can be related to an of-node parent of the of-node used for the already done automatically devlink creation. For instance, a driver can be bound to a device and, during the probe(), the driver can walk its of-node children to get the GPIO described in these children nodes. In that case, an additional devlink between the device attached to the driver and the gpio consumer need to be created. Indeed, if the GPIO is removed, the consumer/supplier dependency should lead to remove first the consuming driver before removing the supplier. In order to give the possibility to this kind of driver to add additional devlinks, introduce gpiod_device_add_link(). Signed-off-by: Herve Codina --- drivers/gpio/gpiolib.c | 32 ++++++++++++++++++++++++++++++++ include/linux/gpio/consumer.h | 5 +++++ 2 files changed, 37 insertions(+) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 8b3a0f45b574..416ab334b02d 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -4195,6 +4195,38 @@ static struct gpio_desc *gpiod_find_and_request(struct device *consumer, return desc; } +/** + * gpiod_device_add_link - Add a link between a GPIO consumer and a GPIO. + * @consumer: GPIO consumer. + * @desc: GPIO consumed. + * @flags: Link flags, see device_link_add(). + * + * This function can be used for drivers that need to add an additional + * consumer/supplier device link to a GPIO. + * + * Returns: + * On successful, the link created. + * NULL if the link was not created due to a missing GPIO parent. + * + * In case of error an ERR_PTR() is returned. + */ +struct device_link *gpiod_device_add_link(struct device *consumer, + struct gpio_desc *desc, + u32 flags) +{ + struct device_link *link; + + if (!desc->gdev->dev.parent) + return NULL; + + link = device_link_add(consumer, desc->gdev->dev.parent, flags); + if (!link) + return ERR_PTR(-EINVAL); + + return link; +} +EXPORT_SYMBOL_GPL(gpiod_device_add_link); + /** * fwnode_gpiod_get_index - obtain a GPIO from firmware node * @fwnode: handle of the firmware node diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h index db2dfbae8edb..4feed4e166b0 100644 --- a/include/linux/gpio/consumer.h +++ b/include/linux/gpio/consumer.h @@ -7,6 +7,7 @@ struct acpi_device; struct device; +struct device_link; struct fwnode_handle; struct gpio_array; @@ -106,6 +107,10 @@ void devm_gpiod_put(struct device *dev, struct gpio_desc *desc); void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc); void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs); +struct device_link *gpiod_device_add_link(struct device *consumer, + struct gpio_desc *desc, + u32 flags); + int gpiod_get_direction(struct gpio_desc *desc); int gpiod_direction_input(struct gpio_desc *desc); int gpiod_direction_output(struct gpio_desc *desc, int value); From patchwork Tue Feb 20 13:39:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herve Codina X-Patchwork-Id: 203605 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp405016dyc; Tue, 20 Feb 2024 05:40:36 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWWy7sNraU2sQalc6rb6/OeQeYTIfPZ7GiN6c0QrP2o9MRyQwjpOiLC9YRq3cQaNgBaB/Vb1sJPl665ZPIURXT+WuoCIg== X-Google-Smtp-Source: AGHT+IGo7ddj254sfLJvDvNZwlnsK1fTU6DOBLirTZaOYcj6uXaaGOC3yi5U5dZiYXkbj14ws1rW X-Received: by 2002:a05:6902:2310:b0:dc7:6f13:61d1 with SMTP id do16-20020a056902231000b00dc76f1361d1mr18100730ybb.20.1708436436211; Tue, 20 Feb 2024 05:40:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708436436; cv=pass; d=google.com; s=arc-20160816; b=O+CbUYa7FJ4tVeXrhEju9SZKo3qb5YAdg/Ip4mA6rYj5jcPc8BBGJoGmsBcjVAPkoB f2tINNP8H4hzlHluU2UNzpvCO5VmivRfRCtsjHT/V1Q+JgCE1OPFV6pFFw3yKWlJLdQ2 t5SP5s/VERtTWrm85wLWJKfh6PR1XQUVoedlLgqtiVQbBcIs79uLCKuwaU/qv5sOCnbR X1iF9vEVMjzYz5XN8wyNIf0lvSQInUj07bN/yYFUZmbaoxugoI1ERewm0EoHAm0fdmBc 4yKNQBbEwqRapTJdkaW4OgIMcFnhKo1imxu3RUjmJERIxH9K1t5ekPapEu1aD9j8TQZq DPPA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=THtvQDT+qQLPub8vG8YsD0SpllI5xUKHDkxR4GFPKUc=; fh=QafpXXBPN+evVxhvNLO0vOTmFz/JzXhK29Onx49STnY=; b=oqopsh1CWQZdwrhv0d3OJZg2IZ0niG6RIR4olxbyS2rcHCzmJCNEqorlJASrLSrNN1 F+uNtVgJ2ADiEEq8eZcRivyh7Y483dzpHFOgYGkqnx1UToc3uCHpWm+otoy5GhBNkFSs 3IqlEg4GtcfAYmneMW6vexMp/lSLjP07PwXiFo1tgYq6F0zNGFF24zbE6PXybCLi48bD u/yZBa2nVW/Hn7CZ3wDEsBb75daRXCIOTx0/eg9aznAnj6W/kGKprU2pMQVKD8JNwUhA 4lqBWYjp6KixoDxdXwN0vR20uWhal8Med6PUiMKhqX/ZZp6e1zveXqOJgrOgCZd2zOtl QmFA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=dyt63mmK; arc=pass (i=1 spf=pass spfdomain=bootlin.com dkim=pass dkdomain=bootlin.com dmarc=pass fromdomain=bootlin.com); spf=pass (google.com: domain of linux-kernel+bounces-73085-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73085-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id r20-20020ac867d4000000b0042dc85eeda8si2359696qtp.121.2024.02.20.05.40.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 05:40:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-73085-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=dyt63mmK; arc=pass (i=1 spf=pass spfdomain=bootlin.com dkim=pass dkdomain=bootlin.com dmarc=pass fromdomain=bootlin.com); spf=pass (google.com: domain of linux-kernel+bounces-73085-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73085-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 446971C22DEB for ; Tue, 20 Feb 2024 13:40:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8AEAA6BB20; Tue, 20 Feb 2024 13:40:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="dyt63mmK" Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 88FB36A346; Tue, 20 Feb 2024 13:40:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708436403; cv=none; b=VSK07IHiJvkG3EwkAOiKyYfWLJIU3oVXENnTrSuuqnGyvH8xb5CHKQV2yMg+C7WHAEHL9HK1r4hm7/xw0e0kK8Pidk663BGqZfid8RMN2c8k9BZlKCNdj2vn0AD3zxifk5M4RY0QcTEOZ7PNvjFKd0efZQMhq07JhOhbzYS4VA4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708436403; c=relaxed/simple; bh=G5QNoctspJ1RDquSBWI4re9ufm9ecSTNMxVtKdOzPC4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kTEdHGNLrci+3WCJtSAUntTpKKK1PhPzTSNaPyrDNdeM748XjCAIwAgRERnjjaS1xTDldCxkcAlREB0sawgy2Ft6DwahT4BeR9qX1hMMtrvdkzoKrWQ7UP/HFlVYmKcsXDjQPhrTO/3CEpFXpkelliZY92s2rXG9RZkFxvtt1W4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=dyt63mmK; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Received: by mail.gandi.net (Postfix) with ESMTPA id 8916820011; Tue, 20 Feb 2024 13:39:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1708436399; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=THtvQDT+qQLPub8vG8YsD0SpllI5xUKHDkxR4GFPKUc=; b=dyt63mmKnd2Z/+FblfX5f/mKstSpdWCixUiXcAjswnNreAWHDLHGYt1BaRFxvUsnHTUags LHksF5XcBusLoE7jcn/YsTyiDylHsblQwVWca+QnQqYxbYMtfMQ3iUVA/PaSOHoCnrblaY 9lEBlNzIBfv/IVHWRnmOhb/tkpg24xeTV6WQsBVXPzKWgCezLNisM5qWLkjFTkqlXuhSnr q+2beu/laXjyXqEpjbFcCKUQKdBZNiF44J1CtIARcJEjdlOx3ujuLa6/tDWpPjNJraWGPm s0n3u9GGXPLDdUFAggMkENlqWakT4Kz0Vl6RxV1KpId6CWR6CzDp5EeR3hak9A== From: Herve Codina To: Linus Walleij , Bartosz Golaszewski , Pavel Machek , Lee Jones Cc: Saravana Kannan , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-leds@vger.kernel.org, Luca Ceresoli , Thomas Petazzoni , Herve Codina Subject: [PATCH RESEND 2/2] leds: gpio: Add devlinks between the gpio consumed and the gpio leds device Date: Tue, 20 Feb 2024 14:39:49 +0100 Message-ID: <20240220133950.138452-3-herve.codina@bootlin.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240220133950.138452-1-herve.codina@bootlin.com> References: <20240220133950.138452-1-herve.codina@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-GND-Sasl: herve.codina@bootlin.com X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791425444539121965 X-GMAIL-MSGID: 1791425444539121965 When a gpio is removed, the gpio leds consumer need to be removed first. This dependency consumer/supplier can be described by devlink links. In case of device-tree, even if some devlinks are created due to the presence of gpio phandles in the gpio leds children, these links do not help in removing the gpio leds device (i.e. the real consumer) before the consumed gpio. We can reach cases where the gpio are no more present and the gpio leds driver continue to have leds using these gpio. Further more, when the gpio come back, the gpio leds still use the old removed one. Indeed, the gpio are consumed by the parent of the consumer used in the devlink creation due to phandles. A link is missing between the gpio and the real gpio consumer, the gpio leds device itself. Use the newly introduced gpiod_device_add_link() to create this missing link between the gpio leds devices and the gpios. With that done, if a gpio is removed, the gpio leds is removed and the resources are correctly released. Signed-off-by: Herve Codina --- drivers/leds/leds-gpio.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/drivers/leds/leds-gpio.c b/drivers/leds/leds-gpio.c index 83fcd7b6afff..b3ec8ecbe5da 100644 --- a/drivers/leds/leds-gpio.c +++ b/drivers/leds/leds-gpio.c @@ -150,6 +150,7 @@ static struct gpio_leds_priv *gpio_leds_create(struct device *dev) { struct fwnode_handle *child; struct gpio_leds_priv *priv; + struct device_link *link; int count, ret; count = device_get_child_node_count(dev); @@ -197,6 +198,20 @@ static struct gpio_leds_priv *gpio_leds_create(struct device *dev) /* Set gpiod label to match the corresponding LED name. */ gpiod_set_consumer_name(led_dat->gpiod, led_dat->cdev.dev->kobj.name); + + /* + * Create a link between the GPIO and the gpio-leds device. + * This allow to have a relationship between the gpio used and + * the gpio-leds device in order to automatically remove the + * gpio-leds device (consumer) when a GPIO (supplier) is removed. + */ + link = gpiod_device_add_link(dev, led_dat->gpiod, + DL_FLAG_AUTOREMOVE_CONSUMER); + if (IS_ERR(link)) { + fwnode_handle_put(child); + return ERR_CAST(link); + } + priv->num_leds++; }