2 * STMicroelectronics accelerometers driver
4 * Copyright 2012-2013 STMicroelectronics Inc.
6 * Denis Ciocca <denis.ciocca@st.com>
8 * Licensed under the GPL-2.
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>
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
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
[] = {
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
,
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, },
125 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
128 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
129 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
136 .num
= ST_ACCEL_FS_AVL_2G
,
138 .gain
= IIO_G_TO_M_S_2(1000),
141 .num
= ST_ACCEL_FS_AVL_4G
,
143 .gain
= IIO_G_TO_M_S_2(2000),
146 .num
= ST_ACCEL_FS_AVL_8G
,
148 .gain
= IIO_G_TO_M_S_2(4000),
151 .num
= ST_ACCEL_FS_AVL_16G
,
153 .gain
= IIO_G_TO_M_S_2(12000),
167 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
169 .multi_read_bit
= true,
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
,
186 { .hz
= 50, .value
= 0x00, },
187 { .hz
= 100, .value
= 0x01, },
188 { .hz
= 400, .value
= 0x02, },
189 { .hz
= 1000, .value
= 0x03, },
195 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
196 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
199 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
200 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
207 .num
= ST_ACCEL_FS_AVL_2G
,
209 .gain
= IIO_G_TO_M_S_2(1000),
212 .num
= ST_ACCEL_FS_AVL_4G
,
214 .gain
= IIO_G_TO_M_S_2(2000),
217 .num
= ST_ACCEL_FS_AVL_8G
,
219 .gain
= IIO_G_TO_M_S_2(3900),
235 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
237 .multi_read_bit
= true,
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
,
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, },
266 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
269 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
270 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
277 .num
= ST_ACCEL_FS_AVL_2G
,
279 .gain
= IIO_G_TO_M_S_2(61),
282 .num
= ST_ACCEL_FS_AVL_4G
,
284 .gain
= IIO_G_TO_M_S_2(122),
287 .num
= ST_ACCEL_FS_AVL_6G
,
289 .gain
= IIO_G_TO_M_S_2(183),
292 .num
= ST_ACCEL_FS_AVL_8G
,
294 .gain
= IIO_G_TO_M_S_2(244),
297 .num
= ST_ACCEL_FS_AVL_16G
,
299 .gain
= IIO_G_TO_M_S_2(732),
313 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
319 .multi_read_bit
= false,
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
,
331 .mask
= 0x30, /* DF1 and DF0 */
333 { .hz
= 40, .value
= 0x00, },
334 { .hz
= 160, .value
= 0x01, },
335 { .hz
= 640, .value
= 0x02, },
336 { .hz
= 2560, .value
= 0x03, },
342 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
343 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
346 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
347 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
354 .num
= ST_ACCEL_FS_AVL_2G
,
356 .gain
= IIO_G_TO_M_S_2(1024),
359 .num
= ST_ACCEL_FS_AVL_6G
,
361 .gain
= IIO_G_TO_M_S_2(340),
372 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
374 .multi_read_bit
= true,
375 .bootime
= 2, /* guess */
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
,
388 { .hz
= 100, .value
= 0x00, },
389 { .hz
= 400, .value
= 0x01, },
395 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
396 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
399 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
400 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
406 * TODO: check these resulting gain settings, these are
407 * not in the datsheet
411 .num
= ST_ACCEL_FS_AVL_2G
,
413 .gain
= IIO_G_TO_M_S_2(18000),
416 .num
= ST_ACCEL_FS_AVL_8G
,
418 .gain
= IIO_G_TO_M_S_2(72000),
430 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
432 .multi_read_bit
= false,
433 .bootime
= 2, /* guess */
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
,
446 { .hz
= 50, .value
= 0x00, },
447 { .hz
= 100, .value
= 0x01, },
448 { .hz
= 400, .value
= 0x02, },
449 { .hz
= 1000, .value
= 0x03, },
455 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
456 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
459 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
460 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
467 .num
= ST_ACCEL_FS_AVL_100G
,
469 .gain
= IIO_G_TO_M_S_2(49000),
472 .num
= ST_ACCEL_FS_AVL_200G
,
474 .gain
= IIO_G_TO_M_S_2(98000),
477 .num
= ST_ACCEL_FS_AVL_400G
,
479 .gain
= IIO_G_TO_M_S_2(195000),
494 .multi_read_bit
= true,
498 /* No WAI register present */
499 .sensors_supported
= {
500 [0] = LIS3L02DQ_ACCEL_DEV_NAME
,
502 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
507 { .hz
= 280, .value
= 0x00, },
508 { .hz
= 560, .value
= 0x01, },
509 { .hz
= 1120, .value
= 0x02, },
510 { .hz
= 4480, .value
= 0x03, },
516 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
517 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
520 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
521 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
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.
540 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
542 .multi_read_bit
= false,
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
,
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, },
569 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
572 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
573 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
580 .num
= ST_ACCEL_FS_AVL_2G
,
582 .gain
= IIO_G_TO_M_S_2(15600),
585 .num
= ST_ACCEL_FS_AVL_4G
,
587 .gain
= IIO_G_TO_M_S_2(31200),
590 .num
= ST_ACCEL_FS_AVL_8G
,
592 .gain
= IIO_G_TO_M_S_2(62500),
595 .num
= ST_ACCEL_FS_AVL_16G
,
597 .gain
= IIO_G_TO_M_S_2(187500),
607 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
609 .multi_read_bit
= true,
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
)
619 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
622 case IIO_CHAN_INFO_RAW
:
623 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
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
:
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
)
649 case IIO_CHAN_INFO_SCALE
: {
652 gain
= val
* 1000000 + val2
;
653 err
= st_sensors_set_fullscale_by_gain(indio_dev
, gain
);
656 case IIO_CHAN_INFO_SAMP_FREQ
:
659 mutex_lock(&indio_dev
->mlock
);
660 err
= st_sensors_set_odr(indio_dev
, val
);
661 mutex_unlock(&indio_dev
->mlock
);
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
,
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)
699 #define ST_ACCEL_TRIGGER_OPS NULL
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
);
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
);
716 err
= st_sensors_check_device_support(indio_dev
,
717 ARRAY_SIZE(st_accel_sensors_settings
),
718 st_accel_sensors_settings
);
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
);
737 goto st_accel_power_off
;
739 err
= st_accel_allocate_ring(indio_dev
);
741 goto st_accel_power_off
;
744 err
= st_sensors_allocate_trigger(indio_dev
,
745 ST_ACCEL_TRIGGER_OPS
);
747 goto st_accel_probe_trigger_error
;
750 err
= iio_device_register(indio_dev
);
752 goto st_accel_device_register_error
;
754 dev_info(&indio_dev
->dev
, "registered accelerometer %s\n",
759 st_accel_device_register_error
:
761 st_sensors_deallocate_trigger(indio_dev
);
762 st_accel_probe_trigger_error
:
763 st_accel_deallocate_ring(indio_dev
);
765 st_sensors_power_disable(indio_dev
);
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");