Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
adxrs450_core.c
Go to the documentation of this file.
1 /*
2  * ADXRS450/ADXRS453 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/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>
20 
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 
24 #include "adxrs450.h"
25 
33 static int adxrs450_spi_read_reg_16(struct iio_dev *indio_dev,
34  u8 reg_address,
35  u16 *val)
36 {
37  struct adxrs450_state *st = iio_priv(indio_dev);
38  int ret;
39 
40  mutex_lock(&st->buf_lock);
41  st->tx[0] = ADXRS450_READ_DATA | (reg_address >> 7);
42  st->tx[1] = reg_address << 1;
43  st->tx[2] = 0;
44  st->tx[3] = 0;
45 
46  if (!(hweight32(be32_to_cpu(*(u32 *)st->tx)) & 1))
47  st->tx[3] |= ADXRS450_P;
48 
49  ret = spi_write(st->us, st->tx, 4);
50  if (ret) {
51  dev_err(&st->us->dev, "problem while reading 16 bit register 0x%02x\n",
52  reg_address);
53  goto error_ret;
54  }
55  ret = spi_read(st->us, st->rx, 4);
56  if (ret) {
57  dev_err(&st->us->dev, "problem while reading 16 bit register 0x%02x\n",
58  reg_address);
59  goto error_ret;
60  }
61 
62  *val = (be32_to_cpu(*(u32 *)st->rx) >> 5) & 0xFFFF;
63 
64 error_ret:
65  mutex_unlock(&st->buf_lock);
66  return ret;
67 }
68 
76 static int adxrs450_spi_write_reg_16(struct iio_dev *indio_dev,
77  u8 reg_address,
78  u16 val)
79 {
80  struct adxrs450_state *st = iio_priv(indio_dev);
81  int ret;
82 
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;
86  st->tx[2] = val >> 7;
87  st->tx[3] = val << 1;
88 
89  if (!(hweight32(be32_to_cpu(*(u32 *)st->tx)) & 1))
90  st->tx[3] |= ADXRS450_P;
91 
92  ret = spi_write(st->us, st->tx, 4);
93  if (ret)
94  dev_err(&st->us->dev, "problem while writing 16 bit register 0x%02x\n",
95  reg_address);
96  msleep(1); /* enforce sequential transfer delay 0.1ms */
97  mutex_unlock(&st->buf_lock);
98  return ret;
99 }
100 
106 static int adxrs450_spi_sensor_data(struct iio_dev *indio_dev, s16 *val)
107 {
108  struct adxrs450_state *st = iio_priv(indio_dev);
109  int ret;
110 
111  mutex_lock(&st->buf_lock);
112  st->tx[0] = ADXRS450_SENSOR_DATA;
113  st->tx[1] = 0;
114  st->tx[2] = 0;
115  st->tx[3] = 0;
116 
117  ret = spi_write(st->us, st->tx, 4);
118  if (ret) {
119  dev_err(&st->us->dev, "Problem while reading sensor data\n");
120  goto error_ret;
121  }
122 
123  ret = spi_read(st->us, st->rx, 4);
124  if (ret) {
125  dev_err(&st->us->dev, "Problem while reading sensor data\n");
126  goto error_ret;
127  }
128 
129  *val = (be32_to_cpu(*(u32 *)st->rx) >> 10) & 0xFFFF;
130 
131 error_ret:
132  mutex_unlock(&st->buf_lock);
133  return ret;
134 }
135 
141 static int adxrs450_spi_initial(struct adxrs450_state *st,
142  u32 *val, char chk)
143 {
144  struct spi_message msg;
145  int ret;
146  struct spi_transfer xfers = {
147  .tx_buf = st->tx,
148  .rx_buf = st->rx,
149  .bits_per_word = 8,
150  .len = 4,
151  };
152 
153  mutex_lock(&st->buf_lock);
154  st->tx[0] = ADXRS450_SENSOR_DATA;
155  st->tx[1] = 0;
156  st->tx[2] = 0;
157  st->tx[3] = 0;
158  if (chk)
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);
163  if (ret) {
164  dev_err(&st->us->dev, "Problem while reading initializing data\n");
165  goto error_ret;
166  }
167 
168  *val = be32_to_cpu(*(u32 *)st->rx);
169 
170 error_ret:
171  mutex_unlock(&st->buf_lock);
172  return ret;
173 }
174 
175 /* Recommended Startup Sequence by spec */
176 static int adxrs450_initial_setup(struct iio_dev *indio_dev)
177 {
178  u32 t;
179  u16 data;
180  int ret;
181  struct adxrs450_state *st = iio_priv(indio_dev);
182 
184  ret = adxrs450_spi_initial(st, &t, 1);
185  if (ret)
186  return ret;
187  if (t != 0x01)
188  dev_warn(&st->us->dev, "The initial power on response "
189  "is not correct! Restart without reset?\n");
190 
192  ret = adxrs450_spi_initial(st, &t, 0);
193  if (ret)
194  return ret;
195 
197  ret = adxrs450_spi_initial(st, &t, 0);
198  if (ret)
199  return ret;
200  if (((t & 0xff) | 0x01) != 0xff || ADXRS450_GET_ST(t) != 2) {
201  dev_err(&st->us->dev, "The second response is not correct!\n");
202  return -EIO;
203 
204  }
205  ret = adxrs450_spi_initial(st, &t, 0);
206  if (ret)
207  return ret;
208  if (((t & 0xff) | 0x01) != 0xff || ADXRS450_GET_ST(t) != 2) {
209  dev_err(&st->us->dev, "The third response is not correct!\n");
210  return -EIO;
211 
212  }
213  ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_FAULT1, &data);
214  if (ret)
215  return ret;
216  if (data & 0x0fff) {
217  dev_err(&st->us->dev, "The device is not in normal status!\n");
218  return -EINVAL;
219  }
220  ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_PID1, &data);
221  if (ret)
222  return ret;
223  dev_info(&st->us->dev, "The Part ID is 0x%x\n", data);
224 
225  ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_SNL, &data);
226  if (ret)
227  return ret;
228  t = data;
229  ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_SNH, &data);
230  if (ret)
231  return ret;
232  t |= data << 16;
233  dev_info(&st->us->dev, "The Serial Number is 0x%x\n", t);
234 
235  return 0;
236 }
237 
238 static int adxrs450_write_raw(struct iio_dev *indio_dev,
239  struct iio_chan_spec const *chan,
240  int val,
241  int val2,
242  long mask)
243 {
244  int ret;
245  switch (mask) {
247  ret = adxrs450_spi_write_reg_16(indio_dev,
249  val & 0x3FF);
250  break;
251  default:
252  ret = -EINVAL;
253  break;
254  }
255  return ret;
256 }
257 
258 static int adxrs450_read_raw(struct iio_dev *indio_dev,
259  struct iio_chan_spec const *chan,
260  int *val,
261  int *val2,
262  long mask)
263 {
264  int ret;
265  s16 t;
266 
267  switch (mask) {
268  case IIO_CHAN_INFO_RAW:
269  switch (chan->type) {
270  case IIO_ANGL_VEL:
271  ret = adxrs450_spi_sensor_data(indio_dev, &t);
272  if (ret)
273  break;
274  *val = t;
275  ret = IIO_VAL_INT;
276  break;
277  case IIO_TEMP:
278  ret = adxrs450_spi_read_reg_16(indio_dev,
279  ADXRS450_TEMP1, &t);
280  if (ret)
281  break;
282  *val = (t >> 6) + 225;
283  ret = IIO_VAL_INT;
284  break;
285  default:
286  ret = -EINVAL;
287  break;
288  }
289  break;
290  case IIO_CHAN_INFO_SCALE:
291  switch (chan->type) {
292  case IIO_ANGL_VEL:
293  *val = 0;
294  *val2 = 218166;
295  return IIO_VAL_INT_PLUS_NANO;
296  case IIO_TEMP:
297  *val = 200;
298  *val2 = 0;
299  return IIO_VAL_INT;
300  default:
301  return -EINVAL;
302  }
303  break;
305  ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_QUAD1, &t);
306  if (ret)
307  break;
308  *val = t;
309  ret = IIO_VAL_INT;
310  break;
312  ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_DNC1, &t);
313  if (ret)
314  break;
315  *val = t;
316  ret = IIO_VAL_INT;
317  break;
318  default:
319  ret = -EINVAL;
320  break;
321  }
322 
323  return ret;
324 }
325 
326 static const struct iio_chan_spec adxrs450_channels[2][2] = {
327  [ID_ADXRS450] = {
328  {
329  .type = IIO_ANGL_VEL,
330  .modified = 1,
331  .channel2 = IIO_MOD_Z,
332  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
336  }, {
337  .type = IIO_TEMP,
338  .indexed = 1,
339  .channel = 0,
340  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
342  }
343  },
344  [ID_ADXRS453] = {
345  {
346  .type = IIO_ANGL_VEL,
347  .modified = 1,
348  .channel2 = IIO_MOD_Z,
349  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
352  }, {
353  .type = IIO_TEMP,
354  .indexed = 1,
355  .channel = 0,
356  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
358  }
359  },
360 };
361 
362 static const struct iio_info adxrs450_info = {
363  .driver_module = THIS_MODULE,
364  .read_raw = &adxrs450_read_raw,
365  .write_raw = &adxrs450_write_raw,
366 };
367 
368 static int __devinit adxrs450_probe(struct spi_device *spi)
369 {
370  int ret;
371  struct adxrs450_state *st;
372  struct iio_dev *indio_dev;
373 
374  /* setup the industrialio driver allocated elements */
375  indio_dev = iio_device_alloc(sizeof(*st));
376  if (indio_dev == NULL) {
377  ret = -ENOMEM;
378  goto error_ret;
379  }
380  st = iio_priv(indio_dev);
381  st->us = spi;
382  mutex_init(&st->buf_lock);
383  /* This is only used for removal purposes */
384  spi_set_drvdata(spi, indio_dev);
385 
386  indio_dev->dev.parent = &spi->dev;
387  indio_dev->info = &adxrs450_info;
388  indio_dev->modes = INDIO_DIRECT_MODE;
389  indio_dev->channels =
390  adxrs450_channels[spi_get_device_id(spi)->driver_data];
391  indio_dev->num_channels = ARRAY_SIZE(adxrs450_channels);
392  indio_dev->name = spi->dev.driver->name;
393 
394  ret = iio_device_register(indio_dev);
395  if (ret)
396  goto error_free_dev;
397 
398  /* Get the device into a sane initial state */
399  ret = adxrs450_initial_setup(indio_dev);
400  if (ret)
401  goto error_initial;
402  return 0;
403 error_initial:
404  iio_device_unregister(indio_dev);
405 error_free_dev:
406  iio_device_free(indio_dev);
407 
408 error_ret:
409  return ret;
410 }
411 
412 static int __devexit adxrs450_remove(struct spi_device *spi)
413 {
414  iio_device_unregister(spi_get_drvdata(spi));
415  iio_device_free(spi_get_drvdata(spi));
416 
417  return 0;
418 }
419 
420 static const struct spi_device_id adxrs450_id[] = {
421  {"adxrs450", ID_ADXRS450},
422  {"adxrs453", ID_ADXRS453},
423  {}
424 };
425 MODULE_DEVICE_TABLE(spi, adxrs450_id);
426 
427 static struct spi_driver adxrs450_driver = {
428  .driver = {
429  .name = "adxrs450",
430  .owner = THIS_MODULE,
431  },
432  .probe = adxrs450_probe,
433  .remove = __devexit_p(adxrs450_remove),
434  .id_table = adxrs450_id,
435 };
436 module_spi_driver(adxrs450_driver);
437 
438 MODULE_AUTHOR("Cliff Cai <cliff.cai@xxxxxxxxxx>");
439 MODULE_DESCRIPTION("Analog Devices ADXRS450/ADXRS453 Gyroscope SPI driver");
440 MODULE_LICENSE("GPL v2");