Commit 7f85e42a authored by Jean-Baptiste Maneyrol's avatar Jean-Baptiste Maneyrol Committed by Jonathan Cameron

iio: imu: inv_icm42600: add buffer support in iio devices

Add all FIFO parsing and reading functions. Add accel and gyro
kfifo buffer and FIFO data parsing. Use device interrupt for
reading data FIFO and launching accel and gyro parsing.

Support hwfifo watermark by multiplexing gyro and accel settings.
Support hwfifo flush.
Signed-off-by: default avatarJean-Baptiste Maneyrol <jmaneyrol@invensense.com>
Signed-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent e5efa104
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
config INV_ICM42600 config INV_ICM42600
tristate tristate
select IIO_BUFFER
config INV_ICM42600_I2C config INV_ICM42600_I2C
tristate "InvenSense ICM-426xx I2C driver" tristate "InvenSense ICM-426xx I2C driver"
......
...@@ -5,6 +5,7 @@ inv-icm42600-y += inv_icm42600_core.o ...@@ -5,6 +5,7 @@ inv-icm42600-y += inv_icm42600_core.o
inv-icm42600-y += inv_icm42600_gyro.o inv-icm42600-y += inv_icm42600_gyro.o
inv-icm42600-y += inv_icm42600_accel.o inv-icm42600-y += inv_icm42600_accel.o
inv-icm42600-y += inv_icm42600_temp.o inv-icm42600-y += inv_icm42600_temp.o
inv-icm42600-y += inv_icm42600_buffer.o
obj-$(CONFIG_INV_ICM42600_I2C) += inv-icm42600-i2c.o obj-$(CONFIG_INV_ICM42600_I2C) += inv-icm42600-i2c.o
inv-icm42600-i2c-y += inv_icm42600_i2c.o inv-icm42600-i2c-y += inv_icm42600_i2c.o
......
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
#include <linux/pm.h> #include <linux/pm.h>
#include <linux/iio/iio.h> #include <linux/iio/iio.h>
#include "inv_icm42600_buffer.h"
enum inv_icm42600_chip { enum inv_icm42600_chip {
INV_CHIP_ICM42600, INV_CHIP_ICM42600,
INV_CHIP_ICM42602, INV_CHIP_ICM42602,
...@@ -123,6 +125,7 @@ struct inv_icm42600_suspended { ...@@ -123,6 +125,7 @@ struct inv_icm42600_suspended {
* @indio_gyro: gyroscope IIO device. * @indio_gyro: gyroscope IIO device.
* @indio_accel: accelerometer IIO device. * @indio_accel: accelerometer IIO device.
* @buffer: data transfer buffer aligned for DMA. * @buffer: data transfer buffer aligned for DMA.
* @fifo: FIFO management structure.
*/ */
struct inv_icm42600_state { struct inv_icm42600_state {
struct mutex lock; struct mutex lock;
...@@ -137,6 +140,7 @@ struct inv_icm42600_state { ...@@ -137,6 +140,7 @@ struct inv_icm42600_state {
struct iio_dev *indio_gyro; struct iio_dev *indio_gyro;
struct iio_dev *indio_accel; struct iio_dev *indio_accel;
uint8_t buffer[2] ____cacheline_aligned; uint8_t buffer[2] ____cacheline_aligned;
struct inv_icm42600_fifo fifo;
}; };
/* Virtual register addresses: @bank on MSB (4 upper bits), @address on LSB */ /* Virtual register addresses: @bank on MSB (4 upper bits), @address on LSB */
...@@ -377,6 +381,10 @@ int inv_icm42600_core_probe(struct regmap *regmap, int chip, int irq, ...@@ -377,6 +381,10 @@ int inv_icm42600_core_probe(struct regmap *regmap, int chip, int irq,
struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st); struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st);
int inv_icm42600_gyro_parse_fifo(struct iio_dev *indio_dev);
struct iio_dev *inv_icm42600_accel_init(struct inv_icm42600_state *st); struct iio_dev *inv_icm42600_accel_init(struct inv_icm42600_state *st);
int inv_icm42600_accel_parse_fifo(struct iio_dev *indio_dev);
#endif #endif
...@@ -11,9 +11,12 @@ ...@@ -11,9 +11,12 @@
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/math64.h> #include <linux/math64.h>
#include <linux/iio/iio.h> #include <linux/iio/iio.h>
#include <linux/iio/buffer.h>
#include <linux/iio/kfifo_buf.h>
#include "inv_icm42600.h" #include "inv_icm42600.h"
#include "inv_icm42600_temp.h" #include "inv_icm42600_temp.h"
#include "inv_icm42600_buffer.h"
#define INV_ICM42600_ACCEL_CHAN(_modifier, _index, _ext_info) \ #define INV_ICM42600_ACCEL_CHAN(_modifier, _index, _ext_info) \
{ \ { \
...@@ -64,6 +67,78 @@ static const struct iio_chan_spec inv_icm42600_accel_channels[] = { ...@@ -64,6 +67,78 @@ static const struct iio_chan_spec inv_icm42600_accel_channels[] = {
INV_ICM42600_TEMP_CHAN(INV_ICM42600_ACCEL_SCAN_TEMP), INV_ICM42600_TEMP_CHAN(INV_ICM42600_ACCEL_SCAN_TEMP),
}; };
/*
* IIO buffer data: size must be a power of 2
* 8 bytes: 6 bytes acceleration and 2 bytes temperature
*/
struct inv_icm42600_accel_buffer {
struct inv_icm42600_fifo_sensor_data accel;
int16_t temp;
};
#define INV_ICM42600_SCAN_MASK_ACCEL_3AXIS \
(BIT(INV_ICM42600_ACCEL_SCAN_X) | \
BIT(INV_ICM42600_ACCEL_SCAN_Y) | \
BIT(INV_ICM42600_ACCEL_SCAN_Z))
#define INV_ICM42600_SCAN_MASK_TEMP BIT(INV_ICM42600_ACCEL_SCAN_TEMP)
static const unsigned long inv_icm42600_accel_scan_masks[] = {
/* 3-axis accel + temperature */
INV_ICM42600_SCAN_MASK_ACCEL_3AXIS | INV_ICM42600_SCAN_MASK_TEMP,
0,
};
/* enable accelerometer sensor and FIFO write */
static int inv_icm42600_accel_update_scan_mode(struct iio_dev *indio_dev,
const unsigned long *scan_mask)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
unsigned int fifo_en = 0;
unsigned int sleep_temp = 0;
unsigned int sleep_accel = 0;
unsigned int sleep;
int ret;
mutex_lock(&st->lock);
if (*scan_mask & INV_ICM42600_SCAN_MASK_TEMP) {
/* enable temp sensor */
ret = inv_icm42600_set_temp_conf(st, true, &sleep_temp);
if (ret)
goto out_unlock;
fifo_en |= INV_ICM42600_SENSOR_TEMP;
}
if (*scan_mask & INV_ICM42600_SCAN_MASK_ACCEL_3AXIS) {
/* enable accel sensor */
conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
ret = inv_icm42600_set_accel_conf(st, &conf, &sleep_accel);
if (ret)
goto out_unlock;
fifo_en |= INV_ICM42600_SENSOR_ACCEL;
}
/* update data FIFO write */
ret = inv_icm42600_buffer_set_fifo_en(st, fifo_en | st->fifo.en);
if (ret)
goto out_unlock;
ret = inv_icm42600_buffer_update_watermark(st);
out_unlock:
mutex_unlock(&st->lock);
/* sleep maximum required time */
if (sleep_accel > sleep_temp)
sleep = sleep_accel;
else
sleep = sleep_temp;
if (sleep)
msleep(sleep);
return ret;
}
static int inv_icm42600_accel_read_sensor(struct inv_icm42600_state *st, static int inv_icm42600_accel_read_sensor(struct inv_icm42600_state *st,
struct iio_chan_spec const *chan, struct iio_chan_spec const *chan,
int16_t *val) int16_t *val)
...@@ -248,7 +323,12 @@ static int inv_icm42600_accel_write_odr(struct inv_icm42600_state *st, ...@@ -248,7 +323,12 @@ static int inv_icm42600_accel_write_odr(struct inv_icm42600_state *st,
mutex_lock(&st->lock); mutex_lock(&st->lock);
ret = inv_icm42600_set_accel_conf(st, &conf, NULL); ret = inv_icm42600_set_accel_conf(st, &conf, NULL);
if (ret)
goto out_unlock;
inv_icm42600_buffer_update_fifo_period(st);
inv_icm42600_buffer_update_watermark(st);
out_unlock:
mutex_unlock(&st->lock); mutex_unlock(&st->lock);
pm_runtime_mark_last_busy(dev); pm_runtime_mark_last_busy(dev);
pm_runtime_put_autosuspend(dev); pm_runtime_put_autosuspend(dev);
...@@ -563,12 +643,51 @@ static int inv_icm42600_accel_write_raw_get_fmt(struct iio_dev *indio_dev, ...@@ -563,12 +643,51 @@ static int inv_icm42600_accel_write_raw_get_fmt(struct iio_dev *indio_dev,
} }
} }
static int inv_icm42600_accel_hwfifo_set_watermark(struct iio_dev *indio_dev,
unsigned int val)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
int ret;
mutex_lock(&st->lock);
st->fifo.watermark.accel = val;
ret = inv_icm42600_buffer_update_watermark(st);
mutex_unlock(&st->lock);
return ret;
}
static int inv_icm42600_accel_hwfifo_flush(struct iio_dev *indio_dev,
unsigned int count)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
int ret;
if (count == 0)
return 0;
mutex_lock(&st->lock);
ret = inv_icm42600_buffer_hwfifo_flush(st, count);
if (!ret)
ret = st->fifo.nb.accel;
mutex_unlock(&st->lock);
return ret;
}
static const struct iio_info inv_icm42600_accel_info = { static const struct iio_info inv_icm42600_accel_info = {
.read_raw = inv_icm42600_accel_read_raw, .read_raw = inv_icm42600_accel_read_raw,
.read_avail = inv_icm42600_accel_read_avail, .read_avail = inv_icm42600_accel_read_avail,
.write_raw = inv_icm42600_accel_write_raw, .write_raw = inv_icm42600_accel_write_raw,
.write_raw_get_fmt = inv_icm42600_accel_write_raw_get_fmt, .write_raw_get_fmt = inv_icm42600_accel_write_raw_get_fmt,
.debugfs_reg_access = inv_icm42600_debugfs_reg, .debugfs_reg_access = inv_icm42600_debugfs_reg,
.update_scan_mode = inv_icm42600_accel_update_scan_mode,
.hwfifo_set_watermark = inv_icm42600_accel_hwfifo_set_watermark,
.hwfifo_flush_to_buffer = inv_icm42600_accel_hwfifo_flush,
}; };
struct iio_dev *inv_icm42600_accel_init(struct inv_icm42600_state *st) struct iio_dev *inv_icm42600_accel_init(struct inv_icm42600_state *st)
...@@ -576,6 +695,7 @@ struct iio_dev *inv_icm42600_accel_init(struct inv_icm42600_state *st) ...@@ -576,6 +695,7 @@ struct iio_dev *inv_icm42600_accel_init(struct inv_icm42600_state *st)
struct device *dev = regmap_get_device(st->map); struct device *dev = regmap_get_device(st->map);
const char *name; const char *name;
struct iio_dev *indio_dev; struct iio_dev *indio_dev;
struct iio_buffer *buffer;
int ret; int ret;
name = devm_kasprintf(dev, GFP_KERNEL, "%s-accel", st->name); name = devm_kasprintf(dev, GFP_KERNEL, "%s-accel", st->name);
...@@ -586,12 +706,20 @@ struct iio_dev *inv_icm42600_accel_init(struct inv_icm42600_state *st) ...@@ -586,12 +706,20 @@ struct iio_dev *inv_icm42600_accel_init(struct inv_icm42600_state *st)
if (!indio_dev) if (!indio_dev)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
buffer = devm_iio_kfifo_allocate(dev);
if (!buffer)
return ERR_PTR(-ENOMEM);
iio_device_set_drvdata(indio_dev, st); iio_device_set_drvdata(indio_dev, st);
indio_dev->name = name; indio_dev->name = name;
indio_dev->info = &inv_icm42600_accel_info; indio_dev->info = &inv_icm42600_accel_info;
indio_dev->modes = INDIO_DIRECT_MODE; indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
indio_dev->channels = inv_icm42600_accel_channels; indio_dev->channels = inv_icm42600_accel_channels;
indio_dev->num_channels = ARRAY_SIZE(inv_icm42600_accel_channels); indio_dev->num_channels = ARRAY_SIZE(inv_icm42600_accel_channels);
indio_dev->available_scan_masks = inv_icm42600_accel_scan_masks;
indio_dev->setup_ops = &inv_icm42600_buffer_ops;
iio_device_attach_buffer(indio_dev, buffer);
ret = devm_iio_device_register(dev, indio_dev); ret = devm_iio_device_register(dev, indio_dev);
if (ret) if (ret)
...@@ -599,3 +727,35 @@ struct iio_dev *inv_icm42600_accel_init(struct inv_icm42600_state *st) ...@@ -599,3 +727,35 @@ struct iio_dev *inv_icm42600_accel_init(struct inv_icm42600_state *st)
return indio_dev; return indio_dev;
} }
int inv_icm42600_accel_parse_fifo(struct iio_dev *indio_dev)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
ssize_t i, size;
const void *accel, *gyro, *timestamp;
const int8_t *temp;
unsigned int odr;
struct inv_icm42600_accel_buffer buffer;
/* parse all fifo packets */
for (i = 0; i < st->fifo.count; i += size) {
size = inv_icm42600_fifo_decode_packet(&st->fifo.data[i],
&accel, &gyro, &temp, &timestamp, &odr);
/* quit if error or FIFO is empty */
if (size <= 0)
return size;
/* skip packet if no accel data or data is invalid */
if (accel == NULL || !inv_icm42600_fifo_is_data_valid(accel))
continue;
/* buffer is copied to userspace, zeroing it to avoid any data leak */
memset(&buffer, 0, sizeof(buffer));
memcpy(&buffer.accel, accel, sizeof(buffer.accel));
/* convert 8 bits FIFO temperature in high resolution format */
buffer.temp = temp ? (*temp * 64) : 0;
iio_push_to_buffers(indio_dev, &buffer);
}
return 0;
}
This diff is collapsed.
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2020 Invensense, Inc.
*/
#ifndef INV_ICM42600_BUFFER_H_
#define INV_ICM42600_BUFFER_H_
#include <linux/kernel.h>
#include <linux/bits.h>
struct inv_icm42600_state;
#define INV_ICM42600_SENSOR_GYRO BIT(0)
#define INV_ICM42600_SENSOR_ACCEL BIT(1)
#define INV_ICM42600_SENSOR_TEMP BIT(2)
/**
* struct inv_icm42600_fifo - FIFO state variables
* @on: reference counter for FIFO on.
* @en: bits field of INV_ICM42600_SENSOR_* for FIFO EN bits.
* @period: FIFO internal period.
* @watermark: watermark configuration values for accel and gyro.
* @count: number of bytes in the FIFO data buffer.
* @nb: gyro, accel and total samples in the FIFO data buffer.
* @data: FIFO data buffer aligned for DMA (2kB + 32 bytes of read cache).
*/
struct inv_icm42600_fifo {
unsigned int on;
unsigned int en;
uint32_t period;
struct {
unsigned int gyro;
unsigned int accel;
} watermark;
size_t count;
struct {
size_t gyro;
size_t accel;
size_t total;
} nb;
uint8_t data[2080] ____cacheline_aligned;
};
/* FIFO data packet */
struct inv_icm42600_fifo_sensor_data {
__be16 x;
__be16 y;
__be16 z;
} __packed;
#define INV_ICM42600_FIFO_DATA_INVALID -32768
static inline int16_t inv_icm42600_fifo_get_sensor_data(__be16 d)
{
return be16_to_cpu(d);
}
static inline bool
inv_icm42600_fifo_is_data_valid(const struct inv_icm42600_fifo_sensor_data *s)
{
int16_t x, y, z;
x = inv_icm42600_fifo_get_sensor_data(s->x);
y = inv_icm42600_fifo_get_sensor_data(s->y);
z = inv_icm42600_fifo_get_sensor_data(s->z);
if (x == INV_ICM42600_FIFO_DATA_INVALID &&
y == INV_ICM42600_FIFO_DATA_INVALID &&
z == INV_ICM42600_FIFO_DATA_INVALID)
return false;
return true;
}
ssize_t inv_icm42600_fifo_decode_packet(const void *packet, const void **accel,
const void **gyro, const int8_t **temp,
const void **timestamp, unsigned int *odr);
extern const struct iio_buffer_setup_ops inv_icm42600_buffer_ops;
int inv_icm42600_buffer_init(struct inv_icm42600_state *st);
void inv_icm42600_buffer_update_fifo_period(struct inv_icm42600_state *st);
int inv_icm42600_buffer_set_fifo_en(struct inv_icm42600_state *st,
unsigned int fifo_en);
int inv_icm42600_buffer_update_watermark(struct inv_icm42600_state *st);
int inv_icm42600_buffer_fifo_read(struct inv_icm42600_state *st,
unsigned int max);
int inv_icm42600_buffer_fifo_parse(struct inv_icm42600_state *st);
int inv_icm42600_buffer_hwfifo_flush(struct inv_icm42600_state *st,
unsigned int count);
#endif
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include <linux/iio/iio.h> #include <linux/iio/iio.h>
#include "inv_icm42600.h" #include "inv_icm42600.h"
#include "inv_icm42600_buffer.h"
static const struct regmap_range_cfg inv_icm42600_regmap_ranges[] = { static const struct regmap_range_cfg inv_icm42600_regmap_ranges[] = {
{ {
...@@ -428,6 +429,18 @@ static irqreturn_t inv_icm42600_irq_handler(int irq, void *_data) ...@@ -428,6 +429,18 @@ static irqreturn_t inv_icm42600_irq_handler(int irq, void *_data)
if (status & INV_ICM42600_INT_STATUS_FIFO_FULL) if (status & INV_ICM42600_INT_STATUS_FIFO_FULL)
dev_warn(dev, "FIFO full data lost!\n"); dev_warn(dev, "FIFO full data lost!\n");
/* FIFO threshold reached */
if (status & INV_ICM42600_INT_STATUS_FIFO_THS) {
ret = inv_icm42600_buffer_fifo_read(st, 0);
if (ret) {
dev_err(dev, "FIFO read error %d\n", ret);
goto out_unlock;
}
ret = inv_icm42600_buffer_fifo_parse(st);
if (ret)
dev_err(dev, "FIFO parsing error %d\n", ret);
}
out_unlock: out_unlock:
mutex_unlock(&st->lock); mutex_unlock(&st->lock);
return IRQ_HANDLED; return IRQ_HANDLED;
...@@ -604,6 +617,10 @@ int inv_icm42600_core_probe(struct regmap *regmap, int chip, int irq, ...@@ -604,6 +617,10 @@ int inv_icm42600_core_probe(struct regmap *regmap, int chip, int irq,
if (ret) if (ret)
return ret; return ret;
ret = inv_icm42600_buffer_init(st);
if (ret)
return ret;
st->indio_gyro = inv_icm42600_gyro_init(st); st->indio_gyro = inv_icm42600_gyro_init(st);
if (IS_ERR(st->indio_gyro)) if (IS_ERR(st->indio_gyro))
return PTR_ERR(st->indio_gyro); return PTR_ERR(st->indio_gyro);
...@@ -649,6 +666,14 @@ static int __maybe_unused inv_icm42600_suspend(struct device *dev) ...@@ -649,6 +666,14 @@ static int __maybe_unused inv_icm42600_suspend(struct device *dev)
goto out_unlock; goto out_unlock;
} }
/* disable FIFO data streaming */
if (st->fifo.on) {
ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG,
INV_ICM42600_FIFO_CONFIG_BYPASS);
if (ret)
goto out_unlock;
}
ret = inv_icm42600_set_pwr_mgmt0(st, INV_ICM42600_SENSOR_MODE_OFF, ret = inv_icm42600_set_pwr_mgmt0(st, INV_ICM42600_SENSOR_MODE_OFF,
INV_ICM42600_SENSOR_MODE_OFF, false, INV_ICM42600_SENSOR_MODE_OFF, false,
NULL); NULL);
...@@ -688,6 +713,11 @@ static int __maybe_unused inv_icm42600_resume(struct device *dev) ...@@ -688,6 +713,11 @@ static int __maybe_unused inv_icm42600_resume(struct device *dev)
if (ret) if (ret)
goto out_unlock; goto out_unlock;
/* restore FIFO data streaming */
if (st->fifo.on)
ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG,
INV_ICM42600_FIFO_CONFIG_STREAM);
out_unlock: out_unlock:
mutex_unlock(&st->lock); mutex_unlock(&st->lock);
return ret; return ret;
......
...@@ -11,9 +11,12 @@ ...@@ -11,9 +11,12 @@
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/math64.h> #include <linux/math64.h>
#include <linux/iio/iio.h> #include <linux/iio/iio.h>
#include <linux/iio/buffer.h>
#include <linux/iio/kfifo_buf.h>
#include "inv_icm42600.h" #include "inv_icm42600.h"
#include "inv_icm42600_temp.h" #include "inv_icm42600_temp.h"
#include "inv_icm42600_buffer.h"
#define INV_ICM42600_GYRO_CHAN(_modifier, _index, _ext_info) \ #define INV_ICM42600_GYRO_CHAN(_modifier, _index, _ext_info) \
{ \ { \
...@@ -64,6 +67,78 @@ static const struct iio_chan_spec inv_icm42600_gyro_channels[] = { ...@@ -64,6 +67,78 @@ static const struct iio_chan_spec inv_icm42600_gyro_channels[] = {
INV_ICM42600_TEMP_CHAN(INV_ICM42600_GYRO_SCAN_TEMP), INV_ICM42600_TEMP_CHAN(INV_ICM42600_GYRO_SCAN_TEMP),
}; };
/*
* IIO buffer data: size must be a power of 2
* 8 bytes: 6 bytes angular velocity and 2 bytes temperature
*/
struct inv_icm42600_gyro_buffer {
struct inv_icm42600_fifo_sensor_data gyro;
int16_t temp;
};
#define INV_ICM42600_SCAN_MASK_GYRO_3AXIS \
(BIT(INV_ICM42600_GYRO_SCAN_X) | \
BIT(INV_ICM42600_GYRO_SCAN_Y) | \
BIT(INV_ICM42600_GYRO_SCAN_Z))
#define INV_ICM42600_SCAN_MASK_TEMP BIT(INV_ICM42600_GYRO_SCAN_TEMP)
static const unsigned long inv_icm42600_gyro_scan_masks[] = {
/* 3-axis gyro + temperature */
INV_ICM42600_SCAN_MASK_GYRO_3AXIS | INV_ICM42600_SCAN_MASK_TEMP,
0,
};
/* enable gyroscope sensor and FIFO write */
static int inv_icm42600_gyro_update_scan_mode(struct iio_dev *indio_dev,
const unsigned long *scan_mask)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
unsigned int fifo_en = 0;
unsigned int sleep_gyro = 0;
unsigned int sleep_temp = 0;
unsigned int sleep;
int ret;
mutex_lock(&st->lock);
if (*scan_mask & INV_ICM42600_SCAN_MASK_TEMP) {
/* enable temp sensor */
ret = inv_icm42600_set_temp_conf(st, true, &sleep_temp);
if (ret)
goto out_unlock;
fifo_en |= INV_ICM42600_SENSOR_TEMP;
}
if (*scan_mask & INV_ICM42600_SCAN_MASK_GYRO_3AXIS) {
/* enable gyro sensor */
conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
ret = inv_icm42600_set_gyro_conf(st, &conf, &sleep_gyro);
if (ret)
goto out_unlock;
fifo_en |= INV_ICM42600_SENSOR_GYRO;
}
/* update data FIFO write */
ret = inv_icm42600_buffer_set_fifo_en(st, fifo_en | st->fifo.en);
if (ret)
goto out_unlock;
ret = inv_icm42600_buffer_update_watermark(st);
out_unlock:
mutex_unlock(&st->lock);
/* sleep maximum required time */
if (sleep_gyro > sleep_temp)
sleep = sleep_gyro;
else
sleep = sleep_temp;
if (sleep)
msleep(sleep);
return ret;
}
static int inv_icm42600_gyro_read_sensor(struct inv_icm42600_state *st, static int inv_icm42600_gyro_read_sensor(struct inv_icm42600_state *st,
struct iio_chan_spec const *chan, struct iio_chan_spec const *chan,
int16_t *val) int16_t *val)
...@@ -260,7 +335,12 @@ static int inv_icm42600_gyro_write_odr(struct inv_icm42600_state *st, ...@@ -260,7 +335,12 @@ static int inv_icm42600_gyro_write_odr(struct inv_icm42600_state *st,
mutex_lock(&st->lock); mutex_lock(&st->lock);
ret = inv_icm42600_set_gyro_conf(st, &conf, NULL); ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
if (ret)
goto out_unlock;
inv_icm42600_buffer_update_fifo_period(st);
inv_icm42600_buffer_update_watermark(st);
out_unlock:
mutex_unlock(&st->lock); mutex_unlock(&st->lock);
pm_runtime_mark_last_busy(dev); pm_runtime_mark_last_busy(dev);
pm_runtime_put_autosuspend(dev); pm_runtime_put_autosuspend(dev);
...@@ -574,12 +654,51 @@ static int inv_icm42600_gyro_write_raw_get_fmt(struct iio_dev *indio_dev, ...@@ -574,12 +654,51 @@ static int inv_icm42600_gyro_write_raw_get_fmt(struct iio_dev *indio_dev,
} }
} }
static int inv_icm42600_gyro_hwfifo_set_watermark(struct iio_dev *indio_dev,
unsigned int val)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
int ret;
mutex_lock(&st->lock);
st->fifo.watermark.gyro = val;
ret = inv_icm42600_buffer_update_watermark(st);
mutex_unlock(&st->lock);
return ret;
}
static int inv_icm42600_gyro_hwfifo_flush(struct iio_dev *indio_dev,
unsigned int count)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
int ret;
if (count == 0)
return 0;
mutex_lock(&st->lock);
ret = inv_icm42600_buffer_hwfifo_flush(st, count);
if (!ret)
ret = st->fifo.nb.gyro;
mutex_unlock(&st->lock);
return ret;
}
static const struct iio_info inv_icm42600_gyro_info = { static const struct iio_info inv_icm42600_gyro_info = {
.read_raw = inv_icm42600_gyro_read_raw, .read_raw = inv_icm42600_gyro_read_raw,
.read_avail = inv_icm42600_gyro_read_avail, .read_avail = inv_icm42600_gyro_read_avail,
.write_raw = inv_icm42600_gyro_write_raw, .write_raw = inv_icm42600_gyro_write_raw,
.write_raw_get_fmt = inv_icm42600_gyro_write_raw_get_fmt, .write_raw_get_fmt = inv_icm42600_gyro_write_raw_get_fmt,
.debugfs_reg_access = inv_icm42600_debugfs_reg, .debugfs_reg_access = inv_icm42600_debugfs_reg,
.update_scan_mode = inv_icm42600_gyro_update_scan_mode,
.hwfifo_set_watermark = inv_icm42600_gyro_hwfifo_set_watermark,
.hwfifo_flush_to_buffer = inv_icm42600_gyro_hwfifo_flush,
}; };
struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st) struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st)
...@@ -587,6 +706,7 @@ struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st) ...@@ -587,6 +706,7 @@ struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st)
struct device *dev = regmap_get_device(st->map); struct device *dev = regmap_get_device(st->map);
const char *name; const char *name;
struct iio_dev *indio_dev; struct iio_dev *indio_dev;
struct iio_buffer *buffer;
int ret; int ret;
name = devm_kasprintf(dev, GFP_KERNEL, "%s-gyro", st->name); name = devm_kasprintf(dev, GFP_KERNEL, "%s-gyro", st->name);
...@@ -597,12 +717,20 @@ struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st) ...@@ -597,12 +717,20 @@ struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st)
if (!indio_dev) if (!indio_dev)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
buffer = devm_iio_kfifo_allocate(dev);
if (!buffer)
return ERR_PTR(-ENOMEM);
iio_device_set_drvdata(indio_dev, st); iio_device_set_drvdata(indio_dev, st);
indio_dev->name = name; indio_dev->name = name;
indio_dev->info = &inv_icm42600_gyro_info; indio_dev->info = &inv_icm42600_gyro_info;
indio_dev->modes = INDIO_DIRECT_MODE; indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
indio_dev->channels = inv_icm42600_gyro_channels; indio_dev->channels = inv_icm42600_gyro_channels;
indio_dev->num_channels = ARRAY_SIZE(inv_icm42600_gyro_channels); indio_dev->num_channels = ARRAY_SIZE(inv_icm42600_gyro_channels);
indio_dev->available_scan_masks = inv_icm42600_gyro_scan_masks;
indio_dev->setup_ops = &inv_icm42600_buffer_ops;
iio_device_attach_buffer(indio_dev, buffer);
ret = devm_iio_device_register(dev, indio_dev); ret = devm_iio_device_register(dev, indio_dev);
if (ret) if (ret)
...@@ -610,3 +738,35 @@ struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st) ...@@ -610,3 +738,35 @@ struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st)
return indio_dev; return indio_dev;
} }
int inv_icm42600_gyro_parse_fifo(struct iio_dev *indio_dev)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
ssize_t i, size;
const void *accel, *gyro, *timestamp;
const int8_t *temp;
unsigned int odr;
struct inv_icm42600_gyro_buffer buffer;
/* parse all fifo packets */
for (i = 0; i < st->fifo.count; i += size) {
size = inv_icm42600_fifo_decode_packet(&st->fifo.data[i],
&accel, &gyro, &temp, &timestamp, &odr);
/* quit if error or FIFO is empty */
if (size <= 0)
return size;
/* skip packet if no gyro data or data is invalid */
if (gyro == NULL || !inv_icm42600_fifo_is_data_valid(gyro))
continue;
/* buffer is copied to userspace, zeroing it to avoid any data leak */
memset(&buffer, 0, sizeof(buffer));
memcpy(&buffer.gyro, gyro, sizeof(buffer.gyro));
/* convert 8 bits FIFO temperature in high resolution format */
buffer.temp = temp ? (*temp * 64) : 0;
iio_push_to_buffers(indio_dev, &buffer);
}
return 0;
}
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