xref: /linux/drivers/iio/accel/st_accel_core.c (revision 48dea9a700c8728cc31a1dd44588b97578de86ee)
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/slab.h>
13 #include <linux/acpi.h>
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/interrupt.h>
17 #include <linux/i2c.h>
18 #include <linux/irq.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/trigger.h>
22 #include <linux/iio/buffer.h>
23 
24 #include <linux/iio/common/st_sensors.h>
25 #include "st_accel.h"
26 
27 #define ST_ACCEL_NUMBER_DATA_CHANNELS		3
28 
29 /* DEFAULT VALUE FOR SENSORS */
30 #define ST_ACCEL_DEFAULT_OUT_X_L_ADDR		0x28
31 #define ST_ACCEL_DEFAULT_OUT_Y_L_ADDR		0x2a
32 #define ST_ACCEL_DEFAULT_OUT_Z_L_ADDR		0x2c
33 
34 /* FULLSCALE */
35 #define ST_ACCEL_FS_AVL_2G			2
36 #define ST_ACCEL_FS_AVL_4G			4
37 #define ST_ACCEL_FS_AVL_6G			6
38 #define ST_ACCEL_FS_AVL_8G			8
39 #define ST_ACCEL_FS_AVL_16G			16
40 #define ST_ACCEL_FS_AVL_100G			100
41 #define ST_ACCEL_FS_AVL_200G			200
42 #define ST_ACCEL_FS_AVL_400G			400
43 
44 static const struct iio_chan_spec st_accel_8bit_channels[] = {
45 	ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
46 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
47 			ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 8, 8,
48 			ST_ACCEL_DEFAULT_OUT_X_L_ADDR+1),
49 	ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
50 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
51 			ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 8, 8,
52 			ST_ACCEL_DEFAULT_OUT_Y_L_ADDR+1),
53 	ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
54 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
55 			ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 8, 8,
56 			ST_ACCEL_DEFAULT_OUT_Z_L_ADDR+1),
57 	IIO_CHAN_SOFT_TIMESTAMP(3)
58 };
59 
60 static const struct iio_chan_spec st_accel_12bit_channels[] = {
61 	ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
62 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
63 			ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 12, 16,
64 			ST_ACCEL_DEFAULT_OUT_X_L_ADDR),
65 	ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
66 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
67 			ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 12, 16,
68 			ST_ACCEL_DEFAULT_OUT_Y_L_ADDR),
69 	ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
70 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
71 			ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 12, 16,
72 			ST_ACCEL_DEFAULT_OUT_Z_L_ADDR),
73 	IIO_CHAN_SOFT_TIMESTAMP(3)
74 };
75 
76 static const struct iio_chan_spec st_accel_16bit_channels[] = {
77 	ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
78 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
79 			ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
80 			ST_ACCEL_DEFAULT_OUT_X_L_ADDR),
81 	ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
82 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
83 			ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
84 			ST_ACCEL_DEFAULT_OUT_Y_L_ADDR),
85 	ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
86 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
87 			ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
88 			ST_ACCEL_DEFAULT_OUT_Z_L_ADDR),
89 	IIO_CHAN_SOFT_TIMESTAMP(3)
90 };
91 
92 static const struct st_sensor_settings st_accel_sensors_settings[] = {
93 	{
94 		.wai = 0x33,
95 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
96 		.sensors_supported = {
97 			[0] = LIS3DH_ACCEL_DEV_NAME,
98 			[1] = LSM303DLHC_ACCEL_DEV_NAME,
99 			[2] = LSM330D_ACCEL_DEV_NAME,
100 			[3] = LSM330DL_ACCEL_DEV_NAME,
101 			[4] = LSM330DLC_ACCEL_DEV_NAME,
102 			[5] = LSM303AGR_ACCEL_DEV_NAME,
103 			[6] = LIS2DH12_ACCEL_DEV_NAME,
104 			[7] = LIS3DE_ACCEL_DEV_NAME,
105 		},
106 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
107 		.odr = {
108 			.addr = 0x20,
109 			.mask = 0xf0,
110 			.odr_avl = {
111 				{ .hz = 1, .value = 0x01, },
112 				{ .hz = 10, .value = 0x02, },
113 				{ .hz = 25, .value = 0x03, },
114 				{ .hz = 50, .value = 0x04, },
115 				{ .hz = 100, .value = 0x05, },
116 				{ .hz = 200, .value = 0x06, },
117 				{ .hz = 400, .value = 0x07, },
118 				{ .hz = 1600, .value = 0x08, },
119 			},
120 		},
121 		.pw = {
122 			.addr = 0x20,
123 			.mask = 0xf0,
124 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
125 		},
126 		.enable_axis = {
127 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
128 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
129 		},
130 		.fs = {
131 			.addr = 0x23,
132 			.mask = 0x30,
133 			.fs_avl = {
134 				[0] = {
135 					.num = ST_ACCEL_FS_AVL_2G,
136 					.value = 0x00,
137 					.gain = IIO_G_TO_M_S_2(1000),
138 				},
139 				[1] = {
140 					.num = ST_ACCEL_FS_AVL_4G,
141 					.value = 0x01,
142 					.gain = IIO_G_TO_M_S_2(2000),
143 				},
144 				[2] = {
145 					.num = ST_ACCEL_FS_AVL_8G,
146 					.value = 0x02,
147 					.gain = IIO_G_TO_M_S_2(4000),
148 				},
149 				[3] = {
150 					.num = ST_ACCEL_FS_AVL_16G,
151 					.value = 0x03,
152 					.gain = IIO_G_TO_M_S_2(12000),
153 				},
154 			},
155 		},
156 		.bdu = {
157 			.addr = 0x23,
158 			.mask = 0x80,
159 		},
160 		.drdy_irq = {
161 			.int1 = {
162 				.addr = 0x22,
163 				.mask = 0x10,
164 			},
165 			.addr_ihl = 0x25,
166 			.mask_ihl = 0x02,
167 			.stat_drdy = {
168 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
169 				.mask = 0x07,
170 			},
171 		},
172 		.sim = {
173 			.addr = 0x23,
174 			.value = BIT(0),
175 		},
176 		.multi_read_bit = true,
177 		.bootime = 2,
178 	},
179 	{
180 		.wai = 0x32,
181 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
182 		.sensors_supported = {
183 			[0] = LIS331DLH_ACCEL_DEV_NAME,
184 			[1] = LSM303DL_ACCEL_DEV_NAME,
185 			[2] = LSM303DLH_ACCEL_DEV_NAME,
186 			[3] = LSM303DLM_ACCEL_DEV_NAME,
187 		},
188 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
189 		.odr = {
190 			.addr = 0x20,
191 			.mask = 0x18,
192 			.odr_avl = {
193 				{ .hz = 50, .value = 0x00, },
194 				{ .hz = 100, .value = 0x01, },
195 				{ .hz = 400, .value = 0x02, },
196 				{ .hz = 1000, .value = 0x03, },
197 			},
198 		},
199 		.pw = {
200 			.addr = 0x20,
201 			.mask = 0xe0,
202 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
203 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
204 		},
205 		.enable_axis = {
206 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
207 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
208 		},
209 		.fs = {
210 			.addr = 0x23,
211 			.mask = 0x30,
212 			.fs_avl = {
213 				[0] = {
214 					.num = ST_ACCEL_FS_AVL_2G,
215 					.value = 0x00,
216 					.gain = IIO_G_TO_M_S_2(1000),
217 				},
218 				[1] = {
219 					.num = ST_ACCEL_FS_AVL_4G,
220 					.value = 0x01,
221 					.gain = IIO_G_TO_M_S_2(2000),
222 				},
223 				[2] = {
224 					.num = ST_ACCEL_FS_AVL_8G,
225 					.value = 0x03,
226 					.gain = IIO_G_TO_M_S_2(3900),
227 				},
228 			},
229 		},
230 		.bdu = {
231 			.addr = 0x23,
232 			.mask = 0x80,
233 		},
234 		.drdy_irq = {
235 			.int1 = {
236 				.addr = 0x22,
237 				.mask = 0x02,
238 				.addr_od = 0x22,
239 				.mask_od = 0x40,
240 			},
241 			.int2 = {
242 				.addr = 0x22,
243 				.mask = 0x10,
244 				.addr_od = 0x22,
245 				.mask_od = 0x40,
246 			},
247 			.addr_ihl = 0x22,
248 			.mask_ihl = 0x80,
249 			.stat_drdy = {
250 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
251 				.mask = 0x07,
252 			},
253 		},
254 		.sim = {
255 			.addr = 0x23,
256 			.value = BIT(0),
257 		},
258 		.multi_read_bit = true,
259 		.bootime = 2,
260 	},
261 	{
262 		.wai = 0x40,
263 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
264 		.sensors_supported = {
265 			[0] = LSM330_ACCEL_DEV_NAME,
266 		},
267 		.ch = (struct iio_chan_spec *)st_accel_16bit_channels,
268 		.odr = {
269 			.addr = 0x20,
270 			.mask = 0xf0,
271 			.odr_avl = {
272 				{ .hz = 3, .value = 0x01, },
273 				{ .hz = 6, .value = 0x02, },
274 				{ .hz = 12, .value = 0x03, },
275 				{ .hz = 25, .value = 0x04, },
276 				{ .hz = 50, .value = 0x05, },
277 				{ .hz = 100, .value = 0x06, },
278 				{ .hz = 200, .value = 0x07, },
279 				{ .hz = 400, .value = 0x08, },
280 				{ .hz = 800, .value = 0x09, },
281 				{ .hz = 1600, .value = 0x0a, },
282 			},
283 		},
284 		.pw = {
285 			.addr = 0x20,
286 			.mask = 0xf0,
287 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
288 		},
289 		.enable_axis = {
290 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
291 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
292 		},
293 		.fs = {
294 			.addr = 0x24,
295 			.mask = 0x38,
296 			.fs_avl = {
297 				[0] = {
298 					.num = ST_ACCEL_FS_AVL_2G,
299 					.value = 0x00,
300 					.gain = IIO_G_TO_M_S_2(61),
301 				},
302 				[1] = {
303 					.num = ST_ACCEL_FS_AVL_4G,
304 					.value = 0x01,
305 					.gain = IIO_G_TO_M_S_2(122),
306 				},
307 				[2] = {
308 					.num = ST_ACCEL_FS_AVL_6G,
309 					.value = 0x02,
310 					.gain = IIO_G_TO_M_S_2(183),
311 				},
312 				[3] = {
313 					.num = ST_ACCEL_FS_AVL_8G,
314 					.value = 0x03,
315 					.gain = IIO_G_TO_M_S_2(244),
316 				},
317 				[4] = {
318 					.num = ST_ACCEL_FS_AVL_16G,
319 					.value = 0x04,
320 					.gain = IIO_G_TO_M_S_2(732),
321 				},
322 			},
323 		},
324 		.bdu = {
325 			.addr = 0x20,
326 			.mask = 0x08,
327 		},
328 		.drdy_irq = {
329 			.int1 = {
330 				.addr = 0x23,
331 				.mask = 0x80,
332 			},
333 			.addr_ihl = 0x23,
334 			.mask_ihl = 0x40,
335 			.stat_drdy = {
336 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
337 				.mask = 0x07,
338 			},
339 			.ig1 = {
340 				.en_addr = 0x23,
341 				.en_mask = 0x08,
342 			},
343 		},
344 		.sim = {
345 			.addr = 0x24,
346 			.value = BIT(0),
347 		},
348 		.multi_read_bit = false,
349 		.bootime = 2,
350 	},
351 	{
352 		.wai = 0x3a,
353 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
354 		.sensors_supported = {
355 			[0] = LIS3LV02DL_ACCEL_DEV_NAME,
356 		},
357 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
358 		.odr = {
359 			.addr = 0x20,
360 			.mask = 0x30, /* DF1 and DF0 */
361 			.odr_avl = {
362 				{ .hz = 40, .value = 0x00, },
363 				{ .hz = 160, .value = 0x01, },
364 				{ .hz = 640, .value = 0x02, },
365 				{ .hz = 2560, .value = 0x03, },
366 			},
367 		},
368 		.pw = {
369 			.addr = 0x20,
370 			.mask = 0xc0,
371 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
372 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
373 		},
374 		.enable_axis = {
375 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
376 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
377 		},
378 		.fs = {
379 			.addr = 0x21,
380 			.mask = 0x80,
381 			.fs_avl = {
382 				[0] = {
383 					.num = ST_ACCEL_FS_AVL_2G,
384 					.value = 0x00,
385 					.gain = IIO_G_TO_M_S_2(1000),
386 				},
387 				[1] = {
388 					.num = ST_ACCEL_FS_AVL_6G,
389 					.value = 0x01,
390 					.gain = IIO_G_TO_M_S_2(3000),
391 				},
392 			},
393 		},
394 		.bdu = {
395 			.addr = 0x21,
396 			.mask = 0x40,
397 		},
398 		/*
399 		 * Data Alignment Setting - needs to be set to get
400 		 * left-justified data like all other sensors.
401 		 */
402 		.das = {
403 			.addr = 0x21,
404 			.mask = 0x01,
405 		},
406 		.drdy_irq = {
407 			.int1 = {
408 				.addr = 0x21,
409 				.mask = 0x04,
410 			},
411 			.stat_drdy = {
412 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
413 				.mask = 0x07,
414 			},
415 		},
416 		.sim = {
417 			.addr = 0x21,
418 			.value = BIT(1),
419 		},
420 		.multi_read_bit = true,
421 		.bootime = 2, /* guess */
422 	},
423 	{
424 		.wai = 0x3b,
425 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
426 		.sensors_supported = {
427 			[0] = LIS331DL_ACCEL_DEV_NAME,
428 		},
429 		.ch = (struct iio_chan_spec *)st_accel_8bit_channels,
430 		.odr = {
431 			.addr = 0x20,
432 			.mask = 0x80,
433 			.odr_avl = {
434 				{ .hz = 100, .value = 0x00, },
435 				{ .hz = 400, .value = 0x01, },
436 			},
437 		},
438 		.pw = {
439 			.addr = 0x20,
440 			.mask = 0x40,
441 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
442 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
443 		},
444 		.enable_axis = {
445 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
446 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
447 		},
448 		.fs = {
449 			.addr = 0x20,
450 			.mask = 0x20,
451 			/*
452 			 * TODO: check these resulting gain settings, these are
453 			 * not in the datsheet
454 			 */
455 			.fs_avl = {
456 				[0] = {
457 					.num = ST_ACCEL_FS_AVL_2G,
458 					.value = 0x00,
459 					.gain = IIO_G_TO_M_S_2(18000),
460 				},
461 				[1] = {
462 					.num = ST_ACCEL_FS_AVL_8G,
463 					.value = 0x01,
464 					.gain = IIO_G_TO_M_S_2(72000),
465 				},
466 			},
467 		},
468 		.drdy_irq = {
469 			.int1 = {
470 				.addr = 0x22,
471 				.mask = 0x04,
472 				.addr_od = 0x22,
473 				.mask_od = 0x40,
474 			},
475 			.int2 = {
476 				.addr = 0x22,
477 				.mask = 0x20,
478 				.addr_od = 0x22,
479 				.mask_od = 0x40,
480 			},
481 			.addr_ihl = 0x22,
482 			.mask_ihl = 0x80,
483 			.stat_drdy = {
484 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
485 				.mask = 0x07,
486 			},
487 		},
488 		.sim = {
489 			.addr = 0x21,
490 			.value = BIT(7),
491 		},
492 		.multi_read_bit = false,
493 		.bootime = 2, /* guess */
494 	},
495 	{
496 		.wai = 0x32,
497 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
498 		.sensors_supported = {
499 			[0] = H3LIS331DL_ACCEL_DEV_NAME,
500 		},
501 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
502 		.odr = {
503 			.addr = 0x20,
504 			.mask = 0x18,
505 			.odr_avl = {
506 				{ .hz = 50, .value = 0x00, },
507 				{ .hz = 100, .value = 0x01, },
508 				{ .hz = 400, .value = 0x02, },
509 				{ .hz = 1000, .value = 0x03, },
510 			},
511 		},
512 		.pw = {
513 			.addr = 0x20,
514 			.mask = 0x20,
515 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
516 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
517 		},
518 		.enable_axis = {
519 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
520 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
521 		},
522 		.fs = {
523 			.addr = 0x23,
524 			.mask = 0x30,
525 			.fs_avl = {
526 				[0] = {
527 					.num = ST_ACCEL_FS_AVL_100G,
528 					.value = 0x00,
529 					.gain = IIO_G_TO_M_S_2(49000),
530 				},
531 				[1] = {
532 					.num = ST_ACCEL_FS_AVL_200G,
533 					.value = 0x01,
534 					.gain = IIO_G_TO_M_S_2(98000),
535 				},
536 				[2] = {
537 					.num = ST_ACCEL_FS_AVL_400G,
538 					.value = 0x03,
539 					.gain = IIO_G_TO_M_S_2(195000),
540 				},
541 			},
542 		},
543 		.bdu = {
544 			.addr = 0x23,
545 			.mask = 0x80,
546 		},
547 		.drdy_irq = {
548 			.int1 = {
549 				.addr = 0x22,
550 				.mask = 0x02,
551 			},
552 			.int2 = {
553 				.addr = 0x22,
554 				.mask = 0x10,
555 			},
556 			.addr_ihl = 0x22,
557 			.mask_ihl = 0x80,
558 		},
559 		.sim = {
560 			.addr = 0x23,
561 			.value = BIT(0),
562 		},
563 		.multi_read_bit = true,
564 		.bootime = 2,
565 	},
566 	{
567 		/* No WAI register present */
568 		.sensors_supported = {
569 			[0] = LIS3L02DQ_ACCEL_DEV_NAME,
570 		},
571 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
572 		.odr = {
573 			.addr = 0x20,
574 			.mask = 0x30,
575 			.odr_avl = {
576 				{ .hz = 280, .value = 0x00, },
577 				{ .hz = 560, .value = 0x01, },
578 				{ .hz = 1120, .value = 0x02, },
579 				{ .hz = 4480, .value = 0x03, },
580 			},
581 		},
582 		.pw = {
583 			.addr = 0x20,
584 			.mask = 0xc0,
585 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
586 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
587 		},
588 		.enable_axis = {
589 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
590 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
591 		},
592 		.fs = {
593 			.fs_avl = {
594 				[0] = {
595 					.num = ST_ACCEL_FS_AVL_2G,
596 					.gain = IIO_G_TO_M_S_2(488),
597 				},
598 			},
599 		},
600 		/*
601 		 * The part has a BDU bit but if set the data is never
602 		 * updated so don't set it.
603 		 */
604 		.bdu = {
605 		},
606 		.drdy_irq = {
607 			.int1 = {
608 				.addr = 0x21,
609 				.mask = 0x04,
610 			},
611 			.stat_drdy = {
612 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
613 				.mask = 0x07,
614 			},
615 		},
616 		.sim = {
617 			.addr = 0x21,
618 			.value = BIT(1),
619 		},
620 		.multi_read_bit = false,
621 		.bootime = 2,
622 	},
623 	{
624 		.wai = 0x33,
625 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
626 		.sensors_supported = {
627 			[0] = LNG2DM_ACCEL_DEV_NAME,
628 		},
629 		.ch = (struct iio_chan_spec *)st_accel_8bit_channels,
630 		.odr = {
631 			.addr = 0x20,
632 			.mask = 0xf0,
633 			.odr_avl = {
634 				{ .hz = 1, .value = 0x01, },
635 				{ .hz = 10, .value = 0x02, },
636 				{ .hz = 25, .value = 0x03, },
637 				{ .hz = 50, .value = 0x04, },
638 				{ .hz = 100, .value = 0x05, },
639 				{ .hz = 200, .value = 0x06, },
640 				{ .hz = 400, .value = 0x07, },
641 				{ .hz = 1600, .value = 0x08, },
642 			},
643 		},
644 		.pw = {
645 			.addr = 0x20,
646 			.mask = 0xf0,
647 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
648 		},
649 		.enable_axis = {
650 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
651 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
652 		},
653 		.fs = {
654 			.addr = 0x23,
655 			.mask = 0x30,
656 			.fs_avl = {
657 				[0] = {
658 					.num = ST_ACCEL_FS_AVL_2G,
659 					.value = 0x00,
660 					.gain = IIO_G_TO_M_S_2(15600),
661 				},
662 				[1] = {
663 					.num = ST_ACCEL_FS_AVL_4G,
664 					.value = 0x01,
665 					.gain = IIO_G_TO_M_S_2(31200),
666 				},
667 				[2] = {
668 					.num = ST_ACCEL_FS_AVL_8G,
669 					.value = 0x02,
670 					.gain = IIO_G_TO_M_S_2(62500),
671 				},
672 				[3] = {
673 					.num = ST_ACCEL_FS_AVL_16G,
674 					.value = 0x03,
675 					.gain = IIO_G_TO_M_S_2(187500),
676 				},
677 			},
678 		},
679 		.drdy_irq = {
680 			.int1 = {
681 				.addr = 0x22,
682 				.mask = 0x10,
683 			},
684 			.addr_ihl = 0x25,
685 			.mask_ihl = 0x02,
686 			.stat_drdy = {
687 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
688 				.mask = 0x07,
689 			},
690 		},
691 		.sim = {
692 			.addr = 0x23,
693 			.value = BIT(0),
694 		},
695 		.multi_read_bit = true,
696 		.bootime = 2,
697 	},
698 	{
699 		.wai = 0x44,
700 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
701 		.sensors_supported = {
702 			[0] = LIS2DW12_ACCEL_DEV_NAME,
703 		},
704 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
705 		.odr = {
706 			.addr = 0x20,
707 			.mask = 0xf0,
708 			.odr_avl = {
709 				{ .hz = 1, .value = 0x01, },
710 				{ .hz = 12, .value = 0x02, },
711 				{ .hz = 25, .value = 0x03, },
712 				{ .hz = 50, .value = 0x04, },
713 				{ .hz = 100, .value = 0x05, },
714 				{ .hz = 200, .value = 0x06, },
715 			},
716 		},
717 		.pw = {
718 			.addr = 0x20,
719 			.mask = 0xf0,
720 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
721 		},
722 		.fs = {
723 			.addr = 0x25,
724 			.mask = 0x30,
725 			.fs_avl = {
726 				[0] = {
727 					.num = ST_ACCEL_FS_AVL_2G,
728 					.value = 0x00,
729 					.gain = IIO_G_TO_M_S_2(976),
730 				},
731 				[1] = {
732 					.num = ST_ACCEL_FS_AVL_4G,
733 					.value = 0x01,
734 					.gain = IIO_G_TO_M_S_2(1952),
735 				},
736 				[2] = {
737 					.num = ST_ACCEL_FS_AVL_8G,
738 					.value = 0x02,
739 					.gain = IIO_G_TO_M_S_2(3904),
740 				},
741 				[3] = {
742 					.num = ST_ACCEL_FS_AVL_16G,
743 					.value = 0x03,
744 					.gain = IIO_G_TO_M_S_2(7808),
745 				},
746 			},
747 		},
748 		.bdu = {
749 			.addr = 0x21,
750 			.mask = 0x08,
751 		},
752 		.drdy_irq = {
753 			.int1 = {
754 				.addr = 0x23,
755 				.mask = 0x01,
756 				.addr_od = 0x22,
757 				.mask_od = 0x20,
758 			},
759 			.int2 = {
760 				.addr = 0x24,
761 				.mask = 0x01,
762 				.addr_od = 0x22,
763 				.mask_od = 0x20,
764 			},
765 			.addr_ihl = 0x22,
766 			.mask_ihl = 0x08,
767 			.stat_drdy = {
768 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
769 				.mask = 0x01,
770 			},
771 		},
772 		.sim = {
773 			.addr = 0x21,
774 			.value = BIT(0),
775 		},
776 		.multi_read_bit = false,
777 		.bootime = 2,
778 	},
779 	{
780 		.wai = 0x11,
781 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
782 		.sensors_supported = {
783 			[0] = LIS3DHH_ACCEL_DEV_NAME,
784 		},
785 		.ch = (struct iio_chan_spec *)st_accel_16bit_channels,
786 		.odr = {
787 			/* just ODR = 1100Hz available */
788 			.odr_avl = {
789 				{ .hz = 1100, .value = 0x00, },
790 			},
791 		},
792 		.pw = {
793 			.addr = 0x20,
794 			.mask = 0x80,
795 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
796 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
797 		},
798 		.fs = {
799 			.fs_avl = {
800 				[0] = {
801 					.num = ST_ACCEL_FS_AVL_2G,
802 					.gain = IIO_G_TO_M_S_2(76),
803 				},
804 			},
805 		},
806 		.bdu = {
807 			.addr = 0x20,
808 			.mask = 0x01,
809 		},
810 		.drdy_irq = {
811 			.int1 = {
812 				.addr = 0x21,
813 				.mask = 0x80,
814 				.addr_od = 0x23,
815 				.mask_od = 0x04,
816 			},
817 			.int2 = {
818 				.addr = 0x22,
819 				.mask = 0x80,
820 				.addr_od = 0x23,
821 				.mask_od = 0x08,
822 			},
823 			.stat_drdy = {
824 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
825 				.mask = 0x07,
826 			},
827 		},
828 		.multi_read_bit = false,
829 		.bootime = 2,
830 	},
831 	{
832 		.wai = 0x33,
833 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
834 		.sensors_supported = {
835 			[0] = LIS2DE12_ACCEL_DEV_NAME,
836 		},
837 		.ch = (struct iio_chan_spec *)st_accel_8bit_channels,
838 		.odr = {
839 			.addr = 0x20,
840 			.mask = 0xf0,
841 			.odr_avl = {
842 				{ .hz = 1, .value = 0x01, },
843 				{ .hz = 10, .value = 0x02, },
844 				{ .hz = 25, .value = 0x03, },
845 				{ .hz = 50, .value = 0x04, },
846 				{ .hz = 100, .value = 0x05, },
847 				{ .hz = 200, .value = 0x06, },
848 				{ .hz = 400, .value = 0x07, },
849 				{ .hz = 1620, .value = 0x08, },
850 				{ .hz = 5376, .value = 0x09, },
851 			},
852 		},
853 		.pw = {
854 			.addr = 0x20,
855 			.mask = 0xf0,
856 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
857 		},
858 		.enable_axis = {
859 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
860 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
861 		},
862 		.fs = {
863 			.addr = 0x23,
864 			.mask = 0x30,
865 			.fs_avl = {
866 				[0] = {
867 					.num = ST_ACCEL_FS_AVL_2G,
868 					.value = 0x00,
869 					.gain = IIO_G_TO_M_S_2(15600),
870 				},
871 				[1] = {
872 					.num = ST_ACCEL_FS_AVL_4G,
873 					.value = 0x01,
874 					.gain = IIO_G_TO_M_S_2(31200),
875 				},
876 				[2] = {
877 					.num = ST_ACCEL_FS_AVL_8G,
878 					.value = 0x02,
879 					.gain = IIO_G_TO_M_S_2(62500),
880 				},
881 				[3] = {
882 					.num = ST_ACCEL_FS_AVL_16G,
883 					.value = 0x03,
884 					.gain = IIO_G_TO_M_S_2(187500),
885 				},
886 			},
887 		},
888 		.drdy_irq = {
889 			.int1 = {
890 				.addr = 0x22,
891 				.mask = 0x10,
892 			},
893 			.addr_ihl = 0x25,
894 			.mask_ihl = 0x02,
895 			.stat_drdy = {
896 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
897 				.mask = 0x07,
898 			},
899 		},
900 		.sim = {
901 			.addr = 0x23,
902 			.value = BIT(0),
903 		},
904 		.multi_read_bit = true,
905 		.bootime = 2,
906 	},
907 	{
908 		.wai = 0x41,
909 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
910 		.sensors_supported = {
911 			[0] = LIS2HH12_ACCEL_DEV_NAME,
912 		},
913 		.ch = (struct iio_chan_spec *)st_accel_16bit_channels,
914 		.odr = {
915 			.addr = 0x20,
916 			.mask = 0x70,
917 			.odr_avl = {
918 				{ .hz = 10, .value = 0x01, },
919 				{ .hz = 50, .value = 0x02, },
920 				{ .hz = 100, .value = 0x03, },
921 				{ .hz = 200, .value = 0x04, },
922 				{ .hz = 400, .value = 0x05, },
923 				{ .hz = 800, .value = 0x06, },
924 			},
925 		},
926 		.pw = {
927 			.addr = 0x20,
928 			.mask = 0x70,
929 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
930 		},
931 		.enable_axis = {
932 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
933 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
934 		},
935 		.fs = {
936 			.addr = 0x23,
937 			.mask = 0x30,
938 			.fs_avl = {
939 				[0] = {
940 					.num = ST_ACCEL_FS_AVL_2G,
941 					.value = 0x00,
942 					.gain = IIO_G_TO_M_S_2(61),
943 				},
944 				[1] = {
945 					.num = ST_ACCEL_FS_AVL_4G,
946 					.value = 0x02,
947 					.gain = IIO_G_TO_M_S_2(122),
948 				},
949 				[2] = {
950 					.num = ST_ACCEL_FS_AVL_8G,
951 					.value = 0x03,
952 					.gain = IIO_G_TO_M_S_2(244),
953 				},
954 			},
955 		},
956 		.bdu = {
957 			.addr = 0x20,
958 			.mask = 0x08,
959 		},
960 		.drdy_irq = {
961 			.int1 = {
962 				.addr = 0x22,
963 				.mask = 0x01,
964 			},
965 			.int2 = {
966 				.addr = 0x25,
967 				.mask = 0x01,
968 			},
969 			.addr_ihl = 0x24,
970 			.mask_ihl = 0x02,
971 			.stat_drdy = {
972 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
973 				.mask = 0x07,
974 			},
975 		},
976 		.sim = {
977 			.addr = 0x23,
978 			.value = BIT(0),
979 		},
980 		.multi_read_bit = true,
981 		.bootime = 2,
982 	},
983 
984 };
985 
986 static int st_accel_read_raw(struct iio_dev *indio_dev,
987 			struct iio_chan_spec const *ch, int *val,
988 							int *val2, long mask)
989 {
990 	int err;
991 	struct st_sensor_data *adata = iio_priv(indio_dev);
992 
993 	switch (mask) {
994 	case IIO_CHAN_INFO_RAW:
995 		err = st_sensors_read_info_raw(indio_dev, ch, val);
996 		if (err < 0)
997 			goto read_error;
998 
999 		return IIO_VAL_INT;
1000 	case IIO_CHAN_INFO_SCALE:
1001 		*val = adata->current_fullscale->gain / 1000000;
1002 		*val2 = adata->current_fullscale->gain % 1000000;
1003 		return IIO_VAL_INT_PLUS_MICRO;
1004 	case IIO_CHAN_INFO_SAMP_FREQ:
1005 		*val = adata->odr;
1006 		return IIO_VAL_INT;
1007 	default:
1008 		return -EINVAL;
1009 	}
1010 
1011 read_error:
1012 	return err;
1013 }
1014 
1015 static int st_accel_write_raw(struct iio_dev *indio_dev,
1016 		struct iio_chan_spec const *chan, int val, int val2, long mask)
1017 {
1018 	int err;
1019 
1020 	switch (mask) {
1021 	case IIO_CHAN_INFO_SCALE: {
1022 		int gain;
1023 
1024 		gain = val * 1000000 + val2;
1025 		err = st_sensors_set_fullscale_by_gain(indio_dev, gain);
1026 		break;
1027 	}
1028 	case IIO_CHAN_INFO_SAMP_FREQ:
1029 		if (val2)
1030 			return -EINVAL;
1031 		mutex_lock(&indio_dev->mlock);
1032 		err = st_sensors_set_odr(indio_dev, val);
1033 		mutex_unlock(&indio_dev->mlock);
1034 		return err;
1035 	default:
1036 		return -EINVAL;
1037 	}
1038 
1039 	return err;
1040 }
1041 
1042 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
1043 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_accel_scale_available);
1044 
1045 static struct attribute *st_accel_attributes[] = {
1046 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
1047 	&iio_dev_attr_in_accel_scale_available.dev_attr.attr,
1048 	NULL,
1049 };
1050 
1051 static const struct attribute_group st_accel_attribute_group = {
1052 	.attrs = st_accel_attributes,
1053 };
1054 
1055 static const struct iio_info accel_info = {
1056 	.attrs = &st_accel_attribute_group,
1057 	.read_raw = &st_accel_read_raw,
1058 	.write_raw = &st_accel_write_raw,
1059 	.debugfs_reg_access = &st_sensors_debugfs_reg_access,
1060 };
1061 
1062 #ifdef CONFIG_IIO_TRIGGER
1063 static const struct iio_trigger_ops st_accel_trigger_ops = {
1064 	.set_trigger_state = ST_ACCEL_TRIGGER_SET_STATE,
1065 	.validate_device = st_sensors_validate_device,
1066 };
1067 #define ST_ACCEL_TRIGGER_OPS (&st_accel_trigger_ops)
1068 #else
1069 #define ST_ACCEL_TRIGGER_OPS NULL
1070 #endif
1071 
1072 #ifdef CONFIG_ACPI
1073 static const struct iio_mount_matrix *
1074 get_mount_matrix(const struct iio_dev *indio_dev,
1075 		 const struct iio_chan_spec *chan)
1076 {
1077 	struct st_sensor_data *adata = iio_priv(indio_dev);
1078 
1079 	return adata->mount_matrix;
1080 }
1081 
1082 static const struct iio_chan_spec_ext_info mount_matrix_ext_info[] = {
1083 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, get_mount_matrix),
1084 	{ },
1085 };
1086 
1087 /* Read ST-specific _ONT orientation data from ACPI and generate an
1088  * appropriate mount matrix.
1089  */
1090 static int apply_acpi_orientation(struct iio_dev *indio_dev,
1091 				  struct iio_chan_spec *channels)
1092 {
1093 	struct st_sensor_data *adata = iio_priv(indio_dev);
1094 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1095 	struct acpi_device *adev;
1096 	union acpi_object *ont;
1097 	union acpi_object *elements;
1098 	acpi_status status;
1099 	int ret = -EINVAL;
1100 	unsigned int val;
1101 	int i, j;
1102 	int final_ont[3][3] = { { 0 }, };
1103 
1104 	/* For some reason, ST's _ONT translation does not apply directly
1105 	 * to the data read from the sensor. Another translation must be
1106 	 * performed first, as described by the matrix below. Perhaps
1107 	 * ST required this specific translation for the first product
1108 	 * where the device was mounted?
1109 	 */
1110 	const int default_ont[3][3] = {
1111 		{  0,  1,  0 },
1112 		{ -1,  0,  0 },
1113 		{  0,  0, -1 },
1114 	};
1115 
1116 
1117 	adev = ACPI_COMPANION(adata->dev);
1118 	if (!adev)
1119 		return 0;
1120 
1121 	/* Read _ONT data, which should be a package of 6 integers. */
1122 	status = acpi_evaluate_object(adev->handle, "_ONT", NULL, &buffer);
1123 	if (status == AE_NOT_FOUND) {
1124 		return 0;
1125 	} else if (ACPI_FAILURE(status)) {
1126 		dev_warn(&indio_dev->dev, "failed to execute _ONT: %d\n",
1127 			 status);
1128 		return status;
1129 	}
1130 
1131 	ont = buffer.pointer;
1132 	if (ont->type != ACPI_TYPE_PACKAGE || ont->package.count != 6)
1133 		goto out;
1134 
1135 	/* The first 3 integers provide axis order information.
1136 	 * e.g. 0 1 2 would indicate normal X,Y,Z ordering.
1137 	 * e.g. 1 0 2 indicates that data arrives in order Y,X,Z.
1138 	 */
1139 	elements = ont->package.elements;
1140 	for (i = 0; i < 3; i++) {
1141 		if (elements[i].type != ACPI_TYPE_INTEGER)
1142 			goto out;
1143 
1144 		val = elements[i].integer.value;
1145 		if (val > 2)
1146 			goto out;
1147 
1148 		/* Avoiding full matrix multiplication, we simply reorder the
1149 		 * columns in the default_ont matrix according to the
1150 		 * ordering provided by _ONT.
1151 		 */
1152 		final_ont[0][i] = default_ont[0][val];
1153 		final_ont[1][i] = default_ont[1][val];
1154 		final_ont[2][i] = default_ont[2][val];
1155 	}
1156 
1157 	/* The final 3 integers provide sign flip information.
1158 	 * 0 means no change, 1 means flip.
1159 	 * e.g. 0 0 1 means that Z data should be sign-flipped.
1160 	 * This is applied after the axis reordering from above.
1161 	 */
1162 	elements += 3;
1163 	for (i = 0; i < 3; i++) {
1164 		if (elements[i].type != ACPI_TYPE_INTEGER)
1165 			goto out;
1166 
1167 		val = elements[i].integer.value;
1168 		if (val != 0 && val != 1)
1169 			goto out;
1170 		if (!val)
1171 			continue;
1172 
1173 		/* Flip the values in the indicated column */
1174 		final_ont[0][i] *= -1;
1175 		final_ont[1][i] *= -1;
1176 		final_ont[2][i] *= -1;
1177 	}
1178 
1179 	/* Convert our integer matrix to a string-based iio_mount_matrix */
1180 	adata->mount_matrix = devm_kmalloc(&indio_dev->dev,
1181 					   sizeof(*adata->mount_matrix),
1182 					   GFP_KERNEL);
1183 	if (!adata->mount_matrix) {
1184 		ret = -ENOMEM;
1185 		goto out;
1186 	}
1187 
1188 	for (i = 0; i < 3; i++) {
1189 		for (j = 0; j < 3; j++) {
1190 			int matrix_val = final_ont[i][j];
1191 			char *str_value;
1192 
1193 			switch (matrix_val) {
1194 			case -1:
1195 				str_value = "-1";
1196 				break;
1197 			case 0:
1198 				str_value = "0";
1199 				break;
1200 			case 1:
1201 				str_value = "1";
1202 				break;
1203 			default:
1204 				goto out;
1205 			}
1206 			adata->mount_matrix->rotation[i * 3 + j] = str_value;
1207 		}
1208 	}
1209 
1210 	/* Expose the mount matrix via ext_info */
1211 	for (i = 0; i < indio_dev->num_channels; i++)
1212 		channels[i].ext_info = mount_matrix_ext_info;
1213 
1214 	ret = 0;
1215 	dev_info(&indio_dev->dev, "computed mount matrix from ACPI\n");
1216 
1217 out:
1218 	kfree(buffer.pointer);
1219 	return ret;
1220 }
1221 #else /* !CONFIG_ACPI */
1222 static int apply_acpi_orientation(struct iio_dev *indio_dev,
1223 				  struct iio_chan_spec *channels)
1224 {
1225 	return 0;
1226 }
1227 #endif
1228 
1229 /*
1230  * st_accel_get_settings() - get sensor settings from device name
1231  * @name: device name buffer reference.
1232  *
1233  * Return: valid reference on success, NULL otherwise.
1234  */
1235 const struct st_sensor_settings *st_accel_get_settings(const char *name)
1236 {
1237 	int index = st_sensors_get_settings_index(name,
1238 					st_accel_sensors_settings,
1239 					ARRAY_SIZE(st_accel_sensors_settings));
1240 	if (index < 0)
1241 		return NULL;
1242 
1243 	return &st_accel_sensors_settings[index];
1244 }
1245 EXPORT_SYMBOL(st_accel_get_settings);
1246 
1247 int st_accel_common_probe(struct iio_dev *indio_dev)
1248 {
1249 	struct st_sensor_data *adata = iio_priv(indio_dev);
1250 	struct st_sensors_platform_data *pdata = dev_get_platdata(adata->dev);
1251 	struct iio_chan_spec *channels;
1252 	size_t channels_size;
1253 	int err;
1254 
1255 	indio_dev->modes = INDIO_DIRECT_MODE;
1256 	indio_dev->info = &accel_info;
1257 
1258 	err = st_sensors_power_enable(indio_dev);
1259 	if (err)
1260 		return err;
1261 
1262 	err = st_sensors_verify_id(indio_dev);
1263 	if (err < 0)
1264 		goto st_accel_power_off;
1265 
1266 	adata->num_data_channels = ST_ACCEL_NUMBER_DATA_CHANNELS;
1267 	indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
1268 
1269 	channels_size = indio_dev->num_channels * sizeof(struct iio_chan_spec);
1270 	channels = devm_kmemdup(&indio_dev->dev,
1271 				adata->sensor_settings->ch,
1272 				channels_size, GFP_KERNEL);
1273 	if (!channels) {
1274 		err = -ENOMEM;
1275 		goto st_accel_power_off;
1276 	}
1277 
1278 	if (apply_acpi_orientation(indio_dev, channels))
1279 		dev_warn(&indio_dev->dev,
1280 			 "failed to apply ACPI orientation data: %d\n", err);
1281 
1282 	indio_dev->channels = channels;
1283 	adata->current_fullscale = &adata->sensor_settings->fs.fs_avl[0];
1284 	adata->odr = adata->sensor_settings->odr.odr_avl[0].hz;
1285 
1286 	if (!pdata)
1287 		pdata = (struct st_sensors_platform_data *)&default_accel_pdata;
1288 
1289 	err = st_sensors_init_sensor(indio_dev, pdata);
1290 	if (err < 0)
1291 		goto st_accel_power_off;
1292 
1293 	err = st_accel_allocate_ring(indio_dev);
1294 	if (err < 0)
1295 		goto st_accel_power_off;
1296 
1297 	if (adata->irq > 0) {
1298 		err = st_sensors_allocate_trigger(indio_dev,
1299 						 ST_ACCEL_TRIGGER_OPS);
1300 		if (err < 0)
1301 			goto st_accel_probe_trigger_error;
1302 	}
1303 
1304 	err = iio_device_register(indio_dev);
1305 	if (err)
1306 		goto st_accel_device_register_error;
1307 
1308 	dev_info(&indio_dev->dev, "registered accelerometer %s\n",
1309 		 indio_dev->name);
1310 
1311 	return 0;
1312 
1313 st_accel_device_register_error:
1314 	if (adata->irq > 0)
1315 		st_sensors_deallocate_trigger(indio_dev);
1316 st_accel_probe_trigger_error:
1317 	st_accel_deallocate_ring(indio_dev);
1318 st_accel_power_off:
1319 	st_sensors_power_disable(indio_dev);
1320 
1321 	return err;
1322 }
1323 EXPORT_SYMBOL(st_accel_common_probe);
1324 
1325 void st_accel_common_remove(struct iio_dev *indio_dev)
1326 {
1327 	struct st_sensor_data *adata = iio_priv(indio_dev);
1328 
1329 	st_sensors_power_disable(indio_dev);
1330 
1331 	iio_device_unregister(indio_dev);
1332 	if (adata->irq > 0)
1333 		st_sensors_deallocate_trigger(indio_dev);
1334 
1335 	st_accel_deallocate_ring(indio_dev);
1336 }
1337 EXPORT_SYMBOL(st_accel_common_remove);
1338 
1339 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
1340 MODULE_DESCRIPTION("STMicroelectronics accelerometers driver");
1341 MODULE_LICENSE("GPL v2");
1342