diff mbox series

iio: accel: mma7660: add mount-matrix support

Message ID 20240527080043.2709-1-val@packett.cool (mailing list archive)
State Accepted
Headers show
Series iio: accel: mma7660: add mount-matrix support | expand

Commit Message

Val Packett May 27, 2024, 8 a.m. UTC
Allow using the mount-matrix device tree property to align the
accelerometer relative to the whole device.

Signed-off-by: Val Packett <val@packett.cool>
---
 drivers/iio/accel/mma7660.c | 50 ++++++++++++++++++++++++++-----------
 1 file changed, 35 insertions(+), 15 deletions(-)

Comments

Jonathan Cameron June 2, 2024, 1:08 p.m. UTC | #1
On Mon, 27 May 2024 05:00:18 -0300
Val Packett <val@packett.cool> wrote:

> Allow using the mount-matrix device tree property to align the
> accelerometer relative to the whole device.
> 
> Signed-off-by: Val Packett <val@packett.cool>
Applied to the togreg branch of iio.git.  Initially pushed out as testing
to let 0-day see if it can find anything we missed

Thanks,

Jonathan

> ---
>  drivers/iio/accel/mma7660.c | 50 ++++++++++++++++++++++++++-----------
>  1 file changed, 35 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/iio/accel/mma7660.c b/drivers/iio/accel/mma7660.c
> index 260cbceaa..9d23f28d5 100644
> --- a/drivers/iio/accel/mma7660.c
> +++ b/drivers/iio/accel/mma7660.c
> @@ -38,21 +38,6 @@
>  
>  static const int mma7660_nscale = 467142857;
>  
> -#define MMA7660_CHANNEL(reg, axis) {	\
> -	.type = IIO_ACCEL,	\
> -	.address = reg,	\
> -	.modified = 1,	\
> -	.channel2 = IIO_MOD_##axis,	\
> -	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),	\
> -	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
> -}
> -
> -static const struct iio_chan_spec mma7660_channels[] = {
> -	MMA7660_CHANNEL(MMA7660_REG_XOUT, X),
> -	MMA7660_CHANNEL(MMA7660_REG_YOUT, Y),
> -	MMA7660_CHANNEL(MMA7660_REG_ZOUT, Z),
> -};
> -
>  enum mma7660_mode {
>  	MMA7660_MODE_STANDBY,
>  	MMA7660_MODE_ACTIVE
> @@ -62,6 +47,21 @@ struct mma7660_data {
>  	struct i2c_client *client;
>  	struct mutex lock;
>  	enum mma7660_mode mode;
> +	struct iio_mount_matrix orientation;
> +};
> +
> +static const struct iio_mount_matrix *
> +mma7660_get_mount_matrix(const struct iio_dev *indio_dev,
> +			const struct iio_chan_spec *chan)
> +{
> +	struct mma7660_data *data = iio_priv(indio_dev);
> +
> +	return &data->orientation;
> +}
> +
> +static const struct iio_chan_spec_ext_info mma7660_ext_info[] = {
> +	IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, mma7660_get_mount_matrix),
> +	{ }
>  };
>  
>  static IIO_CONST_ATTR(in_accel_scale_available, MMA7660_SCALE_AVAIL);
> @@ -75,6 +75,22 @@ static const struct attribute_group mma7660_attribute_group = {
>  	.attrs = mma7660_attributes
>  };
>  
> +#define MMA7660_CHANNEL(reg, axis) {	\
> +	.type = IIO_ACCEL,	\
> +	.address = reg,	\
> +	.modified = 1,	\
> +	.channel2 = IIO_MOD_##axis,	\
> +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),	\
> +	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
> +	.ext_info = mma7660_ext_info,				\
> +}
> +
> +static const struct iio_chan_spec mma7660_channels[] = {
> +	MMA7660_CHANNEL(MMA7660_REG_XOUT, X),
> +	MMA7660_CHANNEL(MMA7660_REG_YOUT, Y),
> +	MMA7660_CHANNEL(MMA7660_REG_ZOUT, Z),
> +};
> +
>  static int mma7660_set_mode(struct mma7660_data *data,
>  				enum mma7660_mode mode)
>  {
> @@ -187,6 +203,10 @@ static int mma7660_probe(struct i2c_client *client)
>  	mutex_init(&data->lock);
>  	data->mode = MMA7660_MODE_STANDBY;
>  
> +	ret = iio_read_mount_matrix(&client->dev, &data->orientation);
> +	if (ret)
> +		return ret;
> +
>  	indio_dev->info = &mma7660_info;
>  	indio_dev->name = MMA7660_DRIVER_NAME;
>  	indio_dev->modes = INDIO_DIRECT_MODE;
diff mbox series

Patch

diff --git a/drivers/iio/accel/mma7660.c b/drivers/iio/accel/mma7660.c
index 260cbceaa..9d23f28d5 100644
--- a/drivers/iio/accel/mma7660.c
+++ b/drivers/iio/accel/mma7660.c
@@ -38,21 +38,6 @@ 
 
 static const int mma7660_nscale = 467142857;
 
-#define MMA7660_CHANNEL(reg, axis) {	\
-	.type = IIO_ACCEL,	\
-	.address = reg,	\
-	.modified = 1,	\
-	.channel2 = IIO_MOD_##axis,	\
-	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),	\
-	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
-}
-
-static const struct iio_chan_spec mma7660_channels[] = {
-	MMA7660_CHANNEL(MMA7660_REG_XOUT, X),
-	MMA7660_CHANNEL(MMA7660_REG_YOUT, Y),
-	MMA7660_CHANNEL(MMA7660_REG_ZOUT, Z),
-};
-
 enum mma7660_mode {
 	MMA7660_MODE_STANDBY,
 	MMA7660_MODE_ACTIVE
@@ -62,6 +47,21 @@  struct mma7660_data {
 	struct i2c_client *client;
 	struct mutex lock;
 	enum mma7660_mode mode;
+	struct iio_mount_matrix orientation;
+};
+
+static const struct iio_mount_matrix *
+mma7660_get_mount_matrix(const struct iio_dev *indio_dev,
+			const struct iio_chan_spec *chan)
+{
+	struct mma7660_data *data = iio_priv(indio_dev);
+
+	return &data->orientation;
+}
+
+static const struct iio_chan_spec_ext_info mma7660_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, mma7660_get_mount_matrix),
+	{ }
 };
 
 static IIO_CONST_ATTR(in_accel_scale_available, MMA7660_SCALE_AVAIL);
@@ -75,6 +75,22 @@  static const struct attribute_group mma7660_attribute_group = {
 	.attrs = mma7660_attributes
 };
 
+#define MMA7660_CHANNEL(reg, axis) {	\
+	.type = IIO_ACCEL,	\
+	.address = reg,	\
+	.modified = 1,	\
+	.channel2 = IIO_MOD_##axis,	\
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),	\
+	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
+	.ext_info = mma7660_ext_info,				\
+}
+
+static const struct iio_chan_spec mma7660_channels[] = {
+	MMA7660_CHANNEL(MMA7660_REG_XOUT, X),
+	MMA7660_CHANNEL(MMA7660_REG_YOUT, Y),
+	MMA7660_CHANNEL(MMA7660_REG_ZOUT, Z),
+};
+
 static int mma7660_set_mode(struct mma7660_data *data,
 				enum mma7660_mode mode)
 {
@@ -187,6 +203,10 @@  static int mma7660_probe(struct i2c_client *client)
 	mutex_init(&data->lock);
 	data->mode = MMA7660_MODE_STANDBY;
 
+	ret = iio_read_mount_matrix(&client->dev, &data->orientation);
+	if (ret)
+		return ret;
+
 	indio_dev->info = &mma7660_info;
 	indio_dev->name = MMA7660_DRIVER_NAME;
 	indio_dev->modes = INDIO_DIRECT_MODE;