Merge remote branch 'alsa/devel' into topic/misc
[pandora-kernel.git] / drivers / staging / iio / imu / adis16300_ring.c
1 #include <linux/interrupt.h>
2 #include <linux/irq.h>
3 #include <linux/gpio.h>
4 #include <linux/workqueue.h>
5 #include <linux/mutex.h>
6 #include <linux/device.h>
7 #include <linux/kernel.h>
8 #include <linux/spi/spi.h>
9 #include <linux/slab.h>
10 #include <linux/sysfs.h>
11 #include <linux/list.h>
12
13 #include "../iio.h"
14 #include "../sysfs.h"
15 #include "../ring_sw.h"
16 #include "../accel/accel.h"
17 #include "../trigger.h"
18 #include "adis16300.h"
19
20 static IIO_SCAN_EL_C(supply, ADIS16300_SCAN_SUPPLY, IIO_UNSIGNED(14),
21                      ADIS16300_SUPPLY_OUT, NULL);
22
23 static IIO_SCAN_EL_C(gyro_x, ADIS16300_SCAN_GYRO_X, IIO_SIGNED(14),
24                      ADIS16300_XGYRO_OUT, NULL);
25
26 static IIO_SCAN_EL_C(accel_x, ADIS16300_SCAN_ACC_X, IIO_SIGNED(14),
27                      ADIS16300_XACCL_OUT, NULL);
28 static IIO_SCAN_EL_C(accel_y, ADIS16300_SCAN_ACC_Y, IIO_SIGNED(14),
29                      ADIS16300_YACCL_OUT, NULL);
30 static IIO_SCAN_EL_C(accel_z, ADIS16300_SCAN_ACC_Z, IIO_SIGNED(14),
31                      ADIS16300_ZACCL_OUT, NULL);
32
33 static IIO_SCAN_EL_C(temp, ADIS16300_SCAN_TEMP, IIO_UNSIGNED(12),
34                      ADIS16300_TEMP_OUT, NULL);
35 static IIO_SCAN_EL_C(adc_0, ADIS16300_SCAN_ADC_0, IIO_UNSIGNED(12),
36                      ADIS16300_AUX_ADC, NULL);
37
38 static IIO_SCAN_EL_C(incli_x, ADIS16300_SCAN_INCLI_X, IIO_SIGNED(12),
39                      ADIS16300_XINCLI_OUT, NULL);
40 static IIO_SCAN_EL_C(incli_y, ADIS16300_SCAN_INCLI_Y, IIO_SIGNED(12),
41                      ADIS16300_YINCLI_OUT, NULL);
42
43 static IIO_SCAN_EL_TIMESTAMP(9);
44
45 static struct attribute *adis16300_scan_el_attrs[] = {
46         &iio_scan_el_supply.dev_attr.attr,
47         &iio_scan_el_gyro_x.dev_attr.attr,
48         &iio_scan_el_temp.dev_attr.attr,
49         &iio_scan_el_accel_x.dev_attr.attr,
50         &iio_scan_el_accel_y.dev_attr.attr,
51         &iio_scan_el_accel_z.dev_attr.attr,
52         &iio_scan_el_incli_x.dev_attr.attr,
53         &iio_scan_el_incli_y.dev_attr.attr,
54         &iio_scan_el_adc_0.dev_attr.attr,
55         &iio_scan_el_timestamp.dev_attr.attr,
56         NULL,
57 };
58
59 static struct attribute_group adis16300_scan_el_group = {
60         .attrs = adis16300_scan_el_attrs,
61         .name = "scan_elements",
62 };
63
64 /**
65  * adis16300_poll_func_th() top half interrupt handler called by trigger
66  * @private_data:       iio_dev
67  **/
68 static void adis16300_poll_func_th(struct iio_dev *indio_dev, s64 time)
69 {
70         struct adis16300_state *st = iio_dev_get_devdata(indio_dev);
71         st->last_timestamp = time;
72         schedule_work(&st->work_trigger_to_ring);
73         /* Indicate that this interrupt is being handled */
74
75         /* Technically this is trigger related, but without this
76          * handler running there is currently no way for the interrupt
77          * to clear.
78          */
79 }
80
81 /**
82  * adis16300_spi_read_burst() - read all data registers
83  * @dev: device associated with child of actual device (iio_dev or iio_trig)
84  * @rx: somewhere to pass back the value read (min size is 24 bytes)
85  **/
86 static int adis16300_spi_read_burst(struct device *dev, u8 *rx)
87 {
88         struct spi_message msg;
89         struct iio_dev *indio_dev = dev_get_drvdata(dev);
90         struct adis16300_state *st = iio_dev_get_devdata(indio_dev);
91         u32 old_speed_hz = st->us->max_speed_hz;
92         int ret;
93
94         struct spi_transfer xfers[] = {
95                 {
96                         .tx_buf = st->tx,
97                         .bits_per_word = 8,
98                         .len = 2,
99                         .cs_change = 0,
100                 }, {
101                         .rx_buf = rx,
102                         .bits_per_word = 8,
103                         .len = 18,
104                         .cs_change = 0,
105                 },
106         };
107
108         mutex_lock(&st->buf_lock);
109         st->tx[0] = ADIS16300_READ_REG(ADIS16300_GLOB_CMD);
110         st->tx[1] = 0;
111
112         spi_message_init(&msg);
113         spi_message_add_tail(&xfers[0], &msg);
114         spi_message_add_tail(&xfers[1], &msg);
115
116         st->us->max_speed_hz = ADIS16300_SPI_BURST;
117         spi_setup(st->us);
118
119         ret = spi_sync(st->us, &msg);
120         if (ret)
121                 dev_err(&st->us->dev, "problem when burst reading");
122
123         st->us->max_speed_hz = old_speed_hz;
124         spi_setup(st->us);
125         mutex_unlock(&st->buf_lock);
126         return ret;
127 }
128
129 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
130  * specific to be rolled into the core.
131  */
132 static void adis16300_trigger_bh_to_ring(struct work_struct *work_s)
133 {
134         struct adis16300_state *st
135                 = container_of(work_s, struct adis16300_state,
136                                work_trigger_to_ring);
137
138         int i = 0;
139         s16 *data;
140         size_t datasize = st->indio_dev
141                 ->ring->access.get_bpd(st->indio_dev->ring);
142
143         data = kmalloc(datasize , GFP_KERNEL);
144         if (data == NULL) {
145                 dev_err(&st->us->dev, "memory alloc failed in ring bh");
146                 return;
147         }
148
149         if (st->indio_dev->scan_count)
150                 if (adis16300_spi_read_burst(&st->indio_dev->dev, st->rx) >= 0)
151                         for (; i < st->indio_dev->scan_count; i++)
152                                 data[i] = be16_to_cpup(
153                                         (__be16 *)&(st->rx[i*2]));
154
155         /* Guaranteed to be aligned with 8 byte boundary */
156         if (st->indio_dev->scan_timestamp)
157                 *((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp;
158
159         st->indio_dev->ring->access.store_to(st->indio_dev->ring,
160                                             (u8 *)data,
161                                             st->last_timestamp);
162
163         iio_trigger_notify_done(st->indio_dev->trig);
164         kfree(data);
165
166         return;
167 }
168
169 void adis16300_unconfigure_ring(struct iio_dev *indio_dev)
170 {
171         kfree(indio_dev->pollfunc);
172         iio_sw_rb_free(indio_dev->ring);
173 }
174
175 int adis16300_configure_ring(struct iio_dev *indio_dev)
176 {
177         int ret = 0;
178         struct adis16300_state *st = indio_dev->dev_data;
179         struct iio_ring_buffer *ring;
180         INIT_WORK(&st->work_trigger_to_ring, adis16300_trigger_bh_to_ring);
181         /* Set default scan mode */
182
183         iio_scan_mask_set(indio_dev, iio_scan_el_supply.number);
184         iio_scan_mask_set(indio_dev, iio_scan_el_gyro_x.number);
185         iio_scan_mask_set(indio_dev, iio_scan_el_accel_x.number);
186         iio_scan_mask_set(indio_dev, iio_scan_el_accel_y.number);
187         iio_scan_mask_set(indio_dev, iio_scan_el_accel_z.number);
188         iio_scan_mask_set(indio_dev, iio_scan_el_temp.number);
189         iio_scan_mask_set(indio_dev, iio_scan_el_adc_0.number);
190         iio_scan_mask_set(indio_dev, iio_scan_el_incli_x.number);
191         iio_scan_mask_set(indio_dev, iio_scan_el_incli_y.number);
192         indio_dev->scan_timestamp = true;
193
194         indio_dev->scan_el_attrs = &adis16300_scan_el_group;
195
196         ring = iio_sw_rb_allocate(indio_dev);
197         if (!ring) {
198                 ret = -ENOMEM;
199                 return ret;
200         }
201         indio_dev->ring = ring;
202         /* Effectively select the ring buffer implementation */
203         iio_ring_sw_register_funcs(&ring->access);
204         ring->bpe = 2;
205         ring->preenable = &iio_sw_ring_preenable;
206         ring->postenable = &iio_triggered_ring_postenable;
207         ring->predisable = &iio_triggered_ring_predisable;
208         ring->owner = THIS_MODULE;
209
210         ret = iio_alloc_pollfunc(indio_dev, NULL, &adis16300_poll_func_th);
211         if (ret)
212                 goto error_iio_sw_rb_free;
213
214         indio_dev->modes |= INDIO_RING_TRIGGERED;
215         return 0;
216
217 error_iio_sw_rb_free:
218         iio_sw_rb_free(indio_dev->ring);
219         return ret;
220 }
221