sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / iio / accel / st_accel_core.c
blobf6b6d42385e1148b2ad7f279feb9a43396e66894
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 .addr = 0x22,
163 .mask_int1 = 0x10,
164 .mask_int2 = 0x08,
165 .addr_ihl = 0x25,
166 .mask_ihl = 0x02,
167 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
169 .multi_read_bit = true,
170 .bootime = 2,
173 .wai = 0x32,
174 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
175 .sensors_supported = {
176 [0] = LIS331DLH_ACCEL_DEV_NAME,
177 [1] = LSM303DL_ACCEL_DEV_NAME,
178 [2] = LSM303DLH_ACCEL_DEV_NAME,
179 [3] = LSM303DLM_ACCEL_DEV_NAME,
181 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
182 .odr = {
183 .addr = 0x20,
184 .mask = 0x18,
185 .odr_avl = {
186 { .hz = 50, .value = 0x00, },
187 { .hz = 100, .value = 0x01, },
188 { .hz = 400, .value = 0x02, },
189 { .hz = 1000, .value = 0x03, },
192 .pw = {
193 .addr = 0x20,
194 .mask = 0xe0,
195 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
196 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
198 .enable_axis = {
199 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
200 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
202 .fs = {
203 .addr = 0x23,
204 .mask = 0x30,
205 .fs_avl = {
206 [0] = {
207 .num = ST_ACCEL_FS_AVL_2G,
208 .value = 0x00,
209 .gain = IIO_G_TO_M_S_2(1000),
211 [1] = {
212 .num = ST_ACCEL_FS_AVL_4G,
213 .value = 0x01,
214 .gain = IIO_G_TO_M_S_2(2000),
216 [2] = {
217 .num = ST_ACCEL_FS_AVL_8G,
218 .value = 0x03,
219 .gain = IIO_G_TO_M_S_2(3900),
223 .bdu = {
224 .addr = 0x23,
225 .mask = 0x80,
227 .drdy_irq = {
228 .addr = 0x22,
229 .mask_int1 = 0x02,
230 .mask_int2 = 0x10,
231 .addr_ihl = 0x22,
232 .mask_ihl = 0x80,
233 .addr_od = 0x22,
234 .mask_od = 0x40,
235 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
237 .multi_read_bit = true,
238 .bootime = 2,
241 .wai = 0x40,
242 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
243 .sensors_supported = {
244 [0] = LSM330_ACCEL_DEV_NAME,
246 .ch = (struct iio_chan_spec *)st_accel_16bit_channels,
247 .odr = {
248 .addr = 0x20,
249 .mask = 0xf0,
250 .odr_avl = {
251 { .hz = 3, .value = 0x01, },
252 { .hz = 6, .value = 0x02, },
253 { .hz = 12, .value = 0x03, },
254 { .hz = 25, .value = 0x04, },
255 { .hz = 50, .value = 0x05, },
256 { .hz = 100, .value = 0x06, },
257 { .hz = 200, .value = 0x07, },
258 { .hz = 400, .value = 0x08, },
259 { .hz = 800, .value = 0x09, },
260 { .hz = 1600, .value = 0x0a, },
263 .pw = {
264 .addr = 0x20,
265 .mask = 0xf0,
266 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
268 .enable_axis = {
269 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
270 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
272 .fs = {
273 .addr = 0x24,
274 .mask = 0x38,
275 .fs_avl = {
276 [0] = {
277 .num = ST_ACCEL_FS_AVL_2G,
278 .value = 0x00,
279 .gain = IIO_G_TO_M_S_2(61),
281 [1] = {
282 .num = ST_ACCEL_FS_AVL_4G,
283 .value = 0x01,
284 .gain = IIO_G_TO_M_S_2(122),
286 [2] = {
287 .num = ST_ACCEL_FS_AVL_6G,
288 .value = 0x02,
289 .gain = IIO_G_TO_M_S_2(183),
291 [3] = {
292 .num = ST_ACCEL_FS_AVL_8G,
293 .value = 0x03,
294 .gain = IIO_G_TO_M_S_2(244),
296 [4] = {
297 .num = ST_ACCEL_FS_AVL_16G,
298 .value = 0x04,
299 .gain = IIO_G_TO_M_S_2(732),
303 .bdu = {
304 .addr = 0x20,
305 .mask = 0x08,
307 .drdy_irq = {
308 .addr = 0x23,
309 .mask_int1 = 0x80,
310 .mask_int2 = 0x00,
311 .addr_ihl = 0x23,
312 .mask_ihl = 0x40,
313 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
314 .ig1 = {
315 .en_addr = 0x23,
316 .en_mask = 0x08,
319 .multi_read_bit = false,
320 .bootime = 2,
323 .wai = 0x3a,
324 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
325 .sensors_supported = {
326 [0] = LIS3LV02DL_ACCEL_DEV_NAME,
328 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
329 .odr = {
330 .addr = 0x20,
331 .mask = 0x30, /* DF1 and DF0 */
332 .odr_avl = {
333 { .hz = 40, .value = 0x00, },
334 { .hz = 160, .value = 0x01, },
335 { .hz = 640, .value = 0x02, },
336 { .hz = 2560, .value = 0x03, },
339 .pw = {
340 .addr = 0x20,
341 .mask = 0xc0,
342 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
343 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
345 .enable_axis = {
346 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
347 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
349 .fs = {
350 .addr = 0x21,
351 .mask = 0x80,
352 .fs_avl = {
353 [0] = {
354 .num = ST_ACCEL_FS_AVL_2G,
355 .value = 0x00,
356 .gain = IIO_G_TO_M_S_2(1024),
358 [1] = {
359 .num = ST_ACCEL_FS_AVL_6G,
360 .value = 0x01,
361 .gain = IIO_G_TO_M_S_2(340),
365 .bdu = {
366 .addr = 0x21,
367 .mask = 0x40,
369 .drdy_irq = {
370 .addr = 0x21,
371 .mask_int1 = 0x04,
372 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
374 .multi_read_bit = true,
375 .bootime = 2, /* guess */
378 .wai = 0x3b,
379 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
380 .sensors_supported = {
381 [0] = LIS331DL_ACCEL_DEV_NAME,
383 .ch = (struct iio_chan_spec *)st_accel_8bit_channels,
384 .odr = {
385 .addr = 0x20,
386 .mask = 0x80,
387 .odr_avl = {
388 { .hz = 100, .value = 0x00, },
389 { .hz = 400, .value = 0x01, },
392 .pw = {
393 .addr = 0x20,
394 .mask = 0x40,
395 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
396 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
398 .enable_axis = {
399 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
400 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
402 .fs = {
403 .addr = 0x20,
404 .mask = 0x20,
406 * TODO: check these resulting gain settings, these are
407 * not in the datsheet
409 .fs_avl = {
410 [0] = {
411 .num = ST_ACCEL_FS_AVL_2G,
412 .value = 0x00,
413 .gain = IIO_G_TO_M_S_2(18000),
415 [1] = {
416 .num = ST_ACCEL_FS_AVL_8G,
417 .value = 0x01,
418 .gain = IIO_G_TO_M_S_2(72000),
422 .drdy_irq = {
423 .addr = 0x22,
424 .mask_int1 = 0x04,
425 .mask_int2 = 0x20,
426 .addr_ihl = 0x22,
427 .mask_ihl = 0x80,
428 .addr_od = 0x22,
429 .mask_od = 0x40,
430 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
432 .multi_read_bit = false,
433 .bootime = 2, /* guess */
436 .wai = 0x32,
437 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
438 .sensors_supported = {
439 [0] = H3LIS331DL_DRIVER_NAME,
441 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
442 .odr = {
443 .addr = 0x20,
444 .mask = 0x18,
445 .odr_avl = {
446 { .hz = 50, .value = 0x00, },
447 { .hz = 100, .value = 0x01, },
448 { .hz = 400, .value = 0x02, },
449 { .hz = 1000, .value = 0x03, },
452 .pw = {
453 .addr = 0x20,
454 .mask = 0x20,
455 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
456 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
458 .enable_axis = {
459 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
460 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
462 .fs = {
463 .addr = 0x23,
464 .mask = 0x30,
465 .fs_avl = {
466 [0] = {
467 .num = ST_ACCEL_FS_AVL_100G,
468 .value = 0x00,
469 .gain = IIO_G_TO_M_S_2(49000),
471 [1] = {
472 .num = ST_ACCEL_FS_AVL_200G,
473 .value = 0x01,
474 .gain = IIO_G_TO_M_S_2(98000),
476 [2] = {
477 .num = ST_ACCEL_FS_AVL_400G,
478 .value = 0x03,
479 .gain = IIO_G_TO_M_S_2(195000),
483 .bdu = {
484 .addr = 0x23,
485 .mask = 0x80,
487 .drdy_irq = {
488 .addr = 0x22,
489 .mask_int1 = 0x02,
490 .mask_int2 = 0x10,
491 .addr_ihl = 0x22,
492 .mask_ihl = 0x80,
494 .multi_read_bit = true,
495 .bootime = 2,
498 /* No WAI register present */
499 .sensors_supported = {
500 [0] = LIS3L02DQ_ACCEL_DEV_NAME,
502 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
503 .odr = {
504 .addr = 0x20,
505 .mask = 0x30,
506 .odr_avl = {
507 { .hz = 280, .value = 0x00, },
508 { .hz = 560, .value = 0x01, },
509 { .hz = 1120, .value = 0x02, },
510 { .hz = 4480, .value = 0x03, },
513 .pw = {
514 .addr = 0x20,
515 .mask = 0xc0,
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 .fs_avl = {
525 [0] = {
526 .num = ST_ACCEL_FS_AVL_2G,
527 .gain = IIO_G_TO_M_S_2(488),
532 * The part has a BDU bit but if set the data is never
533 * updated so don't set it.
535 .bdu = {
537 .drdy_irq = {
538 .addr = 0x21,
539 .mask_int1 = 0x04,
540 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
542 .multi_read_bit = false,
543 .bootime = 2,
546 .wai = 0x33,
547 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
548 .sensors_supported = {
549 [0] = LNG2DM_ACCEL_DEV_NAME,
551 .ch = (struct iio_chan_spec *)st_accel_8bit_channels,
552 .odr = {
553 .addr = 0x20,
554 .mask = 0xf0,
555 .odr_avl = {
556 { .hz = 1, .value = 0x01, },
557 { .hz = 10, .value = 0x02, },
558 { .hz = 25, .value = 0x03, },
559 { .hz = 50, .value = 0x04, },
560 { .hz = 100, .value = 0x05, },
561 { .hz = 200, .value = 0x06, },
562 { .hz = 400, .value = 0x07, },
563 { .hz = 1600, .value = 0x08, },
566 .pw = {
567 .addr = 0x20,
568 .mask = 0xf0,
569 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
571 .enable_axis = {
572 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
573 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
575 .fs = {
576 .addr = 0x23,
577 .mask = 0x30,
578 .fs_avl = {
579 [0] = {
580 .num = ST_ACCEL_FS_AVL_2G,
581 .value = 0x00,
582 .gain = IIO_G_TO_M_S_2(15600),
584 [1] = {
585 .num = ST_ACCEL_FS_AVL_4G,
586 .value = 0x01,
587 .gain = IIO_G_TO_M_S_2(31200),
589 [2] = {
590 .num = ST_ACCEL_FS_AVL_8G,
591 .value = 0x02,
592 .gain = IIO_G_TO_M_S_2(62500),
594 [3] = {
595 .num = ST_ACCEL_FS_AVL_16G,
596 .value = 0x03,
597 .gain = IIO_G_TO_M_S_2(187500),
601 .drdy_irq = {
602 .addr = 0x22,
603 .mask_int1 = 0x10,
604 .mask_int2 = 0x08,
605 .addr_ihl = 0x25,
606 .mask_ihl = 0x02,
607 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
609 .multi_read_bit = true,
610 .bootime = 2,
614 static int st_accel_read_raw(struct iio_dev *indio_dev,
615 struct iio_chan_spec const *ch, int *val,
616 int *val2, long mask)
618 int err;
619 struct st_sensor_data *adata = iio_priv(indio_dev);
621 switch (mask) {
622 case IIO_CHAN_INFO_RAW:
623 err = st_sensors_read_info_raw(indio_dev, ch, val);
624 if (err < 0)
625 goto read_error;
627 return IIO_VAL_INT;
628 case IIO_CHAN_INFO_SCALE:
629 *val = adata->current_fullscale->gain / 1000000;
630 *val2 = adata->current_fullscale->gain % 1000000;
631 return IIO_VAL_INT_PLUS_MICRO;
632 case IIO_CHAN_INFO_SAMP_FREQ:
633 *val = adata->odr;
634 return IIO_VAL_INT;
635 default:
636 return -EINVAL;
639 read_error:
640 return err;
643 static int st_accel_write_raw(struct iio_dev *indio_dev,
644 struct iio_chan_spec const *chan, int val, int val2, long mask)
646 int err;
648 switch (mask) {
649 case IIO_CHAN_INFO_SCALE: {
650 int gain;
652 gain = val * 1000000 + val2;
653 err = st_sensors_set_fullscale_by_gain(indio_dev, gain);
654 break;
656 case IIO_CHAN_INFO_SAMP_FREQ:
657 if (val2)
658 return -EINVAL;
659 mutex_lock(&indio_dev->mlock);
660 err = st_sensors_set_odr(indio_dev, val);
661 mutex_unlock(&indio_dev->mlock);
662 return err;
663 default:
664 return -EINVAL;
667 return err;
670 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
671 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_accel_scale_available);
673 static struct attribute *st_accel_attributes[] = {
674 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
675 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
676 NULL,
679 static const struct attribute_group st_accel_attribute_group = {
680 .attrs = st_accel_attributes,
683 static const struct iio_info accel_info = {
684 .driver_module = THIS_MODULE,
685 .attrs = &st_accel_attribute_group,
686 .read_raw = &st_accel_read_raw,
687 .write_raw = &st_accel_write_raw,
688 .debugfs_reg_access = &st_sensors_debugfs_reg_access,
691 #ifdef CONFIG_IIO_TRIGGER
692 static const struct iio_trigger_ops st_accel_trigger_ops = {
693 .owner = THIS_MODULE,
694 .set_trigger_state = ST_ACCEL_TRIGGER_SET_STATE,
695 .validate_device = st_sensors_validate_device,
697 #define ST_ACCEL_TRIGGER_OPS (&st_accel_trigger_ops)
698 #else
699 #define ST_ACCEL_TRIGGER_OPS NULL
700 #endif
702 int st_accel_common_probe(struct iio_dev *indio_dev)
704 struct st_sensor_data *adata = iio_priv(indio_dev);
705 int irq = adata->get_irq_data_ready(indio_dev);
706 int err;
708 indio_dev->modes = INDIO_DIRECT_MODE;
709 indio_dev->info = &accel_info;
710 mutex_init(&adata->tb.buf_lock);
712 err = st_sensors_power_enable(indio_dev);
713 if (err)
714 return err;
716 err = st_sensors_check_device_support(indio_dev,
717 ARRAY_SIZE(st_accel_sensors_settings),
718 st_accel_sensors_settings);
719 if (err < 0)
720 goto st_accel_power_off;
722 adata->num_data_channels = ST_ACCEL_NUMBER_DATA_CHANNELS;
723 adata->multiread_bit = adata->sensor_settings->multi_read_bit;
724 indio_dev->channels = adata->sensor_settings->ch;
725 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
727 adata->current_fullscale = (struct st_sensor_fullscale_avl *)
728 &adata->sensor_settings->fs.fs_avl[0];
729 adata->odr = adata->sensor_settings->odr.odr_avl[0].hz;
731 if (!adata->dev->platform_data)
732 adata->dev->platform_data =
733 (struct st_sensors_platform_data *)&default_accel_pdata;
735 err = st_sensors_init_sensor(indio_dev, adata->dev->platform_data);
736 if (err < 0)
737 goto st_accel_power_off;
739 err = st_accel_allocate_ring(indio_dev);
740 if (err < 0)
741 goto st_accel_power_off;
743 if (irq > 0) {
744 err = st_sensors_allocate_trigger(indio_dev,
745 ST_ACCEL_TRIGGER_OPS);
746 if (err < 0)
747 goto st_accel_probe_trigger_error;
750 err = iio_device_register(indio_dev);
751 if (err)
752 goto st_accel_device_register_error;
754 dev_info(&indio_dev->dev, "registered accelerometer %s\n",
755 indio_dev->name);
757 return 0;
759 st_accel_device_register_error:
760 if (irq > 0)
761 st_sensors_deallocate_trigger(indio_dev);
762 st_accel_probe_trigger_error:
763 st_accel_deallocate_ring(indio_dev);
764 st_accel_power_off:
765 st_sensors_power_disable(indio_dev);
767 return err;
769 EXPORT_SYMBOL(st_accel_common_probe);
771 void st_accel_common_remove(struct iio_dev *indio_dev)
773 struct st_sensor_data *adata = iio_priv(indio_dev);
775 st_sensors_power_disable(indio_dev);
777 iio_device_unregister(indio_dev);
778 if (adata->get_irq_data_ready(indio_dev) > 0)
779 st_sensors_deallocate_trigger(indio_dev);
781 st_accel_deallocate_ring(indio_dev);
783 EXPORT_SYMBOL(st_accel_common_remove);
785 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
786 MODULE_DESCRIPTION("STMicroelectronics accelerometers driver");
787 MODULE_LICENSE("GPL v2");