Re: [PATCH v2 2/2] iio: adc: Add rtq6056 support

From: Jonathan Cameron
Date: Thu Jul 07 2022 - 12:53:17 EST


On Tue, 5 Jul 2022 09:41:39 +0800
ChiYuan Huang <u0084500@xxxxxxxxx> wrote:

> Andy Shevchenko <andy.shevchenko@xxxxxxxxx> 於 2022年7月5日 週二 清晨5:52寫道:
> >
> > On Mon, Jul 4, 2022 at 9:27 AM ChiYuan Huang <u0084500@xxxxxxxxx> wrote:
> > > ChiYuan Huang <u0084500@xxxxxxxxx> 於 2022年7月4日 週一 上午11:16寫道:
> > > > Andy Shevchenko <andy.shevchenko@xxxxxxxxx> 於 2022年7月1日 週五 下午6:05寫道:
> > > > > On Wed, Jun 29, 2022 at 4:23 AM cy_huang <u0084500@xxxxxxxxx> wrote:
> >
> > ...
> >
> > > > > > + *val = DIV_ROUND_UP(1000000, sample_time);
> > > > >
> > > > > USEC_PER_SEC ?
> > > > >
> > > > No, sample time is (vshunt convesion time + vbus conversion time) *
> > > > average sample.
> > > > And the sample freq returns the unit by HZ (sample frequency per second)
> > > >
> > > The 'sample time' is unit by micro-second like as you mentioned.
> >
> > Ah, then it should be MICRO, so we will get Hz.
> >
> > > > > > + return IIO_VAL_INT;
> > > > > > +}
> >
> > ...
> >
> > > > > > + struct {
> > > > > > + u16 vals[RTQ6056_MAX_CHANNEL];
> > > > > > + int64_t timestamp;
> > > > > > + } data __aligned(8);
> > > > >
> > > > > Hmm... alignment of this struct will be at least 4 bytes, but
> > > > > shouldn't we rather be sure that the timestamp member is aligned
> > > > > properly? Otherwise this seems fragile and dependent on
> > > > > RTQ6056_MAX_CHANNEL % 4 == 0.
> > > > >
> > > > Yap, from the 'max channel', it already guarantee this struct will be
> > > > aligned at lease 4.
> > > > Actually, It can be removed.
> >
> > I think for the safest side it should be given to the timestamp member. No?
> >
> Sorry, following your comment, Why to use 'align' for the timestamp member?
> the data member already guarantee 2 * 4 = 8 byte, then timestamp will
> be 8 byte aligned, right?
>
> what you mentioned is to put __aligned(8) only for timestamp.
>
> I try to put aligned in two ways ( one is only for timestamp, another
> is the whole struct). the result is the same.
> From my thinking, in this case, the struct is already 8 byte aligned
> for timestamp member. don't you think to put 'aligned' is redundant?

On the 8 byte alignment question... Look up alignment of s64 on x86_32...
It's 4 byte aligned. We had a lot of 'fun' fixing this a few years ago.

So the marking of __aligned(8) for the timestamp does 2 things (and it
takes a fairly close reading of the c spec to check this).

1) Forces alignment of the timestamp. Needed so we can cheaply write
the timestamp
2) Forces alignment of the containing structure.

The combination of these 2 enforces the padding being
consistent across architectures whether or not they align s64 to
4 or 8 bytes. This last part is the subtle element that
explains why on some architectures you need the __aligned(8) on the
timestamp not the outer structure.

Jonathan




> > --
> > With Best Regards,
> > Andy Shevchenko