Commit | Line | Data |
---|---|---|
8210cfe9 BS |
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 "ade7758.h" | |
19 | ||
20 | /** | |
21 | * combine_8_to_32() utility function to munge to u8s into u32 | |
22 | **/ | |
23 | static inline u32 combine_8_to_32(u8 lower, u8 mid, u8 upper) | |
24 | { | |
25 | u32 _lower = lower; | |
26 | u32 _mid = mid; | |
27 | u32 _upper = upper; | |
28 | ||
29 | return _lower | (_mid << 8) | (_upper << 16); | |
30 | } | |
31 | ||
32 | static IIO_SCAN_EL_C(wform, ADE7758_SCAN_WFORM, ADE7758_WFORM, NULL); | |
33 | static IIO_CONST_ATTR_SCAN_EL_TYPE(wform, s, 24, 32); | |
34 | static IIO_SCAN_EL_TIMESTAMP(1); | |
35 | static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp, s, 64, 64); | |
36 | ||
37 | static struct attribute *ade7758_scan_el_attrs[] = { | |
38 | &iio_scan_el_wform.dev_attr.attr, | |
39 | &iio_const_attr_wform_index.dev_attr.attr, | |
40 | &iio_const_attr_wform_type.dev_attr.attr, | |
41 | &iio_scan_el_timestamp.dev_attr.attr, | |
42 | &iio_const_attr_timestamp_index.dev_attr.attr, | |
43 | &iio_const_attr_timestamp_type.dev_attr.attr, | |
44 | NULL, | |
45 | }; | |
46 | ||
47 | static struct attribute_group ade7758_scan_el_group = { | |
48 | .attrs = ade7758_scan_el_attrs, | |
49 | .name = "scan_elements", | |
50 | }; | |
51 | ||
52 | /** | |
53 | * ade7758_poll_func_th() top half interrupt handler called by trigger | |
54 | * @private_data: iio_dev | |
55 | **/ | |
56 | static void ade7758_poll_func_th(struct iio_dev *indio_dev, s64 time) | |
57 | { | |
58 | struct ade7758_state *st = iio_dev_get_devdata(indio_dev); | |
59 | st->last_timestamp = time; | |
60 | schedule_work(&st->work_trigger_to_ring); | |
61 | /* Indicate that this interrupt is being handled */ | |
62 | ||
63 | /* Technically this is trigger related, but without this | |
64 | * handler running there is currently no way for the interrupt | |
65 | * to clear. | |
66 | */ | |
67 | } | |
68 | ||
69 | /** | |
70 | * ade7758_spi_read_burst() - read all data registers | |
71 | * @dev: device associated with child of actual device (iio_dev or iio_trig) | |
72 | * @rx: somewhere to pass back the value read (min size is 24 bytes) | |
73 | **/ | |
74 | static int ade7758_spi_read_burst(struct device *dev, u8 *rx) | |
75 | { | |
76 | struct spi_message msg; | |
77 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
78 | struct ade7758_state *st = iio_dev_get_devdata(indio_dev); | |
79 | int ret; | |
80 | ||
81 | struct spi_transfer xfers[] = { | |
82 | { | |
83 | .tx_buf = st->tx, | |
84 | .rx_buf = rx, | |
85 | .bits_per_word = 8, | |
86 | .len = 4, | |
87 | }, { | |
88 | .tx_buf = st->tx + 4, | |
89 | .rx_buf = rx, | |
90 | .bits_per_word = 8, | |
91 | .len = 4, | |
92 | }, | |
93 | }; | |
94 | ||
95 | mutex_lock(&st->buf_lock); | |
96 | st->tx[0] = ADE7758_READ_REG(ADE7758_RSTATUS); | |
97 | st->tx[1] = 0; | |
98 | st->tx[2] = 0; | |
99 | st->tx[3] = 0; | |
100 | st->tx[4] = ADE7758_READ_REG(ADE7758_WFORM); | |
101 | st->tx[5] = 0; | |
102 | st->tx[6] = 0; | |
103 | st->tx[7] = 0; | |
104 | ||
105 | spi_message_init(&msg); | |
106 | spi_message_add_tail(&xfers[0], &msg); | |
107 | spi_message_add_tail(&xfers[1], &msg); | |
108 | ret = spi_sync(st->us, &msg); | |
109 | if (ret) | |
110 | dev_err(&st->us->dev, "problem when reading WFORM value\n"); | |
111 | ||
112 | mutex_unlock(&st->buf_lock); | |
113 | ||
114 | return ret; | |
115 | } | |
116 | ||
117 | /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device | |
118 | * specific to be rolled into the core. | |
119 | */ | |
120 | static void ade7758_trigger_bh_to_ring(struct work_struct *work_s) | |
121 | { | |
122 | struct ade7758_state *st | |
123 | = container_of(work_s, struct ade7758_state, | |
124 | work_trigger_to_ring); | |
125 | struct iio_ring_buffer *ring = st->indio_dev->ring; | |
126 | ||
127 | int i = 0; | |
128 | s32 *data; | |
129 | size_t datasize = ring->access.get_bytes_per_datum(ring); | |
130 | ||
131 | data = kmalloc(datasize, GFP_KERNEL); | |
132 | if (data == NULL) { | |
133 | dev_err(&st->us->dev, "memory alloc failed in ring bh"); | |
134 | return; | |
135 | } | |
136 | ||
137 | if (ring->scan_count) | |
138 | if (ade7758_spi_read_burst(&st->indio_dev->dev, st->rx) >= 0) | |
139 | for (; i < ring->scan_count; i++) | |
140 | data[i] = combine_8_to_32(st->rx[i*2+2], | |
141 | st->rx[i*2+1], | |
142 | st->rx[i*2]); | |
143 | ||
144 | /* Guaranteed to be aligned with 8 byte boundary */ | |
145 | if (ring->scan_timestamp) | |
146 | *((s64 *) | |
147 | (((u32)data + 4 * ring->scan_count + 4) & ~0x7)) = | |
148 | st->last_timestamp; | |
149 | ||
150 | ring->access.store_to(ring, | |
151 | (u8 *)data, | |
152 | st->last_timestamp); | |
153 | ||
154 | iio_trigger_notify_done(st->indio_dev->trig); | |
155 | kfree(data); | |
156 | ||
157 | return; | |
158 | } | |
159 | ||
160 | void ade7758_unconfigure_ring(struct iio_dev *indio_dev) | |
161 | { | |
162 | kfree(indio_dev->pollfunc); | |
163 | iio_sw_rb_free(indio_dev->ring); | |
164 | } | |
165 | ||
166 | int ade7758_configure_ring(struct iio_dev *indio_dev) | |
167 | { | |
168 | int ret = 0; | |
169 | struct ade7758_state *st = indio_dev->dev_data; | |
170 | struct iio_ring_buffer *ring; | |
171 | INIT_WORK(&st->work_trigger_to_ring, ade7758_trigger_bh_to_ring); | |
172 | ||
173 | ring = iio_sw_rb_allocate(indio_dev); | |
174 | if (!ring) { | |
175 | ret = -ENOMEM; | |
176 | return ret; | |
177 | } | |
178 | indio_dev->ring = ring; | |
179 | /* Effectively select the ring buffer implementation */ | |
180 | iio_ring_sw_register_funcs(&ring->access); | |
181 | ring->bpe = 4; | |
182 | ring->scan_el_attrs = &ade7758_scan_el_group; | |
183 | ring->scan_timestamp = true; | |
184 | ring->preenable = &iio_sw_ring_preenable; | |
185 | ring->postenable = &iio_triggered_ring_postenable; | |
186 | ring->predisable = &iio_triggered_ring_predisable; | |
187 | ring->owner = THIS_MODULE; | |
188 | ||
189 | /* Set default scan mode */ | |
190 | iio_scan_mask_set(ring, iio_scan_el_wform.number); | |
191 | ||
192 | ret = iio_alloc_pollfunc(indio_dev, NULL, &ade7758_poll_func_th); | |
193 | if (ret) | |
194 | goto error_iio_sw_rb_free; | |
195 | ||
196 | indio_dev->modes |= INDIO_RING_TRIGGERED; | |
197 | return 0; | |
198 | ||
199 | error_iio_sw_rb_free: | |
200 | iio_sw_rb_free(indio_dev->ring); | |
201 | return ret; | |
202 | } | |
203 | ||
204 | int ade7758_initialize_ring(struct iio_ring_buffer *ring) | |
205 | { | |
206 | return iio_ring_buffer_register(ring, 0); | |
207 | } | |
208 | ||
209 | void ade7758_uninitialize_ring(struct iio_ring_buffer *ring) | |
210 | { | |
211 | iio_ring_buffer_unregister(ring); | |
212 | } |