Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp1157238rda; Mon, 23 Oct 2023 04:37:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGUXYDXCoBVj5Rg9KJx33d9cH8feNBmcmJYdE3LUsr2Kl+Ixg3DRbPZdHIxKCtgfMmlfpMG X-Received: by 2002:a05:6a21:6d99:b0:16b:b0ea:b0a1 with SMTP id wl25-20020a056a216d9900b0016bb0eab0a1mr4364349pzb.34.1698061043702; Mon, 23 Oct 2023 04:37:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698061043; cv=none; d=google.com; s=arc-20160816; b=tfADKq1eWPLekmkYlWo8PjOdlDDHErre6f0p/4/Ycva+1EalL8LXse8DFiQu8+7/Mc 703LnbhYH48c3cjans1Crwjy7aueANfsHd4mMNqc9tM8XhwSeGKM0nde4mXaYUvkf4y7 5ag/5q7Z/5SCJCOhW7k6W73FLQx5gY3t1wpfBIP72bit1P1irEgL2WC0Taw7YVrGK1aZ 4PZ2dhNs5esFkyBuPmxp1a2LNExgsymnzSrWyruQUR0y7GjWsZrnHDXSzqnPuMD1oQdB J3Ok2jWeGxErd/rnQlP1tO/vebaDex+ZYLFy2/eMYJV6EmZDUrlIk44BviE1rEyUBwgD 7WXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:subject:user-agent:mime-version:date:message-id; bh=JRuRkBK/bZ6++eHeGsWbuOp1LoWTwMxDUnf5aPTmXpQ=; fh=tr/fgHUcpLpbS4VIozjfnV7mG4hJKNrvAEbZeaGGeJc=; b=CUN6+UF438Y3IePkyMOEfTPq8VK96HkIKoFPJHQca/Pk1fe9NoAw51NGCz8qT457Ic VJcWnnGGeGguOpLukkm1K8pexZd1snaBmU/ESBuEqEvkr5Cz92lqWrvy1f+5Z3YHc50Z CGxJ9wMC/oR7C3pH9Ku/TXMXPUKtH392qAvoZ2MUR0EYN86vbF55XeG0m1yMgcsjdhEe H4E4I3v8FY3No9bewwYR7TJwypPbvmBBAn8wtWo4xw+5HpqCXscBbAqdawO1Fbo5Eqf2 g+y9m0Ep/XOtfXwlwF9kPwKVq1SKXdFKbozO1H0XlE9HIojeFsJjPVQ5k3QvJu2uVo/o K4UQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id by14-20020a056a02058e00b0057760853706si6938546pgb.578.2023.10.23.04.37.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 04:37:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 006A280B1235; Mon, 23 Oct 2023 04:37:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234172AbjJWLhD (ORCPT + 99 others); Mon, 23 Oct 2023 07:37:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234164AbjJWLhC (ORCPT ); Mon, 23 Oct 2023 07:37:02 -0400 Received: from out30-119.freemail.mail.aliyun.com (out30-119.freemail.mail.aliyun.com [115.124.30.119]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83ADDFF; Mon, 23 Oct 2023 04:36:58 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R101e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046060;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=8;SR=0;TI=SMTPD_---0VuktlA6_1698061013; Received: from 30.97.48.63(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0VuktlA6_1698061013) by smtp.aliyun-inc.com; Mon, 23 Oct 2023 19:36:54 +0800 Message-ID: Date: Mon, 23 Oct 2023 19:37:10 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: [PATCH V2 4/7] i2c: sprd: Add I2C controller driver to support dynamic switching of 400K/1M/3.4M frequency To: Huangzheng Lai , Andi Shyti Cc: Orson Zhai , Chunyan Zhang , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, huangzheng lai , Xiongpeng Wu References: <20231023081158.10654-1-Huangzheng.Lai@unisoc.com> <20231023081158.10654-5-Huangzheng.Lai@unisoc.com> From: Baolin Wang In-Reply-To: <20231023081158.10654-5-Huangzheng.Lai@unisoc.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.1 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 23 Oct 2023 04:37:21 -0700 (PDT) On 10/23/2023 4:11 PM, Huangzheng Lai wrote: > When I2C-slaves supporting different frequencies use the same I2C > controller, the I2C controller usually only operates at lower frequencies. > In order to improve the performance of I2C-slaves transmission supporting > faster frequencies, we dynamically configure the I2C operating frequency > based on the value of the input parameter msg ->flag. I am not sure if this is suitable to expand the msg->flag. Andi, how do you think? Thanks. > Signed-off-by: Huangzheng Lai > --- > drivers/i2c/busses/i2c-sprd.c | 101 +++++++++++++++++++--------------- > 1 file changed, 57 insertions(+), 44 deletions(-) > > diff --git a/drivers/i2c/busses/i2c-sprd.c b/drivers/i2c/busses/i2c-sprd.c > index dec627ef408c..f1f7fad42ecd 100644 > --- a/drivers/i2c/busses/i2c-sprd.c > +++ b/drivers/i2c/busses/i2c-sprd.c > @@ -75,7 +75,14 @@ > #define SPRD_I2C_PM_TIMEOUT 1000 > /* timeout (ms) for transfer message */ > #define I2C_XFER_TIMEOUT 1000 > - > +/* dynamic modify clk_freq flag */ > +#define I2C_3M4_FLAG 0x0100 > +#define I2C_1M_FLAG 0x0080 > +#define I2C_400K_FLAG 0x0040 > + > +#define I2C_FREQ_400K 400000 > +#define I2C_FREQ_1M 1000000 > +#define I2C_FREQ_3_4M 3400000 > /* SPRD i2c data structure */ > struct sprd_i2c { > struct i2c_adapter adap; > @@ -94,6 +101,49 @@ struct sprd_i2c { > int err; > }; > > +static void sprd_i2c_set_clk(struct sprd_i2c *i2c_dev, u32 freq) > +{ > + u32 apb_clk = i2c_dev->src_clk; > + /* > + * From I2C databook, the prescale calculation formula: > + * prescale = freq_i2c / (4 * freq_scl) - 1; > + */ > + u32 i2c_dvd = apb_clk / (4 * freq) - 1; > + /* > + * From I2C databook, the high period of SCL clock is recommended as > + * 40% (2/5), and the low period of SCL clock is recommended as 60% > + * (3/5), then the formula should be: > + * high = (prescale * 2 * 2) / 5 > + * low = (prescale * 2 * 3) / 5 > + */ > + u32 high = ((i2c_dvd << 1) * 2) / 5; > + u32 low = ((i2c_dvd << 1) * 3) / 5; > + u32 div0 = I2C_ADDR_DVD0_CALC(high, low); > + u32 div1 = I2C_ADDR_DVD1_CALC(high, low); > + > + writel(div0, i2c_dev->base + ADDR_DVD0); > + writel(div1, i2c_dev->base + ADDR_DVD1); > + > + /* Start hold timing = hold time(us) * source clock */ > + switch (freq) { > + case I2C_MAX_STANDARD_MODE_FREQ: > + writel((4 * apb_clk) / 1000000, i2c_dev->base + ADDR_STA0_DVD); > + break; > + case I2C_MAX_FAST_MODE_FREQ: > + writel((6 * apb_clk) / 10000000, i2c_dev->base + ADDR_STA0_DVD); > + break; > + case I2C_MAX_FAST_MODE_PLUS_FREQ: > + writel((8 * apb_clk) / 10000000, i2c_dev->base + ADDR_STA0_DVD); > + break; > + case I2C_MAX_HIGH_SPEED_MODE_FREQ: > + writel((8 * apb_clk) / 10000000, i2c_dev->base + ADDR_STA0_DVD); > + break; > + default: > + dev_err(i2c_dev->dev, "Unsupported frequency: %d\n", freq); > + break; > + } > +} > + > static void sprd_i2c_set_count(struct sprd_i2c *i2c_dev, u32 count) > { > writel(count, i2c_dev->base + I2C_COUNT); > @@ -269,6 +319,12 @@ static int sprd_i2c_handle_msg(struct i2c_adapter *i2c_adap, > sprd_i2c_send_stop(i2c_dev, !!is_last_msg); > } > > + if (msg->flags & I2C_400K_FLAG) > + sprd_i2c_set_clk(i2c_dev, I2C_FREQ_400K); > + else if (msg->flags & I2C_1M_FLAG) > + sprd_i2c_set_clk(i2c_dev, I2C_FREQ_1M); > + else if (msg->flags & I2C_3M4_FLAG) > + sprd_i2c_set_clk(i2c_dev, I2C_FREQ_3_4M); > /* > * We should enable rx fifo full interrupt to get data when receiving > * full data. > @@ -331,49 +387,6 @@ static const struct i2c_algorithm sprd_i2c_algo = { > .functionality = sprd_i2c_func, > }; > > -static void sprd_i2c_set_clk(struct sprd_i2c *i2c_dev, u32 freq) > -{ > - u32 apb_clk = i2c_dev->src_clk; > - /* > - * From I2C databook, the prescale calculation formula: > - * prescale = freq_i2c / (4 * freq_scl) - 1; > - */ > - u32 i2c_dvd = apb_clk / (4 * freq) - 1; > - /* > - * From I2C databook, the high period of SCL clock is recommended as > - * 40% (2/5), and the low period of SCL clock is recommended as 60% > - * (3/5), then the formula should be: > - * high = (prescale * 2 * 2) / 5 > - * low = (prescale * 2 * 3) / 5 > - */ > - u32 high = ((i2c_dvd << 1) * 2) / 5; > - u32 low = ((i2c_dvd << 1) * 3) / 5; > - u32 div0 = I2C_ADDR_DVD0_CALC(high, low); > - u32 div1 = I2C_ADDR_DVD1_CALC(high, low); > - > - writel(div0, i2c_dev->base + ADDR_DVD0); > - writel(div1, i2c_dev->base + ADDR_DVD1); > - > - /* Start hold timing = hold time(us) * source clock */ > - switch (freq) { > - case I2C_MAX_STANDARD_MODE_FREQ: > - writel((4 * apb_clk) / 1000000, i2c_dev->base + ADDR_STA0_DVD); > - break; > - case I2C_MAX_FAST_MODE_FREQ: > - writel((6 * apb_clk) / 10000000, i2c_dev->base + ADDR_STA0_DVD); > - break; > - case I2C_MAX_FAST_MODE_PLUS_FREQ: > - writel((8 * apb_clk) / 10000000, i2c_dev->base + ADDR_STA0_DVD); > - break; > - case I2C_MAX_HIGH_SPEED_MODE_FREQ: > - writel((8 * apb_clk) / 10000000, i2c_dev->base + ADDR_STA0_DVD); > - break; > - default: > - dev_err(i2c_dev->dev, "Unsupported frequency: %d\n", freq); > - break; > - } > -} > - > static void sprd_i2c_enable(struct sprd_i2c *i2c_dev) > { > u32 tmp = I2C_DVD_OPT;