All of lore.kernel.org
 help / color / mirror / Atom feed
From: Linus Walleij <linus.walleij@linaro.org>
To: Jonathan Cameron <jic23@kernel.org>, linux-iio@vger.kernel.org
Cc: Hartmut Knaack <knaack.h@gmx.de>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Peter Meerwald-Stadler <pmeerw@pmeerw.net>,
	Linus Walleij <linus.walleij@linaro.org>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Denis Ciocca <denis.ciocca@st.com>,
	Daniel Drake <drake@endlessm.com>
Subject: [PATCH] iio: accel: st_sensors: Support generic mounting matrix
Date: Sat, 15 May 2021 02:00:58 +0200	[thread overview]
Message-ID: <20210515000058.204601-1-linus.walleij@linaro.org> (raw)

The ST accelerators support a special type of quirky
mounting matrix found in ACPI systems, but not a generic
mounting matrix such as from the device tree.

Augment the ACPI hack to be a bit more generic and
accept a mounting matrix from device properties.

This makes it possible to fix orientation on the Ux500
HREF device.

Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Denis Ciocca <denis.ciocca@st.com>
Cc: Daniel Drake <drake@endlessm.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
 drivers/iio/accel/st_accel_core.c | 51 ++++++++++++++++++++-----------
 1 file changed, 34 insertions(+), 17 deletions(-)

diff --git a/drivers/iio/accel/st_accel_core.c b/drivers/iio/accel/st_accel_core.c
index 43c50167d220..cfbcf740e3cb 100644
--- a/drivers/iio/accel/st_accel_core.c
+++ b/drivers/iio/accel/st_accel_core.c
@@ -1069,26 +1069,25 @@ static const struct iio_trigger_ops st_accel_trigger_ops = {
 #define ST_ACCEL_TRIGGER_OPS NULL
 #endif
 
-#ifdef CONFIG_ACPI
 static const struct iio_mount_matrix *
-get_mount_matrix(const struct iio_dev *indio_dev,
-		 const struct iio_chan_spec *chan)
+st_accel_get_mount_matrix(const struct iio_dev *indio_dev,
+			  const struct iio_chan_spec *chan)
 {
 	struct st_sensor_data *adata = iio_priv(indio_dev);
 
 	return adata->mount_matrix;
 }
 
-static const struct iio_chan_spec_ext_info mount_matrix_ext_info[] = {
-	IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, get_mount_matrix),
+static const struct iio_chan_spec_ext_info st_accel_mount_matrix_ext_info[] = {
+	IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, st_accel_get_mount_matrix),
 	{ },
 };
 
+#ifdef CONFIG_ACPI
 /* Read ST-specific _ONT orientation data from ACPI and generate an
  * appropriate mount matrix.
  */
-static int apply_acpi_orientation(struct iio_dev *indio_dev,
-				  struct iio_chan_spec *channels)
+static int apply_acpi_orientation(struct iio_dev *indio_dev)
 {
 	struct st_sensor_data *adata = iio_priv(indio_dev);
 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
@@ -1207,22 +1206,20 @@ static int apply_acpi_orientation(struct iio_dev *indio_dev,
 		}
 	}
 
-	/* Expose the mount matrix via ext_info */
-	for (i = 0; i < indio_dev->num_channels; i++)
-		channels[i].ext_info = mount_matrix_ext_info;
-
 	ret = 0;
 	dev_info(&indio_dev->dev, "computed mount matrix from ACPI\n");
 
 out:
 	kfree(buffer.pointer);
+	dev_warn(&indio_dev->dev,
+		 "failed to apply ACPI orientation data: %d\n", ret);
+
 	return ret;
 }
 #else /* !CONFIG_ACPI */
-static int apply_acpi_orientation(struct iio_dev *indio_dev,
-				  struct iio_chan_spec *channels)
+static int apply_acpi_orientation(struct iio_dev *indio_dev)
 {
-	return 0;
+	return -EINVAL;
 }
 #endif
 
@@ -1251,6 +1248,7 @@ int st_accel_common_probe(struct iio_dev *indio_dev)
 	struct iio_chan_spec *channels;
 	size_t channels_size;
 	int err;
+	int i;
 
 	indio_dev->modes = INDIO_DIRECT_MODE;
 	indio_dev->info = &accel_info;
@@ -1275,9 +1273,28 @@ int st_accel_common_probe(struct iio_dev *indio_dev)
 		goto st_accel_power_off;
 	}
 
-	if (apply_acpi_orientation(indio_dev, channels))
-		dev_warn(&indio_dev->dev,
-			 "failed to apply ACPI orientation data: %d\n", err);
+	/* First try ACPI orientation then try the generic function */
+	err = apply_acpi_orientation(indio_dev);
+	if (err) {
+		adata->mount_matrix = devm_kmalloc(&indio_dev->dev,
+						   sizeof(*adata->mount_matrix),
+						   GFP_KERNEL);
+		if (!adata->mount_matrix) {
+			err = -ENOMEM;
+			goto st_accel_power_off;
+		}
+
+		err = iio_read_mount_matrix(adata->dev, "mount-matrix",
+					    adata->mount_matrix);
+		if (err)
+			goto st_accel_power_off;
+	}
+	/*
+	 * We have at least an identity matrix, so expose the mount
+	 * matrix via ext_info
+	 */
+	for (i = 0; i < indio_dev->num_channels; i++)
+		channels[i].ext_info = st_accel_mount_matrix_ext_info;
 
 	indio_dev->channels = channels;
 	adata->current_fullscale = &adata->sensor_settings->fs.fs_avl[0];
-- 
2.31.1


             reply	other threads:[~2021-05-15  0:03 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-15  0:00 Linus Walleij [this message]
2021-05-16  9:48 ` [PATCH] iio: accel: st_sensors: Support generic mounting matrix Jonathan Cameron
2021-05-16 16:20   ` Stephan Gerhold
2021-05-17  7:46 ` Andy Shevchenko
2021-05-17  9:23   ` Hans de Goede
2021-05-17 10:15     ` Linus Walleij

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210515000058.204601-1-linus.walleij@linaro.org \
    --to=linus.walleij@linaro.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=denis.ciocca@st.com \
    --cc=drake@endlessm.com \
    --cc=jic23@kernel.org \
    --cc=knaack.h@gmx.de \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=pmeerw@pmeerw.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.