fs/dlm: Drop unnecessary null test
[sfrench/cifs-2.6.git] / drivers / staging / iio / gyro / adis16260_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/sysfs.h>
10 #include <linux/list.h>
11
12 #include "../iio.h"
13 #include "../sysfs.h"
14 #include "../ring_sw.h"
15 #include "../accel/accel.h"
16 #include "../trigger.h"
17 #include "adis16260.h"
18
19 /**
20  * combine_8_to_16() utility function to munge to u8s into u16
21  **/
22 static inline u16 combine_8_to_16(u8 lower, u8 upper)
23 {
24         u16 _lower = lower;
25         u16 _upper = upper;
26         return _lower | (_upper << 8);
27 }
28
29 static IIO_SCAN_EL_C(supply, ADIS16260_SCAN_SUPPLY, IIO_UNSIGNED(12),
30                 ADIS16260_SUPPLY_OUT, NULL);
31 static IIO_SCAN_EL_C(gyro, ADIS16260_SCAN_GYRO, IIO_SIGNED(14),
32                 ADIS16260_GYRO_OUT, NULL);
33 static IIO_SCAN_EL_C(aux_adc, ADIS16260_SCAN_AUX_ADC, IIO_SIGNED(14),
34                 ADIS16260_AUX_ADC, NULL);
35 static IIO_SCAN_EL_C(temp, ADIS16260_SCAN_TEMP, IIO_UNSIGNED(12),
36                 ADIS16260_TEMP_OUT, NULL);
37 static IIO_SCAN_EL_C(angl, ADIS16260_SCAN_ANGL, IIO_UNSIGNED(12),
38                 ADIS16260_ANGL_OUT, NULL);
39
40 static IIO_SCAN_EL_TIMESTAMP(5);
41
42 static struct attribute *adis16260_scan_el_attrs[] = {
43         &iio_scan_el_supply.dev_attr.attr,
44         &iio_scan_el_gyro.dev_attr.attr,
45         &iio_scan_el_aux_adc.dev_attr.attr,
46         &iio_scan_el_temp.dev_attr.attr,
47         &iio_scan_el_angl.dev_attr.attr,
48         &iio_scan_el_timestamp.dev_attr.attr,
49         NULL,
50 };
51
52 static struct attribute_group adis16260_scan_el_group = {
53         .attrs = adis16260_scan_el_attrs,
54         .name = "scan_elements",
55 };
56
57 /**
58  * adis16260_poll_func_th() top half interrupt handler called by trigger
59  * @private_data:       iio_dev
60  **/
61 static void adis16260_poll_func_th(struct iio_dev *indio_dev)
62 {
63         struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
64         st->last_timestamp = indio_dev->trig->timestamp;
65         schedule_work(&st->work_trigger_to_ring);
66 }
67
68 /**
69  * adis16260_read_ring_data() read data registers which will be placed into ring
70  * @dev: device associated with child of actual device (iio_dev or iio_trig)
71  * @rx: somewhere to pass back the value read
72  **/
73 static int adis16260_read_ring_data(struct device *dev, u8 *rx)
74 {
75         struct spi_message msg;
76         struct iio_dev *indio_dev = dev_get_drvdata(dev);
77         struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
78         struct spi_transfer xfers[ADIS16260_OUTPUTS + 1];
79         int ret;
80         int i;
81
82         mutex_lock(&st->buf_lock);
83
84         spi_message_init(&msg);
85
86         memset(xfers, 0, sizeof(xfers));
87         for (i = 0; i <= ADIS16260_OUTPUTS; i++) {
88                 xfers[i].bits_per_word = 8;
89                 xfers[i].cs_change = 1;
90                 xfers[i].len = 2;
91                 xfers[i].delay_usecs = 30;
92                 xfers[i].tx_buf = st->tx + 2 * i;
93                 if (i < 2) /* SUPPLY_OUT:0x02 GYRO_OUT:0x04 */
94                         st->tx[2 * i]
95                                 = ADIS16260_READ_REG(ADIS16260_SUPPLY_OUT
96                                                 + 2 * i);
97                 else /* 0x06 to 0x09 is reserved */
98                         st->tx[2 * i]
99                                 = ADIS16260_READ_REG(ADIS16260_SUPPLY_OUT
100                                                 + 2 * i + 4);
101                 st->tx[2 * i + 1] = 0;
102                 if (i >= 1)
103                         xfers[i].rx_buf = rx + 2 * (i - 1);
104                 spi_message_add_tail(&xfers[i], &msg);
105         }
106
107         ret = spi_sync(st->us, &msg);
108         if (ret)
109                 dev_err(&st->us->dev, "problem when burst reading");
110
111         mutex_unlock(&st->buf_lock);
112
113         return ret;
114 }
115
116
117 static void adis16260_trigger_bh_to_ring(struct work_struct *work_s)
118 {
119         struct adis16260_state *st
120                 = container_of(work_s, struct adis16260_state,
121                                 work_trigger_to_ring);
122
123         int i = 0;
124         s16 *data;
125         size_t datasize = st->indio_dev
126                 ->ring->access.get_bpd(st->indio_dev->ring);
127
128         data = kmalloc(datasize , GFP_KERNEL);
129         if (data == NULL) {
130                 dev_err(&st->us->dev, "memory alloc failed in ring bh");
131                 return;
132         }
133
134         if (st->indio_dev->scan_count)
135                 if (adis16260_read_ring_data(&st->indio_dev->dev, st->rx) >= 0)
136                         for (; i < st->indio_dev->scan_count; i++) {
137                                 data[i] = combine_8_to_16(st->rx[i*2+1],
138                                                 st->rx[i*2]);
139                         }
140
141         /* Guaranteed to be aligned with 8 byte boundary */
142         if (st->indio_dev->scan_timestamp)
143                 *((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp;
144
145         st->indio_dev->ring->access.store_to(st->indio_dev->ring,
146                         (u8 *)data,
147                         st->last_timestamp);
148
149         iio_trigger_notify_done(st->indio_dev->trig);
150         kfree(data);
151
152         return;
153 }
154
155 static int adis16260_data_rdy_ring_preenable(struct iio_dev *indio_dev)
156 {
157         size_t size;
158         dev_dbg(&indio_dev->dev, "%s\n", __func__);
159         /* Check if there are any scan elements enabled, if not fail*/
160         if (!(indio_dev->scan_count || indio_dev->scan_timestamp))
161                 return -EINVAL;
162
163         if (indio_dev->ring->access.set_bpd) {
164                 if (indio_dev->scan_timestamp)
165                         if (indio_dev->scan_count)
166                                 /* Timestamp (aligned s64) and data */
167                                 size = (((indio_dev->scan_count * sizeof(s16))
168                                                 + sizeof(s64) - 1)
169                                         & ~(sizeof(s64) - 1))
170                                         + sizeof(s64);
171                         else /* Timestamp only  */
172                                 size = sizeof(s64);
173                 else /* Data only */
174                         size = indio_dev->scan_count*sizeof(s16);
175                 indio_dev->ring->access.set_bpd(indio_dev->ring, size);
176         }
177
178         return 0;
179 }
180
181 static int adis16260_data_rdy_ring_postenable(struct iio_dev *indio_dev)
182 {
183         return indio_dev->trig
184                 ? iio_trigger_attach_poll_func(indio_dev->trig,
185                                 indio_dev->pollfunc)
186                 : 0;
187 }
188
189 static int adis16260_data_rdy_ring_predisable(struct iio_dev *indio_dev)
190 {
191         return indio_dev->trig
192                 ? iio_trigger_dettach_poll_func(indio_dev->trig,
193                                 indio_dev->pollfunc)
194                 : 0;
195 }
196
197 void adis16260_unconfigure_ring(struct iio_dev *indio_dev)
198 {
199         kfree(indio_dev->pollfunc);
200         iio_sw_rb_free(indio_dev->ring);
201 }
202
203 int adis16260_configure_ring(struct iio_dev *indio_dev)
204 {
205         int ret = 0;
206         struct adis16260_state *st = indio_dev->dev_data;
207         struct iio_ring_buffer *ring;
208         INIT_WORK(&st->work_trigger_to_ring, adis16260_trigger_bh_to_ring);
209         /* Set default scan mode */
210
211         iio_scan_mask_set(indio_dev, iio_scan_el_supply.number);
212         iio_scan_mask_set(indio_dev, iio_scan_el_gyro.number);
213         iio_scan_mask_set(indio_dev, iio_scan_el_aux_adc.number);
214         iio_scan_mask_set(indio_dev, iio_scan_el_temp.number);
215         iio_scan_mask_set(indio_dev, iio_scan_el_angl.number);
216         indio_dev->scan_timestamp = true;
217
218         indio_dev->scan_el_attrs = &adis16260_scan_el_group;
219
220         ring = iio_sw_rb_allocate(indio_dev);
221         if (!ring) {
222                 ret = -ENOMEM;
223                 return ret;
224         }
225         indio_dev->ring = ring;
226         /* Effectively select the ring buffer implementation */
227         iio_ring_sw_register_funcs(&ring->access);
228         ring->preenable = &adis16260_data_rdy_ring_preenable;
229         ring->postenable = &adis16260_data_rdy_ring_postenable;
230         ring->predisable = &adis16260_data_rdy_ring_predisable;
231         ring->owner = THIS_MODULE;
232
233         indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
234         if (indio_dev->pollfunc == NULL) {
235                 ret = -ENOMEM;
236                 goto error_iio_sw_rb_free;;
237         }
238         indio_dev->pollfunc->poll_func_main = &adis16260_poll_func_th;
239         indio_dev->pollfunc->private_data = indio_dev;
240         indio_dev->modes |= INDIO_RING_TRIGGERED;
241         return 0;
242
243 error_iio_sw_rb_free:
244         iio_sw_rb_free(indio_dev->ring);
245         return ret;
246 }
247
248 int adis16260_initialize_ring(struct iio_ring_buffer *ring)
249 {
250         return iio_ring_buffer_register(ring, 0);
251 }
252
253 void adis16260_uninitialize_ring(struct iio_ring_buffer *ring)
254 {
255         iio_ring_buffer_unregister(ring);
256 }