Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2078178pxb; Sat, 28 Aug 2021 03:54:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwEefC1W7Tho1FhCPzbVI+7V7d7WA95EbHnRfcsAbEJpNmQspeVbOaDRAzkCrUmzX1uPvYG X-Received: by 2002:aa7:c4c3:: with SMTP id p3mr14340841edr.122.1630148064771; Sat, 28 Aug 2021 03:54:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630148064; cv=none; d=google.com; s=arc-20160816; b=lxO87GCYXuooKq6nPIoJ0iEcxtqsau2/baSQI7jMaWjJOXJnhL67VBN4sMQR9EPv5b kEHiGY1iyBIewaac3XokzjlCXtHiQmIHccEUHDxIHJLx2zPbBb67LJeZyB9skCESYTQ8 Uv3sEeYwwrlm5w2U8CwblF8vKfoMU51jeuPDpBHYDGpDf4usPGDq4U34eonNXaFEmdlC SPkDDmLfj/WGNpO2LRDRC/h+GV6+tWErDDupUFfz4pC3VGxLdWv+zVileliGmnyqxbLi GvWeFbgzlJR1Ra/9a03ZB3+IcYL2YaKMAtO31R5VwB4ef9H5L/7wsDAOogpSzjthftV3 EHBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=OUOY+kYrRtphkz0ClVOiCr0n1DtRjcAPWxdLb9iCQq4=; b=CYU/SJLVdXMVhd4Bna8/b1KhDJwQn/KCMC+h61zcee+DmNpmlw9gL9Cwj8d+GwT4MW 8nNrjykptQbyWP5JCbhoTJ+4iwyXqVN10h+IMQYUgOZ9FXP200oh0F1P+wlvvNqQ2hYV sUGTid3EmQj0h50kAyDgIpSF74eU6apUaslPtxaodSaGcSYkVELSgjpVdZJNwIcFxQ0F 7+G2YPUU7tbUhNsjh1+MgdDDklmVDnnWYrDsLkwVRtYhW6mkPkcEXSWVQuBh5uGU9N1V Gadrjcj3QQQbZnOzj2BwshqOBzlT/4PGPcyHW/De50vwK1M+aLErOz7+8uDjFiBHVnlU KPwA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p4si9560210eju.288.2021.08.28.03.54.01; Sat, 28 Aug 2021 03:54:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234052AbhH1Kw3 (ORCPT + 99 others); Sat, 28 Aug 2021 06:52:29 -0400 Received: from mailgw01.mediatek.com ([60.244.123.138]:34394 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S233757AbhH1KwL (ORCPT ); Sat, 28 Aug 2021 06:52:11 -0400 X-UUID: 25a4b006b7844fb08774cf3b3b38c097-20210828 X-UUID: 25a4b006b7844fb08774cf3b3b38c097-20210828 Received: from mtkcas06.mediatek.inc [(172.21.101.30)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 96953242; Sat, 28 Aug 2021 18:51:17 +0800 Received: from mtkcas07.mediatek.inc (172.21.101.84) by mtkmbs07n1.mediatek.inc (172.21.101.16) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Sat, 28 Aug 2021 18:51:16 +0800 Received: from localhost.localdomain (10.17.3.153) by mtkcas07.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Sat, 28 Aug 2021 18:51:15 +0800 From: Kewei Xu To: CC: , , , , , , , , , , , , , , Subject: [PATCH v6 7/7] i2c: mediatek: modify bus speed calculation formula Date: Sat, 28 Aug 2021 18:50:59 +0800 Message-ID: <1630147859-17031-8-git-send-email-kewei.xu@mediatek.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1630147859-17031-1-git-send-email-kewei.xu@mediatek.com> References: <1630147859-17031-1-git-send-email-kewei.xu@mediatek.com> MIME-Version: 1.0 Content-Type: text/plain X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When clock-div is 0 or greater than 1, the bus speed calculated by the old speed calculation formula will be larger than the target speed. So we update the formula. Signed-off-by: Kewei Xu --- drivers/i2c/busses/i2c-mt65xx.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/drivers/i2c/busses/i2c-mt65xx.c b/drivers/i2c/busses/i2c-mt65xx.c index 0cf130b..b2dcdc3f 100644 --- a/drivers/i2c/busses/i2c-mt65xx.c +++ b/drivers/i2c/busses/i2c-mt65xx.c @@ -69,11 +69,12 @@ #define I2C_DEFAULT_CLK_DIV 5 #define MAX_SAMPLE_CNT_DIV 8 #define MAX_STEP_CNT_DIV 64 -#define MAX_CLOCK_DIV 256 +#define MAX_CLOCK_DIV_8BITS 256 +#define MAX_CLOCK_DIV_5BITS 32 #define MAX_HS_STEP_CNT_DIV 8 -#define I2C_STANDARD_MODE_BUFFER (1000 / 2) -#define I2C_FAST_MODE_BUFFER (300 / 2) -#define I2C_FAST_MODE_PLUS_BUFFER (20 / 2) +#define I2C_STANDARD_MODE_BUFFER (1000 / 3) +#define I2C_FAST_MODE_BUFFER (300 / 3) +#define I2C_FAST_MODE_PLUS_BUFFER (20 / 3) #define I2C_CONTROL_RS (0x1 << 1) #define I2C_CONTROL_DMA_EN (0x1 << 2) @@ -725,14 +726,26 @@ static int mtk_i2c_calculate_speed(struct mtk_i2c *i2c, unsigned int clk_src, unsigned int best_mul; unsigned int cnt_mul; int ret = -EINVAL; + int clock_div_constraint = 0; if (target_speed > I2C_MAX_HIGH_SPEED_MODE_FREQ) target_speed = I2C_MAX_HIGH_SPEED_MODE_FREQ; + if (i2c->use_default_timing) { + clock_div_constraint = 0; + } else if (i2c->dev_comp->ltiming_adjust && + i2c->ac_timing.inter_clk_div > 1) { + clock_div_constraint = 1; + } else if (i2c->dev_comp->ltiming_adjust && + i2c->ac_timing.inter_clk_div == 0) { + clock_div_constraint = -1; + } + max_step_cnt = mtk_i2c_max_step_cnt(target_speed); base_step_cnt = max_step_cnt; /* Find the best combination */ - opt_div = DIV_ROUND_UP(clk_src >> 1, target_speed); + opt_div = DIV_ROUND_UP(clk_src >> 1, target_speed) + + clock_div_constraint; best_mul = MAX_SAMPLE_CNT_DIV * max_step_cnt; /* Search for the best pair (sample_cnt, step_cnt) with @@ -767,7 +780,8 @@ static int mtk_i2c_calculate_speed(struct mtk_i2c *i2c, unsigned int clk_src, sample_cnt = base_sample_cnt; step_cnt = base_step_cnt; - if ((clk_src / (2 * sample_cnt * step_cnt)) > target_speed) { + if ((clk_src / (2 * (sample_cnt * step_cnt - clock_div_constraint))) > + target_speed) { /* In this case, hardware can't support such * low i2c_bus_freq */ @@ -854,13 +868,16 @@ static int mtk_i2c_set_speed_adjust_timing(struct mtk_i2c *i2c, target_speed = i2c->speed_hz; parent_clk /= i2c->clk_src_div; - if (i2c->dev_comp->timing_adjust) - max_clk_div = MAX_CLOCK_DIV; + if (i2c->dev_comp->timing_adjust && i2c->dev_comp->ltiming_adjust) + max_clk_div = MAX_CLOCK_DIV_5BITS; + else if (i2c->dev_comp->timing_adjust) + max_clk_div = MAX_CLOCK_DIV_8BITS; else max_clk_div = 1; for (clk_div = 1; clk_div <= max_clk_div; clk_div++) { clk_src = parent_clk / clk_div; + i2c->ac_timing.inter_clk_div = clk_div - 1; if (target_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) { /* Set master code speed register */ @@ -907,8 +924,6 @@ static int mtk_i2c_set_speed_adjust_timing(struct mtk_i2c *i2c, break; } - i2c->ac_timing.inter_clk_div = clk_div - 1; - return 0; } -- 1.9.1