BigW Consortium Gitlab

Commit 2435f8f6 by David Frey

Initial version of lsm6ds3 driver from ST

Source came from here: https://github.com/STMicroelectronics/STMems_Linux_Input_drivers/tree/linux-3.10.y-gh/drivers/input/misc/st/imu/lsm6ds3 Last update in this revision: 2b218416b197598225c50cddc26582301cc1e91e
parent fa481a5c
config INPUT_LSM6DS3
tristate "STMicroelectronics LSM6DS3/LSM6DS3H sensor"
depends on (I2C || SPI) && SYSFS
select INPUT_LSM6DS3_I2C if (I2C)
select INPUT_LSM6DS3_SPI if (SPI)
help
This driver support the STMicroelectronics LSM6DS3 sensor.
To compile this driver as a module, choose M here. The module
will be called lsm6ds3.
config INPUT_LSM6DS3_I2C
tristate
depends on INPUT_LSM6DS3
depends on I2C
config INPUT_LSM6DS3_SPI
tristate
depends on INPUT_LSM6DS3
depends on SPI
#
# Makefile for the input misc STM imu lsm6ds3 driver
#
lsm6ds3-core-y += lsm6ds3_core.o
obj-$(CONFIG_INPUT_LSM6DS3) += lsm6ds3-core.o
obj-$(CONFIG_INPUT_LSM6DS3_I2C) += lsm6ds3_i2c.o
obj-$(CONFIG_INPUT_LSM6DS3_SPI) += lsm6ds3_spi.o
LSM6DS3 Linux Kernel Driver
===========================
lsm6ds3 driver pulled from ST's [Linux kernel tree
repository](https://github.com/STMicroelectronics/STMems_Linux_Input_drivers). Specifically, the
lsm6ds3 driver was extracted from the path `drivers/input/misc/st/imu/lsm6ds3` which was last
updated in revision 2b218416b197598225c50cddc26582301cc1e91e
/*
* STMicroelectronics lsm6ds3 driver
*
* Copyright 2014 STMicroelectronics Inc.
*
* Giuseppe Barba <giuseppe.barba@st.com>
* v 1.2.1
* Licensed under the GPL-2.
*/
#ifndef DRIVERS_INPUT_MISC_LSM6DS3_CORE_H_
#define DRIVERS_INPUT_MISC_LSM6DS3_CORE_H_
#define LSM6DS3_ACC_GYR_DEV_NAME "lsm6ds3"
#define HZ_TO_PERIOD_NSEC(hz) (1000 * 1000 * 1000 / ((u32)(hz)))
#define MS_TO_US(x) ({ typeof(x) _x = (x); ((_x) * \
((typeof(x)) 1000));})
#define US_TO_NS(x) (MS_TO_US(x))
#define MS_TO_NS(x) (US_TO_NS(MS_TO_US(x)))
#define US_TO_MS(x) ({ typeof(x) _x = (x); ((_x) / \
((typeof(x)) 1000));})
#define NS_TO_US(x) (US_TO_MS(x))
#define NS_TO_MS(x) (US_TO_MS(NS_TO_US(x)))
enum {
LSM6DS3_ACCEL = 0,
LSM6DS3_GYRO,
LSM6DS3_SIGN_MOTION,
LSM6DS3_STEP_COUNTER,
LSM6DS3_STEP_DETECTOR,
LSM6DS3_TILT,
LSM6DS3_SENSORS_NUMB,
};
#define DEF_ZERO 0x00
/* First Out register for Acc and Gyro */
#define LSM6DS3_ACC_OUT_X_L_ADDR 0x28
#define LSM6DS3_GYR_OUT_X_L_ADDR 0x22
/* Output data rate registers */
#define LSM6DS3_ACC_ODR_ADDR CTRL1_ADDR
#define LSM6DS3_ACC_ODR_MASK 0xf0
#define LSM6DS3_GYR_ODR_ADDR CTRL2_ADDR
#define LSM6DS3_GYR_ODR_MASK 0xf0
#define LSM6DS3_ACC_FS_ADDR CTRL1_ADDR
#define LSM6DS3_GYR_FS_ADDR CTRL2_ADDR
#define LSM6DS3_IF_INC_MASK 0x04
#define LSM6DS3_HPERF_GYR_ADDR CTRL7_ADDR
#define LSM6DS3_HPERF_GYR_MASK 0x80
#define LSM6DS3_HPERF_ACC_ADDR CTRL6_ADDR
#define LSM6DS3_HPERF_ACC_MASK 0x10
#define LSM6DS3_HPERF_ACC_ENABLE 0x00
#define CTRL1_ADDR 0x10
#define CTRL2_ADDR 0x11
#define CTRL3_ADDR 0x12
#define CTRL6_ADDR 0x15
#define CTRL7_ADDR 0x16
/* Sensitivity Acc */
#define SENSITIVITY_ACC_2G 61 /** ug/LSB */
#define SENSITIVITY_ACC_4G 122 /** ug/LSB */
#define SENSITIVITY_ACC_8G 244 /** ug/LSB */
#define SENSITIVITY_ACC_16G 488 /** ug/LSB */
/* Sensitivity Gyr */
#define SENSITIVITY_GYR_125 437 /** 10udps/LSB */
#define SENSITIVITY_GYR_245 875 /** 10udps/LSB */
#define SENSITIVITY_GYR_500 1750 /** 10udps/LSB */
#define SENSITIVITY_GYR_1000 3500 /** 10udps/LSB */
#define SENSITIVITY_GYR_2000 7000 /** 10udps/LSB */
#define FUZZ 0
#define FLAT 0
#define INPUT_EVENT_TYPE EV_MSC
#define INPUT_EVENT_X MSC_SERIAL
#define INPUT_EVENT_Y MSC_PULSELED
#define INPUT_EVENT_Z MSC_GESTURE
#define INPUT_EVENT_TIME_MSB MSC_SCAN
#define INPUT_EVENT_TIME_LSB MSC_MAX
#define LSM6DS3_RX_MAX_LENGTH 500
#define LSM6DS3_TX_MAX_LENGTH 500
#define to_dev(obj) container_of(obj, struct device, kobj)
struct reg_rw {
u8 const address;
u8 const init_val;
u8 resume_val;
};
struct reg_r {
const u8 address;
const u8 init_val;
};
struct lsm6ds3_transfer_buffer {
struct mutex buf_lock;
u8 rx_buf[LSM6DS3_RX_MAX_LENGTH];
u8 tx_buf[LSM6DS3_TX_MAX_LENGTH] ____cacheline_aligned;
};
struct lsm6ds3_data;
struct lsm6ds3_transfer_function {
int (*write) (struct lsm6ds3_data *cdata, u8 reg_addr, int len, u8 *data,
bool b_lock);
int (*read) (struct lsm6ds3_data *cdata, u8 reg_addr, int len, u8 *data,
bool b_lock);
};
struct lsm6ds3_sensor_data {
struct lsm6ds3_data *cdata;
const char* name;
u8 enabled;
u32 c_odr;
u32 c_gain;
u8 sindex;
u8 sample_to_discard;
struct input_dev *input_dev;
unsigned int poll_interval;
int64_t timestamp;
struct work_struct input_work;
struct hrtimer hr_timer;
ktime_t delta_ts;
};
struct lsm6ds3_data {
const char *name;
bool reset_steps;
bool sign_motion_event_ready;
u16 steps_c;
u8 drdy_int_pin;
int irq;
int64_t timestamp;
struct mutex lock;
struct device *dev;
struct lsm6ds3_sensor_data sensors[LSM6DS3_SENSORS_NUMB];
struct mutex bank_registers_lock;
const struct lsm6ds3_transfer_function *tf;
struct lsm6ds3_transfer_buffer tb;
};
int lsm6ds3_common_probe(struct lsm6ds3_data *cdata, int irq, u16 bustype);
void lsm6ds3_common_remove(struct lsm6ds3_data *cdata, int irq);
#ifdef CONFIG_PM
int lsm6ds3_common_suspend(struct lsm6ds3_data *cdata);
int lsm6ds3_common_resume(struct lsm6ds3_data *cdata);
#endif /* CONFIG_PM */
#endif /* DRIVERS_INPUT_MISC_LSM6DS3_CORE_H_ */
/*
* STMicroelectronics lsm6ds3 i2c driver
*
* Copyright 2014 STMicroelectronics Inc.
*
* Giuseppe Barba <giuseppe.barba@st.com>
* v 1.2.1
* Licensed under the GPL-2.
*/
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/hrtimer.h>
#include <linux/input.h>
#include <linux/types.h>
#include "lsm6ds3_core.h"
static int lsm6ds3_i2c_read(struct lsm6ds3_data *cdata, u8 reg_addr, int len,
u8 *data, bool b_lock)
{
int err = 0;
struct i2c_msg msg[2];
struct i2c_client *client = to_i2c_client(cdata->dev);
msg[0].addr = client->addr;
msg[0].flags = client->flags;
msg[0].len = 1;
msg[0].buf = &reg_addr;
msg[1].addr = client->addr;
msg[1].flags = client->flags | I2C_M_RD;
msg[1].len = len;
msg[1].buf = data;
if (b_lock) {
mutex_lock(&cdata->bank_registers_lock);
err = i2c_transfer(client->adapter, msg, 2);
mutex_unlock(&cdata->bank_registers_lock);
} else
err = i2c_transfer(client->adapter, msg, 2);
return err;
}
static int lsm6ds3_i2c_write(struct lsm6ds3_data *cdata, u8 reg_addr, int len,
u8 *data, bool b_lock)
{
int err = 0;
u8 send[len + 1];
struct i2c_msg msg;
struct i2c_client *client = to_i2c_client(cdata->dev);
send[0] = reg_addr;
memcpy(&send[1], data, len * sizeof(u8));
len++;
msg.addr = client->addr;
msg.flags = client->flags;
msg.len = len;
msg.buf = send;
if (b_lock) {
mutex_lock(&cdata->bank_registers_lock);
err = i2c_transfer(client->adapter, &msg, 1);
mutex_unlock(&cdata->bank_registers_lock);
} else
err = i2c_transfer(client->adapter, &msg, 1);
return err;
}
static const struct lsm6ds3_transfer_function lsm6ds3_tf_i2c = {
.write = lsm6ds3_i2c_write,
.read = lsm6ds3_i2c_read,
};
static int lsm6ds3_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
int err;
struct lsm6ds3_data *cdata;
cdata = kzalloc(sizeof(*cdata), GFP_KERNEL);
if (!cdata)
return -ENOMEM;
cdata->dev = &client->dev;
cdata->name = client->name;
cdata->tf = &lsm6ds3_tf_i2c;
i2c_set_clientdata(client, cdata);
err = lsm6ds3_common_probe(cdata, client->irq, BUS_I2C);
if (err < 0)
goto free_data;
return 0;
free_data:
kfree(cdata);
return err;
}
static int lsm6ds3_i2c_remove(struct i2c_client *client)
{
struct lsm6ds3_data *cdata = i2c_get_clientdata(client);
lsm6ds3_common_remove(cdata, client->irq);
dev_info(cdata->dev, "%s: removed\n", LSM6DS3_ACC_GYR_DEV_NAME);
kfree(cdata);
return 0;
}
#ifdef CONFIG_PM
static int lsm6ds3_suspend(struct device *dev)
{
struct lsm6ds3_data *cdata = i2c_get_clientdata(to_i2c_client(dev));
return lsm6ds3_common_suspend(cdata);
}
static int lsm6ds3_resume(struct device *dev)
{
struct lsm6ds3_data *cdata = i2c_get_clientdata(to_i2c_client(dev));
return lsm6ds3_common_resume(cdata);
}
static const struct dev_pm_ops lsm6ds3_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(lsm6ds3_suspend, lsm6ds3_resume)
};
#define LSM6DS3_PM_OPS (&lsm6ds3_pm_ops)
#else /* CONFIG_PM */
#define LSM6DS3_PM_OPS NULL
#endif /* CONFIG_PM */
static const struct i2c_device_id lsm6ds3_ids[] = {
{LSM6DS3_ACC_GYR_DEV_NAME, 0},
{ }
};
MODULE_DEVICE_TABLE(i2c, lsm6ds3_ids);
#ifdef CONFIG_OF
static const struct of_device_id lsm6ds3_id_table[] = {
{.compatible = "st,lsm6ds3", },
{.compatible = "st,lsm6ds3h", },
{ },
};
MODULE_DEVICE_TABLE(of, lsm6ds3_id_table);
#endif
static struct i2c_driver lsm6ds3_i2c_driver = {
.driver = {
.owner = THIS_MODULE,
.name = LSM6DS3_ACC_GYR_DEV_NAME,
.pm = LSM6DS3_PM_OPS,
#ifdef CONFIG_OF
.of_match_table = lsm6ds3_id_table,
#endif
},
.probe = lsm6ds3_i2c_probe,
.remove = lsm6ds3_i2c_remove,
.id_table = lsm6ds3_ids,
};
module_i2c_driver(lsm6ds3_i2c_driver);
MODULE_DESCRIPTION("STMicroelectronics lsm6ds3 i2c driver");
MODULE_AUTHOR("Giuseppe Barba");
MODULE_LICENSE("GPL v2");
/*
* STMicroelectronics lsm6ds3 i2c driver
*
* Copyright 2014 STMicroelectronics Inc.
*
* Giuseppe Barba <giuseppe.barba@st.com>
* v 1.2.1
* Licensed under the GPL-2.
*/
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/spi/spi.h>
#include <linux/hrtimer.h>
#include <linux/input.h>
#include <linux/types.h>
#include "lsm6ds3_core.h"
#define SENSORS_SPI_READ 0x80
static int lsm6ds3_spi_read(struct lsm6ds3_data *cdata, u8 reg_addr, int len,
u8 *data, bool b_lock)
{
int err;
struct spi_message msg;
struct spi_transfer xfers[] = {
{
.tx_buf = cdata->tb.tx_buf,
.bits_per_word = 8,
.len = 1,
},
{
.rx_buf = cdata->tb.rx_buf,
.bits_per_word = 8,
.len = len,
}
};
if (b_lock)
mutex_lock(&cdata->bank_registers_lock);
mutex_lock(&cdata->tb.buf_lock);
cdata->tb.tx_buf[0] = reg_addr | SENSORS_SPI_READ;
spi_message_init(&msg);
spi_message_add_tail(&xfers[0], &msg);
spi_message_add_tail(&xfers[1], &msg);
err = spi_sync(to_spi_device(cdata->dev), &msg);
if (err)
goto acc_spi_read_error;
memcpy(data, cdata->tb.rx_buf, len * sizeof(u8));
mutex_unlock(&cdata->tb.buf_lock);
if (b_lock)
mutex_unlock(&cdata->bank_registers_lock);
return len;
acc_spi_read_error:
mutex_unlock(&cdata->tb.buf_lock);
if (b_lock)
mutex_unlock(&cdata->bank_registers_lock);
return err;
}
static int lsm6ds3_spi_write(struct lsm6ds3_data *cdata, u8 reg_addr, int len,
u8 *data, bool b_lock)
{
int err;
struct spi_message msg;
struct spi_transfer xfers = {
.tx_buf = cdata->tb.tx_buf,
.bits_per_word = 8,
.len = len + 1,
};
if (len >= LSM6DS3_RX_MAX_LENGTH)
return -ENOMEM;
if (b_lock)
mutex_lock(&cdata->bank_registers_lock);
mutex_lock(&cdata->tb.buf_lock);
cdata->tb.tx_buf[0] = reg_addr;
memcpy(&cdata->tb.tx_buf[1], data, len);
spi_message_init(&msg);
spi_message_add_tail(&xfers, &msg);
err = spi_sync(to_spi_device(cdata->dev), &msg);
mutex_unlock(&cdata->tb.buf_lock);
if (b_lock)
mutex_unlock(&cdata->bank_registers_lock);
return err;
}
static const struct lsm6ds3_transfer_function lsm6ds3_tf_spi = {
.write = lsm6ds3_spi_write,
.read = lsm6ds3_spi_read,
};
static int lsm6ds3_spi_probe(struct spi_device *spi)
{
int err;
struct lsm6ds3_data *cdata;
cdata = kzalloc(sizeof(*cdata), GFP_KERNEL);
if (!cdata)
return -ENOMEM;
cdata->dev = &spi->dev;
cdata->name = spi->modalias;
cdata->tf = &lsm6ds3_tf_spi;
spi_set_drvdata(spi, cdata);
err = lsm6ds3_common_probe(cdata, spi->irq, BUS_SPI);
if (err < 0)
goto free_data;
return 0;
free_data:
kfree(cdata);
return err;
}
static int lsm6ds3_spi_remove(struct spi_device *spi)
{
/* TODO: check the function */
struct lsm6ds3_data *cdata = spi_get_drvdata(spi);
lsm6ds3_common_remove(cdata, spi->irq);
dev_info(cdata->dev, "%s: removed\n", LSM6DS3_ACC_GYR_DEV_NAME);
kfree(cdata);
return 0;
}
#ifdef CONFIG_PM
static int lsm6ds3_suspend(struct device *dev)
{
struct lsm6ds3_data *cdata = spi_get_drvdata(to_spi_device(dev));
return lsm6ds3_common_suspend(cdata);
}
static int lsm6ds3_resume(struct device *dev)
{
struct lsm6ds3_data *cdata = spi_get_drvdata(to_spi_device(dev));
return lsm6ds3_common_resume(cdata);
}
static const struct dev_pm_ops lsm6ds3_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(lsm6ds3_suspend, lsm6ds3_resume)
};
#define LSM6DS3_PM_OPS (&lsm6ds3_pm_ops)
#else /* CONFIG_PM */
#define LSM6DS3_PM_OPS NULL
#endif /* CONFIG_PM */
static const struct spi_device_id lsm6ds3_ids[] = {
{LSM6DS3_ACC_GYR_DEV_NAME, 0},
{ }
};
MODULE_DEVICE_TABLE(spi, lsm6ds3_ids);
#ifdef CONFIG_OF
static const struct of_device_id lsm6ds3_id_table[] = {
{.compatible = "st,lsm6ds3", },
{.compatible = "st,lsm6ds3h", },
{ },
};
MODULE_DEVICE_TABLE(of, lsm6ds3_id_table);
#endif
static struct spi_driver lsm6ds3_spi_driver = {
.driver = {
.owner = THIS_MODULE,
.name = LSM6DS3_ACC_GYR_DEV_NAME,
.pm = LSM6DS3_PM_OPS,
#ifdef CONFIG_OF
.of_match_table = lsm6ds3_id_table,
#endif
},
.probe = lsm6ds3_spi_probe,
.remove = lsm6ds3_spi_remove,
.id_table = lsm6ds3_ids,
};
module_spi_driver(lsm6ds3_spi_driver);
MODULE_DESCRIPTION("STMicroelectronics lsm6ds3 spi driver");
MODULE_AUTHOR("Giuseppe Barba");
MODULE_LICENSE("GPL v2");
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment