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 		},
448 		.ch = (struct iio_chan_spec *)st_accel_8bit_channels,
449 		.odr = {
450 			.addr = 0x20,
451 			.mask = 0x80,
452 			.odr_avl = {
453 				{ .hz = 100, .value = 0x00, },
454 				{ .hz = 400, .value = 0x01, },
455 			},
456 		},
457 		.pw = {
458 			.addr = 0x20,
459 			.mask = 0x40,
460 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
461 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
462 		},
463 		.enable_axis = {
464 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
465 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
466 		},
467 		.fs = {
468 			.addr = 0x20,
469 			.mask = 0x20,
470 			/*
471 			 * TODO: check these resulting gain settings, these are
472 			 * not in the datsheet
473 			 */
474 			.fs_avl = {
475 				[0] = {
476 					.num = ST_ACCEL_FS_AVL_2G,
477 					.value = 0x00,
478 					.gain = IIO_G_TO_M_S_2(18000),
479 				},
480 				[1] = {
481 					.num = ST_ACCEL_FS_AVL_8G,
482 					.value = 0x01,
483 					.gain = IIO_G_TO_M_S_2(72000),
484 				},
485 			},
486 		},
487 		.drdy_irq = {
488 			.int1 = {
489 				.addr = 0x22,
490 				.mask = 0x04,
491 				.addr_od = 0x22,
492 				.mask_od = 0x40,
493 			},
494 			.int2 = {
495 				.addr = 0x22,
496 				.mask = 0x20,
497 				.addr_od = 0x22,
498 				.mask_od = 0x40,
499 			},
500 			.addr_ihl = 0x22,
501 			.mask_ihl = 0x80,
502 			.stat_drdy = {
503 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
504 				.mask = 0x07,
505 			},
506 		},
507 		.sim = {
508 			.addr = 0x21,
509 			.value = BIT(7),
510 		},
511 		.multi_read_bit = false,
512 		.bootime = 2, /* guess */
513 	},
514 	{
515 		.wai = 0x32,
516 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
517 		.sensors_supported = {
518 			[0] = H3LIS331DL_ACCEL_DEV_NAME,
519 		},
520 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
521 		.odr = {
522 			.addr = 0x20,
523 			.mask = 0x18,
524 			.odr_avl = {
525 				{ .hz = 50, .value = 0x00, },
526 				{ .hz = 100, .value = 0x01, },
527 				{ .hz = 400, .value = 0x02, },
528 				{ .hz = 1000, .value = 0x03, },
529 			},
530 		},
531 		.pw = {
532 			.addr = 0x20,
533 			.mask = 0x20,
534 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
535 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
536 		},
537 		.enable_axis = {
538 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
539 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
540 		},
541 		.fs = {
542 			.addr = 0x23,
543 			.mask = 0x30,
544 			.fs_avl = {
545 				[0] = {
546 					.num = ST_ACCEL_FS_AVL_100G,
547 					.value = 0x00,
548 					.gain = IIO_G_TO_M_S_2(49000),
549 				},
550 				[1] = {
551 					.num = ST_ACCEL_FS_AVL_200G,
552 					.value = 0x01,
553 					.gain = IIO_G_TO_M_S_2(98000),
554 				},
555 				[2] = {
556 					.num = ST_ACCEL_FS_AVL_400G,
557 					.value = 0x03,
558 					.gain = IIO_G_TO_M_S_2(195000),
559 				},
560 			},
561 		},
562 		.bdu = {
563 			.addr = 0x23,
564 			.mask = 0x80,
565 		},
566 		.drdy_irq = {
567 			.int1 = {
568 				.addr = 0x22,
569 				.mask = 0x02,
570 			},
571 			.int2 = {
572 				.addr = 0x22,
573 				.mask = 0x10,
574 			},
575 			.addr_ihl = 0x22,
576 			.mask_ihl = 0x80,
577 		},
578 		.sim = {
579 			.addr = 0x23,
580 			.value = BIT(0),
581 		},
582 		.multi_read_bit = true,
583 		.bootime = 2,
584 	},
585 	{
586 		/* No WAI register present */
587 		.sensors_supported = {
588 			[0] = LIS3L02DQ_ACCEL_DEV_NAME,
589 		},
590 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
591 		.odr = {
592 			.addr = 0x20,
593 			.mask = 0x30,
594 			.odr_avl = {
595 				{ .hz = 280, .value = 0x00, },
596 				{ .hz = 560, .value = 0x01, },
597 				{ .hz = 1120, .value = 0x02, },
598 				{ .hz = 4480, .value = 0x03, },
599 			},
600 		},
601 		.pw = {
602 			.addr = 0x20,
603 			.mask = 0xc0,
604 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
605 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
606 		},
607 		.enable_axis = {
608 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
609 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
610 		},
611 		.fs = {
612 			.fs_avl = {
613 				[0] = {
614 					.num = ST_ACCEL_FS_AVL_2G,
615 					.gain = IIO_G_TO_M_S_2(488),
616 				},
617 			},
618 		},
619 		/*
620 		 * The part has a BDU bit but if set the data is never
621 		 * updated so don't set it.
622 		 */
623 		.bdu = {
624 		},
625 		.drdy_irq = {
626 			.int1 = {
627 				.addr = 0x21,
628 				.mask = 0x04,
629 			},
630 			.stat_drdy = {
631 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
632 				.mask = 0x07,
633 			},
634 		},
635 		.sim = {
636 			.addr = 0x21,
637 			.value = BIT(1),
638 		},
639 		.multi_read_bit = false,
640 		.bootime = 2,
641 	},
642 	{
643 		.wai = 0x33,
644 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
645 		.sensors_supported = {
646 			[0] = LNG2DM_ACCEL_DEV_NAME,
647 		},
648 		.ch = (struct iio_chan_spec *)st_accel_8bit_channels,
649 		.odr = {
650 			.addr = 0x20,
651 			.mask = 0xf0,
652 			.odr_avl = {
653 				{ .hz = 1, .value = 0x01, },
654 				{ .hz = 10, .value = 0x02, },
655 				{ .hz = 25, .value = 0x03, },
656 				{ .hz = 50, .value = 0x04, },
657 				{ .hz = 100, .value = 0x05, },
658 				{ .hz = 200, .value = 0x06, },
659 				{ .hz = 400, .value = 0x07, },
660 				{ .hz = 1600, .value = 0x08, },
661 			},
662 		},
663 		.pw = {
664 			.addr = 0x20,
665 			.mask = 0xf0,
666 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
667 		},
668 		.enable_axis = {
669 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
670 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
671 		},
672 		.fs = {
673 			.addr = 0x23,
674 			.mask = 0x30,
675 			.fs_avl = {
676 				[0] = {
677 					.num = ST_ACCEL_FS_AVL_2G,
678 					.value = 0x00,
679 					.gain = IIO_G_TO_M_S_2(15600),
680 				},
681 				[1] = {
682 					.num = ST_ACCEL_FS_AVL_4G,
683 					.value = 0x01,
684 					.gain = IIO_G_TO_M_S_2(31200),
685 				},
686 				[2] = {
687 					.num = ST_ACCEL_FS_AVL_8G,
688 					.value = 0x02,
689 					.gain = IIO_G_TO_M_S_2(62500),
690 				},
691 				[3] = {
692 					.num = ST_ACCEL_FS_AVL_16G,
693 					.value = 0x03,
694 					.gain = IIO_G_TO_M_S_2(187500),
695 				},
696 			},
697 		},
698 		.drdy_irq = {
699 			.int1 = {
700 				.addr = 0x22,
701 				.mask = 0x10,
702 			},
703 			.addr_ihl = 0x25,
704 			.mask_ihl = 0x02,
705 			.stat_drdy = {
706 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
707 				.mask = 0x07,
708 			},
709 		},
710 		.sim = {
711 			.addr = 0x23,
712 			.value = BIT(0),
713 		},
714 		.multi_read_bit = true,
715 		.bootime = 2,
716 	},
717 	{
718 		.wai = 0x44,
719 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
720 		.sensors_supported = {
721 			[0] = LIS2DW12_ACCEL_DEV_NAME,
722 		},
723 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
724 		.odr = {
725 			.addr = 0x20,
726 			.mask = 0xf0,
727 			.odr_avl = {
728 				{ .hz = 1, .value = 0x01, },
729 				{ .hz = 12, .value = 0x02, },
730 				{ .hz = 25, .value = 0x03, },
731 				{ .hz = 50, .value = 0x04, },
732 				{ .hz = 100, .value = 0x05, },
733 				{ .hz = 200, .value = 0x06, },
734 			},
735 		},
736 		.pw = {
737 			.addr = 0x20,
738 			.mask = 0xf0,
739 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
740 		},
741 		.fs = {
742 			.addr = 0x25,
743 			.mask = 0x30,
744 			.fs_avl = {
745 				[0] = {
746 					.num = ST_ACCEL_FS_AVL_2G,
747 					.value = 0x00,
748 					.gain = IIO_G_TO_M_S_2(976),
749 				},
750 				[1] = {
751 					.num = ST_ACCEL_FS_AVL_4G,
752 					.value = 0x01,
753 					.gain = IIO_G_TO_M_S_2(1952),
754 				},
755 				[2] = {
756 					.num = ST_ACCEL_FS_AVL_8G,
757 					.value = 0x02,
758 					.gain = IIO_G_TO_M_S_2(3904),
759 				},
760 				[3] = {
761 					.num = ST_ACCEL_FS_AVL_16G,
762 					.value = 0x03,
763 					.gain = IIO_G_TO_M_S_2(7808),
764 				},
765 			},
766 		},
767 		.bdu = {
768 			.addr = 0x21,
769 			.mask = 0x08,
770 		},
771 		.drdy_irq = {
772 			.int1 = {
773 				.addr = 0x23,
774 				.mask = 0x01,
775 				.addr_od = 0x22,
776 				.mask_od = 0x20,
777 			},
778 			.int2 = {
779 				.addr = 0x24,
780 				.mask = 0x01,
781 				.addr_od = 0x22,
782 				.mask_od = 0x20,
783 			},
784 			.addr_ihl = 0x22,
785 			.mask_ihl = 0x08,
786 			.stat_drdy = {
787 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
788 				.mask = 0x01,
789 			},
790 		},
791 		.sim = {
792 			.addr = 0x21,
793 			.value = BIT(0),
794 		},
795 		.multi_read_bit = false,
796 		.bootime = 2,
797 	},
798 	{
799 		.wai = 0x11,
800 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
801 		.sensors_supported = {
802 			[0] = LIS3DHH_ACCEL_DEV_NAME,
803 		},
804 		.ch = (struct iio_chan_spec *)st_accel_16bit_channels,
805 		.odr = {
806 			/* just ODR = 1100Hz available */
807 			.odr_avl = {
808 				{ .hz = 1100, .value = 0x00, },
809 			},
810 		},
811 		.pw = {
812 			.addr = 0x20,
813 			.mask = 0x80,
814 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
815 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
816 		},
817 		.fs = {
818 			.fs_avl = {
819 				[0] = {
820 					.num = ST_ACCEL_FS_AVL_2G,
821 					.gain = IIO_G_TO_M_S_2(76),
822 				},
823 			},
824 		},
825 		.bdu = {
826 			.addr = 0x20,
827 			.mask = 0x01,
828 		},
829 		.drdy_irq = {
830 			.int1 = {
831 				.addr = 0x21,
832 				.mask = 0x80,
833 				.addr_od = 0x23,
834 				.mask_od = 0x04,
835 			},
836 			.int2 = {
837 				.addr = 0x22,
838 				.mask = 0x80,
839 				.addr_od = 0x23,
840 				.mask_od = 0x08,
841 			},
842 			.stat_drdy = {
843 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
844 				.mask = 0x07,
845 			},
846 		},
847 		.multi_read_bit = false,
848 		.bootime = 2,
849 	},
850 	{
851 		.wai = 0x33,
852 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
853 		.sensors_supported = {
854 			[0] = LIS2DE12_ACCEL_DEV_NAME,
855 		},
856 		.ch = (struct iio_chan_spec *)st_accel_8bit_channels,
857 		.odr = {
858 			.addr = 0x20,
859 			.mask = 0xf0,
860 			.odr_avl = {
861 				{ .hz = 1, .value = 0x01, },
862 				{ .hz = 10, .value = 0x02, },
863 				{ .hz = 25, .value = 0x03, },
864 				{ .hz = 50, .value = 0x04, },
865 				{ .hz = 100, .value = 0x05, },
866 				{ .hz = 200, .value = 0x06, },
867 				{ .hz = 400, .value = 0x07, },
868 				{ .hz = 1620, .value = 0x08, },
869 				{ .hz = 5376, .value = 0x09, },
870 			},
871 		},
872 		.pw = {
873 			.addr = 0x20,
874 			.mask = 0xf0,
875 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
876 		},
877 		.enable_axis = {
878 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
879 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
880 		},
881 		.fs = {
882 			.addr = 0x23,
883 			.mask = 0x30,
884 			.fs_avl = {
885 				[0] = {
886 					.num = ST_ACCEL_FS_AVL_2G,
887 					.value = 0x00,
888 					.gain = IIO_G_TO_M_S_2(15600),
889 				},
890 				[1] = {
891 					.num = ST_ACCEL_FS_AVL_4G,
892 					.value = 0x01,
893 					.gain = IIO_G_TO_M_S_2(31200),
894 				},
895 				[2] = {
896 					.num = ST_ACCEL_FS_AVL_8G,
897 					.value = 0x02,
898 					.gain = IIO_G_TO_M_S_2(62500),
899 				},
900 				[3] = {
901 					.num = ST_ACCEL_FS_AVL_16G,
902 					.value = 0x03,
903 					.gain = IIO_G_TO_M_S_2(187500),
904 				},
905 			},
906 		},
907 		.drdy_irq = {
908 			.int1 = {
909 				.addr = 0x22,
910 				.mask = 0x10,
911 			},
912 			.addr_ihl = 0x25,
913 			.mask_ihl = 0x02,
914 			.stat_drdy = {
915 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
916 				.mask = 0x07,
917 			},
918 		},
919 		.sim = {
920 			.addr = 0x23,
921 			.value = BIT(0),
922 		},
923 		.multi_read_bit = true,
924 		.bootime = 2,
925 	},
926 	{
927 		.wai = 0x41,
928 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
929 		.sensors_supported = {
930 			[0] = LIS2HH12_ACCEL_DEV_NAME,
931 		},
932 		.ch = (struct iio_chan_spec *)st_accel_16bit_channels,
933 		.odr = {
934 			.addr = 0x20,
935 			.mask = 0x70,
936 			.odr_avl = {
937 				{ .hz = 10, .value = 0x01, },
938 				{ .hz = 50, .value = 0x02, },
939 				{ .hz = 100, .value = 0x03, },
940 				{ .hz = 200, .value = 0x04, },
941 				{ .hz = 400, .value = 0x05, },
942 				{ .hz = 800, .value = 0x06, },
943 			},
944 		},
945 		.pw = {
946 			.addr = 0x20,
947 			.mask = 0x70,
948 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
949 		},
950 		.enable_axis = {
951 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
952 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
953 		},
954 		.fs = {
955 			.addr = 0x23,
956 			.mask = 0x30,
957 			.fs_avl = {
958 				[0] = {
959 					.num = ST_ACCEL_FS_AVL_2G,
960 					.value = 0x00,
961 					.gain = IIO_G_TO_M_S_2(61),
962 				},
963 				[1] = {
964 					.num = ST_ACCEL_FS_AVL_4G,
965 					.value = 0x02,
966 					.gain = IIO_G_TO_M_S_2(122),
967 				},
968 				[2] = {
969 					.num = ST_ACCEL_FS_AVL_8G,
970 					.value = 0x03,
971 					.gain = IIO_G_TO_M_S_2(244),
972 				},
973 			},
974 		},
975 		.bdu = {
976 			.addr = 0x20,
977 			.mask = 0x08,
978 		},
979 		.drdy_irq = {
980 			.int1 = {
981 				.addr = 0x22,
982 				.mask = 0x01,
983 			},
984 			.int2 = {
985 				.addr = 0x25,
986 				.mask = 0x01,
987 			},
988 			.addr_ihl = 0x24,
989 			.mask_ihl = 0x02,
990 			.stat_drdy = {
991 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
992 				.mask = 0x07,
993 			},
994 		},
995 		.sim = {
996 			.addr = 0x23,
997 			.value = BIT(0),
998 		},
999 		.multi_read_bit = true,
1000 		.bootime = 2,
1001 	},
1002 	{
1003 		.wai = 0x49,
1004 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
1005 		.sensors_supported = {
1006 			[0] = LSM9DS0_IMU_DEV_NAME,
1007 		},
1008 		.ch = (struct iio_chan_spec *)st_accel_16bit_channels,
1009 		.odr = {
1010 			.addr = 0x20,
1011 			.mask = GENMASK(7, 4),
1012 			.odr_avl = {
1013 				{ 3, 0x01, },
1014 				{ 6, 0x02, },
1015 				{ 12, 0x03, },
1016 				{ 25, 0x04, },
1017 				{ 50, 0x05, },
1018 				{ 100, 0x06, },
1019 				{ 200, 0x07, },
1020 				{ 400, 0x08, },
1021 				{ 800, 0x09, },
1022 				{ 1600, 0x0a, },
1023 			},
1024 		},
1025 		.pw = {
1026 			.addr = 0x20,
1027 			.mask = GENMASK(7, 4),
1028 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
1029 		},
1030 		.enable_axis = {
1031 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
1032 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
1033 		},
1034 		.fs = {
1035 			.addr = 0x21,
1036 			.mask = GENMASK(5, 3),
1037 			.fs_avl = {
1038 				[0] = {
1039 					.num = ST_ACCEL_FS_AVL_2G,
1040 					.value = 0x00,
1041 					.gain = IIO_G_TO_M_S_2(61),
1042 				},
1043 				[1] = {
1044 					.num = ST_ACCEL_FS_AVL_4G,
1045 					.value = 0x01,
1046 					.gain = IIO_G_TO_M_S_2(122),
1047 				},
1048 				[2] = {
1049 					.num = ST_ACCEL_FS_AVL_6G,
1050 					.value = 0x02,
1051 					.gain = IIO_G_TO_M_S_2(183),
1052 				},
1053 				[3] = {
1054 					.num = ST_ACCEL_FS_AVL_8G,
1055 					.value = 0x03,
1056 					.gain = IIO_G_TO_M_S_2(244),
1057 				},
1058 				[4] = {
1059 					.num = ST_ACCEL_FS_AVL_16G,
1060 					.value = 0x04,
1061 					.gain = IIO_G_TO_M_S_2(732),
1062 				},
1063 			},
1064 		},
1065 		.bdu = {
1066 			.addr = 0x20,
1067 			.mask = BIT(3),
1068 		},
1069 		.drdy_irq = {
1070 			.int1 = {
1071 				.addr = 0x22,
1072 				.mask = BIT(2),
1073 			},
1074 			.int2 = {
1075 				.addr = 0x23,
1076 				.mask = BIT(3),
1077 			},
1078 			.stat_drdy = {
1079 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
1080 				.mask = GENMASK(2, 0),
1081 			},
1082 		},
1083 		.sim = {
1084 			.addr = 0x21,
1085 			.value = BIT(0),
1086 		},
1087 		.multi_read_bit = true,
1088 		.bootime = 2,
1089 	},
1090 	{
1091 		/*
1092 		 * Not an ST part. Register-compatible with the LIS2DH, even
1093 		 * though the WAI value is different.
1094 		 */
1095 		.wai = 0x11,
1096 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
1097 		.sensors_supported = {
1098 			[0] = SC7A20_ACCEL_DEV_NAME,
1099 		},
1100 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
1101 		.odr = {
1102 			.addr = 0x20,
1103 			.mask = 0xf0,
1104 			.odr_avl = {
1105 				{ .hz = 1, .value = 0x01, },
1106 				{ .hz = 10, .value = 0x02, },
1107 				{ .hz = 25, .value = 0x03, },
1108 				{ .hz = 50, .value = 0x04, },
1109 				{ .hz = 100, .value = 0x05, },
1110 				{ .hz = 200, .value = 0x06, },
1111 				{ .hz = 400, .value = 0x07, },
1112 				{ .hz = 1600, .value = 0x08, },
1113 			},
1114 		},
1115 		.pw = {
1116 			.addr = 0x20,
1117 			.mask = 0xf0,
1118 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
1119 		},
1120 		.enable_axis = {
1121 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
1122 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
1123 		},
1124 		.fs = {
1125 			.addr = 0x23,
1126 			.mask = 0x30,
1127 			.fs_avl = {
1128 				[0] = {
1129 					.num = ST_ACCEL_FS_AVL_2G,
1130 					.value = 0x00,
1131 					.gain = IIO_G_TO_M_S_2(1000),
1132 				},
1133 				[1] = {
1134 					.num = ST_ACCEL_FS_AVL_4G,
1135 					.value = 0x01,
1136 					.gain = IIO_G_TO_M_S_2(2000),
1137 				},
1138 				[2] = {
1139 					.num = ST_ACCEL_FS_AVL_8G,
1140 					.value = 0x02,
1141 					.gain = IIO_G_TO_M_S_2(4000),
1142 				},
1143 				[3] = {
1144 					.num = ST_ACCEL_FS_AVL_16G,
1145 					.value = 0x03,
1146 					.gain = IIO_G_TO_M_S_2(12000),
1147 				},
1148 			},
1149 		},
1150 		.bdu = {
1151 			.addr = 0x23,
1152 			.mask = 0x80,
1153 		},
1154 		.drdy_irq = {
1155 			.int1 = {
1156 				.addr = 0x22,
1157 				.mask = 0x10,
1158 			},
1159 			.addr_ihl = 0x25,
1160 			.mask_ihl = 0x02,
1161 			.stat_drdy = {
1162 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
1163 				.mask = 0x07,
1164 			},
1165 		},
1166 		.sim = {
1167 			.addr = 0x23,
1168 			.value = BIT(0),
1169 		},
1170 		.multi_read_bit = true,
1171 		.bootime = 2,
1172 	},
1173 };
1174 
1175 /* Default accel DRDY is available on INT1 pin */
1176 static const struct st_sensors_platform_data default_accel_pdata = {
1177 	.drdy_int_pin = 1,
1178 };
1179 
1180 static int st_accel_read_raw(struct iio_dev *indio_dev,
1181 			struct iio_chan_spec const *ch, int *val,
1182 							int *val2, long mask)
1183 {
1184 	int err;
1185 	struct st_sensor_data *adata = iio_priv(indio_dev);
1186 
1187 	switch (mask) {
1188 	case IIO_CHAN_INFO_RAW:
1189 		err = st_sensors_read_info_raw(indio_dev, ch, val);
1190 		if (err < 0)
1191 			goto read_error;
1192 
1193 		return IIO_VAL_INT;
1194 	case IIO_CHAN_INFO_SCALE:
1195 		*val = adata->current_fullscale->gain / 1000000;
1196 		*val2 = adata->current_fullscale->gain % 1000000;
1197 		return IIO_VAL_INT_PLUS_MICRO;
1198 	case IIO_CHAN_INFO_SAMP_FREQ:
1199 		*val = adata->odr;
1200 		return IIO_VAL_INT;
1201 	default:
1202 		return -EINVAL;
1203 	}
1204 
1205 read_error:
1206 	return err;
1207 }
1208 
1209 static int st_accel_write_raw(struct iio_dev *indio_dev,
1210 		struct iio_chan_spec const *chan, int val, int val2, long mask)
1211 {
1212 	int err;
1213 
1214 	switch (mask) {
1215 	case IIO_CHAN_INFO_SCALE: {
1216 		int gain;
1217 
1218 		gain = val * 1000000 + val2;
1219 		err = st_sensors_set_fullscale_by_gain(indio_dev, gain);
1220 		break;
1221 	}
1222 	case IIO_CHAN_INFO_SAMP_FREQ:
1223 		if (val2)
1224 			return -EINVAL;
1225 		mutex_lock(&indio_dev->mlock);
1226 		err = st_sensors_set_odr(indio_dev, val);
1227 		mutex_unlock(&indio_dev->mlock);
1228 		return err;
1229 	default:
1230 		return -EINVAL;
1231 	}
1232 
1233 	return err;
1234 }
1235 
1236 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
1237 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_accel_scale_available);
1238 
1239 static struct attribute *st_accel_attributes[] = {
1240 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
1241 	&iio_dev_attr_in_accel_scale_available.dev_attr.attr,
1242 	NULL,
1243 };
1244 
1245 static const struct attribute_group st_accel_attribute_group = {
1246 	.attrs = st_accel_attributes,
1247 };
1248 
1249 static const struct iio_info accel_info = {
1250 	.attrs = &st_accel_attribute_group,
1251 	.read_raw = &st_accel_read_raw,
1252 	.write_raw = &st_accel_write_raw,
1253 	.debugfs_reg_access = &st_sensors_debugfs_reg_access,
1254 };
1255 
1256 #ifdef CONFIG_IIO_TRIGGER
1257 static const struct iio_trigger_ops st_accel_trigger_ops = {
1258 	.set_trigger_state = ST_ACCEL_TRIGGER_SET_STATE,
1259 	.validate_device = st_sensors_validate_device,
1260 };
1261 #define ST_ACCEL_TRIGGER_OPS (&st_accel_trigger_ops)
1262 #else
1263 #define ST_ACCEL_TRIGGER_OPS NULL
1264 #endif
1265 
1266 #ifdef CONFIG_ACPI
1267 /* Read ST-specific _ONT orientation data from ACPI and generate an
1268  * appropriate mount matrix.
1269  */
1270 static int apply_acpi_orientation(struct iio_dev *indio_dev)
1271 {
1272 	struct st_sensor_data *adata = iio_priv(indio_dev);
1273 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1274 	struct acpi_device *adev;
1275 	union acpi_object *ont;
1276 	union acpi_object *elements;
1277 	acpi_status status;
1278 	int ret = -EINVAL;
1279 	unsigned int val;
1280 	int i, j;
1281 	int final_ont[3][3] = { { 0 }, };
1282 
1283 	/* For some reason, ST's _ONT translation does not apply directly
1284 	 * to the data read from the sensor. Another translation must be
1285 	 * performed first, as described by the matrix below. Perhaps
1286 	 * ST required this specific translation for the first product
1287 	 * where the device was mounted?
1288 	 */
1289 	const int default_ont[3][3] = {
1290 		{  0,  1,  0 },
1291 		{ -1,  0,  0 },
1292 		{  0,  0, -1 },
1293 	};
1294 
1295 
1296 	adev = ACPI_COMPANION(indio_dev->dev.parent);
1297 	if (!adev)
1298 		return 0;
1299 
1300 	/* Read _ONT data, which should be a package of 6 integers. */
1301 	status = acpi_evaluate_object(adev->handle, "_ONT", NULL, &buffer);
1302 	if (status == AE_NOT_FOUND) {
1303 		return 0;
1304 	} else if (ACPI_FAILURE(status)) {
1305 		dev_warn(&indio_dev->dev, "failed to execute _ONT: %d\n",
1306 			 status);
1307 		return status;
1308 	}
1309 
1310 	ont = buffer.pointer;
1311 	if (ont->type != ACPI_TYPE_PACKAGE || ont->package.count != 6)
1312 		goto out;
1313 
1314 	/* The first 3 integers provide axis order information.
1315 	 * e.g. 0 1 2 would indicate normal X,Y,Z ordering.
1316 	 * e.g. 1 0 2 indicates that data arrives in order Y,X,Z.
1317 	 */
1318 	elements = ont->package.elements;
1319 	for (i = 0; i < 3; i++) {
1320 		if (elements[i].type != ACPI_TYPE_INTEGER)
1321 			goto out;
1322 
1323 		val = elements[i].integer.value;
1324 		if (val > 2)
1325 			goto out;
1326 
1327 		/* Avoiding full matrix multiplication, we simply reorder the
1328 		 * columns in the default_ont matrix according to the
1329 		 * ordering provided by _ONT.
1330 		 */
1331 		final_ont[0][i] = default_ont[0][val];
1332 		final_ont[1][i] = default_ont[1][val];
1333 		final_ont[2][i] = default_ont[2][val];
1334 	}
1335 
1336 	/* The final 3 integers provide sign flip information.
1337 	 * 0 means no change, 1 means flip.
1338 	 * e.g. 0 0 1 means that Z data should be sign-flipped.
1339 	 * This is applied after the axis reordering from above.
1340 	 */
1341 	elements += 3;
1342 	for (i = 0; i < 3; i++) {
1343 		if (elements[i].type != ACPI_TYPE_INTEGER)
1344 			goto out;
1345 
1346 		val = elements[i].integer.value;
1347 		if (val != 0 && val != 1)
1348 			goto out;
1349 		if (!val)
1350 			continue;
1351 
1352 		/* Flip the values in the indicated column */
1353 		final_ont[0][i] *= -1;
1354 		final_ont[1][i] *= -1;
1355 		final_ont[2][i] *= -1;
1356 	}
1357 
1358 	/* Convert our integer matrix to a string-based iio_mount_matrix */
1359 	for (i = 0; i < 3; i++) {
1360 		for (j = 0; j < 3; j++) {
1361 			int matrix_val = final_ont[i][j];
1362 			char *str_value;
1363 
1364 			switch (matrix_val) {
1365 			case -1:
1366 				str_value = "-1";
1367 				break;
1368 			case 0:
1369 				str_value = "0";
1370 				break;
1371 			case 1:
1372 				str_value = "1";
1373 				break;
1374 			default:
1375 				goto out;
1376 			}
1377 			adata->mount_matrix.rotation[i * 3 + j] = str_value;
1378 		}
1379 	}
1380 
1381 	ret = 0;
1382 	dev_info(&indio_dev->dev, "computed mount matrix from ACPI\n");
1383 
1384 out:
1385 	kfree(buffer.pointer);
1386 	if (ret)
1387 		dev_dbg(&indio_dev->dev,
1388 			"failed to apply ACPI orientation data: %d\n", ret);
1389 
1390 	return ret;
1391 }
1392 #else /* !CONFIG_ACPI */
1393 static int apply_acpi_orientation(struct iio_dev *indio_dev)
1394 {
1395 	return -EINVAL;
1396 }
1397 #endif
1398 
1399 /*
1400  * st_accel_get_settings() - get sensor settings from device name
1401  * @name: device name buffer reference.
1402  *
1403  * Return: valid reference on success, NULL otherwise.
1404  */
1405 const struct st_sensor_settings *st_accel_get_settings(const char *name)
1406 {
1407 	int index = st_sensors_get_settings_index(name,
1408 					st_accel_sensors_settings,
1409 					ARRAY_SIZE(st_accel_sensors_settings));
1410 	if (index < 0)
1411 		return NULL;
1412 
1413 	return &st_accel_sensors_settings[index];
1414 }
1415 EXPORT_SYMBOL_NS(st_accel_get_settings, IIO_ST_SENSORS);
1416 
1417 int st_accel_common_probe(struct iio_dev *indio_dev)
1418 {
1419 	struct st_sensor_data *adata = iio_priv(indio_dev);
1420 	struct device *parent = indio_dev->dev.parent;
1421 	struct st_sensors_platform_data *pdata = dev_get_platdata(parent);
1422 	int err;
1423 
1424 	indio_dev->modes = INDIO_DIRECT_MODE;
1425 	indio_dev->info = &accel_info;
1426 
1427 	err = st_sensors_verify_id(indio_dev);
1428 	if (err < 0)
1429 		return err;
1430 
1431 	adata->num_data_channels = ST_ACCEL_NUMBER_DATA_CHANNELS;
1432 	indio_dev->channels = adata->sensor_settings->ch;
1433 	indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
1434 
1435 	/*
1436 	 * First try specific ACPI methods to retrieve orientation then try the
1437 	 * generic function.
1438 	 */
1439 	err = apply_acpi_orientation(indio_dev);
1440 	if (err) {
1441 		err = iio_read_mount_matrix(parent, &adata->mount_matrix);
1442 		if (err)
1443 			return err;
1444 	}
1445 
1446 	adata->current_fullscale = &adata->sensor_settings->fs.fs_avl[0];
1447 	adata->odr = adata->sensor_settings->odr.odr_avl[0].hz;
1448 
1449 	if (!pdata)
1450 		pdata = (struct st_sensors_platform_data *)&default_accel_pdata;
1451 
1452 	err = st_sensors_init_sensor(indio_dev, pdata);
1453 	if (err < 0)
1454 		return err;
1455 
1456 	err = st_accel_allocate_ring(indio_dev);
1457 	if (err < 0)
1458 		return err;
1459 
1460 	if (adata->irq > 0) {
1461 		err = st_sensors_allocate_trigger(indio_dev,
1462 						 ST_ACCEL_TRIGGER_OPS);
1463 		if (err < 0)
1464 			return err;
1465 	}
1466 
1467 	return devm_iio_device_register(parent, indio_dev);
1468 }
1469 EXPORT_SYMBOL_NS(st_accel_common_probe, IIO_ST_SENSORS);
1470 
1471 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
1472 MODULE_DESCRIPTION("STMicroelectronics accelerometers driver");
1473 MODULE_LICENSE("GPL v2");
1474 MODULE_IMPORT_NS(IIO_ST_SENSORS);
1475