All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wolfram Sang <wsa@the-dreams.de>
To: Codrin Ciubotariu <codrin.ciubotariu@microchip.com>
Cc: linux-i2c@vger.kernel.org, devicetree@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, kamel.bouhara@bootlin.com,
	Nicolas.Ferre@microchip.com, alexandre.belloni@bootlin.com,
	Ludovic.Desroches@microchip.com, robh@kernel.org,
	peda@axentia.se, linux@armlinux.org.uk
Subject: Re: [PATCH v3 3/6] i2c: at91: Send bus clear command if SDA is down
Date: Sat, 22 Feb 2020 12:44:33 +0100	[thread overview]
Message-ID: <20200222114433.GC1716@kunai> (raw)
In-Reply-To: <20200115115422.17097-4-codrin.ciubotariu@microchip.com>

[-- Attachment #1: Type: text/plain, Size: 1546 bytes --]

On Wed, Jan 15, 2020 at 01:54:19PM +0200, Codrin Ciubotariu wrote:
> After a transfer timeout, some faulty I2C slave devices might hold down
> the SDA pin. We can generate a bus clear command, hoping that the slave
> might release the pins.
> If the CLEAR command is not supported, we will use gpio recovery, if
> available, to reset the bus.
> 
> Signed-off-by: Codrin Ciubotariu <codrin.ciubotariu@microchip.com>

One thing to improve:

> +	/*
> +	 * some faulty I2C slave devices might hold SDA down;
> +	 * we can send a bus clear command, hoping that the pins will be
> +	 * released
> +	 */
> +	if (has_clear_cmd) {
> +		if (!(dev->transfer_status & AT91_TWI_SDA)) {
> +			dev_dbg(dev->dev,
> +				"SDA is down; sending bus clear command\n");
> +			if (dev->use_alt_cmd) {
> +				unsigned int acr;
> +
> +				acr = at91_twi_read(dev, AT91_TWI_ACR);
> +				acr &= ~AT91_TWI_ACR_DATAL_MASK;
> +				at91_twi_write(dev, AT91_TWI_ACR, acr);
> +			}
> +			at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_CLEAR);
> +		}

The inner if-block should be a seperate function, then you could do in
probe:

	if (has_clear_cmd)
		rinfo->recover_bus = <the above function>;
	else
		rinfo->recover_bus = i2c_generic_scl_recovery;

Then, i2c_recover_bus() will always do the right thing. More readable
and better maintainable IMO.

If this is not possible (maybe I overlooked some logic), then maybe this
will work:

	rinfo->recover_bus = <your custom function>;

and put the

	if (has_clear_cmd)

block there.


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

WARNING: multiple messages have this Message-ID (diff)
From: Wolfram Sang <wsa@the-dreams.de>
To: Codrin Ciubotariu <codrin.ciubotariu@microchip.com>
Cc: devicetree@vger.kernel.org, alexandre.belloni@bootlin.com,
	kamel.bouhara@bootlin.com, linux-kernel@vger.kernel.org,
	Ludovic.Desroches@microchip.com, linux-i2c@vger.kernel.org,
	peda@axentia.se, linux@armlinux.org.uk, robh@kernel.org,
	linux-arm-kernel@lists.infradead.org
Subject: Re: [PATCH v3 3/6] i2c: at91: Send bus clear command if SDA is down
Date: Sat, 22 Feb 2020 12:44:33 +0100	[thread overview]
Message-ID: <20200222114433.GC1716@kunai> (raw)
In-Reply-To: <20200115115422.17097-4-codrin.ciubotariu@microchip.com>


[-- Attachment #1.1: Type: text/plain, Size: 1546 bytes --]

On Wed, Jan 15, 2020 at 01:54:19PM +0200, Codrin Ciubotariu wrote:
> After a transfer timeout, some faulty I2C slave devices might hold down
> the SDA pin. We can generate a bus clear command, hoping that the slave
> might release the pins.
> If the CLEAR command is not supported, we will use gpio recovery, if
> available, to reset the bus.
> 
> Signed-off-by: Codrin Ciubotariu <codrin.ciubotariu@microchip.com>

One thing to improve:

> +	/*
> +	 * some faulty I2C slave devices might hold SDA down;
> +	 * we can send a bus clear command, hoping that the pins will be
> +	 * released
> +	 */
> +	if (has_clear_cmd) {
> +		if (!(dev->transfer_status & AT91_TWI_SDA)) {
> +			dev_dbg(dev->dev,
> +				"SDA is down; sending bus clear command\n");
> +			if (dev->use_alt_cmd) {
> +				unsigned int acr;
> +
> +				acr = at91_twi_read(dev, AT91_TWI_ACR);
> +				acr &= ~AT91_TWI_ACR_DATAL_MASK;
> +				at91_twi_write(dev, AT91_TWI_ACR, acr);
> +			}
> +			at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_CLEAR);
> +		}

The inner if-block should be a seperate function, then you could do in
probe:

	if (has_clear_cmd)
		rinfo->recover_bus = <the above function>;
	else
		rinfo->recover_bus = i2c_generic_scl_recovery;

Then, i2c_recover_bus() will always do the right thing. More readable
and better maintainable IMO.

If this is not possible (maybe I overlooked some logic), then maybe this
will work:

	rinfo->recover_bus = <your custom function>;

and put the

	if (has_clear_cmd)

block there.


[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

[-- Attachment #2: Type: text/plain, Size: 176 bytes --]

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  parent reply	other threads:[~2020-02-22 11:44 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-15 11:54 [PATCH v3 0/6] i2c bus recovery for Microchip SoCs Codrin Ciubotariu
2020-01-15 11:54 ` Codrin Ciubotariu
2020-01-15 11:54 ` Codrin Ciubotariu
2020-01-15 11:54 ` [PATCH v3 1/6] dt-bindings: i2c: at91: document optional bus recovery properties Codrin Ciubotariu
2020-01-15 11:54   ` Codrin Ciubotariu
2020-01-15 11:54   ` Codrin Ciubotariu
     [not found]   ` <20200115115422.17097-2-codrin.ciubotariu-UWL1GkI3JZL3oGB3hsPCZA@public.gmane.org>
2020-01-27  8:49     ` Ludovic Desroches
2020-01-27  8:49       ` Ludovic Desroches
2020-01-27  8:49       ` Ludovic Desroches
2020-02-22 11:33     ` Wolfram Sang
2020-02-22 11:33       ` Wolfram Sang
2020-02-22 11:33       ` Wolfram Sang
2020-01-15 11:54 ` [PATCH v3 2/6] i2c: at91: implement i2c bus recovery Codrin Ciubotariu
2020-01-15 11:54   ` Codrin Ciubotariu
2020-01-15 11:54   ` Codrin Ciubotariu
2020-01-27  8:50   ` Ludovic Desroches
2020-01-27  8:50     ` Ludovic Desroches
2020-01-27  8:50     ` Ludovic Desroches
2020-02-22 11:33   ` Wolfram Sang
2020-02-22 11:33     ` Wolfram Sang
2020-01-15 11:54 ` [PATCH v3 3/6] i2c: at91: Send bus clear command if SDA is down Codrin Ciubotariu
2020-01-15 11:54   ` Codrin Ciubotariu
2020-01-15 11:54   ` Codrin Ciubotariu
2020-01-27  8:54   ` Ludovic Desroches
2020-01-27  8:54     ` Ludovic Desroches
2020-01-27  8:54     ` Ludovic Desroches
2020-02-22 11:44   ` Wolfram Sang [this message]
2020-02-22 11:44     ` Wolfram Sang
2020-01-15 11:54 ` [PATCH v3 4/6] ARM: at91/dt: sama5d3: add i2c gpio pinctrl Codrin Ciubotariu
2020-01-15 11:54   ` Codrin Ciubotariu
2020-01-15 11:54   ` Codrin Ciubotariu
     [not found]   ` <20200115115422.17097-5-codrin.ciubotariu-UWL1GkI3JZL3oGB3hsPCZA@public.gmane.org>
2020-01-27  8:55     ` Ludovic Desroches
2020-01-27  8:55       ` Ludovic Desroches
2020-01-27  8:55       ` Ludovic Desroches
2020-01-15 11:54 ` [PATCH v3 5/6] ARM: at91/dt: sama5d4: " Codrin Ciubotariu
2020-01-15 11:54   ` Codrin Ciubotariu
2020-01-15 11:54   ` Codrin Ciubotariu
     [not found]   ` <20200115115422.17097-6-codrin.ciubotariu-UWL1GkI3JZL3oGB3hsPCZA@public.gmane.org>
2020-01-27  8:56     ` Ludovic Desroches
2020-01-27  8:56       ` Ludovic Desroches
2020-01-27  8:56       ` Ludovic Desroches
     [not found] ` <20200115115422.17097-1-codrin.ciubotariu-UWL1GkI3JZL3oGB3hsPCZA@public.gmane.org>
2020-01-15 11:54   ` [PATCH v3 6/6] ARM: at91/dt: sama5d2: " Codrin Ciubotariu
2020-01-15 11:54     ` Codrin Ciubotariu
2020-01-15 11:54     ` Codrin Ciubotariu
2020-01-27  8:57     ` Ludovic Desroches
2020-01-27  8:57       ` Ludovic Desroches
2020-01-27  8:57       ` Ludovic Desroches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200222114433.GC1716@kunai \
    --to=wsa@the-dreams.de \
    --cc=Ludovic.Desroches@microchip.com \
    --cc=Nicolas.Ferre@microchip.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=codrin.ciubotariu@microchip.com \
    --cc=devicetree@vger.kernel.org \
    --cc=kamel.bouhara@bootlin.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=peda@axentia.se \
    --cc=robh@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.