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/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 		},
107 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
108 		.odr = {
109 			.addr = 0x20,
110 			.mask = 0xf0,
111 			.odr_avl = {
112 				{ .hz = 1, .value = 0x01, },
113 				{ .hz = 10, .value = 0x02, },
114 				{ .hz = 25, .value = 0x03, },
115 				{ .hz = 50, .value = 0x04, },
116 				{ .hz = 100, .value = 0x05, },
117 				{ .hz = 200, .value = 0x06, },
118 				{ .hz = 400, .value = 0x07, },
119 				{ .hz = 1600, .value = 0x08, },
120 			},
121 		},
122 		.pw = {
123 			.addr = 0x20,
124 			.mask = 0xf0,
125 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
126 		},
127 		.enable_axis = {
128 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
129 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
130 		},
131 		.fs = {
132 			.addr = 0x23,
133 			.mask = 0x30,
134 			.fs_avl = {
135 				[0] = {
136 					.num = ST_ACCEL_FS_AVL_2G,
137 					.value = 0x00,
138 					.gain = IIO_G_TO_M_S_2(1000),
139 				},
140 				[1] = {
141 					.num = ST_ACCEL_FS_AVL_4G,
142 					.value = 0x01,
143 					.gain = IIO_G_TO_M_S_2(2000),
144 				},
145 				[2] = {
146 					.num = ST_ACCEL_FS_AVL_8G,
147 					.value = 0x02,
148 					.gain = IIO_G_TO_M_S_2(4000),
149 				},
150 				[3] = {
151 					.num = ST_ACCEL_FS_AVL_16G,
152 					.value = 0x03,
153 					.gain = IIO_G_TO_M_S_2(12000),
154 				},
155 			},
156 		},
157 		.bdu = {
158 			.addr = 0x23,
159 			.mask = 0x80,
160 		},
161 		.drdy_irq = {
162 			.int1 = {
163 				.addr = 0x22,
164 				.mask = 0x10,
165 			},
166 			.addr_ihl = 0x25,
167 			.mask_ihl = 0x02,
168 			.stat_drdy = {
169 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
170 				.mask = 0x07,
171 			},
172 		},
173 		.sim = {
174 			.addr = 0x23,
175 			.value = BIT(0),
176 		},
177 		.multi_read_bit = true,
178 		.bootime = 2,
179 	},
180 	{
181 		.wai = 0x32,
182 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
183 		.sensors_supported = {
184 			[0] = LIS331DLH_ACCEL_DEV_NAME,
185 			[1] = LSM303DL_ACCEL_DEV_NAME,
186 			[2] = LSM303DLH_ACCEL_DEV_NAME,
187 			[3] = LSM303DLM_ACCEL_DEV_NAME,
188 		},
189 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
190 		.odr = {
191 			.addr = 0x20,
192 			.mask = 0x18,
193 			.odr_avl = {
194 				{ .hz = 50, .value = 0x00, },
195 				{ .hz = 100, .value = 0x01, },
196 				{ .hz = 400, .value = 0x02, },
197 				{ .hz = 1000, .value = 0x03, },
198 			},
199 		},
200 		.pw = {
201 			.addr = 0x20,
202 			.mask = 0xe0,
203 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
204 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
205 		},
206 		.enable_axis = {
207 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
208 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
209 		},
210 		.fs = {
211 			.addr = 0x23,
212 			.mask = 0x30,
213 			.fs_avl = {
214 				[0] = {
215 					.num = ST_ACCEL_FS_AVL_2G,
216 					.value = 0x00,
217 					.gain = IIO_G_TO_M_S_2(1000),
218 				},
219 				[1] = {
220 					.num = ST_ACCEL_FS_AVL_4G,
221 					.value = 0x01,
222 					.gain = IIO_G_TO_M_S_2(2000),
223 				},
224 				[2] = {
225 					.num = ST_ACCEL_FS_AVL_8G,
226 					.value = 0x03,
227 					.gain = IIO_G_TO_M_S_2(3900),
228 				},
229 			},
230 		},
231 		.bdu = {
232 			.addr = 0x23,
233 			.mask = 0x80,
234 		},
235 		.drdy_irq = {
236 			.int1 = {
237 				.addr = 0x22,
238 				.mask = 0x02,
239 				.addr_od = 0x22,
240 				.mask_od = 0x40,
241 			},
242 			.int2 = {
243 				.addr = 0x22,
244 				.mask = 0x10,
245 				.addr_od = 0x22,
246 				.mask_od = 0x40,
247 			},
248 			.addr_ihl = 0x22,
249 			.mask_ihl = 0x80,
250 			.stat_drdy = {
251 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
252 				.mask = 0x07,
253 			},
254 		},
255 		.sim = {
256 			.addr = 0x23,
257 			.value = BIT(0),
258 		},
259 		.multi_read_bit = true,
260 		.bootime = 2,
261 	},
262 	{
263 		.wai = 0x40,
264 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
265 		.sensors_supported = {
266 			[0] = LSM330_ACCEL_DEV_NAME,
267 		},
268 		.ch = (struct iio_chan_spec *)st_accel_16bit_channels,
269 		.odr = {
270 			.addr = 0x20,
271 			.mask = 0xf0,
272 			.odr_avl = {
273 				{ .hz = 3, .value = 0x01, },
274 				{ .hz = 6, .value = 0x02, },
275 				{ .hz = 12, .value = 0x03, },
276 				{ .hz = 25, .value = 0x04, },
277 				{ .hz = 50, .value = 0x05, },
278 				{ .hz = 100, .value = 0x06, },
279 				{ .hz = 200, .value = 0x07, },
280 				{ .hz = 400, .value = 0x08, },
281 				{ .hz = 800, .value = 0x09, },
282 				{ .hz = 1600, .value = 0x0a, },
283 			},
284 		},
285 		.pw = {
286 			.addr = 0x20,
287 			.mask = 0xf0,
288 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
289 		},
290 		.enable_axis = {
291 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
292 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
293 		},
294 		.fs = {
295 			.addr = 0x24,
296 			.mask = 0x38,
297 			.fs_avl = {
298 				[0] = {
299 					.num = ST_ACCEL_FS_AVL_2G,
300 					.value = 0x00,
301 					.gain = IIO_G_TO_M_S_2(61),
302 				},
303 				[1] = {
304 					.num = ST_ACCEL_FS_AVL_4G,
305 					.value = 0x01,
306 					.gain = IIO_G_TO_M_S_2(122),
307 				},
308 				[2] = {
309 					.num = ST_ACCEL_FS_AVL_6G,
310 					.value = 0x02,
311 					.gain = IIO_G_TO_M_S_2(183),
312 				},
313 				[3] = {
314 					.num = ST_ACCEL_FS_AVL_8G,
315 					.value = 0x03,
316 					.gain = IIO_G_TO_M_S_2(244),
317 				},
318 				[4] = {
319 					.num = ST_ACCEL_FS_AVL_16G,
320 					.value = 0x04,
321 					.gain = IIO_G_TO_M_S_2(732),
322 				},
323 			},
324 		},
325 		.bdu = {
326 			.addr = 0x20,
327 			.mask = 0x08,
328 		},
329 		.drdy_irq = {
330 			.int1 = {
331 				.addr = 0x23,
332 				.mask = 0x80,
333 			},
334 			.addr_ihl = 0x23,
335 			.mask_ihl = 0x40,
336 			.stat_drdy = {
337 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
338 				.mask = 0x07,
339 			},
340 			.ig1 = {
341 				.en_addr = 0x23,
342 				.en_mask = 0x08,
343 			},
344 		},
345 		.sim = {
346 			.addr = 0x24,
347 			.value = BIT(0),
348 		},
349 		.multi_read_bit = false,
350 		.bootime = 2,
351 	},
352 	{
353 		.wai = 0x3a,
354 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
355 		.sensors_supported = {
356 			[0] = LIS3LV02DL_ACCEL_DEV_NAME,
357 		},
358 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
359 		.odr = {
360 			.addr = 0x20,
361 			.mask = 0x30, /* DF1 and DF0 */
362 			.odr_avl = {
363 				{ .hz = 40, .value = 0x00, },
364 				{ .hz = 160, .value = 0x01, },
365 				{ .hz = 640, .value = 0x02, },
366 				{ .hz = 2560, .value = 0x03, },
367 			},
368 		},
369 		.pw = {
370 			.addr = 0x20,
371 			.mask = 0xc0,
372 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
373 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
374 		},
375 		.enable_axis = {
376 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
377 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
378 		},
379 		.fs = {
380 			.addr = 0x21,
381 			.mask = 0x80,
382 			.fs_avl = {
383 				[0] = {
384 					.num = ST_ACCEL_FS_AVL_2G,
385 					.value = 0x00,
386 					.gain = IIO_G_TO_M_S_2(1000),
387 				},
388 				[1] = {
389 					.num = ST_ACCEL_FS_AVL_6G,
390 					.value = 0x01,
391 					.gain = IIO_G_TO_M_S_2(3000),
392 				},
393 			},
394 		},
395 		.bdu = {
396 			.addr = 0x21,
397 			.mask = 0x40,
398 		},
399 		/*
400 		 * Data Alignment Setting - needs to be set to get
401 		 * left-justified data like all other sensors.
402 		 */
403 		.das = {
404 			.addr = 0x21,
405 			.mask = 0x01,
406 		},
407 		.drdy_irq = {
408 			.int1 = {
409 				.addr = 0x21,
410 				.mask = 0x04,
411 			},
412 			.stat_drdy = {
413 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
414 				.mask = 0x07,
415 			},
416 		},
417 		.sim = {
418 			.addr = 0x21,
419 			.value = BIT(1),
420 		},
421 		.multi_read_bit = true,
422 		.bootime = 2, /* guess */
423 	},
424 	{
425 		.wai = 0x3b,
426 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
427 		.sensors_supported = {
428 			[0] = LIS331DL_ACCEL_DEV_NAME,
429 		},
430 		.ch = (struct iio_chan_spec *)st_accel_8bit_channels,
431 		.odr = {
432 			.addr = 0x20,
433 			.mask = 0x80,
434 			.odr_avl = {
435 				{ .hz = 100, .value = 0x00, },
436 				{ .hz = 400, .value = 0x01, },
437 			},
438 		},
439 		.pw = {
440 			.addr = 0x20,
441 			.mask = 0x40,
442 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
443 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
444 		},
445 		.enable_axis = {
446 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
447 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
448 		},
449 		.fs = {
450 			.addr = 0x20,
451 			.mask = 0x20,
452 			/*
453 			 * TODO: check these resulting gain settings, these are
454 			 * not in the datsheet
455 			 */
456 			.fs_avl = {
457 				[0] = {
458 					.num = ST_ACCEL_FS_AVL_2G,
459 					.value = 0x00,
460 					.gain = IIO_G_TO_M_S_2(18000),
461 				},
462 				[1] = {
463 					.num = ST_ACCEL_FS_AVL_8G,
464 					.value = 0x01,
465 					.gain = IIO_G_TO_M_S_2(72000),
466 				},
467 			},
468 		},
469 		.drdy_irq = {
470 			.int1 = {
471 				.addr = 0x22,
472 				.mask = 0x04,
473 				.addr_od = 0x22,
474 				.mask_od = 0x40,
475 			},
476 			.int2 = {
477 				.addr = 0x22,
478 				.mask = 0x20,
479 				.addr_od = 0x22,
480 				.mask_od = 0x40,
481 			},
482 			.addr_ihl = 0x22,
483 			.mask_ihl = 0x80,
484 			.stat_drdy = {
485 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
486 				.mask = 0x07,
487 			},
488 		},
489 		.sim = {
490 			.addr = 0x21,
491 			.value = BIT(7),
492 		},
493 		.multi_read_bit = false,
494 		.bootime = 2, /* guess */
495 	},
496 	{
497 		.wai = 0x32,
498 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
499 		.sensors_supported = {
500 			[0] = H3LIS331DL_ACCEL_DEV_NAME,
501 		},
502 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
503 		.odr = {
504 			.addr = 0x20,
505 			.mask = 0x18,
506 			.odr_avl = {
507 				{ .hz = 50, .value = 0x00, },
508 				{ .hz = 100, .value = 0x01, },
509 				{ .hz = 400, .value = 0x02, },
510 				{ .hz = 1000, .value = 0x03, },
511 			},
512 		},
513 		.pw = {
514 			.addr = 0x20,
515 			.mask = 0x20,
516 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
517 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
518 		},
519 		.enable_axis = {
520 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
521 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
522 		},
523 		.fs = {
524 			.addr = 0x23,
525 			.mask = 0x30,
526 			.fs_avl = {
527 				[0] = {
528 					.num = ST_ACCEL_FS_AVL_100G,
529 					.value = 0x00,
530 					.gain = IIO_G_TO_M_S_2(49000),
531 				},
532 				[1] = {
533 					.num = ST_ACCEL_FS_AVL_200G,
534 					.value = 0x01,
535 					.gain = IIO_G_TO_M_S_2(98000),
536 				},
537 				[2] = {
538 					.num = ST_ACCEL_FS_AVL_400G,
539 					.value = 0x03,
540 					.gain = IIO_G_TO_M_S_2(195000),
541 				},
542 			},
543 		},
544 		.bdu = {
545 			.addr = 0x23,
546 			.mask = 0x80,
547 		},
548 		.drdy_irq = {
549 			.int1 = {
550 				.addr = 0x22,
551 				.mask = 0x02,
552 			},
553 			.int2 = {
554 				.addr = 0x22,
555 				.mask = 0x10,
556 			},
557 			.addr_ihl = 0x22,
558 			.mask_ihl = 0x80,
559 		},
560 		.sim = {
561 			.addr = 0x23,
562 			.value = BIT(0),
563 		},
564 		.multi_read_bit = true,
565 		.bootime = 2,
566 	},
567 	{
568 		/* No WAI register present */
569 		.sensors_supported = {
570 			[0] = LIS3L02DQ_ACCEL_DEV_NAME,
571 		},
572 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
573 		.odr = {
574 			.addr = 0x20,
575 			.mask = 0x30,
576 			.odr_avl = {
577 				{ .hz = 280, .value = 0x00, },
578 				{ .hz = 560, .value = 0x01, },
579 				{ .hz = 1120, .value = 0x02, },
580 				{ .hz = 4480, .value = 0x03, },
581 			},
582 		},
583 		.pw = {
584 			.addr = 0x20,
585 			.mask = 0xc0,
586 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
587 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
588 		},
589 		.enable_axis = {
590 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
591 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
592 		},
593 		.fs = {
594 			.fs_avl = {
595 				[0] = {
596 					.num = ST_ACCEL_FS_AVL_2G,
597 					.gain = IIO_G_TO_M_S_2(488),
598 				},
599 			},
600 		},
601 		/*
602 		 * The part has a BDU bit but if set the data is never
603 		 * updated so don't set it.
604 		 */
605 		.bdu = {
606 		},
607 		.drdy_irq = {
608 			.int1 = {
609 				.addr = 0x21,
610 				.mask = 0x04,
611 			},
612 			.stat_drdy = {
613 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
614 				.mask = 0x07,
615 			},
616 		},
617 		.sim = {
618 			.addr = 0x21,
619 			.value = BIT(1),
620 		},
621 		.multi_read_bit = false,
622 		.bootime = 2,
623 	},
624 	{
625 		.wai = 0x33,
626 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
627 		.sensors_supported = {
628 			[0] = LNG2DM_ACCEL_DEV_NAME,
629 		},
630 		.ch = (struct iio_chan_spec *)st_accel_8bit_channels,
631 		.odr = {
632 			.addr = 0x20,
633 			.mask = 0xf0,
634 			.odr_avl = {
635 				{ .hz = 1, .value = 0x01, },
636 				{ .hz = 10, .value = 0x02, },
637 				{ .hz = 25, .value = 0x03, },
638 				{ .hz = 50, .value = 0x04, },
639 				{ .hz = 100, .value = 0x05, },
640 				{ .hz = 200, .value = 0x06, },
641 				{ .hz = 400, .value = 0x07, },
642 				{ .hz = 1600, .value = 0x08, },
643 			},
644 		},
645 		.pw = {
646 			.addr = 0x20,
647 			.mask = 0xf0,
648 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
649 		},
650 		.enable_axis = {
651 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
652 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
653 		},
654 		.fs = {
655 			.addr = 0x23,
656 			.mask = 0x30,
657 			.fs_avl = {
658 				[0] = {
659 					.num = ST_ACCEL_FS_AVL_2G,
660 					.value = 0x00,
661 					.gain = IIO_G_TO_M_S_2(15600),
662 				},
663 				[1] = {
664 					.num = ST_ACCEL_FS_AVL_4G,
665 					.value = 0x01,
666 					.gain = IIO_G_TO_M_S_2(31200),
667 				},
668 				[2] = {
669 					.num = ST_ACCEL_FS_AVL_8G,
670 					.value = 0x02,
671 					.gain = IIO_G_TO_M_S_2(62500),
672 				},
673 				[3] = {
674 					.num = ST_ACCEL_FS_AVL_16G,
675 					.value = 0x03,
676 					.gain = IIO_G_TO_M_S_2(187500),
677 				},
678 			},
679 		},
680 		.drdy_irq = {
681 			.int1 = {
682 				.addr = 0x22,
683 				.mask = 0x10,
684 			},
685 			.addr_ihl = 0x25,
686 			.mask_ihl = 0x02,
687 			.stat_drdy = {
688 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
689 				.mask = 0x07,
690 			},
691 		},
692 		.sim = {
693 			.addr = 0x23,
694 			.value = BIT(0),
695 		},
696 		.multi_read_bit = true,
697 		.bootime = 2,
698 	},
699 	{
700 		.wai = 0x44,
701 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
702 		.sensors_supported = {
703 			[0] = LIS2DW12_ACCEL_DEV_NAME,
704 		},
705 		.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
706 		.odr = {
707 			.addr = 0x20,
708 			.mask = 0xf0,
709 			.odr_avl = {
710 				{ .hz = 1, .value = 0x01, },
711 				{ .hz = 12, .value = 0x02, },
712 				{ .hz = 25, .value = 0x03, },
713 				{ .hz = 50, .value = 0x04, },
714 				{ .hz = 100, .value = 0x05, },
715 				{ .hz = 200, .value = 0x06, },
716 			},
717 		},
718 		.pw = {
719 			.addr = 0x20,
720 			.mask = 0xf0,
721 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
722 		},
723 		.fs = {
724 			.addr = 0x25,
725 			.mask = 0x30,
726 			.fs_avl = {
727 				[0] = {
728 					.num = ST_ACCEL_FS_AVL_2G,
729 					.value = 0x00,
730 					.gain = IIO_G_TO_M_S_2(976),
731 				},
732 				[1] = {
733 					.num = ST_ACCEL_FS_AVL_4G,
734 					.value = 0x01,
735 					.gain = IIO_G_TO_M_S_2(1952),
736 				},
737 				[2] = {
738 					.num = ST_ACCEL_FS_AVL_8G,
739 					.value = 0x02,
740 					.gain = IIO_G_TO_M_S_2(3904),
741 				},
742 				[3] = {
743 					.num = ST_ACCEL_FS_AVL_16G,
744 					.value = 0x03,
745 					.gain = IIO_G_TO_M_S_2(7808),
746 				},
747 			},
748 		},
749 		.bdu = {
750 			.addr = 0x21,
751 			.mask = 0x08,
752 		},
753 		.drdy_irq = {
754 			.int1 = {
755 				.addr = 0x23,
756 				.mask = 0x01,
757 				.addr_od = 0x22,
758 				.mask_od = 0x20,
759 			},
760 			.int2 = {
761 				.addr = 0x24,
762 				.mask = 0x01,
763 				.addr_od = 0x22,
764 				.mask_od = 0x20,
765 			},
766 			.addr_ihl = 0x22,
767 			.mask_ihl = 0x08,
768 			.stat_drdy = {
769 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
770 				.mask = 0x01,
771 			},
772 		},
773 		.sim = {
774 			.addr = 0x21,
775 			.value = BIT(0),
776 		},
777 		.multi_read_bit = false,
778 		.bootime = 2,
779 	},
780 	{
781 		.wai = 0x11,
782 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
783 		.sensors_supported = {
784 			[0] = LIS3DHH_ACCEL_DEV_NAME,
785 		},
786 		.ch = (struct iio_chan_spec *)st_accel_16bit_channels,
787 		.odr = {
788 			/* just ODR = 1100Hz available */
789 			.odr_avl = {
790 				{ .hz = 1100, .value = 0x00, },
791 			},
792 		},
793 		.pw = {
794 			.addr = 0x20,
795 			.mask = 0x80,
796 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
797 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
798 		},
799 		.fs = {
800 			.fs_avl = {
801 				[0] = {
802 					.num = ST_ACCEL_FS_AVL_2G,
803 					.gain = IIO_G_TO_M_S_2(76),
804 				},
805 			},
806 		},
807 		.bdu = {
808 			.addr = 0x20,
809 			.mask = 0x01,
810 		},
811 		.drdy_irq = {
812 			.int1 = {
813 				.addr = 0x21,
814 				.mask = 0x80,
815 				.addr_od = 0x23,
816 				.mask_od = 0x04,
817 			},
818 			.int2 = {
819 				.addr = 0x22,
820 				.mask = 0x80,
821 				.addr_od = 0x23,
822 				.mask_od = 0x08,
823 			},
824 			.stat_drdy = {
825 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
826 				.mask = 0x07,
827 			},
828 		},
829 		.multi_read_bit = false,
830 		.bootime = 2,
831 	},
832 };
833 
834 static int st_accel_read_raw(struct iio_dev *indio_dev,
835 			struct iio_chan_spec const *ch, int *val,
836 							int *val2, long mask)
837 {
838 	int err;
839 	struct st_sensor_data *adata = iio_priv(indio_dev);
840 
841 	switch (mask) {
842 	case IIO_CHAN_INFO_RAW:
843 		err = st_sensors_read_info_raw(indio_dev, ch, val);
844 		if (err < 0)
845 			goto read_error;
846 
847 		return IIO_VAL_INT;
848 	case IIO_CHAN_INFO_SCALE:
849 		*val = adata->current_fullscale->gain / 1000000;
850 		*val2 = adata->current_fullscale->gain % 1000000;
851 		return IIO_VAL_INT_PLUS_MICRO;
852 	case IIO_CHAN_INFO_SAMP_FREQ:
853 		*val = adata->odr;
854 		return IIO_VAL_INT;
855 	default:
856 		return -EINVAL;
857 	}
858 
859 read_error:
860 	return err;
861 }
862 
863 static int st_accel_write_raw(struct iio_dev *indio_dev,
864 		struct iio_chan_spec const *chan, int val, int val2, long mask)
865 {
866 	int err;
867 
868 	switch (mask) {
869 	case IIO_CHAN_INFO_SCALE: {
870 		int gain;
871 
872 		gain = val * 1000000 + val2;
873 		err = st_sensors_set_fullscale_by_gain(indio_dev, gain);
874 		break;
875 	}
876 	case IIO_CHAN_INFO_SAMP_FREQ:
877 		if (val2)
878 			return -EINVAL;
879 		mutex_lock(&indio_dev->mlock);
880 		err = st_sensors_set_odr(indio_dev, val);
881 		mutex_unlock(&indio_dev->mlock);
882 		return err;
883 	default:
884 		return -EINVAL;
885 	}
886 
887 	return err;
888 }
889 
890 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
891 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_accel_scale_available);
892 
893 static struct attribute *st_accel_attributes[] = {
894 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
895 	&iio_dev_attr_in_accel_scale_available.dev_attr.attr,
896 	NULL,
897 };
898 
899 static const struct attribute_group st_accel_attribute_group = {
900 	.attrs = st_accel_attributes,
901 };
902 
903 static const struct iio_info accel_info = {
904 	.attrs = &st_accel_attribute_group,
905 	.read_raw = &st_accel_read_raw,
906 	.write_raw = &st_accel_write_raw,
907 	.debugfs_reg_access = &st_sensors_debugfs_reg_access,
908 };
909 
910 #ifdef CONFIG_IIO_TRIGGER
911 static const struct iio_trigger_ops st_accel_trigger_ops = {
912 	.set_trigger_state = ST_ACCEL_TRIGGER_SET_STATE,
913 	.validate_device = st_sensors_validate_device,
914 };
915 #define ST_ACCEL_TRIGGER_OPS (&st_accel_trigger_ops)
916 #else
917 #define ST_ACCEL_TRIGGER_OPS NULL
918 #endif
919 
920 int st_accel_common_probe(struct iio_dev *indio_dev)
921 {
922 	struct st_sensor_data *adata = iio_priv(indio_dev);
923 	struct st_sensors_platform_data *pdata =
924 		(struct st_sensors_platform_data *)adata->dev->platform_data;
925 	int irq = adata->get_irq_data_ready(indio_dev);
926 	int err;
927 
928 	indio_dev->modes = INDIO_DIRECT_MODE;
929 	indio_dev->info = &accel_info;
930 	mutex_init(&adata->tb.buf_lock);
931 
932 	err = st_sensors_power_enable(indio_dev);
933 	if (err)
934 		return err;
935 
936 	err = st_sensors_check_device_support(indio_dev,
937 					ARRAY_SIZE(st_accel_sensors_settings),
938 					st_accel_sensors_settings);
939 	if (err < 0)
940 		goto st_accel_power_off;
941 
942 	adata->num_data_channels = ST_ACCEL_NUMBER_DATA_CHANNELS;
943 	adata->multiread_bit = adata->sensor_settings->multi_read_bit;
944 	indio_dev->channels = adata->sensor_settings->ch;
945 	indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
946 
947 	adata->current_fullscale = (struct st_sensor_fullscale_avl *)
948 					&adata->sensor_settings->fs.fs_avl[0];
949 	adata->odr = adata->sensor_settings->odr.odr_avl[0].hz;
950 
951 	if (!pdata)
952 		pdata = (struct st_sensors_platform_data *)&default_accel_pdata;
953 
954 	err = st_sensors_init_sensor(indio_dev, pdata);
955 	if (err < 0)
956 		goto st_accel_power_off;
957 
958 	err = st_accel_allocate_ring(indio_dev);
959 	if (err < 0)
960 		goto st_accel_power_off;
961 
962 	if (irq > 0) {
963 		err = st_sensors_allocate_trigger(indio_dev,
964 						 ST_ACCEL_TRIGGER_OPS);
965 		if (err < 0)
966 			goto st_accel_probe_trigger_error;
967 	}
968 
969 	err = iio_device_register(indio_dev);
970 	if (err)
971 		goto st_accel_device_register_error;
972 
973 	dev_info(&indio_dev->dev, "registered accelerometer %s\n",
974 		 indio_dev->name);
975 
976 	return 0;
977 
978 st_accel_device_register_error:
979 	if (irq > 0)
980 		st_sensors_deallocate_trigger(indio_dev);
981 st_accel_probe_trigger_error:
982 	st_accel_deallocate_ring(indio_dev);
983 st_accel_power_off:
984 	st_sensors_power_disable(indio_dev);
985 
986 	return err;
987 }
988 EXPORT_SYMBOL(st_accel_common_probe);
989 
990 void st_accel_common_remove(struct iio_dev *indio_dev)
991 {
992 	struct st_sensor_data *adata = iio_priv(indio_dev);
993 
994 	st_sensors_power_disable(indio_dev);
995 
996 	iio_device_unregister(indio_dev);
997 	if (adata->get_irq_data_ready(indio_dev) > 0)
998 		st_sensors_deallocate_trigger(indio_dev);
999 
1000 	st_accel_deallocate_ring(indio_dev);
1001 }
1002 EXPORT_SYMBOL(st_accel_common_remove);
1003 
1004 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
1005 MODULE_DESCRIPTION("STMicroelectronics accelerometers driver");
1006 MODULE_LICENSE("GPL v2");
1007