Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2390386pxb; Mon, 23 Aug 2021 20:33:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzsKTUSjzH0C7DlrBQ9NES3k0N0Tq+TrUea/P+SnmC1s8dM6hdXQ0CHlxNZmjnHXryWqdOQ X-Received: by 2002:a50:cc4b:: with SMTP id n11mr7966137edi.132.1629775993947; Mon, 23 Aug 2021 20:33:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629775993; cv=none; d=google.com; s=arc-20160816; b=TErPfCxvSrOAyfTJdiYBLr9U46oie2OoSFe5LMOlroMi6P5nQLjzmOgLOC0fJZbmy6 Ap5R2NRRglSiHx9vtE5IBIwkLriHy7mb21kqr8dIc8UwdsxsFNQfuqkY5ZI+xgmjCEdv ycPrMWhjmDhPf6JFH47OI5PwIEA7BE7LgWW413DPAu/zSoBxySJiKP0M+z6jkGz08COE ZfjAvVQN810o3IJ9+QOTGH4zbNwSSh3YD0heukHTY3CjZMp/BUtHC7V/K0/cKKCWCN3a Vq/NiYF96QH8YLinBfl7yDfSLmWu/KMr8MdI5Vq5RDQUaF+FbfiR4XUU0XlSVdSVJgAM 48dw== 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=GQIL8sjqXelhHsgis1DACEjn8MQn9BX+WgOZH1EoNLc=; b=jOauq7RdzR70aJRzPCKmcZ4IYfmEEo/3VIyMWoxWtgjO6i0Btk9HYPtVRVabXnOPBO 6abR7gWPzynsRK6mLtcjjBGi7yimVbbQ6bwZ4MeUnScSyEo/v1CaFuWGmJxCjqDgsqK8 IHb7CS9nlqi21hQgFHJwdRAzDT5iYGDxa6yGnGXb5Qo17uqTJQo/OgdEOc2CxvTdZpxo m9FoLt0tJ+Wlj4z8PTvSga+v7AEYucDqPQY8bsCNGwRmBVkOVyDnbjE8TgkrNQxSVqyM cQl+XMe0otdYF3cv5P/n60ri+5f+db3TW3Pl3kVL6ERo1NFVDDsYYkIJd+2XbyRzEINY FR6w== 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 gn39si5376960ejc.349.2021.08.23.20.32.51; Mon, 23 Aug 2021 20:33:13 -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 S234374AbhHXDbk (ORCPT + 99 others); Mon, 23 Aug 2021 23:31:40 -0400 Received: from mailgw01.mediatek.com ([60.244.123.138]:37570 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S232937AbhHXDb1 (ORCPT ); Mon, 23 Aug 2021 23:31:27 -0400 X-UUID: e13adde845df445da4c0281484c3e099-20210824 X-UUID: e13adde845df445da4c0281484c3e099-20210824 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 708686720; Tue, 24 Aug 2021 11:30:40 +0800 Received: from MTKCAS06.mediatek.inc (172.21.101.30) by mtkmbs07n1.mediatek.inc (172.21.101.16) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 24 Aug 2021 11:30:39 +0800 Received: from localhost.localdomain (10.17.3.153) by MTKCAS06.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Tue, 24 Aug 2021 11:30:38 +0800 From: Kewei Xu To: CC: , , , , , , , , , , , , , , Subject: [RESEND PATCH v5 7/7] i2c: mediatek: modify bus speed calculation formula Date: Tue, 24 Aug 2021 11:30:28 +0800 Message-ID: <1629775828-19993-8-git-send-email-kewei.xu@mediatek.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1629775828-19993-1-git-send-email-kewei.xu@mediatek.com> References: <1629775828-19993-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 18b5659..0b42acd8 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