ARM Sunxi Platform Development
 help / color / mirror / Atom feed
* [PATCH 0/4] iio: adc: Add the Miyoo A30 and PocketGo S30 analog stick MCUs
@ 2026-11-27  1:26 Romain TISSERAND
  2026-11-27  1:26 ` [PATCH 1/4] dt-bindings: iio: adc: Add the Miyoo A30 analog stick MCU Romain TISSERAND
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Romain TISSERAND @ 2026-11-27  1:26 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá, Andy Shevchenko, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, linux-iio, devicetree,
	linux-sunxi, linux-kernel, sashiko-reviews

The Miyoo A30 and the PocketGo S30 are two Allwinner A33 handhelds
whose analog stick is read by a small microcontroller: the MCU samples
the stick potentiometers and hands the raw 8-bit readings to the SoC,
on a UART (A30, a stream of frames at 9600 baud) or over I2C (S30, a
four-byte read with complements). Neither MCU takes commands.

Both drivers expose the readings as raw IIO channels for the existing
adc-joystick driver. The A30 MCU pushes its frames on its own, so its
driver also feeds a software IIO buffer and adc-joystick takes the stick
from it without polling. The S30 MCU only answers when read and has no
data-ready line, so adc-joystick polls it, as on the Anbernic RGxx3
boards where the stick is behind a SARADC. There is no scale: the
readings are raw ADC codes of the MCU.

Patches 1 and 2 add the A30 binding and driver, patches 3 and 4 the S30
ones. Both use the existing "miyoo" vendor prefix (the PocketGo S30 is
a Miyoo/Bittboy product). The board devicetrees will be sent to the
sunxi tree.

Testing: each patch built with W=1 for arm (multi_v7_defconfig, both
drivers =y and =m) and arm64 (defconfig, =m), no warnings;
dt_binding_check clean for both bindings; the board devicetrees pass
dtbs_check with these bindings.

On a PocketGo S30 on 7.3.1: about one single read in four fails an exact
complement check (the value and its complement differ by a count or two
at rest, up to about thirty while the stick moves fast); with the +-2
tolerance and up to three tries, polling as adc-joystick does (two reads
every 10 ms), no read failed over 2 x 6000 reads at rest and moving, and
under 2% needed a second try (exact check: about 4% failed). The in-tree
adc-joystick driver on the S30 channels reported 365 and 374 events over
15 s of full circles, 42..231 and 24..246.

On a Miyoo A30 on 7.3.1: rest 136/123; reading the channels from sysfs,
full circles 22..248 (X) and 11..221 (Y). The in-tree adc-joystick
driver, without poll-interval, takes the stick from the IIO buffer: 826
and 852 events over 15 s of full circles (the MCU sends about 67 frames
a second), 37..246 and 11..220.

Retested with this version on the A30 (7.3.1): with only one channel
enabled in the buffer, each channel gives its own axis (stick held
right: ch0 alone 141..242, ch1 alone 116..119); three unbind/bind rounds
with adc-joystick bound: no warning, the readings and the adc-joystick
device come back; at rest 139/118, +-1 over 50 sysfs reads.
On the S30 (7.3.1): at rest 152/126, no failed read; adc-joystick 1213
events over 15 s of full circles; three unbind/bind rounds: no warning,
readings and adc-joystick back.

Tool disclosure: this series was prepared with Claude Opus 5.5
(Anthropic) as a coding assistant, in an interactive session: it
captured the MCU protocols on the devices, wrote the drivers, the
bindings and the messages. I reviewed and understand it, and tested it
as described.

Romain TISSERAND (4):
  dt-bindings: iio: adc: Add the Miyoo A30 analog stick MCU
  iio: adc: Add the Miyoo A30 analog stick MCU
  dt-bindings: iio: adc: Add the PocketGo S30 analog stick MCU
  iio: adc: Add the PocketGo S30 analog stick MCU

 .../bindings/iio/adc/miyoo,a30-stick.yaml     |  42 ++++
 .../iio/adc/miyoo,pocketgo-s30-stick.yaml     |  48 +++++
 MAINTAINERS                                   |  14 ++
 drivers/iio/adc/Kconfig                       |  24 +++
 drivers/iio/adc/Makefile                      |   2 +
 drivers/iio/adc/miyoo-a30-stick.c             | 204 ++++++++++++++++++
 drivers/iio/adc/pocketgo-s30-stick.c          | 149 +++++++++++++
 7 files changed, 483 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/miyoo,a30-stick.yaml
 create mode 100644 Documentation/devicetree/bindings/iio/adc/miyoo,pocketgo-s30-stick.yaml
 create mode 100644 drivers/iio/adc/miyoo-a30-stick.c
 create mode 100644 drivers/iio/adc/pocketgo-s30-stick.c


base-commit: 92c9d64c0cf749c8369403a32630562f0e855a6c
-- 
2.43.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/4] dt-bindings: iio: adc: Add the Miyoo A30 analog stick MCU
  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 ` Romain TISSERAND
  2026-11-27  1:26 ` [PATCH 2/4] " Romain TISSERAND
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Romain TISSERAND @ 2026-11-27  1:26 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá, Andy Shevchenko, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, linux-iio, devicetree,
	linux-sunxi, linux-kernel, sashiko-reviews

The Miyoo A30 handheld (Allwinner A33) reads its analog stick through a
microcontroller that samples the two stick potentiometers and streams
the raw 8-bit readings on a UART. Describe it as a serial device with two
IIO channels, X and Y, for adc-joystick.

Signed-off-by: Romain TISSERAND <romain.tisserand@gmail.com>
---
 .../bindings/iio/adc/miyoo,a30-stick.yaml     | 42 +++++++++++++++++++
 1 file changed, 42 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/miyoo,a30-stick.yaml

diff --git a/Documentation/devicetree/bindings/iio/adc/miyoo,a30-stick.yaml b/Documentation/devicetree/bindings/iio/adc/miyoo,a30-stick.yaml
new file mode 100644
index 000000000000..6c0e997d4ff0
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/adc/miyoo,a30-stick.yaml
@@ -0,0 +1,42 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/miyoo,a30-stick.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Miyoo A30 analog stick MCU
+
+maintainers:
+  - Romain TISSERAND <romain.tisserand@gmail.com>
+
+description:
+  The Miyoo A30 handheld reads its analog stick through a microcontroller
+  that samples the stick potentiometers and streams the raw readings on a
+  UART at 9600 baud. It provides two channels, X (0) and Y (1).
+
+allOf:
+  - $ref: /schemas/serial/serial-peripheral-props.yaml#
+
+properties:
+  compatible:
+    const: miyoo,a30-stick
+
+  "#io-channel-cells":
+    const: 1
+
+  vdd-supply: true
+
+required:
+  - compatible
+  - "#io-channel-cells"
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    serial {
+        adc {
+            compatible = "miyoo,a30-stick";
+            #io-channel-cells = <1>;
+        };
+    };
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/4] iio: adc: Add the Miyoo A30 analog stick MCU
  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 ` Romain TISSERAND
  2026-11-27  1:40   ` sashiko-bot
  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
  3 siblings, 1 reply; 7+ messages in thread
From: Romain TISSERAND @ 2026-11-27  1:26 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá, Andy Shevchenko, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, linux-iio, devicetree,
	linux-sunxi, linux-kernel, sashiko-reviews

The Miyoo A30 handheld (Allwinner A33) reads its analog stick through a
microcontroller on uart0. From power-up it streams frames at 9600 baud,
about 67 per second:

  0xff <b0> <b1> <b2> <b3> 0xfe

b2 and b3 are the raw 8-bit readings of the X and Y potentiometers, the
ones the stock firmware uses. b0 and b1 also move with the stick, but
their meaning is unknown, so they are not exposed.

Add a serdev driver that exposes X and Y as two raw IIO channels. As
the MCU pushes its readings, each complete frame also goes to a software
IIO buffer (kfifo) with a timestamp: adc-joystick then takes the stick
from the buffer, without polling, both axes from the same frame. Direct
reads return the last complete frame, -EAGAIN before the first one.

Signed-off-by: Romain TISSERAND <romain.tisserand@gmail.com>
---
 MAINTAINERS                       |   7 +
 drivers/iio/adc/Kconfig           |  13 ++
 drivers/iio/adc/Makefile          |   1 +
 drivers/iio/adc/miyoo-a30-stick.c | 204 ++++++++++++++++++++++++++++++
 4 files changed, 225 insertions(+)
 create mode 100644 drivers/iio/adc/miyoo-a30-stick.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 02015eaec312..253275882ab9 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -18746,6 +18746,13 @@ M:	Konrad Dybcio <konradybcio@kernel.org>
 F:	Documentation/devicetree/bindings/power/supply/mitsumi,mm8013.yaml
 F:	drivers/power/supply/mm8013.c
 
+MIYOO A30 ANALOG STICK MCU DRIVER
+M:	Romain TISSERAND <romain.tisserand@gmail.com>
+L:	linux-iio@vger.kernel.org
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/adc/miyoo,a30-stick.yaml
+F:	drivers/iio/adc/miyoo-a30-stick.c
+
 MMP SUPPORT
 R:	Lubomir Rintel <lkundrak@v3.sk>
 L:	linux-arm-kernel@lists.infradead.org (moderated for non-subscribers)
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 1d6ed49041ab..48a0c4a88731 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -1346,6 +1346,19 @@ config MESON_SARADC
 	  To compile this driver as a module, choose M here: the
 	  module will be called meson_saradc.
 
+config MIYOO_A30_STICK
+	tristate "Miyoo A30 analog stick MCU"
+	depends on SERIAL_DEV_BUS
+	select IIO_BUFFER
+	select IIO_KFIFO_BUF
+	help
+	  Say yes here to support the analog stick microcontroller of the
+	  Miyoo A30 handheld, which streams the stick readings on a UART.
+	  The readings are exposed as IIO channels, for adc-joystick.
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called miyoo-a30-stick.
+
 config MP2629_ADC
 	tristate "Monolithic MP2629 ADC driver"
 	depends on MFD_MP2629
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index 769965295dcc..eba4d8eb716a 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -113,6 +113,7 @@ obj-$(CONFIG_MEDIATEK_MT6397_AUXADC) += mt6397-auxadc.o
 obj-$(CONFIG_MEDIATEK_MT6577_AUXADC) += mt6577_auxadc.o
 obj-$(CONFIG_MEN_Z188_ADC) += men_z188_adc.o
 obj-$(CONFIG_MESON_SARADC) += meson_saradc.o
+obj-$(CONFIG_MIYOO_A30_STICK) += miyoo-a30-stick.o
 obj-$(CONFIG_MP2629_ADC) += mp2629_adc.o
 obj-$(CONFIG_MXS_LRADC_ADC) += mxs-lradc-adc.o
 obj-$(CONFIG_NAU7802) += nau7802.o
diff --git a/drivers/iio/adc/miyoo-a30-stick.c b/drivers/iio/adc/miyoo-a30-stick.c
new file mode 100644
index 000000000000..d83761558ee7
--- /dev/null
+++ b/drivers/iio/adc/miyoo-a30-stick.c
@@ -0,0 +1,204 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Miyoo A30 analog stick MCU
+ *
+ * The MCU samples the stick potentiometers and streams binary frames on a
+ * UART from power-up, at 9600 baud, about 67 per second:
+ *
+ *   0xff <b0> <b1> <b2> <b3> 0xfe
+ *
+ * b2 and b3 are the raw 8-bit readings of the X and Y potentiometers (the
+ * ones the stock firmware uses); b0 and b1 are not documented and are not
+ * exposed. The last good frame is exposed as two IIO channels, and each
+ * good frame is pushed to an IIO buffer as it arrives, so that adc-joystick
+ * can take the stick without polling it.
+ */
+
+#include <linux/array_size.h>
+#include <linux/bits.h>
+#include <linux/cleanup.h>
+#include <linux/dev_printk.h>
+#include <linux/err.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/kfifo_buf.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/serdev.h>
+#include <linux/spinlock.h>
+#include <linux/types.h>
+
+#define A30_STICK_FRAME_LEN	6
+#define A30_STICK_START		0xff
+#define A30_STICK_END		0xfe
+#define A30_STICK_BAUD		9600
+
+struct a30_stick {
+	struct iio_dev *indio_dev;
+	spinlock_t lock;	/* protects val and valid */
+	u8 val[2];
+	bool valid;
+	u8 frame[A30_STICK_FRAME_LEN];
+	unsigned int len;
+};
+
+static size_t a30_stick_receive(struct serdev_device *serdev, const u8 *data,
+				size_t count)
+{
+	struct a30_stick *st = serdev_device_get_drvdata(serdev);
+	size_t i;
+
+	for (i = 0; i < count; i++) {
+		/* resynchronise on the start byte */
+		if (!st->len && data[i] != A30_STICK_START)
+			continue;
+
+		st->frame[st->len++] = data[i];
+		if (st->len < A30_STICK_FRAME_LEN)
+			continue;
+
+		st->len = 0;
+		if (st->frame[A30_STICK_FRAME_LEN - 1] != A30_STICK_END)
+			continue;
+
+		scoped_guard(spinlock, &st->lock) {
+			st->val[0] = st->frame[3];
+			st->val[1] = st->frame[4];
+			st->valid = true;
+		}
+
+		if (iio_buffer_enabled(st->indio_dev)) {
+			IIO_DECLARE_BUFFER_WITH_TS(u8, scan, 2) = { };
+
+			scan[0] = st->frame[3];
+			scan[1] = st->frame[4];
+			iio_push_to_buffers_with_ts(st->indio_dev, scan,
+						    sizeof(scan),
+						    iio_get_time_ns(st->indio_dev));
+		}
+	}
+
+	return count;
+}
+
+static const struct serdev_device_ops a30_stick_serdev_ops = {
+	.receive_buf = a30_stick_receive,
+};
+
+static int a30_stick_read_raw(struct iio_dev *indio_dev,
+			      struct iio_chan_spec const *chan, int *val,
+			      int *val2, long mask)
+{
+	struct a30_stick *st = iio_priv(indio_dev);
+
+	if (mask != IIO_CHAN_INFO_RAW)
+		return -EINVAL;
+
+	guard(spinlock)(&st->lock);
+	if (!st->valid)
+		return -EAGAIN;
+
+	*val = st->val[chan->channel];
+	return IIO_VAL_INT;
+}
+
+static const struct iio_info a30_stick_info = {
+	.read_raw = a30_stick_read_raw,
+};
+
+#define A30_STICK_CHAN(ch) {					\
+	.type = IIO_VOLTAGE,					\
+	.indexed = 1,						\
+	.channel = (ch),					\
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
+	.scan_index = (ch),					\
+	.scan_type = {						\
+		.sign = 'u',					\
+		.realbits = 8,					\
+		.storagebits = 8,				\
+	},							\
+}
+
+static const struct iio_chan_spec a30_stick_channels[] = {
+	A30_STICK_CHAN(0),
+	A30_STICK_CHAN(1),
+	IIO_CHAN_SOFT_TIMESTAMP(2),
+};
+
+/* every frame carries both axes: the IIO core extracts the enabled ones */
+static const unsigned long a30_stick_scan_masks[] = { BIT(0) | BIT(1), 0 };
+
+static int a30_stick_probe(struct serdev_device *serdev)
+{
+	struct device *dev = &serdev->dev;
+	struct iio_dev *indio_dev;
+	struct a30_stick *st;
+	unsigned int baud;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	st = iio_priv(indio_dev);
+	st->indio_dev = indio_dev;
+	spin_lock_init(&st->lock);
+
+	indio_dev->name = "miyoo-a30-stick";
+	indio_dev->info = &a30_stick_info;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = a30_stick_channels;
+	indio_dev->num_channels = ARRAY_SIZE(a30_stick_channels);
+	indio_dev->available_scan_masks = a30_stick_scan_masks;
+
+	ret = devm_iio_kfifo_buffer_setup(dev, indio_dev, NULL);
+	if (ret)
+		return ret;
+
+	ret = devm_iio_device_register(dev, indio_dev);
+	if (ret)
+		return ret;
+
+	/*
+	 * Open the UART last, so that it is closed first on removal: no frame
+	 * is pushed to the buffer once the IIO device is going away. Reads
+	 * return -EAGAIN until the first frame.
+	 */
+	serdev_device_set_drvdata(serdev, st);
+	serdev_device_set_client_ops(serdev, &a30_stick_serdev_ops);
+
+	ret = devm_serdev_device_open(dev, serdev);
+	if (ret)
+		return dev_err_probe(dev, ret, "cannot open the UART\n");
+
+	baud = serdev_device_set_baudrate(serdev, A30_STICK_BAUD);
+	if (baud != A30_STICK_BAUD)
+		return dev_err_probe(dev, -EINVAL, "cannot set %u baud (got %u)\n",
+				     A30_STICK_BAUD, baud);
+
+	serdev_device_set_flow_control(serdev, false);
+	ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
+	if (ret)
+		return dev_err_probe(dev, ret, "cannot set the parity\n");
+
+	return 0;
+}
+
+static const struct of_device_id a30_stick_of_match[] = {
+	{ .compatible = "miyoo,a30-stick" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, a30_stick_of_match);
+
+static struct serdev_device_driver a30_stick_driver = {
+	.driver = {
+		.name = "miyoo-a30-stick",
+		.of_match_table = a30_stick_of_match,
+	},
+	.probe = a30_stick_probe,
+};
+module_serdev_device_driver(a30_stick_driver);
+
+MODULE_AUTHOR("Romain TISSERAND <romain.tisserand@gmail.com>");
+MODULE_DESCRIPTION("Miyoo A30 analog stick MCU");
+MODULE_LICENSE("GPL");
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/4] dt-bindings: iio: adc: Add the PocketGo S30 analog stick MCU
  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:26 ` Romain TISSERAND
  2026-11-27  1:26 ` [PATCH 4/4] " Romain TISSERAND
  3 siblings, 0 replies; 7+ messages in thread
From: Romain TISSERAND @ 2026-11-27  1:26 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá, Andy Shevchenko, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, linux-iio, devicetree,
	linux-sunxi, linux-kernel, sashiko-reviews

The PocketGo S30 handheld (Allwinner A33) reads its analog stick through
a microcontroller on I2C that samples the two stick potentiometers and
returns the raw 8-bit readings. Describe it as an I2C device with two
IIO channels, for adc-joystick.

Signed-off-by: Romain TISSERAND <romain.tisserand@gmail.com>
---
 .../iio/adc/miyoo,pocketgo-s30-stick.yaml     | 48 +++++++++++++++++++
 1 file changed, 48 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/miyoo,pocketgo-s30-stick.yaml

diff --git a/Documentation/devicetree/bindings/iio/adc/miyoo,pocketgo-s30-stick.yaml b/Documentation/devicetree/bindings/iio/adc/miyoo,pocketgo-s30-stick.yaml
new file mode 100644
index 000000000000..103a11c96fbb
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/adc/miyoo,pocketgo-s30-stick.yaml
@@ -0,0 +1,48 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/miyoo,pocketgo-s30-stick.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: PocketGo S30 analog stick MCU
+
+maintainers:
+  - Romain TISSERAND <romain.tisserand@gmail.com>
+
+description:
+  The PocketGo S30 handheld reads its analog stick through a microcontroller
+  on I2C that samples the stick potentiometers and returns the raw readings.
+  It provides two channels, one per potentiometer. As the stick is mounted,
+  channel 0 follows the vertical axis and channel 1 the horizontal one.
+
+properties:
+  compatible:
+    const: miyoo,pocketgo-s30-stick
+
+  reg:
+    maxItems: 1
+
+  "#io-channel-cells":
+    const: 1
+
+  vdd-supply: true
+
+required:
+  - compatible
+  - reg
+  - "#io-channel-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        adc@60 {
+            compatible = "miyoo,pocketgo-s30-stick";
+            reg = <0x60>;
+            #io-channel-cells = <1>;
+        };
+    };
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 4/4] iio: adc: Add the PocketGo S30 analog stick MCU
  2026-11-27  1:26 [PATCH 0/4] iio: adc: Add the Miyoo A30 and PocketGo S30 analog stick MCUs Romain TISSERAND
                   ` (2 preceding siblings ...)
  2026-11-27  1:26 ` [PATCH 3/4] dt-bindings: iio: adc: Add the PocketGo S30 " Romain TISSERAND
@ 2026-11-27  1:26 ` Romain TISSERAND
  2026-11-27  1:34   ` sashiko-bot
  3 siblings, 1 reply; 7+ messages in thread
From: Romain TISSERAND @ 2026-11-27  1:26 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá, Andy Shevchenko, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, linux-iio, devicetree,
	linux-sunxi, linux-kernel, sashiko-reviews

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>
---
 MAINTAINERS                          |   7 ++
 drivers/iio/adc/Kconfig              |  11 ++
 drivers/iio/adc/Makefile             |   1 +
 drivers/iio/adc/pocketgo-s30-stick.c | 149 +++++++++++++++++++++++++++
 4 files changed, 168 insertions(+)
 create mode 100644 drivers/iio/adc/pocketgo-s30-stick.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 253275882ab9..7f99ad72e412 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -22256,6 +22256,13 @@ S:	Maintained
 F:	drivers/pnp/
 F:	include/linux/pnp.h
 
+POCKETGO S30 ANALOG STICK MCU DRIVER
+M:	Romain TISSERAND <romain.tisserand@gmail.com>
+L:	linux-iio@vger.kernel.org
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/adc/miyoo,pocketgo-s30-stick.yaml
+F:	drivers/iio/adc/pocketgo-s30-stick.c
+
 PORTUGUESE (BRAZILIAN) TRANSLATION
 M:	Daniel Pereira <danielmaraboo@gmail.com>
 L:	linux-doc@vger.kernel.org
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 48a0c4a88731..e1d619dcf0e8 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -1443,6 +1443,17 @@ config PALMAS_GPADC
 	  is used in smartphones and tablets and supports a 16 channel
 	  general purpose ADC.
 
+config POCKETGO_S30_STICK
+	tristate "PocketGo S30 analog stick MCU"
+	depends on I2C
+	help
+	  Say yes here to support the analog stick microcontroller of the
+	  PocketGo S30 handheld, read over I2C. The readings are exposed as
+	  IIO channels, for adc-joystick.
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called pocketgo-s30-stick.
+
 config QCOM_VADC_COMMON
 	tristate
 
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index eba4d8eb716a..d707ce30e728 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -123,6 +123,7 @@ obj-$(CONFIG_NXP_SAR_ADC) += nxp-sar-adc.o
 obj-$(CONFIG_PAC1921) += pac1921.o
 obj-$(CONFIG_PAC1934) += pac1934.o
 obj-$(CONFIG_PALMAS_GPADC) += palmas_gpadc.o
+obj-$(CONFIG_POCKETGO_S30_STICK) += pocketgo-s30-stick.o
 obj-$(CONFIG_QCOM_PM8XXX_XOADC) += qcom-pm8xxx-xoadc.o
 obj-$(CONFIG_QCOM_SPMI_ADC5) += qcom-spmi-adc5.o
 obj-$(CONFIG_QCOM_SPMI_ADC5_GEN3) += qcom-spmi-adc5-gen3.o
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.
+ */
+
+#include <linux/array_size.h>
+#include <linux/bits.h>
+#include <linux/dev_printk.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/iio/iio.h>
+#include <linux/math.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/types.h>
+
+#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++) {
+		ret = i2c_master_recv(client, buf, sizeof(buf));
+		if (ret != sizeof(buf)) {
+			if (ret >= 0)
+				ret = -EIO;
+			continue;
+		}
+
+		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);
+	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 iio_info s30_stick_info = {
+	.read_raw = s30_stick_read_raw,
+};
+
+#define S30_STICK_CHAN(ch) {					\
+	.type = IIO_VOLTAGE,					\
+	.indexed = 1,						\
+	.channel = (ch),					\
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
+}
+
+static const struct iio_chan_spec s30_stick_channels[] = {
+	S30_STICK_CHAN(0),
+	S30_STICK_CHAN(1),
+};
+
+static int s30_stick_probe(struct i2c_client *client)
+{
+	struct device *dev = &client->dev;
+	struct iio_dev *indio_dev;
+	u8 v[2];
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(dev, 0);
+	if (!indio_dev)
+		return -ENOMEM;
+
+	ret = s30_stick_read(client, v);
+	if (ret)
+		return dev_err_probe(dev, ret, "no answer from the stick MCU\n");
+
+	iio_device_set_drvdata(indio_dev, client);
+	indio_dev->name = "pocketgo-s30-stick";
+	indio_dev->info = &s30_stick_info;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = s30_stick_channels;
+	indio_dev->num_channels = ARRAY_SIZE(s30_stick_channels);
+
+	return devm_iio_device_register(dev, indio_dev);
+}
+
+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" },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, s30_stick_id);
+
+static struct i2c_driver s30_stick_driver = {
+	.driver = {
+		.name = "pocketgo-s30-stick",
+		.of_match_table = s30_stick_of_match,
+	},
+	.probe = s30_stick_probe,
+	.id_table = s30_stick_id,
+};
+module_i2c_driver(s30_stick_driver);
+
+MODULE_AUTHOR("Romain TISSERAND <romain.tisserand@gmail.com>");
+MODULE_DESCRIPTION("PocketGo S30 analog stick MCU");
+MODULE_LICENSE("GPL");
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 4/4] iio: adc: Add the PocketGo S30 analog stick MCU
  2026-11-27  1:26 ` [PATCH 4/4] " Romain TISSERAND
@ 2026-11-27  1:34   ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-27  1:34 UTC (permalink / raw)
  To: Romain TISSERAND; +Cc: devicetree, linux-sunxi, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] iio: adc: missing vdd-supply regulator enable in
  s30_stick_probe()
- [Low] iio: adc: positional initializer in struct i2c_device_id table
  s30_stick_id
--

commit c592863ae00c89b0a1034f60c564180d8b5bb825
Author: Romain TISSERAND <romain.tisserand@gmail.com>

iio: adc: Add the PocketGo S30 analog stick MCU

This commit adds an I2C driver for the PocketGo S30 analog stick
microcontroller, exposing the two potentiometer readings as raw IIO
voltage channels.

> diff --git a/drivers/iio/adc/pocketgo-s30-stick.c b/drivers/iio/adc/pocketgo-s30-stick.c
> new file mode 100644
> index 0000000000000..09a60cb307d97
> --- /dev/null
> +++ b/drivers/iio/adc/pocketgo-s30-stick.c

[ ... ]

> +static int s30_stick_probe(struct i2c_client *client)
> +{
> +	struct device *dev = &client->dev;
> +	struct iio_dev *indio_dev;
> +	u8 v[2];
> +	int ret;
> +
> +	indio_dev = devm_iio_device_alloc(dev, 0);
> +	if (!indio_dev)
> +		return -ENOMEM;
> +
> +	ret = s30_stick_read(client, v);
> +	if (ret)
> +		return dev_err_probe(dev, ret, "no answer from the stick MCU\n");

[Severity: Medium]
Should s30_stick_probe() enable the vdd regulator via
devm_regulator_get_enable(dev, "vdd") before communicating with the MCU?

The miyoo,pocketgo-s30-stick device tree binding declares vdd-supply.  If
a board wires vdd-supply to a switchable regulator, the MCU can be
unpowered during probe or powered off at late_initcall when the regulator
core disables unused supplies.

[ ... ]

> +static const struct i2c_device_id s30_stick_id[] = {
> +	{ "pocketgo-s30-stick" },
> +	{ }
> +};

[Severity: Low]
This isn't a bug, but I2C subsystem convention prefers named initializers
({ .name = "pocketgo-s30-stick" }) for struct i2c_device_id entries.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127012612.1589039-1-romain.tisserand@gmail.com?part=4

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/4] iio: adc: Add the Miyoo A30 analog stick MCU
  2026-11-27  1:26 ` [PATCH 2/4] " Romain TISSERAND
@ 2026-11-27  1:40   ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-27  1:40 UTC (permalink / raw)
  To: Romain TISSERAND; +Cc: devicetree, linux-sunxi, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] iio: adc: unlocked buffer push in a30_stick_receive() races
  with update
- [Medium] iio: adc: missing vdd-supply regulator enablement in
  a30_stick_probe()
- [Medium] iio: adc: premature IIO device registration in
  a30_stick_probe()
- [Medium] iio: adc: broken frame resynchronization on error in
  a30_stick_receive()
--

commit a72f188bfdce946d5cc6f759b74732e22612ab52
Author: Romain TISSERAND <romain.tisserand@gmail.com>

iio: adc: Add the Miyoo A30 analog stick MCU

This commit adds an IIO ADC serdev driver for the Miyoo A30 analog stick
microcontroller, exposing the X and Y axes through direct raw reads and a
kfifo buffer.

> diff --git a/drivers/iio/adc/miyoo-a30-stick.c b/drivers/iio/adc/miyoo-a30-stick.c
> new file mode 100644
> index 0000000000000..d83761558ee75
> --- /dev/null
> +++ b/drivers/iio/adc/miyoo-a30-stick.c
> @@ -0,0 +1,204 @@

[ ... ]

> +static size_t a30_stick_receive(struct serdev_device *serdev, const u8 *data,
> +				size_t count)
> +{
> +	struct a30_stick *st = serdev_device_get_drvdata(serdev);
> +	size_t i;
> +
> +	for (i = 0; i < count; i++) {
> +		/* resynchronise on the start byte */
> +		if (!st->len && data[i] != A30_STICK_START)
> +			continue;
> +
> +		st->frame[st->len++] = data[i];
> +		if (st->len < A30_STICK_FRAME_LEN)
> +			continue;
> +
> +		st->len = 0;
> +		if (st->frame[A30_STICK_FRAME_LEN - 1] != A30_STICK_END)
> +			continue;

[Severity: Medium]
Does resetting st->len = 0 on a bad end byte discard the real start byte of
the next frame already buffered in st->frame[1..5]?

Because the MCU streams continuously from power-up, opening the UART mid-frame
can latch onto a payload byte equal to 0xff (for example, when an axis is at
full deflection 0xff).  Consuming 6 bytes and resetting st->len = 0 discards
the real 0xff start byte inside st->frame and leaves the parser at the exact
same payload offset for the next 6-byte frame, dropping every frame for as
long as that axis stays at 0xff.

Scanning st->frame[1..5] for the next A30_STICK_START and shifting the
remaining bytes to the front of st->frame would allow the parser to
resynchronize within a single frame.

> +
> +		scoped_guard(spinlock, &st->lock) {
> +			st->val[0] = st->frame[3];
> +			st->val[1] = st->frame[4];
> +			st->valid = true;
> +		}
> +
> +		if (iio_buffer_enabled(st->indio_dev)) {
> +			IIO_DECLARE_BUFFER_WITH_TS(u8, scan, 2) = { };
> +
> +			scan[0] = st->frame[3];
> +			scan[1] = st->frame[4];
> +			iio_push_to_buffers_with_ts(st->indio_dev, scan,
> +						    sizeof(scan),
> +						    iio_get_time_ns(st->indio_dev));
> +		}
> +	}
> +
> +	return count;
> +}

[Severity: High]
Can checking iio_buffer_enabled() and calling iio_push_to_buffers_with_ts()
without claiming buffer mode race with __iio_update_buffers()?

The UART stream is never stopped across buffer state transitions, so
a30_stick_receive() runs asynchronously in the TTY flip-buffer workqueue
while a consumer (such as adc_joystick_open() or sysfs buffer0/enable)
enables, reconfigures, or disables buffers under iio_dev_opaque->mlock:

CPU0                                  CPU1 (kworker)
__iio_update_buffers()
  iio_enable_buffers()
    iio_dev_opaque->currentmode = ...
    iio_update_demux()
      iio_buffer_update_demux()
        kfree(buffer->demux_bounce);
        buffer->demux_bounce = NULL;
        iio_buffer_add_demux()
                                      a30_stick_receive()
                                        if (iio_buffer_enabled(...))
                                          iio_push_to_buffers_with_ts()
                                            ...
                                              iio_demux()
                                                memcpy(buffer->demux_bounce + ..., ...)
                                                <-- NULL pointer dereference
        buffer->demux_bounce = kzalloc(...);

When buffers are being disabled or reconfigured, the unlocked push also races
with iio_buffer_deactivate() modifying iio_dev_opaque->buffer_list and dropping
the buffer reference, and with iio_request_update_kfifo() freeing and
reallocating the kfifo.

Wrapping the push in iio_device_try_claim_buffer_mode() and
iio_device_release_buffer_mode() serializes it against buffer transitions.

[ ... ]

> +static int a30_stick_probe(struct serdev_device *serdev)
> +{
> +	struct device *dev = &serdev->dev;
> +	struct iio_dev *indio_dev;
> +	struct a30_stick *st;
> +	unsigned int baud;
> +	int ret;
> +
> +	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
> +	if (!indio_dev)
> +		return -ENOMEM;

[Severity: Medium]
Should a30_stick_probe() enable the "vdd" supply with
devm_regulator_get_enable(dev, "vdd")?

The miyoo,a30-stick devicetree binding defines vdd-supply, and without
enabling it here the MCU can remain unpowered or have its rail turned off by
regulator_late_cleanup(), leaving all reads returning -EAGAIN.

[ ... ]

> +	ret = devm_iio_kfifo_buffer_setup(dev, indio_dev, NULL);
> +	if (ret)
> +		return ret;
> +
> +	ret = devm_iio_device_register(dev, indio_dev);
> +	if (ret)
> +		return ret;
> +
> +	/*
> +	 * Open the UART last, so that it is closed first on removal: no frame
> +	 * is pushed to the buffer once the IIO device is going away. Reads
> +	 * return -EAGAIN until the first frame.
> +	 */
> +	serdev_device_set_drvdata(serdev, st);
> +	serdev_device_set_client_ops(serdev, &a30_stick_serdev_ops);
> +
> +	ret = devm_serdev_device_open(dev, serdev);
> +	if (ret)
> +		return dev_err_probe(dev, ret, "cannot open the UART\n");
> +
> +	baud = serdev_device_set_baudrate(serdev, A30_STICK_BAUD);
> +	if (baud != A30_STICK_BAUD)
> +		return dev_err_probe(dev, -EINVAL, "cannot set %u baud (got %u)\n",
> +				     A30_STICK_BAUD, baud);
> +
> +	serdev_device_set_flow_control(serdev, false);
> +	ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
> +	if (ret)
> +		return dev_err_probe(dev, ret, "cannot set the parity\n");

[Severity: Medium]
Does calling devm_iio_device_register() before opening and configuring the
UART expose the IIO device to consumers before the transport is ready?

If devm_serdev_device_open() (for example on -EPROBE_DEFER),
serdev_device_set_baudrate(), or serdev_device_set_parity() fails, consumers
such as adc-joystick or userspace can bind to an IIO device that is
immediately torn down, and opening the port while the IIO device is already
live starts reception with default line settings before baudrate, flow
control, and parity are configured.

Once a30_stick_receive() uses iio_device_try_claim_buffer_mode(),
iio_device_unregister() disables all buffers under mlock on removal, so the
UART can be opened and configured before devm_iio_device_register().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127012612.1589039-1-romain.tisserand@gmail.com?part=2

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-27  1:40 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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  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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox