IIO: GYRO: ADXRS450: Cleanup result extraction and update license notice
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / gyro / adxrs450_core.c
blobc02e3ef40a0fc6f02ac1010a691a2f206409243d
1 /*
2 * ADXRS450 Digital Output Gyroscope Driver
4 * Copyright 2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
7 */
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>
21 #include "../iio.h"
22 #include "../sysfs.h"
23 #include "gyro.h"
24 #include "../adc/adc.h"
26 #include "adxrs450.h"
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)
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[] = {
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,
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;
62 spi_message_init(&msg);
63 spi_message_add_tail(&xfers[0], &msg);
64 spi_message_add_tail(&xfers[1], &msg);
65 ret = spi_sync(st->us, &msg);
66 if (ret) {
67 dev_err(&st->us->dev, "problem while reading 16 bit register 0x%02x\n",
68 reg_address);
69 goto error_ret;
72 *val = (be32_to_cpu(*(u32 *)st->rx) >> 5) & 0xFFFF;
74 error_ret:
75 mutex_unlock(&st->buf_lock);
76 return ret;
79 /**
80 * adxrs450_spi_write_reg_16() - write 2 bytes data to a register pair
81 * @dev: device associated with child of actual actual iio_dev
82 * @reg_address: the address of the lower of the two registers,which should be an even address,
83 * Second register's address is reg_address + 1.
84 * @val: value to be written.
85 **/
86 static int adxrs450_spi_write_reg_16(struct device *dev,
87 u8 reg_address,
88 u16 val)
90 struct spi_message msg;
91 struct iio_dev *indio_dev = dev_get_drvdata(dev);
92 struct adxrs450_state *st = iio_dev_get_devdata(indio_dev);
93 int ret;
94 struct spi_transfer xfers = {
95 .tx_buf = st->tx,
96 .rx_buf = st->rx,
97 .bits_per_word = 8,
98 .len = 4,
101 mutex_lock(&st->buf_lock);
102 st->tx[0] = ADXRS450_WRITE_DATA | reg_address >> 7;
103 st->tx[1] = reg_address << 1 | val >> 15;
104 st->tx[2] = val >> 7;
105 st->tx[3] = val << 1;
106 spi_message_init(&msg);
107 spi_message_add_tail(&xfers, &msg);
108 ret = spi_sync(st->us, &msg);
109 if (ret)
110 dev_err(&st->us->dev, "problem while writing 16 bit register 0x%02x\n",
111 reg_address);
112 mutex_unlock(&st->buf_lock);
113 return ret;
117 * adxrs450_spi_sensor_data() - read 2 bytes sensor data
118 * @dev: device associated with child of actual iio_dev
119 * @val: somewhere to pass back the value read
121 static int adxrs450_spi_sensor_data(struct device *dev, u16 *val)
123 struct spi_message msg;
124 struct iio_dev *indio_dev = dev_get_drvdata(dev);
125 struct adxrs450_state *st = iio_dev_get_devdata(indio_dev);
126 int ret;
127 struct spi_transfer xfers[] = {
129 .tx_buf = st->tx,
130 .bits_per_word = 8,
131 .len = 4,
132 .cs_change = 1,
133 }, {
134 .rx_buf = st->rx,
135 .bits_per_word = 8,
136 .len = 4,
140 mutex_lock(&st->buf_lock);
141 st->tx[0] = ADXRS450_SENSOR_DATA;
142 st->tx[1] = 0;
143 st->tx[2] = 0;
144 st->tx[3] = 0;
146 spi_message_init(&msg);
147 spi_message_add_tail(&xfers[0], &msg);
148 spi_message_add_tail(&xfers[1], &msg);
149 ret = spi_sync(st->us, &msg);
150 if (ret) {
151 dev_err(&st->us->dev, "Problem while reading sensor data\n");
152 goto error_ret;
155 *val = (be32_to_cpu(*(u32 *)st->rx) >> 10) & 0xFFFF;
157 error_ret:
158 mutex_unlock(&st->buf_lock);
159 return ret;
163 * adxrs450_spi_initial() - use for initializing procedure.
164 * @st: device instance specific data
165 * @val: somewhere to pass back the value read
167 static int adxrs450_spi_initial(struct adxrs450_state *st,
168 u32 *val, char chk)
170 struct spi_message msg;
171 int ret;
172 struct spi_transfer xfers = {
173 .tx_buf = st->tx,
174 .rx_buf = st->rx,
175 .bits_per_word = 8,
176 .len = 4,
179 mutex_lock(&st->buf_lock);
180 st->tx[0] = ADXRS450_SENSOR_DATA;
181 st->tx[1] = 0;
182 st->tx[2] = 0;
183 st->tx[3] = 0;
184 if (chk)
185 st->tx[3] |= (ADXRS450_CHK | ADXRS450_P);
186 spi_message_init(&msg);
187 spi_message_add_tail(&xfers, &msg);
188 ret = spi_sync(st->us, &msg);
189 if (ret) {
190 dev_err(&st->us->dev, "Problem while reading initializing data\n");
191 goto error_ret;
194 *val = be32_to_cpu(*(u32 *)st->rx);
196 error_ret:
197 mutex_unlock(&st->buf_lock);
198 return ret;
201 static ssize_t adxrs450_read_temp(struct device *dev,
202 struct device_attribute *attr,
203 char *buf)
205 int ret;
206 u16 t;
207 ret = adxrs450_spi_read_reg_16(dev,
208 ADXRS450_TEMP1,
209 &t);
210 if (ret)
211 return ret;
212 return sprintf(buf, "%d\n", t);
215 static ssize_t adxrs450_read_quad(struct device *dev,
216 struct device_attribute *attr,
217 char *buf)
219 int ret;
220 u16 t;
221 ret = adxrs450_spi_read_reg_16(dev,
222 ADXRS450_QUAD1,
223 &t);
224 if (ret)
225 return ret;
226 return sprintf(buf, "%d\n", t);
229 static ssize_t adxrs450_write_dnc(struct device *dev,
230 struct device_attribute *attr,
231 const char *buf,
232 size_t len)
234 int ret;
235 long val;
237 ret = strict_strtol(buf, 10, &val);
238 if (ret)
239 goto error_ret;
240 ret = adxrs450_spi_write_reg_16(dev,
241 ADXRS450_DNC1,
242 val);
243 error_ret:
244 return ret ? ret : len;
247 static ssize_t adxrs450_read_sensor_data(struct device *dev,
248 struct device_attribute *attr,
249 char *buf)
251 int ret;
252 u16 t;
254 ret = adxrs450_spi_sensor_data(dev, &t);
255 if (ret)
256 return ret;
258 return sprintf(buf, "%d\n", t);
261 /* Recommended Startup Sequence by spec */
262 static int adxrs450_initial_setup(struct adxrs450_state *st)
264 u32 t;
265 u16 data;
266 int ret;
267 struct device *dev = &st->indio_dev->dev;
269 msleep(ADXRS450_STARTUP_DELAY*2);
270 ret = adxrs450_spi_initial(st, &t, 1);
271 if (ret)
272 return ret;
273 if (t != 0x01) {
274 dev_err(&st->us->dev, "The initial response is not correct!\n");
275 return -ENODEV;
279 msleep(ADXRS450_STARTUP_DELAY);
280 ret = adxrs450_spi_initial(st, &t, 0);
281 if (ret)
282 return ret;
284 msleep(ADXRS450_STARTUP_DELAY);
285 ret = adxrs450_spi_initial(st, &t, 0);
286 if (ret)
287 return ret;
288 if (((t & 0xff) | 0x01) != 0xff || ADXRS450_GET_ST(t) != 2) {
289 dev_err(&st->us->dev, "The second response is not correct!\n");
290 return -EIO;
293 ret = adxrs450_spi_initial(st, &t, 0);
294 if (ret)
295 return ret;
296 if (((t & 0xff) | 0x01) != 0xff || ADXRS450_GET_ST(t) != 2) {
297 dev_err(&st->us->dev, "The third response is not correct!\n");
298 return -EIO;
301 ret = adxrs450_spi_read_reg_16(dev, ADXRS450_FAULT1, &data);
302 if (ret)
303 return ret;
304 if (data & 0x0fff) {
305 dev_err(&st->us->dev, "The device is not in normal status!\n");
306 return -EINVAL;
308 ret = adxrs450_spi_read_reg_16(dev, ADXRS450_PID1, &data);
309 if (ret)
310 return ret;
311 dev_info(&st->us->dev, "The Part ID is 0x%x\n", data);
313 ret = adxrs450_spi_read_reg_16(dev, ADXRS450_SNL, &data);
314 if (ret)
315 return ret;
316 t = data;
317 ret = adxrs450_spi_read_reg_16(dev, ADXRS450_SNH, &data);
318 if (ret)
319 return ret;
320 t |= data << 16;
321 dev_info(&st->us->dev, "The Serial Number is 0x%x\n", t);
323 return 0;
326 static IIO_DEV_ATTR_GYRO_Z(adxrs450_read_sensor_data, 0);
327 static IIO_DEV_ATTR_TEMP_RAW(adxrs450_read_temp);
328 static IIO_DEV_ATTR_GYRO_Z_QUADRATURE_CORRECTION(adxrs450_read_quad, 0);
329 static IIO_DEV_ATTR_GYRO_Z_CALIBBIAS(S_IWUSR,
330 NULL, adxrs450_write_dnc, 0);
331 static IIO_CONST_ATTR(name, "adxrs450");
333 static struct attribute *adxrs450_attributes[] = {
334 &iio_dev_attr_gyro_z_raw.dev_attr.attr,
335 &iio_dev_attr_temp_raw.dev_attr.attr,
336 &iio_dev_attr_gyro_z_quadrature_correction_raw.dev_attr.attr,
337 &iio_dev_attr_gyro_z_calibbias.dev_attr.attr,
338 &iio_const_attr_name.dev_attr.attr,
339 NULL
342 static const struct attribute_group adxrs450_attribute_group = {
343 .attrs = adxrs450_attributes,
346 static int __devinit adxrs450_probe(struct spi_device *spi)
348 int ret, regdone = 0;
349 struct adxrs450_state *st = kzalloc(sizeof *st, GFP_KERNEL);
350 if (!st) {
351 ret = -ENOMEM;
352 goto error_ret;
354 /* This is only used for removal purposes */
355 spi_set_drvdata(spi, st);
357 /* Allocate the comms buffers */
358 st->rx = kzalloc(sizeof(*st->rx)*ADXRS450_MAX_RX, GFP_KERNEL);
359 if (st->rx == NULL) {
360 ret = -ENOMEM;
361 goto error_free_st;
363 st->tx = kzalloc(sizeof(*st->tx)*ADXRS450_MAX_TX, GFP_KERNEL);
364 if (st->tx == NULL) {
365 ret = -ENOMEM;
366 goto error_free_rx;
368 st->us = spi;
369 mutex_init(&st->buf_lock);
370 /* setup the industrialio driver allocated elements */
371 st->indio_dev = iio_allocate_device(0);
372 if (st->indio_dev == NULL) {
373 ret = -ENOMEM;
374 goto error_free_tx;
377 st->indio_dev->dev.parent = &spi->dev;
378 st->indio_dev->attrs = &adxrs450_attribute_group;
379 st->indio_dev->dev_data = (void *)(st);
380 st->indio_dev->driver_module = THIS_MODULE;
381 st->indio_dev->modes = INDIO_DIRECT_MODE;
383 ret = iio_device_register(st->indio_dev);
384 if (ret)
385 goto error_free_dev;
386 regdone = 1;
388 /* Get the device into a sane initial state */
389 ret = adxrs450_initial_setup(st);
390 if (ret)
391 goto error_initial;
392 return 0;
394 error_initial:
395 error_free_dev:
396 if (regdone)
397 iio_device_unregister(st->indio_dev);
398 else
399 iio_free_device(st->indio_dev);
400 error_free_tx:
401 kfree(st->tx);
402 error_free_rx:
403 kfree(st->rx);
404 error_free_st:
405 kfree(st);
406 error_ret:
407 return ret;
410 static int adxrs450_remove(struct spi_device *spi)
412 struct adxrs450_state *st = spi_get_drvdata(spi);
414 iio_device_unregister(st->indio_dev);
415 kfree(st->tx);
416 kfree(st->rx);
417 kfree(st);
419 return 0;
422 static struct spi_driver adxrs450_driver = {
423 .driver = {
424 .name = "adxrs450",
425 .owner = THIS_MODULE,
427 .probe = adxrs450_probe,
428 .remove = __devexit_p(adxrs450_remove),
431 static __init int adxrs450_init(void)
433 return spi_register_driver(&adxrs450_driver);
435 module_init(adxrs450_init);
437 static __exit void adxrs450_exit(void)
439 spi_unregister_driver(&adxrs450_driver);
441 module_exit(adxrs450_exit);
443 MODULE_AUTHOR("Cliff Cai <cliff.cai@xxxxxxxxxx>");
444 MODULE_DESCRIPTION("Analog Devices ADXRS450 Gyroscope SPI driver");
445 MODULE_LICENSE("GPL v2");