Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp2274928rdb; Thu, 21 Sep 2023 13:43:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFWATee8/FDGAi14+SW1/YYWcMlvZ8lj9BHZSrq0S3XjdR/nnOM9cWkRI9PLsrR+YeS3QZZ X-Received: by 2002:a05:6a00:139a:b0:68f:cb69:8e66 with SMTP id t26-20020a056a00139a00b0068fcb698e66mr1017850pfg.15.1695329012689; Thu, 21 Sep 2023 13:43:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695329012; cv=none; d=google.com; s=arc-20160816; b=0XKy+Kx/JxvuP7/+w3J0qyfADqPIOxEi2DcGZjozBAhfMspEabCnGMyoVb2xG6dCGf G7QlXerBpXjiZW7XtILnNnw3fnImHuKMNMXgoWWbqwJzSePM0snru5e1x07bs6Rl7NMa Z4+lmRz0jaaNljjEaUklmQLYh7/CQTqAzbQKd2dyd1vWT5eiGvDKbFDb2mC/jQI2oS1X 3XaMCiMT2ZHHlA6aQuMhaFyv59jUHYNnRxtsxOBfvFSI8vKY6n/ddzfwAvyL2brSSAtU DewUDja252+X2/sT835mKNISh1lv094LsI/E3ddNWAne2MQ7UtfTaojAvO64KTuEGiYA H/5w== 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=Y36MztlvKP8XCZ/7k3Bg8j/V4FzxZBMXF1bBRowIpVw=; fh=ZqJ3xrq0DGyvWtPBQo5VWOM7gaftcxyjM72UWd4YoC8=; b=tEgxBRNtp2JtX+BsZdLCbLZZrx5vieDhwt7732wHJVevZojJGfHtfdlXIx2OGmDABE jLo8LHttxUOs7+kowx+xSbbGDzEzH0XM+wdwaoknkkESc+euKrRUWTRCX90RbMn03Dmu V2skrfakjLAxnWTk4SyQK1i7VoNQl3/9gMraFFvkxjFVFnr9NM/xcIWIu7sXnZBgRlcN umEgrnLqJY9SUdrPtdeehnLHHhMtyyoeXiXj4C42G9MGw5sjVi+/ON2sEhNrlMzZD+ul vDh0sQWbhGufVX1D9WscxSaGWVc2/GsaxY7hU6+Qm2mZstdT2HqH6efEwGDAUkV6XD8M jo9w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id ea22-20020a056a004c1600b00686baf235easi2221842pfb.258.2023.09.21.13.43.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 13:43:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 1D1CE839233C; Thu, 21 Sep 2023 13:03:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231418AbjIUUDg (ORCPT + 99 others); Thu, 21 Sep 2023 16:03:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231593AbjIUUC7 (ORCPT ); Thu, 21 Sep 2023 16:02:59 -0400 Received: from SHSQR01.spreadtrum.com (unknown [222.66.158.135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5528D573F4; Thu, 21 Sep 2023 10:18:43 -0700 (PDT) Received: from SHSQR01.spreadtrum.com (localhost [127.0.0.2] (may be forged)) by SHSQR01.spreadtrum.com with ESMTP id 38L8vNUw048315; Thu, 21 Sep 2023 16:57:23 +0800 (+08) (envelope-from Huangzheng.Lai@unisoc.com) Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 38L8tWjg040670; Thu, 21 Sep 2023 16:55:32 +0800 (+08) (envelope-from Huangzheng.Lai@unisoc.com) Received: from SHDLP.spreadtrum.com (shmbx04.spreadtrum.com [10.0.1.214]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4Rrq0C6JnCz2SZykd; Thu, 21 Sep 2023 16:52:11 +0800 (CST) Received: from xm9614pcu.spreadtrum.com (10.13.2.29) by shmbx04.spreadtrum.com (10.0.1.214) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Thu, 21 Sep 2023 16:55:30 +0800 From: Huangzheng Lai To: Andi Shyti CC: Orson Zhai , Baolin Wang , Chunyan Zhang , , , huangzheng lai , Huangzheng Lai , Xiongpeng Wu Subject: [PATCH V2 4/7] i2c: sprd: Add I2C controller driver to support dynamic switching of 400K/1M/3.4M frequency Date: Thu, 21 Sep 2023 16:54:54 +0800 Message-ID: <20230921085457.32446-5-Huangzheng.Lai@unisoc.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230921085457.32446-1-Huangzheng.Lai@unisoc.com> References: <20230921085457.32446-1-Huangzheng.Lai@unisoc.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.13.2.29] X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To shmbx04.spreadtrum.com (10.0.1.214) X-MAIL: SHSQR01.spreadtrum.com 38L8tWjg040670 X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.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 (fry.vger.email [0.0.0.0]); Thu, 21 Sep 2023 13:03:41 -0700 (PDT) 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. 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; -- 2.17.1