Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sca3000_core.c
Go to the documentation of this file.
1 /*
2  * sca3000_core.c -- support VTI sca3000 series accelerometers via SPI
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published by
6  * the Free Software Foundation.
7  *
8  * Copyright (c) 2009 Jonathan Cameron <[email protected]>
9  *
10  * See industrialio/accels/sca3000.h for comments.
11  */
12 
13 #include <linux/interrupt.h>
14 #include <linux/fs.h>
15 #include <linux/device.h>
16 #include <linux/slab.h>
17 #include <linux/kernel.h>
18 #include <linux/spi/spi.h>
19 #include <linux/sysfs.h>
20 #include <linux/module.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/events.h>
24 #include <linux/iio/buffer.h>
25 
26 #include "sca3000.h"
27 
29  d01,
30  e02,
31  e04,
32  e05,
33 };
34 
35 /* Note where option modes are not defined, the chip simply does not
36  * support any.
37  * Other chips in the sca3000 series use i2c and are not included here.
38  *
39  * Some of these devices are only listed in the family data sheet and
40  * do not actually appear to be available.
41  */
42 static const struct sca3000_chip_info sca3000_spi_chip_info_tbl[] = {
43  [d01] = {
44  .scale = 7357,
45  .temp_output = true,
46  .measurement_mode_freq = 250,
47  .option_mode_1 = SCA3000_OP_MODE_BYPASS,
48  .option_mode_1_freq = 250,
49  .mot_det_mult_xz = {50, 100, 200, 350, 650, 1300},
50  .mot_det_mult_y = {50, 100, 150, 250, 450, 850, 1750},
51  },
52  [e02] = {
53  .scale = 9810,
54  .measurement_mode_freq = 125,
55  .option_mode_1 = SCA3000_OP_MODE_NARROW,
56  .option_mode_1_freq = 63,
57  .mot_det_mult_xz = {100, 150, 300, 550, 1050, 2050},
58  .mot_det_mult_y = {50, 100, 200, 350, 700, 1350, 2700},
59  },
60  [e04] = {
61  .scale = 19620,
62  .measurement_mode_freq = 100,
63  .option_mode_1 = SCA3000_OP_MODE_NARROW,
64  .option_mode_1_freq = 50,
65  .option_mode_2 = SCA3000_OP_MODE_WIDE,
66  .option_mode_2_freq = 400,
67  .mot_det_mult_xz = {200, 300, 600, 1100, 2100, 4100},
68  .mot_det_mult_y = {100, 200, 400, 7000, 1400, 2700, 54000},
69  },
70  [e05] = {
71  .scale = 61313,
72  .measurement_mode_freq = 200,
73  .option_mode_1 = SCA3000_OP_MODE_NARROW,
74  .option_mode_1_freq = 50,
75  .option_mode_2 = SCA3000_OP_MODE_WIDE,
76  .option_mode_2_freq = 400,
77  .mot_det_mult_xz = {600, 900, 1700, 3200, 6100, 11900},
78  .mot_det_mult_y = {300, 600, 1200, 2000, 4100, 7800, 15600},
79  },
80 };
81 
83 {
84  st->tx[0] = SCA3000_WRITE_REG(address);
85  st->tx[1] = val;
86  return spi_write(st->us, st->tx, 2);
87 }
88 
90  uint8_t reg_address_high,
91  int len)
92 {
93  struct spi_message msg;
94  struct spi_transfer xfer[2] = {
95  {
96  .len = 1,
97  .tx_buf = st->tx,
98  }, {
99  .len = len,
100  .rx_buf = st->rx,
101  }
102  };
103  st->tx[0] = SCA3000_READ_REG(reg_address_high);
104  spi_message_init(&msg);
105  spi_message_add_tail(&xfer[0], &msg);
106  spi_message_add_tail(&xfer[1], &msg);
107 
108  return spi_sync(st->us, &msg);
109 }
110 
116 static int sca3000_reg_lock_on(struct sca3000_state *st)
117 {
118  int ret;
119 
121  if (ret < 0)
122  return ret;
123 
124  return !(st->rx[0] & SCA3000_LOCKED);
125 }
126 
134 static int __sca3000_unlock_reg_lock(struct sca3000_state *st)
135 {
136  struct spi_message msg;
137  struct spi_transfer xfer[3] = {
138  {
139  .len = 2,
140  .cs_change = 1,
141  .tx_buf = st->tx,
142  }, {
143  .len = 2,
144  .cs_change = 1,
145  .tx_buf = st->tx + 2,
146  }, {
147  .len = 2,
148  .tx_buf = st->tx + 4,
149  },
150  };
152  st->tx[1] = 0x00;
154  st->tx[3] = 0x50;
156  st->tx[5] = 0xA0;
157  spi_message_init(&msg);
158  spi_message_add_tail(&xfer[0], &msg);
159  spi_message_add_tail(&xfer[1], &msg);
160  spi_message_add_tail(&xfer[2], &msg);
161 
162  return spi_sync(st->us, &msg);
163 }
164 
175 static int sca3000_write_ctrl_reg(struct sca3000_state *st,
176  uint8_t sel,
177  uint8_t val)
178 {
179 
180  int ret;
181 
182  ret = sca3000_reg_lock_on(st);
183  if (ret < 0)
184  goto error_ret;
185  if (ret) {
186  ret = __sca3000_unlock_reg_lock(st);
187  if (ret)
188  goto error_ret;
189  }
190 
191  /* Set the control select register */
193  if (ret)
194  goto error_ret;
195 
196  /* Write the actual value into the register */
198 
199 error_ret:
200  return ret;
201 }
202 
203 /* Crucial that lock is called before calling this */
209 static int sca3000_read_ctrl_reg(struct sca3000_state *st,
210  u8 ctrl_reg)
211 {
212  int ret;
213 
214  ret = sca3000_reg_lock_on(st);
215  if (ret < 0)
216  goto error_ret;
217  if (ret) {
218  ret = __sca3000_unlock_reg_lock(st);
219  if (ret)
220  goto error_ret;
221  }
222  /* Set the control select register */
223  ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, ctrl_reg);
224  if (ret)
225  goto error_ret;
227  if (ret)
228  goto error_ret;
229  else
230  return st->rx[0];
231 error_ret:
232  return ret;
233 }
234 
235 #ifdef SCA3000_DEBUG
236 
241 static int sca3000_check_status(struct device *dev)
242 {
243  int ret;
244  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
245  struct sca3000_state *st = iio_priv(indio_dev);
246 
247  mutex_lock(&st->lock);
249  if (ret < 0)
250  goto error_ret;
251  if (st->rx[0] & SCA3000_EEPROM_CS_ERROR)
252  dev_err(dev, "eeprom error\n");
253  if (st->rx[0] & SCA3000_SPI_FRAME_ERROR)
254  dev_err(dev, "Previous SPI Frame was corrupt\n");
255 
256 error_ret:
257  mutex_unlock(&st->lock);
258  return ret;
259 }
260 #endif /* SCA3000_DEBUG */
261 
262 
266 static ssize_t sca3000_show_rev(struct device *dev,
267  struct device_attribute *attr,
268  char *buf)
269 {
270  int len = 0, ret;
271  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
272  struct sca3000_state *st = iio_priv(indio_dev);
273 
274  mutex_lock(&st->lock);
276  if (ret < 0)
277  goto error_ret;
278  len += sprintf(buf + len,
279  "major=%d, minor=%d\n",
280  st->rx[0] & SCA3000_REVID_MAJOR_MASK,
281  st->rx[0] & SCA3000_REVID_MINOR_MASK);
282 error_ret:
283  mutex_unlock(&st->lock);
284 
285  return ret ? ret : len;
286 }
287 
294 static ssize_t
295 sca3000_show_available_measurement_modes(struct device *dev,
296  struct device_attribute *attr,
297  char *buf)
298 {
299  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
300  struct sca3000_state *st = iio_priv(indio_dev);
301  int len = 0;
302 
303  len += sprintf(buf + len, "0 - normal mode");
304  switch (st->info->option_mode_1) {
306  len += sprintf(buf + len, ", 1 - narrow mode");
307  break;
309  len += sprintf(buf + len, ", 1 - bypass mode");
310  break;
311  }
312  switch (st->info->option_mode_2) {
314  len += sprintf(buf + len, ", 2 - wide mode");
315  break;
316  }
317  /* always supported */
318  len += sprintf(buf + len, " 3 - motion detection\n");
319 
320  return len;
321 }
322 
326 static ssize_t
327 sca3000_show_measurement_mode(struct device *dev,
328  struct device_attribute *attr,
329  char *buf)
330 {
331  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
332  struct sca3000_state *st = iio_priv(indio_dev);
333  int len = 0, ret;
334 
335  mutex_lock(&st->lock);
337  if (ret)
338  goto error_ret;
339  /* mask bottom 2 bits - only ones that are relevant */
340  st->rx[0] &= 0x03;
341  switch (st->rx[0]) {
343  len += sprintf(buf + len, "0 - normal mode\n");
344  break;
346  len += sprintf(buf + len, "3 - motion detection\n");
347  break;
349  switch (st->info->option_mode_1) {
351  len += sprintf(buf + len, "1 - narrow mode\n");
352  break;
354  len += sprintf(buf + len, "1 - bypass mode\n");
355  break;
356  }
357  break;
359  switch (st->info->option_mode_2) {
361  len += sprintf(buf + len, "2 - wide mode\n");
362  break;
363  }
364  break;
365  }
366 
367 error_ret:
368  mutex_unlock(&st->lock);
369 
370  return ret ? ret : len;
371 }
372 
376 static ssize_t
377 sca3000_store_measurement_mode(struct device *dev,
378  struct device_attribute *attr,
379  const char *buf,
380  size_t len)
381 {
382  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
383  struct sca3000_state *st = iio_priv(indio_dev);
384  int ret;
385  u8 mask = 0x03;
386  u8 val;
387 
388  mutex_lock(&st->lock);
389  ret = kstrtou8(buf, 10, &val);
390  if (ret)
391  goto error_ret;
392  if (val > 3) {
393  ret = -EINVAL;
394  goto error_ret;
395  }
397  if (ret)
398  goto error_ret;
399  st->rx[0] &= ~mask;
400  st->rx[0] |= (val & mask);
401  ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, st->rx[0]);
402  if (ret)
403  goto error_ret;
404  mutex_unlock(&st->lock);
405 
406  return len;
407 
408 error_ret:
409  mutex_unlock(&st->lock);
410 
411  return ret;
412 }
413 
414 
415 /* Not even vaguely standard attributes so defined here rather than
416  * in the relevant IIO core headers
417  */
418 static IIO_DEVICE_ATTR(measurement_mode_available, S_IRUGO,
419  sca3000_show_available_measurement_modes,
420  NULL, 0);
421 
422 static IIO_DEVICE_ATTR(measurement_mode, S_IRUGO | S_IWUSR,
423  sca3000_show_measurement_mode,
424  sca3000_store_measurement_mode,
425  0);
426 
427 /* More standard attributes */
428 
429 static IIO_DEVICE_ATTR(revision, S_IRUGO, sca3000_show_rev, NULL, 0);
430 
431 #define SCA3000_INFO_MASK \
432  IIO_CHAN_INFO_RAW_SEPARATE_BIT | IIO_CHAN_INFO_SCALE_SHARED_BIT
433 #define SCA3000_EVENT_MASK \
434  (IIO_EV_BIT(IIO_EV_TYPE_MAG, IIO_EV_DIR_RISING))
435 
436 #define SCA3000_CHAN(index, mod) \
437  { \
438  .type = IIO_ACCEL, \
439  .modified = 1, \
440  .channel2 = mod, \
441  .info_mask = SCA3000_INFO_MASK, \
442  .address = index, \
443  .scan_index = index, \
444  .scan_type = { \
445  .sign = 's', \
446  .realbits = 11, \
447  .storagebits = 16, \
448  .shift = 5, \
449  }, \
450  .event_mask = SCA3000_EVENT_MASK, \
451  }
452 
453 static const struct iio_chan_spec sca3000_channels[] = {
457 };
458 
459 static u8 sca3000_addresses[3][3] = {
466 };
467 
468 static int sca3000_read_raw(struct iio_dev *indio_dev,
469  struct iio_chan_spec const *chan,
470  int *val,
471  int *val2,
472  long mask)
473 {
474  struct sca3000_state *st = iio_priv(indio_dev);
475  int ret;
476  u8 address;
477 
478  switch (mask) {
479  case IIO_CHAN_INFO_RAW:
480  mutex_lock(&st->lock);
481  if (st->mo_det_use_count) {
482  mutex_unlock(&st->lock);
483  return -EBUSY;
484  }
485  address = sca3000_addresses[chan->address][0];
486  ret = sca3000_read_data_short(st, address, 2);
487  if (ret < 0) {
488  mutex_unlock(&st->lock);
489  return ret;
490  }
491  *val = (be16_to_cpup((__be16 *)st->rx) >> 3) & 0x1FFF;
492  *val = ((*val) << (sizeof(*val)*8 - 13)) >>
493  (sizeof(*val)*8 - 13);
494  mutex_unlock(&st->lock);
495  return IIO_VAL_INT;
496  case IIO_CHAN_INFO_SCALE:
497  *val = 0;
498  if (chan->type == IIO_ACCEL)
499  *val2 = st->info->scale;
500  else /* temperature */
501  *val2 = 555556;
502  return IIO_VAL_INT_PLUS_MICRO;
503  default:
504  return -EINVAL;
505  }
506 }
507 
516 static ssize_t sca3000_read_av_freq(struct device *dev,
517  struct device_attribute *attr,
518  char *buf)
519 {
520  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
521  struct sca3000_state *st = iio_priv(indio_dev);
522  int len = 0, ret, val;
523 
524  mutex_lock(&st->lock);
526  val = st->rx[0];
527  mutex_unlock(&st->lock);
528  if (ret)
529  goto error_ret;
530 
531  switch (val & 0x03) {
533  len += sprintf(buf + len, "%d %d %d\n",
534  st->info->measurement_mode_freq,
535  st->info->measurement_mode_freq/2,
536  st->info->measurement_mode_freq/4);
537  break;
539  len += sprintf(buf + len, "%d %d %d\n",
540  st->info->option_mode_1_freq,
541  st->info->option_mode_1_freq/2,
542  st->info->option_mode_1_freq/4);
543  break;
545  len += sprintf(buf + len, "%d %d %d\n",
546  st->info->option_mode_2_freq,
547  st->info->option_mode_2_freq/2,
548  st->info->option_mode_2_freq/4);
549  break;
550  }
551  return len;
552 error_ret:
553  return ret;
554 }
560 static inline int __sca3000_get_base_freq(struct sca3000_state *st,
561  const struct sca3000_chip_info *info,
562  int *base_freq)
563 {
564  int ret;
565 
567  if (ret)
568  goto error_ret;
569  switch (0x03 & st->rx[0]) {
571  *base_freq = info->measurement_mode_freq;
572  break;
574  *base_freq = info->option_mode_1_freq;
575  break;
577  *base_freq = info->option_mode_2_freq;
578  break;
579  }
580 error_ret:
581  return ret;
582 }
583 
587 static ssize_t sca3000_read_frequency(struct device *dev,
588  struct device_attribute *attr,
589  char *buf)
590 {
591  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
592  struct sca3000_state *st = iio_priv(indio_dev);
593  int ret, len = 0, base_freq = 0, val;
594 
595  mutex_lock(&st->lock);
596  ret = __sca3000_get_base_freq(st, st->info, &base_freq);
597  if (ret)
598  goto error_ret_mut;
599  ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
600  mutex_unlock(&st->lock);
601  if (ret)
602  goto error_ret;
603  val = ret;
604  if (base_freq > 0)
605  switch (val & 0x03) {
606  case 0x00:
607  case 0x03:
608  len = sprintf(buf, "%d\n", base_freq);
609  break;
610  case 0x01:
611  len = sprintf(buf, "%d\n", base_freq/2);
612  break;
613  case 0x02:
614  len = sprintf(buf, "%d\n", base_freq/4);
615  break;
616  }
617 
618  return len;
619 error_ret_mut:
620  mutex_unlock(&st->lock);
621 error_ret:
622  return ret;
623 }
624 
628 static ssize_t sca3000_set_frequency(struct device *dev,
629  struct device_attribute *attr,
630  const char *buf,
631  size_t len)
632 {
633  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
634  struct sca3000_state *st = iio_priv(indio_dev);
635  int ret, base_freq = 0;
636  int ctrlval;
637  long val;
638 
639  ret = strict_strtol(buf, 10, &val);
640  if (ret)
641  return ret;
642 
643  mutex_lock(&st->lock);
644  /* What mode are we in? */
645  ret = __sca3000_get_base_freq(st, st->info, &base_freq);
646  if (ret)
647  goto error_free_lock;
648 
649  ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
650  if (ret < 0)
651  goto error_free_lock;
652  ctrlval = ret;
653  /* clear the bits */
654  ctrlval &= ~0x03;
655 
656  if (val == base_freq/2) {
657  ctrlval |= SCA3000_OUT_CTRL_BUF_DIV_2;
658  } else if (val == base_freq/4) {
659  ctrlval |= SCA3000_OUT_CTRL_BUF_DIV_4;
660  } else if (val != base_freq) {
661  ret = -EINVAL;
662  goto error_free_lock;
663  }
664  ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL,
665  ctrlval);
666 error_free_lock:
667  mutex_unlock(&st->lock);
668 
669  return ret ? ret : len;
670 }
671 
672 /* Should only really be registered if ring buffer support is compiled in.
673  * Does no harm however and doing it right would add a fair bit of complexity
674  */
675 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sca3000_read_av_freq);
676 
678  sca3000_read_frequency,
679  sca3000_set_frequency);
680 
681 
689 static ssize_t sca3000_read_temp(struct device *dev,
690  struct device_attribute *attr,
691  char *buf)
692 {
693  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
694  struct sca3000_state *st = iio_priv(indio_dev);
695  int ret;
696  int val;
698  if (ret < 0)
699  goto error_ret;
700  val = ((st->rx[0] & 0x3F) << 3) | ((st->rx[1] & 0xE0) >> 5);
701 
702  return sprintf(buf, "%d\n", val);
703 
704 error_ret:
705  return ret;
706 }
707 static IIO_DEV_ATTR_TEMP_RAW(sca3000_read_temp);
708 
709 static IIO_CONST_ATTR_TEMP_SCALE("0.555556");
710 static IIO_CONST_ATTR_TEMP_OFFSET("-214.6");
711 
715 static int sca3000_read_thresh(struct iio_dev *indio_dev,
716  u64 e,
717  int *val)
718 {
719  int ret, i;
720  struct sca3000_state *st = iio_priv(indio_dev);
722  mutex_lock(&st->lock);
723  ret = sca3000_read_ctrl_reg(st, sca3000_addresses[num][1]);
724  mutex_unlock(&st->lock);
725  if (ret < 0)
726  return ret;
727  *val = 0;
728  if (num == 1)
729  for_each_set_bit(i, (unsigned long *)&ret,
730  ARRAY_SIZE(st->info->mot_det_mult_y))
731  *val += st->info->mot_det_mult_y[i];
732  else
733  for_each_set_bit(i, (unsigned long *)&ret,
734  ARRAY_SIZE(st->info->mot_det_mult_xz))
735  *val += st->info->mot_det_mult_xz[i];
736 
737  return 0;
738 }
739 
743 static int sca3000_write_thresh(struct iio_dev *indio_dev,
744  u64 e,
745  int val)
746 {
747  struct sca3000_state *st = iio_priv(indio_dev);
748  int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
749  int ret;
750  int i;
751  u8 nonlinear = 0;
752 
753  if (num == 1) {
754  i = ARRAY_SIZE(st->info->mot_det_mult_y);
755  while (i > 0)
756  if (val >= st->info->mot_det_mult_y[--i]) {
757  nonlinear |= (1 << i);
758  val -= st->info->mot_det_mult_y[i];
759  }
760  } else {
761  i = ARRAY_SIZE(st->info->mot_det_mult_xz);
762  while (i > 0)
763  if (val >= st->info->mot_det_mult_xz[--i]) {
764  nonlinear |= (1 << i);
765  val -= st->info->mot_det_mult_xz[i];
766  }
767  }
768 
769  mutex_lock(&st->lock);
770  ret = sca3000_write_ctrl_reg(st, sca3000_addresses[num][1], nonlinear);
771  mutex_unlock(&st->lock);
772 
773  return ret;
774 }
775 
776 static struct attribute *sca3000_attributes[] = {
777  &iio_dev_attr_revision.dev_attr.attr,
778  &iio_dev_attr_measurement_mode_available.dev_attr.attr,
779  &iio_dev_attr_measurement_mode.dev_attr.attr,
780  &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
781  &iio_dev_attr_sampling_frequency.dev_attr.attr,
782  NULL,
783 };
784 
785 static struct attribute *sca3000_attributes_with_temp[] = {
786  &iio_dev_attr_revision.dev_attr.attr,
787  &iio_dev_attr_measurement_mode_available.dev_attr.attr,
788  &iio_dev_attr_measurement_mode.dev_attr.attr,
789  &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
790  &iio_dev_attr_sampling_frequency.dev_attr.attr,
791  /* Only present if temp sensor is */
792  &iio_dev_attr_in_temp_raw.dev_attr.attr,
793  &iio_const_attr_in_temp_offset.dev_attr.attr,
794  &iio_const_attr_in_temp_scale.dev_attr.attr,
795  NULL,
796 };
797 
798 static const struct attribute_group sca3000_attribute_group = {
799  .attrs = sca3000_attributes,
800 };
801 
802 static const struct attribute_group sca3000_attribute_group_with_temp = {
803  .attrs = sca3000_attributes_with_temp,
804 };
805 
806 /* RING RELATED interrupt handler */
807 /* depending on event, push to the ring buffer event chrdev or the event one */
808 
816 static irqreturn_t sca3000_event_handler(int irq, void *private)
817 {
818  struct iio_dev *indio_dev = private;
819  struct sca3000_state *st = iio_priv(indio_dev);
820  int ret, val;
821  s64 last_timestamp = iio_get_time_ns();
822 
823  /* Could lead if badly timed to an extra read of status reg,
824  * but ensures no interrupt is missed.
825  */
826  mutex_lock(&st->lock);
828  val = st->rx[0];
829  mutex_unlock(&st->lock);
830  if (ret)
831  goto done;
832 
833  sca3000_ring_int_process(val, indio_dev->buffer);
834 
836  iio_push_event(indio_dev,
838  0,
842  last_timestamp);
843 
845  iio_push_event(indio_dev,
847  0,
848  IIO_MOD_Y,
851  last_timestamp);
852 
854  iio_push_event(indio_dev,
856  0,
857  IIO_MOD_X,
860  last_timestamp);
861 
863  iio_push_event(indio_dev,
865  0,
866  IIO_MOD_Z,
869  last_timestamp);
870 
871 done:
872  return IRQ_HANDLED;
873 }
874 
878 static int sca3000_read_event_config(struct iio_dev *indio_dev,
879  u64 e)
880 {
881  struct sca3000_state *st = iio_priv(indio_dev);
882  int ret;
883  u8 protect_mask = 0x03;
884  int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
885 
886  /* read current value of mode register */
887  mutex_lock(&st->lock);
889  if (ret)
890  goto error_ret;
891 
892  if ((st->rx[0] & protect_mask) != SCA3000_MEAS_MODE_MOT_DET)
893  ret = 0;
894  else {
895  ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
896  if (ret < 0)
897  goto error_ret;
898  /* only supporting logical or's for now */
899  ret = !!(ret & sca3000_addresses[num][2]);
900  }
901 error_ret:
902  mutex_unlock(&st->lock);
903 
904  return ret;
905 }
909 static ssize_t sca3000_query_free_fall_mode(struct device *dev,
910  struct device_attribute *attr,
911  char *buf)
912 {
913  int ret, len;
914  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
915  struct sca3000_state *st = iio_priv(indio_dev);
916  int val;
917 
918  mutex_lock(&st->lock);
920  val = st->rx[0];
921  mutex_unlock(&st->lock);
922  if (ret < 0)
923  return ret;
924  len = sprintf(buf, "%d\n",
925  !!(val & SCA3000_FREE_FALL_DETECT));
926  return len;
927 }
928 
937 static ssize_t sca3000_set_free_fall_mode(struct device *dev,
938  struct device_attribute *attr,
939  const char *buf,
940  size_t len)
941 {
942  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
943  struct sca3000_state *st = iio_priv(indio_dev);
944  long val;
945  int ret;
946  u8 protect_mask = SCA3000_FREE_FALL_DETECT;
947 
948  mutex_lock(&st->lock);
949  ret = strict_strtol(buf, 10, &val);
950  if (ret)
951  goto error_ret;
952 
953  /* read current value of mode register */
955  if (ret)
956  goto error_ret;
957 
958  /*if off and should be on*/
959  if (val && !(st->rx[0] & protect_mask))
961  (st->rx[0] | SCA3000_FREE_FALL_DETECT));
962  /* if on and should be off */
963  else if (!val && (st->rx[0] & protect_mask))
965  (st->rx[0] & ~protect_mask));
966 error_ret:
967  mutex_unlock(&st->lock);
968 
969  return ret ? ret : len;
970 }
971 
981 static int sca3000_write_event_config(struct iio_dev *indio_dev,
982  u64 e,
983  int state)
984 {
985  struct sca3000_state *st = iio_priv(indio_dev);
986  int ret, ctrlval;
987  u8 protect_mask = 0x03;
988  int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
989 
990  mutex_lock(&st->lock);
991  /* First read the motion detector config to find out if
992  * this axis is on*/
993  ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
994  if (ret < 0)
995  goto exit_point;
996  ctrlval = ret;
997  /* Off and should be on */
998  if (state && !(ctrlval & sca3000_addresses[num][2])) {
999  ret = sca3000_write_ctrl_reg(st,
1001  ctrlval |
1002  sca3000_addresses[num][2]);
1003  if (ret)
1004  goto exit_point;
1005  st->mo_det_use_count++;
1006  } else if (!state && (ctrlval & sca3000_addresses[num][2])) {
1007  ret = sca3000_write_ctrl_reg(st,
1009  ctrlval &
1010  ~(sca3000_addresses[num][2]));
1011  if (ret)
1012  goto exit_point;
1013  st->mo_det_use_count--;
1014  }
1015 
1016  /* read current value of mode register */
1018  if (ret)
1019  goto exit_point;
1020  /*if off and should be on*/
1021  if ((st->mo_det_use_count)
1022  && ((st->rx[0] & protect_mask) != SCA3000_MEAS_MODE_MOT_DET))
1024  (st->rx[0] & ~protect_mask)
1026  /* if on and should be off */
1027  else if (!(st->mo_det_use_count)
1028  && ((st->rx[0] & protect_mask) == SCA3000_MEAS_MODE_MOT_DET))
1030  (st->rx[0] & ~protect_mask));
1031 exit_point:
1032  mutex_unlock(&st->lock);
1033 
1034  return ret;
1035 }
1036 
1037 /* Free fall detector related event attribute */
1038 static IIO_DEVICE_ATTR_NAMED(accel_xayaz_mag_falling_en,
1039  in_accel_x&y&z_mag_falling_en,
1040  S_IRUGO | S_IWUSR,
1041  sca3000_query_free_fall_mode,
1042  sca3000_set_free_fall_mode,
1043  0);
1044 
1045 static IIO_CONST_ATTR_NAMED(accel_xayaz_mag_falling_period,
1046  in_accel_x&y&z_mag_falling_period,
1047  "0.226");
1048 
1049 static struct attribute *sca3000_event_attributes[] = {
1050  &iio_dev_attr_accel_xayaz_mag_falling_en.dev_attr.attr,
1051  &iio_const_attr_accel_xayaz_mag_falling_period.dev_attr.attr,
1052  NULL,
1053 };
1054 
1055 static struct attribute_group sca3000_event_attribute_group = {
1056  .attrs = sca3000_event_attributes,
1057  .name = "events",
1058 };
1059 
1067 static int sca3000_clean_setup(struct sca3000_state *st)
1068 {
1069  int ret;
1070 
1071  mutex_lock(&st->lock);
1072  /* Ensure all interrupts have been acknowledged */
1074  if (ret)
1075  goto error_ret;
1076 
1077  /* Turn off all motion detection channels */
1078  ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
1079  if (ret < 0)
1080  goto error_ret;
1081  ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL,
1083  if (ret)
1084  goto error_ret;
1085 
1086  /* Disable ring buffer */
1087  ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
1088  ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL,
1094  if (ret)
1095  goto error_ret;
1096  /* Enable interrupts, relevant to mode and set up as active low */
1098  if (ret)
1099  goto error_ret;
1100  ret = sca3000_write_reg(st,
1104  if (ret)
1105  goto error_ret;
1106  /* Select normal measurement mode, free fall off, ring off */
1107  /* Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5
1108  * as that occurs in one of the example on the datasheet */
1110  if (ret)
1111  goto error_ret;
1113  (st->rx[0] & SCA3000_MODE_PROT_MASK));
1114  st->bpse = 11;
1115 
1116 error_ret:
1117  mutex_unlock(&st->lock);
1118  return ret;
1119 }
1120 
1121 static const struct iio_info sca3000_info = {
1122  .attrs = &sca3000_attribute_group,
1123  .read_raw = &sca3000_read_raw,
1124  .event_attrs = &sca3000_event_attribute_group,
1125  .read_event_value = &sca3000_read_thresh,
1126  .write_event_value = &sca3000_write_thresh,
1127  .read_event_config = &sca3000_read_event_config,
1128  .write_event_config = &sca3000_write_event_config,
1129  .driver_module = THIS_MODULE,
1130 };
1131 
1132 static const struct iio_info sca3000_info_with_temp = {
1133  .attrs = &sca3000_attribute_group_with_temp,
1134  .read_raw = &sca3000_read_raw,
1135  .read_event_value = &sca3000_read_thresh,
1136  .write_event_value = &sca3000_write_thresh,
1137  .read_event_config = &sca3000_read_event_config,
1138  .write_event_config = &sca3000_write_event_config,
1139  .driver_module = THIS_MODULE,
1140 };
1141 
1142 static int __devinit sca3000_probe(struct spi_device *spi)
1143 {
1144  int ret;
1145  struct sca3000_state *st;
1146  struct iio_dev *indio_dev;
1147 
1148  indio_dev = iio_device_alloc(sizeof(*st));
1149  if (indio_dev == NULL) {
1150  ret = -ENOMEM;
1151  goto error_ret;
1152  }
1153 
1154  st = iio_priv(indio_dev);
1155  spi_set_drvdata(spi, indio_dev);
1156  st->us = spi;
1157  mutex_init(&st->lock);
1158  st->info = &sca3000_spi_chip_info_tbl[spi_get_device_id(spi)
1159  ->driver_data];
1160 
1161  indio_dev->dev.parent = &spi->dev;
1162  indio_dev->name = spi_get_device_id(spi)->name;
1163  if (st->info->temp_output)
1164  indio_dev->info = &sca3000_info_with_temp;
1165  else {
1166  indio_dev->info = &sca3000_info;
1167  indio_dev->channels = sca3000_channels;
1168  indio_dev->num_channels = ARRAY_SIZE(sca3000_channels);
1169  }
1170  indio_dev->modes = INDIO_DIRECT_MODE;
1171 
1172  sca3000_configure_ring(indio_dev);
1173  ret = iio_device_register(indio_dev);
1174  if (ret < 0)
1175  goto error_free_dev;
1176 
1177  ret = iio_buffer_register(indio_dev,
1178  sca3000_channels,
1179  ARRAY_SIZE(sca3000_channels));
1180  if (ret < 0)
1181  goto error_unregister_dev;
1182  if (indio_dev->buffer) {
1183  iio_scan_mask_set(indio_dev, indio_dev->buffer, 0);
1184  iio_scan_mask_set(indio_dev, indio_dev->buffer, 1);
1185  iio_scan_mask_set(indio_dev, indio_dev->buffer, 2);
1186  }
1187 
1188  if (spi->irq) {
1189  ret = request_threaded_irq(spi->irq,
1190  NULL,
1191  &sca3000_event_handler,
1193  "sca3000",
1194  indio_dev);
1195  if (ret)
1196  goto error_unregister_ring;
1197  }
1198  sca3000_register_ring_funcs(indio_dev);
1199  ret = sca3000_clean_setup(st);
1200  if (ret)
1201  goto error_free_irq;
1202  return 0;
1203 
1204 error_free_irq:
1205  if (spi->irq)
1206  free_irq(spi->irq, indio_dev);
1207 error_unregister_ring:
1208  iio_buffer_unregister(indio_dev);
1209 error_unregister_dev:
1210  iio_device_unregister(indio_dev);
1211 error_free_dev:
1212  iio_device_free(indio_dev);
1213 
1214 error_ret:
1215  return ret;
1216 }
1217 
1218 static int sca3000_stop_all_interrupts(struct sca3000_state *st)
1219 {
1220  int ret;
1221 
1222  mutex_lock(&st->lock);
1224  if (ret)
1225  goto error_ret;
1227  (st->rx[0] &
1231 error_ret:
1232  mutex_unlock(&st->lock);
1233  return ret;
1234 }
1235 
1236 static int __devexit sca3000_remove(struct spi_device *spi)
1237 {
1238  struct iio_dev *indio_dev = spi_get_drvdata(spi);
1239  struct sca3000_state *st = iio_priv(indio_dev);
1240 
1241  /* Must ensure no interrupts can be generated after this!*/
1242  sca3000_stop_all_interrupts(st);
1243  if (spi->irq)
1244  free_irq(spi->irq, indio_dev);
1245  iio_device_unregister(indio_dev);
1246  iio_buffer_unregister(indio_dev);
1247  sca3000_unconfigure_ring(indio_dev);
1248  iio_device_free(indio_dev);
1249 
1250  return 0;
1251 }
1252 
1253 static const struct spi_device_id sca3000_id[] = {
1254  {"sca3000_d01", d01},
1255  {"sca3000_e02", e02},
1256  {"sca3000_e04", e04},
1257  {"sca3000_e05", e05},
1258  {}
1259 };
1260 MODULE_DEVICE_TABLE(spi, sca3000_id);
1261 
1262 static struct spi_driver sca3000_driver = {
1263  .driver = {
1264  .name = "sca3000",
1265  .owner = THIS_MODULE,
1266  },
1267  .probe = sca3000_probe,
1268  .remove = __devexit_p(sca3000_remove),
1269  .id_table = sca3000_id,
1270 };
1271 module_spi_driver(sca3000_driver);
1272 
1273 MODULE_AUTHOR("Jonathan Cameron <[email protected]>");
1274 MODULE_DESCRIPTION("VTI SCA3000 Series Accelerometers SPI driver");
1275 MODULE_LICENSE("GPL v2");