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