forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/iio/accel/st_accel_core.c
....@@ -1,22 +1,20 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * STMicroelectronics accelerometers driver
34 *
45 * Copyright 2012-2013 STMicroelectronics Inc.
56 *
67 * Denis Ciocca <denis.ciocca@st.com>
7
- *
8
- * Licensed under the GPL-2.
98 */
109
1110 #include <linux/kernel.h>
1211 #include <linux/module.h>
1312 #include <linux/slab.h>
13
+#include <linux/acpi.h>
1414 #include <linux/errno.h>
1515 #include <linux/types.h>
16
-#include <linux/mutex.h>
1716 #include <linux/interrupt.h>
1817 #include <linux/i2c.h>
19
-#include <linux/gpio.h>
2018 #include <linux/irq.h>
2119 #include <linux/iio/iio.h>
2220 #include <linux/iio/sysfs.h>
....@@ -103,6 +101,7 @@
103101 [4] = LSM330DLC_ACCEL_DEV_NAME,
104102 [5] = LSM303AGR_ACCEL_DEV_NAME,
105103 [6] = LIS2DH12_ACCEL_DEV_NAME,
104
+ [7] = LIS3DE_ACCEL_DEV_NAME,
106105 },
107106 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
108107 .odr = {
....@@ -829,6 +828,159 @@
829828 .multi_read_bit = false,
830829 .bootime = 2,
831830 },
831
+ {
832
+ .wai = 0x33,
833
+ .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
834
+ .sensors_supported = {
835
+ [0] = LIS2DE12_ACCEL_DEV_NAME,
836
+ },
837
+ .ch = (struct iio_chan_spec *)st_accel_8bit_channels,
838
+ .odr = {
839
+ .addr = 0x20,
840
+ .mask = 0xf0,
841
+ .odr_avl = {
842
+ { .hz = 1, .value = 0x01, },
843
+ { .hz = 10, .value = 0x02, },
844
+ { .hz = 25, .value = 0x03, },
845
+ { .hz = 50, .value = 0x04, },
846
+ { .hz = 100, .value = 0x05, },
847
+ { .hz = 200, .value = 0x06, },
848
+ { .hz = 400, .value = 0x07, },
849
+ { .hz = 1620, .value = 0x08, },
850
+ { .hz = 5376, .value = 0x09, },
851
+ },
852
+ },
853
+ .pw = {
854
+ .addr = 0x20,
855
+ .mask = 0xf0,
856
+ .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
857
+ },
858
+ .enable_axis = {
859
+ .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
860
+ .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
861
+ },
862
+ .fs = {
863
+ .addr = 0x23,
864
+ .mask = 0x30,
865
+ .fs_avl = {
866
+ [0] = {
867
+ .num = ST_ACCEL_FS_AVL_2G,
868
+ .value = 0x00,
869
+ .gain = IIO_G_TO_M_S_2(15600),
870
+ },
871
+ [1] = {
872
+ .num = ST_ACCEL_FS_AVL_4G,
873
+ .value = 0x01,
874
+ .gain = IIO_G_TO_M_S_2(31200),
875
+ },
876
+ [2] = {
877
+ .num = ST_ACCEL_FS_AVL_8G,
878
+ .value = 0x02,
879
+ .gain = IIO_G_TO_M_S_2(62500),
880
+ },
881
+ [3] = {
882
+ .num = ST_ACCEL_FS_AVL_16G,
883
+ .value = 0x03,
884
+ .gain = IIO_G_TO_M_S_2(187500),
885
+ },
886
+ },
887
+ },
888
+ .drdy_irq = {
889
+ .int1 = {
890
+ .addr = 0x22,
891
+ .mask = 0x10,
892
+ },
893
+ .addr_ihl = 0x25,
894
+ .mask_ihl = 0x02,
895
+ .stat_drdy = {
896
+ .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
897
+ .mask = 0x07,
898
+ },
899
+ },
900
+ .sim = {
901
+ .addr = 0x23,
902
+ .value = BIT(0),
903
+ },
904
+ .multi_read_bit = true,
905
+ .bootime = 2,
906
+ },
907
+ {
908
+ .wai = 0x41,
909
+ .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
910
+ .sensors_supported = {
911
+ [0] = LIS2HH12_ACCEL_DEV_NAME,
912
+ },
913
+ .ch = (struct iio_chan_spec *)st_accel_16bit_channels,
914
+ .odr = {
915
+ .addr = 0x20,
916
+ .mask = 0x70,
917
+ .odr_avl = {
918
+ { .hz = 10, .value = 0x01, },
919
+ { .hz = 50, .value = 0x02, },
920
+ { .hz = 100, .value = 0x03, },
921
+ { .hz = 200, .value = 0x04, },
922
+ { .hz = 400, .value = 0x05, },
923
+ { .hz = 800, .value = 0x06, },
924
+ },
925
+ },
926
+ .pw = {
927
+ .addr = 0x20,
928
+ .mask = 0x70,
929
+ .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
930
+ },
931
+ .enable_axis = {
932
+ .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
933
+ .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
934
+ },
935
+ .fs = {
936
+ .addr = 0x23,
937
+ .mask = 0x30,
938
+ .fs_avl = {
939
+ [0] = {
940
+ .num = ST_ACCEL_FS_AVL_2G,
941
+ .value = 0x00,
942
+ .gain = IIO_G_TO_M_S_2(61),
943
+ },
944
+ [1] = {
945
+ .num = ST_ACCEL_FS_AVL_4G,
946
+ .value = 0x02,
947
+ .gain = IIO_G_TO_M_S_2(122),
948
+ },
949
+ [2] = {
950
+ .num = ST_ACCEL_FS_AVL_8G,
951
+ .value = 0x03,
952
+ .gain = IIO_G_TO_M_S_2(244),
953
+ },
954
+ },
955
+ },
956
+ .bdu = {
957
+ .addr = 0x20,
958
+ .mask = 0x08,
959
+ },
960
+ .drdy_irq = {
961
+ .int1 = {
962
+ .addr = 0x22,
963
+ .mask = 0x01,
964
+ },
965
+ .int2 = {
966
+ .addr = 0x25,
967
+ .mask = 0x01,
968
+ },
969
+ .addr_ihl = 0x24,
970
+ .mask_ihl = 0x02,
971
+ .stat_drdy = {
972
+ .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
973
+ .mask = 0x07,
974
+ },
975
+ },
976
+ .sim = {
977
+ .addr = 0x23,
978
+ .value = BIT(0),
979
+ },
980
+ .multi_read_bit = true,
981
+ .bootime = 2,
982
+ },
983
+
832984 };
833985
834986 static int st_accel_read_raw(struct iio_dev *indio_dev,
....@@ -917,35 +1069,212 @@
9171069 #define ST_ACCEL_TRIGGER_OPS NULL
9181070 #endif
9191071
1072
+#ifdef CONFIG_ACPI
1073
+static const struct iio_mount_matrix *
1074
+get_mount_matrix(const struct iio_dev *indio_dev,
1075
+ const struct iio_chan_spec *chan)
1076
+{
1077
+ struct st_sensor_data *adata = iio_priv(indio_dev);
1078
+
1079
+ return adata->mount_matrix;
1080
+}
1081
+
1082
+static const struct iio_chan_spec_ext_info mount_matrix_ext_info[] = {
1083
+ IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, get_mount_matrix),
1084
+ { },
1085
+};
1086
+
1087
+/* Read ST-specific _ONT orientation data from ACPI and generate an
1088
+ * appropriate mount matrix.
1089
+ */
1090
+static int apply_acpi_orientation(struct iio_dev *indio_dev,
1091
+ struct iio_chan_spec *channels)
1092
+{
1093
+ struct st_sensor_data *adata = iio_priv(indio_dev);
1094
+ struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1095
+ struct acpi_device *adev;
1096
+ union acpi_object *ont;
1097
+ union acpi_object *elements;
1098
+ acpi_status status;
1099
+ int ret = -EINVAL;
1100
+ unsigned int val;
1101
+ int i, j;
1102
+ int final_ont[3][3] = { { 0 }, };
1103
+
1104
+ /* For some reason, ST's _ONT translation does not apply directly
1105
+ * to the data read from the sensor. Another translation must be
1106
+ * performed first, as described by the matrix below. Perhaps
1107
+ * ST required this specific translation for the first product
1108
+ * where the device was mounted?
1109
+ */
1110
+ const int default_ont[3][3] = {
1111
+ { 0, 1, 0 },
1112
+ { -1, 0, 0 },
1113
+ { 0, 0, -1 },
1114
+ };
1115
+
1116
+
1117
+ adev = ACPI_COMPANION(adata->dev);
1118
+ if (!adev)
1119
+ return 0;
1120
+
1121
+ /* Read _ONT data, which should be a package of 6 integers. */
1122
+ status = acpi_evaluate_object(adev->handle, "_ONT", NULL, &buffer);
1123
+ if (status == AE_NOT_FOUND) {
1124
+ return 0;
1125
+ } else if (ACPI_FAILURE(status)) {
1126
+ dev_warn(&indio_dev->dev, "failed to execute _ONT: %d\n",
1127
+ status);
1128
+ return status;
1129
+ }
1130
+
1131
+ ont = buffer.pointer;
1132
+ if (ont->type != ACPI_TYPE_PACKAGE || ont->package.count != 6)
1133
+ goto out;
1134
+
1135
+ /* The first 3 integers provide axis order information.
1136
+ * e.g. 0 1 2 would indicate normal X,Y,Z ordering.
1137
+ * e.g. 1 0 2 indicates that data arrives in order Y,X,Z.
1138
+ */
1139
+ elements = ont->package.elements;
1140
+ for (i = 0; i < 3; i++) {
1141
+ if (elements[i].type != ACPI_TYPE_INTEGER)
1142
+ goto out;
1143
+
1144
+ val = elements[i].integer.value;
1145
+ if (val > 2)
1146
+ goto out;
1147
+
1148
+ /* Avoiding full matrix multiplication, we simply reorder the
1149
+ * columns in the default_ont matrix according to the
1150
+ * ordering provided by _ONT.
1151
+ */
1152
+ final_ont[0][i] = default_ont[0][val];
1153
+ final_ont[1][i] = default_ont[1][val];
1154
+ final_ont[2][i] = default_ont[2][val];
1155
+ }
1156
+
1157
+ /* The final 3 integers provide sign flip information.
1158
+ * 0 means no change, 1 means flip.
1159
+ * e.g. 0 0 1 means that Z data should be sign-flipped.
1160
+ * This is applied after the axis reordering from above.
1161
+ */
1162
+ elements += 3;
1163
+ for (i = 0; i < 3; i++) {
1164
+ if (elements[i].type != ACPI_TYPE_INTEGER)
1165
+ goto out;
1166
+
1167
+ val = elements[i].integer.value;
1168
+ if (val != 0 && val != 1)
1169
+ goto out;
1170
+ if (!val)
1171
+ continue;
1172
+
1173
+ /* Flip the values in the indicated column */
1174
+ final_ont[0][i] *= -1;
1175
+ final_ont[1][i] *= -1;
1176
+ final_ont[2][i] *= -1;
1177
+ }
1178
+
1179
+ /* Convert our integer matrix to a string-based iio_mount_matrix */
1180
+ adata->mount_matrix = devm_kmalloc(&indio_dev->dev,
1181
+ sizeof(*adata->mount_matrix),
1182
+ GFP_KERNEL);
1183
+ if (!adata->mount_matrix) {
1184
+ ret = -ENOMEM;
1185
+ goto out;
1186
+ }
1187
+
1188
+ for (i = 0; i < 3; i++) {
1189
+ for (j = 0; j < 3; j++) {
1190
+ int matrix_val = final_ont[i][j];
1191
+ char *str_value;
1192
+
1193
+ switch (matrix_val) {
1194
+ case -1:
1195
+ str_value = "-1";
1196
+ break;
1197
+ case 0:
1198
+ str_value = "0";
1199
+ break;
1200
+ case 1:
1201
+ str_value = "1";
1202
+ break;
1203
+ default:
1204
+ goto out;
1205
+ }
1206
+ adata->mount_matrix->rotation[i * 3 + j] = str_value;
1207
+ }
1208
+ }
1209
+
1210
+ /* Expose the mount matrix via ext_info */
1211
+ for (i = 0; i < indio_dev->num_channels; i++)
1212
+ channels[i].ext_info = mount_matrix_ext_info;
1213
+
1214
+ ret = 0;
1215
+ dev_info(&indio_dev->dev, "computed mount matrix from ACPI\n");
1216
+
1217
+out:
1218
+ kfree(buffer.pointer);
1219
+ return ret;
1220
+}
1221
+#else /* !CONFIG_ACPI */
1222
+static int apply_acpi_orientation(struct iio_dev *indio_dev,
1223
+ struct iio_chan_spec *channels)
1224
+{
1225
+ return 0;
1226
+}
1227
+#endif
1228
+
1229
+/*
1230
+ * st_accel_get_settings() - get sensor settings from device name
1231
+ * @name: device name buffer reference.
1232
+ *
1233
+ * Return: valid reference on success, NULL otherwise.
1234
+ */
1235
+const struct st_sensor_settings *st_accel_get_settings(const char *name)
1236
+{
1237
+ int index = st_sensors_get_settings_index(name,
1238
+ st_accel_sensors_settings,
1239
+ ARRAY_SIZE(st_accel_sensors_settings));
1240
+ if (index < 0)
1241
+ return NULL;
1242
+
1243
+ return &st_accel_sensors_settings[index];
1244
+}
1245
+EXPORT_SYMBOL(st_accel_get_settings);
1246
+
9201247 int st_accel_common_probe(struct iio_dev *indio_dev)
9211248 {
9221249 struct st_sensor_data *adata = iio_priv(indio_dev);
923
- struct st_sensors_platform_data *pdata =
924
- (struct st_sensors_platform_data *)adata->dev->platform_data;
925
- int irq = adata->get_irq_data_ready(indio_dev);
1250
+ struct st_sensors_platform_data *pdata = dev_get_platdata(adata->dev);
1251
+ struct iio_chan_spec *channels;
1252
+ size_t channels_size;
9261253 int err;
9271254
9281255 indio_dev->modes = INDIO_DIRECT_MODE;
9291256 indio_dev->info = &accel_info;
930
- mutex_init(&adata->tb.buf_lock);
9311257
932
- err = st_sensors_power_enable(indio_dev);
933
- if (err)
1258
+ err = st_sensors_verify_id(indio_dev);
1259
+ if (err < 0)
9341260 return err;
9351261
936
- err = st_sensors_check_device_support(indio_dev,
937
- ARRAY_SIZE(st_accel_sensors_settings),
938
- st_accel_sensors_settings);
939
- if (err < 0)
940
- goto st_accel_power_off;
941
-
9421262 adata->num_data_channels = ST_ACCEL_NUMBER_DATA_CHANNELS;
943
- adata->multiread_bit = adata->sensor_settings->multi_read_bit;
944
- indio_dev->channels = adata->sensor_settings->ch;
9451263 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
9461264
947
- adata->current_fullscale = (struct st_sensor_fullscale_avl *)
948
- &adata->sensor_settings->fs.fs_avl[0];
1265
+ channels_size = indio_dev->num_channels * sizeof(struct iio_chan_spec);
1266
+ channels = devm_kmemdup(&indio_dev->dev,
1267
+ adata->sensor_settings->ch,
1268
+ channels_size, GFP_KERNEL);
1269
+ if (!channels)
1270
+ return -ENOMEM;
1271
+
1272
+ if (apply_acpi_orientation(indio_dev, channels))
1273
+ dev_warn(&indio_dev->dev,
1274
+ "failed to apply ACPI orientation data: %d\n", err);
1275
+
1276
+ indio_dev->channels = channels;
1277
+ adata->current_fullscale = &adata->sensor_settings->fs.fs_avl[0];
9491278 adata->odr = adata->sensor_settings->odr.odr_avl[0].hz;
9501279
9511280 if (!pdata)
....@@ -953,13 +1282,13 @@
9531282
9541283 err = st_sensors_init_sensor(indio_dev, pdata);
9551284 if (err < 0)
956
- goto st_accel_power_off;
1285
+ return err;
9571286
9581287 err = st_accel_allocate_ring(indio_dev);
9591288 if (err < 0)
960
- goto st_accel_power_off;
1289
+ return err;
9611290
962
- if (irq > 0) {
1291
+ if (adata->irq > 0) {
9631292 err = st_sensors_allocate_trigger(indio_dev,
9641293 ST_ACCEL_TRIGGER_OPS);
9651294 if (err < 0)
....@@ -976,13 +1305,10 @@
9761305 return 0;
9771306
9781307 st_accel_device_register_error:
979
- if (irq > 0)
1308
+ if (adata->irq > 0)
9801309 st_sensors_deallocate_trigger(indio_dev);
9811310 st_accel_probe_trigger_error:
9821311 st_accel_deallocate_ring(indio_dev);
983
-st_accel_power_off:
984
- st_sensors_power_disable(indio_dev);
985
-
9861312 return err;
9871313 }
9881314 EXPORT_SYMBOL(st_accel_common_probe);
....@@ -991,10 +1317,8 @@
9911317 {
9921318 struct st_sensor_data *adata = iio_priv(indio_dev);
9931319
994
- st_sensors_power_disable(indio_dev);
995
-
9961320 iio_device_unregister(indio_dev);
997
- if (adata->get_irq_data_ready(indio_dev) > 0)
1321
+ if (adata->irq > 0)
9981322 st_sensors_deallocate_trigger(indio_dev);
9991323
10001324 st_accel_deallocate_ring(indio_dev);