~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to drivers/staging/iio/imu/adis16350_core.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * ADIS16350/54/55/60/62/64/65 high precision tri-axis inertial sensor
3
 
 *
4
 
 * Copyright 2010 Analog Devices Inc.
5
 
 *
6
 
 * Licensed under the GPL-2 or later.
7
 
 */
8
 
 
9
 
#include <linux/interrupt.h>
10
 
#include <linux/irq.h>
11
 
#include <linux/gpio.h>
12
 
#include <linux/delay.h>
13
 
#include <linux/mutex.h>
14
 
#include <linux/device.h>
15
 
#include <linux/kernel.h>
16
 
#include <linux/spi/spi.h>
17
 
#include <linux/slab.h>
18
 
#include <linux/sysfs.h>
19
 
#include <linux/list.h>
20
 
 
21
 
#include "../iio.h"
22
 
#include "../sysfs.h"
23
 
#include "../ring_generic.h"
24
 
#include "../accel/accel.h"
25
 
#include "../adc/adc.h"
26
 
#include "../gyro/gyro.h"
27
 
 
28
 
#include "adis16350.h"
29
 
 
30
 
#define DRIVER_NAME             "adis16350"
31
 
 
32
 
static int adis16350_check_status(struct device *dev);
33
 
 
34
 
/**
35
 
 * adis16350_spi_write_reg_8() - write single byte to a register
36
 
 * @dev: device associated with child of actual device (iio_dev or iio_trig)
37
 
 * @reg_address: the address of the register to be written
38
 
 * @val: the value to write
39
 
 **/
40
 
static int adis16350_spi_write_reg_8(struct device *dev,
41
 
                u8 reg_address,
42
 
                u8 val)
43
 
{
44
 
        int ret;
45
 
        struct iio_dev *indio_dev = dev_get_drvdata(dev);
46
 
        struct adis16350_state *st = iio_dev_get_devdata(indio_dev);
47
 
 
48
 
        mutex_lock(&st->buf_lock);
49
 
        st->tx[0] = ADIS16350_WRITE_REG(reg_address);
50
 
        st->tx[1] = val;
51
 
 
52
 
        ret = spi_write(st->us, st->tx, 2);
53
 
        mutex_unlock(&st->buf_lock);
54
 
 
55
 
        return ret;
56
 
}
57
 
 
58
 
/**
59
 
 * adis16350_spi_write_reg_16() - write 2 bytes to a pair of registers
60
 
 * @dev: device associated with child of actual device (iio_dev or iio_trig)
61
 
 * @reg_address: the address of the lower of the two registers. Second register
62
 
 *               is assumed to have address one greater.
63
 
 * @val: value to be written
64
 
 **/
65
 
static int adis16350_spi_write_reg_16(struct device *dev,
66
 
                u8 lower_reg_address,
67
 
                u16 value)
68
 
{
69
 
        int ret;
70
 
        struct spi_message msg;
71
 
        struct iio_dev *indio_dev = dev_get_drvdata(dev);
72
 
        struct adis16350_state *st = iio_dev_get_devdata(indio_dev);
73
 
        struct spi_transfer xfers[] = {
74
 
                {
75
 
                        .tx_buf = st->tx,
76
 
                        .bits_per_word = 8,
77
 
                        .len = 2,
78
 
                        .cs_change = 1,
79
 
                        .delay_usecs = 35,
80
 
                }, {
81
 
                        .tx_buf = st->tx + 2,
82
 
                        .bits_per_word = 8,
83
 
                        .len = 2,
84
 
                        .cs_change = 1,
85
 
                        .delay_usecs = 35,
86
 
                },
87
 
        };
88
 
 
89
 
        mutex_lock(&st->buf_lock);
90
 
        st->tx[0] = ADIS16350_WRITE_REG(lower_reg_address);
91
 
        st->tx[1] = value & 0xFF;
92
 
        st->tx[2] = ADIS16350_WRITE_REG(lower_reg_address + 1);
93
 
        st->tx[3] = (value >> 8) & 0xFF;
94
 
 
95
 
        spi_message_init(&msg);
96
 
        spi_message_add_tail(&xfers[0], &msg);
97
 
        spi_message_add_tail(&xfers[1], &msg);
98
 
        ret = spi_sync(st->us, &msg);
99
 
        mutex_unlock(&st->buf_lock);
100
 
 
101
 
        return ret;
102
 
}
103
 
 
104
 
/**
105
 
 * adis16350_spi_read_reg_16() - read 2 bytes from a 16-bit register
106
 
 * @dev: device associated with child of actual device (iio_dev or iio_trig)
107
 
 * @reg_address: the address of the lower of the two registers. Second register
108
 
 *               is assumed to have address one greater.
109
 
 * @val: somewhere to pass back the value read
110
 
 **/
111
 
static int adis16350_spi_read_reg_16(struct device *dev,
112
 
                u8 lower_reg_address,
113
 
                u16 *val)
114
 
{
115
 
        struct spi_message msg;
116
 
        struct iio_dev *indio_dev = dev_get_drvdata(dev);
117
 
        struct adis16350_state *st = iio_dev_get_devdata(indio_dev);
118
 
        int ret;
119
 
        struct spi_transfer xfers[] = {
120
 
                {
121
 
                        .tx_buf = st->tx,
122
 
                        .bits_per_word = 8,
123
 
                        .len = 2,
124
 
                        .cs_change = 1,
125
 
                        .delay_usecs = 35,
126
 
                }, {
127
 
                        .rx_buf = st->rx,
128
 
                        .bits_per_word = 8,
129
 
                        .len = 2,
130
 
                        .cs_change = 1,
131
 
                        .delay_usecs = 35,
132
 
                },
133
 
        };
134
 
 
135
 
        mutex_lock(&st->buf_lock);
136
 
        st->tx[0] = ADIS16350_READ_REG(lower_reg_address);
137
 
        st->tx[1] = 0;
138
 
        st->tx[2] = 0;
139
 
        st->tx[3] = 0;
140
 
 
141
 
        spi_message_init(&msg);
142
 
        spi_message_add_tail(&xfers[0], &msg);
143
 
        spi_message_add_tail(&xfers[1], &msg);
144
 
        ret = spi_sync(st->us, &msg);
145
 
        if (ret) {
146
 
                dev_err(&st->us->dev,
147
 
                        "problem when reading 16 bit register 0x%02X",
148
 
                        lower_reg_address);
149
 
                goto error_ret;
150
 
        }
151
 
        *val = (st->rx[0] << 8) | st->rx[1];
152
 
 
153
 
error_ret:
154
 
        mutex_unlock(&st->buf_lock);
155
 
        return ret;
156
 
}
157
 
 
158
 
 
159
 
static ssize_t adis16350_spi_read_signed(struct device *dev,
160
 
                struct device_attribute *attr,
161
 
                char *buf,
162
 
                unsigned bits)
163
 
{
164
 
        int ret;
165
 
        s16 val = 0;
166
 
        unsigned shift = 16 - bits;
167
 
        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
168
 
 
169
 
        ret = adis16350_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
170
 
        if (ret)
171
 
                return ret;
172
 
 
173
 
        if (val & ADIS16350_ERROR_ACTIVE)
174
 
                adis16350_check_status(dev);
175
 
        val = ((s16)(val << shift) >> shift);
176
 
        return sprintf(buf, "%d\n", val);
177
 
}
178
 
 
179
 
static ssize_t adis16350_read_12bit_unsigned(struct device *dev,
180
 
                struct device_attribute *attr,
181
 
                char *buf)
182
 
{
183
 
        int ret;
184
 
        u16 val = 0;
185
 
        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
186
 
 
187
 
        ret = adis16350_spi_read_reg_16(dev, this_attr->address, &val);
188
 
        if (ret)
189
 
                return ret;
190
 
 
191
 
        if (val & ADIS16350_ERROR_ACTIVE)
192
 
                adis16350_check_status(dev);
193
 
 
194
 
        return sprintf(buf, "%u\n", val & 0x0FFF);
195
 
}
196
 
 
197
 
static ssize_t adis16350_read_14bit_signed(struct device *dev,
198
 
                struct device_attribute *attr,
199
 
                char *buf)
200
 
{
201
 
        struct iio_dev *indio_dev = dev_get_drvdata(dev);
202
 
        ssize_t ret;
203
 
 
204
 
        /* Take the iio_dev status lock */
205
 
        mutex_lock(&indio_dev->mlock);
206
 
        ret =  adis16350_spi_read_signed(dev, attr, buf, 14);
207
 
        mutex_unlock(&indio_dev->mlock);
208
 
 
209
 
        return ret;
210
 
}
211
 
 
212
 
static ssize_t adis16350_read_12bit_signed(struct device *dev,
213
 
                struct device_attribute *attr,
214
 
                char *buf)
215
 
{
216
 
        struct iio_dev *indio_dev = dev_get_drvdata(dev);
217
 
        ssize_t ret;
218
 
 
219
 
        /* Take the iio_dev status lock */
220
 
        mutex_lock(&indio_dev->mlock);
221
 
        ret =  adis16350_spi_read_signed(dev, attr, buf, 12);
222
 
        mutex_unlock(&indio_dev->mlock);
223
 
 
224
 
        return ret;
225
 
}
226
 
 
227
 
static ssize_t adis16350_write_16bit(struct device *dev,
228
 
                struct device_attribute *attr,
229
 
                const char *buf,
230
 
                size_t len)
231
 
{
232
 
        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
233
 
        int ret;
234
 
        long val;
235
 
 
236
 
        ret = strict_strtol(buf, 10, &val);
237
 
        if (ret)
238
 
                goto error_ret;
239
 
        ret = adis16350_spi_write_reg_16(dev, this_attr->address, val);
240
 
 
241
 
error_ret:
242
 
        return ret ? ret : len;
243
 
}
244
 
 
245
 
static ssize_t adis16350_read_frequency(struct device *dev,
246
 
                struct device_attribute *attr,
247
 
                char *buf)
248
 
{
249
 
        int ret, len = 0;
250
 
        u16 t;
251
 
        int sps;
252
 
        ret = adis16350_spi_read_reg_16(dev,
253
 
                        ADIS16350_SMPL_PRD,
254
 
                        &t);
255
 
        if (ret)
256
 
                return ret;
257
 
        sps =  (t & ADIS16350_SMPL_PRD_TIME_BASE) ? 53 : 1638;
258
 
        sps /= (t & ADIS16350_SMPL_PRD_DIV_MASK) + 1;
259
 
        len = sprintf(buf, "%d SPS\n", sps);
260
 
        return len;
261
 
}
262
 
 
263
 
static ssize_t adis16350_write_frequency(struct device *dev,
264
 
                struct device_attribute *attr,
265
 
                const char *buf,
266
 
                size_t len)
267
 
{
268
 
        struct iio_dev *indio_dev = dev_get_drvdata(dev);
269
 
        struct adis16350_state *st = iio_dev_get_devdata(indio_dev);
270
 
        long val;
271
 
        int ret;
272
 
        u8 t;
273
 
 
274
 
        ret = strict_strtol(buf, 10, &val);
275
 
        if (ret)
276
 
                return ret;
277
 
 
278
 
        mutex_lock(&indio_dev->mlock);
279
 
 
280
 
        t = (1638 / val);
281
 
        if (t > 0)
282
 
                t--;
283
 
        t &= ADIS16350_SMPL_PRD_DIV_MASK;
284
 
        if ((t & ADIS16350_SMPL_PRD_DIV_MASK) >= 0x0A)
285
 
                st->us->max_speed_hz = ADIS16350_SPI_SLOW;
286
 
        else
287
 
                st->us->max_speed_hz = ADIS16350_SPI_FAST;
288
 
 
289
 
        ret = adis16350_spi_write_reg_8(dev,
290
 
                        ADIS16350_SMPL_PRD,
291
 
                        t);
292
 
 
293
 
        mutex_unlock(&indio_dev->mlock);
294
 
 
295
 
        return ret ? ret : len;
296
 
}
297
 
 
298
 
static int adis16350_reset(struct device *dev)
299
 
{
300
 
        int ret;
301
 
        ret = adis16350_spi_write_reg_8(dev,
302
 
                        ADIS16350_GLOB_CMD,
303
 
                        ADIS16350_GLOB_CMD_SW_RESET);
304
 
        if (ret)
305
 
                dev_err(dev, "problem resetting device");
306
 
 
307
 
        return ret;
308
 
}
309
 
 
310
 
static ssize_t adis16350_write_reset(struct device *dev,
311
 
                struct device_attribute *attr,
312
 
                const char *buf, size_t len)
313
 
{
314
 
        if (len < 1)
315
 
                return -1;
316
 
        switch (buf[0]) {
317
 
        case '1':
318
 
        case 'y':
319
 
        case 'Y':
320
 
                return adis16350_reset(dev);
321
 
        }
322
 
        return -1;
323
 
}
324
 
 
325
 
int adis16350_set_irq(struct device *dev, bool enable)
326
 
{
327
 
        int ret;
328
 
        u16 msc;
329
 
        ret = adis16350_spi_read_reg_16(dev, ADIS16350_MSC_CTRL, &msc);
330
 
        if (ret)
331
 
                goto error_ret;
332
 
 
333
 
        msc |= ADIS16350_MSC_CTRL_DATA_RDY_POL_HIGH;
334
 
        msc &= ~ADIS16350_MSC_CTRL_DATA_RDY_DIO2;
335
 
 
336
 
        if (enable)
337
 
                msc |= ADIS16350_MSC_CTRL_DATA_RDY_EN;
338
 
        else
339
 
                msc &= ~ADIS16350_MSC_CTRL_DATA_RDY_EN;
340
 
 
341
 
        ret = adis16350_spi_write_reg_16(dev, ADIS16350_MSC_CTRL, msc);
342
 
        if (ret)
343
 
                goto error_ret;
344
 
 
345
 
error_ret:
346
 
        return ret;
347
 
}
348
 
 
349
 
/* Power down the device */
350
 
static int adis16350_stop_device(struct device *dev)
351
 
{
352
 
        int ret;
353
 
        u16 val = ADIS16350_SLP_CNT_POWER_OFF;
354
 
 
355
 
        ret = adis16350_spi_write_reg_16(dev, ADIS16350_SLP_CNT, val);
356
 
        if (ret)
357
 
                dev_err(dev, "problem with turning device off: SLP_CNT");
358
 
 
359
 
        return ret;
360
 
}
361
 
 
362
 
static int adis16350_self_test(struct device *dev)
363
 
{
364
 
        int ret;
365
 
        ret = adis16350_spi_write_reg_16(dev,
366
 
                        ADIS16350_MSC_CTRL,
367
 
                        ADIS16350_MSC_CTRL_MEM_TEST);
368
 
        if (ret) {
369
 
                dev_err(dev, "problem starting self test");
370
 
                goto err_ret;
371
 
        }
372
 
 
373
 
        adis16350_check_status(dev);
374
 
 
375
 
err_ret:
376
 
        return ret;
377
 
}
378
 
 
379
 
static int adis16350_check_status(struct device *dev)
380
 
{
381
 
        u16 status;
382
 
        int ret;
383
 
 
384
 
        ret = adis16350_spi_read_reg_16(dev, ADIS16350_DIAG_STAT, &status);
385
 
 
386
 
        if (ret < 0) {
387
 
                dev_err(dev, "Reading status failed\n");
388
 
                goto error_ret;
389
 
        }
390
 
        ret = status;
391
 
        if (status & ADIS16350_DIAG_STAT_ZACCL_FAIL)
392
 
                dev_err(dev, "Z-axis accelerometer self-test failure\n");
393
 
        if (status & ADIS16350_DIAG_STAT_YACCL_FAIL)
394
 
                dev_err(dev, "Y-axis accelerometer self-test failure\n");
395
 
        if (status & ADIS16350_DIAG_STAT_XACCL_FAIL)
396
 
                dev_err(dev, "X-axis accelerometer self-test failure\n");
397
 
        if (status & ADIS16350_DIAG_STAT_XGYRO_FAIL)
398
 
                dev_err(dev, "X-axis gyroscope self-test failure\n");
399
 
        if (status & ADIS16350_DIAG_STAT_YGYRO_FAIL)
400
 
                dev_err(dev, "Y-axis gyroscope self-test failure\n");
401
 
        if (status & ADIS16350_DIAG_STAT_ZGYRO_FAIL)
402
 
                dev_err(dev, "Z-axis gyroscope self-test failure\n");
403
 
        if (status & ADIS16350_DIAG_STAT_ALARM2)
404
 
                dev_err(dev, "Alarm 2 active\n");
405
 
        if (status & ADIS16350_DIAG_STAT_ALARM1)
406
 
                dev_err(dev, "Alarm 1 active\n");
407
 
        if (status & ADIS16350_DIAG_STAT_FLASH_CHK)
408
 
                dev_err(dev, "Flash checksum error\n");
409
 
        if (status & ADIS16350_DIAG_STAT_SELF_TEST)
410
 
                dev_err(dev, "Self test error\n");
411
 
        if (status & ADIS16350_DIAG_STAT_OVERFLOW)
412
 
                dev_err(dev, "Sensor overrange\n");
413
 
        if (status & ADIS16350_DIAG_STAT_SPI_FAIL)
414
 
                dev_err(dev, "SPI failure\n");
415
 
        if (status & ADIS16350_DIAG_STAT_FLASH_UPT)
416
 
                dev_err(dev, "Flash update failed\n");
417
 
        if (status & ADIS16350_DIAG_STAT_POWER_HIGH)
418
 
                dev_err(dev, "Power supply above 5.25V\n");
419
 
        if (status & ADIS16350_DIAG_STAT_POWER_LOW)
420
 
                dev_err(dev, "Power supply below 4.75V\n");
421
 
 
422
 
error_ret:
423
 
        return ret;
424
 
}
425
 
 
426
 
static int adis16350_initial_setup(struct adis16350_state *st)
427
 
{
428
 
        int ret;
429
 
        u16 smp_prd;
430
 
        struct device *dev = &st->indio_dev->dev;
431
 
 
432
 
        /* use low spi speed for init */
433
 
        st->us->max_speed_hz = ADIS16350_SPI_SLOW;
434
 
        st->us->mode = SPI_MODE_3;
435
 
        spi_setup(st->us);
436
 
 
437
 
        /* Disable IRQ */
438
 
        ret = adis16350_set_irq(dev, false);
439
 
        if (ret) {
440
 
                dev_err(dev, "disable irq failed");
441
 
                goto err_ret;
442
 
        }
443
 
 
444
 
        /* Do self test */
445
 
        ret = adis16350_self_test(dev);
446
 
        if (ret) {
447
 
                dev_err(dev, "self test failure");
448
 
                goto err_ret;
449
 
        }
450
 
 
451
 
        /* Read status register to check the result */
452
 
        ret = adis16350_check_status(dev);
453
 
        if (ret) {
454
 
                adis16350_reset(dev);
455
 
                dev_err(dev, "device not playing ball -> reset");
456
 
                msleep(ADIS16350_STARTUP_DELAY);
457
 
                ret = adis16350_check_status(dev);
458
 
                if (ret) {
459
 
                        dev_err(dev, "giving up");
460
 
                        goto err_ret;
461
 
                }
462
 
        }
463
 
 
464
 
        printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
465
 
                        st->us->chip_select, st->us->irq);
466
 
 
467
 
        /* use high spi speed if possible */
468
 
        ret = adis16350_spi_read_reg_16(dev, ADIS16350_SMPL_PRD, &smp_prd);
469
 
        if (!ret && (smp_prd & ADIS16350_SMPL_PRD_DIV_MASK) < 0x0A) {
470
 
                st->us->max_speed_hz = ADIS16350_SPI_SLOW;
471
 
                spi_setup(st->us);
472
 
        }
473
 
 
474
 
err_ret:
475
 
        return ret;
476
 
}
477
 
 
478
 
static IIO_DEV_ATTR_GYRO_X_CALIBBIAS(S_IWUSR | S_IRUGO,
479
 
                adis16350_read_12bit_signed,
480
 
                adis16350_write_16bit,
481
 
                ADIS16350_XGYRO_OFF);
482
 
 
483
 
static IIO_DEV_ATTR_GYRO_Y_CALIBBIAS(S_IWUSR | S_IRUGO,
484
 
                adis16350_read_12bit_signed,
485
 
                adis16350_write_16bit,
486
 
                ADIS16350_YGYRO_OFF);
487
 
 
488
 
static IIO_DEV_ATTR_GYRO_Z_CALIBBIAS(S_IWUSR | S_IRUGO,
489
 
                adis16350_read_12bit_signed,
490
 
                adis16350_write_16bit,
491
 
                ADIS16350_ZGYRO_OFF);
492
 
 
493
 
static IIO_DEV_ATTR_ACCEL_X_CALIBBIAS(S_IWUSR | S_IRUGO,
494
 
                adis16350_read_12bit_signed,
495
 
                adis16350_write_16bit,
496
 
                ADIS16350_XACCL_OFF);
497
 
 
498
 
static IIO_DEV_ATTR_ACCEL_Y_CALIBBIAS(S_IWUSR | S_IRUGO,
499
 
                adis16350_read_12bit_signed,
500
 
                adis16350_write_16bit,
501
 
                ADIS16350_YACCL_OFF);
502
 
 
503
 
static IIO_DEV_ATTR_ACCEL_Z_CALIBBIAS(S_IWUSR | S_IRUGO,
504
 
                adis16350_read_12bit_signed,
505
 
                adis16350_write_16bit,
506
 
                ADIS16350_ZACCL_OFF);
507
 
 
508
 
static IIO_DEV_ATTR_IN_NAMED_RAW(0, supply, adis16350_read_12bit_unsigned,
509
 
                ADIS16350_SUPPLY_OUT);
510
 
static IIO_CONST_ATTR_IN_NAMED_SCALE(0, supply, "0.002418");
511
 
 
512
 
static IIO_DEV_ATTR_GYRO_X(adis16350_read_14bit_signed,
513
 
                ADIS16350_XGYRO_OUT);
514
 
static IIO_DEV_ATTR_GYRO_Y(adis16350_read_14bit_signed,
515
 
                ADIS16350_YGYRO_OUT);
516
 
static IIO_DEV_ATTR_GYRO_Z(adis16350_read_14bit_signed,
517
 
                ADIS16350_ZGYRO_OUT);
518
 
static IIO_CONST_ATTR_GYRO_SCALE("0.00127862821");
519
 
 
520
 
static IIO_DEV_ATTR_ACCEL_X(adis16350_read_14bit_signed,
521
 
                ADIS16350_XACCL_OUT);
522
 
static IIO_DEV_ATTR_ACCEL_Y(adis16350_read_14bit_signed,
523
 
                ADIS16350_YACCL_OUT);
524
 
static IIO_DEV_ATTR_ACCEL_Z(adis16350_read_14bit_signed,
525
 
                ADIS16350_ZACCL_OUT);
526
 
static IIO_CONST_ATTR_ACCEL_SCALE("0.0247323713");
527
 
 
528
 
static IIO_DEVICE_ATTR(temp_x_raw, S_IRUGO, adis16350_read_12bit_signed,
529
 
                NULL, ADIS16350_XTEMP_OUT);
530
 
static IIO_DEVICE_ATTR(temp_y_raw, S_IRUGO, adis16350_read_12bit_signed,
531
 
                NULL, ADIS16350_YTEMP_OUT);
532
 
static IIO_DEVICE_ATTR(temp_z_raw, S_IRUGO, adis16350_read_12bit_signed,
533
 
                NULL, ADIS16350_ZTEMP_OUT);
534
 
static IIO_CONST_ATTR_TEMP_SCALE("0.14534");
535
 
static IIO_CONST_ATTR_TEMP_OFFSET("198.16");
536
 
 
537
 
static IIO_DEV_ATTR_IN_RAW(1, adis16350_read_12bit_unsigned,
538
 
                ADIS16350_AUX_ADC);
539
 
static IIO_CONST_ATTR(in1_scale, "0.000806");
540
 
 
541
 
static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
542
 
                adis16350_read_frequency,
543
 
                adis16350_write_frequency);
544
 
 
545
 
static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL,
546
 
                adis16350_write_reset, 0);
547
 
 
548
 
static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("409 546 819 1638");
549
 
 
550
 
static IIO_CONST_ATTR_NAME("adis16350");
551
 
 
552
 
static struct attribute *adis16350_attributes[] = {
553
 
        &iio_dev_attr_gyro_x_calibbias.dev_attr.attr,
554
 
        &iio_dev_attr_gyro_y_calibbias.dev_attr.attr,
555
 
        &iio_dev_attr_gyro_z_calibbias.dev_attr.attr,
556
 
        &iio_dev_attr_accel_x_calibbias.dev_attr.attr,
557
 
        &iio_dev_attr_accel_y_calibbias.dev_attr.attr,
558
 
        &iio_dev_attr_accel_z_calibbias.dev_attr.attr,
559
 
        &iio_dev_attr_in0_supply_raw.dev_attr.attr,
560
 
        &iio_const_attr_in0_supply_scale.dev_attr.attr,
561
 
        &iio_dev_attr_gyro_x_raw.dev_attr.attr,
562
 
        &iio_dev_attr_gyro_y_raw.dev_attr.attr,
563
 
        &iio_dev_attr_gyro_z_raw.dev_attr.attr,
564
 
        &iio_const_attr_gyro_scale.dev_attr.attr,
565
 
        &iio_dev_attr_accel_x_raw.dev_attr.attr,
566
 
        &iio_dev_attr_accel_y_raw.dev_attr.attr,
567
 
        &iio_dev_attr_accel_z_raw.dev_attr.attr,
568
 
        &iio_const_attr_accel_scale.dev_attr.attr,
569
 
        &iio_dev_attr_temp_x_raw.dev_attr.attr,
570
 
        &iio_dev_attr_temp_y_raw.dev_attr.attr,
571
 
        &iio_dev_attr_temp_z_raw.dev_attr.attr,
572
 
        &iio_const_attr_temp_scale.dev_attr.attr,
573
 
        &iio_const_attr_temp_offset.dev_attr.attr,
574
 
        &iio_dev_attr_in1_raw.dev_attr.attr,
575
 
        &iio_const_attr_in1_scale.dev_attr.attr,
576
 
        &iio_dev_attr_sampling_frequency.dev_attr.attr,
577
 
        &iio_const_attr_sampling_frequency_available.dev_attr.attr,
578
 
        &iio_dev_attr_reset.dev_attr.attr,
579
 
        &iio_const_attr_name.dev_attr.attr,
580
 
        NULL
581
 
};
582
 
 
583
 
static const struct attribute_group adis16350_attribute_group = {
584
 
        .attrs = adis16350_attributes,
585
 
};
586
 
 
587
 
static struct attribute *adis16350_event_attributes[] = {
588
 
        NULL,
589
 
};
590
 
 
591
 
static struct attribute_group adis16350_event_attribute_group = {
592
 
        .attrs = adis16350_event_attributes,
593
 
};
594
 
 
595
 
static int __devinit adis16350_probe(struct spi_device *spi)
596
 
{
597
 
        int ret, regdone = 0;
598
 
        struct adis16350_state *st = kzalloc(sizeof *st, GFP_KERNEL);
599
 
        if (!st) {
600
 
                ret =  -ENOMEM;
601
 
                goto error_ret;
602
 
        }
603
 
        /* this is only used for removal purposes */
604
 
        spi_set_drvdata(spi, st);
605
 
 
606
 
        /* Allocate the comms buffers */
607
 
        st->rx = kzalloc(sizeof(*st->rx)*ADIS16350_MAX_RX, GFP_KERNEL);
608
 
        if (st->rx == NULL) {
609
 
                ret = -ENOMEM;
610
 
                goto error_free_st;
611
 
        }
612
 
        st->tx = kzalloc(sizeof(*st->tx)*ADIS16350_MAX_TX, GFP_KERNEL);
613
 
        if (st->tx == NULL) {
614
 
                ret = -ENOMEM;
615
 
                goto error_free_rx;
616
 
        }
617
 
        st->us = spi;
618
 
        mutex_init(&st->buf_lock);
619
 
        /* setup the industrialio driver allocated elements */
620
 
        st->indio_dev = iio_allocate_device();
621
 
        if (st->indio_dev == NULL) {
622
 
                ret = -ENOMEM;
623
 
                goto error_free_tx;
624
 
        }
625
 
 
626
 
        st->indio_dev->dev.parent = &spi->dev;
627
 
        st->indio_dev->num_interrupt_lines = 1;
628
 
        st->indio_dev->event_attrs = &adis16350_event_attribute_group;
629
 
        st->indio_dev->attrs = &adis16350_attribute_group;
630
 
        st->indio_dev->dev_data = (void *)(st);
631
 
        st->indio_dev->driver_module = THIS_MODULE;
632
 
        st->indio_dev->modes = INDIO_DIRECT_MODE;
633
 
 
634
 
        ret = adis16350_configure_ring(st->indio_dev);
635
 
        if (ret)
636
 
                goto error_free_dev;
637
 
 
638
 
        ret = iio_device_register(st->indio_dev);
639
 
        if (ret)
640
 
                goto error_unreg_ring_funcs;
641
 
        regdone = 1;
642
 
 
643
 
        ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
644
 
        if (ret) {
645
 
                printk(KERN_ERR "failed to initialize the ring\n");
646
 
                goto error_unreg_ring_funcs;
647
 
        }
648
 
 
649
 
        if (spi->irq) {
650
 
                ret = iio_register_interrupt_line(spi->irq,
651
 
                                st->indio_dev,
652
 
                                0,
653
 
                                IRQF_TRIGGER_RISING,
654
 
                                "adis16350");
655
 
                if (ret)
656
 
                        goto error_uninitialize_ring;
657
 
 
658
 
                ret = adis16350_probe_trigger(st->indio_dev);
659
 
                if (ret)
660
 
                        goto error_unregister_line;
661
 
        }
662
 
 
663
 
        /* Get the device into a sane initial state */
664
 
        ret = adis16350_initial_setup(st);
665
 
        if (ret)
666
 
                goto error_remove_trigger;
667
 
        return 0;
668
 
 
669
 
error_remove_trigger:
670
 
        adis16350_remove_trigger(st->indio_dev);
671
 
error_unregister_line:
672
 
        if (spi->irq)
673
 
                iio_unregister_interrupt_line(st->indio_dev, 0);
674
 
error_uninitialize_ring:
675
 
        iio_ring_buffer_unregister(st->indio_dev->ring);
676
 
error_unreg_ring_funcs:
677
 
        adis16350_unconfigure_ring(st->indio_dev);
678
 
error_free_dev:
679
 
        if (regdone)
680
 
                iio_device_unregister(st->indio_dev);
681
 
        else
682
 
                iio_free_device(st->indio_dev);
683
 
error_free_tx:
684
 
        kfree(st->tx);
685
 
error_free_rx:
686
 
        kfree(st->rx);
687
 
error_free_st:
688
 
        kfree(st);
689
 
error_ret:
690
 
        return ret;
691
 
}
692
 
 
693
 
static int adis16350_remove(struct spi_device *spi)
694
 
{
695
 
        int ret;
696
 
        struct adis16350_state *st = spi_get_drvdata(spi);
697
 
        struct iio_dev *indio_dev = st->indio_dev;
698
 
 
699
 
        ret = adis16350_stop_device(&(indio_dev->dev));
700
 
        if (ret)
701
 
                goto err_ret;
702
 
 
703
 
        flush_scheduled_work();
704
 
 
705
 
        adis16350_remove_trigger(indio_dev);
706
 
        if (spi->irq)
707
 
                iio_unregister_interrupt_line(indio_dev, 0);
708
 
 
709
 
        iio_ring_buffer_unregister(indio_dev->ring);
710
 
        iio_device_unregister(indio_dev);
711
 
        adis16350_unconfigure_ring(indio_dev);
712
 
        kfree(st->tx);
713
 
        kfree(st->rx);
714
 
        kfree(st);
715
 
 
716
 
        return 0;
717
 
 
718
 
err_ret:
719
 
        return ret;
720
 
}
721
 
 
722
 
static const struct spi_device_id adis16350_id[] = {
723
 
        {"adis16350", 0},
724
 
        {"adis16354", 0},
725
 
        {"adis16355", 0},
726
 
        {"adis16360", 0},
727
 
        {"adis16362", 0},
728
 
        {"adis16364", 0},
729
 
        {"adis16365", 0},
730
 
        {}
731
 
};
732
 
 
733
 
static struct spi_driver adis16350_driver = {
734
 
        .driver = {
735
 
                .name = "adis16350",
736
 
                .owner = THIS_MODULE,
737
 
        },
738
 
        .probe = adis16350_probe,
739
 
        .remove = __devexit_p(adis16350_remove),
740
 
        .id_table = adis16350_id,
741
 
};
742
 
 
743
 
static __init int adis16350_init(void)
744
 
{
745
 
        return spi_register_driver(&adis16350_driver);
746
 
}
747
 
module_init(adis16350_init);
748
 
 
749
 
static __exit void adis16350_exit(void)
750
 
{
751
 
        spi_unregister_driver(&adis16350_driver);
752
 
}
753
 
module_exit(adis16350_exit);
754
 
 
755
 
MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
756
 
MODULE_DESCRIPTION("Analog Devices ADIS16350/54/55/60/62/64/65 IMU SPI driver");
757
 
MODULE_LICENSE("GPL v2");