IIO: GYRO: ADXRS450: Add missing parity bit generation
[pandora-kernel.git] / drivers / staging / iio / gyro / adxrs450_core.c
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         mutex_unlock(&st->buf_lock);
120         return ret;
121 }
122
123 /**
124  * adxrs450_spi_sensor_data() - read 2 bytes sensor data
125  * @dev: device associated with child of actual iio_dev
126  * @val: somewhere to pass back the value read
127  **/
128 static int adxrs450_spi_sensor_data(struct device *dev, u16 *val)
129 {
130         struct spi_message msg;
131         struct iio_dev *indio_dev = dev_get_drvdata(dev);
132         struct adxrs450_state *st = iio_dev_get_devdata(indio_dev);
133         int ret;
134         struct spi_transfer xfers[] = {
135                 {
136                         .tx_buf = st->tx,
137                         .bits_per_word = 8,
138                         .len = 4,
139                         .cs_change = 1,
140                 }, {
141                         .rx_buf = st->rx,
142                         .bits_per_word = 8,
143                         .len = 4,
144                 },
145         };
146
147         mutex_lock(&st->buf_lock);
148         st->tx[0] = ADXRS450_SENSOR_DATA;
149         st->tx[1] = 0;
150         st->tx[2] = 0;
151         st->tx[3] = 0;
152
153         spi_message_init(&msg);
154         spi_message_add_tail(&xfers[0], &msg);
155         spi_message_add_tail(&xfers[1], &msg);
156         ret = spi_sync(st->us, &msg);
157         if (ret) {
158                 dev_err(&st->us->dev, "Problem while reading sensor data\n");
159                 goto error_ret;
160         }
161
162         *val = (be32_to_cpu(*(u32 *)st->rx) >> 10) & 0xFFFF;
163
164 error_ret:
165         mutex_unlock(&st->buf_lock);
166         return ret;
167 }
168
169 /**
170  * adxrs450_spi_initial() - use for initializing procedure.
171  * @st: device instance specific data
172  * @val: somewhere to pass back the value read
173  **/
174 static int adxrs450_spi_initial(struct adxrs450_state *st,
175                 u32 *val, char chk)
176 {
177         struct spi_message msg;
178         int ret;
179         struct spi_transfer xfers = {
180                 .tx_buf = st->tx,
181                 .rx_buf = st->rx,
182                 .bits_per_word = 8,
183                 .len = 4,
184         };
185
186         mutex_lock(&st->buf_lock);
187         st->tx[0] = ADXRS450_SENSOR_DATA;
188         st->tx[1] = 0;
189         st->tx[2] = 0;
190         st->tx[3] = 0;
191         if (chk)
192                 st->tx[3] |= (ADXRS450_CHK | ADXRS450_P);
193         spi_message_init(&msg);
194         spi_message_add_tail(&xfers, &msg);
195         ret = spi_sync(st->us, &msg);
196         if (ret) {
197                 dev_err(&st->us->dev, "Problem while reading initializing data\n");
198                 goto error_ret;
199         }
200
201         *val = be32_to_cpu(*(u32 *)st->rx);
202
203 error_ret:
204         mutex_unlock(&st->buf_lock);
205         return ret;
206 }
207
208 static ssize_t adxrs450_read_temp(struct device *dev,
209                 struct device_attribute *attr,
210                 char *buf)
211 {
212         int ret;
213         u16 t;
214         ret = adxrs450_spi_read_reg_16(dev,
215                         ADXRS450_TEMP1,
216                         &t);
217         if (ret)
218                 return ret;
219         return sprintf(buf, "%d\n", t);
220 }
221
222 static ssize_t adxrs450_read_quad(struct device *dev,
223                 struct device_attribute *attr,
224                 char *buf)
225 {
226         int ret;
227         u16 t;
228         ret = adxrs450_spi_read_reg_16(dev,
229                         ADXRS450_QUAD1,
230                         &t);
231         if (ret)
232                 return ret;
233         return sprintf(buf, "%d\n", t);
234 }
235
236 static ssize_t adxrs450_write_dnc(struct device *dev,
237                 struct device_attribute *attr,
238                 const char *buf,
239                 size_t len)
240 {
241         int ret;
242         long val;
243
244         ret = strict_strtol(buf, 10, &val);
245         if (ret)
246                 goto error_ret;
247         ret = adxrs450_spi_write_reg_16(dev,
248                         ADXRS450_DNC1,
249                         val);
250 error_ret:
251         return ret ? ret : len;
252 }
253
254 static ssize_t adxrs450_read_sensor_data(struct device *dev,
255                 struct device_attribute *attr,
256                 char *buf)
257 {
258         int ret;
259         u16 t;
260
261         ret = adxrs450_spi_sensor_data(dev, &t);
262         if (ret)
263                 return ret;
264
265         return sprintf(buf, "%d\n", t);
266 }
267
268 /* Recommended Startup Sequence by spec */
269 static int adxrs450_initial_setup(struct adxrs450_state *st)
270 {
271         u32 t;
272         u16 data;
273         int ret;
274         struct device *dev = &st->indio_dev->dev;
275
276         msleep(ADXRS450_STARTUP_DELAY*2);
277         ret = adxrs450_spi_initial(st, &t, 1);
278         if (ret)
279                 return ret;
280         if (t != 0x01) {
281                 dev_err(&st->us->dev, "The initial response is not correct!\n");
282                 return -ENODEV;
283
284         }
285
286         msleep(ADXRS450_STARTUP_DELAY);
287         ret = adxrs450_spi_initial(st, &t, 0);
288         if (ret)
289                 return ret;
290
291         msleep(ADXRS450_STARTUP_DELAY);
292         ret = adxrs450_spi_initial(st, &t, 0);
293         if (ret)
294                 return ret;
295         if (((t & 0xff) | 0x01) != 0xff || ADXRS450_GET_ST(t) != 2) {
296                 dev_err(&st->us->dev, "The second response is not correct!\n");
297                 return -EIO;
298
299         }
300         ret = adxrs450_spi_initial(st, &t, 0);
301         if (ret)
302                 return ret;
303         if (((t & 0xff) | 0x01) != 0xff || ADXRS450_GET_ST(t) != 2) {
304                 dev_err(&st->us->dev, "The third response is not correct!\n");
305                 return -EIO;
306
307         }
308         ret = adxrs450_spi_read_reg_16(dev, ADXRS450_FAULT1, &data);
309         if (ret)
310                 return ret;
311         if (data & 0x0fff) {
312                 dev_err(&st->us->dev, "The device is not in normal status!\n");
313                 return -EINVAL;
314         }
315         ret = adxrs450_spi_read_reg_16(dev, ADXRS450_PID1, &data);
316         if (ret)
317                 return ret;
318         dev_info(&st->us->dev, "The Part ID is 0x%x\n", data);
319
320         ret = adxrs450_spi_read_reg_16(dev, ADXRS450_SNL, &data);
321         if (ret)
322                 return ret;
323         t = data;
324         ret = adxrs450_spi_read_reg_16(dev, ADXRS450_SNH, &data);
325         if (ret)
326                 return ret;
327         t |= data << 16;
328         dev_info(&st->us->dev, "The Serial Number is 0x%x\n", t);
329
330         return 0;
331 }
332
333 static IIO_DEV_ATTR_GYRO_Z(adxrs450_read_sensor_data, 0);
334 static IIO_DEV_ATTR_TEMP_RAW(adxrs450_read_temp);
335 static IIO_DEV_ATTR_GYRO_Z_QUADRATURE_CORRECTION(adxrs450_read_quad, 0);
336 static IIO_DEV_ATTR_GYRO_Z_CALIBBIAS(S_IWUSR,
337                 NULL, adxrs450_write_dnc, 0);
338 static IIO_CONST_ATTR(name, "adxrs450");
339
340 static struct attribute *adxrs450_attributes[] = {
341         &iio_dev_attr_gyro_z_raw.dev_attr.attr,
342         &iio_dev_attr_temp_raw.dev_attr.attr,
343         &iio_dev_attr_gyro_z_quadrature_correction_raw.dev_attr.attr,
344         &iio_dev_attr_gyro_z_calibbias.dev_attr.attr,
345         &iio_const_attr_name.dev_attr.attr,
346         NULL
347 };
348
349 static const struct attribute_group adxrs450_attribute_group = {
350         .attrs = adxrs450_attributes,
351 };
352
353 static int __devinit adxrs450_probe(struct spi_device *spi)
354 {
355         int ret, regdone = 0;
356         struct adxrs450_state *st = kzalloc(sizeof *st, GFP_KERNEL);
357         if (!st) {
358                 ret =  -ENOMEM;
359                 goto error_ret;
360         }
361         /* This is only used for removal purposes */
362         spi_set_drvdata(spi, st);
363
364         /* Allocate the comms buffers */
365         st->rx = kzalloc(sizeof(*st->rx)*ADXRS450_MAX_RX, GFP_KERNEL);
366         if (st->rx == NULL) {
367                 ret = -ENOMEM;
368                 goto error_free_st;
369         }
370         st->tx = kzalloc(sizeof(*st->tx)*ADXRS450_MAX_TX, GFP_KERNEL);
371         if (st->tx == NULL) {
372                 ret = -ENOMEM;
373                 goto error_free_rx;
374         }
375         st->us = spi;
376         mutex_init(&st->buf_lock);
377         /* setup the industrialio driver allocated elements */
378         st->indio_dev = iio_allocate_device(0);
379         if (st->indio_dev == NULL) {
380                 ret = -ENOMEM;
381                 goto error_free_tx;
382         }
383
384         st->indio_dev->dev.parent = &spi->dev;
385         st->indio_dev->attrs = &adxrs450_attribute_group;
386         st->indio_dev->dev_data = (void *)(st);
387         st->indio_dev->driver_module = THIS_MODULE;
388         st->indio_dev->modes = INDIO_DIRECT_MODE;
389
390         ret = iio_device_register(st->indio_dev);
391         if (ret)
392                 goto error_free_dev;
393         regdone = 1;
394
395         /* Get the device into a sane initial state */
396         ret = adxrs450_initial_setup(st);
397         if (ret)
398                 goto error_initial;
399         return 0;
400
401 error_initial:
402 error_free_dev:
403         if (regdone)
404                 iio_device_unregister(st->indio_dev);
405         else
406                 iio_free_device(st->indio_dev);
407 error_free_tx:
408         kfree(st->tx);
409 error_free_rx:
410         kfree(st->rx);
411 error_free_st:
412         kfree(st);
413 error_ret:
414         return ret;
415 }
416
417 static int adxrs450_remove(struct spi_device *spi)
418 {
419         struct adxrs450_state *st = spi_get_drvdata(spi);
420
421         iio_device_unregister(st->indio_dev);
422         kfree(st->tx);
423         kfree(st->rx);
424         kfree(st);
425
426         return 0;
427 }
428
429 static struct spi_driver adxrs450_driver = {
430         .driver = {
431                 .name = "adxrs450",
432                 .owner = THIS_MODULE,
433         },
434         .probe = adxrs450_probe,
435         .remove = __devexit_p(adxrs450_remove),
436 };
437
438 static __init int adxrs450_init(void)
439 {
440         return spi_register_driver(&adxrs450_driver);
441 }
442 module_init(adxrs450_init);
443
444 static __exit void adxrs450_exit(void)
445 {
446         spi_unregister_driver(&adxrs450_driver);
447 }
448 module_exit(adxrs450_exit);
449
450 MODULE_AUTHOR("Cliff Cai <cliff.cai@xxxxxxxxxx>");
451 MODULE_DESCRIPTION("Analog Devices ADXRS450 Gyroscope SPI driver");
452 MODULE_LICENSE("GPL v2");