[PATCH 2/4] iio: adc: Add the Miyoo A30 analog stick MCU
From: Romain TISSERAND
Date: Thu Nov 26 2026 - 20:26:45 EST
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@xxxxxxxxx>
---
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@xxxxxxxxxx>
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@xxxxxxxxx>
+L: linux-iio@xxxxxxxxxxxxxxx
+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@xxxxx>
L: linux-arm-kernel@xxxxxxxxxxxxxxxxxxx (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@xxxxxxxxx>");
+MODULE_DESCRIPTION("Miyoo A30 analog stick MCU");
+MODULE_LICENSE("GPL");
--
2.43.0