x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / drivers / iio / accel / st_accel_core.c
blob784670e2736b4fbbc540382afc1aefe66ccc1e5c
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(1000),
358 [1] = {
359 .num = ST_ACCEL_FS_AVL_6G,
360 .value = 0x01,
361 .gain = IIO_G_TO_M_S_2(3000),
365 .bdu = {
366 .addr = 0x21,
367 .mask = 0x40,
370 * Data Alignment Setting - needs to be set to get
371 * left-justified data like all other sensors.
373 .das = {
374 .addr = 0x21,
375 .mask = 0x01,
377 .drdy_irq = {
378 .addr = 0x21,
379 .mask_int1 = 0x04,
380 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
382 .multi_read_bit = true,
383 .bootime = 2, /* guess */
386 .wai = 0x3b,
387 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
388 .sensors_supported = {
389 [0] = LIS331DL_ACCEL_DEV_NAME,
391 .ch = (struct iio_chan_spec *)st_accel_8bit_channels,
392 .odr = {
393 .addr = 0x20,
394 .mask = 0x80,
395 .odr_avl = {
396 { .hz = 100, .value = 0x00, },
397 { .hz = 400, .value = 0x01, },
400 .pw = {
401 .addr = 0x20,
402 .mask = 0x40,
403 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
404 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
406 .enable_axis = {
407 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
408 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
410 .fs = {
411 .addr = 0x20,
412 .mask = 0x20,
414 * TODO: check these resulting gain settings, these are
415 * not in the datsheet
417 .fs_avl = {
418 [0] = {
419 .num = ST_ACCEL_FS_AVL_2G,
420 .value = 0x00,
421 .gain = IIO_G_TO_M_S_2(18000),
423 [1] = {
424 .num = ST_ACCEL_FS_AVL_8G,
425 .value = 0x01,
426 .gain = IIO_G_TO_M_S_2(72000),
430 .drdy_irq = {
431 .addr = 0x22,
432 .mask_int1 = 0x04,
433 .mask_int2 = 0x20,
434 .addr_ihl = 0x22,
435 .mask_ihl = 0x80,
436 .addr_od = 0x22,
437 .mask_od = 0x40,
438 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
440 .multi_read_bit = false,
441 .bootime = 2, /* guess */
444 .wai = 0x32,
445 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
446 .sensors_supported = {
447 [0] = H3LIS331DL_DRIVER_NAME,
449 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
450 .odr = {
451 .addr = 0x20,
452 .mask = 0x18,
453 .odr_avl = {
454 { .hz = 50, .value = 0x00, },
455 { .hz = 100, .value = 0x01, },
456 { .hz = 400, .value = 0x02, },
457 { .hz = 1000, .value = 0x03, },
460 .pw = {
461 .addr = 0x20,
462 .mask = 0x20,
463 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
464 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
466 .enable_axis = {
467 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
468 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
470 .fs = {
471 .addr = 0x23,
472 .mask = 0x30,
473 .fs_avl = {
474 [0] = {
475 .num = ST_ACCEL_FS_AVL_100G,
476 .value = 0x00,
477 .gain = IIO_G_TO_M_S_2(49000),
479 [1] = {
480 .num = ST_ACCEL_FS_AVL_200G,
481 .value = 0x01,
482 .gain = IIO_G_TO_M_S_2(98000),
484 [2] = {
485 .num = ST_ACCEL_FS_AVL_400G,
486 .value = 0x03,
487 .gain = IIO_G_TO_M_S_2(195000),
491 .bdu = {
492 .addr = 0x23,
493 .mask = 0x80,
495 .drdy_irq = {
496 .addr = 0x22,
497 .mask_int1 = 0x02,
498 .mask_int2 = 0x10,
499 .addr_ihl = 0x22,
500 .mask_ihl = 0x80,
502 .multi_read_bit = true,
503 .bootime = 2,
506 /* No WAI register present */
507 .sensors_supported = {
508 [0] = LIS3L02DQ_ACCEL_DEV_NAME,
510 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
511 .odr = {
512 .addr = 0x20,
513 .mask = 0x30,
514 .odr_avl = {
515 { .hz = 280, .value = 0x00, },
516 { .hz = 560, .value = 0x01, },
517 { .hz = 1120, .value = 0x02, },
518 { .hz = 4480, .value = 0x03, },
521 .pw = {
522 .addr = 0x20,
523 .mask = 0xc0,
524 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
525 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
527 .enable_axis = {
528 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
529 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
531 .fs = {
532 .fs_avl = {
533 [0] = {
534 .num = ST_ACCEL_FS_AVL_2G,
535 .gain = IIO_G_TO_M_S_2(488),
540 * The part has a BDU bit but if set the data is never
541 * updated so don't set it.
543 .bdu = {
545 .drdy_irq = {
546 .addr = 0x21,
547 .mask_int1 = 0x04,
548 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
550 .multi_read_bit = false,
551 .bootime = 2,
554 .wai = 0x33,
555 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
556 .sensors_supported = {
557 [0] = LNG2DM_ACCEL_DEV_NAME,
559 .ch = (struct iio_chan_spec *)st_accel_8bit_channels,
560 .odr = {
561 .addr = 0x20,
562 .mask = 0xf0,
563 .odr_avl = {
564 { .hz = 1, .value = 0x01, },
565 { .hz = 10, .value = 0x02, },
566 { .hz = 25, .value = 0x03, },
567 { .hz = 50, .value = 0x04, },
568 { .hz = 100, .value = 0x05, },
569 { .hz = 200, .value = 0x06, },
570 { .hz = 400, .value = 0x07, },
571 { .hz = 1600, .value = 0x08, },
574 .pw = {
575 .addr = 0x20,
576 .mask = 0xf0,
577 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
579 .enable_axis = {
580 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
581 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
583 .fs = {
584 .addr = 0x23,
585 .mask = 0x30,
586 .fs_avl = {
587 [0] = {
588 .num = ST_ACCEL_FS_AVL_2G,
589 .value = 0x00,
590 .gain = IIO_G_TO_M_S_2(15600),
592 [1] = {
593 .num = ST_ACCEL_FS_AVL_4G,
594 .value = 0x01,
595 .gain = IIO_G_TO_M_S_2(31200),
597 [2] = {
598 .num = ST_ACCEL_FS_AVL_8G,
599 .value = 0x02,
600 .gain = IIO_G_TO_M_S_2(62500),
602 [3] = {
603 .num = ST_ACCEL_FS_AVL_16G,
604 .value = 0x03,
605 .gain = IIO_G_TO_M_S_2(187500),
609 .drdy_irq = {
610 .addr = 0x22,
611 .mask_int1 = 0x10,
612 .mask_int2 = 0x08,
613 .addr_ihl = 0x25,
614 .mask_ihl = 0x02,
615 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
617 .multi_read_bit = true,
618 .bootime = 2,
622 static int st_accel_read_raw(struct iio_dev *indio_dev,
623 struct iio_chan_spec const *ch, int *val,
624 int *val2, long mask)
626 int err;
627 struct st_sensor_data *adata = iio_priv(indio_dev);
629 switch (mask) {
630 case IIO_CHAN_INFO_RAW:
631 err = st_sensors_read_info_raw(indio_dev, ch, val);
632 if (err < 0)
633 goto read_error;
635 return IIO_VAL_INT;
636 case IIO_CHAN_INFO_SCALE:
637 *val = adata->current_fullscale->gain / 1000000;
638 *val2 = adata->current_fullscale->gain % 1000000;
639 return IIO_VAL_INT_PLUS_MICRO;
640 case IIO_CHAN_INFO_SAMP_FREQ:
641 *val = adata->odr;
642 return IIO_VAL_INT;
643 default:
644 return -EINVAL;
647 read_error:
648 return err;
651 static int st_accel_write_raw(struct iio_dev *indio_dev,
652 struct iio_chan_spec const *chan, int val, int val2, long mask)
654 int err;
656 switch (mask) {
657 case IIO_CHAN_INFO_SCALE: {
658 int gain;
660 gain = val * 1000000 + val2;
661 err = st_sensors_set_fullscale_by_gain(indio_dev, gain);
662 break;
664 case IIO_CHAN_INFO_SAMP_FREQ:
665 if (val2)
666 return -EINVAL;
667 mutex_lock(&indio_dev->mlock);
668 err = st_sensors_set_odr(indio_dev, val);
669 mutex_unlock(&indio_dev->mlock);
670 return err;
671 default:
672 return -EINVAL;
675 return err;
678 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
679 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_accel_scale_available);
681 static struct attribute *st_accel_attributes[] = {
682 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
683 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
684 NULL,
687 static const struct attribute_group st_accel_attribute_group = {
688 .attrs = st_accel_attributes,
691 static const struct iio_info accel_info = {
692 .driver_module = THIS_MODULE,
693 .attrs = &st_accel_attribute_group,
694 .read_raw = &st_accel_read_raw,
695 .write_raw = &st_accel_write_raw,
696 .debugfs_reg_access = &st_sensors_debugfs_reg_access,
699 #ifdef CONFIG_IIO_TRIGGER
700 static const struct iio_trigger_ops st_accel_trigger_ops = {
701 .owner = THIS_MODULE,
702 .set_trigger_state = ST_ACCEL_TRIGGER_SET_STATE,
703 .validate_device = st_sensors_validate_device,
705 #define ST_ACCEL_TRIGGER_OPS (&st_accel_trigger_ops)
706 #else
707 #define ST_ACCEL_TRIGGER_OPS NULL
708 #endif
710 int st_accel_common_probe(struct iio_dev *indio_dev)
712 struct st_sensor_data *adata = iio_priv(indio_dev);
713 int irq = adata->get_irq_data_ready(indio_dev);
714 int err;
716 indio_dev->modes = INDIO_DIRECT_MODE;
717 indio_dev->info = &accel_info;
718 mutex_init(&adata->tb.buf_lock);
720 err = st_sensors_power_enable(indio_dev);
721 if (err)
722 return err;
724 err = st_sensors_check_device_support(indio_dev,
725 ARRAY_SIZE(st_accel_sensors_settings),
726 st_accel_sensors_settings);
727 if (err < 0)
728 goto st_accel_power_off;
730 adata->num_data_channels = ST_ACCEL_NUMBER_DATA_CHANNELS;
731 adata->multiread_bit = adata->sensor_settings->multi_read_bit;
732 indio_dev->channels = adata->sensor_settings->ch;
733 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
735 adata->current_fullscale = (struct st_sensor_fullscale_avl *)
736 &adata->sensor_settings->fs.fs_avl[0];
737 adata->odr = adata->sensor_settings->odr.odr_avl[0].hz;
739 if (!adata->dev->platform_data)
740 adata->dev->platform_data =
741 (struct st_sensors_platform_data *)&default_accel_pdata;
743 err = st_sensors_init_sensor(indio_dev, adata->dev->platform_data);
744 if (err < 0)
745 goto st_accel_power_off;
747 err = st_accel_allocate_ring(indio_dev);
748 if (err < 0)
749 goto st_accel_power_off;
751 if (irq > 0) {
752 err = st_sensors_allocate_trigger(indio_dev,
753 ST_ACCEL_TRIGGER_OPS);
754 if (err < 0)
755 goto st_accel_probe_trigger_error;
758 err = iio_device_register(indio_dev);
759 if (err)
760 goto st_accel_device_register_error;
762 dev_info(&indio_dev->dev, "registered accelerometer %s\n",
763 indio_dev->name);
765 return 0;
767 st_accel_device_register_error:
768 if (irq > 0)
769 st_sensors_deallocate_trigger(indio_dev);
770 st_accel_probe_trigger_error:
771 st_accel_deallocate_ring(indio_dev);
772 st_accel_power_off:
773 st_sensors_power_disable(indio_dev);
775 return err;
777 EXPORT_SYMBOL(st_accel_common_probe);
779 void st_accel_common_remove(struct iio_dev *indio_dev)
781 struct st_sensor_data *adata = iio_priv(indio_dev);
783 st_sensors_power_disable(indio_dev);
785 iio_device_unregister(indio_dev);
786 if (adata->get_irq_data_ready(indio_dev) > 0)
787 st_sensors_deallocate_trigger(indio_dev);
789 st_accel_deallocate_ring(indio_dev);
791 EXPORT_SYMBOL(st_accel_common_remove);
793 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
794 MODULE_DESCRIPTION("STMicroelectronics accelerometers driver");
795 MODULE_LICENSE("GPL v2");