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_250DPS 250
39 #define ST_GYRO_FS_AVL_500DPS 500
40 #define ST_GYRO_FS_AVL_2000DPS 2000
42 static const struct iio_chan_spec st_gyro_16bit_channels
[] = {
43 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL
,
44 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
45 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 16, 16,
46 ST_GYRO_DEFAULT_OUT_X_L_ADDR
),
47 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL
,
48 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
49 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 16, 16,
50 ST_GYRO_DEFAULT_OUT_Y_L_ADDR
),
51 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL
,
52 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
53 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 16, 16,
54 ST_GYRO_DEFAULT_OUT_Z_L_ADDR
),
55 IIO_CHAN_SOFT_TIMESTAMP(3)
58 static const struct st_sensor_settings st_gyro_sensors_settings
[] = {
61 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
62 .sensors_supported
= {
63 [0] = L3G4200D_GYRO_DEV_NAME
,
64 [1] = LSM330DL_GYRO_DEV_NAME
,
66 .ch
= (struct iio_chan_spec
*)st_gyro_16bit_channels
,
71 { .hz
= 100, .value
= 0x00, },
72 { .hz
= 200, .value
= 0x01, },
73 { .hz
= 400, .value
= 0x02, },
74 { .hz
= 800, .value
= 0x03, },
80 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
81 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
84 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
85 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
92 .num
= ST_GYRO_FS_AVL_250DPS
,
94 .gain
= IIO_DEGREE_TO_RAD(8750),
97 .num
= ST_GYRO_FS_AVL_500DPS
,
99 .gain
= IIO_DEGREE_TO_RAD(17500),
102 .num
= ST_GYRO_FS_AVL_2000DPS
,
104 .gain
= IIO_DEGREE_TO_RAD(70000),
116 * The sensor has IHL (active low) and open
117 * drain settings, but only for INT1 and not
118 * for the DRDY line on INT2.
120 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
122 .multi_read_bit
= true,
127 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
128 .sensors_supported
= {
129 [0] = L3GD20_GYRO_DEV_NAME
,
130 [1] = LSM330D_GYRO_DEV_NAME
,
131 [2] = LSM330DLC_GYRO_DEV_NAME
,
132 [3] = L3G4IS_GYRO_DEV_NAME
,
133 [4] = LSM330_GYRO_DEV_NAME
,
134 [5] = LSM9DS0_GYRO_DEV_NAME
,
136 .ch
= (struct iio_chan_spec
*)st_gyro_16bit_channels
,
141 { .hz
= 95, .value
= 0x00, },
142 { .hz
= 190, .value
= 0x01, },
143 { .hz
= 380, .value
= 0x02, },
144 { .hz
= 760, .value
= 0x03, },
150 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
151 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
154 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
155 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
162 .num
= ST_GYRO_FS_AVL_250DPS
,
164 .gain
= IIO_DEGREE_TO_RAD(8750),
167 .num
= ST_GYRO_FS_AVL_500DPS
,
169 .gain
= IIO_DEGREE_TO_RAD(17500),
172 .num
= ST_GYRO_FS_AVL_2000DPS
,
174 .gain
= IIO_DEGREE_TO_RAD(70000),
186 * The sensor has IHL (active low) and open
187 * drain settings, but only for INT1 and not
188 * for the DRDY line on INT2.
190 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
192 .multi_read_bit
= true,
197 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
198 .sensors_supported
= {
199 [0] = L3GD20_GYRO_DEV_NAME
,
201 .ch
= (struct iio_chan_spec
*)st_gyro_16bit_channels
,
206 { .hz
= 95, .value
= 0x00, },
207 { .hz
= 190, .value
= 0x01, },
208 { .hz
= 380, .value
= 0x02, },
209 { .hz
= 760, .value
= 0x03, },
215 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
216 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
219 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
220 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
227 .num
= ST_GYRO_FS_AVL_250DPS
,
229 .gain
= IIO_DEGREE_TO_RAD(8750),
232 .num
= ST_GYRO_FS_AVL_500DPS
,
234 .gain
= IIO_DEGREE_TO_RAD(17500),
237 .num
= ST_GYRO_FS_AVL_2000DPS
,
239 .gain
= IIO_DEGREE_TO_RAD(70000),
251 * The sensor has IHL (active low) and open
252 * drain settings, but only for INT1 and not
253 * for the DRDY line on INT2.
255 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
257 .multi_read_bit
= true,
262 static int st_gyro_read_raw(struct iio_dev
*indio_dev
,
263 struct iio_chan_spec
const *ch
, int *val
,
264 int *val2
, long mask
)
267 struct st_sensor_data
*gdata
= iio_priv(indio_dev
);
270 case IIO_CHAN_INFO_RAW
:
271 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
276 case IIO_CHAN_INFO_SCALE
:
278 *val2
= gdata
->current_fullscale
->gain
;
279 return IIO_VAL_INT_PLUS_MICRO
;
280 case IIO_CHAN_INFO_SAMP_FREQ
:
291 static int st_gyro_write_raw(struct iio_dev
*indio_dev
,
292 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
297 case IIO_CHAN_INFO_SCALE
:
298 err
= st_sensors_set_fullscale_by_gain(indio_dev
, val2
);
300 case IIO_CHAN_INFO_SAMP_FREQ
:
303 mutex_lock(&indio_dev
->mlock
);
304 err
= st_sensors_set_odr(indio_dev
, val
);
305 mutex_unlock(&indio_dev
->mlock
);
314 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
315 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_anglvel_scale_available
);
317 static struct attribute
*st_gyro_attributes
[] = {
318 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
319 &iio_dev_attr_in_anglvel_scale_available
.dev_attr
.attr
,
323 static const struct attribute_group st_gyro_attribute_group
= {
324 .attrs
= st_gyro_attributes
,
327 static const struct iio_info gyro_info
= {
328 .driver_module
= THIS_MODULE
,
329 .attrs
= &st_gyro_attribute_group
,
330 .read_raw
= &st_gyro_read_raw
,
331 .write_raw
= &st_gyro_write_raw
,
332 .debugfs_reg_access
= &st_sensors_debugfs_reg_access
,
335 #ifdef CONFIG_IIO_TRIGGER
336 static const struct iio_trigger_ops st_gyro_trigger_ops
= {
337 .owner
= THIS_MODULE
,
338 .set_trigger_state
= ST_GYRO_TRIGGER_SET_STATE
,
339 .validate_device
= st_sensors_validate_device
,
341 #define ST_GYRO_TRIGGER_OPS (&st_gyro_trigger_ops)
343 #define ST_GYRO_TRIGGER_OPS NULL
346 int st_gyro_common_probe(struct iio_dev
*indio_dev
)
348 struct st_sensor_data
*gdata
= iio_priv(indio_dev
);
349 int irq
= gdata
->get_irq_data_ready(indio_dev
);
352 indio_dev
->modes
= INDIO_DIRECT_MODE
;
353 indio_dev
->info
= &gyro_info
;
354 mutex_init(&gdata
->tb
.buf_lock
);
356 err
= st_sensors_power_enable(indio_dev
);
360 err
= st_sensors_check_device_support(indio_dev
,
361 ARRAY_SIZE(st_gyro_sensors_settings
),
362 st_gyro_sensors_settings
);
364 goto st_gyro_power_off
;
366 gdata
->num_data_channels
= ST_GYRO_NUMBER_DATA_CHANNELS
;
367 gdata
->multiread_bit
= gdata
->sensor_settings
->multi_read_bit
;
368 indio_dev
->channels
= gdata
->sensor_settings
->ch
;
369 indio_dev
->num_channels
= ST_SENSORS_NUMBER_ALL_CHANNELS
;
371 gdata
->current_fullscale
= (struct st_sensor_fullscale_avl
*)
372 &gdata
->sensor_settings
->fs
.fs_avl
[0];
373 gdata
->odr
= gdata
->sensor_settings
->odr
.odr_avl
[0].hz
;
375 err
= st_sensors_init_sensor(indio_dev
,
376 (struct st_sensors_platform_data
*)&gyro_pdata
);
378 goto st_gyro_power_off
;
380 err
= st_gyro_allocate_ring(indio_dev
);
382 goto st_gyro_power_off
;
385 err
= st_sensors_allocate_trigger(indio_dev
,
386 ST_GYRO_TRIGGER_OPS
);
388 goto st_gyro_probe_trigger_error
;
391 err
= iio_device_register(indio_dev
);
393 goto st_gyro_device_register_error
;
395 dev_info(&indio_dev
->dev
, "registered gyroscope %s\n",
400 st_gyro_device_register_error
:
402 st_sensors_deallocate_trigger(indio_dev
);
403 st_gyro_probe_trigger_error
:
404 st_gyro_deallocate_ring(indio_dev
);
406 st_sensors_power_disable(indio_dev
);
410 EXPORT_SYMBOL(st_gyro_common_probe
);
412 void st_gyro_common_remove(struct iio_dev
*indio_dev
)
414 struct st_sensor_data
*gdata
= iio_priv(indio_dev
);
416 st_sensors_power_disable(indio_dev
);
418 iio_device_unregister(indio_dev
);
419 if (gdata
->get_irq_data_ready(indio_dev
) > 0)
420 st_sensors_deallocate_trigger(indio_dev
);
422 st_gyro_deallocate_ring(indio_dev
);
424 EXPORT_SYMBOL(st_gyro_common_remove
);
426 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
427 MODULE_DESCRIPTION("STMicroelectronics gyroscopes driver");
428 MODULE_LICENSE("GPL v2");