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