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