Skip to content

Commit

Permalink
iio: imu: add support to lsm6dsx driver
Browse files Browse the repository at this point in the history
Add support to STM LSM6DS3-LSM6DSM 6-axis (acc + gyro) Mems sensor

http://www.st.com/resource/en/datasheet/lsm6ds3.pdf
http://www.st.com/resource/en/datasheet/lsm6dsm.pdf

- continuous mode support
- i2c support
- spi support
- sw fifo mode support
- supported devices: lsm6ds3, lsm6dsm

Signed-off-by: Lorenzo Bianconi <lorenzo.bianconi@st.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
  • Loading branch information
LorenzoBianconi authored and jic23 committed Jan 14, 2017
1 parent adc8ec5 commit 290a6ce
Show file tree
Hide file tree
Showing 9 changed files with 1,517 additions and 0 deletions.
1 change: 1 addition & 0 deletions drivers/iio/imu/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ config KMX61
be called kmx61.

source "drivers/iio/imu/inv_mpu6050/Kconfig"
source "drivers/iio/imu/st_lsm6dsx/Kconfig"

endmenu

Expand Down
2 changes: 2 additions & 0 deletions drivers/iio/imu/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ obj-y += bmi160/
obj-y += inv_mpu6050/

obj-$(CONFIG_KMX61) += kmx61.o

obj-y += st_lsm6dsx/
22 changes: 22 additions & 0 deletions drivers/iio/imu/st_lsm6dsx/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

config IIO_ST_LSM6DSX
tristate "ST_LSM6DSx driver for STM 6-axis IMU MEMS sensors"
depends on (I2C || SPI)
select IIO_BUFFER
select IIO_KFIFO_BUF
select IIO_ST_LSM6DSX_I2C if (I2C)
select IIO_ST_LSM6DSX_SPI if (SPI_MASTER)
help
Say yes here to build support for STMicroelectronics LSM6DSx imu
sensor. Supported devices: lsm6ds3, lsm6dsm

To compile this driver as a module, choose M here: the module
will be called st_lsm6dsx.

config IIO_ST_LSM6DSX_I2C
tristate
depends on IIO_ST_LSM6DSX

config IIO_ST_LSM6DSX_SPI
tristate
depends on IIO_ST_LSM6DSX
5 changes: 5 additions & 0 deletions drivers/iio/imu/st_lsm6dsx/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
st_lsm6dsx-y := st_lsm6dsx_core.o st_lsm6dsx_buffer.o

obj-$(CONFIG_IIO_ST_LSM6DSX) += st_lsm6dsx.o
obj-$(CONFIG_IIO_ST_LSM6DSX_I2C) += st_lsm6dsx_i2c.o
obj-$(CONFIG_IIO_ST_LSM6DSX_SPI) += st_lsm6dsx_spi.o
141 changes: 141 additions & 0 deletions drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* STMicroelectronics st_lsm6dsx sensor driver
*
* Copyright 2016 STMicroelectronics Inc.
*
* Lorenzo Bianconi <lorenzo.bianconi@st.com>
* Denis Ciocca <denis.ciocca@st.com>
*
* Licensed under the GPL-2.
*/

#ifndef ST_LSM6DSX_H
#define ST_LSM6DSX_H

#include <linux/device.h>

#define ST_LSM6DS3_DEV_NAME "lsm6ds3"
#define ST_LSM6DSM_DEV_NAME "lsm6dsm"

enum st_lsm6dsx_hw_id {
ST_LSM6DS3_ID,
ST_LSM6DSM_ID,
};

#define ST_LSM6DSX_CHAN_SIZE 2
#define ST_LSM6DSX_SAMPLE_SIZE 6
#define ST_LSM6DSX_SAMPLE_DEPTH (ST_LSM6DSX_SAMPLE_SIZE / \
ST_LSM6DSX_CHAN_SIZE)

#if defined(CONFIG_SPI_MASTER)
#define ST_LSM6DSX_RX_MAX_LENGTH 256
#define ST_LSM6DSX_TX_MAX_LENGTH 8

struct st_lsm6dsx_transfer_buffer {
u8 rx_buf[ST_LSM6DSX_RX_MAX_LENGTH];
u8 tx_buf[ST_LSM6DSX_TX_MAX_LENGTH] ____cacheline_aligned;
};
#endif /* CONFIG_SPI_MASTER */

struct st_lsm6dsx_transfer_function {
int (*read)(struct device *dev, u8 addr, int len, u8 *data);
int (*write)(struct device *dev, u8 addr, int len, u8 *data);
};

struct st_lsm6dsx_reg {
u8 addr;
u8 mask;
};

struct st_lsm6dsx_settings {
u8 wai;
u16 max_fifo_size;
enum st_lsm6dsx_hw_id id;
};

enum st_lsm6dsx_sensor_id {
ST_LSM6DSX_ID_ACC,
ST_LSM6DSX_ID_GYRO,
ST_LSM6DSX_ID_MAX,
};

enum st_lsm6dsx_fifo_mode {
ST_LSM6DSX_FIFO_BYPASS = 0x0,
ST_LSM6DSX_FIFO_CONT = 0x6,
};

/**
* struct st_lsm6dsx_sensor - ST IMU sensor instance
* @id: Sensor identifier.
* @hw: Pointer to instance of struct st_lsm6dsx_hw.
* @gain: Configured sensor sensitivity.
* @odr: Output data rate of the sensor [Hz].
* @watermark: Sensor watermark level.
* @sip: Number of samples in a given pattern.
* @decimator: FIFO decimation factor.
* @decimator_mask: Sensor mask for decimation register.
* @delta_ts: Delta time between two consecutive interrupts.
* @ts: Latest timestamp from the interrupt handler.
*/
struct st_lsm6dsx_sensor {
enum st_lsm6dsx_sensor_id id;
struct st_lsm6dsx_hw *hw;

u32 gain;
u16 odr;

u16 watermark;
u8 sip;
u8 decimator;
u8 decimator_mask;

s64 delta_ts;
s64 ts;
};

/**
* struct st_lsm6dsx_hw - ST IMU MEMS hw instance
* @dev: Pointer to instance of struct device (I2C or SPI).
* @irq: Device interrupt line (I2C or SPI).
* @lock: Mutex to protect read and write operations.
* @fifo_lock: Mutex to prevent concurrent access to the hw FIFO.
* @fifo_mode: FIFO operating mode supported by the device.
* @enable_mask: Enabled sensor bitmask.
* @sip: Total number of samples (acc/gyro) in a given pattern.
* @iio_devs: Pointers to acc/gyro iio_dev instances.
* @settings: Pointer to the specific sensor settings in use.
* @tf: Transfer function structure used by I/O operations.
* @tb: Transfer buffers used by SPI I/O operations.
*/
struct st_lsm6dsx_hw {
struct device *dev;
int irq;

struct mutex lock;
struct mutex fifo_lock;

enum st_lsm6dsx_fifo_mode fifo_mode;
u8 enable_mask;
u8 sip;

struct iio_dev *iio_devs[ST_LSM6DSX_ID_MAX];

const struct st_lsm6dsx_settings *settings;

const struct st_lsm6dsx_transfer_function *tf;
#if defined(CONFIG_SPI_MASTER)
struct st_lsm6dsx_transfer_buffer tb;
#endif /* CONFIG_SPI_MASTER */
};

int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id,
const struct st_lsm6dsx_transfer_function *tf_ops);
int st_lsm6dsx_sensor_enable(struct st_lsm6dsx_sensor *sensor);
int st_lsm6dsx_sensor_disable(struct st_lsm6dsx_sensor *sensor);
int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw);
int st_lsm6dsx_write_with_mask(struct st_lsm6dsx_hw *hw, u8 addr, u8 mask,
u8 val);
int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor,
u16 watermark);

#endif /* ST_LSM6DSX_H */

0 comments on commit 290a6ce

Please sign in to comment.