Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
adis16204_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 "adis16204.h"
12 
18 static int adis16204_read_ring_data(struct iio_dev *indio_dev, u8 *rx)
19 {
20  struct spi_message msg;
21  struct adis16204_state *st = iio_priv(indio_dev);
22  struct spi_transfer xfers[ADIS16204_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 <= ADIS16204_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 = 20;
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 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
55  * specific to be rolled into the core.
56  */
57 static irqreturn_t adis16204_trigger_handler(int irq, void *p)
58 {
59  struct iio_poll_func *pf = p;
60  struct iio_dev *indio_dev = pf->indio_dev;
61  struct adis16204_state *st = iio_priv(indio_dev);
62  int i = 0;
63  s16 *data;
64 
65  data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
66  if (data == NULL) {
67  dev_err(&st->us->dev, "memory alloc failed in ring bh");
68  goto done;
69  }
70 
71  if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength) &&
72  adis16204_read_ring_data(indio_dev, st->rx) >= 0)
73  for (; i < bitmap_weight(indio_dev->active_scan_mask,
74  indio_dev->masklength); i++)
75  data[i] = be16_to_cpup((__be16 *)&(st->rx[i*2]));
76 
77  /* Guaranteed to be aligned with 8 byte boundary */
78  if (indio_dev->scan_timestamp)
79  *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp;
80 
81  iio_push_to_buffer(indio_dev->buffer, (u8 *)data);
82 
83  kfree(data);
84 done:
85  iio_trigger_notify_done(indio_dev->trig);
86 
87  return IRQ_HANDLED;
88 }
89 
90 void adis16204_unconfigure_ring(struct iio_dev *indio_dev)
91 {
92  iio_dealloc_pollfunc(indio_dev->pollfunc);
93  iio_sw_rb_free(indio_dev->buffer);
94 }
95 
96 static const struct iio_buffer_setup_ops adis16204_ring_setup_ops = {
97  .preenable = &iio_sw_buffer_preenable,
98  .postenable = &iio_triggered_buffer_postenable,
99  .predisable = &iio_triggered_buffer_predisable,
100 };
101 
102 int adis16204_configure_ring(struct iio_dev *indio_dev)
103 {
104  int ret = 0;
105  struct iio_buffer *ring;
106 
107  ring = iio_sw_rb_allocate(indio_dev);
108  if (!ring) {
109  ret = -ENOMEM;
110  return ret;
111  }
112  indio_dev->buffer = ring;
113  ring->scan_timestamp = true;
114  indio_dev->setup_ops = &adis16204_ring_setup_ops;
115 
117  &adis16204_trigger_handler,
118  IRQF_ONESHOT,
119  indio_dev,
120  "%s_consumer%d",
121  indio_dev->name,
122  indio_dev->id);
123  if (indio_dev->pollfunc == NULL) {
124  ret = -ENOMEM;
125  goto error_iio_sw_rb_free;
126  }
127 
128  indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
129  return 0;
130 
131 error_iio_sw_rb_free:
132  iio_sw_rb_free(indio_dev->buffer);
133  return ret;
134 }