* [PATCH v2 0/2] iio: magnetometer: add support for QST QMC6308
@ 2026-07-21 13:48 Jorijn van der Graaf
2026-07-21 13:48 ` [PATCH v2 1/2] dt-bindings: iio: magnetometer: add " Jorijn van der Graaf
2026-07-21 13:48 ` [PATCH v2 2/2] iio: magnetometer: add support for " Jorijn van der Graaf
0 siblings, 2 replies; 5+ messages in thread
From: Jorijn van der Graaf @ 2026-07-21 13:48 UTC (permalink / raw)
To: Jonathan Cameron
Cc: Jorijn van der Graaf, Jonathan Cameron, David Lechner,
Nuno Sá, Andy Shevchenko, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Siratul Islam, Uwe Kleine-König, Luca Weiss,
linux-iio, devicetree, linux-kernel
This adds a driver and DT binding for the QST QMC6308, a 3-axis
anisotropic magnetoresistive (AMR) magnetometer found e.g. in the
Fairphone 6. The series is based on iio/togreg.
The chip is a sibling of the QMC5883L but is not register compatible
with it, so this is a separate driver; its structure intentionally
follows qmc5883l.c. Reads are polled from the chip's periodic
measurement mode, with runtime PM dropping the chip into its suspend
mode between uses.
The binding stays a separate file rather than being folded into
qstcorp,qmc5883l.yaml: as discussed with Siratul in the v1 thread, the
two schemas share only compatible/reg/vdd-supply, and Jonathan leaned
towards keeping the files separate given that small overlap. If the DT
maintainers prefer the combined shape I'm happy to rework it that way.
Adding mount-matrix to qstcorp,qmc5883l.yaml, also raised in that
discussion, is a natural follow-up patch either way.
Tested on a Fairphone 6 (bit-banged i2c-gpio bus) using a
byte-identical backport on that device's 7.1-based kernel; on this
base the driver is build-verified (arm64, W=1 clean). v2 testing
re-covered raw reads, every ODR/oversampling/scale setting (the same
ambient field reads consistently across all four ranges), the
stale-sample discard on range changes, and runtime PM, including that
the chip autosuspends after probe without the v1 usage-counter
dance and that configuration writes land while suspended. The FP6
board DTS will be submitted separately through the qcom tree once
this lands.
Per-patch changelogs are in the individual patches.
v1: https://lore.kernel.org/20260714202842.340293-1-jorijnvdgraaf@catcrafts.net
Jorijn van der Graaf (2):
dt-bindings: iio: magnetometer: add QST QMC6308
iio: magnetometer: add support for QST QMC6308
.../iio/magnetometer/qstcorp,qmc6308.yaml | 47 ++
MAINTAINERS | 7 +
drivers/iio/magnetometer/Kconfig | 11 +
drivers/iio/magnetometer/Makefile | 1 +
drivers/iio/magnetometer/qmc6308.c | 602 ++++++++++++++++++
5 files changed, 668 insertions(+)
create mode 100644 Documentation/devicetree/bindings/iio/magnetometer/qstcorp,qmc6308.yaml
create mode 100644 drivers/iio/magnetometer/qmc6308.c
base-commit: 036d96ceec6d966339aea34d034420991e24cfac
--
2.55.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 1/2] dt-bindings: iio: magnetometer: add QST QMC6308
2026-07-21 13:48 [PATCH v2 0/2] iio: magnetometer: add support for QST QMC6308 Jorijn van der Graaf
@ 2026-07-21 13:48 ` Jorijn van der Graaf
2026-07-21 13:48 ` [PATCH v2 2/2] iio: magnetometer: add support for " Jorijn van der Graaf
1 sibling, 0 replies; 5+ messages in thread
From: Jorijn van der Graaf @ 2026-07-21 13:48 UTC (permalink / raw)
To: Jonathan Cameron
Cc: Jorijn van der Graaf, Jonathan Cameron, David Lechner,
Nuno Sá, Andy Shevchenko, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Siratul Islam, Uwe Kleine-König, Luca Weiss,
linux-iio, devicetree, linux-kernel
Add device tree bindings for the QST QMC6308, a 3-axis anisotropic
magnetoresistive (AMR) magnetometer. It is an I2C device with a single
supply and no interrupt pin, at I2C address 0x2c.
Assisted-by: Claude:claude-fable-5
Signed-off-by: Jorijn van der Graaf <jorijnvdgraaf@catcrafts.net>
---
v2:
- Spell out AMR (anisotropic magnetoresistive) (Jonathan).
- Drop the package mentions from the description and commit message
(Jonathan).
- compatible: enum -> const (Jonathan).
- Use the current GPL-2.0-only form in the SPDX identifier.
- Kept as a separate file rather than folded into
qstcorp,qmc5883l.yaml - the two schemas share only
compatible/reg/vdd-supply; see the cover letter.
v1: https://lore.kernel.org/20260714202842.340293-2-jorijnvdgraaf@catcrafts.net
.../iio/magnetometer/qstcorp,qmc6308.yaml | 47 +++++++++++++++++++
MAINTAINERS | 6 +++
2 files changed, 53 insertions(+)
create mode 100644 Documentation/devicetree/bindings/iio/magnetometer/qstcorp,qmc6308.yaml
diff --git a/Documentation/devicetree/bindings/iio/magnetometer/qstcorp,qmc6308.yaml b/Documentation/devicetree/bindings/iio/magnetometer/qstcorp,qmc6308.yaml
new file mode 100644
index 000000000000..6201a44cfe38
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/magnetometer/qstcorp,qmc6308.yaml
@@ -0,0 +1,47 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/magnetometer/qstcorp,qmc6308.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: QST QMC6308 3-Axis Magnetic Sensor
+
+maintainers:
+ - Jorijn van der Graaf <jorijnvdgraaf@catcrafts.net>
+
+description: |
+ QST QMC6308 3-axis anisotropic magnetoresistive (AMR) magnetic sensor
+ on I2C bus, with a single supply and no interrupt pin.
+ https://qstcorp.com/upload/pdf/202202/13-52-15%20QMC6308%20Datasheet%20Rev.%20F(1).pdf
+
+properties:
+ compatible:
+ const: qstcorp,qmc6308
+
+ reg:
+ maxItems: 1
+
+ vdd-supply: true
+
+ mount-matrix: true
+
+additionalProperties: false
+
+required:
+ - compatible
+ - reg
+ - vdd-supply
+
+examples:
+ - |
+ i2c {
+ #address-cells = <1>;
+ #size-cells = <0>;
+
+ magnetometer@2c {
+ compatible = "qstcorp,qmc6308";
+ reg = <0x2c>;
+ vdd-supply = <&vdd_1v8_reg>;
+ };
+ };
+...
diff --git a/MAINTAINERS b/MAINTAINERS
index 2b1ec46c5919..2315c94ad0e8 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -22061,6 +22061,12 @@ S: Maintained
F: Documentation/devicetree/bindings/iio/magnetometer/qstcorp,qmc5883l.yaml
F: drivers/iio/magnetometer/qmc5883l.c
+QST QMC6308 3-AXIS MAGNETIC SENSOR
+M: Jorijn van der Graaf <jorijnvdgraaf@catcrafts.net>
+L: linux-iio@vger.kernel.org
+S: Maintained
+F: Documentation/devicetree/bindings/iio/magnetometer/qstcorp,qmc6308.yaml
+
QT1010 MEDIA DRIVER
L: linux-media@vger.kernel.org
S: Orphan
--
2.55.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v2 2/2] iio: magnetometer: add support for QST QMC6308
2026-07-21 13:48 [PATCH v2 0/2] iio: magnetometer: add support for QST QMC6308 Jorijn van der Graaf
2026-07-21 13:48 ` [PATCH v2 1/2] dt-bindings: iio: magnetometer: add " Jorijn van der Graaf
@ 2026-07-21 13:48 ` Jorijn van der Graaf
2026-07-21 14:04 ` sashiko-bot
2026-07-21 20:19 ` Siratul Islam
1 sibling, 2 replies; 5+ messages in thread
From: Jorijn van der Graaf @ 2026-07-21 13:48 UTC (permalink / raw)
To: Jonathan Cameron
Cc: Jorijn van der Graaf, Jonathan Cameron, David Lechner,
Nuno Sá, Andy Shevchenko, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Siratul Islam, Uwe Kleine-König, Luca Weiss,
linux-iio, devicetree, linux-kernel
The QST QMC6308 is a 3-axis anisotropic magnetoresistive (AMR)
magnetometer on I2C, found e.g. in the Fairphone 6. Its register map
is not compatible with the QMC5883L's, so add a separate driver rather
than extending the QMC5883L driver.
Support raw X/Y/Z reads, output data rate, field range, filter
oversampling ratio, the mount matrix, and runtime PM.
Run measurements in the chip's periodic "normal" mode paced by the
DRDY flag rather than in its one-shot "single" mode: the datasheet
specifies no conversion time that could bound a one-shot wait, while
normal mode is paced by the specified output data rates, which also
keeps the sampling_frequency ABI meaningful.
Runtime PM puts the chip into its suspend mode after 500 ms without a
reading, dropping supply current to a few microamps. The suspended
chip retains its registers and keeps responding on I2C, so resuming
only rewrites the mode field and discards one stale sample, and
configuration changes apply even while suspended; VDD is left enabled
across runtime suspend.
Assisted-by: Claude:claude-fable-5
Signed-off-by: Jorijn van der Graaf <jorijnvdgraaf@catcrafts.net>
---
v2:
- Trim the commit message (Jonathan).
- Switch the license to GPL-2.0-only: the dual license had been
carried over from qmc5883l.c, which this driver is modeled on,
without a reason of our own (Jonathan).
- take_measurement(): use PM_RUNTIME_ACQUIRE_AUTOSUSPEND() plus
guard(mutex) and direct returns, removing the goto/cleanup.h mix
(Jonathan, Sashiko AI).
- Print the power-on failure unconditionally; the transparent acquire
no longer returns -EACCES when runtime PM is disabled, so any
remaining error is a real resume failure (Jonathan).
- Drop the pm_runtime_get_noresume()/pm_runtime_put_autosuspend()
pair in probe; the driver core idles the device after probe,
verified on hardware: the chip autosuspends after probing without a
read (Jonathan). This also removes the usage-count leak on the
devm_pm_runtime_enable() failure path (Sashiko AI).
- On a runtime-resume status-read failure, put the chip back into
suspend mode. The stale-DRDY clear itself stays in resume - only
that path knows a suspend happened - and is now documented at the
measurement poll (Jonathan).
- Write OSR2 explicitly to its power-on default in init, with a
comment; the define is no longer unused, and the commit message no
longer claims the field is left untouched (Jonathan, Sashiko AI,
Siratul).
- Drop the mod_devicetable.h include (Uwe).
- Split the POR_US comment: the define documents the power-on time
(datasheet Table 7); the conservative-bound-after-soft-reset
rationale moved to the call site (Siratul).
- Reduce the mutex comment to what the mutex protects; the
DRDY-clears-on-status-read behaviour is documented at the poll
(Siratul).
- Log and continue when the post-range-change discard read fails:
the range change took effect, so an error would mislead userspace.
If the bus recovers before the next read, one old-range sample can
slip through (Siratul).
- Group the data->{range,odr,osr} default assignments (Siratul).
- Drop the map local in take_measurement() (Siratul).
- Check the pm_runtime_set_active() return in probe.
- Soften the range-change discard comment: a conversion already in
flight can still complete at the old range, so the discard narrows
the stale-sample window rather than closing it.
- Unchanged, per the v1 thread: the periodic normal-mode measurement
approach (rationale kept in the commit message), the
QMC6308_AUTOSUSPEND_DELAY_MS name, the designated-initializer value
tables with lookup loops, and the declaration order in
take_measurement().
v1: https://lore.kernel.org/20260714202842.340293-3-jorijnvdgraaf@catcrafts.net
MAINTAINERS | 1 +
drivers/iio/magnetometer/Kconfig | 11 +
drivers/iio/magnetometer/Makefile | 1 +
drivers/iio/magnetometer/qmc6308.c | 602 +++++++++++++++++++++++++++++
4 files changed, 615 insertions(+)
create mode 100644 drivers/iio/magnetometer/qmc6308.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 2315c94ad0e8..b1fce287b329 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -22066,6 +22066,7 @@ M: Jorijn van der Graaf <jorijnvdgraaf@catcrafts.net>
L: linux-iio@vger.kernel.org
S: Maintained
F: Documentation/devicetree/bindings/iio/magnetometer/qstcorp,qmc6308.yaml
+F: drivers/iio/magnetometer/qmc6308.c
QT1010 MEDIA DRIVER
L: linux-media@vger.kernel.org
diff --git a/drivers/iio/magnetometer/Kconfig b/drivers/iio/magnetometer/Kconfig
index 2dee566ba703..d359003b248e 100644
--- a/drivers/iio/magnetometer/Kconfig
+++ b/drivers/iio/magnetometer/Kconfig
@@ -220,6 +220,17 @@ config QMC5883L
To compile this driver as a module, choose M here: the
module will be called qmc5883l.
+config QMC6308
+ tristate "QST QMC6308 3-Axis Magnetic Sensor"
+ depends on I2C
+ select REGMAP_I2C
+ help
+ Say Y here to add support for the QST QMC6308 3-Axis
+ Magnetic Sensor.
+
+ To compile this driver as a module, choose M here: the
+ module will be called qmc6308.
+
config SENSORS_HMC5843
tristate
select IIO_BUFFER
diff --git a/drivers/iio/magnetometer/Makefile b/drivers/iio/magnetometer/Makefile
index b9478e6513e4..8f2205975c1b 100644
--- a/drivers/iio/magnetometer/Makefile
+++ b/drivers/iio/magnetometer/Makefile
@@ -28,6 +28,7 @@ obj-$(CONFIG_IIO_ST_MAGN_SPI_3AXIS) += st_magn_spi.o
obj-$(CONFIG_INFINEON_TLV493D) += tlv493d.o
obj-$(CONFIG_QMC5883L) += qmc5883l.o
+obj-$(CONFIG_QMC6308) += qmc6308.o
obj-$(CONFIG_SENSORS_HMC5843) += hmc5843_core.o
obj-$(CONFIG_SENSORS_HMC5843_I2C) += hmc5843_i2c.o
diff --git a/drivers/iio/magnetometer/qmc6308.c b/drivers/iio/magnetometer/qmc6308.c
new file mode 100644
index 000000000000..7d4df846bbc5
--- /dev/null
+++ b/drivers/iio/magnetometer/qmc6308.c
@@ -0,0 +1,602 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Support for QST QMC6308 3-Axis Magnetic Sensor on I2C bus.
+ *
+ * Copyright (C) 2026 Jorijn van der Graaf <jorijnvdgraaf@catcrafts.net>
+ *
+ * Datasheet available at
+ * <https://qstcorp.com/upload/pdf/202202/13-52-15%20QMC6308%20Datasheet%20Rev.%20F(1).pdf>
+ */
+
+#include <linux/array_size.h>
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/cleanup.h>
+#include <linux/delay.h>
+#include <linux/dev_printk.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/pm_runtime.h>
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
+#include <linux/time.h>
+#include <linux/types.h>
+
+#include <asm/byteorder.h>
+
+#include <linux/iio/iio.h>
+
+#define QMC6308_REG_ID 0x00
+#define QMC6308_REG_X_LSB 0x01
+#define QMC6308_REG_STATUS 0x09
+#define QMC6308_REG_CTRL1 0x0A
+#define QMC6308_REG_CTRL2 0x0B
+#define QMC6308_REG_CTRL3 0x0D
+#define QMC6308_REG_CTRL4 0x29
+
+#define QMC6308_CHIP_ID 0x80
+
+/* Control register 1 */
+#define QMC6308_MODE_MASK GENMASK(1, 0)
+#define QMC6308_ODR_MASK GENMASK(3, 2)
+#define QMC6308_OSR1_MASK GENMASK(5, 4)
+#define QMC6308_OSR2_MASK GENMASK(7, 6)
+
+#define QMC6308_MODE_SUSPEND 0x00
+#define QMC6308_MODE_NORMAL 0x01
+
+#define QMC6308_ODR_10HZ 0x00
+#define QMC6308_ODR_50HZ 0x01
+#define QMC6308_ODR_100HZ 0x02
+#define QMC6308_ODR_200HZ 0x03
+
+#define QMC6308_OSR1_8 0x00
+#define QMC6308_OSR1_4 0x01
+#define QMC6308_OSR1_2 0x02
+#define QMC6308_OSR1_1 0x03
+
+/* Control register 2 */
+#define QMC6308_SET_RESET_MASK GENMASK(1, 0)
+#define QMC6308_RNG_MASK GENMASK(3, 2)
+#define QMC6308_SELF_TEST BIT(6)
+#define QMC6308_SOFT_RST BIT(7)
+
+#define QMC6308_SET_RESET_ON 0x00
+
+#define QMC6308_RNG_30G 0x00
+#define QMC6308_RNG_12G 0x01
+#define QMC6308_RNG_8G 0x02
+#define QMC6308_RNG_2G 0x03
+
+/* Status register */
+#define QMC6308_STATUS_DRDY BIT(0)
+#define QMC6308_STATUS_OVFL BIT(1)
+
+/* Power-on completion time (datasheet Table 7) */
+#define QMC6308_POR_US 250
+
+#define QMC6308_AUTOSUSPEND_DELAY_MS 500
+
+struct qmc6308_data {
+ struct regmap *regmap;
+ /* Protect data->range/odr/osr and serialize measurements */
+ struct mutex mutex;
+ struct iio_mount_matrix orientation;
+ u8 range;
+ u8 odr;
+ u8 osr;
+};
+
+enum qmc6308_axis {
+ QMC6308_AXIS_X,
+ QMC6308_AXIS_Y,
+ QMC6308_AXIS_Z,
+};
+
+static const int qmc6308_odr_avail[] = {
+ [QMC6308_ODR_10HZ] = 10,
+ [QMC6308_ODR_50HZ] = 50,
+ [QMC6308_ODR_100HZ] = 100,
+ [QMC6308_ODR_200HZ] = 200,
+};
+
+static const int qmc6308_osr1_avail[] = {
+ [QMC6308_OSR1_8] = 8,
+ [QMC6308_OSR1_4] = 4,
+ [QMC6308_OSR1_2] = 2,
+ [QMC6308_OSR1_1] = 1,
+};
+
+/*
+ * Sensitivity is 1000/2500/3750/15000 LSB/Gauss for the
+ * +-30/12/8/2 Gauss ranges respectively.
+ */
+static const int qmc6308_scales[][2] = {
+ [QMC6308_RNG_30G] = { 0, 1000000 },
+ [QMC6308_RNG_12G] = { 0, 400000 },
+ [QMC6308_RNG_8G] = { 0, 266667 },
+ [QMC6308_RNG_2G] = { 0, 66667 },
+};
+
+static int qmc6308_set_mode(struct qmc6308_data *data, unsigned int mode)
+{
+ return regmap_update_bits(data->regmap, QMC6308_REG_CTRL1,
+ QMC6308_MODE_MASK,
+ FIELD_PREP(QMC6308_MODE_MASK, mode));
+}
+
+static int qmc6308_take_measurement(struct iio_dev *indio_dev, int index,
+ int *val)
+{
+ struct qmc6308_data *data = iio_priv(indio_dev);
+ struct device *dev = regmap_get_device(data->regmap);
+ unsigned int status;
+ __le16 buf[3];
+ int ret;
+
+ PM_RUNTIME_ACQUIRE_AUTOSUSPEND(dev, pm);
+ ret = PM_RUNTIME_ACQUIRE_ERR(&pm);
+ if (ret) {
+ dev_err(dev, "Failed to power on (%d)\n", ret);
+ return ret;
+ }
+
+ guard(mutex)(&data->mutex);
+
+ /*
+ * Reading the status register clears DRDY, which is why the poll
+ * and the data read stay under one mutex hold. A runtime resume
+ * clears DRDY too, so a sample converted before the last suspend
+ * is never returned here.
+ *
+ * The timeout is 50ms of headroom over the slowest ODR (10Hz).
+ */
+ ret = regmap_read_poll_timeout(data->regmap, QMC6308_REG_STATUS,
+ status,
+ (status & QMC6308_STATUS_DRDY),
+ 2 * USEC_PER_MSEC,
+ 150 * USEC_PER_MSEC);
+ if (ret)
+ return ret;
+
+ ret = regmap_bulk_read(data->regmap, QMC6308_REG_X_LSB, buf,
+ sizeof(buf));
+ if (ret)
+ return ret;
+
+ if (status & QMC6308_STATUS_OVFL)
+ return -ERANGE;
+
+ *val = (s16)le16_to_cpu(buf[index]);
+
+ return 0;
+}
+
+static int qmc6308_read_raw(struct iio_dev *indio_dev,
+ const struct iio_chan_spec *chan,
+ int *val, int *val2, long mask)
+{
+ struct qmc6308_data *data = iio_priv(indio_dev);
+ int ret;
+
+ switch (mask) {
+ case IIO_CHAN_INFO_RAW:
+ ret = qmc6308_take_measurement(indio_dev, chan->address, val);
+ if (ret)
+ return ret;
+ return IIO_VAL_INT;
+ case IIO_CHAN_INFO_SCALE: {
+ guard(mutex)(&data->mutex);
+
+ *val = qmc6308_scales[data->range][0];
+ *val2 = qmc6308_scales[data->range][1];
+
+ return IIO_VAL_INT_PLUS_NANO;
+ }
+ case IIO_CHAN_INFO_SAMP_FREQ: {
+ guard(mutex)(&data->mutex);
+
+ *val = qmc6308_odr_avail[data->odr];
+
+ return IIO_VAL_INT;
+ }
+ case IIO_CHAN_INFO_OVERSAMPLING_RATIO: {
+ guard(mutex)(&data->mutex);
+
+ *val = qmc6308_osr1_avail[data->osr];
+
+ return IIO_VAL_INT;
+ }
+ default:
+ return -EINVAL;
+ }
+}
+
+static int qmc6308_write_raw(struct iio_dev *indio_dev,
+ const struct iio_chan_spec *chan,
+ int val, int val2, long mask)
+{
+ struct qmc6308_data *data = iio_priv(indio_dev);
+ unsigned int status;
+ unsigned int i;
+ int ret;
+
+ switch (mask) {
+ case IIO_CHAN_INFO_SCALE: {
+ if (val != 0)
+ return -EINVAL;
+
+ for (i = 0; i < ARRAY_SIZE(qmc6308_scales); i++) {
+ if (val2 == qmc6308_scales[i][1])
+ break;
+ }
+ if (i == ARRAY_SIZE(qmc6308_scales))
+ return -EINVAL;
+
+ guard(mutex)(&data->mutex);
+
+ ret = regmap_update_bits(data->regmap, QMC6308_REG_CTRL2,
+ QMC6308_RNG_MASK,
+ FIELD_PREP(QMC6308_RNG_MASK, i));
+ if (ret)
+ return ret;
+
+ data->range = i;
+
+ /*
+ * The data registers still hold (and DRDY still
+ * advertises) a sample converted at the previous range;
+ * discard it so that a read does not pair old-range data
+ * with the new scale. A conversion already in flight may
+ * still complete at the old range, so this narrows the
+ * window rather than closing it. The range change itself
+ * took effect, so only log a failure here: an error
+ * would mislead userspace about an effective write.
+ */
+ ret = regmap_read(data->regmap, QMC6308_REG_STATUS,
+ &status);
+ if (ret)
+ dev_warn(regmap_get_device(data->regmap),
+ "Failed to discard stale sample (%d)\n",
+ ret);
+
+ return 0;
+ }
+ case IIO_CHAN_INFO_SAMP_FREQ: {
+ for (i = 0; i < ARRAY_SIZE(qmc6308_odr_avail); i++) {
+ if (val == qmc6308_odr_avail[i])
+ break;
+ }
+ if (i == ARRAY_SIZE(qmc6308_odr_avail))
+ return -EINVAL;
+
+ guard(mutex)(&data->mutex);
+
+ ret = regmap_update_bits(data->regmap, QMC6308_REG_CTRL1,
+ QMC6308_ODR_MASK,
+ FIELD_PREP(QMC6308_ODR_MASK, i));
+ if (ret)
+ return ret;
+
+ data->odr = i;
+
+ return 0;
+ }
+ case IIO_CHAN_INFO_OVERSAMPLING_RATIO: {
+ for (i = 0; i < ARRAY_SIZE(qmc6308_osr1_avail); i++) {
+ if (val == qmc6308_osr1_avail[i])
+ break;
+ }
+ if (i == ARRAY_SIZE(qmc6308_osr1_avail))
+ return -EINVAL;
+
+ guard(mutex)(&data->mutex);
+
+ ret = regmap_update_bits(data->regmap, QMC6308_REG_CTRL1,
+ QMC6308_OSR1_MASK,
+ FIELD_PREP(QMC6308_OSR1_MASK, i));
+ if (ret)
+ return ret;
+
+ data->osr = i;
+
+ return 0;
+ }
+ default:
+ return -EINVAL;
+ }
+}
+
+static int qmc6308_read_avail(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ const int **vals, int *type, int *length,
+ long mask)
+{
+ switch (mask) {
+ case IIO_CHAN_INFO_SAMP_FREQ:
+ *vals = qmc6308_odr_avail;
+ *type = IIO_VAL_INT;
+ *length = ARRAY_SIZE(qmc6308_odr_avail);
+ return IIO_AVAIL_LIST;
+ case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
+ *vals = qmc6308_osr1_avail;
+ *type = IIO_VAL_INT;
+ *length = ARRAY_SIZE(qmc6308_osr1_avail);
+ return IIO_AVAIL_LIST;
+ case IIO_CHAN_INFO_SCALE:
+ *vals = (const int *)qmc6308_scales;
+ *type = IIO_VAL_INT_PLUS_NANO;
+ *length = ARRAY_SIZE(qmc6308_scales) * 2;
+ return IIO_AVAIL_LIST;
+ default:
+ return -EINVAL;
+ }
+}
+
+static int qmc6308_write_raw_get_fmt(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ long mask)
+{
+ switch (mask) {
+ case IIO_CHAN_INFO_SCALE:
+ return IIO_VAL_INT_PLUS_NANO;
+ default:
+ return IIO_VAL_INT;
+ }
+}
+
+static const struct iio_mount_matrix *
+qmc6308_get_mount_matrix(const struct iio_dev *indio_dev,
+ const struct iio_chan_spec *chan)
+{
+ struct qmc6308_data *data = iio_priv(indio_dev);
+
+ return &data->orientation;
+}
+
+static const struct iio_chan_spec_ext_info qmc6308_ext_info[] = {
+ IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, qmc6308_get_mount_matrix),
+ { }
+};
+
+static const struct iio_info qmc6308_info = {
+ .read_raw = qmc6308_read_raw,
+ .write_raw = qmc6308_write_raw,
+ .read_avail = qmc6308_read_avail,
+ .write_raw_get_fmt = qmc6308_write_raw_get_fmt,
+};
+
+static int qmc6308_init(struct qmc6308_data *data)
+{
+ struct regmap *map = data->regmap;
+ unsigned int reg;
+ int ret;
+
+ ret = regmap_read(map, QMC6308_REG_ID, ®);
+ if (ret)
+ return ret;
+
+ /* Allow unknown IDs so that fallback compatibles work */
+ if (reg != QMC6308_CHIP_ID)
+ dev_warn(regmap_get_device(map),
+ "Unknown chip id: 0x%02x, continuing\n", reg);
+
+ /* The SOFT_RST bit is not auto-cleared and must be written back 0 */
+ ret = regmap_write(map, QMC6308_REG_CTRL2, QMC6308_SOFT_RST);
+ if (ret)
+ return ret;
+
+ /*
+ * The datasheet gives no soft-reset completion figure; reuse the
+ * power-on time as a conservative bound.
+ */
+ fsleep(QMC6308_POR_US);
+
+ data->range = QMC6308_RNG_30G;
+ data->odr = QMC6308_ODR_50HZ;
+ data->osr = QMC6308_OSR1_8;
+
+ ret = regmap_write(map, QMC6308_REG_CTRL2,
+ FIELD_PREP(QMC6308_SET_RESET_MASK,
+ QMC6308_SET_RESET_ON) |
+ FIELD_PREP(QMC6308_RNG_MASK, data->range));
+ if (ret)
+ return ret;
+
+ /* OSR2 (second-stage filter) set to its power-on default of 0 */
+ return regmap_write(map, QMC6308_REG_CTRL1,
+ FIELD_PREP(QMC6308_MODE_MASK,
+ QMC6308_MODE_NORMAL) |
+ FIELD_PREP(QMC6308_ODR_MASK, data->odr) |
+ FIELD_PREP(QMC6308_OSR1_MASK, data->osr) |
+ FIELD_PREP(QMC6308_OSR2_MASK, 0));
+}
+
+static void qmc6308_power_down_action(void *priv)
+{
+ struct qmc6308_data *data = priv;
+
+ if (!pm_runtime_status_suspended(regmap_get_device(data->regmap)))
+ qmc6308_set_mode(data, QMC6308_MODE_SUSPEND);
+}
+
+static bool qmc6308_volatile_reg(struct device *dev, unsigned int reg)
+{
+ return reg >= QMC6308_REG_X_LSB && reg <= QMC6308_REG_STATUS;
+}
+
+static bool qmc6308_writable_reg(struct device *dev, unsigned int reg)
+{
+ switch (reg) {
+ case QMC6308_REG_CTRL1:
+ case QMC6308_REG_CTRL2:
+ case QMC6308_REG_CTRL3:
+ case QMC6308_REG_CTRL4:
+ return true;
+ default:
+ return false;
+ }
+}
+
+static const struct regmap_config qmc6308_regmap_config = {
+ .reg_bits = 8,
+ .val_bits = 8,
+ .max_register = QMC6308_REG_CTRL4,
+ .cache_type = REGCACHE_MAPLE,
+ .volatile_reg = qmc6308_volatile_reg,
+ .writeable_reg = qmc6308_writable_reg,
+};
+
+#define QMC6308_CHANNEL(_axis) \
+ { \
+ .type = IIO_MAGN, \
+ .modified = 1, \
+ .channel2 = IIO_MOD_##_axis, \
+ .address = QMC6308_AXIS_##_axis, \
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
+ .info_mask_shared_by_type = \
+ BIT(IIO_CHAN_INFO_SCALE) | \
+ BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
+ BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
+ .info_mask_shared_by_type_available = \
+ BIT(IIO_CHAN_INFO_SCALE) | \
+ BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
+ BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
+ .ext_info = qmc6308_ext_info, \
+ }
+
+static const struct iio_chan_spec qmc6308_channels[] = {
+ QMC6308_CHANNEL(X),
+ QMC6308_CHANNEL(Y),
+ QMC6308_CHANNEL(Z),
+};
+
+static int qmc6308_probe(struct i2c_client *client)
+{
+ struct device *dev = &client->dev;
+ struct qmc6308_data *data;
+ struct iio_dev *indio_dev;
+ struct regmap *map;
+ int ret;
+
+ indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
+ if (!indio_dev)
+ return -ENOMEM;
+
+ i2c_set_clientdata(client, indio_dev);
+
+ map = devm_regmap_init_i2c(client, &qmc6308_regmap_config);
+ if (IS_ERR(map))
+ return dev_err_probe(dev, PTR_ERR(map),
+ "regmap initialization failed\n");
+
+ ret = devm_regulator_get_enable(dev, "vdd");
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "Failed to enable VDD regulator\n");
+
+ fsleep(QMC6308_POR_US);
+
+ data = iio_priv(indio_dev);
+ data->regmap = map;
+
+ ret = devm_mutex_init(dev, &data->mutex);
+ if (ret)
+ return ret;
+
+ ret = iio_read_mount_matrix(dev, &data->orientation);
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "Failed to read mount matrix\n");
+
+ indio_dev->name = "qmc6308";
+ indio_dev->info = &qmc6308_info;
+ indio_dev->channels = qmc6308_channels;
+ indio_dev->num_channels = ARRAY_SIZE(qmc6308_channels);
+ indio_dev->modes = INDIO_DIRECT_MODE;
+
+ ret = qmc6308_init(data);
+ if (ret)
+ return dev_err_probe(dev, ret, "qmc6308 init failed\n");
+
+ ret = pm_runtime_set_active(dev);
+ if (ret)
+ return ret;
+
+ ret = devm_add_action_or_reset(dev, qmc6308_power_down_action, data);
+ if (ret)
+ return ret;
+
+ pm_runtime_use_autosuspend(dev);
+ pm_runtime_set_autosuspend_delay(dev, QMC6308_AUTOSUSPEND_DELAY_MS);
+ ret = devm_pm_runtime_enable(dev);
+ if (ret)
+ return ret;
+
+ return devm_iio_device_register(dev, indio_dev);
+}
+
+static int qmc6308_runtime_suspend(struct device *dev)
+{
+ struct iio_dev *indio_dev = dev_get_drvdata(dev);
+ struct qmc6308_data *data = iio_priv(indio_dev);
+
+ return qmc6308_set_mode(data, QMC6308_MODE_SUSPEND);
+}
+
+static int qmc6308_runtime_resume(struct device *dev)
+{
+ struct iio_dev *indio_dev = dev_get_drvdata(dev);
+ struct qmc6308_data *data = iio_priv(indio_dev);
+ unsigned int status;
+ int ret;
+
+ ret = qmc6308_set_mode(data, QMC6308_MODE_NORMAL);
+ if (ret)
+ return ret;
+
+ /*
+ * DRDY may still be set for a sample converted before the last
+ * suspend; reading the status register clears it so the next
+ * measurement waits for fresh data.
+ */
+ ret = regmap_read(data->regmap, QMC6308_REG_STATUS, &status);
+ if (ret) {
+ /* Best effort to leave the chip in a consistent state */
+ qmc6308_set_mode(data, QMC6308_MODE_SUSPEND);
+ }
+
+ return ret;
+}
+
+static DEFINE_RUNTIME_DEV_PM_OPS(qmc6308_pm_ops, qmc6308_runtime_suspend,
+ qmc6308_runtime_resume, NULL);
+
+static const struct of_device_id qmc6308_match[] = {
+ { .compatible = "qstcorp,qmc6308" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, qmc6308_match);
+
+static const struct i2c_device_id qmc6308_id[] = {
+ { .name = "qmc6308" },
+ { }
+};
+MODULE_DEVICE_TABLE(i2c, qmc6308_id);
+
+static struct i2c_driver qmc6308_driver = {
+ .driver = {
+ .name = "qmc6308",
+ .of_match_table = qmc6308_match,
+ .pm = pm_ptr(&qmc6308_pm_ops),
+ },
+ .id_table = qmc6308_id,
+ .probe = qmc6308_probe,
+};
+module_i2c_driver(qmc6308_driver);
+
+MODULE_DESCRIPTION("QST QMC6308 3-Axis Magnetic Sensor driver");
+MODULE_AUTHOR("Jorijn van der Graaf <jorijnvdgraaf@catcrafts.net>");
+MODULE_LICENSE("GPL");
--
2.55.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2 2/2] iio: magnetometer: add support for QST QMC6308
2026-07-21 13:48 ` [PATCH v2 2/2] iio: magnetometer: add support for " Jorijn van der Graaf
@ 2026-07-21 14:04 ` sashiko-bot
2026-07-21 20:19 ` Siratul Islam
1 sibling, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-07-21 14:04 UTC (permalink / raw)
To: Jorijn van der Graaf; +Cc: conor+dt, devicetree, robh
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Potential hardware initialization race condition during soft reset deassertion.
--
--- Patch [2]: [PATCH v2 2/2] iio: magnetometer: add support for QST QMC6308 ---
commit 50ae109e19c53c095dd871b420a64fb846f258d5
Author: Jorijn van der Graaf <jorijnvdgraaf@catcrafts.net>
iio: magnetometer: add support for QST QMC6308
This commit adds support for the QST QMC6308 3-axis anisotropic
magnetoresistive magnetometer on I2C. It implements raw readings, output
data rate, field range, filter oversampling ratio, mount matrix, and
runtime PM.
> diff --git a/drivers/iio/magnetometer/qmc6308.c b/drivers/iio/magnetometer/qmc6308.c
> new file mode 100644
> index 0000000000000..7d4df846bbc54
> --- /dev/null
> +++ b/drivers/iio/magnetometer/qmc6308.c
[ ... ]
> +static int qmc6308_init(struct qmc6308_data *data)
> +{
[ ... ]
> + /* The SOFT_RST bit is not auto-cleared and must be written back 0 */
> + ret = regmap_write(map, QMC6308_REG_CTRL2, QMC6308_SOFT_RST);
> + if (ret)
> + return ret;
> +
> + /*
> + * The datasheet gives no soft-reset completion figure; reuse the
> + * power-on time as a conservative bound.
> + */
> + fsleep(QMC6308_POR_US);
> +
> + data->range = QMC6308_RNG_30G;
> + data->odr = QMC6308_ODR_50HZ;
> + data->osr = QMC6308_OSR1_8;
> +
> + ret = regmap_write(map, QMC6308_REG_CTRL2,
> + FIELD_PREP(QMC6308_SET_RESET_MASK,
> + QMC6308_SET_RESET_ON) |
> + FIELD_PREP(QMC6308_RNG_MASK, data->range));
> + if (ret)
> + return ret;
> +
> + /* OSR2 (second-stage filter) set to its power-on default of 0 */
> + return regmap_write(map, QMC6308_REG_CTRL1,
[Severity: Medium]
If the soft reset logic is level-sensitive and holds the chip in reset while
the bit is 1, the initialization occurs only after the bit is cleared to 0 by
the second write to QMC6308_REG_CTRL2.
Can the hardware reliably accept this immediate subsequent write to
QMC6308_REG_CTRL1 without any delay, or does it need time to initialize after
the reset is deasserted?
> + FIELD_PREP(QMC6308_MODE_MASK,
> + QMC6308_MODE_NORMAL) |
> + FIELD_PREP(QMC6308_ODR_MASK, data->odr) |
> + FIELD_PREP(QMC6308_OSR1_MASK, data->osr) |
> + FIELD_PREP(QMC6308_OSR2_MASK, 0));
> +}
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260721134843.7484-1-jorijnvdgraaf@catcrafts.net?part=2
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 2/2] iio: magnetometer: add support for QST QMC6308
2026-07-21 13:48 ` [PATCH v2 2/2] iio: magnetometer: add support for " Jorijn van der Graaf
2026-07-21 14:04 ` sashiko-bot
@ 2026-07-21 20:19 ` Siratul Islam
1 sibling, 0 replies; 5+ messages in thread
From: Siratul Islam @ 2026-07-21 20:19 UTC (permalink / raw)
To: Jorijn van der Graaf, Jonathan Cameron
Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Rob Herring, Krzysztof Kozlowski, Conor Dooley,
Uwe Kleine-König, Luca Weiss, linux-iio, devicetree,
linux-kernel
On Tue, 2026-07-21 at 15:48 +0200, Jorijn van der Graaf wrote:
> The QST QMC6308 is a 3-axis anisotropic magnetoresistive (AMR)
> magnetometer on I2C, found e.g. in the Fairphone 6. Its register map
> is not compatible with the QMC5883L's, so add a separate driver rather
> than extending the QMC5883L driver.
>
...
> - Switch the license to GPL-2.0-only: the dual license had been
> carried over from qmc5883l.c, which this driver is modeled on,
> without a reason of our own (Jonathan).
I wrote the QMC5883L driver. My reason to use dual-license was I wanted
people to be able to port the driver to Non-GPL OSes like Zephyr, NuttX, BSDs (specially RTOSes,
none of them are GPL, and these sensors are widely used with MCUs) with least friction.
There are stuff that can't be ported (like regmap etc.) but the rest of it could.
Also, I read the "Linux Device Drivers 3" book where the samples use Dual BSD/GPL.
>
...
> + /*
> + * Reading the status register clears DRDY, which is why the poll
> + * and the data read stay under one mutex hold. A runtime resume
> + * clears DRDY too, so a sample converted before the last suspend
> + * is never returned here.
> + *
> + * The timeout is 50ms of headroom over the slowest ODR (10Hz).
> + */
> + ret = regmap_read_poll_timeout(data->regmap, QMC6308_REG_STATUS,
> + status,
> + (status & QMC6308_STATUS_DRDY),
You can split by logic. i.e., put "status, (status & QMC6308_STATUS_DRDY)" on the same line.
> + 2 * USEC_PER_MSEC,
> + 150 * USEC_PER_MSEC);
> + if (ret)
> + return ret;
> +
>
...
> +
> +static int qmc6308_write_raw(struct iio_dev *indio_dev,
> + const struct iio_chan_spec *chan,
> + int val, int val2, long mask)
> +{
>
...
> + if (ret)
> + dev_warn(regmap_get_device(data->regmap),
> + "Failed to discard stale sample (%d)\n",
> + ret);
Move "ret" to the line above.
> +
> + return 0;
> + }
> +
...
> +static int qmc6308_probe(struct i2c_client *client)
> +{
...
> +
> + ret = devm_add_action_or_reset(dev, qmc6308_power_down_action, data);
> + if (ret)
> + return ret;
> +
> + pm_runtime_use_autosuspend(dev);
> + pm_runtime_set_autosuspend_delay(dev, QMC6308_AUTOSUSPEND_DELAY_MS);
Add a space here.
> + ret = devm_pm_runtime_enable(dev);
> + if (ret)
> + return ret;
> +
> + return devm_iio_device_register(dev, indio_dev);
> +}
> +
> +static int qmc6308_runtime_resume(struct device *dev)
> +{
> + struct iio_dev *indio_dev = dev_get_drvdata(dev);
> + struct qmc6308_data *data = iio_priv(indio_dev);
> + unsigned int status;
> + int ret;
> +
> + ret = qmc6308_set_mode(data, QMC6308_MODE_NORMAL);
> + if (ret)
> + return ret;
> +
> + /*
> + * DRDY may still be set for a sample converted before the last
> + * suspend; reading the status register clears it so the next
> + * measurement waits for fresh data.
> + */
> + ret = regmap_read(data->regmap, QMC6308_REG_STATUS, &status);
> + if (ret) {
> + /* Best effort to leave the chip in a consistent state */
> + qmc6308_set_mode(data, QMC6308_MODE_SUSPEND);
> + }
It's single line so the brackets { } are not needed.
> +
> + return ret;
> +}
> +
...
> +
> +MODULE_DESCRIPTION("QST QMC6308 3-Axis Magnetic Sensor driver");
> +MODULE_AUTHOR("Jorijn van der Graaf <jorijnvdgraaf@catcrafts.net>");
> +MODULE_LICENSE("GPL");
--
Best regards,
Sirat
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-07-21 20:20 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-21 13:48 [PATCH v2 0/2] iio: magnetometer: add support for QST QMC6308 Jorijn van der Graaf
2026-07-21 13:48 ` [PATCH v2 1/2] dt-bindings: iio: magnetometer: add " Jorijn van der Graaf
2026-07-21 13:48 ` [PATCH v2 2/2] iio: magnetometer: add support for " Jorijn van der Graaf
2026-07-21 14:04 ` sashiko-bot
2026-07-21 20:19 ` Siratul Islam
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox