iio: accel: mc3230: improve driver, add mc3510c
Subsystems (2)
v2 cover iio: accel: mc3230: improve driver, add mc3510c
Changes includes:
- Add mount matrix handling
- Add match table to work with DT
- Add MC3510C support
MC3510C use same registors as MC3230, but different value scale.
---
Changes in v2:
- Ordered commits
- Fixed comment style
- Ordered struct members
- Fixed device table
- MC5310C commit splitted
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Vasiliy Doylov (5):
dt-bindings: iio: accel: mc3230: document mc3510c
iio: accel: mc3230: add mount matrix support
iio: accel: mc3230: add OF match table
iio: accel: mc3230: add multiple devices support
iio: accel: mc3230: add mc3510c support
.../devicetree/bindings/trivial-devices.yaml | 2 + drivers/iio/accel/mc3230.c | 93 ++++++++++++++++++---- 2 files changed, 79 insertions(+), 16 deletions(-)
---
base-commit: 2b88851f583d3c4e40bcd40cfe1965241ec229dd
change-id: 20250111-mainlining-mc3510c-564702fba487
Best regards,
Signature · 1 line
Vasiliy Doylov <[email protected]>
PATCH v2 1/5 dt-bindings: iio: accel: mc3230: document mc3510c
The MC3510C is a 3 asix digital accelerometer.
It handled by the same driver as MC3230.
Document it as a trivial device.
--- Documentation/devicetree/bindings/trivial-devices.yaml | 2 ++ 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/trivial-devices.yaml +2 -0
@@ -187,6 +187,8 @@ properties: - maxim,max6621 # mCube 3-axis 8-bit digital accelerometer - mcube,mc3230 + # mCube 3-axis 8-bit digital accelerometer + - mcube,mc3510c # Measurement Specialities I2C temperature and humidity sensor - meas,htu21 # Measurement Specialities I2C pressure and temperature sensor
Signature · 1 line
2.47.1
PATCH v2 2/5 iio: accel: mc3230: add mount matrix support
In reply to Vasiliy DoylovThis patch allows to read a mount-matrix device tree property and report to user-space or in-kernel iio clients.
--- drivers/iio/accel/mc3230.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-)
drivers/iio/accel/mc3230.c +24 -4
@@ -44,18 +44,34 @@ static const int mc3230_nscale = 115411765; .channel2 = IIO_MOD_##axis, \ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ + .ext_info = mc3230_ext_info, \ } +struct mc3230_data { +struct i2c_client *client; +struct iio_mount_matrix orientation; +}; + +static const struct iio_mount_matrix * +mc3230_get_mount_matrix (const struct iio_dev *indio_dev, +const struct iio_chan_spec *chan) +{ +struct mc3230_data *data =iio_priv (indio_dev); + +return &data->orientation; +} + +static const struct iio_chan_spec_ext_info mc3230_ext_info[] = { +IIO_MOUNT_MATRIX (IIO_SHARED_BY_DIR , mc3230_get_mount_matrix), + {} +}; +static const struct iio_chan_spec mc3230_channels[] = {MC3230_CHANNEL (MC3230_REG_XOUT ,X ),MC3230_CHANNEL (MC3230_REG_YOUT ,Y ),MC3230_CHANNEL (MC3230_REG_ZOUT ,Z ), }; -struct mc3230_data { -struct i2c_client *client; -}; -static int mc3230_set_opcon (struct mc3230_data *data,int opcon) {int ret;
@@ -141,6 +157,10 @@ static int mc3230_probe(struct i2c_client *client)if (ret <0 )return ret; + ret =iio_read_mount_matrix (&client->dev, &data->orientation); +if (ret) +return ret; + ret =iio_device_register (indio_dev);if (ret <0 ) {dev_err (&client->dev,"device_register failed\n" );
Signature · 1 line
2.47.1
PATCH v2 3/5 iio: accel: mc3230: add OF match table
In reply to Vasiliy DoylovThis will make the driver auto loaded via device-tree.
--- drivers/iio/accel/mc3230.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/iio/accel/mc3230.c +7 -0
@@ -205,9 +205,16 @@ static const struct i2c_device_id mc3230_i2c_id[] = { };MODULE_DEVICE_TABLE (i2c, mc3230_i2c_id); +static const struct of_device_id mc3230_of_match[] = { + { .compatible ="mcube,mc3230" }, + {} +}; +MODULE_DEVICE_TABLE (of, mc3230_of_match); +static struct i2c_driver mc3230_driver = { .driver = { .name ="mc3230" , + .of_match_table = mc3230_of_match, .pm =pm_sleep_ptr (&mc3230_pm_ops), }, .probe = mc3230_probe,
Signature · 1 line
2.47.1
PATCH v2 4/5 iio: accel: mc3230: add multiple devices support
In reply to Vasiliy DoylovThis patch allows to add new devices to this driver.
--- drivers/iio/accel/mc3230.c | 50 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 13 deletions(-)
drivers/iio/accel/mc3230.c +37 -13
@@ -22,20 +22,29 @@#define MC3230_MODE_OPCON_STANDBY 0x03#define MC3230_REG_CHIP_ID 0x18 -#define MC3230_CHIP_ID 0x01 -#define MC3230_REG_PRODUCT_CODE 0x3b -#define MC3230_PRODUCT_CODE 0x19/* * The accelerometer has one measurement range: * * -1.5g - +1.5g (8-bit, signed) * -* scale = (1.5 + 1.5) * 9.81 / (2^8 - 1) = 0.115411765 */ -static const int mc3230_nscale =115411765 ; +struct mc3230_chip_info { +const u8 chip_id; +const char *name; +const u8 product_code; +const int scale; +}; + +static struct mc3230_chip_info mc3230_chip_info = { + .chip_id =0x01 , + .name ="mc3230" , + .product_code =0x19 , +/* (1.5 + 1.5) * 9.81 / (2^8 - 1) = 0.115411765 */ + .scale =115411765 , +};#define MC3230_CHANNEL (reg, axis) { \ .type = IIO_ACCEL, \
@@ -48,6 +57,7 @@ static const int mc3230_nscale = 115411765; }struct mc3230_data { +const struct mc3xxx_chip_info *chip_info;struct i2c_client *client;struct iio_mount_matrix orientation; };
@@ -111,7 +121,7 @@ static int mc3230_read_raw(struct iio_dev *indio_dev,return IIO_VAL_INT ;case IIO_CHAN_INFO_SCALE : *val =0 ; - *val2 = mc3230_nscale; + *val2 = data->chip_info->scale;return IIO_VAL_INT_PLUS_NANO ;default :return -EINVAL ;
@@ -127,15 +137,28 @@ static int mc3230_probe(struct i2c_client *client)int ret;struct iio_dev *indio_dev;struct mc3230_data *data; +const struct mc3xxx_chip_info *chip_info; + + chip_info =i2c_get_match_data (client); +if (chip_info ==NULL ) { +dev_err (&client->dev,"failed to get match data" ); +return -ENODATA ; + }/* First check chip-id and product-id */ ret =i2c_smbus_read_byte_data (client,MC3230_REG_CHIP_ID ); -if (ret !=MC3230_CHIP_ID ) -return (ret <0 ) ? ret : -ENODEV ; +if (ret != chip_info->chip_id) { +dev_info (&client->dev, +"chip id check fail: 0x%x != 0x%x !\n" , + ret, chip_info->chip_id); + } ret =i2c_smbus_read_byte_data (client,MC3230_REG_PRODUCT_CODE ); -if (ret !=MC3230_PRODUCT_CODE ) -return (ret <0 ) ? ret : -ENODEV ; +if (ret != chip_info->product_code) { +dev_info (&client->dev, +"product code check fail: 0x%x != 0x%x !\n" , + ret, chip_info->product_code); + } indio_dev =devm_iio_device_alloc (&client->dev,sizeof (*data));if (!indio_dev) {
@@ -144,11 +167,12 @@ static int mc3230_probe(struct i2c_client *client) } data =iio_priv (indio_dev); + data->chip_info = chip_info; data->client = client;i2c_set_clientdata (client, indio_dev); indio_dev->info = &mc3230_info; - indio_dev->name ="mc3230" ; + indio_dev->name = chip_info->name; indio_dev->modes =INDIO_DIRECT_MODE ; indio_dev->channels = mc3230_channels; indio_dev->num_channels =ARRAY_SIZE (mc3230_channels);
@@ -200,13 +224,13 @@ static int mc3230_resume(struct device *dev) static DEFINE_SIMPLE_DEV_PM_OPS(mc3230_pm_ops, mc3230_suspend, mc3230_resume);static const struct i2c_device_id mc3230_i2c_id[] = { - {"mc3230" }, + {"mc3230" , (kernel_ulong_t)&mc3230_chip_info }, {} };MODULE_DEVICE_TABLE (i2c, mc3230_i2c_id);static const struct of_device_id mc3230_of_match[] = { - { .compatible ="mcube,mc3230" }, + { .compatible ="mcube,mc3230" , &c3230_chip_info }, {} };MODULE_DEVICE_TABLE (of, mc3230_of_match);
Signature · 1 line
2.47.1
PATCH v2 5/5 iio: accel: mc3230: add mc3510c support
In reply to Vasiliy DoylovThis commit integrates support for the mc3510c into the mc3230 driver.
Tested on Huawei MediaPad T3 10 (huawei-agassi)
--- drivers/iio/accel/mc3230.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/iio/accel/mc3230.c +10 -0
@@ -46,6 +46,14 @@ static struct mc3230_chip_info mc3230_chip_info = { .scale = 115411765, }; +static struct mc3230_chip_info mc3510c_chip_info = { + .chip_id =0x23 , + .name ="mc3510c" , + .product_code =0x10 , +/* Was obtained empirically */ + .scale =625000000 , +}; +#define MC3230_CHANNEL (reg, axis) { \ .type = IIO_ACCEL, \ .address = reg, \
@@ -225,12 +233,14 @@ static DEFINE_SIMPLE_DEV_PM_OPS(mc3230_pm_ops, mc3230_suspend, mc3230_resume);static const struct i2c_device_id mc3230_i2c_id[] = { {"mc3230" , (kernel_ulong_t)&mc3230_chip_info }, + {"mc3510c" , (kernel_ulong_t)&mc3510c_chip_info }, {} };MODULE_DEVICE_TABLE (i2c, mc3230_i2c_id);static const struct of_device_id mc3230_of_match[] = { { .compatible ="mcube,mc3230" , &c3230_chip_info }, + { .compatible ="mcube,mc3510c" , &mc3510c_chip_info }, {} };MODULE_DEVICE_TABLE (of, mc3230_of_match);
Signature · 1 line
2.47.1
I've fucked up with it. Sorry
Quoted 33 lines · On 1/12/25 3:06 PM, Vasiliy Doylov via B4 Relay wrote:
Changes includes:
- Add mount matrix handling
- Add match table to work with DT
- Add MC3510C supportMC3510C use same registors as MC3230, but different value scale.
Signed-off-by Vasiliy Doylov <[email protected]>---
Changes in v2:
- Ordered commits
- Fixed comment style
- Ordered struct members
- Fixed device table
- MC5310C commit splitted
- Link to v1: https://lore.kernel.org/r/[email protected]---
Vasiliy Doylov (5):
dt-bindings: iio: accel: mc3230: document mc3510c
iio: accel: mc3230: add mount matrix support
iio: accel: mc3230: add OF match table
iio: accel: mc3230: add multiple devices support
iio: accel: mc3230: add mc3510c support.../devicetree/bindings/trivial-devices.yaml | 2 + drivers/iio/accel/mc3230.c | 93 ++++++++++++++++++---- 2 files changed, 79 insertions(+), 16 deletions(-)---
base-commit:2b88851f583d3c4e40bcd40cfe1965241ec229dd
change-id: 20250111-mainlining-mc3510c-564702fba487Best regards,