Re: [PATCH 4/4] iio: adc: Add the PocketGo S30 analog stick MCU

From: Jonathan Cameron

Date: Fri Nov 27 2026 - 21:33:44 EST


On Fri, 27 Nov 2026 02:26:12 +0100
Romain TISSERAND <romain.tisserand@xxxxxxxxx> 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@xxxxxxxxx>
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);