Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
@@ -0,0 +1,30 @@
# SPDX-License-Identifier: GPL-2.0-or-later
config INV_ICM42600
tristate
select IIO_BUFFER
select IIO_INV_SENSORS_TIMESTAMP
config INV_ICM42600_I2C
tristate "InvenSense ICM-426xx I2C driver"
depends on I2C
select INV_ICM42600
select REGMAP_I2C
help
This driver supports the InvenSense ICM-426xx motion tracking
devices over I2C.
This driver can be built as a module. The module will be called
inv-icm42600-i2c.
config INV_ICM42600_SPI
tristate "InvenSense ICM-426xx SPI driver"
depends on SPI_MASTER
select INV_ICM42600
select REGMAP_SPI
help
This driver supports the InvenSense ICM-426xx motion tracking
devices over SPI.
This driver can be built as a module. The module will be called
inv-icm42600-spi.
@@ -0,0 +1,14 @@
# SPDX-License-Identifier: GPL-2.0-or-later
obj-$(CONFIG_INV_ICM42600) += inv-icm42600.o
inv-icm42600-y += inv_icm42600_core.o
inv-icm42600-y += inv_icm42600_gyro.o
inv-icm42600-y += inv_icm42600_accel.o
inv-icm42600-y += inv_icm42600_temp.o
inv-icm42600-y += inv_icm42600_buffer.o
obj-$(CONFIG_INV_ICM42600_I2C) += inv-icm42600-i2c.o
inv-icm42600-i2c-y += inv_icm42600_i2c.o
obj-$(CONFIG_INV_ICM42600_SPI) += inv-icm42600-spi.o
inv-icm42600-spi-y += inv_icm42600_spi.o
@@ -0,0 +1,487 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2020 Invensense, Inc.
*/
#ifndef INV_ICM42600_H_
#define INV_ICM42600_H_
#include <linux/bits.h>
#include <linux/bitfield.h>
#include <linux/regmap.h>
#include <linux/mutex.h>
#include <linux/regulator/consumer.h>
#include <linux/pm.h>
#include <linux/iio/iio.h>
#include <linux/iio/common/inv_sensors_timestamp.h>
#include "inv_icm42600_buffer.h"
enum inv_icm42600_chip {
INV_CHIP_INVALID,
INV_CHIP_ICM42600,
INV_CHIP_ICM42602,
INV_CHIP_ICM42605,
INV_CHIP_ICM42686,
INV_CHIP_ICM42622,
INV_CHIP_ICM42688,
INV_CHIP_ICM42631,
INV_CHIP_NB,
};
/* serial bus slew rates */
enum inv_icm42600_slew_rate {
INV_ICM42600_SLEW_RATE_20_60NS,
INV_ICM42600_SLEW_RATE_12_36NS,
INV_ICM42600_SLEW_RATE_6_18NS,
INV_ICM42600_SLEW_RATE_4_12NS,
INV_ICM42600_SLEW_RATE_2_6NS,
INV_ICM42600_SLEW_RATE_INF_2NS,
};
enum inv_icm42600_sensor_mode {
INV_ICM42600_SENSOR_MODE_OFF,
INV_ICM42600_SENSOR_MODE_STANDBY,
INV_ICM42600_SENSOR_MODE_LOW_POWER,
INV_ICM42600_SENSOR_MODE_LOW_NOISE,
INV_ICM42600_SENSOR_MODE_NB,
};
/* gyroscope fullscale values */
enum inv_icm42600_gyro_fs {
INV_ICM42600_GYRO_FS_2000DPS,
INV_ICM42600_GYRO_FS_1000DPS,
INV_ICM42600_GYRO_FS_500DPS,
INV_ICM42600_GYRO_FS_250DPS,
INV_ICM42600_GYRO_FS_125DPS,
INV_ICM42600_GYRO_FS_62_5DPS,
INV_ICM42600_GYRO_FS_31_25DPS,
INV_ICM42600_GYRO_FS_15_625DPS,
INV_ICM42600_GYRO_FS_NB,
};
enum inv_icm42686_gyro_fs {
INV_ICM42686_GYRO_FS_4000DPS,
INV_ICM42686_GYRO_FS_2000DPS,
INV_ICM42686_GYRO_FS_1000DPS,
INV_ICM42686_GYRO_FS_500DPS,
INV_ICM42686_GYRO_FS_250DPS,
INV_ICM42686_GYRO_FS_125DPS,
INV_ICM42686_GYRO_FS_62_5DPS,
INV_ICM42686_GYRO_FS_31_25DPS,
INV_ICM42686_GYRO_FS_NB,
};
/* accelerometer fullscale values */
enum inv_icm42600_accel_fs {
INV_ICM42600_ACCEL_FS_16G,
INV_ICM42600_ACCEL_FS_8G,
INV_ICM42600_ACCEL_FS_4G,
INV_ICM42600_ACCEL_FS_2G,
INV_ICM42600_ACCEL_FS_NB,
};
enum inv_icm42686_accel_fs {
INV_ICM42686_ACCEL_FS_32G,
INV_ICM42686_ACCEL_FS_16G,
INV_ICM42686_ACCEL_FS_8G,
INV_ICM42686_ACCEL_FS_4G,
INV_ICM42686_ACCEL_FS_2G,
INV_ICM42686_ACCEL_FS_NB,
};
/* ODR suffixed by LN or LP are Low-Noise or Low-Power mode only */
enum inv_icm42600_odr {
INV_ICM42600_ODR_8KHZ_LN = 3,
INV_ICM42600_ODR_4KHZ_LN,
INV_ICM42600_ODR_2KHZ_LN,
INV_ICM42600_ODR_1KHZ_LN,
INV_ICM42600_ODR_200HZ,
INV_ICM42600_ODR_100HZ,
INV_ICM42600_ODR_50HZ,
INV_ICM42600_ODR_25HZ,
INV_ICM42600_ODR_12_5HZ,
INV_ICM42600_ODR_6_25HZ_LP,
INV_ICM42600_ODR_3_125HZ_LP,
INV_ICM42600_ODR_1_5625HZ_LP,
INV_ICM42600_ODR_500HZ,
INV_ICM42600_ODR_NB,
};
enum inv_icm42600_filter {
/* Low-Noise mode sensor data filter (3rd order filter by default) */
INV_ICM42600_FILTER_BW_ODR_DIV_2,
/* Low-Power mode sensor data filter (averaging) */
INV_ICM42600_FILTER_AVG_1X = 1,
INV_ICM42600_FILTER_AVG_16X = 6,
};
struct inv_icm42600_sensor_conf {
int mode;
int fs;
int odr;
int filter;
};
#define INV_ICM42600_SENSOR_CONF_INIT {-1, -1, -1, -1}
struct inv_icm42600_conf {
struct inv_icm42600_sensor_conf gyro;
struct inv_icm42600_sensor_conf accel;
bool temp_en;
};
struct inv_icm42600_suspended {
enum inv_icm42600_sensor_mode gyro;
enum inv_icm42600_sensor_mode accel;
bool temp;
};
struct inv_icm42600_apex {
unsigned int on;
struct {
u64 value;
bool enable;
} wom;
};
/**
* struct inv_icm42600_state - driver state variables
* @lock: lock for serializing multiple registers access.
* @chip: chip identifier.
* @name: chip name.
* @map: regmap pointer.
* @vdd_supply: VDD voltage regulator for the chip.
* @vddio_supply: I/O voltage regulator for the chip.
* @irq: chip irq, required to enable/disable and set wakeup
* @orientation: sensor chip orientation relative to main hardware.
* @conf: chip sensors configurations.
* @suspended: suspended sensors configuration.
* @indio_gyro: gyroscope IIO device.
* @indio_accel: accelerometer IIO device.
* @timestamp: interrupt timestamps.
* @apex: APEX (Advanced Pedometer and Event detection) management
* @fifo: FIFO management structure.
* @buffer: data transfer buffer aligned for DMA.
*/
struct inv_icm42600_state {
struct mutex lock;
enum inv_icm42600_chip chip;
const char *name;
struct regmap *map;
struct regulator *vddio_supply;
int irq;
struct iio_mount_matrix orientation;
struct inv_icm42600_conf conf;
struct inv_icm42600_suspended suspended;
struct iio_dev *indio_gyro;
struct iio_dev *indio_accel;
struct {
s64 gyro;
s64 accel;
} timestamp;
struct inv_icm42600_apex apex;
struct inv_icm42600_fifo fifo;
u8 buffer[3] __aligned(IIO_DMA_MINALIGN);
};
/**
* struct inv_icm42600_sensor_state - sensor state variables
* @scales: table of scales.
* @scales_len: length (nb of items) of the scales table.
* @power_mode: sensor requested power mode (for common frequencies)
* @filter: sensor filter.
* @ts: timestamp module states.
*/
struct inv_icm42600_sensor_state {
const int *scales;
size_t scales_len;
enum inv_icm42600_sensor_mode power_mode;
enum inv_icm42600_filter filter;
struct inv_sensors_timestamp ts;
};
/* Virtual register addresses: @bank on MSB (4 upper bits), @address on LSB */
/* Bank selection register, available in all banks */
#define INV_ICM42600_REG_BANK_SEL 0x76
#define INV_ICM42600_BANK_SEL_MASK GENMASK(2, 0)
/* User bank 0 (MSB 0x00) */
#define INV_ICM42600_REG_DEVICE_CONFIG 0x0011
#define INV_ICM42600_DEVICE_CONFIG_SOFT_RESET BIT(0)
#define INV_ICM42600_REG_DRIVE_CONFIG 0x0013
#define INV_ICM42600_DRIVE_CONFIG_I2C_MASK GENMASK(5, 3)
#define INV_ICM42600_DRIVE_CONFIG_I2C(_rate) \
FIELD_PREP(INV_ICM42600_DRIVE_CONFIG_I2C_MASK, (_rate))
#define INV_ICM42600_DRIVE_CONFIG_SPI_MASK GENMASK(2, 0)
#define INV_ICM42600_DRIVE_CONFIG_SPI(_rate) \
FIELD_PREP(INV_ICM42600_DRIVE_CONFIG_SPI_MASK, (_rate))
#define INV_ICM42600_REG_INT_CONFIG 0x0014
#define INV_ICM42600_INT_CONFIG_INT2_LATCHED BIT(5)
#define INV_ICM42600_INT_CONFIG_INT2_PUSH_PULL BIT(4)
#define INV_ICM42600_INT_CONFIG_INT2_ACTIVE_HIGH BIT(3)
#define INV_ICM42600_INT_CONFIG_INT2_ACTIVE_LOW 0x00
#define INV_ICM42600_INT_CONFIG_INT1_LATCHED BIT(2)
#define INV_ICM42600_INT_CONFIG_INT1_PUSH_PULL BIT(1)
#define INV_ICM42600_INT_CONFIG_INT1_ACTIVE_HIGH BIT(0)
#define INV_ICM42600_INT_CONFIG_INT1_ACTIVE_LOW 0x00
#define INV_ICM42600_REG_FIFO_CONFIG 0x0016
#define INV_ICM42600_FIFO_CONFIG_MASK GENMASK(7, 6)
#define INV_ICM42600_FIFO_CONFIG_BYPASS \
FIELD_PREP(INV_ICM42600_FIFO_CONFIG_MASK, 0)
#define INV_ICM42600_FIFO_CONFIG_STREAM \
FIELD_PREP(INV_ICM42600_FIFO_CONFIG_MASK, 1)
#define INV_ICM42600_FIFO_CONFIG_STOP_ON_FULL \
FIELD_PREP(INV_ICM42600_FIFO_CONFIG_MASK, 2)
/* all sensor data are 16 bits (2 registers wide) in big-endian */
#define INV_ICM42600_REG_TEMP_DATA 0x001D
#define INV_ICM42600_REG_ACCEL_DATA_X 0x001F
#define INV_ICM42600_REG_ACCEL_DATA_Y 0x0021
#define INV_ICM42600_REG_ACCEL_DATA_Z 0x0023
#define INV_ICM42600_REG_GYRO_DATA_X 0x0025
#define INV_ICM42600_REG_GYRO_DATA_Y 0x0027
#define INV_ICM42600_REG_GYRO_DATA_Z 0x0029
#define INV_ICM42600_DATA_INVALID -32768
#define INV_ICM42600_REG_INT_STATUS 0x002D
#define INV_ICM42600_INT_STATUS_UI_FSYNC BIT(6)
#define INV_ICM42600_INT_STATUS_PLL_RDY BIT(5)
#define INV_ICM42600_INT_STATUS_RESET_DONE BIT(4)
#define INV_ICM42600_INT_STATUS_DATA_RDY BIT(3)
#define INV_ICM42600_INT_STATUS_FIFO_THS BIT(2)
#define INV_ICM42600_INT_STATUS_FIFO_FULL BIT(1)
#define INV_ICM42600_INT_STATUS_AGC_RDY BIT(0)
/*
* FIFO access registers
* FIFO count is 16 bits (2 registers) big-endian
* FIFO data is a continuous read register to read FIFO content
*/
#define INV_ICM42600_REG_FIFO_COUNT 0x002E
#define INV_ICM42600_REG_FIFO_DATA 0x0030
#define INV_ICM42600_REG_INT_STATUS2 0x0037
#define INV_ICM42600_INT_STATUS2_SMD_INT BIT(3)
#define INV_ICM42600_INT_STATUS2_WOM_INT GENMASK(2, 0)
#define INV_ICM42600_REG_INT_STATUS3 0x0038
#define INV_ICM42600_INT_STATUS3_STEP_DET_INT BIT(5)
#define INV_ICM42600_INT_STATUS3_STEP_CNT_OVF_INT BIT(4)
#define INV_ICM42600_INT_STATUS3_TILT_DET_INT BIT(3)
#define INV_ICM42600_INT_STATUS3_WAKE_INT BIT(2)
#define INV_ICM42600_INT_STATUS3_SLEEP_INT BIT(1)
#define INV_ICM42600_INT_STATUS3_TAP_DET_INT BIT(0)
#define INV_ICM42600_REG_SIGNAL_PATH_RESET 0x004B
#define INV_ICM42600_SIGNAL_PATH_RESET_DMP_INIT_EN BIT(6)
#define INV_ICM42600_SIGNAL_PATH_RESET_DMP_MEM_RESET BIT(5)
#define INV_ICM42600_SIGNAL_PATH_RESET_RESET BIT(3)
#define INV_ICM42600_SIGNAL_PATH_RESET_TMST_STROBE BIT(2)
#define INV_ICM42600_SIGNAL_PATH_RESET_FIFO_FLUSH BIT(1)
/* default configuration: all data big-endian and fifo count in bytes */
#define INV_ICM42600_REG_INTF_CONFIG0 0x004C
#define INV_ICM42600_INTF_CONFIG0_FIFO_HOLD_LAST_DATA BIT(7)
#define INV_ICM42600_INTF_CONFIG0_FIFO_COUNT_REC BIT(6)
#define INV_ICM42600_INTF_CONFIG0_FIFO_COUNT_ENDIAN BIT(5)
#define INV_ICM42600_INTF_CONFIG0_SENSOR_DATA_ENDIAN BIT(4)
#define INV_ICM42600_INTF_CONFIG0_UI_SIFS_CFG_MASK GENMASK(1, 0)
#define INV_ICM42600_INTF_CONFIG0_UI_SIFS_CFG_SPI_DIS \
FIELD_PREP(INV_ICM42600_INTF_CONFIG0_UI_SIFS_CFG_MASK, 2)
#define INV_ICM42600_INTF_CONFIG0_UI_SIFS_CFG_I2C_DIS \
FIELD_PREP(INV_ICM42600_INTF_CONFIG0_UI_SIFS_CFG_MASK, 3)
#define INV_ICM42600_REG_INTF_CONFIG1 0x004D
#define INV_ICM42600_INTF_CONFIG1_ACCEL_LP_CLK_RC BIT(3)
#define INV_ICM42600_REG_PWR_MGMT0 0x004E
#define INV_ICM42600_PWR_MGMT0_TEMP_DIS BIT(5)
#define INV_ICM42600_PWR_MGMT0_IDLE BIT(4)
#define INV_ICM42600_PWR_MGMT0_GYRO(_mode) \
FIELD_PREP(GENMASK(3, 2), (_mode))
#define INV_ICM42600_PWR_MGMT0_ACCEL(_mode) \
FIELD_PREP(GENMASK(1, 0), (_mode))
#define INV_ICM42600_REG_GYRO_CONFIG0 0x004F
#define INV_ICM42600_GYRO_CONFIG0_FS(_fs) \
FIELD_PREP(GENMASK(7, 5), (_fs))
#define INV_ICM42600_GYRO_CONFIG0_ODR(_odr) \
FIELD_PREP(GENMASK(3, 0), (_odr))
#define INV_ICM42600_REG_ACCEL_CONFIG0 0x0050
#define INV_ICM42600_ACCEL_CONFIG0_FS(_fs) \
FIELD_PREP(GENMASK(7, 5), (_fs))
#define INV_ICM42600_ACCEL_CONFIG0_ODR(_odr) \
FIELD_PREP(GENMASK(3, 0), (_odr))
#define INV_ICM42600_REG_GYRO_ACCEL_CONFIG0 0x0052
#define INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(_f) \
FIELD_PREP(GENMASK(7, 4), (_f))
#define INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(_f) \
FIELD_PREP(GENMASK(3, 0), (_f))
#define INV_ICM42600_REG_TMST_CONFIG 0x0054
#define INV_ICM42600_TMST_CONFIG_MASK GENMASK(4, 0)
#define INV_ICM42600_TMST_CONFIG_TMST_TO_REGS_EN BIT(4)
#define INV_ICM42600_TMST_CONFIG_TMST_RES_16US BIT(3)
#define INV_ICM42600_TMST_CONFIG_TMST_DELTA_EN BIT(2)
#define INV_ICM42600_TMST_CONFIG_TMST_FSYNC_EN BIT(1)
#define INV_ICM42600_TMST_CONFIG_TMST_EN BIT(0)
#define INV_ICM42600_REG_SMD_CONFIG 0x0057
#define INV_ICM42600_SMD_CONFIG_WOM_INT_MODE BIT(3)
#define INV_ICM42600_SMD_CONFIG_WOM_MODE BIT(2)
#define INV_ICM42600_SMD_CONFIG_SMD_MODE_OFF 0x00
#define INV_ICM42600_SMD_CONFIG_SMD_MODE_WOM 0x01
#define INV_ICM42600_SMD_CONFIG_SMD_MODE_SHORT 0x02
#define INV_ICM42600_SMD_CONFIG_SMD_MODE_LONG 0x03
#define INV_ICM42600_REG_FIFO_CONFIG1 0x005F
#define INV_ICM42600_FIFO_CONFIG1_RESUME_PARTIAL_RD BIT(6)
#define INV_ICM42600_FIFO_CONFIG1_WM_GT_TH BIT(5)
#define INV_ICM42600_FIFO_CONFIG1_TMST_FSYNC_EN BIT(3)
#define INV_ICM42600_FIFO_CONFIG1_TEMP_EN BIT(2)
#define INV_ICM42600_FIFO_CONFIG1_GYRO_EN BIT(1)
#define INV_ICM42600_FIFO_CONFIG1_ACCEL_EN BIT(0)
/* FIFO watermark is 16 bits (2 registers wide) in little-endian */
#define INV_ICM42600_REG_FIFO_WATERMARK 0x0060
#define INV_ICM42600_FIFO_WATERMARK_VAL(_wm) \
cpu_to_le16((_wm) & GENMASK(11, 0))
/* FIFO is 2048 bytes, let 12 samples for reading latency */
#define INV_ICM42600_FIFO_WATERMARK_MAX (2048 - 12 * 16)
#define INV_ICM42600_REG_INT_CONFIG1 0x0064
#define INV_ICM42600_INT_CONFIG1_TPULSE_DURATION BIT(6)
#define INV_ICM42600_INT_CONFIG1_TDEASSERT_DISABLE BIT(5)
#define INV_ICM42600_INT_CONFIG1_ASYNC_RESET BIT(4)
#define INV_ICM42600_REG_INT_SOURCE0 0x0065
#define INV_ICM42600_INT_SOURCE0_UI_FSYNC_INT1_EN BIT(6)
#define INV_ICM42600_INT_SOURCE0_PLL_RDY_INT1_EN BIT(5)
#define INV_ICM42600_INT_SOURCE0_RESET_DONE_INT1_EN BIT(4)
#define INV_ICM42600_INT_SOURCE0_UI_DRDY_INT1_EN BIT(3)
#define INV_ICM42600_INT_SOURCE0_FIFO_THS_INT1_EN BIT(2)
#define INV_ICM42600_INT_SOURCE0_FIFO_FULL_INT1_EN BIT(1)
#define INV_ICM42600_INT_SOURCE0_UI_AGC_RDY_INT1_EN BIT(0)
#define INV_ICM42600_REG_INT_SOURCE1 0x0066
#define INV_ICM42600_INT_SOURCE1_I3C_ERROR_INT1_EN BIT(6)
#define INV_ICM42600_INT_SOURCE1_SMD_INT1_EN BIT(3)
#define INV_ICM42600_INT_SOURCE1_WOM_INT1_EN GENMASK(2, 0)
#define INV_ICM42600_REG_WHOAMI 0x0075
#define INV_ICM42600_WHOAMI_ICM42600 0x40
#define INV_ICM42600_WHOAMI_ICM42602 0x41
#define INV_ICM42600_WHOAMI_ICM42605 0x42
#define INV_ICM42600_WHOAMI_ICM42686 0x44
#define INV_ICM42600_WHOAMI_ICM42622 0x46
#define INV_ICM42600_WHOAMI_ICM42688 0x47
#define INV_ICM42600_WHOAMI_ICM42631 0x5C
/* User bank 1 (MSB 0x10) */
#define INV_ICM42600_REG_SENSOR_CONFIG0 0x1003
#define INV_ICM42600_SENSOR_CONFIG0_ZG_DISABLE BIT(5)
#define INV_ICM42600_SENSOR_CONFIG0_YG_DISABLE BIT(4)
#define INV_ICM42600_SENSOR_CONFIG0_XG_DISABLE BIT(3)
#define INV_ICM42600_SENSOR_CONFIG0_ZA_DISABLE BIT(2)
#define INV_ICM42600_SENSOR_CONFIG0_YA_DISABLE BIT(1)
#define INV_ICM42600_SENSOR_CONFIG0_XA_DISABLE BIT(0)
/* Timestamp value is 20 bits (3 registers) in little-endian */
#define INV_ICM42600_REG_TMSTVAL 0x1062
#define INV_ICM42600_TMSTVAL_MASK GENMASK(19, 0)
#define INV_ICM42600_REG_INTF_CONFIG4 0x107A
#define INV_ICM42600_INTF_CONFIG4_I3C_BUS_ONLY BIT(6)
#define INV_ICM42600_INTF_CONFIG4_SPI_AP_4WIRE BIT(1)
#define INV_ICM42600_REG_INTF_CONFIG6 0x107C
#define INV_ICM42600_INTF_CONFIG6_MASK GENMASK(4, 0)
#define INV_ICM42600_INTF_CONFIG6_I3C_EN BIT(4)
#define INV_ICM42600_INTF_CONFIG6_I3C_IBI_BYTE_EN BIT(3)
#define INV_ICM42600_INTF_CONFIG6_I3C_IBI_EN BIT(2)
#define INV_ICM42600_INTF_CONFIG6_I3C_DDR_EN BIT(1)
#define INV_ICM42600_INTF_CONFIG6_I3C_SDR_EN BIT(0)
/* User bank 4 (MSB 0x40) */
#define INV_ICM42600_REG_ACCEL_WOM_X_THR 0x404A
#define INV_ICM42600_REG_ACCEL_WOM_Y_THR 0x404B
#define INV_ICM42600_REG_ACCEL_WOM_Z_THR 0x404C
#define INV_ICM42600_REG_INT_SOURCE8 0x404F
#define INV_ICM42600_INT_SOURCE8_FSYNC_IBI_EN BIT(5)
#define INV_ICM42600_INT_SOURCE8_PLL_RDY_IBI_EN BIT(4)
#define INV_ICM42600_INT_SOURCE8_UI_DRDY_IBI_EN BIT(3)
#define INV_ICM42600_INT_SOURCE8_FIFO_THS_IBI_EN BIT(2)
#define INV_ICM42600_INT_SOURCE8_FIFO_FULL_IBI_EN BIT(1)
#define INV_ICM42600_INT_SOURCE8_AGC_RDY_IBI_EN BIT(0)
#define INV_ICM42600_REG_OFFSET_USER0 0x4077
#define INV_ICM42600_REG_OFFSET_USER1 0x4078
#define INV_ICM42600_REG_OFFSET_USER2 0x4079
#define INV_ICM42600_REG_OFFSET_USER3 0x407A
#define INV_ICM42600_REG_OFFSET_USER4 0x407B
#define INV_ICM42600_REG_OFFSET_USER5 0x407C
#define INV_ICM42600_REG_OFFSET_USER6 0x407D
#define INV_ICM42600_REG_OFFSET_USER7 0x407E
#define INV_ICM42600_REG_OFFSET_USER8 0x407F
/* Sleep times required by the driver */
#define INV_ICM42600_POWER_UP_TIME_MS 100
#define INV_ICM42600_RESET_TIME_MS 1
#define INV_ICM42600_ACCEL_STARTUP_TIME_MS 20
#define INV_ICM42600_GYRO_STARTUP_TIME_MS 60
#define INV_ICM42600_GYRO_STOP_TIME_MS 150
#define INV_ICM42600_TEMP_STARTUP_TIME_MS 14
#define INV_ICM42600_SUSPEND_DELAY_MS 2000
typedef int (*inv_icm42600_bus_setup)(struct inv_icm42600_state *);
extern const struct regmap_config inv_icm42600_regmap_config;
extern const struct regmap_config inv_icm42600_spi_regmap_config;
extern const struct dev_pm_ops inv_icm42600_pm_ops;
const struct iio_mount_matrix *
inv_icm42600_get_mount_matrix(const struct iio_dev *indio_dev,
const struct iio_chan_spec *chan);
u32 inv_icm42600_odr_to_period(enum inv_icm42600_odr odr);
int inv_icm42600_set_accel_conf(struct inv_icm42600_state *st,
struct inv_icm42600_sensor_conf *conf,
unsigned int *sleep_ms);
int inv_icm42600_set_gyro_conf(struct inv_icm42600_state *st,
struct inv_icm42600_sensor_conf *conf,
unsigned int *sleep_ms);
int inv_icm42600_set_temp_conf(struct inv_icm42600_state *st, bool enable,
unsigned int *sleep_ms);
int inv_icm42600_enable_wom(struct inv_icm42600_state *st);
int inv_icm42600_disable_wom(struct inv_icm42600_state *st);
int inv_icm42600_debugfs_reg(struct iio_dev *indio_dev, unsigned int reg,
unsigned int writeval, unsigned int *readval);
int inv_icm42600_core_probe(struct regmap *regmap, int chip,
inv_icm42600_bus_setup bus_setup);
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);
int inv_icm42600_accel_parse_fifo(struct iio_dev *indio_dev);
void inv_icm42600_accel_handle_events(struct iio_dev *indio_dev,
unsigned int status2, unsigned int status3,
s64 timestamp);
#endif
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,599 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2020 Invensense, Inc.
*/
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/minmax.h>
#include <linux/mutex.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/delay.h>
#include <linux/iio/buffer.h>
#include <linux/iio/common/inv_sensors_timestamp.h>
#include <linux/iio/iio.h>
#include "inv_icm42600.h"
#include "inv_icm42600_buffer.h"
/* FIFO header: 1 byte */
#define INV_ICM42600_FIFO_HEADER_MSG BIT(7)
#define INV_ICM42600_FIFO_HEADER_ACCEL BIT(6)
#define INV_ICM42600_FIFO_HEADER_GYRO BIT(5)
#define INV_ICM42600_FIFO_HEADER_TMST_FSYNC GENMASK(3, 2)
#define INV_ICM42600_FIFO_HEADER_ODR_ACCEL BIT(1)
#define INV_ICM42600_FIFO_HEADER_ODR_GYRO BIT(0)
struct inv_icm42600_fifo_1sensor_packet {
u8 header;
struct inv_icm42600_fifo_sensor_data data;
s8 temp;
} __packed;
#define INV_ICM42600_FIFO_1SENSOR_PACKET_SIZE 8
struct inv_icm42600_fifo_2sensors_packet {
u8 header;
struct inv_icm42600_fifo_sensor_data accel;
struct inv_icm42600_fifo_sensor_data gyro;
s8 temp;
__be16 timestamp;
} __packed;
#define INV_ICM42600_FIFO_2SENSORS_PACKET_SIZE 16
ssize_t inv_icm42600_fifo_decode_packet(const void *packet, const void **accel,
const void **gyro, const s8 **temp,
const void **timestamp, unsigned int *odr)
{
const struct inv_icm42600_fifo_1sensor_packet *pack1 = packet;
const struct inv_icm42600_fifo_2sensors_packet *pack2 = packet;
u8 header = *((const u8 *)packet);
/* FIFO empty */
if (header & INV_ICM42600_FIFO_HEADER_MSG) {
*accel = NULL;
*gyro = NULL;
*temp = NULL;
*timestamp = NULL;
*odr = 0;
return 0;
}
/* handle odr flags */
*odr = 0;
if (header & INV_ICM42600_FIFO_HEADER_ODR_GYRO)
*odr |= INV_ICM42600_SENSOR_GYRO;
if (header & INV_ICM42600_FIFO_HEADER_ODR_ACCEL)
*odr |= INV_ICM42600_SENSOR_ACCEL;
/* accel + gyro */
if ((header & INV_ICM42600_FIFO_HEADER_ACCEL) &&
(header & INV_ICM42600_FIFO_HEADER_GYRO)) {
*accel = &pack2->accel;
*gyro = &pack2->gyro;
*temp = &pack2->temp;
*timestamp = &pack2->timestamp;
return INV_ICM42600_FIFO_2SENSORS_PACKET_SIZE;
}
/* accel only */
if (header & INV_ICM42600_FIFO_HEADER_ACCEL) {
*accel = &pack1->data;
*gyro = NULL;
*temp = &pack1->temp;
*timestamp = NULL;
return INV_ICM42600_FIFO_1SENSOR_PACKET_SIZE;
}
/* gyro only */
if (header & INV_ICM42600_FIFO_HEADER_GYRO) {
*accel = NULL;
*gyro = &pack1->data;
*temp = &pack1->temp;
*timestamp = NULL;
return INV_ICM42600_FIFO_1SENSOR_PACKET_SIZE;
}
/* invalid packet if here */
return -EINVAL;
}
void inv_icm42600_buffer_update_fifo_period(struct inv_icm42600_state *st)
{
u32 period_gyro, period_accel;
if (st->fifo.en & INV_ICM42600_SENSOR_GYRO)
period_gyro = inv_icm42600_odr_to_period(st->conf.gyro.odr);
else
period_gyro = U32_MAX;
if (st->fifo.en & INV_ICM42600_SENSOR_ACCEL)
period_accel = inv_icm42600_odr_to_period(st->conf.accel.odr);
else
period_accel = U32_MAX;
st->fifo.period = min(period_gyro, period_accel);
}
int inv_icm42600_buffer_set_fifo_en(struct inv_icm42600_state *st,
unsigned int fifo_en)
{
unsigned int mask, val;
int ret;
/* update only FIFO EN bits */
mask = INV_ICM42600_FIFO_CONFIG1_TMST_FSYNC_EN |
INV_ICM42600_FIFO_CONFIG1_TEMP_EN |
INV_ICM42600_FIFO_CONFIG1_GYRO_EN |
INV_ICM42600_FIFO_CONFIG1_ACCEL_EN;
val = 0;
if (fifo_en & INV_ICM42600_SENSOR_GYRO)
val |= INV_ICM42600_FIFO_CONFIG1_GYRO_EN;
if (fifo_en & INV_ICM42600_SENSOR_ACCEL)
val |= INV_ICM42600_FIFO_CONFIG1_ACCEL_EN;
if (fifo_en & INV_ICM42600_SENSOR_TEMP)
val |= INV_ICM42600_FIFO_CONFIG1_TEMP_EN;
ret = regmap_update_bits(st->map, INV_ICM42600_REG_FIFO_CONFIG1, mask, val);
if (ret)
return ret;
st->fifo.en = fifo_en;
inv_icm42600_buffer_update_fifo_period(st);
return 0;
}
static size_t inv_icm42600_get_packet_size(unsigned int fifo_en)
{
size_t packet_size;
if ((fifo_en & INV_ICM42600_SENSOR_GYRO) &&
(fifo_en & INV_ICM42600_SENSOR_ACCEL))
packet_size = INV_ICM42600_FIFO_2SENSORS_PACKET_SIZE;
else
packet_size = INV_ICM42600_FIFO_1SENSOR_PACKET_SIZE;
return packet_size;
}
static unsigned int inv_icm42600_wm_truncate(unsigned int watermark,
size_t packet_size)
{
size_t wm_size;
unsigned int wm;
wm_size = watermark * packet_size;
if (wm_size > INV_ICM42600_FIFO_WATERMARK_MAX)
wm_size = INV_ICM42600_FIFO_WATERMARK_MAX;
wm = wm_size / packet_size;
return wm;
}
/**
* inv_icm42600_buffer_update_watermark - update watermark FIFO threshold
* @st: driver internal state
*
* Returns 0 on success, a negative error code otherwise.
*
* FIFO watermark threshold is computed based on the required watermark values
* set for gyro and accel sensors. Since watermark is all about acceptable data
* latency, use the smallest setting between the 2. It means choosing the
* smallest latency but this is not as simple as choosing the smallest watermark
* value. Latency depends on watermark and ODR. It requires several steps:
* 1) compute gyro and accel latencies and choose the smallest value.
* 2) adapt the choosen latency so that it is a multiple of both gyro and accel
* ones. Otherwise it is possible that you don't meet a requirement. (for
* example with gyro @100Hz wm 4 and accel @100Hz with wm 6, choosing the
* value of 4 will not meet accel latency requirement because 6 is not a
* multiple of 4. You need to use the value 2.)
* 3) Since all periods are multiple of each others, watermark is computed by
* dividing this computed latency by the smallest period, which corresponds
* to the FIFO frequency. Beware that this is only true because we are not
* using 500Hz frequency which is not a multiple of the others.
*/
int inv_icm42600_buffer_update_watermark(struct inv_icm42600_state *st)
{
size_t packet_size, wm_size;
unsigned int wm_gyro, wm_accel, watermark;
u32 period_gyro, period_accel;
u32 latency_gyro, latency_accel, latency;
bool restore;
__le16 raw_wm;
int ret;
packet_size = inv_icm42600_get_packet_size(st->fifo.en);
/* compute sensors latency, depending on sensor watermark and odr */
wm_gyro = inv_icm42600_wm_truncate(st->fifo.watermark.gyro, packet_size);
wm_accel = inv_icm42600_wm_truncate(st->fifo.watermark.accel, packet_size);
/* use us for odr to avoid overflow using 32 bits values */
period_gyro = inv_icm42600_odr_to_period(st->conf.gyro.odr) / 1000UL;
period_accel = inv_icm42600_odr_to_period(st->conf.accel.odr) / 1000UL;
latency_gyro = period_gyro * wm_gyro;
latency_accel = period_accel * wm_accel;
/* 0 value for watermark means that the sensor is turned off */
if (wm_gyro == 0 && wm_accel == 0)
return 0;
if (latency_gyro == 0) {
watermark = wm_accel;
st->fifo.watermark.eff_accel = wm_accel;
} else if (latency_accel == 0) {
watermark = wm_gyro;
st->fifo.watermark.eff_gyro = wm_gyro;
} else {
/* compute the smallest latency that is a multiple of both */
if (latency_gyro <= latency_accel)
latency = latency_gyro - (latency_accel % latency_gyro);
else
latency = latency_accel - (latency_gyro % latency_accel);
/* all this works because periods are multiple of each others */
watermark = latency / min(period_gyro, period_accel);
if (watermark < 1)
watermark = 1;
/* update effective watermark */
st->fifo.watermark.eff_gyro = latency / period_gyro;
if (st->fifo.watermark.eff_gyro < 1)
st->fifo.watermark.eff_gyro = 1;
st->fifo.watermark.eff_accel = latency / period_accel;
if (st->fifo.watermark.eff_accel < 1)
st->fifo.watermark.eff_accel = 1;
}
/* compute watermark value in bytes */
wm_size = watermark * packet_size;
st->fifo.watermark.value = watermark;
/* changing FIFO watermark requires to turn off watermark interrupt */
ret = regmap_update_bits_check(st->map, INV_ICM42600_REG_INT_SOURCE0,
INV_ICM42600_INT_SOURCE0_FIFO_THS_INT1_EN,
0, &restore);
if (ret)
return ret;
raw_wm = INV_ICM42600_FIFO_WATERMARK_VAL(wm_size);
memcpy(st->buffer, &raw_wm, sizeof(raw_wm));
ret = regmap_bulk_write(st->map, INV_ICM42600_REG_FIFO_WATERMARK,
st->buffer, sizeof(raw_wm));
if (ret)
return ret;
/* restore watermark interrupt */
if (restore) {
ret = regmap_set_bits(st->map, INV_ICM42600_REG_INT_SOURCE0,
INV_ICM42600_INT_SOURCE0_FIFO_THS_INT1_EN);
if (ret)
return ret;
}
return 0;
}
static int inv_icm42600_buffer_preenable(struct iio_dev *indio_dev)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
struct device *dev = regmap_get_device(st->map);
struct inv_icm42600_sensor_state *sensor_st = iio_priv(indio_dev);
struct inv_sensors_timestamp *ts = &sensor_st->ts;
pm_runtime_get_sync(dev);
guard(mutex)(&st->lock);
inv_sensors_timestamp_reset(ts);
return 0;
}
/*
* update_scan_mode callback is turning sensors on and setting data FIFO enable
* bits.
*/
static int inv_icm42600_buffer_postenable(struct iio_dev *indio_dev)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
int ret;
guard(mutex)(&st->lock);
if (st->fifo.on) {
st->fifo.on++;
return 0;
}
/* set FIFO threshold interrupt */
ret = regmap_set_bits(st->map, INV_ICM42600_REG_INT_SOURCE0,
INV_ICM42600_INT_SOURCE0_FIFO_THS_INT1_EN);
if (ret)
return ret;
/* flush FIFO data */
ret = regmap_write(st->map, INV_ICM42600_REG_SIGNAL_PATH_RESET,
INV_ICM42600_SIGNAL_PATH_RESET_FIFO_FLUSH);
if (ret)
return ret;
/* set FIFO in streaming mode */
ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG,
INV_ICM42600_FIFO_CONFIG_STREAM);
if (ret)
return ret;
/* workaround: first read of FIFO count after reset is always 0 */
ret = regmap_bulk_read(st->map, INV_ICM42600_REG_FIFO_COUNT, st->buffer, 2);
if (ret)
return ret;
st->fifo.on++;
return 0;
}
static int inv_icm42600_buffer_predisable(struct iio_dev *indio_dev)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
int ret;
guard(mutex)(&st->lock);
if (st->fifo.on > 1) {
st->fifo.on--;
return 0;
}
/* set FIFO in bypass mode */
ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG,
INV_ICM42600_FIFO_CONFIG_BYPASS);
if (ret)
return ret;
/* flush FIFO data */
ret = regmap_write(st->map, INV_ICM42600_REG_SIGNAL_PATH_RESET,
INV_ICM42600_SIGNAL_PATH_RESET_FIFO_FLUSH);
if (ret)
return ret;
/* disable FIFO threshold interrupt */
ret = regmap_clear_bits(st->map, INV_ICM42600_REG_INT_SOURCE0,
INV_ICM42600_INT_SOURCE0_FIFO_THS_INT1_EN);
if (ret)
return ret;
st->fifo.on--;
return 0;
}
static int inv_icm42600_buffer_postdisable(struct iio_dev *indio_dev)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
struct inv_icm42600_sensor_state *sensor_st = iio_priv(indio_dev);
struct inv_sensors_timestamp *ts = &sensor_st->ts;
struct device *dev = regmap_get_device(st->map);
unsigned int sensor;
unsigned int *watermark;
struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
unsigned int sleep_temp = 0;
unsigned int sleep_sensor = 0;
unsigned int sleep;
int ret;
if (indio_dev == st->indio_gyro) {
sensor = INV_ICM42600_SENSOR_GYRO;
watermark = &st->fifo.watermark.gyro;
} else if (indio_dev == st->indio_accel) {
sensor = INV_ICM42600_SENSOR_ACCEL;
watermark = &st->fifo.watermark.accel;
} else {
return -EINVAL;
}
mutex_lock(&st->lock);
inv_sensors_timestamp_apply_odr(ts, 0, 0, 0);
ret = inv_icm42600_buffer_set_fifo_en(st, st->fifo.en & ~sensor);
if (ret)
goto out_unlock;
*watermark = 0;
ret = inv_icm42600_buffer_update_watermark(st);
if (ret)
goto out_unlock;
conf.mode = INV_ICM42600_SENSOR_MODE_OFF;
if (sensor == INV_ICM42600_SENSOR_GYRO)
ret = inv_icm42600_set_gyro_conf(st, &conf, &sleep_sensor);
else if (!st->apex.on)
ret = inv_icm42600_set_accel_conf(st, &conf, &sleep_sensor);
if (ret)
goto out_unlock;
/* if FIFO is off, turn temperature off */
if (!st->fifo.on)
ret = inv_icm42600_set_temp_conf(st, false, &sleep_temp);
out_unlock:
mutex_unlock(&st->lock);
/* sleep maximum required time */
sleep = max(sleep_sensor, sleep_temp);
if (sleep)
msleep(sleep);
pm_runtime_put_autosuspend(dev);
return ret;
}
const struct iio_buffer_setup_ops inv_icm42600_buffer_ops = {
.preenable = inv_icm42600_buffer_preenable,
.postenable = inv_icm42600_buffer_postenable,
.predisable = inv_icm42600_buffer_predisable,
.postdisable = inv_icm42600_buffer_postdisable,
};
int inv_icm42600_buffer_fifo_read(struct inv_icm42600_state *st,
unsigned int max)
{
size_t max_count;
__be16 *raw_fifo_count;
ssize_t i, size;
const void *accel, *gyro, *timestamp;
const s8 *temp;
unsigned int odr;
int ret;
/* reset all samples counters */
st->fifo.count = 0;
st->fifo.nb.gyro = 0;
st->fifo.nb.accel = 0;
st->fifo.nb.total = 0;
/* compute maximum FIFO read size (watermark for max = 0 interrupt case) */
if (max == 0)
max = st->fifo.watermark.value;
max_count = max * inv_icm42600_get_packet_size(st->fifo.en);
/* read FIFO count value */
raw_fifo_count = (__be16 *)st->buffer;
ret = regmap_bulk_read(st->map, INV_ICM42600_REG_FIFO_COUNT,
raw_fifo_count, sizeof(*raw_fifo_count));
if (ret)
return ret;
st->fifo.count = be16_to_cpup(raw_fifo_count);
/* check and clamp FIFO count value */
if (st->fifo.count == 0)
return 0;
if (st->fifo.count > max_count)
st->fifo.count = max_count;
/* read all FIFO data in internal buffer */
ret = regmap_noinc_read(st->map, INV_ICM42600_REG_FIFO_DATA,
st->fifo.data, st->fifo.count);
if (ret)
return ret;
/* compute number of samples for each sensor */
for (i = 0; i < st->fifo.count; i += size) {
size = inv_icm42600_fifo_decode_packet(&st->fifo.data[i],
&accel, &gyro, &temp, &timestamp, &odr);
if (size <= 0)
break;
if (gyro != NULL && inv_icm42600_fifo_is_data_valid(gyro))
st->fifo.nb.gyro++;
if (accel != NULL && inv_icm42600_fifo_is_data_valid(accel))
st->fifo.nb.accel++;
st->fifo.nb.total++;
}
return 0;
}
int inv_icm42600_buffer_fifo_parse(struct inv_icm42600_state *st)
{
struct inv_icm42600_sensor_state *gyro_st = iio_priv(st->indio_gyro);
struct inv_icm42600_sensor_state *accel_st = iio_priv(st->indio_accel);
struct inv_sensors_timestamp *ts;
int ret;
if (st->fifo.nb.total == 0)
return 0;
/* handle gyroscope timestamp and FIFO data parsing */
if (st->fifo.nb.gyro > 0) {
ts = &gyro_st->ts;
inv_sensors_timestamp_interrupt(ts, st->fifo.watermark.eff_gyro,
st->timestamp.gyro);
ret = inv_icm42600_gyro_parse_fifo(st->indio_gyro);
if (ret)
return ret;
}
/* handle accelerometer timestamp and FIFO data parsing */
if (st->fifo.nb.accel > 0) {
ts = &accel_st->ts;
inv_sensors_timestamp_interrupt(ts, st->fifo.watermark.eff_accel,
st->timestamp.accel);
ret = inv_icm42600_accel_parse_fifo(st->indio_accel);
if (ret)
return ret;
}
return 0;
}
int inv_icm42600_buffer_hwfifo_flush(struct inv_icm42600_state *st,
unsigned int count)
{
struct inv_icm42600_sensor_state *gyro_st = iio_priv(st->indio_gyro);
struct inv_icm42600_sensor_state *accel_st = iio_priv(st->indio_accel);
struct inv_sensors_timestamp *ts;
s64 gyro_ts, accel_ts;
int ret;
gyro_ts = iio_get_time_ns(st->indio_gyro);
accel_ts = iio_get_time_ns(st->indio_accel);
ret = inv_icm42600_buffer_fifo_read(st, count);
if (ret)
return ret;
if (st->fifo.nb.total == 0)
return 0;
if (st->fifo.nb.gyro > 0) {
ts = &gyro_st->ts;
inv_sensors_timestamp_interrupt(ts, st->fifo.nb.gyro, gyro_ts);
ret = inv_icm42600_gyro_parse_fifo(st->indio_gyro);
if (ret)
return ret;
}
if (st->fifo.nb.accel > 0) {
ts = &accel_st->ts;
inv_sensors_timestamp_interrupt(ts, st->fifo.nb.accel, accel_ts);
ret = inv_icm42600_accel_parse_fifo(st->indio_accel);
if (ret)
return ret;
}
return 0;
}
int inv_icm42600_buffer_init(struct inv_icm42600_state *st)
{
unsigned int val;
int ret;
st->fifo.watermark.eff_gyro = 1;
st->fifo.watermark.eff_accel = 1;
st->fifo.watermark.value = 1;
/*
* Default FIFO configuration (bits 7 to 5)
* - use invalid value
* - FIFO count in bytes
* - FIFO count in big endian
*/
val = INV_ICM42600_INTF_CONFIG0_FIFO_COUNT_ENDIAN;
ret = regmap_update_bits(st->map, INV_ICM42600_REG_INTF_CONFIG0,
GENMASK(7, 5), val);
if (ret)
return ret;
/*
* Enable FIFO partial read and continuous watermark interrupt.
* Disable all FIFO EN bits.
*/
val = INV_ICM42600_FIFO_CONFIG1_RESUME_PARTIAL_RD |
INV_ICM42600_FIFO_CONFIG1_WM_GT_TH;
return regmap_update_bits(st->map, INV_ICM42600_REG_FIFO_CONFIG1,
GENMASK(6, 5) | GENMASK(3, 0), val);
}
@@ -0,0 +1,101 @@
/* 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;
u32 period;
struct {
unsigned int gyro;
unsigned int accel;
unsigned int eff_gyro;
unsigned int eff_accel;
unsigned int value;
} watermark;
size_t count;
struct {
size_t gyro;
size_t accel;
size_t total;
} nb;
u8 data[2080] __aligned(IIO_DMA_MINALIGN);
};
/* 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 s16 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)
{
s16 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 s8 **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
@@ -0,0 +1,961 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2020 Invensense, Inc.
*/
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/mutex.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/regulator/consumer.h>
#include <linux/pm_runtime.h>
#include <linux/property.h>
#include <linux/regmap.h>
#include <linux/iio/iio.h>
#include "inv_icm42600.h"
#include "inv_icm42600_buffer.h"
static const struct regmap_range_cfg inv_icm42600_regmap_ranges[] = {
{
.name = "user banks",
.range_min = 0x0000,
.range_max = 0x4FFF,
.selector_reg = INV_ICM42600_REG_BANK_SEL,
.selector_mask = INV_ICM42600_BANK_SEL_MASK,
.selector_shift = 0,
.window_start = 0,
.window_len = 0x1000,
},
};
static const struct regmap_range inv_icm42600_regmap_volatile_yes_ranges[] = {
/* Sensor data registers */
regmap_reg_range(0x001D, 0x002A),
/* INT status, FIFO, APEX data */
regmap_reg_range(0x002D, 0x0038),
/* Signal path reset */
regmap_reg_range(0x004B, 0x004B),
/* FIFO lost packets */
regmap_reg_range(0x006C, 0x006D),
/* Timestamp value */
regmap_reg_range(0x1062, 0x1064),
};
static const struct regmap_range inv_icm42600_regmap_volatile_no_ranges[] = {
regmap_reg_range(0x0000, 0x001C),
regmap_reg_range(0x006E, 0x1061),
regmap_reg_range(0x1065, 0x4FFF),
};
static const struct regmap_access_table inv_icm42600_regmap_volatile_accesses[] = {
{
.yes_ranges = inv_icm42600_regmap_volatile_yes_ranges,
.n_yes_ranges = ARRAY_SIZE(inv_icm42600_regmap_volatile_yes_ranges),
.no_ranges = inv_icm42600_regmap_volatile_no_ranges,
.n_no_ranges = ARRAY_SIZE(inv_icm42600_regmap_volatile_no_ranges),
},
};
static const struct regmap_range inv_icm42600_regmap_rd_noinc_no_ranges[] = {
regmap_reg_range(0x0000, INV_ICM42600_REG_FIFO_DATA - 1),
regmap_reg_range(INV_ICM42600_REG_FIFO_DATA + 1, 0x4FFF),
};
static const struct regmap_access_table inv_icm42600_regmap_rd_noinc_accesses[] = {
{
.no_ranges = inv_icm42600_regmap_rd_noinc_no_ranges,
.n_no_ranges = ARRAY_SIZE(inv_icm42600_regmap_rd_noinc_no_ranges),
},
};
const struct regmap_config inv_icm42600_regmap_config = {
.name = "inv_icm42600",
.reg_bits = 8,
.val_bits = 8,
.max_register = 0x4FFF,
.ranges = inv_icm42600_regmap_ranges,
.num_ranges = ARRAY_SIZE(inv_icm42600_regmap_ranges),
.volatile_table = inv_icm42600_regmap_volatile_accesses,
.rd_noinc_table = inv_icm42600_regmap_rd_noinc_accesses,
.cache_type = REGCACHE_MAPLE,
};
EXPORT_SYMBOL_NS_GPL(inv_icm42600_regmap_config, "IIO_ICM42600");
/* define specific regmap for SPI not supporting burst write */
const struct regmap_config inv_icm42600_spi_regmap_config = {
.name = "inv_icm42600",
.reg_bits = 8,
.val_bits = 8,
.max_register = 0x4FFF,
.ranges = inv_icm42600_regmap_ranges,
.num_ranges = ARRAY_SIZE(inv_icm42600_regmap_ranges),
.volatile_table = inv_icm42600_regmap_volatile_accesses,
.rd_noinc_table = inv_icm42600_regmap_rd_noinc_accesses,
.cache_type = REGCACHE_MAPLE,
.use_single_write = true,
};
EXPORT_SYMBOL_NS_GPL(inv_icm42600_spi_regmap_config, "IIO_ICM42600");
struct inv_icm42600_hw {
u8 whoami;
const char *name;
const struct inv_icm42600_conf *conf;
};
/* chip initial default configuration */
static const struct inv_icm42600_conf inv_icm42600_default_conf = {
.gyro = {
.mode = INV_ICM42600_SENSOR_MODE_OFF,
.fs = INV_ICM42600_GYRO_FS_2000DPS,
.odr = INV_ICM42600_ODR_50HZ,
.filter = INV_ICM42600_FILTER_BW_ODR_DIV_2,
},
.accel = {
.mode = INV_ICM42600_SENSOR_MODE_OFF,
.fs = INV_ICM42600_ACCEL_FS_16G,
.odr = INV_ICM42600_ODR_50HZ,
.filter = INV_ICM42600_FILTER_BW_ODR_DIV_2,
},
.temp_en = false,
};
static const struct inv_icm42600_conf inv_icm42686_default_conf = {
.gyro = {
.mode = INV_ICM42600_SENSOR_MODE_OFF,
.fs = INV_ICM42686_GYRO_FS_4000DPS,
.odr = INV_ICM42600_ODR_50HZ,
.filter = INV_ICM42600_FILTER_BW_ODR_DIV_2,
},
.accel = {
.mode = INV_ICM42600_SENSOR_MODE_OFF,
.fs = INV_ICM42686_ACCEL_FS_32G,
.odr = INV_ICM42600_ODR_50HZ,
.filter = INV_ICM42600_FILTER_BW_ODR_DIV_2,
},
.temp_en = false,
};
static const struct inv_icm42600_hw inv_icm42600_hw[INV_CHIP_NB] = {
[INV_CHIP_ICM42600] = {
.whoami = INV_ICM42600_WHOAMI_ICM42600,
.name = "icm42600",
.conf = &inv_icm42600_default_conf,
},
[INV_CHIP_ICM42602] = {
.whoami = INV_ICM42600_WHOAMI_ICM42602,
.name = "icm42602",
.conf = &inv_icm42600_default_conf,
},
[INV_CHIP_ICM42605] = {
.whoami = INV_ICM42600_WHOAMI_ICM42605,
.name = "icm42605",
.conf = &inv_icm42600_default_conf,
},
[INV_CHIP_ICM42686] = {
.whoami = INV_ICM42600_WHOAMI_ICM42686,
.name = "icm42686",
.conf = &inv_icm42686_default_conf,
},
[INV_CHIP_ICM42622] = {
.whoami = INV_ICM42600_WHOAMI_ICM42622,
.name = "icm42622",
.conf = &inv_icm42600_default_conf,
},
[INV_CHIP_ICM42688] = {
.whoami = INV_ICM42600_WHOAMI_ICM42688,
.name = "icm42688",
.conf = &inv_icm42600_default_conf,
},
[INV_CHIP_ICM42631] = {
.whoami = INV_ICM42600_WHOAMI_ICM42631,
.name = "icm42631",
.conf = &inv_icm42600_default_conf,
},
};
const struct iio_mount_matrix *
inv_icm42600_get_mount_matrix(const struct iio_dev *indio_dev,
const struct iio_chan_spec *chan)
{
const struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
return &st->orientation;
}
u32 inv_icm42600_odr_to_period(enum inv_icm42600_odr odr)
{
static u32 odr_periods[INV_ICM42600_ODR_NB] = {
/* reserved values */
0, 0, 0,
/* 8kHz */
125000,
/* 4kHz */
250000,
/* 2kHz */
500000,
/* 1kHz */
1000000,
/* 200Hz */
5000000,
/* 100Hz */
10000000,
/* 50Hz */
20000000,
/* 25Hz */
40000000,
/* 12.5Hz */
80000000,
/* 6.25Hz */
160000000,
/* 3.125Hz */
320000000,
/* 1.5625Hz */
640000000,
/* 500Hz */
2000000,
};
return odr_periods[odr];
}
static int inv_icm42600_set_pwr_mgmt0(struct inv_icm42600_state *st,
enum inv_icm42600_sensor_mode gyro,
enum inv_icm42600_sensor_mode accel,
bool temp, unsigned int *sleep_ms)
{
enum inv_icm42600_sensor_mode oldgyro = st->conf.gyro.mode;
enum inv_icm42600_sensor_mode oldaccel = st->conf.accel.mode;
bool oldtemp = st->conf.temp_en;
unsigned int sleepval;
unsigned int val;
int ret;
/* if nothing changed, exit */
if (gyro == oldgyro && accel == oldaccel && temp == oldtemp)
return 0;
val = INV_ICM42600_PWR_MGMT0_GYRO(gyro) |
INV_ICM42600_PWR_MGMT0_ACCEL(accel);
if (!temp)
val |= INV_ICM42600_PWR_MGMT0_TEMP_DIS;
ret = regmap_write(st->map, INV_ICM42600_REG_PWR_MGMT0, val);
if (ret)
return ret;
st->conf.gyro.mode = gyro;
st->conf.accel.mode = accel;
st->conf.temp_en = temp;
/* compute required wait time for sensors to stabilize */
sleepval = 0;
/* temperature stabilization time */
if (temp && !oldtemp) {
if (sleepval < INV_ICM42600_TEMP_STARTUP_TIME_MS)
sleepval = INV_ICM42600_TEMP_STARTUP_TIME_MS;
}
/* accel startup time */
if (accel != oldaccel && oldaccel == INV_ICM42600_SENSOR_MODE_OFF) {
/* block any register write for at least 200 µs */
usleep_range(200, 300);
if (sleepval < INV_ICM42600_ACCEL_STARTUP_TIME_MS)
sleepval = INV_ICM42600_ACCEL_STARTUP_TIME_MS;
}
if (gyro != oldgyro) {
/* gyro startup time */
if (oldgyro == INV_ICM42600_SENSOR_MODE_OFF) {
/* block any register write for at least 200 µs */
usleep_range(200, 300);
if (sleepval < INV_ICM42600_GYRO_STARTUP_TIME_MS)
sleepval = INV_ICM42600_GYRO_STARTUP_TIME_MS;
/* gyro stop time */
} else if (gyro == INV_ICM42600_SENSOR_MODE_OFF) {
if (sleepval < INV_ICM42600_GYRO_STOP_TIME_MS)
sleepval = INV_ICM42600_GYRO_STOP_TIME_MS;
}
}
/* deferred sleep value if sleep pointer is provided or direct sleep */
if (sleep_ms)
*sleep_ms = sleepval;
else if (sleepval)
msleep(sleepval);
return 0;
}
int inv_icm42600_set_accel_conf(struct inv_icm42600_state *st,
struct inv_icm42600_sensor_conf *conf,
unsigned int *sleep_ms)
{
struct inv_icm42600_sensor_conf *oldconf = &st->conf.accel;
unsigned int val;
int ret;
/* Sanitize missing values with current values */
if (conf->mode < 0)
conf->mode = oldconf->mode;
if (conf->fs < 0)
conf->fs = oldconf->fs;
if (conf->odr < 0)
conf->odr = oldconf->odr;
if (conf->filter < 0)
conf->filter = oldconf->filter;
/* force power mode against ODR when sensor is on */
switch (conf->mode) {
case INV_ICM42600_SENSOR_MODE_LOW_POWER:
case INV_ICM42600_SENSOR_MODE_LOW_NOISE:
if (conf->odr <= INV_ICM42600_ODR_1KHZ_LN) {
conf->mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
conf->filter = INV_ICM42600_FILTER_BW_ODR_DIV_2;
} else if (conf->odr >= INV_ICM42600_ODR_6_25HZ_LP &&
conf->odr <= INV_ICM42600_ODR_1_5625HZ_LP) {
conf->mode = INV_ICM42600_SENSOR_MODE_LOW_POWER;
conf->filter = INV_ICM42600_FILTER_AVG_16X;
}
break;
default:
break;
}
/* set ACCEL_CONFIG0 register (accel fullscale & odr) */
if (conf->fs != oldconf->fs || conf->odr != oldconf->odr) {
val = INV_ICM42600_ACCEL_CONFIG0_FS(conf->fs) |
INV_ICM42600_ACCEL_CONFIG0_ODR(conf->odr);
ret = regmap_write(st->map, INV_ICM42600_REG_ACCEL_CONFIG0, val);
if (ret)
return ret;
oldconf->fs = conf->fs;
oldconf->odr = conf->odr;
}
/* set GYRO_ACCEL_CONFIG0 register (accel filter) */
if (conf->filter != oldconf->filter) {
val = INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(conf->filter) |
INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(st->conf.gyro.filter);
ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_ACCEL_CONFIG0, val);
if (ret)
return ret;
oldconf->filter = conf->filter;
}
/* set PWR_MGMT0 register (accel sensor mode) */
return inv_icm42600_set_pwr_mgmt0(st, st->conf.gyro.mode, conf->mode,
st->conf.temp_en, sleep_ms);
}
int inv_icm42600_set_gyro_conf(struct inv_icm42600_state *st,
struct inv_icm42600_sensor_conf *conf,
unsigned int *sleep_ms)
{
struct inv_icm42600_sensor_conf *oldconf = &st->conf.gyro;
unsigned int val;
int ret;
/* sanitize missing values with current values */
if (conf->mode < 0)
conf->mode = oldconf->mode;
if (conf->fs < 0)
conf->fs = oldconf->fs;
if (conf->odr < 0)
conf->odr = oldconf->odr;
if (conf->filter < 0)
conf->filter = oldconf->filter;
/* set GYRO_CONFIG0 register (gyro fullscale & odr) */
if (conf->fs != oldconf->fs || conf->odr != oldconf->odr) {
val = INV_ICM42600_GYRO_CONFIG0_FS(conf->fs) |
INV_ICM42600_GYRO_CONFIG0_ODR(conf->odr);
ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_CONFIG0, val);
if (ret)
return ret;
oldconf->fs = conf->fs;
oldconf->odr = conf->odr;
}
/* set GYRO_ACCEL_CONFIG0 register (gyro filter) */
if (conf->filter != oldconf->filter) {
val = INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(st->conf.accel.filter) |
INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(conf->filter);
ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_ACCEL_CONFIG0, val);
if (ret)
return ret;
oldconf->filter = conf->filter;
}
/* set PWR_MGMT0 register (gyro sensor mode) */
return inv_icm42600_set_pwr_mgmt0(st, conf->mode, st->conf.accel.mode,
st->conf.temp_en, sleep_ms);
return 0;
}
int inv_icm42600_set_temp_conf(struct inv_icm42600_state *st, bool enable,
unsigned int *sleep_ms)
{
return inv_icm42600_set_pwr_mgmt0(st, st->conf.gyro.mode,
st->conf.accel.mode, enable,
sleep_ms);
}
int inv_icm42600_enable_wom(struct inv_icm42600_state *st)
{
int ret;
/* enable WoM hardware */
ret = regmap_write(st->map, INV_ICM42600_REG_SMD_CONFIG,
INV_ICM42600_SMD_CONFIG_SMD_MODE_WOM |
INV_ICM42600_SMD_CONFIG_WOM_MODE);
if (ret)
return ret;
/* enable WoM interrupt */
return regmap_set_bits(st->map, INV_ICM42600_REG_INT_SOURCE1,
INV_ICM42600_INT_SOURCE1_WOM_INT1_EN);
}
int inv_icm42600_disable_wom(struct inv_icm42600_state *st)
{
int ret;
/* disable WoM interrupt */
ret = regmap_clear_bits(st->map, INV_ICM42600_REG_INT_SOURCE1,
INV_ICM42600_INT_SOURCE1_WOM_INT1_EN);
if (ret)
return ret;
/* disable WoM hardware */
return regmap_write(st->map, INV_ICM42600_REG_SMD_CONFIG,
INV_ICM42600_SMD_CONFIG_SMD_MODE_OFF);
}
int inv_icm42600_debugfs_reg(struct iio_dev *indio_dev, unsigned int reg,
unsigned int writeval, unsigned int *readval)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
guard(mutex)(&st->lock);
if (readval)
return regmap_read(st->map, reg, readval);
return regmap_write(st->map, reg, writeval);
}
static int inv_icm42600_set_conf(struct inv_icm42600_state *st,
const struct inv_icm42600_conf *conf)
{
unsigned int val;
int ret;
/* set PWR_MGMT0 register (gyro & accel sensor mode, temp enabled) */
val = INV_ICM42600_PWR_MGMT0_GYRO(conf->gyro.mode) |
INV_ICM42600_PWR_MGMT0_ACCEL(conf->accel.mode);
if (!conf->temp_en)
val |= INV_ICM42600_PWR_MGMT0_TEMP_DIS;
ret = regmap_write(st->map, INV_ICM42600_REG_PWR_MGMT0, val);
if (ret)
return ret;
/* set GYRO_CONFIG0 register (gyro fullscale & odr) */
val = INV_ICM42600_GYRO_CONFIG0_FS(conf->gyro.fs) |
INV_ICM42600_GYRO_CONFIG0_ODR(conf->gyro.odr);
ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_CONFIG0, val);
if (ret)
return ret;
/* set ACCEL_CONFIG0 register (accel fullscale & odr) */
val = INV_ICM42600_ACCEL_CONFIG0_FS(conf->accel.fs) |
INV_ICM42600_ACCEL_CONFIG0_ODR(conf->accel.odr);
ret = regmap_write(st->map, INV_ICM42600_REG_ACCEL_CONFIG0, val);
if (ret)
return ret;
/* set GYRO_ACCEL_CONFIG0 register (gyro & accel filters) */
val = INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(conf->accel.filter) |
INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(conf->gyro.filter);
ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_ACCEL_CONFIG0, val);
if (ret)
return ret;
/* update internal conf */
st->conf = *conf;
return 0;
}
/**
* inv_icm42600_setup() - check and setup chip
* @st: driver internal state
* @bus_setup: callback for setting up bus specific registers
*
* Returns 0 on success, a negative error code otherwise.
*/
static int inv_icm42600_setup(struct inv_icm42600_state *st,
inv_icm42600_bus_setup bus_setup)
{
const struct inv_icm42600_hw *hw = &inv_icm42600_hw[st->chip];
const struct device *dev = regmap_get_device(st->map);
unsigned int val;
int ret;
/* check chip self-identification value */
ret = regmap_read(st->map, INV_ICM42600_REG_WHOAMI, &val);
if (ret)
return ret;
if (val != hw->whoami) {
dev_err(dev, "invalid whoami %#02x expected %#02x (%s)\n",
val, hw->whoami, hw->name);
return -ENODEV;
}
st->name = hw->name;
/* reset to make sure previous state are not there */
ret = regmap_write(st->map, INV_ICM42600_REG_DEVICE_CONFIG,
INV_ICM42600_DEVICE_CONFIG_SOFT_RESET);
if (ret)
return ret;
msleep(INV_ICM42600_RESET_TIME_MS);
ret = regmap_read(st->map, INV_ICM42600_REG_INT_STATUS, &val);
if (ret)
return ret;
if (!(val & INV_ICM42600_INT_STATUS_RESET_DONE)) {
dev_err(dev, "reset error, reset done bit not set\n");
return -ENODEV;
}
/* set chip bus configuration */
ret = bus_setup(st);
if (ret)
return ret;
/* sensor data in big-endian (default) */
ret = regmap_set_bits(st->map, INV_ICM42600_REG_INTF_CONFIG0,
INV_ICM42600_INTF_CONFIG0_SENSOR_DATA_ENDIAN);
if (ret)
return ret;
/*
* Use RC clock for accel low-power to fix glitches when switching
* gyro on/off while accel low-power is on.
*/
ret = regmap_update_bits(st->map, INV_ICM42600_REG_INTF_CONFIG1,
INV_ICM42600_INTF_CONFIG1_ACCEL_LP_CLK_RC,
INV_ICM42600_INTF_CONFIG1_ACCEL_LP_CLK_RC);
if (ret)
return ret;
return inv_icm42600_set_conf(st, hw->conf);
}
static irqreturn_t inv_icm42600_irq_timestamp(int irq, void *_data)
{
struct inv_icm42600_state *st = _data;
st->timestamp.gyro = iio_get_time_ns(st->indio_gyro);
st->timestamp.accel = iio_get_time_ns(st->indio_accel);
return IRQ_WAKE_THREAD;
}
static irqreturn_t inv_icm42600_irq_handler(int irq, void *_data)
{
struct inv_icm42600_state *st = _data;
struct device *dev = regmap_get_device(st->map);
unsigned int status;
int ret;
mutex_lock(&st->lock);
if (st->apex.on) {
unsigned int status2, status3;
/* read INT_STATUS2 and INT_STATUS3 in 1 operation */
ret = regmap_bulk_read(st->map, INV_ICM42600_REG_INT_STATUS2, st->buffer, 2);
if (ret)
goto out_unlock;
status2 = st->buffer[0];
status3 = st->buffer[1];
inv_icm42600_accel_handle_events(st->indio_accel, status2, status3,
st->timestamp.accel);
}
ret = regmap_read(st->map, INV_ICM42600_REG_INT_STATUS, &status);
if (ret)
goto out_unlock;
/* FIFO full */
if (status & INV_ICM42600_INT_STATUS_FIFO_FULL)
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:
mutex_unlock(&st->lock);
return IRQ_HANDLED;
}
/**
* inv_icm42600_irq_init() - initialize int pin and interrupt handler
* @st: driver internal state
* @irq: irq number
* @irq_type: irq trigger type
* @open_drain: true if irq is open drain, false for push-pull
*
* Returns 0 on success, a negative error code otherwise.
*/
static int inv_icm42600_irq_init(struct inv_icm42600_state *st, int irq,
int irq_type, bool open_drain)
{
struct device *dev = regmap_get_device(st->map);
unsigned int val;
int ret;
/* configure INT1 interrupt: default is active low on edge */
switch (irq_type) {
case IRQF_TRIGGER_RISING:
case IRQF_TRIGGER_HIGH:
val = INV_ICM42600_INT_CONFIG_INT1_ACTIVE_HIGH;
break;
default:
val = INV_ICM42600_INT_CONFIG_INT1_ACTIVE_LOW;
break;
}
switch (irq_type) {
case IRQF_TRIGGER_LOW:
case IRQF_TRIGGER_HIGH:
val |= INV_ICM42600_INT_CONFIG_INT1_LATCHED;
break;
default:
break;
}
if (!open_drain)
val |= INV_ICM42600_INT_CONFIG_INT1_PUSH_PULL;
ret = regmap_write(st->map, INV_ICM42600_REG_INT_CONFIG, val);
if (ret)
return ret;
/* Deassert async reset for proper INT pin operation (cf datasheet) */
ret = regmap_clear_bits(st->map, INV_ICM42600_REG_INT_CONFIG1,
INV_ICM42600_INT_CONFIG1_ASYNC_RESET);
if (ret)
return ret;
irq_type |= IRQF_ONESHOT;
return devm_request_threaded_irq(dev, irq, inv_icm42600_irq_timestamp,
inv_icm42600_irq_handler, irq_type,
"inv_icm42600", st);
}
static int inv_icm42600_timestamp_setup(struct inv_icm42600_state *st)
{
unsigned int val;
/* enable timestamp register */
val = INV_ICM42600_TMST_CONFIG_TMST_TO_REGS_EN |
INV_ICM42600_TMST_CONFIG_TMST_EN;
return regmap_update_bits(st->map, INV_ICM42600_REG_TMST_CONFIG,
INV_ICM42600_TMST_CONFIG_MASK, val);
}
static int inv_icm42600_enable_regulator_vddio(struct inv_icm42600_state *st)
{
int ret;
ret = regulator_enable(st->vddio_supply);
if (ret)
return ret;
/* wait a little for supply ramp */
usleep_range(3000, 4000);
return 0;
}
static void inv_icm42600_disable_vddio_reg(void *_data)
{
struct inv_icm42600_state *st = _data;
struct device *dev = regmap_get_device(st->map);
if (pm_runtime_status_suspended(dev))
return;
regulator_disable(st->vddio_supply);
}
int inv_icm42600_core_probe(struct regmap *regmap, int chip,
inv_icm42600_bus_setup bus_setup)
{
struct device *dev = regmap_get_device(regmap);
struct fwnode_handle *fwnode = dev_fwnode(dev);
struct inv_icm42600_state *st;
int irq, irq_type;
bool open_drain;
int ret;
if (chip <= INV_CHIP_INVALID || chip >= INV_CHIP_NB) {
dev_err(dev, "invalid chip = %d\n", chip);
return -ENODEV;
}
/* get INT1 only supported interrupt or fallback to first interrupt */
irq = fwnode_irq_get_byname(fwnode, "INT1");
if (irq < 0 && irq != -EPROBE_DEFER) {
dev_info(dev, "no INT1 interrupt defined, fallback to first interrupt\n");
irq = fwnode_irq_get(fwnode, 0);
}
if (irq < 0)
return dev_err_probe(dev, irq, "error missing INT1 interrupt\n");
irq_type = irq_get_trigger_type(irq);
if (!irq_type)
irq_type = IRQF_TRIGGER_FALLING;
open_drain = device_property_read_bool(dev, "drive-open-drain");
st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
if (!st)
return -ENOMEM;
dev_set_drvdata(dev, st);
mutex_init(&st->lock);
st->chip = chip;
st->map = regmap;
st->irq = irq;
ret = iio_read_mount_matrix(dev, &st->orientation);
if (ret) {
dev_err(dev, "failed to retrieve mounting matrix %d\n", ret);
return ret;
}
ret = devm_regulator_get_enable(dev, "vdd");
if (ret)
return dev_err_probe(dev, ret,
"Failed to get vdd regulator\n");
msleep(INV_ICM42600_POWER_UP_TIME_MS);
st->vddio_supply = devm_regulator_get(dev, "vddio");
if (IS_ERR(st->vddio_supply))
return PTR_ERR(st->vddio_supply);
ret = inv_icm42600_enable_regulator_vddio(st);
if (ret)
return ret;
ret = devm_add_action_or_reset(dev, inv_icm42600_disable_vddio_reg, st);
if (ret)
return ret;
/* setup chip registers */
ret = inv_icm42600_setup(st, bus_setup);
if (ret)
return ret;
ret = inv_icm42600_timestamp_setup(st);
if (ret)
return ret;
ret = inv_icm42600_buffer_init(st);
if (ret)
return ret;
st->indio_gyro = inv_icm42600_gyro_init(st);
if (IS_ERR(st->indio_gyro))
return PTR_ERR(st->indio_gyro);
st->indio_accel = inv_icm42600_accel_init(st);
if (IS_ERR(st->indio_accel))
return PTR_ERR(st->indio_accel);
ret = inv_icm42600_irq_init(st, irq, irq_type, open_drain);
if (ret)
return ret;
/* setup runtime power management */
ret = devm_pm_runtime_set_active_enabled(dev);
if (ret)
return ret;
pm_runtime_set_autosuspend_delay(dev, INV_ICM42600_SUSPEND_DELAY_MS);
pm_runtime_use_autosuspend(dev);
return ret;
}
EXPORT_SYMBOL_NS_GPL(inv_icm42600_core_probe, "IIO_ICM42600");
/*
* Suspend saves sensors state and turns everything off.
* Check first if runtime suspend has not already done the job.
*/
static int inv_icm42600_suspend(struct device *dev)
{
struct inv_icm42600_state *st = dev_get_drvdata(dev);
struct device *accel_dev;
bool wakeup;
int accel_conf;
int ret;
guard(mutex)(&st->lock);
st->suspended.gyro = st->conf.gyro.mode;
st->suspended.accel = st->conf.accel.mode;
st->suspended.temp = st->conf.temp_en;
if (pm_runtime_suspended(dev))
return 0;
/* 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)
return ret;
}
/* keep chip on and wake-up capable if APEX and wakeup on */
accel_dev = &st->indio_accel->dev;
wakeup = st->apex.on && device_may_wakeup(accel_dev);
if (wakeup) {
/* keep accel on and setup irq for wakeup */
accel_conf = st->conf.accel.mode;
enable_irq_wake(st->irq);
disable_irq(st->irq);
} else {
/* disable APEX features and accel if wakeup disabled */
if (st->apex.wom.enable) {
ret = inv_icm42600_disable_wom(st);
if (ret)
return ret;
}
accel_conf = INV_ICM42600_SENSOR_MODE_OFF;
}
ret = inv_icm42600_set_pwr_mgmt0(st, INV_ICM42600_SENSOR_MODE_OFF,
accel_conf, false, NULL);
if (ret)
return ret;
/* disable vddio regulator if chip is sleeping */
if (!wakeup)
regulator_disable(st->vddio_supply);
return 0;
}
/*
* System resume gets the system back on and restores the sensors state.
* Manually put runtime power management in system active state.
*/
static int inv_icm42600_resume(struct device *dev)
{
struct inv_icm42600_state *st = dev_get_drvdata(dev);
struct inv_icm42600_sensor_state *gyro_st = iio_priv(st->indio_gyro);
struct inv_icm42600_sensor_state *accel_st = iio_priv(st->indio_accel);
struct device *accel_dev;
bool wakeup;
int ret;
guard(mutex)(&st->lock);
if (pm_runtime_suspended(dev))
return 0;
/* check wakeup capability */
accel_dev = &st->indio_accel->dev;
wakeup = st->apex.on && device_may_wakeup(accel_dev);
/* restore irq state or vddio if cut off */
if (wakeup) {
enable_irq(st->irq);
disable_irq_wake(st->irq);
} else {
ret = inv_icm42600_enable_regulator_vddio(st);
if (ret)
return ret;
}
/* restore sensors state */
ret = inv_icm42600_set_pwr_mgmt0(st, st->suspended.gyro,
st->suspended.accel,
st->suspended.temp, NULL);
if (ret)
return ret;
/* restore APEX features if disabled */
if (!wakeup && st->apex.wom.enable) {
ret = inv_icm42600_enable_wom(st);
if (ret)
return ret;
}
/* restore FIFO data streaming */
if (st->fifo.on) {
inv_sensors_timestamp_reset(&gyro_st->ts);
inv_sensors_timestamp_reset(&accel_st->ts);
ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG,
INV_ICM42600_FIFO_CONFIG_STREAM);
}
return 0;
}
/* Runtime suspend will turn off sensors that are enabled by iio devices. */
static int inv_icm42600_runtime_suspend(struct device *dev)
{
struct inv_icm42600_state *st = dev_get_drvdata(dev);
int ret;
guard(mutex)(&st->lock);
/* disable all sensors */
ret = inv_icm42600_set_pwr_mgmt0(st, INV_ICM42600_SENSOR_MODE_OFF,
INV_ICM42600_SENSOR_MODE_OFF, false,
NULL);
if (ret)
return ret;
regulator_disable(st->vddio_supply);
return 0;
}
/* Sensors are enabled by iio devices, no need to turn them back on here. */
static int inv_icm42600_runtime_resume(struct device *dev)
{
struct inv_icm42600_state *st = dev_get_drvdata(dev);
guard(mutex)(&st->lock);
return inv_icm42600_enable_regulator_vddio(st);
}
EXPORT_NS_GPL_DEV_PM_OPS(inv_icm42600_pm_ops, IIO_ICM42600) = {
SYSTEM_SLEEP_PM_OPS(inv_icm42600_suspend, inv_icm42600_resume)
RUNTIME_PM_OPS(inv_icm42600_runtime_suspend,
inv_icm42600_runtime_resume, NULL)
};
MODULE_AUTHOR("InvenSense, Inc.");
MODULE_DESCRIPTION("InvenSense ICM-426xx device driver");
MODULE_LICENSE("GPL");
MODULE_IMPORT_NS("IIO_INV_SENSORS_TIMESTAMP");
@@ -0,0 +1,826 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2020 Invensense, Inc.
*/
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/mutex.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/delay.h>
#include <linux/math64.h>
#include <linux/iio/buffer.h>
#include <linux/iio/common/inv_sensors_timestamp.h>
#include <linux/iio/iio.h>
#include <linux/iio/kfifo_buf.h>
#include "inv_icm42600.h"
#include "inv_icm42600_temp.h"
#include "inv_icm42600_buffer.h"
#define INV_ICM42600_GYRO_CHAN(_modifier, _index, _ext_info) \
{ \
.type = IIO_ANGL_VEL, \
.modified = 1, \
.channel2 = _modifier, \
.info_mask_separate = \
BIT(IIO_CHAN_INFO_RAW) | \
BIT(IIO_CHAN_INFO_CALIBBIAS), \
.info_mask_shared_by_type = \
BIT(IIO_CHAN_INFO_SCALE), \
.info_mask_shared_by_type_available = \
BIT(IIO_CHAN_INFO_SCALE) | \
BIT(IIO_CHAN_INFO_CALIBBIAS), \
.info_mask_shared_by_all = \
BIT(IIO_CHAN_INFO_SAMP_FREQ), \
.info_mask_shared_by_all_available = \
BIT(IIO_CHAN_INFO_SAMP_FREQ), \
.scan_index = _index, \
.scan_type = { \
.sign = 's', \
.realbits = 16, \
.storagebits = 16, \
.endianness = IIO_BE, \
}, \
.ext_info = _ext_info, \
}
enum inv_icm42600_gyro_scan {
INV_ICM42600_GYRO_SCAN_X,
INV_ICM42600_GYRO_SCAN_Y,
INV_ICM42600_GYRO_SCAN_Z,
INV_ICM42600_GYRO_SCAN_TEMP,
INV_ICM42600_GYRO_SCAN_TIMESTAMP,
};
static const struct iio_chan_spec_ext_info inv_icm42600_gyro_ext_infos[] = {
IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, inv_icm42600_get_mount_matrix),
{ }
};
static const struct iio_chan_spec inv_icm42600_gyro_channels[] = {
INV_ICM42600_GYRO_CHAN(IIO_MOD_X, INV_ICM42600_GYRO_SCAN_X,
inv_icm42600_gyro_ext_infos),
INV_ICM42600_GYRO_CHAN(IIO_MOD_Y, INV_ICM42600_GYRO_SCAN_Y,
inv_icm42600_gyro_ext_infos),
INV_ICM42600_GYRO_CHAN(IIO_MOD_Z, INV_ICM42600_GYRO_SCAN_Z,
inv_icm42600_gyro_ext_infos),
INV_ICM42600_TEMP_CHAN(INV_ICM42600_GYRO_SCAN_TEMP),
IIO_CHAN_SOFT_TIMESTAMP(INV_ICM42600_GYRO_SCAN_TIMESTAMP),
};
/*
* IIO buffer data: size must be a power of 2 and timestamp aligned
* 16 bytes: 6 bytes angular velocity, 2 bytes temperature, 8 bytes timestamp
*/
struct inv_icm42600_gyro_buffer {
struct inv_icm42600_fifo_sensor_data gyro;
s16 temp;
aligned_s64 timestamp;
};
#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);
out_unlock:
mutex_unlock(&st->lock);
/* sleep maximum required time */
sleep = max(sleep_gyro, sleep_temp);
if (sleep)
msleep(sleep);
return ret;
}
static int inv_icm42600_gyro_read_sensor(struct inv_icm42600_state *st,
struct iio_chan_spec const *chan,
s16 *val)
{
struct device *dev = regmap_get_device(st->map);
struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
unsigned int reg;
__be16 *data;
int ret;
if (chan->type != IIO_ANGL_VEL)
return -EINVAL;
switch (chan->channel2) {
case IIO_MOD_X:
reg = INV_ICM42600_REG_GYRO_DATA_X;
break;
case IIO_MOD_Y:
reg = INV_ICM42600_REG_GYRO_DATA_Y;
break;
case IIO_MOD_Z:
reg = INV_ICM42600_REG_GYRO_DATA_Z;
break;
default:
return -EINVAL;
}
pm_runtime_get_sync(dev);
mutex_lock(&st->lock);
/* enable gyro sensor */
conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
if (ret)
goto exit;
/* read gyro register data */
data = (__be16 *)&st->buffer[0];
ret = regmap_bulk_read(st->map, reg, data, sizeof(*data));
if (ret)
goto exit;
*val = (s16)be16_to_cpup(data);
if (*val == INV_ICM42600_DATA_INVALID)
ret = -EINVAL;
exit:
mutex_unlock(&st->lock);
pm_runtime_put_autosuspend(dev);
return ret;
}
/* IIO format int + nano */
static const int inv_icm42600_gyro_scale[] = {
/* +/- 2000dps => 0.001065264 rad/s */
[2 * INV_ICM42600_GYRO_FS_2000DPS] = 0,
[2 * INV_ICM42600_GYRO_FS_2000DPS + 1] = 1065264,
/* +/- 1000dps => 0.000532632 rad/s */
[2 * INV_ICM42600_GYRO_FS_1000DPS] = 0,
[2 * INV_ICM42600_GYRO_FS_1000DPS + 1] = 532632,
/* +/- 500dps => 0.000266316 rad/s */
[2 * INV_ICM42600_GYRO_FS_500DPS] = 0,
[2 * INV_ICM42600_GYRO_FS_500DPS + 1] = 266316,
/* +/- 250dps => 0.000133158 rad/s */
[2 * INV_ICM42600_GYRO_FS_250DPS] = 0,
[2 * INV_ICM42600_GYRO_FS_250DPS + 1] = 133158,
/* +/- 125dps => 0.000066579 rad/s */
[2 * INV_ICM42600_GYRO_FS_125DPS] = 0,
[2 * INV_ICM42600_GYRO_FS_125DPS + 1] = 66579,
/* +/- 62.5dps => 0.000033290 rad/s */
[2 * INV_ICM42600_GYRO_FS_62_5DPS] = 0,
[2 * INV_ICM42600_GYRO_FS_62_5DPS + 1] = 33290,
/* +/- 31.25dps => 0.000016645 rad/s */
[2 * INV_ICM42600_GYRO_FS_31_25DPS] = 0,
[2 * INV_ICM42600_GYRO_FS_31_25DPS + 1] = 16645,
/* +/- 15.625dps => 0.000008322 rad/s */
[2 * INV_ICM42600_GYRO_FS_15_625DPS] = 0,
[2 * INV_ICM42600_GYRO_FS_15_625DPS + 1] = 8322,
};
static const int inv_icm42686_gyro_scale[] = {
/* +/- 4000dps => 0.002130529 rad/s */
[2 * INV_ICM42686_GYRO_FS_4000DPS] = 0,
[2 * INV_ICM42686_GYRO_FS_4000DPS + 1] = 2130529,
/* +/- 2000dps => 0.001065264 rad/s */
[2 * INV_ICM42686_GYRO_FS_2000DPS] = 0,
[2 * INV_ICM42686_GYRO_FS_2000DPS + 1] = 1065264,
/* +/- 1000dps => 0.000532632 rad/s */
[2 * INV_ICM42686_GYRO_FS_1000DPS] = 0,
[2 * INV_ICM42686_GYRO_FS_1000DPS + 1] = 532632,
/* +/- 500dps => 0.000266316 rad/s */
[2 * INV_ICM42686_GYRO_FS_500DPS] = 0,
[2 * INV_ICM42686_GYRO_FS_500DPS + 1] = 266316,
/* +/- 250dps => 0.000133158 rad/s */
[2 * INV_ICM42686_GYRO_FS_250DPS] = 0,
[2 * INV_ICM42686_GYRO_FS_250DPS + 1] = 133158,
/* +/- 125dps => 0.000066579 rad/s */
[2 * INV_ICM42686_GYRO_FS_125DPS] = 0,
[2 * INV_ICM42686_GYRO_FS_125DPS + 1] = 66579,
/* +/- 62.5dps => 0.000033290 rad/s */
[2 * INV_ICM42686_GYRO_FS_62_5DPS] = 0,
[2 * INV_ICM42686_GYRO_FS_62_5DPS + 1] = 33290,
/* +/- 31.25dps => 0.000016645 rad/s */
[2 * INV_ICM42686_GYRO_FS_31_25DPS] = 0,
[2 * INV_ICM42686_GYRO_FS_31_25DPS + 1] = 16645,
};
static int inv_icm42600_gyro_read_scale(struct iio_dev *indio_dev,
int *val, int *val2)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
unsigned int idx;
idx = st->conf.gyro.fs;
*val = gyro_st->scales[2 * idx];
*val2 = gyro_st->scales[2 * idx + 1];
return IIO_VAL_INT_PLUS_NANO;
}
static int inv_icm42600_gyro_write_scale(struct iio_dev *indio_dev,
int val, int val2)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
struct device *dev = regmap_get_device(st->map);
unsigned int idx;
struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
int ret;
for (idx = 0; idx < gyro_st->scales_len; idx += 2) {
if (val == gyro_st->scales[idx] &&
val2 == gyro_st->scales[idx + 1])
break;
}
if (idx >= gyro_st->scales_len)
return -EINVAL;
conf.fs = idx / 2;
pm_runtime_get_sync(dev);
scoped_guard(mutex, &st->lock)
ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
pm_runtime_put_autosuspend(dev);
return ret;
}
/* IIO format int + micro */
static const int inv_icm42600_gyro_odr[] = {
/* 12.5Hz */
12, 500000,
/* 25Hz */
25, 0,
/* 50Hz */
50, 0,
/* 100Hz */
100, 0,
/* 200Hz */
200, 0,
/* 1kHz */
1000, 0,
/* 2kHz */
2000, 0,
/* 4kHz */
4000, 0,
};
static const int inv_icm42600_gyro_odr_conv[] = {
INV_ICM42600_ODR_12_5HZ,
INV_ICM42600_ODR_25HZ,
INV_ICM42600_ODR_50HZ,
INV_ICM42600_ODR_100HZ,
INV_ICM42600_ODR_200HZ,
INV_ICM42600_ODR_1KHZ_LN,
INV_ICM42600_ODR_2KHZ_LN,
INV_ICM42600_ODR_4KHZ_LN,
};
static int inv_icm42600_gyro_read_odr(struct inv_icm42600_state *st,
int *val, int *val2)
{
unsigned int odr;
unsigned int i;
odr = st->conf.gyro.odr;
for (i = 0; i < ARRAY_SIZE(inv_icm42600_gyro_odr_conv); ++i) {
if (inv_icm42600_gyro_odr_conv[i] == odr)
break;
}
if (i >= ARRAY_SIZE(inv_icm42600_gyro_odr_conv))
return -EINVAL;
*val = inv_icm42600_gyro_odr[2 * i];
*val2 = inv_icm42600_gyro_odr[2 * i + 1];
return IIO_VAL_INT_PLUS_MICRO;
}
static int inv_icm42600_gyro_write_odr(struct iio_dev *indio_dev,
int val, int val2)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
struct inv_sensors_timestamp *ts = &gyro_st->ts;
struct device *dev = regmap_get_device(st->map);
unsigned int idx;
struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
int ret;
for (idx = 0; idx < ARRAY_SIZE(inv_icm42600_gyro_odr); idx += 2) {
if (val == inv_icm42600_gyro_odr[idx] &&
val2 == inv_icm42600_gyro_odr[idx + 1])
break;
}
if (idx >= ARRAY_SIZE(inv_icm42600_gyro_odr))
return -EINVAL;
conf.odr = inv_icm42600_gyro_odr_conv[idx / 2];
if (conf.odr == st->conf.gyro.odr)
return 0;
pm_runtime_get_sync(dev);
mutex_lock(&st->lock);
ret = inv_sensors_timestamp_update_odr(ts, inv_icm42600_odr_to_period(conf.odr),
iio_buffer_enabled(indio_dev));
if (ret)
goto out_unlock;
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);
pm_runtime_put_autosuspend(dev);
return ret;
}
/*
* Calibration bias values, IIO range format int + nano.
* Value is limited to +/-64dps coded on 12 bits signed. Step is 1/32 dps.
*/
static int inv_icm42600_gyro_calibbias[] = {
-1, 117010721, /* min: -1.117010721 rad/s */
0, 545415, /* step: 0.000545415 rad/s */
1, 116465306, /* max: 1.116465306 rad/s */
};
static int inv_icm42600_gyro_read_offset(struct inv_icm42600_state *st,
struct iio_chan_spec const *chan,
int *val, int *val2)
{
struct device *dev = regmap_get_device(st->map);
s64 val64;
s32 bias;
unsigned int reg;
s16 offset;
u8 data[2];
int ret;
if (chan->type != IIO_ANGL_VEL)
return -EINVAL;
switch (chan->channel2) {
case IIO_MOD_X:
reg = INV_ICM42600_REG_OFFSET_USER0;
break;
case IIO_MOD_Y:
reg = INV_ICM42600_REG_OFFSET_USER1;
break;
case IIO_MOD_Z:
reg = INV_ICM42600_REG_OFFSET_USER3;
break;
default:
return -EINVAL;
}
pm_runtime_get_sync(dev);
mutex_lock(&st->lock);
ret = regmap_bulk_read(st->map, reg, st->buffer, sizeof(data));
memcpy(data, st->buffer, sizeof(data));
mutex_unlock(&st->lock);
pm_runtime_put_autosuspend(dev);
if (ret)
return ret;
/* 12 bits signed value */
switch (chan->channel2) {
case IIO_MOD_X:
offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
break;
case IIO_MOD_Y:
offset = sign_extend32(((data[0] & 0xF0) << 4) | data[1], 11);
break;
case IIO_MOD_Z:
offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
break;
default:
return -EINVAL;
}
/*
* convert raw offset to dps then to rad/s
* 12 bits signed raw max 64 to dps: 64 / 2048
* dps to rad: Pi / 180
* result in nano (1000000000)
* (offset * 64 * Pi * 1000000000) / (2048 * 180)
*/
val64 = (s64)offset * 64LL * 3141592653LL;
/* for rounding, add + or - divisor (2048 * 180) divided by 2 */
if (val64 >= 0)
val64 += 2048 * 180 / 2;
else
val64 -= 2048 * 180 / 2;
bias = div_s64(val64, 2048 * 180);
*val = bias / 1000000000L;
*val2 = bias % 1000000000L;
return IIO_VAL_INT_PLUS_NANO;
}
static int inv_icm42600_gyro_write_offset(struct inv_icm42600_state *st,
struct iio_chan_spec const *chan,
int val, int val2)
{
struct device *dev = regmap_get_device(st->map);
s64 val64, min, max;
unsigned int reg, regval;
s16 offset;
int ret;
if (chan->type != IIO_ANGL_VEL)
return -EINVAL;
switch (chan->channel2) {
case IIO_MOD_X:
reg = INV_ICM42600_REG_OFFSET_USER0;
break;
case IIO_MOD_Y:
reg = INV_ICM42600_REG_OFFSET_USER1;
break;
case IIO_MOD_Z:
reg = INV_ICM42600_REG_OFFSET_USER3;
break;
default:
return -EINVAL;
}
/* inv_icm42600_gyro_calibbias: min - step - max in nano */
min = (s64)inv_icm42600_gyro_calibbias[0] * 1000000000LL +
(s64)inv_icm42600_gyro_calibbias[1];
max = (s64)inv_icm42600_gyro_calibbias[4] * 1000000000LL +
(s64)inv_icm42600_gyro_calibbias[5];
val64 = (s64)val * 1000000000LL + (s64)val2;
if (val64 < min || val64 > max)
return -EINVAL;
/*
* convert rad/s to dps then to raw value
* rad to dps: 180 / Pi
* dps to raw 12 bits signed, max 64: 2048 / 64
* val in nano (1000000000)
* val * 180 * 2048 / (Pi * 1000000000 * 64)
*/
val64 = val64 * 180LL * 2048LL;
/* for rounding, add + or - divisor (3141592653 * 64) divided by 2 */
if (val64 >= 0)
val64 += 3141592653LL * 64LL / 2LL;
else
val64 -= 3141592653LL * 64LL / 2LL;
offset = div64_s64(val64, 3141592653LL * 64LL);
/* clamp value limited to 12 bits signed */
if (offset < -2048)
offset = -2048;
else if (offset > 2047)
offset = 2047;
pm_runtime_get_sync(dev);
mutex_lock(&st->lock);
switch (chan->channel2) {
case IIO_MOD_X:
/* OFFSET_USER1 register is shared */
ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1,
&regval);
if (ret)
goto out_unlock;
st->buffer[0] = offset & 0xFF;
st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8);
break;
case IIO_MOD_Y:
/* OFFSET_USER1 register is shared */
ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1,
&regval);
if (ret)
goto out_unlock;
st->buffer[0] = ((offset & 0xF00) >> 4) | (regval & 0x0F);
st->buffer[1] = offset & 0xFF;
break;
case IIO_MOD_Z:
/* OFFSET_USER4 register is shared */
ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER4,
&regval);
if (ret)
goto out_unlock;
st->buffer[0] = offset & 0xFF;
st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8);
break;
default:
ret = -EINVAL;
goto out_unlock;
}
ret = regmap_bulk_write(st->map, reg, st->buffer, 2);
out_unlock:
mutex_unlock(&st->lock);
pm_runtime_put_autosuspend(dev);
return ret;
}
static int inv_icm42600_gyro_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int *val, int *val2, long mask)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
s16 data;
int ret;
switch (chan->type) {
case IIO_ANGL_VEL:
break;
case IIO_TEMP:
return inv_icm42600_temp_read_raw(indio_dev, chan, val, val2, mask);
default:
return -EINVAL;
}
switch (mask) {
case IIO_CHAN_INFO_RAW:
if (!iio_device_claim_direct(indio_dev))
return -EBUSY;
ret = inv_icm42600_gyro_read_sensor(st, chan, &data);
iio_device_release_direct(indio_dev);
if (ret)
return ret;
*val = data;
return IIO_VAL_INT;
case IIO_CHAN_INFO_SCALE:
return inv_icm42600_gyro_read_scale(indio_dev, val, val2);
case IIO_CHAN_INFO_SAMP_FREQ:
return inv_icm42600_gyro_read_odr(st, val, val2);
case IIO_CHAN_INFO_CALIBBIAS:
return inv_icm42600_gyro_read_offset(st, chan, val, val2);
default:
return -EINVAL;
}
}
static int inv_icm42600_gyro_read_avail(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
const int **vals,
int *type, int *length, long mask)
{
struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
if (chan->type != IIO_ANGL_VEL)
return -EINVAL;
switch (mask) {
case IIO_CHAN_INFO_SCALE:
*vals = gyro_st->scales;
*type = IIO_VAL_INT_PLUS_NANO;
*length = gyro_st->scales_len;
return IIO_AVAIL_LIST;
case IIO_CHAN_INFO_SAMP_FREQ:
*vals = inv_icm42600_gyro_odr;
*type = IIO_VAL_INT_PLUS_MICRO;
*length = ARRAY_SIZE(inv_icm42600_gyro_odr);
return IIO_AVAIL_LIST;
case IIO_CHAN_INFO_CALIBBIAS:
*vals = inv_icm42600_gyro_calibbias;
*type = IIO_VAL_INT_PLUS_NANO;
return IIO_AVAIL_RANGE;
default:
return -EINVAL;
}
}
static int inv_icm42600_gyro_write_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int val, int val2, long mask)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
int ret;
if (chan->type != IIO_ANGL_VEL)
return -EINVAL;
switch (mask) {
case IIO_CHAN_INFO_SCALE:
if (!iio_device_claim_direct(indio_dev))
return -EBUSY;
ret = inv_icm42600_gyro_write_scale(indio_dev, val, val2);
iio_device_release_direct(indio_dev);
return ret;
case IIO_CHAN_INFO_SAMP_FREQ:
return inv_icm42600_gyro_write_odr(indio_dev, val, val2);
case IIO_CHAN_INFO_CALIBBIAS:
if (!iio_device_claim_direct(indio_dev))
return -EBUSY;
ret = inv_icm42600_gyro_write_offset(st, chan, val, val2);
iio_device_release_direct(indio_dev);
return ret;
default:
return -EINVAL;
}
}
static int inv_icm42600_gyro_write_raw_get_fmt(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
long mask)
{
if (chan->type != IIO_ANGL_VEL)
return -EINVAL;
switch (mask) {
case IIO_CHAN_INFO_SCALE:
return IIO_VAL_INT_PLUS_NANO;
case IIO_CHAN_INFO_SAMP_FREQ:
return IIO_VAL_INT_PLUS_MICRO;
case IIO_CHAN_INFO_CALIBBIAS:
return IIO_VAL_INT_PLUS_NANO;
default:
return -EINVAL;
}
}
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);
guard(mutex)(&st->lock);
st->fifo.watermark.gyro = val;
return inv_icm42600_buffer_update_watermark(st);
}
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;
guard(mutex)(&st->lock);
ret = inv_icm42600_buffer_hwfifo_flush(st, count);
if (ret)
return ret;
return st->fifo.nb.gyro;
}
static const struct iio_info inv_icm42600_gyro_info = {
.read_raw = inv_icm42600_gyro_read_raw,
.read_avail = inv_icm42600_gyro_read_avail,
.write_raw = inv_icm42600_gyro_write_raw,
.write_raw_get_fmt = inv_icm42600_gyro_write_raw_get_fmt,
.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 device *dev = regmap_get_device(st->map);
const char *name;
struct inv_icm42600_sensor_state *gyro_st;
struct inv_sensors_timestamp_chip ts_chip;
struct iio_dev *indio_dev;
int ret;
name = devm_kasprintf(dev, GFP_KERNEL, "%s-gyro", st->name);
if (!name)
return ERR_PTR(-ENOMEM);
indio_dev = devm_iio_device_alloc(dev, sizeof(*gyro_st));
if (!indio_dev)
return ERR_PTR(-ENOMEM);
gyro_st = iio_priv(indio_dev);
switch (st->chip) {
case INV_CHIP_ICM42686:
gyro_st->scales = inv_icm42686_gyro_scale;
gyro_st->scales_len = ARRAY_SIZE(inv_icm42686_gyro_scale);
break;
default:
gyro_st->scales = inv_icm42600_gyro_scale;
gyro_st->scales_len = ARRAY_SIZE(inv_icm42600_gyro_scale);
break;
}
/*
* clock period is 8kHz (125000ns)
* jitter is +/- 2% (20 per mille)
*/
ts_chip.clock_period = 125000;
ts_chip.jitter = 20;
ts_chip.init_period = inv_icm42600_odr_to_period(st->conf.accel.odr);
inv_sensors_timestamp_init(&gyro_st->ts, &ts_chip);
iio_device_set_drvdata(indio_dev, st);
indio_dev->name = name;
indio_dev->info = &inv_icm42600_gyro_info;
indio_dev->modes = INDIO_DIRECT_MODE;
indio_dev->channels = 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;
ret = devm_iio_kfifo_buffer_setup(dev, indio_dev,
&inv_icm42600_buffer_ops);
if (ret)
return ERR_PTR(ret);
ret = devm_iio_device_register(dev, indio_dev);
if (ret)
return ERR_PTR(ret);
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);
struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
struct inv_sensors_timestamp *ts = &gyro_st->ts;
ssize_t i, size;
unsigned int no;
const void *accel, *gyro, *timestamp;
const s8 *temp;
unsigned int odr;
s64 ts_val;
/* buffer is copied to userspace, zeroing it to avoid any data leak */
struct inv_icm42600_gyro_buffer buffer = { };
/* parse all fifo packets */
for (i = 0, no = 0; i < st->fifo.count; i += size, ++no) {
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;
/* update odr */
if (odr & INV_ICM42600_SENSOR_GYRO)
inv_sensors_timestamp_apply_odr(ts, st->fifo.period,
st->fifo.nb.total, no);
memcpy(&buffer.gyro, gyro, sizeof(buffer.gyro));
/* convert 8 bits FIFO temperature in high resolution format */
buffer.temp = temp ? (*temp * 64) : 0;
ts_val = inv_sensors_timestamp_pop(ts);
iio_push_to_buffers_with_timestamp(indio_dev, &buffer, ts_val);
}
return 0;
}
@@ -0,0 +1,129 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2020 InvenSense, Inc.
*/
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/regmap.h>
#include <linux/property.h>
#include "inv_icm42600.h"
static int inv_icm42600_i2c_bus_setup(struct inv_icm42600_state *st)
{
unsigned int mask, val;
int ret;
/*
* setup interface registers
* This register write to REG_INTF_CONFIG6 enables a spike filter that
* is impacting the line and can prevent the I2C ACK to be seen by the
* controller. So we don't test the return value.
*/
regmap_update_bits(st->map, INV_ICM42600_REG_INTF_CONFIG6,
INV_ICM42600_INTF_CONFIG6_MASK,
INV_ICM42600_INTF_CONFIG6_I3C_EN);
ret = regmap_clear_bits(st->map, INV_ICM42600_REG_INTF_CONFIG4,
INV_ICM42600_INTF_CONFIG4_I3C_BUS_ONLY);
if (ret)
return ret;
/* set slew rates for I2C and SPI */
mask = INV_ICM42600_DRIVE_CONFIG_I2C_MASK |
INV_ICM42600_DRIVE_CONFIG_SPI_MASK;
val = INV_ICM42600_DRIVE_CONFIG_I2C(INV_ICM42600_SLEW_RATE_12_36NS) |
INV_ICM42600_DRIVE_CONFIG_SPI(INV_ICM42600_SLEW_RATE_12_36NS);
ret = regmap_update_bits(st->map, INV_ICM42600_REG_DRIVE_CONFIG,
mask, val);
if (ret)
return ret;
/* disable SPI bus */
return regmap_update_bits(st->map, INV_ICM42600_REG_INTF_CONFIG0,
INV_ICM42600_INTF_CONFIG0_UI_SIFS_CFG_MASK,
INV_ICM42600_INTF_CONFIG0_UI_SIFS_CFG_SPI_DIS);
}
static int inv_icm42600_probe(struct i2c_client *client)
{
const void *match;
enum inv_icm42600_chip chip;
struct regmap *regmap;
if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
return -ENOTSUPP;
match = device_get_match_data(&client->dev);
if (!match)
return -EINVAL;
chip = (uintptr_t)match;
regmap = devm_regmap_init_i2c(client, &inv_icm42600_regmap_config);
if (IS_ERR(regmap))
return PTR_ERR(regmap);
return inv_icm42600_core_probe(regmap, chip, inv_icm42600_i2c_bus_setup);
}
/*
* device id table is used to identify what device can be
* supported by this driver
*/
static const struct i2c_device_id inv_icm42600_id[] = {
{ .name = "icm42600", .driver_data = INV_CHIP_ICM42600 },
{ .name = "icm42602", .driver_data = INV_CHIP_ICM42602 },
{ .name = "icm42605", .driver_data = INV_CHIP_ICM42605 },
{ .name = "icm42686", .driver_data = INV_CHIP_ICM42686 },
{ .name = "icm42622", .driver_data = INV_CHIP_ICM42622 },
{ .name = "icm42688", .driver_data = INV_CHIP_ICM42688 },
{ .name = "icm42631", .driver_data = INV_CHIP_ICM42631 },
{ }
};
MODULE_DEVICE_TABLE(i2c, inv_icm42600_id);
static const struct of_device_id inv_icm42600_of_matches[] = {
{
.compatible = "invensense,icm42600",
.data = (void *)INV_CHIP_ICM42600,
}, {
.compatible = "invensense,icm42602",
.data = (void *)INV_CHIP_ICM42602,
}, {
.compatible = "invensense,icm42605",
.data = (void *)INV_CHIP_ICM42605,
}, {
.compatible = "invensense,icm42686",
.data = (void *)INV_CHIP_ICM42686,
}, {
.compatible = "invensense,icm42622",
.data = (void *)INV_CHIP_ICM42622,
}, {
.compatible = "invensense,icm42688",
.data = (void *)INV_CHIP_ICM42688,
}, {
.compatible = "invensense,icm42631",
.data = (void *)INV_CHIP_ICM42631,
},
{ }
};
MODULE_DEVICE_TABLE(of, inv_icm42600_of_matches);
static struct i2c_driver inv_icm42600_driver = {
.driver = {
.name = "inv-icm42600-i2c",
.of_match_table = inv_icm42600_of_matches,
.pm = pm_ptr(&inv_icm42600_pm_ops),
},
.id_table = inv_icm42600_id,
.probe = inv_icm42600_probe,
};
module_i2c_driver(inv_icm42600_driver);
MODULE_AUTHOR("InvenSense, Inc.");
MODULE_DESCRIPTION("InvenSense ICM-426xx I2C driver");
MODULE_LICENSE("GPL");
MODULE_IMPORT_NS("IIO_ICM42600");
@@ -0,0 +1,126 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2020 InvenSense, Inc.
*/
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/spi/spi.h>
#include <linux/regmap.h>
#include <linux/property.h>
#include "inv_icm42600.h"
static int inv_icm42600_spi_bus_setup(struct inv_icm42600_state *st)
{
unsigned int mask, val;
int ret;
/* setup interface registers */
val = INV_ICM42600_INTF_CONFIG6_I3C_EN |
INV_ICM42600_INTF_CONFIG6_I3C_SDR_EN |
INV_ICM42600_INTF_CONFIG6_I3C_DDR_EN;
ret = regmap_update_bits(st->map, INV_ICM42600_REG_INTF_CONFIG6,
INV_ICM42600_INTF_CONFIG6_MASK, val);
if (ret)
return ret;
ret = regmap_clear_bits(st->map, INV_ICM42600_REG_INTF_CONFIG4,
INV_ICM42600_INTF_CONFIG4_I3C_BUS_ONLY);
if (ret)
return ret;
/* set slew rates for I2C and SPI */
mask = INV_ICM42600_DRIVE_CONFIG_I2C_MASK |
INV_ICM42600_DRIVE_CONFIG_SPI_MASK;
val = INV_ICM42600_DRIVE_CONFIG_I2C(INV_ICM42600_SLEW_RATE_20_60NS) |
INV_ICM42600_DRIVE_CONFIG_SPI(INV_ICM42600_SLEW_RATE_INF_2NS);
ret = regmap_update_bits(st->map, INV_ICM42600_REG_DRIVE_CONFIG,
mask, val);
if (ret)
return ret;
/* disable i2c bus */
return regmap_update_bits(st->map, INV_ICM42600_REG_INTF_CONFIG0,
INV_ICM42600_INTF_CONFIG0_UI_SIFS_CFG_MASK,
INV_ICM42600_INTF_CONFIG0_UI_SIFS_CFG_I2C_DIS);
}
static int inv_icm42600_probe(struct spi_device *spi)
{
const void *match;
enum inv_icm42600_chip chip;
struct regmap *regmap;
match = device_get_match_data(&spi->dev);
if (!match)
return -EINVAL;
chip = (uintptr_t)match;
/* use SPI specific regmap */
regmap = devm_regmap_init_spi(spi, &inv_icm42600_spi_regmap_config);
if (IS_ERR(regmap))
return PTR_ERR(regmap);
return inv_icm42600_core_probe(regmap, chip, inv_icm42600_spi_bus_setup);
}
/*
* device id table is used to identify what device can be
* supported by this driver
*/
static const struct spi_device_id inv_icm42600_id[] = {
{ "icm42600", INV_CHIP_ICM42600 },
{ "icm42602", INV_CHIP_ICM42602 },
{ "icm42605", INV_CHIP_ICM42605 },
{ "icm42686", INV_CHIP_ICM42686 },
{ "icm42622", INV_CHIP_ICM42622 },
{ "icm42688", INV_CHIP_ICM42688 },
{ "icm42631", INV_CHIP_ICM42631 },
{ }
};
MODULE_DEVICE_TABLE(spi, inv_icm42600_id);
static const struct of_device_id inv_icm42600_of_matches[] = {
{
.compatible = "invensense,icm42600",
.data = (void *)INV_CHIP_ICM42600,
}, {
.compatible = "invensense,icm42602",
.data = (void *)INV_CHIP_ICM42602,
}, {
.compatible = "invensense,icm42605",
.data = (void *)INV_CHIP_ICM42605,
}, {
.compatible = "invensense,icm42686",
.data = (void *)INV_CHIP_ICM42686,
}, {
.compatible = "invensense,icm42622",
.data = (void *)INV_CHIP_ICM42622,
}, {
.compatible = "invensense,icm42688",
.data = (void *)INV_CHIP_ICM42688,
}, {
.compatible = "invensense,icm42631",
.data = (void *)INV_CHIP_ICM42631,
},
{ }
};
MODULE_DEVICE_TABLE(of, inv_icm42600_of_matches);
static struct spi_driver inv_icm42600_driver = {
.driver = {
.name = "inv-icm42600-spi",
.of_match_table = inv_icm42600_of_matches,
.pm = pm_ptr(&inv_icm42600_pm_ops),
},
.id_table = inv_icm42600_id,
.probe = inv_icm42600_probe,
};
module_spi_driver(inv_icm42600_driver);
MODULE_AUTHOR("InvenSense, Inc.");
MODULE_DESCRIPTION("InvenSense ICM-426xx SPI driver");
MODULE_LICENSE("GPL");
MODULE_IMPORT_NS("IIO_ICM42600");
@@ -0,0 +1,85 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2020 Invensense, Inc.
*/
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/mutex.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/iio/iio.h>
#include "inv_icm42600.h"
#include "inv_icm42600_temp.h"
static int inv_icm42600_temp_read(struct inv_icm42600_state *st, s16 *temp)
{
struct device *dev = regmap_get_device(st->map);
__be16 *raw;
int ret;
pm_runtime_get_sync(dev);
mutex_lock(&st->lock);
ret = inv_icm42600_set_temp_conf(st, true, NULL);
if (ret)
goto exit;
raw = (__be16 *)&st->buffer[0];
ret = regmap_bulk_read(st->map, INV_ICM42600_REG_TEMP_DATA, raw, sizeof(*raw));
if (ret)
goto exit;
*temp = (s16)be16_to_cpup(raw);
/*
* Temperature data is invalid if both accel and gyro are off.
* Return -EBUSY in this case.
*/
if (*temp == INV_ICM42600_DATA_INVALID)
ret = -EBUSY;
exit:
mutex_unlock(&st->lock);
pm_runtime_put_autosuspend(dev);
return ret;
}
int inv_icm42600_temp_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int *val, int *val2, long mask)
{
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
s16 temp;
int ret;
if (chan->type != IIO_TEMP)
return -EINVAL;
switch (mask) {
case IIO_CHAN_INFO_RAW:
ret = inv_icm42600_temp_read(st, &temp);
if (ret)
return ret;
*val = temp;
return IIO_VAL_INT;
/*
* T°C = (temp / 132.48) + 25
* Tm°C = 1000 * ((temp / 132.48) + 25)
* Tm°C = 7.548309 * temp + 25000
* Tm°C = (temp + 3312) * 7.548309
* scale: 100000 / 13248 ~= 7.548309
* offset: 3312
*/
case IIO_CHAN_INFO_SCALE:
*val = 7;
*val2 = 548309;
return IIO_VAL_INT_PLUS_MICRO;
case IIO_CHAN_INFO_OFFSET:
*val = 3312;
return IIO_VAL_INT;
default:
return -EINVAL;
}
}
@@ -0,0 +1,30 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2020 Invensense, Inc.
*/
#ifndef INV_ICM42600_TEMP_H_
#define INV_ICM42600_TEMP_H_
#include <linux/iio/iio.h>
#define INV_ICM42600_TEMP_CHAN(_index) \
{ \
.type = IIO_TEMP, \
.info_mask_separate = \
BIT(IIO_CHAN_INFO_RAW) | \
BIT(IIO_CHAN_INFO_OFFSET) | \
BIT(IIO_CHAN_INFO_SCALE), \
.scan_index = _index, \
.scan_type = { \
.sign = 's', \
.realbits = 16, \
.storagebits = 16, \
}, \
}
int inv_icm42600_temp_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int *val, int *val2, long mask);
#endif