Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1818077pxp; Mon, 21 Mar 2022 05:55:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzytXttJsny0qSMEZQJTiO+9GFZfnSHdGbmPTbLa9UoCqkgw91dCY/anvqxqWB28JQHtT9m X-Received: by 2002:a05:6402:3592:b0:418:6d45:2008 with SMTP id y18-20020a056402359200b004186d452008mr22543981edc.131.1647867358476; Mon, 21 Mar 2022 05:55:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647867358; cv=none; d=google.com; s=arc-20160816; b=gf3GWPiO/uSYAr6N6Fl1GPh9Wx1xff1Z/C5s79EjTZK69xvPFXoGnreAnhTIrnh7QP YNmGJetHRAvPMJImJabIYdeLUbxm1YHphttypWOFsek1I6ns/NxyCMLRRnT/rncvHcGU Zfld0LLvpaNJtIGI90UHbp0vck1mmknfvi/UnghRkl3+PmqFmfU3oPjbYzpGSFfkM1Fi PwAGih6/v4CJONH4uSbDJBqOlPiiYbjvlkWTahKglbcLlwiQyylMm0PxGu+3QZrP+UzO oATdI9LJaorQReuH8PuSO65QJTBuFd2wa1ntlvQ1M+pq75P6gt9ccf9c+ZRlPgxnynXv OE+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=56lPZX1oVQJS/5a7tNWzYHg+FGwdNs7uZ1Kt0v+qH3A=; b=PTwlwnTyWGtX5xm30AnJwHg5PBEiJUSZIe8E2gm/Czobk2P54ecZ6WUceDnD2nJ84o TWC3MVZIKZzdYz7ooUymxG/I75GBKsMUqJjoIyGzDfibBOSCjRgBQM5AdQVwz9Ip3LbQ envxr7gsrRuSbxZVh5eFA7mImah/68/Plag+XRf3WC1RQxSYErSQseBvxH6T0WCAtV3H g9oLt0xLlCm+QFMP6w98jYIf3+g1vAtvhv75tRdFpDEgicImpCqEhk1EeWBmi9CSEhbv HdlXnnNGIayNFwoVKjhyAxwkupBRfK0zyJYuuIKZDMe9t48/J19I4pJppi8/ovJRnr5A wqJg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e8-20020a50e448000000b00418c2b5bd63si10823553edm.69.2022.03.21.05.55.31; Mon, 21 Mar 2022 05:55:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345224AbiCUIR1 (ORCPT + 99 others); Mon, 21 Mar 2022 04:17:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345210AbiCUIRJ (ORCPT ); Mon, 21 Mar 2022 04:17:09 -0400 Received: from inva020.nxp.com (inva020.nxp.com [92.121.34.13]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CDBA122987; Mon, 21 Mar 2022 01:15:19 -0700 (PDT) Received: from inva020.nxp.com (localhost [127.0.0.1]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id 430D01A1D7B; Mon, 21 Mar 2022 09:15:18 +0100 (CET) Received: from aprdc01srsp001v.ap-rdc01.nxp.com (aprdc01srsp001v.ap-rdc01.nxp.com [165.114.16.16]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id F12361A0F97; Mon, 21 Mar 2022 09:15:17 +0100 (CET) Received: from localhost.localdomain (shlinux2.ap.freescale.net [10.192.224.44]) by aprdc01srsp001v.ap-rdc01.nxp.com (Postfix) with ESMTP id A3B07183AC99; Mon, 21 Mar 2022 16:15:16 +0800 (+08) From: haibo.chen@nxp.com To: linus.walleij@linaro.org, brgl@bgdev.pl Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-imx@nxp.com, haibo.chen@nxp.com Subject: [PATCH] gpio: Add gpio alias support Date: Mon, 21 Mar 2022 16:05:19 +0800 Message-Id: <1647849919-8778-1-git-send-email-haibo.chen@nxp.com> X-Mailer: git-send-email 2.7.4 X-Virus-Scanned: ClamAV using ClamSMTP X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Haibo Chen This patch allow setting gpio device id via device tree alias. Signed-off-by: Haibo Chen --- drivers/gpio/gpiolib.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 56d090258d62..3d24351a33db 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -495,7 +495,7 @@ static void gpiodevice_release(struct device *dev) list_del(&gdev->list); spin_unlock_irqrestore(&gpio_lock, flags); - ida_free(&gpio_ida, gdev->id); + ida_simple_remove(&gpio_ida, gdev->id); kfree_const(gdev->label); kfree(gdev->descs); kfree(gdev); @@ -594,6 +594,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, unsigned long flags; int base = gc->base; unsigned int i; + int alias_id, first_dynamic; int ret = 0; u32 ngpios; @@ -623,11 +624,20 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, */ gdev->dev.fwnode = dev_fwnode(&gdev->dev) ?: fwnode; - gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL); - if (gdev->id < 0) { - ret = gdev->id; - goto err_free_gdev; + alias_id = of_alias_get_id(gdev->dev.of_node, "gpio"); + if (alias_id < 0) { + first_dynamic = of_alias_get_highest_id("gpio"); + if (first_dynamic < 0) + first_dynamic = 0; + else + first_dynamic++; + alias_id = ida_simple_get(&gpio_ida, first_dynamic, 0, GFP_KERNEL); + if (alias_id < 0) { + ret = alias_id; + goto err_free_gdev; + } } + gdev->id = alias_id; ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id); if (ret) @@ -821,7 +831,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, err_free_dev_name: kfree(dev_name(&gdev->dev)); err_free_ida: - ida_free(&gpio_ida, gdev->id); + ida_simple_remove(&gpio_ida, gdev->id); err_free_gdev: /* failures here can mean systems won't boot... */ if (ret != -EPROBE_DEFER) { -- 2.25.1