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 		},
933 		.ch = (struct iio_chan_spec *)st_accel_16bit_channels,
934 		.odr = {
935 			.addr = 0x20,
936 			.mask = 0x70,
937 			.odr_avl = {
938 				{ .hz = 10, .value = 0x01, },
939 				{ .hz = 50, .value = 0x02, },
940 				{ .hz = 100, .value = 0x03, },
941 				{ .hz = 200, .value = 0x04, },
942 				{ .hz = 400, .value = 0x05, },
943 				{ .hz = 800, .value = 0x06, },
944 			},
945 		},
946 		.pw = {
947 			.addr = 0x20,
948 			.mask = 0x70,
949 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
950 		},
951 		.enable_axis = {
952 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
953 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
954 		},
955 		.fs = {
956 			.addr = 0x23,
957 			.mask = 0x30,
958 			.fs_avl = {
959 				[0] = {
960 					.num = ST_ACCEL_FS_AVL_2G,
961 					.value = 0x00,
962 					.gain = IIO_G_TO_M_S_2(61),
963 				},
964 				[1] = {
965 					.num = ST_ACCEL_FS_AVL_4G,
966 					.value = 0x02,
967 					.gain = IIO_G_TO_M_S_2(122),
968 				},
969 				[2] = {
970 					.num = ST_ACCEL_FS_AVL_8G,
971 					.value = 0x03,
972 					.gain = IIO_G_TO_M_S_2(244),
973 				},
974 			},
975 		},
976 		.bdu = {
977 			.addr = 0x20,
978 			.mask = 0x08,
979 		},
980 		.drdy_irq = {
981 			.int1 = {
982 				.addr = 0x22,
983 				.mask = 0x01,
984 			},
985 			.int2 = {
986 				.addr = 0x25,
987 				.mask = 0x01,
988 			},
989 			.addr_ihl = 0x24,
990 			.mask_ihl = 0x02,
991 			.stat_drdy = {
992 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
993 				.mask = 0x07,
994 			},
995 		},
996 		.sim = {
997 			.addr = 0x23,
998 			.value = BIT(0),
999 		},
1000 		.multi_read_bit = true,
1001 		.bootime = 2,
1002 	},
1003 	{
1004 		.wai = 0x49,
1005 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
1006 		.sensors_supported = {
1007 			[0] = LSM9DS0_IMU_DEV_NAME,
1008 		},
1009 		.ch = (struct iio_chan_spec *)st_accel_16bit_channels,
1010 		.odr = {
1011 			.addr = 0x20,
1012 			.mask = GENMASK(7, 4),
1013 			.odr_avl = {
1014 				{ 3, 0x01, },
1015 				{ 6, 0x02, },
1016 				{ 12, 0x03, },
1017 				{ 25, 0x04, },
1018 				{ 50, 0x05, },
1019 				{ 100, 0x06, },
1020 				{ 200, 0x07, },
1021 				{ 400, 0x08, },
1022 				{ 800, 0x09, },
1023 				{ 1600, 0x0a, },
1024 			},
1025 		},
1026 		.pw = {
1027 			.addr = 0x20,
1028 			.mask = GENMASK(7, 4),
1029 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
1030 		},
1031 		.enable_axis = {
1032 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
1033 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
1034 		},
1035 		.fs = {
1036 			.addr = 0x21,
1037 			.mask = GENMASK(5, 3),
1038 			.fs_avl = {
1039 				[0] = {
1040 					.num = ST_ACCEL_FS_AVL_2G,
1041 					.value = 0x00,
1042 					.gain = IIO_G_TO_M_S_2(61),
1043 				},
1044 				[1] = {
1045 					.num = ST_ACCEL_FS_AVL_4G,
1046 					.value = 0x01,
1047 					.gain = IIO_G_TO_M_S_2(122),
1048 				},
1049 				[2] = {
1050 					.num = ST_ACCEL_FS_AVL_6G,
1051 					.value = 0x02,
1052 					.gain = IIO_G_TO_M_S_2(183),
1053 				},
1054 				[3] = {
1055 					.num = ST_ACCEL_FS_AVL_8G,
1056 					.value = 0x03,
1057 					.gain = IIO_G_TO_M_S_2(244),
1058 				},
1059 				[4] = {
1060 					.num = ST_ACCEL_FS_AVL_16G,
1061 					.value = 0x04,
1062 					.gain = IIO_G_TO_M_S_2(732),
1063 				},
1064 			},
1065 		},
1066 		.bdu = {
1067 			.addr = 0x20,
1068 			.mask = BIT(3),
1069 		},
1070 		.drdy_irq = {
1071 			.int1 = {
1072 				.addr = 0x22,
1073 				.mask = BIT(2),
1074 			},
1075 			.int2 = {
1076 				.addr = 0x23,
1077 				.mask = BIT(3),
1078 			},
1079 			.stat_drdy = {
1080 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
1081 				.mask = GENMASK(2, 0),
1082 			},
1083 		},
1084 		.sim = {
1085 			.addr = 0x21,
1086 			.value = BIT(0),
1087 		},
1088 		.multi_read_bit = true,
1089 		.bootime = 2,
1090 	},
1091 	{
1092 		/*
1093 		 * Not an ST part. Register-compatible with the LIS2DH, even
1094 		 * though the WAI value is different.
1095 		 */
1096 		.wai = 0x11,
1097 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
1098 		.sensors_supported = {
1099 			[0] = SC7A20_ACCEL_DEV_NAME,
1100 		},
1101 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
1102 		.odr = {
1103 			.addr = 0x20,
1104 			.mask = 0xf0,
1105 			.odr_avl = {
1106 				{ .hz = 1, .value = 0x01, },
1107 				{ .hz = 10, .value = 0x02, },
1108 				{ .hz = 25, .value = 0x03, },
1109 				{ .hz = 50, .value = 0x04, },
1110 				{ .hz = 100, .value = 0x05, },
1111 				{ .hz = 200, .value = 0x06, },
1112 				{ .hz = 400, .value = 0x07, },
1113 				{ .hz = 1600, .value = 0x08, },
1114 			},
1115 		},
1116 		.pw = {
1117 			.addr = 0x20,
1118 			.mask = 0xf0,
1119 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
1120 		},
1121 		.enable_axis = {
1122 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
1123 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
1124 		},
1125 		.fs = {
1126 			.addr = 0x23,
1127 			.mask = 0x30,
1128 			.fs_avl = {
1129 				[0] = {
1130 					.num = ST_ACCEL_FS_AVL_2G,
1131 					.value = 0x00,
1132 					.gain = IIO_G_TO_M_S_2(1000),
1133 				},
1134 				[1] = {
1135 					.num = ST_ACCEL_FS_AVL_4G,
1136 					.value = 0x01,
1137 					.gain = IIO_G_TO_M_S_2(2000),
1138 				},
1139 				[2] = {
1140 					.num = ST_ACCEL_FS_AVL_8G,
1141 					.value = 0x02,
1142 					.gain = IIO_G_TO_M_S_2(4000),
1143 				},
1144 				[3] = {
1145 					.num = ST_ACCEL_FS_AVL_16G,
1146 					.value = 0x03,
1147 					.gain = IIO_G_TO_M_S_2(12000),
1148 				},
1149 			},
1150 		},
1151 		.bdu = {
1152 			.addr = 0x23,
1153 			.mask = 0x80,
1154 		},
1155 		.drdy_irq = {
1156 			.int1 = {
1157 				.addr = 0x22,
1158 				.mask = 0x10,
1159 			},
1160 			.addr_ihl = 0x25,
1161 			.mask_ihl = 0x02,
1162 			.stat_drdy = {
1163 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
1164 				.mask = 0x07,
1165 			},
1166 		},
1167 		.sim = {
1168 			.addr = 0x23,
1169 			.value = BIT(0),
1170 		},
1171 		.multi_read_bit = true,
1172 		.bootime = 2,
1173 	},
1174 };
1175 
1176 /* Default accel DRDY is available on INT1 pin */
1177 static const struct st_sensors_platform_data default_accel_pdata = {
1178 	.drdy_int_pin = 1,
1179 };
1180 
1181 static int st_accel_read_raw(struct iio_dev *indio_dev,
1182 			struct iio_chan_spec const *ch, int *val,
1183 							int *val2, long mask)
1184 {
1185 	int err;
1186 	struct st_sensor_data *adata = iio_priv(indio_dev);
1187 
1188 	switch (mask) {
1189 	case IIO_CHAN_INFO_RAW:
1190 		err = st_sensors_read_info_raw(indio_dev, ch, val);
1191 		if (err < 0)
1192 			goto read_error;
1193 
1194 		return IIO_VAL_INT;
1195 	case IIO_CHAN_INFO_SCALE:
1196 		*val = adata->current_fullscale->gain / 1000000;
1197 		*val2 = adata->current_fullscale->gain % 1000000;
1198 		return IIO_VAL_INT_PLUS_MICRO;
1199 	case IIO_CHAN_INFO_SAMP_FREQ:
1200 		*val = adata->odr;
1201 		return IIO_VAL_INT;
1202 	default:
1203 		return -EINVAL;
1204 	}
1205 
1206 read_error:
1207 	return err;
1208 }
1209 
1210 static int st_accel_write_raw(struct iio_dev *indio_dev,
1211 		struct iio_chan_spec const *chan, int val, int val2, long mask)
1212 {
1213 	switch (mask) {
1214 	case IIO_CHAN_INFO_SCALE: {
1215 		int gain;
1216 
1217 		gain = val * 1000000 + val2;
1218 		return st_sensors_set_fullscale_by_gain(indio_dev, gain);
1219 	}
1220 	case IIO_CHAN_INFO_SAMP_FREQ:
1221 		if (val2)
1222 			return -EINVAL;
1223 
1224 		return st_sensors_set_odr(indio_dev, val);
1225 	default:
1226 		return -EINVAL;
1227 	}
1228 }
1229 
1230 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
1231 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_accel_scale_available);
1232 
1233 static struct attribute *st_accel_attributes[] = {
1234 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
1235 	&iio_dev_attr_in_accel_scale_available.dev_attr.attr,
1236 	NULL,
1237 };
1238 
1239 static const struct attribute_group st_accel_attribute_group = {
1240 	.attrs = st_accel_attributes,
1241 };
1242 
1243 static const struct iio_info accel_info = {
1244 	.attrs = &st_accel_attribute_group,
1245 	.read_raw = &st_accel_read_raw,
1246 	.write_raw = &st_accel_write_raw,
1247 	.debugfs_reg_access = &st_sensors_debugfs_reg_access,
1248 };
1249 
1250 #ifdef CONFIG_IIO_TRIGGER
1251 static const struct iio_trigger_ops st_accel_trigger_ops = {
1252 	.set_trigger_state = ST_ACCEL_TRIGGER_SET_STATE,
1253 	.validate_device = st_sensors_validate_device,
1254 };
1255 #define ST_ACCEL_TRIGGER_OPS (&st_accel_trigger_ops)
1256 #else
1257 #define ST_ACCEL_TRIGGER_OPS NULL
1258 #endif
1259 
1260 #ifdef CONFIG_ACPI
1261 /* Read ST-specific _ONT orientation data from ACPI and generate an
1262  * appropriate mount matrix.
1263  */
1264 static int apply_acpi_orientation(struct iio_dev *indio_dev)
1265 {
1266 	struct st_sensor_data *adata = iio_priv(indio_dev);
1267 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1268 	struct acpi_device *adev;
1269 	union acpi_object *ont;
1270 	union acpi_object *elements;
1271 	acpi_status status;
1272 	int ret = -EINVAL;
1273 	unsigned int val;
1274 	int i, j;
1275 	int final_ont[3][3] = { { 0 }, };
1276 
1277 	/* For some reason, ST's _ONT translation does not apply directly
1278 	 * to the data read from the sensor. Another translation must be
1279 	 * performed first, as described by the matrix below. Perhaps
1280 	 * ST required this specific translation for the first product
1281 	 * where the device was mounted?
1282 	 */
1283 	const int default_ont[3][3] = {
1284 		{  0,  1,  0 },
1285 		{ -1,  0,  0 },
1286 		{  0,  0, -1 },
1287 	};
1288 
1289 
1290 	adev = ACPI_COMPANION(indio_dev->dev.parent);
1291 	if (!adev)
1292 		return 0;
1293 
1294 	/* Read _ONT data, which should be a package of 6 integers. */
1295 	status = acpi_evaluate_object(adev->handle, "_ONT", NULL, &buffer);
1296 	if (status == AE_NOT_FOUND) {
1297 		return 0;
1298 	} else if (ACPI_FAILURE(status)) {
1299 		dev_warn(&indio_dev->dev, "failed to execute _ONT: %d\n",
1300 			 status);
1301 		return status;
1302 	}
1303 
1304 	ont = buffer.pointer;
1305 	if (ont->type != ACPI_TYPE_PACKAGE || ont->package.count != 6)
1306 		goto out;
1307 
1308 	/* The first 3 integers provide axis order information.
1309 	 * e.g. 0 1 2 would indicate normal X,Y,Z ordering.
1310 	 * e.g. 1 0 2 indicates that data arrives in order Y,X,Z.
1311 	 */
1312 	elements = ont->package.elements;
1313 	for (i = 0; i < 3; i++) {
1314 		if (elements[i].type != ACPI_TYPE_INTEGER)
1315 			goto out;
1316 
1317 		val = elements[i].integer.value;
1318 		if (val > 2)
1319 			goto out;
1320 
1321 		/* Avoiding full matrix multiplication, we simply reorder the
1322 		 * columns in the default_ont matrix according to the
1323 		 * ordering provided by _ONT.
1324 		 */
1325 		final_ont[0][i] = default_ont[0][val];
1326 		final_ont[1][i] = default_ont[1][val];
1327 		final_ont[2][i] = default_ont[2][val];
1328 	}
1329 
1330 	/* The final 3 integers provide sign flip information.
1331 	 * 0 means no change, 1 means flip.
1332 	 * e.g. 0 0 1 means that Z data should be sign-flipped.
1333 	 * This is applied after the axis reordering from above.
1334 	 */
1335 	elements += 3;
1336 	for (i = 0; i < 3; i++) {
1337 		if (elements[i].type != ACPI_TYPE_INTEGER)
1338 			goto out;
1339 
1340 		val = elements[i].integer.value;
1341 		if (val != 0 && val != 1)
1342 			goto out;
1343 		if (!val)
1344 			continue;
1345 
1346 		/* Flip the values in the indicated column */
1347 		final_ont[0][i] *= -1;
1348 		final_ont[1][i] *= -1;
1349 		final_ont[2][i] *= -1;
1350 	}
1351 
1352 	/* Convert our integer matrix to a string-based iio_mount_matrix */
1353 	for (i = 0; i < 3; i++) {
1354 		for (j = 0; j < 3; j++) {
1355 			int matrix_val = final_ont[i][j];
1356 			char *str_value;
1357 
1358 			switch (matrix_val) {
1359 			case -1:
1360 				str_value = "-1";
1361 				break;
1362 			case 0:
1363 				str_value = "0";
1364 				break;
1365 			case 1:
1366 				str_value = "1";
1367 				break;
1368 			default:
1369 				goto out;
1370 			}
1371 			adata->mount_matrix.rotation[i * 3 + j] = str_value;
1372 		}
1373 	}
1374 
1375 	ret = 0;
1376 	dev_info(&indio_dev->dev, "computed mount matrix from ACPI\n");
1377 
1378 out:
1379 	kfree(buffer.pointer);
1380 	if (ret)
1381 		dev_dbg(&indio_dev->dev,
1382 			"failed to apply ACPI orientation data: %d\n", ret);
1383 
1384 	return ret;
1385 }
1386 #else /* !CONFIG_ACPI */
1387 static int apply_acpi_orientation(struct iio_dev *indio_dev)
1388 {
1389 	return -EINVAL;
1390 }
1391 #endif
1392 
1393 /*
1394  * st_accel_get_settings() - get sensor settings from device name
1395  * @name: device name buffer reference.
1396  *
1397  * Return: valid reference on success, NULL otherwise.
1398  */
1399 const struct st_sensor_settings *st_accel_get_settings(const char *name)
1400 {
1401 	int index = st_sensors_get_settings_index(name,
1402 					st_accel_sensors_settings,
1403 					ARRAY_SIZE(st_accel_sensors_settings));
1404 	if (index < 0)
1405 		return NULL;
1406 
1407 	return &st_accel_sensors_settings[index];
1408 }
1409 EXPORT_SYMBOL_NS(st_accel_get_settings, IIO_ST_SENSORS);
1410 
1411 int st_accel_common_probe(struct iio_dev *indio_dev)
1412 {
1413 	struct st_sensor_data *adata = iio_priv(indio_dev);
1414 	struct device *parent = indio_dev->dev.parent;
1415 	struct st_sensors_platform_data *pdata = dev_get_platdata(parent);
1416 	int err;
1417 
1418 	indio_dev->modes = INDIO_DIRECT_MODE;
1419 	indio_dev->info = &accel_info;
1420 
1421 	err = st_sensors_verify_id(indio_dev);
1422 	if (err < 0)
1423 		return err;
1424 
1425 	adata->num_data_channels = ST_ACCEL_NUMBER_DATA_CHANNELS;
1426 	indio_dev->channels = adata->sensor_settings->ch;
1427 	indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
1428 
1429 	/*
1430 	 * First try specific ACPI methods to retrieve orientation then try the
1431 	 * generic function.
1432 	 */
1433 	err = apply_acpi_orientation(indio_dev);
1434 	if (err) {
1435 		err = iio_read_mount_matrix(parent, &adata->mount_matrix);
1436 		if (err)
1437 			return err;
1438 	}
1439 
1440 	adata->current_fullscale = &adata->sensor_settings->fs.fs_avl[0];
1441 	adata->odr = adata->sensor_settings->odr.odr_avl[0].hz;
1442 
1443 	if (!pdata)
1444 		pdata = (struct st_sensors_platform_data *)&default_accel_pdata;
1445 
1446 	err = st_sensors_init_sensor(indio_dev, pdata);
1447 	if (err < 0)
1448 		return err;
1449 
1450 	err = st_accel_allocate_ring(indio_dev);
1451 	if (err < 0)
1452 		return err;
1453 
1454 	if (adata->irq > 0) {
1455 		err = st_sensors_allocate_trigger(indio_dev,
1456 						 ST_ACCEL_TRIGGER_OPS);
1457 		if (err < 0)
1458 			return err;
1459 	}
1460 
1461 	return devm_iio_device_register(parent, indio_dev);
1462 }
1463 EXPORT_SYMBOL_NS(st_accel_common_probe, IIO_ST_SENSORS);
1464 
1465 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
1466 MODULE_DESCRIPTION("STMicroelectronics accelerometers driver");
1467 MODULE_LICENSE("GPL v2");
1468 MODULE_IMPORT_NS(IIO_ST_SENSORS);
1469