Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
adis16260_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 "adis16260.h"
12 
18 static int adis16260_read_ring_data(struct iio_dev *indio_dev, u8 *rx)
19 {
20  struct spi_message msg;
21  struct adis16260_state *st = iio_priv(indio_dev);
22  struct spi_transfer xfers[ADIS16260_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 <= ADIS16260_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  if (i < 2) /* SUPPLY_OUT:0x02 GYRO_OUT:0x04 */
38  st->tx[2 * i]
40  + 2 * i);
41  else /* 0x06 to 0x09 is reserved */
42  st->tx[2 * i]
44  + 2 * i + 4);
45  st->tx[2 * i + 1] = 0;
46  if (i >= 1)
47  xfers[i].rx_buf = rx + 2 * (i - 1);
48  spi_message_add_tail(&xfers[i], &msg);
49  }
50 
51  ret = spi_sync(st->us, &msg);
52  if (ret)
53  dev_err(&st->us->dev, "problem when burst reading");
54 
55  mutex_unlock(&st->buf_lock);
56 
57  return ret;
58 }
59 
60 static irqreturn_t adis16260_trigger_handler(int irq, void *p)
61 {
62  struct iio_poll_func *pf = p;
63  struct iio_dev *indio_dev = pf->indio_dev;
64  struct adis16260_state *st = iio_priv(indio_dev);
65  int i = 0;
66  s16 *data;
67 
68  data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
69  if (data == NULL) {
70  dev_err(&st->us->dev, "memory alloc failed in ring bh");
71  goto done;
72  }
73 
74  if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength) &&
75  adis16260_read_ring_data(indio_dev, st->rx) >= 0)
76  for (; i < bitmap_weight(indio_dev->active_scan_mask,
77  indio_dev->masklength); i++)
78  data[i] = be16_to_cpup((__be16 *)&(st->rx[i*2]));
79 
80  /* Guaranteed to be aligned with 8 byte boundary */
81  if (indio_dev->scan_timestamp)
82  *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp;
83 
84  iio_push_to_buffer(indio_dev->buffer, (u8 *)data);
85 
86  kfree(data);
87 done:
88  iio_trigger_notify_done(indio_dev->trig);
89 
90  return IRQ_HANDLED;
91 }
92 
93 void adis16260_unconfigure_ring(struct iio_dev *indio_dev)
94 {
95  iio_dealloc_pollfunc(indio_dev->pollfunc);
96  iio_sw_rb_free(indio_dev->buffer);
97 }
98 
99 static const struct iio_buffer_setup_ops adis16260_ring_setup_ops = {
100  .preenable = &iio_sw_buffer_preenable,
101  .postenable = &iio_triggered_buffer_postenable,
102  .predisable = &iio_triggered_buffer_predisable,
103 };
104 
105 int adis16260_configure_ring(struct iio_dev *indio_dev)
106 {
107  int ret = 0;
108  struct iio_buffer *ring;
109 
110  ring = iio_sw_rb_allocate(indio_dev);
111  if (!ring) {
112  ret = -ENOMEM;
113  return ret;
114  }
115  indio_dev->buffer = ring;
116  ring->scan_timestamp = true;
117  indio_dev->setup_ops = &adis16260_ring_setup_ops;
118 
120  &adis16260_trigger_handler,
121  IRQF_ONESHOT,
122  indio_dev,
123  "adis16260_consumer%d",
124  indio_dev->id);
125  if (indio_dev->pollfunc == NULL) {
126  ret = -ENOMEM;
127  goto error_iio_sw_rb_free;
128  }
129 
130  indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
131  return 0;
132 
133 error_iio_sw_rb_free:
134  iio_sw_rb_free(indio_dev->buffer);
135  return ret;
136 }