Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp35582424rwd; Mon, 10 Jul 2023 09:24:03 -0700 (PDT) X-Google-Smtp-Source: APBJJlG17jDpnVWZ5XWZdsHFP/B/40fNN79Bih6A0u4p3bwmjRr7RdiaFfJKwR8eA8diZD0P12kK X-Received: by 2002:a05:6512:3b29:b0:4f9:557e:35ae with SMTP id f41-20020a0565123b2900b004f9557e35aemr12949659lfv.19.1689006243576; Mon, 10 Jul 2023 09:24:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689006243; cv=none; d=google.com; s=arc-20160816; b=VTkgbc7E0bnLwUHZaRomgxL1CLshdZ7CUtgPk8Bsio9+ErKTQSg+/Dco7t7sevgtM7 uuFweWut6qW4BWVCER2XbPACg9n6F+eSjSpwbs8oM6ahIY8IFNo0V3D3/qjSq2SNffPJ OOtP30lzMAp+nAcMqgIjzA1PXkyYfodlIbUvWhYRGE7cmEQjGWkxbuPYCOZ6t7qxUj3B F07p7L/NeqDMFBaTzxgFAylWyYaiJ9Y6qHc62zVsFfzOkLELIuWBv/SourRMm/fIONNy E3/OmdKQlbC4ReoTQ7HM5Vjr6KZOycmZbv6VOnW2/0IQhzAE9Qjh9cId7g+6+Qljiwb9 B+zg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=05Eo8yAcewTO+WPsEETnF/DlVCQ3v4XdrP+vRxVBOe8=; fh=CWfEt6/vd4IsHMc+T8W3bMkH4b5uOoX72TeJSijIGlU=; b=gfphF41LWNQJhRoAzsE9zX1jRUHnJ54grVOx76nj9o2ptUESLArFuWrXNGKGEKMATB kAPnkgmFGxTRCSY2eAIXTRruTY5UYwkgsQJxA3Jf5opBqs8qiRgSGy/H5Q48uFGJfvYL zZOH7aFJOsmX222Pm/HM9h/CdgKFi4fXJFVu4Juaamht5cW2NlVs8fteAvwiRk0Jx4qs 0rkYHYhQuoRWoqNF1OHGGAo3RjTnkjjP7vLzPhDo/m8mfNUPDuc3wv0sw8EMPuvoEID9 Sel4j4LIkwyg2gY5ZpdokulstGgkGZHTJGeNx9K5IyCZQeb5JW48oI+GbeYmB2AQJYqe SJ8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=UwygpstZ; 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=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u7-20020a05640207c700b0051e037805acsi6813538edy.125.2023.07.10.09.23.31; Mon, 10 Jul 2023 09:24:03 -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; dkim=pass header.i=@intel.com header.s=Intel header.b=UwygpstZ; 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=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233561AbjGJPtn (ORCPT + 99 others); Mon, 10 Jul 2023 11:49:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233554AbjGJPtm (ORCPT ); Mon, 10 Jul 2023 11:49:42 -0400 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4090E11F; Mon, 10 Jul 2023 08:49:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1689004181; x=1720540181; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=REWyC/nAC6Z96ggpA1NLsyDE3cGETw1DuCTFixDA/T8=; b=UwygpstZm01bGkJjy4XU9BnFvg5PsxZxOMwSe9HDbbGLPoeppOUCS8tA y5w99YgV+9BUeGiEgqSsnfMTdlMJHQg0E8ooiQRulthMyD92Qey26oUR8 rHFVIhnoE79E0VjI4vJumbFIwXseC85q7u4O1akMJqp6DgtWaF0ZYs3kI 9q2wy9O0iJyTdPsA+ypVzqQEMl3KD4SB8RxKQKTdVLD/5gHO8eyrVQuuB 0Kl85W4bPU4Gf4sKVoktwg62seIZK9WGFh1bNrr7osAaR+zVmD7x1gmSz ZC8iBtt/CAhvmvaa/B9LZbgUDbulqZeW1OiqteCcqchAWuI7W9I6peaOY Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10767"; a="361842417" X-IronPort-AV: E=Sophos;i="6.01,194,1684825200"; d="scan'208";a="361842417" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jul 2023 08:49:40 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10767"; a="720743949" X-IronPort-AV: E=Sophos;i="6.01,194,1684825200"; d="scan'208";a="720743949" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga002.jf.intel.com with ESMTP; 10 Jul 2023 08:49:29 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 36E48385; Mon, 10 Jul 2023 18:49:34 +0300 (EEST) From: Andy Shevchenko To: Mark Brown , Cristian Ciocaltea , Yang Yingliang , Andy Shevchenko , Amit Kumar Mahapatra via Alsa-devel , Neil Armstrong , Tharun Kumar P , Vijaya Krishna Nivarthi , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-amlogic@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-arm-msm@vger.kernel.org, linux-rockchip@lists.infradead.org, linux-riscv@lists.infradead.org, linux-stm32@st-md-mailman.stormreply.com, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org Cc: Sanjay R Mehta , Radu Pirea , Nicolas Ferre , Alexandre Belloni , Claudiu Beznea , Tudor Ambarus , Serge Semin , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , NXP Linux Team , Kevin Hilman , Jerome Brunet , Martin Blumenstingl , Matthias Brugger , AngeloGioacchino Del Regno , Andy Gross , Bjorn Andersson , Konrad Dybcio , Heiko Stuebner , Palmer Dabbelt , Paul Walmsley , Orson Zhai , Baolin Wang , Chunyan Zhang , Alain Volmat , Maxime Coquelin , Alexandre Torgue , Max Filippov , Steven Rostedt , Masami Hiramatsu , Richard Cochran Subject: [PATCH v2 02/15] spi: Drop duplicate IDR allocation code in spi_register_controller() Date: Mon, 10 Jul 2023 18:49:19 +0300 Message-Id: <20230710154932.68377-3-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.40.0.1.gaa8946217a0b In-Reply-To: <20230710154932.68377-1-andriy.shevchenko@linux.intel.com> References: <20230710154932.68377-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 Refactor spi_register_controller() to drop duplicate IDR allocation. Instead of if-else-if branching use two sequential if:s, which allows to re-use the logic of IDR allocation in all cases. Signed-off-by: Andy Shevchenko --- drivers/spi/spi.c | 50 ++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 8f3282a71c63..6d74218cf38e 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -3081,6 +3081,20 @@ static int spi_controller_check_ops(struct spi_controller *ctlr) return 0; } +/* Allocate dynamic bus number using Linux idr */ +static int spi_controller_id_alloc(struct spi_controller *ctlr, int start, int end) +{ + int id; + + mutex_lock(&board_lock); + id = idr_alloc(&spi_master_idr, ctlr, start, end, GFP_KERNEL); + mutex_unlock(&board_lock); + if (WARN(id < 0, "couldn't get idr")) + return id == -ENOSPC ? -EBUSY : id; + ctlr->bus_num = id; + return 0; +} + /** * spi_register_controller - register SPI master or slave controller * @ctlr: initialized master, originally from spi_alloc_master() or @@ -3108,8 +3122,8 @@ int spi_register_controller(struct spi_controller *ctlr) { struct device *dev = ctlr->dev.parent; struct boardinfo *bi; + int first_dynamic; int status; - int id, first_dynamic; if (!dev) return -ENODEV; @@ -3122,27 +3136,13 @@ int spi_register_controller(struct spi_controller *ctlr) if (status) return status; + if (ctlr->bus_num < 0) + ctlr->bus_num = of_alias_get_id(ctlr->dev.of_node, "spi"); if (ctlr->bus_num >= 0) { /* Devices with a fixed bus num must check-in with the num */ - mutex_lock(&board_lock); - id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, - ctlr->bus_num + 1, GFP_KERNEL); - mutex_unlock(&board_lock); - if (WARN(id < 0, "couldn't get idr")) - return id == -ENOSPC ? -EBUSY : id; - ctlr->bus_num = id; - } else { - /* Allocate dynamic bus number using Linux idr */ - id = of_alias_get_id(ctlr->dev.of_node, "spi"); - if (id >= 0) { - ctlr->bus_num = id; - mutex_lock(&board_lock); - id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, - ctlr->bus_num + 1, GFP_KERNEL); - mutex_unlock(&board_lock); - if (WARN(id < 0, "couldn't get idr")) - return id == -ENOSPC ? -EBUSY : id; - } + status = spi_controller_id_alloc(ctlr, ctlr->bus_num, ctlr->bus_num + 1); + if (status) + return status; } if (ctlr->bus_num < 0) { first_dynamic = of_alias_get_highest_id("spi"); @@ -3151,13 +3151,9 @@ int spi_register_controller(struct spi_controller *ctlr) else first_dynamic++; - mutex_lock(&board_lock); - id = idr_alloc(&spi_master_idr, ctlr, first_dynamic, - 0, GFP_KERNEL); - mutex_unlock(&board_lock); - if (WARN(id < 0, "couldn't get idr")) - return id; - ctlr->bus_num = id; + status = spi_controller_id_alloc(ctlr, first_dynamic, 0); + if (status) + return status; } ctlr->bus_lock_flag = 0; init_completion(&ctlr->xfer_completion); -- 2.40.0.1.gaa8946217a0b