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