mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Aldo Conte <aldocontelk@gmail.com>
Cc: dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org,
	shuah@kernel.org, joshua.crofts1@gmail.com,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-kernel-mentees@lists.linux.dev
Subject: Re: [PATCH v3 8/8] iio: tcs3472: implement wait time and sampling frequency
Date: Sat, 6 Jun 2026 15:09:39 +0100	[thread overview]
Message-ID: <20260606150939.72974703@jic23-huawei> (raw)
In-Reply-To: <8392f8a6-719e-43d9-813a-0a163f450ed8@gmail.com>

On Sat, 6 Jun 2026 11:27:16 +0200
Aldo Conte <aldocontelk@gmail.com> wrote:

> On 04/06/26 11:23, Aldo Conte wrote:
> > On 22/05/26 14:34, Aldo Conte wrote:
> >   
> >>   static int tcs3472_req_data(struct tcs3472_data *data)
> >>   {
> >>       int tries = 50;
> >> @@ -166,16 +214,131 @@ static int tcs3472_read_raw(struct iio_dev *indio_dev,
> >>           *val = 0;
> >>           *val2 = (256 - data->atime) * 2400;
> >>           return IIO_VAL_INT_PLUS_MICRO;
> >> +    case IIO_CHAN_INFO_SAMP_FREQ: {
> >> +        unsigned int cycle_us = tcs3472_cycle_time_us(data);
> >> +
> >> +        tcs3472_cycle_to_freq(cycle_us, val, val2);
> >> +        return IIO_VAL_INT_PLUS_MICRO;
> >> +    }
> >>       default:
> >>           return -EINVAL;
> >>       }
> >>   }Hi Jonathan,  
> > 
> > One more thing on v4 before I send it.
> > 
> > For Sashiko's "tries too short" finding, I made the timeout dynamic
> > based on the actual cycle time:
> > 
> >      cycle_us = tcs3472_cycle_time_us(data);
> >      timeout_ms = max(1000U, (cycle_us * 2) / USEC_PER_MSEC);
> >      tries = DIV_ROUND_UP(timeout_ms, 20);
> > 
> > 2 * cycle_us gives some safety margin, and the 1-second floor keeps
> > the original behavior on default configurations.
> > 
> > This solves the timeout issue but introduces a new concern:
> > tcs3472_cycle_time_us() reads data->{enable,wlong,wtime,atime}
> > without holding the lock, both here and in the SAMP_FREQ case of
> > read_raw() (also flagged by Sashiko).
> > 
> > For the SAMP_FREQ case I'll just add guard(mutex)(&data->lock)
> > before the call:
> > 
> >      case IIO_CHAN_INFO_SAMP_FREQ: {
> >          unsigned int cycle_us;
> > 
> >          guard(mutex)(&data->lock);
> >          cycle_us = tcs3472_cycle_time_us(data);
> >          tcs3472_cycle_to_freq(cycle_us, val, val2);
> >          return IIO_VAL_INT_PLUS_MICRO;
> >      }
> > 
> > For tcs3472_req_data() I plan to use scoped_guard() only around the
> > cycle_us computation:
> > 
> >      scoped_guard(mutex, &data->lock)
> >          cycle_us = tcs3472_cycle_time_us(data);
> > 
> >      timeout_ms = max(1000U, (cycle_us * 2) / USEC_PER_MSEC);
> >      tries = DIV_ROUND_UP(timeout_ms, 20);
> > 
> >      while (tries--) {
> >          ...
> >      }
> > 
> > Could this be ok?

If this races with an update can we end up with too short a dynamic timeout?
I.e. what happens if that time changes - is the current read cycle completed
with old timing and it only affect the next one, or is it super simple and
the affect is immediate - maybe changing some threshold on a counter that
is used to trigger / stop the acquisition?

I would not expect us to either be able to rely on particular behaviour or
find it documented anywhere.  So two options.
1) Lock around the retry loop
2) Set the retry max to the worse possible case if that's not insanely long.
   Give the polling should exist early anyway it shouldn't make a practical
   difference and is always long enough.   I think the max is about 7 seconds? That's
   rather long to hold a lock for, but should never apply if real timing is a
   microseconds.  This would be my preference as it's simple.

Only remaining thing to check is there is nothing in the datasheet to imply it
is unsafe to change the timings during a capture - as if that were the case
stronger locking would be needed.

> > 
> > Thanks,
> > Aldo  
> Hi Jonathan,
> do you have any updates on this idea?
> 
> I have already implemented it and it seems to work.
> 
> If you agree, I am ready to submit the v4 of the patch.
> 
> thanks,
> -- Aldo


  reply	other threads:[~2026-06-06 14:09 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-22 12:34 [PATCH v3 0/8] iio: tcs3472: cleanups, devm conversion and wait time Aldo Conte
2026-05-22 12:34 ` [PATCH v3 1/8] iio: tcs3472: power down chip on probe failure Aldo Conte
2026-05-22 12:34 ` [PATCH v3 2/8] iio: tcs3472: sort headers alphabetically Aldo Conte
2026-05-22 12:34 ` [PATCH v3 3/8] iio: tcs3472: convert remaining locking to guard(mutex) Aldo Conte
2026-05-26 12:57   ` Jonathan Cameron
2026-05-22 12:34 ` [PATCH v3 4/8] iio: tcs3472: use ! instead of explicit NULL check Aldo Conte
2026-05-26 12:58   ` Jonathan Cameron
2026-06-04  8:31   ` Andy Shevchenko
2026-06-04  8:38     ` Joshua Crofts
2026-06-04 10:37       ` Jonathan Cameron
2026-05-22 12:34 ` [PATCH v3 5/8] iio: tcs3472: use devm for resource management Aldo Conte
2026-05-26 13:00   ` Jonathan Cameron
2026-05-22 12:34 ` [PATCH v3 6/8] iio: tcs3472: use local struct device * for remaining cases Aldo Conte
2026-05-26 13:04   ` Jonathan Cameron
2026-05-22 12:34 ` [PATCH v3 7/8] iio: tcs3472: move standalone return to default case Aldo Conte
2026-05-22 12:34 ` [PATCH v3 8/8] iio: tcs3472: implement wait time and sampling frequency Aldo Conte
2026-05-26 13:11   ` Jonathan Cameron
2026-05-26 15:10     ` Aldo Conte
2026-05-26 16:52       ` Jonathan Cameron
2026-06-04  8:10   ` Aldo Conte
2026-06-04 10:42     ` Jonathan Cameron
2026-06-04  9:23   ` Aldo Conte
2026-06-06  9:27     ` Aldo Conte
2026-06-06 14:09       ` Jonathan Cameron [this message]
2026-06-07  9:44         ` Aldo Conte

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=20260606150939.72974703@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=aldocontelk@gmail.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=joshua.crofts1@gmail.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel-mentees@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=shuah@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®