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.
59 * @indio_dev: associated industrial IO device
61 * @rx: single rx buffer storage
62 * @tx: single tx buffer storage
65 struct mutex buf_lock;
66 struct iio_dev *indio_dev;
67 struct spi_device *us;
72 /* This may want to move to mili g to allow for non integer ranges */
73 static ssize_t kxsd9_read_scale(struct device *dev,
74 struct device_attribute *attr,
79 struct iio_dev *indio_dev = dev_get_drvdata(dev);
80 struct kxsd9_state *st = indio_dev->dev_data;
81 struct spi_transfer xfer = {
88 struct spi_message msg;
90 mutex_lock(&st->buf_lock);
91 st->tx[0] = KXSD9_READ(KXSD9_REG_CTRL_C);
93 spi_message_init(&msg);
94 spi_message_add_tail(&xfer, &msg);
95 ret = spi_sync(st->us, &msg);
99 switch (st->rx[1] & KXSD9_FS_MASK) {
101 len += sprintf(buf, "%s\n", KXSD9_SCALE_8G);
104 len += sprintf(buf, "%s\n", KXSD9_SCALE_6G);
107 len += sprintf(buf, "%s\n", KXSD9_SCALE_4G);
110 len += sprintf(buf, "%s\n", KXSD9_SCALE_2G);
115 mutex_unlock(&st->buf_lock);
117 return ret ? ret : len;
119 static ssize_t kxsd9_write_scale(struct device *dev,
120 struct device_attribute *attr,
125 struct spi_message msg;
127 struct iio_dev *indio_dev = dev_get_drvdata(dev);
128 struct kxsd9_state *st = indio_dev->dev_data;
130 struct spi_transfer xfers[] = {
145 if (!strncmp(buf, KXSD9_SCALE_8G,
146 strlen(buf) < strlen(KXSD9_SCALE_8G)
147 ? strlen(buf) : strlen(KXSD9_SCALE_8G)))
149 else if (!strncmp(buf, KXSD9_SCALE_6G,
150 strlen(buf) < strlen(KXSD9_SCALE_6G)
151 ? strlen(buf) : strlen(KXSD9_SCALE_6G)))
153 else if (!strncmp(buf, KXSD9_SCALE_4G,
154 strlen(buf) < strlen(KXSD9_SCALE_4G)
155 ? strlen(buf) : strlen(KXSD9_SCALE_4G)))
157 else if (!strncmp(buf, KXSD9_SCALE_2G,
158 strlen(buf) < strlen(KXSD9_SCALE_2G)
159 ? strlen(buf) : strlen(KXSD9_SCALE_2G)))
164 mutex_lock(&st->buf_lock);
165 st->tx[0] = KXSD9_READ(KXSD9_REG_CTRL_C);
167 spi_message_init(&msg);
168 spi_message_add_tail(&xfers[0], &msg);
169 ret = spi_sync(st->us, &msg);
172 st->tx[0] = KXSD9_WRITE(KXSD9_REG_CTRL_C);
173 st->tx[1] = (st->rx[1] & ~KXSD9_FS_MASK) | val;
175 spi_message_init(&msg);
176 spi_message_add_tail(&xfers[1], &msg);
177 ret = spi_sync(st->us, &msg);
179 mutex_unlock(&st->buf_lock);
180 return ret ? ret : len;
183 static ssize_t kxsd9_read_accel(struct device *dev,
184 struct device_attribute *attr,
187 struct spi_message msg;
191 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
192 struct iio_dev *indio_dev = dev_get_drvdata(dev);
193 struct kxsd9_state *st = indio_dev->dev_data;
194 struct spi_transfer xfers[] = {
209 mutex_lock(&st->buf_lock);
210 st->tx[0] = KXSD9_READ(this_attr->address);
211 spi_message_init(&msg);
212 spi_message_add_tail(&xfers[0], &msg);
213 spi_message_add_tail(&xfers[1], &msg);
214 ret = spi_sync(st->us, &msg);
217 val = (((u16)(st->rx[0])) << 8) | (st->rx[1] & 0xF0);
218 len = sprintf(buf, "%d\n", val);
220 mutex_unlock(&st->buf_lock);
222 return ret ? ret : len;
225 static IIO_DEV_ATTR_ACCEL_X(kxsd9_read_accel, KXSD9_REG_X);
226 static IIO_DEV_ATTR_ACCEL_Y(kxsd9_read_accel, KXSD9_REG_Y);
227 static IIO_DEV_ATTR_ACCEL_Z(kxsd9_read_accel, KXSD9_REG_Z);
228 static IIO_DEV_ATTR_IN_RAW(0, kxsd9_read_accel, KXSD9_REG_AUX);
230 static IIO_DEVICE_ATTR(accel_scale,
236 static IIO_CONST_ATTR(accel_scale_available,
242 static struct attribute *kxsd9_attributes[] = {
243 &iio_dev_attr_accel_x_raw.dev_attr.attr,
244 &iio_dev_attr_accel_y_raw.dev_attr.attr,
245 &iio_dev_attr_accel_z_raw.dev_attr.attr,
246 &iio_dev_attr_in0_raw.dev_attr.attr,
247 &iio_dev_attr_accel_scale.dev_attr.attr,
248 &iio_const_attr_accel_scale_available.dev_attr.attr,
252 static const struct attribute_group kxsd9_attribute_group = {
253 .attrs = kxsd9_attributes,
256 static int __devinit kxsd9_power_up(struct spi_device *spi)
259 struct spi_transfer xfers[2] = {
270 struct spi_message msg;
272 u8 *tx = kmalloc(2, GFP_KERNEL);
278 tx2 = kmalloc(2, GFP_KERNEL);
289 xfers[0].tx_buf = tx;
290 xfers[1].tx_buf = tx2;
291 spi_message_init(&msg);
292 spi_message_add_tail(&xfers[0], &msg);
293 spi_message_add_tail(&xfers[1], &msg);
294 ret = spi_sync(spi, &msg);
304 static int __devinit kxsd9_probe(struct spi_device *spi)
307 struct kxsd9_state *st;
310 st = kzalloc(sizeof(*st), GFP_KERNEL);
315 spi_set_drvdata(spi, st);
317 st->rx = kmalloc(sizeof(*st->rx)*KXSD9_STATE_RX_SIZE,
319 if (st->rx == NULL) {
323 st->tx = kmalloc(sizeof(*st->tx)*KXSD9_STATE_TX_SIZE,
325 if (st->tx == NULL) {
331 mutex_init(&st->buf_lock);
332 st->indio_dev = iio_allocate_device();
333 if (st->indio_dev == NULL) {
337 st->indio_dev->dev.parent = &spi->dev;
339 st->indio_dev->num_interrupt_lines = 0;
340 st->indio_dev->event_attrs = NULL;
342 st->indio_dev->attrs = &kxsd9_attribute_group;
343 st->indio_dev->dev_data = (void *)(st);
344 st->indio_dev->driver_module = THIS_MODULE;
345 st->indio_dev->modes = INDIO_DIRECT_MODE;
347 ret = iio_device_register(st->indio_dev);
351 spi->mode = SPI_MODE_0;
358 iio_free_device(st->indio_dev);
369 static int __devexit kxsd9_remove(struct spi_device *spi)
371 struct kxsd9_state *st = spi_get_drvdata(spi);
373 iio_device_unregister(st->indio_dev);
381 static struct spi_driver kxsd9_driver = {
384 .owner = THIS_MODULE,
386 .probe = kxsd9_probe,
387 .remove = __devexit_p(kxsd9_remove),
390 static __init int kxsd9_spi_init(void)
392 return spi_register_driver(&kxsd9_driver);
394 module_init(kxsd9_spi_init);
396 static __exit void kxsd9_spi_exit(void)
398 spi_unregister_driver(&kxsd9_driver);
400 module_exit(kxsd9_spi_exit);
402 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
403 MODULE_DESCRIPTION("Kionix KXSD9 SPI driver");
404 MODULE_LICENSE("GPL v2");