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
,
144 [5] = LSM9DS0_GYRO_DEV_NAME
,
146 .ch
= (struct iio_chan_spec
*)st_gyro_16bit_channels
,
151 { .hz
= 95, .value
= 0x00, },
152 { .hz
= 190, .value
= 0x01, },
153 { .hz
= 380, .value
= 0x02, },
154 { .hz
= 760, .value
= 0x03, },
160 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
161 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
164 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
165 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
172 .num
= ST_GYRO_FS_AVL_250DPS
,
174 .gain
= IIO_DEGREE_TO_RAD(8750),
177 .num
= ST_GYRO_FS_AVL_500DPS
,
179 .gain
= IIO_DEGREE_TO_RAD(17500),
182 .num
= ST_GYRO_FS_AVL_2000DPS
,
184 .gain
= IIO_DEGREE_TO_RAD(70000),
198 * The sensor has IHL (active low) and open
199 * drain settings, but only for INT1 and not
200 * for the DRDY line on INT2.
203 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
211 .multi_read_bit
= true,
216 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
217 .sensors_supported
= {
218 [0] = L3GD20H_GYRO_DEV_NAME
,
220 .ch
= (struct iio_chan_spec
*)st_gyro_16bit_channels
,
225 { .hz
= 100, .value
= 0x00, },
226 { .hz
= 200, .value
= 0x01, },
227 { .hz
= 400, .value
= 0x02, },
228 { .hz
= 800, .value
= 0x03, },
234 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
235 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
238 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
239 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
246 .num
= ST_GYRO_FS_AVL_245DPS
,
248 .gain
= IIO_DEGREE_TO_RAD(8750),
251 .num
= ST_GYRO_FS_AVL_500DPS
,
253 .gain
= IIO_DEGREE_TO_RAD(17500),
256 .num
= ST_GYRO_FS_AVL_2000DPS
,
258 .gain
= IIO_DEGREE_TO_RAD(70000),
272 * The sensor has IHL (active low) and open
273 * drain settings, but only for INT1 and not
274 * for the DRDY line on INT2.
277 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
285 .multi_read_bit
= true,
290 static int st_gyro_read_raw(struct iio_dev
*indio_dev
,
291 struct iio_chan_spec
const *ch
, int *val
,
292 int *val2
, long mask
)
295 struct st_sensor_data
*gdata
= iio_priv(indio_dev
);
298 case IIO_CHAN_INFO_RAW
:
299 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
304 case IIO_CHAN_INFO_SCALE
:
306 *val2
= gdata
->current_fullscale
->gain
;
307 return IIO_VAL_INT_PLUS_MICRO
;
308 case IIO_CHAN_INFO_SAMP_FREQ
:
319 static int st_gyro_write_raw(struct iio_dev
*indio_dev
,
320 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
325 case IIO_CHAN_INFO_SCALE
:
326 err
= st_sensors_set_fullscale_by_gain(indio_dev
, val2
);
328 case IIO_CHAN_INFO_SAMP_FREQ
:
331 mutex_lock(&indio_dev
->mlock
);
332 err
= st_sensors_set_odr(indio_dev
, val
);
333 mutex_unlock(&indio_dev
->mlock
);
342 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
343 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_anglvel_scale_available
);
345 static struct attribute
*st_gyro_attributes
[] = {
346 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
347 &iio_dev_attr_in_anglvel_scale_available
.dev_attr
.attr
,
351 static const struct attribute_group st_gyro_attribute_group
= {
352 .attrs
= st_gyro_attributes
,
355 static const struct iio_info gyro_info
= {
356 .attrs
= &st_gyro_attribute_group
,
357 .read_raw
= &st_gyro_read_raw
,
358 .write_raw
= &st_gyro_write_raw
,
359 .debugfs_reg_access
= &st_sensors_debugfs_reg_access
,
362 #ifdef CONFIG_IIO_TRIGGER
363 static const struct iio_trigger_ops st_gyro_trigger_ops
= {
364 .set_trigger_state
= ST_GYRO_TRIGGER_SET_STATE
,
365 .validate_device
= st_sensors_validate_device
,
367 #define ST_GYRO_TRIGGER_OPS (&st_gyro_trigger_ops)
369 #define ST_GYRO_TRIGGER_OPS NULL
372 int st_gyro_common_probe(struct iio_dev
*indio_dev
)
374 struct st_sensor_data
*gdata
= iio_priv(indio_dev
);
375 int irq
= gdata
->get_irq_data_ready(indio_dev
);
378 indio_dev
->modes
= INDIO_DIRECT_MODE
;
379 indio_dev
->info
= &gyro_info
;
380 mutex_init(&gdata
->tb
.buf_lock
);
382 err
= st_sensors_power_enable(indio_dev
);
386 err
= st_sensors_check_device_support(indio_dev
,
387 ARRAY_SIZE(st_gyro_sensors_settings
),
388 st_gyro_sensors_settings
);
390 goto st_gyro_power_off
;
392 gdata
->num_data_channels
= ST_GYRO_NUMBER_DATA_CHANNELS
;
393 gdata
->multiread_bit
= gdata
->sensor_settings
->multi_read_bit
;
394 indio_dev
->channels
= gdata
->sensor_settings
->ch
;
395 indio_dev
->num_channels
= ST_SENSORS_NUMBER_ALL_CHANNELS
;
397 gdata
->current_fullscale
= (struct st_sensor_fullscale_avl
*)
398 &gdata
->sensor_settings
->fs
.fs_avl
[0];
399 gdata
->odr
= gdata
->sensor_settings
->odr
.odr_avl
[0].hz
;
401 err
= st_sensors_init_sensor(indio_dev
,
402 (struct st_sensors_platform_data
*)&gyro_pdata
);
404 goto st_gyro_power_off
;
406 err
= st_gyro_allocate_ring(indio_dev
);
408 goto st_gyro_power_off
;
411 err
= st_sensors_allocate_trigger(indio_dev
,
412 ST_GYRO_TRIGGER_OPS
);
414 goto st_gyro_probe_trigger_error
;
417 err
= iio_device_register(indio_dev
);
419 goto st_gyro_device_register_error
;
421 dev_info(&indio_dev
->dev
, "registered gyroscope %s\n",
426 st_gyro_device_register_error
:
428 st_sensors_deallocate_trigger(indio_dev
);
429 st_gyro_probe_trigger_error
:
430 st_gyro_deallocate_ring(indio_dev
);
432 st_sensors_power_disable(indio_dev
);
436 EXPORT_SYMBOL(st_gyro_common_probe
);
438 void st_gyro_common_remove(struct iio_dev
*indio_dev
)
440 struct st_sensor_data
*gdata
= iio_priv(indio_dev
);
442 st_sensors_power_disable(indio_dev
);
444 iio_device_unregister(indio_dev
);
445 if (gdata
->get_irq_data_ready(indio_dev
) > 0)
446 st_sensors_deallocate_trigger(indio_dev
);
448 st_gyro_deallocate_ring(indio_dev
);
450 EXPORT_SYMBOL(st_gyro_common_remove
);
452 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
453 MODULE_DESCRIPTION("STMicroelectronics gyroscopes driver");
454 MODULE_LICENSE("GPL v2");