From nobody Fri Dec 19 14:38:29 2025 Received: from relmlie5.idc.renesas.com (relmlor1.renesas.com [210.160.252.171]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F0A602F7AB8; Wed, 5 Nov 2025 09:15:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=210.160.252.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762334129; cv=none; b=LpwfTdQ4lyvfvjrInwxUgFbmNXKp00huoBC+5YVrL+W6EkQnFyVbYfs5fCAwd7blLIboCLsAtRbhpBM90bS3zJJuuL4E6OFSUsmwvS3flxlnrcElYj9sZtLV4uPbV3JlTJdNJYUrZnIvXDL/fKXzKnaJ1ouneVt98ZixgNMCSy4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762334129; c=relaxed/simple; bh=tGxHwADi5ufjYK4AqVUI1K6k1hsgUmIJgmE9xlatDSY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KbMoJIjjjIUCdVWkCyzJL+UkPoEwA37EooYThID7qimFVVMLdM0WYCk7QdPrcI2l/IDa0yaipXZ4ob1umrWn8vsw+XezLSQzPGz18t+tFuxDLjnrrkx513akK1u808W5eCzsVLl7fS25lr/MtarnCM30xlzan2zzxs9eMV3ExZk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=renesas.com; spf=pass smtp.mailfrom=renesas.com; arc=none smtp.client-ip=210.160.252.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=renesas.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=renesas.com X-CSE-ConnectionGUID: j3VFttw5SymNXxz9qA41sw== X-CSE-MsgGUID: LVnxNeP0Teu7CsvV6gA68w== Received: from unknown (HELO relmlir6.idc.renesas.com) ([10.200.68.152]) by relmlie5.idc.renesas.com with ESMTP; 05 Nov 2025 18:15:25 +0900 Received: from demon-pc.localdomain (unknown [10.226.92.38]) by relmlir6.idc.renesas.com (Postfix) with ESMTP id E593141763F1; Wed, 5 Nov 2025 18:15:20 +0900 (JST) From: Cosmin Tanislav To: Cc: Fabrizio Castro , Mark Brown , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Geert Uytterhoeven , Magnus Damm , Michael Turquette , Stephen Boyd , Philipp Zabel , linux-spi@vger.kernel.org, linux-renesas-soc@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, Cosmin Tanislav Subject: [PATCH 07/14] spi: rzv2h-rspi: make transfer clock rate finding chip-specific Date: Wed, 5 Nov 2025 11:13:51 +0200 Message-ID: <20251105091401.1462985-8-cosmin-gabriel.tanislav.xa@renesas.com> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20251105091401.1462985-1-cosmin-gabriel.tanislav.xa@renesas.com> References: <20251105091401.1462985-1-cosmin-gabriel.tanislav.xa@renesas.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The Renesas RZ/T2H (R9A09G077) and RZ/N2H (R9A09G087) SoCs have a more complicated clocking setup for the SPI transfer clock than RZ/V2H, as the clock from which it is generated supports multiple dividers. To prepare for adding support for these SoCs, split out the logic for finding the SPR and BRDV for a fixed clock into rzv2h_rspi_find_rate_fixed(), and add and use a .find_tclk_rate() callback into the chip-specific structure. Signed-off-by: Cosmin Tanislav --- drivers/spi/spi-rzv2h-rspi.c | 62 ++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/drivers/spi/spi-rzv2h-rspi.c b/drivers/spi/spi-rzv2h-rspi.c index d7719f3c7b13..f59bcadf5e38 100644 --- a/drivers/spi/spi-rzv2h-rspi.c +++ b/drivers/spi/spi-rzv2h-rspi.c @@ -67,7 +67,18 @@ =20 #define RSPI_RESET_NUM 2 =20 +struct rzv2h_rspi_best_clock { + struct clk *clk; + unsigned long clk_rate; + unsigned long error; + u32 actual_hz; + u8 brdv; + u8 spr; +}; + struct rzv2h_rspi_info { + void (*find_tclk_rate)(struct clk *clk, u32 hz, u8 spr_min, u8 spr_max, + struct rzv2h_rspi_best_clock *best_clk); const char *tclk_name; unsigned int fifo_size; unsigned int num_clks; @@ -240,9 +251,13 @@ static inline u32 rzv2h_rspi_calc_bitrate(unsigned lon= g tclk_rate, u8 spr, return DIV_ROUND_UP(tclk_rate, (2 * (spr + 1) * (1 << brdv))); } =20 -static u32 rzv2h_rspi_setup_clock(struct rzv2h_rspi_priv *rspi, u32 hz) +static void rzv2h_rspi_find_rate_fixed(struct clk *clk, u32 hz, + u8 spr_min, u8 spr_max, + struct rzv2h_rspi_best_clock *best) { - unsigned long tclk_rate; + unsigned long clk_rate; + unsigned long error; + u32 actual_hz; int spr; u8 brdv; =20 @@ -255,21 +270,49 @@ static u32 rzv2h_rspi_setup_clock(struct rzv2h_rspi_p= riv *rspi, u32 hz) * * n =3D SPR - is RSPI_SPBR.SPR (from 0 to 255) * * N =3D BRDV - is RSPI_SPCMD.BRDV (from 0 to 3) */ - tclk_rate =3D clk_get_rate(rspi->tclk); + clk_rate =3D clk_get_rate(clk); for (brdv =3D RSPI_SPCMD_BRDV_MIN; brdv <=3D RSPI_SPCMD_BRDV_MAX; brdv++)= { - spr =3D DIV_ROUND_UP(tclk_rate, hz * (1 << (brdv + 1))); + spr =3D DIV_ROUND_UP(clk_rate, hz * (1 << (brdv + 1))); spr--; - if (spr >=3D RSPI_SPBR_SPR_MIN && spr <=3D RSPI_SPBR_SPR_MAX) + if (spr >=3D spr_min && spr <=3D spr_max) goto clock_found; } =20 - return 0; + return; =20 clock_found: - rspi->spr =3D spr; - rspi->brdv =3D brdv; + actual_hz =3D rzv2h_rspi_calc_bitrate(clk_rate, spr, brdv); + error =3D abs((long)hz - (long)actual_hz); =20 - return rzv2h_rspi_calc_bitrate(tclk_rate, spr, brdv); + if (error >=3D best->error) + return; + + *best =3D (struct rzv2h_rspi_best_clock) { + .clk =3D clk, + .clk_rate =3D clk_rate, + .error =3D error, + .actual_hz =3D actual_hz, + .brdv =3D brdv, + .spr =3D spr, + }; +} + +static u32 rzv2h_rspi_setup_clock(struct rzv2h_rspi_priv *rspi, u32 hz) +{ + struct rzv2h_rspi_best_clock best_clock =3D { + .error =3D ULONG_MAX, + }; + + rspi->info->find_tclk_rate(rspi->tclk, hz, RSPI_SPBR_SPR_MIN, + RSPI_SPBR_SPR_MAX, &best_clock); + + if (!best_clock.clk_rate) + return -EINVAL; + + rspi->spr =3D best_clock.spr; + rspi->brdv =3D best_clock.brdv; + + return best_clock.actual_hz; } =20 static int rzv2h_rspi_prepare_message(struct spi_controller *ctlr, @@ -463,6 +506,7 @@ static void rzv2h_rspi_remove(struct platform_device *p= dev) } =20 static const struct rzv2h_rspi_info rzv2h_info =3D { + .find_tclk_rate =3D rzv2h_rspi_find_rate_fixed, .tclk_name =3D "tclk", .fifo_size =3D 16, .num_clks =3D 3, --=20 2.51.2