From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.43 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796062392; b=nFt3TKqoSCerPwxnQWsj1eAXrAg2ZsqpOyu5oPOcFdU+aL9YYF++KpjTKPqDv+zB18tj hWl8NN/aScuu+pE3XlVERgxtdFeEqx2HL1vnJNQK+KYrygkbDibV2WoYYwFPiD2sGfmu4 sOpZo12Ot8aOYzo62Yjc+OwPqeDNm+sicNLnFh7KsRA81rwZPYcue2J9ezac6tyLhBTTI cksaQ1i1ob+6U7hJIBkcRrgvUS8R3huhFlxwxgSRcVi/Rxy4NPOeWCygTMqe6RtfmMRJ2 ZVqMHPRQxQe3jPbYxi5gcKXZmSOzUVfVXN2Pglq5Hf1azI25CSCqewjFfaneLRg02KQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796062392; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=d7oYC3ronuwKE3I44AJd8ReU8uQ1UUHDhKEuu1jF0Nw=; b=nyrIXdGD9WmI/AD14PGPQpSH2PSfpLEDP+AKz0YRAL88l6d1QE6WKuUhSA4fMrro1ie5 AnX5YUlx9xQcy0r/g64qA7kuDuDE2LHp94f0kk1f3j6dxqmhhqVSPRLnU4fgBCkCavEj/ 6xDyL7zlGCkQsKj5AnfXA/HDGaBgeH6TtqR0nuF/tIKGJQi4mrz6EvaWDXRn3poAhUy7v x0N4/dCKzQrmKH6Xlx1F7rNEeh7QG6OyUPGP0FPoUiLSrMU+RAur42mryn9nPZajOEOIM 5e6j8m7lRKnbK/GhXIaH1RXtKmB6Crs5gpeCvwdLOX/xskbN6JVYbKjIWJD8pQqswKw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=bw9znkpb; arc=none smtp.remote-ip=209.85.128.43 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 61325606A682 for ; Mon, 30 Nov 2026 18:13:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 61325606A682 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=bw9znkpb Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-4bb64ef5a14so102890195e9.1 for ; Mon, 30 Nov 2026 10:13:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796062391; x=1796667191; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=d7oYC3ronuwKE3I44AJd8ReU8uQ1UUHDhKEuu1jF0Nw=; b=bw9znkpbmlU5KIvK288ytnZ+bHkq2bCHFpT7vmisr/TS2E7DcxfF6ZwTFlmHAlreVL BeTx3n/3asoBW2ZeiQmy5ZPF4tEljI83DRNrFgF8Kw0pxnVy2Qbo9+e/Ufzup8boAyDF ZiBEoyxqmxE4iY86o/nXzDx3UtHIMu26qO4nQ2PMCdcCCeKFJ5ekZm1afHI2HVIrFMD7 1PcjmpnvqsrCNC/drCoRs+bLCCtxLKKyQWw7uDRZfSfp8s1DFOPetsl+svJsffXzeG4u Ookj8MKjFUe5JLDwd22xC5Mt5hMM7pxYLKV7yDll7pbeFg+Ah5eWcTp5YO2jCKP81qRs 3Ohw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796062391; x=1796667191; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=d7oYC3ronuwKE3I44AJd8ReU8uQ1UUHDhKEuu1jF0Nw=; b=NRmNe1X3EYxCMPoi4Y0gdsBKH9LbBYfnitKJJhK3nHd9gzYR9yYNUHBdCKDPJjy+sM ENj6/pS6dHJdyBfbteDtmf2kxpdsKGIWw7DveB4EDUltZNcBrRnHx68mxw4QkjUbwKhh 5SkI7rq1TZRcvly80crkt3+uDNORASWv3HpcVpe9Drp6pAVlmlN4vwFTjhPK8p7fMRWa YnoOaI0zxljgN8n8FoI+3kkqMGDDk5WFBgPKnbSrtWz8e53406MWnqLZ00DLdTcTlDme +2jtJh45KON6gDJXl+Hewh75HoKHQxMu1Ib3GsxZA0ehBZW30OmFoiA5Getf7zZ/zhaV Yyhw== X-Forwarded-Encrypted: i=1; AL+hErGrykH2ovVF9WV0HuVUvMa9wULvZEriuoC6iarPHTIsbfU9ESk3LtRGc690AbbYR48fK3Y0X2Y+Xb0=@vger.kernel.org X-Gm-Message-State: APuU4Z+2TipH/VVrxct+30mvHwr9WLsJTNml/AXWTgPuFSTMTCcMHaWp OS+EQS4dD1Adueb8G5qyOGpfXn7zYTc0MLqbyo2zmGk7r3Fq7qyvNIom X-Gm-Gg: AZFdTZfOmHPG3IGWhLnxImRNjiQOaE8U+86joNUyH/PiCLyUGFcAnSGKdvGtcFWcjEp Zo31dNlALpQvQ0rEepWNjYojheMeed1Ji+Kg+1pei5jfI3bN2JAnMQdbMM+oYEfuoDAmwOezUbr HczDJynBcGW40EC1jRq9sEVIZakpa4h2tIi3iRLDQ3JYBqw9kmLYoTuxLlprleL104watcZw6ah eM7ID6qMVaGRt4lJsRtETImIGcMPTiy+qsVN4/UJwgWLRaPfPf4JZCy3Q16pt5dxX39li5lOrwD HL/X7LSjrUY1OXFyOUSAtBumHt4QoBSEnUlGeBGOAMbD4EXh+xN8W98VyznJidB8+8FhCrjk1sJ GurIIgpJs/DYkaC6C6nkEr5M44gtjzIuxJeVGLpTZCyiKMbMnWY24thyO+nlWvFG1T5D0y323Iq BSy8YJveUfCBvAKugkkq4B/o+2KR//s0U8CYlnnHIs1zUWcfDrVjXRJd0k6m+oUl8o9uUIJdk7B R7xWz3+qpZTnxiDvpN7darXgk7kHv+j9mJC7XbhIrm9z4elr+HoZcXnFqOr1rUgYoXYlplaE/vv AoX3wqySaEFpWTOByWTe7WnOQFaWHhwhb3n5w0bHksVbf8Z/Wvbj6MuJ0eyZr69FM0PgEbXYaK8 vMmP1oYJb7J19Y2FYVFozlgArpp8meo7zHPdfOdv2gDOLFg6c+v4LI98= X-Received: by 2002:a05:600c:5493:b0:4a2:8c5:5e5 with SMTP id 5b1f17b1804b1-4bb8881c734mr750557635e9.7.1796062390584; Mon, 30 Nov 2026 10:13:10 -0800 (PST) Received: from Ansuel-XPS24.localdomain (93-34-92-103.ip49.fastwebnet.it. [93.34.92.103]) by smtp.googlemail.com with ESMTPSA id 5b1f17b1804b1-4bb884b39b6sm881685715e9.12.2026.11.30.10.13.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 10:13:10 -0800 (PST) From: Christian Marangi To: Stephen Boyd , Brian Masney , Jerome Brunet , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Philipp Zabel , Felix Fietkau , linux-clk@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Christian Marangi Subject: [PATCH v17 6/8] clk: en7523: add support for .set_rate for en7581 Date: Mon, 30 Nov 2026 19:12:51 +0100 Message-ID: <20261130181255.693022-7-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261130181255.693022-1-ansuelsmth@gmail.com> References: <20261130181255.693022-1-ansuelsmth@gmail.com> X-Mailing-List: linux-clk@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add support for EN7523 driver to configure rate for Airoha EN7581 SoC. The SoC expose both base clock selector and clock divisor hence it's possible to change the rate. This will be especially needed for new SoC AN7583 that require changes for the MDIO and the eMMC. The clock were assumed correctly configured by the bootloader but this goes against the rule of "kernel should not depend on external configuration". To support the use of standard clock divider API, clock_div_table are added where needed (BUS, SPI and SLIC) in additionl to the previous info to derive and simulate the clock from the divisor registers. Recalc rate for Airoha EN7581 is updated to use the divider_recalc_rate API for consistency with .set_rate and .determine_rate. en7523_get_div() is updated to use the div table to drop redundant values. Signed-off-by: Christian Marangi --- drivers/clk/clk-en7523.c | 344 +++++++++++++++++++++++++++++++++++---- 1 file changed, 310 insertions(+), 34 deletions(-) diff --git a/drivers/clk/clk-en7523.c b/drivers/clk/clk-en7523.c index 20b387bd4e64..59eb41f9fa80 100644 --- a/drivers/clk/clk-en7523.c +++ b/drivers/clk/clk-en7523.c @@ -116,9 +116,7 @@ struct en_clk_desc { u16 div_reg; u8 div_bits; u8 div_shift; - u16 div_val0; - u8 div_step; - u8 div_offset; + const struct clk_div_table *div_table; }; struct en_clk_gate { @@ -162,7 +160,88 @@ struct en_clk_soc_data { static const u32 gsw_base[] = { 400000000, 500000000 }; static const u32 emi_base[] = { 333000000, 400000000 }; static const u32 bus_base[] = { 500000000, 540000000 }; +/* From documentation divisor 1 is n/a */ +static const struct clk_div_table bus_div[] = { + { .val = 1, .div = 2 }, + { .val = 2, .div = 3 }, + { .val = 3, .div = 4 }, + { .val = 4, .div = 5 }, + { .val = 5, .div = 6 }, + { .val = 6, .div = 7 }, + { .val = 7, .div = 8 }, + { /* sentinel */ } +}; static const u32 slic_base[] = { 100000000, 3125000 }; +static const struct clk_div_table slic_div[] = { + { .val = 0, .div = 20 }, + { .val = 1, .div = 2 }, + { .val = 2, .div = 4 }, + { .val = 3, .div = 6 }, + { .val = 4, .div = 8 }, + { .val = 5, .div = 10 }, + { .val = 6, .div = 12 }, + { .val = 7, .div = 14 }, + { .val = 8, .div = 16 }, + { .val = 9, .div = 18 }, + { .val = 10, .div = 20 }, + { .val = 11, .div = 22 }, + { .val = 12, .div = 24 }, + { .val = 13, .div = 26 }, + { .val = 14, .div = 28 }, + { .val = 15, .div = 30 }, + { .val = 16, .div = 32 }, + { .val = 17, .div = 34 }, + { .val = 18, .div = 36 }, + { .val = 19, .div = 38 }, + { .val = 20, .div = 40 }, + { .val = 21, .div = 42 }, + { .val = 22, .div = 44 }, + { .val = 23, .div = 46 }, + { .val = 24, .div = 48 }, + { .val = 25, .div = 50 }, + { .val = 26, .div = 52 }, + { .val = 27, .div = 54 }, + { .val = 28, .div = 56 }, + { .val = 29, .div = 58 }, + { .val = 30, .div = 60 }, + { .val = 31, .div = 62 }, + { /* sentinel */ } +}; +static const struct clk_div_table spi_div[] = { + { .val = 0, .div = 40 }, + { .val = 1, .div = 2 }, + { .val = 2, .div = 4 }, + { .val = 3, .div = 6 }, + { .val = 4, .div = 8 }, + { .val = 5, .div = 10 }, + { .val = 6, .div = 12 }, + { .val = 7, .div = 14 }, + { .val = 8, .div = 16 }, + { .val = 9, .div = 18 }, + { .val = 10, .div = 20 }, + { .val = 11, .div = 22 }, + { .val = 12, .div = 24 }, + { .val = 13, .div = 26 }, + { .val = 14, .div = 28 }, + { .val = 15, .div = 30 }, + { .val = 16, .div = 32 }, + { .val = 17, .div = 34 }, + { .val = 18, .div = 36 }, + { .val = 19, .div = 38 }, + { .val = 20, .div = 40 }, + { .val = 21, .div = 42 }, + { .val = 22, .div = 44 }, + { .val = 23, .div = 46 }, + { .val = 24, .div = 48 }, + { .val = 25, .div = 50 }, + { .val = 26, .div = 52 }, + { .val = 27, .div = 54 }, + { .val = 28, .div = 56 }, + { .val = 29, .div = 58 }, + { .val = 30, .div = 60 }, + { .val = 31, .div = 62 }, + { /* sentinel */ } +}; static const u32 npu_base[] = { 333000000, 400000000, 500000000 }; /* EN7581 */ static const u32 emi7581_base[] = { 540000000, 480000000, 400000000, 300000000 }; @@ -186,8 +265,6 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_EMI, .name = "emi", @@ -200,8 +277,6 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_BUS, .name = "bus", @@ -214,8 +289,7 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, + .div_table = bus_div, }, { .id = EN7523_CLK_SLIC, .name = "slic", @@ -229,8 +303,7 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_reg = REG_SPI_CLK_DIV_SEL, .div_bits = 5, .div_shift = 24, - .div_val0 = 20, - .div_step = 2, + .div_table = slic_div, }, { .id = EN7523_CLK_SPI, .name = "spi", @@ -241,8 +314,7 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 5, .div_shift = 8, - .div_val0 = 40, - .div_step = 2, + .div_table = spi_div, }, { .id = EN7523_CLK_NPU, .name = "npu", @@ -255,8 +327,6 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_CRYPTO, .name = "crypto", @@ -282,8 +352,6 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_EMI, .name = "emi", @@ -296,8 +364,6 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_BUS, .name = "bus", @@ -310,8 +376,7 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, + .div_table = bus_div, }, { .id = EN7523_CLK_SLIC, .name = "slic", @@ -325,8 +390,7 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_reg = REG_SPI_CLK_DIV_SEL, .div_bits = 5, .div_shift = 24, - .div_val0 = 20, - .div_step = 2, + .div_table = slic_div, }, { .id = EN7523_CLK_SPI, .name = "spi", @@ -337,8 +401,7 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 5, .div_shift = 8, - .div_val0 = 40, - .div_step = 2, + .div_table = spi_div, }, { .id = EN7523_CLK_NPU, .name = "npu", @@ -351,8 +414,6 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_CRYPTO, .name = "crypto", @@ -569,16 +630,20 @@ static u32 en7523_get_base_rate(const struct en_clk_desc *desc, u32 val) static u32 en7523_get_div(const struct en_clk_desc *desc, u32 val) { - if (!desc->div_bits) - return 1; + const struct clk_div_table *clkt; val >>= desc->div_shift; val &= (1 << desc->div_bits) - 1; - if (!val && desc->div_val0) - return desc->div_val0; + if (desc->div_table) { + for (clkt = desc->div_table; clkt->div; clkt++) + if (clkt->val == val) + return clkt->div; - return (val + desc->div_offset) * desc->div_step; + return 0; + } + + return val + 1; } static int en7523_pci_is_enabled(struct clk_hw *hw) @@ -765,10 +830,21 @@ static void en7523_register_clocks(struct device *dev, struct clk_hw_onecell_dat const struct en_clk_desc *desc = &en7523_base_clks[i]; u32 reg = desc->div_reg ? desc->div_reg : desc->base_reg; u32 val = readl(base + desc->base_reg); + u32 div; rate = en7523_get_base_rate(desc, val); + val = readl(base + reg); - rate /= en7523_get_div(desc, val); + if (desc->div_bits) { + div = en7523_get_div(desc, val); + if (!div) { + dev_err(dev, "Invalid div val for clk %s: %x\n", + desc->name, val); + continue; + } + + rate /= div; + } hw = devm_clk_hw_register_fixed_rate(dev, desc->name, NULL, 0, rate); if (IS_ERR(hw)) { @@ -831,11 +907,211 @@ static unsigned long en7581_recalc_rate(struct clk_hw *hw, return 0; } - return rate / en7523_get_div(desc, val); + if (desc->div_bits) { + val >>= desc->div_shift; + val &= (1 << desc->div_bits) - 1; + + rate = divider_recalc_rate(hw, rate, val, desc->div_table, + 0, desc->div_bits); + } + + return rate; +} + +/* + * Simulate rate with every divisor supported + * 1. Search the divisor for the requested rate + * 2. Simulate the final rate with the new divisor + */ +static unsigned long en7581_get_div_for_rate(struct clk_hw *hw, + unsigned long rate, + unsigned long parent_rate, + const struct en_clk_desc *desc, + int *div_val) +{ + *div_val = divider_get_val(rate, parent_rate, desc->div_table, + desc->div_bits, 0); + if (*div_val < 0) + return 0; + + return divider_recalc_rate(hw, parent_rate, *div_val, + desc->div_table, 0, + desc->div_bits); +} + +static int en7581_get_vals_for_rate(struct clk_hw *hw, + const struct en_clk_desc *desc, + unsigned long rate, + u32 *base_val, u32 *div_val) +{ + struct device *dev = clk_hw_get_dev(hw); + int tmp_base_val = 0, tmp_div_val = 0; + unsigned long parent_rate, tmp_rate; + + if (!rate || (!desc->base_bits && !desc->div_bits)) + return -EINVAL; + + if (!desc->base_bits) { + parent_rate = desc->base_value; + + tmp_rate = en7581_get_div_for_rate(hw, rate, parent_rate, + desc, &tmp_div_val); + + /* Matching clock found */ + if (tmp_rate == rate) + goto exit; + } + + /* Search requested rate with every matching base rate + divisor */ + for (; tmp_base_val < desc->n_base_values; tmp_base_val++) { + parent_rate = desc->base_values[tmp_base_val]; + tmp_rate = parent_rate; + + if (desc->div_bits) + tmp_rate = en7581_get_div_for_rate(hw, rate, parent_rate, + desc, &tmp_div_val); + + /* Matching clock found */ + if (tmp_rate == rate) + break; + } + + /* Impossible, should be validated by determine rate */ + if (tmp_base_val >= desc->n_base_values) { + dev_dbg(dev, "Invalid rate for clock %s\n", + desc->name); + return -EINVAL; + } + +exit: + *base_val = tmp_base_val; + *div_val = tmp_div_val; + + return 0; +} + +static void en7581_update_best_rate(struct clk_rate_request *req, + unsigned long requested_rate, + unsigned long tmp_rate, + unsigned long *best_diff) +{ + unsigned long diff; + + if (!tmp_rate || tmp_rate < req->min_rate || + tmp_rate > req->max_rate) + return; + + diff = abs_diff(requested_rate, tmp_rate); + if (diff < *best_diff) { + *best_diff = diff; + req->rate = tmp_rate; + } +} + +static void en7581_find_best_rate(struct clk_hw *hw, + struct clk_rate_request *req, + const struct en_clk_desc *desc, + unsigned long parent_rate, + unsigned long requested_rate, + unsigned long *best_diff) +{ + struct clk_rate_request div_req = *req; + int ret; + + if (!desc->div_bits) { + en7581_update_best_rate(req, requested_rate, parent_rate, + best_diff); + return; + } + + /* Treat each internal base rate as a fixed parent for the divider. */ + div_req.rate = clamp(requested_rate, req->min_rate, req->max_rate); + div_req.rate = min(div_req.rate, parent_rate); + div_req.best_parent_rate = parent_rate; + ret = divider_determine_rate(hw, &div_req, desc->div_table, + desc->div_bits, + CLK_DIVIDER_ROUND_CLOSEST); + if (ret) + return; + + en7581_update_best_rate(req, requested_rate, div_req.rate, best_diff); +} + +static int en7581_determine_rate(struct clk_hw *hw, + struct clk_rate_request *req) +{ + struct en_clk *en_clk = container_of(hw, struct en_clk, hw); + const struct en_clk_desc *desc = en_clk->desc; + unsigned long best_diff = ULONG_MAX; + unsigned long rate = req->rate; + int tmp_base_val = 0; + + if (!rate || (!desc->base_bits && !desc->div_bits)) + return -EINVAL; + + if (!desc->base_bits) { + en7581_find_best_rate(hw, req, desc, desc->base_value, + rate, &best_diff); + goto exit; + } + + for (; tmp_base_val < desc->n_base_values; tmp_base_val++) + en7581_find_best_rate(hw, req, desc, + desc->base_values[tmp_base_val], + rate, &best_diff); + +exit: + return best_diff == ULONG_MAX ? -EINVAL : 0; +} + +static int en7581_set_rate(struct clk_hw *hw, unsigned long rate, + unsigned long parent_rate) +{ + struct en_clk *en_clk = container_of(hw, struct en_clk, hw); + const struct en_clk_desc *desc = en_clk->desc; + struct device *dev = clk_hw_get_dev(hw); + u32 base_val, div_val, reg, val, mask; + struct regmap *map = en_clk->map; + int err; + + err = en7581_get_vals_for_rate(hw, en_clk->desc, rate, + &base_val, &div_val); + if (err) + return err; + + if (desc->div_bits) { + reg = desc->div_reg ? desc->div_reg : desc->base_reg; + + mask = (BIT(desc->div_bits) - 1) << desc->div_shift; + val = div_val << desc->div_shift; + + err = regmap_update_bits(map, reg, mask, val); + if (err) { + dev_dbg(dev, "Failed to update div reg for clock %s\n", + desc->name); + return err; + } + } + + if (desc->base_bits) { + mask = (BIT(desc->base_bits) - 1) << desc->base_shift; + val = base_val << desc->base_shift; + + err = regmap_update_bits(map, desc->base_reg, mask, val); + if (err) { + dev_dbg(dev, "Failed to update reg for clock %s\n", + desc->name); + return err; + } + } + + return 0; } static const struct clk_ops en7581_clk_ops = { .recalc_rate = en7581_recalc_rate, + .determine_rate = en7581_determine_rate, + .set_rate = en7581_set_rate, }; static int en7581_register_clocks(struct device *dev, struct clk_hw_onecell_data *clk_data, -- 2.55.0