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

« back to all changes in this revision

Viewing changes to drivers/staging/iio/gyro/adxrs450_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
 * ADXRS450 Digital Output Gyroscope Driver
 
3
 *
 
4
 * Copyright 2011 Analog Devices Inc.
 
5
 *
 
6
 * Licensed under the GPL-2.
 
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 "gyro.h"
 
24
#include "../adc/adc.h"
 
25
 
 
26
#include "adxrs450.h"
 
27
 
 
28
/**
 
29
 * adxrs450_spi_read_reg_16() - read 2 bytes from a register pair
 
30
 * @dev: device associated with child of actual iio_dev
 
31
 * @reg_address: the address of the lower of the two registers,which should be an even address,
 
32
 * Second register's address is reg_address + 1.
 
33
 * @val: somewhere to pass back the value read
 
34
 **/
 
35
static int adxrs450_spi_read_reg_16(struct device *dev,
 
36
                u8 reg_address,
 
37
                u16 *val)
 
38
{
 
39
        struct spi_message msg;
 
40
        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 
41
        struct adxrs450_state *st = iio_dev_get_devdata(indio_dev);
 
42
        int ret;
 
43
        struct spi_transfer xfers[] = {
 
44
                {
 
45
                        .tx_buf = st->tx,
 
46
                        .bits_per_word = 8,
 
47
                        .len = 4,
 
48
                        .cs_change = 1,
 
49
                }, {
 
50
                        .rx_buf = st->rx,
 
51
                        .bits_per_word = 8,
 
52
                        .len = 4,
 
53
                },
 
54
        };
 
55
 
 
56
        mutex_lock(&st->buf_lock);
 
57
        st->tx[0] = ADXRS450_READ_DATA | (reg_address >> 7);
 
58
        st->tx[1] = reg_address << 1;
 
59
        st->tx[2] = 0;
 
60
        st->tx[3] = 0;
 
61
 
 
62
        if (!(hweight32(be32_to_cpu(*(u32 *)st->tx)) & 1))
 
63
                st->tx[3]  |= ADXRS450_P;
 
64
 
 
65
        spi_message_init(&msg);
 
66
        spi_message_add_tail(&xfers[0], &msg);
 
67
        spi_message_add_tail(&xfers[1], &msg);
 
68
        ret = spi_sync(st->us, &msg);
 
69
        if (ret) {
 
70
                dev_err(&st->us->dev, "problem while reading 16 bit register 0x%02x\n",
 
71
                                reg_address);
 
72
                goto error_ret;
 
73
        }
 
74
 
 
75
        *val = (be32_to_cpu(*(u32 *)st->rx) >> 5) & 0xFFFF;
 
76
 
 
77
error_ret:
 
78
        mutex_unlock(&st->buf_lock);
 
79
        return ret;
 
80
}
 
81
 
 
82
/**
 
83
 * adxrs450_spi_write_reg_16() - write 2 bytes data to a register pair
 
84
 * @dev: device associated with child of actual actual iio_dev
 
85
 * @reg_address: the address of the lower of the two registers,which should be an even address,
 
86
 * Second register's address is reg_address + 1.
 
87
 * @val: value to be written.
 
88
 **/
 
89
static int adxrs450_spi_write_reg_16(struct device *dev,
 
90
                u8 reg_address,
 
91
                u16 val)
 
92
{
 
93
        struct spi_message msg;
 
94
        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 
95
        struct adxrs450_state *st = iio_dev_get_devdata(indio_dev);
 
96
        int ret;
 
97
        struct spi_transfer xfers = {
 
98
                .tx_buf = st->tx,
 
99
                .rx_buf = st->rx,
 
100
                .bits_per_word = 8,
 
101
                .len = 4,
 
102
        };
 
103
 
 
104
        mutex_lock(&st->buf_lock);
 
105
        st->tx[0] = ADXRS450_WRITE_DATA | reg_address >> 7;
 
106
        st->tx[1] = reg_address << 1 | val >> 15;
 
107
        st->tx[2] = val >> 7;
 
108
        st->tx[3] = val << 1;
 
109
 
 
110
        if (!(hweight32(be32_to_cpu(*(u32 *)st->tx)) & 1))
 
111
                st->tx[3]  |= ADXRS450_P;
 
112
 
 
113
        spi_message_init(&msg);
 
114
        spi_message_add_tail(&xfers, &msg);
 
115
        ret = spi_sync(st->us, &msg);
 
116
        if (ret)
 
117
                dev_err(&st->us->dev, "problem while writing 16 bit register 0x%02x\n",
 
118
                                reg_address);
 
119
        msleep(1); /* enforce sequential transfer delay 0.1ms */
 
120
        mutex_unlock(&st->buf_lock);
 
121
        return ret;
 
122
}
 
123
 
 
124
/**
 
125
 * adxrs450_spi_sensor_data() - read 2 bytes sensor data
 
126
 * @dev: device associated with child of actual iio_dev
 
127
 * @val: somewhere to pass back the value read
 
128
 **/
 
129
static int adxrs450_spi_sensor_data(struct device *dev, s16 *val)
 
130
{
 
131
        struct spi_message msg;
 
132
        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 
133
        struct adxrs450_state *st = iio_dev_get_devdata(indio_dev);
 
134
        int ret;
 
135
        struct spi_transfer xfers[] = {
 
136
                {
 
137
                        .tx_buf = st->tx,
 
138
                        .bits_per_word = 8,
 
139
                        .len = 4,
 
140
                        .cs_change = 1,
 
141
                }, {
 
142
                        .rx_buf = st->rx,
 
143
                        .bits_per_word = 8,
 
144
                        .len = 4,
 
145
                },
 
146
        };
 
147
 
 
148
        mutex_lock(&st->buf_lock);
 
149
        st->tx[0] = ADXRS450_SENSOR_DATA;
 
150
        st->tx[1] = 0;
 
151
        st->tx[2] = 0;
 
152
        st->tx[3] = 0;
 
153
 
 
154
        spi_message_init(&msg);
 
155
        spi_message_add_tail(&xfers[0], &msg);
 
156
        spi_message_add_tail(&xfers[1], &msg);
 
157
        ret = spi_sync(st->us, &msg);
 
158
        if (ret) {
 
159
                dev_err(&st->us->dev, "Problem while reading sensor data\n");
 
160
                goto error_ret;
 
161
        }
 
162
 
 
163
        *val = (be32_to_cpu(*(u32 *)st->rx) >> 10) & 0xFFFF;
 
164
 
 
165
error_ret:
 
166
        mutex_unlock(&st->buf_lock);
 
167
        return ret;
 
168
}
 
169
 
 
170
/**
 
171
 * adxrs450_spi_initial() - use for initializing procedure.
 
172
 * @st: device instance specific data
 
173
 * @val: somewhere to pass back the value read
 
174
 **/
 
175
static int adxrs450_spi_initial(struct adxrs450_state *st,
 
176
                u32 *val, char chk)
 
177
{
 
178
        struct spi_message msg;
 
179
        int ret;
 
180
        struct spi_transfer xfers = {
 
181
                .tx_buf = st->tx,
 
182
                .rx_buf = st->rx,
 
183
                .bits_per_word = 8,
 
184
                .len = 4,
 
185
        };
 
186
 
 
187
        mutex_lock(&st->buf_lock);
 
188
        st->tx[0] = ADXRS450_SENSOR_DATA;
 
189
        st->tx[1] = 0;
 
190
        st->tx[2] = 0;
 
191
        st->tx[3] = 0;
 
192
        if (chk)
 
193
                st->tx[3] |= (ADXRS450_CHK | ADXRS450_P);
 
194
        spi_message_init(&msg);
 
195
        spi_message_add_tail(&xfers, &msg);
 
196
        ret = spi_sync(st->us, &msg);
 
197
        if (ret) {
 
198
                dev_err(&st->us->dev, "Problem while reading initializing data\n");
 
199
                goto error_ret;
 
200
        }
 
201
 
 
202
        *val = be32_to_cpu(*(u32 *)st->rx);
 
203
 
 
204
error_ret:
 
205
        mutex_unlock(&st->buf_lock);
 
206
        return ret;
 
207
}
 
208
 
 
209
static ssize_t adxrs450_read_temp(struct device *dev,
 
210
                struct device_attribute *attr,
 
211
                char *buf)
 
212
{
 
213
        int ret;
 
214
        u16 t;
 
215
        ret = adxrs450_spi_read_reg_16(dev,
 
216
                        ADXRS450_TEMP1,
 
217
                        &t);
 
218
        if (ret)
 
219
                return ret;
 
220
        return sprintf(buf, "%d\n", t >> 7);
 
221
}
 
222
 
 
223
static ssize_t adxrs450_read_quad(struct device *dev,
 
224
                struct device_attribute *attr,
 
225
                char *buf)
 
226
{
 
227
        int ret;
 
228
        s16 t;
 
229
        ret = adxrs450_spi_read_reg_16(dev,
 
230
                        ADXRS450_QUAD1,
 
231
                        &t);
 
232
        if (ret)
 
233
                return ret;
 
234
        return sprintf(buf, "%d\n", t);
 
235
}
 
236
 
 
237
static ssize_t adxrs450_write_dnc(struct device *dev,
 
238
                struct device_attribute *attr,
 
239
                const char *buf,
 
240
                size_t len)
 
241
{
 
242
        int ret;
 
243
        long val;
 
244
 
 
245
        ret = strict_strtol(buf, 10, &val);
 
246
        if (ret)
 
247
                goto error_ret;
 
248
        ret = adxrs450_spi_write_reg_16(dev,
 
249
                        ADXRS450_DNC1,
 
250
                        val & 0x3FF);
 
251
error_ret:
 
252
        return ret ? ret : len;
 
253
}
 
254
 
 
255
static ssize_t adxrs450_read_sensor_data(struct device *dev,
 
256
                struct device_attribute *attr,
 
257
                char *buf)
 
258
{
 
259
        int ret;
 
260
        s16 t;
 
261
 
 
262
        ret = adxrs450_spi_sensor_data(dev, &t);
 
263
        if (ret)
 
264
                return ret;
 
265
 
 
266
        return sprintf(buf, "%d\n", t);
 
267
}
 
268
 
 
269
/* Recommended Startup Sequence by spec */
 
270
static int adxrs450_initial_setup(struct adxrs450_state *st)
 
271
{
 
272
        u32 t;
 
273
        u16 data;
 
274
        int ret;
 
275
        struct device *dev = &st->indio_dev->dev;
 
276
 
 
277
        msleep(ADXRS450_STARTUP_DELAY*2);
 
278
        ret = adxrs450_spi_initial(st, &t, 1);
 
279
        if (ret)
 
280
                return ret;
 
281
        if (t != 0x01)
 
282
                dev_warn(&st->us->dev, "The initial power on response "
 
283
                         "is not correct! Restart without reset?\n");
 
284
 
 
285
        msleep(ADXRS450_STARTUP_DELAY);
 
286
        ret = adxrs450_spi_initial(st, &t, 0);
 
287
        if (ret)
 
288
                return ret;
 
289
 
 
290
        msleep(ADXRS450_STARTUP_DELAY);
 
291
        ret = adxrs450_spi_initial(st, &t, 0);
 
292
        if (ret)
 
293
                return ret;
 
294
        if (((t & 0xff) | 0x01) != 0xff || ADXRS450_GET_ST(t) != 2) {
 
295
                dev_err(&st->us->dev, "The second response is not correct!\n");
 
296
                return -EIO;
 
297
 
 
298
        }
 
299
        ret = adxrs450_spi_initial(st, &t, 0);
 
300
        if (ret)
 
301
                return ret;
 
302
        if (((t & 0xff) | 0x01) != 0xff || ADXRS450_GET_ST(t) != 2) {
 
303
                dev_err(&st->us->dev, "The third response is not correct!\n");
 
304
                return -EIO;
 
305
 
 
306
        }
 
307
        ret = adxrs450_spi_read_reg_16(dev, ADXRS450_FAULT1, &data);
 
308
        if (ret)
 
309
                return ret;
 
310
        if (data & 0x0fff) {
 
311
                dev_err(&st->us->dev, "The device is not in normal status!\n");
 
312
                return -EINVAL;
 
313
        }
 
314
        ret = adxrs450_spi_read_reg_16(dev, ADXRS450_PID1, &data);
 
315
        if (ret)
 
316
                return ret;
 
317
        dev_info(&st->us->dev, "The Part ID is 0x%x\n", data);
 
318
 
 
319
        ret = adxrs450_spi_read_reg_16(dev, ADXRS450_SNL, &data);
 
320
        if (ret)
 
321
                return ret;
 
322
        t = data;
 
323
        ret = adxrs450_spi_read_reg_16(dev, ADXRS450_SNH, &data);
 
324
        if (ret)
 
325
                return ret;
 
326
        t |= data << 16;
 
327
        dev_info(&st->us->dev, "The Serial Number is 0x%x\n", t);
 
328
 
 
329
        return 0;
 
330
}
 
331
 
 
332
static IIO_DEV_ATTR_GYRO_Z(adxrs450_read_sensor_data, 0);
 
333
static IIO_DEV_ATTR_TEMP_RAW(adxrs450_read_temp);
 
334
static IIO_DEV_ATTR_GYRO_Z_QUADRATURE_CORRECTION(adxrs450_read_quad, 0);
 
335
static IIO_DEV_ATTR_GYRO_Z_CALIBBIAS(S_IWUSR,
 
336
                NULL, adxrs450_write_dnc, 0);
 
337
static IIO_CONST_ATTR(name, "adxrs450");
 
338
 
 
339
static struct attribute *adxrs450_attributes[] = {
 
340
        &iio_dev_attr_gyro_z_raw.dev_attr.attr,
 
341
        &iio_dev_attr_temp_raw.dev_attr.attr,
 
342
        &iio_dev_attr_gyro_z_quadrature_correction_raw.dev_attr.attr,
 
343
        &iio_dev_attr_gyro_z_calibbias.dev_attr.attr,
 
344
        &iio_const_attr_name.dev_attr.attr,
 
345
        NULL
 
346
};
 
347
 
 
348
static const struct attribute_group adxrs450_attribute_group = {
 
349
        .attrs = adxrs450_attributes,
 
350
};
 
351
 
 
352
static const struct iio_info adxrs450_info = {
 
353
        .attrs = &adxrs450_attribute_group,
 
354
        .driver_module = THIS_MODULE,
 
355
};
 
356
 
 
357
static int __devinit adxrs450_probe(struct spi_device *spi)
 
358
{
 
359
        int ret, regdone = 0;
 
360
        struct adxrs450_state *st = kzalloc(sizeof *st, GFP_KERNEL);
 
361
        if (!st) {
 
362
                ret =  -ENOMEM;
 
363
                goto error_ret;
 
364
        }
 
365
        /* This is only used for removal purposes */
 
366
        spi_set_drvdata(spi, st);
 
367
 
 
368
        /* Allocate the comms buffers */
 
369
        st->rx = kzalloc(sizeof(*st->rx)*ADXRS450_MAX_RX, GFP_KERNEL);
 
370
        if (st->rx == NULL) {
 
371
                ret = -ENOMEM;
 
372
                goto error_free_st;
 
373
        }
 
374
        st->tx = kzalloc(sizeof(*st->tx)*ADXRS450_MAX_TX, GFP_KERNEL);
 
375
        if (st->tx == NULL) {
 
376
                ret = -ENOMEM;
 
377
                goto error_free_rx;
 
378
        }
 
379
        st->us = spi;
 
380
        mutex_init(&st->buf_lock);
 
381
        /* setup the industrialio driver allocated elements */
 
382
        st->indio_dev = iio_allocate_device(0);
 
383
        if (st->indio_dev == NULL) {
 
384
                ret = -ENOMEM;
 
385
                goto error_free_tx;
 
386
        }
 
387
 
 
388
        st->indio_dev->dev.parent = &spi->dev;
 
389
        st->indio_dev->info = &adxrs450_info;
 
390
        st->indio_dev->dev_data = (void *)(st);
 
391
        st->indio_dev->modes = INDIO_DIRECT_MODE;
 
392
 
 
393
        ret = iio_device_register(st->indio_dev);
 
394
        if (ret)
 
395
                goto error_free_dev;
 
396
        regdone = 1;
 
397
 
 
398
        /* Get the device into a sane initial state */
 
399
        ret = adxrs450_initial_setup(st);
 
400
        if (ret)
 
401
                goto error_initial;
 
402
        return 0;
 
403
 
 
404
error_initial:
 
405
error_free_dev:
 
406
        if (regdone)
 
407
                iio_device_unregister(st->indio_dev);
 
408
        else
 
409
                iio_free_device(st->indio_dev);
 
410
error_free_tx:
 
411
        kfree(st->tx);
 
412
error_free_rx:
 
413
        kfree(st->rx);
 
414
error_free_st:
 
415
        kfree(st);
 
416
error_ret:
 
417
        return ret;
 
418
}
 
419
 
 
420
static int adxrs450_remove(struct spi_device *spi)
 
421
{
 
422
        struct adxrs450_state *st = spi_get_drvdata(spi);
 
423
 
 
424
        iio_device_unregister(st->indio_dev);
 
425
        kfree(st->tx);
 
426
        kfree(st->rx);
 
427
        kfree(st);
 
428
 
 
429
        return 0;
 
430
}
 
431
 
 
432
static struct spi_driver adxrs450_driver = {
 
433
        .driver = {
 
434
                .name = "adxrs450",
 
435
                .owner = THIS_MODULE,
 
436
        },
 
437
        .probe = adxrs450_probe,
 
438
        .remove = __devexit_p(adxrs450_remove),
 
439
};
 
440
 
 
441
static __init int adxrs450_init(void)
 
442
{
 
443
        return spi_register_driver(&adxrs450_driver);
 
444
}
 
445
module_init(adxrs450_init);
 
446
 
 
447
static __exit void adxrs450_exit(void)
 
448
{
 
449
        spi_unregister_driver(&adxrs450_driver);
 
450
}
 
451
module_exit(adxrs450_exit);
 
452
 
 
453
MODULE_AUTHOR("Cliff Cai <cliff.cai@xxxxxxxxxx>");
 
454
MODULE_DESCRIPTION("Analog Devices ADXRS450 Gyroscope SPI driver");
 
455
MODULE_LICENSE("GPL v2");