From patchwork Fri Jun 16 07:42:57 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phil Reid X-Patchwork-Id: 776603 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3wpsmf0Rltz9sBR for ; Fri, 16 Jun 2017 17:43:14 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752469AbdFPHnL (ORCPT ); Fri, 16 Jun 2017 03:43:11 -0400 Received: from anchovy3.45ru.net.au ([203.30.46.155]:49624 "EHLO anchovy.45ru.net.au" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752369AbdFPHnL (ORCPT ); Fri, 16 Jun 2017 03:43:11 -0400 Received: (qmail 21574 invoked by uid 5089); 16 Jun 2017 07:43:08 -0000 Received: by simscan 1.2.0 ppid: 21500, pid: 21501, t: 0.0505s scanners: regex: 1.2.0 attach: 1.2.0 clamav: 0.88.3/m:40/d:1950 X-RBL: $rbltext Received: from unknown (HELO preid-centos7.electromag.com.au) (preid@electromag.com.au@203.59.230.133) by anchovy2.45ru.net.au with ESMTPA; 16 Jun 2017 07:43:08 -0000 Received: by preid-centos7.electromag.com.au (Postfix, from userid 1000) id 24BAA309F2720; Fri, 16 Jun 2017 15:43:05 +0800 (AWST) From: Phil Reid To: jarkko.nikula@linux.intel.com, andriy.shevchenko@linux.intel.com, mika.westerberg@linux.intel.com, wsa@the-dreams.de, tim@krieglstein.org, linux-i2c@vger.kernel.org Cc: Phil Reid Subject: [PATCH v2 2/2] i2c: designware: add i2c gpio recovery option Date: Fri, 16 Jun 2017 15:42:57 +0800 Message-Id: <1497598977-37622-3-git-send-email-preid@electromag.com.au> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1497598977-37622-1-git-send-email-preid@electromag.com.au> References: <1497598977-37622-1-git-send-email-preid@electromag.com.au> Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org From: Tim Sander This patch contains much input from Phil Reid and has been tested on Intel/Altera Cyclone V SOC Hardware with Altera GPIO's for the SCL and SDA GPIO's. I am still a little unsure about the recover in the timeout case (i2c-designware-core.c:770) as i could not test this codepath. Signed-off-by: Tim Sander Tested-by: Phil Reid Signed-off-by: Phil Reid --- drivers/i2c/busses/i2c-designware-core.c | 12 ++++-- drivers/i2c/busses/i2c-designware-core.h | 1 + drivers/i2c/busses/i2c-designware-platdrv.c | 58 +++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/drivers/i2c/busses/i2c-designware-core.c b/drivers/i2c/busses/i2c-designware-core.c index 3c41995..9696433 100644 --- a/drivers/i2c/busses/i2c-designware-core.c +++ b/drivers/i2c/busses/i2c-designware-core.c @@ -463,7 +463,11 @@ static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev) while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) { if (timeout <= 0) { dev_warn(dev->dev, "timeout waiting for bus ready\n"); - return -ETIMEDOUT; + i2c_recover_bus(&dev->adapter); + + if (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) + return -ETIMEDOUT; + return 0; } timeout--; usleep_range(1000, 1100); @@ -719,9 +723,10 @@ static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev) for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]); - if (abort_source & DW_IC_TX_ARB_LOST) + if (abort_source & DW_IC_TX_ARB_LOST) { + i2c_recover_bus(&dev->adapter); return -EAGAIN; - else if (abort_source & DW_IC_TX_ABRT_GCALL_READ) + } else if (abort_source & DW_IC_TX_ABRT_GCALL_READ) return -EINVAL; /* wrong msgs[] data */ else return -EIO; @@ -766,6 +771,7 @@ static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev) if (!wait_for_completion_timeout(&dev->cmd_complete, adap->timeout)) { dev_err(dev->dev, "controller timed out\n"); /* i2c_dw_init implicitly disables the adapter */ + i2c_recover_bus(&dev->adapter); i2c_dw_init(dev); ret = -ETIMEDOUT; goto done; diff --git a/drivers/i2c/busses/i2c-designware-core.h b/drivers/i2c/busses/i2c-designware-core.h index a7cf429..dc203e4 100644 --- a/drivers/i2c/busses/i2c-designware-core.h +++ b/drivers/i2c/busses/i2c-designware-core.h @@ -129,6 +129,7 @@ struct dw_i2c_dev { int (*acquire_lock)(struct dw_i2c_dev *dev); void (*release_lock)(struct dw_i2c_dev *dev); bool pm_disabled; + struct i2c_bus_recovery_info rinfo; }; #define ACCESS_SWAP 0x00000001 diff --git a/drivers/i2c/busses/i2c-designware-platdrv.c b/drivers/i2c/busses/i2c-designware-platdrv.c index d1263b8..f88c061 100644 --- a/drivers/i2c/busses/i2c-designware-platdrv.c +++ b/drivers/i2c/busses/i2c-designware-platdrv.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -206,6 +207,59 @@ static void dw_i2c_set_fifo_size(struct dw_i2c_dev *dev, int id) } } +static void i2c_dw_prepare_recovery(struct i2c_adapter *adap) +{ + struct platform_device *pdev = to_platform_device(&adap->dev); + struct dw_i2c_dev *i_dev = platform_get_drvdata(pdev); + + i2c_dw_disable(i_dev); + reset_control_assert(i_dev->rst); + i2c_dw_plat_prepare_clk(i_dev, false); +} + +static void i2c_dw_unprepare_recovery(struct i2c_adapter *adap) +{ + struct platform_device *pdev = to_platform_device(&adap->dev); + struct dw_i2c_dev *i_dev = platform_get_drvdata(pdev); + + i2c_dw_plat_prepare_clk(i_dev, true); + reset_control_deassert(i_dev->rst); + i2c_dw_init(i_dev); +} + +static int i2c_dw_init_recovery_info(struct dw_i2c_dev *dev, + struct i2c_adapter *adap) +{ + struct i2c_bus_recovery_info *rinfo = &dev->rinfo; + struct gpio_desc *gpio; + int r; + + gpio = devm_gpiod_get(dev->dev, "scl", GPIOD_OUT_HIGH); + if (IS_ERR(gpio)) { + r = PTR_ERR(gpio); + if ((r == -ENOENT) || (r == -ENOENT)) + return 0; + return r; + } + rinfo->scl_gpiod = gpio; + + gpio = devm_gpiod_get_optional(dev->dev, "sda", GPIOD_IN); + if (IS_ERR(gpio)) + return PTR_ERR(gpio); + rinfo->sda_gpiod = gpio; + + rinfo->recover_bus = i2c_generic_scl_recovery; + rinfo->prepare_recovery = i2c_dw_prepare_recovery; + rinfo->unprepare_recovery = i2c_dw_unprepare_recovery; + adap->bus_recovery_info = rinfo; + + dev_info(dev->dev, + "adapter: %s running with gpio recovery mode! scl:%i sda:%i\n", + adap->name, !!rinfo->scl_gpiod, !!rinfo->sda_gpiod); + + return 0; +} + static int dw_i2c_plat_probe(struct platform_device *pdev) { struct dw_i2c_platform_data *pdata = dev_get_platdata(&pdev->dev); @@ -327,6 +381,10 @@ static int dw_i2c_plat_probe(struct platform_device *pdev) pm_runtime_enable(&pdev->dev); } + r = i2c_dw_init_recovery_info(dev, adap); + if (r == -EPROBE_DEFER) + goto exit_probe; + r = i2c_dw_probe(dev); if (r) goto exit_probe;