linux/drivers/staging/iio/adc/ad799x_ring.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2010 Michael Hennerich, Analog Devices Inc.
   3 * Copyright (C) 2008-2010 Jonathan Cameron
   4 *
   5 * This program is free software; you can redistribute it and/or modify
   6 * it under the terms of the GNU General Public License version 2 as
   7 * published by the Free Software Foundation.
   8 *
   9 * ad799x_ring.c
  10 */
  11
  12#include <linux/interrupt.h>
  13#include <linux/workqueue.h>
  14#include <linux/device.h>
  15#include <linux/slab.h>
  16#include <linux/kernel.h>
  17#include <linux/sysfs.h>
  18#include <linux/list.h>
  19#include <linux/i2c.h>
  20#include <linux/bitops.h>
  21
  22#include "../iio.h"
  23#include "../ring_generic.h"
  24#include "../ring_sw.h"
  25#include "../trigger.h"
  26#include "../sysfs.h"
  27
  28#include "ad799x.h"
  29
  30int ad799x_single_channel_from_ring(struct ad799x_state *st, long mask)
  31{
  32        struct iio_ring_buffer *ring = st->indio_dev->ring;
  33        int count = 0, ret;
  34        u16 *ring_data;
  35
  36        if (!(ring->scan_mask & mask)) {
  37                ret = -EBUSY;
  38                goto error_ret;
  39        }
  40
  41        ring_data = kmalloc(ring->access.get_bytes_per_datum(ring), GFP_KERNEL);
  42        if (ring_data == NULL) {
  43                ret = -ENOMEM;
  44                goto error_ret;
  45        }
  46        ret = ring->access.read_last(ring, (u8 *) ring_data);
  47        if (ret)
  48                goto error_free_ring_data;
  49        /* Need a count of channels prior to this one */
  50        mask >>= 1;
  51        while (mask) {
  52                if (mask & ring->scan_mask)
  53                        count++;
  54                mask >>= 1;
  55        }
  56
  57        ret = be16_to_cpu(ring_data[count]);
  58
  59error_free_ring_data:
  60        kfree(ring_data);
  61error_ret:
  62        return ret;
  63}
  64
  65/**
  66 * ad799x_ring_preenable() setup the parameters of the ring before enabling
  67 *
  68 * The complex nature of the setting of the nuber of bytes per datum is due
  69 * to this driver currently ensuring that the timestamp is stored at an 8
  70 * byte boundary.
  71 **/
  72static int ad799x_ring_preenable(struct iio_dev *indio_dev)
  73{
  74        struct iio_ring_buffer *ring = indio_dev->ring;
  75        struct ad799x_state *st = indio_dev->dev_data;
  76        size_t d_size;
  77        unsigned long numvals;
  78
  79        /*
  80         * Need to figure out the current mode based upon the requested
  81         * scan mask in iio_dev
  82         */
  83
  84        if (st->id == ad7997 || st->id == ad7998)
  85                ad799x_set_scan_mode(st, ring->scan_mask);
  86
  87        numvals = ring->scan_count;
  88
  89        if (ring->access.set_bytes_per_datum) {
  90                d_size = numvals*2 + sizeof(s64);
  91                if (d_size % 8)
  92                        d_size += 8 - (d_size % 8);
  93                ring->access.set_bytes_per_datum(ring, d_size);
  94        }
  95
  96        return 0;
  97}
  98
  99/**
 100 * ad799x_poll_func_th() th of trigger launched polling to ring buffer
 101 *
 102 * As sampling only occurs on i2c comms occuring, leave timestamping until
 103 * then.  Some triggers will generate their own time stamp.  Currently
 104 * there is no way of notifying them when no one cares.
 105 **/
 106static void ad799x_poll_func_th(struct iio_dev *indio_dev, s64 time)
 107{
 108        struct ad799x_state *st = indio_dev->dev_data;
 109
 110        schedule_work(&st->poll_work);
 111
 112        return;
 113}
 114/**
 115 * ad799x_poll_bh_to_ring() bh of trigger launched polling to ring buffer
 116 * @work_s:     the work struct through which this was scheduled
 117 *
 118 * Currently there is no option in this driver to disable the saving of
 119 * timestamps within the ring.
 120 * I think the one copy of this at a time was to avoid problems if the
 121 * trigger was set far too high and the reads then locked up the computer.
 122 **/
 123static void ad799x_poll_bh_to_ring(struct work_struct *work_s)
 124{
 125        struct ad799x_state *st = container_of(work_s, struct ad799x_state,
 126                                                  poll_work);
 127        struct iio_dev *indio_dev = st->indio_dev;
 128        struct iio_ring_buffer *ring = indio_dev->ring;
 129        struct iio_sw_ring_buffer *ring_sw = iio_to_sw_ring(indio_dev->ring);
 130        s64 time_ns;
 131        __u8 *rxbuf;
 132        int b_sent;
 133        size_t d_size;
 134        u8 cmd;
 135
 136        unsigned long numvals = ring->scan_count;
 137
 138        /* Ensure the timestamp is 8 byte aligned */
 139        d_size = numvals*2 + sizeof(s64);
 140
 141        if (d_size % sizeof(s64))
 142                d_size += sizeof(s64) - (d_size % sizeof(s64));
 143
 144        /* Ensure only one copy of this function running at a time */
 145        if (atomic_inc_return(&st->protect_ring) > 1)
 146                return;
 147
 148        /* Monitor mode prevents reading. Whilst not currently implemented
 149         * might as well have this test in here in the meantime as it does
 150         * no harm.
 151         */
 152        if (numvals == 0)
 153                return;
 154
 155        rxbuf = kmalloc(d_size, GFP_KERNEL);
 156        if (rxbuf == NULL)
 157                return;
 158
 159        switch (st->id) {
 160        case ad7991:
 161        case ad7995:
 162        case ad7999:
 163                cmd = st->config | (ring->scan_mask << AD799X_CHANNEL_SHIFT);
 164                break;
 165        case ad7992:
 166        case ad7993:
 167        case ad7994:
 168                cmd = (ring->scan_mask << AD799X_CHANNEL_SHIFT) |
 169                        AD7998_CONV_RES_REG;
 170                break;
 171        case ad7997:
 172        case ad7998:
 173                cmd = AD7997_8_READ_SEQUENCE | AD7998_CONV_RES_REG;
 174                break;
 175        default:
 176                cmd = 0;
 177        }
 178
 179        b_sent = i2c_smbus_read_i2c_block_data(st->client,
 180                        cmd, numvals*2, rxbuf);
 181        if (b_sent < 0)
 182                goto done;
 183
 184        time_ns = iio_get_time_ns();
 185
 186        memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
 187
 188        ring->access.store_to(&ring_sw->buf, rxbuf, time_ns);
 189done:
 190        kfree(rxbuf);
 191        atomic_dec(&st->protect_ring);
 192}
 193
 194
 195int ad799x_register_ring_funcs_and_init(struct iio_dev *indio_dev)
 196{
 197        struct ad799x_state *st = indio_dev->dev_data;
 198        int ret = 0;
 199
 200        indio_dev->ring = iio_sw_rb_allocate(indio_dev);
 201        if (!indio_dev->ring) {
 202                ret = -ENOMEM;
 203                goto error_ret;
 204        }
 205        /* Effectively select the ring buffer implementation */
 206        iio_ring_sw_register_funcs(&st->indio_dev->ring->access);
 207        ret = iio_alloc_pollfunc(indio_dev, NULL, &ad799x_poll_func_th);
 208        if (ret)
 209                goto error_deallocate_sw_rb;
 210
 211        /* Ring buffer functions - here trigger setup related */
 212
 213        indio_dev->ring->preenable = &ad799x_ring_preenable;
 214        indio_dev->ring->postenable = &iio_triggered_ring_postenable;
 215        indio_dev->ring->predisable = &iio_triggered_ring_predisable;
 216
 217        INIT_WORK(&st->poll_work, &ad799x_poll_bh_to_ring);
 218
 219        indio_dev->ring->scan_el_attrs = st->chip_info->scan_attrs;
 220
 221        /* Flag that polled ring buffering is possible */
 222        indio_dev->modes |= INDIO_RING_TRIGGERED;
 223        return 0;
 224error_deallocate_sw_rb:
 225        iio_sw_rb_free(indio_dev->ring);
 226error_ret:
 227        return ret;
 228}
 229
 230void ad799x_ring_cleanup(struct iio_dev *indio_dev)
 231{
 232        /* ensure that the trigger has been detached */
 233        if (indio_dev->trig) {
 234                iio_put_trigger(indio_dev->trig);
 235                iio_trigger_dettach_poll_func(indio_dev->trig,
 236                                              indio_dev->pollfunc);
 237        }
 238        kfree(indio_dev->pollfunc);
 239        iio_sw_rb_free(indio_dev->ring);
 240}
 241