Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
adis16260_core.c
Go to the documentation of this file.
1 /*
2  * ADIS16260/ADIS16265 Programmable Digital Gyroscope Sensor Driver
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/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 #include <linux/iio/buffer.h>
24 
25 #include "adis16260.h"
26 
27 #define DRIVER_NAME "adis16260"
28 
29 static int adis16260_check_status(struct iio_dev *indio_dev);
30 
37 static int adis16260_spi_write_reg_8(struct iio_dev *indio_dev,
38  u8 reg_address,
39  u8 val)
40 {
41  int ret;
42  struct adis16260_state *st = iio_priv(indio_dev);
43 
44  mutex_lock(&st->buf_lock);
45  st->tx[0] = ADIS16260_WRITE_REG(reg_address);
46  st->tx[1] = val;
47 
48  ret = spi_write(st->us, st->tx, 2);
49  mutex_unlock(&st->buf_lock);
50 
51  return ret;
52 }
53 
61 static int adis16260_spi_write_reg_16(struct iio_dev *indio_dev,
62  u8 lower_reg_address,
63  u16 value)
64 {
65  int ret;
66  struct spi_message msg;
67  struct adis16260_state *st = iio_priv(indio_dev);
68  struct spi_transfer xfers[] = {
69  {
70  .tx_buf = st->tx,
71  .bits_per_word = 8,
72  .len = 2,
73  .cs_change = 1,
74  .delay_usecs = 20,
75  }, {
76  .tx_buf = st->tx + 2,
77  .bits_per_word = 8,
78  .len = 2,
79  .delay_usecs = 20,
80  },
81  };
82 
83  mutex_lock(&st->buf_lock);
84  st->tx[0] = ADIS16260_WRITE_REG(lower_reg_address);
85  st->tx[1] = value & 0xFF;
86  st->tx[2] = ADIS16260_WRITE_REG(lower_reg_address + 1);
87  st->tx[3] = (value >> 8) & 0xFF;
88 
89  spi_message_init(&msg);
90  spi_message_add_tail(&xfers[0], &msg);
91  spi_message_add_tail(&xfers[1], &msg);
92  ret = spi_sync(st->us, &msg);
93  mutex_unlock(&st->buf_lock);
94 
95  return ret;
96 }
97 
105 static int adis16260_spi_read_reg_16(struct iio_dev *indio_dev,
106  u8 lower_reg_address,
107  u16 *val)
108 {
109  struct spi_message msg;
110  struct adis16260_state *st = iio_priv(indio_dev);
111  int ret;
112  struct spi_transfer xfers[] = {
113  {
114  .tx_buf = st->tx,
115  .bits_per_word = 8,
116  .len = 2,
117  .cs_change = 1,
118  .delay_usecs = 30,
119  }, {
120  .rx_buf = st->rx,
121  .bits_per_word = 8,
122  .len = 2,
123  .delay_usecs = 30,
124  },
125  };
126 
127  mutex_lock(&st->buf_lock);
128  st->tx[0] = ADIS16260_READ_REG(lower_reg_address);
129  st->tx[1] = 0;
130 
131  spi_message_init(&msg);
132  spi_message_add_tail(&xfers[0], &msg);
133  spi_message_add_tail(&xfers[1], &msg);
134  ret = spi_sync(st->us, &msg);
135  if (ret) {
136  dev_err(&st->us->dev,
137  "problem when reading 16 bit register 0x%02X",
138  lower_reg_address);
139  goto error_ret;
140  }
141  *val = (st->rx[0] << 8) | st->rx[1];
142 
143 error_ret:
144  mutex_unlock(&st->buf_lock);
145  return ret;
146 }
147 
148 static ssize_t adis16260_read_frequency_available(struct device *dev,
149  struct device_attribute *attr,
150  char *buf)
151 {
152  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
153  struct adis16260_state *st = iio_priv(indio_dev);
154  if (spi_get_device_id(st->us)->driver_data)
155  return sprintf(buf, "%s\n", "0.129 ~ 256");
156  else
157  return sprintf(buf, "%s\n", "256 2048");
158 }
159 
160 static ssize_t adis16260_read_frequency(struct device *dev,
161  struct device_attribute *attr,
162  char *buf)
163 {
164  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
165  struct adis16260_state *st = iio_priv(indio_dev);
166  int ret, len = 0;
167  u16 t;
168  int sps;
169  ret = adis16260_spi_read_reg_16(indio_dev,
171  &t);
172  if (ret)
173  return ret;
174 
175  if (spi_get_device_id(st->us)->driver_data) /* If an adis16251 */
176  sps = (t & ADIS16260_SMPL_PRD_TIME_BASE) ? 8 : 256;
177  else
178  sps = (t & ADIS16260_SMPL_PRD_TIME_BASE) ? 66 : 2048;
179  sps /= (t & ADIS16260_SMPL_PRD_DIV_MASK) + 1;
180  len = sprintf(buf, "%d SPS\n", sps);
181  return len;
182 }
183 
184 static ssize_t adis16260_write_frequency(struct device *dev,
185  struct device_attribute *attr,
186  const char *buf,
187  size_t len)
188 {
189  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
190  struct adis16260_state *st = iio_priv(indio_dev);
191  long val;
192  int ret;
193  u8 t;
194 
195  ret = strict_strtol(buf, 10, &val);
196  if (ret)
197  return ret;
198  if (val == 0)
199  return -EINVAL;
200 
201  mutex_lock(&indio_dev->mlock);
202  if (spi_get_device_id(st->us)) {
203  t = (256 / val);
204  if (t > 0)
205  t--;
207  } else {
208  t = (2048 / val);
209  if (t > 0)
210  t--;
212  }
213  if ((t & ADIS16260_SMPL_PRD_DIV_MASK) >= 0x0A)
214  st->us->max_speed_hz = ADIS16260_SPI_SLOW;
215  else
216  st->us->max_speed_hz = ADIS16260_SPI_FAST;
217  ret = adis16260_spi_write_reg_8(indio_dev,
219  t);
220 
221  mutex_unlock(&indio_dev->mlock);
222 
223  return ret ? ret : len;
224 }
225 
226 static int adis16260_reset(struct iio_dev *indio_dev)
227 {
228  int ret;
229  ret = adis16260_spi_write_reg_8(indio_dev,
232  if (ret)
233  dev_err(&indio_dev->dev, "problem resetting device");
234 
235  return ret;
236 }
237 
238 int adis16260_set_irq(struct iio_dev *indio_dev, bool enable)
239 {
240  int ret;
241  u16 msc;
242  ret = adis16260_spi_read_reg_16(indio_dev, ADIS16260_MSC_CTRL, &msc);
243  if (ret)
244  goto error_ret;
245 
247  if (enable)
249  else
251 
252  ret = adis16260_spi_write_reg_16(indio_dev, ADIS16260_MSC_CTRL, msc);
253  if (ret)
254  goto error_ret;
255 
256 error_ret:
257  return ret;
258 }
259 
260 /* Power down the device */
261 static int adis16260_stop_device(struct iio_dev *indio_dev)
262 {
263  int ret;
265 
266  ret = adis16260_spi_write_reg_16(indio_dev, ADIS16260_SLP_CNT, val);
267  if (ret)
268  dev_err(&indio_dev->dev, "problem with turning device off: SLP_CNT");
269 
270  return ret;
271 }
272 
273 static int adis16260_self_test(struct iio_dev *indio_dev)
274 {
275  int ret;
276  ret = adis16260_spi_write_reg_16(indio_dev,
279  if (ret) {
280  dev_err(&indio_dev->dev, "problem starting self test");
281  goto err_ret;
282  }
283 
284  adis16260_check_status(indio_dev);
285 
286 err_ret:
287  return ret;
288 }
289 
290 static int adis16260_check_status(struct iio_dev *indio_dev)
291 {
292  u16 status;
293  int ret;
294  struct device *dev = &indio_dev->dev;
295 
296  ret = adis16260_spi_read_reg_16(indio_dev,
298  &status);
299 
300  if (ret < 0) {
301  dev_err(dev, "Reading status failed\n");
302  goto error_ret;
303  }
304  ret = status & 0x7F;
305  if (status & ADIS16260_DIAG_STAT_FLASH_CHK)
306  dev_err(dev, "Flash checksum error\n");
307  if (status & ADIS16260_DIAG_STAT_SELF_TEST)
308  dev_err(dev, "Self test error\n");
309  if (status & ADIS16260_DIAG_STAT_OVERFLOW)
310  dev_err(dev, "Sensor overrange\n");
311  if (status & ADIS16260_DIAG_STAT_SPI_FAIL)
312  dev_err(dev, "SPI failure\n");
313  if (status & ADIS16260_DIAG_STAT_FLASH_UPT)
314  dev_err(dev, "Flash update failed\n");
315  if (status & ADIS16260_DIAG_STAT_POWER_HIGH)
316  dev_err(dev, "Power supply above 5.25V\n");
317  if (status & ADIS16260_DIAG_STAT_POWER_LOW)
318  dev_err(dev, "Power supply below 4.75V\n");
319 
320 error_ret:
321  return ret;
322 }
323 
324 static int adis16260_initial_setup(struct iio_dev *indio_dev)
325 {
326  int ret;
327  struct device *dev = &indio_dev->dev;
328 
329  /* Disable IRQ */
330  ret = adis16260_set_irq(indio_dev, false);
331  if (ret) {
332  dev_err(dev, "disable irq failed");
333  goto err_ret;
334  }
335 
336  /* Do self test */
337  ret = adis16260_self_test(indio_dev);
338  if (ret) {
339  dev_err(dev, "self test failure");
340  goto err_ret;
341  }
342 
343  /* Read status register to check the result */
344  ret = adis16260_check_status(indio_dev);
345  if (ret) {
346  adis16260_reset(indio_dev);
347  dev_err(dev, "device not playing ball -> reset");
349  ret = adis16260_check_status(indio_dev);
350  if (ret) {
351  dev_err(dev, "giving up");
352  goto err_ret;
353  }
354  }
355 
356 err_ret:
357  return ret;
358 }
359 
361  adis16260_read_frequency,
362  adis16260_write_frequency);
363 
364 static IIO_DEVICE_ATTR(sampling_frequency_available,
365  S_IRUGO, adis16260_read_frequency_available, NULL, 0);
366 
373 };
374 #define ADIS16260_GYRO_CHANNEL_SET(axis, mod) \
375  struct iio_chan_spec adis16260_channels_##axis[] = { \
376  { \
377  .type = IIO_ANGL_VEL, \
378  .modified = 1, \
379  .channel2 = mod, \
380  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
381  IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT | \
382  IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT | \
383  IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
384  .address = gyro, \
385  .scan_index = ADIS16260_SCAN_GYRO, \
386  .scan_type = { \
387  .sign = 's', \
388  .realbits = 14, \
389  .storagebits = 16, \
390  }, \
391  }, { \
392  .type = IIO_ANGL, \
393  .modified = 1, \
394  .channel2 = mod, \
395  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT, \
396  .address = angle, \
397  .scan_index = ADIS16260_SCAN_ANGL, \
398  .scan_type = { \
399  .sign = 'u', \
400  .realbits = 14, \
401  .storagebits = 16, \
402  }, \
403  }, { \
404  .type = IIO_TEMP, \
405  .indexed = 1, \
406  .channel = 0, \
407  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
408  IIO_CHAN_INFO_OFFSET_SEPARATE_BIT | \
409  IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
410  .address = temp, \
411  .scan_index = ADIS16260_SCAN_TEMP, \
412  .scan_type = { \
413  .sign = 'u', \
414  .realbits = 12, \
415  .storagebits = 16, \
416  }, \
417  }, { \
418  .type = IIO_VOLTAGE, \
419  .indexed = 1, \
420  .channel = 0, \
421  .extend_name = "supply", \
422  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
423  IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
424  .address = in_supply, \
425  .scan_index = ADIS16260_SCAN_SUPPLY, \
426  .scan_type = { \
427  .sign = 'u', \
428  .realbits = 12, \
429  .storagebits = 16, \
430  }, \
431  }, { \
432  .type = IIO_VOLTAGE, \
433  .indexed = 1, \
434  .channel = 1, \
435  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
436  IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
437  .address = in_aux, \
438  .scan_index = ADIS16260_SCAN_AUX_ADC, \
439  .scan_type = { \
440  .sign = 'u', \
441  .realbits = 12, \
442  .storagebits = 16, \
443  }, \
444  }, \
445  IIO_CHAN_SOFT_TIMESTAMP(5), \
446  }
447 
448 static const ADIS16260_GYRO_CHANNEL_SET(x, IIO_MOD_X);
449 static const ADIS16260_GYRO_CHANNEL_SET(y, IIO_MOD_Y);
450 static const ADIS16260_GYRO_CHANNEL_SET(z, IIO_MOD_Z);
451 
452 static const u8 adis16260_addresses[5][3] = {
453  [gyro] = { ADIS16260_GYRO_OUT,
456  [angle] = { ADIS16260_ANGL_OUT },
458  [in_aux] = { ADIS16260_AUX_ADC },
459  [temp] = { ADIS16260_TEMP_OUT },
460 };
461 static int adis16260_read_raw(struct iio_dev *indio_dev,
462  struct iio_chan_spec const *chan,
463  int *val, int *val2,
464  long mask)
465 {
466  struct adis16260_state *st = iio_priv(indio_dev);
467  int ret;
468  int bits;
469  u8 addr;
470  s16 val16;
471 
472  switch (mask) {
473  case IIO_CHAN_INFO_RAW:
474  mutex_lock(&indio_dev->mlock);
475  addr = adis16260_addresses[chan->address][0];
476  ret = adis16260_spi_read_reg_16(indio_dev, addr, &val16);
477  if (ret) {
478  mutex_unlock(&indio_dev->mlock);
479  return ret;
480  }
481 
482  if (val16 & ADIS16260_ERROR_ACTIVE) {
483  ret = adis16260_check_status(indio_dev);
484  if (ret) {
485  mutex_unlock(&indio_dev->mlock);
486  return ret;
487  }
488  }
489  val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
490  if (chan->scan_type.sign == 's')
491  val16 = (s16)(val16 <<
492  (16 - chan->scan_type.realbits)) >>
493  (16 - chan->scan_type.realbits);
494  *val = val16;
495  mutex_unlock(&indio_dev->mlock);
496  return IIO_VAL_INT;
497  case IIO_CHAN_INFO_SCALE:
498  switch (chan->type) {
499  case IIO_ANGL_VEL:
500  *val = 0;
501  if (spi_get_device_id(st->us)->driver_data) {
502  /* 0.01832 degree / sec */
503  *val2 = IIO_DEGREE_TO_RAD(18320);
504  } else {
505  /* 0.07326 degree / sec */
506  *val2 = IIO_DEGREE_TO_RAD(73260);
507  }
508  return IIO_VAL_INT_PLUS_MICRO;
509  case IIO_VOLTAGE:
510  if (chan->channel == 0) {
511  *val = 1;
512  *val2 = 831500; /* 1.8315 mV */
513  } else {
514  *val = 0;
515  *val2 = 610500; /* 610.5 uV */
516  }
517  return IIO_VAL_INT_PLUS_MICRO;
518  case IIO_TEMP:
519  *val = 145;
520  *val2 = 300000; /* 0.1453 C */
521  return IIO_VAL_INT_PLUS_MICRO;
522  default:
523  return -EINVAL;
524  }
525  break;
527  *val = 250000 / 1453; /* 25 C = 0x00 */
528  return IIO_VAL_INT;
530  switch (chan->type) {
531  case IIO_ANGL_VEL:
532  bits = 12;
533  break;
534  default:
535  return -EINVAL;
536  };
537  mutex_lock(&indio_dev->mlock);
538  addr = adis16260_addresses[chan->address][1];
539  ret = adis16260_spi_read_reg_16(indio_dev, addr, &val16);
540  if (ret) {
541  mutex_unlock(&indio_dev->mlock);
542  return ret;
543  }
544  val16 &= (1 << bits) - 1;
545  val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
546  *val = val16;
547  mutex_unlock(&indio_dev->mlock);
548  return IIO_VAL_INT;
550  switch (chan->type) {
551  case IIO_ANGL_VEL:
552  bits = 12;
553  break;
554  default:
555  return -EINVAL;
556  };
557  mutex_lock(&indio_dev->mlock);
558  addr = adis16260_addresses[chan->address][2];
559  ret = adis16260_spi_read_reg_16(indio_dev, addr, &val16);
560  if (ret) {
561  mutex_unlock(&indio_dev->mlock);
562  return ret;
563  }
564  *val = (1 << bits) - 1;
565  mutex_unlock(&indio_dev->mlock);
566  return IIO_VAL_INT;
567  }
568  return -EINVAL;
569 }
570 
571 static int adis16260_write_raw(struct iio_dev *indio_dev,
572  struct iio_chan_spec const *chan,
573  int val,
574  int val2,
575  long mask)
576 {
577  int bits = 12;
578  s16 val16;
579  u8 addr;
580  switch (mask) {
582  val16 = val & ((1 << bits) - 1);
583  addr = adis16260_addresses[chan->address][1];
584  return adis16260_spi_write_reg_16(indio_dev, addr, val16);
586  val16 = val & ((1 << bits) - 1);
587  addr = adis16260_addresses[chan->address][2];
588  return adis16260_spi_write_reg_16(indio_dev, addr, val16);
589  }
590  return -EINVAL;
591 }
592 
593 static struct attribute *adis16260_attributes[] = {
594  &iio_dev_attr_sampling_frequency.dev_attr.attr,
595  &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
596  NULL
597 };
598 
599 static const struct attribute_group adis16260_attribute_group = {
600  .attrs = adis16260_attributes,
601 };
602 
603 static const struct iio_info adis16260_info = {
604  .attrs = &adis16260_attribute_group,
605  .read_raw = &adis16260_read_raw,
606  .write_raw = &adis16260_write_raw,
607  .driver_module = THIS_MODULE,
608 };
609 
610 static int __devinit adis16260_probe(struct spi_device *spi)
611 {
612  int ret;
613  struct adis16260_platform_data *pd = spi->dev.platform_data;
614  struct adis16260_state *st;
615  struct iio_dev *indio_dev;
616 
617  /* setup the industrialio driver allocated elements */
618  indio_dev = iio_device_alloc(sizeof(*st));
619  if (indio_dev == NULL) {
620  ret = -ENOMEM;
621  goto error_ret;
622  }
623  st = iio_priv(indio_dev);
624  if (pd)
625  st->negate = pd->negate;
626  /* this is only used for removal purposes */
627  spi_set_drvdata(spi, indio_dev);
628 
629  st->us = spi;
630  mutex_init(&st->buf_lock);
631 
632  indio_dev->name = spi_get_device_id(st->us)->name;
633  indio_dev->dev.parent = &spi->dev;
634  indio_dev->info = &adis16260_info;
635  indio_dev->num_channels
636  = ARRAY_SIZE(adis16260_channels_x);
637  if (pd && pd->direction)
638  switch (pd->direction) {
639  case 'x':
640  indio_dev->channels = adis16260_channels_x;
641  break;
642  case 'y':
643  indio_dev->channels = adis16260_channels_y;
644  break;
645  case 'z':
646  indio_dev->channels = adis16260_channels_z;
647  break;
648  default:
649  return -EINVAL;
650  }
651  else
652  indio_dev->channels = adis16260_channels_x;
653  indio_dev->num_channels = ARRAY_SIZE(adis16260_channels_x);
654  indio_dev->modes = INDIO_DIRECT_MODE;
655 
656  ret = adis16260_configure_ring(indio_dev);
657  if (ret)
658  goto error_free_dev;
659 
660  ret = iio_buffer_register(indio_dev,
661  indio_dev->channels,
662  ARRAY_SIZE(adis16260_channels_x));
663  if (ret) {
664  printk(KERN_ERR "failed to initialize the ring\n");
665  goto error_unreg_ring_funcs;
666  }
667  if (indio_dev->buffer) {
668  /* Set default scan mode */
669  iio_scan_mask_set(indio_dev, indio_dev->buffer,
671  iio_scan_mask_set(indio_dev, indio_dev->buffer,
673  iio_scan_mask_set(indio_dev, indio_dev->buffer,
675  iio_scan_mask_set(indio_dev, indio_dev->buffer,
677  iio_scan_mask_set(indio_dev, indio_dev->buffer,
679  }
680  if (spi->irq) {
681  ret = adis16260_probe_trigger(indio_dev);
682  if (ret)
683  goto error_uninitialize_ring;
684  }
685 
686  /* Get the device into a sane initial state */
687  ret = adis16260_initial_setup(indio_dev);
688  if (ret)
689  goto error_remove_trigger;
690  ret = iio_device_register(indio_dev);
691  if (ret)
692  goto error_remove_trigger;
693 
694  return 0;
695 
696 error_remove_trigger:
697  adis16260_remove_trigger(indio_dev);
698 error_uninitialize_ring:
699  iio_buffer_unregister(indio_dev);
700 error_unreg_ring_funcs:
701  adis16260_unconfigure_ring(indio_dev);
702 error_free_dev:
703  iio_device_free(indio_dev);
704 error_ret:
705  return ret;
706 }
707 
708 static int __devexit adis16260_remove(struct spi_device *spi)
709 {
710  struct iio_dev *indio_dev = spi_get_drvdata(spi);
711 
712  iio_device_unregister(indio_dev);
713  adis16260_stop_device(indio_dev);
714  adis16260_remove_trigger(indio_dev);
715  iio_buffer_unregister(indio_dev);
716  adis16260_unconfigure_ring(indio_dev);
717  iio_device_free(indio_dev);
718 
719  return 0;
720 }
721 
722 /*
723  * These parts do not need to be differentiated until someone adds
724  * support for the on chip filtering.
725  */
726 static const struct spi_device_id adis16260_id[] = {
727  {"adis16260", 0},
728  {"adis16265", 0},
729  {"adis16250", 0},
730  {"adis16255", 0},
731  {"adis16251", 1},
732  {}
733 };
734 MODULE_DEVICE_TABLE(spi, adis16260_id);
735 
736 static struct spi_driver adis16260_driver = {
737  .driver = {
738  .name = "adis16260",
739  .owner = THIS_MODULE,
740  },
741  .probe = adis16260_probe,
742  .remove = __devexit_p(adis16260_remove),
743  .id_table = adis16260_id,
744 };
745 module_spi_driver(adis16260_driver);
746 
747 MODULE_AUTHOR("Barry Song <[email protected]>");
748 MODULE_DESCRIPTION("Analog Devices ADIS16260/5 Digital Gyroscope Sensor");
749 MODULE_LICENSE("GPL v2");