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