Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ad7298_ring.c
Go to the documentation of this file.
1 /*
2  * AD7298 SPI ADC driver
3  *
4  * Copyright 2011-2012 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2.
7  */
8 
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/spi/spi.h>
13 
14 #include <linux/iio/iio.h>
15 #include <linux/iio/buffer.h>
18 
19 #include "ad7298.h"
20 
24 int ad7298_update_scan_mode(struct iio_dev *indio_dev,
25  const unsigned long *active_scan_mask)
26 {
27  struct ad7298_state *st = iio_priv(indio_dev);
28  int i, m;
29  unsigned short command;
30  int scan_count;
31 
32  /* Now compute overall size */
33  scan_count = bitmap_weight(active_scan_mask, indio_dev->masklength);
34 
35  command = AD7298_WRITE | st->ext_ref;
36 
37  for (i = 0, m = AD7298_CH(0); i < AD7298_MAX_CHAN; i++, m >>= 1)
38  if (test_bit(i, active_scan_mask))
39  command |= m;
40 
41  st->tx_buf[0] = cpu_to_be16(command);
42 
43  /* build spi ring message */
44  st->ring_xfer[0].tx_buf = &st->tx_buf[0];
45  st->ring_xfer[0].len = 2;
46  st->ring_xfer[0].cs_change = 1;
47  st->ring_xfer[1].tx_buf = &st->tx_buf[1];
48  st->ring_xfer[1].len = 2;
49  st->ring_xfer[1].cs_change = 1;
50 
51  spi_message_init(&st->ring_msg);
52  spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
53  spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
54 
55  for (i = 0; i < scan_count; i++) {
56  st->ring_xfer[i + 2].rx_buf = &st->rx_buf[i];
57  st->ring_xfer[i + 2].len = 2;
58  st->ring_xfer[i + 2].cs_change = 1;
59  spi_message_add_tail(&st->ring_xfer[i + 2], &st->ring_msg);
60  }
61  /* make sure last transfer cs_change is not set */
62  st->ring_xfer[i + 1].cs_change = 0;
63 
64  return 0;
65 }
66 
73 static irqreturn_t ad7298_trigger_handler(int irq, void *p)
74 {
75  struct iio_poll_func *pf = p;
76  struct iio_dev *indio_dev = pf->indio_dev;
77  struct ad7298_state *st = iio_priv(indio_dev);
78  s64 time_ns = 0;
79  __u16 buf[16];
80  int b_sent, i;
81 
82  b_sent = spi_sync(st->spi, &st->ring_msg);
83  if (b_sent)
84  goto done;
85 
86  if (indio_dev->scan_timestamp) {
87  time_ns = iio_get_time_ns();
88  memcpy((u8 *)buf + indio_dev->scan_bytes - sizeof(s64),
89  &time_ns, sizeof(time_ns));
90  }
91 
92  for (i = 0; i < bitmap_weight(indio_dev->active_scan_mask,
93  indio_dev->masklength); i++)
94  buf[i] = be16_to_cpu(st->rx_buf[i]);
95 
96  iio_push_to_buffer(indio_dev->buffer, (u8 *)buf);
97 
98 done:
99  iio_trigger_notify_done(indio_dev->trig);
100 
101  return IRQ_HANDLED;
102 }
103 
105 {
106  return iio_triggered_buffer_setup(indio_dev, NULL,
107  &ad7298_trigger_handler, NULL);
108 }
109 
110 void ad7298_ring_cleanup(struct iio_dev *indio_dev)
111 {
112  iio_triggered_buffer_cleanup(indio_dev);
113 }