diff options
Diffstat (limited to 'drivers/staging/iio/meter/ade7758_ring.c')
-rw-r--r-- | drivers/staging/iio/meter/ade7758_ring.c | 281 |
1 files changed, 145 insertions, 136 deletions
diff --git a/drivers/staging/iio/meter/ade7758_ring.c b/drivers/staging/iio/meter/ade7758_ring.c index 274b4a07808c..b89b7f882e84 100644 --- a/drivers/staging/iio/meter/ade7758_ring.c +++ b/drivers/staging/iio/meter/ade7758_ring.c @@ -1,3 +1,10 @@ +/* + * ADE7758 Poly Phase Multifunction Energy Metering IC driver + * + * Copyright 2010-2011 Analog Devices Inc. + * + * Licensed under the GPL-2. + */ #include <linux/interrupt.h> #include <linux/irq.h> #include <linux/gpio.h> @@ -9,6 +16,7 @@ #include <linux/slab.h> #include <linux/sysfs.h> #include <linux/list.h> +#include <asm/unaligned.h> #include "../iio.h" #include "../sysfs.h" @@ -18,182 +26,188 @@ #include "ade7758.h" /** - * combine_8_to_32() utility function to munge to u8s into u32 + * ade7758_spi_read_burst() - read data registers + * @dev: device associated with child of actual device (iio_dev or iio_trig) **/ -static inline u32 combine_8_to_32(u8 lower, u8 mid, u8 upper) +static int ade7758_spi_read_burst(struct device *dev) { - u32 _lower = lower; - u32 _mid = mid; - u32 _upper = upper; - - return _lower | (_mid << 8) | (_upper << 16); -} - -static IIO_SCAN_EL_C(wform, ADE7758_SCAN_WFORM, ADE7758_WFORM, NULL); -static IIO_CONST_ATTR_SCAN_EL_TYPE(wform, s, 24, 32); -static IIO_SCAN_EL_TIMESTAMP(1); -static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp, s, 64, 64); - -static struct attribute *ade7758_scan_el_attrs[] = { - &iio_scan_el_wform.dev_attr.attr, - &iio_const_attr_wform_index.dev_attr.attr, - &iio_const_attr_wform_type.dev_attr.attr, - &iio_scan_el_timestamp.dev_attr.attr, - &iio_const_attr_timestamp_index.dev_attr.attr, - &iio_const_attr_timestamp_type.dev_attr.attr, - NULL, -}; + struct iio_dev *indio_dev = dev_get_drvdata(dev); + struct ade7758_state *st = iio_priv(indio_dev); + int ret; -static struct attribute_group ade7758_scan_el_group = { - .attrs = ade7758_scan_el_attrs, - .name = "scan_elements", -}; + ret = spi_sync(st->us, &st->ring_msg); + if (ret) + dev_err(&st->us->dev, "problem when reading WFORM value\n"); -/** - * ade7758_poll_func_th() top half interrupt handler called by trigger - * @private_data: iio_dev - **/ -static void ade7758_poll_func_th(struct iio_dev *indio_dev, s64 time) -{ - struct ade7758_state *st = iio_dev_get_devdata(indio_dev); - st->last_timestamp = time; - schedule_work(&st->work_trigger_to_ring); - /* Indicate that this interrupt is being handled */ - - /* Technically this is trigger related, but without this - * handler running there is currently no way for the interrupt - * to clear. - */ + return ret; } -/** - * ade7758_spi_read_burst() - read all data registers - * @dev: device associated with child of actual device (iio_dev or iio_trig) - * @rx: somewhere to pass back the value read (min size is 24 bytes) - **/ -static int ade7758_spi_read_burst(struct device *dev, u8 *rx) +static int ade7758_write_waveform_type(struct device *dev, unsigned type) { - struct spi_message msg; - struct iio_dev *indio_dev = dev_get_drvdata(dev); - struct ade7758_state *st = iio_dev_get_devdata(indio_dev); int ret; + u8 reg; - struct spi_transfer xfers[] = { - { - .tx_buf = st->tx, - .rx_buf = rx, - .bits_per_word = 8, - .len = 4, - }, { - .tx_buf = st->tx + 4, - .rx_buf = rx, - .bits_per_word = 8, - .len = 4, - }, - }; - - mutex_lock(&st->buf_lock); - st->tx[0] = ADE7758_READ_REG(ADE7758_RSTATUS); - st->tx[1] = 0; - st->tx[2] = 0; - st->tx[3] = 0; - st->tx[4] = ADE7758_READ_REG(ADE7758_WFORM); - st->tx[5] = 0; - st->tx[6] = 0; - st->tx[7] = 0; - - spi_message_init(&msg); - spi_message_add_tail(&xfers[0], &msg); - spi_message_add_tail(&xfers[1], &msg); - ret = spi_sync(st->us, &msg); + ret = ade7758_spi_read_reg_8(dev, + ADE7758_WAVMODE, + ®); if (ret) - dev_err(&st->us->dev, "problem when reading WFORM value\n"); + goto out; - mutex_unlock(&st->buf_lock); + reg &= ~0x1F; + reg |= type & 0x1F; + ret = ade7758_spi_write_reg_8(dev, + ADE7758_WAVMODE, + reg); +out: return ret; } /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device * specific to be rolled into the core. */ -static void ade7758_trigger_bh_to_ring(struct work_struct *work_s) +static irqreturn_t ade7758_trigger_handler(int irq, void *p) { - struct ade7758_state *st - = container_of(work_s, struct ade7758_state, - work_trigger_to_ring); - struct iio_ring_buffer *ring = st->indio_dev->ring; - - int i = 0; - s32 *data; - size_t datasize = ring->access.get_bytes_per_datum(ring); - - data = kmalloc(datasize, GFP_KERNEL); - if (data == NULL) { - dev_err(&st->us->dev, "memory alloc failed in ring bh"); - return; - } + struct iio_poll_func *pf = p; + struct iio_dev *indio_dev = pf->private_data; + struct iio_ring_buffer *ring = indio_dev->ring; + struct ade7758_state *st = iio_priv(indio_dev); + s64 dat64[2]; + u32 *dat32 = (u32 *)dat64; if (ring->scan_count) - if (ade7758_spi_read_burst(&st->indio_dev->dev, st->rx) >= 0) - for (; i < ring->scan_count; i++) - data[i] = combine_8_to_32(st->rx[i*2+2], - st->rx[i*2+1], - st->rx[i*2]); + if (ade7758_spi_read_burst(&indio_dev->dev) >= 0) + *dat32 = get_unaligned_be32(&st->rx_buf[5]) & 0xFFFFFF; /* Guaranteed to be aligned with 8 byte boundary */ if (ring->scan_timestamp) - *((s64 *) - (((u32)data + 4 * ring->scan_count + 4) & ~0x7)) = - st->last_timestamp; + dat64[1] = pf->timestamp; + + ring->access->store_to(ring, (u8 *)dat64, pf->timestamp); + + iio_trigger_notify_done(indio_dev->trig); + + return IRQ_HANDLED; +} + +/** + * ade7758_ring_preenable() setup the parameters of the ring before enabling + * + * The complex nature of the setting of the nuber of bytes per datum is due + * to this driver currently ensuring that the timestamp is stored at an 8 + * byte boundary. + **/ +static int ade7758_ring_preenable(struct iio_dev *indio_dev) +{ + struct ade7758_state *st = iio_priv(indio_dev); + struct iio_ring_buffer *ring = indio_dev->ring; + size_t d_size; + unsigned channel; + + if (!ring->scan_count) + return -EINVAL; - ring->access.store_to(ring, - (u8 *)data, - st->last_timestamp); + channel = __ffs(ring->scan_mask); - iio_trigger_notify_done(st->indio_dev->trig); - kfree(data); + d_size = st->ade7758_ring_channels[channel].scan_type.storagebits / 8; - return; + if (ring->scan_timestamp) { + d_size += sizeof(s64); + + if (d_size % sizeof(s64)) + d_size += sizeof(s64) - (d_size % sizeof(s64)); + } + + if (indio_dev->ring->access->set_bytes_per_datum) + indio_dev->ring->access->set_bytes_per_datum(indio_dev->ring, + d_size); + + ade7758_write_waveform_type(&indio_dev->dev, + st->ade7758_ring_channels[channel].address); + + return 0; } +static const struct iio_ring_setup_ops ade7758_ring_setup_ops = { + .preenable = &ade7758_ring_preenable, + .postenable = &iio_triggered_ring_postenable, + .predisable = &iio_triggered_ring_predisable, +}; + void ade7758_unconfigure_ring(struct iio_dev *indio_dev) { - kfree(indio_dev->pollfunc); + /* ensure that the trigger has been detached */ + if (indio_dev->trig) { + iio_put_trigger(indio_dev->trig); + iio_trigger_dettach_poll_func(indio_dev->trig, + indio_dev->pollfunc); + } + iio_dealloc_pollfunc(indio_dev->pollfunc); iio_sw_rb_free(indio_dev->ring); } int ade7758_configure_ring(struct iio_dev *indio_dev) { + struct ade7758_state *st = iio_priv(indio_dev); int ret = 0; - struct ade7758_state *st = indio_dev->dev_data; - struct iio_ring_buffer *ring; - INIT_WORK(&st->work_trigger_to_ring, ade7758_trigger_bh_to_ring); - ring = iio_sw_rb_allocate(indio_dev); - if (!ring) { + indio_dev->ring = iio_sw_rb_allocate(indio_dev); + if (!indio_dev->ring) { ret = -ENOMEM; return ret; } - indio_dev->ring = ring; + /* Effectively select the ring buffer implementation */ - iio_ring_sw_register_funcs(&ring->access); - ring->bpe = 4; - ring->scan_el_attrs = &ade7758_scan_el_group; - ring->scan_timestamp = true; - ring->preenable = &iio_sw_ring_preenable; - ring->postenable = &iio_triggered_ring_postenable; - ring->predisable = &iio_triggered_ring_predisable; - ring->owner = THIS_MODULE; - - /* Set default scan mode */ - iio_scan_mask_set(ring, iio_scan_el_wform.number); - - ret = iio_alloc_pollfunc(indio_dev, NULL, &ade7758_poll_func_th); - if (ret) + indio_dev->ring->access = &ring_sw_access_funcs; + indio_dev->ring->setup_ops = &ade7758_ring_setup_ops; + indio_dev->ring->owner = THIS_MODULE; + + indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time, + &ade7758_trigger_handler, + 0, + indio_dev, + "ade7759_consumer%d", + indio_dev->id); + if (indio_dev->pollfunc == NULL) { + ret = -ENOMEM; goto error_iio_sw_rb_free; + } indio_dev->modes |= INDIO_RING_TRIGGERED; + + st->tx_buf[0] = ADE7758_READ_REG(ADE7758_RSTATUS); + st->tx_buf[1] = 0; + st->tx_buf[2] = 0; + st->tx_buf[3] = 0; + st->tx_buf[4] = ADE7758_READ_REG(ADE7758_WFORM); + st->tx_buf[5] = 0; + st->tx_buf[6] = 0; + st->tx_buf[7] = 0; + + /* build spi ring message */ + st->ring_xfer[0].tx_buf = &st->tx_buf[0]; + st->ring_xfer[0].len = 1; + st->ring_xfer[0].bits_per_word = 8; + st->ring_xfer[0].delay_usecs = 4; + st->ring_xfer[1].rx_buf = &st->rx_buf[1]; + st->ring_xfer[1].len = 3; + st->ring_xfer[1].bits_per_word = 8; + st->ring_xfer[1].cs_change = 1; + + st->ring_xfer[2].tx_buf = &st->tx_buf[4]; + st->ring_xfer[2].len = 1; + st->ring_xfer[2].bits_per_word = 8; + st->ring_xfer[2].delay_usecs = 1; + st->ring_xfer[3].rx_buf = &st->rx_buf[5]; + st->ring_xfer[3].len = 3; + st->ring_xfer[3].bits_per_word = 8; + + spi_message_init(&st->ring_msg); + spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg); + spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg); + spi_message_add_tail(&st->ring_xfer[2], &st->ring_msg); + spi_message_add_tail(&st->ring_xfer[3], &st->ring_msg); + return 0; error_iio_sw_rb_free: @@ -201,11 +215,6 @@ error_iio_sw_rb_free: return ret; } -int ade7758_initialize_ring(struct iio_ring_buffer *ring) -{ - return iio_ring_buffer_register(ring, 0); -} - void ade7758_uninitialize_ring(struct iio_ring_buffer *ring) { iio_ring_buffer_unregister(ring); |