2
* ADXRS450 Digital Output Gyroscope Driver
4
* Copyright 2011 Analog Devices Inc.
6
* Licensed under the GPL-2.
9
#include <linux/interrupt.h>
10
#include <linux/irq.h>
11
#include <linux/delay.h>
12
#include <linux/mutex.h>
13
#include <linux/device.h>
14
#include <linux/kernel.h>
15
#include <linux/spi/spi.h>
16
#include <linux/slab.h>
17
#include <linux/sysfs.h>
18
#include <linux/list.h>
19
#include <linux/module.h>
27
* adxrs450_spi_read_reg_16() - read 2 bytes from a register pair
28
* @dev: device associated with child of actual iio_dev
29
* @reg_address: the address of the lower of the two registers,which should be an even address,
30
* Second register's address is reg_address + 1.
31
* @val: somewhere to pass back the value read
33
static int adxrs450_spi_read_reg_16(struct iio_dev *indio_dev,
37
struct adxrs450_state *st = iio_priv(indio_dev);
40
mutex_lock(&st->buf_lock);
41
st->tx[0] = ADXRS450_READ_DATA | (reg_address >> 7);
42
st->tx[1] = reg_address << 1;
46
if (!(hweight32(be32_to_cpu(*(u32 *)st->tx)) & 1))
47
st->tx[3] |= ADXRS450_P;
49
ret = spi_write(st->us, st->tx, 4);
51
dev_err(&st->us->dev, "problem while reading 16 bit register 0x%02x\n",
55
ret = spi_read(st->us, st->rx, 4);
57
dev_err(&st->us->dev, "problem while reading 16 bit register 0x%02x\n",
62
*val = (be32_to_cpu(*(u32 *)st->rx) >> 5) & 0xFFFF;
65
mutex_unlock(&st->buf_lock);
70
* adxrs450_spi_write_reg_16() - write 2 bytes data to a register pair
71
* @dev: device associated with child of actual actual iio_dev
72
* @reg_address: the address of the lower of the two registers,which should be an even address,
73
* Second register's address is reg_address + 1.
74
* @val: value to be written.
76
static int adxrs450_spi_write_reg_16(struct iio_dev *indio_dev,
80
struct adxrs450_state *st = iio_priv(indio_dev);
83
mutex_lock(&st->buf_lock);
84
st->tx[0] = ADXRS450_WRITE_DATA | reg_address >> 7;
85
st->tx[1] = reg_address << 1 | val >> 15;
89
if (!(hweight32(be32_to_cpu(*(u32 *)st->tx)) & 1))
90
st->tx[3] |= ADXRS450_P;
92
ret = spi_write(st->us, st->tx, 4);
94
dev_err(&st->us->dev, "problem while writing 16 bit register 0x%02x\n",
96
msleep(1); /* enforce sequential transfer delay 0.1ms */
97
mutex_unlock(&st->buf_lock);
102
* adxrs450_spi_sensor_data() - read 2 bytes sensor data
103
* @dev: device associated with child of actual iio_dev
104
* @val: somewhere to pass back the value read
106
static int adxrs450_spi_sensor_data(struct iio_dev *indio_dev, s16 *val)
108
struct adxrs450_state *st = iio_priv(indio_dev);
111
mutex_lock(&st->buf_lock);
112
st->tx[0] = ADXRS450_SENSOR_DATA;
117
ret = spi_write(st->us, st->tx, 4);
119
dev_err(&st->us->dev, "Problem while reading sensor data\n");
123
ret = spi_read(st->us, st->rx, 4);
125
dev_err(&st->us->dev, "Problem while reading sensor data\n");
129
*val = (be32_to_cpu(*(u32 *)st->rx) >> 10) & 0xFFFF;
132
mutex_unlock(&st->buf_lock);
137
* adxrs450_spi_initial() - use for initializing procedure.
138
* @st: device instance specific data
139
* @val: somewhere to pass back the value read
141
static int adxrs450_spi_initial(struct adxrs450_state *st,
144
struct spi_message msg;
146
struct spi_transfer xfers = {
153
mutex_lock(&st->buf_lock);
154
st->tx[0] = ADXRS450_SENSOR_DATA;
159
st->tx[3] |= (ADXRS450_CHK | ADXRS450_P);
160
spi_message_init(&msg);
161
spi_message_add_tail(&xfers, &msg);
162
ret = spi_sync(st->us, &msg);
164
dev_err(&st->us->dev, "Problem while reading initializing data\n");
168
*val = be32_to_cpu(*(u32 *)st->rx);
171
mutex_unlock(&st->buf_lock);
175
/* Recommended Startup Sequence by spec */
176
static int adxrs450_initial_setup(struct iio_dev *indio_dev)
181
struct adxrs450_state *st = iio_priv(indio_dev);
183
msleep(ADXRS450_STARTUP_DELAY*2);
184
ret = adxrs450_spi_initial(st, &t, 1);
188
dev_warn(&st->us->dev, "The initial power on response "
189
"is not correct! Restart without reset?\n");
191
msleep(ADXRS450_STARTUP_DELAY);
192
ret = adxrs450_spi_initial(st, &t, 0);
196
msleep(ADXRS450_STARTUP_DELAY);
197
ret = adxrs450_spi_initial(st, &t, 0);
200
if (((t & 0xff) | 0x01) != 0xff || ADXRS450_GET_ST(t) != 2) {
201
dev_err(&st->us->dev, "The second response is not correct!\n");
205
ret = adxrs450_spi_initial(st, &t, 0);
208
if (((t & 0xff) | 0x01) != 0xff || ADXRS450_GET_ST(t) != 2) {
209
dev_err(&st->us->dev, "The third response is not correct!\n");
213
ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_FAULT1, &data);
217
dev_err(&st->us->dev, "The device is not in normal status!\n");
220
ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_PID1, &data);
223
dev_info(&st->us->dev, "The Part ID is 0x%x\n", data);
225
ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_SNL, &data);
229
ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_SNH, &data);
233
dev_info(&st->us->dev, "The Serial Number is 0x%x\n", t);
238
static int adxrs450_write_raw(struct iio_dev *indio_dev,
239
struct iio_chan_spec const *chan,
246
case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
247
ret = adxrs450_spi_write_reg_16(indio_dev,
258
static int adxrs450_read_raw(struct iio_dev *indio_dev,
259
struct iio_chan_spec const *chan,
269
switch (chan->type) {
271
ret = adxrs450_spi_sensor_data(indio_dev, &t);
278
ret = adxrs450_spi_read_reg_16(indio_dev,
279
ADXRS450_TEMP1, &ut);
290
case (1 << IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SEPARATE):
291
ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_QUAD1, &t);
305
static const struct iio_chan_spec adxrs450_channels[] = {
307
.type = IIO_ANGL_VEL,
309
.channel2 = IIO_MOD_Z,
310
.info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
311
(1 << IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SEPARATE)
319
static const struct iio_info adxrs450_info = {
320
.driver_module = THIS_MODULE,
321
.read_raw = &adxrs450_read_raw,
322
.write_raw = &adxrs450_write_raw,
325
static int __devinit adxrs450_probe(struct spi_device *spi)
328
struct adxrs450_state *st;
329
struct iio_dev *indio_dev;
331
/* setup the industrialio driver allocated elements */
332
indio_dev = iio_allocate_device(sizeof(*st));
333
if (indio_dev == NULL) {
337
st = iio_priv(indio_dev);
339
mutex_init(&st->buf_lock);
340
/* This is only used for removal purposes */
341
spi_set_drvdata(spi, indio_dev);
343
indio_dev->dev.parent = &spi->dev;
344
indio_dev->info = &adxrs450_info;
345
indio_dev->modes = INDIO_DIRECT_MODE;
346
indio_dev->channels = adxrs450_channels;
347
indio_dev->num_channels = ARRAY_SIZE(adxrs450_channels);
348
indio_dev->name = spi->dev.driver->name;
350
ret = iio_device_register(indio_dev);
354
/* Get the device into a sane initial state */
355
ret = adxrs450_initial_setup(indio_dev);
360
iio_device_unregister(indio_dev);
362
iio_free_device(indio_dev);
368
static int adxrs450_remove(struct spi_device *spi)
370
iio_device_unregister(spi_get_drvdata(spi));
371
iio_free_device(spi_get_drvdata(spi));
376
static struct spi_driver adxrs450_driver = {
379
.owner = THIS_MODULE,
381
.probe = adxrs450_probe,
382
.remove = __devexit_p(adxrs450_remove),
385
static __init int adxrs450_init(void)
387
return spi_register_driver(&adxrs450_driver);
389
module_init(adxrs450_init);
391
static __exit void adxrs450_exit(void)
393
spi_unregister_driver(&adxrs450_driver);
395
module_exit(adxrs450_exit);
397
MODULE_AUTHOR("Cliff Cai <cliff.cai@xxxxxxxxxx>");
398
MODULE_DESCRIPTION("Analog Devices ADXRS450 Gyroscope SPI driver");
399
MODULE_LICENSE("GPL v2");