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(1000),
359 .num
= ST_ACCEL_FS_AVL_6G
,
361 .gain
= IIO_G_TO_M_S_2(3000),
370 * Data Alignment Setting - needs to be set to get
371 * left-justified data like all other sensors.
380 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
382 .multi_read_bit
= true,
383 .bootime
= 2, /* guess */
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
,
396 { .hz
= 100, .value
= 0x00, },
397 { .hz
= 400, .value
= 0x01, },
403 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
404 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
407 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
408 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
414 * TODO: check these resulting gain settings, these are
415 * not in the datsheet
419 .num
= ST_ACCEL_FS_AVL_2G
,
421 .gain
= IIO_G_TO_M_S_2(18000),
424 .num
= ST_ACCEL_FS_AVL_8G
,
426 .gain
= IIO_G_TO_M_S_2(72000),
438 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
440 .multi_read_bit
= false,
441 .bootime
= 2, /* guess */
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
,
454 { .hz
= 50, .value
= 0x00, },
455 { .hz
= 100, .value
= 0x01, },
456 { .hz
= 400, .value
= 0x02, },
457 { .hz
= 1000, .value
= 0x03, },
463 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
464 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
467 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
468 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
475 .num
= ST_ACCEL_FS_AVL_100G
,
477 .gain
= IIO_G_TO_M_S_2(49000),
480 .num
= ST_ACCEL_FS_AVL_200G
,
482 .gain
= IIO_G_TO_M_S_2(98000),
485 .num
= ST_ACCEL_FS_AVL_400G
,
487 .gain
= IIO_G_TO_M_S_2(195000),
502 .multi_read_bit
= true,
506 /* No WAI register present */
507 .sensors_supported
= {
508 [0] = LIS3L02DQ_ACCEL_DEV_NAME
,
510 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
515 { .hz
= 280, .value
= 0x00, },
516 { .hz
= 560, .value
= 0x01, },
517 { .hz
= 1120, .value
= 0x02, },
518 { .hz
= 4480, .value
= 0x03, },
524 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
525 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
528 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
529 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
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.
548 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
550 .multi_read_bit
= false,
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
,
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, },
577 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
580 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
581 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
588 .num
= ST_ACCEL_FS_AVL_2G
,
590 .gain
= IIO_G_TO_M_S_2(15600),
593 .num
= ST_ACCEL_FS_AVL_4G
,
595 .gain
= IIO_G_TO_M_S_2(31200),
598 .num
= ST_ACCEL_FS_AVL_8G
,
600 .gain
= IIO_G_TO_M_S_2(62500),
603 .num
= ST_ACCEL_FS_AVL_16G
,
605 .gain
= IIO_G_TO_M_S_2(187500),
615 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
617 .multi_read_bit
= true,
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
)
627 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
630 case IIO_CHAN_INFO_RAW
:
631 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
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
:
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
)
657 case IIO_CHAN_INFO_SCALE
: {
660 gain
= val
* 1000000 + val2
;
661 err
= st_sensors_set_fullscale_by_gain(indio_dev
, gain
);
664 case IIO_CHAN_INFO_SAMP_FREQ
:
667 mutex_lock(&indio_dev
->mlock
);
668 err
= st_sensors_set_odr(indio_dev
, val
);
669 mutex_unlock(&indio_dev
->mlock
);
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
,
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)
707 #define ST_ACCEL_TRIGGER_OPS NULL
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
);
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
);
724 err
= st_sensors_check_device_support(indio_dev
,
725 ARRAY_SIZE(st_accel_sensors_settings
),
726 st_accel_sensors_settings
);
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
);
745 goto st_accel_power_off
;
747 err
= st_accel_allocate_ring(indio_dev
);
749 goto st_accel_power_off
;
752 err
= st_sensors_allocate_trigger(indio_dev
,
753 ST_ACCEL_TRIGGER_OPS
);
755 goto st_accel_probe_trigger_error
;
758 err
= iio_device_register(indio_dev
);
760 goto st_accel_device_register_error
;
762 dev_info(&indio_dev
->dev
, "registered accelerometer %s\n",
767 st_accel_device_register_error
:
769 st_sensors_deallocate_trigger(indio_dev
);
770 st_accel_probe_trigger_error
:
771 st_accel_deallocate_ring(indio_dev
);
773 st_sensors_power_disable(indio_dev
);
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");