Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
adis16240_ring.c
Go to the documentation of this file.
1 #include <linux/export.h>
2 #include <linux/interrupt.h>
3 #include <linux/mutex.h>
4 #include <linux/kernel.h>
5 #include <linux/spi/spi.h>
6 #include <linux/slab.h>
7 
8 #include <linux/iio/iio.h>
9 #include "../ring_sw.h"
11 #include "adis16240.h"
12 
18 static int adis16240_read_ring_data(struct iio_dev *indio_dev, u8 *rx)
19 {
20  struct spi_message msg;
21  struct adis16240_state *st = iio_priv(indio_dev);
22  struct spi_transfer xfers[ADIS16240_OUTPUTS + 1];
23  int ret;
24  int i;
25 
26  mutex_lock(&st->buf_lock);
27 
28  spi_message_init(&msg);
29 
30  memset(xfers, 0, sizeof(xfers));
31  for (i = 0; i <= ADIS16240_OUTPUTS; i++) {
32  xfers[i].bits_per_word = 8;
33  xfers[i].cs_change = 1;
34  xfers[i].len = 2;
35  xfers[i].delay_usecs = 30;
36  xfers[i].tx_buf = st->tx + 2 * i;
37  st->tx[2 * i]
39  st->tx[2 * i + 1] = 0;
40  if (i >= 1)
41  xfers[i].rx_buf = rx + 2 * (i - 1);
42  spi_message_add_tail(&xfers[i], &msg);
43  }
44 
45  ret = spi_sync(st->us, &msg);
46  if (ret)
47  dev_err(&st->us->dev, "problem when burst reading");
48 
49  mutex_unlock(&st->buf_lock);
50 
51  return ret;
52 }
53 
54 static irqreturn_t adis16240_trigger_handler(int irq, void *p)
55 {
56  struct iio_poll_func *pf = p;
57  struct iio_dev *indio_dev = pf->indio_dev;
58  struct adis16240_state *st = iio_priv(indio_dev);
59 
60  int i = 0;
61  s16 *data;
62 
63  data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
64  if (data == NULL) {
65  dev_err(&st->us->dev, "memory alloc failed in ring bh");
66  goto done;
67  }
68 
69  if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength) &&
70  adis16240_read_ring_data(indio_dev, st->rx) >= 0)
71  for (; i < bitmap_weight(indio_dev->active_scan_mask,
72  indio_dev->masklength); i++)
73  data[i] = be16_to_cpup((__be16 *)&(st->rx[i*2]));
74 
75  /* Guaranteed to be aligned with 8 byte boundary */
76  if (indio_dev->scan_timestamp)
77  *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp;
78 
79  iio_push_to_buffer(indio_dev->buffer, (u8 *)data);
80 
81  kfree(data);
82 done:
83  iio_trigger_notify_done(indio_dev->trig);
84 
85  return IRQ_HANDLED;
86 }
87 
88 void adis16240_unconfigure_ring(struct iio_dev *indio_dev)
89 {
90  iio_dealloc_pollfunc(indio_dev->pollfunc);
91  iio_sw_rb_free(indio_dev->buffer);
92 }
93 
94 static const struct iio_buffer_setup_ops adis16240_ring_setup_ops = {
95  .preenable = &iio_sw_buffer_preenable,
96  .postenable = &iio_triggered_buffer_postenable,
97  .predisable = &iio_triggered_buffer_predisable,
98 };
99 
100 int adis16240_configure_ring(struct iio_dev *indio_dev)
101 {
102  int ret = 0;
103  struct iio_buffer *ring;
104 
105  ring = iio_sw_rb_allocate(indio_dev);
106  if (!ring) {
107  ret = -ENOMEM;
108  return ret;
109  }
110  indio_dev->buffer = ring;
111  ring->scan_timestamp = true;
112  indio_dev->setup_ops = &adis16240_ring_setup_ops;
113 
115  &adis16240_trigger_handler,
116  IRQF_ONESHOT,
117  indio_dev,
118  "%s_consumer%d",
119  indio_dev->name,
120  indio_dev->id);
121  if (indio_dev->pollfunc == NULL) {
122  ret = -ENOMEM;
123  goto error_iio_sw_rb_free;
124  }
125 
126  indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
127  return 0;
128 
129 error_iio_sw_rb_free:
130  iio_sw_rb_free(indio_dev->buffer);
131  return ret;
132 }