iio: magnetometer: add driver for QST QMC5883L Sensor
Subsystems (3)
Siratul Islam
maintainerv2 cover iio: magnetometer: add driver for QST QMC5883L Sensor
This patch series introduces the QST QMC5883L 3-Axis Magnetic Sensor driver. It is a simple device with minimal magnetometer functionalities. Commonly used as (software incompatible) replacement for the Honeywell HMC5883L sensor.
This driver implements the basic functionalities of the QMC5883L sensor, and intentionally leaves out some features like DRDY interrupt pin support and power management for simplicity, both of which will be addressed in future patches.
There was an attempt to introduce this device about an year ago but the author seems to have abandoned the patch series. Since the device is simple enough, I decided to start from scratch.
Note: I also noticed a patch for the QMC5883P variant. Despite similar naming, the sensors are different including different register maps, so these devices are not compatible with each other.
---
Changes in v2:
- Update commit message in binding.
- Reformat header includes.
- Remove iio_device_claim_direct()/iio_device_release_direct()
- Read the measurement registers before checking the OVL status bit
- Replace scoped_guard() with guard()
- Fix function parameter styling
- Replace FIELD_PREP_CONST() with FIELD_PREP()
- Replace qmc5883l_rng_avail[] with a 2D qmc5883l_scales[][2] array
- Update mutex comment
- Add a devm_add_action_or_reset() callback
- Use a named initializer in the i2c_device_id table
- Combine u8 rng, osr, odr; onto one line
- Various styling fixes
- Update maintainer email to [email protected]
Link to v1: https://lore.kernel.org/linux-iio/[email protected]/
Siratul Islam (3):
dt-bindings: add entry for qstcorp
dt-bindings: iio: magnetometer: add QST QMC5883L Sensor
iio: magnetometer: add driver for QST QMC5883L Sensor
.../iio/magnetometer/qstcorp,qmc5883l.yaml | 52 ++ .../devicetree/bindings/vendor-prefixes.yaml | 2 + MAINTAINERS | 7 + drivers/iio/magnetometer/Kconfig | 11 + drivers/iio/magnetometer/Makefile | 2 + drivers/iio/magnetometer/qmc5883l.c | 516 ++++++++++++++++++ 6 files changed, 590 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/magnetometer/qstcorp,qmc5883l.yaml create mode 100644 drivers/iio/magnetometer/qmc5883l.c
--
2.54.0
Siratul Islam
maintainerPATCH v2 1/3 dt-bindings: add entry for qstcorp
Add an entry for QST Corporation Limited
--- Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++ 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/vendor-prefixes.yaml +2 -0
@@ -1355,6 +1355,8 @@ patternProperties: description: Shenzhen QiShenglong Industrialist Co., Ltd. "^qnap,.*": description: QNAP Systems, Inc. + "^qstcorp,.*": + description: QST Corporation Limited "^quanta,.*": description: Quanta Computer Inc. "^radxa,.*":
Signature · 1 line
2.54.0
Siratul Islam
maintainerPATCH v2 2/3 dt-bindings: iio: magnetometer: add QST QMC5883L Sensor
In reply to Siratul IslamAdd devicetree binding for the QST QMC5883L 3-Axis Magnetic Sensor connected via I2C.
Used enum so that more devices could use this binding
--- .../iio/magnetometer/qstcorp,qmc5883l.yaml | 52 +++++++++++++++++++ MAINTAINERS | 6 +++ 2 files changed, 58 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/magnetometer/qstcorp,qmc5883l.yaml
Documentation/devicetree/bindings/iio/magnetometer/qstcorp,qmc5883l.yaml new file +52 -0
@@ -0,0 +1,52 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/magnetometer/qstcorp,qmc5883l.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: QST QMC5883L 3-Axis Magnetic Sensor + +maintainers: + - Siratul Islam <[email protected]> + +description: | + QST QMC5883L 3-Axis Magnetic Sensor on I2C bus. + https://www.qstcorp.com/upload/pdf/202512/13-52-04%20QMC5883L%20Datasheet%20Rev.%20B.pdf + +properties: + compatible: + enum: + - qstcorp,qmc5883l + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + + vdd-supply: true + + vddio-supply: true + +additionalProperties: false + +required: + - compatible + - reg + - vdd-supply + - vddio-supply + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + magnetometer@d { + compatible = "qstcorp,qmc5883l"; + reg = <0x0d>; + vdd-supply = <&vdd_3v3_reg>; + vddio-supply = <&vdd_3v3_reg>; + }; + }; +...
MAINTAINERS +6 -0
@@ -21787,6 +21787,12 @@ F: Documentation/networking/device_drivers/ethernet/freescale/dpaa2/overview.rst F: drivers/bus/fsl-mc/ F: include/uapi/linux/fsl_mc.h +QST QMC5883L 3-Axis Magnetic Sensor +M: Siratul Islam <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/devicetree/bindings/iio/magnetometer/qstcorp,qmc5883l.yaml + QT1010 MEDIA DRIVER L: [email protected] S: Orphan
Signature · 1 line
2.54.0
Siratul Islam
maintainerPATCH v2 3/3 iio: magnetometer: add driver for QST QMC5883L Sensor
In reply to Siratul IslamAdd driver for the QST QMC5883L 3-Axis Magnetic Sensor connected via i2c.
--- MAINTAINERS | 1 + drivers/iio/magnetometer/Kconfig | 11 + drivers/iio/magnetometer/Makefile | 2 + drivers/iio/magnetometer/qmc5883l.c | 516 ++++++++++++++++++++++++++++ 4 files changed, 530 insertions(+) create mode 100644 drivers/iio/magnetometer/qmc5883l.c
MAINTAINERS +1 -0
@@ -21792,6 +21792,7 @@ M: Siratul Islam <[email protected]> L: [email protected] S: Maintained F: Documentation/devicetree/bindings/iio/magnetometer/qstcorp,qmc5883l.yaml +F: drivers/iio/magnetometer/qmc5883l.c QT1010 MEDIA DRIVER L: [email protected]
drivers/iio/magnetometer/Kconfig +11 -0
@@ -198,6 +198,17 @@ config INFINEON_TLV493D To compile this driver as a module, choose M here: the module will be called tlv493d. +config QMC5883L + tristate "QST QMC5883L 3-Axis Magnetic Sensor" + depends on I2C + select REGMAP_I2C + help + Say Y here to add support driver for QST QMC5883L 3-Axis + Magnetic Sensor. + + To compile this driver as a module, choose M here: the + module will be called qmc5883l. + config SENSORS_HMC5843 tristate select IIO_BUFFER
drivers/iio/magnetometer/Makefile +2 -0
@@ -26,6 +26,8 @@ obj-$(CONFIG_IIO_ST_MAGN_SPI_3AXIS) += st_magn_spi.o obj-$(CONFIG_INFINEON_TLV493D) += tlv493d.o +obj-$(CONFIG_QMC5883L) += qmc5883l.o + obj-$(CONFIG_SENSORS_HMC5843) += hmc5843_core.o obj-$(CONFIG_SENSORS_HMC5843_I2C) += hmc5843_i2c.o obj-$(CONFIG_SENSORS_HMC5843_SPI) += hmc5843_spi.o
drivers/iio/magnetometer/qmc5883l.c new file +516 -0
516 lines not on this page · raw message
Signature · 1 line
2.54.0
Re: [PATCH v2 2/3] dt-bindings: iio: magnetometer: add QST QMC5883L Sensor
In reply to Siratul Islampw-bot: not-applicable
Quoted 2 lines · Siratul Islam
Add driver for the QST QMC5883L 3-Axis Magnetic Sensor connected via i2c.
...
drivers/iio/magnetometer/qmc5883l.c line 24
Quoted 13 lines
drivers/iio/magnetometer/qmc5883l.c new file +13
-0+#include <linux/array_size.h> +#include <linux/bits.h> +#include <linux/bitfield.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/mod_devicetable.h> +#include <linux/regmap.h> +#include <linux/regulator/consumer.h> +#include <linux/time.h>
+ types.h
drivers/iio/magnetometer/qmc5883l.c line 28
Quoted 3 lines
drivers/iio/magnetometer/qmc5883l.c new file +3
-0+#include <linux/iio/iio.h> + +#include <asm/byteorder.h>
Better to put generic headers before more custom ones
linux/*.h
...blank line...
asm/*.h
...blank line...
linux/iio/*.h
...
drivers/iio/magnetometer/qmc5883l.c line 85
Quoted 4 lines
drivers/iio/magnetometer/qmc5883l.c new file +4
-0+enum qmc5883l_chan { +QMC5883L_AXIS_X , +QMC5883L_AXIS_Y , +QMC5883L_AXIS_Z
Leave trailing comma, it's not a dedicated terminator.
Quoted 1 line
+};
...
drivers/iio/magnetometer/qmc5883l.c line 100
Quoted 4 lines
drivers/iio/magnetometer/qmc5883l.c new file +4
-0+static int qmc5883l_take_measurement (struct iio_dev *indio_dev,int index, +int *val) +{ +struct qmc5883l_data *data =iio_priv (indio_dev);
With
struct regmap *map = data->regmap;
the below will be shorter.
drivers/iio/magnetometer/qmc5883l.c line 109
Quoted 9 lines
drivers/iio/magnetometer/qmc5883l.c new file +9
-0+unsigned int status; +__le16 buf[3 ]; +int ret; + +guard (mutex) (&data->mutex); + +/* 50ms headroom over the slowest ODR (10Hz) */ + ret = regmap_read_poll_timeout(data->regmap, + QMC5883L_REG_STATUS1,
ret = regmap_read_poll_timeout(map, QMC5883L_REG_STATUS1,
drivers/iio/magnetometer/qmc5883l.c line 116
Quoted 7 lines
drivers/iio/magnetometer/qmc5883l.c new file +7
-0+ status, (status &QMC5883L_STATUS_DRDY ), + 2 * USEC_PER_MSEC, 150 * USEC_PER_MSEC); +if (ret) +return ret; + + ret =regmap_bulk_read (data->regmap,QMC5883L_REG_X_LSB , buf, +sizeof (buf));
ret = regmap_bulk_read(map, QMC5883L_REG_X_LSB, buf, sizeof(buf));
drivers/iio/magnetometer/qmc5883l.c line 123
Quoted 7 lines
drivers/iio/magnetometer/qmc5883l.c new file +7
-0+if (ret) +return ret; + +if (status &QMC5883L_STATUS_OVL ) +return -ERANGE ; + + *val = (s16 )le16_to_cpu (buf[index]);
While casting works, sign_extend32() is more explicit, but I leave it up to you and others to decide.
Quoted 3 lines
drivers/iio/magnetometer/qmc5883l.c new file +3
-0+ +return 0 ; +}
...
drivers/iio/magnetometer/qmc5883l.c line 342
Quoted 3 lines
drivers/iio/magnetometer/qmc5883l.c new file +3
-0+static int qmc5883l_init (struct qmc5883l_data *data) +{ +struct regmap *regmap = data->regmap;
Name it 'map'.
Quoted 5 lines
drivers/iio/magnetometer/qmc5883l.c new file +5
-0+unsigned int reg; +int ret; + + ret =regmap_read (regmap,QMC5883L_REG_ID , ®); +if (ret)
drivers/iio/magnetometer/qmc5883l.c line 357
Quoted 10 lines
drivers/iio/magnetometer/qmc5883l.c new file +10
-0+return ret; + +/* Not failing because rev 1.0 had this register reserved */ +if (reg !=QMC5883L_CHIP_ID ) +dev_warn (regmap_get_device (regmap), +"Unknown chip id: 0x%02x, continuing\n" , reg); + + ret =regmap_write (regmap,QMC5883L_REG_CTRL2 ,QMC5883L_SOFT_RESET ); +if (ret) +return ret;
Ideally this should have a comment with a reference to the datasheet where this delay is specified. Otherwise a comment why this exact value has been chosen.
Quoted 21 lines
drivers/iio/magnetometer/qmc5883l.c new file +21
-0+fsleep (QMC5883L_PORT_US ); + +/* DRDY pin no used in this version of the driver */ + ret =regmap_write (regmap,QMC5883L_REG_CTRL2 ,QMC5883L_INT_DISABLE ); +if (ret) +return ret; + + ret =regmap_write (regmap,QMC5883L_REG_SET_RESET ,QMC5883L_SET_RESET_VAL ); +if (ret) +return ret; + + data->odr =QMC5883L_ODR_50HZ ; + data->range =QMC5883L_RNG_2G ; + data->osr =QMC5883L_OSR_64 ; + +return regmap_write (regmap,QMC5883L_REG_CTRL1 , +FIELD_PREP (QMC5883L_MODE_MASK ,QMC5883L_MODE_CONT ) | +FIELD_PREP (QMC5883L_ODR_MASK , data->odr) | +FIELD_PREP (QMC5883L_RNG_MASK , data->range) | +FIELD_PREP (QMC5883L_OSR_MASK , data->osr)); +}
...
drivers/iio/magnetometer/qmc5883l.c line 413
Quoted 7 lines
drivers/iio/magnetometer/qmc5883l.c new file +7
-0+static const struct regmap_config qmc5883l_regmap_config = { + .reg_bits =8 , + .val_bits =8 , + .max_register =QMC5883L_REG_ID , + .cache_type =REGCACHE_MAPLE , + .volatile_reg = qmc5883l_volatile_reg, + .writeable_reg = qmc5883l_writable_reg
Leave trailing comma, it's not a dedicated terminator.
Quoted 1 line
+};
...
drivers/iio/magnetometer/qmc5883l.c line 436
Quoted 4 lines
drivers/iio/magnetometer/qmc5883l.c new file +4
-0+static const struct iio_chan_spec qmc5883l_channels[] = { +QMC5883L_CHANNEL (X ), +QMC5883L_CHANNEL (Y ), +QMC5883L_CHANNEL (Z )
Ditto.
Quoted 1 line
+};
...
drivers/iio/magnetometer/qmc5883l.c line 444
Quoted 6 lines
drivers/iio/magnetometer/qmc5883l.c new file +6
-0+static int qmc5883l_probe (struct i2c_client *client) +{ +struct device *dev = &client->dev; +struct qmc5883l_data *data; +struct iio_dev *indio_dev; +struct regmap *regmap;
Call it 'map'.
Quoted 46 lines
drivers/iio/magnetometer/qmc5883l.c new file +46
-0+int ret; + + indio_dev =devm_iio_device_alloc (dev,sizeof (*data)); +if (!indio_dev) +return -ENOMEM ; + + regmap =devm_regmap_init_i2c (client, &qmc5883l_regmap_config); +if (IS_ERR (regmap)) +return dev_err_probe (dev,PTR_ERR (regmap), +"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" ); + + ret =devm_regulator_get_enable (dev,"vddio" ); +if (ret) +return dev_err_probe (dev, ret, +"Failed to enable VDDIO regulator\n" ); + +fsleep (QMC5883L_PORT_US ); + + data =iio_priv (indio_dev); + data->regmap = regmap; + + ret =devm_mutex_init (dev, &data->mutex); +if (ret) +return ret; + + indio_dev->name ="qmc5883l" ; + indio_dev->info = &qmc5883l_info; + indio_dev->channels = qmc5883l_channels; + indio_dev->num_channels =ARRAY_SIZE (qmc5883l_channels); + indio_dev->modes =INDIO_DIRECT_MODE ; + + ret =qmc5883l_init (data); +if (ret) +return dev_err_probe (dev, ret,"qmc5883l init failed\n" ); + + ret =devm_add_action_or_reset (dev, qmc5883l_power_down_action, data); +if (ret) +return ret; + +return devm_iio_device_register (dev, indio_dev); +}
...
drivers/iio/magnetometer/qmc5883l.c line 510
Quoted 7 lines
drivers/iio/magnetometer/qmc5883l.c new file +7
-0+static struct i2c_driver qmc5883l_driver = { + .driver = { + .name ="qmc5883l" , + .of_match_table = qmc5883l_match, + }, + .id_table = qmc5883l_id, + .probe = qmc5883l_probe
Leave a trailing comma.
Quoted 1 line
+};
Signature · 2 lines
With Best Regards, Andy Shevchenko
Siratul Islam
maintainerQuoted 10 lines · Andy Shevchenko
Quoted 4 lines · Siratul Islam
Add driver for the QST QMC5883L 3-Axis Magnetic Sensor
connected via i2c....
Quoted 1 line
Quoted 9 lines
Quoted 5 lines
+enum
qmc5883l_chan{
+QMC5883L_AXIS_X,
+QMC5883L_AXIS_Y,
+QMC5883L_AXIS_ZLeave trailing comma, it's not a dedicated terminator.
Quoted 2 lines
+};
I generally have a habit of not adding a comma at the end as it reminds me of javascript/json. But sometimes I add it to help clang-format. Jonathan suggested me to remove the commas at "terminating" entries. So I took the opportunity to remove them everywhere. Apparently it was a wrong decision. Thnaks for pointing this out, I'll make it a new habit. As always I'm learning a lot from these reviews.
Quoted 11 lines
...
Quoted 5 lines
+
+ return 0;
+}...
Ideally this should have a comment with a reference to the datasheet where this delay is specified. Otherwise a comment why this exact value has been chosen.
I have a comment where I define this, about this value being from the datasheet. I'll update it to specify where though.
Quoted 2 lines
Quoted 2 lines
+
fsleep(QMC5883L_PORT_US);
...
Quoted 8 lines
Quoted 4 lines
+};
Signature · 2 lines
With Best Regards, Andy Shevchenko
Thanks
Sirat