1 #include <linux/interrupt.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>
15 #include "../ring_sw.h"
16 #include "../accel/accel.h"
17 #include "../trigger.h"
21 * combine_8_to_32() utility function to munge to u8s into u32
23 static inline u32 combine_8_to_32(u8 lower, u8 mid, u8 upper)
29 return _lower | (_mid << 8) | (_upper << 16);
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);
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,
47 static struct attribute_group ade7758_scan_el_group = {
48 .attrs = ade7758_scan_el_attrs,
49 .name = "scan_elements",
53 * ade7758_poll_func_th() top half interrupt handler called by trigger
54 * @private_data: iio_dev
56 static void ade7758_poll_func_th(struct iio_dev *indio_dev, s64 time)
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 */
63 /* Technically this is trigger related, but without this
64 * handler running there is currently no way for the interrupt
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)
74 static int ade7758_spi_read_burst(struct device *dev, u8 *rx)
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);
81 struct spi_transfer xfers[] = {
95 mutex_lock(&st->buf_lock);
96 st->tx[0] = ADE7758_READ_REG(ADE7758_RSTATUS);
100 st->tx[4] = ADE7758_READ_REG(ADE7758_WFORM);
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);
110 dev_err(&st->us->dev, "problem when reading WFORM value\n");
112 mutex_unlock(&st->buf_lock);
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.
120 static void ade7758_trigger_bh_to_ring(struct work_struct *work_s)
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;
129 size_t datasize = ring->access.get_bytes_per_datum(ring);
131 data = kmalloc(datasize, GFP_KERNEL);
133 dev_err(&st->us->dev, "memory alloc failed in ring bh");
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],
144 /* Guaranteed to be aligned with 8 byte boundary */
145 if (ring->scan_timestamp)
147 (((u32)data + 4 * ring->scan_count + 4) & ~0x7)) =
150 ring->access.store_to(ring,
154 iio_trigger_notify_done(st->indio_dev->trig);
160 void ade7758_unconfigure_ring(struct iio_dev *indio_dev)
162 kfree(indio_dev->pollfunc);
163 iio_sw_rb_free(indio_dev->ring);
166 int ade7758_configure_ring(struct iio_dev *indio_dev)
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);
173 ring = iio_sw_rb_allocate(indio_dev);
178 indio_dev->ring = ring;
179 /* Effectively select the ring buffer implementation */
180 iio_ring_sw_register_funcs(&ring->access);
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;
189 /* Set default scan mode */
190 iio_scan_mask_set(ring, iio_scan_el_wform.number);
192 ret = iio_alloc_pollfunc(indio_dev, NULL, &ade7758_poll_func_th);
194 goto error_iio_sw_rb_free;
196 indio_dev->modes |= INDIO_RING_TRIGGERED;
199 error_iio_sw_rb_free:
200 iio_sw_rb_free(indio_dev->ring);
204 int ade7758_initialize_ring(struct iio_ring_buffer *ring)
206 return iio_ring_buffer_register(ring, 0);
209 void ade7758_uninitialize_ring(struct iio_ring_buffer *ring)
211 iio_ring_buffer_unregister(ring);