Commit | Line | Data |
---|---|---|
0a1231df JC |
1 | /* |
2 | * Copyright (C) 2008 Jonathan Cameron | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * max1363_ring.c | |
9 | */ | |
10 | ||
11 | #include <linux/interrupt.h> | |
5a0e3ad6 | 12 | #include <linux/slab.h> |
0a1231df | 13 | #include <linux/kernel.h> |
0a1231df | 14 | #include <linux/i2c.h> |
82020b0e | 15 | #include <linux/bitops.h> |
0a1231df | 16 | |
06458e27 JC |
17 | #include <linux/iio/iio.h> |
18 | #include <linux/iio/buffer.h> | |
0a1231df | 19 | #include "../ring_sw.h" |
06458e27 | 20 | #include <linux/iio/trigger_consumer.h> |
0a1231df JC |
21 | |
22 | #include "max1363.h" | |
23 | ||
c52cfb63 JC |
24 | int max1363_update_scan_mode(struct iio_dev *indio_dev, |
25 | const unsigned long *scan_mask) | |
0a1231df | 26 | { |
3dba81ba | 27 | struct max1363_state *st = iio_priv(indio_dev); |
82020b0e JC |
28 | |
29 | /* | |
30 | * Need to figure out the current mode based upon the requested | |
31 | * scan mask in iio_dev | |
32 | */ | |
c52cfb63 | 33 | st->current_mode = max1363_match_mode(scan_mask, st->chip_info); |
82020b0e JC |
34 | if (!st->current_mode) |
35 | return -EINVAL; | |
82020b0e | 36 | max1363_set_scan_mode(st); |
0a1231df JC |
37 | return 0; |
38 | } | |
39 | ||
6da288a3 | 40 | static irqreturn_t max1363_trigger_handler(int irq, void *p) |
0a1231df | 41 | { |
6da288a3 | 42 | struct iio_poll_func *pf = p; |
e65bc6ac | 43 | struct iio_dev *indio_dev = pf->indio_dev; |
3dba81ba | 44 | struct max1363_state *st = iio_priv(indio_dev); |
0a1231df JC |
45 | s64 time_ns; |
46 | __u8 *rxbuf; | |
47 | int b_sent; | |
48 | size_t d_size; | |
32b5eeca JC |
49 | unsigned long numvals = bitmap_weight(st->current_mode->modemask, |
50 | MAX1363_MAX_CHANNELS); | |
0a1231df JC |
51 | |
52 | /* Ensure the timestamp is 8 byte aligned */ | |
3bf877c1 | 53 | if (st->chip_info->bits != 8) |
c52cfb63 | 54 | d_size = numvals*2; |
3bf877c1 | 55 | else |
c52cfb63 | 56 | d_size = numvals; |
fd6487f8 | 57 | if (indio_dev->scan_timestamp) { |
c52cfb63 JC |
58 | d_size += sizeof(s64); |
59 | if (d_size % sizeof(s64)) | |
60 | d_size += sizeof(s64) - (d_size % sizeof(s64)); | |
61 | } | |
0a1231df JC |
62 | /* Monitor mode prevents reading. Whilst not currently implemented |
63 | * might as well have this test in here in the meantime as it does | |
64 | * no harm. | |
65 | */ | |
82020b0e | 66 | if (numvals == 0) |
6da288a3 | 67 | return IRQ_HANDLED; |
0a1231df JC |
68 | |
69 | rxbuf = kmalloc(d_size, GFP_KERNEL); | |
70 | if (rxbuf == NULL) | |
6da288a3 | 71 | return -ENOMEM; |
3bf877c1 JC |
72 | if (st->chip_info->bits != 8) |
73 | b_sent = i2c_master_recv(st->client, rxbuf, numvals*2); | |
74 | else | |
75 | b_sent = i2c_master_recv(st->client, rxbuf, numvals); | |
0a1231df JC |
76 | if (b_sent < 0) |
77 | goto done; | |
78 | ||
79 | time_ns = iio_get_time_ns(); | |
c52cfb63 | 80 | |
fd6487f8 | 81 | if (indio_dev->scan_timestamp) |
29bf6263 | 82 | memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns)); |
c52cfb63 JC |
83 | iio_push_to_buffer(indio_dev->buffer, rxbuf, time_ns); |
84 | ||
0a1231df | 85 | done: |
6da288a3 | 86 | iio_trigger_notify_done(indio_dev->trig); |
0a1231df | 87 | kfree(rxbuf); |
6da288a3 JC |
88 | |
89 | return IRQ_HANDLED; | |
0a1231df JC |
90 | } |
91 | ||
14555b14 | 92 | static const struct iio_buffer_setup_ops max1363_ring_setup_ops = { |
3b99fb76 | 93 | .postenable = &iio_triggered_buffer_postenable, |
c52cfb63 | 94 | .preenable = &iio_sw_buffer_preenable, |
3b99fb76 | 95 | .predisable = &iio_triggered_buffer_predisable, |
5565a450 | 96 | }; |
0a1231df JC |
97 | |
98 | int max1363_register_ring_funcs_and_init(struct iio_dev *indio_dev) | |
99 | { | |
3dba81ba | 100 | struct max1363_state *st = iio_priv(indio_dev); |
0a1231df JC |
101 | int ret = 0; |
102 | ||
14555b14 JC |
103 | indio_dev->buffer = iio_sw_rb_allocate(indio_dev); |
104 | if (!indio_dev->buffer) { | |
0a1231df JC |
105 | ret = -ENOMEM; |
106 | goto error_ret; | |
107 | } | |
0ed731d2 JC |
108 | indio_dev->pollfunc = iio_alloc_pollfunc(NULL, |
109 | &max1363_trigger_handler, | |
110 | IRQF_ONESHOT, | |
111 | indio_dev, | |
112 | "%s_consumer%d", | |
113 | st->client->name, | |
114 | indio_dev->id); | |
6da288a3 JC |
115 | if (indio_dev->pollfunc == NULL) { |
116 | ret = -ENOMEM; | |
0a1231df | 117 | goto error_deallocate_sw_rb; |
6da288a3 | 118 | } |
0a1231df | 119 | /* Ring buffer functions - here trigger setup related */ |
1612244f | 120 | indio_dev->setup_ops = &max1363_ring_setup_ops; |
0a1231df JC |
121 | |
122 | /* Flag that polled ring buffering is possible */ | |
ec3afa40 | 123 | indio_dev->modes |= INDIO_BUFFER_TRIGGERED; |
6da288a3 | 124 | |
0a1231df | 125 | return 0; |
0ed731d2 | 126 | |
0a1231df | 127 | error_deallocate_sw_rb: |
14555b14 | 128 | iio_sw_rb_free(indio_dev->buffer); |
0a1231df JC |
129 | error_ret: |
130 | return ret; | |
131 | } | |
132 | ||
133 | void max1363_ring_cleanup(struct iio_dev *indio_dev) | |
134 | { | |
135 | /* ensure that the trigger has been detached */ | |
0ed731d2 | 136 | iio_dealloc_pollfunc(indio_dev->pollfunc); |
14555b14 | 137 | iio_sw_rb_free(indio_dev->buffer); |
0a1231df | 138 | } |