Skip to content

Commit 3d8ad94

Browse files
Linus WalleijJonathan Cameron
authored andcommitted
iio: accel: st_sensors: Support generic mounting matrix
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: Hans de Goede <hdegoede@redhat.com> Cc: Denis Ciocca <denis.ciocca@st.com> Cc: Daniel Drake <drake@endlessm.com> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Signed-off-by: Stephan Gerhold <stephan@gerhold.net> Signed-off-by: Linus Walleij <linus.walleij@linaro.org> Reviewed-by: Hans de Goede <hdegoede@redhat.com> Link: https://lore.kernel.org/r/20210518230722.522446-2-linus.walleij@linaro.org Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 42ef8aa commit 3d8ad94

2 files changed

Lines changed: 62 additions & 54 deletions

File tree

‎drivers/iio/accel/st_accel_core.c‎

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -41,51 +41,74 @@
4141
#define ST_ACCEL_FS_AVL_200G 200
4242
#define ST_ACCEL_FS_AVL_400G 400
4343

44+
static const struct iio_mount_matrix *
45+
st_accel_get_mount_matrix(const struct iio_dev *indio_dev,
46+
const struct iio_chan_spec *chan)
47+
{
48+
struct st_sensor_data *adata = iio_priv(indio_dev);
49+
50+
return &adata->mount_matrix;
51+
}
52+
53+
static const struct iio_chan_spec_ext_info st_accel_mount_matrix_ext_info[] = {
54+
IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, st_accel_get_mount_matrix),
55+
{ }
56+
};
57+
4458
static const struct iio_chan_spec st_accel_8bit_channels[] = {
45-
ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
59+
ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
4660
BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
4761
ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 8, 8,
48-
ST_ACCEL_DEFAULT_OUT_X_L_ADDR+1),
49-
ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
62+
ST_ACCEL_DEFAULT_OUT_X_L_ADDR+1,
63+
st_accel_mount_matrix_ext_info),
64+
ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
5065
BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
5166
ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 8, 8,
52-
ST_ACCEL_DEFAULT_OUT_Y_L_ADDR+1),
53-
ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
67+
ST_ACCEL_DEFAULT_OUT_Y_L_ADDR+1,
68+
st_accel_mount_matrix_ext_info),
69+
ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
5470
BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
5571
ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 8, 8,
56-
ST_ACCEL_DEFAULT_OUT_Z_L_ADDR+1),
72+
ST_ACCEL_DEFAULT_OUT_Z_L_ADDR+1,
73+
st_accel_mount_matrix_ext_info),
5774
IIO_CHAN_SOFT_TIMESTAMP(3)
5875
};
5976

6077
static const struct iio_chan_spec st_accel_12bit_channels[] = {
61-
ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
78+
ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
6279
BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
6380
ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 12, 16,
64-
ST_ACCEL_DEFAULT_OUT_X_L_ADDR),
65-
ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
81+
ST_ACCEL_DEFAULT_OUT_X_L_ADDR,
82+
st_accel_mount_matrix_ext_info),
83+
ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
6684
BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
6785
ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 12, 16,
68-
ST_ACCEL_DEFAULT_OUT_Y_L_ADDR),
69-
ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
86+
ST_ACCEL_DEFAULT_OUT_Y_L_ADDR,
87+
st_accel_mount_matrix_ext_info),
88+
ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
7089
BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
7190
ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 12, 16,
72-
ST_ACCEL_DEFAULT_OUT_Z_L_ADDR),
91+
ST_ACCEL_DEFAULT_OUT_Z_L_ADDR,
92+
st_accel_mount_matrix_ext_info),
7393
IIO_CHAN_SOFT_TIMESTAMP(3)
7494
};
7595

7696
static const struct iio_chan_spec st_accel_16bit_channels[] = {
77-
ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
97+
ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
7898
BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
7999
ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
80-
ST_ACCEL_DEFAULT_OUT_X_L_ADDR),
81-
ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
100+
ST_ACCEL_DEFAULT_OUT_X_L_ADDR,
101+
st_accel_mount_matrix_ext_info),
102+
ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
82103
BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
83104
ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
84-
ST_ACCEL_DEFAULT_OUT_Y_L_ADDR),
85-
ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
105+
ST_ACCEL_DEFAULT_OUT_Y_L_ADDR,
106+
st_accel_mount_matrix_ext_info),
107+
ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
86108
BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
87109
ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
88-
ST_ACCEL_DEFAULT_OUT_Z_L_ADDR),
110+
ST_ACCEL_DEFAULT_OUT_Z_L_ADDR,
111+
st_accel_mount_matrix_ext_info),
89112
IIO_CHAN_SOFT_TIMESTAMP(3)
90113
};
91114

@@ -1162,25 +1185,10 @@ static const struct iio_trigger_ops st_accel_trigger_ops = {
11621185
#endif
11631186

11641187
#ifdef CONFIG_ACPI
1165-
static const struct iio_mount_matrix *
1166-
get_mount_matrix(const struct iio_dev *indio_dev,
1167-
const struct iio_chan_spec *chan)
1168-
{
1169-
struct st_sensor_data *adata = iio_priv(indio_dev);
1170-
1171-
return adata->mount_matrix;
1172-
}
1173-
1174-
static const struct iio_chan_spec_ext_info mount_matrix_ext_info[] = {
1175-
IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, get_mount_matrix),
1176-
{ },
1177-
};
1178-
11791188
/* Read ST-specific _ONT orientation data from ACPI and generate an
11801189
* appropriate mount matrix.
11811190
*/
1182-
static int apply_acpi_orientation(struct iio_dev *indio_dev,
1183-
struct iio_chan_spec *channels)
1191+
static int apply_acpi_orientation(struct iio_dev *indio_dev)
11841192
{
11851193
struct st_sensor_data *adata = iio_priv(indio_dev);
11861194
struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
@@ -1269,14 +1277,6 @@ static int apply_acpi_orientation(struct iio_dev *indio_dev,
12691277
}
12701278

12711279
/* Convert our integer matrix to a string-based iio_mount_matrix */
1272-
adata->mount_matrix = devm_kmalloc(&indio_dev->dev,
1273-
sizeof(*adata->mount_matrix),
1274-
GFP_KERNEL);
1275-
if (!adata->mount_matrix) {
1276-
ret = -ENOMEM;
1277-
goto out;
1278-
}
1279-
12801280
for (i = 0; i < 3; i++) {
12811281
for (j = 0; j < 3; j++) {
12821282
int matrix_val = final_ont[i][j];
@@ -1295,26 +1295,25 @@ static int apply_acpi_orientation(struct iio_dev *indio_dev,
12951295
default:
12961296
goto out;
12971297
}
1298-
adata->mount_matrix->rotation[i * 3 + j] = str_value;
1298+
adata->mount_matrix.rotation[i * 3 + j] = str_value;
12991299
}
13001300
}
13011301

1302-
/* Expose the mount matrix via ext_info */
1303-
for (i = 0; i < indio_dev->num_channels; i++)
1304-
channels[i].ext_info = mount_matrix_ext_info;
1305-
13061302
ret = 0;
13071303
dev_info(&indio_dev->dev, "computed mount matrix from ACPI\n");
13081304

13091305
out:
13101306
kfree(buffer.pointer);
1307+
if (ret)
1308+
dev_dbg(&indio_dev->dev,
1309+
"failed to apply ACPI orientation data: %d\n", ret);
1310+
13111311
return ret;
13121312
}
13131313
#else /* !CONFIG_ACPI */
1314-
static int apply_acpi_orientation(struct iio_dev *indio_dev,
1315-
struct iio_chan_spec *channels)
1314+
static int apply_acpi_orientation(struct iio_dev *indio_dev)
13161315
{
1317-
return 0;
1316+
return -EINVAL;
13181317
}
13191318
#endif
13201319

@@ -1361,9 +1360,16 @@ int st_accel_common_probe(struct iio_dev *indio_dev)
13611360
if (!channels)
13621361
return -ENOMEM;
13631362

1364-
if (apply_acpi_orientation(indio_dev, channels))
1365-
dev_warn(&indio_dev->dev,
1366-
"failed to apply ACPI orientation data: %d\n", err);
1363+
/*
1364+
* First try specific ACPI methods to retrieve orientation then try the
1365+
* generic function.
1366+
*/
1367+
err = apply_acpi_orientation(indio_dev);
1368+
if (err) {
1369+
err = iio_read_mount_matrix(adata->dev, &adata->mount_matrix);
1370+
if (err)
1371+
return err;
1372+
}
13671373

13681374
indio_dev->channels = channels;
13691375
adata->current_fullscale = &adata->sensor_settings->fs.fs_avl[0];

‎include/linux/iio/common/st_sensors.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/i2c.h>
1414
#include <linux/spi/spi.h>
1515
#include <linux/irqreturn.h>
16+
#include <linux/iio/iio.h>
1617
#include <linux/iio/trigger.h>
1718
#include <linux/bitops.h>
1819
#include <linux/regulator/consumer.h>
@@ -221,6 +222,7 @@ struct st_sensor_settings {
221222
* struct st_sensor_data - ST sensor device status
222223
* @dev: Pointer to instance of struct device (I2C or SPI).
223224
* @trig: The trigger in use by the core driver.
225+
* @mount_matrix: The mounting matrix of the sensor.
224226
* @sensor_settings: Pointer to the specific sensor settings in use.
225227
* @current_fullscale: Maximum range of measure by the sensor.
226228
* @vdd: Pointer to sensor's Vdd power supply
@@ -240,7 +242,7 @@ struct st_sensor_settings {
240242
struct st_sensor_data {
241243
struct device *dev;
242244
struct iio_trigger *trig;
243-
struct iio_mount_matrix *mount_matrix;
245+
struct iio_mount_matrix mount_matrix;
244246
struct st_sensor_settings *sensor_settings;
245247
struct st_sensor_fullscale_avl *current_fullscale;
246248
struct regulator *vdd;

0 commit comments

Comments
 (0)