Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
adis16203_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 "adis16203.h"
12 
18 static int adis16203_read_ring_data(struct iio_dev *indio_dev, u8 *rx)
19 {
20  struct spi_message msg;
21  struct adis16203_state *st = iio_priv(indio_dev);
22  struct spi_transfer xfers[ADIS16203_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 <= ADIS16203_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  if (i < 1) /* SUPPLY_OUT: 0x02, AUX_ADC: 0x08 */
38  st->tx[2 * i] = ADIS16203_READ_REG(ADIS16203_SUPPLY_OUT + 2 * i);
39  else
40  st->tx[2 * i] = ADIS16203_READ_REG(ADIS16203_SUPPLY_OUT + 2 * i + 6);
41  st->tx[2 * i + 1] = 0;
42  if (i >= 1)
43  xfers[i].rx_buf = rx + 2 * (i - 1);
44  spi_message_add_tail(&xfers[i], &msg);
45  }
46 
47  ret = spi_sync(st->us, &msg);
48  if (ret)
49  dev_err(&st->us->dev, "problem when burst reading");
50 
51  mutex_unlock(&st->buf_lock);
52 
53  return ret;
54 }
55 
56 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
57  * specific to be rolled into the core.
58  */
59 static irqreturn_t adis16203_trigger_handler(int irq, void *p)
60 {
61  struct iio_poll_func *pf = p;
62  struct iio_dev *indio_dev = pf->indio_dev;
63  struct adis16203_state *st = iio_priv(indio_dev);
64 
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  adis16203_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 adis16203_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 adis16203_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 adis16203_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 = &adis16203_ring_setup_ops;
118 
120  &adis16203_trigger_handler,
121  IRQF_ONESHOT,
122  indio_dev,
123  "adis16203_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 }