From patchwork Thu Nov 6 08:11:54 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: addy ke X-Patchwork-Id: 5239941 Return-Path: X-Original-To: patchwork-linux-rockchip@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 48EC89F8B3 for ; Thu, 6 Nov 2014 08:12:56 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 5BFC12011E for ; Thu, 6 Nov 2014 08:12:55 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7705420117 for ; Thu, 6 Nov 2014 08:12:54 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1XmIBa-0003wd-6v; Thu, 06 Nov 2014 08:12:54 +0000 Received: from regular1.263xmail.com ([211.150.99.131]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1XmIBO-0003lB-AS; Thu, 06 Nov 2014 08:12:43 +0000 Received: from addy.ke?rock-chips.com (unknown [192.168.167.78]) by regular1.263xmail.com (Postfix) with SMTP id 822E525E4; Thu, 6 Nov 2014 16:12:10 +0800 (CST) X-263anti-spam: KSV:0; X-MAIL-GRAY: 0 X-MAIL-DELIVERY: 1 X-KSVirus-check: 0 X-ABS-CHECKED: 4 Received: from addy-vm.localdomain (localhost.localdomain [127.0.0.1]) by smtp.263.net (Postfix) with ESMTP id 8DA962A158; Thu, 6 Nov 2014 16:12:03 +0800 (CST) X-RL-SENDER: addy.ke@rock-chips.com X-FST-TO: wsa@the-dreams.de X-SENDER-IP: 127.0.0.1 X-LOGIN-NAME: addy.ke@rock-chips.com X-UNIQUE-TAG: <70dad6a6034f865c668b382ca3029277> X-ATTACHMENT-NUM: 0 X-SENDER: kfx@rock-chips.com X-DNS-TYPE: 0 Received: from addy-vm.localdomain (unknown [127.0.0.1]) by smtp.263.net (Postfix) whith ESMTP id 28081TVLKQ2; Thu, 06 Nov 2014 16:12:05 +0800 (CST) From: Addy Ke To: wsa@the-dreams.de, max.schwarz@online.de, heiko@sntech.de, olof@lixom.net, dianders@chromium.org Subject: [PATCH] i2c: rk3x: fix bug that cause measured high_ns doesn't meet I2C spec Date: Thu, 6 Nov 2014 16:11:54 +0800 Message-Id: <1415261514-4051-1-git-send-email-addy.ke@rock-chips.com> X-Mailer: git-send-email 1.8.3.2 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20141106_001242_748016_FC04F901 X-CRM114-Status: UNSURE ( 8.72 ) X-CRM114-Notice: Please train this message. X-Spam-Score: -0.0 (/) Cc: huangtao@rock-chips.com, Addy Ke , hl@rock-chips.com, yzq@rock-chips.com, zyw@rock-chips.com, linux-kernel@vger.kernel.org, kever.yang@rock-chips.com, linux-rockchip@lists.infradead.org, xjq@rock-chips.com, linux-i2c@vger.kernel.org, caesar.wang@rock-chips.com, cf@rock-chips.com, hj@rock-chips.com, zhengsq@rock-chips.com, linux-arm-kernel@lists.infradead.org X-BeenThere: linux-rockchip@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Upstream kernel work for Rockchip platforms List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "Linux-rockchip" Errors-To: linux-rockchip-bounces+patchwork-linux-rockchip=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP high_ns calculated from the low division of CLKDIV register is the sum of actual measured high_ns and rise_ns. The rise time which related to external pull-up resistor can be up to the maximum rise time in I2C spec. In my test, if external pull-up resistor is 4.7K, rise_ns is about 700ns. So the actual measured high_ns is about 3900ns, which is less than 4000ns (the minimum high_ns in I2C spec). Signed-off-by: Addy Ke --- drivers/i2c/busses/i2c-rk3x.c | 58 +++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/drivers/i2c/busses/i2c-rk3x.c b/drivers/i2c/busses/i2c-rk3x.c index e276ffb..8e1cc2b 100644 --- a/drivers/i2c/busses/i2c-rk3x.c +++ b/drivers/i2c/busses/i2c-rk3x.c @@ -432,9 +432,12 @@ out: static int rk3x_i2c_calc_divs(unsigned long i2c_rate, unsigned long scl_rate, unsigned long *div_low, unsigned long *div_high) { + unsigned long spec_min_low_ns, spec_min_high_ns; + unsigned long spec_max_data_hold_ns; + unsigned long spec_data_hold_buffer_ns; + unsigned long spec_max_rise_ns; + unsigned long min_low_ns, min_high_ns; - unsigned long max_data_hold_ns; - unsigned long data_hold_buffer_ns; unsigned long max_low_ns, min_total_ns; unsigned long i2c_rate_khz, scl_rate_khz; @@ -453,30 +456,43 @@ static int rk3x_i2c_calc_divs(unsigned long i2c_rate, unsigned long scl_rate, if (WARN_ON(scl_rate < 1000)) scl_rate = 1000; + if (scl_rate <= 100000) { + spec_min_low_ns = 4700; + spec_min_high_ns = 4000; + spec_max_rise_ns = 1000; + spec_max_data_hold_ns = 3450; + spec_data_hold_buffer_ns = 50; + } else { + spec_min_low_ns = 1300; + spec_min_high_ns = 600; + spec_max_rise_ns = 300; + spec_max_data_hold_ns = 900; + spec_data_hold_buffer_ns = 50; + } + /* - * min_low_ns: The minimum number of ns we need to hold low - * to meet i2c spec - * min_high_ns: The minimum number of ns we need to hold high - * to meet i2c spec - * max_low_ns: The maximum number of ns we can hold low - * to meet i2c spec + * min_low_ns: The minimum number of ns we need to hold low. + * The fall time in RK3X's I2C controller is approximately + * equal to 0. So min_low_ns = spec_min_low_ns. + * Note: low_ns should be (measured_low_ns + measured_fall_time) + * and measured_low_ns must meet I2C spec. * - * Note: max_low_ns should be (max data hold time * 2 - buffer) + * min_high_ns: The minimum number of ns we need to hold high. + * The rise time which related to external pull-up resistor + * can be up to spec_max_rise_ns. + * So min_high_ns = spec_min_high_ns + spec_max_rise_ns + * Note: high_ns should be (measured_high_ns + measured_rise_time) + * and measured_high_ns must meet I2C spec. + * + * max_low_ns: The maximum number of ns we can hold low. + * Note: max_low_ns should be (max_data_hold_time * 2 - buffer) * This is because the i2c host on Rockchip holds the data line * for half the low time. */ - if (scl_rate <= 100000) { - min_low_ns = 4700; - min_high_ns = 4000; - max_data_hold_ns = 3450; - data_hold_buffer_ns = 50; - } else { - min_low_ns = 1300; - min_high_ns = 600; - max_data_hold_ns = 900; - data_hold_buffer_ns = 50; - } - max_low_ns = max_data_hold_ns * 2 - data_hold_buffer_ns; + min_low_ns = spec_min_low_ns; + min_high_ns = spec_min_high_ns + spec_max_rise_ns; + max_low_ns = spec_max_data_hold_ns * 2 - spec_data_hold_buffer_ns; + min_total_ns = min_low_ns + min_high_ns; /* Adjust to avoid overflow */