19 #include <linux/device.h>
20 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
29 #define KXSD9_REG_X 0x00
30 #define KXSD9_REG_Y 0x02
31 #define KXSD9_REG_Z 0x04
32 #define KXSD9_REG_AUX 0x06
33 #define KXSD9_REG_RESET 0x0a
34 #define KXSD9_REG_CTRL_C 0x0c
36 #define KXSD9_FS_MASK 0x03
38 #define KXSD9_REG_CTRL_B 0x0d
39 #define KXSD9_REG_CTRL_A 0x0e
41 #define KXSD9_READ(a) (0x80 | (a))
42 #define KXSD9_WRITE(a) (a)
44 #define KXSD9_STATE_RX_SIZE 2
45 #define KXSD9_STATE_TX_SIZE 2
60 #define KXSD9_SCALE_2G "0.011978"
61 #define KXSD9_SCALE_4G "0.023927"
62 #define KXSD9_SCALE_6G "0.035934"
63 #define KXSD9_SCALE_8G "0.047853"
66 static const int kxsd9_micro_scales[4] = { 47853, 35934, 23927, 11978 };
68 static int kxsd9_write_scale(
struct iio_dev *indio_dev,
int micro)
74 for (i = 0; i < 4; i++)
75 if (micro == kxsd9_micro_scales[i]) {
89 ret = spi_write(st->
us, st->
tx, 2);
115 spi_message_init(&
msg);
116 spi_message_add_tail(&xfers[0], &
msg);
117 spi_message_add_tail(&xfers[1], &
msg);
121 return (((
u16)(st->rx[0])) << 8) | (st->rx[1] & 0xF0);
130 static struct attribute *kxsd9_attributes[] = {
131 &iio_const_attr_accel_scale_available.dev_attr.attr,
135 static int kxsd9_write_raw(
struct iio_dev *indio_dev,
147 ret = kxsd9_write_scale(indio_dev, val2);
153 static int kxsd9_read_raw(
struct iio_dev *indio_dev,
155 int *val,
int *val2,
long mask)
162 ret = kxsd9_read(indio_dev, chan->
address);
179 #define KXSD9_ACCEL_CHAN(axis) \
183 .channel2 = IIO_MOD_##axis, \
184 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
185 IIO_CHAN_INFO_SCALE_SHARED_BIT, \
186 .address = KXSD9_REG_##axis, \
200 .attrs = kxsd9_attributes,
209 ret = spi_write(st->
us, st->
tx, 2);
215 return spi_write(st->
us, st->
tx, 2);
218 static const struct iio_info kxsd9_info = {
219 .read_raw = &kxsd9_read_raw,
220 .write_raw = &kxsd9_write_raw,
221 .attrs = &kxsd9_attribute_group,
232 if (indio_dev ==
NULL) {
236 st = iio_priv(indio_dev);
237 spi_set_drvdata(spi, indio_dev);
241 indio_dev->
channels = kxsd9_channels;
244 indio_dev->
dev.parent = &spi->
dev;
245 indio_dev->
info = &kxsd9_info;
283 .probe = kxsd9_probe,
285 .id_table = kxsd9_id,