staging: rtl8188eu: rename HalSetBrateCfg() - style
[linux/fpc-iii.git] / drivers / iio / accel / st_accel_core.c
blob3e6fd5a8ac5be13fba3a8bf1d14f75481eafd2a9
1 /*
2 * STMicroelectronics accelerometers driver
4 * Copyright 2012-2013 STMicroelectronics Inc.
6 * Denis Ciocca <denis.ciocca@st.com>
8 * Licensed under the GPL-2.
9 */
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>
26 #include <linux/iio/common/st_sensors.h>
27 #include "st_accel.h"
29 #define ST_ACCEL_NUMBER_DATA_CHANNELS 3
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
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
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)
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)
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)
94 static const struct st_sensor_settings st_accel_sensors_settings[] = {
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,
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, },
122 .pw = {
123 .addr = 0x20,
124 .mask = 0xf0,
125 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
127 .enable_axis = {
128 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
129 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
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),
140 [1] = {
141 .num = ST_ACCEL_FS_AVL_4G,
142 .value = 0x01,
143 .gain = IIO_G_TO_M_S_2(2000),
145 [2] = {
146 .num = ST_ACCEL_FS_AVL_8G,
147 .value = 0x02,
148 .gain = IIO_G_TO_M_S_2(4000),
150 [3] = {
151 .num = ST_ACCEL_FS_AVL_16G,
152 .value = 0x03,
153 .gain = IIO_G_TO_M_S_2(12000),
157 .bdu = {
158 .addr = 0x23,
159 .mask = 0x80,
161 .drdy_irq = {
162 .int1 = {
163 .addr = 0x22,
164 .mask = 0x10,
166 .addr_ihl = 0x25,
167 .mask_ihl = 0x02,
168 .stat_drdy = {
169 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
170 .mask = 0x07,
173 .sim = {
174 .addr = 0x23,
175 .value = BIT(0),
177 .multi_read_bit = true,
178 .bootime = 2,
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,
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, },
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,
206 .enable_axis = {
207 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
208 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
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),
219 [1] = {
220 .num = ST_ACCEL_FS_AVL_4G,
221 .value = 0x01,
222 .gain = IIO_G_TO_M_S_2(2000),
224 [2] = {
225 .num = ST_ACCEL_FS_AVL_8G,
226 .value = 0x03,
227 .gain = IIO_G_TO_M_S_2(3900),
231 .bdu = {
232 .addr = 0x23,
233 .mask = 0x80,
235 .drdy_irq = {
236 .int1 = {
237 .addr = 0x22,
238 .mask = 0x02,
239 .addr_od = 0x22,
240 .mask_od = 0x40,
242 .int2 = {
243 .addr = 0x22,
244 .mask = 0x10,
245 .addr_od = 0x22,
246 .mask_od = 0x40,
248 .addr_ihl = 0x22,
249 .mask_ihl = 0x80,
250 .stat_drdy = {
251 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
252 .mask = 0x07,
255 .sim = {
256 .addr = 0x23,
257 .value = BIT(0),
259 .multi_read_bit = true,
260 .bootime = 2,
263 .wai = 0x40,
264 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
265 .sensors_supported = {
266 [0] = LSM330_ACCEL_DEV_NAME,
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, },
285 .pw = {
286 .addr = 0x20,
287 .mask = 0xf0,
288 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
290 .enable_axis = {
291 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
292 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
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),
303 [1] = {
304 .num = ST_ACCEL_FS_AVL_4G,
305 .value = 0x01,
306 .gain = IIO_G_TO_M_S_2(122),
308 [2] = {
309 .num = ST_ACCEL_FS_AVL_6G,
310 .value = 0x02,
311 .gain = IIO_G_TO_M_S_2(183),
313 [3] = {
314 .num = ST_ACCEL_FS_AVL_8G,
315 .value = 0x03,
316 .gain = IIO_G_TO_M_S_2(244),
318 [4] = {
319 .num = ST_ACCEL_FS_AVL_16G,
320 .value = 0x04,
321 .gain = IIO_G_TO_M_S_2(732),
325 .bdu = {
326 .addr = 0x20,
327 .mask = 0x08,
329 .drdy_irq = {
330 .int1 = {
331 .addr = 0x23,
332 .mask = 0x80,
334 .addr_ihl = 0x23,
335 .mask_ihl = 0x40,
336 .stat_drdy = {
337 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
338 .mask = 0x07,
340 .ig1 = {
341 .en_addr = 0x23,
342 .en_mask = 0x08,
345 .sim = {
346 .addr = 0x24,
347 .value = BIT(0),
349 .multi_read_bit = false,
350 .bootime = 2,
353 .wai = 0x3a,
354 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
355 .sensors_supported = {
356 [0] = LIS3LV02DL_ACCEL_DEV_NAME,
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, },
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,
375 .enable_axis = {
376 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
377 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
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),
388 [1] = {
389 .num = ST_ACCEL_FS_AVL_6G,
390 .value = 0x01,
391 .gain = IIO_G_TO_M_S_2(3000),
395 .bdu = {
396 .addr = 0x21,
397 .mask = 0x40,
400 * Data Alignment Setting - needs to be set to get
401 * left-justified data like all other sensors.
403 .das = {
404 .addr = 0x21,
405 .mask = 0x01,
407 .drdy_irq = {
408 .int1 = {
409 .addr = 0x21,
410 .mask = 0x04,
412 .stat_drdy = {
413 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
414 .mask = 0x07,
417 .sim = {
418 .addr = 0x21,
419 .value = BIT(1),
421 .multi_read_bit = true,
422 .bootime = 2, /* guess */
425 .wai = 0x3b,
426 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
427 .sensors_supported = {
428 [0] = LIS331DL_ACCEL_DEV_NAME,
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, },
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,
445 .enable_axis = {
446 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
447 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
449 .fs = {
450 .addr = 0x20,
451 .mask = 0x20,
453 * TODO: check these resulting gain settings, these are
454 * not in the datsheet
456 .fs_avl = {
457 [0] = {
458 .num = ST_ACCEL_FS_AVL_2G,
459 .value = 0x00,
460 .gain = IIO_G_TO_M_S_2(18000),
462 [1] = {
463 .num = ST_ACCEL_FS_AVL_8G,
464 .value = 0x01,
465 .gain = IIO_G_TO_M_S_2(72000),
469 .drdy_irq = {
470 .int1 = {
471 .addr = 0x22,
472 .mask = 0x04,
473 .addr_od = 0x22,
474 .mask_od = 0x40,
476 .int2 = {
477 .addr = 0x22,
478 .mask = 0x20,
479 .addr_od = 0x22,
480 .mask_od = 0x40,
482 .addr_ihl = 0x22,
483 .mask_ihl = 0x80,
484 .stat_drdy = {
485 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
486 .mask = 0x07,
489 .sim = {
490 .addr = 0x21,
491 .value = BIT(7),
493 .multi_read_bit = false,
494 .bootime = 2, /* guess */
497 .wai = 0x32,
498 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
499 .sensors_supported = {
500 [0] = H3LIS331DL_ACCEL_DEV_NAME,
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, },
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,
519 .enable_axis = {
520 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
521 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
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),
532 [1] = {
533 .num = ST_ACCEL_FS_AVL_200G,
534 .value = 0x01,
535 .gain = IIO_G_TO_M_S_2(98000),
537 [2] = {
538 .num = ST_ACCEL_FS_AVL_400G,
539 .value = 0x03,
540 .gain = IIO_G_TO_M_S_2(195000),
544 .bdu = {
545 .addr = 0x23,
546 .mask = 0x80,
548 .drdy_irq = {
549 .int1 = {
550 .addr = 0x22,
551 .mask = 0x02,
553 .int2 = {
554 .addr = 0x22,
555 .mask = 0x10,
557 .addr_ihl = 0x22,
558 .mask_ihl = 0x80,
560 .sim = {
561 .addr = 0x23,
562 .value = BIT(0),
564 .multi_read_bit = true,
565 .bootime = 2,
568 /* No WAI register present */
569 .sensors_supported = {
570 [0] = LIS3L02DQ_ACCEL_DEV_NAME,
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, },
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,
589 .enable_axis = {
590 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
591 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
593 .fs = {
594 .fs_avl = {
595 [0] = {
596 .num = ST_ACCEL_FS_AVL_2G,
597 .gain = IIO_G_TO_M_S_2(488),
602 * The part has a BDU bit but if set the data is never
603 * updated so don't set it.
605 .bdu = {
607 .drdy_irq = {
608 .int1 = {
609 .addr = 0x21,
610 .mask = 0x04,
612 .stat_drdy = {
613 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
614 .mask = 0x07,
617 .sim = {
618 .addr = 0x21,
619 .value = BIT(1),
621 .multi_read_bit = false,
622 .bootime = 2,
625 .wai = 0x33,
626 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
627 .sensors_supported = {
628 [0] = LNG2DM_ACCEL_DEV_NAME,
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, },
645 .pw = {
646 .addr = 0x20,
647 .mask = 0xf0,
648 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
650 .enable_axis = {
651 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
652 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
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),
663 [1] = {
664 .num = ST_ACCEL_FS_AVL_4G,
665 .value = 0x01,
666 .gain = IIO_G_TO_M_S_2(31200),
668 [2] = {
669 .num = ST_ACCEL_FS_AVL_8G,
670 .value = 0x02,
671 .gain = IIO_G_TO_M_S_2(62500),
673 [3] = {
674 .num = ST_ACCEL_FS_AVL_16G,
675 .value = 0x03,
676 .gain = IIO_G_TO_M_S_2(187500),
680 .drdy_irq = {
681 .int1 = {
682 .addr = 0x22,
683 .mask = 0x10,
685 .addr_ihl = 0x25,
686 .mask_ihl = 0x02,
687 .stat_drdy = {
688 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
689 .mask = 0x07,
692 .sim = {
693 .addr = 0x23,
694 .value = BIT(0),
696 .multi_read_bit = true,
697 .bootime = 2,
700 .wai = 0x44,
701 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
702 .sensors_supported = {
703 [0] = LIS2DW12_ACCEL_DEV_NAME,
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, },
718 .pw = {
719 .addr = 0x20,
720 .mask = 0xf0,
721 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
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),
732 [1] = {
733 .num = ST_ACCEL_FS_AVL_4G,
734 .value = 0x01,
735 .gain = IIO_G_TO_M_S_2(1952),
737 [2] = {
738 .num = ST_ACCEL_FS_AVL_8G,
739 .value = 0x02,
740 .gain = IIO_G_TO_M_S_2(3904),
742 [3] = {
743 .num = ST_ACCEL_FS_AVL_16G,
744 .value = 0x03,
745 .gain = IIO_G_TO_M_S_2(7808),
749 .bdu = {
750 .addr = 0x21,
751 .mask = 0x08,
753 .drdy_irq = {
754 .int1 = {
755 .addr = 0x23,
756 .mask = 0x01,
757 .addr_od = 0x22,
758 .mask_od = 0x20,
760 .int2 = {
761 .addr = 0x24,
762 .mask = 0x01,
763 .addr_od = 0x22,
764 .mask_od = 0x20,
766 .addr_ihl = 0x22,
767 .mask_ihl = 0x08,
768 .stat_drdy = {
769 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
770 .mask = 0x01,
773 .sim = {
774 .addr = 0x21,
775 .value = BIT(0),
777 .multi_read_bit = false,
778 .bootime = 2,
781 .wai = 0x11,
782 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
783 .sensors_supported = {
784 [0] = LIS3DHH_ACCEL_DEV_NAME,
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, },
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,
799 .fs = {
800 .fs_avl = {
801 [0] = {
802 .num = ST_ACCEL_FS_AVL_2G,
803 .gain = IIO_G_TO_M_S_2(76),
807 .bdu = {
808 .addr = 0x20,
809 .mask = 0x01,
811 .drdy_irq = {
812 .int1 = {
813 .addr = 0x21,
814 .mask = 0x80,
815 .addr_od = 0x23,
816 .mask_od = 0x04,
818 .int2 = {
819 .addr = 0x22,
820 .mask = 0x80,
821 .addr_od = 0x23,
822 .mask_od = 0x08,
824 .stat_drdy = {
825 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
826 .mask = 0x07,
829 .multi_read_bit = false,
830 .bootime = 2,
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)
838 int err;
839 struct st_sensor_data *adata = iio_priv(indio_dev);
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;
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;
859 read_error:
860 return err;
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)
866 int err;
868 switch (mask) {
869 case IIO_CHAN_INFO_SCALE: {
870 int gain;
872 gain = val * 1000000 + val2;
873 err = st_sensors_set_fullscale_by_gain(indio_dev, gain);
874 break;
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;
887 return err;
890 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
891 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_accel_scale_available);
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,
899 static const struct attribute_group st_accel_attribute_group = {
900 .attrs = st_accel_attributes,
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,
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,
915 #define ST_ACCEL_TRIGGER_OPS (&st_accel_trigger_ops)
916 #else
917 #define ST_ACCEL_TRIGGER_OPS NULL
918 #endif
920 int st_accel_common_probe(struct iio_dev *indio_dev)
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;
928 indio_dev->modes = INDIO_DIRECT_MODE;
929 indio_dev->info = &accel_info;
930 mutex_init(&adata->tb.buf_lock);
932 err = st_sensors_power_enable(indio_dev);
933 if (err)
934 return err;
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;
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;
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;
951 if (!pdata)
952 pdata = (struct st_sensors_platform_data *)&default_accel_pdata;
954 err = st_sensors_init_sensor(indio_dev, pdata);
955 if (err < 0)
956 goto st_accel_power_off;
958 err = st_accel_allocate_ring(indio_dev);
959 if (err < 0)
960 goto st_accel_power_off;
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;
969 err = iio_device_register(indio_dev);
970 if (err)
971 goto st_accel_device_register_error;
973 dev_info(&indio_dev->dev, "registered accelerometer %s\n",
974 indio_dev->name);
976 return 0;
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);
986 return err;
988 EXPORT_SYMBOL(st_accel_common_probe);
990 void st_accel_common_remove(struct iio_dev *indio_dev)
992 struct st_sensor_data *adata = iio_priv(indio_dev);
994 st_sensors_power_disable(indio_dev);
996 iio_device_unregister(indio_dev);
997 if (adata->get_irq_data_ready(indio_dev) > 0)
998 st_sensors_deallocate_trigger(indio_dev);
1000 st_accel_deallocate_ring(indio_dev);
1002 EXPORT_SYMBOL(st_accel_common_remove);
1004 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
1005 MODULE_DESCRIPTION("STMicroelectronics accelerometers driver");
1006 MODULE_LICENSE("GPL v2");