Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2078225pxb; Sat, 28 Aug 2021 03:54:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxRTfCVGXXfhwqWFzgQmNRsWgQgQYCWuMgslln+Nga4ClFtx0ZOklfRjLtkdmlAd580Sirp X-Received: by 2002:a17:907:2151:: with SMTP id rk17mr4971460ejb.41.1630148075264; Sat, 28 Aug 2021 03:54:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630148075; cv=none; d=google.com; s=arc-20160816; b=0NxyBHgbvn2qCIhtUX0I9A8GdI0Mttnf7CekQwUqzxzcUqiEzxMsIw45UEYgEXtS3+ 9vdG5I10GrNIIPsc8eI1OzNh9neDiRg8swA5EuII7PvXvX6s2KBBMgJ760Kjf0Cj4rxU 9oox+D+oBhCH5+jg/jHpogalpLanRytG43e/jhFxR0n2NElUHlA9uAU1BmITTWqyeZGS Yb9INbDUKEkSWRmoUd/Y/XW3tNEmFcrY/68q3GsiAJwLp3ITstxSrYHL8NV9a8Memiz1 0UuOk2YYhAvwAzBW3hbLCKaIzdH7CQr9h7D90n/ovsor8XJ/DTc5FgeW/yoeqMhYB6gg E8LA== 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=3sa+HaYV8f5IMopG8CMS04IExkkGzBzmiwWwlzhIw8c=; b=KeyVgToH0kT2YlLCjov/zRd3ix6oYttDmQKfXKEGBRlooHM6t+L2HNUDJ/SIRLmTDJ T8RlNjznQ+PJhfoIFf9MFX9ZsChso+0BHcFGpj/HCYO5rOGZrWCfPGUq3fm32lHXuOlj 3hispK2C4xeIz5p2jVj5XzFnePCrRtla5p55/YQCYXDQ+1k+am5wrOKPTsNIppPbUbE+ bIErn8kkfHlZtV4qVzSNRFMLTrbsLKVe3HjwpKcs0L8ZgYOsQDGXbr37DD8qGHrG6k6U zidCAw6NjSS7jZQiWuadzvgw2ghQY6JSMBojujTEA2tfFvpn+HM03yeBZABcznx3nAaJ LsJg== 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 qf29si2426013ejc.618.2021.08.28.03.54.12; Sat, 28 Aug 2021 03:54:35 -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 S234073AbhH1Kwf (ORCPT + 99 others); Sat, 28 Aug 2021 06:52:35 -0400 Received: from mailgw01.mediatek.com ([60.244.123.138]:34372 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S233886AbhH1KwK (ORCPT ); Sat, 28 Aug 2021 06:52:10 -0400 X-UUID: fd844b79cae741618a74609e590f430c-20210828 X-UUID: fd844b79cae741618a74609e590f430c-20210828 Received: from mtkmbs10n1.mediatek.inc [(172.21.101.34)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1221047945; Sat, 28 Aug 2021 18:51:16 +0800 Received: from mtkcas07.mediatek.inc (172.21.101.84) by mtkmbs07n2.mediatek.inc (172.21.101.141) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Sat, 28 Aug 2021 18:51:15 +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:14 +0800 From: Kewei Xu To: CC: , , , , , , , , , , , , , , Subject: [PATCH v6 6/7] i2c: mediatek: Isolate speed setting via dts for special devices Date: Sat, 28 Aug 2021 18:50:58 +0800 Message-ID: <1630147859-17031-7-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 In the commit be5ce0e97cc7 ("i2c: mediatek: Add i2c ac-timing adjust support"), the I2C timing calculation has been revised to support ac-timing adjustment, however that will break on some I2C components. As a result we want to introduce a new setting "default-adjust-timing" so those components can choose to use the old (default) timing algorithm. Fixes: be5ce0e97cc7 ("i2c: mediatek: Add i2c ac-timing adjust support") Signed-off-by: Kewei Xu --- drivers/i2c/busses/i2c-mt65xx.c | 77 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 4 deletions(-) diff --git a/drivers/i2c/busses/i2c-mt65xx.c b/drivers/i2c/busses/i2c-mt65xx.c index 6ded82e..0cf130b 100644 --- a/drivers/i2c/busses/i2c-mt65xx.c +++ b/drivers/i2c/busses/i2c-mt65xx.c @@ -66,6 +66,7 @@ #define I2C_DMA_HARD_RST 0x0002 #define I2C_DMA_HANDSHAKE_RST 0x0004 +#define I2C_DEFAULT_CLK_DIV 5 #define MAX_SAMPLE_CNT_DIV 8 #define MAX_STEP_CNT_DIV 64 #define MAX_CLOCK_DIV 256 @@ -250,6 +251,7 @@ struct mtk_i2c { struct clk *clk_arb; /* Arbitrator clock for i2c */ bool have_pmic; /* can use i2c pins from PMIC */ bool use_push_pull; /* IO config push-pull mode */ + bool use_default_timing; /* no timing adjust mode */ u16 irq_stat; /* interrupt status */ unsigned int clk_src_div; @@ -532,7 +534,11 @@ static void mtk_i2c_init_hw(struct mtk_i2c *i2c) else ext_conf_val = I2C_FS_START_CON; - if (i2c->dev_comp->timing_adjust) { + if (i2c->use_default_timing) { + if (i2c->dev_comp->timing_adjust) + mtk_i2c_writew(i2c, I2C_DEFAULT_CLK_DIV - 1, + OFFSET_CLOCK_DIV); + } else if (i2c->dev_comp->timing_adjust) { ext_conf_val = i2c->ac_timing.ext; mtk_i2c_writew(i2c, i2c->ac_timing.inter_clk_div, OFFSET_CLOCK_DIV); @@ -615,7 +621,7 @@ static int mtk_i2c_check_ac_timing(struct mtk_i2c *i2c, unsigned int sample_ns = div_u64(1000000000ULL * (sample_cnt + 1), clk_src); - if (!i2c->dev_comp->timing_adjust) + if (i2c->use_default_timing || !i2c->dev_comp->timing_adjust) return 0; if (i2c->dev_comp->ltiming_adjust) @@ -775,7 +781,65 @@ static int mtk_i2c_calculate_speed(struct mtk_i2c *i2c, unsigned int clk_src, return 0; } -static int mtk_i2c_set_speed(struct mtk_i2c *i2c, unsigned int parent_clk) +static int mtk_i2c_set_speed_default_timing(struct mtk_i2c *i2c, + unsigned int parent_clk) +{ + unsigned int clk_src; + unsigned int step_cnt; + unsigned int sample_cnt; + unsigned int l_step_cnt; + unsigned int l_sample_cnt; + unsigned int target_speed; + int ret; + + if (i2c->dev_comp->timing_adjust) + i2c->clk_src_div *= I2C_DEFAULT_CLK_DIV; + + clk_src = parent_clk / i2c->clk_src_div; + target_speed = i2c->speed_hz; + + if (target_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) { + /* Set master code speed register */ + ret = mtk_i2c_calculate_speed(i2c, clk_src, + I2C_MAX_FAST_MODE_FREQ, + &l_step_cnt, &l_sample_cnt); + if (ret < 0) + return ret; + + i2c->timing_reg = (l_sample_cnt << 8) | l_step_cnt; + + /* Set the high speed mode register */ + ret = mtk_i2c_calculate_speed(i2c, clk_src, target_speed, + &step_cnt, &sample_cnt); + if (ret < 0) + return ret; + + i2c->high_speed_reg = I2C_TIME_DEFAULT_VALUE | + (sample_cnt << 12) | (step_cnt << 8); + + if (i2c->dev_comp->ltiming_adjust) + i2c->ltiming_reg = (l_sample_cnt << 6) | l_step_cnt | + (sample_cnt << 12) | (step_cnt << 9); + } else { + ret = mtk_i2c_calculate_speed(i2c, clk_src, target_speed, + &step_cnt, &sample_cnt); + if (ret < 0) + return ret; + + i2c->timing_reg = (sample_cnt << 8) | step_cnt; + + /* Disable the high speed transaction */ + i2c->high_speed_reg = I2C_TIME_CLR_VALUE; + + if (i2c->dev_comp->ltiming_adjust) + i2c->ltiming_reg = (sample_cnt << 6) | step_cnt; + } + + return 0; +} + +static int mtk_i2c_set_speed_adjust_timing(struct mtk_i2c *i2c, + unsigned int parent_clk) { unsigned int clk_src; unsigned int step_cnt; @@ -1271,6 +1335,8 @@ static int mtk_i2c_parse_dt(struct device_node *np, struct mtk_i2c *i2c) i2c->have_pmic = of_property_read_bool(np, "mediatek,have-pmic"); i2c->use_push_pull = of_property_read_bool(np, "mediatek,use-push-pull"); + i2c->use_default_timing = + of_property_read_bool(np, "mediatek,use-default-timing"); i2c_parse_fw_timings(i2c->dev, &i2c->timing_info, true); @@ -1357,7 +1423,10 @@ static int mtk_i2c_probe(struct platform_device *pdev) strlcpy(i2c->adap.name, I2C_DRV_NAME, sizeof(i2c->adap.name)); - ret = mtk_i2c_set_speed(i2c, clk_get_rate(clk)); + if (i2c->use_default_timing) + ret = mtk_i2c_set_speed_default_timing(i2c, clk_get_rate(clk)); + else + ret = mtk_i2c_set_speed_adjust_timing(i2c, clk_get_rate(clk)); if (ret) { dev_err(&pdev->dev, "Failed to set the speed.\n"); return -EINVAL; -- 1.9.1