From patchwork Fri Jul 14 09:17:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 703531 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DBF87EB64DA for ; Fri, 14 Jul 2023 09:18:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235807AbjGNJSW (ORCPT ); Fri, 14 Jul 2023 05:18:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235800AbjGNJSV (ORCPT ); Fri, 14 Jul 2023 05:18:21 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 281392D63; Fri, 14 Jul 2023 02:18:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1689326298; x=1720862298; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=f2TaQRe8Y+T7dRECaJ0Jn57KTug3LWzKNLUk11W0vjM=; b=bvBqOUEMuGPpdYbBheo1pvCUJUqhxhcA1ZqYSIeiXw257tznfOKS7Mp5 uIIU/jW4eDokT+P5kBKJuaWt7npUmYD5e0mnx7Vp26P5ADBYGg4X8rh1/ yB68ulzzTR+3sn/l0acb47iuQkeAA+Rok2TWy+dOhceUX8AgCYcDssx/N IpS1pZvlUB3YTCFWAMSvvKYVfkpUW4OmIiH05lJhp3mTeUr2FnSpinGLf OhLyBWpvBr/pJ2Rjy8MtGG9dbaORihu/rBukSLaarPluZ4sBvWWDMNO93 MkBYxGJ72Yqyo2mSvuJ/56UqNywPkdiZvKrntOolcRA+Trqm4rZKe4Czs w==; X-IronPort-AV: E=McAfee;i="6600,9927,10770"; a="365469001" X-IronPort-AV: E=Sophos;i="6.01,204,1684825200"; d="scan'208";a="365469001" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Jul 2023 02:17:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10770"; a="716281743" X-IronPort-AV: E=Sophos;i="6.01,204,1684825200"; d="scan'208";a="716281743" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga007.jf.intel.com with ESMTP; 14 Jul 2023 02:17:44 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 8421F241; Fri, 14 Jul 2023 12:17:50 +0300 (EEST) From: Andy Shevchenko To: Mark Brown , Andy Shevchenko , linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Sebastian Reichel Subject: [PATCH v4 1/4] spi: Remove code duplication in spi_add_device*() Date: Fri, 14 Jul 2023 12:17:45 +0300 Message-Id: <20230714091748.89681-2-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.40.0.1.gaa8946217a0b In-Reply-To: <20230714091748.89681-1-andriy.shevchenko@linux.intel.com> References: <20230714091748.89681-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org The commit 0c79378c0199 ("spi: add ancillary device support") added a dozen of duplicating lines of code. We may move them to the __spi_add_device(). Note, that the code may be called under the mutex. Signed-off-by: Andy Shevchenko Reviewed-by: Sebastian Reichel --- drivers/spi/spi.c | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index ae2693ba1744..8e70f4183e62 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -631,6 +631,16 @@ static int __spi_add_device(struct spi_device *spi) struct device *dev = ctlr->dev.parent; int status; + /* Chipselects are numbered 0..max; validate. */ + if (spi_get_chipselect(spi, 0) >= ctlr->num_chipselect) { + dev_err(dev, "cs%d >= max %d\n", spi_get_chipselect(spi, 0), + ctlr->num_chipselect); + return -EINVAL; + } + + /* Set the bus ID string */ + spi_dev_set_name(spi); + /* * We need to make sure there's no other device with this * chipselect **BEFORE** we call setup(), else we'll trash @@ -689,19 +699,8 @@ static int __spi_add_device(struct spi_device *spi) int spi_add_device(struct spi_device *spi) { struct spi_controller *ctlr = spi->controller; - struct device *dev = ctlr->dev.parent; int status; - /* Chipselects are numbered 0..max; validate. */ - if (spi_get_chipselect(spi, 0) >= ctlr->num_chipselect) { - dev_err(dev, "cs%d >= max %d\n", spi_get_chipselect(spi, 0), - ctlr->num_chipselect); - return -EINVAL; - } - - /* Set the bus ID string */ - spi_dev_set_name(spi); - mutex_lock(&ctlr->add_lock); status = __spi_add_device(spi); mutex_unlock(&ctlr->add_lock); @@ -712,17 +711,6 @@ EXPORT_SYMBOL_GPL(spi_add_device); static int spi_add_device_locked(struct spi_device *spi) { struct spi_controller *ctlr = spi->controller; - struct device *dev = ctlr->dev.parent; - - /* Chipselects are numbered 0..max; validate. */ - if (spi_get_chipselect(spi, 0) >= ctlr->num_chipselect) { - dev_err(dev, "cs%d >= max %d\n", spi_get_chipselect(spi, 0), - ctlr->num_chipselect); - return -EINVAL; - } - - /* Set the bus ID string */ - spi_dev_set_name(spi); WARN_ON(!mutex_is_locked(&ctlr->add_lock)); return __spi_add_device(spi);