From nobody Sat Jul 25 00:56:27 2026 Received: from MTA-10-3.privateemail.com (mta-10-3.privateemail.com [198.54.127.62]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4FE70372686; Tue, 21 Jul 2026 13:49:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.54.127.62 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784641777; cv=none; b=T8gdzbASs7/KjTXaFO8JaxZIFlFbtS9BGAcRsrCuQ2ShggGcwzNtWmxHijaYDGobgk2BIO9sUcu88n99MgxxmND6uZOaAV3aZrxNliCL69bvzFPL32gU86Qm4NU/AEqVHvVaiAHs3N5IxXt5IkHWifUWoU3VKu93GZcjBw/kVmc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784641777; c=relaxed/simple; bh=IA6tu8VvKBsU9nA56PvhKSyDoPFqlr1Ovuo0Ms6wSEc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PloXauC8I6L1vGWFNPU4/tg+hPS/87bm028G2po9KI/yhrnDQ5y/kAe89KTkJah+heKkAvZn2QlJABb71OUdgWfHjXBEgYVYRRCE+WOtZXmpks6+0a+b4VnSEIBKTbmFjdvL4kiE48laiDELTYMJkd+CjI6EHjjeDHZpxUmAqYk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=catcrafts.net; spf=pass smtp.mailfrom=catcrafts.net; arc=none smtp.client-ip=198.54.127.62 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=catcrafts.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=catcrafts.net Received: from mail.privateemail.com (K8S-PROD-WORKER-13 [87.215.145.39]) by mta-10.privateemail.com (Postfix) with ESMTPA id 4h4Jdp4M9Sz3hhTq; Tue, 21 Jul 2026 09:49:17 -0400 (EDT) From: Jorijn van der Graaf To: Jonathan Cameron Cc: Jorijn van der Graaf , Jonathan Cameron , David Lechner , =?UTF-8?q?Nuno=20S=C3=A1?= , Andy Shevchenko , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Siratul Islam , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Luca Weiss , linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/2] dt-bindings: iio: magnetometer: add QST QMC6308 Date: Tue, 21 Jul 2026 15:48:42 +0200 Message-ID: <20260721134843.7484-2-jorijnvdgraaf@catcrafts.net> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260721134843.7484-1-jorijnvdgraaf@catcrafts.net> References: <20260721134843.7484-1-jorijnvdgraaf@catcrafts.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Krzysztof Kozlowski --- 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/qstc= orp,qmc6308.yaml diff --git a/Documentation/devicetree/bindings/iio/magnetometer/qstcorp,qmc= 6308.yaml b/Documentation/devicetree/bindings/iio/magnetometer/qstcorp,qmc6= 308.yaml new file mode 100644 index 000000000000..6201a44cfe38 --- /dev/null +++ b/Documentation/devicetree/bindings/iio/magnetometer/qstcorp,qmc6308.ya= ml @@ -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 + +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%20R= ev.%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 =3D <1>; + #size-cells =3D <0>; + + magnetometer@2c { + compatible =3D "qstcorp,qmc6308"; + reg =3D <0x2c>; + vdd-supply =3D <&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 =20 +QST QMC6308 3-AXIS MAGNETIC SENSOR +M: Jorijn van der Graaf +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 --=20 2.55.0 From nobody Sat Jul 25 00:56:27 2026 Received: from MTA-10-3.privateemail.com (mta-10-3.privateemail.com [198.54.127.62]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F399F376BFB; Tue, 21 Jul 2026 13:49:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.54.127.62 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784641797; cv=none; b=Ksk8Fc4TY9zcQKXwR28CZS7rML6sqpsixZhg7+QW6yRT/+VpYKxs6gyvBNlZ/9pBBvi3dzE9Qg0sIkx0Eckfwg8xYF2yaWp2vI3jpVXvfbnBcd/nk2P7kCXYePWZxbIehuIVT7tivXCa7ASHsApYuvSJRfzb9hnDP4yfiwgPsNc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784641797; c=relaxed/simple; bh=pdRiKR0K8Sw6CvEwFVkM4DDynCdNz2Pox81y9BctUxY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T4pHja46RzYTISLEyHFa6kSO2Ly0TsIrEPvhZzQGsljOeTPadleE/q1VDABRE6FZo263sjHOsj4soSdw4KNDMmxnOCxT5IUp+wdIS4McvPBj8qX92DnmjO4VhFWvFkjedCTIfj9reDLlzwqVLNSNhmKveWPRkwoktLh9M2N6WYw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=catcrafts.net; spf=pass smtp.mailfrom=catcrafts.net; arc=none smtp.client-ip=198.54.127.62 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=catcrafts.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=catcrafts.net Received: from mail.privateemail.com (K8S-PROD-WORKER-13 [87.215.145.39]) by mta-10.privateemail.com (Postfix) with ESMTPA id 4h4Jf90tkYz3hhTD; Tue, 21 Jul 2026 09:49:35 -0400 (EDT) From: Jorijn van der Graaf To: Jonathan Cameron Cc: Jorijn van der Graaf , Jonathan Cameron , David Lechner , =?UTF-8?q?Nuno=20S=C3=A1?= , Andy Shevchenko , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Siratul Islam , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Luca Weiss , linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/2] iio: magnetometer: add support for QST QMC6308 Date: Tue, 21 Jul 2026 15:48:43 +0200 Message-ID: <20260721134843.7484-3-jorijnvdgraaf@catcrafts.net> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260721134843.7484-1-jorijnvdgraaf@catcrafts.net> References: <20260721134843.7484-1-jorijnvdgraaf@catcrafts.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- 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 L: linux-iio@vger.kernel.org S: Maintained F: Documentation/devicetree/bindings/iio/magnetometer/qstcorp,qmc6308.yaml +F: drivers/iio/magnetometer/qmc6308.c =20 QT1010 MEDIA DRIVER L: linux-media@vger.kernel.org diff --git a/drivers/iio/magnetometer/Kconfig b/drivers/iio/magnetometer/Kc= onfig 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. =20 +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/M= akefile 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) +=3D st_magn_spi.o obj-$(CONFIG_INFINEON_TLV493D) +=3D tlv493d.o =20 obj-$(CONFIG_QMC5883L) +=3D qmc5883l.o +obj-$(CONFIG_QMC6308) +=3D qmc6308.o =20 obj-$(CONFIG_SENSORS_HMC5843) +=3D hmc5843_core.o obj-$(CONFIG_SENSORS_HMC5843_I2C) +=3D 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 + * + * Datasheet available at + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +#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[] =3D { + [QMC6308_ODR_10HZ] =3D 10, + [QMC6308_ODR_50HZ] =3D 50, + [QMC6308_ODR_100HZ] =3D 100, + [QMC6308_ODR_200HZ] =3D 200, +}; + +static const int qmc6308_osr1_avail[] =3D { + [QMC6308_OSR1_8] =3D 8, + [QMC6308_OSR1_4] =3D 4, + [QMC6308_OSR1_2] =3D 2, + [QMC6308_OSR1_1] =3D 1, +}; + +/* + * Sensitivity is 1000/2500/3750/15000 LSB/Gauss for the + * +-30/12/8/2 Gauss ranges respectively. + */ +static const int qmc6308_scales[][2] =3D { + [QMC6308_RNG_30G] =3D { 0, 1000000 }, + [QMC6308_RNG_12G] =3D { 0, 400000 }, + [QMC6308_RNG_8G] =3D { 0, 266667 }, + [QMC6308_RNG_2G] =3D { 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 =3D iio_priv(indio_dev); + struct device *dev =3D regmap_get_device(data->regmap); + unsigned int status; + __le16 buf[3]; + int ret; + + PM_RUNTIME_ACQUIRE_AUTOSUSPEND(dev, pm); + ret =3D 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 =3D 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 =3D regmap_bulk_read(data->regmap, QMC6308_REG_X_LSB, buf, + sizeof(buf)); + if (ret) + return ret; + + if (status & QMC6308_STATUS_OVFL) + return -ERANGE; + + *val =3D (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 =3D iio_priv(indio_dev); + int ret; + + switch (mask) { + case IIO_CHAN_INFO_RAW: + ret =3D 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 =3D qmc6308_scales[data->range][0]; + *val2 =3D qmc6308_scales[data->range][1]; + + return IIO_VAL_INT_PLUS_NANO; + } + case IIO_CHAN_INFO_SAMP_FREQ: { + guard(mutex)(&data->mutex); + + *val =3D qmc6308_odr_avail[data->odr]; + + return IIO_VAL_INT; + } + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: { + guard(mutex)(&data->mutex); + + *val =3D 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 =3D iio_priv(indio_dev); + unsigned int status; + unsigned int i; + int ret; + + switch (mask) { + case IIO_CHAN_INFO_SCALE: { + if (val !=3D 0) + return -EINVAL; + + for (i =3D 0; i < ARRAY_SIZE(qmc6308_scales); i++) { + if (val2 =3D=3D qmc6308_scales[i][1]) + break; + } + if (i =3D=3D ARRAY_SIZE(qmc6308_scales)) + return -EINVAL; + + guard(mutex)(&data->mutex); + + ret =3D regmap_update_bits(data->regmap, QMC6308_REG_CTRL2, + QMC6308_RNG_MASK, + FIELD_PREP(QMC6308_RNG_MASK, i)); + if (ret) + return ret; + + data->range =3D 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 =3D 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 =3D 0; i < ARRAY_SIZE(qmc6308_odr_avail); i++) { + if (val =3D=3D qmc6308_odr_avail[i]) + break; + } + if (i =3D=3D ARRAY_SIZE(qmc6308_odr_avail)) + return -EINVAL; + + guard(mutex)(&data->mutex); + + ret =3D regmap_update_bits(data->regmap, QMC6308_REG_CTRL1, + QMC6308_ODR_MASK, + FIELD_PREP(QMC6308_ODR_MASK, i)); + if (ret) + return ret; + + data->odr =3D i; + + return 0; + } + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: { + for (i =3D 0; i < ARRAY_SIZE(qmc6308_osr1_avail); i++) { + if (val =3D=3D qmc6308_osr1_avail[i]) + break; + } + if (i =3D=3D ARRAY_SIZE(qmc6308_osr1_avail)) + return -EINVAL; + + guard(mutex)(&data->mutex); + + ret =3D regmap_update_bits(data->regmap, QMC6308_REG_CTRL1, + QMC6308_OSR1_MASK, + FIELD_PREP(QMC6308_OSR1_MASK, i)); + if (ret) + return ret; + + data->osr =3D 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 =3D qmc6308_odr_avail; + *type =3D IIO_VAL_INT; + *length =3D ARRAY_SIZE(qmc6308_odr_avail); + return IIO_AVAIL_LIST; + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: + *vals =3D qmc6308_osr1_avail; + *type =3D IIO_VAL_INT; + *length =3D ARRAY_SIZE(qmc6308_osr1_avail); + return IIO_AVAIL_LIST; + case IIO_CHAN_INFO_SCALE: + *vals =3D (const int *)qmc6308_scales; + *type =3D IIO_VAL_INT_PLUS_NANO; + *length =3D 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 =3D iio_priv(indio_dev); + + return &data->orientation; +} + +static const struct iio_chan_spec_ext_info qmc6308_ext_info[] =3D { + IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, qmc6308_get_mount_matrix), + { } +}; + +static const struct iio_info qmc6308_info =3D { + .read_raw =3D qmc6308_read_raw, + .write_raw =3D qmc6308_write_raw, + .read_avail =3D qmc6308_read_avail, + .write_raw_get_fmt =3D qmc6308_write_raw_get_fmt, +}; + +static int qmc6308_init(struct qmc6308_data *data) +{ + struct regmap *map =3D data->regmap; + unsigned int reg; + int ret; + + ret =3D regmap_read(map, QMC6308_REG_ID, ®); + if (ret) + return ret; + + /* Allow unknown IDs so that fallback compatibles work */ + if (reg !=3D 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 =3D 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 =3D QMC6308_RNG_30G; + data->odr =3D QMC6308_ODR_50HZ; + data->osr =3D QMC6308_OSR1_8; + + ret =3D 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 =3D 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 >=3D QMC6308_REG_X_LSB && reg <=3D 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 =3D { + .reg_bits =3D 8, + .val_bits =3D 8, + .max_register =3D QMC6308_REG_CTRL4, + .cache_type =3D REGCACHE_MAPLE, + .volatile_reg =3D qmc6308_volatile_reg, + .writeable_reg =3D qmc6308_writable_reg, +}; + +#define QMC6308_CHANNEL(_axis) \ + { \ + .type =3D IIO_MAGN, \ + .modified =3D 1, \ + .channel2 =3D IIO_MOD_##_axis, \ + .address =3D QMC6308_AXIS_##_axis, \ + .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW), \ + .info_mask_shared_by_type =3D \ + BIT(IIO_CHAN_INFO_SCALE) | \ + BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ + .info_mask_shared_by_type_available =3D \ + BIT(IIO_CHAN_INFO_SCALE) | \ + BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ + .ext_info =3D qmc6308_ext_info, \ + } + +static const struct iio_chan_spec qmc6308_channels[] =3D { + QMC6308_CHANNEL(X), + QMC6308_CHANNEL(Y), + QMC6308_CHANNEL(Z), +}; + +static int qmc6308_probe(struct i2c_client *client) +{ + struct device *dev =3D &client->dev; + struct qmc6308_data *data; + struct iio_dev *indio_dev; + struct regmap *map; + int ret; + + indio_dev =3D devm_iio_device_alloc(dev, sizeof(*data)); + if (!indio_dev) + return -ENOMEM; + + i2c_set_clientdata(client, indio_dev); + + map =3D 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 =3D 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 =3D iio_priv(indio_dev); + data->regmap =3D map; + + ret =3D devm_mutex_init(dev, &data->mutex); + if (ret) + return ret; + + ret =3D iio_read_mount_matrix(dev, &data->orientation); + if (ret) + return dev_err_probe(dev, ret, + "Failed to read mount matrix\n"); + + indio_dev->name =3D "qmc6308"; + indio_dev->info =3D &qmc6308_info; + indio_dev->channels =3D qmc6308_channels; + indio_dev->num_channels =3D ARRAY_SIZE(qmc6308_channels); + indio_dev->modes =3D INDIO_DIRECT_MODE; + + ret =3D qmc6308_init(data); + if (ret) + return dev_err_probe(dev, ret, "qmc6308 init failed\n"); + + ret =3D pm_runtime_set_active(dev); + if (ret) + return ret; + + ret =3D 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 =3D 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 =3D dev_get_drvdata(dev); + struct qmc6308_data *data =3D 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 =3D dev_get_drvdata(dev); + struct qmc6308_data *data =3D iio_priv(indio_dev); + unsigned int status; + int ret; + + ret =3D 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 =3D 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[] =3D { + { .compatible =3D "qstcorp,qmc6308" }, + { } +}; +MODULE_DEVICE_TABLE(of, qmc6308_match); + +static const struct i2c_device_id qmc6308_id[] =3D { + { .name =3D "qmc6308" }, + { } +}; +MODULE_DEVICE_TABLE(i2c, qmc6308_id); + +static struct i2c_driver qmc6308_driver =3D { + .driver =3D { + .name =3D "qmc6308", + .of_match_table =3D qmc6308_match, + .pm =3D pm_ptr(&qmc6308_pm_ops), + }, + .id_table =3D qmc6308_id, + .probe =3D qmc6308_probe, +}; +module_i2c_driver(qmc6308_driver); + +MODULE_DESCRIPTION("QST QMC6308 3-Axis Magnetic Sensor driver"); +MODULE_AUTHOR("Jorijn van der Graaf "); +MODULE_LICENSE("GPL"); --=20 2.55.0