2 * kxsd9.c simple support for the Kionix KXSD9 3D
5 * Copyright (c) 2008-2009 Jonathan Cameron <jic23@cam.ac.uk>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * The i2c interface is very similar, so shouldn't be a problem once
12 * I have a suitable wire made up.
14 * TODO: Support the motion detector
15 * Uses register address incrementing so could have a
16 * heavily optimized ring buffer access function.
19 #include <linux/device.h>
20 #include <linux/kernel.h>
21 #include <linux/spi/spi.h>
22 #include <linux/sysfs.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
47 * struct kxsd9_state - device related storage
48 * @buf_lock: protect the rx and tx buffers.
50 * @rx: single rx buffer storage
51 * @tx: single tx buffer storage
54 struct mutex buf_lock
;
55 struct spi_device
*us
;
56 u8 rx
[KXSD9_STATE_RX_SIZE
] ____cacheline_aligned
;
57 u8 tx
[KXSD9_STATE_TX_SIZE
];
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
)
71 struct kxsd9_state
*st
= iio_priv(indio_dev
);
74 for (i
= 0; i
< 4; i
++)
75 if (micro
== kxsd9_micro_scales
[i
]) {
82 mutex_lock(&st
->buf_lock
);
83 ret
= spi_w8r8(st
->us
, KXSD9_READ(KXSD9_REG_CTRL_C
));
86 st
->tx
[0] = KXSD9_WRITE(KXSD9_REG_CTRL_C
);
87 st
->tx
[1] = (ret
& ~KXSD9_FS_MASK
) | i
;
89 ret
= spi_write(st
->us
, st
->tx
, 2);
91 mutex_unlock(&st
->buf_lock
);
95 static int kxsd9_read(struct iio_dev
*indio_dev
, u8 address
)
97 struct spi_message msg
;
99 struct kxsd9_state
*st
= iio_priv(indio_dev
);
100 struct spi_transfer xfers
[] = {
113 mutex_lock(&st
->buf_lock
);
114 st
->tx
[0] = KXSD9_READ(address
);
115 spi_message_init(&msg
);
116 spi_message_add_tail(&xfers
[0], &msg
);
117 spi_message_add_tail(&xfers
[1], &msg
);
118 ret
= spi_sync(st
->us
, &msg
);
121 return (((u16
)(st
->rx
[0])) << 8) | (st
->rx
[1] & 0xF0);
124 static IIO_CONST_ATTR(accel_scale_available
,
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
,
136 struct iio_chan_spec
const *chan
,
143 if (mask
== IIO_CHAN_INFO_SCALE
) {
144 /* Check no integer component */
147 ret
= kxsd9_write_scale(indio_dev
, val2
);
153 static int kxsd9_read_raw(struct iio_dev
*indio_dev
,
154 struct iio_chan_spec
const *chan
,
155 int *val
, int *val2
, long mask
)
158 struct kxsd9_state
*st
= iio_priv(indio_dev
);
162 ret
= kxsd9_read(indio_dev
, chan
->address
);
167 case IIO_CHAN_INFO_SCALE
:
168 ret
= spi_w8r8(st
->us
, KXSD9_READ(KXSD9_REG_CTRL_C
));
171 *val2
= kxsd9_micro_scales
[ret
& KXSD9_FS_MASK
];
172 ret
= IIO_VAL_INT_PLUS_MICRO
;
179 #define KXSD9_ACCEL_CHAN(axis) \
183 .channel2 = IIO_MOD_##axis, \
184 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT, \
185 .address = KXSD9_REG_##axis, \
188 static struct iio_chan_spec kxsd9_channels
[] = {
189 KXSD9_ACCEL_CHAN(X
), KXSD9_ACCEL_CHAN(Y
), KXSD9_ACCEL_CHAN(Z
),
193 .address
= KXSD9_REG_AUX
,
197 static const struct attribute_group kxsd9_attribute_group
= {
198 .attrs
= kxsd9_attributes
,
201 static int __devinit
kxsd9_power_up(struct kxsd9_state
*st
)
207 ret
= spi_write(st
->us
, st
->tx
, 2);
213 return spi_write(st
->us
, st
->tx
, 2);
216 static const struct iio_info kxsd9_info
= {
217 .read_raw
= &kxsd9_read_raw
,
218 .write_raw
= &kxsd9_write_raw
,
219 .attrs
= &kxsd9_attribute_group
,
220 .driver_module
= THIS_MODULE
,
223 static int __devinit
kxsd9_probe(struct spi_device
*spi
)
225 struct iio_dev
*indio_dev
;
226 struct kxsd9_state
*st
;
229 indio_dev
= iio_allocate_device(sizeof(*st
));
230 if (indio_dev
== NULL
) {
234 st
= iio_priv(indio_dev
);
235 spi_set_drvdata(spi
, indio_dev
);
238 mutex_init(&st
->buf_lock
);
239 indio_dev
->channels
= kxsd9_channels
;
240 indio_dev
->num_channels
= ARRAY_SIZE(kxsd9_channels
);
241 indio_dev
->name
= spi_get_device_id(spi
)->name
;
242 indio_dev
->dev
.parent
= &spi
->dev
;
243 indio_dev
->info
= &kxsd9_info
;
244 indio_dev
->modes
= INDIO_DIRECT_MODE
;
246 ret
= iio_device_register(indio_dev
);
250 spi
->mode
= SPI_MODE_0
;
257 iio_free_device(indio_dev
);
262 static int __devexit
kxsd9_remove(struct spi_device
*spi
)
264 iio_device_unregister(spi_get_drvdata(spi
));
265 iio_free_device(spi_get_drvdata(spi
));
270 static const struct spi_device_id kxsd9_id
[] = {
274 MODULE_DEVICE_TABLE(spi
, kxsd9_id
);
276 static struct spi_driver kxsd9_driver
= {
279 .owner
= THIS_MODULE
,
281 .probe
= kxsd9_probe
,
282 .remove
= __devexit_p(kxsd9_remove
),
283 .id_table
= kxsd9_id
,
285 module_spi_driver(kxsd9_driver
);
287 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
288 MODULE_DESCRIPTION("Kionix KXSD9 SPI driver");
289 MODULE_LICENSE("GPL v2");