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>
27 #include "../adc/adc.h"
30 #define KXSD9_REG_X 0x00
31 #define KXSD9_REG_Y 0x02
32 #define KXSD9_REG_Z 0x04
33 #define KXSD9_REG_AUX 0x06
34 #define KXSD9_REG_RESET 0x0a
35 #define KXSD9_REG_CTRL_C 0x0c
37 #define KXSD9_FS_8 0x00
38 #define KXSD9_FS_6 0x01
39 #define KXSD9_FS_4 0x02
40 #define KXSD9_FS_2 0x03
41 #define KXSD9_FS_MASK 0x03
43 #define KXSD9_REG_CTRL_B 0x0d
44 #define KXSD9_REG_CTRL_A 0x0e
46 #define KXSD9_READ(a) (0x80 | (a))
47 #define KXSD9_WRITE(a) (a)
49 #define KXSD9_SCALE_2G "0.011978"
50 #define KXSD9_SCALE_4G "0.023927"
51 #define KXSD9_SCALE_6G "0.035934"
52 #define KXSD9_SCALE_8G "0.047853"
54 #define KXSD9_STATE_RX_SIZE 2
55 #define KXSD9_STATE_TX_SIZE 4
57 * struct kxsd9_state - device related storage
58 * @buf_lock: protect the rx and tx buffers.
60 * @rx: single rx buffer storage
61 * @tx: single tx buffer storage
64 struct mutex buf_lock
;
65 struct spi_device
*us
;
66 u8 rx
[KXSD9_STATE_RX_SIZE
] ____cacheline_aligned
;
67 u8 tx
[KXSD9_STATE_TX_SIZE
];
70 /* This may want to move to mili g to allow for non integer ranges */
71 static ssize_t
kxsd9_read_scale(struct device
*dev
,
72 struct device_attribute
*attr
,
77 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
78 struct kxsd9_state
*st
= iio_priv(indio_dev
);
79 struct spi_transfer xfer
= {
86 struct spi_message msg
;
88 mutex_lock(&st
->buf_lock
);
89 st
->tx
[0] = KXSD9_READ(KXSD9_REG_CTRL_C
);
91 spi_message_init(&msg
);
92 spi_message_add_tail(&xfer
, &msg
);
93 ret
= spi_sync(st
->us
, &msg
);
97 switch (st
->rx
[1] & KXSD9_FS_MASK
) {
99 len
+= sprintf(buf
, "%s\n", KXSD9_SCALE_8G
);
102 len
+= sprintf(buf
, "%s\n", KXSD9_SCALE_6G
);
105 len
+= sprintf(buf
, "%s\n", KXSD9_SCALE_4G
);
108 len
+= sprintf(buf
, "%s\n", KXSD9_SCALE_2G
);
113 mutex_unlock(&st
->buf_lock
);
115 return ret
? ret
: len
;
117 static ssize_t
kxsd9_write_scale(struct device
*dev
,
118 struct device_attribute
*attr
,
123 struct spi_message msg
;
125 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
126 struct kxsd9_state
*st
= iio_priv(indio_dev
);
128 struct spi_transfer xfers
[] = {
143 if (!strncmp(buf
, KXSD9_SCALE_8G
,
144 strlen(buf
) < strlen(KXSD9_SCALE_8G
)
145 ? strlen(buf
) : strlen(KXSD9_SCALE_8G
)))
147 else if (!strncmp(buf
, KXSD9_SCALE_6G
,
148 strlen(buf
) < strlen(KXSD9_SCALE_6G
)
149 ? strlen(buf
) : strlen(KXSD9_SCALE_6G
)))
151 else if (!strncmp(buf
, KXSD9_SCALE_4G
,
152 strlen(buf
) < strlen(KXSD9_SCALE_4G
)
153 ? strlen(buf
) : strlen(KXSD9_SCALE_4G
)))
155 else if (!strncmp(buf
, KXSD9_SCALE_2G
,
156 strlen(buf
) < strlen(KXSD9_SCALE_2G
)
157 ? strlen(buf
) : strlen(KXSD9_SCALE_2G
)))
162 mutex_lock(&st
->buf_lock
);
163 st
->tx
[0] = KXSD9_READ(KXSD9_REG_CTRL_C
);
165 spi_message_init(&msg
);
166 spi_message_add_tail(&xfers
[0], &msg
);
167 ret
= spi_sync(st
->us
, &msg
);
170 st
->tx
[0] = KXSD9_WRITE(KXSD9_REG_CTRL_C
);
171 st
->tx
[1] = (st
->rx
[1] & ~KXSD9_FS_MASK
) | val
;
173 spi_message_init(&msg
);
174 spi_message_add_tail(&xfers
[1], &msg
);
175 ret
= spi_sync(st
->us
, &msg
);
177 mutex_unlock(&st
->buf_lock
);
178 return ret
? ret
: len
;
181 static ssize_t
kxsd9_read_accel(struct device
*dev
,
182 struct device_attribute
*attr
,
185 struct spi_message msg
;
189 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
190 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
191 struct kxsd9_state
*st
= iio_priv(indio_dev
);
192 struct spi_transfer xfers
[] = {
207 mutex_lock(&st
->buf_lock
);
208 st
->tx
[0] = KXSD9_READ(this_attr
->address
);
209 spi_message_init(&msg
);
210 spi_message_add_tail(&xfers
[0], &msg
);
211 spi_message_add_tail(&xfers
[1], &msg
);
212 ret
= spi_sync(st
->us
, &msg
);
215 val
= (((u16
)(st
->rx
[0])) << 8) | (st
->rx
[1] & 0xF0);
216 len
= sprintf(buf
, "%d\n", val
);
218 mutex_unlock(&st
->buf_lock
);
220 return ret
? ret
: len
;
223 static IIO_DEV_ATTR_ACCEL_X(kxsd9_read_accel
, KXSD9_REG_X
);
224 static IIO_DEV_ATTR_ACCEL_Y(kxsd9_read_accel
, KXSD9_REG_Y
);
225 static IIO_DEV_ATTR_ACCEL_Z(kxsd9_read_accel
, KXSD9_REG_Z
);
226 static IIO_DEV_ATTR_IN_RAW(0, kxsd9_read_accel
, KXSD9_REG_AUX
);
228 static IIO_DEVICE_ATTR(accel_scale
,
234 static IIO_CONST_ATTR(accel_scale_available
,
240 static struct attribute
*kxsd9_attributes
[] = {
241 &iio_dev_attr_accel_x_raw
.dev_attr
.attr
,
242 &iio_dev_attr_accel_y_raw
.dev_attr
.attr
,
243 &iio_dev_attr_accel_z_raw
.dev_attr
.attr
,
244 &iio_dev_attr_in0_raw
.dev_attr
.attr
,
245 &iio_dev_attr_accel_scale
.dev_attr
.attr
,
246 &iio_const_attr_accel_scale_available
.dev_attr
.attr
,
250 static const struct attribute_group kxsd9_attribute_group
= {
251 .attrs
= kxsd9_attributes
,
254 static int __devinit
kxsd9_power_up(struct kxsd9_state
*st
)
256 struct spi_transfer xfers
[2] = {
266 .tx_buf
= st
->tx
+ 2,
269 struct spi_message msg
;
275 spi_message_init(&msg
);
276 spi_message_add_tail(&xfers
[0], &msg
);
277 spi_message_add_tail(&xfers
[1], &msg
);
279 return spi_sync(st
->us
, &msg
);
282 static const struct iio_info kxsd9_info
= {
283 .attrs
= &kxsd9_attribute_group
,
284 .driver_module
= THIS_MODULE
,
287 static int __devinit
kxsd9_probe(struct spi_device
*spi
)
289 struct iio_dev
*indio_dev
;
290 struct kxsd9_state
*st
;
293 indio_dev
= iio_allocate_device(sizeof(*st
));
294 if (indio_dev
== NULL
) {
298 st
= iio_priv(indio_dev
);
299 spi_set_drvdata(spi
, indio_dev
);
302 mutex_init(&st
->buf_lock
);
304 indio_dev
->dev
.parent
= &spi
->dev
;
305 indio_dev
->info
= &kxsd9_info
;
306 indio_dev
->modes
= INDIO_DIRECT_MODE
;
308 ret
= iio_device_register(indio_dev
);
312 spi
->mode
= SPI_MODE_0
;
319 iio_free_device(indio_dev
);
324 static int __devexit
kxsd9_remove(struct spi_device
*spi
)
326 iio_device_unregister(spi_get_drvdata(spi
));
331 static struct spi_driver kxsd9_driver
= {
334 .owner
= THIS_MODULE
,
336 .probe
= kxsd9_probe
,
337 .remove
= __devexit_p(kxsd9_remove
),
340 static __init
int kxsd9_spi_init(void)
342 return spi_register_driver(&kxsd9_driver
);
344 module_init(kxsd9_spi_init
);
346 static __exit
void kxsd9_spi_exit(void)
348 spi_unregister_driver(&kxsd9_driver
);
350 module_exit(kxsd9_spi_exit
);
352 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
353 MODULE_DESCRIPTION("Kionix KXSD9 SPI driver");
354 MODULE_LICENSE("GPL v2");