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#include <linux/slab.h>
12
13#include "../iio.h"
14#include "../sysfs.h"
15#include "../ring_sw.h"
16#include "accel.h"
17#include "../trigger.h"
18#include "lis3l02dq.h"
19
20
21
22
23static inline u16 combine_8_to_16(u8 lower, u8 upper)
24{
25 u16 _lower = lower;
26 u16 _upper = upper;
27 return _lower | (_upper << 8);
28}
29
30
31
32
33
34
35
36
37
38static int lis3l02dq_scan_el_set_state(struct iio_scan_el *scan_el,
39 struct iio_dev *indio_dev,
40 bool state)
41{
42 u8 t, mask;
43 int ret;
44
45 ret = lis3l02dq_spi_read_reg_8(&indio_dev->dev,
46 LIS3L02DQ_REG_CTRL_1_ADDR,
47 &t);
48 if (ret)
49 goto error_ret;
50 switch (scan_el->label) {
51 case LIS3L02DQ_REG_OUT_X_L_ADDR:
52 mask = LIS3L02DQ_REG_CTRL_1_AXES_X_ENABLE;
53 break;
54 case LIS3L02DQ_REG_OUT_Y_L_ADDR:
55 mask = LIS3L02DQ_REG_CTRL_1_AXES_Y_ENABLE;
56 break;
57 case LIS3L02DQ_REG_OUT_Z_L_ADDR:
58 mask = LIS3L02DQ_REG_CTRL_1_AXES_Z_ENABLE;
59 break;
60 default:
61 ret = -EINVAL;
62 goto error_ret;
63 }
64
65 if (!(mask & t) == state) {
66 if (state)
67 t |= mask;
68 else
69 t &= ~mask;
70 ret = lis3l02dq_spi_write_reg_8(&indio_dev->dev,
71 LIS3L02DQ_REG_CTRL_1_ADDR,
72 &t);
73 }
74error_ret:
75 return ret;
76
77}
78static IIO_SCAN_EL_C(accel_x, 0,
79 LIS3L02DQ_REG_OUT_X_L_ADDR,
80 &lis3l02dq_scan_el_set_state);
81static IIO_SCAN_EL_C(accel_y, 1,
82 LIS3L02DQ_REG_OUT_Y_L_ADDR,
83 &lis3l02dq_scan_el_set_state);
84static IIO_SCAN_EL_C(accel_z, 2,
85 LIS3L02DQ_REG_OUT_Z_L_ADDR,
86 &lis3l02dq_scan_el_set_state);
87static IIO_CONST_ATTR_SCAN_EL_TYPE(accel, s, 12, 16);
88static IIO_SCAN_EL_TIMESTAMP(3);
89static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp, s, 64, 64);
90
91static struct attribute *lis3l02dq_scan_el_attrs[] = {
92 &iio_scan_el_accel_x.dev_attr.attr,
93 &iio_const_attr_accel_x_index.dev_attr.attr,
94 &iio_scan_el_accel_y.dev_attr.attr,
95 &iio_const_attr_accel_y_index.dev_attr.attr,
96 &iio_scan_el_accel_z.dev_attr.attr,
97 &iio_const_attr_accel_z_index.dev_attr.attr,
98 &iio_const_attr_accel_type.dev_attr.attr,
99 &iio_scan_el_timestamp.dev_attr.attr,
100 &iio_const_attr_timestamp_index.dev_attr.attr,
101 &iio_const_attr_timestamp_type.dev_attr.attr,
102 NULL,
103};
104
105static struct attribute_group lis3l02dq_scan_el_group = {
106 .attrs = lis3l02dq_scan_el_attrs,
107 .name = "scan_elements",
108};
109
110
111
112
113
114static void lis3l02dq_poll_func_th(struct iio_dev *indio_dev, s64 time)
115{
116 struct iio_sw_ring_helper_state *h
117 = iio_dev_get_devdata(indio_dev);
118 struct lis3l02dq_state *st = lis3l02dq_h_to_s(h);
119
120 iio_sw_poll_func_th(indio_dev, time);
121
122
123
124
125
126
127 st->inter = 1;
128}
129
130
131
132
133static int lis3l02dq_data_rdy_trig_poll(struct iio_dev *indio_dev,
134 int index,
135 s64 timestamp,
136 int no_test)
137{
138 struct iio_sw_ring_helper_state *h
139 = iio_dev_get_devdata(indio_dev);
140 struct lis3l02dq_state *st = lis3l02dq_h_to_s(h);
141
142 iio_trigger_poll(st->trig, timestamp);
143
144 return IRQ_HANDLED;
145}
146
147
148IIO_EVENT_SH(data_rdy_trig, &lis3l02dq_data_rdy_trig_poll);
149
150
151
152
153ssize_t lis3l02dq_read_accel_from_ring(struct device *dev,
154 struct device_attribute *attr,
155 char *buf)
156{
157 struct iio_scan_el *el = NULL;
158 int ret, len = 0, i = 0;
159 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
160 struct iio_dev *dev_info = dev_get_drvdata(dev);
161 struct iio_ring_buffer *ring = dev_info->ring;
162 struct attribute_group *scan_el_attrs = ring->scan_el_attrs;
163 s16 *data;
164
165 while (scan_el_attrs->attrs[i]) {
166 el = to_iio_scan_el((struct device_attribute *)
167 (scan_el_attrs->attrs[i]));
168
169 if (el->label == this_attr->address)
170 break;
171 i++;
172 }
173 if (!scan_el_attrs->attrs[i]) {
174 ret = -EINVAL;
175 goto error_ret;
176 }
177
178 ret = iio_scan_mask_query(ring, el->number);
179 if (ret < 0)
180 goto error_ret;
181 if (ret) {
182 data = kmalloc(ring->access.get_bytes_per_datum(ring),
183 GFP_KERNEL);
184 if (data == NULL)
185 return -ENOMEM;
186 ret = ring->access.read_last(ring,
187 (u8 *)data);
188 if (ret)
189 goto error_free_data;
190 } else {
191 ret = -EINVAL;
192 goto error_ret;
193 }
194 len = iio_scan_mask_count_to_right(ring, el->number);
195 if (len < 0) {
196 ret = len;
197 goto error_free_data;
198 }
199 len = sprintf(buf, "ring %d\n", data[len]);
200error_free_data:
201 kfree(data);
202error_ret:
203 return ret ? ret : len;
204
205}
206
207static const u8 read_all_tx_array[] = {
208 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_X_L_ADDR), 0,
209 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_X_H_ADDR), 0,
210 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_Y_L_ADDR), 0,
211 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_Y_H_ADDR), 0,
212 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_Z_L_ADDR), 0,
213 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_Z_H_ADDR), 0,
214};
215
216
217
218
219
220
221
222static int lis3l02dq_read_all(struct lis3l02dq_state *st, u8 *rx_array)
223{
224 struct iio_ring_buffer *ring = st->help.indio_dev->ring;
225 struct spi_transfer *xfers;
226 struct spi_message msg;
227 int ret, i, j = 0;
228
229 xfers = kzalloc((ring->scan_count) * 2
230 * sizeof(*xfers), GFP_KERNEL);
231 if (!xfers)
232 return -ENOMEM;
233
234 mutex_lock(&st->buf_lock);
235
236 for (i = 0; i < ARRAY_SIZE(read_all_tx_array)/4; i++) {
237 if (ring->scan_mask & (1 << i)) {
238
239 xfers[j].tx_buf = st->tx + 2*j;
240 st->tx[2*j] = read_all_tx_array[i*4];
241 st->tx[2*j + 1] = 0;
242 if (rx_array)
243 xfers[j].rx_buf = rx_array + j*2;
244 xfers[j].bits_per_word = 8;
245 xfers[j].len = 2;
246 xfers[j].cs_change = 1;
247 j++;
248
249
250 xfers[j].tx_buf = st->tx + 2*j;
251 st->tx[2*j] = read_all_tx_array[i*4 + 2];
252 st->tx[2*j + 1] = 0;
253 if (rx_array)
254 xfers[j].rx_buf = rx_array + j*2;
255 xfers[j].bits_per_word = 8;
256 xfers[j].len = 2;
257 xfers[j].cs_change = 1;
258 j++;
259 }
260 }
261
262
263
264 spi_message_init(&msg);
265 for (j = 0; j < ring->scan_count * 2; j++)
266 spi_message_add_tail(&xfers[j], &msg);
267
268 ret = spi_sync(st->us, &msg);
269 mutex_unlock(&st->buf_lock);
270 kfree(xfers);
271
272 return ret;
273}
274
275static void lis3l02dq_trigger_bh_to_ring(struct work_struct *work_s)
276{
277 struct iio_sw_ring_helper_state *h
278 = container_of(work_s, struct iio_sw_ring_helper_state,
279 work_trigger_to_ring);
280 struct lis3l02dq_state *st = lis3l02dq_h_to_s(h);
281
282 st->inter = 0;
283 iio_sw_trigger_bh_to_ring(work_s);
284}
285
286static int lis3l02dq_get_ring_element(struct iio_sw_ring_helper_state *h,
287 u8 *buf)
288{
289 int ret, i;
290 u8 *rx_array ;
291 s16 *data = (s16 *)buf;
292
293 rx_array = kzalloc(4 * (h->indio_dev->ring->scan_count), GFP_KERNEL);
294 if (rx_array == NULL)
295 return -ENOMEM;
296 ret = lis3l02dq_read_all(lis3l02dq_h_to_s(h), rx_array);
297 if (ret < 0)
298 return ret;
299 for (i = 0; i < h->indio_dev->ring->scan_count; i++)
300 data[i] = combine_8_to_16(rx_array[i*4+1],
301 rx_array[i*4+3]);
302 kfree(rx_array);
303
304 return i*sizeof(data[0]);
305}
306
307
308static int
309__lis3l02dq_write_data_ready_config(struct device *dev,
310 struct iio_event_handler_list *list,
311 bool state)
312{
313 int ret;
314 u8 valold;
315 bool currentlyset;
316 struct iio_dev *indio_dev = dev_get_drvdata(dev);
317
318
319 ret = lis3l02dq_spi_read_reg_8(dev,
320 LIS3L02DQ_REG_CTRL_2_ADDR,
321 &valold);
322 if (ret)
323 goto error_ret;
324
325 currentlyset
326 = valold & LIS3L02DQ_REG_CTRL_2_ENABLE_DATA_READY_GENERATION;
327
328
329 if (!state && currentlyset) {
330
331 valold &= ~LIS3L02DQ_REG_CTRL_2_ENABLE_DATA_READY_GENERATION;
332
333 ret = lis3l02dq_spi_write_reg_8(dev,
334 LIS3L02DQ_REG_CTRL_2_ADDR,
335 &valold);
336 if (ret)
337 goto error_ret;
338 ret = lis3l02dq_spi_write_reg_8(dev,
339 LIS3L02DQ_REG_CTRL_2_ADDR,
340 &valold);
341 if (ret)
342 goto error_ret;
343
344 iio_remove_event_from_list(list,
345 &indio_dev->interrupts[0]
346 ->ev_list);
347
348
349 } else if (state && !currentlyset) {
350
351 valold |= LIS3L02DQ_REG_CTRL_2_ENABLE_DATA_READY_GENERATION;
352 iio_add_event_to_list(list, &indio_dev->interrupts[0]->ev_list);
353 ret = lis3l02dq_spi_write_reg_8(dev,
354 LIS3L02DQ_REG_CTRL_2_ADDR,
355 &valold);
356 if (ret)
357 goto error_ret;
358 }
359
360 return 0;
361error_ret:
362 return ret;
363}
364
365
366
367
368
369
370
371
372static int lis3l02dq_data_rdy_trigger_set_state(struct iio_trigger *trig,
373 bool state)
374{
375 struct lis3l02dq_state *st = trig->private_data;
376 int ret = 0;
377 u8 t;
378 __lis3l02dq_write_data_ready_config(&st->help.indio_dev->dev,
379 &iio_event_data_rdy_trig,
380 state);
381 if (state == false) {
382
383
384 flush_scheduled_work();
385
386 ret = lis3l02dq_read_all(st, NULL);
387 }
388 lis3l02dq_spi_read_reg_8(&st->help.indio_dev->dev,
389 LIS3L02DQ_REG_WAKE_UP_SRC_ADDR,
390 &t);
391 return ret;
392}
393
394static IIO_TRIGGER_NAME_ATTR;
395
396static struct attribute *lis3l02dq_trigger_attrs[] = {
397 &dev_attr_name.attr,
398 NULL,
399};
400
401static const struct attribute_group lis3l02dq_trigger_attr_group = {
402 .attrs = lis3l02dq_trigger_attrs,
403};
404
405
406
407
408
409
410
411
412
413
414
415
416
417static int lis3l02dq_trig_try_reen(struct iio_trigger *trig)
418{
419 struct lis3l02dq_state *st = trig->private_data;
420 enable_irq(st->us->irq);
421
422 if (gpio_get_value(irq_to_gpio(st->us->irq)))
423 if (st->inter == 0) {
424
425 disable_irq_nosync(st->us->irq);
426 if (st->inter == 1) {
427
428
429 enable_irq(st->us->irq);
430 return 0;
431 }
432 return -EAGAIN;
433 }
434
435 return 0;
436}
437
438int lis3l02dq_probe_trigger(struct iio_dev *indio_dev)
439{
440 int ret;
441 struct lis3l02dq_state *state = indio_dev->dev_data;
442
443 state->trig = iio_allocate_trigger();
444 if (!state->trig)
445 return -ENOMEM;
446
447 state->trig->name = kasprintf(GFP_KERNEL,
448 "lis3l02dq-dev%d",
449 indio_dev->id);
450 if (!state->trig->name) {
451 ret = -ENOMEM;
452 goto error_free_trig;
453 }
454
455 state->trig->dev.parent = &state->us->dev;
456 state->trig->owner = THIS_MODULE;
457 state->trig->private_data = state;
458 state->trig->set_trigger_state = &lis3l02dq_data_rdy_trigger_set_state;
459 state->trig->try_reenable = &lis3l02dq_trig_try_reen;
460 state->trig->control_attrs = &lis3l02dq_trigger_attr_group;
461 ret = iio_trigger_register(state->trig);
462 if (ret)
463 goto error_free_trig_name;
464
465 return 0;
466
467error_free_trig_name:
468 kfree(state->trig->name);
469error_free_trig:
470 iio_free_trigger(state->trig);
471
472 return ret;
473}
474
475void lis3l02dq_remove_trigger(struct iio_dev *indio_dev)
476{
477 struct lis3l02dq_state *state = indio_dev->dev_data;
478
479 iio_trigger_unregister(state->trig);
480 kfree(state->trig->name);
481 iio_free_trigger(state->trig);
482}
483
484void lis3l02dq_unconfigure_ring(struct iio_dev *indio_dev)
485{
486 kfree(indio_dev->pollfunc);
487 iio_sw_rb_free(indio_dev->ring);
488}
489
490int lis3l02dq_configure_ring(struct iio_dev *indio_dev)
491{
492 int ret;
493 struct iio_sw_ring_helper_state *h = iio_dev_get_devdata(indio_dev);
494 struct iio_ring_buffer *ring;
495 INIT_WORK(&h->work_trigger_to_ring, lis3l02dq_trigger_bh_to_ring);
496 h->get_ring_element = &lis3l02dq_get_ring_element;
497
498 ring = iio_sw_rb_allocate(indio_dev);
499 if (!ring)
500 return -ENOMEM;
501
502 indio_dev->ring = ring;
503
504 iio_ring_sw_register_funcs(&ring->access);
505 ring->bpe = 2;
506 ring->scan_el_attrs = &lis3l02dq_scan_el_group;
507 ring->scan_timestamp = true;
508 ring->preenable = &iio_sw_ring_preenable;
509 ring->postenable = &iio_triggered_ring_postenable;
510 ring->predisable = &iio_triggered_ring_predisable;
511 ring->owner = THIS_MODULE;
512
513
514 iio_scan_mask_set(ring, iio_scan_el_accel_x.number);
515 iio_scan_mask_set(ring, iio_scan_el_accel_y.number);
516 iio_scan_mask_set(ring, iio_scan_el_accel_z.number);
517
518 ret = iio_alloc_pollfunc(indio_dev, NULL, &lis3l02dq_poll_func_th);
519 if (ret)
520 goto error_iio_sw_rb_free;
521 indio_dev->modes |= INDIO_RING_TRIGGERED;
522 return 0;
523
524error_iio_sw_rb_free:
525 iio_sw_rb_free(indio_dev->ring);
526 return ret;
527}
528