linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Li, Meng" <Meng.Li@windriver.com>
To: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>,
	"Jonathan Cameron" <jic23@kernel.org>
Cc: "lars@metafoo.de" <lars@metafoo.de>,
	"Michael.Hennerich@analog.com" <Michael.Hennerich@analog.com>,
	"pmeerw@pmeerw.net" <pmeerw@pmeerw.net>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-iio@vger.kernel.org" <linux-iio@vger.kernel.org>
Subject: RE: [PATCH] driver: adc: ltc2497: return directly after reading the adc conversion value
Date: Fri, 4 Jun 2021 09:04:48 +0000	[thread overview]
Message-ID: <PH0PR11MB51916BF5C390C90F66385E8BF13B9@PH0PR11MB5191.namprd11.prod.outlook.com> (raw)
In-Reply-To: <20210604085318.dpgazzldn3g3xpq6@pengutronix.de>



> -----Original Message-----
> From: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> Sent: Friday, June 4, 2021 4:53 PM
> To: Jonathan Cameron <jic23@kernel.org>
> Cc: Li, Meng <Meng.Li@windriver.com>; lars@metafoo.de;
> Michael.Hennerich@analog.com; pmeerw@pmeerw.net; linux-
> kernel@vger.kernel.org; linux-iio@vger.kernel.org
> Subject: Re: [PATCH] driver: adc: ltc2497: return directly after reading the adc
> conversion value
> 
> On Fri, Jun 04, 2021 at 09:20:43AM +0100, Jonathan Cameron wrote:
> > On Fri, 4 Jun 2021 06:43:20 +0000
> > "Li, Meng" <Meng.Li@windriver.com> wrote:
> >
> > > > -----Original Message-----
> > > > From: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> > > > Sent: Friday, June 4, 2021 2:13 PM
> > > > To: Li, Meng <Meng.Li@windriver.com>
> > > > Cc: Jonathan Cameron <jic23@kernel.org>; lars@metafoo.de;
> > > > Michael.Hennerich@analog.com; pmeerw@pmeerw.net; linux-
> > > > kernel@vger.kernel.org; linux-iio@vger.kernel.org
> > > > Subject: Re: [PATCH] driver: adc: ltc2497: return directly after
> > > > reading the adc conversion value
> > > >
> > > > Hello,
> > > >
> > > > On Fri, Jun 04, 2021 at 02:16:39AM +0000, Li, Meng wrote:
> > > > >
> > > > >
> > > > > > -----Original Message-----
> > > > > > From: Jonathan Cameron <jic23@kernel.org>
> > > > > > Sent: Friday, June 4, 2021 12:20 AM
> > > > > > To: Li, Meng <Meng.Li@windriver.com>
> > > > > > Cc: lars@metafoo.de; Michael.Hennerich@analog.com;
> > > > > > pmeerw@pmeerw.net; u.kleine-koenig@pengutronix.de; linux-
> > > > > > kernel@vger.kernel.org; linux-iio@vger.kernel.org
> > > > > > Subject: Re: [PATCH] driver: adc: ltc2497: return directly
> > > > > > after reading the adc conversion value
> > > > > >
> > > > > > [Please note: This e-mail is from an EXTERNAL e-mail address]
> > > > > >
> > > > > > On Tue,  1 Jun 2021 17:28:05 +0800 Meng.Li@windriver.com
> > > > > > wrote:
> > > > > >
> > > > > > > From: Meng Li <Meng.Li@windriver.com>
> > > > > > >
> > > > > > > When read adc conversion value with below command:
> > > > > > > cat /sys/.../iio:device0/in_voltage0-voltage1_raw
> > > > > > > There is an error reported as below:
> > > > > > > ltc2497 0-0014: i2c transfer failed: -EREMOTEIO This i2c
> > > > > > > transfer issue is introduced by commit 69548b7c2c4f ("iio:
> > > > > > > adc: ltc2497: split protocol independent part in a separate
> module").
> > > > > > > When extract the common code into ltc2497-core.c, it change
> > > > > > > the code logic of function ltc2497core_read(). With wrong
> > > > > > > reading sequence, the action of enable adc channel is sent
> > > > > > > to chip again during adc channel is in conversion status. In
> > > > > > > this way, there is no ack from chip, and then cause i2c transfer
> failed.
> > > > > > > In order to keep the code logic is the same with original
> > > > > > > ideal, it is need to return direct after reading the adc conversion
> value.
> > > >
> > > > As background about the choice of the .result_and_measure callback:
> > > > A difference between the ltc2497 (i2c) and ltc2496 (spi) is that
> > > > for the latter reading the result of the last conversion and
> > > > starting a new is a single bus operation and the one cannot be done
> without the other.
> > > >
> > > > > > > v2:
> > > > > > > According to ltc2497 datasheet, the max value of conversion
> > > > > > > time is
> > > > > > > 149.9 ms. So, add 20% to the 150msecs so that there is
> > > > > > > enough time for data conversion.
> > > > > >
> > > > > > Version change logs go below the --- as we don't want to
> > > > > > preserve them forever in the git history.
> > > > > >
> > > > > > I may have lost track of the discussion, but I thought the
> > > > > > idea was that perhaps the longer time period would remove the
> > > > > > need for the early
> > > > return?
> > > > > >
> > > > >
> > > > > No!
> > > > > I think the ret is essential.
> > > >
> > > > I'd like to understand why. Currently ltc2497core_read() looks as
> > > > follows (simplified by dropping error handling, and unrolling the
> > > > result_and_measure callback for the i2c case):
> > > >
> > > > 	ltc2497core_wait_conv()
> > > >
> > > > 	// result_and_measure(address, NULL)
> > > > 	i2c_smbus_write_byte(client, LTC2497_ENABLE | address);
> > > >
> > > > 	msleep_interruptible(LTC2497_CONVERSION_TIME_MS)
> > > >
> > > > 	// result_and_measure(address, val);
> > > > 	i2c_master_recv(client, &buf, 3);
> > > > 	i2c_smbus_write_byte(client, LTC2497_ENABLE | address);
> > > >
> > > >
> > > > With the early return you suggest to introduce with your patch you
> > > > save the last i2c_smbus_write_byte call. The data sheet indeed
> > > > claims to start a new conversion at the stop condition.
> > > >
> > > > So either the reading of the conversion result and programming of
> > > > the
> > > > (maybe) new address has to be done in a single i2c transfer, or we
> > > > have to do something like your patch.
> > > >
> > > > What I don't like about your approach is that it changes the
> > > > semantic of the callback to result_*or*_measure which is something
> > > > the spi variant cannot implement. With the current use of the
> > > > function this is fine, however if at some time in the future we
> implement a bulk conversion shortcut this hurts.
> > > >
> > > > So I suggest to do:
> > > >
> > > > ---->8----
> > > > From: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> > > > Date: Fri, 4 Jun 2021 08:02:44 +0200
> > > > Subject: [PATCH] iio: ltc2497: Fix reading conversion results
> > > >
> > > > After the result of the previous conversion is read the chip
> > > > automatically starts a new conversion and doesn't accept new i2c
> > > > transfers until this conversion is completed which makes the function
> return failure.
> > > >
> > > > So add an early return iff the programming of the new address isn't
> needed.
> > > > Note this will not fix the problem in general, but all cases that
> > > > are currently used. Once this changes we get the failure back, but
> > > > this can be addressed when the need arises.
> > > >
> > > > Fixes: 69548b7c2c4f ("iio: adc: ltc2497: split protocol
> > > > independent part in a separate module ")
> > > > Reported-by: Meng Li <Meng.Li@windriver.com>
> > > > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> > > > ---
> > > >  drivers/iio/adc/ltc2497.c | 13 +++++++++++++
> > > >  1 file changed, 13 insertions(+)
> > > >
> > > > diff --git a/drivers/iio/adc/ltc2497.c b/drivers/iio/adc/ltc2497.c
> > > > --- a/drivers/iio/adc/ltc2497.c
> > > > +++ b/drivers/iio/adc/ltc2497.c
> > > > @@ -41,6 +41,19 @@ static int ltc2497_result_and_measure(struct
> > > > ltc2497core_driverdata *ddata,
> > > >  		}
> > > >
> > > >  		*val = (be32_to_cpu(st->buf) >> 14) - (1 << 17);
> > > > +
> > > > +		/*
> > > > +		 * The part started a new conversion at the end of the above
> i2c
> > > > +		 * transfer, so if the address didn't change since the last call
> > > > +		 * everything is fine and we can return early.
> > > > +		 * If not (which should only happen when some sort of bulk
> > > > +		 * conversion is implemented) we have to program the new
> > > > +		 * address. Note that this probably fails as the conversion
> that
> > > > +		 * was triggered above is like not complete yet and the two
> > > > +		 * operations have to be done in a single transfer.
> > > > +		 */
> >
> > I'm a little confused by this comment.  It seems to say it will fail
> > if we ever do have a different address?  That doesn't seem very helpful...
> 
> It's not a real problem in the sense that it triggers today. If you want to read
> out (say) the channels 1, 5, 6 and 7, you could do:
> 
> 	start conversion for channel 1
> 	wait for the conversion to complete
> 	read out conversion for channel 1 and start channel 5
> 	wait for the conversion to complete
> 	read out conversion for channel 5 and start channel 6
> 	wait for the conversion to complete
> 	read out conversion for channel 6 and start channel 7
> 	wait for the conversion to complete
> 	read out conversion for channel 7
> 

 Have you tested above case on real hardware? Or only a inference based on data sheet?

Thanks.
Limeng

> With this procedure the step "read out conversion for channel X and start
> channel Y" has to (and can) be done in a single transfer. But the status quo is,
> that when these channels are to be read the following
> happens:
> 
> 	start conversion for channel 1
> 	wait for the conversion to complete
> 	read out conversion for channel 1 and (implicitly) start another
> conversion for channel 1
> 
> 	wait for the conversion to complete
> 
> 	start conversion for channel 5
> 	wait for the conversion to complete
> 	read out conversion for channel 5 and (implicitly) start another
> conversion for channel 5
> 
> 	wait for the conversion to complete
> 
> 	...
> 
> and ltc2497_result_and_measure is well suited to handle this.
> 
> So maybe reword the comment to:
> 
> 	The part started a new conversion at the end of the above i2c
> 	transfer. With the current implementation of how reading is
> 	implemented in ltc2497core it never happens that this new
> 	conversion should be done for a different channel which would
> 	require writing a new channel address. (Actually writing such a
> 	new address requires more effort, either another delay must be
> 	added or the now two transfers must be combined into a single
> 	one.)
> 
> 	So check the assumption that the channel really didn't change
> 	and then return early which does the right thing today.
> 
> ?
> 
> Best regards
> Uwe
> 
> --
> Pengutronix e.K.                           | Uwe Kleine-König            |
> Industrial Linux Solutions                 | https://www.pengutronix.de/ |

  reply	other threads:[~2021-06-04  9:05 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-01  9:28 [PATCH] driver: adc: ltc2497: return directly after reading the adc conversion value Meng.Li
2021-06-03 16:20 ` Jonathan Cameron
2021-06-04  2:16   ` Li, Meng
2021-06-04  6:13     ` Uwe Kleine-König
2021-06-04  6:43       ` Li, Meng
2021-06-04  8:20         ` Jonathan Cameron
2021-06-04  8:53           ` Uwe Kleine-König
2021-06-04  9:04             ` Li, Meng [this message]
2021-06-04  9:32               ` Uwe Kleine-König
  -- strict thread matches above, loose matches on Subject: below --
2021-05-12  4:57 Meng.Li
2021-05-18 17:46 ` Jonathan Cameron
2021-05-19  9:21 ` Uwe Kleine-König
2021-05-21 17:01   ` Jonathan Cameron
2021-05-24  2:49     ` Li, Meng
2021-05-25  8:19       ` Jonathan Cameron
2021-05-24  2:53   ` Li, Meng
2021-05-25  8:22     ` Felix Knopf
2021-05-25 10:36       ` Li, Meng

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=PH0PR11MB51916BF5C390C90F66385E8BF13B9@PH0PR11MB5191.namprd11.prod.outlook.com \
    --to=meng.li@windriver.com \
    --cc=Michael.Hennerich@analog.com \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pmeerw@pmeerw.net \
    --cc=u.kleine-koenig@pengutronix.de \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).