2 * STMicroelectronics gyroscopes 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/delay.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/trigger.h>
25 #include <linux/iio/buffer.h>
27 #include <linux/iio/common/st_sensors.h>
30 #define ST_GYRO_NUMBER_DATA_CHANNELS 3
32 /* DEFAULT VALUE FOR SENSORS */
33 #define ST_GYRO_DEFAULT_OUT_X_L_ADDR 0x28
34 #define ST_GYRO_DEFAULT_OUT_Y_L_ADDR 0x2a
35 #define ST_GYRO_DEFAULT_OUT_Z_L_ADDR 0x2c
38 #define ST_GYRO_FS_AVL_245DPS 245
39 #define ST_GYRO_FS_AVL_250DPS 250
40 #define ST_GYRO_FS_AVL_500DPS 500
41 #define ST_GYRO_FS_AVL_2000DPS 2000
43 static const struct iio_chan_spec st_gyro_16bit_channels
[] = {
44 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL
,
45 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
46 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 16, 16,
47 ST_GYRO_DEFAULT_OUT_X_L_ADDR
),
48 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL
,
49 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
50 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 16, 16,
51 ST_GYRO_DEFAULT_OUT_Y_L_ADDR
),
52 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL
,
53 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
54 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 16, 16,
55 ST_GYRO_DEFAULT_OUT_Z_L_ADDR
),
56 IIO_CHAN_SOFT_TIMESTAMP(3)
59 static const struct st_sensor_settings st_gyro_sensors_settings
[] = {
62 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
63 .sensors_supported
= {
64 [0] = L3G4200D_GYRO_DEV_NAME
,
65 [1] = LSM330DL_GYRO_DEV_NAME
,
67 .ch
= (struct iio_chan_spec
*)st_gyro_16bit_channels
,
72 { .hz
= 100, .value
= 0x00, },
73 { .hz
= 200, .value
= 0x01, },
74 { .hz
= 400, .value
= 0x02, },
75 { .hz
= 800, .value
= 0x03, },
81 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
82 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
85 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
86 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
93 .num
= ST_GYRO_FS_AVL_250DPS
,
95 .gain
= IIO_DEGREE_TO_RAD(8750),
98 .num
= ST_GYRO_FS_AVL_500DPS
,
100 .gain
= IIO_DEGREE_TO_RAD(17500),
103 .num
= ST_GYRO_FS_AVL_2000DPS
,
105 .gain
= IIO_DEGREE_TO_RAD(70000),
119 * The sensor has IHL (active low) and open
120 * drain settings, but only for INT1 and not
121 * for the DRDY line on INT2.
124 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
132 .multi_read_bit
= true,
137 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
138 .sensors_supported
= {
139 [0] = L3GD20_GYRO_DEV_NAME
,
140 [1] = LSM330D_GYRO_DEV_NAME
,
141 [2] = LSM330DLC_GYRO_DEV_NAME
,
142 [3] = L3G4IS_GYRO_DEV_NAME
,
143 [4] = LSM330_GYRO_DEV_NAME
,
145 .ch
= (struct iio_chan_spec
*)st_gyro_16bit_channels
,
150 { .hz
= 95, .value
= 0x00, },
151 { .hz
= 190, .value
= 0x01, },
152 { .hz
= 380, .value
= 0x02, },
153 { .hz
= 760, .value
= 0x03, },
159 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
160 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
163 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
164 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
171 .num
= ST_GYRO_FS_AVL_250DPS
,
173 .gain
= IIO_DEGREE_TO_RAD(8750),
176 .num
= ST_GYRO_FS_AVL_500DPS
,
178 .gain
= IIO_DEGREE_TO_RAD(17500),
181 .num
= ST_GYRO_FS_AVL_2000DPS
,
183 .gain
= IIO_DEGREE_TO_RAD(70000),
197 * The sensor has IHL (active low) and open
198 * drain settings, but only for INT1 and not
199 * for the DRDY line on INT2.
202 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
210 .multi_read_bit
= true,
215 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
216 .sensors_supported
= {
217 [0] = LSM9DS0_GYRO_DEV_NAME
,
219 .ch
= (struct iio_chan_spec
*)st_gyro_16bit_channels
,
222 .mask
= GENMASK(7, 6),
224 { .hz
= 95, .value
= 0x00, },
225 { .hz
= 190, .value
= 0x01, },
226 { .hz
= 380, .value
= 0x02, },
227 { .hz
= 760, .value
= 0x03, },
233 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
234 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
237 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
238 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
242 .mask
= GENMASK(5, 4),
245 .num
= ST_GYRO_FS_AVL_245DPS
,
247 .gain
= IIO_DEGREE_TO_RAD(8750),
250 .num
= ST_GYRO_FS_AVL_500DPS
,
252 .gain
= IIO_DEGREE_TO_RAD(17500),
255 .num
= ST_GYRO_FS_AVL_2000DPS
,
257 .gain
= IIO_DEGREE_TO_RAD(70000),
271 * The sensor has IHL (active low) and open
272 * drain settings, but only for INT1 and not
273 * for the DRDY line on INT2.
276 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
277 .mask
= GENMASK(2, 0),
284 .multi_read_bit
= true,
289 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
290 .sensors_supported
= {
291 [0] = L3GD20H_GYRO_DEV_NAME
,
293 .ch
= (struct iio_chan_spec
*)st_gyro_16bit_channels
,
298 { .hz
= 100, .value
= 0x00, },
299 { .hz
= 200, .value
= 0x01, },
300 { .hz
= 400, .value
= 0x02, },
301 { .hz
= 800, .value
= 0x03, },
307 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
308 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
311 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
312 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
319 .num
= ST_GYRO_FS_AVL_245DPS
,
321 .gain
= IIO_DEGREE_TO_RAD(8750),
324 .num
= ST_GYRO_FS_AVL_500DPS
,
326 .gain
= IIO_DEGREE_TO_RAD(17500),
329 .num
= ST_GYRO_FS_AVL_2000DPS
,
331 .gain
= IIO_DEGREE_TO_RAD(70000),
345 * The sensor has IHL (active low) and open
346 * drain settings, but only for INT1 and not
347 * for the DRDY line on INT2.
350 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
358 .multi_read_bit
= true,
363 static int st_gyro_read_raw(struct iio_dev
*indio_dev
,
364 struct iio_chan_spec
const *ch
, int *val
,
365 int *val2
, long mask
)
368 struct st_sensor_data
*gdata
= iio_priv(indio_dev
);
371 case IIO_CHAN_INFO_RAW
:
372 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
377 case IIO_CHAN_INFO_SCALE
:
379 *val2
= gdata
->current_fullscale
->gain
;
380 return IIO_VAL_INT_PLUS_MICRO
;
381 case IIO_CHAN_INFO_SAMP_FREQ
:
392 static int st_gyro_write_raw(struct iio_dev
*indio_dev
,
393 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
398 case IIO_CHAN_INFO_SCALE
:
399 err
= st_sensors_set_fullscale_by_gain(indio_dev
, val2
);
401 case IIO_CHAN_INFO_SAMP_FREQ
:
404 mutex_lock(&indio_dev
->mlock
);
405 err
= st_sensors_set_odr(indio_dev
, val
);
406 mutex_unlock(&indio_dev
->mlock
);
415 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
416 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_anglvel_scale_available
);
418 static struct attribute
*st_gyro_attributes
[] = {
419 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
420 &iio_dev_attr_in_anglvel_scale_available
.dev_attr
.attr
,
424 static const struct attribute_group st_gyro_attribute_group
= {
425 .attrs
= st_gyro_attributes
,
428 static const struct iio_info gyro_info
= {
429 .attrs
= &st_gyro_attribute_group
,
430 .read_raw
= &st_gyro_read_raw
,
431 .write_raw
= &st_gyro_write_raw
,
432 .debugfs_reg_access
= &st_sensors_debugfs_reg_access
,
435 #ifdef CONFIG_IIO_TRIGGER
436 static const struct iio_trigger_ops st_gyro_trigger_ops
= {
437 .set_trigger_state
= ST_GYRO_TRIGGER_SET_STATE
,
438 .validate_device
= st_sensors_validate_device
,
440 #define ST_GYRO_TRIGGER_OPS (&st_gyro_trigger_ops)
442 #define ST_GYRO_TRIGGER_OPS NULL
445 int st_gyro_common_probe(struct iio_dev
*indio_dev
)
447 struct st_sensor_data
*gdata
= iio_priv(indio_dev
);
448 int irq
= gdata
->get_irq_data_ready(indio_dev
);
451 indio_dev
->modes
= INDIO_DIRECT_MODE
;
452 indio_dev
->info
= &gyro_info
;
453 mutex_init(&gdata
->tb
.buf_lock
);
455 err
= st_sensors_power_enable(indio_dev
);
459 err
= st_sensors_check_device_support(indio_dev
,
460 ARRAY_SIZE(st_gyro_sensors_settings
),
461 st_gyro_sensors_settings
);
463 goto st_gyro_power_off
;
465 gdata
->num_data_channels
= ST_GYRO_NUMBER_DATA_CHANNELS
;
466 gdata
->multiread_bit
= gdata
->sensor_settings
->multi_read_bit
;
467 indio_dev
->channels
= gdata
->sensor_settings
->ch
;
468 indio_dev
->num_channels
= ST_SENSORS_NUMBER_ALL_CHANNELS
;
470 gdata
->current_fullscale
= (struct st_sensor_fullscale_avl
*)
471 &gdata
->sensor_settings
->fs
.fs_avl
[0];
472 gdata
->odr
= gdata
->sensor_settings
->odr
.odr_avl
[0].hz
;
474 err
= st_sensors_init_sensor(indio_dev
,
475 (struct st_sensors_platform_data
*)&gyro_pdata
);
477 goto st_gyro_power_off
;
479 err
= st_gyro_allocate_ring(indio_dev
);
481 goto st_gyro_power_off
;
484 err
= st_sensors_allocate_trigger(indio_dev
,
485 ST_GYRO_TRIGGER_OPS
);
487 goto st_gyro_probe_trigger_error
;
490 err
= iio_device_register(indio_dev
);
492 goto st_gyro_device_register_error
;
494 dev_info(&indio_dev
->dev
, "registered gyroscope %s\n",
499 st_gyro_device_register_error
:
501 st_sensors_deallocate_trigger(indio_dev
);
502 st_gyro_probe_trigger_error
:
503 st_gyro_deallocate_ring(indio_dev
);
505 st_sensors_power_disable(indio_dev
);
509 EXPORT_SYMBOL(st_gyro_common_probe
);
511 void st_gyro_common_remove(struct iio_dev
*indio_dev
)
513 struct st_sensor_data
*gdata
= iio_priv(indio_dev
);
515 st_sensors_power_disable(indio_dev
);
517 iio_device_unregister(indio_dev
);
518 if (gdata
->get_irq_data_ready(indio_dev
) > 0)
519 st_sensors_deallocate_trigger(indio_dev
);
521 st_gyro_deallocate_ring(indio_dev
);
523 EXPORT_SYMBOL(st_gyro_common_remove
);
525 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
526 MODULE_DESCRIPTION("STMicroelectronics gyroscopes driver");
527 MODULE_LICENSE("GPL v2");