Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp82777pxv; Wed, 14 Jul 2021 19:37:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzUKIonrA/PiaJt+5c2cRg5gWj82OVQ1lRs4u+EAz+QIiVpVrvUYayv51lPIQ59KppUCqXv X-Received: by 2002:a05:6602:249a:: with SMTP id g26mr1128122ioe.150.1626316674299; Wed, 14 Jul 2021 19:37:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626316674; cv=none; d=google.com; s=arc-20160816; b=i3J5CwQMV8iKWOwqCTIXgejkn8fSpW/IzVP01vD1jc+Ma4QbJXQuXUhkb/8AUs09dp u6XohUpHoMAj01xBINkqbWk5ZSFjwVcmaBQXDn2eLBOeHgHpm/dpc4fBoDqhNzhX3nlw EPdhhk7n1GYA0aVfFimjV+CPkwysevg4ovnMaj4/rELMXa14m2IWApaRyHV0hWnKBKUt SCzbJsQNxyqXsn+V1/Y/uzRH72yL4/SQYBiOuKBxyA2LDv0OBZ2XrpZ4LVoQeoA/Tpvr L1uAWjin1WU4ic0NcJlkYsaBpCP1jyqmIfERxwmXO8WNlbEdWbxaQhLLf9pIwU4/CZsI frOQ== 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=HmT34mPDA4ZUyy4hpehS5Lz6rgNqSkXgp2ZFEVnib/M=; b=rfFBB7hn67Uaui3F40lxOoWFPd6wU3ajxsHwx2559C1zKFWZpAzzmnE54yE1ZkJBzd 8c2M3mr8aqGLF1kNKZUe2L0plQrg4kTjnmy0YBc5rvdR+SU56ih8jSYqQwKNhM7qOS6n FJdJrpYikiL0xUCHIosDHTg1dUUqm+mGSAA65PQjPpR2CDE2Q232AWHQ8ahjyp5U85xN FnwfH8aOH8IqCyzNL6Gcq5ZeEswh9a/fknzO/uW9k81x7Y8YDvOwjrgTiNQtkP6sR+bv epw4lUhzNI+LlnHUUliXshaEUPDzdaBWV2gNoyeMV+glqdqE6Uf0lEouE41BcQfnA91G Ak8w== 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 e6si4610839ilm.61.2021.07.14.19.37.42; Wed, 14 Jul 2021 19:37:54 -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 S236460AbhGOCdW (ORCPT + 99 others); Wed, 14 Jul 2021 22:33:22 -0400 Received: from mailgw01.mediatek.com ([60.244.123.138]:57528 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S233148AbhGOCdO (ORCPT ); Wed, 14 Jul 2021 22:33:14 -0400 X-UUID: 6aed61614b424139815e4be14d9e0364-20210715 X-UUID: 6aed61614b424139815e4be14d9e0364-20210715 Received: from mtkcas10.mediatek.inc [(172.21.101.39)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 1762703519; Thu, 15 Jul 2021 10:30:16 +0800 Received: from mtkcas11.mediatek.inc (172.21.101.40) by mtkmbs07n1.mediatek.inc (172.21.101.16) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 15 Jul 2021 10:30:16 +0800 Received: from localhost.localdomain (10.17.3.153) by mtkcas11.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Thu, 15 Jul 2021 10:30:15 +0800 From: Kewei Xu To: CC: , , , , , , , , , , , , Subject: [PATCH 8/8] i2c: mediatek: modify bus speed calculation formula Date: Thu, 15 Jul 2021 10:29:17 +0800 Message-ID: <1626316157-24935-9-git-send-email-kewei.xu@mediatek.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1626316157-24935-1-git-send-email-kewei.xu@mediatek.com> References: <1626316157-24935-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 | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/drivers/i2c/busses/i2c-mt65xx.c b/drivers/i2c/busses/i2c-mt65xx.c index 486076f..4e43a79 100644 --- a/drivers/i2c/busses/i2c-mt65xx.c +++ b/drivers/i2c/busses/i2c-mt65xx.c @@ -68,11 +68,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) @@ -719,14 +720,25 @@ 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->default_timing_adjust) { + 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 @@ -761,7 +773,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 */ @@ -846,13 +859,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 */ -- 1.9.1