xref: /linux/drivers/iio/accel/st_accel_core.c (revision 1a2ac6d7ecdcde74a4e16f31de64124160fc7237)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics accelerometers driver
4  *
5  * Copyright 2012-2013 STMicroelectronics Inc.
6  *
7  * Denis Ciocca <denis.ciocca@st.com>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/sysfs.h>
14 #include <linux/slab.h>
15 #include <linux/acpi.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
18 #include <linux/iio/trigger.h>
19 
20 #include <linux/iio/common/st_sensors.h>
21 #include "st_accel.h"
22 
23 #define ST_ACCEL_NUMBER_DATA_CHANNELS		3
24 
25 /* DEFAULT VALUE FOR SENSORS */
26 #define ST_ACCEL_DEFAULT_OUT_X_L_ADDR		0x28
27 #define ST_ACCEL_DEFAULT_OUT_Y_L_ADDR		0x2a
28 #define ST_ACCEL_DEFAULT_OUT_Z_L_ADDR		0x2c
29 
30 /* FULLSCALE */
31 #define ST_ACCEL_FS_AVL_2G			2
32 #define ST_ACCEL_FS_AVL_4G			4
33 #define ST_ACCEL_FS_AVL_6G			6
34 #define ST_ACCEL_FS_AVL_8G			8
35 #define ST_ACCEL_FS_AVL_16G			16
36 #define ST_ACCEL_FS_AVL_100G			100
37 #define ST_ACCEL_FS_AVL_200G			200
38 #define ST_ACCEL_FS_AVL_400G			400
39 
40 static const struct iio_mount_matrix *
41 st_accel_get_mount_matrix(const struct iio_dev *indio_dev,
42 			  const struct iio_chan_spec *chan)
43 {
44 	struct st_sensor_data *adata = iio_priv(indio_dev);
45 
46 	return &adata->mount_matrix;
47 }
48 
49 static const struct iio_chan_spec_ext_info st_accel_mount_matrix_ext_info[] = {
50 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, st_accel_get_mount_matrix),
51 	{ }
52 };
53 
54 static const struct iio_chan_spec st_accel_8bit_channels[] = {
55 	ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
56 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
57 			ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 8, 8,
58 			ST_ACCEL_DEFAULT_OUT_X_L_ADDR+1,
59 			st_accel_mount_matrix_ext_info),
60 	ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
61 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
62 			ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 8, 8,
63 			ST_ACCEL_DEFAULT_OUT_Y_L_ADDR+1,
64 			st_accel_mount_matrix_ext_info),
65 	ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
66 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
67 			ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 8, 8,
68 			ST_ACCEL_DEFAULT_OUT_Z_L_ADDR+1,
69 			st_accel_mount_matrix_ext_info),
70 	IIO_CHAN_SOFT_TIMESTAMP(3)
71 };
72 
73 static const struct iio_chan_spec st_accel_12bit_channels[] = {
74 	ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
75 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
76 			ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 12, 16,
77 			ST_ACCEL_DEFAULT_OUT_X_L_ADDR,
78 			st_accel_mount_matrix_ext_info),
79 	ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
80 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
81 			ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 12, 16,
82 			ST_ACCEL_DEFAULT_OUT_Y_L_ADDR,
83 			st_accel_mount_matrix_ext_info),
84 	ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
85 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
86 			ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 12, 16,
87 			ST_ACCEL_DEFAULT_OUT_Z_L_ADDR,
88 			st_accel_mount_matrix_ext_info),
89 	IIO_CHAN_SOFT_TIMESTAMP(3)
90 };
91 
92 static const struct iio_chan_spec st_accel_16bit_channels[] = {
93 	ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
94 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
95 			ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
96 			ST_ACCEL_DEFAULT_OUT_X_L_ADDR,
97 			st_accel_mount_matrix_ext_info),
98 	ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
99 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
100 			ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
101 			ST_ACCEL_DEFAULT_OUT_Y_L_ADDR,
102 			st_accel_mount_matrix_ext_info),
103 	ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
104 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
105 			ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
106 			ST_ACCEL_DEFAULT_OUT_Z_L_ADDR,
107 			st_accel_mount_matrix_ext_info),
108 	IIO_CHAN_SOFT_TIMESTAMP(3)
109 };
110 
111 static const struct st_sensor_settings st_accel_sensors_settings[] = {
112 	{
113 		.wai = 0x33,
114 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
115 		.sensors_supported = {
116 			[0] = LIS3DH_ACCEL_DEV_NAME,
117 			[1] = LSM303DLHC_ACCEL_DEV_NAME,
118 			[2] = LSM330D_ACCEL_DEV_NAME,
119 			[3] = LSM330DL_ACCEL_DEV_NAME,
120 			[4] = LSM330DLC_ACCEL_DEV_NAME,
121 			[5] = LSM303AGR_ACCEL_DEV_NAME,
122 			[6] = LIS2DH12_ACCEL_DEV_NAME,
123 			[7] = LIS3DE_ACCEL_DEV_NAME,
124 		},
125 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
126 		.odr = {
127 			.addr = 0x20,
128 			.mask = 0xf0,
129 			.odr_avl = {
130 				{ .hz = 1, .value = 0x01, },
131 				{ .hz = 10, .value = 0x02, },
132 				{ .hz = 25, .value = 0x03, },
133 				{ .hz = 50, .value = 0x04, },
134 				{ .hz = 100, .value = 0x05, },
135 				{ .hz = 200, .value = 0x06, },
136 				{ .hz = 400, .value = 0x07, },
137 				{ .hz = 1600, .value = 0x08, },
138 			},
139 		},
140 		.pw = {
141 			.addr = 0x20,
142 			.mask = 0xf0,
143 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
144 		},
145 		.enable_axis = {
146 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
147 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
148 		},
149 		.fs = {
150 			.addr = 0x23,
151 			.mask = 0x30,
152 			.fs_avl = {
153 				[0] = {
154 					.num = ST_ACCEL_FS_AVL_2G,
155 					.value = 0x00,
156 					.gain = IIO_G_TO_M_S_2(1000),
157 				},
158 				[1] = {
159 					.num = ST_ACCEL_FS_AVL_4G,
160 					.value = 0x01,
161 					.gain = IIO_G_TO_M_S_2(2000),
162 				},
163 				[2] = {
164 					.num = ST_ACCEL_FS_AVL_8G,
165 					.value = 0x02,
166 					.gain = IIO_G_TO_M_S_2(4000),
167 				},
168 				[3] = {
169 					.num = ST_ACCEL_FS_AVL_16G,
170 					.value = 0x03,
171 					.gain = IIO_G_TO_M_S_2(12000),
172 				},
173 			},
174 		},
175 		.bdu = {
176 			.addr = 0x23,
177 			.mask = 0x80,
178 		},
179 		.drdy_irq = {
180 			.int1 = {
181 				.addr = 0x22,
182 				.mask = 0x10,
183 			},
184 			.addr_ihl = 0x25,
185 			.mask_ihl = 0x02,
186 			.stat_drdy = {
187 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
188 				.mask = 0x07,
189 			},
190 		},
191 		.sim = {
192 			.addr = 0x23,
193 			.value = BIT(0),
194 		},
195 		.multi_read_bit = true,
196 		.bootime = 2,
197 	},
198 	{
199 		.wai = 0x32,
200 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
201 		.sensors_supported = {
202 			[0] = LIS331DLH_ACCEL_DEV_NAME,
203 			[1] = LSM303DL_ACCEL_DEV_NAME,
204 			[2] = LSM303DLH_ACCEL_DEV_NAME,
205 			[3] = LSM303DLM_ACCEL_DEV_NAME,
206 		},
207 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
208 		.odr = {
209 			.addr = 0x20,
210 			.mask = 0x18,
211 			.odr_avl = {
212 				{ .hz = 50, .value = 0x00, },
213 				{ .hz = 100, .value = 0x01, },
214 				{ .hz = 400, .value = 0x02, },
215 				{ .hz = 1000, .value = 0x03, },
216 			},
217 		},
218 		.pw = {
219 			.addr = 0x20,
220 			.mask = 0xe0,
221 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
222 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
223 		},
224 		.enable_axis = {
225 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
226 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
227 		},
228 		.fs = {
229 			.addr = 0x23,
230 			.mask = 0x30,
231 			.fs_avl = {
232 				[0] = {
233 					.num = ST_ACCEL_FS_AVL_2G,
234 					.value = 0x00,
235 					.gain = IIO_G_TO_M_S_2(1000),
236 				},
237 				[1] = {
238 					.num = ST_ACCEL_FS_AVL_4G,
239 					.value = 0x01,
240 					.gain = IIO_G_TO_M_S_2(2000),
241 				},
242 				[2] = {
243 					.num = ST_ACCEL_FS_AVL_8G,
244 					.value = 0x03,
245 					.gain = IIO_G_TO_M_S_2(3900),
246 				},
247 			},
248 		},
249 		.bdu = {
250 			.addr = 0x23,
251 			.mask = 0x80,
252 		},
253 		.drdy_irq = {
254 			.int1 = {
255 				.addr = 0x22,
256 				.mask = 0x02,
257 				.addr_od = 0x22,
258 				.mask_od = 0x40,
259 			},
260 			.int2 = {
261 				.addr = 0x22,
262 				.mask = 0x10,
263 				.addr_od = 0x22,
264 				.mask_od = 0x40,
265 			},
266 			.addr_ihl = 0x22,
267 			.mask_ihl = 0x80,
268 			.stat_drdy = {
269 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
270 				.mask = 0x07,
271 			},
272 		},
273 		.sim = {
274 			.addr = 0x23,
275 			.value = BIT(0),
276 		},
277 		.multi_read_bit = true,
278 		.bootime = 2,
279 	},
280 	{
281 		.wai = 0x40,
282 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
283 		.sensors_supported = {
284 			[0] = LSM330_ACCEL_DEV_NAME,
285 		},
286 		.ch = (struct iio_chan_spec *)st_accel_16bit_channels,
287 		.odr = {
288 			.addr = 0x20,
289 			.mask = 0xf0,
290 			.odr_avl = {
291 				{ .hz = 3, .value = 0x01, },
292 				{ .hz = 6, .value = 0x02, },
293 				{ .hz = 12, .value = 0x03, },
294 				{ .hz = 25, .value = 0x04, },
295 				{ .hz = 50, .value = 0x05, },
296 				{ .hz = 100, .value = 0x06, },
297 				{ .hz = 200, .value = 0x07, },
298 				{ .hz = 400, .value = 0x08, },
299 				{ .hz = 800, .value = 0x09, },
300 				{ .hz = 1600, .value = 0x0a, },
301 			},
302 		},
303 		.pw = {
304 			.addr = 0x20,
305 			.mask = 0xf0,
306 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
307 		},
308 		.enable_axis = {
309 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
310 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
311 		},
312 		.fs = {
313 			.addr = 0x24,
314 			.mask = 0x38,
315 			.fs_avl = {
316 				[0] = {
317 					.num = ST_ACCEL_FS_AVL_2G,
318 					.value = 0x00,
319 					.gain = IIO_G_TO_M_S_2(61),
320 				},
321 				[1] = {
322 					.num = ST_ACCEL_FS_AVL_4G,
323 					.value = 0x01,
324 					.gain = IIO_G_TO_M_S_2(122),
325 				},
326 				[2] = {
327 					.num = ST_ACCEL_FS_AVL_6G,
328 					.value = 0x02,
329 					.gain = IIO_G_TO_M_S_2(183),
330 				},
331 				[3] = {
332 					.num = ST_ACCEL_FS_AVL_8G,
333 					.value = 0x03,
334 					.gain = IIO_G_TO_M_S_2(244),
335 				},
336 				[4] = {
337 					.num = ST_ACCEL_FS_AVL_16G,
338 					.value = 0x04,
339 					.gain = IIO_G_TO_M_S_2(732),
340 				},
341 			},
342 		},
343 		.bdu = {
344 			.addr = 0x20,
345 			.mask = 0x08,
346 		},
347 		.drdy_irq = {
348 			.int1 = {
349 				.addr = 0x23,
350 				.mask = 0x80,
351 			},
352 			.addr_ihl = 0x23,
353 			.mask_ihl = 0x40,
354 			.stat_drdy = {
355 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
356 				.mask = 0x07,
357 			},
358 			.ig1 = {
359 				.en_addr = 0x23,
360 				.en_mask = 0x08,
361 			},
362 		},
363 		.sim = {
364 			.addr = 0x24,
365 			.value = BIT(0),
366 		},
367 		.multi_read_bit = false,
368 		.bootime = 2,
369 	},
370 	{
371 		.wai = 0x3a,
372 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
373 		.sensors_supported = {
374 			[0] = LIS3LV02DL_ACCEL_DEV_NAME,
375 		},
376 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
377 		.odr = {
378 			.addr = 0x20,
379 			.mask = 0x30, /* DF1 and DF0 */
380 			.odr_avl = {
381 				{ .hz = 40, .value = 0x00, },
382 				{ .hz = 160, .value = 0x01, },
383 				{ .hz = 640, .value = 0x02, },
384 				{ .hz = 2560, .value = 0x03, },
385 			},
386 		},
387 		.pw = {
388 			.addr = 0x20,
389 			.mask = 0xc0,
390 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
391 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
392 		},
393 		.enable_axis = {
394 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
395 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
396 		},
397 		.fs = {
398 			.addr = 0x21,
399 			.mask = 0x80,
400 			.fs_avl = {
401 				[0] = {
402 					.num = ST_ACCEL_FS_AVL_2G,
403 					.value = 0x00,
404 					.gain = IIO_G_TO_M_S_2(1000),
405 				},
406 				[1] = {
407 					.num = ST_ACCEL_FS_AVL_6G,
408 					.value = 0x01,
409 					.gain = IIO_G_TO_M_S_2(3000),
410 				},
411 			},
412 		},
413 		.bdu = {
414 			.addr = 0x21,
415 			.mask = 0x40,
416 		},
417 		/*
418 		 * Data Alignment Setting - needs to be set to get
419 		 * left-justified data like all other sensors.
420 		 */
421 		.das = {
422 			.addr = 0x21,
423 			.mask = 0x01,
424 		},
425 		.drdy_irq = {
426 			.int1 = {
427 				.addr = 0x21,
428 				.mask = 0x04,
429 			},
430 			.stat_drdy = {
431 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
432 				.mask = 0x07,
433 			},
434 		},
435 		.sim = {
436 			.addr = 0x21,
437 			.value = BIT(1),
438 		},
439 		.multi_read_bit = true,
440 		.bootime = 2, /* guess */
441 	},
442 	{
443 		.wai = 0x3b,
444 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
445 		.sensors_supported = {
446 			[0] = LIS331DL_ACCEL_DEV_NAME,
447 			[1] = LIS302DL_ACCEL_DEV_NAME,
448 		},
449 		.ch = (struct iio_chan_spec *)st_accel_8bit_channels,
450 		.odr = {
451 			.addr = 0x20,
452 			.mask = 0x80,
453 			.odr_avl = {
454 				{ .hz = 100, .value = 0x00, },
455 				{ .hz = 400, .value = 0x01, },
456 			},
457 		},
458 		.pw = {
459 			.addr = 0x20,
460 			.mask = 0x40,
461 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
462 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
463 		},
464 		.enable_axis = {
465 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
466 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
467 		},
468 		.fs = {
469 			.addr = 0x20,
470 			.mask = 0x20,
471 			/*
472 			 * TODO: check these resulting gain settings, these are
473 			 * not in the datsheet
474 			 */
475 			.fs_avl = {
476 				[0] = {
477 					.num = ST_ACCEL_FS_AVL_2G,
478 					.value = 0x00,
479 					.gain = IIO_G_TO_M_S_2(18000),
480 				},
481 				[1] = {
482 					.num = ST_ACCEL_FS_AVL_8G,
483 					.value = 0x01,
484 					.gain = IIO_G_TO_M_S_2(72000),
485 				},
486 			},
487 		},
488 		.drdy_irq = {
489 			.int1 = {
490 				.addr = 0x22,
491 				.mask = 0x04,
492 				.addr_od = 0x22,
493 				.mask_od = 0x40,
494 			},
495 			.int2 = {
496 				.addr = 0x22,
497 				.mask = 0x20,
498 				.addr_od = 0x22,
499 				.mask_od = 0x40,
500 			},
501 			.addr_ihl = 0x22,
502 			.mask_ihl = 0x80,
503 			.stat_drdy = {
504 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
505 				.mask = 0x07,
506 			},
507 		},
508 		.sim = {
509 			.addr = 0x21,
510 			.value = BIT(7),
511 		},
512 		.multi_read_bit = false,
513 		.bootime = 2, /* guess */
514 	},
515 	{
516 		.wai = 0x32,
517 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
518 		.sensors_supported = {
519 			[0] = H3LIS331DL_ACCEL_DEV_NAME,
520 		},
521 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
522 		.odr = {
523 			.addr = 0x20,
524 			.mask = 0x18,
525 			.odr_avl = {
526 				{ .hz = 50, .value = 0x00, },
527 				{ .hz = 100, .value = 0x01, },
528 				{ .hz = 400, .value = 0x02, },
529 				{ .hz = 1000, .value = 0x03, },
530 			},
531 		},
532 		.pw = {
533 			.addr = 0x20,
534 			.mask = 0x20,
535 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
536 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
537 		},
538 		.enable_axis = {
539 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
540 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
541 		},
542 		.fs = {
543 			.addr = 0x23,
544 			.mask = 0x30,
545 			.fs_avl = {
546 				[0] = {
547 					.num = ST_ACCEL_FS_AVL_100G,
548 					.value = 0x00,
549 					.gain = IIO_G_TO_M_S_2(49000),
550 				},
551 				[1] = {
552 					.num = ST_ACCEL_FS_AVL_200G,
553 					.value = 0x01,
554 					.gain = IIO_G_TO_M_S_2(98000),
555 				},
556 				[2] = {
557 					.num = ST_ACCEL_FS_AVL_400G,
558 					.value = 0x03,
559 					.gain = IIO_G_TO_M_S_2(195000),
560 				},
561 			},
562 		},
563 		.bdu = {
564 			.addr = 0x23,
565 			.mask = 0x80,
566 		},
567 		.drdy_irq = {
568 			.int1 = {
569 				.addr = 0x22,
570 				.mask = 0x02,
571 			},
572 			.int2 = {
573 				.addr = 0x22,
574 				.mask = 0x10,
575 			},
576 			.addr_ihl = 0x22,
577 			.mask_ihl = 0x80,
578 		},
579 		.sim = {
580 			.addr = 0x23,
581 			.value = BIT(0),
582 		},
583 		.multi_read_bit = true,
584 		.bootime = 2,
585 	},
586 	{
587 		/* No WAI register present */
588 		.sensors_supported = {
589 			[0] = LIS3L02DQ_ACCEL_DEV_NAME,
590 		},
591 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
592 		.odr = {
593 			.addr = 0x20,
594 			.mask = 0x30,
595 			.odr_avl = {
596 				{ .hz = 280, .value = 0x00, },
597 				{ .hz = 560, .value = 0x01, },
598 				{ .hz = 1120, .value = 0x02, },
599 				{ .hz = 4480, .value = 0x03, },
600 			},
601 		},
602 		.pw = {
603 			.addr = 0x20,
604 			.mask = 0xc0,
605 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
606 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
607 		},
608 		.enable_axis = {
609 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
610 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
611 		},
612 		.fs = {
613 			.fs_avl = {
614 				[0] = {
615 					.num = ST_ACCEL_FS_AVL_2G,
616 					.gain = IIO_G_TO_M_S_2(488),
617 				},
618 			},
619 		},
620 		/*
621 		 * The part has a BDU bit but if set the data is never
622 		 * updated so don't set it.
623 		 */
624 		.bdu = {
625 		},
626 		.drdy_irq = {
627 			.int1 = {
628 				.addr = 0x21,
629 				.mask = 0x04,
630 			},
631 			.stat_drdy = {
632 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
633 				.mask = 0x07,
634 			},
635 		},
636 		.sim = {
637 			.addr = 0x21,
638 			.value = BIT(1),
639 		},
640 		.multi_read_bit = false,
641 		.bootime = 2,
642 	},
643 	{
644 		.wai = 0x33,
645 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
646 		.sensors_supported = {
647 			[0] = LNG2DM_ACCEL_DEV_NAME,
648 		},
649 		.ch = (struct iio_chan_spec *)st_accel_8bit_channels,
650 		.odr = {
651 			.addr = 0x20,
652 			.mask = 0xf0,
653 			.odr_avl = {
654 				{ .hz = 1, .value = 0x01, },
655 				{ .hz = 10, .value = 0x02, },
656 				{ .hz = 25, .value = 0x03, },
657 				{ .hz = 50, .value = 0x04, },
658 				{ .hz = 100, .value = 0x05, },
659 				{ .hz = 200, .value = 0x06, },
660 				{ .hz = 400, .value = 0x07, },
661 				{ .hz = 1600, .value = 0x08, },
662 			},
663 		},
664 		.pw = {
665 			.addr = 0x20,
666 			.mask = 0xf0,
667 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
668 		},
669 		.enable_axis = {
670 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
671 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
672 		},
673 		.fs = {
674 			.addr = 0x23,
675 			.mask = 0x30,
676 			.fs_avl = {
677 				[0] = {
678 					.num = ST_ACCEL_FS_AVL_2G,
679 					.value = 0x00,
680 					.gain = IIO_G_TO_M_S_2(15600),
681 				},
682 				[1] = {
683 					.num = ST_ACCEL_FS_AVL_4G,
684 					.value = 0x01,
685 					.gain = IIO_G_TO_M_S_2(31200),
686 				},
687 				[2] = {
688 					.num = ST_ACCEL_FS_AVL_8G,
689 					.value = 0x02,
690 					.gain = IIO_G_TO_M_S_2(62500),
691 				},
692 				[3] = {
693 					.num = ST_ACCEL_FS_AVL_16G,
694 					.value = 0x03,
695 					.gain = IIO_G_TO_M_S_2(187500),
696 				},
697 			},
698 		},
699 		.drdy_irq = {
700 			.int1 = {
701 				.addr = 0x22,
702 				.mask = 0x10,
703 			},
704 			.addr_ihl = 0x25,
705 			.mask_ihl = 0x02,
706 			.stat_drdy = {
707 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
708 				.mask = 0x07,
709 			},
710 		},
711 		.sim = {
712 			.addr = 0x23,
713 			.value = BIT(0),
714 		},
715 		.multi_read_bit = true,
716 		.bootime = 2,
717 	},
718 	{
719 		.wai = 0x44,
720 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
721 		.sensors_supported = {
722 			[0] = LIS2DW12_ACCEL_DEV_NAME,
723 		},
724 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
725 		.odr = {
726 			.addr = 0x20,
727 			.mask = 0xf0,
728 			.odr_avl = {
729 				{ .hz = 1, .value = 0x01, },
730 				{ .hz = 12, .value = 0x02, },
731 				{ .hz = 25, .value = 0x03, },
732 				{ .hz = 50, .value = 0x04, },
733 				{ .hz = 100, .value = 0x05, },
734 				{ .hz = 200, .value = 0x06, },
735 			},
736 		},
737 		.pw = {
738 			.addr = 0x20,
739 			.mask = 0xf0,
740 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
741 		},
742 		.fs = {
743 			.addr = 0x25,
744 			.mask = 0x30,
745 			.fs_avl = {
746 				[0] = {
747 					.num = ST_ACCEL_FS_AVL_2G,
748 					.value = 0x00,
749 					.gain = IIO_G_TO_M_S_2(976),
750 				},
751 				[1] = {
752 					.num = ST_ACCEL_FS_AVL_4G,
753 					.value = 0x01,
754 					.gain = IIO_G_TO_M_S_2(1952),
755 				},
756 				[2] = {
757 					.num = ST_ACCEL_FS_AVL_8G,
758 					.value = 0x02,
759 					.gain = IIO_G_TO_M_S_2(3904),
760 				},
761 				[3] = {
762 					.num = ST_ACCEL_FS_AVL_16G,
763 					.value = 0x03,
764 					.gain = IIO_G_TO_M_S_2(7808),
765 				},
766 			},
767 		},
768 		.bdu = {
769 			.addr = 0x21,
770 			.mask = 0x08,
771 		},
772 		.drdy_irq = {
773 			.int1 = {
774 				.addr = 0x23,
775 				.mask = 0x01,
776 				.addr_od = 0x22,
777 				.mask_od = 0x20,
778 			},
779 			.int2 = {
780 				.addr = 0x24,
781 				.mask = 0x01,
782 				.addr_od = 0x22,
783 				.mask_od = 0x20,
784 			},
785 			.addr_ihl = 0x22,
786 			.mask_ihl = 0x08,
787 			.stat_drdy = {
788 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
789 				.mask = 0x01,
790 			},
791 		},
792 		.sim = {
793 			.addr = 0x21,
794 			.value = BIT(0),
795 		},
796 		.multi_read_bit = false,
797 		.bootime = 2,
798 	},
799 	{
800 		.wai = 0x11,
801 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
802 		.sensors_supported = {
803 			[0] = LIS3DHH_ACCEL_DEV_NAME,
804 		},
805 		.ch = (struct iio_chan_spec *)st_accel_16bit_channels,
806 		.odr = {
807 			/* just ODR = 1100Hz available */
808 			.odr_avl = {
809 				{ .hz = 1100, .value = 0x00, },
810 			},
811 		},
812 		.pw = {
813 			.addr = 0x20,
814 			.mask = 0x80,
815 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
816 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
817 		},
818 		.fs = {
819 			.fs_avl = {
820 				[0] = {
821 					.num = ST_ACCEL_FS_AVL_2G,
822 					.gain = IIO_G_TO_M_S_2(76),
823 				},
824 			},
825 		},
826 		.bdu = {
827 			.addr = 0x20,
828 			.mask = 0x01,
829 		},
830 		.drdy_irq = {
831 			.int1 = {
832 				.addr = 0x21,
833 				.mask = 0x80,
834 				.addr_od = 0x23,
835 				.mask_od = 0x04,
836 			},
837 			.int2 = {
838 				.addr = 0x22,
839 				.mask = 0x80,
840 				.addr_od = 0x23,
841 				.mask_od = 0x08,
842 			},
843 			.stat_drdy = {
844 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
845 				.mask = 0x07,
846 			},
847 		},
848 		.multi_read_bit = false,
849 		.bootime = 2,
850 	},
851 	{
852 		.wai = 0x33,
853 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
854 		.sensors_supported = {
855 			[0] = LIS2DE12_ACCEL_DEV_NAME,
856 		},
857 		.ch = (struct iio_chan_spec *)st_accel_8bit_channels,
858 		.odr = {
859 			.addr = 0x20,
860 			.mask = 0xf0,
861 			.odr_avl = {
862 				{ .hz = 1, .value = 0x01, },
863 				{ .hz = 10, .value = 0x02, },
864 				{ .hz = 25, .value = 0x03, },
865 				{ .hz = 50, .value = 0x04, },
866 				{ .hz = 100, .value = 0x05, },
867 				{ .hz = 200, .value = 0x06, },
868 				{ .hz = 400, .value = 0x07, },
869 				{ .hz = 1620, .value = 0x08, },
870 				{ .hz = 5376, .value = 0x09, },
871 			},
872 		},
873 		.pw = {
874 			.addr = 0x20,
875 			.mask = 0xf0,
876 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
877 		},
878 		.enable_axis = {
879 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
880 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
881 		},
882 		.fs = {
883 			.addr = 0x23,
884 			.mask = 0x30,
885 			.fs_avl = {
886 				[0] = {
887 					.num = ST_ACCEL_FS_AVL_2G,
888 					.value = 0x00,
889 					.gain = IIO_G_TO_M_S_2(15600),
890 				},
891 				[1] = {
892 					.num = ST_ACCEL_FS_AVL_4G,
893 					.value = 0x01,
894 					.gain = IIO_G_TO_M_S_2(31200),
895 				},
896 				[2] = {
897 					.num = ST_ACCEL_FS_AVL_8G,
898 					.value = 0x02,
899 					.gain = IIO_G_TO_M_S_2(62500),
900 				},
901 				[3] = {
902 					.num = ST_ACCEL_FS_AVL_16G,
903 					.value = 0x03,
904 					.gain = IIO_G_TO_M_S_2(187500),
905 				},
906 			},
907 		},
908 		.drdy_irq = {
909 			.int1 = {
910 				.addr = 0x22,
911 				.mask = 0x10,
912 			},
913 			.addr_ihl = 0x25,
914 			.mask_ihl = 0x02,
915 			.stat_drdy = {
916 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
917 				.mask = 0x07,
918 			},
919 		},
920 		.sim = {
921 			.addr = 0x23,
922 			.value = BIT(0),
923 		},
924 		.multi_read_bit = true,
925 		.bootime = 2,
926 	},
927 	{
928 		.wai = 0x41,
929 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
930 		.sensors_supported = {
931 			[0] = LIS2HH12_ACCEL_DEV_NAME,
932 			[1] = LSM303C_ACCEL_DEV_NAME,
933 		},
934 		.ch = (struct iio_chan_spec *)st_accel_16bit_channels,
935 		.odr = {
936 			.addr = 0x20,
937 			.mask = 0x70,
938 			.odr_avl = {
939 				{ .hz = 10, .value = 0x01, },
940 				{ .hz = 50, .value = 0x02, },
941 				{ .hz = 100, .value = 0x03, },
942 				{ .hz = 200, .value = 0x04, },
943 				{ .hz = 400, .value = 0x05, },
944 				{ .hz = 800, .value = 0x06, },
945 			},
946 		},
947 		.pw = {
948 			.addr = 0x20,
949 			.mask = 0x70,
950 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
951 		},
952 		.enable_axis = {
953 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
954 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
955 		},
956 		.fs = {
957 			.addr = 0x23,
958 			.mask = 0x30,
959 			.fs_avl = {
960 				[0] = {
961 					.num = ST_ACCEL_FS_AVL_2G,
962 					.value = 0x00,
963 					.gain = IIO_G_TO_M_S_2(61),
964 				},
965 				[1] = {
966 					.num = ST_ACCEL_FS_AVL_4G,
967 					.value = 0x02,
968 					.gain = IIO_G_TO_M_S_2(122),
969 				},
970 				[2] = {
971 					.num = ST_ACCEL_FS_AVL_8G,
972 					.value = 0x03,
973 					.gain = IIO_G_TO_M_S_2(244),
974 				},
975 			},
976 		},
977 		.bdu = {
978 			.addr = 0x20,
979 			.mask = 0x08,
980 		},
981 		.drdy_irq = {
982 			.int1 = {
983 				.addr = 0x22,
984 				.mask = 0x01,
985 			},
986 			.int2 = {
987 				.addr = 0x25,
988 				.mask = 0x01,
989 			},
990 			.addr_ihl = 0x24,
991 			.mask_ihl = 0x02,
992 			.stat_drdy = {
993 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
994 				.mask = 0x07,
995 			},
996 		},
997 		.sim = {
998 			.addr = 0x23,
999 			.value = BIT(0),
1000 		},
1001 		.multi_read_bit = true,
1002 		.bootime = 2,
1003 	},
1004 	{
1005 		.wai = 0x49,
1006 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
1007 		.sensors_supported = {
1008 			[0] = LSM9DS0_IMU_DEV_NAME,
1009 		},
1010 		.ch = (struct iio_chan_spec *)st_accel_16bit_channels,
1011 		.odr = {
1012 			.addr = 0x20,
1013 			.mask = GENMASK(7, 4),
1014 			.odr_avl = {
1015 				{ 3, 0x01, },
1016 				{ 6, 0x02, },
1017 				{ 12, 0x03, },
1018 				{ 25, 0x04, },
1019 				{ 50, 0x05, },
1020 				{ 100, 0x06, },
1021 				{ 200, 0x07, },
1022 				{ 400, 0x08, },
1023 				{ 800, 0x09, },
1024 				{ 1600, 0x0a, },
1025 			},
1026 		},
1027 		.pw = {
1028 			.addr = 0x20,
1029 			.mask = GENMASK(7, 4),
1030 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
1031 		},
1032 		.enable_axis = {
1033 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
1034 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
1035 		},
1036 		.fs = {
1037 			.addr = 0x21,
1038 			.mask = GENMASK(5, 3),
1039 			.fs_avl = {
1040 				[0] = {
1041 					.num = ST_ACCEL_FS_AVL_2G,
1042 					.value = 0x00,
1043 					.gain = IIO_G_TO_M_S_2(61),
1044 				},
1045 				[1] = {
1046 					.num = ST_ACCEL_FS_AVL_4G,
1047 					.value = 0x01,
1048 					.gain = IIO_G_TO_M_S_2(122),
1049 				},
1050 				[2] = {
1051 					.num = ST_ACCEL_FS_AVL_6G,
1052 					.value = 0x02,
1053 					.gain = IIO_G_TO_M_S_2(183),
1054 				},
1055 				[3] = {
1056 					.num = ST_ACCEL_FS_AVL_8G,
1057 					.value = 0x03,
1058 					.gain = IIO_G_TO_M_S_2(244),
1059 				},
1060 				[4] = {
1061 					.num = ST_ACCEL_FS_AVL_16G,
1062 					.value = 0x04,
1063 					.gain = IIO_G_TO_M_S_2(732),
1064 				},
1065 			},
1066 		},
1067 		.bdu = {
1068 			.addr = 0x20,
1069 			.mask = BIT(3),
1070 		},
1071 		.drdy_irq = {
1072 			.int1 = {
1073 				.addr = 0x22,
1074 				.mask = BIT(2),
1075 			},
1076 			.int2 = {
1077 				.addr = 0x23,
1078 				.mask = BIT(3),
1079 			},
1080 			.stat_drdy = {
1081 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
1082 				.mask = GENMASK(2, 0),
1083 			},
1084 		},
1085 		.sim = {
1086 			.addr = 0x21,
1087 			.value = BIT(0),
1088 		},
1089 		.multi_read_bit = true,
1090 		.bootime = 2,
1091 	},
1092 	{
1093 		/*
1094 		 * Not an ST part. Register-compatible with the LIS2DH, even
1095 		 * though the WAI value is different.
1096 		 */
1097 		.wai = 0x11,
1098 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
1099 		.sensors_supported = {
1100 			[0] = SC7A20_ACCEL_DEV_NAME,
1101 		},
1102 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
1103 		.odr = {
1104 			.addr = 0x20,
1105 			.mask = 0xf0,
1106 			.odr_avl = {
1107 				{ .hz = 1, .value = 0x01, },
1108 				{ .hz = 10, .value = 0x02, },
1109 				{ .hz = 25, .value = 0x03, },
1110 				{ .hz = 50, .value = 0x04, },
1111 				{ .hz = 100, .value = 0x05, },
1112 				{ .hz = 200, .value = 0x06, },
1113 				{ .hz = 400, .value = 0x07, },
1114 				{ .hz = 1600, .value = 0x08, },
1115 			},
1116 		},
1117 		.pw = {
1118 			.addr = 0x20,
1119 			.mask = 0xf0,
1120 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
1121 		},
1122 		.enable_axis = {
1123 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
1124 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
1125 		},
1126 		.fs = {
1127 			.addr = 0x23,
1128 			.mask = 0x30,
1129 			.fs_avl = {
1130 				[0] = {
1131 					.num = ST_ACCEL_FS_AVL_2G,
1132 					.value = 0x00,
1133 					.gain = IIO_G_TO_M_S_2(1000),
1134 				},
1135 				[1] = {
1136 					.num = ST_ACCEL_FS_AVL_4G,
1137 					.value = 0x01,
1138 					.gain = IIO_G_TO_M_S_2(2000),
1139 				},
1140 				[2] = {
1141 					.num = ST_ACCEL_FS_AVL_8G,
1142 					.value = 0x02,
1143 					.gain = IIO_G_TO_M_S_2(4000),
1144 				},
1145 				[3] = {
1146 					.num = ST_ACCEL_FS_AVL_16G,
1147 					.value = 0x03,
1148 					.gain = IIO_G_TO_M_S_2(12000),
1149 				},
1150 			},
1151 		},
1152 		.bdu = {
1153 			.addr = 0x23,
1154 			.mask = 0x80,
1155 		},
1156 		.drdy_irq = {
1157 			.int1 = {
1158 				.addr = 0x22,
1159 				.mask = 0x10,
1160 			},
1161 			.addr_ihl = 0x25,
1162 			.mask_ihl = 0x02,
1163 			.stat_drdy = {
1164 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
1165 				.mask = 0x07,
1166 			},
1167 		},
1168 		.sim = {
1169 			.addr = 0x23,
1170 			.value = BIT(0),
1171 		},
1172 		.multi_read_bit = true,
1173 		.bootime = 2,
1174 	},
1175 };
1176 
1177 /* Default accel DRDY is available on INT1 pin */
1178 static const struct st_sensors_platform_data default_accel_pdata = {
1179 	.drdy_int_pin = 1,
1180 };
1181 
1182 static int st_accel_read_raw(struct iio_dev *indio_dev,
1183 			struct iio_chan_spec const *ch, int *val,
1184 							int *val2, long mask)
1185 {
1186 	int err;
1187 	struct st_sensor_data *adata = iio_priv(indio_dev);
1188 
1189 	switch (mask) {
1190 	case IIO_CHAN_INFO_RAW:
1191 		err = st_sensors_read_info_raw(indio_dev, ch, val);
1192 		if (err < 0)
1193 			goto read_error;
1194 
1195 		return IIO_VAL_INT;
1196 	case IIO_CHAN_INFO_SCALE:
1197 		*val = adata->current_fullscale->gain / 1000000;
1198 		*val2 = adata->current_fullscale->gain % 1000000;
1199 		return IIO_VAL_INT_PLUS_MICRO;
1200 	case IIO_CHAN_INFO_SAMP_FREQ:
1201 		*val = adata->odr;
1202 		return IIO_VAL_INT;
1203 	default:
1204 		return -EINVAL;
1205 	}
1206 
1207 read_error:
1208 	return err;
1209 }
1210 
1211 static int st_accel_write_raw(struct iio_dev *indio_dev,
1212 		struct iio_chan_spec const *chan, int val, int val2, long mask)
1213 {
1214 	switch (mask) {
1215 	case IIO_CHAN_INFO_SCALE: {
1216 		int gain;
1217 
1218 		gain = val * 1000000 + val2;
1219 		return st_sensors_set_fullscale_by_gain(indio_dev, gain);
1220 	}
1221 	case IIO_CHAN_INFO_SAMP_FREQ:
1222 		if (val2)
1223 			return -EINVAL;
1224 
1225 		return st_sensors_set_odr(indio_dev, val);
1226 	default:
1227 		return -EINVAL;
1228 	}
1229 }
1230 
1231 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
1232 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_accel_scale_available);
1233 
1234 static struct attribute *st_accel_attributes[] = {
1235 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
1236 	&iio_dev_attr_in_accel_scale_available.dev_attr.attr,
1237 	NULL,
1238 };
1239 
1240 static const struct attribute_group st_accel_attribute_group = {
1241 	.attrs = st_accel_attributes,
1242 };
1243 
1244 static const struct iio_info accel_info = {
1245 	.attrs = &st_accel_attribute_group,
1246 	.read_raw = &st_accel_read_raw,
1247 	.write_raw = &st_accel_write_raw,
1248 	.debugfs_reg_access = &st_sensors_debugfs_reg_access,
1249 };
1250 
1251 #ifdef CONFIG_IIO_TRIGGER
1252 static const struct iio_trigger_ops st_accel_trigger_ops = {
1253 	.set_trigger_state = ST_ACCEL_TRIGGER_SET_STATE,
1254 	.validate_device = st_sensors_validate_device,
1255 };
1256 #define ST_ACCEL_TRIGGER_OPS (&st_accel_trigger_ops)
1257 #else
1258 #define ST_ACCEL_TRIGGER_OPS NULL
1259 #endif
1260 
1261 #ifdef CONFIG_ACPI
1262 /* Read ST-specific _ONT orientation data from ACPI and generate an
1263  * appropriate mount matrix.
1264  */
1265 static int apply_acpi_orientation(struct iio_dev *indio_dev)
1266 {
1267 	struct st_sensor_data *adata = iio_priv(indio_dev);
1268 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1269 	struct acpi_device *adev;
1270 	union acpi_object *ont;
1271 	union acpi_object *elements;
1272 	acpi_status status;
1273 	int ret = -EINVAL;
1274 	unsigned int val;
1275 	int i, j;
1276 	int final_ont[3][3] = { { 0 }, };
1277 
1278 	/* For some reason, ST's _ONT translation does not apply directly
1279 	 * to the data read from the sensor. Another translation must be
1280 	 * performed first, as described by the matrix below. Perhaps
1281 	 * ST required this specific translation for the first product
1282 	 * where the device was mounted?
1283 	 */
1284 	const int default_ont[3][3] = {
1285 		{  0,  1,  0 },
1286 		{ -1,  0,  0 },
1287 		{  0,  0, -1 },
1288 	};
1289 
1290 
1291 	adev = ACPI_COMPANION(indio_dev->dev.parent);
1292 	if (!adev)
1293 		return 0;
1294 
1295 	/* Read _ONT data, which should be a package of 6 integers. */
1296 	status = acpi_evaluate_object(adev->handle, "_ONT", NULL, &buffer);
1297 	if (status == AE_NOT_FOUND) {
1298 		return 0;
1299 	} else if (ACPI_FAILURE(status)) {
1300 		dev_warn(&indio_dev->dev, "failed to execute _ONT: %d\n",
1301 			 status);
1302 		return status;
1303 	}
1304 
1305 	ont = buffer.pointer;
1306 	if (ont->type != ACPI_TYPE_PACKAGE || ont->package.count != 6)
1307 		goto out;
1308 
1309 	/* The first 3 integers provide axis order information.
1310 	 * e.g. 0 1 2 would indicate normal X,Y,Z ordering.
1311 	 * e.g. 1 0 2 indicates that data arrives in order Y,X,Z.
1312 	 */
1313 	elements = ont->package.elements;
1314 	for (i = 0; i < 3; i++) {
1315 		if (elements[i].type != ACPI_TYPE_INTEGER)
1316 			goto out;
1317 
1318 		val = elements[i].integer.value;
1319 		if (val > 2)
1320 			goto out;
1321 
1322 		/* Avoiding full matrix multiplication, we simply reorder the
1323 		 * columns in the default_ont matrix according to the
1324 		 * ordering provided by _ONT.
1325 		 */
1326 		final_ont[0][i] = default_ont[0][val];
1327 		final_ont[1][i] = default_ont[1][val];
1328 		final_ont[2][i] = default_ont[2][val];
1329 	}
1330 
1331 	/* The final 3 integers provide sign flip information.
1332 	 * 0 means no change, 1 means flip.
1333 	 * e.g. 0 0 1 means that Z data should be sign-flipped.
1334 	 * This is applied after the axis reordering from above.
1335 	 */
1336 	elements += 3;
1337 	for (i = 0; i < 3; i++) {
1338 		if (elements[i].type != ACPI_TYPE_INTEGER)
1339 			goto out;
1340 
1341 		val = elements[i].integer.value;
1342 		if (val != 0 && val != 1)
1343 			goto out;
1344 		if (!val)
1345 			continue;
1346 
1347 		/* Flip the values in the indicated column */
1348 		final_ont[0][i] *= -1;
1349 		final_ont[1][i] *= -1;
1350 		final_ont[2][i] *= -1;
1351 	}
1352 
1353 	/* Convert our integer matrix to a string-based iio_mount_matrix */
1354 	for (i = 0; i < 3; i++) {
1355 		for (j = 0; j < 3; j++) {
1356 			int matrix_val = final_ont[i][j];
1357 			char *str_value;
1358 
1359 			switch (matrix_val) {
1360 			case -1:
1361 				str_value = "-1";
1362 				break;
1363 			case 0:
1364 				str_value = "0";
1365 				break;
1366 			case 1:
1367 				str_value = "1";
1368 				break;
1369 			default:
1370 				goto out;
1371 			}
1372 			adata->mount_matrix.rotation[i * 3 + j] = str_value;
1373 		}
1374 	}
1375 
1376 	ret = 0;
1377 	dev_info(&indio_dev->dev, "computed mount matrix from ACPI\n");
1378 
1379 out:
1380 	kfree(buffer.pointer);
1381 	if (ret)
1382 		dev_dbg(&indio_dev->dev,
1383 			"failed to apply ACPI orientation data: %d\n", ret);
1384 
1385 	return ret;
1386 }
1387 #else /* !CONFIG_ACPI */
1388 static int apply_acpi_orientation(struct iio_dev *indio_dev)
1389 {
1390 	return -EINVAL;
1391 }
1392 #endif
1393 
1394 /*
1395  * st_accel_get_settings() - get sensor settings from device name
1396  * @name: device name buffer reference.
1397  *
1398  * Return: valid reference on success, NULL otherwise.
1399  */
1400 const struct st_sensor_settings *st_accel_get_settings(const char *name)
1401 {
1402 	int index = st_sensors_get_settings_index(name,
1403 					st_accel_sensors_settings,
1404 					ARRAY_SIZE(st_accel_sensors_settings));
1405 	if (index < 0)
1406 		return NULL;
1407 
1408 	return &st_accel_sensors_settings[index];
1409 }
1410 EXPORT_SYMBOL_NS(st_accel_get_settings, IIO_ST_SENSORS);
1411 
1412 int st_accel_common_probe(struct iio_dev *indio_dev)
1413 {
1414 	struct st_sensor_data *adata = iio_priv(indio_dev);
1415 	struct device *parent = indio_dev->dev.parent;
1416 	struct st_sensors_platform_data *pdata = dev_get_platdata(parent);
1417 	int err;
1418 
1419 	indio_dev->modes = INDIO_DIRECT_MODE;
1420 	indio_dev->info = &accel_info;
1421 
1422 	err = st_sensors_verify_id(indio_dev);
1423 	if (err < 0)
1424 		return err;
1425 
1426 	adata->num_data_channels = ST_ACCEL_NUMBER_DATA_CHANNELS;
1427 	indio_dev->channels = adata->sensor_settings->ch;
1428 	indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
1429 
1430 	/*
1431 	 * First try specific ACPI methods to retrieve orientation then try the
1432 	 * generic function.
1433 	 */
1434 	err = apply_acpi_orientation(indio_dev);
1435 	if (err) {
1436 		err = iio_read_mount_matrix(parent, &adata->mount_matrix);
1437 		if (err)
1438 			return err;
1439 	}
1440 
1441 	adata->current_fullscale = &adata->sensor_settings->fs.fs_avl[0];
1442 	adata->odr = adata->sensor_settings->odr.odr_avl[0].hz;
1443 
1444 	if (!pdata)
1445 		pdata = (struct st_sensors_platform_data *)&default_accel_pdata;
1446 
1447 	err = st_sensors_init_sensor(indio_dev, pdata);
1448 	if (err < 0)
1449 		return err;
1450 
1451 	err = st_accel_allocate_ring(indio_dev);
1452 	if (err < 0)
1453 		return err;
1454 
1455 	if (adata->irq > 0) {
1456 		err = st_sensors_allocate_trigger(indio_dev,
1457 						 ST_ACCEL_TRIGGER_OPS);
1458 		if (err < 0)
1459 			return err;
1460 	}
1461 
1462 	return devm_iio_device_register(parent, indio_dev);
1463 }
1464 EXPORT_SYMBOL_NS(st_accel_common_probe, IIO_ST_SENSORS);
1465 
1466 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
1467 MODULE_DESCRIPTION("STMicroelectronics accelerometers driver");
1468 MODULE_LICENSE("GPL v2");
1469 MODULE_IMPORT_NS(IIO_ST_SENSORS);
1470