From nobody Mon Feb 9 08:01:33 2026 Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [217.70.183.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5503F13E7CE; Wed, 6 Mar 2024 17:59:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.194 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709747978; cv=none; b=h32wv2+3DCGnyUxDdndHoRLBcu/Q5eSSjaJOfeKVehjwxCOsix19Bt4Zwpm1e/V6SAJ3/XokcqxIjUZFlf9V0r5lx1QM0FOb8nsx3PqiAyTP4d01v1729aiZjaSjKDDMMjRN1K5JqmZS/YhYwDz1Mir/XWXNLukCw7FfMXf7ok4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709747978; c=relaxed/simple; bh=Gy7lbfHSlL/QLT1Lx6f4PW9R36O2f6hGj66uu1uWcCA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Y37OX7anaeemHUwxsYEGjR6ckX/fhFUGH6h065amBXBIGqcTQ2q60ff2oNYktT8MMtlvNqDBj2Z5p0KNLuQgBFvVpr36v5mfOvRm+2Hntn5zWzZiWXW2AYuLuPTM8uqFnOCCR/XCjzOlADOPptc0vrvHUOfQ9oBcYQonzlkGaNY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=K8/oX/Lq; arc=none smtp.client-ip=217.70.183.194 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="K8/oX/Lq" Received: by mail.gandi.net (Postfix) with ESMTPSA id 240C240009; Wed, 6 Mar 2024 17:59:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1709747966; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WTr4ZdW9OXziYRW2cJb72ygJqhfyGG5XXQz111gHa7c=; b=K8/oX/LqV46gGDmfHF1MefN5IUp0OAKDtpbh7b6DixvmfZbzA37UQTps62RSsnEjTGBvSC 4Cu3ub8JHxFuCcOeKipatWgr/emOMyI59a0psVj7q1Z05Za4x6r2lua+4a4Gr89xtlwfQp LKQU8eNMm9yAqVlIyQz5plvVB7qO0+cSHn0j2vOwDyFq6Xg8uSsU5fqFlKDfpy6zQQwRdl rmSANGvt8vKkdIMXIKONoEol0T1zPkoW3x4CCjCvG3p4JAROjqOTc8RCS2GX+FKMPeOaB3 /caQN3mvU9U/VxDLy6tv+jUUgRsiFpZa/cw2Wao+IsGy0p0cFQQL4Qz1PYXUoQ== From: =?utf-8?q?Th=C3=A9o_Lebrun?= Date: Wed, 06 Mar 2024 18:59:25 +0100 Subject: [PATCH v3 05/11] i2c: nomadik: support short xfer timeouts using waitqueue & hrtimer Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240306-mbly-i2c-v3-5-605f866aa4ec@bootlin.com> References: <20240306-mbly-i2c-v3-0-605f866aa4ec@bootlin.com> In-Reply-To: <20240306-mbly-i2c-v3-0-605f866aa4ec@bootlin.com> To: Linus Walleij , Andi Shyti , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Thomas Bogendoerfer Cc: linux-arm-kernel@lists.infradead.org, linux-i2c@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, Gregory Clement , Vladimir Kondratiev , Thomas Petazzoni , Tawfik Bayouk , =?utf-8?q?Th=C3=A9o_Lebrun?= , Wolfram Sang X-Mailer: b4 0.13.0 X-GND-Sasl: theo.lebrun@bootlin.com Replace the completion by a waitqueue for synchronization from IRQ handler to task. For short timeouts, use hrtimers, else use timers. Usecase: avoid blocking the I2C bus for too long when an issue occurs. The threshold picked is one jiffy: if timeout is below that, use hrtimers. This threshold is NOT configurable. Implement behavior but do NOT change fetching of timeout. This means the timeout is unchanged (200ms) and the hrtimer case will never trigger. A waitqueue is used because it supports both desired timeout approaches. See wait_event_timeout() and wait_event_hrtimeout(). An atomic boolean serves as synchronization condition. Reviewed-by: Linus Walleij Reviewed-by: Wolfram Sang Reviewed-by: Andi Shyti Signed-off-by: Th=C3=A9o Lebrun --- drivers/i2c/busses/i2c-nomadik.c | 70 +++++++++++++++++++++++++++---------= ---- 1 file changed, 48 insertions(+), 22 deletions(-) diff --git a/drivers/i2c/busses/i2c-nomadik.c b/drivers/i2c/busses/i2c-noma= dik.c index 63ce70f763a8..2ba7d082e205 100644 --- a/drivers/i2c/busses/i2c-nomadik.c +++ b/drivers/i2c/busses/i2c-nomadik.c @@ -168,10 +168,11 @@ struct i2c_nmk_client { * @clk_freq: clock frequency for the operation mode * @tft: Tx FIFO Threshold in bytes * @rft: Rx FIFO Threshold in bytes - * @timeout: Slave response timeout (ms) + * @timeout_usecs: Slave response timeout * @sm: speed mode * @stop: stop condition. - * @xfer_complete: acknowledge completion for a I2C message. + * @xfer_wq: xfer done wait queue. + * @xfer_done: xfer done boolean. * @result: controller propogated result. */ struct nmk_i2c_dev { @@ -185,10 +186,11 @@ struct nmk_i2c_dev { u32 clk_freq; unsigned char tft; unsigned char rft; - int timeout; + u32 timeout_usecs; enum i2c_freq_mode sm; int stop; - struct completion xfer_complete; + struct wait_queue_head xfer_wq; + bool xfer_done; int result; }; =20 @@ -443,6 +445,22 @@ static void setup_i2c_controller(struct nmk_i2c_dev *p= riv) writel(priv->rft, priv->virtbase + I2C_RFTR); } =20 +static bool nmk_i2c_wait_xfer_done(struct nmk_i2c_dev *priv) +{ + if (priv->timeout_usecs < jiffies_to_usecs(1)) { + unsigned long timeout_usecs =3D priv->timeout_usecs; + ktime_t timeout =3D ktime_set(0, timeout_usecs * NSEC_PER_USEC); + + wait_event_hrtimeout(priv->xfer_wq, priv->xfer_done, timeout); + } else { + unsigned long timeout =3D usecs_to_jiffies(priv->timeout_usecs); + + wait_event_timeout(priv->xfer_wq, priv->xfer_done, timeout); + } + + return priv->xfer_done; +} + /** * read_i2c() - Read from I2C client device * @priv: private data of I2C Driver @@ -454,9 +472,9 @@ static void setup_i2c_controller(struct nmk_i2c_dev *pr= iv) */ static int read_i2c(struct nmk_i2c_dev *priv, u16 flags) { - int status =3D 0; u32 mcr, irq_mask; - unsigned long timeout; + int status =3D 0; + bool xfer_done; =20 mcr =3D load_i2c_mcr_reg(priv, flags); writel(mcr, priv->virtbase + I2C_MCR); @@ -468,7 +486,8 @@ static int read_i2c(struct nmk_i2c_dev *priv, u16 flags) /* enable the controller */ i2c_set_bit(priv->virtbase + I2C_CR, I2C_CR_PE); =20 - init_completion(&priv->xfer_complete); + init_waitqueue_head(&priv->xfer_wq); + priv->xfer_done =3D false; =20 /* enable interrupts by setting the mask */ irq_mask =3D (I2C_IT_RXFNF | I2C_IT_RXFF | @@ -484,10 +503,9 @@ static int read_i2c(struct nmk_i2c_dev *priv, u16 flag= s) writel(readl(priv->virtbase + I2C_IMSCR) | irq_mask, priv->virtbase + I2C_IMSCR); =20 - timeout =3D wait_for_completion_timeout( - &priv->xfer_complete, priv->adap.timeout); + xfer_done =3D nmk_i2c_wait_xfer_done(priv); =20 - if (timeout =3D=3D 0) { + if (!xfer_done) { /* Controller timed out */ dev_err(&priv->adev->dev, "read from slave 0x%x timed out\n", priv->cli.slave_adr); @@ -522,9 +540,9 @@ static void fill_tx_fifo(struct nmk_i2c_dev *priv, int = no_bytes) */ static int write_i2c(struct nmk_i2c_dev *priv, u16 flags) { - u32 status =3D 0; u32 mcr, irq_mask; - unsigned long timeout; + u32 status =3D 0; + bool xfer_done; =20 mcr =3D load_i2c_mcr_reg(priv, flags); =20 @@ -537,7 +555,8 @@ static int write_i2c(struct nmk_i2c_dev *priv, u16 flag= s) /* enable the controller */ i2c_set_bit(priv->virtbase + I2C_CR, I2C_CR_PE); =20 - init_completion(&priv->xfer_complete); + init_waitqueue_head(&priv->xfer_wq); + priv->xfer_done =3D false; =20 /* enable interrupts by settings the masks */ irq_mask =3D (I2C_IT_TXFOVR | I2C_IT_MAL | I2C_IT_BERR); @@ -563,10 +582,9 @@ static int write_i2c(struct nmk_i2c_dev *priv, u16 fla= gs) writel(readl(priv->virtbase + I2C_IMSCR) | irq_mask, priv->virtbase + I2C_IMSCR); =20 - timeout =3D wait_for_completion_timeout( - &priv->xfer_complete, priv->adap.timeout); + xfer_done =3D nmk_i2c_wait_xfer_done(priv); =20 - if (timeout =3D=3D 0) { + if (!xfer_done) { /* Controller timed out */ dev_err(&priv->adev->dev, "write to slave 0x%x timed out\n", priv->cli.slave_adr); @@ -816,7 +834,9 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg) priv->cli.count); init_hw(priv); } - complete(&priv->xfer_complete); + priv->xfer_done =3D true; + wake_up(&priv->xfer_wq); + =20 break; =20 @@ -826,7 +846,9 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg) init_hw(priv); =20 i2c_set_bit(priv->virtbase + I2C_ICR, I2C_IT_MAL); - complete(&priv->xfer_complete); + priv->xfer_done =3D true; + wake_up(&priv->xfer_wq); + =20 break; =20 @@ -845,7 +867,9 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg) init_hw(priv); =20 i2c_set_bit(priv->virtbase + I2C_ICR, I2C_IT_BERR); - complete(&priv->xfer_complete); + priv->xfer_done =3D true; + wake_up(&priv->xfer_wq); + } break; =20 @@ -859,7 +883,9 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg) init_hw(priv); =20 dev_err(dev, "Tx Fifo Over run\n"); - complete(&priv->xfer_complete); + priv->xfer_done =3D true; + wake_up(&priv->xfer_wq); + =20 break; =20 @@ -960,7 +986,7 @@ static void nmk_i2c_of_probe(struct device_node *np, priv->sm =3D I2C_FREQ_MODE_FAST; priv->tft =3D 1; /* Tx FIFO threshold */ priv->rft =3D 8; /* Rx FIFO threshold */ - priv->timeout =3D 200; /* Slave response timeout(ms) */ + priv->timeout_usecs =3D 200 * USEC_PER_MSEC; /* Slave response timeout */ } =20 static int nmk_i2c_probe(struct amba_device *adev, const struct amba_id *i= d) @@ -1020,7 +1046,7 @@ static int nmk_i2c_probe(struct amba_device *adev, co= nst struct amba_id *id) adap->owner =3D THIS_MODULE; adap->class =3D I2C_CLASS_DEPRECATED; adap->algo =3D &nmk_i2c_algo; - adap->timeout =3D msecs_to_jiffies(priv->timeout); + adap->timeout =3D usecs_to_jiffies(priv->timeout_usecs); snprintf(adap->name, sizeof(adap->name), "Nomadik I2C at %pR", &adev->res); =20 --=20 2.44.0