From patchwork Fri Jun 16 13:59:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 109155 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1367459vqr; Fri, 16 Jun 2023 07:05:17 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4mIKf6a4HYGXqe77rzWWQXLlYJSCYTbiF4QuTxm30at+oCPx6v0ODxXa727tom+wLjDTXn X-Received: by 2002:a17:90a:a88c:b0:25e:884d:6400 with SMTP id h12-20020a17090aa88c00b0025e884d6400mr1646825pjq.42.1686924316786; Fri, 16 Jun 2023 07:05:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686924316; cv=none; d=google.com; s=arc-20160816; b=d2bkMmSNrYWIp/GNBFz0jQjBIsUn0jrVepIB0sV6oFmN3XIhMiqWLJN2Brnwju1wSM x7ooQTXX40egle32/2/PUuMPHbaAs9MOHHM4dbwHdjfqtq89bH1bltfqDNMkMNWrycs1 yR5E/lNovLvBenPxGRSoM9mXOk5Xird5JdsM+qS0+HLVq5zdwAVzT+zmf6OinH0JGBrf /IGj9iaDk47ViX1FcTc1huakRMTJpTStYcCyB0rEnqyJWnTNuxVQa8IAfyM0bzBngALl PGh3KO523/p7sHptJc8iCPidOVCS7kNIKmoJRJ6kBIRR9IHHhWWqLE+u3izMsaU1qLIC LvuA== 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=Bz64Ao9OEbX244FYZQ0IURUQKd6YNAvxts7E2tyetjY=; b=B4sUIGFueBjW6++XU9ZKjq2+SjvyoG47+O94Tv+ItdFL1z7QhB+l0kiR805qYY7i82 p7dVyKb2YeVqq31HrCsN7ijoQPg6HVR38HlFMGb3Zj/xy675b0e10FMP/7kvP73zTeJt s1br7Swqc2wfwH2gKbIZ1qP8ZdlSCN36FgYY43uKNNVZq1FDhr9H70V7dwI1H2kvFVGw bO401PBhf1ouLgewQJ5DLTxNIUJI3EWqJ8FLddg6IyASg/RVxESGMXvW4IH/H7hmYxxR nOHMN80IcxPncYNJQQVteh9hTmdRJfNmMqhYmDZBTMuA3sgbzfevISMTMgKJcfdg7Eic WeZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@ideasonboard.com header.s=mail header.b=nc2C73sy; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gk10-20020a17090b118a00b0024ef1d1151bsi1685539pjb.17.2023.06.16.07.05.02; Fri, 16 Jun 2023 07:05:16 -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 (test mode) header.i=@ideasonboard.com header.s=mail header.b=nc2C73sy; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234709AbjFPOBe (ORCPT + 99 others); Fri, 16 Jun 2023 10:01:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345493AbjFPOBR (ORCPT ); Fri, 16 Jun 2023 10:01:17 -0400 Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0DBC3C1B; Fri, 16 Jun 2023 07:00:41 -0700 (PDT) Received: from desky.lan (91-154-35-171.elisa-laajakaista.fi [91.154.35.171]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 03E751288; Fri, 16 Jun 2023 16:00:04 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1686924006; bh=yiiSk0ohte72gCz1Vi3cjnyefBLR0mWfSt3DWEVwmbc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nc2C73syYspyRLFR+Rd6kXPCxlKjBlgkuKmohkVYFVNgy7t/1QuMip+Vxw1Hx+F6j NPfLb5/BWOYQKv/lVElTZxPZJ1u1DidD+CYJwtWYu0TGr0fh9BkMHWsziAlUJRdVtv lzUlbLjwuEEIbkpNvZbWPlMSqVRdHPrCm9h0NL/s= From: Tomi Valkeinen To: linux-media@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-i2c@vger.kernel.org, Luca Ceresoli , Andy Shevchenko , Matti Vaittinen , Laurent Pinchart , Sakari Ailus Cc: Wolfram Sang , Rob Herring , Krzysztof Kozlowski , Mauro Carvalho Chehab , Peter Rosin , Liam Girdwood , Mark Brown , Michael Tretter , Hans Verkuil , Mike Pagano , =?utf-8?q?Krzysztof_Ha=C5=82asa?= , Marek Vasut , Satish Nagireddy , Tomi Valkeinen Subject: [PATCH v14 17/18] media: i2c: ds90ub953: Restructure clkout management Date: Fri, 16 Jun 2023 16:59:21 +0300 Message-Id: <20230616135922.442979-18-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230616135922.442979-1-tomi.valkeinen@ideasonboard.com> References: <20230616135922.442979-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768868352441065915?= X-GMAIL-MSGID: =?utf-8?q?1768868352441065915?= Separate clkout calculations and register writes into two functions: ub953_calc_clkout_params and ub953_write_clkout_regs, and add a struct ub953_clkout_data that is used to store the clkout parameters. This simplifies the clkout management. Signed-off-by: Tomi Valkeinen --- drivers/media/i2c/ds90ub953.c | 135 ++++++++++++++++++---------------- 1 file changed, 70 insertions(+), 65 deletions(-) diff --git a/drivers/media/i2c/ds90ub953.c b/drivers/media/i2c/ds90ub953.c index f2e950a00d64..ff55740965eb 100644 --- a/drivers/media/i2c/ds90ub953.c +++ b/drivers/media/i2c/ds90ub953.c @@ -131,6 +131,13 @@ struct ub953_hw_data { bool is_ub971; }; +struct ub953_clkout_data { + u32 hs_div; + u32 m; + u32 n; + unsigned long rate; +}; + struct ub953_data { const struct ub953_hw_data *hw_data; @@ -906,6 +913,58 @@ static unsigned long ub953_calc_clkout_ub971(struct ub953_data *priv, return res; } +static void ub953_calc_clkout_params(struct ub953_data *priv, + unsigned long target_rate, + struct ub953_clkout_data *clkout_data) +{ + struct device *dev = &priv->client->dev; + unsigned long clkout_rate; + u64 fc_rate; + + fc_rate = ub953_get_fc_rate(priv); + + if (priv->hw_data->is_ub971) { + u8 m, n; + + clkout_rate = ub953_calc_clkout_ub971(priv, target_rate, + fc_rate, &m, &n); + + clkout_data->m = m; + clkout_data->n = n; + + dev_dbg(dev, "%s %llu * %u / (8 * %u) = %lu (requested %lu)", + __func__, fc_rate, m, n, clkout_rate, target_rate); + } else { + u8 hs_div, m, n; + + clkout_rate = ub953_calc_clkout_ub953(priv, target_rate, + fc_rate, &hs_div, &m, &n); + + clkout_data->hs_div = hs_div; + clkout_data->m = m; + clkout_data->n = n; + + dev_dbg(dev, "%s %llu / %u * %u / %u = %lu (requested %lu)", + __func__, fc_rate, hs_div, m, n, clkout_rate, + target_rate); + } + + clkout_data->rate = clkout_rate; +} + +static void ub953_write_clkout_regs(struct ub953_data *priv, + struct ub953_clkout_data *clkout_data) +{ + if (priv->hw_data->is_ub971) { + ub953_write(priv, UB953_REG_CLKOUT_CTRL0, clkout_data->m); + ub953_write(priv, UB953_REG_CLKOUT_CTRL1, clkout_data->n); + } else { + ub953_write(priv, UB953_REG_CLKOUT_CTRL0, + (__ffs(clkout_data->hs_div) << 5) | clkout_data->m); + ub953_write(priv, UB953_REG_CLKOUT_CTRL1, clkout_data->n); + } +} + static unsigned long ub953_clkout_recalc_rate(struct clk_hw *hw, unsigned long parent_rate) { @@ -965,52 +1024,25 @@ static long ub953_clkout_round_rate(struct clk_hw *hw, unsigned long rate, unsigned long *parent_rate) { struct ub953_data *priv = container_of(hw, struct ub953_data, clkout_clk_hw); - struct device *dev = &priv->client->dev; - unsigned long res; - u64 fc_rate; - u8 hs_div, m, n; - - fc_rate = ub953_get_fc_rate(priv); + struct ub953_clkout_data clkout_data; - if (priv->hw_data->is_ub971) { - res = ub953_calc_clkout_ub971(priv, rate, fc_rate, &m, &n); + ub953_calc_clkout_params(priv, rate, &clkout_data); - dev_dbg(dev, "%s %llu * %u / (8 * %u) = %lu (requested %lu)", - __func__, fc_rate, m, n, res, rate); - } else { - res = ub953_calc_clkout_ub953(priv, rate, fc_rate, &hs_div, &m, &n); - - dev_dbg(dev, "%s %llu / %u * %u / %u = %lu (requested %lu)", - __func__, fc_rate, hs_div, m, n, res, rate); - } - - return res; + return clkout_data.rate; } static int ub953_clkout_set_rate(struct clk_hw *hw, unsigned long rate, unsigned long parent_rate) { struct ub953_data *priv = container_of(hw, struct ub953_data, clkout_clk_hw); - u64 fc_rate; - u8 hs_div, m, n; - unsigned long res; + struct ub953_clkout_data clkout_data; - fc_rate = ub953_get_fc_rate(priv); - - if (priv->hw_data->is_ub971) { - res = ub953_calc_clkout_ub971(priv, rate, fc_rate, &m, &n); - - ub953_write(priv, UB953_REG_CLKOUT_CTRL0, m); - ub953_write(priv, UB953_REG_CLKOUT_CTRL1, n); - } else { - res = ub953_calc_clkout_ub953(priv, rate, fc_rate, &hs_div, &m, &n); + ub953_calc_clkout_params(priv, rate, &clkout_data); - ub953_write(priv, UB953_REG_CLKOUT_CTRL0, (__ffs(hs_div) << 5) | m); - ub953_write(priv, UB953_REG_CLKOUT_CTRL1, n); - } + dev_dbg(&priv->client->dev, "%s %lu (requested %lu)\n", __func__, + clkout_data.rate, rate); - dev_dbg(&priv->client->dev, "%s %lu (requested %lu)\n", __func__, res, - rate); + ub953_write_clkout_regs(priv, &clkout_data); return 0; } @@ -1021,32 +1053,6 @@ static const struct clk_ops ub953_clkout_ops = { .set_rate = ub953_clkout_set_rate, }; -static void ub953_init_clkout_ub953(struct ub953_data *priv) -{ - u64 fc_rate; - u8 hs_div, m, n; - - fc_rate = ub953_get_fc_rate(priv); - - ub953_calc_clkout_ub953(priv, 25000000, fc_rate, &hs_div, &m, &n); - - ub953_write(priv, UB953_REG_CLKOUT_CTRL0, (__ffs(hs_div) << 5) | m); - ub953_write(priv, UB953_REG_CLKOUT_CTRL1, n); -} - -static void ub953_init_clkout_ub971(struct ub953_data *priv) -{ - u64 fc_rate; - u8 m, n; - - fc_rate = ub953_get_fc_rate(priv); - - ub953_calc_clkout_ub971(priv, 25000000, fc_rate, &m, &n); - - ub953_write(priv, UB953_REG_CLKOUT_CTRL0, m); - ub953_write(priv, UB953_REG_CLKOUT_CTRL1, n); -} - static int ub953_register_clkout(struct ub953_data *priv) { struct device *dev = &priv->client->dev; @@ -1055,16 +1061,15 @@ static int ub953_register_clkout(struct ub953_data *priv) priv->hw_data->model, dev_name(dev)), .ops = &ub953_clkout_ops, }; + struct ub953_clkout_data clkout_data; int ret; if (!init.name) return -ENOMEM; /* Initialize clkout to 25MHz by default */ - if (priv->hw_data->is_ub971) - ub953_init_clkout_ub971(priv); - else - ub953_init_clkout_ub953(priv); + ub953_calc_clkout_params(priv, 25000000, &clkout_data); + ub953_write_clkout_regs(priv, &clkout_data); priv->clkout_clk_hw.init = &init;