1 // SPDX-License-Identifier: GPL-2.0-only
3 * STMicroelectronics gyroscopes driver
5 * Copyright 2012-2013 STMicroelectronics Inc.
7 * Denis Ciocca <denis.ciocca@st.com>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/errno.h>
14 #include <linux/types.h>
15 #include <linux/interrupt.h>
16 #include <linux/i2c.h>
17 #include <linux/irq.h>
18 #include <linux/delay.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/trigger.h>
22 #include <linux/iio/buffer.h>
24 #include <linux/iio/common/st_sensors.h>
27 #define ST_GYRO_NUMBER_DATA_CHANNELS 3
29 /* DEFAULT VALUE FOR SENSORS */
30 #define ST_GYRO_DEFAULT_OUT_X_L_ADDR 0x28
31 #define ST_GYRO_DEFAULT_OUT_Y_L_ADDR 0x2a
32 #define ST_GYRO_DEFAULT_OUT_Z_L_ADDR 0x2c
35 #define ST_GYRO_FS_AVL_245DPS 245
36 #define ST_GYRO_FS_AVL_250DPS 250
37 #define ST_GYRO_FS_AVL_500DPS 500
38 #define ST_GYRO_FS_AVL_2000DPS 2000
40 static const struct iio_chan_spec st_gyro_16bit_channels
[] = {
41 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL
,
42 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
43 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 16, 16,
44 ST_GYRO_DEFAULT_OUT_X_L_ADDR
),
45 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL
,
46 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
47 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 16, 16,
48 ST_GYRO_DEFAULT_OUT_Y_L_ADDR
),
49 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL
,
50 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
51 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 16, 16,
52 ST_GYRO_DEFAULT_OUT_Z_L_ADDR
),
53 IIO_CHAN_SOFT_TIMESTAMP(3)
56 static const struct st_sensor_settings st_gyro_sensors_settings
[] = {
59 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
60 .sensors_supported
= {
61 [0] = L3G4200D_GYRO_DEV_NAME
,
62 [1] = LSM330DL_GYRO_DEV_NAME
,
64 .ch
= (struct iio_chan_spec
*)st_gyro_16bit_channels
,
69 { .hz
= 100, .value
= 0x00, },
70 { .hz
= 200, .value
= 0x01, },
71 { .hz
= 400, .value
= 0x02, },
72 { .hz
= 800, .value
= 0x03, },
78 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
79 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
82 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
83 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
90 .num
= ST_GYRO_FS_AVL_250DPS
,
92 .gain
= IIO_DEGREE_TO_RAD(8750),
95 .num
= ST_GYRO_FS_AVL_500DPS
,
97 .gain
= IIO_DEGREE_TO_RAD(17500),
100 .num
= ST_GYRO_FS_AVL_2000DPS
,
102 .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.
121 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
129 .multi_read_bit
= true,
134 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
135 .sensors_supported
= {
136 [0] = L3GD20_GYRO_DEV_NAME
,
137 [1] = LSM330D_GYRO_DEV_NAME
,
138 [2] = LSM330DLC_GYRO_DEV_NAME
,
139 [3] = L3G4IS_GYRO_DEV_NAME
,
140 [4] = LSM330_GYRO_DEV_NAME
,
142 .ch
= (struct iio_chan_spec
*)st_gyro_16bit_channels
,
147 { .hz
= 95, .value
= 0x00, },
148 { .hz
= 190, .value
= 0x01, },
149 { .hz
= 380, .value
= 0x02, },
150 { .hz
= 760, .value
= 0x03, },
156 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
157 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
160 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
161 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
168 .num
= ST_GYRO_FS_AVL_250DPS
,
170 .gain
= IIO_DEGREE_TO_RAD(8750),
173 .num
= ST_GYRO_FS_AVL_500DPS
,
175 .gain
= IIO_DEGREE_TO_RAD(17500),
178 .num
= ST_GYRO_FS_AVL_2000DPS
,
180 .gain
= IIO_DEGREE_TO_RAD(70000),
194 * The sensor has IHL (active low) and open
195 * drain settings, but only for INT1 and not
196 * for the DRDY line on INT2.
199 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
207 .multi_read_bit
= true,
212 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
213 .sensors_supported
= {
214 [0] = LSM9DS0_GYRO_DEV_NAME
,
216 .ch
= (struct iio_chan_spec
*)st_gyro_16bit_channels
,
219 .mask
= GENMASK(7, 6),
221 { .hz
= 95, .value
= 0x00, },
222 { .hz
= 190, .value
= 0x01, },
223 { .hz
= 380, .value
= 0x02, },
224 { .hz
= 760, .value
= 0x03, },
230 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
231 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
234 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
235 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
239 .mask
= GENMASK(5, 4),
242 .num
= ST_GYRO_FS_AVL_245DPS
,
244 .gain
= IIO_DEGREE_TO_RAD(8750),
247 .num
= ST_GYRO_FS_AVL_500DPS
,
249 .gain
= IIO_DEGREE_TO_RAD(17500),
252 .num
= ST_GYRO_FS_AVL_2000DPS
,
254 .gain
= IIO_DEGREE_TO_RAD(70000),
268 * The sensor has IHL (active low) and open
269 * drain settings, but only for INT1 and not
270 * for the DRDY line on INT2.
273 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
274 .mask
= GENMASK(2, 0),
281 .multi_read_bit
= true,
286 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
287 .sensors_supported
= {
288 [0] = L3GD20H_GYRO_DEV_NAME
,
290 .ch
= (struct iio_chan_spec
*)st_gyro_16bit_channels
,
295 { .hz
= 100, .value
= 0x00, },
296 { .hz
= 200, .value
= 0x01, },
297 { .hz
= 400, .value
= 0x02, },
298 { .hz
= 800, .value
= 0x03, },
304 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
305 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
308 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
309 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
316 .num
= ST_GYRO_FS_AVL_245DPS
,
318 .gain
= IIO_DEGREE_TO_RAD(8750),
321 .num
= ST_GYRO_FS_AVL_500DPS
,
323 .gain
= IIO_DEGREE_TO_RAD(17500),
326 .num
= ST_GYRO_FS_AVL_2000DPS
,
328 .gain
= IIO_DEGREE_TO_RAD(70000),
342 * The sensor has IHL (active low) and open
343 * drain settings, but only for INT1 and not
344 * for the DRDY line on INT2.
347 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
355 .multi_read_bit
= true,
360 static int st_gyro_read_raw(struct iio_dev
*indio_dev
,
361 struct iio_chan_spec
const *ch
, int *val
,
362 int *val2
, long mask
)
365 struct st_sensor_data
*gdata
= iio_priv(indio_dev
);
368 case IIO_CHAN_INFO_RAW
:
369 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
374 case IIO_CHAN_INFO_SCALE
:
376 *val2
= gdata
->current_fullscale
->gain
;
377 return IIO_VAL_INT_PLUS_MICRO
;
378 case IIO_CHAN_INFO_SAMP_FREQ
:
389 static int st_gyro_write_raw(struct iio_dev
*indio_dev
,
390 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
395 case IIO_CHAN_INFO_SCALE
:
396 err
= st_sensors_set_fullscale_by_gain(indio_dev
, val2
);
398 case IIO_CHAN_INFO_SAMP_FREQ
:
401 mutex_lock(&indio_dev
->mlock
);
402 err
= st_sensors_set_odr(indio_dev
, val
);
403 mutex_unlock(&indio_dev
->mlock
);
412 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
413 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_anglvel_scale_available
);
415 static struct attribute
*st_gyro_attributes
[] = {
416 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
417 &iio_dev_attr_in_anglvel_scale_available
.dev_attr
.attr
,
421 static const struct attribute_group st_gyro_attribute_group
= {
422 .attrs
= st_gyro_attributes
,
425 static const struct iio_info gyro_info
= {
426 .attrs
= &st_gyro_attribute_group
,
427 .read_raw
= &st_gyro_read_raw
,
428 .write_raw
= &st_gyro_write_raw
,
429 .debugfs_reg_access
= &st_sensors_debugfs_reg_access
,
432 #ifdef CONFIG_IIO_TRIGGER
433 static const struct iio_trigger_ops st_gyro_trigger_ops
= {
434 .set_trigger_state
= ST_GYRO_TRIGGER_SET_STATE
,
435 .validate_device
= st_sensors_validate_device
,
437 #define ST_GYRO_TRIGGER_OPS (&st_gyro_trigger_ops)
439 #define ST_GYRO_TRIGGER_OPS NULL
443 * st_gyro_get_settings() - get sensor settings from device name
444 * @name: device name buffer reference.
446 * Return: valid reference on success, NULL otherwise.
448 const struct st_sensor_settings
*st_gyro_get_settings(const char *name
)
450 int index
= st_sensors_get_settings_index(name
,
451 st_gyro_sensors_settings
,
452 ARRAY_SIZE(st_gyro_sensors_settings
));
456 return &st_gyro_sensors_settings
[index
];
458 EXPORT_SYMBOL(st_gyro_get_settings
);
460 int st_gyro_common_probe(struct iio_dev
*indio_dev
)
462 struct st_sensor_data
*gdata
= iio_priv(indio_dev
);
463 struct st_sensors_platform_data
*pdata
;
466 indio_dev
->modes
= INDIO_DIRECT_MODE
;
467 indio_dev
->info
= &gyro_info
;
469 err
= st_sensors_power_enable(indio_dev
);
473 err
= st_sensors_verify_id(indio_dev
);
475 goto st_gyro_power_off
;
477 gdata
->num_data_channels
= ST_GYRO_NUMBER_DATA_CHANNELS
;
478 indio_dev
->channels
= gdata
->sensor_settings
->ch
;
479 indio_dev
->num_channels
= ST_SENSORS_NUMBER_ALL_CHANNELS
;
481 gdata
->current_fullscale
= &gdata
->sensor_settings
->fs
.fs_avl
[0];
482 gdata
->odr
= gdata
->sensor_settings
->odr
.odr_avl
[0].hz
;
484 pdata
= (struct st_sensors_platform_data
*)&gyro_pdata
;
486 err
= st_sensors_init_sensor(indio_dev
, pdata
);
488 goto st_gyro_power_off
;
490 err
= st_gyro_allocate_ring(indio_dev
);
492 goto st_gyro_power_off
;
494 if (gdata
->irq
> 0) {
495 err
= st_sensors_allocate_trigger(indio_dev
,
496 ST_GYRO_TRIGGER_OPS
);
498 goto st_gyro_probe_trigger_error
;
501 err
= iio_device_register(indio_dev
);
503 goto st_gyro_device_register_error
;
505 dev_info(&indio_dev
->dev
, "registered gyroscope %s\n",
510 st_gyro_device_register_error
:
512 st_sensors_deallocate_trigger(indio_dev
);
513 st_gyro_probe_trigger_error
:
514 st_gyro_deallocate_ring(indio_dev
);
516 st_sensors_power_disable(indio_dev
);
520 EXPORT_SYMBOL(st_gyro_common_probe
);
522 void st_gyro_common_remove(struct iio_dev
*indio_dev
)
524 struct st_sensor_data
*gdata
= iio_priv(indio_dev
);
526 st_sensors_power_disable(indio_dev
);
528 iio_device_unregister(indio_dev
);
530 st_sensors_deallocate_trigger(indio_dev
);
532 st_gyro_deallocate_ring(indio_dev
);
534 EXPORT_SYMBOL(st_gyro_common_remove
);
536 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
537 MODULE_DESCRIPTION("STMicroelectronics gyroscopes driver");
538 MODULE_LICENSE("GPL v2");