Devicetree
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Romain TISSERAND <romain.tisserand@gmail.com>
Cc: "David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org,
	sashiko-reviews@lists.linux.dev
Subject: Re: [PATCH 4/4] iio: adc: Add the PocketGo S30 analog stick MCU
Date: Sat, 28 Nov 2026 02:33:33 +0000	[thread overview]
Message-ID: <20261128023333.5873929d@jic23-hlaptop> (raw)
In-Reply-To: <20261127012612.1589039-5-romain.tisserand@gmail.com>

On Fri, 27 Nov 2026 02:26:12 +0100
Romain TISSERAND <romain.tisserand@gmail.com> wrote:

> The PocketGo S30 handheld (Allwinner A33) reads its analog stick through
> a microcontroller at address 0x60 on i2c0. A four-byte read returns the
> two raw 8-bit potentiometer readings, each followed by its complement:
> 
>   <a> <~a> <b> <~b>
> 
> The value and its complement appear to come from consecutive samples:
> they differ by a count or two at rest and by more while the stick moves
> fast. With an exact check, about one read in four is rejected, and
> about 4% still fail after three tries when polled as adc-joystick does.
> The stock driver uses an exact check and silently drops such frames.
> Accept a difference of up to 2 instead and read again, up to three
> times, beyond it or on a bus error (the MCU occasionally NACKs its
> address): over 2 x 6000 reads at rest and moving, none failed and under
> 2% needed a second try.
> 
> Add an I2C driver that exposes the two readings as raw IIO channels, so
> that adc-joystick can read the stick in polled mode. Each channel read
> fetches a fresh frame.
> 
> Signed-off-by: Romain TISSERAND <romain.tisserand@gmail.com>
Hi Romain,

A few minor things inline.

Thanks,

Jonathan

> diff --git a/drivers/iio/adc/pocketgo-s30-stick.c b/drivers/iio/adc/pocketgo-s30-stick.c
> new file mode 100644
> index 000000000000..09a60cb307d9
> --- /dev/null
> +++ b/drivers/iio/adc/pocketgo-s30-stick.c
> @@ -0,0 +1,149 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * PocketGo S30 analog stick MCU
> + *
> + * The MCU samples the two stick potentiometers and answers an I2C read of
> + * four bytes with the raw 8-bit readings, each followed by its complement:
> + *
> + *   <a> <~a> <b> <~b>
> + *
> + * The value and its complement appear to come from consecutive samples:
> + * they differ by a count or two at rest, and by more while the stick moves
> + * fast, so an exact check would drop about one read in four. Accept a
> + * difference of up to S30_STICK_TOLERANCE and read again beyond it. The
> + * readings are exposed as two IIO channels, for adc-joystick.
> + */

I'd guess this is swapping ends of the pot to detect shorts or broken connections?
That has to be on consecutive samples.

> +
> +#define S30_STICK_RETRIES	3
> +/* largest accepted difference between a reading and its complement */
> +#define S30_STICK_TOLERANCE	2
> +
> +/* signed distance of a + b from 0xff, -255..255 */
> +static int s30_stick_mismatch(u8 a, u8 b)
> +{
> +	return a + b - 0xff;
> +}
> +
> +static int s30_stick_read(struct i2c_client *client, u8 *val)
> +{
> +	u8 buf[4];
> +	int i, ret = -EIO;
> +
> +	for (i = 0; i < S30_STICK_RETRIES; i++) {

	for (unsigned int i = 0; ...)

> +		ret = i2c_master_recv(client, buf, sizeof(buf));
> +		if (ret != sizeof(buf)) {
> +			if (ret >= 0)
> +				ret = -EIO;
> +			continue;

Does this actually happen?   It is fairly unusual for a recv to
fail. 

> +		}
> +
> +		if (abs(s30_stick_mismatch(buf[0], buf[1])) > S30_STICK_TOLERANCE ||
> +		    abs(s30_stick_mismatch(buf[2], buf[3])) > S30_STICK_TOLERANCE) {
> +			ret = -EIO;
> +			continue;
> +		}
> +
> +		val[0] = buf[0];
> +		val[1] = buf[2];
> +		return 0;
> +	}
> +
> +	return ret;
> +}
> +
> +static int s30_stick_read_raw(struct iio_dev *indio_dev,
> +			      struct iio_chan_spec const *chan, int *val,
> +			      int *val2, long mask)
> +{
> +	struct i2c_client *client = iio_device_get_drvdata(indio_dev);

Prefer that the client is embedded in the iio_priv().  That is a lot
more flexible than using the iio_device drvdata.
Currently you have set the size of that to 0.  Instead provide
a structure for any state you need for the driver.


> +	u8 v[2];
> +	int ret;
> +
> +	if (mask != IIO_CHAN_INFO_RAW)
> +		return -EINVAL;
> +
> +	ret = s30_stick_read(client, v);
> +	if (ret)
> +		return ret;
> +
> +	*val = v[chan->channel];
> +	return IIO_VAL_INT;
> +}

> +
> +static const struct of_device_id s30_stick_of_match[] = {
> +	{ .compatible = "miyoo,pocketgo-s30-stick" },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(of, s30_stick_of_match);
> +
> +static const struct i2c_device_id s30_stick_id[] = {
> +	{ "pocketgo-s30-stick" },

Sashiko got this.  Named initializers for these structures.
There is some rework going on that will shortly require them.

> +	{ }
> +};
> +MODULE_DEVICE_TABLE(i2c, s30_stick_id);

  parent reply	other threads:[~2026-11-28  2:33 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  1:26 [PATCH 0/4] iio: adc: Add the Miyoo A30 and PocketGo S30 analog stick MCUs Romain TISSERAND
2026-11-27  1:26 ` [PATCH 1/4] dt-bindings: iio: adc: Add the Miyoo A30 analog stick MCU Romain TISSERAND
2026-11-27  1:26 ` [PATCH 2/4] " Romain TISSERAND
2026-11-27  1:40   ` sashiko-bot
2026-11-27  8:47   ` Uwe Kleine-König
2026-11-28  2:21   ` Jonathan Cameron
2026-11-27  1:26 ` [PATCH 3/4] dt-bindings: iio: adc: Add the PocketGo S30 " Romain TISSERAND
2026-11-27  1:26 ` [PATCH 4/4] " Romain TISSERAND
2026-11-27  1:34   ` sashiko-bot
2026-11-28  2:33   ` Jonathan Cameron [this message]
2026-11-27 11:49 ` [PATCH 0/4] iio: adc: Add the Miyoo A30 and PocketGo S30 analog stick MCUs Andy Shevchenko
2026-11-28  2:11   ` Jonathan Cameron

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=20261128023333.5873929d@jic23-hlaptop \
    --to=jic23@kernel.org \
    --cc=andy@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sunxi@lists.linux.dev \
    --cc=nuno.sa@analog.com \
    --cc=robh@kernel.org \
    --cc=romain.tisserand@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /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