#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/iio/buffer.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/spi/spi.h>
#include <linux/iio/trigger_consumer.h>
#include <linux/iio/triggered_buffer.h>
#define BMA220_REG_ID 0x00
#define BMA220_REG_ACCEL_X 0x02
#define BMA220_REG_ACCEL_Y 0x03
#define BMA220_REG_ACCEL_Z 0x04
#define BMA220_REG_RANGE 0x11
#define BMA220_REG_SUSPEND 0x18
#define BMA220_CHIP_ID 0xDD
#define BMA220_READ_MASK 0x80
#define BMA220_RANGE_MASK 0x03
#define BMA220_DATA_SHIFT 2
#define BMA220_SUSPEND_SLEEP 0xFF
#define BMA220_SUSPEND_WAKE 0x00
#define BMA220_DEVICE_NAME "bma220"
#define BMA220_SCALE_AVAILABLE "0.623 1.248 2.491 4.983"
#define BMA220_ACCEL_CHANNEL(index, reg, axis) { \
.channel2 = IIO_MOD_##axis, \
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
.shift = BMA220_DATA_SHIFT, \
static IIO_CONST_ATTR(in_accel_scale_available, BMA220_SCALE_AVAILABLE);
static struct attribute *bma220_attributes[] = {
&iio_const_attr_in_accel_scale_available.dev_attr.attr,
static const struct attribute_group bma220_attribute_group = {
.attrs = bma220_attributes,
static const int bma220_scale_table[][4] = {
{0, 623000}, {1, 248000}, {2, 491000}, {4, 983000}
struct spi_device *spi_device;
u8 tx_buf[2] ____cacheline_aligned;
static const struct iio_chan_spec bma220_channels[] = {
BMA220_ACCEL_CHANNEL(0, BMA220_REG_ACCEL_X, X),
BMA220_ACCEL_CHANNEL(1, BMA220_REG_ACCEL_Y, Y),
BMA220_ACCEL_CHANNEL(2, BMA220_REG_ACCEL_Z, Z),
IIO_CHAN_SOFT_TIMESTAMP(3),
static inline int bma220_read_reg(struct spi_device *spi, u8 reg)
return spi_w8r8(spi, reg | BMA220_READ_MASK);