From patchwork Tue Sep 26 11:38:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vaishnav Achath X-Patchwork-Id: 145010 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp2147792vqu; Tue, 26 Sep 2023 12:33:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGdbQRwGCKSAXed2fhSYO3RuY491Hw1j5x5dRu+RnY7V0qFGALPk9Y6UZ0iMowY9WL+wxmN X-Received: by 2002:a17:902:d894:b0:1c0:a5c9:e072 with SMTP id b20-20020a170902d89400b001c0a5c9e072mr8930733plz.11.1695756838866; Tue, 26 Sep 2023 12:33:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695756838; cv=none; d=google.com; s=arc-20160816; b=vZh9GWJig1VgBvBOQ4hqBNyhAfYBOrNkXpwaohI7LjnHNErA1pEkFbF2iixMn6c/0n phkG/cl9r35/BUVCsP5inz7B8DbptEFIdmR8zyR898Y4L5TSiwwEStoM/N76w+vv4d// T8JCLZTUd5W2SZNXNhDNcnHnoc5fII1Tx4y2NZFu1vpReailsMNQ8T9df0Xzv25CQtP3 /MCMjwOE7omcRs3hfKh/B7PLt2TMkKQbVPIsR/svFWqNGIV4ZbxS1O4I9F0nheU6LNvu McXPVwJHsq642Pzz9q5brdPUcXvhjBl1GfTkMx2phaICQzvGeXg6lMmKIAftUQwvrYSy ODow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from :dkim-signature; bh=fSEggdhifP4uqE7eOpaahQ4oqSuxnwMUVKmERVc6X5M=; fh=2EA2EtaZcEGYZEPgzaM+OmMEpPQVbQ3FGGR+yJFJhow=; b=q3ULubW4QFjIENGVIaA7uMBY7P+BB0dWBCDG1QjUaDj7jPG5utBlgnghdkt5a/Hd8L O9NLHKnmqlz/bv954FrVd/TCCibKMzXjzYMzr1xSZRIgDIHVPVLA0+b4+EGAWAgm4r/y zWPgdx2SvECqlXclCYfp+VlHJRpGvrJjtTX7bOT123pYH/XTlOrAygVNsLC9anQuSRij b6/t2ToF8gGRmMLyiGqR4pKEBmDkM7A/zbKwCWU4hBSEQflhSePa9IgoOXIOR/sUpYuT i6KhivngNQRTANeak/Vu4GYZppZuK6ZrpFUsCPxPKM9GD1kKCzeHmdGAAZ6ZbYD4bVPB XV8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=x0s7mxXk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id jz11-20020a170903430b00b001c61512f2a3si5898956plb.362.2023.09.26.12.33.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 12:33:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=x0s7mxXk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 6452B823FAE9; Tue, 26 Sep 2023 04:38:32 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233065AbjIZLic (ORCPT + 27 others); Tue, 26 Sep 2023 07:38:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230231AbjIZLia (ORCPT ); Tue, 26 Sep 2023 07:38:30 -0400 Received: from fllv0015.ext.ti.com (fllv0015.ext.ti.com [198.47.19.141]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 984C3DD; Tue, 26 Sep 2023 04:38:23 -0700 (PDT) Received: from fllv0035.itg.ti.com ([10.64.41.0]) by fllv0015.ext.ti.com (8.15.2/8.15.2) with ESMTP id 38QBcEZ4084315; Tue, 26 Sep 2023 06:38:14 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1695728294; bh=fSEggdhifP4uqE7eOpaahQ4oqSuxnwMUVKmERVc6X5M=; h=From:To:CC:Subject:Date; b=x0s7mxXk99Rk+13HpytZc2ZU3FigovLroiZ4rk+SospAfT9LD36+uhJr/N9Ko6FLx nmc9c4XtOgIznSIKdHWSWkKHaeSXFLx2q8gMjW63ZGva1XkyLAs2a9YMLSuchxorcC JtaZIDpkhVb0UmURPLd1gCUg2R5Dw6Oh3Au+1RoY= Received: from DLEE111.ent.ti.com (dlee111.ent.ti.com [157.170.170.22]) by fllv0035.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 38QBcEmv120727 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 26 Sep 2023 06:38:14 -0500 Received: from DLEE103.ent.ti.com (157.170.170.33) by DLEE111.ent.ti.com (157.170.170.22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23; Tue, 26 Sep 2023 06:38:13 -0500 Received: from lelv0327.itg.ti.com (10.180.67.183) 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.2507.23 via Frontend Transport; Tue, 26 Sep 2023 06:38:13 -0500 Received: from localhost (ileaxei01-snat2.itg.ti.com [10.180.69.6]) by lelv0327.itg.ti.com (8.15.2/8.15.2) with ESMTP id 38QBcC81003293; Tue, 26 Sep 2023 06:38:13 -0500 From: Vaishnav Achath To: , CC: , , , , Subject: [PATCH v2] spi: omap2-mcspi: Fix hardcoded reference clock Date: Tue, 26 Sep 2023 17:08:12 +0530 Message-ID: <20230926113812.30692-1-vaishnav.a@ti.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_PASS,SPF_PASS 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 26 Sep 2023 04:38:32 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778103163946957078 X-GMAIL-MSGID: 1778129923010373879 A hardcoded reference clock of 48 MHz is used to calculate the clock divisor values, but the reference clock frequency can be different across devices and can be configured which can cause a mismatch between the reported frequency and actual SPI clock frequency observed. Fix this by fetching the clock rate from the clock provider and falling back to hardcoded reference only if the clock is not supplied. Fixes: 2cd7d393f461 ("arm64: dts: ti: k3-am654: Add McSPI DT nodes") Signed-off-by: Vaishnav Achath --- V1->V2: * Use u32 instead of unsigned int * rename clk to ref_clk and rebase for latest struct ctlr rename changes. Blamed commit is the first device where the default reference clock was different from the hardcoded value. Tested on TDA4VM SK (6.6.0-rc3-next-20230926) https://gist.github.com/vaishnavachath/7bbc6dd6e02294aed3e8c547cfa039c2 V1 - https://lore.kernel.org/all/20230912100328.31813-1-vaishnav.a@ti.com/ drivers/spi/spi-omap2-mcspi.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/drivers/spi/spi-omap2-mcspi.c b/drivers/spi/spi-omap2-mcspi.c index 79888e6c54c2..ddf1c684bcc7 100644 --- a/drivers/spi/spi-omap2-mcspi.c +++ b/drivers/spi/spi-omap2-mcspi.c @@ -125,10 +125,12 @@ struct omap2_mcspi { struct omap2_mcspi_dma *dma_channels; struct device *dev; struct omap2_mcspi_regs ctx; + struct clk *ref_clk; int fifo_depth; bool target_aborted; unsigned int pin_dir:1; size_t max_xfer_len; + u32 ref_clk_hz; }; struct omap2_mcspi_cs { @@ -880,12 +882,12 @@ omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer) return count - c; } -static u32 omap2_mcspi_calc_divisor(u32 speed_hz) +static u32 omap2_mcspi_calc_divisor(u32 speed_hz, u32 ref_clk_hz) { u32 div; for (div = 0; div < 15; div++) - if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div)) + if (speed_hz >= (ref_clk_hz >> div)) return div; return 15; @@ -897,7 +899,7 @@ static int omap2_mcspi_setup_transfer(struct spi_device *spi, { struct omap2_mcspi_cs *cs = spi->controller_state; struct omap2_mcspi *mcspi; - u32 l = 0, clkd = 0, div, extclk = 0, clkg = 0; + u32 ref_clk_hz, l = 0, clkd = 0, div, extclk = 0, clkg = 0; u8 word_len = spi->bits_per_word; u32 speed_hz = spi->max_speed_hz; @@ -911,14 +913,15 @@ static int omap2_mcspi_setup_transfer(struct spi_device *spi, if (t && t->speed_hz) speed_hz = t->speed_hz; - speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ); - if (speed_hz < (OMAP2_MCSPI_MAX_FREQ / OMAP2_MCSPI_MAX_DIVIDER)) { - clkd = omap2_mcspi_calc_divisor(speed_hz); - speed_hz = OMAP2_MCSPI_MAX_FREQ >> clkd; + ref_clk_hz = mcspi->ref_clk_hz; + speed_hz = min_t(u32, speed_hz, ref_clk_hz); + if (speed_hz < (ref_clk_hz / OMAP2_MCSPI_MAX_DIVIDER)) { + clkd = omap2_mcspi_calc_divisor(speed_hz, ref_clk_hz); + speed_hz = ref_clk_hz >> clkd; clkg = 0; } else { - div = (OMAP2_MCSPI_MAX_FREQ + speed_hz - 1) / speed_hz; - speed_hz = OMAP2_MCSPI_MAX_FREQ / div; + div = (ref_clk_hz + speed_hz - 1) / speed_hz; + speed_hz = ref_clk_hz / div; clkd = (div - 1) & 0xf; extclk = (div - 1) >> 4; clkg = OMAP2_MCSPI_CHCONF_CLKG; @@ -1448,8 +1451,6 @@ static int omap2_mcspi_probe(struct platform_device *pdev) ctlr->cleanup = omap2_mcspi_cleanup; ctlr->target_abort = omap2_mcspi_target_abort; ctlr->dev.of_node = node; - ctlr->max_speed_hz = OMAP2_MCSPI_MAX_FREQ; - ctlr->min_speed_hz = OMAP2_MCSPI_MAX_FREQ >> 15; ctlr->use_gpio_descriptors = true; platform_set_drvdata(pdev, ctlr); @@ -1519,6 +1520,14 @@ static int omap2_mcspi_probe(struct platform_device *pdev) goto free_ctlr; } + mcspi->ref_clk = devm_clk_get_optional_enabled(&pdev->dev, NULL); + if (mcspi->ref_clk) + mcspi->ref_clk_hz = clk_get_rate(mcspi->ref_clk); + else + mcspi->ref_clk_hz = OMAP2_MCSPI_MAX_FREQ; + ctlr->max_speed_hz = mcspi->ref_clk_hz; + ctlr->min_speed_hz = mcspi->ref_clk_hz >> 15; + pm_runtime_use_autosuspend(&pdev->dev); pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT); pm_runtime_enable(&pdev->dev);