From patchwork Wed May 18 20:41:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eddie James X-Patchwork-Id: 574022 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D56F9C4332F for ; Wed, 18 May 2022 20:41:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242711AbiERUlo (ORCPT ); Wed, 18 May 2022 16:41:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242701AbiERUln (ORCPT ); Wed, 18 May 2022 16:41:43 -0400 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 763016D973; Wed, 18 May 2022 13:41:42 -0700 (PDT) Received: from pps.filterd (m0098421.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 24IKW81u021315; Wed, 18 May 2022 20:41:26 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=6Wb1PTNRCtiyiy1UokxHKChzuywEapNSam8gp2jnpt0=; b=Vl3k4om3rUSgWcvytmRPd8gy42NxeB3ArLfct+5Ln++rYLxB/BznjNTyq48ElfJowrEh XbsZGJiBQJADTjsjwDfvQpkPuQrXgTs/INlr2UV5+7/+Al6HV2DMYPkO8TpamxAUBJ91 GjXnL0Z91ckJv1jWWaHJovhGYVfI3rBGihb7hwUvs3hrsCNjp7iW4fCU3Z+fj8LVJ7B6 mqu0oy+RBKwwVseA0gE6DRbXQIusIHV7IDUTsGZfKmxwmu4Zzr6ocFidLtz5O3fuQeCB BpWcmZpBd6LqGiC/jayO2fbHLKemfe4Yak2MjH97pkoFz/KkHpV2Ew8ffmRC4WKL9Uq3 nQ== Received: from ppma01dal.us.ibm.com (83.d6.3fa9.ip4.static.sl-reverse.com [169.63.214.131]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3g57x0r7yw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 18 May 2022 20:41:26 +0000 Received: from pps.filterd (ppma01dal.us.ibm.com [127.0.0.1]) by ppma01dal.us.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 24IKcb8N003416; Wed, 18 May 2022 20:41:25 GMT Received: from b03cxnp08027.gho.boulder.ibm.com (b03cxnp08027.gho.boulder.ibm.com [9.17.130.19]) by ppma01dal.us.ibm.com with ESMTP id 3g242beprn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 18 May 2022 20:41:25 +0000 Received: from b03ledav006.gho.boulder.ibm.com (b03ledav006.gho.boulder.ibm.com [9.17.130.237]) by b03cxnp08027.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 24IKfOnX8782234 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 18 May 2022 20:41:24 GMT Received: from b03ledav006.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 60794C6055; Wed, 18 May 2022 20:41:24 +0000 (GMT) Received: from b03ledav006.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id F153EC6059; Wed, 18 May 2022 20:41:23 +0000 (GMT) Received: from v0005c16.aus.stglabs.ibm.com (unknown [9.163.6.139]) by b03ledav006.gho.boulder.ibm.com (Postfix) with ESMTP; Wed, 18 May 2022 20:41:23 +0000 (GMT) From: Eddie James To: linux-i2c@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, jic23@kernel.org, lars@metafoo.de, wsa@kernel.org, milton@us.ibm.com, peda@axentia.se, Eddie James Subject: [RFC 1/2] i2c: core: Add adapter transfer callback Date: Wed, 18 May 2022 15:41:18 -0500 Message-Id: <20220518204119.38943-2-eajames@linux.ibm.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220518204119.38943-1-eajames@linux.ibm.com> References: <20220518204119.38943-1-eajames@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: S_9GnWowkBKxopX2J-FT3fRZw8U_kvar X-Proofpoint-ORIG-GUID: S_9GnWowkBKxopX2J-FT3fRZw8U_kvar X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.874,Hydra:6.0.486,FMLib:17.11.64.514 definitions=2022-05-18_06,2022-05-17_02,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 bulkscore=0 impostorscore=0 mlxscore=0 lowpriorityscore=0 mlxlogscore=894 malwarescore=0 clxscore=1015 adultscore=0 priorityscore=1501 suspectscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205180119 Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org Add a callback function pointer to be executed after the adapter performs a transfer. The purpose of such a callback is for a client to execute some code while "owning" the bus entirely. Holding the adapter lock is insufficient in the case where the client is behind a mux, as the mux driver could perform mux selection operations on the bus while locked. Signed-off-by: Eddie James --- drivers/i2c/i2c-core-base.c | 3 +++ include/linux/i2c.h | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index d43db2c3876e..a46bfee2d845 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -2128,6 +2128,9 @@ int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) trace_i2c_result(adap, num, ret); } + if (adap->xfer_callback) + adap->xfer_callback(adap->xfer_data, ret); + return ret; } EXPORT_SYMBOL(__i2c_transfer); diff --git a/include/linux/i2c.h b/include/linux/i2c.h index fbda5ada2afc..ea773f2ee9c8 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h @@ -747,6 +747,9 @@ struct i2c_adapter { struct irq_domain *host_notify_domain; struct regulator *bus_regulator; + + void (*xfer_callback)(void *data, int xfer_rc); + void *xfer_data; }; #define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev) @@ -814,6 +817,7 @@ i2c_trylock_bus(struct i2c_adapter *adapter, unsigned int flags) static inline void i2c_unlock_bus(struct i2c_adapter *adapter, unsigned int flags) { + adapter->xfer_callback = NULL; adapter->lock_ops->unlock_bus(adapter, flags); } @@ -849,6 +853,27 @@ static inline void i2c_mark_adapter_resumed(struct i2c_adapter *adap) i2c_unlock_bus(adap, I2C_LOCK_ROOT_ADAPTER); } +/** + * i2c_adapter_xfer_callback - Register a callback function that is executed + * when a transfer completes. + * @adap: Adapter to which the callback function will be registered + * @cb: The callback function pointer + * @data: The data to pass to the callback function + * + * This function should be called with the adapter locked with + * I2C_LOCK_ROOT_ADAPTER to ensure that the whole bus is idle while the + * callback executes. + * The callback is automatically removed when the bus is unlocked to avoid + * spurious executions of the callback. + */ +static inline void i2c_adapter_xfer_callback(struct i2c_adapter *adap, + void (*cb)(void *data, int rc), + void *data) +{ + adap->xfer_callback = cb; + adap->xfer_data = data; +} + /* i2c adapter classes (bitmask) */ #define I2C_CLASS_HWMON (1<<0) /* lm_sensors, ... */ #define I2C_CLASS_DDC (1<<3) /* DDC bus on graphics adapters */ From patchwork Wed May 18 20:41:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eddie James X-Patchwork-Id: 574695 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2AA96C4332F for ; Wed, 18 May 2022 20:41:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242728AbiERUls (ORCPT ); Wed, 18 May 2022 16:41:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33246 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242730AbiERUlq (ORCPT ); Wed, 18 May 2022 16:41:46 -0400 Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 465316EC40; Wed, 18 May 2022 13:41:43 -0700 (PDT) Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 24IKDiOW017312; Wed, 18 May 2022 20:41:27 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=nvXa8af2siriVChrnEyvk2247HQGR9mvpKnd8nb1hxo=; b=rRq2hMeKB7kjfMHkKWXgNQz7oEgaYj5AFVa9s2q0Bno/4cyu5DI+dKgXYONBPYEhOpu2 iWgZKjukXAlL2vgqVMi1U4wYsWJ56ZqOZyA2aKB9nfGcZVuv+qq8X2PX1MY/mr8zhcZA 0XtRXSFQDruMw5FhSF+S669tHA5XhwTsJe8sQ0PkSt3uFW5o1+gZJId0fKhmYPZcNdmc wxpcPnyNONmNJCFjL6FLEgPhDGfWalhr3JyXddE/AI9v1L171eeJI0finBeKGwgpSK8d igL76ei/HaUkA4xbcvihKxpk1OrJWz2iKf/MsJ66mawSreKvhhLY6mP/V6Wiqr2naj9l og== Received: from ppma04wdc.us.ibm.com (1a.90.2fa9.ip4.static.sl-reverse.com [169.47.144.26]) by mx0b-001b2d01.pphosted.com (PPS) with ESMTPS id 3g57n8gk88-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 18 May 2022 20:41:27 +0000 Received: from pps.filterd (ppma04wdc.us.ibm.com [127.0.0.1]) by ppma04wdc.us.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 24IKcgbq006781; Wed, 18 May 2022 20:41:27 GMT Received: from b03cxnp07027.gho.boulder.ibm.com (b03cxnp07027.gho.boulder.ibm.com [9.17.130.14]) by ppma04wdc.us.ibm.com with ESMTP id 3g4wp5bq81-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 18 May 2022 20:41:27 +0000 Received: from b03ledav006.gho.boulder.ibm.com (b03ledav006.gho.boulder.ibm.com [9.17.130.237]) by b03cxnp07027.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 24IKfQVg15991110 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 18 May 2022 20:41:26 GMT Received: from b03ledav006.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 071E1C6055; Wed, 18 May 2022 20:41:26 +0000 (GMT) Received: from b03ledav006.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 96F1EC6059; Wed, 18 May 2022 20:41:25 +0000 (GMT) Received: from v0005c16.aus.stglabs.ibm.com (unknown [9.163.6.139]) by b03ledav006.gho.boulder.ibm.com (Postfix) with ESMTP; Wed, 18 May 2022 20:41:25 +0000 (GMT) From: Eddie James To: linux-i2c@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, jic23@kernel.org, lars@metafoo.de, wsa@kernel.org, milton@us.ibm.com, peda@axentia.se, Eddie James Subject: [RFC 2/2] iio: humidity: si7020: Use core transfer callback to sleep after reset Date: Wed, 18 May 2022 15:41:19 -0500 Message-Id: <20220518204119.38943-3-eajames@linux.ibm.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220518204119.38943-1-eajames@linux.ibm.com> References: <20220518204119.38943-1-eajames@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: hFpAPwzKJH4bwj4G3KwS1VeByDcTB9vV X-Proofpoint-GUID: hFpAPwzKJH4bwj4G3KwS1VeByDcTB9vV X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.874,Hydra:6.0.486,FMLib:17.11.64.514 definitions=2022-05-18_06,2022-05-17_02,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 malwarescore=0 mlxlogscore=999 spamscore=0 phishscore=0 mlxscore=0 adultscore=0 suspectscore=0 bulkscore=0 impostorscore=0 lowpriorityscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205180119 Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org While the SI7020 is starting up after power on or reset, any I2C commands on the bus can potentially upset the startup sequence. Therefore, the host needs to wait for the startup sequence to finish before issuing further i2c commands. This is impractical in cases where the SI7020 is on a shared bus or behind a mux, which may switch channels at any time (potentially generating I2C traffic). To resolve this, use the new transfer callback on the adapter to sleep the required interval. Signed-off-by: Eddie James --- drivers/iio/humidity/si7020.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/iio/humidity/si7020.c b/drivers/iio/humidity/si7020.c index ab6537f136ba..f19e88818bd6 100644 --- a/drivers/iio/humidity/si7020.c +++ b/drivers/iio/humidity/si7020.c @@ -103,6 +103,13 @@ static const struct iio_info si7020_info = { .read_raw = si7020_read_raw, }; +static void si7020_xfer_callback(void *data, int xfer_rc) +{ + /* Wait the maximum power-up time after software reset. */ + if (!xfer_rc) + msleep(15); +} + static int si7020_probe(struct i2c_client *client, const struct i2c_device_id *id) { @@ -115,12 +122,13 @@ static int si7020_probe(struct i2c_client *client, I2C_FUNC_SMBUS_READ_WORD_DATA)) return -EOPNOTSUPP; + i2c_lock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER); + i2c_adapter_xfer_callback(client->adapter, si7020_xfer_callback, NULL); /* Reset device, loads default settings. */ ret = i2c_smbus_write_byte(client, SI7020CMD_RESET); + i2c_unlock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER); if (ret < 0) return ret; - /* Wait the maximum power-up time after software reset. */ - msleep(15); indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); if (!indio_dev)