From patchwork Thu Sep 20 16:14:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfram Sang X-Patchwork-Id: 972517 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-i2c-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=sang-engineering.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 42GMHz5V1gz9sBn for ; Fri, 21 Sep 2018 02:14:39 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729753AbeITV6s (ORCPT ); Thu, 20 Sep 2018 17:58:48 -0400 Received: from sauhun.de ([88.99.104.3]:60916 "EHLO pokefinder.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726700AbeITV6r (ORCPT ); Thu, 20 Sep 2018 17:58:47 -0400 Received: from localhost (i577B999D.versanet.de [87.123.153.157]) by pokefinder.org (Postfix) with ESMTPSA id 36AD82E35A1; Thu, 20 Sep 2018 18:14:33 +0200 (CEST) From: Wolfram Sang To: linux-i2c@vger.kernel.org Cc: linux-renesas-soc@vger.kernel.org, Stefan Lengfeld , preid@electromag.com.au, linux-omap@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Keerthy , Tero Kristo , Grygorii Strashko , Andy Shevchenko , Wolfram Sang Subject: [RFC PATCH 1/4] i2c: core: remove outdated DEBUG output Date: Thu, 20 Sep 2018 18:14:20 +0200 Message-Id: <20180920161423.13990-2-wsa+renesas@sang-engineering.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180920161423.13990-1-wsa+renesas@sang-engineering.com> References: <20180920161423.13990-1-wsa+renesas@sang-engineering.com> Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org The usefulness of this debug output is questionable. It covers only direct i2c_transfer calls and no SMBUS calls, neither direct nor emulated ones. And the latter one is largely used in the kernel, so a lot of stuff is missed. Also, we have a proper tracing mechanism for all these kinds of transfers in place for years now. Remove this old one. Signed-off-by: Wolfram Sang Acked-by: Peter Rosin --- drivers/i2c/i2c-core-base.c | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index 9ee9a15e7134..c2b352c46fae 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -1940,16 +1940,6 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) */ if (adap->algo->master_xfer) { -#ifdef DEBUG - for (ret = 0; ret < num; ret++) { - dev_dbg(&adap->dev, - "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n", - ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W', - msgs[ret].addr, msgs[ret].len, - (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : ""); - } -#endif - if (in_atomic() || irqs_disabled()) { ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT); if (!ret) From patchwork Thu Sep 20 16:14:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfram Sang X-Patchwork-Id: 972516 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-i2c-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=sang-engineering.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 42GMHw38XXz9sBq for ; Fri, 21 Sep 2018 02:14:36 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728413AbeITV6r (ORCPT ); Thu, 20 Sep 2018 17:58:47 -0400 Received: from sauhun.de ([88.99.104.3]:60932 "EHLO pokefinder.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726799AbeITV6r (ORCPT ); Thu, 20 Sep 2018 17:58:47 -0400 Received: from localhost (i577B999D.versanet.de [87.123.153.157]) by pokefinder.org (Postfix) with ESMTPSA id AC2962E35A2; Thu, 20 Sep 2018 18:14:33 +0200 (CEST) From: Wolfram Sang To: linux-i2c@vger.kernel.org Cc: linux-renesas-soc@vger.kernel.org, Stefan Lengfeld , preid@electromag.com.au, linux-omap@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Keerthy , Tero Kristo , Grygorii Strashko , Andy Shevchenko , Wolfram Sang Subject: [RFC PATCH 2/4] i2c: core: remove level of indentation in i2c_transfer Date: Thu, 20 Sep 2018 18:14:21 +0200 Message-Id: <20180920161423.13990-3-wsa+renesas@sang-engineering.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180920161423.13990-1-wsa+renesas@sang-engineering.com> References: <20180920161423.13990-1-wsa+renesas@sang-engineering.com> Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org Using the common kernel pattern to bail out at the beginning if some conditions are not met, we can save a level of indentation. No functional change. Signed-off-by: Wolfram Sang Acked-by: Peter Rosin --- drivers/i2c/i2c-core-base.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index c2b352c46fae..799776c6d421 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -1922,6 +1922,11 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) { int ret; + if (!adap->algo->master_xfer) { + dev_dbg(&adap->dev, "I2C level transfers not supported\n"); + return -EOPNOTSUPP; + } + /* REVISIT the fault reporting model here is weak: * * - When we get an error after receiving N bytes from a slave, @@ -1938,25 +1943,19 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) * one (discarding status on the second message) or errno * (discarding status on the first one). */ - - if (adap->algo->master_xfer) { - if (in_atomic() || irqs_disabled()) { - ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT); - if (!ret) - /* I2C activity is ongoing. */ - return -EAGAIN; - } else { - i2c_lock_bus(adap, I2C_LOCK_SEGMENT); - } - - ret = __i2c_transfer(adap, msgs, num); - i2c_unlock_bus(adap, I2C_LOCK_SEGMENT); - - return ret; + if (in_atomic() || irqs_disabled()) { + ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT); + if (!ret) + /* I2C activity is ongoing. */ + return -EAGAIN; } else { - dev_dbg(&adap->dev, "I2C level transfers not supported\n"); - return -EOPNOTSUPP; + i2c_lock_bus(adap, I2C_LOCK_SEGMENT); } + + ret = __i2c_transfer(adap, msgs, num); + i2c_unlock_bus(adap, I2C_LOCK_SEGMENT); + + return ret; } EXPORT_SYMBOL(i2c_transfer); From patchwork Thu Sep 20 16:14:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfram Sang X-Patchwork-Id: 972518 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-i2c-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=sang-engineering.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 42GMJ03V96z9sC2 for ; Fri, 21 Sep 2018 02:14:40 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730139AbeITV6s (ORCPT ); Thu, 20 Sep 2018 17:58:48 -0400 Received: from sauhun.de ([88.99.104.3]:60958 "EHLO pokefinder.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727659AbeITV6s (ORCPT ); Thu, 20 Sep 2018 17:58:48 -0400 Received: from localhost (i577B999D.versanet.de [87.123.153.157]) by pokefinder.org (Postfix) with ESMTPSA id 1CF462E35A4; Thu, 20 Sep 2018 18:14:34 +0200 (CEST) From: Wolfram Sang To: linux-i2c@vger.kernel.org Cc: linux-renesas-soc@vger.kernel.org, Stefan Lengfeld , preid@electromag.com.au, linux-omap@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Keerthy , Tero Kristo , Grygorii Strashko , Andy Shevchenko , Wolfram Sang Subject: [RFC PATCH 3/4] i2c: core: use I2C locking behaviour also for SMBUS Date: Thu, 20 Sep 2018 18:14:22 +0200 Message-Id: <20180920161423.13990-4-wsa+renesas@sang-engineering.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180920161423.13990-1-wsa+renesas@sang-engineering.com> References: <20180920161423.13990-1-wsa+renesas@sang-engineering.com> Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org If I2C transfers are executed in atomic contexts, trylock is used instead of lock. This behaviour was missing for SMBUS, although a lot of transfers are of SMBUS type, either emulated or direct. So, factor out the locking routine into a helper and use it for I2C and SMBUS. Signed-off-by: Wolfram Sang Acked-by: Peter Rosin --- drivers/i2c/i2c-core-base.c | 11 +++-------- drivers/i2c/i2c-core-smbus.c | 7 ++++++- drivers/i2c/i2c-core.h | 12 ++++++++++++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index 799776c6d421..904b4d2ebefa 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -1943,14 +1943,9 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) * one (discarding status on the second message) or errno * (discarding status on the first one). */ - if (in_atomic() || irqs_disabled()) { - ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT); - if (!ret) - /* I2C activity is ongoing. */ - return -EAGAIN; - } else { - i2c_lock_bus(adap, I2C_LOCK_SEGMENT); - } + ret = __i2c_lock_bus_helper(adap); + if (ret) + return ret; ret = __i2c_transfer(adap, msgs, num); i2c_unlock_bus(adap, I2C_LOCK_SEGMENT); diff --git a/drivers/i2c/i2c-core-smbus.c b/drivers/i2c/i2c-core-smbus.c index 9cd66cabb84f..dbb46edb8e02 100644 --- a/drivers/i2c/i2c-core-smbus.c +++ b/drivers/i2c/i2c-core-smbus.c @@ -20,6 +20,8 @@ #include #include +#include "i2c-core.h" + #define CREATE_TRACE_POINTS #include @@ -530,7 +532,10 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, { s32 res; - i2c_lock_bus(adapter, I2C_LOCK_SEGMENT); + res = __i2c_lock_bus_helper(adapter); + if (res) + return res; + res = __i2c_smbus_xfer(adapter, addr, flags, read_write, command, protocol, data); i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT); diff --git a/drivers/i2c/i2c-core.h b/drivers/i2c/i2c-core.h index 37576f50fe20..6e98aa811980 100644 --- a/drivers/i2c/i2c-core.h +++ b/drivers/i2c/i2c-core.h @@ -29,6 +29,18 @@ extern int __i2c_first_dynamic_bus_num; int i2c_check_7bit_addr_validity_strict(unsigned short addr); +static inline int __i2c_lock_bus_helper(struct i2c_adapter *adap) +{ + int ret = 0; + + if (in_atomic() || irqs_disabled()) + ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT) ? 0 : -EAGAIN; + else + i2c_lock_bus(adap, I2C_LOCK_SEGMENT); + + return ret; +} + #ifdef CONFIG_ACPI const struct acpi_device_id * i2c_acpi_match_device(const struct acpi_device_id *matches, From patchwork Thu Sep 20 16:14:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfram Sang X-Patchwork-Id: 972520 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-i2c-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=sang-engineering.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 42GMJ15KhTz9sBq for ; Fri, 21 Sep 2018 02:14:41 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730646AbeITV6t (ORCPT ); Thu, 20 Sep 2018 17:58:49 -0400 Received: from sauhun.de ([88.99.104.3]:60980 "EHLO pokefinder.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728096AbeITV6t (ORCPT ); Thu, 20 Sep 2018 17:58:49 -0400 Received: from localhost (i577B999D.versanet.de [87.123.153.157]) by pokefinder.org (Postfix) with ESMTPSA id 76A4C2E35A8; Thu, 20 Sep 2018 18:14:34 +0200 (CEST) From: Wolfram Sang To: linux-i2c@vger.kernel.org Cc: linux-renesas-soc@vger.kernel.org, Stefan Lengfeld , preid@electromag.com.au, linux-omap@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Keerthy , Tero Kristo , Grygorii Strashko , Andy Shevchenko , Wolfram Sang Subject: [RFC PATCH 4/4] i2c: core: introduce master_xfer_irqless callback Date: Thu, 20 Sep 2018 18:14:23 +0200 Message-Id: <20180920161423.13990-5-wsa+renesas@sang-engineering.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180920161423.13990-1-wsa+renesas@sang-engineering.com> References: <20180920161423.13990-1-wsa+renesas@sang-engineering.com> Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org We had the request to access devices very late when interrupts are not available anymore multiple times now. Mostly to prepare shutdown or reboot. Allow adapters to specify a specific callback for this case. Note that we fall back to the generic master_xfer callback if this new irqless one is not present. This is intentional to preserve the previous behaviour and avoid regressions. Because there are drivers not using interrupts or because it might have worked "accidently" before. Signed-off-by: Wolfram Sang Acked-by: Peter Rosin --- drivers/i2c/i2c-core-base.c | 6 +++++- include/linux/i2c.h | 10 +++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index 904b4d2ebefa..f827446c3089 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -1887,7 +1887,11 @@ int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) /* Retry automatically on arbitration loss */ orig_jiffies = jiffies; for (ret = 0, try = 0; try <= adap->retries; try++) { - ret = adap->algo->master_xfer(adap, msgs, num); + if ((in_atomic() || irqs_disabled()) && adap->algo->master_xfer_irqless) + ret = adap->algo->master_xfer_irqless(adap, msgs, num); + else + ret = adap->algo->master_xfer(adap, msgs, num); + if (ret != -EAGAIN) break; if (time_after(jiffies, orig_jiffies + adap->timeout)) diff --git a/include/linux/i2c.h b/include/linux/i2c.h index 65b4eaed1d96..11e615123bd0 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h @@ -498,6 +498,8 @@ i2c_register_board_info(int busnum, struct i2c_board_info const *info, * @master_xfer: Issue a set of i2c transactions to the given I2C adapter * defined by the msgs array, with num messages available to transfer via * the adapter specified by adap. + * @master_xfer_irqless: same as master_xfer. Yet, not using any interrupts + * so e.g. PMICs can be accessed very late before shutdown * @smbus_xfer: Issue smbus transactions to the given I2C adapter. If this * is not present, then the bus layer will try and convert the SMBus calls * into I2C transfers instead. @@ -511,9 +513,9 @@ i2c_register_board_info(int busnum, struct i2c_board_info const *info, * be addressed using the same bus algorithms - i.e. bit-banging or the PCF8584 * to name two of the most common. * - * The return codes from the @master_xfer field should indicate the type of - * error code that occurred during the transfer, as documented in the kernel - * Documentation file Documentation/i2c/fault-codes. + * The return codes from the @master_xfer{_irqless} field should indicate the + * type of error code that occurred during the transfer, as documented in the + * Kernel Documentation file Documentation/i2c/fault-codes. */ struct i2c_algorithm { /* If an adapter algorithm can't do I2C-level access, set master_xfer @@ -524,6 +526,8 @@ struct i2c_algorithm { processed, or a negative value on error */ int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs, int num); + int (*master_xfer_irqless)(struct i2c_adapter *adap, + struct i2c_msg *msgs, int num); int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr, unsigned short flags, char read_write, u8 command, int size, union i2c_smbus_data *data);